1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * This version of insmod supports x86, ARM, SH3/4/5, powerpc, m68k,
8 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
9 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.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 * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
23 * Integrated support for sh64 (SH-5), from preliminary modutils
24 * patches from Benedict Gaster <benedict.gaster@superh.com>.
25 * Currently limited to support for 32bit ABI.
27 * Magnus Damm <damm@opensource.se> 22-May-2002.
28 * The plt and got code are now using the same structs.
29 * Added generic linked list code to fully support PowerPC.
30 * Replaced the mess in arch_apply_relocation() with architecture blocks.
31 * The arch_create_got() function got cleaned up with architecture blocks.
32 * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
34 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
35 * PowerPC specific code stolen from modutils-2.3.16,
36 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
37 * I've only tested the code on mpc8xx platforms in big-endian mode.
38 * Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
40 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
41 * based on modutils-2.4.2
42 * MIPS specific support for Elf loading and relocation.
43 * Copyright 1996, 1997 Linux International.
44 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
46 * Based almost entirely on the Linux modutils-2.3.11 implementation.
47 * Copyright 1996, 1997 Linux International.
48 * New implementation contributed by Richard Henderson <rth@tamu.edu>
49 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
50 * Restructured (and partly rewritten) by:
51 * Björn Ekwall <bj0rn@blox.se> February 1999
53 * This program is free software; you can redistribute it and/or modify
54 * it under the terms of the GNU General Public License as published by
55 * the Free Software Foundation; either version 2 of the License, or
56 * (at your option) any later version.
58 * This program is distributed in the hope that it will be useful,
59 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
61 * General Public License for more details.
63 * You should have received a copy of the GNU General Public License
64 * along with this program; if not, write to the Free Software
65 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
80 #include <sys/utsname.h>
83 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
84 # undef CONFIG_FEATURE_OLD_MODULE_INTERFACE
85 # define new_sys_init_module init_module
87 # define old_sys_init_module init_module
90 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
98 #define CONFIG_USE_PLT_ENTRIES
99 #define CONFIG_PLT_ENTRY_SIZE 8
100 #define CONFIG_USE_GOT_ENTRIES
101 #define CONFIG_GOT_ENTRY_SIZE 8
102 #define CONFIG_USE_SINGLE
104 #define MATCH_MACHINE(x) (x == EM_ARM)
105 #define SHT_RELM SHT_REL
106 #define Elf32_RelM Elf32_Rel
107 #define ELFCLASSM ELFCLASS32
110 #if defined(__s390__)
111 #define CONFIG_USE_PLT_ENTRIES
112 #define CONFIG_PLT_ENTRY_SIZE 8
113 #define CONFIG_USE_GOT_ENTRIES
114 #define CONFIG_GOT_ENTRY_SIZE 8
115 #define CONFIG_USE_SINGLE
117 #define MATCH_MACHINE(x) (x == EM_S390)
118 #define SHT_RELM SHT_RELA
119 #define Elf32_RelM Elf32_Rela
120 #define ELFCLASSM ELFCLASS32
123 #if defined(__i386__)
124 #define CONFIG_USE_GOT_ENTRIES
125 #define CONFIG_GOT_ENTRY_SIZE 4
126 #define CONFIG_USE_SINGLE
129 #define MATCH_MACHINE(x) (x == EM_386)
131 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
134 #define SHT_RELM SHT_REL
135 #define Elf32_RelM Elf32_Rel
136 #define ELFCLASSM ELFCLASS32
139 #if defined(__mc68000__)
140 #define CONFIG_USE_GOT_ENTRIES
141 #define CONFIG_GOT_ENTRY_SIZE 4
142 #define CONFIG_USE_SINGLE
144 #define MATCH_MACHINE(x) (x == EM_68K)
145 #define SHT_RELM SHT_RELA
146 #define Elf32_RelM Elf32_Rela
149 #if defined(__mips__)
150 /* Account for ELF spec changes. */
151 #ifndef EM_MIPS_RS3_LE
152 #ifdef EM_MIPS_RS4_BE
153 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
155 #define EM_MIPS_RS3_LE 10
157 #endif /* !EM_MIPS_RS3_LE */
159 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
160 #define SHT_RELM SHT_REL
161 #define Elf32_RelM Elf32_Rel
162 #define ELFCLASSM ELFCLASS32
163 #define ARCHDATAM "__dbe_table"
166 #if defined(__powerpc__)
167 #define CONFIG_USE_PLT_ENTRIES
168 #define CONFIG_PLT_ENTRY_SIZE 16
169 #define CONFIG_USE_PLT_LIST
170 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
171 #define CONFIG_USE_LIST
173 #define MATCH_MACHINE(x) (x == EM_PPC)
174 #define SHT_RELM SHT_RELA
175 #define Elf32_RelM Elf32_Rela
176 #define ELFCLASSM ELFCLASS32
177 #define ARCHDATAM "__ftr_fixup"
181 #define CONFIG_USE_GOT_ENTRIES
182 #define CONFIG_GOT_ENTRY_SIZE 4
183 #define CONFIG_USE_SINGLE
185 #define MATCH_MACHINE(x) (x == EM_SH)
186 #define SHT_RELM SHT_RELA
187 #define Elf32_RelM Elf32_Rela
188 #define ELFCLASSM ELFCLASS32
190 /* the SH changes have only been tested in =little endian= mode */
191 /* I'm not sure about big endian, so let's warn: */
193 #if defined(__sh__) && defined(__BIG_ENDIAN__)
194 #error insmod.c may require changes for use on big endian SH
197 /* it may or may not work on the SH1/SH2... So let's error on those
199 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && \
201 #error insmod.c may require changes for SH1 or SH2 use
205 #if defined (__v850e__)
206 #define CONFIG_USE_PLT_ENTRIES
207 #define CONFIG_PLT_ENTRY_SIZE 8
208 #define CONFIG_USE_SINGLE
210 #ifndef EM_CYGNUS_V850 /* grumble */
211 #define EM_CYGNUS_V850 0x9080
214 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
215 #define SHT_RELM SHT_RELA
216 #define Elf32_RelM Elf32_Rela
217 #define ELFCLASSM ELFCLASS32
219 #define SYMBOL_PREFIX "_"
223 #error Sorry, but insmod.c does not yet support this architecture...
227 //----------------------------------------------------------------------------
228 //--------modutils module.h, lines 45-242
229 //----------------------------------------------------------------------------
231 /* Definitions for the Linux module syscall interface.
232 Copyright 1996, 1997 Linux International.
234 Contributed by Richard Henderson <rth@tamu.edu>
236 This file is part of the Linux modutils.
238 This program is free software; you can redistribute it and/or modify it
239 under the terms of the GNU General Public License as published by the
240 Free Software Foundation; either version 2 of the License, or (at your
241 option) any later version.
243 This program is distributed in the hope that it will be useful, but
244 WITHOUT ANY WARRANTY; without even the implied warranty of
245 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
246 General Public License for more details.
248 You should have received a copy of the GNU General Public License
249 along with this program; if not, write to the Free Software Foundation,
250 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
253 #ifndef MODUTILS_MODULE_H
254 static const int MODUTILS_MODULE_H = 1;
256 #ident "$Id: insmod.c,v 1.102 2003/08/31 01:58:18 bug1 Exp $"
258 /* This file contains the structures used by the 2.0 and 2.1 kernels.
259 We do not use the kernel headers directly because we do not wish
260 to be dependant on a particular kernel version to compile insmod. */
263 /*======================================================================*/
264 /* The structures used by Linux 2.0. */
266 /* The symbol format used by get_kernel_syms(2). */
267 struct old_kernel_sym
273 struct old_module_ref
275 unsigned long module; /* kernel addresses */
279 struct old_module_symbol
285 struct old_symbol_table
287 int size; /* total, including string table!!! */
290 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
291 struct old_module_ref ref[0]; /* actual size defined by n_refs */
294 struct old_mod_routines
297 unsigned long cleanup;
303 unsigned long ref; /* the list of modules that refer to me */
304 unsigned long symtab;
306 int size; /* size of module in pages */
307 unsigned long addr; /* address of module */
309 unsigned long cleanup; /* cleanup routine */
312 /* Sent to init_module(2) or'ed into the code size parameter. */
313 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
315 int get_kernel_syms(struct old_kernel_sym *);
316 int old_sys_init_module(const char *name, char *code, unsigned codesize,
317 struct old_mod_routines *, struct old_symbol_table *);
319 /*======================================================================*/
320 /* For sizeof() which are related to the module platform and not to the
321 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
323 #define tgt_sizeof_char sizeof(char)
324 #define tgt_sizeof_short sizeof(short)
325 #define tgt_sizeof_int sizeof(int)
326 #define tgt_sizeof_long sizeof(long)
327 #define tgt_sizeof_char_p sizeof(char *)
328 #define tgt_sizeof_void_p sizeof(void *)
329 #define tgt_long long
331 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
332 #undef tgt_sizeof_long
333 #undef tgt_sizeof_char_p
334 #undef tgt_sizeof_void_p
336 static const int tgt_sizeof_long = 8;
337 static const int tgt_sizeof_char_p = 8;
338 static const int tgt_sizeof_void_p = 8;
339 #define tgt_long long long
342 /*======================================================================*/
343 /* The structures used in Linux 2.1. */
345 /* Note: new_module_symbol does not use tgt_long intentionally */
346 struct new_module_symbol
352 struct new_module_persist;
354 struct new_module_ref
356 unsigned tgt_long dep; /* kernel addresses */
357 unsigned tgt_long ref;
358 unsigned tgt_long next_ref;
363 unsigned tgt_long size_of_struct; /* == sizeof(module) */
364 unsigned tgt_long next;
365 unsigned tgt_long name;
366 unsigned tgt_long size;
369 unsigned tgt_long flags; /* AUTOCLEAN et al */
374 unsigned tgt_long syms;
375 unsigned tgt_long deps;
376 unsigned tgt_long refs;
377 unsigned tgt_long init;
378 unsigned tgt_long cleanup;
379 unsigned tgt_long ex_table_start;
380 unsigned tgt_long ex_table_end;
382 unsigned tgt_long gp;
384 /* Everything after here is extension. */
385 unsigned tgt_long persist_start;
386 unsigned tgt_long persist_end;
387 unsigned tgt_long can_unload;
388 unsigned tgt_long runsize;
389 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
390 const char *kallsyms_start; /* All symbols for kernel debugging */
391 const char *kallsyms_end;
392 const char *archdata_start; /* arch specific data for module */
393 const char *archdata_end;
394 const char *kernel_data; /* Reserved for kernel internal use */
399 #define ARCHDATA_SEC_NAME ARCHDATAM
401 #define ARCHDATA_SEC_NAME "__archdata"
403 #define KALLSYMS_SEC_NAME "__kallsyms"
406 struct new_module_info
414 /* Bits of module.flags. */
415 static const int NEW_MOD_RUNNING = 1;
416 static const int NEW_MOD_DELETED = 2;
417 static const int NEW_MOD_AUTOCLEAN = 4;
418 static const int NEW_MOD_VISITED = 8;
419 static const int NEW_MOD_USED_ONCE = 16;
421 int new_sys_init_module(const char *name, const struct new_module *);
422 int query_module(const char *name, int which, void *buf, size_t bufsize,
425 /* Values for query_module's which. */
427 static const int QM_MODULES = 1;
428 static const int QM_DEPS = 2;
429 static const int QM_REFS = 3;
430 static const int QM_SYMBOLS = 4;
431 static const int QM_INFO = 5;
433 /*======================================================================*/
434 /* The system calls unchanged between 2.0 and 2.1. */
436 unsigned long create_module(const char *, size_t);
437 int delete_module(const char *);
440 #endif /* module.h */
442 //----------------------------------------------------------------------------
443 //--------end of modutils module.h
444 //----------------------------------------------------------------------------
448 //----------------------------------------------------------------------------
449 //--------modutils obj.h, lines 253-462
450 //----------------------------------------------------------------------------
452 /* Elf object file loading and relocation routines.
453 Copyright 1996, 1997 Linux International.
455 Contributed by Richard Henderson <rth@tamu.edu>
457 This file is part of the Linux modutils.
459 This program is free software; you can redistribute it and/or modify it
460 under the terms of the GNU General Public License as published by the
461 Free Software Foundation; either version 2 of the License, or (at your
462 option) any later version.
464 This program is distributed in the hope that it will be useful, but
465 WITHOUT ANY WARRANTY; without even the implied warranty of
466 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
467 General Public License for more details.
469 You should have received a copy of the GNU General Public License
470 along with this program; if not, write to the Free Software Foundation,
471 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
474 #ifndef MODUTILS_OBJ_H
475 static const int MODUTILS_OBJ_H = 1;
477 #ident "$Id: insmod.c,v 1.102 2003/08/31 01:58:18 bug1 Exp $"
479 /* The relocatable object is manipulated using elfin types. */
485 #if __BYTE_ORDER == __LITTLE_ENDIAN
486 #define ELFDATAM ELFDATA2LSB
487 #elif __BYTE_ORDER == __BIG_ENDIAN
488 #define ELFDATAM ELFDATA2MSB
492 # if ELFCLASSM == ELFCLASS32
493 # define ElfW(x) Elf32_ ## x
494 # define ELFW(x) ELF32_ ## x
496 # define ElfW(x) Elf64_ ## x
497 # define ELFW(x) ELF64_ ## x
501 /* For some reason this is missing from some ancient C libraries.... */
502 #ifndef ELF32_ST_INFO
503 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
506 #ifndef ELF64_ST_INFO
507 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
510 struct obj_string_patch;
511 struct obj_symbol_patch;
518 struct obj_section *load_next;
524 struct obj_symbol *next; /* hash table link */
528 int secidx; /* the defining section index/module */
530 int ksymidx; /* for export to the kernel symtab */
531 int referenced; /* actually used in the link */
534 /* Hardcode the hash table size. We shouldn't be needing so many
535 symbols that we begin to degrade performance, and we get a big win
536 by giving the compiler a constant divisor. */
538 #define HASH_BUCKETS 521
544 struct obj_section **sections;
545 struct obj_section *load_order;
546 struct obj_section **load_order_search_start;
547 struct obj_string_patch *string_patches;
548 struct obj_symbol_patch *symbol_patches;
549 int (*symbol_cmp)(const char *, const char *);
550 unsigned long (*symbol_hash)(const char *);
551 unsigned long local_symtab_size;
552 struct obj_symbol **local_symtab;
553 struct obj_symbol *symtab[HASH_BUCKETS];
564 struct obj_string_patch
566 struct obj_string_patch *next;
568 ElfW(Addr) reloc_offset;
569 ElfW(Addr) string_offset;
572 struct obj_symbol_patch
574 struct obj_symbol_patch *next;
576 ElfW(Addr) reloc_offset;
577 struct obj_symbol *sym;
581 /* Generic object manipulation routines. */
583 static unsigned long obj_elf_hash(const char *);
585 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
587 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
590 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
591 struct obj_symbol *sym);
593 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
594 static void obj_set_symbol_compare(struct obj_file *f,
595 int (*cmp)(const char *, const char *),
596 unsigned long (*hash)(const char *));
599 static struct obj_section *obj_find_section (struct obj_file *f,
602 static void obj_insert_section_load_order (struct obj_file *f,
603 struct obj_section *sec);
605 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
610 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
615 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
617 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
620 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
621 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
622 struct obj_symbol *sym);
625 static int obj_check_undefineds(struct obj_file *f);
627 static void obj_allocate_commons(struct obj_file *f);
629 static unsigned long obj_load_size (struct obj_file *f);
631 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
633 static struct obj_file *obj_load(FILE *f, int loadprogbits);
635 static int obj_create_image (struct obj_file *f, char *image);
637 /* Architecture specific manipulation routines. */
639 static struct obj_file *arch_new_file (void);
641 static struct obj_section *arch_new_section (void);
643 static struct obj_symbol *arch_new_symbol (void);
645 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
646 struct obj_section *targsec,
647 struct obj_section *symsec,
648 struct obj_symbol *sym,
649 ElfW(RelM) *rel, ElfW(Addr) value);
651 static void arch_create_got (struct obj_file *f);
653 static int obj_gpl_license(struct obj_file *f, const char **license);
655 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
656 static int arch_init_module (struct obj_file *f, struct new_module *);
660 //----------------------------------------------------------------------------
661 //--------end of modutils obj.h
662 //----------------------------------------------------------------------------
665 /* SPFX is always a string, so it can be concatenated to string constants. */
667 #define SPFX SYMBOL_PREFIX
673 #define _PATH_MODULES "/lib/modules"
674 static const int STRVERSIONLEN = 32;
676 /*======================================================================*/
678 static int flag_force_load = 0;
679 static int flag_autoclean = 0;
680 static int flag_verbose = 0;
681 static int flag_quiet = 0;
682 static int flag_export = 1;
685 /*======================================================================*/
687 #if defined(CONFIG_USE_LIST)
689 struct arch_list_entry
691 struct arch_list_entry *next;
692 CONFIG_LIST_ARCHTYPE addend;
699 #if defined(CONFIG_USE_SINGLE)
701 struct arch_single_entry
710 #if defined(__mips__)
713 struct mips_hi16 *next;
720 struct obj_file root;
721 #if defined(CONFIG_USE_PLT_ENTRIES)
722 struct obj_section *plt;
724 #if defined(CONFIG_USE_GOT_ENTRIES)
725 struct obj_section *got;
727 #if defined(__mips__)
728 struct mips_hi16 *mips_hi16_list;
733 struct obj_symbol root;
734 #if defined(CONFIG_USE_PLT_ENTRIES)
735 #if defined(CONFIG_USE_PLT_LIST)
736 struct arch_list_entry *pltent;
738 struct arch_single_entry pltent;
741 #if defined(CONFIG_USE_GOT_ENTRIES)
742 struct arch_single_entry gotent;
747 struct external_module {
752 struct new_module_symbol *syms;
755 static struct new_module_symbol *ksyms;
756 static size_t nksyms;
758 static struct external_module *ext_modules;
759 static int n_ext_modules;
760 static int n_ext_modules_used;
761 extern int delete_module(const char *);
763 static char *m_filename;
764 static char *m_fullName;
768 /*======================================================================*/
771 static int check_module_name_match(const char *filename, struct stat *statbuf,
774 char *fullname = (char *) userdata;
776 if (fullname[0] == '\0')
779 char *tmp, *tmp1 = bb_xstrdup(filename);
780 tmp = bb_get_last_path_component(tmp1);
781 if (strcmp(tmp, fullname) == 0) {
783 /* Stop searching if we find a match */
784 m_filename = bb_xstrdup(filename);
793 /*======================================================================*/
795 static struct obj_file *arch_new_file(void)
798 f = xmalloc(sizeof(*f));
800 memset(f, 0, sizeof(*f));
805 static struct obj_section *arch_new_section(void)
807 return xmalloc(sizeof(struct obj_section));
810 static struct obj_symbol *arch_new_symbol(void)
812 struct arch_symbol *sym;
813 sym = xmalloc(sizeof(*sym));
815 memset(sym, 0, sizeof(*sym));
820 static enum obj_reloc
821 arch_apply_relocation(struct obj_file *f,
822 struct obj_section *targsec,
823 struct obj_section *symsec,
824 struct obj_symbol *sym,
825 ElfW(RelM) *rel, ElfW(Addr) v)
827 struct arch_file *ifile = (struct arch_file *) f;
828 enum obj_reloc ret = obj_reloc_ok;
829 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
830 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
831 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
832 struct arch_symbol *isym = (struct arch_symbol *) sym;
834 #if defined(CONFIG_USE_GOT_ENTRIES)
835 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
837 #if defined(CONFIG_USE_PLT_ENTRIES)
838 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
840 #if defined(CONFIG_USE_PLT_LIST)
841 struct arch_list_entry *pe;
843 struct arch_single_entry *pe;
847 switch (ELF32_R_TYPE(rel->r_info)) {
862 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
863 * (which is .got) similar to branch,
864 * but is full 32 bits relative */
874 case R_ARM_GOTOFF: /* address relative to the got */
879 #elif defined(__s390__)
881 *(unsigned int *) loc += v;
884 *(unsigned short *) loc += v;
887 *(unsigned char *) loc += v;
891 *(unsigned int *) loc += v - dot;
894 *(unsigned short *) loc += (v - dot) >> 1;
897 *(unsigned short *) loc += v - dot;
902 /* find the plt entry and initialize it. */
903 assert(isym != NULL);
904 pe = (struct arch_single_entry *) &isym->pltent;
905 assert(pe->allocated);
906 if (pe->inited == 0) {
907 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
908 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
910 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
917 /* Insert relative distance to target. */
918 v = plt + pe->offset - dot;
919 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
920 *(unsigned int *) loc = (unsigned int) v;
921 else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
922 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
936 *(unsigned long *) loc += got - dot;
942 assert(isym != NULL);
944 if (!isym->gotent.inited)
946 isym->gotent.inited = 1;
947 *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
949 if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
950 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
951 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
952 *(unsigned short *) loc += isym->gotent.offset;
953 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
954 *(unsigned int *) loc += isym->gotent.offset;
962 #elif defined(__i386__)
998 #elif defined(__mc68000__)
1009 ret = obj_reloc_overflow;
1016 ret = obj_reloc_overflow;
1023 if ((Elf32_Sword)v > 0x7f ||
1024 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
1025 ret = obj_reloc_overflow;
1032 if ((Elf32_Sword)v > 0x7fff ||
1033 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1034 ret = obj_reloc_overflow;
1040 *(int *)loc = v - dot;
1043 case R_68K_GLOB_DAT:
1044 case R_68K_JMP_SLOT:
1048 case R_68K_RELATIVE:
1049 *(int *)loc += f->baseaddr;
1060 #elif defined(__mips__)
1071 ret = obj_reloc_dangerous;
1072 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1073 ret = obj_reloc_overflow;
1075 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1081 struct mips_hi16 *n;
1083 /* We cannot relocate this one now because we don't know the value
1084 of the carry we need to add. Save the information, and let LO16
1085 do the actual relocation. */
1086 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1089 n->next = ifile->mips_hi16_list;
1090 ifile->mips_hi16_list = n;
1096 unsigned long insnlo = *loc;
1097 Elf32_Addr val, vallo;
1099 /* Sign extend the addend we extract from the lo insn. */
1100 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1102 if (ifile->mips_hi16_list != NULL) {
1103 struct mips_hi16 *l;
1105 l = ifile->mips_hi16_list;
1107 struct mips_hi16 *next;
1110 /* The value for the HI16 had best be the same. */
1111 assert(v == l->value);
1113 /* Do the HI16 relocation. Note that we actually don't
1114 need to know anything about the LO16 itself, except where
1115 to find the low 16 bits of the addend needed by the LO16. */
1118 ((insn & 0xffff) << 16) +
1122 /* Account for the sign extension that will happen in the
1129 insn = (insn & ~0xffff) | val;
1137 ifile->mips_hi16_list = NULL;
1140 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1142 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1147 #elif defined(__powerpc__)
1149 case R_PPC_ADDR16_HA:
1150 *(unsigned short *)loc = (v + 0x8000) >> 16;
1153 case R_PPC_ADDR16_HI:
1154 *(unsigned short *)loc = v >> 16;
1157 case R_PPC_ADDR16_LO:
1158 *(unsigned short *)loc = v;
1172 #elif defined(__sh__)
1195 *loc = f->baseaddr + rel->r_addend;
1200 *loc = got - dot + rel->r_addend;
1211 #if defined(__SH5__)
1212 case R_SH_IMM_MEDLOW16:
1213 case R_SH_IMM_LOW16:
1217 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1221 * movi and shori have the format:
1223 * | op | imm | reg | reserved |
1224 * 31..26 25..10 9.. 4 3 .. 0
1226 * so we simply mask and or in imm.
1228 word = *loc & ~0x3fffc00;
1229 word |= (v & 0xffff) << 10;
1236 case R_SH_IMM_MEDLOW16_PCREL:
1237 case R_SH_IMM_LOW16_PCREL:
1241 word = *loc & ~0x3fffc00;
1245 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1248 word |= (v & 0xffff) << 10;
1254 #endif /* __SH5__ */
1258 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1259 ret = obj_reloc_unhandled;
1262 #if defined (__v850e__)
1267 /* We write two shorts instead of a long because even
1268 32-bit insns only need half-word alignment, but
1269 32-bit data needs to be long-word aligned. */
1270 v += ((unsigned short *)loc)[0];
1271 v += ((unsigned short *)loc)[1] << 16;
1272 ((unsigned short *)loc)[0] = v & 0xffff;
1273 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1276 case R_V850_22_PCREL:
1280 #if defined(CONFIG_USE_PLT_ENTRIES)
1284 /* find the plt entry and initialize it if necessary */
1285 assert(isym != NULL);
1287 #if defined(CONFIG_USE_PLT_LIST)
1288 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1296 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1298 /* generate some machine code */
1300 #if defined(__arm__)
1301 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1302 ip[1] = v; /* sym@ */
1304 #if defined(__powerpc__)
1305 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1306 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1307 ip[2] = 0x7d6903a6; /* mtctr r11 */
1308 ip[3] = 0x4e800420; /* bctr */
1310 #if defined (__v850e__)
1311 /* We have to trash a register, so we assume that any control
1312 transfer more than 21-bits away must be a function call
1313 (so we can use a call-clobbered register). */
1314 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1315 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1320 /* relative distance to target */
1322 /* if the target is too far away.... */
1323 #if defined (__arm__) || defined (__powerpc__)
1324 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1325 #elif defined (__v850e__)
1326 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1328 /* go via the plt */
1329 v = plt + pe->offset - dot;
1331 #if defined (__v850e__)
1336 ret = obj_reloc_dangerous;
1338 /* merge the offset into the instruction. */
1339 #if defined(__arm__)
1340 /* Convert to words. */
1343 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1345 #if defined(__powerpc__)
1346 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1348 #if defined (__v850e__)
1349 /* We write two shorts instead of a long because even 32-bit insns
1350 only need half-word alignment, but the 32-bit data write needs
1351 to be long-word aligned. */
1352 ((unsigned short *)loc)[0] =
1353 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1354 | ((v >> 16) & 0x3f); /* offs high part */
1355 ((unsigned short *)loc)[1] =
1356 (v & 0xffff); /* offs low part */
1359 #endif /* CONFIG_USE_PLT_ENTRIES */
1361 #if defined(CONFIG_USE_GOT_ENTRIES)
1364 assert(isym != NULL);
1365 /* needs an entry in the .got: set it, once */
1366 if (!isym->gotent.inited) {
1367 isym->gotent.inited = 1;
1368 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1370 /* make the reloc with_respect_to_.got */
1372 *loc += isym->gotent.offset + rel->r_addend;
1373 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1374 *loc += isym->gotent.offset;
1378 #endif /* CONFIG_USE_GOT_ENTRIES */
1385 #if defined(CONFIG_USE_LIST)
1387 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1388 int offset, int size)
1390 struct arch_list_entry *pe;
1392 for (pe = *list; pe != NULL; pe = pe->next) {
1393 if (pe->addend == rel->r_addend) {
1399 pe = xmalloc(sizeof(struct arch_list_entry));
1401 pe->addend = rel->r_addend;
1402 pe->offset = offset;
1412 #if defined(CONFIG_USE_SINGLE)
1414 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1415 int offset, int size)
1417 if (single->allocated == 0) {
1418 single->allocated = 1;
1419 single->offset = offset;
1428 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1430 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1431 int offset, int size)
1433 struct obj_section *myrelsec = obj_find_section(f, name);
1440 obj_extend_section(myrelsec, offset);
1442 myrelsec = obj_create_alloced_section(f, name,
1452 static void arch_create_got(struct obj_file *f)
1454 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1455 struct arch_file *ifile = (struct arch_file *) f;
1457 #if defined(CONFIG_USE_GOT_ENTRIES)
1458 int got_offset = 0, got_needed = 0, got_allocate;
1460 #if defined(CONFIG_USE_PLT_ENTRIES)
1461 int plt_offset = 0, plt_needed = 0, plt_allocate;
1463 struct obj_section *relsec, *symsec, *strsec;
1464 ElfW(RelM) *rel, *relend;
1465 ElfW(Sym) *symtab, *extsym;
1466 const char *strtab, *name;
1467 struct arch_symbol *intsym;
1469 for (i = 0; i < f->header.e_shnum; ++i) {
1470 relsec = f->sections[i];
1471 if (relsec->header.sh_type != SHT_RELM)
1474 symsec = f->sections[relsec->header.sh_link];
1475 strsec = f->sections[symsec->header.sh_link];
1477 rel = (ElfW(RelM) *) relsec->contents;
1478 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1479 symtab = (ElfW(Sym) *) symsec->contents;
1480 strtab = (const char *) strsec->contents;
1482 for (; rel < relend; ++rel) {
1483 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1485 #if defined(CONFIG_USE_GOT_ENTRIES)
1488 #if defined(CONFIG_USE_PLT_ENTRIES)
1492 switch (ELF32_R_TYPE(rel->r_info)) {
1493 #if defined(__arm__)
1508 #elif defined(__i386__)
1518 #elif defined(__powerpc__)
1523 #elif defined(__mc68000__)
1532 #elif defined(__sh__)
1542 #elif defined (__v850e__)
1543 case R_V850_22_PCREL:
1552 if (extsym->st_name != 0) {
1553 name = strtab + extsym->st_name;
1555 name = f->sections[extsym->st_shndx]->name;
1557 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1558 #if defined(CONFIG_USE_GOT_ENTRIES)
1560 got_offset += arch_single_init(
1561 rel, &intsym->gotent,
1562 got_offset, CONFIG_GOT_ENTRY_SIZE);
1567 #if defined(CONFIG_USE_PLT_ENTRIES)
1569 #if defined(CONFIG_USE_PLT_LIST)
1570 plt_offset += arch_list_add(
1571 rel, &intsym->pltent,
1572 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1574 plt_offset += arch_single_init(
1575 rel, &intsym->pltent,
1576 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1584 #if defined(CONFIG_USE_GOT_ENTRIES)
1586 ifile->got = arch_xsect_init(f, ".got", got_offset,
1587 CONFIG_GOT_ENTRY_SIZE);
1591 #if defined(CONFIG_USE_PLT_ENTRIES)
1593 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1594 CONFIG_PLT_ENTRY_SIZE);
1598 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1601 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
1602 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1608 /*======================================================================*/
1610 /* Standard ELF hash function. */
1611 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1613 unsigned long h = 0;
1620 if ((g = (h & 0xf0000000)) != 0) {
1629 static unsigned long obj_elf_hash(const char *name)
1631 return obj_elf_hash_n(name, strlen(name));
1634 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1635 /* String comparison for non-co-versioned kernel and module. */
1637 static int ncv_strcmp(const char *a, const char *b)
1639 size_t alen = strlen(a), blen = strlen(b);
1641 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1642 return strncmp(a, b, alen);
1643 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1644 return strncmp(a, b, blen);
1646 return strcmp(a, b);
1649 /* String hashing for non-co-versioned kernel and module. Here
1650 we are simply forced to drop the crc from the hash. */
1652 static unsigned long ncv_symbol_hash(const char *str)
1654 size_t len = strlen(str);
1655 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1657 return obj_elf_hash_n(str, len);
1661 obj_set_symbol_compare(struct obj_file *f,
1662 int (*cmp) (const char *, const char *),
1663 unsigned long (*hash) (const char *))
1666 f->symbol_cmp = cmp;
1668 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1671 f->symbol_hash = hash;
1673 memcpy(tmptab, f->symtab, sizeof(tmptab));
1674 memset(f->symtab, 0, sizeof(f->symtab));
1676 for (i = 0; i < HASH_BUCKETS; ++i)
1677 for (sym = tmptab[i]; sym; sym = next) {
1678 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1680 sym->next = f->symtab[h];
1686 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1688 static struct obj_symbol *
1689 obj_add_symbol(struct obj_file *f, const char *name,
1690 unsigned long symidx, int info,
1691 int secidx, ElfW(Addr) value,
1694 struct obj_symbol *sym;
1695 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1696 int n_type = ELFW(ST_TYPE) (info);
1697 int n_binding = ELFW(ST_BIND) (info);
1699 for (sym = f->symtab[hash]; sym; sym = sym->next)
1700 if (f->symbol_cmp(sym->name, name) == 0) {
1701 int o_secidx = sym->secidx;
1702 int o_info = sym->info;
1703 int o_type = ELFW(ST_TYPE) (o_info);
1704 int o_binding = ELFW(ST_BIND) (o_info);
1706 /* A redefinition! Is it legal? */
1708 if (secidx == SHN_UNDEF)
1710 else if (o_secidx == SHN_UNDEF)
1712 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1713 /* Cope with local and global symbols of the same name
1714 in the same object file, as might have been created
1715 by ld -r. The only reason locals are now seen at this
1716 level at all is so that we can do semi-sensible things
1719 struct obj_symbol *nsym, **p;
1721 nsym = arch_new_symbol();
1722 nsym->next = sym->next;
1725 /* Excise the old (local) symbol from the hash chain. */
1726 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1730 } else if (n_binding == STB_LOCAL) {
1731 /* Another symbol of the same name has already been defined.
1732 Just add this to the local table. */
1733 sym = arch_new_symbol();
1736 f->local_symtab[symidx] = sym;
1738 } else if (n_binding == STB_WEAK)
1740 else if (o_binding == STB_WEAK)
1742 /* Don't unify COMMON symbols with object types the programmer
1744 else if (secidx == SHN_COMMON
1745 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1747 else if (o_secidx == SHN_COMMON
1748 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1751 /* Don't report an error if the symbol is coming from
1752 the kernel or some external module. */
1753 if (secidx <= SHN_HIRESERVE)
1754 bb_error_msg("%s multiply defined", name);
1759 /* Completely new symbol. */
1760 sym = arch_new_symbol();
1761 sym->next = f->symtab[hash];
1762 f->symtab[hash] = sym;
1765 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1766 if (symidx >= f->local_symtab_size)
1767 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1768 name, (long) symidx, (long) f->local_symtab_size);
1770 f->local_symtab[symidx] = sym;
1777 sym->secidx = secidx;
1783 static struct obj_symbol *
1784 obj_find_symbol(struct obj_file *f, const char *name)
1786 struct obj_symbol *sym;
1787 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1789 for (sym = f->symtab[hash]; sym; sym = sym->next)
1790 if (f->symbol_cmp(sym->name, name) == 0)
1797 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1800 if (sym->secidx >= SHN_LORESERVE)
1803 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1805 /* As a special case, a NULL sym has value zero. */
1810 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1812 int i, n = f->header.e_shnum;
1814 for (i = 0; i < n; ++i)
1815 if (strcmp(f->sections[i]->name, name) == 0)
1816 return f->sections[i];
1821 static int obj_load_order_prio(struct obj_section *a)
1823 unsigned long af, ac;
1825 af = a->header.sh_flags;
1828 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1829 strcmp(a->name + 5, ".init"))
1833 if (!(af & SHF_WRITE))
1835 if (af & SHF_EXECINSTR)
1837 if (a->header.sh_type != SHT_NOBITS)
1844 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1846 struct obj_section **p;
1847 int prio = obj_load_order_prio(sec);
1848 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1849 if (obj_load_order_prio(*p) < prio)
1851 sec->load_next = *p;
1855 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1857 unsigned long align,
1860 int newidx = f->header.e_shnum++;
1861 struct obj_section *sec;
1863 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1864 f->sections[newidx] = sec = arch_new_section();
1866 memset(sec, 0, sizeof(*sec));
1867 sec->header.sh_type = SHT_PROGBITS;
1868 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1869 sec->header.sh_size = size;
1870 sec->header.sh_addralign = align;
1874 sec->contents = xmalloc(size);
1876 obj_insert_section_load_order(f, sec);
1881 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1883 unsigned long align,
1886 int newidx = f->header.e_shnum++;
1887 struct obj_section *sec;
1889 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1890 f->sections[newidx] = sec = arch_new_section();
1892 memset(sec, 0, sizeof(*sec));
1893 sec->header.sh_type = SHT_PROGBITS;
1894 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1895 sec->header.sh_size = size;
1896 sec->header.sh_addralign = align;
1900 sec->contents = xmalloc(size);
1902 sec->load_next = f->load_order;
1903 f->load_order = sec;
1904 if (f->load_order_search_start == &f->load_order)
1905 f->load_order_search_start = &sec->load_next;
1910 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1912 unsigned long oldsize = sec->header.sh_size;
1914 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1916 return sec->contents + oldsize;
1920 /* Conditionally add the symbols from the given symbol set to the
1926 int idx, struct new_module_symbol *syms, size_t nsyms)
1928 struct new_module_symbol *s;
1932 #ifdef SYMBOL_PREFIX
1934 size_t name_alloced_size = 0;
1937 gpl = obj_gpl_license(f, NULL) == 0;
1939 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1940 /* Only add symbols that are already marked external.
1941 If we override locals we may cause problems for
1942 argument initialization. We will also create a false
1943 dependency on the module. */
1944 struct obj_symbol *sym;
1947 /* GPL licensed modules can use symbols exported with
1948 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
1949 * exported names. Non-GPL modules never see any GPLONLY_
1950 * symbols so they cannot fudge it by adding the prefix on
1953 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
1955 ((char *)s->name) += 8;
1959 name = (char *)s->name;
1961 #ifdef SYMBOL_PREFIX
1962 /* Prepend SYMBOL_PREFIX to the symbol's name (the
1963 kernel exports `C names', but module object files
1964 reference `linker names'). */
1965 size_t extra = sizeof SYMBOL_PREFIX;
1966 size_t name_size = strlen (name) + extra;
1967 if (name_size > name_alloced_size) {
1968 name_alloced_size = name_size * 2;
1969 name_buf = alloca (name_alloced_size);
1971 strcpy (name_buf, SYMBOL_PREFIX);
1972 strcpy (name_buf + extra - 1, name);
1974 #endif /* SYMBOL_PREFIX */
1976 sym = obj_find_symbol(f, name);
1977 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
1978 #ifdef SYMBOL_PREFIX
1979 /* Put NAME_BUF into more permanent storage. */
1980 name = xmalloc (name_size);
1981 strcpy (name, name_buf);
1983 sym = obj_add_symbol(f, name, -1,
1984 ELFW(ST_INFO) (STB_GLOBAL,
1987 /* Did our symbol just get installed? If so, mark the
1988 module as "used". */
1989 if (sym->secidx == idx)
1997 static void add_kernel_symbols(struct obj_file *f)
1999 struct external_module *m;
2002 /* Add module symbols first. */
2004 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2006 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2007 m->nsyms)) m->used = 1, ++nused;
2009 n_ext_modules_used = nused;
2011 /* And finally the symbols from the kernel proper. */
2014 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2017 static char *get_modinfo_value(struct obj_file *f, const char *key)
2019 struct obj_section *sec;
2020 char *p, *v, *n, *ep;
2021 size_t klen = strlen(key);
2023 sec = obj_find_section(f, ".modinfo");
2027 ep = p + sec->header.sh_size;
2030 n = strchr(p, '\0');
2032 if (p + klen == v && strncmp(p, key, klen) == 0)
2035 if (p + klen == n && strcmp(p, key) == 0)
2045 /*======================================================================*/
2046 /* Functions relating to module loading in pre 2.1 kernels. */
2049 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
2053 struct obj_symbol *sym;
2057 if ((q = strchr(p, '=')) == NULL) {
2063 sym = obj_find_symbol(f, p);
2065 /* Also check that the parameter was not resolved from the kernel. */
2066 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2067 bb_error_msg("symbol for parameter %s not found", p);
2071 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
2073 /* Do C quoting if we begin with a ". */
2077 str = alloca(strlen(q));
2078 for (r = str, q++; *q != '"'; ++q, ++r) {
2080 bb_error_msg("improperly terminated string argument for %s", p);
2082 } else if (*q == '\\')
2116 if (q[1] >= '0' && q[1] <= '7') {
2117 c = (c * 8) + *++q - '0';
2118 if (q[1] >= '0' && q[1] <= '7')
2119 c = (c * 8) + *++q - '0';
2132 obj_string_patch(f, sym->secidx, sym->value, str);
2133 } else if (*q >= '0' && *q <= '9') {
2135 *loc++ = strtoul(q, &q, 0);
2136 while (*q++ == ',');
2138 char *contents = f->sections[sym->secidx]->contents;
2139 char *myloc = contents + sym->value;
2140 char *r; /* To search for commas */
2142 /* Break the string with comas */
2143 while ((r = strchr(q, ',')) != (char *) NULL) {
2145 obj_string_patch(f, sym->secidx, myloc - contents, q);
2146 myloc += sizeof(char *);
2151 obj_string_patch(f, sym->secidx, myloc - contents, q);
2160 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2161 static int old_is_module_checksummed(struct obj_file *f)
2163 return obj_find_symbol(f, "Using_Versions") != NULL;
2165 /* Get the module's kernel version in the canonical integer form. */
2168 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2170 struct obj_symbol *sym;
2174 sym = obj_find_symbol(f, "kernel_version");
2178 p = f->sections[sym->secidx]->contents + sym->value;
2179 safe_strncpy(str, p, STRVERSIONLEN);
2181 a = strtoul(p, &p, 10);
2184 b = strtoul(p + 1, &p, 10);
2187 c = strtoul(p + 1, &q, 10);
2191 return a << 16 | b << 8 | c;
2194 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2196 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
2198 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
2200 static int old_get_kernel_symbols(const char *m_name)
2202 struct old_kernel_sym *ks, *k;
2203 struct new_module_symbol *s;
2204 struct external_module *mod;
2205 int nks, nms, nmod, i;
2207 nks = get_kernel_syms(NULL);
2210 bb_perror_msg("get_kernel_syms: %s", m_name);
2212 bb_error_msg("No kernel symbols");
2216 ks = k = xmalloc(nks * sizeof(*ks));
2218 if (get_kernel_syms(ks) != nks) {
2219 perror("inconsistency with get_kernel_syms -- is someone else "
2220 "playing with modules?");
2225 /* Collect the module information. */
2230 while (k->name[0] == '#' && k->name[1]) {
2231 struct old_kernel_sym *k2;
2233 /* Find out how many symbols this module has. */
2234 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
2238 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
2239 mod[nmod].name = k->name + 1;
2240 mod[nmod].addr = k->value;
2242 mod[nmod].nsyms = nms;
2243 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2245 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
2246 s->name = (unsigned long) k->name;
2247 s->value = k->value;
2254 n_ext_modules = nmod + 1;
2256 /* Now collect the symbols for the kernel proper. */
2258 if (k->name[0] == '#')
2261 nksyms = nms = nks - (k - ks);
2262 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2264 for (i = 0; i < nms; ++i, ++s, ++k) {
2265 s->name = (unsigned long) k->name;
2266 s->value = k->value;
2272 /* Return the kernel symbol checksum version, or zero if not used. */
2274 static int old_is_kernel_checksummed(void)
2276 /* Using_Versions is the first symbol. */
2278 && strcmp((char *) ksyms[0].name,
2279 "Using_Versions") == 0) return ksyms[0].value;
2285 static int old_create_mod_use_count(struct obj_file *f)
2287 struct obj_section *sec;
2289 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2292 obj_add_symbol(f, "mod_use_count_", -1,
2293 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2300 old_init_module(const char *m_name, struct obj_file *f,
2301 unsigned long m_size)
2304 struct old_mod_routines routines;
2305 struct old_symbol_table *symtab;
2308 /* Create the symbol table */
2310 int nsyms = 0, strsize = 0, total;
2312 /* Size things first... */
2315 for (i = 0; i < HASH_BUCKETS; ++i) {
2316 struct obj_symbol *sym;
2317 for (sym = f->symtab[i]; sym; sym = sym->next)
2318 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2319 && sym->secidx <= SHN_HIRESERVE)
2321 sym->ksymidx = nsyms++;
2322 strsize += strlen(sym->name) + 1;
2327 total = (sizeof(struct old_symbol_table)
2328 + nsyms * sizeof(struct old_module_symbol)
2329 + n_ext_modules_used * sizeof(struct old_module_ref)
2331 symtab = xmalloc(total);
2332 symtab->size = total;
2333 symtab->n_symbols = nsyms;
2334 symtab->n_refs = n_ext_modules_used;
2336 if (flag_export && nsyms) {
2337 struct old_module_symbol *ksym;
2341 ksym = symtab->symbol;
2342 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2343 + n_ext_modules_used * sizeof(struct old_module_ref));
2345 for (i = 0; i < HASH_BUCKETS; ++i) {
2346 struct obj_symbol *sym;
2347 for (sym = f->symtab[i]; sym; sym = sym->next)
2348 if (sym->ksymidx >= 0) {
2349 ksym->addr = obj_symbol_final_value(f, sym);
2351 (unsigned long) str - (unsigned long) symtab;
2353 strcpy(str, sym->name);
2354 str += strlen(sym->name) + 1;
2360 if (n_ext_modules_used) {
2361 struct old_module_ref *ref;
2364 ref = (struct old_module_ref *)
2365 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2367 for (i = 0; i < n_ext_modules; ++i)
2368 if (ext_modules[i].used)
2369 ref++->module = ext_modules[i].addr;
2373 /* Fill in routines. */
2376 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2378 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2380 /* Whew! All of the initialization is complete. Collect the final
2381 module image and give it to the kernel. */
2383 image = xmalloc(m_size);
2384 obj_create_image(f, image);
2386 /* image holds the complete relocated module, accounting correctly for
2387 mod_use_count. However the old module kernel support assume that
2388 it is receiving something which does not contain mod_use_count. */
2389 ret = old_sys_init_module(m_name, image + sizeof(long),
2390 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2391 : 0), &routines, symtab);
2393 bb_perror_msg("init_module: %s", m_name);
2403 #define old_create_mod_use_count(x) TRUE
2404 #define old_init_module(x, y, z) TRUE
2406 #endif /* CONFIG_FEATURE_OLD_MODULE_INTERFACE */
2410 /*======================================================================*/
2411 /* Functions relating to module loading after 2.1.18. */
2414 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2417 char *p, *q, *key, *sym_name;
2418 struct obj_symbol *sym;
2419 char *contents, *loc;
2423 if ((q = strchr(p, '=')) == NULL) {
2428 key = alloca(q - p + 6);
2429 memcpy(key, "parm_", 5);
2430 memcpy(key + 5, p, q - p);
2433 p = get_modinfo_value(f, key);
2436 bb_error_msg("invalid parameter %s", key);
2440 #ifdef SYMBOL_PREFIX
2441 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2442 strcpy (sym_name, SYMBOL_PREFIX);
2443 strcat (sym_name, key);
2447 sym = obj_find_symbol(f, sym_name);
2449 /* Also check that the parameter was not resolved from the kernel. */
2450 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2451 bb_error_msg("symbol for parameter %s not found", key);
2456 min = strtoul(p, &p, 10);
2458 max = strtoul(p + 1, &p, 10);
2464 contents = f->sections[sym->secidx]->contents;
2465 loc = contents + sym->value;
2469 if ((*p == 's') || (*p == 'c')) {
2472 /* Do C quoting if we begin with a ", else slurp the lot. */
2476 str = alloca(strlen(q));
2477 for (r = str, q++; *q != '"'; ++q, ++r) {
2479 bb_error_msg("improperly terminated string argument for %s",
2482 } else if (*q == '\\')
2516 if (q[1] >= '0' && q[1] <= '7') {
2517 c = (c * 8) + *++q - '0';
2518 if (q[1] >= '0' && q[1] <= '7')
2519 c = (c * 8) + *++q - '0';
2536 /* In this case, the string is not quoted. We will break
2537 it using the coma (like for ints). If the user wants to
2538 include comas in a string, he just has to quote it */
2540 /* Search the next coma */
2544 if (r != (char *) NULL) {
2545 /* Recopy the current field */
2546 str = alloca(r - q + 1);
2547 memcpy(str, q, r - q);
2549 /* I don't know if it is usefull, as the previous case
2550 doesn't null terminate the string ??? */
2553 /* Keep next fields */
2564 obj_string_patch(f, sym->secidx, loc - contents, str);
2565 loc += tgt_sizeof_char_p;
2567 /* Array of chars (in fact, matrix !) */
2568 unsigned long charssize; /* size of each member */
2570 /* Get the size of each member */
2571 /* Probably we should do that outside the loop ? */
2572 if (!isdigit(*(p + 1))) {
2573 bb_error_msg("parameter type 'c' for %s must be followed by"
2574 " the maximum size", key);
2577 charssize = strtoul(p + 1, (char **) NULL, 10);
2580 if (strlen(str) >= charssize) {
2581 bb_error_msg("string too long for %s (max %ld)", key,
2586 /* Copy to location */
2587 strcpy((char *) loc, str);
2591 long v = strtoul(q, &q, 0);
2598 loc += tgt_sizeof_short;
2602 loc += tgt_sizeof_int;
2606 loc += tgt_sizeof_long;
2610 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2625 goto retry_end_of_value;
2629 bb_error_msg("too many values for %s (max %d)", key, max);
2636 bb_error_msg("invalid argument syntax for %s", key);
2643 bb_error_msg("too few values for %s (min %d)", key, min);
2653 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2654 static int new_is_module_checksummed(struct obj_file *f)
2656 const char *p = get_modinfo_value(f, "using_checksums");
2663 /* Get the module's kernel version in the canonical integer form. */
2666 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2671 p = get_modinfo_value(f, "kernel_version");
2674 safe_strncpy(str, p, STRVERSIONLEN);
2676 a = strtoul(p, &p, 10);
2679 b = strtoul(p + 1, &p, 10);
2682 c = strtoul(p + 1, &q, 10);
2686 return a << 16 | b << 8 | c;
2689 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2692 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2694 /* Fetch the loaded modules, and all currently exported symbols. */
2696 static int new_get_kernel_symbols(void)
2698 char *module_names, *mn;
2699 struct external_module *modules, *m;
2700 struct new_module_symbol *syms, *s;
2701 size_t ret, bufsize, nmod, nsyms, i, j;
2703 /* Collect the loaded modules. */
2705 module_names = xmalloc(bufsize = 256);
2707 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2708 if (errno == ENOSPC && bufsize < ret) {
2709 module_names = xrealloc(module_names, bufsize = ret);
2710 goto retry_modules_load;
2712 bb_perror_msg("QM_MODULES");
2716 n_ext_modules = nmod = ret;
2718 /* Collect the modules' symbols. */
2721 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2722 memset(modules, 0, nmod * sizeof(*modules));
2723 for (i = 0, mn = module_names, m = modules;
2724 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2725 struct new_module_info info;
2727 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2728 if (errno == ENOENT) {
2729 /* The module was removed out from underneath us. */
2732 bb_perror_msg("query_module: QM_INFO: %s", mn);
2736 syms = xmalloc(bufsize = 1024);
2738 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2741 syms = xrealloc(syms, bufsize = ret);
2742 goto retry_mod_sym_load;
2744 /* The module was removed out from underneath us. */
2747 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2754 m->addr = info.addr;
2758 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2759 s->name += (unsigned long) syms;
2764 /* Collect the kernel's symbols. */
2766 syms = xmalloc(bufsize = 16 * 1024);
2767 retry_kern_sym_load:
2768 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2769 if (errno == ENOSPC && bufsize < ret) {
2770 syms = xrealloc(syms, bufsize = ret);
2771 goto retry_kern_sym_load;
2773 bb_perror_msg("kernel: QM_SYMBOLS");
2776 nksyms = nsyms = ret;
2779 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2780 s->name += (unsigned long) syms;
2786 /* Return the kernel symbol checksum version, or zero if not used. */
2788 static int new_is_kernel_checksummed(void)
2790 struct new_module_symbol *s;
2793 /* Using_Versions is not the first symbol, but it should be in there. */
2795 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2796 if (strcmp((char *) s->name, "Using_Versions") == 0)
2803 static int new_create_this_module(struct obj_file *f, const char *m_name)
2805 struct obj_section *sec;
2807 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2808 sizeof(struct new_module));
2809 memset(sec->contents, 0, sizeof(struct new_module));
2811 obj_add_symbol(f, SPFX "__this_module", -1,
2812 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2813 sizeof(struct new_module));
2815 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2821 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2822 /* add an entry to the __ksymtab section, creating it if necessary */
2823 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2825 struct obj_section *sec;
2828 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2829 * If __ksymtab is defined but not marked alloc, x out the first character
2830 * (no obj_delete routine) and create a new __ksymtab with the correct
2833 sec = obj_find_section(f, "__ksymtab");
2834 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2835 *((char *)(sec->name)) = 'x'; /* override const */
2839 sec = obj_create_alloced_section(f, "__ksymtab",
2840 tgt_sizeof_void_p, 0);
2843 sec->header.sh_flags |= SHF_ALLOC;
2844 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2846 ofs = sec->header.sh_size;
2847 obj_symbol_patch(f, sec->idx, ofs, sym);
2848 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2849 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2851 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2853 static int new_create_module_ksymtab(struct obj_file *f)
2855 struct obj_section *sec;
2858 /* We must always add the module references. */
2860 if (n_ext_modules_used) {
2861 struct new_module_ref *dep;
2862 struct obj_symbol *tm;
2864 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2865 (sizeof(struct new_module_ref)
2866 * n_ext_modules_used));
2870 tm = obj_find_symbol(f, SPFX "__this_module");
2871 dep = (struct new_module_ref *) sec->contents;
2872 for (i = 0; i < n_ext_modules; ++i)
2873 if (ext_modules[i].used) {
2874 dep->dep = ext_modules[i].addr;
2875 obj_symbol_patch(f, sec->idx,
2876 (char *) &dep->ref - sec->contents, tm);
2882 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2887 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2890 /* We don't want to export symbols residing in sections that
2891 aren't loaded. There are a number of these created so that
2892 we make sure certain module options don't appear twice. */
2894 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2896 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2898 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2899 struct obj_symbol *sym;
2900 for (sym = f->symtab[i]; sym; sym = sym->next)
2901 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2902 && sym->secidx <= SHN_HIRESERVE
2903 && (sym->secidx >= SHN_LORESERVE
2904 || loaded[sym->secidx])) {
2905 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2907 obj_symbol_patch(f, sec->idx, ofs, sym);
2908 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2915 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2923 new_init_module(const char *m_name, struct obj_file *f,
2924 unsigned long m_size)
2926 struct new_module *module;
2927 struct obj_section *sec;
2932 sec = obj_find_section(f, ".this");
2933 if (!sec || !sec->contents) {
2934 bb_perror_msg_and_die("corrupt module %s?",m_name);
2936 module = (struct new_module *) sec->contents;
2937 m_addr = sec->header.sh_addr;
2939 module->size_of_struct = sizeof(*module);
2940 module->size = m_size;
2941 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2943 sec = obj_find_section(f, "__ksymtab");
2944 if (sec && sec->header.sh_size) {
2945 module->syms = sec->header.sh_addr;
2946 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2949 if (n_ext_modules_used) {
2950 sec = obj_find_section(f, ".kmodtab");
2951 module->deps = sec->header.sh_addr;
2952 module->ndeps = n_ext_modules_used;
2956 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2958 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2960 sec = obj_find_section(f, "__ex_table");
2962 module->ex_table_start = sec->header.sh_addr;
2963 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2966 sec = obj_find_section(f, ".text.init");
2968 module->runsize = sec->header.sh_addr - m_addr;
2970 sec = obj_find_section(f, ".data.init");
2972 if (!module->runsize ||
2973 module->runsize > sec->header.sh_addr - m_addr)
2974 module->runsize = sec->header.sh_addr - m_addr;
2976 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2977 if (sec && sec->header.sh_size) {
2978 module->archdata_start = (void*)sec->header.sh_addr;
2979 module->archdata_end = module->archdata_start + sec->header.sh_size;
2981 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2982 if (sec && sec->header.sh_size) {
2983 module->kallsyms_start = (void*)sec->header.sh_addr;
2984 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2987 if (!arch_init_module(f, module))
2990 /* Whew! All of the initialization is complete. Collect the final
2991 module image and give it to the kernel. */
2993 image = xmalloc(m_size);
2994 obj_create_image(f, image);
2996 ret = new_sys_init_module(m_name, (struct new_module *) image);
2998 bb_perror_msg("init_module: %s", m_name);
3007 #define new_init_module(x, y, z) TRUE
3008 #define new_create_this_module(x, y) 0
3009 #define new_add_ksymtab(x, y) -1
3010 #define new_create_module_ksymtab(x)
3011 #define query_module(v, w, x, y, z) -1
3013 #endif /* CONFIG_FEATURE_NEW_MODULE_INTERFACE */
3016 /*======================================================================*/
3019 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3022 struct obj_string_patch *p;
3023 struct obj_section *strsec;
3024 size_t len = strlen(string) + 1;
3027 p = xmalloc(sizeof(*p));
3028 p->next = f->string_patches;
3029 p->reloc_secidx = secidx;
3030 p->reloc_offset = offset;
3031 f->string_patches = p;
3033 strsec = obj_find_section(f, ".kstrtab");
3034 if (strsec == NULL) {
3035 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3036 p->string_offset = 0;
3037 loc = strsec->contents;
3039 p->string_offset = strsec->header.sh_size;
3040 loc = obj_extend_section(strsec, len);
3042 memcpy(loc, string, len);
3047 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
3049 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3050 struct obj_symbol *sym)
3052 struct obj_symbol_patch *p;
3054 p = xmalloc(sizeof(*p));
3055 p->next = f->symbol_patches;
3056 p->reloc_secidx = secidx;
3057 p->reloc_offset = offset;
3059 f->symbol_patches = p;
3065 static int obj_check_undefineds(struct obj_file *f)
3070 for (i = 0; i < HASH_BUCKETS; ++i) {
3071 struct obj_symbol *sym;
3072 for (sym = f->symtab[i]; sym; sym = sym->next)
3073 if (sym->secidx == SHN_UNDEF) {
3074 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
3075 sym->secidx = SHN_ABS;
3079 bb_error_msg("unresolved symbol %s", sym->name);
3089 static void obj_allocate_commons(struct obj_file *f)
3091 struct common_entry {
3092 struct common_entry *next;
3093 struct obj_symbol *sym;
3094 } *common_head = NULL;
3098 for (i = 0; i < HASH_BUCKETS; ++i) {
3099 struct obj_symbol *sym;
3100 for (sym = f->symtab[i]; sym; sym = sym->next)
3101 if (sym->secidx == SHN_COMMON) {
3102 /* Collect all COMMON symbols and sort them by size so as to
3103 minimize space wasted by alignment requirements. */
3105 struct common_entry **p, *n;
3106 for (p = &common_head; *p; p = &(*p)->next)
3107 if (sym->size <= (*p)->sym->size)
3110 n = alloca(sizeof(*n));
3118 for (i = 1; i < f->local_symtab_size; ++i) {
3119 struct obj_symbol *sym = f->local_symtab[i];
3120 if (sym && sym->secidx == SHN_COMMON) {
3121 struct common_entry **p, *n;
3122 for (p = &common_head; *p; p = &(*p)->next)
3123 if (sym == (*p)->sym)
3125 else if (sym->size < (*p)->sym->size) {
3126 n = alloca(sizeof(*n));
3136 /* Find the bss section. */
3137 for (i = 0; i < f->header.e_shnum; ++i)
3138 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3141 /* If for some reason there hadn't been one, create one. */
3142 if (i == f->header.e_shnum) {
3143 struct obj_section *sec;
3145 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3146 f->sections[i] = sec = arch_new_section();
3147 f->header.e_shnum = i + 1;
3149 memset(sec, 0, sizeof(*sec));
3150 sec->header.sh_type = SHT_PROGBITS;
3151 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3156 /* Allocate the COMMONS. */
3158 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3159 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3160 struct common_entry *c;
3162 for (c = common_head; c; c = c->next) {
3163 ElfW(Addr) align = c->sym->value;
3165 if (align > max_align)
3167 if (bss_size & (align - 1))
3168 bss_size = (bss_size | (align - 1)) + 1;
3171 c->sym->value = bss_size;
3173 bss_size += c->sym->size;
3176 f->sections[i]->header.sh_size = bss_size;
3177 f->sections[i]->header.sh_addralign = max_align;
3181 /* For the sake of patch relocation and parameter initialization,
3182 allocate zeroed data for NOBITS sections now. Note that after
3183 this we cannot assume NOBITS are really empty. */
3184 for (i = 0; i < f->header.e_shnum; ++i) {
3185 struct obj_section *s = f->sections[i];
3186 if (s->header.sh_type == SHT_NOBITS) {
3187 if (s->header.sh_size != 0)
3188 s->contents = memset(xmalloc(s->header.sh_size),
3189 0, s->header.sh_size);
3193 s->header.sh_type = SHT_PROGBITS;
3198 static unsigned long obj_load_size(struct obj_file *f)
3200 unsigned long dot = 0;
3201 struct obj_section *sec;
3203 /* Finalize the positions of the sections relative to one another. */
3205 for (sec = f->load_order; sec; sec = sec->load_next) {
3208 align = sec->header.sh_addralign;
3209 if (align && (dot & (align - 1)))
3210 dot = (dot | (align - 1)) + 1;
3212 sec->header.sh_addr = dot;
3213 dot += sec->header.sh_size;
3219 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3221 int i, n = f->header.e_shnum;
3224 /* Finalize the addresses of the sections. */
3227 for (i = 0; i < n; ++i)
3228 f->sections[i]->header.sh_addr += base;
3230 /* And iterate over all of the relocations. */
3232 for (i = 0; i < n; ++i) {
3233 struct obj_section *relsec, *symsec, *targsec, *strsec;
3234 ElfW(RelM) * rel, *relend;
3238 relsec = f->sections[i];
3239 if (relsec->header.sh_type != SHT_RELM)
3242 symsec = f->sections[relsec->header.sh_link];
3243 targsec = f->sections[relsec->header.sh_info];
3244 strsec = f->sections[symsec->header.sh_link];
3246 rel = (ElfW(RelM) *) relsec->contents;
3247 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3248 symtab = (ElfW(Sym) *) symsec->contents;
3249 strtab = (const char *) strsec->contents;
3251 for (; rel < relend; ++rel) {
3252 ElfW(Addr) value = 0;
3253 struct obj_symbol *intsym = NULL;
3254 unsigned long symndx;
3255 ElfW(Sym) * extsym = 0;
3258 /* Attempt to find a value to use for this relocation. */
3260 symndx = ELFW(R_SYM) (rel->r_info);
3262 /* Note we've already checked for undefined symbols. */
3264 extsym = &symtab[symndx];
3265 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
3266 /* Local symbols we look up in the local table to be sure
3267 we get the one that is really intended. */
3268 intsym = f->local_symtab[symndx];
3270 /* Others we look up in the hash table. */
3272 if (extsym->st_name)
3273 name = strtab + extsym->st_name;
3275 name = f->sections[extsym->st_shndx]->name;
3276 intsym = obj_find_symbol(f, name);
3279 value = obj_symbol_final_value(f, intsym);
3280 intsym->referenced = 1;
3282 #if SHT_RELM == SHT_RELA
3283 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3284 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3285 if (!extsym || !extsym->st_name ||
3286 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
3288 value += rel->r_addend;
3292 switch (arch_apply_relocation
3293 (f, targsec, symsec, intsym, rel, value)) {
3297 case obj_reloc_overflow:
3298 errmsg = "Relocation overflow";
3300 case obj_reloc_dangerous:
3301 errmsg = "Dangerous relocation";
3303 case obj_reloc_unhandled:
3304 errmsg = "Unhandled relocation";
3307 bb_error_msg("%s of type %ld for %s", errmsg,
3308 (long) ELFW(R_TYPE) (rel->r_info),
3309 strtab + extsym->st_name);
3311 bb_error_msg("%s of type %ld", errmsg,
3312 (long) ELFW(R_TYPE) (rel->r_info));
3320 /* Finally, take care of the patches. */
3322 if (f->string_patches) {
3323 struct obj_string_patch *p;
3324 struct obj_section *strsec;
3325 ElfW(Addr) strsec_base;
3326 strsec = obj_find_section(f, ".kstrtab");
3327 strsec_base = strsec->header.sh_addr;
3329 for (p = f->string_patches; p; p = p->next) {
3330 struct obj_section *targsec = f->sections[p->reloc_secidx];
3331 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3332 = strsec_base + p->string_offset;
3336 if (f->symbol_patches) {
3337 struct obj_symbol_patch *p;
3339 for (p = f->symbol_patches; p; p = p->next) {
3340 struct obj_section *targsec = f->sections[p->reloc_secidx];
3341 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3342 = obj_symbol_final_value(f, p->sym);
3349 static int obj_create_image(struct obj_file *f, char *image)
3351 struct obj_section *sec;
3352 ElfW(Addr) base = f->baseaddr;
3354 for (sec = f->load_order; sec; sec = sec->load_next) {
3357 if (sec->contents == 0 || sec->header.sh_size == 0)
3360 secimg = image + (sec->header.sh_addr - base);
3362 /* Note that we allocated data for NOBITS sections earlier. */
3363 memcpy(secimg, sec->contents, sec->header.sh_size);
3369 /*======================================================================*/
3371 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3374 ElfW(Shdr) * section_headers;
3378 /* Read the file header. */
3380 f = arch_new_file();
3381 memset(f, 0, sizeof(*f));
3382 f->symbol_cmp = strcmp;
3383 f->symbol_hash = obj_elf_hash;
3384 f->load_order_search_start = &f->load_order;
3386 fseek(fp, 0, SEEK_SET);
3387 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3388 bb_perror_msg("error reading ELF header");
3392 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3393 || f->header.e_ident[EI_MAG1] != ELFMAG1
3394 || f->header.e_ident[EI_MAG2] != ELFMAG2
3395 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3396 bb_error_msg("not an ELF file");
3399 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3400 || f->header.e_ident[EI_DATA] != ELFDATAM
3401 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3402 || !MATCH_MACHINE(f->header.e_machine)) {
3403 bb_error_msg("ELF file not for this architecture");
3406 if (f->header.e_type != ET_REL) {
3407 bb_error_msg("ELF file not a relocatable object");
3411 /* Read the section headers. */
3413 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3414 bb_error_msg("section header size mismatch: %lu != %lu",
3415 (unsigned long) f->header.e_shentsize,
3416 (unsigned long) sizeof(ElfW(Shdr)));
3420 shnum = f->header.e_shnum;
3421 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3422 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3424 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3425 fseek(fp, f->header.e_shoff, SEEK_SET);
3426 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3427 bb_perror_msg("error reading ELF section headers");
3431 /* Read the section data. */
3433 for (i = 0; i < shnum; ++i) {
3434 struct obj_section *sec;
3436 f->sections[i] = sec = arch_new_section();
3437 memset(sec, 0, sizeof(*sec));
3439 sec->header = section_headers[i];
3442 if(sec->header.sh_size) switch (sec->header.sh_type) {
3451 if (!loadprogbits) {
3452 sec->contents = NULL;
3459 if (sec->header.sh_size > 0) {
3460 sec->contents = xmalloc(sec->header.sh_size);
3461 fseek(fp, sec->header.sh_offset, SEEK_SET);
3462 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3463 bb_perror_msg("error reading ELF section data");
3467 sec->contents = NULL;
3471 #if SHT_RELM == SHT_REL
3473 bb_error_msg("RELA relocations not supported on this architecture");
3477 bb_error_msg("REL relocations not supported on this architecture");
3482 if (sec->header.sh_type >= SHT_LOPROC) {
3483 /* Assume processor specific section types are debug
3484 info and can safely be ignored. If this is ever not
3485 the case (Hello MIPS?), don't put ifdefs here but
3486 create an arch_load_proc_section(). */
3490 bb_error_msg("can't handle sections of type %ld",
3491 (long) sec->header.sh_type);
3496 /* Do what sort of interpretation as needed by each section. */
3498 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3500 for (i = 0; i < shnum; ++i) {
3501 struct obj_section *sec = f->sections[i];
3502 sec->name = shstrtab + sec->header.sh_name;
3505 for (i = 0; i < shnum; ++i) {
3506 struct obj_section *sec = f->sections[i];
3508 /* .modinfo should be contents only but gcc has no attribute for that.
3509 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3511 if (strcmp(sec->name, ".modinfo") == 0)
3512 sec->header.sh_flags &= ~SHF_ALLOC;
3514 if (sec->header.sh_flags & SHF_ALLOC)
3515 obj_insert_section_load_order(f, sec);
3517 switch (sec->header.sh_type) {
3520 unsigned long nsym, j;
3524 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3525 bb_error_msg("symbol size mismatch: %lu != %lu",
3526 (unsigned long) sec->header.sh_entsize,
3527 (unsigned long) sizeof(ElfW(Sym)));
3531 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3532 strtab = f->sections[sec->header.sh_link]->contents;
3533 sym = (ElfW(Sym) *) sec->contents;
3535 /* Allocate space for a table of local symbols. */
3536 j = f->local_symtab_size = sec->header.sh_info;
3537 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3539 /* Insert all symbols into the hash table. */
3540 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3541 ElfW(Addr) val = sym->st_value;
3544 name = strtab + sym->st_name;
3546 name = f->sections[sym->st_shndx]->name;
3548 #if defined(__SH5__)
3550 * For sh64 it is possible that the target of a branch
3551 * requires a mode switch (32 to 16 and back again).
3553 * This is implied by the lsb being set in the target
3554 * address for SHmedia mode and clear for SHcompact.
3556 val |= sym->st_other & 4;
3559 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3566 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3567 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3568 (unsigned long) sec->header.sh_entsize,
3569 (unsigned long) sizeof(ElfW(RelM)));
3573 /* XXX Relocation code from modutils-2.3.19 is not here.
3574 * Why? That's about 20 lines of code from obj/obj_load.c,
3575 * which gets done in a second pass through the sections.
3576 * This BusyBox insmod does similar work in obj_relocate(). */
3583 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3585 * load the unloaded sections directly into the memory allocated by
3586 * kernel for the module
3589 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3591 ElfW(Addr) base = f->baseaddr;
3592 struct obj_section* sec;
3594 for (sec = f->load_order; sec; sec = sec->load_next) {
3596 /* section already loaded? */
3597 if (sec->contents != NULL)
3600 if (sec->header.sh_size == 0)
3603 sec->contents = imagebase + (sec->header.sh_addr - base);
3604 fseek(fp, sec->header.sh_offset, SEEK_SET);
3605 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3606 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3615 static void hide_special_symbols(struct obj_file *f)
3617 static const char *const specials[] = {
3618 SPFX "cleanup_module",
3620 SPFX "kernel_version",
3624 struct obj_symbol *sym;
3625 const char *const *p;
3627 for (p = specials; *p; ++p)
3628 if ((sym = obj_find_symbol(f, *p)) != NULL)
3630 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3634 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3635 static int obj_gpl_license(struct obj_file *f, const char **license)
3637 struct obj_section *sec;
3638 /* This list must match *exactly* the list of allowable licenses in
3639 * linux/include/linux/module.h. Checking for leading "GPL" will not
3640 * work, somebody will use "GPL sucks, this is proprietary".
3642 static const char *gpl_licenses[] = {
3645 "GPL and additional rights",
3650 if ((sec = obj_find_section(f, ".modinfo"))) {
3651 const char *value, *ptr, *endptr;
3652 ptr = sec->contents;
3653 endptr = ptr + sec->header.sh_size;
3654 while (ptr < endptr) {
3655 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3659 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3660 if (strcmp(value+1, gpl_licenses[i]) == 0)
3665 if (strchr(ptr, '\0'))
3666 ptr = strchr(ptr, '\0') + 1;
3674 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3675 #define TAINT_PROPRIETORY_MODULE (1<<0)
3676 #define TAINT_FORCED_MODULE (1<<1)
3677 #define TAINT_UNSAFE_SMP (1<<2)
3678 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3680 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3681 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3685 static int first = 1;
3686 if (fd < 0 && !kernel_has_tainted)
3687 return; /* New modutils on old kernel */
3688 printf("Warning: loading %s will taint the kernel: %s%s\n",
3689 m_name, text1, text2);
3691 printf(" See %s for information about tainted modules\n", TAINT_URL);
3695 read(fd, buf, sizeof(buf)-1);
3696 buf[sizeof(buf)-1] = '\0';
3697 oldval = strtoul(buf, NULL, 10);
3698 sprintf(buf, "%d\n", oldval | taint);
3699 write(fd, buf, strlen(buf));
3703 /* Check if loading this module will taint the kernel. */
3704 static void check_tainted_module(struct obj_file *f, char *m_name)
3706 static const char tainted_file[] = TAINT_FILENAME;
3707 int fd, kernel_has_tainted;
3710 kernel_has_tainted = 1;
3711 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3712 if (errno == ENOENT)
3713 kernel_has_tainted = 0;
3714 else if (errno == EACCES)
3715 kernel_has_tainted = 1;
3717 perror(tainted_file);
3718 kernel_has_tainted = 0;
3722 switch (obj_gpl_license(f, &ptr)) {
3726 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3729 /* The module has a non-GPL license so we pretend that the
3730 * kernel always has a taint flag to get a warning even on
3731 * kernels without the proc flag.
3733 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3736 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3740 if (flag_force_load)
3741 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3746 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3747 #define check_tainted_module(x, y) do { } while(0);
3748 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3750 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3751 /* add module source, timestamp, kernel version and a symbol for the
3752 * start of some sections. this info is used by ksymoops to do better
3756 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3758 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3759 if (get_modinfo_value(f, "kernel_version") == NULL)
3760 return old_get_module_version(f, str);
3762 return new_get_module_version(f, str);
3763 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3764 strncpy(str, "???", sizeof(str));
3766 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3769 /* add module source, timestamp, kernel version and a symbol for the
3770 * start of some sections. this info is used by ksymoops to do better
3774 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3777 static const char symprefix[] = "__insmod_";
3778 struct obj_section *sec;
3779 struct obj_symbol *sym;
3780 char *name, *absolute_filename;
3781 char str[STRVERSIONLEN], real[PATH_MAX];
3782 int i, l, lm_name, lfilename, use_ksymtab, version;
3783 struct stat statbuf;
3785 static const char *section_names[] = {
3793 if (realpath(filename, real)) {
3794 absolute_filename = bb_xstrdup(real);
3797 int save_errno = errno;
3798 bb_error_msg("cannot get realpath for %s", filename);
3801 absolute_filename = bb_xstrdup(filename);
3804 lm_name = strlen(m_name);
3805 lfilename = strlen(absolute_filename);
3807 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3808 * are not to be exported. otherwise leave ksymtab alone for now, the
3809 * "export all symbols" compatibility code will export these symbols later.
3811 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3813 if ((sec = obj_find_section(f, ".this"))) {
3814 /* tag the module header with the object name, last modified
3815 * timestamp and module version. worst case for module version
3816 * is 0xffffff, decimal 16777215. putting all three fields in
3817 * one symbol is less readable but saves kernel space.
3819 l = sizeof(symprefix)+ /* "__insmod_" */
3820 lm_name+ /* module name */
3822 lfilename+ /* object filename */
3824 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3826 8+ /* version in dec */
3829 if (stat(absolute_filename, &statbuf) != 0)
3830 statbuf.st_mtime = 0;
3831 version = get_module_version(f, str); /* -1 if not found */
3832 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3833 symprefix, m_name, absolute_filename,
3834 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3836 sym = obj_add_symbol(f, name, -1,
3837 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3838 sec->idx, sec->header.sh_addr, 0);
3840 new_add_ksymtab(f, sym);
3842 free(absolute_filename);
3843 #ifdef _NOT_SUPPORTED_
3844 /* record where the persistent data is going, same address as previous symbol */
3847 l = sizeof(symprefix)+ /* "__insmod_" */
3848 lm_name+ /* module name */
3850 strlen(f->persist)+ /* data store */
3853 snprintf(name, l, "%s%s_P%s",
3854 symprefix, m_name, f->persist);
3855 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3856 sec->idx, sec->header.sh_addr, 0);
3858 new_add_ksymtab(f, sym);
3860 #endif /* _NOT_SUPPORTED_ */
3861 /* tag the desired sections if size is non-zero */
3863 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3864 if ((sec = obj_find_section(f, section_names[i])) &&
3865 sec->header.sh_size) {
3866 l = sizeof(symprefix)+ /* "__insmod_" */
3867 lm_name+ /* module name */
3869 strlen(sec->name)+ /* section name */
3871 8+ /* length in dec */
3874 snprintf(name, l, "%s%s_S%s_L%ld",
3875 symprefix, m_name, sec->name,
3876 (long)sec->header.sh_size);
3877 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3878 sec->idx, sec->header.sh_addr, 0);
3880 new_add_ksymtab(f, sym);
3884 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3886 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3887 static void print_load_map(struct obj_file *f)
3889 struct obj_symbol *sym;
3890 struct obj_symbol **all, **p;
3891 struct obj_section *sec;
3892 int i, nsyms, *loaded;
3894 /* Report on the section layout. */
3896 printf("Sections: Size %-*s Align\n",
3897 (int) (2 * sizeof(void *)), "Address");
3899 for (sec = f->load_order; sec; sec = sec->load_next) {
3903 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3908 printf("%-15s %08lx %0*lx 2**%d\n",
3910 (long)sec->header.sh_size,
3911 (int) (2 * sizeof(void *)),
3912 (long)sec->header.sh_addr,
3915 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3916 /* Quick reference which section indicies are loaded. */
3918 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3920 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3922 /* Collect the symbols we'll be listing. */
3924 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3925 for (sym = f->symtab[i]; sym; sym = sym->next)
3926 if (sym->secidx <= SHN_HIRESERVE
3927 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3930 all = alloca(nsyms * sizeof(struct obj_symbol *));
3932 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3933 for (sym = f->symtab[i]; sym; sym = sym->next)
3934 if (sym->secidx <= SHN_HIRESERVE
3935 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3938 /* And list them. */
3939 printf("\nSymbols:\n");
3940 for (p = all; p < all + nsyms; ++p) {
3942 unsigned long value;
3945 if (sym->secidx == SHN_ABS) {
3948 } else if (sym->secidx == SHN_UNDEF) {
3952 sec = f->sections[sym->secidx];
3954 if (sec->header.sh_type == SHT_NOBITS)
3956 else if (sec->header.sh_flags & SHF_ALLOC) {
3957 if (sec->header.sh_flags & SHF_EXECINSTR)
3959 else if (sec->header.sh_flags & SHF_WRITE)
3964 value = sym->value + sec->header.sh_addr;
3967 if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
3968 type = tolower(type);
3970 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3978 extern int insmod_main( int argc, char **argv)
3985 unsigned long m_size;
3990 int exit_status = EXIT_FAILURE;
3992 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3993 struct utsname uts_info;
3994 char m_strversion[STRVERSIONLEN];
3998 #ifdef CONFIG_FEATURE_CLEAN_UP
4003 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4004 int flag_print_load_map = 0;
4007 /* Parse any options */
4008 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4009 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0) {
4011 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0) {
4014 case 'f': /* force loading */
4015 flag_force_load = 1;
4017 case 'k': /* module loaded by kerneld, auto-cleanable */
4020 case 's': /* log to syslog */
4021 /* log to syslog -- not supported */
4022 /* but kernel needs this for request_module(), */
4023 /* as this calls: modprobe -k -s -- <module> */
4024 /* so silently ignore this flag */
4026 case 'v': /* verbose output */
4029 case 'q': /* silent */
4032 case 'x': /* do not export externs */
4035 case 'o': /* name the output module */
4037 m_name = bb_xstrdup(optarg);
4039 case 'L': /* Stub warning */
4040 /* This is needed for compatibility with modprobe.
4041 * In theory, this does locking, but we don't do
4042 * that. So be careful and plan your life around not
4043 * loading the same module 50 times concurrently. */
4045 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4046 case 'm': /* print module load map */
4047 flag_print_load_map = 1;
4055 if (argv[optind] == NULL) {
4059 /* Grab the module name */
4060 tmp1 = bb_xstrdup(argv[optind]);
4061 tmp = basename(tmp1);
4064 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4069 bb_xasprintf(&m_fullName, "%s.o", tmp);
4075 tmp1 = 0; /* flag for free(m_name) before exit() */
4078 /* Get a filedesc for the module. Check we we have a complete path */
4079 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
4080 (fp = fopen(argv[optind], "r")) == NULL) {
4081 struct utsname myuname;
4083 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4084 * but do not error out yet if we fail to find it... */
4085 if (uname(&myuname) == 0) {
4088 char real_module_dir[FILENAME_MAX];
4090 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4091 /* Jump through hoops in case /lib/modules/`uname -r`
4092 * is a symlink. We do not want recursive_action to
4093 * follow symlinks, but we do want to follow the
4094 * /lib/modules/`uname -r` dir, So resolve it ourselves
4095 * if it is a link... */
4096 if (realpath (tmdn, real_module_dir) == NULL)
4099 module_dir = real_module_dir;
4100 recursive_action(module_dir, TRUE, FALSE, FALSE,
4101 check_module_name_match, 0, m_fullName);
4105 /* Check if we have found anything yet */
4106 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4108 char module_dir[FILENAME_MAX];
4112 if (realpath (_PATH_MODULES, module_dir) == NULL)
4113 strcpy(module_dir, _PATH_MODULES);
4114 /* No module found under /lib/modules/`uname -r`, this
4115 * time cast the net a bit wider. Search /lib/modules/ */
4116 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4117 check_module_name_match, 0, m_fullName))
4120 || ((fp = fopen(m_filename, "r")) == NULL))
4122 bb_error_msg("%s: no module by that name found", m_fullName);
4126 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4129 m_filename = bb_xstrdup(argv[optind]);
4131 printf("Using %s\n", m_filename);
4133 #ifdef CONFIG_FEATURE_REALLY_NEW_MODULE_INTERFACE
4134 if (create_module(NULL, 0) < 0 && errno == ENOSYS) {
4136 argv[optind] = m_filename;
4137 return insmod_ng_main(argc - optind, argv + optind);
4141 if ((f = obj_load(fp, LOADBITS)) == NULL)
4142 bb_perror_msg_and_die("Could not load the module");
4144 if (get_modinfo_value(f, "kernel_version") == NULL)
4149 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4150 /* Version correspondence? */
4152 if (uname(&uts_info) < 0)
4153 uts_info.release[0] = '\0';
4154 if (m_has_modinfo) {
4155 m_version = new_get_module_version(f, m_strversion);
4157 m_version = old_get_module_version(f, m_strversion);
4158 if (m_version == -1) {
4159 bb_error_msg("couldn't find the kernel version the module was "
4165 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4166 if (flag_force_load) {
4167 bb_error_msg("Warning: kernel-module version mismatch\n"
4168 "\t%s was compiled for kernel version %s\n"
4169 "\twhile this kernel is version %s",
4170 m_filename, m_strversion, uts_info.release);
4172 bb_error_msg("kernel-module version mismatch\n"
4173 "\t%s was compiled for kernel version %s\n"
4174 "\twhile this kernel is version %s.",
4175 m_filename, m_strversion, uts_info.release);
4181 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4183 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
4185 if (k_new_syscalls) {
4186 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
4187 if (!new_get_kernel_symbols())
4189 k_crcs = new_is_kernel_checksummed();
4191 bb_error_msg("Not configured to support new kernels");
4195 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
4196 if (!old_get_kernel_symbols(m_name))
4198 k_crcs = old_is_kernel_checksummed();
4200 bb_error_msg("Not configured to support old kernels");
4205 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4207 m_crcs = new_is_module_checksummed(f);
4209 m_crcs = old_is_module_checksummed(f);
4211 if (m_crcs != k_crcs)
4212 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4213 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4215 /* Let the module know about the kernel symbols. */
4216 add_kernel_symbols(f);
4218 /* Allocate common symbols, symbol tables, and string tables. */
4221 ? !new_create_this_module(f, m_name)
4222 : !old_create_mod_use_count(f))
4227 if (!obj_check_undefineds(f)) {
4230 obj_allocate_commons(f);
4231 check_tainted_module(f, m_name);
4233 /* done with the module name, on to the optional var=value arguments */
4236 if (optind < argc) {
4238 ? !new_process_module_arguments(f, argc - optind, argv + optind)
4239 : !old_process_module_arguments(f, argc - optind, argv + optind))
4246 hide_special_symbols(f);
4248 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4249 add_ksymoops_symbols(f, m_filename, m_name);
4250 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4253 new_create_module_ksymtab(f);
4255 /* Find current size of the module */
4256 m_size = obj_load_size(f);
4259 m_addr = create_module(m_name, m_size);
4260 if (m_addr == -1) switch (errno) {
4262 bb_error_msg("A module named %s already exists", m_name);
4265 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4269 bb_perror_msg("create_module: %s", m_name);
4275 * the PROGBITS section was not loaded by the obj_load
4276 * now we can load them directly into the kernel memory
4278 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4279 delete_module(m_name);
4284 if (!obj_relocate(f, m_addr)) {
4285 delete_module(m_name);
4290 ? !new_init_module(m_name, f, m_size)
4291 : !old_init_module(m_name, f, m_size))
4293 delete_module(m_name);
4297 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4298 if(flag_print_load_map)
4302 exit_status = EXIT_SUCCESS;
4305 #ifdef CONFIG_FEATURE_CLEAN_UP
4315 return(exit_status);