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-2004 by Erik Andersen <andersen@codepoet.org>
9 * and Ron Alder <alder@lineo.com>
11 * Miles Bader <miles@gnu.org> added NEC V850E support.
13 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
14 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
16 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
17 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
18 * very minor changes required to also work with StrongArm and presumably
19 * all ARM based systems.
21 * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
22 * Integrated support for sh64 (SH-5), from preliminary modutils
23 * patches from Benedict Gaster <benedict.gaster@superh.com>.
24 * Currently limited to support for 32bit ABI.
26 * Magnus Damm <damm@opensource.se> 22-May-2002.
27 * The plt and got code are now using the same structs.
28 * Added generic linked list code to fully support PowerPC.
29 * Replaced the mess in arch_apply_relocation() with architecture blocks.
30 * The arch_create_got() function got cleaned up with architecture blocks.
31 * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
33 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
34 * PowerPC specific code stolen from modutils-2.3.16,
35 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
36 * I've only tested the code on mpc8xx platforms in big-endian mode.
37 * Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
39 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
40 * based on modutils-2.4.2
41 * MIPS specific support for Elf loading and relocation.
42 * Copyright 1996, 1997 Linux International.
43 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
45 * Based almost entirely on the Linux modutils-2.3.11 implementation.
46 * Copyright 1996, 1997 Linux International.
47 * New implementation contributed by Richard Henderson <rth@tamu.edu>
48 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
49 * Restructured (and partly rewritten) by:
50 * Björn Ekwall <bj0rn@blox.se> February 1999
52 * This program is free software; you can redistribute it and/or modify
53 * it under the terms of the GNU General Public License as published by
54 * the Free Software Foundation; either version 2 of the License, or
55 * (at your option) any later version.
57 * This program is distributed in the hope that it will be useful,
58 * but WITHOUT ANY WARRANTY; without even the implied warranty of
59 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
60 * General Public License for more details.
62 * You should have received a copy of the GNU General Public License
63 * along with this program; if not, write to the Free Software
64 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
79 #include <sys/utsname.h>
82 #if !defined(CONFIG_FEATURE_2_4_MODULES) && \
83 !defined(CONFIG_FEATURE_2_2_MODULES) && \
84 !defined(CONFIG_FEATURE_2_6_MODULES)
85 #define CONFIG_FEATURE_2_4_MODULES
88 #if !defined(CONFIG_FEATURE_2_4_MODULES) && !defined(CONFIG_FEATURE_2_2_MODULES)
89 #define insmod_ng_main insmod_main
92 #if defined(CONFIG_FEATURE_2_4_MODULES) || defined(CONFIG_FEATURE_2_2_MODULES)
94 #if defined(CONFIG_FEATURE_2_6_MODULES)
95 extern int insmod_ng_main( int argc, char **argv);
98 #ifdef CONFIG_FEATURE_2_4_MODULES
99 # undef CONFIG_FEATURE_2_2_MODULES
100 # define new_sys_init_module init_module
102 # define old_sys_init_module init_module
105 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
113 #define CONFIG_USE_PLT_ENTRIES
114 #define CONFIG_PLT_ENTRY_SIZE 8
115 #define CONFIG_USE_GOT_ENTRIES
116 #define CONFIG_GOT_ENTRY_SIZE 8
117 #define CONFIG_USE_SINGLE
119 #define MATCH_MACHINE(x) (x == EM_ARM)
120 #define SHT_RELM SHT_REL
121 #define Elf32_RelM Elf32_Rel
122 #define ELFCLASSM ELFCLASS32
125 #if defined(__s390__)
126 #define CONFIG_USE_PLT_ENTRIES
127 #define CONFIG_PLT_ENTRY_SIZE 8
128 #define CONFIG_USE_GOT_ENTRIES
129 #define CONFIG_GOT_ENTRY_SIZE 8
130 #define CONFIG_USE_SINGLE
132 #define MATCH_MACHINE(x) (x == EM_S390)
133 #define SHT_RELM SHT_RELA
134 #define Elf32_RelM Elf32_Rela
135 #define ELFCLASSM ELFCLASS32
138 #if defined(__i386__)
139 #define CONFIG_USE_GOT_ENTRIES
140 #define CONFIG_GOT_ENTRY_SIZE 4
141 #define CONFIG_USE_SINGLE
144 #define MATCH_MACHINE(x) (x == EM_386)
146 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
149 #define SHT_RELM SHT_REL
150 #define Elf32_RelM Elf32_Rel
151 #define ELFCLASSM ELFCLASS32
154 #if defined(__mc68000__)
155 #define CONFIG_USE_GOT_ENTRIES
156 #define CONFIG_GOT_ENTRY_SIZE 4
157 #define CONFIG_USE_SINGLE
159 #define MATCH_MACHINE(x) (x == EM_68K)
160 #define SHT_RELM SHT_RELA
161 #define Elf32_RelM Elf32_Rela
162 #define ELFCLASSM ELFCLASS32
165 #if defined(__mips__)
166 /* Account for ELF spec changes. */
167 #ifndef EM_MIPS_RS3_LE
168 #ifdef EM_MIPS_RS4_BE
169 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
171 #define EM_MIPS_RS3_LE 10
173 #endif /* !EM_MIPS_RS3_LE */
175 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
176 #define SHT_RELM SHT_REL
177 #define Elf32_RelM Elf32_Rel
178 #define ELFCLASSM ELFCLASS32
179 #define ARCHDATAM "__dbe_table"
182 #if defined(__powerpc__)
183 #define CONFIG_USE_PLT_ENTRIES
184 #define CONFIG_PLT_ENTRY_SIZE 16
185 #define CONFIG_USE_PLT_LIST
186 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
187 #define CONFIG_USE_LIST
189 #define MATCH_MACHINE(x) (x == EM_PPC)
190 #define SHT_RELM SHT_RELA
191 #define Elf32_RelM Elf32_Rela
192 #define ELFCLASSM ELFCLASS32
193 #define ARCHDATAM "__ftr_fixup"
197 #define CONFIG_USE_GOT_ENTRIES
198 #define CONFIG_GOT_ENTRY_SIZE 4
199 #define CONFIG_USE_SINGLE
201 #define MATCH_MACHINE(x) (x == EM_SH)
202 #define SHT_RELM SHT_RELA
203 #define Elf32_RelM Elf32_Rela
204 #define ELFCLASSM ELFCLASS32
206 /* the SH changes have only been tested in =little endian= mode */
207 /* I'm not sure about big endian, so let's warn: */
209 #if defined(__sh__) && defined(__BIG_ENDIAN__)
210 #error insmod.c may require changes for use on big endian SH
213 /* it may or may not work on the SH1/SH2... So let's error on those
215 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && \
217 #error insmod.c may require changes for SH1 or SH2 use
221 #if defined (__v850e__)
222 #define CONFIG_USE_PLT_ENTRIES
223 #define CONFIG_PLT_ENTRY_SIZE 8
224 #define CONFIG_USE_SINGLE
226 #ifndef EM_CYGNUS_V850 /* grumble */
227 #define EM_CYGNUS_V850 0x9080
230 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
231 #define SHT_RELM SHT_RELA
232 #define Elf32_RelM Elf32_Rela
233 #define ELFCLASSM ELFCLASS32
235 #define SYMBOL_PREFIX "_"
238 #if defined(__cris__)
241 #define R_CRIS_NONE 0
245 #define MATCH_MACHINE(x) (x == EM_CRIS)
246 #define SHT_RELM SHT_RELA
247 #define Elf32_RelM Elf32_Rela
248 #define ELFCLASSM ELFCLASS32
252 #error Sorry, but insmod.c does not yet support this architecture...
256 //----------------------------------------------------------------------------
257 //--------modutils module.h, lines 45-242
258 //----------------------------------------------------------------------------
260 /* Definitions for the Linux module syscall interface.
261 Copyright 1996, 1997 Linux International.
263 Contributed by Richard Henderson <rth@tamu.edu>
265 This file is part of the Linux modutils.
267 This program is free software; you can redistribute it and/or modify it
268 under the terms of the GNU General Public License as published by the
269 Free Software Foundation; either version 2 of the License, or (at your
270 option) any later version.
272 This program is distributed in the hope that it will be useful, but
273 WITHOUT ANY WARRANTY; without even the implied warranty of
274 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
275 General Public License for more details.
277 You should have received a copy of the GNU General Public License
278 along with this program; if not, write to the Free Software Foundation,
279 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
282 #ifndef MODUTILS_MODULE_H
283 static const int MODUTILS_MODULE_H = 1;
285 #ident "$Id: insmod.c,v 1.115 2004/03/19 12:17:04 andersen Exp $"
287 /* This file contains the structures used by the 2.0 and 2.1 kernels.
288 We do not use the kernel headers directly because we do not wish
289 to be dependant on a particular kernel version to compile insmod. */
292 /*======================================================================*/
293 /* The structures used by Linux 2.0. */
295 /* The symbol format used by get_kernel_syms(2). */
296 struct old_kernel_sym
302 struct old_module_ref
304 unsigned long module; /* kernel addresses */
308 struct old_module_symbol
314 struct old_symbol_table
316 int size; /* total, including string table!!! */
319 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
320 struct old_module_ref ref[0]; /* actual size defined by n_refs */
323 struct old_mod_routines
326 unsigned long cleanup;
332 unsigned long ref; /* the list of modules that refer to me */
333 unsigned long symtab;
335 int size; /* size of module in pages */
336 unsigned long addr; /* address of module */
338 unsigned long cleanup; /* cleanup routine */
341 /* Sent to init_module(2) or'ed into the code size parameter. */
342 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
344 int get_kernel_syms(struct old_kernel_sym *);
345 int old_sys_init_module(const char *name, char *code, unsigned codesize,
346 struct old_mod_routines *, struct old_symbol_table *);
348 /*======================================================================*/
349 /* For sizeof() which are related to the module platform and not to the
350 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
352 #define tgt_sizeof_char sizeof(char)
353 #define tgt_sizeof_short sizeof(short)
354 #define tgt_sizeof_int sizeof(int)
355 #define tgt_sizeof_long sizeof(long)
356 #define tgt_sizeof_char_p sizeof(char *)
357 #define tgt_sizeof_void_p sizeof(void *)
358 #define tgt_long long
360 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
361 #undef tgt_sizeof_long
362 #undef tgt_sizeof_char_p
363 #undef tgt_sizeof_void_p
365 static const int tgt_sizeof_long = 8;
366 static const int tgt_sizeof_char_p = 8;
367 static const int tgt_sizeof_void_p = 8;
368 #define tgt_long long long
371 /*======================================================================*/
372 /* The structures used in Linux 2.1. */
374 /* Note: new_module_symbol does not use tgt_long intentionally */
375 struct new_module_symbol
381 struct new_module_persist;
383 struct new_module_ref
385 unsigned tgt_long dep; /* kernel addresses */
386 unsigned tgt_long ref;
387 unsigned tgt_long next_ref;
392 unsigned tgt_long size_of_struct; /* == sizeof(module) */
393 unsigned tgt_long next;
394 unsigned tgt_long name;
395 unsigned tgt_long size;
398 unsigned tgt_long flags; /* AUTOCLEAN et al */
403 unsigned tgt_long syms;
404 unsigned tgt_long deps;
405 unsigned tgt_long refs;
406 unsigned tgt_long init;
407 unsigned tgt_long cleanup;
408 unsigned tgt_long ex_table_start;
409 unsigned tgt_long ex_table_end;
411 unsigned tgt_long gp;
413 /* Everything after here is extension. */
414 unsigned tgt_long persist_start;
415 unsigned tgt_long persist_end;
416 unsigned tgt_long can_unload;
417 unsigned tgt_long runsize;
418 #ifdef CONFIG_FEATURE_2_4_MODULES
419 const char *kallsyms_start; /* All symbols for kernel debugging */
420 const char *kallsyms_end;
421 const char *archdata_start; /* arch specific data for module */
422 const char *archdata_end;
423 const char *kernel_data; /* Reserved for kernel internal use */
428 #define ARCHDATA_SEC_NAME ARCHDATAM
430 #define ARCHDATA_SEC_NAME "__archdata"
432 #define KALLSYMS_SEC_NAME "__kallsyms"
435 struct new_module_info
443 /* Bits of module.flags. */
444 static const int NEW_MOD_RUNNING = 1;
445 static const int NEW_MOD_DELETED = 2;
446 static const int NEW_MOD_AUTOCLEAN = 4;
447 static const int NEW_MOD_VISITED = 8;
448 static const int NEW_MOD_USED_ONCE = 16;
450 int new_sys_init_module(const char *name, const struct new_module *);
451 int query_module(const char *name, int which, void *buf, size_t bufsize,
454 /* Values for query_module's which. */
456 static const int QM_MODULES = 1;
457 static const int QM_DEPS = 2;
458 static const int QM_REFS = 3;
459 static const int QM_SYMBOLS = 4;
460 static const int QM_INFO = 5;
462 /*======================================================================*/
463 /* The system calls unchanged between 2.0 and 2.1. */
465 unsigned long create_module(const char *, size_t);
466 int delete_module(const char *);
469 #endif /* module.h */
471 //----------------------------------------------------------------------------
472 //--------end of modutils module.h
473 //----------------------------------------------------------------------------
477 //----------------------------------------------------------------------------
478 //--------modutils obj.h, lines 253-462
479 //----------------------------------------------------------------------------
481 /* Elf object file loading and relocation routines.
482 Copyright 1996, 1997 Linux International.
484 Contributed by Richard Henderson <rth@tamu.edu>
486 This file is part of the Linux modutils.
488 This program is free software; you can redistribute it and/or modify it
489 under the terms of the GNU General Public License as published by the
490 Free Software Foundation; either version 2 of the License, or (at your
491 option) any later version.
493 This program is distributed in the hope that it will be useful, but
494 WITHOUT ANY WARRANTY; without even the implied warranty of
495 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
496 General Public License for more details.
498 You should have received a copy of the GNU General Public License
499 along with this program; if not, write to the Free Software Foundation,
500 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
503 #ifndef MODUTILS_OBJ_H
504 static const int MODUTILS_OBJ_H = 1;
506 #ident "$Id: insmod.c,v 1.115 2004/03/19 12:17:04 andersen Exp $"
508 /* The relocatable object is manipulated using elfin types. */
514 #if __BYTE_ORDER == __LITTLE_ENDIAN
515 #define ELFDATAM ELFDATA2LSB
516 #elif __BYTE_ORDER == __BIG_ENDIAN
517 #define ELFDATAM ELFDATA2MSB
521 # if ELFCLASSM == ELFCLASS32
522 # define ElfW(x) Elf32_ ## x
523 # define ELFW(x) ELF32_ ## x
525 # define ElfW(x) Elf64_ ## x
526 # define ELFW(x) ELF64_ ## x
530 /* For some reason this is missing from some ancient C libraries.... */
531 #ifndef ELF32_ST_INFO
532 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
535 #ifndef ELF64_ST_INFO
536 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
539 struct obj_string_patch;
540 struct obj_symbol_patch;
547 struct obj_section *load_next;
553 struct obj_symbol *next; /* hash table link */
557 int secidx; /* the defining section index/module */
559 int ksymidx; /* for export to the kernel symtab */
560 int referenced; /* actually used in the link */
563 /* Hardcode the hash table size. We shouldn't be needing so many
564 symbols that we begin to degrade performance, and we get a big win
565 by giving the compiler a constant divisor. */
567 #define HASH_BUCKETS 521
573 struct obj_section **sections;
574 struct obj_section *load_order;
575 struct obj_section **load_order_search_start;
576 struct obj_string_patch *string_patches;
577 struct obj_symbol_patch *symbol_patches;
578 int (*symbol_cmp)(const char *, const char *);
579 unsigned long (*symbol_hash)(const char *);
580 unsigned long local_symtab_size;
581 struct obj_symbol **local_symtab;
582 struct obj_symbol *symtab[HASH_BUCKETS];
593 struct obj_string_patch
595 struct obj_string_patch *next;
597 ElfW(Addr) reloc_offset;
598 ElfW(Addr) string_offset;
601 struct obj_symbol_patch
603 struct obj_symbol_patch *next;
605 ElfW(Addr) reloc_offset;
606 struct obj_symbol *sym;
610 /* Generic object manipulation routines. */
612 static unsigned long obj_elf_hash(const char *);
614 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
616 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
619 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
620 struct obj_symbol *sym);
622 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
623 static void obj_set_symbol_compare(struct obj_file *f,
624 int (*cmp)(const char *, const char *),
625 unsigned long (*hash)(const char *));
628 static struct obj_section *obj_find_section (struct obj_file *f,
631 static void obj_insert_section_load_order (struct obj_file *f,
632 struct obj_section *sec);
634 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
639 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
644 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
646 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
649 #ifdef CONFIG_FEATURE_2_4_MODULES
650 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
651 struct obj_symbol *sym);
654 static int obj_check_undefineds(struct obj_file *f);
656 static void obj_allocate_commons(struct obj_file *f);
658 static unsigned long obj_load_size (struct obj_file *f);
660 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
662 static struct obj_file *obj_load(FILE *f, int loadprogbits);
664 static int obj_create_image (struct obj_file *f, char *image);
666 /* Architecture specific manipulation routines. */
668 static struct obj_file *arch_new_file (void);
670 static struct obj_section *arch_new_section (void);
672 static struct obj_symbol *arch_new_symbol (void);
674 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
675 struct obj_section *targsec,
676 struct obj_section *symsec,
677 struct obj_symbol *sym,
678 ElfW(RelM) *rel, ElfW(Addr) value);
680 static void arch_create_got (struct obj_file *f);
682 static int obj_gpl_license(struct obj_file *f, const char **license);
684 #ifdef CONFIG_FEATURE_2_4_MODULES
685 static int arch_init_module (struct obj_file *f, struct new_module *);
689 //----------------------------------------------------------------------------
690 //--------end of modutils obj.h
691 //----------------------------------------------------------------------------
694 /* SPFX is always a string, so it can be concatenated to string constants. */
696 #define SPFX SYMBOL_PREFIX
702 #define _PATH_MODULES "/lib/modules"
703 static const int STRVERSIONLEN = 32;
705 /*======================================================================*/
707 static int flag_force_load = 0;
708 static int flag_autoclean = 0;
709 static int flag_verbose = 0;
710 static int flag_quiet = 0;
711 static int flag_export = 1;
714 /*======================================================================*/
716 #if defined(CONFIG_USE_LIST)
718 struct arch_list_entry
720 struct arch_list_entry *next;
721 CONFIG_LIST_ARCHTYPE addend;
728 #if defined(CONFIG_USE_SINGLE)
730 struct arch_single_entry
739 #if defined(__mips__)
742 struct mips_hi16 *next;
749 struct obj_file root;
750 #if defined(CONFIG_USE_PLT_ENTRIES)
751 struct obj_section *plt;
753 #if defined(CONFIG_USE_GOT_ENTRIES)
754 struct obj_section *got;
756 #if defined(__mips__)
757 struct mips_hi16 *mips_hi16_list;
762 struct obj_symbol root;
763 #if defined(CONFIG_USE_PLT_ENTRIES)
764 #if defined(CONFIG_USE_PLT_LIST)
765 struct arch_list_entry *pltent;
767 struct arch_single_entry pltent;
770 #if defined(CONFIG_USE_GOT_ENTRIES)
771 struct arch_single_entry gotent;
776 struct external_module {
781 struct new_module_symbol *syms;
784 static struct new_module_symbol *ksyms;
785 static size_t nksyms;
787 static struct external_module *ext_modules;
788 static int n_ext_modules;
789 static int n_ext_modules_used;
790 extern int delete_module(const char *);
792 static char *m_filename;
793 static char *m_fullName;
797 /*======================================================================*/
800 static int check_module_name_match(const char *filename, struct stat *statbuf,
803 char *fullname = (char *) userdata;
805 if (fullname[0] == '\0')
808 char *tmp, *tmp1 = bb_xstrdup(filename);
809 tmp = bb_get_last_path_component(tmp1);
810 if (strcmp(tmp, fullname) == 0) {
812 /* Stop searching if we find a match */
813 m_filename = bb_xstrdup(filename);
822 /*======================================================================*/
824 static struct obj_file *arch_new_file(void)
827 f = xmalloc(sizeof(*f));
829 memset(f, 0, sizeof(*f));
834 static struct obj_section *arch_new_section(void)
836 return xmalloc(sizeof(struct obj_section));
839 static struct obj_symbol *arch_new_symbol(void)
841 struct arch_symbol *sym;
842 sym = xmalloc(sizeof(*sym));
844 memset(sym, 0, sizeof(*sym));
849 static enum obj_reloc
850 arch_apply_relocation(struct obj_file *f,
851 struct obj_section *targsec,
852 struct obj_section *symsec,
853 struct obj_symbol *sym,
854 ElfW(RelM) *rel, ElfW(Addr) v)
856 struct arch_file *ifile = (struct arch_file *) f;
857 enum obj_reloc ret = obj_reloc_ok;
858 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
859 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
860 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
861 struct arch_symbol *isym = (struct arch_symbol *) sym;
863 #if defined(CONFIG_USE_GOT_ENTRIES)
864 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
866 #if defined(CONFIG_USE_PLT_ENTRIES)
867 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
869 #if defined(CONFIG_USE_PLT_LIST)
870 struct arch_list_entry *pe;
872 struct arch_single_entry *pe;
876 switch (ELF32_R_TYPE(rel->r_info)) {
891 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
892 * (which is .got) similar to branch,
893 * but is full 32 bits relative */
903 case R_ARM_GOTOFF: /* address relative to the got */
908 #elif defined(__s390__)
910 *(unsigned int *) loc += v;
913 *(unsigned short *) loc += v;
916 *(unsigned char *) loc += v;
920 *(unsigned int *) loc += v - dot;
923 *(unsigned short *) loc += (v - dot) >> 1;
926 *(unsigned short *) loc += v - dot;
931 /* find the plt entry and initialize it. */
932 assert(isym != NULL);
933 pe = (struct arch_single_entry *) &isym->pltent;
934 assert(pe->allocated);
935 if (pe->inited == 0) {
936 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
937 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
939 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
946 /* Insert relative distance to target. */
947 v = plt + pe->offset - dot;
948 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
949 *(unsigned int *) loc = (unsigned int) v;
950 else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
951 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
965 *(unsigned long *) loc += got - dot;
971 assert(isym != NULL);
973 if (!isym->gotent.inited)
975 isym->gotent.inited = 1;
976 *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
978 if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
979 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
980 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
981 *(unsigned short *) loc += isym->gotent.offset;
982 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
983 *(unsigned int *) loc += isym->gotent.offset;
986 #ifndef R_390_GOTOFF32
987 #define R_390_GOTOFF32 R_390_GOTOFF
994 #elif defined(__i386__)
1008 case R_386_GLOB_DAT:
1009 case R_386_JMP_SLOT:
1013 case R_386_RELATIVE:
1014 *loc += f->baseaddr;
1030 #elif defined(__mc68000__)
1041 ret = obj_reloc_overflow;
1048 ret = obj_reloc_overflow;
1055 if ((Elf32_Sword)v > 0x7f ||
1056 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
1057 ret = obj_reloc_overflow;
1064 if ((Elf32_Sword)v > 0x7fff ||
1065 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1066 ret = obj_reloc_overflow;
1072 *(int *)loc = v - dot;
1075 case R_68K_GLOB_DAT:
1076 case R_68K_JMP_SLOT:
1080 case R_68K_RELATIVE:
1081 *(int *)loc += f->baseaddr;
1094 #elif defined(__mips__)
1105 ret = obj_reloc_dangerous;
1106 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1107 ret = obj_reloc_overflow;
1109 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1115 struct mips_hi16 *n;
1117 /* We cannot relocate this one now because we don't know the value
1118 of the carry we need to add. Save the information, and let LO16
1119 do the actual relocation. */
1120 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1123 n->next = ifile->mips_hi16_list;
1124 ifile->mips_hi16_list = n;
1130 unsigned long insnlo = *loc;
1131 Elf32_Addr val, vallo;
1133 /* Sign extend the addend we extract from the lo insn. */
1134 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1136 if (ifile->mips_hi16_list != NULL) {
1137 struct mips_hi16 *l;
1139 l = ifile->mips_hi16_list;
1141 struct mips_hi16 *next;
1144 /* The value for the HI16 had best be the same. */
1145 assert(v == l->value);
1147 /* Do the HI16 relocation. Note that we actually don't
1148 need to know anything about the LO16 itself, except where
1149 to find the low 16 bits of the addend needed by the LO16. */
1152 ((insn & 0xffff) << 16) +
1156 /* Account for the sign extension that will happen in the
1163 insn = (insn & ~0xffff) | val;
1171 ifile->mips_hi16_list = NULL;
1174 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1176 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1181 #elif defined(__powerpc__)
1183 case R_PPC_ADDR16_HA:
1184 *(unsigned short *)loc = (v + 0x8000) >> 16;
1187 case R_PPC_ADDR16_HI:
1188 *(unsigned short *)loc = v >> 16;
1191 case R_PPC_ADDR16_LO:
1192 *(unsigned short *)loc = v;
1206 #elif defined(__sh__)
1229 *loc = f->baseaddr + rel->r_addend;
1234 *loc = got - dot + rel->r_addend;
1245 #if defined(__SH5__)
1246 case R_SH_IMM_MEDLOW16:
1247 case R_SH_IMM_LOW16:
1251 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1255 * movi and shori have the format:
1257 * | op | imm | reg | reserved |
1258 * 31..26 25..10 9.. 4 3 .. 0
1260 * so we simply mask and or in imm.
1262 word = *loc & ~0x3fffc00;
1263 word |= (v & 0xffff) << 10;
1270 case R_SH_IMM_MEDLOW16_PCREL:
1271 case R_SH_IMM_LOW16_PCREL:
1275 word = *loc & ~0x3fffc00;
1279 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1282 word |= (v & 0xffff) << 10;
1288 #endif /* __SH5__ */
1292 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1293 ret = obj_reloc_unhandled;
1296 #if defined (__v850e__)
1301 /* We write two shorts instead of a long because even
1302 32-bit insns only need half-word alignment, but
1303 32-bit data needs to be long-word aligned. */
1304 v += ((unsigned short *)loc)[0];
1305 v += ((unsigned short *)loc)[1] << 16;
1306 ((unsigned short *)loc)[0] = v & 0xffff;
1307 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1310 case R_V850_22_PCREL:
1314 #if defined (__cris__)
1319 /* CRIS keeps the relocation value in the r_addend field and
1320 * should not use whats in *loc at all
1326 #if defined(CONFIG_USE_PLT_ENTRIES)
1330 /* find the plt entry and initialize it if necessary */
1331 assert(isym != NULL);
1333 #if defined(CONFIG_USE_PLT_LIST)
1334 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1342 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1344 /* generate some machine code */
1346 #if defined(__arm__)
1347 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1348 ip[1] = v; /* sym@ */
1350 #if defined(__powerpc__)
1351 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1352 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1353 ip[2] = 0x7d6903a6; /* mtctr r11 */
1354 ip[3] = 0x4e800420; /* bctr */
1356 #if defined (__v850e__)
1357 /* We have to trash a register, so we assume that any control
1358 transfer more than 21-bits away must be a function call
1359 (so we can use a call-clobbered register). */
1360 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1361 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1366 /* relative distance to target */
1368 /* if the target is too far away.... */
1369 #if defined (__arm__) || defined (__powerpc__)
1370 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1371 #elif defined (__v850e__)
1372 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1374 /* go via the plt */
1375 v = plt + pe->offset - dot;
1377 #if defined (__v850e__)
1382 ret = obj_reloc_dangerous;
1384 /* merge the offset into the instruction. */
1385 #if defined(__arm__)
1386 /* Convert to words. */
1389 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1391 #if defined(__powerpc__)
1392 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1394 #if defined (__v850e__)
1395 /* We write two shorts instead of a long because even 32-bit insns
1396 only need half-word alignment, but the 32-bit data write needs
1397 to be long-word aligned. */
1398 ((unsigned short *)loc)[0] =
1399 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1400 | ((v >> 16) & 0x3f); /* offs high part */
1401 ((unsigned short *)loc)[1] =
1402 (v & 0xffff); /* offs low part */
1405 #endif /* CONFIG_USE_PLT_ENTRIES */
1407 #if defined(CONFIG_USE_GOT_ENTRIES)
1410 assert(isym != NULL);
1411 /* needs an entry in the .got: set it, once */
1412 if (!isym->gotent.inited) {
1413 isym->gotent.inited = 1;
1414 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1416 /* make the reloc with_respect_to_.got */
1418 *loc += isym->gotent.offset + rel->r_addend;
1419 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1420 *loc += isym->gotent.offset;
1424 #endif /* CONFIG_USE_GOT_ENTRIES */
1431 #if defined(CONFIG_USE_LIST)
1433 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1434 int offset, int size)
1436 struct arch_list_entry *pe;
1438 for (pe = *list; pe != NULL; pe = pe->next) {
1439 if (pe->addend == rel->r_addend) {
1445 pe = xmalloc(sizeof(struct arch_list_entry));
1447 pe->addend = rel->r_addend;
1448 pe->offset = offset;
1458 #if defined(CONFIG_USE_SINGLE)
1460 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1461 int offset, int size)
1463 if (single->allocated == 0) {
1464 single->allocated = 1;
1465 single->offset = offset;
1474 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1476 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1477 int offset, int size)
1479 struct obj_section *myrelsec = obj_find_section(f, name);
1486 obj_extend_section(myrelsec, offset);
1488 myrelsec = obj_create_alloced_section(f, name,
1498 static void arch_create_got(struct obj_file *f)
1500 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1501 struct arch_file *ifile = (struct arch_file *) f;
1503 #if defined(CONFIG_USE_GOT_ENTRIES)
1504 int got_offset = 0, got_needed = 0, got_allocate;
1506 #if defined(CONFIG_USE_PLT_ENTRIES)
1507 int plt_offset = 0, plt_needed = 0, plt_allocate;
1509 struct obj_section *relsec, *symsec, *strsec;
1510 ElfW(RelM) *rel, *relend;
1511 ElfW(Sym) *symtab, *extsym;
1512 const char *strtab, *name;
1513 struct arch_symbol *intsym;
1515 for (i = 0; i < f->header.e_shnum; ++i) {
1516 relsec = f->sections[i];
1517 if (relsec->header.sh_type != SHT_RELM)
1520 symsec = f->sections[relsec->header.sh_link];
1521 strsec = f->sections[symsec->header.sh_link];
1523 rel = (ElfW(RelM) *) relsec->contents;
1524 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1525 symtab = (ElfW(Sym) *) symsec->contents;
1526 strtab = (const char *) strsec->contents;
1528 for (; rel < relend; ++rel) {
1529 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1531 #if defined(CONFIG_USE_GOT_ENTRIES)
1534 #if defined(CONFIG_USE_PLT_ENTRIES)
1538 switch (ELF32_R_TYPE(rel->r_info)) {
1539 #if defined(__arm__)
1554 #elif defined(__i386__)
1564 #elif defined(__powerpc__)
1569 #elif defined(__mc68000__)
1580 #elif defined(__sh__)
1590 #elif defined (__v850e__)
1591 case R_V850_22_PCREL:
1600 if (extsym->st_name != 0) {
1601 name = strtab + extsym->st_name;
1603 name = f->sections[extsym->st_shndx]->name;
1605 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1606 #if defined(CONFIG_USE_GOT_ENTRIES)
1608 got_offset += arch_single_init(
1609 rel, &intsym->gotent,
1610 got_offset, CONFIG_GOT_ENTRY_SIZE);
1615 #if defined(CONFIG_USE_PLT_ENTRIES)
1617 #if defined(CONFIG_USE_PLT_LIST)
1618 plt_offset += arch_list_add(
1619 rel, &intsym->pltent,
1620 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1622 plt_offset += arch_single_init(
1623 rel, &intsym->pltent,
1624 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1632 #if defined(CONFIG_USE_GOT_ENTRIES)
1634 ifile->got = arch_xsect_init(f, ".got", got_offset,
1635 CONFIG_GOT_ENTRY_SIZE);
1639 #if defined(CONFIG_USE_PLT_ENTRIES)
1641 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1642 CONFIG_PLT_ENTRY_SIZE);
1646 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1649 #ifdef CONFIG_FEATURE_2_4_MODULES
1650 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1656 /*======================================================================*/
1658 /* Standard ELF hash function. */
1659 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1661 unsigned long h = 0;
1668 if ((g = (h & 0xf0000000)) != 0) {
1677 static unsigned long obj_elf_hash(const char *name)
1679 return obj_elf_hash_n(name, strlen(name));
1682 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1683 /* String comparison for non-co-versioned kernel and module. */
1685 static int ncv_strcmp(const char *a, const char *b)
1687 size_t alen = strlen(a), blen = strlen(b);
1689 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1690 return strncmp(a, b, alen);
1691 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1692 return strncmp(a, b, blen);
1694 return strcmp(a, b);
1697 /* String hashing for non-co-versioned kernel and module. Here
1698 we are simply forced to drop the crc from the hash. */
1700 static unsigned long ncv_symbol_hash(const char *str)
1702 size_t len = strlen(str);
1703 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1705 return obj_elf_hash_n(str, len);
1709 obj_set_symbol_compare(struct obj_file *f,
1710 int (*cmp) (const char *, const char *),
1711 unsigned long (*hash) (const char *))
1714 f->symbol_cmp = cmp;
1716 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1719 f->symbol_hash = hash;
1721 memcpy(tmptab, f->symtab, sizeof(tmptab));
1722 memset(f->symtab, 0, sizeof(f->symtab));
1724 for (i = 0; i < HASH_BUCKETS; ++i)
1725 for (sym = tmptab[i]; sym; sym = next) {
1726 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1728 sym->next = f->symtab[h];
1734 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1736 static struct obj_symbol *
1737 obj_add_symbol(struct obj_file *f, const char *name,
1738 unsigned long symidx, int info,
1739 int secidx, ElfW(Addr) value,
1742 struct obj_symbol *sym;
1743 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1744 int n_type = ELFW(ST_TYPE) (info);
1745 int n_binding = ELFW(ST_BIND) (info);
1747 for (sym = f->symtab[hash]; sym; sym = sym->next)
1748 if (f->symbol_cmp(sym->name, name) == 0) {
1749 int o_secidx = sym->secidx;
1750 int o_info = sym->info;
1751 int o_type = ELFW(ST_TYPE) (o_info);
1752 int o_binding = ELFW(ST_BIND) (o_info);
1754 /* A redefinition! Is it legal? */
1756 if (secidx == SHN_UNDEF)
1758 else if (o_secidx == SHN_UNDEF)
1760 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1761 /* Cope with local and global symbols of the same name
1762 in the same object file, as might have been created
1763 by ld -r. The only reason locals are now seen at this
1764 level at all is so that we can do semi-sensible things
1767 struct obj_symbol *nsym, **p;
1769 nsym = arch_new_symbol();
1770 nsym->next = sym->next;
1773 /* Excise the old (local) symbol from the hash chain. */
1774 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1778 } else if (n_binding == STB_LOCAL) {
1779 /* Another symbol of the same name has already been defined.
1780 Just add this to the local table. */
1781 sym = arch_new_symbol();
1784 f->local_symtab[symidx] = sym;
1786 } else if (n_binding == STB_WEAK)
1788 else if (o_binding == STB_WEAK)
1790 /* Don't unify COMMON symbols with object types the programmer
1792 else if (secidx == SHN_COMMON
1793 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1795 else if (o_secidx == SHN_COMMON
1796 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1799 /* Don't report an error if the symbol is coming from
1800 the kernel or some external module. */
1801 if (secidx <= SHN_HIRESERVE)
1802 bb_error_msg("%s multiply defined", name);
1807 /* Completely new symbol. */
1808 sym = arch_new_symbol();
1809 sym->next = f->symtab[hash];
1810 f->symtab[hash] = sym;
1813 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1814 if (symidx >= f->local_symtab_size)
1815 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1816 name, (long) symidx, (long) f->local_symtab_size);
1818 f->local_symtab[symidx] = sym;
1825 sym->secidx = secidx;
1831 static struct obj_symbol *
1832 obj_find_symbol(struct obj_file *f, const char *name)
1834 struct obj_symbol *sym;
1835 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1837 for (sym = f->symtab[hash]; sym; sym = sym->next)
1838 if (f->symbol_cmp(sym->name, name) == 0)
1845 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1848 if (sym->secidx >= SHN_LORESERVE)
1851 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1853 /* As a special case, a NULL sym has value zero. */
1858 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1860 int i, n = f->header.e_shnum;
1862 for (i = 0; i < n; ++i)
1863 if (strcmp(f->sections[i]->name, name) == 0)
1864 return f->sections[i];
1869 static int obj_load_order_prio(struct obj_section *a)
1871 unsigned long af, ac;
1873 af = a->header.sh_flags;
1876 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1877 strcmp(a->name + 5, ".init"))
1881 if (!(af & SHF_WRITE))
1883 if (af & SHF_EXECINSTR)
1885 if (a->header.sh_type != SHT_NOBITS)
1892 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1894 struct obj_section **p;
1895 int prio = obj_load_order_prio(sec);
1896 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1897 if (obj_load_order_prio(*p) < prio)
1899 sec->load_next = *p;
1903 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1905 unsigned long align,
1908 int newidx = f->header.e_shnum++;
1909 struct obj_section *sec;
1911 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1912 f->sections[newidx] = sec = arch_new_section();
1914 memset(sec, 0, sizeof(*sec));
1915 sec->header.sh_type = SHT_PROGBITS;
1916 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1917 sec->header.sh_size = size;
1918 sec->header.sh_addralign = align;
1922 sec->contents = xmalloc(size);
1924 obj_insert_section_load_order(f, sec);
1929 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1931 unsigned long align,
1934 int newidx = f->header.e_shnum++;
1935 struct obj_section *sec;
1937 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1938 f->sections[newidx] = sec = arch_new_section();
1940 memset(sec, 0, sizeof(*sec));
1941 sec->header.sh_type = SHT_PROGBITS;
1942 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1943 sec->header.sh_size = size;
1944 sec->header.sh_addralign = align;
1948 sec->contents = xmalloc(size);
1950 sec->load_next = f->load_order;
1951 f->load_order = sec;
1952 if (f->load_order_search_start == &f->load_order)
1953 f->load_order_search_start = &sec->load_next;
1958 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1960 unsigned long oldsize = sec->header.sh_size;
1962 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1964 return sec->contents + oldsize;
1968 /* Conditionally add the symbols from the given symbol set to the
1974 int idx, struct new_module_symbol *syms, size_t nsyms)
1976 struct new_module_symbol *s;
1979 #ifdef SYMBOL_PREFIX
1981 size_t name_alloced_size = 0;
1983 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1986 gpl = obj_gpl_license(f, NULL) == 0;
1988 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1989 /* Only add symbols that are already marked external.
1990 If we override locals we may cause problems for
1991 argument initialization. We will also create a false
1992 dependency on the module. */
1993 struct obj_symbol *sym;
1996 /* GPL licensed modules can use symbols exported with
1997 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
1998 * exported names. Non-GPL modules never see any GPLONLY_
1999 * symbols so they cannot fudge it by adding the prefix on
2002 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2003 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2005 ((char *)s->name) += 8;
2010 name = (char *)s->name;
2012 #ifdef SYMBOL_PREFIX
2013 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2014 kernel exports `C names', but module object files
2015 reference `linker names'). */
2016 size_t extra = sizeof SYMBOL_PREFIX;
2017 size_t name_size = strlen (name) + extra;
2018 if (name_size > name_alloced_size) {
2019 name_alloced_size = name_size * 2;
2020 name_buf = alloca (name_alloced_size);
2022 strcpy (name_buf, SYMBOL_PREFIX);
2023 strcpy (name_buf + extra - 1, name);
2025 #endif /* SYMBOL_PREFIX */
2027 sym = obj_find_symbol(f, name);
2028 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
2029 #ifdef SYMBOL_PREFIX
2030 /* Put NAME_BUF into more permanent storage. */
2031 name = xmalloc (name_size);
2032 strcpy (name, name_buf);
2034 sym = obj_add_symbol(f, name, -1,
2035 ELFW(ST_INFO) (STB_GLOBAL,
2038 /* Did our symbol just get installed? If so, mark the
2039 module as "used". */
2040 if (sym->secidx == idx)
2048 static void add_kernel_symbols(struct obj_file *f)
2050 struct external_module *m;
2053 /* Add module symbols first. */
2055 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2057 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2058 m->nsyms)) m->used = 1, ++nused;
2060 n_ext_modules_used = nused;
2062 /* And finally the symbols from the kernel proper. */
2065 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2068 static char *get_modinfo_value(struct obj_file *f, const char *key)
2070 struct obj_section *sec;
2071 char *p, *v, *n, *ep;
2072 size_t klen = strlen(key);
2074 sec = obj_find_section(f, ".modinfo");
2078 ep = p + sec->header.sh_size;
2081 n = strchr(p, '\0');
2083 if (p + klen == v && strncmp(p, key, klen) == 0)
2086 if (p + klen == n && strcmp(p, key) == 0)
2096 /*======================================================================*/
2097 /* Functions relating to module loading in pre 2.1 kernels. */
2100 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
2104 struct obj_symbol *sym;
2108 if ((q = strchr(p, '=')) == NULL) {
2114 sym = obj_find_symbol(f, p);
2116 /* Also check that the parameter was not resolved from the kernel. */
2117 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2118 bb_error_msg("symbol for parameter %s not found", p);
2122 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
2124 /* Do C quoting if we begin with a ". */
2128 str = alloca(strlen(q));
2129 for (r = str, q++; *q != '"'; ++q, ++r) {
2131 bb_error_msg("improperly terminated string argument for %s", p);
2133 } else if (*q == '\\')
2167 if (q[1] >= '0' && q[1] <= '7') {
2168 c = (c * 8) + *++q - '0';
2169 if (q[1] >= '0' && q[1] <= '7')
2170 c = (c * 8) + *++q - '0';
2183 obj_string_patch(f, sym->secidx, sym->value, str);
2184 } else if (*q >= '0' && *q <= '9') {
2186 *loc++ = strtoul(q, &q, 0);
2187 while (*q++ == ',');
2189 char *contents = f->sections[sym->secidx]->contents;
2190 char *myloc = contents + sym->value;
2191 char *r; /* To search for commas */
2193 /* Break the string with comas */
2194 while ((r = strchr(q, ',')) != (char *) NULL) {
2196 obj_string_patch(f, sym->secidx, myloc - contents, q);
2197 myloc += sizeof(char *);
2202 obj_string_patch(f, sym->secidx, myloc - contents, q);
2211 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2212 static int old_is_module_checksummed(struct obj_file *f)
2214 return obj_find_symbol(f, "Using_Versions") != NULL;
2216 /* Get the module's kernel version in the canonical integer form. */
2219 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2221 struct obj_symbol *sym;
2225 sym = obj_find_symbol(f, "kernel_version");
2229 p = f->sections[sym->secidx]->contents + sym->value;
2230 safe_strncpy(str, p, STRVERSIONLEN);
2232 a = strtoul(p, &p, 10);
2235 b = strtoul(p + 1, &p, 10);
2238 c = strtoul(p + 1, &q, 10);
2242 return a << 16 | b << 8 | c;
2245 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2247 #ifdef CONFIG_FEATURE_2_2_MODULES
2249 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
2251 static int old_get_kernel_symbols(const char *m_name)
2253 struct old_kernel_sym *ks, *k;
2254 struct new_module_symbol *s;
2255 struct external_module *mod;
2256 int nks, nms, nmod, i;
2258 nks = get_kernel_syms(NULL);
2261 bb_perror_msg("get_kernel_syms: %s", m_name);
2263 bb_error_msg("No kernel symbols");
2267 ks = k = xmalloc(nks * sizeof(*ks));
2269 if (get_kernel_syms(ks) != nks) {
2270 perror("inconsistency with get_kernel_syms -- is someone else "
2271 "playing with modules?");
2276 /* Collect the module information. */
2281 while (k->name[0] == '#' && k->name[1]) {
2282 struct old_kernel_sym *k2;
2284 /* Find out how many symbols this module has. */
2285 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
2289 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
2290 mod[nmod].name = k->name + 1;
2291 mod[nmod].addr = k->value;
2293 mod[nmod].nsyms = nms;
2294 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2296 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
2297 s->name = (unsigned long) k->name;
2298 s->value = k->value;
2305 n_ext_modules = nmod + 1;
2307 /* Now collect the symbols for the kernel proper. */
2309 if (k->name[0] == '#')
2312 nksyms = nms = nks - (k - ks);
2313 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2315 for (i = 0; i < nms; ++i, ++s, ++k) {
2316 s->name = (unsigned long) k->name;
2317 s->value = k->value;
2323 /* Return the kernel symbol checksum version, or zero if not used. */
2325 static int old_is_kernel_checksummed(void)
2327 /* Using_Versions is the first symbol. */
2329 && strcmp((char *) ksyms[0].name,
2330 "Using_Versions") == 0) return ksyms[0].value;
2336 static int old_create_mod_use_count(struct obj_file *f)
2338 struct obj_section *sec;
2340 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2343 obj_add_symbol(f, "mod_use_count_", -1,
2344 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2351 old_init_module(const char *m_name, struct obj_file *f,
2352 unsigned long m_size)
2355 struct old_mod_routines routines;
2356 struct old_symbol_table *symtab;
2359 /* Create the symbol table */
2361 int nsyms = 0, strsize = 0, total;
2363 /* Size things first... */
2366 for (i = 0; i < HASH_BUCKETS; ++i) {
2367 struct obj_symbol *sym;
2368 for (sym = f->symtab[i]; sym; sym = sym->next)
2369 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2370 && sym->secidx <= SHN_HIRESERVE)
2372 sym->ksymidx = nsyms++;
2373 strsize += strlen(sym->name) + 1;
2378 total = (sizeof(struct old_symbol_table)
2379 + nsyms * sizeof(struct old_module_symbol)
2380 + n_ext_modules_used * sizeof(struct old_module_ref)
2382 symtab = xmalloc(total);
2383 symtab->size = total;
2384 symtab->n_symbols = nsyms;
2385 symtab->n_refs = n_ext_modules_used;
2387 if (flag_export && nsyms) {
2388 struct old_module_symbol *ksym;
2392 ksym = symtab->symbol;
2393 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2394 + n_ext_modules_used * sizeof(struct old_module_ref));
2396 for (i = 0; i < HASH_BUCKETS; ++i) {
2397 struct obj_symbol *sym;
2398 for (sym = f->symtab[i]; sym; sym = sym->next)
2399 if (sym->ksymidx >= 0) {
2400 ksym->addr = obj_symbol_final_value(f, sym);
2402 (unsigned long) str - (unsigned long) symtab;
2404 strcpy(str, sym->name);
2405 str += strlen(sym->name) + 1;
2411 if (n_ext_modules_used) {
2412 struct old_module_ref *ref;
2415 ref = (struct old_module_ref *)
2416 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2418 for (i = 0; i < n_ext_modules; ++i)
2419 if (ext_modules[i].used)
2420 ref++->module = ext_modules[i].addr;
2424 /* Fill in routines. */
2427 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2429 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2431 /* Whew! All of the initialization is complete. Collect the final
2432 module image and give it to the kernel. */
2434 image = xmalloc(m_size);
2435 obj_create_image(f, image);
2437 /* image holds the complete relocated module, accounting correctly for
2438 mod_use_count. However the old module kernel support assume that
2439 it is receiving something which does not contain mod_use_count. */
2440 ret = old_sys_init_module(m_name, image + sizeof(long),
2441 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2442 : 0), &routines, symtab);
2444 bb_perror_msg("init_module: %s", m_name);
2454 #define old_create_mod_use_count(x) TRUE
2455 #define old_init_module(x, y, z) TRUE
2457 #endif /* CONFIG_FEATURE_2_2_MODULES */
2461 /*======================================================================*/
2462 /* Functions relating to module loading after 2.1.18. */
2465 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2468 char *p, *q, *key, *sym_name;
2469 struct obj_symbol *sym;
2470 char *contents, *loc;
2474 if ((q = strchr(p, '=')) == NULL) {
2479 key = alloca(q - p + 6);
2480 memcpy(key, "parm_", 5);
2481 memcpy(key + 5, p, q - p);
2484 p = get_modinfo_value(f, key);
2487 bb_error_msg("invalid parameter %s", key);
2491 #ifdef SYMBOL_PREFIX
2492 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2493 strcpy (sym_name, SYMBOL_PREFIX);
2494 strcat (sym_name, key);
2498 sym = obj_find_symbol(f, sym_name);
2500 /* Also check that the parameter was not resolved from the kernel. */
2501 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2502 bb_error_msg("symbol for parameter %s not found", key);
2507 min = strtoul(p, &p, 10);
2509 max = strtoul(p + 1, &p, 10);
2515 contents = f->sections[sym->secidx]->contents;
2516 loc = contents + sym->value;
2520 if ((*p == 's') || (*p == 'c')) {
2523 /* Do C quoting if we begin with a ", else slurp the lot. */
2527 str = alloca(strlen(q));
2528 for (r = str, q++; *q != '"'; ++q, ++r) {
2530 bb_error_msg("improperly terminated string argument for %s",
2533 } else if (*q == '\\')
2567 if (q[1] >= '0' && q[1] <= '7') {
2568 c = (c * 8) + *++q - '0';
2569 if (q[1] >= '0' && q[1] <= '7')
2570 c = (c * 8) + *++q - '0';
2587 /* In this case, the string is not quoted. We will break
2588 it using the coma (like for ints). If the user wants to
2589 include comas in a string, he just has to quote it */
2591 /* Search the next coma */
2595 if (r != (char *) NULL) {
2596 /* Recopy the current field */
2597 str = alloca(r - q + 1);
2598 memcpy(str, q, r - q);
2600 /* I don't know if it is usefull, as the previous case
2601 doesn't null terminate the string ??? */
2604 /* Keep next fields */
2615 obj_string_patch(f, sym->secidx, loc - contents, str);
2616 loc += tgt_sizeof_char_p;
2618 /* Array of chars (in fact, matrix !) */
2619 unsigned long charssize; /* size of each member */
2621 /* Get the size of each member */
2622 /* Probably we should do that outside the loop ? */
2623 if (!isdigit(*(p + 1))) {
2624 bb_error_msg("parameter type 'c' for %s must be followed by"
2625 " the maximum size", key);
2628 charssize = strtoul(p + 1, (char **) NULL, 10);
2631 if (strlen(str) >= charssize) {
2632 bb_error_msg("string too long for %s (max %ld)", key,
2637 /* Copy to location */
2638 strcpy((char *) loc, str);
2642 long v = strtoul(q, &q, 0);
2649 loc += tgt_sizeof_short;
2653 loc += tgt_sizeof_int;
2657 loc += tgt_sizeof_long;
2661 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2676 goto retry_end_of_value;
2680 bb_error_msg("too many values for %s (max %d)", key, max);
2687 bb_error_msg("invalid argument syntax for %s", key);
2694 bb_error_msg("too few values for %s (min %d)", key, min);
2704 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2705 static int new_is_module_checksummed(struct obj_file *f)
2707 const char *p = get_modinfo_value(f, "using_checksums");
2714 /* Get the module's kernel version in the canonical integer form. */
2717 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2722 p = get_modinfo_value(f, "kernel_version");
2725 safe_strncpy(str, p, STRVERSIONLEN);
2727 a = strtoul(p, &p, 10);
2730 b = strtoul(p + 1, &p, 10);
2733 c = strtoul(p + 1, &q, 10);
2737 return a << 16 | b << 8 | c;
2740 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2743 #ifdef CONFIG_FEATURE_2_4_MODULES
2745 /* Fetch the loaded modules, and all currently exported symbols. */
2747 static int new_get_kernel_symbols(void)
2749 char *module_names, *mn;
2750 struct external_module *modules, *m;
2751 struct new_module_symbol *syms, *s;
2752 size_t ret, bufsize, nmod, nsyms, i, j;
2754 /* Collect the loaded modules. */
2756 module_names = xmalloc(bufsize = 256);
2758 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2759 if (errno == ENOSPC && bufsize < ret) {
2760 module_names = xrealloc(module_names, bufsize = ret);
2761 goto retry_modules_load;
2763 bb_perror_msg("QM_MODULES");
2767 n_ext_modules = nmod = ret;
2769 /* Collect the modules' symbols. */
2772 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2773 memset(modules, 0, nmod * sizeof(*modules));
2774 for (i = 0, mn = module_names, m = modules;
2775 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2776 struct new_module_info info;
2778 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2779 if (errno == ENOENT) {
2780 /* The module was removed out from underneath us. */
2783 bb_perror_msg("query_module: QM_INFO: %s", mn);
2787 syms = xmalloc(bufsize = 1024);
2789 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2792 syms = xrealloc(syms, bufsize = ret);
2793 goto retry_mod_sym_load;
2795 /* The module was removed out from underneath us. */
2798 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2805 m->addr = info.addr;
2809 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2810 s->name += (unsigned long) syms;
2815 /* Collect the kernel's symbols. */
2817 syms = xmalloc(bufsize = 16 * 1024);
2818 retry_kern_sym_load:
2819 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2820 if (errno == ENOSPC && bufsize < ret) {
2821 syms = xrealloc(syms, bufsize = ret);
2822 goto retry_kern_sym_load;
2824 bb_perror_msg("kernel: QM_SYMBOLS");
2827 nksyms = nsyms = ret;
2830 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2831 s->name += (unsigned long) syms;
2837 /* Return the kernel symbol checksum version, or zero if not used. */
2839 static int new_is_kernel_checksummed(void)
2841 struct new_module_symbol *s;
2844 /* Using_Versions is not the first symbol, but it should be in there. */
2846 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2847 if (strcmp((char *) s->name, "Using_Versions") == 0)
2854 static int new_create_this_module(struct obj_file *f, const char *m_name)
2856 struct obj_section *sec;
2858 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2859 sizeof(struct new_module));
2860 memset(sec->contents, 0, sizeof(struct new_module));
2862 obj_add_symbol(f, SPFX "__this_module", -1,
2863 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2864 sizeof(struct new_module));
2866 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2872 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2873 /* add an entry to the __ksymtab section, creating it if necessary */
2874 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2876 struct obj_section *sec;
2879 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2880 * If __ksymtab is defined but not marked alloc, x out the first character
2881 * (no obj_delete routine) and create a new __ksymtab with the correct
2884 sec = obj_find_section(f, "__ksymtab");
2885 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2886 *((char *)(sec->name)) = 'x'; /* override const */
2890 sec = obj_create_alloced_section(f, "__ksymtab",
2891 tgt_sizeof_void_p, 0);
2894 sec->header.sh_flags |= SHF_ALLOC;
2895 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2897 ofs = sec->header.sh_size;
2898 obj_symbol_patch(f, sec->idx, ofs, sym);
2899 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2900 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2902 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2904 static int new_create_module_ksymtab(struct obj_file *f)
2906 struct obj_section *sec;
2909 /* We must always add the module references. */
2911 if (n_ext_modules_used) {
2912 struct new_module_ref *dep;
2913 struct obj_symbol *tm;
2915 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2916 (sizeof(struct new_module_ref)
2917 * n_ext_modules_used));
2921 tm = obj_find_symbol(f, SPFX "__this_module");
2922 dep = (struct new_module_ref *) sec->contents;
2923 for (i = 0; i < n_ext_modules; ++i)
2924 if (ext_modules[i].used) {
2925 dep->dep = ext_modules[i].addr;
2926 obj_symbol_patch(f, sec->idx,
2927 (char *) &dep->ref - sec->contents, tm);
2933 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2938 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2941 /* We don't want to export symbols residing in sections that
2942 aren't loaded. There are a number of these created so that
2943 we make sure certain module options don't appear twice. */
2945 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2947 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2949 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2950 struct obj_symbol *sym;
2951 for (sym = f->symtab[i]; sym; sym = sym->next)
2952 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2953 && sym->secidx <= SHN_HIRESERVE
2954 && (sym->secidx >= SHN_LORESERVE
2955 || loaded[sym->secidx])) {
2956 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2958 obj_symbol_patch(f, sec->idx, ofs, sym);
2959 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2966 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2974 new_init_module(const char *m_name, struct obj_file *f,
2975 unsigned long m_size)
2977 struct new_module *module;
2978 struct obj_section *sec;
2983 sec = obj_find_section(f, ".this");
2984 if (!sec || !sec->contents) {
2985 bb_perror_msg_and_die("corrupt module %s?",m_name);
2987 module = (struct new_module *) sec->contents;
2988 m_addr = sec->header.sh_addr;
2990 module->size_of_struct = sizeof(*module);
2991 module->size = m_size;
2992 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2994 sec = obj_find_section(f, "__ksymtab");
2995 if (sec && sec->header.sh_size) {
2996 module->syms = sec->header.sh_addr;
2997 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
3000 if (n_ext_modules_used) {
3001 sec = obj_find_section(f, ".kmodtab");
3002 module->deps = sec->header.sh_addr;
3003 module->ndeps = n_ext_modules_used;
3007 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
3009 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
3011 sec = obj_find_section(f, "__ex_table");
3013 module->ex_table_start = sec->header.sh_addr;
3014 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
3017 sec = obj_find_section(f, ".text.init");
3019 module->runsize = sec->header.sh_addr - m_addr;
3021 sec = obj_find_section(f, ".data.init");
3023 if (!module->runsize ||
3024 module->runsize > sec->header.sh_addr - m_addr)
3025 module->runsize = sec->header.sh_addr - m_addr;
3027 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
3028 if (sec && sec->header.sh_size) {
3029 module->archdata_start = (void*)sec->header.sh_addr;
3030 module->archdata_end = module->archdata_start + sec->header.sh_size;
3032 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
3033 if (sec && sec->header.sh_size) {
3034 module->kallsyms_start = (void*)sec->header.sh_addr;
3035 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
3038 if (!arch_init_module(f, module))
3041 /* Whew! All of the initialization is complete. Collect the final
3042 module image and give it to the kernel. */
3044 image = xmalloc(m_size);
3045 obj_create_image(f, image);
3047 ret = new_sys_init_module(m_name, (struct new_module *) image);
3049 bb_perror_msg("init_module: %s", m_name);
3058 #define new_init_module(x, y, z) TRUE
3059 #define new_create_this_module(x, y) 0
3060 #define new_add_ksymtab(x, y) -1
3061 #define new_create_module_ksymtab(x)
3062 #define query_module(v, w, x, y, z) -1
3064 #endif /* CONFIG_FEATURE_2_4_MODULES */
3067 /*======================================================================*/
3070 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3073 struct obj_string_patch *p;
3074 struct obj_section *strsec;
3075 size_t len = strlen(string) + 1;
3078 p = xmalloc(sizeof(*p));
3079 p->next = f->string_patches;
3080 p->reloc_secidx = secidx;
3081 p->reloc_offset = offset;
3082 f->string_patches = p;
3084 strsec = obj_find_section(f, ".kstrtab");
3085 if (strsec == NULL) {
3086 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3087 p->string_offset = 0;
3088 loc = strsec->contents;
3090 p->string_offset = strsec->header.sh_size;
3091 loc = obj_extend_section(strsec, len);
3093 memcpy(loc, string, len);
3098 #ifdef CONFIG_FEATURE_2_4_MODULES
3100 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3101 struct obj_symbol *sym)
3103 struct obj_symbol_patch *p;
3105 p = xmalloc(sizeof(*p));
3106 p->next = f->symbol_patches;
3107 p->reloc_secidx = secidx;
3108 p->reloc_offset = offset;
3110 f->symbol_patches = p;
3116 static int obj_check_undefineds(struct obj_file *f)
3121 for (i = 0; i < HASH_BUCKETS; ++i) {
3122 struct obj_symbol *sym;
3123 for (sym = f->symtab[i]; sym; sym = sym->next)
3124 if (sym->secidx == SHN_UNDEF) {
3125 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
3126 sym->secidx = SHN_ABS;
3130 bb_error_msg("unresolved symbol %s", sym->name);
3140 static void obj_allocate_commons(struct obj_file *f)
3142 struct common_entry {
3143 struct common_entry *next;
3144 struct obj_symbol *sym;
3145 } *common_head = NULL;
3149 for (i = 0; i < HASH_BUCKETS; ++i) {
3150 struct obj_symbol *sym;
3151 for (sym = f->symtab[i]; sym; sym = sym->next)
3152 if (sym->secidx == SHN_COMMON) {
3153 /* Collect all COMMON symbols and sort them by size so as to
3154 minimize space wasted by alignment requirements. */
3156 struct common_entry **p, *n;
3157 for (p = &common_head; *p; p = &(*p)->next)
3158 if (sym->size <= (*p)->sym->size)
3161 n = alloca(sizeof(*n));
3169 for (i = 1; i < f->local_symtab_size; ++i) {
3170 struct obj_symbol *sym = f->local_symtab[i];
3171 if (sym && sym->secidx == SHN_COMMON) {
3172 struct common_entry **p, *n;
3173 for (p = &common_head; *p; p = &(*p)->next)
3174 if (sym == (*p)->sym)
3176 else if (sym->size < (*p)->sym->size) {
3177 n = alloca(sizeof(*n));
3187 /* Find the bss section. */
3188 for (i = 0; i < f->header.e_shnum; ++i)
3189 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3192 /* If for some reason there hadn't been one, create one. */
3193 if (i == f->header.e_shnum) {
3194 struct obj_section *sec;
3196 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3197 f->sections[i] = sec = arch_new_section();
3198 f->header.e_shnum = i + 1;
3200 memset(sec, 0, sizeof(*sec));
3201 sec->header.sh_type = SHT_PROGBITS;
3202 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3207 /* Allocate the COMMONS. */
3209 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3210 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3211 struct common_entry *c;
3213 for (c = common_head; c; c = c->next) {
3214 ElfW(Addr) align = c->sym->value;
3216 if (align > max_align)
3218 if (bss_size & (align - 1))
3219 bss_size = (bss_size | (align - 1)) + 1;
3222 c->sym->value = bss_size;
3224 bss_size += c->sym->size;
3227 f->sections[i]->header.sh_size = bss_size;
3228 f->sections[i]->header.sh_addralign = max_align;
3232 /* For the sake of patch relocation and parameter initialization,
3233 allocate zeroed data for NOBITS sections now. Note that after
3234 this we cannot assume NOBITS are really empty. */
3235 for (i = 0; i < f->header.e_shnum; ++i) {
3236 struct obj_section *s = f->sections[i];
3237 if (s->header.sh_type == SHT_NOBITS) {
3238 if (s->header.sh_size != 0)
3239 s->contents = memset(xmalloc(s->header.sh_size),
3240 0, s->header.sh_size);
3244 s->header.sh_type = SHT_PROGBITS;
3249 static unsigned long obj_load_size(struct obj_file *f)
3251 unsigned long dot = 0;
3252 struct obj_section *sec;
3254 /* Finalize the positions of the sections relative to one another. */
3256 for (sec = f->load_order; sec; sec = sec->load_next) {
3259 align = sec->header.sh_addralign;
3260 if (align && (dot & (align - 1)))
3261 dot = (dot | (align - 1)) + 1;
3263 sec->header.sh_addr = dot;
3264 dot += sec->header.sh_size;
3270 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3272 int i, n = f->header.e_shnum;
3275 /* Finalize the addresses of the sections. */
3278 for (i = 0; i < n; ++i)
3279 f->sections[i]->header.sh_addr += base;
3281 /* And iterate over all of the relocations. */
3283 for (i = 0; i < n; ++i) {
3284 struct obj_section *relsec, *symsec, *targsec, *strsec;
3285 ElfW(RelM) * rel, *relend;
3289 relsec = f->sections[i];
3290 if (relsec->header.sh_type != SHT_RELM)
3293 symsec = f->sections[relsec->header.sh_link];
3294 targsec = f->sections[relsec->header.sh_info];
3295 strsec = f->sections[symsec->header.sh_link];
3297 rel = (ElfW(RelM) *) relsec->contents;
3298 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3299 symtab = (ElfW(Sym) *) symsec->contents;
3300 strtab = (const char *) strsec->contents;
3302 for (; rel < relend; ++rel) {
3303 ElfW(Addr) value = 0;
3304 struct obj_symbol *intsym = NULL;
3305 unsigned long symndx;
3306 ElfW(Sym) * extsym = 0;
3309 /* Attempt to find a value to use for this relocation. */
3311 symndx = ELFW(R_SYM) (rel->r_info);
3313 /* Note we've already checked for undefined symbols. */
3315 extsym = &symtab[symndx];
3316 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
3317 /* Local symbols we look up in the local table to be sure
3318 we get the one that is really intended. */
3319 intsym = f->local_symtab[symndx];
3321 /* Others we look up in the hash table. */
3323 if (extsym->st_name)
3324 name = strtab + extsym->st_name;
3326 name = f->sections[extsym->st_shndx]->name;
3327 intsym = obj_find_symbol(f, name);
3330 value = obj_symbol_final_value(f, intsym);
3331 intsym->referenced = 1;
3333 #if SHT_RELM == SHT_RELA
3334 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3335 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3336 if (!extsym || !extsym->st_name ||
3337 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
3339 value += rel->r_addend;
3343 switch (arch_apply_relocation
3344 (f, targsec, symsec, intsym, rel, value)) {
3348 case obj_reloc_overflow:
3349 errmsg = "Relocation overflow";
3351 case obj_reloc_dangerous:
3352 errmsg = "Dangerous relocation";
3354 case obj_reloc_unhandled:
3355 errmsg = "Unhandled relocation";
3358 bb_error_msg("%s of type %ld for %s", errmsg,
3359 (long) ELFW(R_TYPE) (rel->r_info),
3360 strtab + extsym->st_name);
3362 bb_error_msg("%s of type %ld", errmsg,
3363 (long) ELFW(R_TYPE) (rel->r_info));
3371 /* Finally, take care of the patches. */
3373 if (f->string_patches) {
3374 struct obj_string_patch *p;
3375 struct obj_section *strsec;
3376 ElfW(Addr) strsec_base;
3377 strsec = obj_find_section(f, ".kstrtab");
3378 strsec_base = strsec->header.sh_addr;
3380 for (p = f->string_patches; p; p = p->next) {
3381 struct obj_section *targsec = f->sections[p->reloc_secidx];
3382 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3383 = strsec_base + p->string_offset;
3387 if (f->symbol_patches) {
3388 struct obj_symbol_patch *p;
3390 for (p = f->symbol_patches; p; p = p->next) {
3391 struct obj_section *targsec = f->sections[p->reloc_secidx];
3392 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3393 = obj_symbol_final_value(f, p->sym);
3400 static int obj_create_image(struct obj_file *f, char *image)
3402 struct obj_section *sec;
3403 ElfW(Addr) base = f->baseaddr;
3405 for (sec = f->load_order; sec; sec = sec->load_next) {
3408 if (sec->contents == 0 || sec->header.sh_size == 0)
3411 secimg = image + (sec->header.sh_addr - base);
3413 /* Note that we allocated data for NOBITS sections earlier. */
3414 memcpy(secimg, sec->contents, sec->header.sh_size);
3420 /*======================================================================*/
3422 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3425 ElfW(Shdr) * section_headers;
3429 /* Read the file header. */
3431 f = arch_new_file();
3432 memset(f, 0, sizeof(*f));
3433 f->symbol_cmp = strcmp;
3434 f->symbol_hash = obj_elf_hash;
3435 f->load_order_search_start = &f->load_order;
3437 fseek(fp, 0, SEEK_SET);
3438 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3439 bb_perror_msg("error reading ELF header");
3443 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3444 || f->header.e_ident[EI_MAG1] != ELFMAG1
3445 || f->header.e_ident[EI_MAG2] != ELFMAG2
3446 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3447 bb_error_msg("not an ELF file");
3450 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3451 || f->header.e_ident[EI_DATA] != ELFDATAM
3452 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3453 || !MATCH_MACHINE(f->header.e_machine)) {
3454 bb_error_msg("ELF file not for this architecture");
3457 if (f->header.e_type != ET_REL) {
3458 bb_error_msg("ELF file not a relocatable object");
3462 /* Read the section headers. */
3464 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3465 bb_error_msg("section header size mismatch: %lu != %lu",
3466 (unsigned long) f->header.e_shentsize,
3467 (unsigned long) sizeof(ElfW(Shdr)));
3471 shnum = f->header.e_shnum;
3472 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3473 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3475 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3476 fseek(fp, f->header.e_shoff, SEEK_SET);
3477 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3478 bb_perror_msg("error reading ELF section headers");
3482 /* Read the section data. */
3484 for (i = 0; i < shnum; ++i) {
3485 struct obj_section *sec;
3487 f->sections[i] = sec = arch_new_section();
3488 memset(sec, 0, sizeof(*sec));
3490 sec->header = section_headers[i];
3493 if(sec->header.sh_size) switch (sec->header.sh_type) {
3502 if (!loadprogbits) {
3503 sec->contents = NULL;
3510 if (sec->header.sh_size > 0) {
3511 sec->contents = xmalloc(sec->header.sh_size);
3512 fseek(fp, sec->header.sh_offset, SEEK_SET);
3513 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3514 bb_perror_msg("error reading ELF section data");
3518 sec->contents = NULL;
3522 #if SHT_RELM == SHT_REL
3524 bb_error_msg("RELA relocations not supported on this architecture");
3528 bb_error_msg("REL relocations not supported on this architecture");
3533 if (sec->header.sh_type >= SHT_LOPROC) {
3534 /* Assume processor specific section types are debug
3535 info and can safely be ignored. If this is ever not
3536 the case (Hello MIPS?), don't put ifdefs here but
3537 create an arch_load_proc_section(). */
3541 bb_error_msg("can't handle sections of type %ld",
3542 (long) sec->header.sh_type);
3547 /* Do what sort of interpretation as needed by each section. */
3549 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3551 for (i = 0; i < shnum; ++i) {
3552 struct obj_section *sec = f->sections[i];
3553 sec->name = shstrtab + sec->header.sh_name;
3556 for (i = 0; i < shnum; ++i) {
3557 struct obj_section *sec = f->sections[i];
3559 /* .modinfo should be contents only but gcc has no attribute for that.
3560 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3562 if (strcmp(sec->name, ".modinfo") == 0)
3563 sec->header.sh_flags &= ~SHF_ALLOC;
3565 if (sec->header.sh_flags & SHF_ALLOC)
3566 obj_insert_section_load_order(f, sec);
3568 switch (sec->header.sh_type) {
3571 unsigned long nsym, j;
3575 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3576 bb_error_msg("symbol size mismatch: %lu != %lu",
3577 (unsigned long) sec->header.sh_entsize,
3578 (unsigned long) sizeof(ElfW(Sym)));
3582 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3583 strtab = f->sections[sec->header.sh_link]->contents;
3584 sym = (ElfW(Sym) *) sec->contents;
3586 /* Allocate space for a table of local symbols. */
3587 j = f->local_symtab_size = sec->header.sh_info;
3588 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3590 /* Insert all symbols into the hash table. */
3591 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3592 ElfW(Addr) val = sym->st_value;
3595 name = strtab + sym->st_name;
3596 else if (sym->st_shndx < shnum)
3597 name = f->sections[sym->st_shndx]->name;
3601 #if defined(__SH5__)
3603 * For sh64 it is possible that the target of a branch
3604 * requires a mode switch (32 to 16 and back again).
3606 * This is implied by the lsb being set in the target
3607 * address for SHmedia mode and clear for SHcompact.
3609 val |= sym->st_other & 4;
3612 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3619 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3620 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3621 (unsigned long) sec->header.sh_entsize,
3622 (unsigned long) sizeof(ElfW(RelM)));
3626 /* XXX Relocation code from modutils-2.3.19 is not here.
3627 * Why? That's about 20 lines of code from obj/obj_load.c,
3628 * which gets done in a second pass through the sections.
3629 * This BusyBox insmod does similar work in obj_relocate(). */
3636 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3638 * load the unloaded sections directly into the memory allocated by
3639 * kernel for the module
3642 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3644 ElfW(Addr) base = f->baseaddr;
3645 struct obj_section* sec;
3647 for (sec = f->load_order; sec; sec = sec->load_next) {
3649 /* section already loaded? */
3650 if (sec->contents != NULL)
3653 if (sec->header.sh_size == 0)
3656 sec->contents = imagebase + (sec->header.sh_addr - base);
3657 fseek(fp, sec->header.sh_offset, SEEK_SET);
3658 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3659 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3668 static void hide_special_symbols(struct obj_file *f)
3670 static const char *const specials[] = {
3671 SPFX "cleanup_module",
3673 SPFX "kernel_version",
3677 struct obj_symbol *sym;
3678 const char *const *p;
3680 for (p = specials; *p; ++p)
3681 if ((sym = obj_find_symbol(f, *p)) != NULL)
3683 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3687 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3688 static int obj_gpl_license(struct obj_file *f, const char **license)
3690 struct obj_section *sec;
3691 /* This list must match *exactly* the list of allowable licenses in
3692 * linux/include/linux/module.h. Checking for leading "GPL" will not
3693 * work, somebody will use "GPL sucks, this is proprietary".
3695 static const char *gpl_licenses[] = {
3698 "GPL and additional rights",
3703 if ((sec = obj_find_section(f, ".modinfo"))) {
3704 const char *value, *ptr, *endptr;
3705 ptr = sec->contents;
3706 endptr = ptr + sec->header.sh_size;
3707 while (ptr < endptr) {
3708 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3712 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3713 if (strcmp(value+1, gpl_licenses[i]) == 0)
3718 if (strchr(ptr, '\0'))
3719 ptr = strchr(ptr, '\0') + 1;
3727 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3728 #define TAINT_PROPRIETORY_MODULE (1<<0)
3729 #define TAINT_FORCED_MODULE (1<<1)
3730 #define TAINT_UNSAFE_SMP (1<<2)
3731 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3733 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3734 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3738 static int first = 1;
3739 if (fd < 0 && !kernel_has_tainted)
3740 return; /* New modutils on old kernel */
3741 printf("Warning: loading %s will taint the kernel: %s%s\n",
3742 m_name, text1, text2);
3744 printf(" See %s for information about tainted modules\n", TAINT_URL);
3748 read(fd, buf, sizeof(buf)-1);
3749 buf[sizeof(buf)-1] = '\0';
3750 oldval = strtoul(buf, NULL, 10);
3751 sprintf(buf, "%d\n", oldval | taint);
3752 write(fd, buf, strlen(buf));
3756 /* Check if loading this module will taint the kernel. */
3757 static void check_tainted_module(struct obj_file *f, char *m_name)
3759 static const char tainted_file[] = TAINT_FILENAME;
3760 int fd, kernel_has_tainted;
3763 kernel_has_tainted = 1;
3764 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3765 if (errno == ENOENT)
3766 kernel_has_tainted = 0;
3767 else if (errno == EACCES)
3768 kernel_has_tainted = 1;
3770 perror(tainted_file);
3771 kernel_has_tainted = 0;
3775 switch (obj_gpl_license(f, &ptr)) {
3779 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3782 /* The module has a non-GPL license so we pretend that the
3783 * kernel always has a taint flag to get a warning even on
3784 * kernels without the proc flag.
3786 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3789 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3793 if (flag_force_load)
3794 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3799 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3800 #define check_tainted_module(x, y) do { } while(0);
3801 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3803 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3804 /* add module source, timestamp, kernel version and a symbol for the
3805 * start of some sections. this info is used by ksymoops to do better
3809 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3811 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3812 if (get_modinfo_value(f, "kernel_version") == NULL)
3813 return old_get_module_version(f, str);
3815 return new_get_module_version(f, str);
3816 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3817 strncpy(str, "???", sizeof(str));
3819 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3822 /* add module source, timestamp, kernel version and a symbol for the
3823 * start of some sections. this info is used by ksymoops to do better
3827 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3830 static const char symprefix[] = "__insmod_";
3831 struct obj_section *sec;
3832 struct obj_symbol *sym;
3833 char *name, *absolute_filename;
3834 char str[STRVERSIONLEN], real[PATH_MAX];
3835 int i, l, lm_name, lfilename, use_ksymtab, version;
3836 struct stat statbuf;
3838 static const char *section_names[] = {
3846 if (realpath(filename, real)) {
3847 absolute_filename = bb_xstrdup(real);
3850 int save_errno = errno;
3851 bb_error_msg("cannot get realpath for %s", filename);
3854 absolute_filename = bb_xstrdup(filename);
3857 lm_name = strlen(m_name);
3858 lfilename = strlen(absolute_filename);
3860 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3861 * are not to be exported. otherwise leave ksymtab alone for now, the
3862 * "export all symbols" compatibility code will export these symbols later.
3864 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3866 if ((sec = obj_find_section(f, ".this"))) {
3867 /* tag the module header with the object name, last modified
3868 * timestamp and module version. worst case for module version
3869 * is 0xffffff, decimal 16777215. putting all three fields in
3870 * one symbol is less readable but saves kernel space.
3872 l = sizeof(symprefix)+ /* "__insmod_" */
3873 lm_name+ /* module name */
3875 lfilename+ /* object filename */
3877 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3879 8+ /* version in dec */
3882 if (stat(absolute_filename, &statbuf) != 0)
3883 statbuf.st_mtime = 0;
3884 version = get_module_version(f, str); /* -1 if not found */
3885 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3886 symprefix, m_name, absolute_filename,
3887 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3889 sym = obj_add_symbol(f, name, -1,
3890 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3891 sec->idx, sec->header.sh_addr, 0);
3893 new_add_ksymtab(f, sym);
3895 free(absolute_filename);
3896 #ifdef _NOT_SUPPORTED_
3897 /* record where the persistent data is going, same address as previous symbol */
3900 l = sizeof(symprefix)+ /* "__insmod_" */
3901 lm_name+ /* module name */
3903 strlen(f->persist)+ /* data store */
3906 snprintf(name, l, "%s%s_P%s",
3907 symprefix, m_name, f->persist);
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);
3913 #endif /* _NOT_SUPPORTED_ */
3914 /* tag the desired sections if size is non-zero */
3916 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3917 if ((sec = obj_find_section(f, section_names[i])) &&
3918 sec->header.sh_size) {
3919 l = sizeof(symprefix)+ /* "__insmod_" */
3920 lm_name+ /* module name */
3922 strlen(sec->name)+ /* section name */
3924 8+ /* length in dec */
3927 snprintf(name, l, "%s%s_S%s_L%ld",
3928 symprefix, m_name, sec->name,
3929 (long)sec->header.sh_size);
3930 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3931 sec->idx, sec->header.sh_addr, 0);
3933 new_add_ksymtab(f, sym);
3937 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3939 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3940 static void print_load_map(struct obj_file *f)
3942 struct obj_symbol *sym;
3943 struct obj_symbol **all, **p;
3944 struct obj_section *sec;
3945 int i, nsyms, *loaded;
3947 /* Report on the section layout. */
3949 printf("Sections: Size %-*s Align\n",
3950 (int) (2 * sizeof(void *)), "Address");
3952 for (sec = f->load_order; sec; sec = sec->load_next) {
3956 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3961 printf("%-15s %08lx %0*lx 2**%d\n",
3963 (long)sec->header.sh_size,
3964 (int) (2 * sizeof(void *)),
3965 (long)sec->header.sh_addr,
3968 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3969 /* Quick reference which section indicies are loaded. */
3971 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3973 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3975 /* Collect the symbols we'll be listing. */
3977 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3978 for (sym = f->symtab[i]; sym; sym = sym->next)
3979 if (sym->secidx <= SHN_HIRESERVE
3980 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3983 all = alloca(nsyms * sizeof(struct obj_symbol *));
3985 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3986 for (sym = f->symtab[i]; sym; sym = sym->next)
3987 if (sym->secidx <= SHN_HIRESERVE
3988 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3991 /* And list them. */
3992 printf("\nSymbols:\n");
3993 for (p = all; p < all + nsyms; ++p) {
3995 unsigned long value;
3998 if (sym->secidx == SHN_ABS) {
4001 } else if (sym->secidx == SHN_UNDEF) {
4005 sec = f->sections[sym->secidx];
4007 if (sec->header.sh_type == SHT_NOBITS)
4009 else if (sec->header.sh_flags & SHF_ALLOC) {
4010 if (sec->header.sh_flags & SHF_EXECINSTR)
4012 else if (sec->header.sh_flags & SHF_WRITE)
4017 value = sym->value + sec->header.sh_addr;
4020 if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
4021 type = tolower(type);
4023 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
4031 extern int insmod_main( int argc, char **argv)
4038 unsigned long m_size;
4043 int exit_status = EXIT_FAILURE;
4045 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4046 struct utsname uts_info;
4047 char m_strversion[STRVERSIONLEN];
4051 #ifdef CONFIG_FEATURE_CLEAN_UP
4056 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4057 int flag_print_load_map = 0;
4060 struct utsname myuname;
4062 /* Parse any options */
4063 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4064 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
4066 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
4070 case 'f': /* force loading */
4071 flag_force_load = 1;
4073 case 'k': /* module loaded by kerneld, auto-cleanable */
4076 case 's': /* log to syslog */
4077 /* log to syslog -- not supported */
4078 /* but kernel needs this for request_module(), */
4079 /* as this calls: modprobe -k -s -- <module> */
4080 /* so silently ignore this flag */
4082 case 'v': /* verbose output */
4085 case 'q': /* silent */
4088 case 'x': /* do not export externs */
4091 case 'o': /* name the output module */
4093 m_name = bb_xstrdup(optarg);
4095 case 'L': /* Stub warning */
4096 /* This is needed for compatibility with modprobe.
4097 * In theory, this does locking, but we don't do
4098 * that. So be careful and plan your life around not
4099 * loading the same module 50 times concurrently. */
4101 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4102 case 'm': /* print module load map */
4103 flag_print_load_map = 1;
4111 if (argv[optind] == NULL) {
4115 /* Grab the module name */
4116 tmp1 = bb_xstrdup(argv[optind]);
4117 tmp = basename(tmp1);
4120 if (uname(&myuname) == 0) {
4121 if (myuname.release[0] == '2') {
4122 k_version = myuname.release[2] - '0';
4126 #if defined(CONFIG_FEATURE_2_6_MODULES)
4127 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
4128 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
4134 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4140 #if defined(CONFIG_FEATURE_2_6_MODULES)
4142 bb_xasprintf(&m_fullName, "%s.ko", tmp);
4145 bb_xasprintf(&m_fullName, "%s.o", tmp);
4151 tmp1 = 0; /* flag for free(m_name) before exit() */
4154 /* Get a filedesc for the module. Check we we have a complete path */
4155 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
4156 (fp = fopen(argv[optind], "r")) == NULL) {
4157 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4158 * but do not error out yet if we fail to find it... */
4159 if (k_version) { /* uname succeedd */
4162 char real_module_dir[FILENAME_MAX];
4164 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4165 /* Jump through hoops in case /lib/modules/`uname -r`
4166 * is a symlink. We do not want recursive_action to
4167 * follow symlinks, but we do want to follow the
4168 * /lib/modules/`uname -r` dir, So resolve it ourselves
4169 * if it is a link... */
4170 if (realpath (tmdn, real_module_dir) == NULL)
4173 module_dir = real_module_dir;
4174 recursive_action(module_dir, TRUE, FALSE, FALSE,
4175 check_module_name_match, 0, m_fullName);
4179 /* Check if we have found anything yet */
4180 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4182 char module_dir[FILENAME_MAX];
4186 if (realpath (_PATH_MODULES, module_dir) == NULL)
4187 strcpy(module_dir, _PATH_MODULES);
4188 /* No module found under /lib/modules/`uname -r`, this
4189 * time cast the net a bit wider. Search /lib/modules/ */
4190 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4191 check_module_name_match, 0, m_fullName))
4194 || ((fp = fopen(m_filename, "r")) == NULL))
4196 bb_error_msg("%s: no module by that name found", m_fullName);
4200 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4203 m_filename = bb_xstrdup(argv[optind]);
4206 printf("Using %s\n", m_filename);
4208 #ifdef CONFIG_FEATURE_2_6_MODULES
4212 argv[optind + 1] = m_filename;
4213 return insmod_ng_main(argc - optind, argv + optind);
4217 if ((f = obj_load(fp, LOADBITS)) == NULL)
4218 bb_perror_msg_and_die("Could not load the module");
4220 if (get_modinfo_value(f, "kernel_version") == NULL)
4225 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4226 /* Version correspondence? */
4228 if (uname(&uts_info) < 0)
4229 uts_info.release[0] = '\0';
4230 if (m_has_modinfo) {
4231 m_version = new_get_module_version(f, m_strversion);
4233 m_version = old_get_module_version(f, m_strversion);
4234 if (m_version == -1) {
4235 bb_error_msg("couldn't find the kernel version the module was "
4241 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4242 if (flag_force_load) {
4243 bb_error_msg("Warning: kernel-module version mismatch\n"
4244 "\t%s was compiled for kernel version %s\n"
4245 "\twhile this kernel is version %s",
4246 m_filename, m_strversion, uts_info.release);
4248 bb_error_msg("kernel-module version mismatch\n"
4249 "\t%s was compiled for kernel version %s\n"
4250 "\twhile this kernel is version %s.",
4251 m_filename, m_strversion, uts_info.release);
4257 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4259 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
4261 if (k_new_syscalls) {
4262 #ifdef CONFIG_FEATURE_2_4_MODULES
4263 if (!new_get_kernel_symbols())
4265 k_crcs = new_is_kernel_checksummed();
4267 bb_error_msg("Not configured to support new kernels");
4271 #ifdef CONFIG_FEATURE_2_2_MODULES
4272 if (!old_get_kernel_symbols(m_name))
4274 k_crcs = old_is_kernel_checksummed();
4276 bb_error_msg("Not configured to support old kernels");
4281 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4283 m_crcs = new_is_module_checksummed(f);
4285 m_crcs = old_is_module_checksummed(f);
4287 if (m_crcs != k_crcs)
4288 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4289 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4291 /* Let the module know about the kernel symbols. */
4292 add_kernel_symbols(f);
4294 /* Allocate common symbols, symbol tables, and string tables. */
4297 ? !new_create_this_module(f, m_name)
4298 : !old_create_mod_use_count(f))
4303 if (!obj_check_undefineds(f)) {
4306 obj_allocate_commons(f);
4307 check_tainted_module(f, m_name);
4309 /* done with the module name, on to the optional var=value arguments */
4312 if (optind < argc) {
4314 ? !new_process_module_arguments(f, argc - optind, argv + optind)
4315 : !old_process_module_arguments(f, argc - optind, argv + optind))
4322 hide_special_symbols(f);
4324 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4325 add_ksymoops_symbols(f, m_filename, m_name);
4326 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4329 new_create_module_ksymtab(f);
4331 /* Find current size of the module */
4332 m_size = obj_load_size(f);
4335 m_addr = create_module(m_name, m_size);
4336 if (m_addr == -1) switch (errno) {
4338 bb_error_msg("A module named %s already exists", m_name);
4341 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4345 bb_perror_msg("create_module: %s", m_name);
4351 * the PROGBITS section was not loaded by the obj_load
4352 * now we can load them directly into the kernel memory
4354 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4355 delete_module(m_name);
4360 if (!obj_relocate(f, m_addr)) {
4361 delete_module(m_name);
4366 ? !new_init_module(m_name, f, m_size)
4367 : !old_init_module(m_name, f, m_size))
4369 delete_module(m_name);
4373 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4374 if(flag_print_load_map)
4378 exit_status = EXIT_SUCCESS;
4381 #ifdef CONFIG_FEATURE_CLEAN_UP
4391 return(exit_status);
4398 #ifdef CONFIG_FEATURE_2_6_MODULES
4400 #include <sys/mman.h>
4401 #include <asm/unistd.h>
4402 #include <sys/syscall.h>
4404 /* We use error numbers in a loose translation... */
4405 static const char *moderror(int err)
4409 return "Invalid module format";
4411 return "Unknown symbol in module";
4413 return "Module has wrong symbol version";
4415 return "Invalid parameters";
4417 return strerror(err);
4421 extern int insmod_ng_main( int argc, char **argv)
4429 char *filename, *options = bb_xstrdup("");
4437 /* Rest is options */
4438 for (i = 2; i < argc; i++) {
4439 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4440 /* Spaces handled by "" pairs, but no way of escaping quotes */
4441 if (strchr(argv[i], ' ')) {
4442 strcat(options, "\"");
4443 strcat(options, argv[i]);
4444 strcat(options, "\"");
4446 strcat(options, argv[i]);
4448 strcat(options, " ");
4451 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4452 bb_perror_msg_and_die("cannot open module `%s'", filename);
4457 map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
4458 if (map == MAP_FAILED) {
4459 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4462 ret = syscall(__NR_init_module, map, len, options);
4464 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4465 filename, moderror(errno), ret);