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
164 #if defined(__mips__)
165 /* Account for ELF spec changes. */
166 #ifndef EM_MIPS_RS3_LE
167 #ifdef EM_MIPS_RS4_BE
168 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
170 #define EM_MIPS_RS3_LE 10
172 #endif /* !EM_MIPS_RS3_LE */
174 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
175 #define SHT_RELM SHT_REL
176 #define Elf32_RelM Elf32_Rel
177 #define ELFCLASSM ELFCLASS32
178 #define ARCHDATAM "__dbe_table"
181 #if defined(__powerpc__)
182 #define CONFIG_USE_PLT_ENTRIES
183 #define CONFIG_PLT_ENTRY_SIZE 16
184 #define CONFIG_USE_PLT_LIST
185 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
186 #define CONFIG_USE_LIST
188 #define MATCH_MACHINE(x) (x == EM_PPC)
189 #define SHT_RELM SHT_RELA
190 #define Elf32_RelM Elf32_Rela
191 #define ELFCLASSM ELFCLASS32
192 #define ARCHDATAM "__ftr_fixup"
196 #define CONFIG_USE_GOT_ENTRIES
197 #define CONFIG_GOT_ENTRY_SIZE 4
198 #define CONFIG_USE_SINGLE
200 #define MATCH_MACHINE(x) (x == EM_SH)
201 #define SHT_RELM SHT_RELA
202 #define Elf32_RelM Elf32_Rela
203 #define ELFCLASSM ELFCLASS32
205 /* the SH changes have only been tested in =little endian= mode */
206 /* I'm not sure about big endian, so let's warn: */
208 #if defined(__sh__) && defined(__BIG_ENDIAN__)
209 #error insmod.c may require changes for use on big endian SH
212 /* it may or may not work on the SH1/SH2... So let's error on those
214 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && \
216 #error insmod.c may require changes for SH1 or SH2 use
220 #if defined (__v850e__)
221 #define CONFIG_USE_PLT_ENTRIES
222 #define CONFIG_PLT_ENTRY_SIZE 8
223 #define CONFIG_USE_SINGLE
225 #ifndef EM_CYGNUS_V850 /* grumble */
226 #define EM_CYGNUS_V850 0x9080
229 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
230 #define SHT_RELM SHT_RELA
231 #define Elf32_RelM Elf32_Rela
232 #define ELFCLASSM ELFCLASS32
234 #define SYMBOL_PREFIX "_"
237 #if defined(__cris__)
240 #define R_CRIS_NONE 0
244 #define MATCH_MACHINE(x) (x == EM_CRIS)
245 #define SHT_RELM SHT_RELA
246 #define Elf32_RelM Elf32_Rela
247 #define ELFCLASSM ELFCLASS32
251 #error Sorry, but insmod.c does not yet support this architecture...
255 //----------------------------------------------------------------------------
256 //--------modutils module.h, lines 45-242
257 //----------------------------------------------------------------------------
259 /* Definitions for the Linux module syscall interface.
260 Copyright 1996, 1997 Linux International.
262 Contributed by Richard Henderson <rth@tamu.edu>
264 This file is part of the Linux modutils.
266 This program is free software; you can redistribute it and/or modify it
267 under the terms of the GNU General Public License as published by the
268 Free Software Foundation; either version 2 of the License, or (at your
269 option) any later version.
271 This program is distributed in the hope that it will be useful, but
272 WITHOUT ANY WARRANTY; without even the implied warranty of
273 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
274 General Public License for more details.
276 You should have received a copy of the GNU General Public License
277 along with this program; if not, write to the Free Software Foundation,
278 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
281 #ifndef MODUTILS_MODULE_H
282 static const int MODUTILS_MODULE_H = 1;
284 #ident "$Id: insmod.c,v 1.114 2004/03/19 12:16:18 andersen Exp $"
286 /* This file contains the structures used by the 2.0 and 2.1 kernels.
287 We do not use the kernel headers directly because we do not wish
288 to be dependant on a particular kernel version to compile insmod. */
291 /*======================================================================*/
292 /* The structures used by Linux 2.0. */
294 /* The symbol format used by get_kernel_syms(2). */
295 struct old_kernel_sym
301 struct old_module_ref
303 unsigned long module; /* kernel addresses */
307 struct old_module_symbol
313 struct old_symbol_table
315 int size; /* total, including string table!!! */
318 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
319 struct old_module_ref ref[0]; /* actual size defined by n_refs */
322 struct old_mod_routines
325 unsigned long cleanup;
331 unsigned long ref; /* the list of modules that refer to me */
332 unsigned long symtab;
334 int size; /* size of module in pages */
335 unsigned long addr; /* address of module */
337 unsigned long cleanup; /* cleanup routine */
340 /* Sent to init_module(2) or'ed into the code size parameter. */
341 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
343 int get_kernel_syms(struct old_kernel_sym *);
344 int old_sys_init_module(const char *name, char *code, unsigned codesize,
345 struct old_mod_routines *, struct old_symbol_table *);
347 /*======================================================================*/
348 /* For sizeof() which are related to the module platform and not to the
349 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
351 #define tgt_sizeof_char sizeof(char)
352 #define tgt_sizeof_short sizeof(short)
353 #define tgt_sizeof_int sizeof(int)
354 #define tgt_sizeof_long sizeof(long)
355 #define tgt_sizeof_char_p sizeof(char *)
356 #define tgt_sizeof_void_p sizeof(void *)
357 #define tgt_long long
359 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
360 #undef tgt_sizeof_long
361 #undef tgt_sizeof_char_p
362 #undef tgt_sizeof_void_p
364 static const int tgt_sizeof_long = 8;
365 static const int tgt_sizeof_char_p = 8;
366 static const int tgt_sizeof_void_p = 8;
367 #define tgt_long long long
370 /*======================================================================*/
371 /* The structures used in Linux 2.1. */
373 /* Note: new_module_symbol does not use tgt_long intentionally */
374 struct new_module_symbol
380 struct new_module_persist;
382 struct new_module_ref
384 unsigned tgt_long dep; /* kernel addresses */
385 unsigned tgt_long ref;
386 unsigned tgt_long next_ref;
391 unsigned tgt_long size_of_struct; /* == sizeof(module) */
392 unsigned tgt_long next;
393 unsigned tgt_long name;
394 unsigned tgt_long size;
397 unsigned tgt_long flags; /* AUTOCLEAN et al */
402 unsigned tgt_long syms;
403 unsigned tgt_long deps;
404 unsigned tgt_long refs;
405 unsigned tgt_long init;
406 unsigned tgt_long cleanup;
407 unsigned tgt_long ex_table_start;
408 unsigned tgt_long ex_table_end;
410 unsigned tgt_long gp;
412 /* Everything after here is extension. */
413 unsigned tgt_long persist_start;
414 unsigned tgt_long persist_end;
415 unsigned tgt_long can_unload;
416 unsigned tgt_long runsize;
417 #ifdef CONFIG_FEATURE_2_4_MODULES
418 const char *kallsyms_start; /* All symbols for kernel debugging */
419 const char *kallsyms_end;
420 const char *archdata_start; /* arch specific data for module */
421 const char *archdata_end;
422 const char *kernel_data; /* Reserved for kernel internal use */
427 #define ARCHDATA_SEC_NAME ARCHDATAM
429 #define ARCHDATA_SEC_NAME "__archdata"
431 #define KALLSYMS_SEC_NAME "__kallsyms"
434 struct new_module_info
442 /* Bits of module.flags. */
443 static const int NEW_MOD_RUNNING = 1;
444 static const int NEW_MOD_DELETED = 2;
445 static const int NEW_MOD_AUTOCLEAN = 4;
446 static const int NEW_MOD_VISITED = 8;
447 static const int NEW_MOD_USED_ONCE = 16;
449 int new_sys_init_module(const char *name, const struct new_module *);
450 int query_module(const char *name, int which, void *buf, size_t bufsize,
453 /* Values for query_module's which. */
455 static const int QM_MODULES = 1;
456 static const int QM_DEPS = 2;
457 static const int QM_REFS = 3;
458 static const int QM_SYMBOLS = 4;
459 static const int QM_INFO = 5;
461 /*======================================================================*/
462 /* The system calls unchanged between 2.0 and 2.1. */
464 unsigned long create_module(const char *, size_t);
465 int delete_module(const char *);
468 #endif /* module.h */
470 //----------------------------------------------------------------------------
471 //--------end of modutils module.h
472 //----------------------------------------------------------------------------
476 //----------------------------------------------------------------------------
477 //--------modutils obj.h, lines 253-462
478 //----------------------------------------------------------------------------
480 /* Elf object file loading and relocation routines.
481 Copyright 1996, 1997 Linux International.
483 Contributed by Richard Henderson <rth@tamu.edu>
485 This file is part of the Linux modutils.
487 This program is free software; you can redistribute it and/or modify it
488 under the terms of the GNU General Public License as published by the
489 Free Software Foundation; either version 2 of the License, or (at your
490 option) any later version.
492 This program is distributed in the hope that it will be useful, but
493 WITHOUT ANY WARRANTY; without even the implied warranty of
494 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
495 General Public License for more details.
497 You should have received a copy of the GNU General Public License
498 along with this program; if not, write to the Free Software Foundation,
499 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
502 #ifndef MODUTILS_OBJ_H
503 static const int MODUTILS_OBJ_H = 1;
505 #ident "$Id: insmod.c,v 1.114 2004/03/19 12:16:18 andersen Exp $"
507 /* The relocatable object is manipulated using elfin types. */
513 #if __BYTE_ORDER == __LITTLE_ENDIAN
514 #define ELFDATAM ELFDATA2LSB
515 #elif __BYTE_ORDER == __BIG_ENDIAN
516 #define ELFDATAM ELFDATA2MSB
520 # if ELFCLASSM == ELFCLASS32
521 # define ElfW(x) Elf32_ ## x
522 # define ELFW(x) ELF32_ ## x
524 # define ElfW(x) Elf64_ ## x
525 # define ELFW(x) ELF64_ ## x
529 /* For some reason this is missing from some ancient C libraries.... */
530 #ifndef ELF32_ST_INFO
531 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
534 #ifndef ELF64_ST_INFO
535 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
538 struct obj_string_patch;
539 struct obj_symbol_patch;
546 struct obj_section *load_next;
552 struct obj_symbol *next; /* hash table link */
556 int secidx; /* the defining section index/module */
558 int ksymidx; /* for export to the kernel symtab */
559 int referenced; /* actually used in the link */
562 /* Hardcode the hash table size. We shouldn't be needing so many
563 symbols that we begin to degrade performance, and we get a big win
564 by giving the compiler a constant divisor. */
566 #define HASH_BUCKETS 521
572 struct obj_section **sections;
573 struct obj_section *load_order;
574 struct obj_section **load_order_search_start;
575 struct obj_string_patch *string_patches;
576 struct obj_symbol_patch *symbol_patches;
577 int (*symbol_cmp)(const char *, const char *);
578 unsigned long (*symbol_hash)(const char *);
579 unsigned long local_symtab_size;
580 struct obj_symbol **local_symtab;
581 struct obj_symbol *symtab[HASH_BUCKETS];
592 struct obj_string_patch
594 struct obj_string_patch *next;
596 ElfW(Addr) reloc_offset;
597 ElfW(Addr) string_offset;
600 struct obj_symbol_patch
602 struct obj_symbol_patch *next;
604 ElfW(Addr) reloc_offset;
605 struct obj_symbol *sym;
609 /* Generic object manipulation routines. */
611 static unsigned long obj_elf_hash(const char *);
613 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
615 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
618 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
619 struct obj_symbol *sym);
621 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
622 static void obj_set_symbol_compare(struct obj_file *f,
623 int (*cmp)(const char *, const char *),
624 unsigned long (*hash)(const char *));
627 static struct obj_section *obj_find_section (struct obj_file *f,
630 static void obj_insert_section_load_order (struct obj_file *f,
631 struct obj_section *sec);
633 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
638 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
643 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
645 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
648 #ifdef CONFIG_FEATURE_2_4_MODULES
649 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
650 struct obj_symbol *sym);
653 static int obj_check_undefineds(struct obj_file *f);
655 static void obj_allocate_commons(struct obj_file *f);
657 static unsigned long obj_load_size (struct obj_file *f);
659 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
661 static struct obj_file *obj_load(FILE *f, int loadprogbits);
663 static int obj_create_image (struct obj_file *f, char *image);
665 /* Architecture specific manipulation routines. */
667 static struct obj_file *arch_new_file (void);
669 static struct obj_section *arch_new_section (void);
671 static struct obj_symbol *arch_new_symbol (void);
673 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
674 struct obj_section *targsec,
675 struct obj_section *symsec,
676 struct obj_symbol *sym,
677 ElfW(RelM) *rel, ElfW(Addr) value);
679 static void arch_create_got (struct obj_file *f);
681 static int obj_gpl_license(struct obj_file *f, const char **license);
683 #ifdef CONFIG_FEATURE_2_4_MODULES
684 static int arch_init_module (struct obj_file *f, struct new_module *);
688 //----------------------------------------------------------------------------
689 //--------end of modutils obj.h
690 //----------------------------------------------------------------------------
693 /* SPFX is always a string, so it can be concatenated to string constants. */
695 #define SPFX SYMBOL_PREFIX
701 #define _PATH_MODULES "/lib/modules"
702 static const int STRVERSIONLEN = 32;
704 /*======================================================================*/
706 static int flag_force_load = 0;
707 static int flag_autoclean = 0;
708 static int flag_verbose = 0;
709 static int flag_quiet = 0;
710 static int flag_export = 1;
713 /*======================================================================*/
715 #if defined(CONFIG_USE_LIST)
717 struct arch_list_entry
719 struct arch_list_entry *next;
720 CONFIG_LIST_ARCHTYPE addend;
727 #if defined(CONFIG_USE_SINGLE)
729 struct arch_single_entry
738 #if defined(__mips__)
741 struct mips_hi16 *next;
748 struct obj_file root;
749 #if defined(CONFIG_USE_PLT_ENTRIES)
750 struct obj_section *plt;
752 #if defined(CONFIG_USE_GOT_ENTRIES)
753 struct obj_section *got;
755 #if defined(__mips__)
756 struct mips_hi16 *mips_hi16_list;
761 struct obj_symbol root;
762 #if defined(CONFIG_USE_PLT_ENTRIES)
763 #if defined(CONFIG_USE_PLT_LIST)
764 struct arch_list_entry *pltent;
766 struct arch_single_entry pltent;
769 #if defined(CONFIG_USE_GOT_ENTRIES)
770 struct arch_single_entry gotent;
775 struct external_module {
780 struct new_module_symbol *syms;
783 static struct new_module_symbol *ksyms;
784 static size_t nksyms;
786 static struct external_module *ext_modules;
787 static int n_ext_modules;
788 static int n_ext_modules_used;
789 extern int delete_module(const char *);
791 static char *m_filename;
792 static char *m_fullName;
796 /*======================================================================*/
799 static int check_module_name_match(const char *filename, struct stat *statbuf,
802 char *fullname = (char *) userdata;
804 if (fullname[0] == '\0')
807 char *tmp, *tmp1 = bb_xstrdup(filename);
808 tmp = bb_get_last_path_component(tmp1);
809 if (strcmp(tmp, fullname) == 0) {
811 /* Stop searching if we find a match */
812 m_filename = bb_xstrdup(filename);
821 /*======================================================================*/
823 static struct obj_file *arch_new_file(void)
826 f = xmalloc(sizeof(*f));
828 memset(f, 0, sizeof(*f));
833 static struct obj_section *arch_new_section(void)
835 return xmalloc(sizeof(struct obj_section));
838 static struct obj_symbol *arch_new_symbol(void)
840 struct arch_symbol *sym;
841 sym = xmalloc(sizeof(*sym));
843 memset(sym, 0, sizeof(*sym));
848 static enum obj_reloc
849 arch_apply_relocation(struct obj_file *f,
850 struct obj_section *targsec,
851 struct obj_section *symsec,
852 struct obj_symbol *sym,
853 ElfW(RelM) *rel, ElfW(Addr) v)
855 struct arch_file *ifile = (struct arch_file *) f;
856 enum obj_reloc ret = obj_reloc_ok;
857 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
858 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
859 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
860 struct arch_symbol *isym = (struct arch_symbol *) sym;
862 #if defined(CONFIG_USE_GOT_ENTRIES)
863 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
865 #if defined(CONFIG_USE_PLT_ENTRIES)
866 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
868 #if defined(CONFIG_USE_PLT_LIST)
869 struct arch_list_entry *pe;
871 struct arch_single_entry *pe;
875 switch (ELF32_R_TYPE(rel->r_info)) {
890 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
891 * (which is .got) similar to branch,
892 * but is full 32 bits relative */
902 case R_ARM_GOTOFF: /* address relative to the got */
907 #elif defined(__s390__)
909 *(unsigned int *) loc += v;
912 *(unsigned short *) loc += v;
915 *(unsigned char *) loc += v;
919 *(unsigned int *) loc += v - dot;
922 *(unsigned short *) loc += (v - dot) >> 1;
925 *(unsigned short *) loc += v - dot;
930 /* find the plt entry and initialize it. */
931 assert(isym != NULL);
932 pe = (struct arch_single_entry *) &isym->pltent;
933 assert(pe->allocated);
934 if (pe->inited == 0) {
935 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
936 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
938 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
945 /* Insert relative distance to target. */
946 v = plt + pe->offset - dot;
947 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
948 *(unsigned int *) loc = (unsigned int) v;
949 else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
950 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
964 *(unsigned long *) loc += got - dot;
970 assert(isym != NULL);
972 if (!isym->gotent.inited)
974 isym->gotent.inited = 1;
975 *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
977 if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
978 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
979 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
980 *(unsigned short *) loc += isym->gotent.offset;
981 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
982 *(unsigned int *) loc += isym->gotent.offset;
985 #ifndef R_390_GOTOFF32
986 #define R_390_GOTOFF32 R_390_GOTOFF
993 #elif defined(__i386__)
1007 case R_386_GLOB_DAT:
1008 case R_386_JMP_SLOT:
1012 case R_386_RELATIVE:
1013 *loc += f->baseaddr;
1029 #elif defined(__mc68000__)
1040 ret = obj_reloc_overflow;
1047 ret = obj_reloc_overflow;
1054 if ((Elf32_Sword)v > 0x7f ||
1055 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
1056 ret = obj_reloc_overflow;
1063 if ((Elf32_Sword)v > 0x7fff ||
1064 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1065 ret = obj_reloc_overflow;
1071 *(int *)loc = v - dot;
1074 case R_68K_GLOB_DAT:
1075 case R_68K_JMP_SLOT:
1079 case R_68K_RELATIVE:
1080 *(int *)loc += f->baseaddr;
1093 #elif defined(__mips__)
1104 ret = obj_reloc_dangerous;
1105 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1106 ret = obj_reloc_overflow;
1108 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1114 struct mips_hi16 *n;
1116 /* We cannot relocate this one now because we don't know the value
1117 of the carry we need to add. Save the information, and let LO16
1118 do the actual relocation. */
1119 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1122 n->next = ifile->mips_hi16_list;
1123 ifile->mips_hi16_list = n;
1129 unsigned long insnlo = *loc;
1130 Elf32_Addr val, vallo;
1132 /* Sign extend the addend we extract from the lo insn. */
1133 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1135 if (ifile->mips_hi16_list != NULL) {
1136 struct mips_hi16 *l;
1138 l = ifile->mips_hi16_list;
1140 struct mips_hi16 *next;
1143 /* The value for the HI16 had best be the same. */
1144 assert(v == l->value);
1146 /* Do the HI16 relocation. Note that we actually don't
1147 need to know anything about the LO16 itself, except where
1148 to find the low 16 bits of the addend needed by the LO16. */
1151 ((insn & 0xffff) << 16) +
1155 /* Account for the sign extension that will happen in the
1162 insn = (insn & ~0xffff) | val;
1170 ifile->mips_hi16_list = NULL;
1173 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1175 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1180 #elif defined(__powerpc__)
1182 case R_PPC_ADDR16_HA:
1183 *(unsigned short *)loc = (v + 0x8000) >> 16;
1186 case R_PPC_ADDR16_HI:
1187 *(unsigned short *)loc = v >> 16;
1190 case R_PPC_ADDR16_LO:
1191 *(unsigned short *)loc = v;
1205 #elif defined(__sh__)
1228 *loc = f->baseaddr + rel->r_addend;
1233 *loc = got - dot + rel->r_addend;
1244 #if defined(__SH5__)
1245 case R_SH_IMM_MEDLOW16:
1246 case R_SH_IMM_LOW16:
1250 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1254 * movi and shori have the format:
1256 * | op | imm | reg | reserved |
1257 * 31..26 25..10 9.. 4 3 .. 0
1259 * so we simply mask and or in imm.
1261 word = *loc & ~0x3fffc00;
1262 word |= (v & 0xffff) << 10;
1269 case R_SH_IMM_MEDLOW16_PCREL:
1270 case R_SH_IMM_LOW16_PCREL:
1274 word = *loc & ~0x3fffc00;
1278 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1281 word |= (v & 0xffff) << 10;
1287 #endif /* __SH5__ */
1291 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1292 ret = obj_reloc_unhandled;
1295 #if defined (__v850e__)
1300 /* We write two shorts instead of a long because even
1301 32-bit insns only need half-word alignment, but
1302 32-bit data needs to be long-word aligned. */
1303 v += ((unsigned short *)loc)[0];
1304 v += ((unsigned short *)loc)[1] << 16;
1305 ((unsigned short *)loc)[0] = v & 0xffff;
1306 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1309 case R_V850_22_PCREL:
1313 #if defined (__cris__)
1318 /* CRIS keeps the relocation value in the r_addend field and
1319 * should not use whats in *loc at all
1325 #if defined(CONFIG_USE_PLT_ENTRIES)
1329 /* find the plt entry and initialize it if necessary */
1330 assert(isym != NULL);
1332 #if defined(CONFIG_USE_PLT_LIST)
1333 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1341 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1343 /* generate some machine code */
1345 #if defined(__arm__)
1346 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1347 ip[1] = v; /* sym@ */
1349 #if defined(__powerpc__)
1350 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1351 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1352 ip[2] = 0x7d6903a6; /* mtctr r11 */
1353 ip[3] = 0x4e800420; /* bctr */
1355 #if defined (__v850e__)
1356 /* We have to trash a register, so we assume that any control
1357 transfer more than 21-bits away must be a function call
1358 (so we can use a call-clobbered register). */
1359 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1360 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1365 /* relative distance to target */
1367 /* if the target is too far away.... */
1368 #if defined (__arm__) || defined (__powerpc__)
1369 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1370 #elif defined (__v850e__)
1371 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1373 /* go via the plt */
1374 v = plt + pe->offset - dot;
1376 #if defined (__v850e__)
1381 ret = obj_reloc_dangerous;
1383 /* merge the offset into the instruction. */
1384 #if defined(__arm__)
1385 /* Convert to words. */
1388 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1390 #if defined(__powerpc__)
1391 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1393 #if defined (__v850e__)
1394 /* We write two shorts instead of a long because even 32-bit insns
1395 only need half-word alignment, but the 32-bit data write needs
1396 to be long-word aligned. */
1397 ((unsigned short *)loc)[0] =
1398 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1399 | ((v >> 16) & 0x3f); /* offs high part */
1400 ((unsigned short *)loc)[1] =
1401 (v & 0xffff); /* offs low part */
1404 #endif /* CONFIG_USE_PLT_ENTRIES */
1406 #if defined(CONFIG_USE_GOT_ENTRIES)
1409 assert(isym != NULL);
1410 /* needs an entry in the .got: set it, once */
1411 if (!isym->gotent.inited) {
1412 isym->gotent.inited = 1;
1413 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1415 /* make the reloc with_respect_to_.got */
1417 *loc += isym->gotent.offset + rel->r_addend;
1418 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1419 *loc += isym->gotent.offset;
1423 #endif /* CONFIG_USE_GOT_ENTRIES */
1430 #if defined(CONFIG_USE_LIST)
1432 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1433 int offset, int size)
1435 struct arch_list_entry *pe;
1437 for (pe = *list; pe != NULL; pe = pe->next) {
1438 if (pe->addend == rel->r_addend) {
1444 pe = xmalloc(sizeof(struct arch_list_entry));
1446 pe->addend = rel->r_addend;
1447 pe->offset = offset;
1457 #if defined(CONFIG_USE_SINGLE)
1459 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1460 int offset, int size)
1462 if (single->allocated == 0) {
1463 single->allocated = 1;
1464 single->offset = offset;
1473 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1475 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1476 int offset, int size)
1478 struct obj_section *myrelsec = obj_find_section(f, name);
1485 obj_extend_section(myrelsec, offset);
1487 myrelsec = obj_create_alloced_section(f, name,
1497 static void arch_create_got(struct obj_file *f)
1499 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1500 struct arch_file *ifile = (struct arch_file *) f;
1502 #if defined(CONFIG_USE_GOT_ENTRIES)
1503 int got_offset = 0, got_needed = 0, got_allocate;
1505 #if defined(CONFIG_USE_PLT_ENTRIES)
1506 int plt_offset = 0, plt_needed = 0, plt_allocate;
1508 struct obj_section *relsec, *symsec, *strsec;
1509 ElfW(RelM) *rel, *relend;
1510 ElfW(Sym) *symtab, *extsym;
1511 const char *strtab, *name;
1512 struct arch_symbol *intsym;
1514 for (i = 0; i < f->header.e_shnum; ++i) {
1515 relsec = f->sections[i];
1516 if (relsec->header.sh_type != SHT_RELM)
1519 symsec = f->sections[relsec->header.sh_link];
1520 strsec = f->sections[symsec->header.sh_link];
1522 rel = (ElfW(RelM) *) relsec->contents;
1523 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1524 symtab = (ElfW(Sym) *) symsec->contents;
1525 strtab = (const char *) strsec->contents;
1527 for (; rel < relend; ++rel) {
1528 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1530 #if defined(CONFIG_USE_GOT_ENTRIES)
1533 #if defined(CONFIG_USE_PLT_ENTRIES)
1537 switch (ELF32_R_TYPE(rel->r_info)) {
1538 #if defined(__arm__)
1553 #elif defined(__i386__)
1563 #elif defined(__powerpc__)
1568 #elif defined(__mc68000__)
1579 #elif defined(__sh__)
1589 #elif defined (__v850e__)
1590 case R_V850_22_PCREL:
1599 if (extsym->st_name != 0) {
1600 name = strtab + extsym->st_name;
1602 name = f->sections[extsym->st_shndx]->name;
1604 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1605 #if defined(CONFIG_USE_GOT_ENTRIES)
1607 got_offset += arch_single_init(
1608 rel, &intsym->gotent,
1609 got_offset, CONFIG_GOT_ENTRY_SIZE);
1614 #if defined(CONFIG_USE_PLT_ENTRIES)
1616 #if defined(CONFIG_USE_PLT_LIST)
1617 plt_offset += arch_list_add(
1618 rel, &intsym->pltent,
1619 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1621 plt_offset += arch_single_init(
1622 rel, &intsym->pltent,
1623 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1631 #if defined(CONFIG_USE_GOT_ENTRIES)
1633 ifile->got = arch_xsect_init(f, ".got", got_offset,
1634 CONFIG_GOT_ENTRY_SIZE);
1638 #if defined(CONFIG_USE_PLT_ENTRIES)
1640 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1641 CONFIG_PLT_ENTRY_SIZE);
1645 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1648 #ifdef CONFIG_FEATURE_2_4_MODULES
1649 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1655 /*======================================================================*/
1657 /* Standard ELF hash function. */
1658 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1660 unsigned long h = 0;
1667 if ((g = (h & 0xf0000000)) != 0) {
1676 static unsigned long obj_elf_hash(const char *name)
1678 return obj_elf_hash_n(name, strlen(name));
1681 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1682 /* String comparison for non-co-versioned kernel and module. */
1684 static int ncv_strcmp(const char *a, const char *b)
1686 size_t alen = strlen(a), blen = strlen(b);
1688 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1689 return strncmp(a, b, alen);
1690 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1691 return strncmp(a, b, blen);
1693 return strcmp(a, b);
1696 /* String hashing for non-co-versioned kernel and module. Here
1697 we are simply forced to drop the crc from the hash. */
1699 static unsigned long ncv_symbol_hash(const char *str)
1701 size_t len = strlen(str);
1702 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1704 return obj_elf_hash_n(str, len);
1708 obj_set_symbol_compare(struct obj_file *f,
1709 int (*cmp) (const char *, const char *),
1710 unsigned long (*hash) (const char *))
1713 f->symbol_cmp = cmp;
1715 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1718 f->symbol_hash = hash;
1720 memcpy(tmptab, f->symtab, sizeof(tmptab));
1721 memset(f->symtab, 0, sizeof(f->symtab));
1723 for (i = 0; i < HASH_BUCKETS; ++i)
1724 for (sym = tmptab[i]; sym; sym = next) {
1725 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1727 sym->next = f->symtab[h];
1733 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1735 static struct obj_symbol *
1736 obj_add_symbol(struct obj_file *f, const char *name,
1737 unsigned long symidx, int info,
1738 int secidx, ElfW(Addr) value,
1741 struct obj_symbol *sym;
1742 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1743 int n_type = ELFW(ST_TYPE) (info);
1744 int n_binding = ELFW(ST_BIND) (info);
1746 for (sym = f->symtab[hash]; sym; sym = sym->next)
1747 if (f->symbol_cmp(sym->name, name) == 0) {
1748 int o_secidx = sym->secidx;
1749 int o_info = sym->info;
1750 int o_type = ELFW(ST_TYPE) (o_info);
1751 int o_binding = ELFW(ST_BIND) (o_info);
1753 /* A redefinition! Is it legal? */
1755 if (secidx == SHN_UNDEF)
1757 else if (o_secidx == SHN_UNDEF)
1759 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1760 /* Cope with local and global symbols of the same name
1761 in the same object file, as might have been created
1762 by ld -r. The only reason locals are now seen at this
1763 level at all is so that we can do semi-sensible things
1766 struct obj_symbol *nsym, **p;
1768 nsym = arch_new_symbol();
1769 nsym->next = sym->next;
1772 /* Excise the old (local) symbol from the hash chain. */
1773 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1777 } else if (n_binding == STB_LOCAL) {
1778 /* Another symbol of the same name has already been defined.
1779 Just add this to the local table. */
1780 sym = arch_new_symbol();
1783 f->local_symtab[symidx] = sym;
1785 } else if (n_binding == STB_WEAK)
1787 else if (o_binding == STB_WEAK)
1789 /* Don't unify COMMON symbols with object types the programmer
1791 else if (secidx == SHN_COMMON
1792 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1794 else if (o_secidx == SHN_COMMON
1795 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1798 /* Don't report an error if the symbol is coming from
1799 the kernel or some external module. */
1800 if (secidx <= SHN_HIRESERVE)
1801 bb_error_msg("%s multiply defined", name);
1806 /* Completely new symbol. */
1807 sym = arch_new_symbol();
1808 sym->next = f->symtab[hash];
1809 f->symtab[hash] = sym;
1812 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1813 if (symidx >= f->local_symtab_size)
1814 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1815 name, (long) symidx, (long) f->local_symtab_size);
1817 f->local_symtab[symidx] = sym;
1824 sym->secidx = secidx;
1830 static struct obj_symbol *
1831 obj_find_symbol(struct obj_file *f, const char *name)
1833 struct obj_symbol *sym;
1834 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1836 for (sym = f->symtab[hash]; sym; sym = sym->next)
1837 if (f->symbol_cmp(sym->name, name) == 0)
1844 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1847 if (sym->secidx >= SHN_LORESERVE)
1850 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1852 /* As a special case, a NULL sym has value zero. */
1857 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1859 int i, n = f->header.e_shnum;
1861 for (i = 0; i < n; ++i)
1862 if (strcmp(f->sections[i]->name, name) == 0)
1863 return f->sections[i];
1868 static int obj_load_order_prio(struct obj_section *a)
1870 unsigned long af, ac;
1872 af = a->header.sh_flags;
1875 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1876 strcmp(a->name + 5, ".init"))
1880 if (!(af & SHF_WRITE))
1882 if (af & SHF_EXECINSTR)
1884 if (a->header.sh_type != SHT_NOBITS)
1891 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1893 struct obj_section **p;
1894 int prio = obj_load_order_prio(sec);
1895 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1896 if (obj_load_order_prio(*p) < prio)
1898 sec->load_next = *p;
1902 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1904 unsigned long align,
1907 int newidx = f->header.e_shnum++;
1908 struct obj_section *sec;
1910 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1911 f->sections[newidx] = sec = arch_new_section();
1913 memset(sec, 0, sizeof(*sec));
1914 sec->header.sh_type = SHT_PROGBITS;
1915 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1916 sec->header.sh_size = size;
1917 sec->header.sh_addralign = align;
1921 sec->contents = xmalloc(size);
1923 obj_insert_section_load_order(f, sec);
1928 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1930 unsigned long align,
1933 int newidx = f->header.e_shnum++;
1934 struct obj_section *sec;
1936 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1937 f->sections[newidx] = sec = arch_new_section();
1939 memset(sec, 0, sizeof(*sec));
1940 sec->header.sh_type = SHT_PROGBITS;
1941 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1942 sec->header.sh_size = size;
1943 sec->header.sh_addralign = align;
1947 sec->contents = xmalloc(size);
1949 sec->load_next = f->load_order;
1950 f->load_order = sec;
1951 if (f->load_order_search_start == &f->load_order)
1952 f->load_order_search_start = &sec->load_next;
1957 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1959 unsigned long oldsize = sec->header.sh_size;
1961 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1963 return sec->contents + oldsize;
1967 /* Conditionally add the symbols from the given symbol set to the
1973 int idx, struct new_module_symbol *syms, size_t nsyms)
1975 struct new_module_symbol *s;
1978 #ifdef SYMBOL_PREFIX
1980 size_t name_alloced_size = 0;
1982 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1985 gpl = obj_gpl_license(f, NULL) == 0;
1987 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1988 /* Only add symbols that are already marked external.
1989 If we override locals we may cause problems for
1990 argument initialization. We will also create a false
1991 dependency on the module. */
1992 struct obj_symbol *sym;
1995 /* GPL licensed modules can use symbols exported with
1996 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
1997 * exported names. Non-GPL modules never see any GPLONLY_
1998 * symbols so they cannot fudge it by adding the prefix on
2001 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2002 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2004 ((char *)s->name) += 8;
2009 name = (char *)s->name;
2011 #ifdef SYMBOL_PREFIX
2012 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2013 kernel exports `C names', but module object files
2014 reference `linker names'). */
2015 size_t extra = sizeof SYMBOL_PREFIX;
2016 size_t name_size = strlen (name) + extra;
2017 if (name_size > name_alloced_size) {
2018 name_alloced_size = name_size * 2;
2019 name_buf = alloca (name_alloced_size);
2021 strcpy (name_buf, SYMBOL_PREFIX);
2022 strcpy (name_buf + extra - 1, name);
2024 #endif /* SYMBOL_PREFIX */
2026 sym = obj_find_symbol(f, name);
2027 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
2028 #ifdef SYMBOL_PREFIX
2029 /* Put NAME_BUF into more permanent storage. */
2030 name = xmalloc (name_size);
2031 strcpy (name, name_buf);
2033 sym = obj_add_symbol(f, name, -1,
2034 ELFW(ST_INFO) (STB_GLOBAL,
2037 /* Did our symbol just get installed? If so, mark the
2038 module as "used". */
2039 if (sym->secidx == idx)
2047 static void add_kernel_symbols(struct obj_file *f)
2049 struct external_module *m;
2052 /* Add module symbols first. */
2054 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2056 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2057 m->nsyms)) m->used = 1, ++nused;
2059 n_ext_modules_used = nused;
2061 /* And finally the symbols from the kernel proper. */
2064 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2067 static char *get_modinfo_value(struct obj_file *f, const char *key)
2069 struct obj_section *sec;
2070 char *p, *v, *n, *ep;
2071 size_t klen = strlen(key);
2073 sec = obj_find_section(f, ".modinfo");
2077 ep = p + sec->header.sh_size;
2080 n = strchr(p, '\0');
2082 if (p + klen == v && strncmp(p, key, klen) == 0)
2085 if (p + klen == n && strcmp(p, key) == 0)
2095 /*======================================================================*/
2096 /* Functions relating to module loading in pre 2.1 kernels. */
2099 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
2103 struct obj_symbol *sym;
2107 if ((q = strchr(p, '=')) == NULL) {
2113 sym = obj_find_symbol(f, p);
2115 /* Also check that the parameter was not resolved from the kernel. */
2116 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2117 bb_error_msg("symbol for parameter %s not found", p);
2121 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
2123 /* Do C quoting if we begin with a ". */
2127 str = alloca(strlen(q));
2128 for (r = str, q++; *q != '"'; ++q, ++r) {
2130 bb_error_msg("improperly terminated string argument for %s", p);
2132 } else if (*q == '\\')
2166 if (q[1] >= '0' && q[1] <= '7') {
2167 c = (c * 8) + *++q - '0';
2168 if (q[1] >= '0' && q[1] <= '7')
2169 c = (c * 8) + *++q - '0';
2182 obj_string_patch(f, sym->secidx, sym->value, str);
2183 } else if (*q >= '0' && *q <= '9') {
2185 *loc++ = strtoul(q, &q, 0);
2186 while (*q++ == ',');
2188 char *contents = f->sections[sym->secidx]->contents;
2189 char *myloc = contents + sym->value;
2190 char *r; /* To search for commas */
2192 /* Break the string with comas */
2193 while ((r = strchr(q, ',')) != (char *) NULL) {
2195 obj_string_patch(f, sym->secidx, myloc - contents, q);
2196 myloc += sizeof(char *);
2201 obj_string_patch(f, sym->secidx, myloc - contents, q);
2210 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2211 static int old_is_module_checksummed(struct obj_file *f)
2213 return obj_find_symbol(f, "Using_Versions") != NULL;
2215 /* Get the module's kernel version in the canonical integer form. */
2218 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2220 struct obj_symbol *sym;
2224 sym = obj_find_symbol(f, "kernel_version");
2228 p = f->sections[sym->secidx]->contents + sym->value;
2229 safe_strncpy(str, p, STRVERSIONLEN);
2231 a = strtoul(p, &p, 10);
2234 b = strtoul(p + 1, &p, 10);
2237 c = strtoul(p + 1, &q, 10);
2241 return a << 16 | b << 8 | c;
2244 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2246 #ifdef CONFIG_FEATURE_2_2_MODULES
2248 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
2250 static int old_get_kernel_symbols(const char *m_name)
2252 struct old_kernel_sym *ks, *k;
2253 struct new_module_symbol *s;
2254 struct external_module *mod;
2255 int nks, nms, nmod, i;
2257 nks = get_kernel_syms(NULL);
2260 bb_perror_msg("get_kernel_syms: %s", m_name);
2262 bb_error_msg("No kernel symbols");
2266 ks = k = xmalloc(nks * sizeof(*ks));
2268 if (get_kernel_syms(ks) != nks) {
2269 perror("inconsistency with get_kernel_syms -- is someone else "
2270 "playing with modules?");
2275 /* Collect the module information. */
2280 while (k->name[0] == '#' && k->name[1]) {
2281 struct old_kernel_sym *k2;
2283 /* Find out how many symbols this module has. */
2284 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
2288 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
2289 mod[nmod].name = k->name + 1;
2290 mod[nmod].addr = k->value;
2292 mod[nmod].nsyms = nms;
2293 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2295 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
2296 s->name = (unsigned long) k->name;
2297 s->value = k->value;
2304 n_ext_modules = nmod + 1;
2306 /* Now collect the symbols for the kernel proper. */
2308 if (k->name[0] == '#')
2311 nksyms = nms = nks - (k - ks);
2312 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2314 for (i = 0; i < nms; ++i, ++s, ++k) {
2315 s->name = (unsigned long) k->name;
2316 s->value = k->value;
2322 /* Return the kernel symbol checksum version, or zero if not used. */
2324 static int old_is_kernel_checksummed(void)
2326 /* Using_Versions is the first symbol. */
2328 && strcmp((char *) ksyms[0].name,
2329 "Using_Versions") == 0) return ksyms[0].value;
2335 static int old_create_mod_use_count(struct obj_file *f)
2337 struct obj_section *sec;
2339 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2342 obj_add_symbol(f, "mod_use_count_", -1,
2343 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2350 old_init_module(const char *m_name, struct obj_file *f,
2351 unsigned long m_size)
2354 struct old_mod_routines routines;
2355 struct old_symbol_table *symtab;
2358 /* Create the symbol table */
2360 int nsyms = 0, strsize = 0, total;
2362 /* Size things first... */
2365 for (i = 0; i < HASH_BUCKETS; ++i) {
2366 struct obj_symbol *sym;
2367 for (sym = f->symtab[i]; sym; sym = sym->next)
2368 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2369 && sym->secidx <= SHN_HIRESERVE)
2371 sym->ksymidx = nsyms++;
2372 strsize += strlen(sym->name) + 1;
2377 total = (sizeof(struct old_symbol_table)
2378 + nsyms * sizeof(struct old_module_symbol)
2379 + n_ext_modules_used * sizeof(struct old_module_ref)
2381 symtab = xmalloc(total);
2382 symtab->size = total;
2383 symtab->n_symbols = nsyms;
2384 symtab->n_refs = n_ext_modules_used;
2386 if (flag_export && nsyms) {
2387 struct old_module_symbol *ksym;
2391 ksym = symtab->symbol;
2392 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2393 + n_ext_modules_used * sizeof(struct old_module_ref));
2395 for (i = 0; i < HASH_BUCKETS; ++i) {
2396 struct obj_symbol *sym;
2397 for (sym = f->symtab[i]; sym; sym = sym->next)
2398 if (sym->ksymidx >= 0) {
2399 ksym->addr = obj_symbol_final_value(f, sym);
2401 (unsigned long) str - (unsigned long) symtab;
2403 strcpy(str, sym->name);
2404 str += strlen(sym->name) + 1;
2410 if (n_ext_modules_used) {
2411 struct old_module_ref *ref;
2414 ref = (struct old_module_ref *)
2415 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2417 for (i = 0; i < n_ext_modules; ++i)
2418 if (ext_modules[i].used)
2419 ref++->module = ext_modules[i].addr;
2423 /* Fill in routines. */
2426 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2428 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2430 /* Whew! All of the initialization is complete. Collect the final
2431 module image and give it to the kernel. */
2433 image = xmalloc(m_size);
2434 obj_create_image(f, image);
2436 /* image holds the complete relocated module, accounting correctly for
2437 mod_use_count. However the old module kernel support assume that
2438 it is receiving something which does not contain mod_use_count. */
2439 ret = old_sys_init_module(m_name, image + sizeof(long),
2440 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2441 : 0), &routines, symtab);
2443 bb_perror_msg("init_module: %s", m_name);
2453 #define old_create_mod_use_count(x) TRUE
2454 #define old_init_module(x, y, z) TRUE
2456 #endif /* CONFIG_FEATURE_2_2_MODULES */
2460 /*======================================================================*/
2461 /* Functions relating to module loading after 2.1.18. */
2464 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2467 char *p, *q, *key, *sym_name;
2468 struct obj_symbol *sym;
2469 char *contents, *loc;
2473 if ((q = strchr(p, '=')) == NULL) {
2478 key = alloca(q - p + 6);
2479 memcpy(key, "parm_", 5);
2480 memcpy(key + 5, p, q - p);
2483 p = get_modinfo_value(f, key);
2486 bb_error_msg("invalid parameter %s", key);
2490 #ifdef SYMBOL_PREFIX
2491 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2492 strcpy (sym_name, SYMBOL_PREFIX);
2493 strcat (sym_name, key);
2497 sym = obj_find_symbol(f, sym_name);
2499 /* Also check that the parameter was not resolved from the kernel. */
2500 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2501 bb_error_msg("symbol for parameter %s not found", key);
2506 min = strtoul(p, &p, 10);
2508 max = strtoul(p + 1, &p, 10);
2514 contents = f->sections[sym->secidx]->contents;
2515 loc = contents + sym->value;
2519 if ((*p == 's') || (*p == 'c')) {
2522 /* Do C quoting if we begin with a ", else slurp the lot. */
2526 str = alloca(strlen(q));
2527 for (r = str, q++; *q != '"'; ++q, ++r) {
2529 bb_error_msg("improperly terminated string argument for %s",
2532 } else if (*q == '\\')
2566 if (q[1] >= '0' && q[1] <= '7') {
2567 c = (c * 8) + *++q - '0';
2568 if (q[1] >= '0' && q[1] <= '7')
2569 c = (c * 8) + *++q - '0';
2586 /* In this case, the string is not quoted. We will break
2587 it using the coma (like for ints). If the user wants to
2588 include comas in a string, he just has to quote it */
2590 /* Search the next coma */
2594 if (r != (char *) NULL) {
2595 /* Recopy the current field */
2596 str = alloca(r - q + 1);
2597 memcpy(str, q, r - q);
2599 /* I don't know if it is usefull, as the previous case
2600 doesn't null terminate the string ??? */
2603 /* Keep next fields */
2614 obj_string_patch(f, sym->secidx, loc - contents, str);
2615 loc += tgt_sizeof_char_p;
2617 /* Array of chars (in fact, matrix !) */
2618 unsigned long charssize; /* size of each member */
2620 /* Get the size of each member */
2621 /* Probably we should do that outside the loop ? */
2622 if (!isdigit(*(p + 1))) {
2623 bb_error_msg("parameter type 'c' for %s must be followed by"
2624 " the maximum size", key);
2627 charssize = strtoul(p + 1, (char **) NULL, 10);
2630 if (strlen(str) >= charssize) {
2631 bb_error_msg("string too long for %s (max %ld)", key,
2636 /* Copy to location */
2637 strcpy((char *) loc, str);
2641 long v = strtoul(q, &q, 0);
2648 loc += tgt_sizeof_short;
2652 loc += tgt_sizeof_int;
2656 loc += tgt_sizeof_long;
2660 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2675 goto retry_end_of_value;
2679 bb_error_msg("too many values for %s (max %d)", key, max);
2686 bb_error_msg("invalid argument syntax for %s", key);
2693 bb_error_msg("too few values for %s (min %d)", key, min);
2703 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2704 static int new_is_module_checksummed(struct obj_file *f)
2706 const char *p = get_modinfo_value(f, "using_checksums");
2713 /* Get the module's kernel version in the canonical integer form. */
2716 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2721 p = get_modinfo_value(f, "kernel_version");
2724 safe_strncpy(str, p, STRVERSIONLEN);
2726 a = strtoul(p, &p, 10);
2729 b = strtoul(p + 1, &p, 10);
2732 c = strtoul(p + 1, &q, 10);
2736 return a << 16 | b << 8 | c;
2739 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2742 #ifdef CONFIG_FEATURE_2_4_MODULES
2744 /* Fetch the loaded modules, and all currently exported symbols. */
2746 static int new_get_kernel_symbols(void)
2748 char *module_names, *mn;
2749 struct external_module *modules, *m;
2750 struct new_module_symbol *syms, *s;
2751 size_t ret, bufsize, nmod, nsyms, i, j;
2753 /* Collect the loaded modules. */
2755 module_names = xmalloc(bufsize = 256);
2757 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2758 if (errno == ENOSPC && bufsize < ret) {
2759 module_names = xrealloc(module_names, bufsize = ret);
2760 goto retry_modules_load;
2762 bb_perror_msg("QM_MODULES");
2766 n_ext_modules = nmod = ret;
2768 /* Collect the modules' symbols. */
2771 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2772 memset(modules, 0, nmod * sizeof(*modules));
2773 for (i = 0, mn = module_names, m = modules;
2774 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2775 struct new_module_info info;
2777 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2778 if (errno == ENOENT) {
2779 /* The module was removed out from underneath us. */
2782 bb_perror_msg("query_module: QM_INFO: %s", mn);
2786 syms = xmalloc(bufsize = 1024);
2788 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2791 syms = xrealloc(syms, bufsize = ret);
2792 goto retry_mod_sym_load;
2794 /* The module was removed out from underneath us. */
2797 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2804 m->addr = info.addr;
2808 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2809 s->name += (unsigned long) syms;
2814 /* Collect the kernel's symbols. */
2816 syms = xmalloc(bufsize = 16 * 1024);
2817 retry_kern_sym_load:
2818 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2819 if (errno == ENOSPC && bufsize < ret) {
2820 syms = xrealloc(syms, bufsize = ret);
2821 goto retry_kern_sym_load;
2823 bb_perror_msg("kernel: QM_SYMBOLS");
2826 nksyms = nsyms = ret;
2829 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2830 s->name += (unsigned long) syms;
2836 /* Return the kernel symbol checksum version, or zero if not used. */
2838 static int new_is_kernel_checksummed(void)
2840 struct new_module_symbol *s;
2843 /* Using_Versions is not the first symbol, but it should be in there. */
2845 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2846 if (strcmp((char *) s->name, "Using_Versions") == 0)
2853 static int new_create_this_module(struct obj_file *f, const char *m_name)
2855 struct obj_section *sec;
2857 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2858 sizeof(struct new_module));
2859 memset(sec->contents, 0, sizeof(struct new_module));
2861 obj_add_symbol(f, SPFX "__this_module", -1,
2862 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2863 sizeof(struct new_module));
2865 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2871 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2872 /* add an entry to the __ksymtab section, creating it if necessary */
2873 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2875 struct obj_section *sec;
2878 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2879 * If __ksymtab is defined but not marked alloc, x out the first character
2880 * (no obj_delete routine) and create a new __ksymtab with the correct
2883 sec = obj_find_section(f, "__ksymtab");
2884 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2885 *((char *)(sec->name)) = 'x'; /* override const */
2889 sec = obj_create_alloced_section(f, "__ksymtab",
2890 tgt_sizeof_void_p, 0);
2893 sec->header.sh_flags |= SHF_ALLOC;
2894 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2896 ofs = sec->header.sh_size;
2897 obj_symbol_patch(f, sec->idx, ofs, sym);
2898 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2899 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2901 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2903 static int new_create_module_ksymtab(struct obj_file *f)
2905 struct obj_section *sec;
2908 /* We must always add the module references. */
2910 if (n_ext_modules_used) {
2911 struct new_module_ref *dep;
2912 struct obj_symbol *tm;
2914 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2915 (sizeof(struct new_module_ref)
2916 * n_ext_modules_used));
2920 tm = obj_find_symbol(f, SPFX "__this_module");
2921 dep = (struct new_module_ref *) sec->contents;
2922 for (i = 0; i < n_ext_modules; ++i)
2923 if (ext_modules[i].used) {
2924 dep->dep = ext_modules[i].addr;
2925 obj_symbol_patch(f, sec->idx,
2926 (char *) &dep->ref - sec->contents, tm);
2932 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2937 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2940 /* We don't want to export symbols residing in sections that
2941 aren't loaded. There are a number of these created so that
2942 we make sure certain module options don't appear twice. */
2944 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2946 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2948 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2949 struct obj_symbol *sym;
2950 for (sym = f->symtab[i]; sym; sym = sym->next)
2951 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2952 && sym->secidx <= SHN_HIRESERVE
2953 && (sym->secidx >= SHN_LORESERVE
2954 || loaded[sym->secidx])) {
2955 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2957 obj_symbol_patch(f, sec->idx, ofs, sym);
2958 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2965 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2973 new_init_module(const char *m_name, struct obj_file *f,
2974 unsigned long m_size)
2976 struct new_module *module;
2977 struct obj_section *sec;
2982 sec = obj_find_section(f, ".this");
2983 if (!sec || !sec->contents) {
2984 bb_perror_msg_and_die("corrupt module %s?",m_name);
2986 module = (struct new_module *) sec->contents;
2987 m_addr = sec->header.sh_addr;
2989 module->size_of_struct = sizeof(*module);
2990 module->size = m_size;
2991 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2993 sec = obj_find_section(f, "__ksymtab");
2994 if (sec && sec->header.sh_size) {
2995 module->syms = sec->header.sh_addr;
2996 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2999 if (n_ext_modules_used) {
3000 sec = obj_find_section(f, ".kmodtab");
3001 module->deps = sec->header.sh_addr;
3002 module->ndeps = n_ext_modules_used;
3006 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
3008 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
3010 sec = obj_find_section(f, "__ex_table");
3012 module->ex_table_start = sec->header.sh_addr;
3013 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
3016 sec = obj_find_section(f, ".text.init");
3018 module->runsize = sec->header.sh_addr - m_addr;
3020 sec = obj_find_section(f, ".data.init");
3022 if (!module->runsize ||
3023 module->runsize > sec->header.sh_addr - m_addr)
3024 module->runsize = sec->header.sh_addr - m_addr;
3026 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
3027 if (sec && sec->header.sh_size) {
3028 module->archdata_start = (void*)sec->header.sh_addr;
3029 module->archdata_end = module->archdata_start + sec->header.sh_size;
3031 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
3032 if (sec && sec->header.sh_size) {
3033 module->kallsyms_start = (void*)sec->header.sh_addr;
3034 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
3037 if (!arch_init_module(f, module))
3040 /* Whew! All of the initialization is complete. Collect the final
3041 module image and give it to the kernel. */
3043 image = xmalloc(m_size);
3044 obj_create_image(f, image);
3046 ret = new_sys_init_module(m_name, (struct new_module *) image);
3048 bb_perror_msg("init_module: %s", m_name);
3057 #define new_init_module(x, y, z) TRUE
3058 #define new_create_this_module(x, y) 0
3059 #define new_add_ksymtab(x, y) -1
3060 #define new_create_module_ksymtab(x)
3061 #define query_module(v, w, x, y, z) -1
3063 #endif /* CONFIG_FEATURE_2_4_MODULES */
3066 /*======================================================================*/
3069 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3072 struct obj_string_patch *p;
3073 struct obj_section *strsec;
3074 size_t len = strlen(string) + 1;
3077 p = xmalloc(sizeof(*p));
3078 p->next = f->string_patches;
3079 p->reloc_secidx = secidx;
3080 p->reloc_offset = offset;
3081 f->string_patches = p;
3083 strsec = obj_find_section(f, ".kstrtab");
3084 if (strsec == NULL) {
3085 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3086 p->string_offset = 0;
3087 loc = strsec->contents;
3089 p->string_offset = strsec->header.sh_size;
3090 loc = obj_extend_section(strsec, len);
3092 memcpy(loc, string, len);
3097 #ifdef CONFIG_FEATURE_2_4_MODULES
3099 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3100 struct obj_symbol *sym)
3102 struct obj_symbol_patch *p;
3104 p = xmalloc(sizeof(*p));
3105 p->next = f->symbol_patches;
3106 p->reloc_secidx = secidx;
3107 p->reloc_offset = offset;
3109 f->symbol_patches = p;
3115 static int obj_check_undefineds(struct obj_file *f)
3120 for (i = 0; i < HASH_BUCKETS; ++i) {
3121 struct obj_symbol *sym;
3122 for (sym = f->symtab[i]; sym; sym = sym->next)
3123 if (sym->secidx == SHN_UNDEF) {
3124 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
3125 sym->secidx = SHN_ABS;
3129 bb_error_msg("unresolved symbol %s", sym->name);
3139 static void obj_allocate_commons(struct obj_file *f)
3141 struct common_entry {
3142 struct common_entry *next;
3143 struct obj_symbol *sym;
3144 } *common_head = NULL;
3148 for (i = 0; i < HASH_BUCKETS; ++i) {
3149 struct obj_symbol *sym;
3150 for (sym = f->symtab[i]; sym; sym = sym->next)
3151 if (sym->secidx == SHN_COMMON) {
3152 /* Collect all COMMON symbols and sort them by size so as to
3153 minimize space wasted by alignment requirements. */
3155 struct common_entry **p, *n;
3156 for (p = &common_head; *p; p = &(*p)->next)
3157 if (sym->size <= (*p)->sym->size)
3160 n = alloca(sizeof(*n));
3168 for (i = 1; i < f->local_symtab_size; ++i) {
3169 struct obj_symbol *sym = f->local_symtab[i];
3170 if (sym && sym->secidx == SHN_COMMON) {
3171 struct common_entry **p, *n;
3172 for (p = &common_head; *p; p = &(*p)->next)
3173 if (sym == (*p)->sym)
3175 else if (sym->size < (*p)->sym->size) {
3176 n = alloca(sizeof(*n));
3186 /* Find the bss section. */
3187 for (i = 0; i < f->header.e_shnum; ++i)
3188 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3191 /* If for some reason there hadn't been one, create one. */
3192 if (i == f->header.e_shnum) {
3193 struct obj_section *sec;
3195 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3196 f->sections[i] = sec = arch_new_section();
3197 f->header.e_shnum = i + 1;
3199 memset(sec, 0, sizeof(*sec));
3200 sec->header.sh_type = SHT_PROGBITS;
3201 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3206 /* Allocate the COMMONS. */
3208 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3209 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3210 struct common_entry *c;
3212 for (c = common_head; c; c = c->next) {
3213 ElfW(Addr) align = c->sym->value;
3215 if (align > max_align)
3217 if (bss_size & (align - 1))
3218 bss_size = (bss_size | (align - 1)) + 1;
3221 c->sym->value = bss_size;
3223 bss_size += c->sym->size;
3226 f->sections[i]->header.sh_size = bss_size;
3227 f->sections[i]->header.sh_addralign = max_align;
3231 /* For the sake of patch relocation and parameter initialization,
3232 allocate zeroed data for NOBITS sections now. Note that after
3233 this we cannot assume NOBITS are really empty. */
3234 for (i = 0; i < f->header.e_shnum; ++i) {
3235 struct obj_section *s = f->sections[i];
3236 if (s->header.sh_type == SHT_NOBITS) {
3237 if (s->header.sh_size != 0)
3238 s->contents = memset(xmalloc(s->header.sh_size),
3239 0, s->header.sh_size);
3243 s->header.sh_type = SHT_PROGBITS;
3248 static unsigned long obj_load_size(struct obj_file *f)
3250 unsigned long dot = 0;
3251 struct obj_section *sec;
3253 /* Finalize the positions of the sections relative to one another. */
3255 for (sec = f->load_order; sec; sec = sec->load_next) {
3258 align = sec->header.sh_addralign;
3259 if (align && (dot & (align - 1)))
3260 dot = (dot | (align - 1)) + 1;
3262 sec->header.sh_addr = dot;
3263 dot += sec->header.sh_size;
3269 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3271 int i, n = f->header.e_shnum;
3274 /* Finalize the addresses of the sections. */
3277 for (i = 0; i < n; ++i)
3278 f->sections[i]->header.sh_addr += base;
3280 /* And iterate over all of the relocations. */
3282 for (i = 0; i < n; ++i) {
3283 struct obj_section *relsec, *symsec, *targsec, *strsec;
3284 ElfW(RelM) * rel, *relend;
3288 relsec = f->sections[i];
3289 if (relsec->header.sh_type != SHT_RELM)
3292 symsec = f->sections[relsec->header.sh_link];
3293 targsec = f->sections[relsec->header.sh_info];
3294 strsec = f->sections[symsec->header.sh_link];
3296 rel = (ElfW(RelM) *) relsec->contents;
3297 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3298 symtab = (ElfW(Sym) *) symsec->contents;
3299 strtab = (const char *) strsec->contents;
3301 for (; rel < relend; ++rel) {
3302 ElfW(Addr) value = 0;
3303 struct obj_symbol *intsym = NULL;
3304 unsigned long symndx;
3305 ElfW(Sym) * extsym = 0;
3308 /* Attempt to find a value to use for this relocation. */
3310 symndx = ELFW(R_SYM) (rel->r_info);
3312 /* Note we've already checked for undefined symbols. */
3314 extsym = &symtab[symndx];
3315 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
3316 /* Local symbols we look up in the local table to be sure
3317 we get the one that is really intended. */
3318 intsym = f->local_symtab[symndx];
3320 /* Others we look up in the hash table. */
3322 if (extsym->st_name)
3323 name = strtab + extsym->st_name;
3325 name = f->sections[extsym->st_shndx]->name;
3326 intsym = obj_find_symbol(f, name);
3329 value = obj_symbol_final_value(f, intsym);
3330 intsym->referenced = 1;
3332 #if SHT_RELM == SHT_RELA
3333 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3334 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3335 if (!extsym || !extsym->st_name ||
3336 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
3338 value += rel->r_addend;
3342 switch (arch_apply_relocation
3343 (f, targsec, symsec, intsym, rel, value)) {
3347 case obj_reloc_overflow:
3348 errmsg = "Relocation overflow";
3350 case obj_reloc_dangerous:
3351 errmsg = "Dangerous relocation";
3353 case obj_reloc_unhandled:
3354 errmsg = "Unhandled relocation";
3357 bb_error_msg("%s of type %ld for %s", errmsg,
3358 (long) ELFW(R_TYPE) (rel->r_info),
3359 strtab + extsym->st_name);
3361 bb_error_msg("%s of type %ld", errmsg,
3362 (long) ELFW(R_TYPE) (rel->r_info));
3370 /* Finally, take care of the patches. */
3372 if (f->string_patches) {
3373 struct obj_string_patch *p;
3374 struct obj_section *strsec;
3375 ElfW(Addr) strsec_base;
3376 strsec = obj_find_section(f, ".kstrtab");
3377 strsec_base = strsec->header.sh_addr;
3379 for (p = f->string_patches; p; p = p->next) {
3380 struct obj_section *targsec = f->sections[p->reloc_secidx];
3381 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3382 = strsec_base + p->string_offset;
3386 if (f->symbol_patches) {
3387 struct obj_symbol_patch *p;
3389 for (p = f->symbol_patches; p; p = p->next) {
3390 struct obj_section *targsec = f->sections[p->reloc_secidx];
3391 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3392 = obj_symbol_final_value(f, p->sym);
3399 static int obj_create_image(struct obj_file *f, char *image)
3401 struct obj_section *sec;
3402 ElfW(Addr) base = f->baseaddr;
3404 for (sec = f->load_order; sec; sec = sec->load_next) {
3407 if (sec->contents == 0 || sec->header.sh_size == 0)
3410 secimg = image + (sec->header.sh_addr - base);
3412 /* Note that we allocated data for NOBITS sections earlier. */
3413 memcpy(secimg, sec->contents, sec->header.sh_size);
3419 /*======================================================================*/
3421 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3424 ElfW(Shdr) * section_headers;
3428 /* Read the file header. */
3430 f = arch_new_file();
3431 memset(f, 0, sizeof(*f));
3432 f->symbol_cmp = strcmp;
3433 f->symbol_hash = obj_elf_hash;
3434 f->load_order_search_start = &f->load_order;
3436 fseek(fp, 0, SEEK_SET);
3437 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3438 bb_perror_msg("error reading ELF header");
3442 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3443 || f->header.e_ident[EI_MAG1] != ELFMAG1
3444 || f->header.e_ident[EI_MAG2] != ELFMAG2
3445 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3446 bb_error_msg("not an ELF file");
3449 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3450 || f->header.e_ident[EI_DATA] != ELFDATAM
3451 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3452 || !MATCH_MACHINE(f->header.e_machine)) {
3453 bb_error_msg("ELF file not for this architecture");
3456 if (f->header.e_type != ET_REL) {
3457 bb_error_msg("ELF file not a relocatable object");
3461 /* Read the section headers. */
3463 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3464 bb_error_msg("section header size mismatch: %lu != %lu",
3465 (unsigned long) f->header.e_shentsize,
3466 (unsigned long) sizeof(ElfW(Shdr)));
3470 shnum = f->header.e_shnum;
3471 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3472 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3474 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3475 fseek(fp, f->header.e_shoff, SEEK_SET);
3476 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3477 bb_perror_msg("error reading ELF section headers");
3481 /* Read the section data. */
3483 for (i = 0; i < shnum; ++i) {
3484 struct obj_section *sec;
3486 f->sections[i] = sec = arch_new_section();
3487 memset(sec, 0, sizeof(*sec));
3489 sec->header = section_headers[i];
3492 if(sec->header.sh_size) switch (sec->header.sh_type) {
3501 if (!loadprogbits) {
3502 sec->contents = NULL;
3509 if (sec->header.sh_size > 0) {
3510 sec->contents = xmalloc(sec->header.sh_size);
3511 fseek(fp, sec->header.sh_offset, SEEK_SET);
3512 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3513 bb_perror_msg("error reading ELF section data");
3517 sec->contents = NULL;
3521 #if SHT_RELM == SHT_REL
3523 bb_error_msg("RELA relocations not supported on this architecture");
3527 bb_error_msg("REL relocations not supported on this architecture");
3532 if (sec->header.sh_type >= SHT_LOPROC) {
3533 /* Assume processor specific section types are debug
3534 info and can safely be ignored. If this is ever not
3535 the case (Hello MIPS?), don't put ifdefs here but
3536 create an arch_load_proc_section(). */
3540 bb_error_msg("can't handle sections of type %ld",
3541 (long) sec->header.sh_type);
3546 /* Do what sort of interpretation as needed by each section. */
3548 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3550 for (i = 0; i < shnum; ++i) {
3551 struct obj_section *sec = f->sections[i];
3552 sec->name = shstrtab + sec->header.sh_name;
3555 for (i = 0; i < shnum; ++i) {
3556 struct obj_section *sec = f->sections[i];
3558 /* .modinfo should be contents only but gcc has no attribute for that.
3559 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3561 if (strcmp(sec->name, ".modinfo") == 0)
3562 sec->header.sh_flags &= ~SHF_ALLOC;
3564 if (sec->header.sh_flags & SHF_ALLOC)
3565 obj_insert_section_load_order(f, sec);
3567 switch (sec->header.sh_type) {
3570 unsigned long nsym, j;
3574 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3575 bb_error_msg("symbol size mismatch: %lu != %lu",
3576 (unsigned long) sec->header.sh_entsize,
3577 (unsigned long) sizeof(ElfW(Sym)));
3581 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3582 strtab = f->sections[sec->header.sh_link]->contents;
3583 sym = (ElfW(Sym) *) sec->contents;
3585 /* Allocate space for a table of local symbols. */
3586 j = f->local_symtab_size = sec->header.sh_info;
3587 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3589 /* Insert all symbols into the hash table. */
3590 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3591 ElfW(Addr) val = sym->st_value;
3594 name = strtab + sym->st_name;
3595 else if (sym->st_shndx < shnum)
3596 name = f->sections[sym->st_shndx]->name;
3600 #if defined(__SH5__)
3602 * For sh64 it is possible that the target of a branch
3603 * requires a mode switch (32 to 16 and back again).
3605 * This is implied by the lsb being set in the target
3606 * address for SHmedia mode and clear for SHcompact.
3608 val |= sym->st_other & 4;
3611 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3618 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3619 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3620 (unsigned long) sec->header.sh_entsize,
3621 (unsigned long) sizeof(ElfW(RelM)));
3625 /* XXX Relocation code from modutils-2.3.19 is not here.
3626 * Why? That's about 20 lines of code from obj/obj_load.c,
3627 * which gets done in a second pass through the sections.
3628 * This BusyBox insmod does similar work in obj_relocate(). */
3635 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3637 * load the unloaded sections directly into the memory allocated by
3638 * kernel for the module
3641 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3643 ElfW(Addr) base = f->baseaddr;
3644 struct obj_section* sec;
3646 for (sec = f->load_order; sec; sec = sec->load_next) {
3648 /* section already loaded? */
3649 if (sec->contents != NULL)
3652 if (sec->header.sh_size == 0)
3655 sec->contents = imagebase + (sec->header.sh_addr - base);
3656 fseek(fp, sec->header.sh_offset, SEEK_SET);
3657 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3658 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3667 static void hide_special_symbols(struct obj_file *f)
3669 static const char *const specials[] = {
3670 SPFX "cleanup_module",
3672 SPFX "kernel_version",
3676 struct obj_symbol *sym;
3677 const char *const *p;
3679 for (p = specials; *p; ++p)
3680 if ((sym = obj_find_symbol(f, *p)) != NULL)
3682 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3686 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3687 static int obj_gpl_license(struct obj_file *f, const char **license)
3689 struct obj_section *sec;
3690 /* This list must match *exactly* the list of allowable licenses in
3691 * linux/include/linux/module.h. Checking for leading "GPL" will not
3692 * work, somebody will use "GPL sucks, this is proprietary".
3694 static const char *gpl_licenses[] = {
3697 "GPL and additional rights",
3702 if ((sec = obj_find_section(f, ".modinfo"))) {
3703 const char *value, *ptr, *endptr;
3704 ptr = sec->contents;
3705 endptr = ptr + sec->header.sh_size;
3706 while (ptr < endptr) {
3707 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3711 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3712 if (strcmp(value+1, gpl_licenses[i]) == 0)
3717 if (strchr(ptr, '\0'))
3718 ptr = strchr(ptr, '\0') + 1;
3726 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3727 #define TAINT_PROPRIETORY_MODULE (1<<0)
3728 #define TAINT_FORCED_MODULE (1<<1)
3729 #define TAINT_UNSAFE_SMP (1<<2)
3730 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3732 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3733 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3737 static int first = 1;
3738 if (fd < 0 && !kernel_has_tainted)
3739 return; /* New modutils on old kernel */
3740 printf("Warning: loading %s will taint the kernel: %s%s\n",
3741 m_name, text1, text2);
3743 printf(" See %s for information about tainted modules\n", TAINT_URL);
3747 read(fd, buf, sizeof(buf)-1);
3748 buf[sizeof(buf)-1] = '\0';
3749 oldval = strtoul(buf, NULL, 10);
3750 sprintf(buf, "%d\n", oldval | taint);
3751 write(fd, buf, strlen(buf));
3755 /* Check if loading this module will taint the kernel. */
3756 static void check_tainted_module(struct obj_file *f, char *m_name)
3758 static const char tainted_file[] = TAINT_FILENAME;
3759 int fd, kernel_has_tainted;
3762 kernel_has_tainted = 1;
3763 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3764 if (errno == ENOENT)
3765 kernel_has_tainted = 0;
3766 else if (errno == EACCES)
3767 kernel_has_tainted = 1;
3769 perror(tainted_file);
3770 kernel_has_tainted = 0;
3774 switch (obj_gpl_license(f, &ptr)) {
3778 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3781 /* The module has a non-GPL license so we pretend that the
3782 * kernel always has a taint flag to get a warning even on
3783 * kernels without the proc flag.
3785 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3788 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3792 if (flag_force_load)
3793 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3798 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3799 #define check_tainted_module(x, y) do { } while(0);
3800 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3802 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3803 /* add module source, timestamp, kernel version and a symbol for the
3804 * start of some sections. this info is used by ksymoops to do better
3808 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3810 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3811 if (get_modinfo_value(f, "kernel_version") == NULL)
3812 return old_get_module_version(f, str);
3814 return new_get_module_version(f, str);
3815 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3816 strncpy(str, "???", sizeof(str));
3818 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3821 /* add module source, timestamp, kernel version and a symbol for the
3822 * start of some sections. this info is used by ksymoops to do better
3826 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3829 static const char symprefix[] = "__insmod_";
3830 struct obj_section *sec;
3831 struct obj_symbol *sym;
3832 char *name, *absolute_filename;
3833 char str[STRVERSIONLEN], real[PATH_MAX];
3834 int i, l, lm_name, lfilename, use_ksymtab, version;
3835 struct stat statbuf;
3837 static const char *section_names[] = {
3845 if (realpath(filename, real)) {
3846 absolute_filename = bb_xstrdup(real);
3849 int save_errno = errno;
3850 bb_error_msg("cannot get realpath for %s", filename);
3853 absolute_filename = bb_xstrdup(filename);
3856 lm_name = strlen(m_name);
3857 lfilename = strlen(absolute_filename);
3859 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3860 * are not to be exported. otherwise leave ksymtab alone for now, the
3861 * "export all symbols" compatibility code will export these symbols later.
3863 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3865 if ((sec = obj_find_section(f, ".this"))) {
3866 /* tag the module header with the object name, last modified
3867 * timestamp and module version. worst case for module version
3868 * is 0xffffff, decimal 16777215. putting all three fields in
3869 * one symbol is less readable but saves kernel space.
3871 l = sizeof(symprefix)+ /* "__insmod_" */
3872 lm_name+ /* module name */
3874 lfilename+ /* object filename */
3876 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3878 8+ /* version in dec */
3881 if (stat(absolute_filename, &statbuf) != 0)
3882 statbuf.st_mtime = 0;
3883 version = get_module_version(f, str); /* -1 if not found */
3884 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3885 symprefix, m_name, absolute_filename,
3886 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3888 sym = obj_add_symbol(f, name, -1,
3889 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3890 sec->idx, sec->header.sh_addr, 0);
3892 new_add_ksymtab(f, sym);
3894 free(absolute_filename);
3895 #ifdef _NOT_SUPPORTED_
3896 /* record where the persistent data is going, same address as previous symbol */
3899 l = sizeof(symprefix)+ /* "__insmod_" */
3900 lm_name+ /* module name */
3902 strlen(f->persist)+ /* data store */
3905 snprintf(name, l, "%s%s_P%s",
3906 symprefix, m_name, f->persist);
3907 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3908 sec->idx, sec->header.sh_addr, 0);
3910 new_add_ksymtab(f, sym);
3912 #endif /* _NOT_SUPPORTED_ */
3913 /* tag the desired sections if size is non-zero */
3915 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3916 if ((sec = obj_find_section(f, section_names[i])) &&
3917 sec->header.sh_size) {
3918 l = sizeof(symprefix)+ /* "__insmod_" */
3919 lm_name+ /* module name */
3921 strlen(sec->name)+ /* section name */
3923 8+ /* length in dec */
3926 snprintf(name, l, "%s%s_S%s_L%ld",
3927 symprefix, m_name, sec->name,
3928 (long)sec->header.sh_size);
3929 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3930 sec->idx, sec->header.sh_addr, 0);
3932 new_add_ksymtab(f, sym);
3936 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3938 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3939 static void print_load_map(struct obj_file *f)
3941 struct obj_symbol *sym;
3942 struct obj_symbol **all, **p;
3943 struct obj_section *sec;
3944 int i, nsyms, *loaded;
3946 /* Report on the section layout. */
3948 printf("Sections: Size %-*s Align\n",
3949 (int) (2 * sizeof(void *)), "Address");
3951 for (sec = f->load_order; sec; sec = sec->load_next) {
3955 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3960 printf("%-15s %08lx %0*lx 2**%d\n",
3962 (long)sec->header.sh_size,
3963 (int) (2 * sizeof(void *)),
3964 (long)sec->header.sh_addr,
3967 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3968 /* Quick reference which section indicies are loaded. */
3970 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3972 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3974 /* Collect the symbols we'll be listing. */
3976 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3977 for (sym = f->symtab[i]; sym; sym = sym->next)
3978 if (sym->secidx <= SHN_HIRESERVE
3979 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3982 all = alloca(nsyms * sizeof(struct obj_symbol *));
3984 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3985 for (sym = f->symtab[i]; sym; sym = sym->next)
3986 if (sym->secidx <= SHN_HIRESERVE
3987 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3990 /* And list them. */
3991 printf("\nSymbols:\n");
3992 for (p = all; p < all + nsyms; ++p) {
3994 unsigned long value;
3997 if (sym->secidx == SHN_ABS) {
4000 } else if (sym->secidx == SHN_UNDEF) {
4004 sec = f->sections[sym->secidx];
4006 if (sec->header.sh_type == SHT_NOBITS)
4008 else if (sec->header.sh_flags & SHF_ALLOC) {
4009 if (sec->header.sh_flags & SHF_EXECINSTR)
4011 else if (sec->header.sh_flags & SHF_WRITE)
4016 value = sym->value + sec->header.sh_addr;
4019 if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
4020 type = tolower(type);
4022 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
4030 extern int insmod_main( int argc, char **argv)
4037 unsigned long m_size;
4042 int exit_status = EXIT_FAILURE;
4044 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4045 struct utsname uts_info;
4046 char m_strversion[STRVERSIONLEN];
4050 #ifdef CONFIG_FEATURE_CLEAN_UP
4055 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4056 int flag_print_load_map = 0;
4059 struct utsname myuname;
4061 /* Parse any options */
4062 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4063 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
4065 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
4069 case 'f': /* force loading */
4070 flag_force_load = 1;
4072 case 'k': /* module loaded by kerneld, auto-cleanable */
4075 case 's': /* log to syslog */
4076 /* log to syslog -- not supported */
4077 /* but kernel needs this for request_module(), */
4078 /* as this calls: modprobe -k -s -- <module> */
4079 /* so silently ignore this flag */
4081 case 'v': /* verbose output */
4084 case 'q': /* silent */
4087 case 'x': /* do not export externs */
4090 case 'o': /* name the output module */
4092 m_name = bb_xstrdup(optarg);
4094 case 'L': /* Stub warning */
4095 /* This is needed for compatibility with modprobe.
4096 * In theory, this does locking, but we don't do
4097 * that. So be careful and plan your life around not
4098 * loading the same module 50 times concurrently. */
4100 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4101 case 'm': /* print module load map */
4102 flag_print_load_map = 1;
4110 if (argv[optind] == NULL) {
4114 /* Grab the module name */
4115 tmp1 = bb_xstrdup(argv[optind]);
4116 tmp = basename(tmp1);
4119 if (uname(&myuname) == 0) {
4120 if (myuname.release[0] == '2') {
4121 k_version = myuname.release[2] - '0';
4125 #if defined(CONFIG_FEATURE_2_6_MODULES)
4126 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
4127 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
4133 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4139 #if defined(CONFIG_FEATURE_2_6_MODULES)
4141 bb_xasprintf(&m_fullName, "%s.ko", tmp);
4144 bb_xasprintf(&m_fullName, "%s.o", tmp);
4150 tmp1 = 0; /* flag for free(m_name) before exit() */
4153 /* Get a filedesc for the module. Check we we have a complete path */
4154 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
4155 (fp = fopen(argv[optind], "r")) == NULL) {
4156 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4157 * but do not error out yet if we fail to find it... */
4158 if (k_version) { /* uname succeedd */
4161 char real_module_dir[FILENAME_MAX];
4163 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4164 /* Jump through hoops in case /lib/modules/`uname -r`
4165 * is a symlink. We do not want recursive_action to
4166 * follow symlinks, but we do want to follow the
4167 * /lib/modules/`uname -r` dir, So resolve it ourselves
4168 * if it is a link... */
4169 if (realpath (tmdn, real_module_dir) == NULL)
4172 module_dir = real_module_dir;
4173 recursive_action(module_dir, TRUE, FALSE, FALSE,
4174 check_module_name_match, 0, m_fullName);
4178 /* Check if we have found anything yet */
4179 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4181 char module_dir[FILENAME_MAX];
4185 if (realpath (_PATH_MODULES, module_dir) == NULL)
4186 strcpy(module_dir, _PATH_MODULES);
4187 /* No module found under /lib/modules/`uname -r`, this
4188 * time cast the net a bit wider. Search /lib/modules/ */
4189 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4190 check_module_name_match, 0, m_fullName))
4193 || ((fp = fopen(m_filename, "r")) == NULL))
4195 bb_error_msg("%s: no module by that name found", m_fullName);
4199 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4202 m_filename = bb_xstrdup(argv[optind]);
4205 printf("Using %s\n", m_filename);
4207 #ifdef CONFIG_FEATURE_2_6_MODULES
4211 argv[optind + 1] = m_filename;
4212 return insmod_ng_main(argc - optind, argv + optind);
4216 if ((f = obj_load(fp, LOADBITS)) == NULL)
4217 bb_perror_msg_and_die("Could not load the module");
4219 if (get_modinfo_value(f, "kernel_version") == NULL)
4224 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4225 /* Version correspondence? */
4227 if (uname(&uts_info) < 0)
4228 uts_info.release[0] = '\0';
4229 if (m_has_modinfo) {
4230 m_version = new_get_module_version(f, m_strversion);
4232 m_version = old_get_module_version(f, m_strversion);
4233 if (m_version == -1) {
4234 bb_error_msg("couldn't find the kernel version the module was "
4240 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4241 if (flag_force_load) {
4242 bb_error_msg("Warning: kernel-module version mismatch\n"
4243 "\t%s was compiled for kernel version %s\n"
4244 "\twhile this kernel is version %s",
4245 m_filename, m_strversion, uts_info.release);
4247 bb_error_msg("kernel-module version mismatch\n"
4248 "\t%s was compiled for kernel version %s\n"
4249 "\twhile this kernel is version %s.",
4250 m_filename, m_strversion, uts_info.release);
4256 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4258 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
4260 if (k_new_syscalls) {
4261 #ifdef CONFIG_FEATURE_2_4_MODULES
4262 if (!new_get_kernel_symbols())
4264 k_crcs = new_is_kernel_checksummed();
4266 bb_error_msg("Not configured to support new kernels");
4270 #ifdef CONFIG_FEATURE_2_2_MODULES
4271 if (!old_get_kernel_symbols(m_name))
4273 k_crcs = old_is_kernel_checksummed();
4275 bb_error_msg("Not configured to support old kernels");
4280 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4282 m_crcs = new_is_module_checksummed(f);
4284 m_crcs = old_is_module_checksummed(f);
4286 if (m_crcs != k_crcs)
4287 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4288 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4290 /* Let the module know about the kernel symbols. */
4291 add_kernel_symbols(f);
4293 /* Allocate common symbols, symbol tables, and string tables. */
4296 ? !new_create_this_module(f, m_name)
4297 : !old_create_mod_use_count(f))
4302 if (!obj_check_undefineds(f)) {
4305 obj_allocate_commons(f);
4306 check_tainted_module(f, m_name);
4308 /* done with the module name, on to the optional var=value arguments */
4311 if (optind < argc) {
4313 ? !new_process_module_arguments(f, argc - optind, argv + optind)
4314 : !old_process_module_arguments(f, argc - optind, argv + optind))
4321 hide_special_symbols(f);
4323 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4324 add_ksymoops_symbols(f, m_filename, m_name);
4325 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4328 new_create_module_ksymtab(f);
4330 /* Find current size of the module */
4331 m_size = obj_load_size(f);
4334 m_addr = create_module(m_name, m_size);
4335 if (m_addr == -1) switch (errno) {
4337 bb_error_msg("A module named %s already exists", m_name);
4340 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4344 bb_perror_msg("create_module: %s", m_name);
4350 * the PROGBITS section was not loaded by the obj_load
4351 * now we can load them directly into the kernel memory
4353 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4354 delete_module(m_name);
4359 if (!obj_relocate(f, m_addr)) {
4360 delete_module(m_name);
4365 ? !new_init_module(m_name, f, m_size)
4366 : !old_init_module(m_name, f, m_size))
4368 delete_module(m_name);
4372 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4373 if(flag_print_load_map)
4377 exit_status = EXIT_SUCCESS;
4380 #ifdef CONFIG_FEATURE_CLEAN_UP
4390 return(exit_status);
4397 #ifdef CONFIG_FEATURE_2_6_MODULES
4399 #include <sys/mman.h>
4400 #include <asm/unistd.h>
4401 #include <sys/syscall.h>
4403 /* We use error numbers in a loose translation... */
4404 static const char *moderror(int err)
4408 return "Invalid module format";
4410 return "Unknown symbol in module";
4412 return "Module has wrong symbol version";
4414 return "Invalid parameters";
4416 return strerror(err);
4420 extern int insmod_ng_main( int argc, char **argv)
4428 char *filename, *options = bb_xstrdup("");
4436 /* Rest is options */
4437 for (i = 2; i < argc; i++) {
4438 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4439 /* Spaces handled by "" pairs, but no way of escaping quotes */
4440 if (strchr(argv[i], ' ')) {
4441 strcat(options, "\"");
4442 strcat(options, argv[i]);
4443 strcat(options, "\"");
4445 strcat(options, argv[i]);
4447 strcat(options, " ");
4450 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4451 bb_perror_msg_and_die("cannot open module `%s'", filename);
4456 map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
4457 if (map == MAP_FAILED) {
4458 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4461 ret = syscall(__NR_init_module, map, len, options);
4463 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4464 filename, moderror(errno), ret);