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 "_"
222 #if defined(__cris__)
225 #define R_CRIS_NONE 0
229 #define MATCH_MACHINE(x) (x == EM_CRIS)
230 #define SHT_RELM SHT_RELA
231 #define Elf32_RelM Elf32_Rela
232 #define ELFCLASSM ELFCLASS32
236 #error Sorry, but insmod.c does not yet support this architecture...
240 //----------------------------------------------------------------------------
241 //--------modutils module.h, lines 45-242
242 //----------------------------------------------------------------------------
244 /* Definitions for the Linux module syscall interface.
245 Copyright 1996, 1997 Linux International.
247 Contributed by Richard Henderson <rth@tamu.edu>
249 This file is part of the Linux modutils.
251 This program is free software; you can redistribute it and/or modify it
252 under the terms of the GNU General Public License as published by the
253 Free Software Foundation; either version 2 of the License, or (at your
254 option) any later version.
256 This program is distributed in the hope that it will be useful, but
257 WITHOUT ANY WARRANTY; without even the implied warranty of
258 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
259 General Public License for more details.
261 You should have received a copy of the GNU General Public License
262 along with this program; if not, write to the Free Software Foundation,
263 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
266 #ifndef MODUTILS_MODULE_H
267 static const int MODUTILS_MODULE_H = 1;
269 #ident "$Id: insmod.c,v 1.106 2003/12/04 15:02:57 mjn3 Exp $"
271 /* This file contains the structures used by the 2.0 and 2.1 kernels.
272 We do not use the kernel headers directly because we do not wish
273 to be dependant on a particular kernel version to compile insmod. */
276 /*======================================================================*/
277 /* The structures used by Linux 2.0. */
279 /* The symbol format used by get_kernel_syms(2). */
280 struct old_kernel_sym
286 struct old_module_ref
288 unsigned long module; /* kernel addresses */
292 struct old_module_symbol
298 struct old_symbol_table
300 int size; /* total, including string table!!! */
303 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
304 struct old_module_ref ref[0]; /* actual size defined by n_refs */
307 struct old_mod_routines
310 unsigned long cleanup;
316 unsigned long ref; /* the list of modules that refer to me */
317 unsigned long symtab;
319 int size; /* size of module in pages */
320 unsigned long addr; /* address of module */
322 unsigned long cleanup; /* cleanup routine */
325 /* Sent to init_module(2) or'ed into the code size parameter. */
326 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
328 int get_kernel_syms(struct old_kernel_sym *);
329 int old_sys_init_module(const char *name, char *code, unsigned codesize,
330 struct old_mod_routines *, struct old_symbol_table *);
332 /*======================================================================*/
333 /* For sizeof() which are related to the module platform and not to the
334 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
336 #define tgt_sizeof_char sizeof(char)
337 #define tgt_sizeof_short sizeof(short)
338 #define tgt_sizeof_int sizeof(int)
339 #define tgt_sizeof_long sizeof(long)
340 #define tgt_sizeof_char_p sizeof(char *)
341 #define tgt_sizeof_void_p sizeof(void *)
342 #define tgt_long long
344 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
345 #undef tgt_sizeof_long
346 #undef tgt_sizeof_char_p
347 #undef tgt_sizeof_void_p
349 static const int tgt_sizeof_long = 8;
350 static const int tgt_sizeof_char_p = 8;
351 static const int tgt_sizeof_void_p = 8;
352 #define tgt_long long long
355 /*======================================================================*/
356 /* The structures used in Linux 2.1. */
358 /* Note: new_module_symbol does not use tgt_long intentionally */
359 struct new_module_symbol
365 struct new_module_persist;
367 struct new_module_ref
369 unsigned tgt_long dep; /* kernel addresses */
370 unsigned tgt_long ref;
371 unsigned tgt_long next_ref;
376 unsigned tgt_long size_of_struct; /* == sizeof(module) */
377 unsigned tgt_long next;
378 unsigned tgt_long name;
379 unsigned tgt_long size;
382 unsigned tgt_long flags; /* AUTOCLEAN et al */
387 unsigned tgt_long syms;
388 unsigned tgt_long deps;
389 unsigned tgt_long refs;
390 unsigned tgt_long init;
391 unsigned tgt_long cleanup;
392 unsigned tgt_long ex_table_start;
393 unsigned tgt_long ex_table_end;
395 unsigned tgt_long gp;
397 /* Everything after here is extension. */
398 unsigned tgt_long persist_start;
399 unsigned tgt_long persist_end;
400 unsigned tgt_long can_unload;
401 unsigned tgt_long runsize;
402 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
403 const char *kallsyms_start; /* All symbols for kernel debugging */
404 const char *kallsyms_end;
405 const char *archdata_start; /* arch specific data for module */
406 const char *archdata_end;
407 const char *kernel_data; /* Reserved for kernel internal use */
412 #define ARCHDATA_SEC_NAME ARCHDATAM
414 #define ARCHDATA_SEC_NAME "__archdata"
416 #define KALLSYMS_SEC_NAME "__kallsyms"
419 struct new_module_info
427 /* Bits of module.flags. */
428 static const int NEW_MOD_RUNNING = 1;
429 static const int NEW_MOD_DELETED = 2;
430 static const int NEW_MOD_AUTOCLEAN = 4;
431 static const int NEW_MOD_VISITED = 8;
432 static const int NEW_MOD_USED_ONCE = 16;
434 int new_sys_init_module(const char *name, const struct new_module *);
435 int query_module(const char *name, int which, void *buf, size_t bufsize,
438 /* Values for query_module's which. */
440 static const int QM_MODULES = 1;
441 static const int QM_DEPS = 2;
442 static const int QM_REFS = 3;
443 static const int QM_SYMBOLS = 4;
444 static const int QM_INFO = 5;
446 /*======================================================================*/
447 /* The system calls unchanged between 2.0 and 2.1. */
449 unsigned long create_module(const char *, size_t);
450 int delete_module(const char *);
453 #endif /* module.h */
455 //----------------------------------------------------------------------------
456 //--------end of modutils module.h
457 //----------------------------------------------------------------------------
461 //----------------------------------------------------------------------------
462 //--------modutils obj.h, lines 253-462
463 //----------------------------------------------------------------------------
465 /* Elf object file loading and relocation routines.
466 Copyright 1996, 1997 Linux International.
468 Contributed by Richard Henderson <rth@tamu.edu>
470 This file is part of the Linux modutils.
472 This program is free software; you can redistribute it and/or modify it
473 under the terms of the GNU General Public License as published by the
474 Free Software Foundation; either version 2 of the License, or (at your
475 option) any later version.
477 This program is distributed in the hope that it will be useful, but
478 WITHOUT ANY WARRANTY; without even the implied warranty of
479 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
480 General Public License for more details.
482 You should have received a copy of the GNU General Public License
483 along with this program; if not, write to the Free Software Foundation,
484 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
487 #ifndef MODUTILS_OBJ_H
488 static const int MODUTILS_OBJ_H = 1;
490 #ident "$Id: insmod.c,v 1.106 2003/12/04 15:02:57 mjn3 Exp $"
492 /* The relocatable object is manipulated using elfin types. */
498 #if __BYTE_ORDER == __LITTLE_ENDIAN
499 #define ELFDATAM ELFDATA2LSB
500 #elif __BYTE_ORDER == __BIG_ENDIAN
501 #define ELFDATAM ELFDATA2MSB
505 # if ELFCLASSM == ELFCLASS32
506 # define ElfW(x) Elf32_ ## x
507 # define ELFW(x) ELF32_ ## x
509 # define ElfW(x) Elf64_ ## x
510 # define ELFW(x) ELF64_ ## x
514 /* For some reason this is missing from some ancient C libraries.... */
515 #ifndef ELF32_ST_INFO
516 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
519 #ifndef ELF64_ST_INFO
520 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
523 struct obj_string_patch;
524 struct obj_symbol_patch;
531 struct obj_section *load_next;
537 struct obj_symbol *next; /* hash table link */
541 int secidx; /* the defining section index/module */
543 int ksymidx; /* for export to the kernel symtab */
544 int referenced; /* actually used in the link */
547 /* Hardcode the hash table size. We shouldn't be needing so many
548 symbols that we begin to degrade performance, and we get a big win
549 by giving the compiler a constant divisor. */
551 #define HASH_BUCKETS 521
557 struct obj_section **sections;
558 struct obj_section *load_order;
559 struct obj_section **load_order_search_start;
560 struct obj_string_patch *string_patches;
561 struct obj_symbol_patch *symbol_patches;
562 int (*symbol_cmp)(const char *, const char *);
563 unsigned long (*symbol_hash)(const char *);
564 unsigned long local_symtab_size;
565 struct obj_symbol **local_symtab;
566 struct obj_symbol *symtab[HASH_BUCKETS];
577 struct obj_string_patch
579 struct obj_string_patch *next;
581 ElfW(Addr) reloc_offset;
582 ElfW(Addr) string_offset;
585 struct obj_symbol_patch
587 struct obj_symbol_patch *next;
589 ElfW(Addr) reloc_offset;
590 struct obj_symbol *sym;
594 /* Generic object manipulation routines. */
596 static unsigned long obj_elf_hash(const char *);
598 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
600 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
603 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
604 struct obj_symbol *sym);
606 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
607 static void obj_set_symbol_compare(struct obj_file *f,
608 int (*cmp)(const char *, const char *),
609 unsigned long (*hash)(const char *));
612 static struct obj_section *obj_find_section (struct obj_file *f,
615 static void obj_insert_section_load_order (struct obj_file *f,
616 struct obj_section *sec);
618 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
623 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
628 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
630 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
633 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
634 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
635 struct obj_symbol *sym);
638 static int obj_check_undefineds(struct obj_file *f);
640 static void obj_allocate_commons(struct obj_file *f);
642 static unsigned long obj_load_size (struct obj_file *f);
644 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
646 static struct obj_file *obj_load(FILE *f, int loadprogbits);
648 static int obj_create_image (struct obj_file *f, char *image);
650 /* Architecture specific manipulation routines. */
652 static struct obj_file *arch_new_file (void);
654 static struct obj_section *arch_new_section (void);
656 static struct obj_symbol *arch_new_symbol (void);
658 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
659 struct obj_section *targsec,
660 struct obj_section *symsec,
661 struct obj_symbol *sym,
662 ElfW(RelM) *rel, ElfW(Addr) value);
664 static void arch_create_got (struct obj_file *f);
666 static int obj_gpl_license(struct obj_file *f, const char **license);
668 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
669 static int arch_init_module (struct obj_file *f, struct new_module *);
673 //----------------------------------------------------------------------------
674 //--------end of modutils obj.h
675 //----------------------------------------------------------------------------
678 /* SPFX is always a string, so it can be concatenated to string constants. */
680 #define SPFX SYMBOL_PREFIX
686 #define _PATH_MODULES "/lib/modules"
687 static const int STRVERSIONLEN = 32;
689 /*======================================================================*/
691 static int flag_force_load = 0;
692 static int flag_autoclean = 0;
693 static int flag_verbose = 0;
694 static int flag_quiet = 0;
695 static int flag_export = 1;
698 /*======================================================================*/
700 #if defined(CONFIG_USE_LIST)
702 struct arch_list_entry
704 struct arch_list_entry *next;
705 CONFIG_LIST_ARCHTYPE addend;
712 #if defined(CONFIG_USE_SINGLE)
714 struct arch_single_entry
723 #if defined(__mips__)
726 struct mips_hi16 *next;
733 struct obj_file root;
734 #if defined(CONFIG_USE_PLT_ENTRIES)
735 struct obj_section *plt;
737 #if defined(CONFIG_USE_GOT_ENTRIES)
738 struct obj_section *got;
740 #if defined(__mips__)
741 struct mips_hi16 *mips_hi16_list;
746 struct obj_symbol root;
747 #if defined(CONFIG_USE_PLT_ENTRIES)
748 #if defined(CONFIG_USE_PLT_LIST)
749 struct arch_list_entry *pltent;
751 struct arch_single_entry pltent;
754 #if defined(CONFIG_USE_GOT_ENTRIES)
755 struct arch_single_entry gotent;
760 struct external_module {
765 struct new_module_symbol *syms;
768 static struct new_module_symbol *ksyms;
769 static size_t nksyms;
771 static struct external_module *ext_modules;
772 static int n_ext_modules;
773 static int n_ext_modules_used;
774 extern int delete_module(const char *);
776 static char *m_filename;
777 static char *m_fullName;
781 /*======================================================================*/
784 static int check_module_name_match(const char *filename, struct stat *statbuf,
787 char *fullname = (char *) userdata;
789 if (fullname[0] == '\0')
792 char *tmp, *tmp1 = bb_xstrdup(filename);
793 tmp = bb_get_last_path_component(tmp1);
794 if (strcmp(tmp, fullname) == 0) {
796 /* Stop searching if we find a match */
797 m_filename = bb_xstrdup(filename);
806 /*======================================================================*/
808 static struct obj_file *arch_new_file(void)
811 f = xmalloc(sizeof(*f));
813 memset(f, 0, sizeof(*f));
818 static struct obj_section *arch_new_section(void)
820 return xmalloc(sizeof(struct obj_section));
823 static struct obj_symbol *arch_new_symbol(void)
825 struct arch_symbol *sym;
826 sym = xmalloc(sizeof(*sym));
828 memset(sym, 0, sizeof(*sym));
833 static enum obj_reloc
834 arch_apply_relocation(struct obj_file *f,
835 struct obj_section *targsec,
836 struct obj_section *symsec,
837 struct obj_symbol *sym,
838 ElfW(RelM) *rel, ElfW(Addr) v)
840 struct arch_file *ifile = (struct arch_file *) f;
841 enum obj_reloc ret = obj_reloc_ok;
842 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
843 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
844 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
845 struct arch_symbol *isym = (struct arch_symbol *) sym;
847 #if defined(CONFIG_USE_GOT_ENTRIES)
848 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
850 #if defined(CONFIG_USE_PLT_ENTRIES)
851 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
853 #if defined(CONFIG_USE_PLT_LIST)
854 struct arch_list_entry *pe;
856 struct arch_single_entry *pe;
860 switch (ELF32_R_TYPE(rel->r_info)) {
875 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
876 * (which is .got) similar to branch,
877 * but is full 32 bits relative */
887 case R_ARM_GOTOFF: /* address relative to the got */
892 #elif defined(__s390__)
894 *(unsigned int *) loc += v;
897 *(unsigned short *) loc += v;
900 *(unsigned char *) loc += v;
904 *(unsigned int *) loc += v - dot;
907 *(unsigned short *) loc += (v - dot) >> 1;
910 *(unsigned short *) loc += v - dot;
915 /* find the plt entry and initialize it. */
916 assert(isym != NULL);
917 pe = (struct arch_single_entry *) &isym->pltent;
918 assert(pe->allocated);
919 if (pe->inited == 0) {
920 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
921 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
923 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
930 /* Insert relative distance to target. */
931 v = plt + pe->offset - dot;
932 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
933 *(unsigned int *) loc = (unsigned int) v;
934 else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
935 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
949 *(unsigned long *) loc += got - dot;
955 assert(isym != NULL);
957 if (!isym->gotent.inited)
959 isym->gotent.inited = 1;
960 *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
962 if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
963 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
964 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
965 *(unsigned short *) loc += isym->gotent.offset;
966 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
967 *(unsigned int *) loc += isym->gotent.offset;
970 #ifndef R_390_GOTOFF32
971 #define R_390_GOTOFF32 R_390_GOTOFF
978 #elif defined(__i386__)
1014 #elif defined(__mc68000__)
1025 ret = obj_reloc_overflow;
1032 ret = obj_reloc_overflow;
1039 if ((Elf32_Sword)v > 0x7f ||
1040 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
1041 ret = obj_reloc_overflow;
1048 if ((Elf32_Sword)v > 0x7fff ||
1049 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1050 ret = obj_reloc_overflow;
1056 *(int *)loc = v - dot;
1059 case R_68K_GLOB_DAT:
1060 case R_68K_JMP_SLOT:
1064 case R_68K_RELATIVE:
1065 *(int *)loc += f->baseaddr;
1076 #elif defined(__mips__)
1087 ret = obj_reloc_dangerous;
1088 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1089 ret = obj_reloc_overflow;
1091 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1097 struct mips_hi16 *n;
1099 /* We cannot relocate this one now because we don't know the value
1100 of the carry we need to add. Save the information, and let LO16
1101 do the actual relocation. */
1102 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1105 n->next = ifile->mips_hi16_list;
1106 ifile->mips_hi16_list = n;
1112 unsigned long insnlo = *loc;
1113 Elf32_Addr val, vallo;
1115 /* Sign extend the addend we extract from the lo insn. */
1116 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1118 if (ifile->mips_hi16_list != NULL) {
1119 struct mips_hi16 *l;
1121 l = ifile->mips_hi16_list;
1123 struct mips_hi16 *next;
1126 /* The value for the HI16 had best be the same. */
1127 assert(v == l->value);
1129 /* Do the HI16 relocation. Note that we actually don't
1130 need to know anything about the LO16 itself, except where
1131 to find the low 16 bits of the addend needed by the LO16. */
1134 ((insn & 0xffff) << 16) +
1138 /* Account for the sign extension that will happen in the
1145 insn = (insn & ~0xffff) | val;
1153 ifile->mips_hi16_list = NULL;
1156 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1158 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1163 #elif defined(__powerpc__)
1165 case R_PPC_ADDR16_HA:
1166 *(unsigned short *)loc = (v + 0x8000) >> 16;
1169 case R_PPC_ADDR16_HI:
1170 *(unsigned short *)loc = v >> 16;
1173 case R_PPC_ADDR16_LO:
1174 *(unsigned short *)loc = v;
1188 #elif defined(__sh__)
1211 *loc = f->baseaddr + rel->r_addend;
1216 *loc = got - dot + rel->r_addend;
1227 #if defined(__SH5__)
1228 case R_SH_IMM_MEDLOW16:
1229 case R_SH_IMM_LOW16:
1233 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1237 * movi and shori have the format:
1239 * | op | imm | reg | reserved |
1240 * 31..26 25..10 9.. 4 3 .. 0
1242 * so we simply mask and or in imm.
1244 word = *loc & ~0x3fffc00;
1245 word |= (v & 0xffff) << 10;
1252 case R_SH_IMM_MEDLOW16_PCREL:
1253 case R_SH_IMM_LOW16_PCREL:
1257 word = *loc & ~0x3fffc00;
1261 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1264 word |= (v & 0xffff) << 10;
1270 #endif /* __SH5__ */
1274 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1275 ret = obj_reloc_unhandled;
1278 #if defined (__v850e__)
1283 /* We write two shorts instead of a long because even
1284 32-bit insns only need half-word alignment, but
1285 32-bit data needs to be long-word aligned. */
1286 v += ((unsigned short *)loc)[0];
1287 v += ((unsigned short *)loc)[1] << 16;
1288 ((unsigned short *)loc)[0] = v & 0xffff;
1289 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1292 case R_V850_22_PCREL:
1296 #if defined (__cris__)
1301 /* CRIS keeps the relocation value in the r_addend field and
1302 * should not use whats in *loc at all
1308 #if defined(CONFIG_USE_PLT_ENTRIES)
1312 /* find the plt entry and initialize it if necessary */
1313 assert(isym != NULL);
1315 #if defined(CONFIG_USE_PLT_LIST)
1316 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1324 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1326 /* generate some machine code */
1328 #if defined(__arm__)
1329 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1330 ip[1] = v; /* sym@ */
1332 #if defined(__powerpc__)
1333 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1334 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1335 ip[2] = 0x7d6903a6; /* mtctr r11 */
1336 ip[3] = 0x4e800420; /* bctr */
1338 #if defined (__v850e__)
1339 /* We have to trash a register, so we assume that any control
1340 transfer more than 21-bits away must be a function call
1341 (so we can use a call-clobbered register). */
1342 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1343 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1348 /* relative distance to target */
1350 /* if the target is too far away.... */
1351 #if defined (__arm__) || defined (__powerpc__)
1352 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1353 #elif defined (__v850e__)
1354 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1356 /* go via the plt */
1357 v = plt + pe->offset - dot;
1359 #if defined (__v850e__)
1364 ret = obj_reloc_dangerous;
1366 /* merge the offset into the instruction. */
1367 #if defined(__arm__)
1368 /* Convert to words. */
1371 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1373 #if defined(__powerpc__)
1374 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1376 #if defined (__v850e__)
1377 /* We write two shorts instead of a long because even 32-bit insns
1378 only need half-word alignment, but the 32-bit data write needs
1379 to be long-word aligned. */
1380 ((unsigned short *)loc)[0] =
1381 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1382 | ((v >> 16) & 0x3f); /* offs high part */
1383 ((unsigned short *)loc)[1] =
1384 (v & 0xffff); /* offs low part */
1387 #endif /* CONFIG_USE_PLT_ENTRIES */
1389 #if defined(CONFIG_USE_GOT_ENTRIES)
1392 assert(isym != NULL);
1393 /* needs an entry in the .got: set it, once */
1394 if (!isym->gotent.inited) {
1395 isym->gotent.inited = 1;
1396 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1398 /* make the reloc with_respect_to_.got */
1400 *loc += isym->gotent.offset + rel->r_addend;
1401 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1402 *loc += isym->gotent.offset;
1406 #endif /* CONFIG_USE_GOT_ENTRIES */
1413 #if defined(CONFIG_USE_LIST)
1415 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1416 int offset, int size)
1418 struct arch_list_entry *pe;
1420 for (pe = *list; pe != NULL; pe = pe->next) {
1421 if (pe->addend == rel->r_addend) {
1427 pe = xmalloc(sizeof(struct arch_list_entry));
1429 pe->addend = rel->r_addend;
1430 pe->offset = offset;
1440 #if defined(CONFIG_USE_SINGLE)
1442 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1443 int offset, int size)
1445 if (single->allocated == 0) {
1446 single->allocated = 1;
1447 single->offset = offset;
1456 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1458 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1459 int offset, int size)
1461 struct obj_section *myrelsec = obj_find_section(f, name);
1468 obj_extend_section(myrelsec, offset);
1470 myrelsec = obj_create_alloced_section(f, name,
1480 static void arch_create_got(struct obj_file *f)
1482 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1483 struct arch_file *ifile = (struct arch_file *) f;
1485 #if defined(CONFIG_USE_GOT_ENTRIES)
1486 int got_offset = 0, got_needed = 0, got_allocate;
1488 #if defined(CONFIG_USE_PLT_ENTRIES)
1489 int plt_offset = 0, plt_needed = 0, plt_allocate;
1491 struct obj_section *relsec, *symsec, *strsec;
1492 ElfW(RelM) *rel, *relend;
1493 ElfW(Sym) *symtab, *extsym;
1494 const char *strtab, *name;
1495 struct arch_symbol *intsym;
1497 for (i = 0; i < f->header.e_shnum; ++i) {
1498 relsec = f->sections[i];
1499 if (relsec->header.sh_type != SHT_RELM)
1502 symsec = f->sections[relsec->header.sh_link];
1503 strsec = f->sections[symsec->header.sh_link];
1505 rel = (ElfW(RelM) *) relsec->contents;
1506 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1507 symtab = (ElfW(Sym) *) symsec->contents;
1508 strtab = (const char *) strsec->contents;
1510 for (; rel < relend; ++rel) {
1511 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1513 #if defined(CONFIG_USE_GOT_ENTRIES)
1516 #if defined(CONFIG_USE_PLT_ENTRIES)
1520 switch (ELF32_R_TYPE(rel->r_info)) {
1521 #if defined(__arm__)
1536 #elif defined(__i386__)
1546 #elif defined(__powerpc__)
1551 #elif defined(__mc68000__)
1560 #elif defined(__sh__)
1570 #elif defined (__v850e__)
1571 case R_V850_22_PCREL:
1580 if (extsym->st_name != 0) {
1581 name = strtab + extsym->st_name;
1583 name = f->sections[extsym->st_shndx]->name;
1585 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1586 #if defined(CONFIG_USE_GOT_ENTRIES)
1588 got_offset += arch_single_init(
1589 rel, &intsym->gotent,
1590 got_offset, CONFIG_GOT_ENTRY_SIZE);
1595 #if defined(CONFIG_USE_PLT_ENTRIES)
1597 #if defined(CONFIG_USE_PLT_LIST)
1598 plt_offset += arch_list_add(
1599 rel, &intsym->pltent,
1600 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1602 plt_offset += arch_single_init(
1603 rel, &intsym->pltent,
1604 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1612 #if defined(CONFIG_USE_GOT_ENTRIES)
1614 ifile->got = arch_xsect_init(f, ".got", got_offset,
1615 CONFIG_GOT_ENTRY_SIZE);
1619 #if defined(CONFIG_USE_PLT_ENTRIES)
1621 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1622 CONFIG_PLT_ENTRY_SIZE);
1626 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1629 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
1630 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1636 /*======================================================================*/
1638 /* Standard ELF hash function. */
1639 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1641 unsigned long h = 0;
1648 if ((g = (h & 0xf0000000)) != 0) {
1657 static unsigned long obj_elf_hash(const char *name)
1659 return obj_elf_hash_n(name, strlen(name));
1662 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1663 /* String comparison for non-co-versioned kernel and module. */
1665 static int ncv_strcmp(const char *a, const char *b)
1667 size_t alen = strlen(a), blen = strlen(b);
1669 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1670 return strncmp(a, b, alen);
1671 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1672 return strncmp(a, b, blen);
1674 return strcmp(a, b);
1677 /* String hashing for non-co-versioned kernel and module. Here
1678 we are simply forced to drop the crc from the hash. */
1680 static unsigned long ncv_symbol_hash(const char *str)
1682 size_t len = strlen(str);
1683 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1685 return obj_elf_hash_n(str, len);
1689 obj_set_symbol_compare(struct obj_file *f,
1690 int (*cmp) (const char *, const char *),
1691 unsigned long (*hash) (const char *))
1694 f->symbol_cmp = cmp;
1696 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1699 f->symbol_hash = hash;
1701 memcpy(tmptab, f->symtab, sizeof(tmptab));
1702 memset(f->symtab, 0, sizeof(f->symtab));
1704 for (i = 0; i < HASH_BUCKETS; ++i)
1705 for (sym = tmptab[i]; sym; sym = next) {
1706 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1708 sym->next = f->symtab[h];
1714 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1716 static struct obj_symbol *
1717 obj_add_symbol(struct obj_file *f, const char *name,
1718 unsigned long symidx, int info,
1719 int secidx, ElfW(Addr) value,
1722 struct obj_symbol *sym;
1723 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1724 int n_type = ELFW(ST_TYPE) (info);
1725 int n_binding = ELFW(ST_BIND) (info);
1727 for (sym = f->symtab[hash]; sym; sym = sym->next)
1728 if (f->symbol_cmp(sym->name, name) == 0) {
1729 int o_secidx = sym->secidx;
1730 int o_info = sym->info;
1731 int o_type = ELFW(ST_TYPE) (o_info);
1732 int o_binding = ELFW(ST_BIND) (o_info);
1734 /* A redefinition! Is it legal? */
1736 if (secidx == SHN_UNDEF)
1738 else if (o_secidx == SHN_UNDEF)
1740 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1741 /* Cope with local and global symbols of the same name
1742 in the same object file, as might have been created
1743 by ld -r. The only reason locals are now seen at this
1744 level at all is so that we can do semi-sensible things
1747 struct obj_symbol *nsym, **p;
1749 nsym = arch_new_symbol();
1750 nsym->next = sym->next;
1753 /* Excise the old (local) symbol from the hash chain. */
1754 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1758 } else if (n_binding == STB_LOCAL) {
1759 /* Another symbol of the same name has already been defined.
1760 Just add this to the local table. */
1761 sym = arch_new_symbol();
1764 f->local_symtab[symidx] = sym;
1766 } else if (n_binding == STB_WEAK)
1768 else if (o_binding == STB_WEAK)
1770 /* Don't unify COMMON symbols with object types the programmer
1772 else if (secidx == SHN_COMMON
1773 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1775 else if (o_secidx == SHN_COMMON
1776 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1779 /* Don't report an error if the symbol is coming from
1780 the kernel or some external module. */
1781 if (secidx <= SHN_HIRESERVE)
1782 bb_error_msg("%s multiply defined", name);
1787 /* Completely new symbol. */
1788 sym = arch_new_symbol();
1789 sym->next = f->symtab[hash];
1790 f->symtab[hash] = sym;
1793 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1794 if (symidx >= f->local_symtab_size)
1795 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1796 name, (long) symidx, (long) f->local_symtab_size);
1798 f->local_symtab[symidx] = sym;
1805 sym->secidx = secidx;
1811 static struct obj_symbol *
1812 obj_find_symbol(struct obj_file *f, const char *name)
1814 struct obj_symbol *sym;
1815 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1817 for (sym = f->symtab[hash]; sym; sym = sym->next)
1818 if (f->symbol_cmp(sym->name, name) == 0)
1825 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1828 if (sym->secidx >= SHN_LORESERVE)
1831 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1833 /* As a special case, a NULL sym has value zero. */
1838 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1840 int i, n = f->header.e_shnum;
1842 for (i = 0; i < n; ++i)
1843 if (strcmp(f->sections[i]->name, name) == 0)
1844 return f->sections[i];
1849 static int obj_load_order_prio(struct obj_section *a)
1851 unsigned long af, ac;
1853 af = a->header.sh_flags;
1856 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1857 strcmp(a->name + 5, ".init"))
1861 if (!(af & SHF_WRITE))
1863 if (af & SHF_EXECINSTR)
1865 if (a->header.sh_type != SHT_NOBITS)
1872 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1874 struct obj_section **p;
1875 int prio = obj_load_order_prio(sec);
1876 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1877 if (obj_load_order_prio(*p) < prio)
1879 sec->load_next = *p;
1883 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1885 unsigned long align,
1888 int newidx = f->header.e_shnum++;
1889 struct obj_section *sec;
1891 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1892 f->sections[newidx] = sec = arch_new_section();
1894 memset(sec, 0, sizeof(*sec));
1895 sec->header.sh_type = SHT_PROGBITS;
1896 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1897 sec->header.sh_size = size;
1898 sec->header.sh_addralign = align;
1902 sec->contents = xmalloc(size);
1904 obj_insert_section_load_order(f, sec);
1909 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1911 unsigned long align,
1914 int newidx = f->header.e_shnum++;
1915 struct obj_section *sec;
1917 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1918 f->sections[newidx] = sec = arch_new_section();
1920 memset(sec, 0, sizeof(*sec));
1921 sec->header.sh_type = SHT_PROGBITS;
1922 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1923 sec->header.sh_size = size;
1924 sec->header.sh_addralign = align;
1928 sec->contents = xmalloc(size);
1930 sec->load_next = f->load_order;
1931 f->load_order = sec;
1932 if (f->load_order_search_start == &f->load_order)
1933 f->load_order_search_start = &sec->load_next;
1938 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1940 unsigned long oldsize = sec->header.sh_size;
1942 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1944 return sec->contents + oldsize;
1948 /* Conditionally add the symbols from the given symbol set to the
1954 int idx, struct new_module_symbol *syms, size_t nsyms)
1956 struct new_module_symbol *s;
1959 #ifdef SYMBOL_PREFIX
1961 size_t name_alloced_size = 0;
1963 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1966 gpl = obj_gpl_license(f, NULL) == 0;
1968 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1969 /* Only add symbols that are already marked external.
1970 If we override locals we may cause problems for
1971 argument initialization. We will also create a false
1972 dependency on the module. */
1973 struct obj_symbol *sym;
1976 /* GPL licensed modules can use symbols exported with
1977 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
1978 * exported names. Non-GPL modules never see any GPLONLY_
1979 * symbols so they cannot fudge it by adding the prefix on
1982 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
1983 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1985 ((char *)s->name) += 8;
1990 name = (char *)s->name;
1992 #ifdef SYMBOL_PREFIX
1993 /* Prepend SYMBOL_PREFIX to the symbol's name (the
1994 kernel exports `C names', but module object files
1995 reference `linker names'). */
1996 size_t extra = sizeof SYMBOL_PREFIX;
1997 size_t name_size = strlen (name) + extra;
1998 if (name_size > name_alloced_size) {
1999 name_alloced_size = name_size * 2;
2000 name_buf = alloca (name_alloced_size);
2002 strcpy (name_buf, SYMBOL_PREFIX);
2003 strcpy (name_buf + extra - 1, name);
2005 #endif /* SYMBOL_PREFIX */
2007 sym = obj_find_symbol(f, name);
2008 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
2009 #ifdef SYMBOL_PREFIX
2010 /* Put NAME_BUF into more permanent storage. */
2011 name = xmalloc (name_size);
2012 strcpy (name, name_buf);
2014 sym = obj_add_symbol(f, name, -1,
2015 ELFW(ST_INFO) (STB_GLOBAL,
2018 /* Did our symbol just get installed? If so, mark the
2019 module as "used". */
2020 if (sym->secidx == idx)
2028 static void add_kernel_symbols(struct obj_file *f)
2030 struct external_module *m;
2033 /* Add module symbols first. */
2035 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2037 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2038 m->nsyms)) m->used = 1, ++nused;
2040 n_ext_modules_used = nused;
2042 /* And finally the symbols from the kernel proper. */
2045 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2048 static char *get_modinfo_value(struct obj_file *f, const char *key)
2050 struct obj_section *sec;
2051 char *p, *v, *n, *ep;
2052 size_t klen = strlen(key);
2054 sec = obj_find_section(f, ".modinfo");
2058 ep = p + sec->header.sh_size;
2061 n = strchr(p, '\0');
2063 if (p + klen == v && strncmp(p, key, klen) == 0)
2066 if (p + klen == n && strcmp(p, key) == 0)
2076 /*======================================================================*/
2077 /* Functions relating to module loading in pre 2.1 kernels. */
2080 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
2084 struct obj_symbol *sym;
2088 if ((q = strchr(p, '=')) == NULL) {
2094 sym = obj_find_symbol(f, p);
2096 /* Also check that the parameter was not resolved from the kernel. */
2097 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2098 bb_error_msg("symbol for parameter %s not found", p);
2102 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
2104 /* Do C quoting if we begin with a ". */
2108 str = alloca(strlen(q));
2109 for (r = str, q++; *q != '"'; ++q, ++r) {
2111 bb_error_msg("improperly terminated string argument for %s", p);
2113 } else if (*q == '\\')
2147 if (q[1] >= '0' && q[1] <= '7') {
2148 c = (c * 8) + *++q - '0';
2149 if (q[1] >= '0' && q[1] <= '7')
2150 c = (c * 8) + *++q - '0';
2163 obj_string_patch(f, sym->secidx, sym->value, str);
2164 } else if (*q >= '0' && *q <= '9') {
2166 *loc++ = strtoul(q, &q, 0);
2167 while (*q++ == ',');
2169 char *contents = f->sections[sym->secidx]->contents;
2170 char *myloc = contents + sym->value;
2171 char *r; /* To search for commas */
2173 /* Break the string with comas */
2174 while ((r = strchr(q, ',')) != (char *) NULL) {
2176 obj_string_patch(f, sym->secidx, myloc - contents, q);
2177 myloc += sizeof(char *);
2182 obj_string_patch(f, sym->secidx, myloc - contents, q);
2191 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2192 static int old_is_module_checksummed(struct obj_file *f)
2194 return obj_find_symbol(f, "Using_Versions") != NULL;
2196 /* Get the module's kernel version in the canonical integer form. */
2199 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2201 struct obj_symbol *sym;
2205 sym = obj_find_symbol(f, "kernel_version");
2209 p = f->sections[sym->secidx]->contents + sym->value;
2210 safe_strncpy(str, p, STRVERSIONLEN);
2212 a = strtoul(p, &p, 10);
2215 b = strtoul(p + 1, &p, 10);
2218 c = strtoul(p + 1, &q, 10);
2222 return a << 16 | b << 8 | c;
2225 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2227 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
2229 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
2231 static int old_get_kernel_symbols(const char *m_name)
2233 struct old_kernel_sym *ks, *k;
2234 struct new_module_symbol *s;
2235 struct external_module *mod;
2236 int nks, nms, nmod, i;
2238 nks = get_kernel_syms(NULL);
2241 bb_perror_msg("get_kernel_syms: %s", m_name);
2243 bb_error_msg("No kernel symbols");
2247 ks = k = xmalloc(nks * sizeof(*ks));
2249 if (get_kernel_syms(ks) != nks) {
2250 perror("inconsistency with get_kernel_syms -- is someone else "
2251 "playing with modules?");
2256 /* Collect the module information. */
2261 while (k->name[0] == '#' && k->name[1]) {
2262 struct old_kernel_sym *k2;
2264 /* Find out how many symbols this module has. */
2265 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
2269 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
2270 mod[nmod].name = k->name + 1;
2271 mod[nmod].addr = k->value;
2273 mod[nmod].nsyms = nms;
2274 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2276 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
2277 s->name = (unsigned long) k->name;
2278 s->value = k->value;
2285 n_ext_modules = nmod + 1;
2287 /* Now collect the symbols for the kernel proper. */
2289 if (k->name[0] == '#')
2292 nksyms = nms = nks - (k - ks);
2293 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2295 for (i = 0; i < nms; ++i, ++s, ++k) {
2296 s->name = (unsigned long) k->name;
2297 s->value = k->value;
2303 /* Return the kernel symbol checksum version, or zero if not used. */
2305 static int old_is_kernel_checksummed(void)
2307 /* Using_Versions is the first symbol. */
2309 && strcmp((char *) ksyms[0].name,
2310 "Using_Versions") == 0) return ksyms[0].value;
2316 static int old_create_mod_use_count(struct obj_file *f)
2318 struct obj_section *sec;
2320 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2323 obj_add_symbol(f, "mod_use_count_", -1,
2324 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2331 old_init_module(const char *m_name, struct obj_file *f,
2332 unsigned long m_size)
2335 struct old_mod_routines routines;
2336 struct old_symbol_table *symtab;
2339 /* Create the symbol table */
2341 int nsyms = 0, strsize = 0, total;
2343 /* Size things first... */
2346 for (i = 0; i < HASH_BUCKETS; ++i) {
2347 struct obj_symbol *sym;
2348 for (sym = f->symtab[i]; sym; sym = sym->next)
2349 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2350 && sym->secidx <= SHN_HIRESERVE)
2352 sym->ksymidx = nsyms++;
2353 strsize += strlen(sym->name) + 1;
2358 total = (sizeof(struct old_symbol_table)
2359 + nsyms * sizeof(struct old_module_symbol)
2360 + n_ext_modules_used * sizeof(struct old_module_ref)
2362 symtab = xmalloc(total);
2363 symtab->size = total;
2364 symtab->n_symbols = nsyms;
2365 symtab->n_refs = n_ext_modules_used;
2367 if (flag_export && nsyms) {
2368 struct old_module_symbol *ksym;
2372 ksym = symtab->symbol;
2373 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2374 + n_ext_modules_used * sizeof(struct old_module_ref));
2376 for (i = 0; i < HASH_BUCKETS; ++i) {
2377 struct obj_symbol *sym;
2378 for (sym = f->symtab[i]; sym; sym = sym->next)
2379 if (sym->ksymidx >= 0) {
2380 ksym->addr = obj_symbol_final_value(f, sym);
2382 (unsigned long) str - (unsigned long) symtab;
2384 strcpy(str, sym->name);
2385 str += strlen(sym->name) + 1;
2391 if (n_ext_modules_used) {
2392 struct old_module_ref *ref;
2395 ref = (struct old_module_ref *)
2396 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2398 for (i = 0; i < n_ext_modules; ++i)
2399 if (ext_modules[i].used)
2400 ref++->module = ext_modules[i].addr;
2404 /* Fill in routines. */
2407 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2409 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2411 /* Whew! All of the initialization is complete. Collect the final
2412 module image and give it to the kernel. */
2414 image = xmalloc(m_size);
2415 obj_create_image(f, image);
2417 /* image holds the complete relocated module, accounting correctly for
2418 mod_use_count. However the old module kernel support assume that
2419 it is receiving something which does not contain mod_use_count. */
2420 ret = old_sys_init_module(m_name, image + sizeof(long),
2421 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2422 : 0), &routines, symtab);
2424 bb_perror_msg("init_module: %s", m_name);
2434 #define old_create_mod_use_count(x) TRUE
2435 #define old_init_module(x, y, z) TRUE
2437 #endif /* CONFIG_FEATURE_OLD_MODULE_INTERFACE */
2441 /*======================================================================*/
2442 /* Functions relating to module loading after 2.1.18. */
2445 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2448 char *p, *q, *key, *sym_name;
2449 struct obj_symbol *sym;
2450 char *contents, *loc;
2454 if ((q = strchr(p, '=')) == NULL) {
2459 key = alloca(q - p + 6);
2460 memcpy(key, "parm_", 5);
2461 memcpy(key + 5, p, q - p);
2464 p = get_modinfo_value(f, key);
2467 bb_error_msg("invalid parameter %s", key);
2471 #ifdef SYMBOL_PREFIX
2472 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2473 strcpy (sym_name, SYMBOL_PREFIX);
2474 strcat (sym_name, key);
2478 sym = obj_find_symbol(f, sym_name);
2480 /* Also check that the parameter was not resolved from the kernel. */
2481 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2482 bb_error_msg("symbol for parameter %s not found", key);
2487 min = strtoul(p, &p, 10);
2489 max = strtoul(p + 1, &p, 10);
2495 contents = f->sections[sym->secidx]->contents;
2496 loc = contents + sym->value;
2500 if ((*p == 's') || (*p == 'c')) {
2503 /* Do C quoting if we begin with a ", else slurp the lot. */
2507 str = alloca(strlen(q));
2508 for (r = str, q++; *q != '"'; ++q, ++r) {
2510 bb_error_msg("improperly terminated string argument for %s",
2513 } else if (*q == '\\')
2547 if (q[1] >= '0' && q[1] <= '7') {
2548 c = (c * 8) + *++q - '0';
2549 if (q[1] >= '0' && q[1] <= '7')
2550 c = (c * 8) + *++q - '0';
2567 /* In this case, the string is not quoted. We will break
2568 it using the coma (like for ints). If the user wants to
2569 include comas in a string, he just has to quote it */
2571 /* Search the next coma */
2575 if (r != (char *) NULL) {
2576 /* Recopy the current field */
2577 str = alloca(r - q + 1);
2578 memcpy(str, q, r - q);
2580 /* I don't know if it is usefull, as the previous case
2581 doesn't null terminate the string ??? */
2584 /* Keep next fields */
2595 obj_string_patch(f, sym->secidx, loc - contents, str);
2596 loc += tgt_sizeof_char_p;
2598 /* Array of chars (in fact, matrix !) */
2599 unsigned long charssize; /* size of each member */
2601 /* Get the size of each member */
2602 /* Probably we should do that outside the loop ? */
2603 if (!isdigit(*(p + 1))) {
2604 bb_error_msg("parameter type 'c' for %s must be followed by"
2605 " the maximum size", key);
2608 charssize = strtoul(p + 1, (char **) NULL, 10);
2611 if (strlen(str) >= charssize) {
2612 bb_error_msg("string too long for %s (max %ld)", key,
2617 /* Copy to location */
2618 strcpy((char *) loc, str);
2622 long v = strtoul(q, &q, 0);
2629 loc += tgt_sizeof_short;
2633 loc += tgt_sizeof_int;
2637 loc += tgt_sizeof_long;
2641 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2656 goto retry_end_of_value;
2660 bb_error_msg("too many values for %s (max %d)", key, max);
2667 bb_error_msg("invalid argument syntax for %s", key);
2674 bb_error_msg("too few values for %s (min %d)", key, min);
2684 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2685 static int new_is_module_checksummed(struct obj_file *f)
2687 const char *p = get_modinfo_value(f, "using_checksums");
2694 /* Get the module's kernel version in the canonical integer form. */
2697 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2702 p = get_modinfo_value(f, "kernel_version");
2705 safe_strncpy(str, p, STRVERSIONLEN);
2707 a = strtoul(p, &p, 10);
2710 b = strtoul(p + 1, &p, 10);
2713 c = strtoul(p + 1, &q, 10);
2717 return a << 16 | b << 8 | c;
2720 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2723 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2725 /* Fetch the loaded modules, and all currently exported symbols. */
2727 static int new_get_kernel_symbols(void)
2729 char *module_names, *mn;
2730 struct external_module *modules, *m;
2731 struct new_module_symbol *syms, *s;
2732 size_t ret, bufsize, nmod, nsyms, i, j;
2734 /* Collect the loaded modules. */
2736 module_names = xmalloc(bufsize = 256);
2738 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2739 if (errno == ENOSPC && bufsize < ret) {
2740 module_names = xrealloc(module_names, bufsize = ret);
2741 goto retry_modules_load;
2743 bb_perror_msg("QM_MODULES");
2747 n_ext_modules = nmod = ret;
2749 /* Collect the modules' symbols. */
2752 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2753 memset(modules, 0, nmod * sizeof(*modules));
2754 for (i = 0, mn = module_names, m = modules;
2755 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2756 struct new_module_info info;
2758 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2759 if (errno == ENOENT) {
2760 /* The module was removed out from underneath us. */
2763 bb_perror_msg("query_module: QM_INFO: %s", mn);
2767 syms = xmalloc(bufsize = 1024);
2769 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2772 syms = xrealloc(syms, bufsize = ret);
2773 goto retry_mod_sym_load;
2775 /* The module was removed out from underneath us. */
2778 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2785 m->addr = info.addr;
2789 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2790 s->name += (unsigned long) syms;
2795 /* Collect the kernel's symbols. */
2797 syms = xmalloc(bufsize = 16 * 1024);
2798 retry_kern_sym_load:
2799 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2800 if (errno == ENOSPC && bufsize < ret) {
2801 syms = xrealloc(syms, bufsize = ret);
2802 goto retry_kern_sym_load;
2804 bb_perror_msg("kernel: QM_SYMBOLS");
2807 nksyms = nsyms = ret;
2810 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2811 s->name += (unsigned long) syms;
2817 /* Return the kernel symbol checksum version, or zero if not used. */
2819 static int new_is_kernel_checksummed(void)
2821 struct new_module_symbol *s;
2824 /* Using_Versions is not the first symbol, but it should be in there. */
2826 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2827 if (strcmp((char *) s->name, "Using_Versions") == 0)
2834 static int new_create_this_module(struct obj_file *f, const char *m_name)
2836 struct obj_section *sec;
2838 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2839 sizeof(struct new_module));
2840 memset(sec->contents, 0, sizeof(struct new_module));
2842 obj_add_symbol(f, SPFX "__this_module", -1,
2843 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2844 sizeof(struct new_module));
2846 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2852 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2853 /* add an entry to the __ksymtab section, creating it if necessary */
2854 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2856 struct obj_section *sec;
2859 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2860 * If __ksymtab is defined but not marked alloc, x out the first character
2861 * (no obj_delete routine) and create a new __ksymtab with the correct
2864 sec = obj_find_section(f, "__ksymtab");
2865 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2866 *((char *)(sec->name)) = 'x'; /* override const */
2870 sec = obj_create_alloced_section(f, "__ksymtab",
2871 tgt_sizeof_void_p, 0);
2874 sec->header.sh_flags |= SHF_ALLOC;
2875 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2877 ofs = sec->header.sh_size;
2878 obj_symbol_patch(f, sec->idx, ofs, sym);
2879 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2880 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2882 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2884 static int new_create_module_ksymtab(struct obj_file *f)
2886 struct obj_section *sec;
2889 /* We must always add the module references. */
2891 if (n_ext_modules_used) {
2892 struct new_module_ref *dep;
2893 struct obj_symbol *tm;
2895 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2896 (sizeof(struct new_module_ref)
2897 * n_ext_modules_used));
2901 tm = obj_find_symbol(f, SPFX "__this_module");
2902 dep = (struct new_module_ref *) sec->contents;
2903 for (i = 0; i < n_ext_modules; ++i)
2904 if (ext_modules[i].used) {
2905 dep->dep = ext_modules[i].addr;
2906 obj_symbol_patch(f, sec->idx,
2907 (char *) &dep->ref - sec->contents, tm);
2913 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2918 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2921 /* We don't want to export symbols residing in sections that
2922 aren't loaded. There are a number of these created so that
2923 we make sure certain module options don't appear twice. */
2925 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2927 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2929 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2930 struct obj_symbol *sym;
2931 for (sym = f->symtab[i]; sym; sym = sym->next)
2932 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2933 && sym->secidx <= SHN_HIRESERVE
2934 && (sym->secidx >= SHN_LORESERVE
2935 || loaded[sym->secidx])) {
2936 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2938 obj_symbol_patch(f, sec->idx, ofs, sym);
2939 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2946 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2954 new_init_module(const char *m_name, struct obj_file *f,
2955 unsigned long m_size)
2957 struct new_module *module;
2958 struct obj_section *sec;
2963 sec = obj_find_section(f, ".this");
2964 if (!sec || !sec->contents) {
2965 bb_perror_msg_and_die("corrupt module %s?",m_name);
2967 module = (struct new_module *) sec->contents;
2968 m_addr = sec->header.sh_addr;
2970 module->size_of_struct = sizeof(*module);
2971 module->size = m_size;
2972 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2974 sec = obj_find_section(f, "__ksymtab");
2975 if (sec && sec->header.sh_size) {
2976 module->syms = sec->header.sh_addr;
2977 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2980 if (n_ext_modules_used) {
2981 sec = obj_find_section(f, ".kmodtab");
2982 module->deps = sec->header.sh_addr;
2983 module->ndeps = n_ext_modules_used;
2987 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2989 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2991 sec = obj_find_section(f, "__ex_table");
2993 module->ex_table_start = sec->header.sh_addr;
2994 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2997 sec = obj_find_section(f, ".text.init");
2999 module->runsize = sec->header.sh_addr - m_addr;
3001 sec = obj_find_section(f, ".data.init");
3003 if (!module->runsize ||
3004 module->runsize > sec->header.sh_addr - m_addr)
3005 module->runsize = sec->header.sh_addr - m_addr;
3007 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
3008 if (sec && sec->header.sh_size) {
3009 module->archdata_start = (void*)sec->header.sh_addr;
3010 module->archdata_end = module->archdata_start + sec->header.sh_size;
3012 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
3013 if (sec && sec->header.sh_size) {
3014 module->kallsyms_start = (void*)sec->header.sh_addr;
3015 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
3018 if (!arch_init_module(f, module))
3021 /* Whew! All of the initialization is complete. Collect the final
3022 module image and give it to the kernel. */
3024 image = xmalloc(m_size);
3025 obj_create_image(f, image);
3027 ret = new_sys_init_module(m_name, (struct new_module *) image);
3029 bb_perror_msg("init_module: %s", m_name);
3038 #define new_init_module(x, y, z) TRUE
3039 #define new_create_this_module(x, y) 0
3040 #define new_add_ksymtab(x, y) -1
3041 #define new_create_module_ksymtab(x)
3042 #define query_module(v, w, x, y, z) -1
3044 #endif /* CONFIG_FEATURE_NEW_MODULE_INTERFACE */
3047 /*======================================================================*/
3050 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3053 struct obj_string_patch *p;
3054 struct obj_section *strsec;
3055 size_t len = strlen(string) + 1;
3058 p = xmalloc(sizeof(*p));
3059 p->next = f->string_patches;
3060 p->reloc_secidx = secidx;
3061 p->reloc_offset = offset;
3062 f->string_patches = p;
3064 strsec = obj_find_section(f, ".kstrtab");
3065 if (strsec == NULL) {
3066 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3067 p->string_offset = 0;
3068 loc = strsec->contents;
3070 p->string_offset = strsec->header.sh_size;
3071 loc = obj_extend_section(strsec, len);
3073 memcpy(loc, string, len);
3078 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
3080 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3081 struct obj_symbol *sym)
3083 struct obj_symbol_patch *p;
3085 p = xmalloc(sizeof(*p));
3086 p->next = f->symbol_patches;
3087 p->reloc_secidx = secidx;
3088 p->reloc_offset = offset;
3090 f->symbol_patches = p;
3096 static int obj_check_undefineds(struct obj_file *f)
3101 for (i = 0; i < HASH_BUCKETS; ++i) {
3102 struct obj_symbol *sym;
3103 for (sym = f->symtab[i]; sym; sym = sym->next)
3104 if (sym->secidx == SHN_UNDEF) {
3105 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
3106 sym->secidx = SHN_ABS;
3110 bb_error_msg("unresolved symbol %s", sym->name);
3120 static void obj_allocate_commons(struct obj_file *f)
3122 struct common_entry {
3123 struct common_entry *next;
3124 struct obj_symbol *sym;
3125 } *common_head = NULL;
3129 for (i = 0; i < HASH_BUCKETS; ++i) {
3130 struct obj_symbol *sym;
3131 for (sym = f->symtab[i]; sym; sym = sym->next)
3132 if (sym->secidx == SHN_COMMON) {
3133 /* Collect all COMMON symbols and sort them by size so as to
3134 minimize space wasted by alignment requirements. */
3136 struct common_entry **p, *n;
3137 for (p = &common_head; *p; p = &(*p)->next)
3138 if (sym->size <= (*p)->sym->size)
3141 n = alloca(sizeof(*n));
3149 for (i = 1; i < f->local_symtab_size; ++i) {
3150 struct obj_symbol *sym = f->local_symtab[i];
3151 if (sym && sym->secidx == SHN_COMMON) {
3152 struct common_entry **p, *n;
3153 for (p = &common_head; *p; p = &(*p)->next)
3154 if (sym == (*p)->sym)
3156 else if (sym->size < (*p)->sym->size) {
3157 n = alloca(sizeof(*n));
3167 /* Find the bss section. */
3168 for (i = 0; i < f->header.e_shnum; ++i)
3169 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3172 /* If for some reason there hadn't been one, create one. */
3173 if (i == f->header.e_shnum) {
3174 struct obj_section *sec;
3176 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3177 f->sections[i] = sec = arch_new_section();
3178 f->header.e_shnum = i + 1;
3180 memset(sec, 0, sizeof(*sec));
3181 sec->header.sh_type = SHT_PROGBITS;
3182 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3187 /* Allocate the COMMONS. */
3189 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3190 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3191 struct common_entry *c;
3193 for (c = common_head; c; c = c->next) {
3194 ElfW(Addr) align = c->sym->value;
3196 if (align > max_align)
3198 if (bss_size & (align - 1))
3199 bss_size = (bss_size | (align - 1)) + 1;
3202 c->sym->value = bss_size;
3204 bss_size += c->sym->size;
3207 f->sections[i]->header.sh_size = bss_size;
3208 f->sections[i]->header.sh_addralign = max_align;
3212 /* For the sake of patch relocation and parameter initialization,
3213 allocate zeroed data for NOBITS sections now. Note that after
3214 this we cannot assume NOBITS are really empty. */
3215 for (i = 0; i < f->header.e_shnum; ++i) {
3216 struct obj_section *s = f->sections[i];
3217 if (s->header.sh_type == SHT_NOBITS) {
3218 if (s->header.sh_size != 0)
3219 s->contents = memset(xmalloc(s->header.sh_size),
3220 0, s->header.sh_size);
3224 s->header.sh_type = SHT_PROGBITS;
3229 static unsigned long obj_load_size(struct obj_file *f)
3231 unsigned long dot = 0;
3232 struct obj_section *sec;
3234 /* Finalize the positions of the sections relative to one another. */
3236 for (sec = f->load_order; sec; sec = sec->load_next) {
3239 align = sec->header.sh_addralign;
3240 if (align && (dot & (align - 1)))
3241 dot = (dot | (align - 1)) + 1;
3243 sec->header.sh_addr = dot;
3244 dot += sec->header.sh_size;
3250 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3252 int i, n = f->header.e_shnum;
3255 /* Finalize the addresses of the sections. */
3258 for (i = 0; i < n; ++i)
3259 f->sections[i]->header.sh_addr += base;
3261 /* And iterate over all of the relocations. */
3263 for (i = 0; i < n; ++i) {
3264 struct obj_section *relsec, *symsec, *targsec, *strsec;
3265 ElfW(RelM) * rel, *relend;
3269 relsec = f->sections[i];
3270 if (relsec->header.sh_type != SHT_RELM)
3273 symsec = f->sections[relsec->header.sh_link];
3274 targsec = f->sections[relsec->header.sh_info];
3275 strsec = f->sections[symsec->header.sh_link];
3277 rel = (ElfW(RelM) *) relsec->contents;
3278 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3279 symtab = (ElfW(Sym) *) symsec->contents;
3280 strtab = (const char *) strsec->contents;
3282 for (; rel < relend; ++rel) {
3283 ElfW(Addr) value = 0;
3284 struct obj_symbol *intsym = NULL;
3285 unsigned long symndx;
3286 ElfW(Sym) * extsym = 0;
3289 /* Attempt to find a value to use for this relocation. */
3291 symndx = ELFW(R_SYM) (rel->r_info);
3293 /* Note we've already checked for undefined symbols. */
3295 extsym = &symtab[symndx];
3296 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
3297 /* Local symbols we look up in the local table to be sure
3298 we get the one that is really intended. */
3299 intsym = f->local_symtab[symndx];
3301 /* Others we look up in the hash table. */
3303 if (extsym->st_name)
3304 name = strtab + extsym->st_name;
3306 name = f->sections[extsym->st_shndx]->name;
3307 intsym = obj_find_symbol(f, name);
3310 value = obj_symbol_final_value(f, intsym);
3311 intsym->referenced = 1;
3313 #if SHT_RELM == SHT_RELA
3314 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3315 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3316 if (!extsym || !extsym->st_name ||
3317 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
3319 value += rel->r_addend;
3323 switch (arch_apply_relocation
3324 (f, targsec, symsec, intsym, rel, value)) {
3328 case obj_reloc_overflow:
3329 errmsg = "Relocation overflow";
3331 case obj_reloc_dangerous:
3332 errmsg = "Dangerous relocation";
3334 case obj_reloc_unhandled:
3335 errmsg = "Unhandled relocation";
3338 bb_error_msg("%s of type %ld for %s", errmsg,
3339 (long) ELFW(R_TYPE) (rel->r_info),
3340 strtab + extsym->st_name);
3342 bb_error_msg("%s of type %ld", errmsg,
3343 (long) ELFW(R_TYPE) (rel->r_info));
3351 /* Finally, take care of the patches. */
3353 if (f->string_patches) {
3354 struct obj_string_patch *p;
3355 struct obj_section *strsec;
3356 ElfW(Addr) strsec_base;
3357 strsec = obj_find_section(f, ".kstrtab");
3358 strsec_base = strsec->header.sh_addr;
3360 for (p = f->string_patches; p; p = p->next) {
3361 struct obj_section *targsec = f->sections[p->reloc_secidx];
3362 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3363 = strsec_base + p->string_offset;
3367 if (f->symbol_patches) {
3368 struct obj_symbol_patch *p;
3370 for (p = f->symbol_patches; p; p = p->next) {
3371 struct obj_section *targsec = f->sections[p->reloc_secidx];
3372 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3373 = obj_symbol_final_value(f, p->sym);
3380 static int obj_create_image(struct obj_file *f, char *image)
3382 struct obj_section *sec;
3383 ElfW(Addr) base = f->baseaddr;
3385 for (sec = f->load_order; sec; sec = sec->load_next) {
3388 if (sec->contents == 0 || sec->header.sh_size == 0)
3391 secimg = image + (sec->header.sh_addr - base);
3393 /* Note that we allocated data for NOBITS sections earlier. */
3394 memcpy(secimg, sec->contents, sec->header.sh_size);
3400 /*======================================================================*/
3402 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3405 ElfW(Shdr) * section_headers;
3409 /* Read the file header. */
3411 f = arch_new_file();
3412 memset(f, 0, sizeof(*f));
3413 f->symbol_cmp = strcmp;
3414 f->symbol_hash = obj_elf_hash;
3415 f->load_order_search_start = &f->load_order;
3417 fseek(fp, 0, SEEK_SET);
3418 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3419 bb_perror_msg("error reading ELF header");
3423 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3424 || f->header.e_ident[EI_MAG1] != ELFMAG1
3425 || f->header.e_ident[EI_MAG2] != ELFMAG2
3426 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3427 bb_error_msg("not an ELF file");
3430 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3431 || f->header.e_ident[EI_DATA] != ELFDATAM
3432 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3433 || !MATCH_MACHINE(f->header.e_machine)) {
3434 bb_error_msg("ELF file not for this architecture");
3437 if (f->header.e_type != ET_REL) {
3438 bb_error_msg("ELF file not a relocatable object");
3442 /* Read the section headers. */
3444 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3445 bb_error_msg("section header size mismatch: %lu != %lu",
3446 (unsigned long) f->header.e_shentsize,
3447 (unsigned long) sizeof(ElfW(Shdr)));
3451 shnum = f->header.e_shnum;
3452 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3453 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3455 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3456 fseek(fp, f->header.e_shoff, SEEK_SET);
3457 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3458 bb_perror_msg("error reading ELF section headers");
3462 /* Read the section data. */
3464 for (i = 0; i < shnum; ++i) {
3465 struct obj_section *sec;
3467 f->sections[i] = sec = arch_new_section();
3468 memset(sec, 0, sizeof(*sec));
3470 sec->header = section_headers[i];
3473 if(sec->header.sh_size) switch (sec->header.sh_type) {
3482 if (!loadprogbits) {
3483 sec->contents = NULL;
3490 if (sec->header.sh_size > 0) {
3491 sec->contents = xmalloc(sec->header.sh_size);
3492 fseek(fp, sec->header.sh_offset, SEEK_SET);
3493 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3494 bb_perror_msg("error reading ELF section data");
3498 sec->contents = NULL;
3502 #if SHT_RELM == SHT_REL
3504 bb_error_msg("RELA relocations not supported on this architecture");
3508 bb_error_msg("REL relocations not supported on this architecture");
3513 if (sec->header.sh_type >= SHT_LOPROC) {
3514 /* Assume processor specific section types are debug
3515 info and can safely be ignored. If this is ever not
3516 the case (Hello MIPS?), don't put ifdefs here but
3517 create an arch_load_proc_section(). */
3521 bb_error_msg("can't handle sections of type %ld",
3522 (long) sec->header.sh_type);
3527 /* Do what sort of interpretation as needed by each section. */
3529 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3531 for (i = 0; i < shnum; ++i) {
3532 struct obj_section *sec = f->sections[i];
3533 sec->name = shstrtab + sec->header.sh_name;
3536 for (i = 0; i < shnum; ++i) {
3537 struct obj_section *sec = f->sections[i];
3539 /* .modinfo should be contents only but gcc has no attribute for that.
3540 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3542 if (strcmp(sec->name, ".modinfo") == 0)
3543 sec->header.sh_flags &= ~SHF_ALLOC;
3545 if (sec->header.sh_flags & SHF_ALLOC)
3546 obj_insert_section_load_order(f, sec);
3548 switch (sec->header.sh_type) {
3551 unsigned long nsym, j;
3555 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3556 bb_error_msg("symbol size mismatch: %lu != %lu",
3557 (unsigned long) sec->header.sh_entsize,
3558 (unsigned long) sizeof(ElfW(Sym)));
3562 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3563 strtab = f->sections[sec->header.sh_link]->contents;
3564 sym = (ElfW(Sym) *) sec->contents;
3566 /* Allocate space for a table of local symbols. */
3567 j = f->local_symtab_size = sec->header.sh_info;
3568 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3570 /* Insert all symbols into the hash table. */
3571 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3572 ElfW(Addr) val = sym->st_value;
3575 name = strtab + sym->st_name;
3577 name = f->sections[sym->st_shndx]->name;
3579 #if defined(__SH5__)
3581 * For sh64 it is possible that the target of a branch
3582 * requires a mode switch (32 to 16 and back again).
3584 * This is implied by the lsb being set in the target
3585 * address for SHmedia mode and clear for SHcompact.
3587 val |= sym->st_other & 4;
3590 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3597 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3598 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3599 (unsigned long) sec->header.sh_entsize,
3600 (unsigned long) sizeof(ElfW(RelM)));
3604 /* XXX Relocation code from modutils-2.3.19 is not here.
3605 * Why? That's about 20 lines of code from obj/obj_load.c,
3606 * which gets done in a second pass through the sections.
3607 * This BusyBox insmod does similar work in obj_relocate(). */
3614 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3616 * load the unloaded sections directly into the memory allocated by
3617 * kernel for the module
3620 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3622 ElfW(Addr) base = f->baseaddr;
3623 struct obj_section* sec;
3625 for (sec = f->load_order; sec; sec = sec->load_next) {
3627 /* section already loaded? */
3628 if (sec->contents != NULL)
3631 if (sec->header.sh_size == 0)
3634 sec->contents = imagebase + (sec->header.sh_addr - base);
3635 fseek(fp, sec->header.sh_offset, SEEK_SET);
3636 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3637 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3646 static void hide_special_symbols(struct obj_file *f)
3648 static const char *const specials[] = {
3649 SPFX "cleanup_module",
3651 SPFX "kernel_version",
3655 struct obj_symbol *sym;
3656 const char *const *p;
3658 for (p = specials; *p; ++p)
3659 if ((sym = obj_find_symbol(f, *p)) != NULL)
3661 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3665 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3666 static int obj_gpl_license(struct obj_file *f, const char **license)
3668 struct obj_section *sec;
3669 /* This list must match *exactly* the list of allowable licenses in
3670 * linux/include/linux/module.h. Checking for leading "GPL" will not
3671 * work, somebody will use "GPL sucks, this is proprietary".
3673 static const char *gpl_licenses[] = {
3676 "GPL and additional rights",
3681 if ((sec = obj_find_section(f, ".modinfo"))) {
3682 const char *value, *ptr, *endptr;
3683 ptr = sec->contents;
3684 endptr = ptr + sec->header.sh_size;
3685 while (ptr < endptr) {
3686 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3690 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3691 if (strcmp(value+1, gpl_licenses[i]) == 0)
3696 if (strchr(ptr, '\0'))
3697 ptr = strchr(ptr, '\0') + 1;
3705 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3706 #define TAINT_PROPRIETORY_MODULE (1<<0)
3707 #define TAINT_FORCED_MODULE (1<<1)
3708 #define TAINT_UNSAFE_SMP (1<<2)
3709 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3711 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3712 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3716 static int first = 1;
3717 if (fd < 0 && !kernel_has_tainted)
3718 return; /* New modutils on old kernel */
3719 printf("Warning: loading %s will taint the kernel: %s%s\n",
3720 m_name, text1, text2);
3722 printf(" See %s for information about tainted modules\n", TAINT_URL);
3726 read(fd, buf, sizeof(buf)-1);
3727 buf[sizeof(buf)-1] = '\0';
3728 oldval = strtoul(buf, NULL, 10);
3729 sprintf(buf, "%d\n", oldval | taint);
3730 write(fd, buf, strlen(buf));
3734 /* Check if loading this module will taint the kernel. */
3735 static void check_tainted_module(struct obj_file *f, char *m_name)
3737 static const char tainted_file[] = TAINT_FILENAME;
3738 int fd, kernel_has_tainted;
3741 kernel_has_tainted = 1;
3742 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3743 if (errno == ENOENT)
3744 kernel_has_tainted = 0;
3745 else if (errno == EACCES)
3746 kernel_has_tainted = 1;
3748 perror(tainted_file);
3749 kernel_has_tainted = 0;
3753 switch (obj_gpl_license(f, &ptr)) {
3757 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3760 /* The module has a non-GPL license so we pretend that the
3761 * kernel always has a taint flag to get a warning even on
3762 * kernels without the proc flag.
3764 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3767 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3771 if (flag_force_load)
3772 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3777 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3778 #define check_tainted_module(x, y) do { } while(0);
3779 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3781 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3782 /* add module source, timestamp, kernel version and a symbol for the
3783 * start of some sections. this info is used by ksymoops to do better
3787 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3789 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3790 if (get_modinfo_value(f, "kernel_version") == NULL)
3791 return old_get_module_version(f, str);
3793 return new_get_module_version(f, str);
3794 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3795 strncpy(str, "???", sizeof(str));
3797 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3800 /* add module source, timestamp, kernel version and a symbol for the
3801 * start of some sections. this info is used by ksymoops to do better
3805 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3808 static const char symprefix[] = "__insmod_";
3809 struct obj_section *sec;
3810 struct obj_symbol *sym;
3811 char *name, *absolute_filename;
3812 char str[STRVERSIONLEN], real[PATH_MAX];
3813 int i, l, lm_name, lfilename, use_ksymtab, version;
3814 struct stat statbuf;
3816 static const char *section_names[] = {
3824 if (realpath(filename, real)) {
3825 absolute_filename = bb_xstrdup(real);
3828 int save_errno = errno;
3829 bb_error_msg("cannot get realpath for %s", filename);
3832 absolute_filename = bb_xstrdup(filename);
3835 lm_name = strlen(m_name);
3836 lfilename = strlen(absolute_filename);
3838 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3839 * are not to be exported. otherwise leave ksymtab alone for now, the
3840 * "export all symbols" compatibility code will export these symbols later.
3842 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3844 if ((sec = obj_find_section(f, ".this"))) {
3845 /* tag the module header with the object name, last modified
3846 * timestamp and module version. worst case for module version
3847 * is 0xffffff, decimal 16777215. putting all three fields in
3848 * one symbol is less readable but saves kernel space.
3850 l = sizeof(symprefix)+ /* "__insmod_" */
3851 lm_name+ /* module name */
3853 lfilename+ /* object filename */
3855 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3857 8+ /* version in dec */
3860 if (stat(absolute_filename, &statbuf) != 0)
3861 statbuf.st_mtime = 0;
3862 version = get_module_version(f, str); /* -1 if not found */
3863 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3864 symprefix, m_name, absolute_filename,
3865 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3867 sym = obj_add_symbol(f, name, -1,
3868 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3869 sec->idx, sec->header.sh_addr, 0);
3871 new_add_ksymtab(f, sym);
3873 free(absolute_filename);
3874 #ifdef _NOT_SUPPORTED_
3875 /* record where the persistent data is going, same address as previous symbol */
3878 l = sizeof(symprefix)+ /* "__insmod_" */
3879 lm_name+ /* module name */
3881 strlen(f->persist)+ /* data store */
3884 snprintf(name, l, "%s%s_P%s",
3885 symprefix, m_name, f->persist);
3886 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3887 sec->idx, sec->header.sh_addr, 0);
3889 new_add_ksymtab(f, sym);
3891 #endif /* _NOT_SUPPORTED_ */
3892 /* tag the desired sections if size is non-zero */
3894 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3895 if ((sec = obj_find_section(f, section_names[i])) &&
3896 sec->header.sh_size) {
3897 l = sizeof(symprefix)+ /* "__insmod_" */
3898 lm_name+ /* module name */
3900 strlen(sec->name)+ /* section name */
3902 8+ /* length in dec */
3905 snprintf(name, l, "%s%s_S%s_L%ld",
3906 symprefix, m_name, sec->name,
3907 (long)sec->header.sh_size);
3908 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3909 sec->idx, sec->header.sh_addr, 0);
3911 new_add_ksymtab(f, sym);
3915 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3917 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3918 static void print_load_map(struct obj_file *f)
3920 struct obj_symbol *sym;
3921 struct obj_symbol **all, **p;
3922 struct obj_section *sec;
3923 int i, nsyms, *loaded;
3925 /* Report on the section layout. */
3927 printf("Sections: Size %-*s Align\n",
3928 (int) (2 * sizeof(void *)), "Address");
3930 for (sec = f->load_order; sec; sec = sec->load_next) {
3934 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3939 printf("%-15s %08lx %0*lx 2**%d\n",
3941 (long)sec->header.sh_size,
3942 (int) (2 * sizeof(void *)),
3943 (long)sec->header.sh_addr,
3946 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3947 /* Quick reference which section indicies are loaded. */
3949 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3951 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3953 /* Collect the symbols we'll be listing. */
3955 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3956 for (sym = f->symtab[i]; sym; sym = sym->next)
3957 if (sym->secidx <= SHN_HIRESERVE
3958 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3961 all = alloca(nsyms * sizeof(struct obj_symbol *));
3963 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3964 for (sym = f->symtab[i]; sym; sym = sym->next)
3965 if (sym->secidx <= SHN_HIRESERVE
3966 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3969 /* And list them. */
3970 printf("\nSymbols:\n");
3971 for (p = all; p < all + nsyms; ++p) {
3973 unsigned long value;
3976 if (sym->secidx == SHN_ABS) {
3979 } else if (sym->secidx == SHN_UNDEF) {
3983 sec = f->sections[sym->secidx];
3985 if (sec->header.sh_type == SHT_NOBITS)
3987 else if (sec->header.sh_flags & SHF_ALLOC) {
3988 if (sec->header.sh_flags & SHF_EXECINSTR)
3990 else if (sec->header.sh_flags & SHF_WRITE)
3995 value = sym->value + sec->header.sh_addr;
3998 if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
3999 type = tolower(type);
4001 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
4009 extern int insmod_main( int argc, char **argv)
4016 unsigned long m_size;
4021 int exit_status = EXIT_FAILURE;
4023 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4024 struct utsname uts_info;
4025 char m_strversion[STRVERSIONLEN];
4029 #ifdef CONFIG_FEATURE_CLEAN_UP
4034 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4035 int flag_print_load_map = 0;
4038 /* Parse any options */
4039 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4040 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0) {
4042 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0) {
4045 case 'f': /* force loading */
4046 flag_force_load = 1;
4048 case 'k': /* module loaded by kerneld, auto-cleanable */
4051 case 's': /* log to syslog */
4052 /* log to syslog -- not supported */
4053 /* but kernel needs this for request_module(), */
4054 /* as this calls: modprobe -k -s -- <module> */
4055 /* so silently ignore this flag */
4057 case 'v': /* verbose output */
4060 case 'q': /* silent */
4063 case 'x': /* do not export externs */
4066 case 'o': /* name the output module */
4068 m_name = bb_xstrdup(optarg);
4070 case 'L': /* Stub warning */
4071 /* This is needed for compatibility with modprobe.
4072 * In theory, this does locking, but we don't do
4073 * that. So be careful and plan your life around not
4074 * loading the same module 50 times concurrently. */
4076 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4077 case 'm': /* print module load map */
4078 flag_print_load_map = 1;
4086 if (argv[optind] == NULL) {
4090 /* Grab the module name */
4091 tmp1 = bb_xstrdup(argv[optind]);
4092 tmp = basename(tmp1);
4095 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4100 bb_xasprintf(&m_fullName, "%s.o", tmp);
4106 tmp1 = 0; /* flag for free(m_name) before exit() */
4109 /* Get a filedesc for the module. Check we we have a complete path */
4110 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
4111 (fp = fopen(argv[optind], "r")) == NULL) {
4112 struct utsname myuname;
4114 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4115 * but do not error out yet if we fail to find it... */
4116 if (uname(&myuname) == 0) {
4119 char real_module_dir[FILENAME_MAX];
4121 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4122 /* Jump through hoops in case /lib/modules/`uname -r`
4123 * is a symlink. We do not want recursive_action to
4124 * follow symlinks, but we do want to follow the
4125 * /lib/modules/`uname -r` dir, So resolve it ourselves
4126 * if it is a link... */
4127 if (realpath (tmdn, real_module_dir) == NULL)
4130 module_dir = real_module_dir;
4131 recursive_action(module_dir, TRUE, FALSE, FALSE,
4132 check_module_name_match, 0, m_fullName);
4136 /* Check if we have found anything yet */
4137 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4139 char module_dir[FILENAME_MAX];
4143 if (realpath (_PATH_MODULES, module_dir) == NULL)
4144 strcpy(module_dir, _PATH_MODULES);
4145 /* No module found under /lib/modules/`uname -r`, this
4146 * time cast the net a bit wider. Search /lib/modules/ */
4147 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4148 check_module_name_match, 0, m_fullName))
4151 || ((fp = fopen(m_filename, "r")) == NULL))
4153 bb_error_msg("%s: no module by that name found", m_fullName);
4157 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4160 m_filename = bb_xstrdup(argv[optind]);
4162 printf("Using %s\n", m_filename);
4164 #ifdef CONFIG_FEATURE_REALLY_NEW_MODULE_INTERFACE
4165 if (create_module(NULL, 0) < 0 && errno == ENOSYS) {
4167 argv[optind] = m_filename;
4168 return insmod_ng_main(argc - optind, argv + optind);
4172 if ((f = obj_load(fp, LOADBITS)) == NULL)
4173 bb_perror_msg_and_die("Could not load the module");
4175 if (get_modinfo_value(f, "kernel_version") == NULL)
4180 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4181 /* Version correspondence? */
4183 if (uname(&uts_info) < 0)
4184 uts_info.release[0] = '\0';
4185 if (m_has_modinfo) {
4186 m_version = new_get_module_version(f, m_strversion);
4188 m_version = old_get_module_version(f, m_strversion);
4189 if (m_version == -1) {
4190 bb_error_msg("couldn't find the kernel version the module was "
4196 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4197 if (flag_force_load) {
4198 bb_error_msg("Warning: kernel-module version mismatch\n"
4199 "\t%s was compiled for kernel version %s\n"
4200 "\twhile this kernel is version %s",
4201 m_filename, m_strversion, uts_info.release);
4203 bb_error_msg("kernel-module version mismatch\n"
4204 "\t%s was compiled for kernel version %s\n"
4205 "\twhile this kernel is version %s.",
4206 m_filename, m_strversion, uts_info.release);
4212 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4214 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
4216 if (k_new_syscalls) {
4217 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
4218 if (!new_get_kernel_symbols())
4220 k_crcs = new_is_kernel_checksummed();
4222 bb_error_msg("Not configured to support new kernels");
4226 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
4227 if (!old_get_kernel_symbols(m_name))
4229 k_crcs = old_is_kernel_checksummed();
4231 bb_error_msg("Not configured to support old kernels");
4236 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4238 m_crcs = new_is_module_checksummed(f);
4240 m_crcs = old_is_module_checksummed(f);
4242 if (m_crcs != k_crcs)
4243 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4244 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4246 /* Let the module know about the kernel symbols. */
4247 add_kernel_symbols(f);
4249 /* Allocate common symbols, symbol tables, and string tables. */
4252 ? !new_create_this_module(f, m_name)
4253 : !old_create_mod_use_count(f))
4258 if (!obj_check_undefineds(f)) {
4261 obj_allocate_commons(f);
4262 check_tainted_module(f, m_name);
4264 /* done with the module name, on to the optional var=value arguments */
4267 if (optind < argc) {
4269 ? !new_process_module_arguments(f, argc - optind, argv + optind)
4270 : !old_process_module_arguments(f, argc - optind, argv + optind))
4277 hide_special_symbols(f);
4279 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4280 add_ksymoops_symbols(f, m_filename, m_name);
4281 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4284 new_create_module_ksymtab(f);
4286 /* Find current size of the module */
4287 m_size = obj_load_size(f);
4290 m_addr = create_module(m_name, m_size);
4291 if (m_addr == -1) switch (errno) {
4293 bb_error_msg("A module named %s already exists", m_name);
4296 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4300 bb_perror_msg("create_module: %s", m_name);
4306 * the PROGBITS section was not loaded by the obj_load
4307 * now we can load them directly into the kernel memory
4309 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4310 delete_module(m_name);
4315 if (!obj_relocate(f, m_addr)) {
4316 delete_module(m_name);
4321 ? !new_init_module(m_name, f, m_size)
4322 : !old_init_module(m_name, f, m_size))
4324 delete_module(m_name);
4328 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4329 if(flag_print_load_map)
4333 exit_status = EXIT_SUCCESS;
4336 #ifdef CONFIG_FEATURE_CLEAN_UP
4346 return(exit_status);