1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * This version of insmod supports x86, ARM, SH3/4, powerpc, m68k,
8 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
9 * Copyright (C) 1999,2000,2001,2002 by Erik Andersen <andersee@debian.org>
10 * and Ron Alder <alder@lineo.com>
12 * Miles Bader <miles@gnu.org> added NEC V850E support.
14 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
15 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
17 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
18 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
19 * very minor changes required to also work with StrongArm and presumably
20 * all ARM based systems.
22 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
23 * PowerPC specific code stolen from modutils-2.3.16,
24 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
25 * I've only tested the code on mpc8xx platforms in big-endian mode.
26 * Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
28 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
29 * based on modutils-2.4.2
30 * MIPS specific support for Elf loading and relocation.
31 * Copyright 1996, 1997 Linux International.
32 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
34 * Based almost entirely on the Linux modutils-2.3.11 implementation.
35 * Copyright 1996, 1997 Linux International.
36 * New implementation contributed by Richard Henderson <rth@tamu.edu>
37 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
38 * Restructured (and partly rewritten) by:
39 * Björn Ekwall <bj0rn@blox.se> February 1999
41 * This program is free software; you can redistribute it and/or modify
42 * it under the terms of the GNU General Public License as published by
43 * the Free Software Foundation; either version 2 of the License, or
44 * (at your option) any later version.
46 * This program is distributed in the hope that it will be useful,
47 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
49 * General Public License for more details.
51 * You should have received a copy of the GNU General Public License
52 * along with this program; if not, write to the Free Software
53 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
67 #include <sys/utsname.h>
70 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
71 # undef CONFIG_FEATURE_OLD_MODULE_INTERFACE
72 # define new_sys_init_module init_module
74 # define old_sys_init_module init_module
77 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
83 #if defined(__powerpc__)
84 #define CONFIG_USE_PLT_ENTRIES
85 #define CONFIG_PLT_ENTRY_SIZE 16
89 #define CONFIG_USE_PLT_ENTRIES
90 #define CONFIG_PLT_ENTRY_SIZE 8
91 #define CONFIG_USE_GOT_ENTRIES
92 #define CONFIG_GOT_ENTRY_SIZE 8
96 #define CONFIG_USE_GOT_ENTRIES
97 #define CONFIG_GOT_ENTRY_SIZE 4
100 #if defined(__i386__)
101 #define CONFIG_USE_GOT_ENTRIES
102 #define CONFIG_GOT_ENTRY_SIZE 4
105 #if defined(__mips__)
109 #if defined (__v850e__)
110 #define CONFIG_USE_PLT_ENTRIES
111 #define CONFIG_PLT_ENTRY_SIZE 8
114 //----------------------------------------------------------------------------
115 //--------modutils module.h, lines 45-242
116 //----------------------------------------------------------------------------
118 /* Definitions for the Linux module syscall interface.
119 Copyright 1996, 1997 Linux International.
121 Contributed by Richard Henderson <rth@tamu.edu>
123 This file is part of the Linux modutils.
125 This program is free software; you can redistribute it and/or modify it
126 under the terms of the GNU General Public License as published by the
127 Free Software Foundation; either version 2 of the License, or (at your
128 option) any later version.
130 This program is distributed in the hope that it will be useful, but
131 WITHOUT ANY WARRANTY; without even the implied warranty of
132 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
133 General Public License for more details.
135 You should have received a copy of the GNU General Public License
136 along with this program; if not, write to the Free Software Foundation,
137 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
140 #ifndef MODUTILS_MODULE_H
141 static const int MODUTILS_MODULE_H = 1;
143 #ident "$Id: insmod.c,v 1.82 2002/05/03 10:34:35 andersen Exp $"
145 /* This file contains the structures used by the 2.0 and 2.1 kernels.
146 We do not use the kernel headers directly because we do not wish
147 to be dependant on a particular kernel version to compile insmod. */
150 /*======================================================================*/
151 /* The structures used by Linux 2.0. */
153 /* The symbol format used by get_kernel_syms(2). */
154 struct old_kernel_sym
160 struct old_module_ref
162 unsigned long module; /* kernel addresses */
166 struct old_module_symbol
172 struct old_symbol_table
174 int size; /* total, including string table!!! */
177 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
178 struct old_module_ref ref[0]; /* actual size defined by n_refs */
181 struct old_mod_routines
184 unsigned long cleanup;
190 unsigned long ref; /* the list of modules that refer to me */
191 unsigned long symtab;
193 int size; /* size of module in pages */
194 unsigned long addr; /* address of module */
196 unsigned long cleanup; /* cleanup routine */
199 /* Sent to init_module(2) or'ed into the code size parameter. */
200 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
202 int get_kernel_syms(struct old_kernel_sym *);
203 int old_sys_init_module(const char *name, char *code, unsigned codesize,
204 struct old_mod_routines *, struct old_symbol_table *);
206 /*======================================================================*/
207 /* For sizeof() which are related to the module platform and not to the
208 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
210 #define tgt_sizeof_char sizeof(char)
211 #define tgt_sizeof_short sizeof(short)
212 #define tgt_sizeof_int sizeof(int)
213 #define tgt_sizeof_long sizeof(long)
214 #define tgt_sizeof_char_p sizeof(char *)
215 #define tgt_sizeof_void_p sizeof(void *)
216 #define tgt_long long
218 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
219 #undef tgt_sizeof_long
220 #undef tgt_sizeof_char_p
221 #undef tgt_sizeof_void_p
223 static const int tgt_sizeof_long = 8;
224 static const int tgt_sizeof_char_p = 8;
225 static const int tgt_sizeof_void_p = 8;
226 #define tgt_long long long
229 /*======================================================================*/
230 /* The structures used in Linux 2.1. */
232 /* Note: new_module_symbol does not use tgt_long intentionally */
233 struct new_module_symbol
239 struct new_module_persist;
241 struct new_module_ref
243 unsigned tgt_long dep; /* kernel addresses */
244 unsigned tgt_long ref;
245 unsigned tgt_long next_ref;
250 unsigned tgt_long size_of_struct; /* == sizeof(module) */
251 unsigned tgt_long next;
252 unsigned tgt_long name;
253 unsigned tgt_long size;
256 unsigned tgt_long flags; /* AUTOCLEAN et al */
261 unsigned tgt_long syms;
262 unsigned tgt_long deps;
263 unsigned tgt_long refs;
264 unsigned tgt_long init;
265 unsigned tgt_long cleanup;
266 unsigned tgt_long ex_table_start;
267 unsigned tgt_long ex_table_end;
269 unsigned tgt_long gp;
271 /* Everything after here is extension. */
272 unsigned tgt_long persist_start;
273 unsigned tgt_long persist_end;
274 unsigned tgt_long can_unload;
275 unsigned tgt_long runsize;
276 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
277 const char *kallsyms_start; /* All symbols for kernel debugging */
278 const char *kallsyms_end;
279 const char *archdata_start; /* arch specific data for module */
280 const char *archdata_end;
281 const char *kernel_data; /* Reserved for kernel internal use */
285 #define ARCHDATA_SEC_NAME "__archdata"
286 #define KALLSYMS_SEC_NAME "__kallsyms"
289 struct new_module_info
297 /* Bits of module.flags. */
298 static const int NEW_MOD_RUNNING = 1;
299 static const int NEW_MOD_DELETED = 2;
300 static const int NEW_MOD_AUTOCLEAN = 4;
301 static const int NEW_MOD_VISITED = 8;
302 static const int NEW_MOD_USED_ONCE = 16;
304 int new_sys_init_module(const char *name, const struct new_module *);
305 int query_module(const char *name, int which, void *buf, size_t bufsize,
308 /* Values for query_module's which. */
310 static const int QM_MODULES = 1;
311 static const int QM_DEPS = 2;
312 static const int QM_REFS = 3;
313 static const int QM_SYMBOLS = 4;
314 static const int QM_INFO = 5;
316 /*======================================================================*/
317 /* The system calls unchanged between 2.0 and 2.1. */
319 unsigned long create_module(const char *, size_t);
320 int delete_module(const char *);
323 #endif /* module.h */
325 //----------------------------------------------------------------------------
326 //--------end of modutils module.h
327 //----------------------------------------------------------------------------
331 //----------------------------------------------------------------------------
332 //--------modutils obj.h, lines 253-462
333 //----------------------------------------------------------------------------
335 /* Elf object file loading and relocation routines.
336 Copyright 1996, 1997 Linux International.
338 Contributed by Richard Henderson <rth@tamu.edu>
340 This file is part of the Linux modutils.
342 This program is free software; you can redistribute it and/or modify it
343 under the terms of the GNU General Public License as published by the
344 Free Software Foundation; either version 2 of the License, or (at your
345 option) any later version.
347 This program is distributed in the hope that it will be useful, but
348 WITHOUT ANY WARRANTY; without even the implied warranty of
349 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
350 General Public License for more details.
352 You should have received a copy of the GNU General Public License
353 along with this program; if not, write to the Free Software Foundation,
354 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
357 #ifndef MODUTILS_OBJ_H
358 static const int MODUTILS_OBJ_H = 1;
360 #ident "$Id: insmod.c,v 1.82 2002/05/03 10:34:35 andersen Exp $"
362 /* The relocatable object is manipulated using elfin types. */
368 #if __BYTE_ORDER == __LITTLE_ENDIAN
369 #define ELFDATAM ELFDATA2LSB
370 #elif __BYTE_ORDER == __BIG_ENDIAN
371 #define ELFDATAM ELFDATA2MSB
375 /* Machine-specific elf macros for i386 et al. */
377 /* the SH changes have only been tested on the SH4 in =little endian= mode */
378 /* I'm not sure about big endian, so let's warn: */
380 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
381 #error insmod.c may require changes for use on big endian SH4/SH3
384 /* it may or may not work on the SH1/SH2... So let's error on those
386 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
387 #error insmod.c may require changes for non-SH3/SH4 use
390 #define ELFCLASSM ELFCLASS32
395 #define MATCH_MACHINE(x) (x == EM_SH)
396 #define SHT_RELM SHT_RELA
397 #define Elf32_RelM Elf32_Rela
399 #elif defined(__arm__)
401 #define MATCH_MACHINE(x) (x == EM_ARM)
402 #define SHT_RELM SHT_REL
403 #define Elf32_RelM Elf32_Rel
405 #elif defined(__powerpc__)
407 #define MATCH_MACHINE(x) (x == EM_PPC)
408 #define SHT_RELM SHT_RELA
409 #define Elf32_RelM Elf32_Rela
411 #elif defined(__mips__)
413 /* Account for ELF spec changes. */
414 #ifndef EM_MIPS_RS3_LE
415 #ifdef EM_MIPS_RS4_BE
416 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
418 #define EM_MIPS_RS3_LE 10
420 #endif /* !EM_MIPS_RS3_LE */
422 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
423 #define SHT_RELM SHT_REL
424 #define Elf32_RelM Elf32_Rel
426 #elif defined(__i386__)
428 /* presumably we can use these for anything but the SH and ARM*/
429 /* this is the previous behavior, but it does result in
430 insmod.c being broken on anything except i386 */
432 #define MATCH_MACHINE(x) (x == EM_386)
434 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
437 #define SHT_RELM SHT_REL
438 #define Elf32_RelM Elf32_Rel
440 #elif defined(__mc68000__)
442 #define MATCH_MACHINE(x) (x == EM_68K)
443 #define SHT_RELM SHT_RELA
444 #define Elf32_RelM Elf32_Rela
446 #elif defined (__v850e__)
448 #ifndef EM_CYGNUS_V850 /* grumble */
449 #define EM_CYGNUS_V850 0x9080
452 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
453 #define SHT_RELM SHT_RELA
454 #define Elf32_RelM Elf32_Rela
456 #define SYMBOL_PREFIX "_"
459 #error Sorry, but insmod.c does not yet support this architecture...
463 # if ELFCLASSM == ELFCLASS32
464 # define ElfW(x) Elf32_ ## x
465 # define ELFW(x) ELF32_ ## x
467 # define ElfW(x) Elf64_ ## x
468 # define ELFW(x) ELF64_ ## x
472 /* For some reason this is missing from libc5. */
473 #ifndef ELF32_ST_INFO
474 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
477 #ifndef ELF64_ST_INFO
478 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
481 struct obj_string_patch;
482 struct obj_symbol_patch;
489 struct obj_section *load_next;
495 struct obj_symbol *next; /* hash table link */
499 int secidx; /* the defining section index/module */
501 int ksymidx; /* for export to the kernel symtab */
502 int referenced; /* actually used in the link */
505 /* Hardcode the hash table size. We shouldn't be needing so many
506 symbols that we begin to degrade performance, and we get a big win
507 by giving the compiler a constant divisor. */
509 #define HASH_BUCKETS 521
515 struct obj_section **sections;
516 struct obj_section *load_order;
517 struct obj_section **load_order_search_start;
518 struct obj_string_patch *string_patches;
519 struct obj_symbol_patch *symbol_patches;
520 int (*symbol_cmp)(const char *, const char *);
521 unsigned long (*symbol_hash)(const char *);
522 unsigned long local_symtab_size;
523 struct obj_symbol **local_symtab;
524 struct obj_symbol *symtab[HASH_BUCKETS];
535 struct obj_string_patch
537 struct obj_string_patch *next;
539 ElfW(Addr) reloc_offset;
540 ElfW(Addr) string_offset;
543 struct obj_symbol_patch
545 struct obj_symbol_patch *next;
547 ElfW(Addr) reloc_offset;
548 struct obj_symbol *sym;
552 /* Generic object manipulation routines. */
554 static unsigned long obj_elf_hash(const char *);
556 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
558 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
561 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
562 struct obj_symbol *sym);
564 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
565 static void obj_set_symbol_compare(struct obj_file *f,
566 int (*cmp)(const char *, const char *),
567 unsigned long (*hash)(const char *));
570 static struct obj_section *obj_find_section (struct obj_file *f,
573 static void obj_insert_section_load_order (struct obj_file *f,
574 struct obj_section *sec);
576 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
581 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
586 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
588 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
591 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
592 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
593 struct obj_symbol *sym);
596 static int obj_check_undefineds(struct obj_file *f);
598 static void obj_allocate_commons(struct obj_file *f);
600 static unsigned long obj_load_size (struct obj_file *f);
602 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
604 static struct obj_file *obj_load(FILE *f, int loadprogbits);
606 static int obj_create_image (struct obj_file *f, char *image);
608 /* Architecture specific manipulation routines. */
610 static struct obj_file *arch_new_file (void);
612 static struct obj_section *arch_new_section (void);
614 static struct obj_symbol *arch_new_symbol (void);
616 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
617 struct obj_section *targsec,
618 struct obj_section *symsec,
619 struct obj_symbol *sym,
620 ElfW(RelM) *rel, ElfW(Addr) value);
622 static int arch_create_got (struct obj_file *f);
624 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
625 static int arch_init_module (struct obj_file *f, struct new_module *);
629 //----------------------------------------------------------------------------
630 //--------end of modutils obj.h
631 //----------------------------------------------------------------------------
635 /* SPFX is always a string, so it can be concatenated to string constants. */
637 #define SPFX SYMBOL_PREFIX
643 #define _PATH_MODULES "/lib/modules"
644 static const int STRVERSIONLEN = 32;
646 /*======================================================================*/
648 static int flag_force_load = 0;
649 static int flag_autoclean = 0;
650 static int flag_verbose = 0;
651 static int flag_export = 1;
654 /*======================================================================*/
656 /* previously, these were named i386_* but since we could be
657 compiling for the sh, I've renamed them to the more general
658 arch_* These structures are the same between the x86 and SH,
659 and we can't support anything else right now anyway. In the
660 future maybe they should be #if defined'd */
666 #if defined(CONFIG_USE_PLT_ENTRIES)
667 struct arch_plt_entry
671 int inited:1; /* has been set up */
675 #if defined(CONFIG_USE_GOT_ENTRIES)
676 struct arch_got_entry {
678 unsigned offset_done:1;
679 unsigned reloc_done:1;
683 #if defined(__mips__)
686 struct mips_hi16 *next;
693 struct obj_file root;
694 #if defined(CONFIG_USE_PLT_ENTRIES)
695 struct obj_section *plt;
697 #if defined(CONFIG_USE_GOT_ENTRIES)
698 struct obj_section *got;
700 #if defined(__mips__)
701 struct mips_hi16 *mips_hi16_list;
706 struct obj_symbol root;
707 #if defined(CONFIG_USE_PLT_ENTRIES)
708 struct arch_plt_entry pltent;
710 #if defined(CONFIG_USE_GOT_ENTRIES)
711 struct arch_got_entry gotent;
716 struct external_module {
721 struct new_module_symbol *syms;
724 static struct new_module_symbol *ksyms;
725 static size_t nksyms;
727 static struct external_module *ext_modules;
728 static int n_ext_modules;
729 static int n_ext_modules_used;
730 extern int delete_module(const char *);
732 static char m_filename[FILENAME_MAX + 1];
733 static char m_fullName[FILENAME_MAX + 1];
737 /*======================================================================*/
740 static int check_module_name_match(const char *filename, struct stat *statbuf,
743 char *fullname = (char *) userdata;
745 if (fullname[0] == '\0')
748 char *tmp, *tmp1 = xstrdup(filename);
749 tmp = get_last_path_component(tmp1);
750 if (strcmp(tmp, fullname) == 0) {
752 /* Stop searching if we find a match */
753 safe_strncpy(m_filename, filename, sizeof(m_filename));
762 /*======================================================================*/
764 static struct obj_file *arch_new_file(void)
767 f = xmalloc(sizeof(*f));
769 #if defined(CONFIG_USE_PLT_ENTRIES)
772 #if defined(CONFIG_USE_GOT_ENTRIES)
775 #if defined(__mips__)
776 f->mips_hi16_list = NULL;
782 static struct obj_section *arch_new_section(void)
784 return xmalloc(sizeof(struct obj_section));
787 static struct obj_symbol *arch_new_symbol(void)
789 struct arch_symbol *sym;
790 sym = xmalloc(sizeof(*sym));
792 #if defined(CONFIG_USE_PLT_ENTRIES)
793 memset(&sym->pltent, 0, sizeof(sym->pltent));
795 #if defined(CONFIG_USE_GOT_ENTRIES)
796 memset(&sym->gotent, 0, sizeof(sym->gotent));
802 static enum obj_reloc
803 arch_apply_relocation(struct obj_file *f,
804 struct obj_section *targsec,
805 struct obj_section *symsec,
806 struct obj_symbol *sym,
807 ElfW(RelM) *rel, ElfW(Addr) v)
809 struct arch_file *ifile = (struct arch_file *) f;
810 #if !(defined(__mips__))
811 struct arch_symbol *isym = (struct arch_symbol *) sym;
814 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
815 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
816 #if defined(CONFIG_USE_GOT_ENTRIES)
817 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
819 #if defined(CONFIG_USE_PLT_ENTRIES)
820 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
821 struct arch_plt_entry *pe;
824 enum obj_reloc ret = obj_reloc_ok;
826 switch (ELF32_R_TYPE(rel->r_info)) {
828 /* even though these constants seem to be the same for
829 the i386 and the sh, we "#if define" them for clarity
830 and in case that ever changes */
833 #elif defined(__arm__)
835 #elif defined(__i386__)
837 #elif defined(__mc68000__)
839 #elif defined(__powerpc__)
841 #elif defined(__mips__)
843 #elif defined (__v850e__)
848 #if defined (__v850e__)
850 /* We write two shorts instead of a long because even
851 32-bit insns only need half-word alignment, but
852 32-bit data needs to be long-word aligned. */
853 v += ((unsigned short *)loc)[0];
854 v += ((unsigned short *)loc)[1] << 16;
855 ((unsigned short *)loc)[0] = v & 0xffff;
856 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
858 #else /* !__v850e__ */
861 #elif defined(__arm__)
863 #elif defined(__i386__)
865 #elif defined(__mc68000__)
867 #elif defined(__powerpc__)
869 #elif defined(__mips__)
874 #endif /* __v850e__ */
876 #if defined(__mc68000__)
879 ret = obj_reloc_overflow;
884 ret = obj_reloc_overflow;
887 #endif /* __mc68000__ */
889 #if defined(__powerpc__)
890 case R_PPC_ADDR16_HA:
891 *(unsigned short *)loc = (v + 0x8000) >> 16;
894 case R_PPC_ADDR16_HI:
895 *(unsigned short *)loc = v >> 16;
898 case R_PPC_ADDR16_LO:
899 *(unsigned short *)loc = v;
903 #if defined(__mips__)
906 ret = obj_reloc_dangerous;
907 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
908 ret = obj_reloc_overflow;
910 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
918 /* We cannot relocate this one now because we don't know the value
919 of the carry we need to add. Save the information, and let LO16
920 do the actual relocation. */
921 n = (struct mips_hi16 *) xmalloc(sizeof *n);
924 n->next = ifile->mips_hi16_list;
925 ifile->mips_hi16_list = n;
931 unsigned long insnlo = *loc;
932 Elf32_Addr val, vallo;
934 /* Sign extend the addend we extract from the lo insn. */
935 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
937 if (ifile->mips_hi16_list != NULL) {
940 l = ifile->mips_hi16_list;
942 struct mips_hi16 *next;
945 /* The value for the HI16 had best be the same. */
946 assert(v == l->value);
948 /* Do the HI16 relocation. Note that we actually don't
949 need to know anything about the LO16 itself, except where
950 to find the low 16 bits of the addend needed by the LO16. */
953 ((insn & 0xffff) << 16) +
957 /* Account for the sign extension that will happen in the
964 insn = (insn & ~0xffff) | val;
972 ifile->mips_hi16_list = NULL;
975 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
977 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
984 #elif defined(__sh__)
988 #elif defined(__i386__)
993 #elif defined(__mc68000__)
996 if ((Elf32_Sword)v > 0x7f || (Elf32_Sword)v < -(Elf32_Sword)0x80)
997 ret = obj_reloc_overflow;
1002 if ((Elf32_Sword)v > 0x7fff || (Elf32_Sword)v < -(Elf32_Sword)0x8000)
1003 ret = obj_reloc_overflow;
1007 *(int *)loc = v - dot;
1009 #elif defined(__powerpc__)
1019 #elif defined(__i386__)
1022 #if defined(CONFIG_USE_PLT_ENTRIES)
1024 #if defined(__arm__)
1028 #if defined(__powerpc__)
1031 #if defined (__v850e__)
1032 case R_V850_22_PCREL:
1034 /* find the plt entry and initialize it if necessary */
1035 assert(isym != NULL);
1037 pe = (struct arch_plt_entry*) &isym->pltent;
1040 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1042 /* generate some machine code */
1044 #if defined(__arm__)
1045 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1046 ip[1] = v; /* sym@ */
1048 #if defined(__powerpc__)
1049 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1050 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1051 ip[2] = 0x7d6903a6; /* mtctr r11 */
1052 ip[3] = 0x4e800420; /* bctr */
1054 #if defined (__v850e__)
1055 /* We have to trash a register, so we assume that any control
1056 transfer more than 21-bits away must be a function call
1057 (so we can use a call-clobbered register). */
1058 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1059 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1064 /* relative distance to target */
1066 /* if the target is too far away.... */
1067 #if defined (__arm__) || defined (__powerpc__)
1068 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1069 #elif defined (__v850e__)
1070 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1072 /* go via the plt */
1073 v = plt + pe->offset - dot;
1075 #if defined (__v850e__)
1080 ret = obj_reloc_dangerous;
1082 /* merge the offset into the instruction. */
1083 #if defined(__arm__)
1084 /* Convert to words. */
1087 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1089 #if defined(__powerpc__)
1090 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1092 #if defined (__v850e__)
1093 /* We write two shorts instead of a long because even 32-bit insns
1094 only need half-word alignment, but the 32-bit data write needs
1095 to be long-word aligned. */
1096 ((unsigned short *)loc)[0] =
1097 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1098 | ((v >> 16) & 0x3f); /* offs high part */
1099 ((unsigned short *)loc)[1] =
1100 (v & 0xffff); /* offs low part */
1104 #endif /* CONFIG_USE_PLT_ENTRIES */
1106 #if defined(__arm__)
1107 #elif defined(__sh__)
1112 #elif defined(__i386__)
1113 case R_386_GLOB_DAT:
1114 case R_386_JMP_SLOT:
1117 #elif defined(__mc68000__)
1118 case R_68K_GLOB_DAT:
1119 case R_68K_JMP_SLOT:
1124 #if defined(__arm__)
1125 #elif defined(__sh__)
1127 *loc += f->baseaddr + rel->r_addend;
1129 #elif defined(__i386__)
1130 case R_386_RELATIVE:
1131 *loc += f->baseaddr;
1133 #elif defined(__mc68000__)
1134 case R_68K_RELATIVE:
1135 *(int *)loc += f->baseaddr;
1139 #if defined(CONFIG_USE_GOT_ENTRIES)
1141 #if !defined(__68k__)
1144 #elif defined(__arm__)
1146 #elif defined(__i386__)
1151 *loc += got - dot + rel->r_addend;;
1152 #elif defined(__i386__) || defined(__arm__) || defined(__m68k_)
1160 #elif defined(__arm__)
1162 #elif defined(__i386__)
1164 #elif defined(__mc68000__)
1167 assert(isym != NULL);
1168 /* needs an entry in the .got: set it, once */
1169 if (!isym->gotent.reloc_done) {
1170 isym->gotent.reloc_done = 1;
1171 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1173 /* make the reloc with_respect_to_.got */
1175 *loc += isym->gotent.offset + rel->r_addend;
1176 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1177 *loc += isym->gotent.offset;
1181 /* address relative to the got */
1182 #if !defined(__mc68000__)
1185 #elif defined(__arm__)
1187 #elif defined(__i386__)
1189 #elif defined(__mc68000__)
1195 #endif // __mc68000__
1197 #endif /* CONFIG_USE_GOT_ENTRIES */
1200 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1201 ret = obj_reloc_unhandled;
1208 static int arch_create_got(struct obj_file *f)
1210 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1211 struct arch_file *ifile = (struct arch_file *) f;
1213 #if defined(CONFIG_USE_GOT_ENTRIES)
1214 int got_offset = 0, gotneeded = 0;
1216 #if defined(CONFIG_USE_PLT_ENTRIES)
1217 int plt_offset = 0, pltneeded = 0;
1219 struct obj_section *relsec, *symsec, *strsec;
1220 ElfW(RelM) *rel, *relend;
1221 ElfW(Sym) *symtab, *extsym;
1222 const char *strtab, *name;
1223 struct arch_symbol *intsym;
1225 for (i = 0; i < f->header.e_shnum; ++i) {
1226 relsec = f->sections[i];
1227 if (relsec->header.sh_type != SHT_RELM)
1230 symsec = f->sections[relsec->header.sh_link];
1231 strsec = f->sections[symsec->header.sh_link];
1233 rel = (ElfW(RelM) *) relsec->contents;
1234 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1235 symtab = (ElfW(Sym) *) symsec->contents;
1236 strtab = (const char *) strsec->contents;
1238 for (; rel < relend; ++rel) {
1239 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1241 switch (ELF32_R_TYPE(rel->r_info)) {
1242 #if defined(__arm__)
1245 #elif defined(__sh__)
1248 #elif defined(__i386__)
1251 #elif defined(__mc68000__)
1256 #if defined(__powerpc__)
1262 #if defined (__v850e__)
1263 case R_V850_22_PCREL:
1268 #if defined(__arm__)
1277 if (got_offset == 0)
1279 #elif defined(__sh__)
1283 #elif defined(__i386__)
1293 if (extsym->st_name != 0) {
1294 name = strtab + extsym->st_name;
1296 name = f->sections[extsym->st_shndx]->name;
1298 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1299 #if defined(CONFIG_USE_GOT_ENTRIES)
1300 if (!intsym->gotent.offset_done) {
1301 intsym->gotent.offset_done = 1;
1302 intsym->gotent.offset = got_offset;
1303 got_offset += CONFIG_GOT_ENTRY_SIZE;
1306 #if defined(CONFIG_USE_PLT_ENTRIES)
1307 if (pltneeded && intsym->pltent.allocated == 0) {
1308 intsym->pltent.allocated = 1;
1309 intsym->pltent.offset = plt_offset;
1310 plt_offset += CONFIG_PLT_ENTRY_SIZE;
1311 intsym->pltent.inited = 0;
1318 #if defined(CONFIG_USE_GOT_ENTRIES)
1320 struct obj_section* myrelsec = obj_find_section(f, ".got");
1323 obj_extend_section(myrelsec, got_offset);
1325 myrelsec = obj_create_alloced_section(f, ".got",
1326 CONFIG_GOT_ENTRY_SIZE,
1331 ifile->got = myrelsec;
1335 #if defined(CONFIG_USE_PLT_ENTRIES)
1337 ifile->plt = obj_create_alloced_section(f, ".plt",
1338 CONFIG_PLT_ENTRY_SIZE,
1345 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
1346 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1352 /*======================================================================*/
1354 /* Standard ELF hash function. */
1355 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1357 unsigned long h = 0;
1364 if ((g = (h & 0xf0000000)) != 0) {
1373 static unsigned long obj_elf_hash(const char *name)
1375 return obj_elf_hash_n(name, strlen(name));
1378 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1379 /* String comparison for non-co-versioned kernel and module. */
1381 static int ncv_strcmp(const char *a, const char *b)
1383 size_t alen = strlen(a), blen = strlen(b);
1385 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1386 return strncmp(a, b, alen);
1387 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1388 return strncmp(a, b, blen);
1390 return strcmp(a, b);
1393 /* String hashing for non-co-versioned kernel and module. Here
1394 we are simply forced to drop the crc from the hash. */
1396 static unsigned long ncv_symbol_hash(const char *str)
1398 size_t len = strlen(str);
1399 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1401 return obj_elf_hash_n(str, len);
1405 obj_set_symbol_compare(struct obj_file *f,
1406 int (*cmp) (const char *, const char *),
1407 unsigned long (*hash) (const char *))
1410 f->symbol_cmp = cmp;
1412 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1415 f->symbol_hash = hash;
1417 memcpy(tmptab, f->symtab, sizeof(tmptab));
1418 memset(f->symtab, 0, sizeof(f->symtab));
1420 for (i = 0; i < HASH_BUCKETS; ++i)
1421 for (sym = tmptab[i]; sym; sym = next) {
1422 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1424 sym->next = f->symtab[h];
1430 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1432 static struct obj_symbol *
1433 obj_add_symbol(struct obj_file *f, const char *name,
1434 unsigned long symidx, int info,
1435 int secidx, ElfW(Addr) value,
1438 struct obj_symbol *sym;
1439 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1440 int n_type = ELFW(ST_TYPE) (info);
1441 int n_binding = ELFW(ST_BIND) (info);
1443 for (sym = f->symtab[hash]; sym; sym = sym->next)
1444 if (f->symbol_cmp(sym->name, name) == 0) {
1445 int o_secidx = sym->secidx;
1446 int o_info = sym->info;
1447 int o_type = ELFW(ST_TYPE) (o_info);
1448 int o_binding = ELFW(ST_BIND) (o_info);
1450 /* A redefinition! Is it legal? */
1452 if (secidx == SHN_UNDEF)
1454 else if (o_secidx == SHN_UNDEF)
1456 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1457 /* Cope with local and global symbols of the same name
1458 in the same object file, as might have been created
1459 by ld -r. The only reason locals are now seen at this
1460 level at all is so that we can do semi-sensible things
1463 struct obj_symbol *nsym, **p;
1465 nsym = arch_new_symbol();
1466 nsym->next = sym->next;
1469 /* Excise the old (local) symbol from the hash chain. */
1470 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1474 } else if (n_binding == STB_LOCAL) {
1475 /* Another symbol of the same name has already been defined.
1476 Just add this to the local table. */
1477 sym = arch_new_symbol();
1480 f->local_symtab[symidx] = sym;
1482 } else if (n_binding == STB_WEAK)
1484 else if (o_binding == STB_WEAK)
1486 /* Don't unify COMMON symbols with object types the programmer
1488 else if (secidx == SHN_COMMON
1489 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1491 else if (o_secidx == SHN_COMMON
1492 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1495 /* Don't report an error if the symbol is coming from
1496 the kernel or some external module. */
1497 if (secidx <= SHN_HIRESERVE)
1498 error_msg("%s multiply defined", name);
1503 /* Completely new symbol. */
1504 sym = arch_new_symbol();
1505 sym->next = f->symtab[hash];
1506 f->symtab[hash] = sym;
1509 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1510 if (symidx >= f->local_symtab_size)
1511 error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1512 name, (long) symidx, (long) f->local_symtab_size);
1514 f->local_symtab[symidx] = sym;
1521 sym->secidx = secidx;
1527 static struct obj_symbol *
1528 obj_find_symbol(struct obj_file *f, const char *name)
1530 struct obj_symbol *sym;
1531 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1533 for (sym = f->symtab[hash]; sym; sym = sym->next)
1534 if (f->symbol_cmp(sym->name, name) == 0)
1541 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1544 if (sym->secidx >= SHN_LORESERVE)
1547 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1549 /* As a special case, a NULL sym has value zero. */
1554 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1556 int i, n = f->header.e_shnum;
1558 for (i = 0; i < n; ++i)
1559 if (strcmp(f->sections[i]->name, name) == 0)
1560 return f->sections[i];
1565 static int obj_load_order_prio(struct obj_section *a)
1567 unsigned long af, ac;
1569 af = a->header.sh_flags;
1572 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1573 strcmp(a->name + 5, ".init"))
1577 if (!(af & SHF_WRITE))
1579 if (af & SHF_EXECINSTR)
1581 if (a->header.sh_type != SHT_NOBITS)
1588 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1590 struct obj_section **p;
1591 int prio = obj_load_order_prio(sec);
1592 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1593 if (obj_load_order_prio(*p) < prio)
1595 sec->load_next = *p;
1599 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1601 unsigned long align,
1604 int newidx = f->header.e_shnum++;
1605 struct obj_section *sec;
1607 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1608 f->sections[newidx] = sec = arch_new_section();
1610 memset(sec, 0, sizeof(*sec));
1611 sec->header.sh_type = SHT_PROGBITS;
1612 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1613 sec->header.sh_size = size;
1614 sec->header.sh_addralign = align;
1618 sec->contents = xmalloc(size);
1620 obj_insert_section_load_order(f, sec);
1625 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1627 unsigned long align,
1630 int newidx = f->header.e_shnum++;
1631 struct obj_section *sec;
1633 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1634 f->sections[newidx] = sec = arch_new_section();
1636 memset(sec, 0, sizeof(*sec));
1637 sec->header.sh_type = SHT_PROGBITS;
1638 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1639 sec->header.sh_size = size;
1640 sec->header.sh_addralign = align;
1644 sec->contents = xmalloc(size);
1646 sec->load_next = f->load_order;
1647 f->load_order = sec;
1648 if (f->load_order_search_start == &f->load_order)
1649 f->load_order_search_start = &sec->load_next;
1654 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1656 unsigned long oldsize = sec->header.sh_size;
1658 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1660 return sec->contents + oldsize;
1664 /* Conditionally add the symbols from the given symbol set to the
1670 int idx, struct new_module_symbol *syms, size_t nsyms)
1672 struct new_module_symbol *s;
1675 #ifdef SYMBOL_PREFIX
1677 size_t name_alloced_size = 0;
1680 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1681 /* Only add symbols that are already marked external.
1682 If we override locals we may cause problems for
1683 argument initialization. We will also create a false
1684 dependency on the module. */
1685 struct obj_symbol *sym;
1686 char *name = (char *)s->name;
1688 #ifdef SYMBOL_PREFIX
1689 /* Prepend SYMBOL_PREFIX to the symbol's name (the
1690 kernel exports `C names', but module object files
1691 reference `linker names'). */
1692 size_t extra = sizeof SYMBOL_PREFIX;
1693 size_t name_size = strlen (name) + extra;
1694 if (name_size > name_alloced_size) {
1695 name_alloced_size = name_size * 2;
1696 name_buf = alloca (name_alloced_size);
1698 strcpy (name_buf, SYMBOL_PREFIX);
1699 strcpy (name_buf + extra - 1, name);
1701 #endif /* SYMBOL_PREFIX */
1703 sym = obj_find_symbol(f, name);
1704 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
1705 #ifdef SYMBOL_PREFIX
1706 /* Put NAME_BUF into more permanent storage. */
1707 name = xmalloc (name_size);
1708 strcpy (name, name_buf);
1710 sym = obj_add_symbol(f, name, -1,
1711 ELFW(ST_INFO) (STB_GLOBAL,
1714 /* Did our symbol just get installed? If so, mark the
1715 module as "used". */
1716 if (sym->secidx == idx)
1724 static void add_kernel_symbols(struct obj_file *f)
1726 struct external_module *m;
1729 /* Add module symbols first. */
1731 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1733 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1734 m->nsyms)) m->used = 1, ++nused;
1736 n_ext_modules_used = nused;
1738 /* And finally the symbols from the kernel proper. */
1741 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1744 static char *get_modinfo_value(struct obj_file *f, const char *key)
1746 struct obj_section *sec;
1747 char *p, *v, *n, *ep;
1748 size_t klen = strlen(key);
1750 sec = obj_find_section(f, ".modinfo");
1754 ep = p + sec->header.sh_size;
1757 n = strchr(p, '\0');
1759 if (p + klen == v && strncmp(p, key, klen) == 0)
1762 if (p + klen == n && strcmp(p, key) == 0)
1772 /*======================================================================*/
1773 /* Functions relating to module loading in pre 2.1 kernels. */
1776 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1780 struct obj_symbol *sym;
1784 if ((q = strchr(p, '=')) == NULL) {
1790 sym = obj_find_symbol(f, p);
1792 /* Also check that the parameter was not resolved from the kernel. */
1793 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1794 error_msg("symbol for parameter %s not found", p);
1798 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1800 /* Do C quoting if we begin with a ". */
1804 str = alloca(strlen(q));
1805 for (r = str, q++; *q != '"'; ++q, ++r) {
1807 error_msg("improperly terminated string argument for %s", p);
1809 } else if (*q == '\\')
1843 if (q[1] >= '0' && q[1] <= '7') {
1844 c = (c * 8) + *++q - '0';
1845 if (q[1] >= '0' && q[1] <= '7')
1846 c = (c * 8) + *++q - '0';
1859 obj_string_patch(f, sym->secidx, sym->value, str);
1860 } else if (*q >= '0' && *q <= '9') {
1862 *loc++ = strtoul(q, &q, 0);
1863 while (*q++ == ',');
1865 char *contents = f->sections[sym->secidx]->contents;
1866 char *myloc = contents + sym->value;
1867 char *r; /* To search for commas */
1869 /* Break the string with comas */
1870 while ((r = strchr(q, ',')) != (char *) NULL) {
1872 obj_string_patch(f, sym->secidx, myloc - contents, q);
1873 myloc += sizeof(char *);
1878 obj_string_patch(f, sym->secidx, myloc - contents, q);
1887 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1888 static int old_is_module_checksummed(struct obj_file *f)
1890 return obj_find_symbol(f, "Using_Versions") != NULL;
1892 /* Get the module's kernel version in the canonical integer form. */
1895 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1897 struct obj_symbol *sym;
1901 sym = obj_find_symbol(f, "kernel_version");
1905 p = f->sections[sym->secidx]->contents + sym->value;
1906 strncpy(str, p, STRVERSIONLEN);
1908 a = strtoul(p, &p, 10);
1911 b = strtoul(p + 1, &p, 10);
1914 c = strtoul(p + 1, &q, 10);
1918 return a << 16 | b << 8 | c;
1921 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1923 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
1925 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1927 static int old_get_kernel_symbols(const char *m_name)
1929 struct old_kernel_sym *ks, *k;
1930 struct new_module_symbol *s;
1931 struct external_module *mod;
1932 int nks, nms, nmod, i;
1934 nks = get_kernel_syms(NULL);
1937 perror_msg("get_kernel_syms: %s", m_name);
1939 error_msg("No kernel symbols");
1943 ks = k = xmalloc(nks * sizeof(*ks));
1945 if (get_kernel_syms(ks) != nks) {
1946 perror("inconsistency with get_kernel_syms -- is someone else "
1947 "playing with modules?");
1952 /* Collect the module information. */
1957 while (k->name[0] == '#' && k->name[1]) {
1958 struct old_kernel_sym *k2;
1960 /* Find out how many symbols this module has. */
1961 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1965 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1966 mod[nmod].name = k->name + 1;
1967 mod[nmod].addr = k->value;
1969 mod[nmod].nsyms = nms;
1970 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1972 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1973 s->name = (unsigned long) k->name;
1974 s->value = k->value;
1981 n_ext_modules = nmod + 1;
1983 /* Now collect the symbols for the kernel proper. */
1985 if (k->name[0] == '#')
1988 nksyms = nms = nks - (k - ks);
1989 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1991 for (i = 0; i < nms; ++i, ++s, ++k) {
1992 s->name = (unsigned long) k->name;
1993 s->value = k->value;
1999 /* Return the kernel symbol checksum version, or zero if not used. */
2001 static int old_is_kernel_checksummed(void)
2003 /* Using_Versions is the first symbol. */
2005 && strcmp((char *) ksyms[0].name,
2006 "Using_Versions") == 0) return ksyms[0].value;
2012 static int old_create_mod_use_count(struct obj_file *f)
2014 struct obj_section *sec;
2016 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2019 obj_add_symbol(f, "mod_use_count_", -1,
2020 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2027 old_init_module(const char *m_name, struct obj_file *f,
2028 unsigned long m_size)
2031 struct old_mod_routines routines;
2032 struct old_symbol_table *symtab;
2035 /* Create the symbol table */
2037 int nsyms = 0, strsize = 0, total;
2039 /* Size things first... */
2042 for (i = 0; i < HASH_BUCKETS; ++i) {
2043 struct obj_symbol *sym;
2044 for (sym = f->symtab[i]; sym; sym = sym->next)
2045 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2046 && sym->secidx <= SHN_HIRESERVE)
2048 sym->ksymidx = nsyms++;
2049 strsize += strlen(sym->name) + 1;
2054 total = (sizeof(struct old_symbol_table)
2055 + nsyms * sizeof(struct old_module_symbol)
2056 + n_ext_modules_used * sizeof(struct old_module_ref)
2058 symtab = xmalloc(total);
2059 symtab->size = total;
2060 symtab->n_symbols = nsyms;
2061 symtab->n_refs = n_ext_modules_used;
2063 if (flag_export && nsyms) {
2064 struct old_module_symbol *ksym;
2068 ksym = symtab->symbol;
2069 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2070 + n_ext_modules_used * sizeof(struct old_module_ref));
2072 for (i = 0; i < HASH_BUCKETS; ++i) {
2073 struct obj_symbol *sym;
2074 for (sym = f->symtab[i]; sym; sym = sym->next)
2075 if (sym->ksymidx >= 0) {
2076 ksym->addr = obj_symbol_final_value(f, sym);
2078 (unsigned long) str - (unsigned long) symtab;
2080 strcpy(str, sym->name);
2081 str += strlen(sym->name) + 1;
2087 if (n_ext_modules_used) {
2088 struct old_module_ref *ref;
2091 ref = (struct old_module_ref *)
2092 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2094 for (i = 0; i < n_ext_modules; ++i)
2095 if (ext_modules[i].used)
2096 ref++->module = ext_modules[i].addr;
2100 /* Fill in routines. */
2103 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2105 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2107 /* Whew! All of the initialization is complete. Collect the final
2108 module image and give it to the kernel. */
2110 image = xmalloc(m_size);
2111 obj_create_image(f, image);
2113 /* image holds the complete relocated module, accounting correctly for
2114 mod_use_count. However the old module kernel support assume that
2115 it is receiving something which does not contain mod_use_count. */
2116 ret = old_sys_init_module(m_name, image + sizeof(long),
2117 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2118 : 0), &routines, symtab);
2120 perror_msg("init_module: %s", m_name);
2130 #define old_create_mod_use_count(x) TRUE
2131 #define old_init_module(x, y, z) TRUE
2133 #endif /* CONFIG_FEATURE_OLD_MODULE_INTERFACE */
2137 /*======================================================================*/
2138 /* Functions relating to module loading after 2.1.18. */
2141 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2144 char *p, *q, *key, *sym_name;
2145 struct obj_symbol *sym;
2146 char *contents, *loc;
2150 if ((q = strchr(p, '=')) == NULL) {
2155 key = alloca(q - p + 6);
2156 memcpy(key, "parm_", 5);
2157 memcpy(key + 5, p, q - p);
2160 p = get_modinfo_value(f, key);
2163 error_msg("invalid parameter %s", key);
2167 #ifdef SYMBOL_PREFIX
2168 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2169 strcpy (sym_name, SYMBOL_PREFIX);
2170 strcat (sym_name, key);
2174 sym = obj_find_symbol(f, sym_name);
2176 /* Also check that the parameter was not resolved from the kernel. */
2177 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2178 error_msg("symbol for parameter %s not found", key);
2183 min = strtoul(p, &p, 10);
2185 max = strtoul(p + 1, &p, 10);
2191 contents = f->sections[sym->secidx]->contents;
2192 loc = contents + sym->value;
2196 if ((*p == 's') || (*p == 'c')) {
2199 /* Do C quoting if we begin with a ", else slurp the lot. */
2203 str = alloca(strlen(q));
2204 for (r = str, q++; *q != '"'; ++q, ++r) {
2206 error_msg("improperly terminated string argument for %s",
2209 } else if (*q == '\\')
2243 if (q[1] >= '0' && q[1] <= '7') {
2244 c = (c * 8) + *++q - '0';
2245 if (q[1] >= '0' && q[1] <= '7')
2246 c = (c * 8) + *++q - '0';
2263 /* In this case, the string is not quoted. We will break
2264 it using the coma (like for ints). If the user wants to
2265 include comas in a string, he just has to quote it */
2267 /* Search the next coma */
2271 if (r != (char *) NULL) {
2272 /* Recopy the current field */
2273 str = alloca(r - q + 1);
2274 memcpy(str, q, r - q);
2276 /* I don't know if it is usefull, as the previous case
2277 doesn't null terminate the string ??? */
2280 /* Keep next fields */
2291 obj_string_patch(f, sym->secidx, loc - contents, str);
2292 loc += tgt_sizeof_char_p;
2294 /* Array of chars (in fact, matrix !) */
2295 unsigned long charssize; /* size of each member */
2297 /* Get the size of each member */
2298 /* Probably we should do that outside the loop ? */
2299 if (!isdigit(*(p + 1))) {
2300 error_msg("parameter type 'c' for %s must be followed by"
2301 " the maximum size", key);
2304 charssize = strtoul(p + 1, (char **) NULL, 10);
2307 if (strlen(str) >= charssize) {
2308 error_msg("string too long for %s (max %ld)", key,
2313 /* Copy to location */
2314 strcpy((char *) loc, str);
2318 long v = strtoul(q, &q, 0);
2325 loc += tgt_sizeof_short;
2329 loc += tgt_sizeof_int;
2333 loc += tgt_sizeof_long;
2337 error_msg("unknown parameter type '%c' for %s", *p, key);
2352 goto retry_end_of_value;
2356 error_msg("too many values for %s (max %d)", key, max);
2363 error_msg("invalid argument syntax for %s", key);
2370 error_msg("too few values for %s (min %d)", key, min);
2380 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2381 static int new_is_module_checksummed(struct obj_file *f)
2383 const char *p = get_modinfo_value(f, "using_checksums");
2390 /* Get the module's kernel version in the canonical integer form. */
2393 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2398 p = get_modinfo_value(f, "kernel_version");
2401 strncpy(str, p, STRVERSIONLEN);
2403 a = strtoul(p, &p, 10);
2406 b = strtoul(p + 1, &p, 10);
2409 c = strtoul(p + 1, &q, 10);
2413 return a << 16 | b << 8 | c;
2416 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2419 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2421 /* Fetch the loaded modules, and all currently exported symbols. */
2423 static int new_get_kernel_symbols(void)
2425 char *module_names, *mn;
2426 struct external_module *modules, *m;
2427 struct new_module_symbol *syms, *s;
2428 size_t ret, bufsize, nmod, nsyms, i, j;
2430 /* Collect the loaded modules. */
2432 module_names = xmalloc(bufsize = 256);
2434 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2435 if (errno == ENOSPC && bufsize < ret) {
2436 module_names = xrealloc(module_names, bufsize = ret);
2437 goto retry_modules_load;
2439 perror_msg("QM_MODULES");
2443 n_ext_modules = nmod = ret;
2445 /* Collect the modules' symbols. */
2448 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2449 memset(modules, 0, nmod * sizeof(*modules));
2450 for (i = 0, mn = module_names, m = modules;
2451 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2452 struct new_module_info info;
2454 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2455 if (errno == ENOENT) {
2456 /* The module was removed out from underneath us. */
2459 perror_msg("query_module: QM_INFO: %s", mn);
2463 syms = xmalloc(bufsize = 1024);
2465 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2468 syms = xrealloc(syms, bufsize = ret);
2469 goto retry_mod_sym_load;
2471 /* The module was removed out from underneath us. */
2474 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2481 m->addr = info.addr;
2485 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2486 s->name += (unsigned long) syms;
2491 /* Collect the kernel's symbols. */
2493 syms = xmalloc(bufsize = 16 * 1024);
2494 retry_kern_sym_load:
2495 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2496 if (errno == ENOSPC && bufsize < ret) {
2497 syms = xrealloc(syms, bufsize = ret);
2498 goto retry_kern_sym_load;
2500 perror_msg("kernel: QM_SYMBOLS");
2503 nksyms = nsyms = ret;
2506 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2507 s->name += (unsigned long) syms;
2513 /* Return the kernel symbol checksum version, or zero if not used. */
2515 static int new_is_kernel_checksummed(void)
2517 struct new_module_symbol *s;
2520 /* Using_Versions is not the first symbol, but it should be in there. */
2522 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2523 if (strcmp((char *) s->name, "Using_Versions") == 0)
2530 static int new_create_this_module(struct obj_file *f, const char *m_name)
2532 struct obj_section *sec;
2534 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2535 sizeof(struct new_module));
2536 memset(sec->contents, 0, sizeof(struct new_module));
2538 obj_add_symbol(f, SPFX "__this_module", -1,
2539 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2540 sizeof(struct new_module));
2542 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2549 static int new_create_module_ksymtab(struct obj_file *f)
2551 struct obj_section *sec;
2554 /* We must always add the module references. */
2556 if (n_ext_modules_used) {
2557 struct new_module_ref *dep;
2558 struct obj_symbol *tm;
2560 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2561 (sizeof(struct new_module_ref)
2562 * n_ext_modules_used));
2566 tm = obj_find_symbol(f, SPFX "__this_module");
2567 dep = (struct new_module_ref *) sec->contents;
2568 for (i = 0; i < n_ext_modules; ++i)
2569 if (ext_modules[i].used) {
2570 dep->dep = ext_modules[i].addr;
2571 obj_symbol_patch(f, sec->idx,
2572 (char *) &dep->ref - sec->contents, tm);
2578 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2583 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2586 /* We don't want to export symbols residing in sections that
2587 aren't loaded. There are a number of these created so that
2588 we make sure certain module options don't appear twice. */
2590 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2592 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2594 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2595 struct obj_symbol *sym;
2596 for (sym = f->symtab[i]; sym; sym = sym->next)
2597 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2598 && sym->secidx <= SHN_HIRESERVE
2599 && (sym->secidx >= SHN_LORESERVE
2600 || loaded[sym->secidx])) {
2601 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2603 obj_symbol_patch(f, sec->idx, ofs, sym);
2604 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2611 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2619 new_init_module(const char *m_name, struct obj_file *f,
2620 unsigned long m_size)
2622 struct new_module *module;
2623 struct obj_section *sec;
2628 sec = obj_find_section(f, ".this");
2629 if (!sec || !sec->contents) {
2630 perror_msg_and_die("corrupt module %s?",m_name);
2632 module = (struct new_module *) sec->contents;
2633 m_addr = sec->header.sh_addr;
2635 module->size_of_struct = sizeof(*module);
2636 module->size = m_size;
2637 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2639 sec = obj_find_section(f, "__ksymtab");
2640 if (sec && sec->header.sh_size) {
2641 module->syms = sec->header.sh_addr;
2642 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2645 if (n_ext_modules_used) {
2646 sec = obj_find_section(f, ".kmodtab");
2647 module->deps = sec->header.sh_addr;
2648 module->ndeps = n_ext_modules_used;
2652 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2654 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2656 sec = obj_find_section(f, "__ex_table");
2658 module->ex_table_start = sec->header.sh_addr;
2659 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2662 sec = obj_find_section(f, ".text.init");
2664 module->runsize = sec->header.sh_addr - m_addr;
2666 sec = obj_find_section(f, ".data.init");
2668 if (!module->runsize ||
2669 module->runsize > sec->header.sh_addr - m_addr)
2670 module->runsize = sec->header.sh_addr - m_addr;
2672 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2673 if (sec && sec->header.sh_size) {
2674 module->archdata_start = (void*)sec->header.sh_addr;
2675 module->archdata_end = module->archdata_start + sec->header.sh_size;
2677 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2678 if (sec && sec->header.sh_size) {
2679 module->kallsyms_start = (void*)sec->header.sh_addr;
2680 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2683 if (!arch_init_module(f, module))
2686 /* Whew! All of the initialization is complete. Collect the final
2687 module image and give it to the kernel. */
2689 image = xmalloc(m_size);
2690 obj_create_image(f, image);
2692 ret = new_sys_init_module(m_name, (struct new_module *) image);
2694 perror_msg("init_module: %s", m_name);
2703 #define new_init_module(x, y, z) TRUE
2704 #define new_create_this_module(x, y) 0
2705 #define new_create_module_ksymtab(x)
2706 #define query_module(v, w, x, y, z) -1
2708 #endif /* CONFIG_FEATURE_NEW_MODULE_INTERFACE */
2711 /*======================================================================*/
2714 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2717 struct obj_string_patch *p;
2718 struct obj_section *strsec;
2719 size_t len = strlen(string) + 1;
2722 p = xmalloc(sizeof(*p));
2723 p->next = f->string_patches;
2724 p->reloc_secidx = secidx;
2725 p->reloc_offset = offset;
2726 f->string_patches = p;
2728 strsec = obj_find_section(f, ".kstrtab");
2729 if (strsec == NULL) {
2730 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2731 p->string_offset = 0;
2732 loc = strsec->contents;
2734 p->string_offset = strsec->header.sh_size;
2735 loc = obj_extend_section(strsec, len);
2737 memcpy(loc, string, len);
2742 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2744 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2745 struct obj_symbol *sym)
2747 struct obj_symbol_patch *p;
2749 p = xmalloc(sizeof(*p));
2750 p->next = f->symbol_patches;
2751 p->reloc_secidx = secidx;
2752 p->reloc_offset = offset;
2754 f->symbol_patches = p;
2760 static int obj_check_undefineds(struct obj_file *f)
2765 for (i = 0; i < HASH_BUCKETS; ++i) {
2766 struct obj_symbol *sym;
2767 for (sym = f->symtab[i]; sym; sym = sym->next)
2768 if (sym->secidx == SHN_UNDEF) {
2769 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2770 sym->secidx = SHN_ABS;
2773 error_msg("unresolved symbol %s", sym->name);
2782 static void obj_allocate_commons(struct obj_file *f)
2784 struct common_entry {
2785 struct common_entry *next;
2786 struct obj_symbol *sym;
2787 } *common_head = NULL;
2791 for (i = 0; i < HASH_BUCKETS; ++i) {
2792 struct obj_symbol *sym;
2793 for (sym = f->symtab[i]; sym; sym = sym->next)
2794 if (sym->secidx == SHN_COMMON) {
2795 /* Collect all COMMON symbols and sort them by size so as to
2796 minimize space wasted by alignment requirements. */
2798 struct common_entry **p, *n;
2799 for (p = &common_head; *p; p = &(*p)->next)
2800 if (sym->size <= (*p)->sym->size)
2803 n = alloca(sizeof(*n));
2811 for (i = 1; i < f->local_symtab_size; ++i) {
2812 struct obj_symbol *sym = f->local_symtab[i];
2813 if (sym && sym->secidx == SHN_COMMON) {
2814 struct common_entry **p, *n;
2815 for (p = &common_head; *p; p = &(*p)->next)
2816 if (sym == (*p)->sym)
2818 else if (sym->size < (*p)->sym->size) {
2819 n = alloca(sizeof(*n));
2829 /* Find the bss section. */
2830 for (i = 0; i < f->header.e_shnum; ++i)
2831 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2834 /* If for some reason there hadn't been one, create one. */
2835 if (i == f->header.e_shnum) {
2836 struct obj_section *sec;
2838 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2839 f->sections[i] = sec = arch_new_section();
2840 f->header.e_shnum = i + 1;
2842 memset(sec, 0, sizeof(*sec));
2843 sec->header.sh_type = SHT_PROGBITS;
2844 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2849 /* Allocate the COMMONS. */
2851 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2852 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2853 struct common_entry *c;
2855 for (c = common_head; c; c = c->next) {
2856 ElfW(Addr) align = c->sym->value;
2858 if (align > max_align)
2860 if (bss_size & (align - 1))
2861 bss_size = (bss_size | (align - 1)) + 1;
2864 c->sym->value = bss_size;
2866 bss_size += c->sym->size;
2869 f->sections[i]->header.sh_size = bss_size;
2870 f->sections[i]->header.sh_addralign = max_align;
2874 /* For the sake of patch relocation and parameter initialization,
2875 allocate zeroed data for NOBITS sections now. Note that after
2876 this we cannot assume NOBITS are really empty. */
2877 for (i = 0; i < f->header.e_shnum; ++i) {
2878 struct obj_section *s = f->sections[i];
2879 if (s->header.sh_type == SHT_NOBITS) {
2880 if (s->header.sh_size != 0)
2881 s->contents = memset(xmalloc(s->header.sh_size),
2882 0, s->header.sh_size);
2886 s->header.sh_type = SHT_PROGBITS;
2891 static unsigned long obj_load_size(struct obj_file *f)
2893 unsigned long dot = 0;
2894 struct obj_section *sec;
2896 /* Finalize the positions of the sections relative to one another. */
2898 for (sec = f->load_order; sec; sec = sec->load_next) {
2901 align = sec->header.sh_addralign;
2902 if (align && (dot & (align - 1)))
2903 dot = (dot | (align - 1)) + 1;
2905 sec->header.sh_addr = dot;
2906 dot += sec->header.sh_size;
2912 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2914 int i, n = f->header.e_shnum;
2917 /* Finalize the addresses of the sections. */
2920 for (i = 0; i < n; ++i)
2921 f->sections[i]->header.sh_addr += base;
2923 /* And iterate over all of the relocations. */
2925 for (i = 0; i < n; ++i) {
2926 struct obj_section *relsec, *symsec, *targsec, *strsec;
2927 ElfW(RelM) * rel, *relend;
2931 relsec = f->sections[i];
2932 if (relsec->header.sh_type != SHT_RELM)
2935 symsec = f->sections[relsec->header.sh_link];
2936 targsec = f->sections[relsec->header.sh_info];
2937 strsec = f->sections[symsec->header.sh_link];
2939 rel = (ElfW(RelM) *) relsec->contents;
2940 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2941 symtab = (ElfW(Sym) *) symsec->contents;
2942 strtab = (const char *) strsec->contents;
2944 for (; rel < relend; ++rel) {
2945 ElfW(Addr) value = 0;
2946 struct obj_symbol *intsym = NULL;
2947 unsigned long symndx;
2948 ElfW(Sym) * extsym = 0;
2951 /* Attempt to find a value to use for this relocation. */
2953 symndx = ELFW(R_SYM) (rel->r_info);
2955 /* Note we've already checked for undefined symbols. */
2957 extsym = &symtab[symndx];
2958 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2959 /* Local symbols we look up in the local table to be sure
2960 we get the one that is really intended. */
2961 intsym = f->local_symtab[symndx];
2963 /* Others we look up in the hash table. */
2965 if (extsym->st_name)
2966 name = strtab + extsym->st_name;
2968 name = f->sections[extsym->st_shndx]->name;
2969 intsym = obj_find_symbol(f, name);
2972 value = obj_symbol_final_value(f, intsym);
2973 intsym->referenced = 1;
2975 #if SHT_RELM == SHT_RELA
2976 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2977 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2978 if (!extsym || !extsym->st_name ||
2979 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2981 value += rel->r_addend;
2985 switch (arch_apply_relocation
2986 (f, targsec, symsec, intsym, rel, value)) {
2990 case obj_reloc_overflow:
2991 errmsg = "Relocation overflow";
2993 case obj_reloc_dangerous:
2994 errmsg = "Dangerous relocation";
2996 case obj_reloc_unhandled:
2997 errmsg = "Unhandled relocation";
3000 error_msg("%s of type %ld for %s", errmsg,
3001 (long) ELFW(R_TYPE) (rel->r_info),
3002 strtab + extsym->st_name);
3004 error_msg("%s of type %ld", errmsg,
3005 (long) ELFW(R_TYPE) (rel->r_info));
3013 /* Finally, take care of the patches. */
3015 if (f->string_patches) {
3016 struct obj_string_patch *p;
3017 struct obj_section *strsec;
3018 ElfW(Addr) strsec_base;
3019 strsec = obj_find_section(f, ".kstrtab");
3020 strsec_base = strsec->header.sh_addr;
3022 for (p = f->string_patches; p; p = p->next) {
3023 struct obj_section *targsec = f->sections[p->reloc_secidx];
3024 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3025 = strsec_base + p->string_offset;
3029 if (f->symbol_patches) {
3030 struct obj_symbol_patch *p;
3032 for (p = f->symbol_patches; p; p = p->next) {
3033 struct obj_section *targsec = f->sections[p->reloc_secidx];
3034 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3035 = obj_symbol_final_value(f, p->sym);
3042 static int obj_create_image(struct obj_file *f, char *image)
3044 struct obj_section *sec;
3045 ElfW(Addr) base = f->baseaddr;
3047 for (sec = f->load_order; sec; sec = sec->load_next) {
3050 if (sec->contents == 0 || sec->header.sh_size == 0)
3053 secimg = image + (sec->header.sh_addr - base);
3055 /* Note that we allocated data for NOBITS sections earlier. */
3056 memcpy(secimg, sec->contents, sec->header.sh_size);
3062 /*======================================================================*/
3064 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3067 ElfW(Shdr) * section_headers;
3071 /* Read the file header. */
3073 f = arch_new_file();
3074 memset(f, 0, sizeof(*f));
3075 f->symbol_cmp = strcmp;
3076 f->symbol_hash = obj_elf_hash;
3077 f->load_order_search_start = &f->load_order;
3079 fseek(fp, 0, SEEK_SET);
3080 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3081 perror_msg("error reading ELF header");
3085 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3086 || f->header.e_ident[EI_MAG1] != ELFMAG1
3087 || f->header.e_ident[EI_MAG2] != ELFMAG2
3088 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3089 error_msg("not an ELF file");
3092 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3093 || f->header.e_ident[EI_DATA] != ELFDATAM
3094 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3095 || !MATCH_MACHINE(f->header.e_machine)) {
3096 error_msg("ELF file not for this architecture");
3099 if (f->header.e_type != ET_REL) {
3100 error_msg("ELF file not a relocatable object");
3104 /* Read the section headers. */
3106 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3107 error_msg("section header size mismatch: %lu != %lu",
3108 (unsigned long) f->header.e_shentsize,
3109 (unsigned long) sizeof(ElfW(Shdr)));
3113 shnum = f->header.e_shnum;
3114 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3115 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3117 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3118 fseek(fp, f->header.e_shoff, SEEK_SET);
3119 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3120 perror_msg("error reading ELF section headers");
3124 /* Read the section data. */
3126 for (i = 0; i < shnum; ++i) {
3127 struct obj_section *sec;
3129 f->sections[i] = sec = arch_new_section();
3130 memset(sec, 0, sizeof(*sec));
3132 sec->header = section_headers[i];
3135 if(sec->header.sh_size) switch (sec->header.sh_type) {
3144 if (!loadprogbits) {
3145 sec->contents = NULL;
3152 if (sec->header.sh_size > 0) {
3153 sec->contents = xmalloc(sec->header.sh_size);
3154 fseek(fp, sec->header.sh_offset, SEEK_SET);
3155 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3156 perror_msg("error reading ELF section data");
3160 sec->contents = NULL;
3164 #if SHT_RELM == SHT_REL
3166 error_msg("RELA relocations not supported on this architecture");
3170 error_msg("REL relocations not supported on this architecture");
3175 if (sec->header.sh_type >= SHT_LOPROC) {
3176 /* Assume processor specific section types are debug
3177 info and can safely be ignored. If this is ever not
3178 the case (Hello MIPS?), don't put ifdefs here but
3179 create an arch_load_proc_section(). */
3183 error_msg("can't handle sections of type %ld",
3184 (long) sec->header.sh_type);
3189 /* Do what sort of interpretation as needed by each section. */
3191 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3193 for (i = 0; i < shnum; ++i) {
3194 struct obj_section *sec = f->sections[i];
3195 sec->name = shstrtab + sec->header.sh_name;
3198 for (i = 0; i < shnum; ++i) {
3199 struct obj_section *sec = f->sections[i];
3201 /* .modinfo should be contents only but gcc has no attribute for that.
3202 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3204 if (strcmp(sec->name, ".modinfo") == 0)
3205 sec->header.sh_flags &= ~SHF_ALLOC;
3207 if (sec->header.sh_flags & SHF_ALLOC)
3208 obj_insert_section_load_order(f, sec);
3210 switch (sec->header.sh_type) {
3213 unsigned long nsym, j;
3217 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3218 error_msg("symbol size mismatch: %lu != %lu",
3219 (unsigned long) sec->header.sh_entsize,
3220 (unsigned long) sizeof(ElfW(Sym)));
3224 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3225 strtab = f->sections[sec->header.sh_link]->contents;
3226 sym = (ElfW(Sym) *) sec->contents;
3228 /* Allocate space for a table of local symbols. */
3229 j = f->local_symtab_size = sec->header.sh_info;
3230 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3232 /* Insert all symbols into the hash table. */
3233 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3236 name = strtab + sym->st_name;
3238 name = f->sections[sym->st_shndx]->name;
3240 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3241 sym->st_value, sym->st_size);
3247 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3248 error_msg("relocation entry size mismatch: %lu != %lu",
3249 (unsigned long) sec->header.sh_entsize,
3250 (unsigned long) sizeof(ElfW(RelM)));
3254 /* XXX Relocation code from modutils-2.3.19 is not here.
3255 * Why? That's about 20 lines of code from obj/obj_load.c,
3256 * which gets done in a second pass through the sections.
3257 * This BusyBox insmod does similar work in obj_relocate(). */
3264 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3266 * load the unloaded sections directly into the memory allocated by
3267 * kernel for the module
3270 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3272 ElfW(Addr) base = f->baseaddr;
3273 struct obj_section* sec;
3275 for (sec = f->load_order; sec; sec = sec->load_next) {
3277 /* section already loaded? */
3278 if (sec->contents != NULL)
3281 if (sec->header.sh_size == 0)
3284 sec->contents = imagebase + (sec->header.sh_addr - base);
3285 fseek(fp, sec->header.sh_offset, SEEK_SET);
3286 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3287 error_msg("error reading ELF section data: %s\n", strerror(errno));
3296 static void hide_special_symbols(struct obj_file *f)
3298 static const char *const specials[] = {
3299 SPFX "cleanup_module",
3301 SPFX "kernel_version",
3305 struct obj_symbol *sym;
3306 const char *const *p;
3308 for (p = specials; *p; ++p)
3309 if ((sym = obj_find_symbol(f, *p)) != NULL)
3311 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3316 extern int insmod_main( int argc, char **argv)
3323 unsigned long m_size;
3328 char m_name[FILENAME_MAX + 1] = "\0";
3329 int exit_status = EXIT_FAILURE;
3331 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3332 struct utsname uts_info;
3333 char m_strversion[STRVERSIONLEN];
3338 /* Parse any options */
3339 while ((opt = getopt(argc, argv, "fksvxLo:")) > 0) {
3341 case 'f': /* force loading */
3342 flag_force_load = 1;
3344 case 'k': /* module loaded by kerneld, auto-cleanable */
3347 case 's': /* log to syslog */
3348 /* log to syslog -- not supported */
3349 /* but kernel needs this for request_module(), */
3350 /* as this calls: modprobe -k -s -- <module> */
3351 /* so silently ignore this flag */
3353 case 'v': /* verbose output */
3356 case 'x': /* do not export externs */
3359 case 'o': /* name the output module */
3360 strncpy(m_name, optarg, FILENAME_MAX);
3362 case 'L': /* Stub warning */
3363 /* This is needed for compatibility with modprobe.
3364 * In theory, this does locking, but we don't do
3365 * that. So be careful and plan your life around not
3366 * loading the same module 50 times concurrently. */
3373 if (argv[optind] == NULL) {
3377 /* Grab the module name */
3378 if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3385 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3387 memcpy(m_fullName, tmp, len);
3388 m_fullName[len]='\0';
3389 if (*m_name == '\0') {
3390 strcpy(m_name, m_fullName);
3392 strcat(m_fullName, ".o");
3394 /* Get a filedesc for the module. Check we we have a complete path */
3395 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3396 (fp = fopen(argv[optind], "r")) == NULL) {
3397 struct utsname myuname;
3399 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3400 * but do not error out yet if we fail to find it... */
3401 if (uname(&myuname) == 0) {
3402 char module_dir[FILENAME_MAX];
3403 char real_module_dir[FILENAME_MAX];
3404 snprintf (module_dir, sizeof(module_dir), "%s/%s",
3405 _PATH_MODULES, myuname.release);
3406 /* Jump through hoops in case /lib/modules/`uname -r`
3407 * is a symlink. We do not want recursive_action to
3408 * follow symlinks, but we do want to follow the
3409 * /lib/modules/`uname -r` dir, So resolve it ourselves
3410 * if it is a link... */
3411 if (realpath (module_dir, real_module_dir) == NULL)
3412 strcpy(real_module_dir, module_dir);
3413 recursive_action(real_module_dir, TRUE, FALSE, FALSE,
3414 check_module_name_match, 0, m_fullName);
3417 /* Check if we have found anything yet */
3418 if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL))
3420 char module_dir[FILENAME_MAX];
3421 if (realpath (_PATH_MODULES, module_dir) == NULL)
3422 strcpy(module_dir, _PATH_MODULES);
3423 /* No module found under /lib/modules/`uname -r`, this
3424 * time cast the net a bit wider. Search /lib/modules/ */
3425 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3426 check_module_name_match, 0, m_fullName))
3428 if (m_filename[0] == '\0'
3429 || ((fp = fopen(m_filename, "r")) == NULL))
3431 error_msg("%s: no module by that name found", m_fullName);
3432 return EXIT_FAILURE;
3435 error_msg_and_die("%s: no module by that name found", m_fullName);
3438 safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
3440 printf("Using %s\n", m_filename);
3442 if ((f = obj_load(fp, LOADBITS)) == NULL)
3443 perror_msg_and_die("Could not load the module");
3445 if (get_modinfo_value(f, "kernel_version") == NULL)
3450 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3451 /* Version correspondence? */
3453 if (uname(&uts_info) < 0)
3454 uts_info.release[0] = '\0';
3455 if (m_has_modinfo) {
3456 m_version = new_get_module_version(f, m_strversion);
3458 m_version = old_get_module_version(f, m_strversion);
3459 if (m_version == -1) {
3460 error_msg("couldn't find the kernel version the module was "
3466 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3467 if (flag_force_load) {
3468 error_msg("Warning: kernel-module version mismatch\n"
3469 "\t%s was compiled for kernel version %s\n"
3470 "\twhile this kernel is version %s",
3471 m_filename, m_strversion, uts_info.release);
3473 error_msg("kernel-module version mismatch\n"
3474 "\t%s was compiled for kernel version %s\n"
3475 "\twhile this kernel is version %s.",
3476 m_filename, m_strversion, uts_info.release);
3481 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3483 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3485 if (k_new_syscalls) {
3486 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
3487 if (!new_get_kernel_symbols())
3489 k_crcs = new_is_kernel_checksummed();
3491 error_msg("Not configured to support new kernels");
3495 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
3496 if (!old_get_kernel_symbols(m_name))
3498 k_crcs = old_is_kernel_checksummed();
3500 error_msg("Not configured to support old kernels");
3505 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3507 m_crcs = new_is_module_checksummed(f);
3509 m_crcs = old_is_module_checksummed(f);
3511 if (m_crcs != k_crcs)
3512 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3513 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3515 /* Let the module know about the kernel symbols. */
3516 add_kernel_symbols(f);
3518 /* Allocate common symbols, symbol tables, and string tables. */
3521 ? !new_create_this_module(f, m_name)
3522 : !old_create_mod_use_count(f))
3527 if (!obj_check_undefineds(f)) {
3530 obj_allocate_commons(f);
3532 /* done with the module name, on to the optional var=value arguments */
3535 if (optind < argc) {
3537 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3538 : !old_process_module_arguments(f, argc - optind, argv + optind))
3545 hide_special_symbols(f);
3548 new_create_module_ksymtab(f);
3550 /* Find current size of the module */
3551 m_size = obj_load_size(f);
3554 m_addr = create_module(m_name, m_size);
3555 if (m_addr==-1) switch (errno) {
3557 error_msg("A module named %s already exists", m_name);
3560 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3564 perror_msg("create_module: %s", m_name);
3570 * the PROGBITS section was not loaded by the obj_load
3571 * now we can load them directly into the kernel memory
3573 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3574 delete_module(m_name);
3579 if (!obj_relocate(f, m_addr)) {
3580 delete_module(m_name);
3585 ? !new_init_module(m_name, f, m_size)
3586 : !old_init_module(m_name, f, m_size))
3588 delete_module(m_name);
3592 exit_status = EXIT_SUCCESS;
3596 return(exit_status);