1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * This version of insmod supports ARM, CRIS, H8/300, x86, ia64, x86_64,
6 * m68k, MIPS, PowerPC, S390, SH3/4/5, Sparc, v850e, and x86_64.
8 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
9 * and Ron Alder <alder@lineo.com>
11 * Rodney Radford <rradford@mindspring.com> 17-Aug-2004.
12 * Added x86_64 support.
14 * Miles Bader <miles@gnu.org> added NEC V850E support.
16 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
17 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
19 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
20 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
21 * very minor changes required to also work with StrongArm and presumably
22 * all ARM based systems.
24 * Yoshinori Sato <ysato@users.sourceforge.jp> 19-May-2004.
25 * added Renesas H8/300 support.
27 * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
28 * Integrated support for sh64 (SH-5), from preliminary modutils
29 * patches from Benedict Gaster <benedict.gaster@superh.com>.
30 * Currently limited to support for 32bit ABI.
32 * Magnus Damm <damm@opensource.se> 22-May-2002.
33 * The plt and got code are now using the same structs.
34 * Added generic linked list code to fully support PowerPC.
35 * Replaced the mess in arch_apply_relocation() with architecture blocks.
36 * The arch_create_got() function got cleaned up with architecture blocks.
37 * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
39 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
40 * PowerPC specific code stolen from modutils-2.3.16,
41 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
42 * I've only tested the code on mpc8xx platforms in big-endian mode.
43 * Did some cleanup and added USE_xxx_ENTRIES...
45 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
46 * based on modutils-2.4.2
47 * MIPS specific support for Elf loading and relocation.
48 * Copyright 1996, 1997 Linux International.
49 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
51 * Based almost entirely on the Linux modutils-2.3.11 implementation.
52 * Copyright 1996, 1997 Linux International.
53 * New implementation contributed by Richard Henderson <rth@tamu.edu>
54 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
55 * Restructured (and partly rewritten) by:
56 * Björn Ekwall <bj0rn@blox.se> February 1999
58 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
61 //kbuild:lib-$(CONFIG_FEATURE_2_4_MODULES) += modutils-24.o
65 #include <sys/utsname.h>
67 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
74 #if defined(__alpha__)
75 #define MATCH_MACHINE(x) (x == EM_ALPHA)
76 #define SHT_RELM SHT_RELA
77 #define Elf64_RelM Elf64_Rela
78 #define ELFCLASSM ELFCLASS64
83 #define MATCH_MACHINE(x) (x == EM_ARM)
84 #define SHT_RELM SHT_REL
85 #define Elf32_RelM Elf32_Rel
86 #define ELFCLASSM ELFCLASS32
87 #define USE_PLT_ENTRIES
88 #define PLT_ENTRY_SIZE 8
89 #define USE_GOT_ENTRIES
90 #define GOT_ENTRY_SIZE 8
95 #if defined(__nds32__) || defined(__NDS32__)
96 #define CONFIG_USE_GOT_ENTRIES
97 #define CONFIG_GOT_ENTRY_SIZE 4
98 #define CONFIG_USE_SINGLE
100 #if defined(__NDS32_EB__)
101 #define MATCH_MACHINE(x) (x == EM_NDS32)
102 #define SHT_RELM SHT_RELA
103 #define Elf32_RelM Elf32_Rela
104 #define ELFCLASSM ELFCLASS32
107 #if defined(__NDS32_EL__)
108 #define MATCH_MACHINE(x) (x == EM_NDS32)
109 #define SHT_RELM SHT_RELA
110 #define Elf32_RelM Elf32_Rela
111 #define ELFCLASSM ELFCLASS32
117 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
118 #define SHT_RELM SHT_RELA
119 #define Elf32_RelM Elf32_Rela
120 #define ELFCLASSM ELFCLASS32
124 #if defined(__cris__)
125 #define MATCH_MACHINE(x) (x == EM_CRIS)
126 #define SHT_RELM SHT_RELA
127 #define Elf32_RelM Elf32_Rela
128 #define ELFCLASSM ELFCLASS32
131 #define R_CRIS_NONE 0
137 #if defined(__H8300H__) || defined(__H8300S__)
138 #define MATCH_MACHINE(x) (x == EM_H8_300)
139 #define SHT_RELM SHT_RELA
140 #define Elf32_RelM Elf32_Rela
141 #define ELFCLASSM ELFCLASS32
143 #define SYMBOL_PREFIX "_"
146 /* PA-RISC / HP-PA */
147 #if defined(__hppa__)
148 #define MATCH_MACHINE(x) (x == EM_PARISC)
149 #define SHT_RELM SHT_RELA
150 #if defined(__LP64__)
151 #define Elf64_RelM Elf64_Rela
152 #define ELFCLASSM ELFCLASS64
154 #define Elf32_RelM Elf32_Rela
155 #define ELFCLASSM ELFCLASS32
160 #if defined(__i386__)
162 #define MATCH_MACHINE(x) (x == EM_386)
164 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
166 #define SHT_RELM SHT_REL
167 #define Elf32_RelM Elf32_Rel
168 #define ELFCLASSM ELFCLASS32
169 #define USE_GOT_ENTRIES
170 #define GOT_ENTRY_SIZE 4
174 /* IA64, aka Itanium */
175 #if defined(__ia64__)
176 #define MATCH_MACHINE(x) (x == EM_IA_64)
177 #define SHT_RELM SHT_RELA
178 #define Elf64_RelM Elf64_Rela
179 #define ELFCLASSM ELFCLASS64
183 #if defined(__mc68000__)
184 #define MATCH_MACHINE(x) (x == EM_68K)
185 #define SHT_RELM SHT_RELA
186 #define Elf32_RelM Elf32_Rela
187 #define ELFCLASSM ELFCLASS32
188 #define USE_GOT_ENTRIES
189 #define GOT_ENTRY_SIZE 4
194 #if defined(__microblaze__)
196 #include <linux/elf-em.h>
197 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
198 #define SHT_RELM SHT_RELA
199 #define Elf32_RelM Elf32_Rela
200 #define ELFCLASSM ELFCLASS32
204 #if defined(__mips__)
205 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
206 #define SHT_RELM SHT_REL
207 #define Elf32_RelM Elf32_Rel
208 #define ELFCLASSM ELFCLASS32
209 /* Account for ELF spec changes. */
210 #ifndef EM_MIPS_RS3_LE
211 #ifdef EM_MIPS_RS4_BE
212 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
214 #define EM_MIPS_RS3_LE 10
216 #endif /* !EM_MIPS_RS3_LE */
217 #define ARCHDATAM "__dbe_table"
221 #if defined(__nios2__)
222 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
223 #define SHT_RELM SHT_RELA
224 #define Elf32_RelM Elf32_Rela
225 #define ELFCLASSM ELFCLASS32
229 #if defined(__powerpc64__)
230 #define MATCH_MACHINE(x) (x == EM_PPC64)
231 #define SHT_RELM SHT_RELA
232 #define Elf64_RelM Elf64_Rela
233 #define ELFCLASSM ELFCLASS64
234 #elif defined(__powerpc__)
235 #define MATCH_MACHINE(x) (x == EM_PPC)
236 #define SHT_RELM SHT_RELA
237 #define Elf32_RelM Elf32_Rela
238 #define ELFCLASSM ELFCLASS32
239 #define USE_PLT_ENTRIES
240 #define PLT_ENTRY_SIZE 16
242 #define LIST_ARCHTYPE ElfW(Addr)
244 #define ARCHDATAM "__ftr_fixup"
248 #if defined(__s390__)
249 #define MATCH_MACHINE(x) (x == EM_S390)
250 #define SHT_RELM SHT_RELA
251 #define Elf32_RelM Elf32_Rela
252 #define ELFCLASSM ELFCLASS32
253 #define USE_PLT_ENTRIES
254 #define PLT_ENTRY_SIZE 8
255 #define USE_GOT_ENTRIES
256 #define GOT_ENTRY_SIZE 8
262 #define MATCH_MACHINE(x) (x == EM_SH)
263 #define SHT_RELM SHT_RELA
264 #define Elf32_RelM Elf32_Rela
265 #define ELFCLASSM ELFCLASS32
266 #define USE_GOT_ENTRIES
267 #define GOT_ENTRY_SIZE 4
269 /* the SH changes have only been tested in =little endian= mode */
270 /* I'm not sure about big endian, so let's warn: */
271 #if defined(__sh__) && BB_BIG_ENDIAN
272 # error insmod.c may require changes for use on big endian SH
274 /* it may or may not work on the SH1/SH2... Error on those also */
275 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
276 #error insmod.c may require changes for SH1 or SH2 use
281 #if defined(__sparc__)
282 #define MATCH_MACHINE(x) (x == EM_SPARC)
283 #define SHT_RELM SHT_RELA
284 #define Elf32_RelM Elf32_Rela
285 #define ELFCLASSM ELFCLASS32
289 #if defined(__v850e__)
290 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
291 #define SHT_RELM SHT_RELA
292 #define Elf32_RelM Elf32_Rela
293 #define ELFCLASSM ELFCLASS32
294 #define USE_PLT_ENTRIES
295 #define PLT_ENTRY_SIZE 8
297 #ifndef EM_CYGNUS_V850 /* grumble */
298 #define EM_CYGNUS_V850 0x9080
300 #define SYMBOL_PREFIX "_"
304 #if defined(__x86_64__)
305 #define MATCH_MACHINE(x) (x == EM_X86_64)
306 #define SHT_RELM SHT_RELA
307 #define USE_GOT_ENTRIES
308 #define GOT_ENTRY_SIZE 8
310 #define Elf64_RelM Elf64_Rela
311 #define ELFCLASSM ELFCLASS64
315 #error Sorry, but insmod.c does not yet support this architecture...
319 //----------------------------------------------------------------------------
320 //--------modutils module.h, lines 45-242
321 //----------------------------------------------------------------------------
323 /* Definitions for the Linux module syscall interface.
324 Copyright 1996, 1997 Linux International.
326 Contributed by Richard Henderson <rth@tamu.edu>
328 This file is part of the Linux modutils.
330 This program is free software; you can redistribute it and/or modify it
331 under the terms of the GNU General Public License as published by the
332 Free Software Foundation; either version 2 of the License, or (at your
333 option) any later version.
335 This program is distributed in the hope that it will be useful, but
336 WITHOUT ANY WARRANTY; without even the implied warranty of
337 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
338 General Public License for more details.
340 You should have received a copy of the GNU General Public License
341 along with this program; if not, write to the Free Software Foundation,
342 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
345 #ifndef MODUTILS_MODULE_H
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
366 tgt_sizeof_char_p = 8,
367 tgt_sizeof_void_p = 8
369 #define tgt_long long long
372 /*======================================================================*/
373 /* The structures used in Linux 2.1. */
375 /* Note: new_module_symbol does not use tgt_long intentionally */
376 struct new_module_symbol {
381 struct new_module_persist;
383 struct new_module_ref {
384 unsigned tgt_long dep; /* kernel addresses */
385 unsigned tgt_long ref;
386 unsigned tgt_long next_ref;
390 unsigned tgt_long size_of_struct; /* == sizeof(module) */
391 unsigned tgt_long next;
392 unsigned tgt_long name;
393 unsigned tgt_long size;
396 unsigned tgt_long flags; /* AUTOCLEAN et al */
401 unsigned tgt_long syms;
402 unsigned tgt_long deps;
403 unsigned tgt_long refs;
404 unsigned tgt_long init;
405 unsigned tgt_long cleanup;
406 unsigned tgt_long ex_table_start;
407 unsigned tgt_long ex_table_end;
409 unsigned tgt_long gp;
411 /* Everything after here is extension. */
412 unsigned tgt_long persist_start;
413 unsigned tgt_long persist_end;
414 unsigned tgt_long can_unload;
415 unsigned tgt_long runsize;
416 const char *kallsyms_start; /* All symbols for kernel debugging */
417 const char *kallsyms_end;
418 const char *archdata_start; /* arch specific data for module */
419 const char *archdata_end;
420 const char *kernel_data; /* Reserved for kernel internal use */
424 #define ARCHDATA_SEC_NAME ARCHDATAM
426 #define ARCHDATA_SEC_NAME "__archdata"
428 #define KALLSYMS_SEC_NAME "__kallsyms"
431 struct new_module_info {
438 /* Bits of module.flags. */
442 NEW_MOD_AUTOCLEAN = 4,
444 NEW_MOD_USED_ONCE = 16
447 int init_module(const char *name, const struct new_module *);
448 int query_module(const char *name, int which, void *buf,
449 size_t bufsize, size_t *ret);
451 /* Values for query_module's which. */
460 /*======================================================================*/
461 /* The system calls unchanged between 2.0 and 2.1. */
463 unsigned long create_module(const char *, size_t);
464 int delete_module(const char *module, unsigned int flags);
467 #endif /* module.h */
469 //----------------------------------------------------------------------------
470 //--------end of modutils module.h
471 //----------------------------------------------------------------------------
475 //----------------------------------------------------------------------------
476 //--------modutils obj.h, lines 253-462
477 //----------------------------------------------------------------------------
479 /* Elf object file loading and relocation routines.
480 Copyright 1996, 1997 Linux International.
482 Contributed by Richard Henderson <rth@tamu.edu>
484 This file is part of the Linux modutils.
486 This program is free software; you can redistribute it and/or modify it
487 under the terms of the GNU General Public License as published by the
488 Free Software Foundation; either version 2 of the License, or (at your
489 option) any later version.
491 This program is distributed in the hope that it will be useful, but
492 WITHOUT ANY WARRANTY; without even the implied warranty of
493 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
494 General Public License for more details.
496 You should have received a copy of the GNU General Public License
497 along with this program; if not, write to the Free Software Foundation,
498 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
501 #ifndef MODUTILS_OBJ_H
503 /* The relocatable object is manipulated using elfin types. */
509 # if ELFCLASSM == ELFCLASS32
510 # define ElfW(x) Elf32_ ## x
511 # define ELFW(x) ELF32_ ## x
513 # define ElfW(x) Elf64_ ## x
514 # define ELFW(x) ELF64_ ## x
518 /* For some reason this is missing from some ancient C libraries.... */
519 #ifndef ELF32_ST_INFO
520 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
523 #ifndef ELF64_ST_INFO
524 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
527 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
528 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
529 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
530 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
531 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
533 struct obj_string_patch;
534 struct obj_symbol_patch;
540 struct obj_section *load_next;
545 struct obj_symbol *next; /* hash table link */
549 int secidx; /* the defining section index/module */
551 int ksymidx; /* for export to the kernel symtab */
552 int referenced; /* actually used in the link */
555 /* Hardcode the hash table size. We shouldn't be needing so many
556 symbols that we begin to degrade performance, and we get a big win
557 by giving the compiler a constant divisor. */
559 #define HASH_BUCKETS 521
564 struct obj_section **sections;
565 struct obj_section *load_order;
566 struct obj_section **load_order_search_start;
567 struct obj_string_patch *string_patches;
568 struct obj_symbol_patch *symbol_patches;
569 int (*symbol_cmp)(const char *, const char *); /* cant be FAST_FUNC */
570 unsigned long (*symbol_hash)(const char *) FAST_FUNC;
571 unsigned long local_symtab_size;
572 struct obj_symbol **local_symtab;
573 struct obj_symbol *symtab[HASH_BUCKETS];
583 struct obj_string_patch {
584 struct obj_string_patch *next;
586 ElfW(Addr) reloc_offset;
587 ElfW(Addr) string_offset;
590 struct obj_symbol_patch {
591 struct obj_symbol_patch *next;
593 ElfW(Addr) reloc_offset;
594 struct obj_symbol *sym;
598 /* Generic object manipulation routines. */
600 static unsigned long FAST_FUNC obj_elf_hash(const char *);
602 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
604 static struct obj_symbol *obj_find_symbol(struct obj_file *f,
607 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
608 struct obj_symbol *sym);
610 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
611 static void obj_set_symbol_compare(struct obj_file *f,
612 int (*cmp)(const char *, const char *),
613 unsigned long (*hash)(const char *) FAST_FUNC);
616 static struct obj_section *obj_find_section(struct obj_file *f,
619 static void obj_insert_section_load_order(struct obj_file *f,
620 struct obj_section *sec);
622 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
627 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
632 static void *obj_extend_section(struct obj_section *sec, unsigned long more);
634 static void obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
637 static void obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
638 struct obj_symbol *sym);
640 static void obj_check_undefineds(struct obj_file *f);
642 static void obj_allocate_commons(struct obj_file *f);
644 static unsigned long obj_load_size(struct obj_file *f);
646 static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
649 #define obj_load(image, image_size, loadprogbits) \
650 obj_load(image, image_size)
652 static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits);
654 static int obj_create_image(struct obj_file *f, char *image);
656 /* Architecture specific manipulation routines. */
658 static struct obj_file *arch_new_file(void);
660 static struct obj_section *arch_new_section(void);
662 static struct obj_symbol *arch_new_symbol(void);
664 static enum obj_reloc arch_apply_relocation(struct obj_file *f,
665 struct obj_section *targsec,
666 /*struct obj_section *symsec,*/
667 struct obj_symbol *sym,
668 ElfW(RelM) *rel, ElfW(Addr) value);
670 static void arch_create_got(struct obj_file *f);
671 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
672 static int obj_gpl_license(struct obj_file *f, const char **license);
675 //----------------------------------------------------------------------------
676 //--------end of modutils obj.h
677 //----------------------------------------------------------------------------
680 /* SPFX is always a string, so it can be concatenated to string constants. */
682 #define SPFX SYMBOL_PREFIX
687 enum { STRVERSIONLEN = 64 };
689 /*======================================================================*/
691 #define flag_force_load (option_mask32 & INSMOD_OPT_FORCE)
692 #define flag_autoclean (option_mask32 & INSMOD_OPT_KERNELD)
693 #define flag_verbose (option_mask32 & INSMOD_OPT_VERBOSE)
694 #define flag_quiet (option_mask32 & INSMOD_OPT_SILENT)
695 #define flag_noexport (option_mask32 & INSMOD_OPT_NO_EXPORT)
696 #define flag_print_load_map (option_mask32 & INSMOD_OPT_PRINT_MAP)
698 /*======================================================================*/
700 #if defined(USE_LIST)
702 struct arch_list_entry {
703 struct arch_list_entry *next;
704 LIST_ARCHTYPE addend;
711 #if defined(USE_SINGLE)
713 struct arch_single_entry {
721 #if defined(__mips__)
723 struct mips_hi16 *next;
730 struct obj_file root;
731 #if defined(USE_PLT_ENTRIES)
732 struct obj_section *plt;
734 #if defined(USE_GOT_ENTRIES)
735 struct obj_section *got;
737 #if defined(__mips__)
738 struct mips_hi16 *mips_hi16_list;
743 struct obj_symbol root;
744 #if defined(USE_PLT_ENTRIES)
745 #if defined(USE_PLT_LIST)
746 struct arch_list_entry *pltent;
748 struct arch_single_entry pltent;
751 #if defined(USE_GOT_ENTRIES)
752 struct arch_single_entry gotent;
757 struct external_module {
762 struct new_module_symbol *syms;
765 static struct new_module_symbol *ksyms;
766 static size_t nksyms;
768 static struct external_module *ext_modules;
769 static int n_ext_modules;
770 static int n_ext_modules_used;
772 /*======================================================================*/
775 static struct obj_file *arch_new_file(void)
778 f = xzalloc(sizeof(*f));
779 return &f->root; /* it's a first member */
782 static struct obj_section *arch_new_section(void)
784 return xzalloc(sizeof(struct obj_section));
787 static struct obj_symbol *arch_new_symbol(void)
789 struct arch_symbol *sym;
790 sym = xzalloc(sizeof(*sym));
794 static enum obj_reloc
795 arch_apply_relocation(struct obj_file *f,
796 struct obj_section *targsec,
797 /*struct obj_section *symsec,*/
798 struct obj_symbol *sym,
799 ElfW(RelM) *rel, ElfW(Addr) v)
801 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
802 || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \
803 || defined(__powerpc__) || defined(__mips__)
804 struct arch_file *ifile = (struct arch_file *) f;
806 enum obj_reloc ret = obj_reloc_ok;
807 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
808 #if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
809 || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
810 || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
811 || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
812 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
814 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
815 struct arch_symbol *isym = (struct arch_symbol *) sym;
817 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
818 || defined(__sh__) || defined(__s390__)
819 #if defined(USE_GOT_ENTRIES)
820 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
823 #if defined(USE_PLT_ENTRIES)
824 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
826 # if defined(USE_PLT_LIST)
827 struct arch_list_entry *pe;
829 struct arch_single_entry *pe;
833 switch (ELF_R_TYPE(rel->r_info)) {
848 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
849 * (which is .got) similar to branch,
850 * but is full 32 bits relative */
859 case R_ARM_GOTOFF: /* address relative to the got */
863 #elif defined(__cris__)
869 /* CRIS keeps the relocation value in the r_addend field and
870 * should not use whats in *loc at all
875 #elif defined(__H8300H__) || defined(__H8300S__)
878 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
879 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
890 if ((ElfW(Sword))v > 0x7fff
891 || (ElfW(Sword))v < -(ElfW(Sword))0x8000
893 ret = obj_reloc_overflow;
895 *(unsigned short *)loc = v;
900 if ((ElfW(Sword))v > 0x7f
901 || (ElfW(Sword))v < -(ElfW(Sword))0x80
903 ret = obj_reloc_overflow;
905 *(unsigned char *)loc = v;
909 #elif defined(__i386__)
941 #elif defined(__microblaze__)
942 case R_MICROBLAZE_NONE:
943 case R_MICROBLAZE_64_NONE:
944 case R_MICROBLAZE_32_SYM_OP_SYM:
945 case R_MICROBLAZE_32_PCREL:
948 case R_MICROBLAZE_64_PCREL: {
949 /* dot is the address of the current instruction.
950 * v is the target symbol address.
951 * So we need to extract the offset in the code,
952 * adding v, then subtrating the current address
953 * of this instruction.
954 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
957 /* Get split offset stored in code */
958 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
961 /* Adjust relative offset. -4 adjustment required
962 * because dot points to the IMM insn, but branch
963 * is computed relative to the branch instruction itself.
967 /* Store back into code */
968 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
969 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
974 case R_MICROBLAZE_32:
978 case R_MICROBLAZE_64: {
979 /* Get split pointer stored in code */
980 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
983 /* Add reloc offset */
986 /* Store back into code */
987 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
988 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
993 case R_MICROBLAZE_32_PCREL_LO:
994 case R_MICROBLAZE_32_LO:
995 case R_MICROBLAZE_SRO32:
996 case R_MICROBLAZE_SRW32:
997 ret = obj_reloc_unhandled;
1000 #elif defined(__mc68000__)
1011 ret = obj_reloc_overflow;
1018 ret = obj_reloc_overflow;
1025 if ((ElfW(Sword))v > 0x7f
1026 || (ElfW(Sword))v < -(ElfW(Sword))0x80
1028 ret = obj_reloc_overflow;
1035 if ((ElfW(Sword))v > 0x7fff
1036 || (ElfW(Sword))v < -(ElfW(Sword))0x8000
1038 ret = obj_reloc_overflow;
1044 *(int *)loc = v - dot;
1047 case R_68K_GLOB_DAT:
1048 case R_68K_JMP_SLOT:
1052 case R_68K_RELATIVE:
1053 *(int *)loc += f->baseaddr;
1059 # ifdef R_68K_GOTOFF
1065 #elif defined(__mips__)
1076 ret = obj_reloc_dangerous;
1077 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1078 ret = obj_reloc_overflow;
1080 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1086 struct mips_hi16 *n;
1088 /* We cannot relocate this one now because we don't know the value
1089 of the carry we need to add. Save the information, and let LO16
1090 do the actual relocation. */
1091 n = xmalloc(sizeof *n);
1094 n->next = ifile->mips_hi16_list;
1095 ifile->mips_hi16_list = n;
1101 unsigned long insnlo = *loc;
1102 ElfW(Addr) val, vallo;
1104 /* Sign extend the addend we extract from the lo insn. */
1105 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1107 if (ifile->mips_hi16_list != NULL) {
1108 struct mips_hi16 *l;
1110 l = ifile->mips_hi16_list;
1112 struct mips_hi16 *next;
1115 /* Do the HI16 relocation. Note that we actually don't
1116 need to know anything about the LO16 itself, except where
1117 to find the low 16 bits of the addend needed by the LO16. */
1120 ((insn & 0xffff) << 16) +
1124 /* Account for the sign extension that will happen in the
1131 insn = (insn & ~0xffff) | val;
1139 ifile->mips_hi16_list = NULL;
1142 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1144 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1149 #elif defined(__nios2__)
1154 case R_NIOS2_BFD_RELOC_32:
1158 case R_NIOS2_BFD_RELOC_16:
1160 ret = obj_reloc_overflow;
1165 case R_NIOS2_BFD_RELOC_8:
1167 ret = obj_reloc_overflow;
1176 if ((Elf32_Sword)v > 0x7fff
1177 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1179 ret = obj_reloc_overflow;
1183 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1193 ret = obj_reloc_overflow;
1197 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1202 case R_NIOS2_PCREL16:
1207 if ((Elf32_Sword)v > 0x7fff
1208 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1210 ret = obj_reloc_overflow;
1214 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1220 Elf32_Addr word, gp;
1222 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1224 if ((Elf32_Sword)v > 0x7fff
1225 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1227 ret = obj_reloc_overflow;
1231 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1235 case R_NIOS2_CALL26:
1237 ret = obj_reloc_dangerous;
1238 if ((v >> 28) != (dot >> 28))
1239 ret = obj_reloc_overflow;
1240 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1248 ret = obj_reloc_overflow;
1251 word = *loc & ~0x7c0;
1252 *loc = word | ((v & 0x1f) << 6);
1261 ret = obj_reloc_overflow;
1264 word = *loc & ~0xfc0;
1265 *loc = word | ((v & 0x3f) << 6);
1274 ret = obj_reloc_overflow;
1277 word = *loc & ~0x3fc0;
1278 *loc = word | ((v & 0xff) << 6);
1287 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1297 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1302 case R_NIOS2_HIADJ16:
1304 Elf32_Addr word1, word2;
1307 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1308 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1313 #elif defined(__powerpc64__)
1314 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1316 #elif defined(__powerpc__)
1318 case R_PPC_ADDR16_HA:
1319 *(unsigned short *)loc = (v + 0x8000) >> 16;
1322 case R_PPC_ADDR16_HI:
1323 *(unsigned short *)loc = v >> 16;
1326 case R_PPC_ADDR16_LO:
1327 *(unsigned short *)loc = v;
1341 #elif defined(__s390__)
1344 *(unsigned int *) loc += v;
1347 *(unsigned short *) loc += v;
1350 *(unsigned char *) loc += v;
1354 *(unsigned int *) loc += v - dot;
1357 *(unsigned short *) loc += (v - dot) >> 1;
1360 *(unsigned short *) loc += v - dot;
1364 case R_390_PLT16DBL:
1365 /* find the plt entry and initialize it. */
1366 pe = (struct arch_single_entry *) &isym->pltent;
1367 if (pe->inited == 0) {
1368 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1369 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1371 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1378 /* Insert relative distance to target. */
1379 v = plt + pe->offset - dot;
1380 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1381 *(unsigned int *) loc = (unsigned int) v;
1382 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1383 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1386 case R_390_GLOB_DAT:
1387 case R_390_JMP_SLOT:
1391 case R_390_RELATIVE:
1392 *loc += f->baseaddr;
1396 *(unsigned long *) loc += got - dot;
1402 if (!isym->gotent.inited)
1404 isym->gotent.inited = 1;
1405 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1407 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1408 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1409 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1410 *(unsigned short *) loc += isym->gotent.offset;
1411 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1412 *(unsigned int *) loc += isym->gotent.offset;
1415 # ifndef R_390_GOTOFF32
1416 # define R_390_GOTOFF32 R_390_GOTOFF
1418 case R_390_GOTOFF32:
1422 #elif defined(__sh__)
1445 *loc = f->baseaddr + rel->r_addend;
1449 *loc = got - dot + rel->r_addend;
1459 # if defined(__SH5__)
1460 case R_SH_IMM_MEDLOW16:
1461 case R_SH_IMM_LOW16:
1465 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1469 * movi and shori have the format:
1471 * | op | imm | reg | reserved |
1472 * 31..26 25..10 9.. 4 3 .. 0
1474 * so we simply mask and or in imm.
1476 word = *loc & ~0x3fffc00;
1477 word |= (v & 0xffff) << 10;
1484 case R_SH_IMM_MEDLOW16_PCREL:
1485 case R_SH_IMM_LOW16_PCREL:
1489 word = *loc & ~0x3fffc00;
1493 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1496 word |= (v & 0xffff) << 10;
1502 # endif /* __SH5__ */
1504 #elif defined(__v850e__)
1510 /* We write two shorts instead of a long because even
1511 32-bit insns only need half-word alignment, but
1512 32-bit data needs to be long-word aligned. */
1513 v += ((unsigned short *)loc)[0];
1514 v += ((unsigned short *)loc)[1] << 16;
1515 ((unsigned short *)loc)[0] = v & 0xffff;
1516 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1519 case R_V850_22_PCREL:
1522 #elif defined(__x86_64__)
1532 *(unsigned int *) loc += v;
1535 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1536 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1541 *(signed int *) loc += v;
1545 *(unsigned short *) loc += v;
1549 *(unsigned char *) loc += v;
1553 *(unsigned int *) loc += v - dot;
1557 *(unsigned short *) loc += v - dot;
1561 *(unsigned char *) loc += v - dot;
1564 case R_X86_64_GLOB_DAT:
1565 case R_X86_64_JUMP_SLOT:
1569 case R_X86_64_RELATIVE:
1570 *loc += f->baseaddr;
1573 case R_X86_64_GOT32:
1574 case R_X86_64_GOTPCREL:
1577 if (!isym->gotent.reloc_done)
1579 isym->gotent.reloc_done = 1;
1580 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1582 /* XXX are these really correct? */
1583 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1584 *(unsigned int *) loc += v + isym->gotent.offset;
1586 *loc += isym->gotent.offset;
1591 # warning "no idea how to handle relocations on your arch"
1595 printf("Warning: unhandled reloc %d\n", (int)ELF_R_TYPE(rel->r_info));
1596 ret = obj_reloc_unhandled;
1599 #if defined(USE_PLT_ENTRIES)
1603 /* find the plt entry and initialize it if necessary */
1605 #if defined(USE_PLT_LIST)
1606 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1613 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1615 /* generate some machine code */
1617 #if defined(__arm__)
1618 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1619 ip[1] = v; /* sym@ */
1621 #if defined(__powerpc__)
1622 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1623 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1624 ip[2] = 0x7d6903a6; /* mtctr r11 */
1625 ip[3] = 0x4e800420; /* bctr */
1627 #if defined(__v850e__)
1628 /* We have to trash a register, so we assume that any control
1629 transfer more than 21-bits away must be a function call
1630 (so we can use a call-clobbered register). */
1631 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1632 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1637 /* relative distance to target */
1639 /* if the target is too far away.... */
1640 #if defined(__arm__) || defined(__powerpc__)
1641 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1642 #elif defined(__v850e__)
1643 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1645 /* go via the plt */
1646 v = plt + pe->offset - dot;
1648 #if defined(__v850e__)
1653 ret = obj_reloc_dangerous;
1655 /* merge the offset into the instruction. */
1656 #if defined(__arm__)
1657 /* Convert to words. */
1660 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1662 #if defined(__powerpc__)
1663 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1665 #if defined(__v850e__)
1666 /* We write two shorts instead of a long because even 32-bit insns
1667 only need half-word alignment, but the 32-bit data write needs
1668 to be long-word aligned. */
1669 ((unsigned short *)loc)[0] =
1670 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1671 | ((v >> 16) & 0x3f); /* offs high part */
1672 ((unsigned short *)loc)[1] =
1673 (v & 0xffff); /* offs low part */
1676 #endif /* USE_PLT_ENTRIES */
1678 #if defined(USE_GOT_ENTRIES)
1681 /* needs an entry in the .got: set it, once */
1682 if (!isym->gotent.inited) {
1683 isym->gotent.inited = 1;
1684 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1686 /* make the reloc with_respect_to_.got */
1688 *loc += isym->gotent.offset + rel->r_addend;
1689 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1690 *loc += isym->gotent.offset;
1694 #endif /* USE_GOT_ENTRIES */
1701 #if defined(USE_LIST)
1703 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1704 int offset, int size)
1706 struct arch_list_entry *pe;
1708 for (pe = *list; pe != NULL; pe = pe->next) {
1709 if (pe->addend == rel->r_addend) {
1715 pe = xzalloc(sizeof(struct arch_list_entry));
1717 pe->addend = rel->r_addend;
1718 pe->offset = offset;
1728 #if defined(USE_SINGLE)
1730 static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
1731 int offset, int size)
1733 if (single->allocated == 0) {
1734 single->allocated = 1;
1735 single->offset = offset;
1744 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1746 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
1747 int offset, int size)
1749 struct obj_section *myrelsec = obj_find_section(f, name);
1756 obj_extend_section(myrelsec, offset);
1758 myrelsec = obj_create_alloced_section(f, name,
1767 static void arch_create_got(struct obj_file *f)
1769 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1770 struct arch_file *ifile = (struct arch_file *) f;
1772 #if defined(USE_GOT_ENTRIES)
1773 int got_offset = 0, got_needed = 0, got_allocate;
1775 #if defined(USE_PLT_ENTRIES)
1776 int plt_offset = 0, plt_needed = 0, plt_allocate;
1778 struct obj_section *relsec, *symsec, *strsec;
1779 ElfW(RelM) *rel, *relend;
1780 ElfW(Sym) *symtab, *extsym;
1781 const char *strtab, *name;
1782 struct arch_symbol *intsym;
1784 for (i = 0; i < f->header.e_shnum; ++i) {
1785 relsec = f->sections[i];
1786 if (relsec->header.sh_type != SHT_RELM)
1789 symsec = f->sections[relsec->header.sh_link];
1790 strsec = f->sections[symsec->header.sh_link];
1792 rel = (ElfW(RelM) *) relsec->contents;
1793 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1794 symtab = (ElfW(Sym) *) symsec->contents;
1795 strtab = (const char *) strsec->contents;
1797 for (; rel < relend; ++rel) {
1798 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1800 #if defined(USE_GOT_ENTRIES)
1803 #if defined(USE_PLT_ENTRIES)
1807 switch (ELF_R_TYPE(rel->r_info)) {
1808 #if defined(__arm__)
1823 #elif defined(__i386__)
1833 #elif defined(__powerpc__)
1838 #elif defined(__mc68000__)
1849 #elif defined(__sh__)
1859 #elif defined(__v850e__)
1860 case R_V850_22_PCREL:
1869 if (extsym->st_name != 0) {
1870 name = strtab + extsym->st_name;
1872 name = f->sections[extsym->st_shndx]->name;
1874 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1875 #if defined(USE_GOT_ENTRIES)
1877 got_offset += arch_single_init(
1878 /*rel,*/ &intsym->gotent,
1879 got_offset, GOT_ENTRY_SIZE);
1884 #if defined(USE_PLT_ENTRIES)
1886 #if defined(USE_PLT_LIST)
1887 plt_offset += arch_list_add(
1888 rel, &intsym->pltent,
1889 plt_offset, PLT_ENTRY_SIZE);
1891 plt_offset += arch_single_init(
1892 /*rel,*/ &intsym->pltent,
1893 plt_offset, PLT_ENTRY_SIZE);
1901 #if defined(USE_GOT_ENTRIES)
1903 ifile->got = arch_xsect_init(f, ".got", got_offset,
1908 #if defined(USE_PLT_ENTRIES)
1910 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1915 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1918 /*======================================================================*/
1920 /* Standard ELF hash function. */
1921 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1923 unsigned long h = 0;
1930 g = (h & 0xf0000000);
1940 static unsigned long FAST_FUNC obj_elf_hash(const char *name)
1942 return obj_elf_hash_n(name, strlen(name));
1945 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1946 /* String comparison for non-co-versioned kernel and module. */
1948 static int ncv_strcmp(const char *a, const char *b)
1950 size_t alen = strlen(a), blen = strlen(b);
1952 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1953 return strncmp(a, b, alen);
1954 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1955 return strncmp(a, b, blen);
1957 return strcmp(a, b);
1960 /* String hashing for non-co-versioned kernel and module. Here
1961 we are simply forced to drop the crc from the hash. */
1963 static unsigned long FAST_FUNC ncv_symbol_hash(const char *str)
1965 size_t len = strlen(str);
1966 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1968 return obj_elf_hash_n(str, len);
1972 obj_set_symbol_compare(struct obj_file *f,
1973 int (*cmp) (const char *, const char *),
1974 unsigned long (*hash) (const char *) FAST_FUNC)
1977 f->symbol_cmp = cmp;
1979 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1982 f->symbol_hash = hash;
1984 memcpy(tmptab, f->symtab, sizeof(tmptab));
1985 memset(f->symtab, 0, sizeof(f->symtab));
1987 for (i = 0; i < HASH_BUCKETS; ++i) {
1988 for (sym = tmptab[i]; sym; sym = next) {
1989 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1991 sym->next = f->symtab[h];
1998 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2000 static struct obj_symbol *
2001 obj_add_symbol(struct obj_file *f, const char *name,
2002 unsigned long symidx, int info,
2003 int secidx, ElfW(Addr) value,
2006 struct obj_symbol *sym;
2007 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2008 int n_type = ELF_ST_TYPE(info);
2009 int n_binding = ELF_ST_BIND(info);
2011 for (sym = f->symtab[hash]; sym; sym = sym->next) {
2012 if (f->symbol_cmp(sym->name, name) == 0) {
2013 int o_secidx = sym->secidx;
2014 int o_info = sym->info;
2015 int o_type = ELF_ST_TYPE(o_info);
2016 int o_binding = ELF_ST_BIND(o_info);
2018 /* A redefinition! Is it legal? */
2020 if (secidx == SHN_UNDEF)
2022 else if (o_secidx == SHN_UNDEF)
2024 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2025 /* Cope with local and global symbols of the same name
2026 in the same object file, as might have been created
2027 by ld -r. The only reason locals are now seen at this
2028 level at all is so that we can do semi-sensible things
2031 struct obj_symbol *nsym, **p;
2033 nsym = arch_new_symbol();
2034 nsym->next = sym->next;
2037 /* Excise the old (local) symbol from the hash chain. */
2038 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2042 } else if (n_binding == STB_LOCAL) {
2043 /* Another symbol of the same name has already been defined.
2044 Just add this to the local table. */
2045 sym = arch_new_symbol();
2048 f->local_symtab[symidx] = sym;
2050 } else if (n_binding == STB_WEAK)
2052 else if (o_binding == STB_WEAK)
2054 /* Don't unify COMMON symbols with object types the programmer
2056 else if (secidx == SHN_COMMON
2057 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2059 else if (o_secidx == SHN_COMMON
2060 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2063 /* Don't report an error if the symbol is coming from
2064 the kernel or some external module. */
2065 if (secidx <= SHN_HIRESERVE)
2066 bb_error_msg("%s multiply defined", name);
2072 /* Completely new symbol. */
2073 sym = arch_new_symbol();
2074 sym->next = f->symtab[hash];
2075 f->symtab[hash] = sym;
2077 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
2078 if (symidx >= f->local_symtab_size)
2079 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2080 name, (long) symidx, (long) f->local_symtab_size);
2082 f->local_symtab[symidx] = sym;
2089 sym->secidx = secidx;
2095 static struct obj_symbol *
2096 obj_find_symbol(struct obj_file *f, const char *name)
2098 struct obj_symbol *sym;
2099 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2101 for (sym = f->symtab[hash]; sym; sym = sym->next)
2102 if (f->symbol_cmp(sym->name, name) == 0)
2107 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2110 if (sym->secidx >= SHN_LORESERVE)
2112 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2114 /* As a special case, a NULL sym has value zero. */
2118 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2120 int i, n = f->header.e_shnum;
2122 for (i = 0; i < n; ++i)
2123 if (strcmp(f->sections[i]->name, name) == 0)
2124 return f->sections[i];
2128 static int obj_load_order_prio(struct obj_section *a)
2130 unsigned long af, ac;
2132 af = a->header.sh_flags;
2135 if (a->name[0] != '.' || strlen(a->name) != 10
2136 || strcmp(a->name + 5, ".init") != 0
2142 if (!(af & SHF_WRITE))
2144 if (af & SHF_EXECINSTR)
2146 if (a->header.sh_type != SHT_NOBITS)
2153 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2155 struct obj_section **p;
2156 int prio = obj_load_order_prio(sec);
2157 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2158 if (obj_load_order_prio(*p) < prio)
2160 sec->load_next = *p;
2164 static struct obj_section *helper_create_alloced_section(struct obj_file *f,
2166 unsigned long align,
2169 int newidx = f->header.e_shnum++;
2170 struct obj_section *sec;
2172 f->sections = xrealloc_vector(f->sections, 2, newidx);
2173 f->sections[newidx] = sec = arch_new_section();
2175 sec->header.sh_type = SHT_PROGBITS;
2176 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2177 sec->header.sh_size = size;
2178 sec->header.sh_addralign = align;
2182 sec->contents = xzalloc(size);
2187 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2189 unsigned long align,
2192 struct obj_section *sec;
2194 sec = helper_create_alloced_section(f, name, align, size);
2195 obj_insert_section_load_order(f, sec);
2199 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2201 unsigned long align,
2204 struct obj_section *sec;
2206 sec = helper_create_alloced_section(f, name, align, size);
2207 sec->load_next = f->load_order;
2208 f->load_order = sec;
2209 if (f->load_order_search_start == &f->load_order)
2210 f->load_order_search_start = &sec->load_next;
2215 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2217 unsigned long oldsize = sec->header.sh_size;
2219 sec->header.sh_size += more;
2220 sec->contents = xrealloc(sec->contents, sec->header.sh_size);
2222 return sec->contents + oldsize;
2226 /* Conditionally add the symbols from the given symbol set to the
2229 static int add_symbols_from(struct obj_file *f,
2231 struct new_module_symbol *syms,
2234 struct new_module_symbol *s;
2237 #ifdef SYMBOL_PREFIX
2238 char *name_buf = NULL;
2239 size_t name_alloced_size = 0;
2241 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2244 gpl = obj_gpl_license(f, NULL) == 0;
2246 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2247 /* Only add symbols that are already marked external.
2248 If we override locals we may cause problems for
2249 argument initialization. We will also create a false
2250 dependency on the module. */
2251 struct obj_symbol *sym;
2254 /* GPL licensed modules can use symbols exported with
2255 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2256 * exported names. Non-GPL modules never see any GPLONLY_
2257 * symbols so they cannot fudge it by adding the prefix on
2260 if (is_prefixed_with((char *)s->name, "GPLONLY_")) {
2261 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2268 name = (char *)s->name;
2270 #ifdef SYMBOL_PREFIX
2271 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2272 kernel exports 'C names', but module object files
2273 reference 'linker names'). */
2274 size_t extra = sizeof SYMBOL_PREFIX;
2275 size_t name_size = strlen(name) + extra;
2276 if (name_size > name_alloced_size) {
2277 name_alloced_size = name_size * 2;
2278 name_buf = alloca(name_alloced_size);
2280 strcpy(name_buf, SYMBOL_PREFIX);
2281 strcpy(name_buf + extra - 1, name);
2285 sym = obj_find_symbol(f, name);
2286 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2287 #ifdef SYMBOL_PREFIX
2288 /* Put NAME_BUF into more permanent storage. */
2289 name = xmalloc(name_size);
2290 strcpy(name, name_buf);
2292 sym = obj_add_symbol(f, name, -1,
2293 ELF_ST_INFO(STB_GLOBAL,
2296 /* Did our symbol just get installed? If so, mark the
2297 module as "used". */
2298 if (sym->secidx == idx)
2306 static void add_kernel_symbols(struct obj_file *f)
2308 struct external_module *m;
2311 /* Add module symbols first. */
2313 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
2315 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
2322 n_ext_modules_used = nused;
2324 /* And finally the symbols from the kernel proper. */
2327 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2330 static char *get_modinfo_value(struct obj_file *f, const char *key)
2332 struct obj_section *sec;
2333 char *p, *v, *n, *ep;
2334 size_t klen = strlen(key);
2336 sec = obj_find_section(f, ".modinfo");
2340 ep = p + sec->header.sh_size;
2343 n = strchr(p, '\0');
2345 if (p + klen == v && strncmp(p, key, klen) == 0)
2348 if (p + klen == n && strcmp(p, key) == 0)
2358 /*======================================================================*/
2359 /* Functions relating to module loading after 2.1.18. */
2361 /* From Linux-2.6 sources */
2362 /* You can use " around spaces, but can't escape ". */
2363 /* Hyphens and underscores equivalent in parameter names. */
2364 static char *next_arg(char *args, char **param, char **val)
2366 unsigned int i, equals = 0;
2367 int in_quote = 0, quoted = 0;
2376 for (i = 0; args[i]; i++) {
2377 if (args[i] == ' ' && !in_quote)
2384 in_quote = !in_quote;
2391 args[equals] = '\0';
2392 *val = args + equals + 1;
2394 /* Don't include quotes in value. */
2397 if (args[i-1] == '"')
2400 if (quoted && args[i-1] == '"')
2406 next = args + i + 1;
2410 /* Chew up trailing spaces. */
2411 return skip_whitespace(next);
2415 new_process_module_arguments(struct obj_file *f, const char *options)
2417 char *xoptions, *pos;
2420 xoptions = pos = xstrdup(skip_whitespace(options));
2422 unsigned long charssize = 0;
2423 char *tmp, *contents, *loc, *pinfo, *p;
2424 struct obj_symbol *sym;
2425 int min, max, n, len;
2427 pos = next_arg(pos, ¶m, &val);
2429 tmp = xasprintf("parm_%s", param);
2430 pinfo = get_modinfo_value(f, tmp);
2433 bb_error_msg_and_die("invalid parameter %s", param);
2435 #ifdef SYMBOL_PREFIX
2436 tmp = xasprintf(SYMBOL_PREFIX "%s", param);
2437 sym = obj_find_symbol(f, tmp);
2440 sym = obj_find_symbol(f, param);
2443 /* Also check that the parameter was not resolved from the kernel. */
2444 if (sym == NULL || sym->secidx > SHN_HIRESERVE)
2445 bb_error_msg_and_die("symbol for parameter %s not found", param);
2447 /* Number of parameters */
2449 if (isdigit(*pinfo)) {
2450 min = max = strtoul(pinfo, &pinfo, 10);
2452 max = strtoul(pinfo + 1, &pinfo, 10);
2455 contents = f->sections[sym->secidx]->contents;
2456 loc = contents + sym->value;
2458 if (*pinfo == 'c') {
2459 if (!isdigit(pinfo[1])) {
2460 bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
2461 " the maximum size", param);
2463 charssize = strtoul(pinfo + 1, NULL, 10);
2468 bb_error_msg_and_die("argument expected for parameter %s", param);
2472 /* Parse parameter values */
2480 bb_error_msg_and_die("too many values for %s (max %d)", param, max);
2484 len = strcspn(p, ",");
2487 obj_string_patch(f, sym->secidx,
2489 loc += tgt_sizeof_char_p;
2494 len = strcspn(p, ",");
2497 if (len >= charssize)
2498 bb_error_msg_and_die("string too long for %s (max %ld)", param,
2500 strcpy((char *) loc, p);
2506 *loc++ = strtoul(p, &endp, 0);
2507 p = endp; /* gcc likes temp var for &endp */
2510 *(short *) loc = strtoul(p, &endp, 0);
2511 loc += tgt_sizeof_short;
2515 *(int *) loc = strtoul(p, &endp, 0);
2516 loc += tgt_sizeof_int;
2520 *(long *) loc = strtoul(p, &endp, 0);
2521 loc += tgt_sizeof_long;
2525 bb_error_msg_and_die("unknown parameter type '%c' for %s",
2529 p = skip_whitespace(p);
2532 p = skip_whitespace(p + 1);
2536 bb_error_msg_and_die("parameter %s requires at least %d arguments", param, min);
2538 bb_error_msg_and_die("invalid argument syntax for %s", param);
2544 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2545 static int new_is_module_checksummed(struct obj_file *f)
2547 const char *p = get_modinfo_value(f, "using_checksums");
2553 /* Get the module's kernel version in the canonical integer form. */
2556 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2561 p = get_modinfo_value(f, "kernel_version");
2564 safe_strncpy(str, p, STRVERSIONLEN);
2566 a = strtoul(p, &p, 10);
2569 b = strtoul(p + 1, &p, 10);
2572 c = strtoul(p + 1, &q, 10);
2576 return a << 16 | b << 8 | c;
2579 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2582 /* Fetch the loaded modules, and all currently exported symbols. */
2584 static void new_get_kernel_symbols(void)
2586 char *module_names, *mn;
2587 struct external_module *modules, *m;
2588 struct new_module_symbol *syms, *s;
2589 size_t ret, bufsize, nmod, nsyms, i, j;
2591 /* Collect the loaded modules. */
2594 module_names = xmalloc(bufsize);
2597 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2598 if (errno == ENOSPC && bufsize < ret) {
2600 module_names = xrealloc(module_names, bufsize);
2601 goto retry_modules_load;
2603 bb_perror_msg_and_die("QM_MODULES");
2606 n_ext_modules = nmod = ret;
2608 /* Collect the modules' symbols. */
2611 ext_modules = modules = xzalloc(nmod * sizeof(*modules));
2612 for (i = 0, mn = module_names, m = modules;
2613 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2614 struct new_module_info info;
2616 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2617 if (errno == ENOENT) {
2618 /* The module was removed out from underneath us. */
2621 bb_perror_msg_and_die("query_module: QM_INFO: %s", mn);
2625 syms = xmalloc(bufsize);
2627 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2631 syms = xrealloc(syms, bufsize);
2632 goto retry_mod_sym_load;
2634 /* The module was removed out from underneath us. */
2637 bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn);
2643 m->addr = info.addr;
2647 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2648 s->name += (unsigned long) syms;
2653 /* Collect the kernel's symbols. */
2655 bufsize = 16 * 1024;
2656 syms = xmalloc(bufsize);
2657 retry_kern_sym_load:
2658 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2659 if (errno == ENOSPC && bufsize < ret) {
2661 syms = xrealloc(syms, bufsize);
2662 goto retry_kern_sym_load;
2664 bb_perror_msg_and_die("kernel: QM_SYMBOLS");
2666 nksyms = nsyms = ret;
2669 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2670 s->name += (unsigned long) syms;
2675 /* Return the kernel symbol checksum version, or zero if not used. */
2677 static int new_is_kernel_checksummed(void)
2679 struct new_module_symbol *s;
2682 /* Using_Versions is not the first symbol, but it should be in there. */
2684 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2685 if (strcmp((char *) s->name, "Using_Versions") == 0)
2692 static void new_create_this_module(struct obj_file *f, const char *m_name)
2694 struct obj_section *sec;
2696 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2697 sizeof(struct new_module));
2698 /* done by obj_create_alloced_section_first: */
2699 /*memset(sec->contents, 0, sizeof(struct new_module));*/
2701 obj_add_symbol(f, SPFX "__this_module", -1,
2702 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2703 sizeof(struct new_module));
2705 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2709 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2710 /* add an entry to the __ksymtab section, creating it if necessary */
2711 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2713 struct obj_section *sec;
2716 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2717 * If __ksymtab is defined but not marked alloc, x out the first character
2718 * (no obj_delete routine) and create a new __ksymtab with the correct
2721 sec = obj_find_section(f, "__ksymtab");
2722 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2723 *((char *)(sec->name)) = 'x'; /* override const */
2727 sec = obj_create_alloced_section(f, "__ksymtab",
2728 tgt_sizeof_void_p, 0);
2731 sec->header.sh_flags |= SHF_ALLOC;
2732 /* Empty section might be byte-aligned */
2733 sec->header.sh_addralign = tgt_sizeof_void_p;
2734 ofs = sec->header.sh_size;
2735 obj_symbol_patch(f, sec->idx, ofs, sym);
2736 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2737 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2739 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2741 static int new_create_module_ksymtab(struct obj_file *f)
2743 struct obj_section *sec;
2746 /* We must always add the module references. */
2748 if (n_ext_modules_used) {
2749 struct new_module_ref *dep;
2750 struct obj_symbol *tm;
2752 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2753 (sizeof(struct new_module_ref)
2754 * n_ext_modules_used));
2758 tm = obj_find_symbol(f, SPFX "__this_module");
2759 dep = (struct new_module_ref *) sec->contents;
2760 for (i = 0; i < n_ext_modules; ++i)
2761 if (ext_modules[i].used) {
2762 dep->dep = ext_modules[i].addr;
2763 obj_symbol_patch(f, sec->idx,
2764 (char *) &dep->ref - sec->contents, tm);
2770 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2774 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2776 /* We don't want to export symbols residing in sections that
2777 aren't loaded. There are a number of these created so that
2778 we make sure certain module options don't appear twice. */
2779 i = f->header.e_shnum;
2780 loaded = alloca(sizeof(int) * i);
2782 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2784 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2785 struct obj_symbol *sym;
2786 for (sym = f->symtab[i]; sym; sym = sym->next) {
2787 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2788 && sym->secidx <= SHN_HIRESERVE
2789 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
2791 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2793 obj_symbol_patch(f, sec->idx, ofs, sym);
2794 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2801 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2809 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2811 struct new_module *module;
2812 struct obj_section *sec;
2817 sec = obj_find_section(f, ".this");
2818 if (!sec || !sec->contents) {
2819 bb_perror_msg_and_die("corrupt module %s?", m_name);
2821 module = (struct new_module *) sec->contents;
2822 m_addr = sec->header.sh_addr;
2824 module->size_of_struct = sizeof(*module);
2825 module->size = m_size;
2826 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2828 sec = obj_find_section(f, "__ksymtab");
2829 if (sec && sec->header.sh_size) {
2830 module->syms = sec->header.sh_addr;
2831 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2834 if (n_ext_modules_used) {
2835 sec = obj_find_section(f, ".kmodtab");
2836 module->deps = sec->header.sh_addr;
2837 module->ndeps = n_ext_modules_used;
2840 module->init = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2841 module->cleanup = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2843 sec = obj_find_section(f, "__ex_table");
2845 module->ex_table_start = sec->header.sh_addr;
2846 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2849 sec = obj_find_section(f, ".text.init");
2851 module->runsize = sec->header.sh_addr - m_addr;
2853 sec = obj_find_section(f, ".data.init");
2855 if (!module->runsize
2856 || module->runsize > sec->header.sh_addr - m_addr
2858 module->runsize = sec->header.sh_addr - m_addr;
2861 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2862 if (sec && sec->header.sh_size) {
2863 module->archdata_start = (void*)sec->header.sh_addr;
2864 module->archdata_end = module->archdata_start + sec->header.sh_size;
2866 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2867 if (sec && sec->header.sh_size) {
2868 module->kallsyms_start = (void*)sec->header.sh_addr;
2869 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2872 /* Whew! All of the initialization is complete. Collect the final
2873 module image and give it to the kernel. */
2875 image = xmalloc(m_size);
2876 obj_create_image(f, image);
2878 ret = init_module(m_name, (struct new_module *) image);
2880 bb_perror_msg("init_module: %s", m_name);
2888 /*======================================================================*/
2891 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2894 struct obj_string_patch *p;
2895 struct obj_section *strsec;
2896 size_t len = strlen(string) + 1;
2899 p = xzalloc(sizeof(*p));
2900 p->next = f->string_patches;
2901 p->reloc_secidx = secidx;
2902 p->reloc_offset = offset;
2903 f->string_patches = p;
2905 strsec = obj_find_section(f, ".kstrtab");
2906 if (strsec == NULL) {
2907 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2908 /*p->string_offset = 0;*/
2909 loc = strsec->contents;
2911 p->string_offset = strsec->header.sh_size;
2912 loc = obj_extend_section(strsec, len);
2914 memcpy(loc, string, len);
2918 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2919 struct obj_symbol *sym)
2921 struct obj_symbol_patch *p;
2923 p = xmalloc(sizeof(*p));
2924 p->next = f->symbol_patches;
2925 p->reloc_secidx = secidx;
2926 p->reloc_offset = offset;
2928 f->symbol_patches = p;
2931 static void obj_check_undefineds(struct obj_file *f)
2935 for (i = 0; i < HASH_BUCKETS; ++i) {
2936 struct obj_symbol *sym;
2937 for (sym = f->symtab[i]; sym; sym = sym->next) {
2938 if (sym->secidx == SHN_UNDEF) {
2939 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
2940 sym->secidx = SHN_ABS;
2944 bb_error_msg_and_die("unresolved symbol %s", sym->name);
2951 static void obj_allocate_commons(struct obj_file *f)
2953 struct common_entry {
2954 struct common_entry *next;
2955 struct obj_symbol *sym;
2956 } *common_head = NULL;
2960 for (i = 0; i < HASH_BUCKETS; ++i) {
2961 struct obj_symbol *sym;
2962 for (sym = f->symtab[i]; sym; sym = sym->next) {
2963 if (sym->secidx == SHN_COMMON) {
2964 /* Collect all COMMON symbols and sort them by size so as to
2965 minimize space wasted by alignment requirements. */
2966 struct common_entry **p, *n;
2967 for (p = &common_head; *p; p = &(*p)->next)
2968 if (sym->size <= (*p)->sym->size)
2970 n = alloca(sizeof(*n));
2978 for (i = 1; i < f->local_symtab_size; ++i) {
2979 struct obj_symbol *sym = f->local_symtab[i];
2980 if (sym && sym->secidx == SHN_COMMON) {
2981 struct common_entry **p, *n;
2982 for (p = &common_head; *p; p = &(*p)->next) {
2983 if (sym == (*p)->sym)
2985 if (sym->size < (*p)->sym->size) {
2986 n = alloca(sizeof(*n));
2997 /* Find the bss section. */
2998 for (i = 0; i < f->header.e_shnum; ++i)
2999 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3002 /* If for some reason there hadn't been one, create one. */
3003 if (i == f->header.e_shnum) {
3004 struct obj_section *sec;
3006 f->header.e_shnum++;
3007 f->sections = xrealloc_vector(f->sections, 2, i);
3008 f->sections[i] = sec = arch_new_section();
3010 sec->header.sh_type = SHT_PROGBITS;
3011 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3016 /* Allocate the COMMONS. */
3018 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3019 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3020 struct common_entry *c;
3022 for (c = common_head; c; c = c->next) {
3023 ElfW(Addr) align = c->sym->value;
3025 if (align > max_align)
3027 if (bss_size & (align - 1))
3028 bss_size = (bss_size | (align - 1)) + 1;
3031 c->sym->value = bss_size;
3033 bss_size += c->sym->size;
3036 f->sections[i]->header.sh_size = bss_size;
3037 f->sections[i]->header.sh_addralign = max_align;
3041 /* For the sake of patch relocation and parameter initialization,
3042 allocate zeroed data for NOBITS sections now. Note that after
3043 this we cannot assume NOBITS are really empty. */
3044 for (i = 0; i < f->header.e_shnum; ++i) {
3045 struct obj_section *s = f->sections[i];
3046 if (s->header.sh_type == SHT_NOBITS) {
3048 if (s->header.sh_size != 0)
3049 s->contents = xzalloc(s->header.sh_size);
3050 s->header.sh_type = SHT_PROGBITS;
3055 static unsigned long obj_load_size(struct obj_file *f)
3057 unsigned long dot = 0;
3058 struct obj_section *sec;
3060 /* Finalize the positions of the sections relative to one another. */
3062 for (sec = f->load_order; sec; sec = sec->load_next) {
3065 align = sec->header.sh_addralign;
3066 if (align && (dot & (align - 1)))
3067 dot = (dot | (align - 1)) + 1;
3069 sec->header.sh_addr = dot;
3070 dot += sec->header.sh_size;
3076 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3078 int i, n = f->header.e_shnum;
3081 /* Finalize the addresses of the sections. */
3084 for (i = 0; i < n; ++i)
3085 f->sections[i]->header.sh_addr += base;
3087 /* And iterate over all of the relocations. */
3089 for (i = 0; i < n; ++i) {
3090 struct obj_section *relsec, *symsec, *targsec, *strsec;
3091 ElfW(RelM) * rel, *relend;
3095 relsec = f->sections[i];
3096 if (relsec->header.sh_type != SHT_RELM)
3099 symsec = f->sections[relsec->header.sh_link];
3100 targsec = f->sections[relsec->header.sh_info];
3101 strsec = f->sections[symsec->header.sh_link];
3103 rel = (ElfW(RelM) *) relsec->contents;
3104 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3105 symtab = (ElfW(Sym) *) symsec->contents;
3106 strtab = (const char *) strsec->contents;
3108 for (; rel < relend; ++rel) {
3109 ElfW(Addr) value = 0;
3110 struct obj_symbol *intsym = NULL;
3111 unsigned long symndx;
3112 ElfW(Sym) *extsym = NULL;
3115 /* Attempt to find a value to use for this relocation. */
3117 symndx = ELF_R_SYM(rel->r_info);
3119 /* Note we've already checked for undefined symbols. */
3121 extsym = &symtab[symndx];
3122 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3123 /* Local symbols we look up in the local table to be sure
3124 we get the one that is really intended. */
3125 intsym = f->local_symtab[symndx];
3127 /* Others we look up in the hash table. */
3129 if (extsym->st_name)
3130 name = strtab + extsym->st_name;
3132 name = f->sections[extsym->st_shndx]->name;
3133 intsym = obj_find_symbol(f, name);
3136 value = obj_symbol_final_value(f, intsym);
3137 intsym->referenced = 1;
3139 #if SHT_RELM == SHT_RELA
3140 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3141 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3142 if (!extsym || !extsym->st_name
3143 || ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3145 value += rel->r_addend;
3149 switch (arch_apply_relocation
3150 (f, targsec, /*symsec,*/ intsym, rel, value)
3155 case obj_reloc_overflow:
3156 errmsg = "Relocation overflow";
3158 case obj_reloc_dangerous:
3159 errmsg = "Dangerous relocation";
3161 case obj_reloc_unhandled:
3162 errmsg = "Unhandled relocation";
3165 bb_error_msg("%s of type %ld for %s", errmsg,
3166 (long) ELF_R_TYPE(rel->r_info),
3167 strtab + extsym->st_name);
3169 bb_error_msg("%s of type %ld", errmsg,
3170 (long) ELF_R_TYPE(rel->r_info));
3178 /* Finally, take care of the patches. */
3180 if (f->string_patches) {
3181 struct obj_string_patch *p;
3182 struct obj_section *strsec;
3183 ElfW(Addr) strsec_base;
3184 strsec = obj_find_section(f, ".kstrtab");
3185 strsec_base = strsec->header.sh_addr;
3187 for (p = f->string_patches; p; p = p->next) {
3188 struct obj_section *targsec = f->sections[p->reloc_secidx];
3189 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3190 = strsec_base + p->string_offset;
3194 if (f->symbol_patches) {
3195 struct obj_symbol_patch *p;
3197 for (p = f->symbol_patches; p; p = p->next) {
3198 struct obj_section *targsec = f->sections[p->reloc_secidx];
3199 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3200 = obj_symbol_final_value(f, p->sym);
3207 static int obj_create_image(struct obj_file *f, char *image)
3209 struct obj_section *sec;
3210 ElfW(Addr) base = f->baseaddr;
3212 for (sec = f->load_order; sec; sec = sec->load_next) {
3215 if (sec->contents == 0 || sec->header.sh_size == 0)
3218 secimg = image + (sec->header.sh_addr - base);
3220 /* Note that we allocated data for NOBITS sections earlier. */
3221 memcpy(secimg, sec->contents, sec->header.sh_size);
3227 /*======================================================================*/
3229 static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits)
3231 typedef uint32_t aliased_uint32_t FIX_ALIASING;
3232 #if BB_LITTLE_ENDIAN
3233 # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3)))))
3235 # define ELFMAG_U32 ((uint32_t)((((ELFMAG0 * 0x100) + ELFMAG1) * 0x100 + ELFMAG2) * 0x100 + ELFMAG3))
3238 ElfW(Shdr) * section_headers;
3242 /* Read the file header. */
3244 f = arch_new_file();
3245 f->symbol_cmp = strcmp;
3246 f->symbol_hash = obj_elf_hash;
3247 f->load_order_search_start = &f->load_order;
3249 if (image_size < sizeof(f->header))
3250 bb_error_msg_and_die("error while loading ELF header");
3251 memcpy(&f->header, image, sizeof(f->header));
3253 if (*(aliased_uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
3254 bb_error_msg_and_die("not an ELF file");
3256 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3257 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB)
3258 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3259 || !MATCH_MACHINE(f->header.e_machine)
3261 bb_error_msg_and_die("ELF file not for this architecture");
3263 if (f->header.e_type != ET_REL) {
3264 bb_error_msg_and_die("ELF file not a relocatable object");
3267 /* Read the section headers. */
3269 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3270 bb_error_msg_and_die("section header size mismatch: %lu != %lu",
3271 (unsigned long) f->header.e_shentsize,
3272 (unsigned long) sizeof(ElfW(Shdr)));
3275 shnum = f->header.e_shnum;
3276 /* Growth of ->sections vector will be done by
3277 * xrealloc_vector(..., 2, ...), therefore we must allocate
3278 * at least 2^2 = 4 extra elements here. */
3279 f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
3281 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3282 if (image_size < f->header.e_shoff + sizeof(ElfW(Shdr)) * shnum)
3283 bb_error_msg_and_die("error while loading section headers");
3284 memcpy(section_headers, image + f->header.e_shoff, sizeof(ElfW(Shdr)) * shnum);
3286 /* Read the section data. */
3288 for (i = 0; i < shnum; ++i) {
3289 struct obj_section *sec;
3291 f->sections[i] = sec = arch_new_section();
3293 sec->header = section_headers[i];
3296 if (sec->header.sh_size) {
3297 switch (sec->header.sh_type) {
3305 if (!loadprogbits) {
3306 sec->contents = NULL;
3313 #if defined(__mips__)
3314 case SHT_MIPS_DWARF:
3316 sec->contents = NULL;
3317 if (sec->header.sh_size > 0) {
3318 sec->contents = xmalloc(sec->header.sh_size);
3319 if (image_size < (sec->header.sh_offset + sec->header.sh_size))
3320 bb_error_msg_and_die("error while loading section data");
3321 memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
3324 #if SHT_RELM == SHT_REL
3326 bb_error_msg_and_die("RELA relocations not supported on this architecture");
3329 bb_error_msg_and_die("REL relocations not supported on this architecture");
3332 if (sec->header.sh_type >= SHT_LOPROC) {
3333 /* Assume processor specific section types are debug
3334 info and can safely be ignored. If this is ever not
3335 the case (Hello MIPS?), don't put ifdefs here but
3336 create an arch_load_proc_section(). */
3340 bb_error_msg_and_die("can't handle sections of type %ld",
3341 (long) sec->header.sh_type);
3346 /* Do what sort of interpretation as needed by each section. */
3348 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3350 for (i = 0; i < shnum; ++i) {
3351 struct obj_section *sec = f->sections[i];
3352 sec->name = shstrtab + sec->header.sh_name;
3355 for (i = 0; i < shnum; ++i) {
3356 struct obj_section *sec = f->sections[i];
3358 /* .modinfo should be contents only but gcc has no attribute for that.
3359 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3361 if (strcmp(sec->name, ".modinfo") == 0)
3362 sec->header.sh_flags &= ~SHF_ALLOC;
3364 if (sec->header.sh_flags & SHF_ALLOC)
3365 obj_insert_section_load_order(f, sec);
3367 switch (sec->header.sh_type) {
3370 unsigned long nsym, j;
3374 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3375 bb_error_msg_and_die("symbol size mismatch: %lu != %lu",
3376 (unsigned long) sec->header.sh_entsize,
3377 (unsigned long) sizeof(ElfW(Sym)));
3380 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3381 strtab = f->sections[sec->header.sh_link]->contents;
3382 sym = (ElfW(Sym) *) sec->contents;
3384 /* Allocate space for a table of local symbols. */
3385 j = f->local_symtab_size = sec->header.sh_info;
3386 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3388 /* Insert all symbols into the hash table. */
3389 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3390 ElfW(Addr) val = sym->st_value;
3393 name = strtab + sym->st_name;
3394 else if (sym->st_shndx < shnum)
3395 name = f->sections[sym->st_shndx]->name;
3398 #if defined(__SH5__)
3400 * For sh64 it is possible that the target of a branch
3401 * requires a mode switch (32 to 16 and back again).
3403 * This is implied by the lsb being set in the target
3404 * address for SHmedia mode and clear for SHcompact.
3406 val |= sym->st_other & 4;
3408 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3415 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3416 bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu",
3417 (unsigned long) sec->header.sh_entsize,
3418 (unsigned long) sizeof(ElfW(RelM)));
3421 /* XXX Relocation code from modutils-2.3.19 is not here.
3422 * Why? That's about 20 lines of code from obj/obj_load.c,
3423 * which gets done in a second pass through the sections.
3424 * This BusyBox insmod does similar work in obj_relocate(). */
3431 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3433 * load the unloaded sections directly into the memory allocated by
3434 * kernel for the module
3437 static int obj_load_progbits(char *image, size_t image_size, struct obj_file *f, char *imagebase)
3439 ElfW(Addr) base = f->baseaddr;
3440 struct obj_section* sec;
3442 for (sec = f->load_order; sec; sec = sec->load_next) {
3443 /* section already loaded? */
3444 if (sec->contents != NULL)
3446 if (sec->header.sh_size == 0)
3448 sec->contents = imagebase + (sec->header.sh_addr - base);
3449 if (image_size < (sec->header.sh_offset + sec->header.sh_size)) {
3450 bb_error_msg("error reading ELF section data");
3451 return 0; /* need to delete half-loaded module! */
3453 memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
3459 static void hide_special_symbols(struct obj_file *f)
3461 static const char *const specials[] = {
3462 SPFX "cleanup_module",
3464 SPFX "kernel_version",
3468 struct obj_symbol *sym;
3469 const char *const *p;
3471 for (p = specials; *p; ++p) {
3472 sym = obj_find_symbol(f, *p);
3474 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3479 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3480 static int obj_gpl_license(struct obj_file *f, const char **license)
3482 struct obj_section *sec;
3483 /* This list must match *exactly* the list of allowable licenses in
3484 * linux/include/linux/module.h. Checking for leading "GPL" will not
3485 * work, somebody will use "GPL sucks, this is proprietary".
3487 static const char *const gpl_licenses[] = {
3490 "GPL and additional rights",
3495 sec = obj_find_section(f, ".modinfo");
3497 const char *value, *ptr, *endptr;
3498 ptr = sec->contents;
3499 endptr = ptr + sec->header.sh_size;
3500 while (ptr < endptr) {
3501 value = strchr(ptr, '=');
3502 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3506 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
3507 if (strcmp(value+1, gpl_licenses[i]) == 0)
3512 ptr = strchr(ptr, '\0');
3522 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3523 #define TAINT_PROPRIETORY_MODULE (1 << 0)
3524 #define TAINT_FORCED_MODULE (1 << 1)
3525 #define TAINT_UNSAFE_SMP (1 << 2)
3526 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3528 static void set_tainted(int fd, const char *m_name,
3529 int kernel_has_tainted, int taint,
3530 const char *text1, const char *text2)
3532 static smallint printed_info;
3537 if (fd < 0 && !kernel_has_tainted)
3538 return; /* New modutils on old kernel */
3539 printf("Warning: loading %s will taint the kernel: %s%s\n",
3540 m_name, text1, text2);
3541 if (!printed_info) {
3542 printf(" See %s for information about tainted modules\n", TAINT_URL);
3546 read(fd, buf, sizeof(buf)-1);
3547 buf[sizeof(buf)-1] = '\0';
3548 oldval = strtoul(buf, NULL, 10);
3549 sprintf(buf, "%d\n", oldval | taint);
3550 xwrite_str(fd, buf);
3554 /* Check if loading this module will taint the kernel. */
3555 static void check_tainted_module(struct obj_file *f, const char *m_name)
3557 int fd, kernel_has_tainted;
3560 kernel_has_tainted = 1;
3561 fd = open(TAINT_FILENAME, O_RDWR);
3563 if (errno == ENOENT)
3564 kernel_has_tainted = 0;
3565 else if (errno == EACCES)
3566 kernel_has_tainted = 1;
3568 bb_simple_perror_msg(TAINT_FILENAME);
3569 kernel_has_tainted = 0;
3573 switch (obj_gpl_license(f, &ptr)) {
3577 set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3579 default: /* case 2: */
3580 /* The module has a non-GPL license so we pretend that the
3581 * kernel always has a taint flag to get a warning even on
3582 * kernels without the proc flag.
3584 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3588 if (flag_force_load)
3589 set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3594 #else /* !FEATURE_CHECK_TAINTED_MODULE */
3595 #define check_tainted_module(x, y) do { } while (0);
3598 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3599 /* add module source, timestamp, kernel version and a symbol for the
3600 * start of some sections. this info is used by ksymoops to do better
3603 #if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3604 #define get_module_version(f, str) get_module_version(str)
3607 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3609 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3610 return new_get_module_version(f, str);
3612 strncpy(str, "???", sizeof(str));
3617 /* add module source, timestamp, kernel version and a symbol for the
3618 * start of some sections. this info is used by ksymoops to do better
3622 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3625 static const char symprefix[] ALIGN1 = "__insmod_";
3626 static const char section_names[][8] = {
3634 struct obj_section *sec;
3635 struct obj_symbol *sym;
3636 char *name, *absolute_filename;
3637 char str[STRVERSIONLEN];
3639 int lm_name, lfilename, use_ksymtab, version;
3640 struct stat statbuf;
3642 /* WARNING: was using realpath, but replaced by readlink to stop using
3643 * lots of stack. But here it seems to be able to cause problems? */
3644 absolute_filename = xmalloc_readlink(filename);
3645 if (!absolute_filename)
3646 absolute_filename = xstrdup(filename);
3648 lm_name = strlen(m_name);
3649 lfilename = strlen(absolute_filename);
3651 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3652 * are not to be exported. otherwise leave ksymtab alone for now, the
3653 * "export all symbols" compatibility code will export these symbols later.
3655 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3657 sec = obj_find_section(f, ".this");
3659 /* tag the module header with the object name, last modified
3660 * timestamp and module version. worst case for module version
3661 * is 0xffffff, decimal 16777215. putting all three fields in
3662 * one symbol is less readable but saves kernel space.
3664 if (stat(absolute_filename, &statbuf) != 0)
3665 statbuf.st_mtime = 0;
3666 version = get_module_version(f, str); /* -1 if not found */
3667 name = xasprintf("%s%s_O%s_M%0*lX_V%d",
3668 symprefix, m_name, absolute_filename,
3669 (int)(2 * sizeof(statbuf.st_mtime)),
3670 (long)statbuf.st_mtime,
3672 sym = obj_add_symbol(f, name, -1,
3673 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3674 sec->idx, sec->header.sh_addr, 0);
3676 new_add_ksymtab(f, sym);
3678 free(absolute_filename);
3679 #ifdef _NOT_SUPPORTED_
3680 /* record where the persistent data is going, same address as previous symbol */
3682 name = xasprintf("%s%s_P%s",
3683 symprefix, m_name, f->persist);
3684 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3685 sec->idx, sec->header.sh_addr, 0);
3687 new_add_ksymtab(f, sym);
3690 /* tag the desired sections if size is non-zero */
3691 for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
3692 sec = obj_find_section(f, section_names[i]);
3693 if (sec && sec->header.sh_size) {
3694 name = xasprintf("%s%s_S%s_L%ld",
3695 symprefix, m_name, sec->name,
3696 (long)sec->header.sh_size);
3697 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3698 sec->idx, sec->header.sh_addr, 0);
3700 new_add_ksymtab(f, sym);
3704 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3706 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3707 static void print_load_map(struct obj_file *f)
3709 struct obj_section *sec;
3710 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3711 struct obj_symbol **all, **p;
3713 char *loaded; /* array of booleans */
3714 struct obj_symbol *sym;
3716 /* Report on the section layout. */
3717 printf("Sections: Size %-*s Align\n",
3718 (int) (2 * sizeof(void *)), "Address");
3720 for (sec = f->load_order; sec; sec = sec->load_next) {
3724 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3729 printf("%-15s %08lx %0*lx 2**%d\n",
3731 (long)sec->header.sh_size,
3732 (int) (2 * sizeof(void *)),
3733 (long)sec->header.sh_addr,
3736 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3737 /* Quick reference which section indices are loaded. */
3738 i = f->header.e_shnum;
3739 loaded = alloca(i * sizeof(loaded[0]));
3741 loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
3743 /* Collect the symbols we'll be listing. */
3744 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3745 for (sym = f->symtab[i]; sym; sym = sym->next)
3746 if (sym->secidx <= SHN_HIRESERVE
3747 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3752 all = alloca(nsyms * sizeof(all[0]));
3754 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3755 for (sym = f->symtab[i]; sym; sym = sym->next)
3756 if (sym->secidx <= SHN_HIRESERVE
3757 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3762 /* And list them. */
3763 printf("\nSymbols:\n");
3764 for (p = all; p < all + nsyms; ++p) {
3766 unsigned long value;
3769 if (sym->secidx == SHN_ABS) {
3772 } else if (sym->secidx == SHN_UNDEF) {
3776 sec = f->sections[sym->secidx];
3778 if (sec->header.sh_type == SHT_NOBITS)
3780 else if (sec->header.sh_flags & SHF_ALLOC) {
3781 if (sec->header.sh_flags & SHF_EXECINSTR)
3783 else if (sec->header.sh_flags & SHF_WRITE)
3788 value = sym->value + sec->header.sh_addr;
3791 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3792 type |= 0x20; /* tolower. safe for '?' too */
3794 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3799 #else /* !FEATURE_INSMOD_LOAD_MAP */
3800 static void print_load_map(struct obj_file *f UNUSED_PARAM)
3805 int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
3808 unsigned long m_size;
3811 int exit_status = EXIT_FAILURE;
3813 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3820 image_size = INT_MAX - 4095;
3822 image = try_to_mmap_module(m_filename, &image_size);
3826 /* Load module into memory and unzip if compressed */
3827 image = xmalloc_open_zipped_read_close(m_filename, &image_size);
3829 return EXIT_FAILURE;
3832 m_name = xstrdup(bb_basename(m_filename));
3833 /* "module.o[.gz]" -> "module" */
3834 *strchrnul(m_name, '.') = '\0';
3836 f = obj_load(image, image_size, LOADBITS);
3838 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3839 /* Version correspondence? */
3840 m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
3842 char m_strversion[STRVERSIONLEN];
3845 if (m_has_modinfo) {
3846 int m_version = new_get_module_version(f, m_strversion);
3847 if (m_version == -1) {
3848 bb_error_msg_and_die("can't find the kernel version "
3849 "the module was compiled for");
3854 if (strncmp(uts.release, m_strversion, STRVERSIONLEN) != 0) {
3855 bb_error_msg("%skernel-module version mismatch\n"
3856 "\t%s was compiled for kernel version %s\n"
3857 "\twhile this kernel is version %s",
3858 flag_force_load ? "warning: " : "",
3859 m_name, m_strversion, uts.release);
3860 if (!flag_force_load)
3866 if (query_module(NULL, 0, NULL, 0, NULL))
3867 bb_error_msg_and_die("old (unsupported) kernel");
3868 new_get_kernel_symbols();
3869 k_crcs = new_is_kernel_checksummed();
3871 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3875 m_crcs = new_is_module_checksummed(f);
3876 if (m_crcs != k_crcs)
3877 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3881 /* Let the module know about the kernel symbols. */
3882 add_kernel_symbols(f);
3884 /* Allocate common symbols, symbol tables, and string tables. */
3885 new_create_this_module(f, m_name);
3886 obj_check_undefineds(f);
3887 obj_allocate_commons(f);
3888 check_tainted_module(f, m_name);
3890 /* Done with the module name, on to the optional var=value arguments */
3891 new_process_module_arguments(f, options);
3894 hide_special_symbols(f);
3896 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3897 add_ksymoops_symbols(f, m_filename, m_name);
3900 new_create_module_ksymtab(f);
3902 /* Find current size of the module */
3903 m_size = obj_load_size(f);
3905 m_addr = create_module(m_name, m_size);
3906 if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
3908 bb_error_msg_and_die("a module named %s already exists", m_name);
3910 bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes",
3913 bb_perror_msg_and_die("create_module: %s", m_name);
3918 * the PROGBITS section was not loaded by the obj_load
3919 * now we can load them directly into the kernel memory
3921 if (!obj_load_progbits(image, image_size, f, (char*)m_addr)) {
3922 delete_module(m_name, 0);
3927 if (!obj_relocate(f, m_addr)) {
3928 delete_module(m_name, 0);
3932 if (!new_init_module(m_name, f, m_size)) {
3933 delete_module(m_name, 0);
3937 if (flag_print_load_map)
3940 exit_status = EXIT_SUCCESS;
3944 munmap(image, image_size);