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.
64 #include <sys/utsname.h>
66 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
73 #if defined(__alpha__)
74 #define MATCH_MACHINE(x) (x == EM_ALPHA)
75 #define SHT_RELM SHT_RELA
76 #define Elf64_RelM Elf64_Rela
77 #define ELFCLASSM ELFCLASS64
82 #define MATCH_MACHINE(x) (x == EM_ARM)
83 #define SHT_RELM SHT_REL
84 #define Elf32_RelM Elf32_Rel
85 #define ELFCLASSM ELFCLASS32
86 #define USE_PLT_ENTRIES
87 #define PLT_ENTRY_SIZE 8
88 #define USE_GOT_ENTRIES
89 #define GOT_ENTRY_SIZE 8
94 #if defined(__nds32__) || defined(__NDS32__)
95 #define CONFIG_USE_GOT_ENTRIES
96 #define CONFIG_GOT_ENTRY_SIZE 4
97 #define CONFIG_USE_SINGLE
99 #if defined(__NDS32_EB__)
100 #define MATCH_MACHINE(x) (x == EM_NDS32)
101 #define SHT_RELM SHT_RELA
102 #define Elf32_RelM Elf32_Rela
103 #define ELFCLASSM ELFCLASS32
106 #if defined(__NDS32_EL__)
107 #define MATCH_MACHINE(x) (x == EM_NDS32)
108 #define SHT_RELM SHT_RELA
109 #define Elf32_RelM Elf32_Rela
110 #define ELFCLASSM ELFCLASS32
116 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
117 #define SHT_RELM SHT_RELA
118 #define Elf32_RelM Elf32_Rela
119 #define ELFCLASSM ELFCLASS32
123 #if defined(__cris__)
124 #define MATCH_MACHINE(x) (x == EM_CRIS)
125 #define SHT_RELM SHT_RELA
126 #define Elf32_RelM Elf32_Rela
127 #define ELFCLASSM ELFCLASS32
130 #define R_CRIS_NONE 0
136 #if defined(__H8300H__) || defined(__H8300S__)
137 #define MATCH_MACHINE(x) (x == EM_H8_300)
138 #define SHT_RELM SHT_RELA
139 #define Elf32_RelM Elf32_Rela
140 #define ELFCLASSM ELFCLASS32
142 #define SYMBOL_PREFIX "_"
145 /* PA-RISC / HP-PA */
146 #if defined(__hppa__)
147 #define MATCH_MACHINE(x) (x == EM_PARISC)
148 #define SHT_RELM SHT_RELA
149 #if defined(__LP64__)
150 #define Elf64_RelM Elf64_Rela
151 #define ELFCLASSM ELFCLASS64
153 #define Elf32_RelM Elf32_Rela
154 #define ELFCLASSM ELFCLASS32
159 #if defined(__i386__)
161 #define MATCH_MACHINE(x) (x == EM_386)
163 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
165 #define SHT_RELM SHT_REL
166 #define Elf32_RelM Elf32_Rel
167 #define ELFCLASSM ELFCLASS32
168 #define USE_GOT_ENTRIES
169 #define GOT_ENTRY_SIZE 4
173 /* IA64, aka Itanium */
174 #if defined(__ia64__)
175 #define MATCH_MACHINE(x) (x == EM_IA_64)
176 #define SHT_RELM SHT_RELA
177 #define Elf64_RelM Elf64_Rela
178 #define ELFCLASSM ELFCLASS64
182 #if defined(__mc68000__)
183 #define MATCH_MACHINE(x) (x == EM_68K)
184 #define SHT_RELM SHT_RELA
185 #define Elf32_RelM Elf32_Rela
186 #define ELFCLASSM ELFCLASS32
187 #define USE_GOT_ENTRIES
188 #define GOT_ENTRY_SIZE 4
193 #if defined(__microblaze__)
195 #include <linux/elf-em.h>
196 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
197 #define SHT_RELM SHT_RELA
198 #define Elf32_RelM Elf32_Rela
199 #define ELFCLASSM ELFCLASS32
203 #if defined(__mips__)
204 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
205 #define SHT_RELM SHT_REL
206 #define Elf32_RelM Elf32_Rel
207 #define ELFCLASSM ELFCLASS32
208 /* Account for ELF spec changes. */
209 #ifndef EM_MIPS_RS3_LE
210 #ifdef EM_MIPS_RS4_BE
211 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
213 #define EM_MIPS_RS3_LE 10
215 #endif /* !EM_MIPS_RS3_LE */
216 #define ARCHDATAM "__dbe_table"
220 #if defined(__nios2__)
221 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
222 #define SHT_RELM SHT_RELA
223 #define Elf32_RelM Elf32_Rela
224 #define ELFCLASSM ELFCLASS32
228 #if defined(__powerpc64__)
229 #define MATCH_MACHINE(x) (x == EM_PPC64)
230 #define SHT_RELM SHT_RELA
231 #define Elf64_RelM Elf64_Rela
232 #define ELFCLASSM ELFCLASS64
233 #elif defined(__powerpc__)
234 #define MATCH_MACHINE(x) (x == EM_PPC)
235 #define SHT_RELM SHT_RELA
236 #define Elf32_RelM Elf32_Rela
237 #define ELFCLASSM ELFCLASS32
238 #define USE_PLT_ENTRIES
239 #define PLT_ENTRY_SIZE 16
241 #define LIST_ARCHTYPE ElfW(Addr)
243 #define ARCHDATAM "__ftr_fixup"
247 #if defined(__s390__)
248 #define MATCH_MACHINE(x) (x == EM_S390)
249 #define SHT_RELM SHT_RELA
250 #define Elf32_RelM Elf32_Rela
251 #define ELFCLASSM ELFCLASS32
252 #define USE_PLT_ENTRIES
253 #define PLT_ENTRY_SIZE 8
254 #define USE_GOT_ENTRIES
255 #define GOT_ENTRY_SIZE 8
261 #define MATCH_MACHINE(x) (x == EM_SH)
262 #define SHT_RELM SHT_RELA
263 #define Elf32_RelM Elf32_Rela
264 #define ELFCLASSM ELFCLASS32
265 #define USE_GOT_ENTRIES
266 #define GOT_ENTRY_SIZE 4
268 /* the SH changes have only been tested in =little endian= mode */
269 /* I'm not sure about big endian, so let's warn: */
270 #if defined(__sh__) && BB_BIG_ENDIAN
271 # error insmod.c may require changes for use on big endian SH
273 /* it may or may not work on the SH1/SH2... Error on those also */
274 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
275 #error insmod.c may require changes for SH1 or SH2 use
280 #if defined(__sparc__)
281 #define MATCH_MACHINE(x) (x == EM_SPARC)
282 #define SHT_RELM SHT_RELA
283 #define Elf32_RelM Elf32_Rela
284 #define ELFCLASSM ELFCLASS32
288 #if defined(__v850e__)
289 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
290 #define SHT_RELM SHT_RELA
291 #define Elf32_RelM Elf32_Rela
292 #define ELFCLASSM ELFCLASS32
293 #define USE_PLT_ENTRIES
294 #define PLT_ENTRY_SIZE 8
296 #ifndef EM_CYGNUS_V850 /* grumble */
297 #define EM_CYGNUS_V850 0x9080
299 #define SYMBOL_PREFIX "_"
303 #if defined(__x86_64__)
304 #define MATCH_MACHINE(x) (x == EM_X86_64)
305 #define SHT_RELM SHT_RELA
306 #define USE_GOT_ENTRIES
307 #define GOT_ENTRY_SIZE 8
309 #define Elf64_RelM Elf64_Rela
310 #define ELFCLASSM ELFCLASS64
314 #error Sorry, but insmod.c does not yet support this architecture...
318 //----------------------------------------------------------------------------
319 //--------modutils module.h, lines 45-242
320 //----------------------------------------------------------------------------
322 /* Definitions for the Linux module syscall interface.
323 Copyright 1996, 1997 Linux International.
325 Contributed by Richard Henderson <rth@tamu.edu>
327 This file is part of the Linux modutils.
329 This program is free software; you can redistribute it and/or modify it
330 under the terms of the GNU General Public License as published by the
331 Free Software Foundation; either version 2 of the License, or (at your
332 option) any later version.
334 This program is distributed in the hope that it will be useful, but
335 WITHOUT ANY WARRANTY; without even the implied warranty of
336 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
337 General Public License for more details.
339 You should have received a copy of the GNU General Public License
340 along with this program; if not, write to the Free Software Foundation,
341 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
344 #ifndef MODUTILS_MODULE_H
346 /*======================================================================*/
347 /* For sizeof() which are related to the module platform and not to the
348 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
350 #define tgt_sizeof_char sizeof(char)
351 #define tgt_sizeof_short sizeof(short)
352 #define tgt_sizeof_int sizeof(int)
353 #define tgt_sizeof_long sizeof(long)
354 #define tgt_sizeof_char_p sizeof(char *)
355 #define tgt_sizeof_void_p sizeof(void *)
356 #define tgt_long long
358 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
359 #undef tgt_sizeof_long
360 #undef tgt_sizeof_char_p
361 #undef tgt_sizeof_void_p
365 tgt_sizeof_char_p = 8,
366 tgt_sizeof_void_p = 8
368 #define tgt_long long long
371 /*======================================================================*/
372 /* The structures used in Linux 2.1. */
374 /* Note: new_module_symbol does not use tgt_long intentionally */
375 struct new_module_symbol {
380 struct new_module_persist;
382 struct new_module_ref {
383 unsigned tgt_long dep; /* kernel addresses */
384 unsigned tgt_long ref;
385 unsigned tgt_long next_ref;
389 unsigned tgt_long size_of_struct; /* == sizeof(module) */
390 unsigned tgt_long next;
391 unsigned tgt_long name;
392 unsigned tgt_long size;
395 unsigned tgt_long flags; /* AUTOCLEAN et al */
400 unsigned tgt_long syms;
401 unsigned tgt_long deps;
402 unsigned tgt_long refs;
403 unsigned tgt_long init;
404 unsigned tgt_long cleanup;
405 unsigned tgt_long ex_table_start;
406 unsigned tgt_long ex_table_end;
408 unsigned tgt_long gp;
410 /* Everything after here is extension. */
411 unsigned tgt_long persist_start;
412 unsigned tgt_long persist_end;
413 unsigned tgt_long can_unload;
414 unsigned tgt_long runsize;
415 const char *kallsyms_start; /* All symbols for kernel debugging */
416 const char *kallsyms_end;
417 const char *archdata_start; /* arch specific data for module */
418 const char *archdata_end;
419 const char *kernel_data; /* Reserved for kernel internal use */
423 #define ARCHDATA_SEC_NAME ARCHDATAM
425 #define ARCHDATA_SEC_NAME "__archdata"
427 #define KALLSYMS_SEC_NAME "__kallsyms"
430 struct new_module_info {
437 /* Bits of module.flags. */
441 NEW_MOD_AUTOCLEAN = 4,
443 NEW_MOD_USED_ONCE = 16
446 int init_module(const char *name, const struct new_module *);
447 int query_module(const char *name, int which, void *buf,
448 size_t bufsize, size_t *ret);
450 /* Values for query_module's which. */
459 /*======================================================================*/
460 /* The system calls unchanged between 2.0 and 2.1. */
462 unsigned long create_module(const char *, size_t);
463 int delete_module(const char *module, unsigned int flags);
466 #endif /* module.h */
468 //----------------------------------------------------------------------------
469 //--------end of modutils module.h
470 //----------------------------------------------------------------------------
474 //----------------------------------------------------------------------------
475 //--------modutils obj.h, lines 253-462
476 //----------------------------------------------------------------------------
478 /* Elf object file loading and relocation routines.
479 Copyright 1996, 1997 Linux International.
481 Contributed by Richard Henderson <rth@tamu.edu>
483 This file is part of the Linux modutils.
485 This program is free software; you can redistribute it and/or modify it
486 under the terms of the GNU General Public License as published by the
487 Free Software Foundation; either version 2 of the License, or (at your
488 option) any later version.
490 This program is distributed in the hope that it will be useful, but
491 WITHOUT ANY WARRANTY; without even the implied warranty of
492 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
493 General Public License for more details.
495 You should have received a copy of the GNU General Public License
496 along with this program; if not, write to the Free Software Foundation,
497 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
500 #ifndef MODUTILS_OBJ_H
502 /* The relocatable object is manipulated using elfin types. */
508 # if ELFCLASSM == ELFCLASS32
509 # define ElfW(x) Elf32_ ## x
510 # define ELFW(x) ELF32_ ## x
512 # define ElfW(x) Elf64_ ## x
513 # define ELFW(x) ELF64_ ## x
517 /* For some reason this is missing from some ancient C libraries.... */
518 #ifndef ELF32_ST_INFO
519 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
522 #ifndef ELF64_ST_INFO
523 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
526 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
527 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
528 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
529 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
530 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
532 struct obj_string_patch;
533 struct obj_symbol_patch;
539 struct obj_section *load_next;
544 struct obj_symbol *next; /* hash table link */
548 int secidx; /* the defining section index/module */
550 int ksymidx; /* for export to the kernel symtab */
551 int referenced; /* actually used in the link */
554 /* Hardcode the hash table size. We shouldn't be needing so many
555 symbols that we begin to degrade performance, and we get a big win
556 by giving the compiler a constant divisor. */
558 #define HASH_BUCKETS 521
563 struct obj_section **sections;
564 struct obj_section *load_order;
565 struct obj_section **load_order_search_start;
566 struct obj_string_patch *string_patches;
567 struct obj_symbol_patch *symbol_patches;
568 int (*symbol_cmp)(const char *, const char *); /* cant be FAST_FUNC */
569 unsigned long (*symbol_hash)(const char *) FAST_FUNC;
570 unsigned long local_symtab_size;
571 struct obj_symbol **local_symtab;
572 struct obj_symbol *symtab[HASH_BUCKETS];
582 struct obj_string_patch {
583 struct obj_string_patch *next;
585 ElfW(Addr) reloc_offset;
586 ElfW(Addr) string_offset;
589 struct obj_symbol_patch {
590 struct obj_symbol_patch *next;
592 ElfW(Addr) reloc_offset;
593 struct obj_symbol *sym;
597 /* Generic object manipulation routines. */
599 static unsigned long FAST_FUNC obj_elf_hash(const char *);
601 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
603 static struct obj_symbol *obj_find_symbol(struct obj_file *f,
606 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
607 struct obj_symbol *sym);
609 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
610 static void obj_set_symbol_compare(struct obj_file *f,
611 int (*cmp)(const char *, const char *),
612 unsigned long (*hash)(const char *) FAST_FUNC);
615 static struct obj_section *obj_find_section(struct obj_file *f,
618 static void obj_insert_section_load_order(struct obj_file *f,
619 struct obj_section *sec);
621 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
626 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
631 static void *obj_extend_section(struct obj_section *sec, unsigned long more);
633 static void obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
636 static void obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
637 struct obj_symbol *sym);
639 static void obj_check_undefineds(struct obj_file *f);
641 static void obj_allocate_commons(struct obj_file *f);
643 static unsigned long obj_load_size(struct obj_file *f);
645 static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
648 #define obj_load(image, image_size, loadprogbits) \
649 obj_load(image, image_size)
651 static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits);
653 static int obj_create_image(struct obj_file *f, char *image);
655 /* Architecture specific manipulation routines. */
657 static struct obj_file *arch_new_file(void);
659 static struct obj_section *arch_new_section(void);
661 static struct obj_symbol *arch_new_symbol(void);
663 static enum obj_reloc arch_apply_relocation(struct obj_file *f,
664 struct obj_section *targsec,
665 /*struct obj_section *symsec,*/
666 struct obj_symbol *sym,
667 ElfW(RelM) *rel, ElfW(Addr) value);
669 static void arch_create_got(struct obj_file *f);
670 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
671 static int obj_gpl_license(struct obj_file *f, const char **license);
674 //----------------------------------------------------------------------------
675 //--------end of modutils obj.h
676 //----------------------------------------------------------------------------
679 /* SPFX is always a string, so it can be concatenated to string constants. */
681 #define SPFX SYMBOL_PREFIX
686 enum { STRVERSIONLEN = 64 };
688 /*======================================================================*/
690 #define flag_force_load (option_mask32 & INSMOD_OPT_FORCE)
691 #define flag_autoclean (option_mask32 & INSMOD_OPT_KERNELD)
692 #define flag_verbose (option_mask32 & INSMOD_OPT_VERBOSE)
693 #define flag_quiet (option_mask32 & INSMOD_OPT_SILENT)
694 #define flag_noexport (option_mask32 & INSMOD_OPT_NO_EXPORT)
695 #define flag_print_load_map (option_mask32 & INSMOD_OPT_PRINT_MAP)
697 /*======================================================================*/
699 #if defined(USE_LIST)
701 struct arch_list_entry {
702 struct arch_list_entry *next;
703 LIST_ARCHTYPE addend;
710 #if defined(USE_SINGLE)
712 struct arch_single_entry {
720 #if defined(__mips__)
722 struct mips_hi16 *next;
729 struct obj_file root;
730 #if defined(USE_PLT_ENTRIES)
731 struct obj_section *plt;
733 #if defined(USE_GOT_ENTRIES)
734 struct obj_section *got;
736 #if defined(__mips__)
737 struct mips_hi16 *mips_hi16_list;
742 struct obj_symbol root;
743 #if defined(USE_PLT_ENTRIES)
744 #if defined(USE_PLT_LIST)
745 struct arch_list_entry *pltent;
747 struct arch_single_entry pltent;
750 #if defined(USE_GOT_ENTRIES)
751 struct arch_single_entry gotent;
756 struct external_module {
761 struct new_module_symbol *syms;
764 static struct new_module_symbol *ksyms;
765 static size_t nksyms;
767 static struct external_module *ext_modules;
768 static int n_ext_modules;
769 static int n_ext_modules_used;
771 /*======================================================================*/
774 static struct obj_file *arch_new_file(void)
777 f = xzalloc(sizeof(*f));
778 return &f->root; /* it's a first member */
781 static struct obj_section *arch_new_section(void)
783 return xzalloc(sizeof(struct obj_section));
786 static struct obj_symbol *arch_new_symbol(void)
788 struct arch_symbol *sym;
789 sym = xzalloc(sizeof(*sym));
793 static enum obj_reloc
794 arch_apply_relocation(struct obj_file *f,
795 struct obj_section *targsec,
796 /*struct obj_section *symsec,*/
797 struct obj_symbol *sym,
798 ElfW(RelM) *rel, ElfW(Addr) v)
800 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
801 || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \
802 || defined(__powerpc__) || defined(__mips__)
803 struct arch_file *ifile = (struct arch_file *) f;
805 enum obj_reloc ret = obj_reloc_ok;
806 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
807 #if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
808 || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
809 || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
810 || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
811 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
813 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
814 struct arch_symbol *isym = (struct arch_symbol *) sym;
816 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
817 || defined(__sh__) || defined(__s390__)
818 #if defined(USE_GOT_ENTRIES)
819 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
822 #if defined(USE_PLT_ENTRIES)
823 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
825 # if defined(USE_PLT_LIST)
826 struct arch_list_entry *pe;
828 struct arch_single_entry *pe;
832 switch (ELF_R_TYPE(rel->r_info)) {
847 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
848 * (which is .got) similar to branch,
849 * but is full 32 bits relative */
858 case R_ARM_GOTOFF: /* address relative to the got */
862 #elif defined(__cris__)
868 /* CRIS keeps the relocation value in the r_addend field and
869 * should not use whats in *loc at all
874 #elif defined(__H8300H__) || defined(__H8300S__)
877 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
878 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
889 if ((ElfW(Sword))v > 0x7fff
890 || (ElfW(Sword))v < -(ElfW(Sword))0x8000
892 ret = obj_reloc_overflow;
894 *(unsigned short *)loc = v;
899 if ((ElfW(Sword))v > 0x7f
900 || (ElfW(Sword))v < -(ElfW(Sword))0x80
902 ret = obj_reloc_overflow;
904 *(unsigned char *)loc = v;
908 #elif defined(__i386__)
940 #elif defined(__microblaze__)
941 case R_MICROBLAZE_NONE:
942 case R_MICROBLAZE_64_NONE:
943 case R_MICROBLAZE_32_SYM_OP_SYM:
944 case R_MICROBLAZE_32_PCREL:
947 case R_MICROBLAZE_64_PCREL: {
948 /* dot is the address of the current instruction.
949 * v is the target symbol address.
950 * So we need to extract the offset in the code,
951 * adding v, then subtrating the current address
952 * of this instruction.
953 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
956 /* Get split offset stored in code */
957 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
960 /* Adjust relative offset. -4 adjustment required
961 * because dot points to the IMM insn, but branch
962 * is computed relative to the branch instruction itself.
966 /* Store back into code */
967 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
968 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
973 case R_MICROBLAZE_32:
977 case R_MICROBLAZE_64: {
978 /* Get split pointer stored in code */
979 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
982 /* Add reloc offset */
985 /* Store back into code */
986 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
987 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
992 case R_MICROBLAZE_32_PCREL_LO:
993 case R_MICROBLAZE_32_LO:
994 case R_MICROBLAZE_SRO32:
995 case R_MICROBLAZE_SRW32:
996 ret = obj_reloc_unhandled;
999 #elif defined(__mc68000__)
1010 ret = obj_reloc_overflow;
1017 ret = obj_reloc_overflow;
1024 if ((ElfW(Sword))v > 0x7f
1025 || (ElfW(Sword))v < -(ElfW(Sword))0x80
1027 ret = obj_reloc_overflow;
1034 if ((ElfW(Sword))v > 0x7fff
1035 || (ElfW(Sword))v < -(ElfW(Sword))0x8000
1037 ret = obj_reloc_overflow;
1043 *(int *)loc = v - dot;
1046 case R_68K_GLOB_DAT:
1047 case R_68K_JMP_SLOT:
1051 case R_68K_RELATIVE:
1052 *(int *)loc += f->baseaddr;
1058 # ifdef R_68K_GOTOFF
1064 #elif defined(__mips__)
1075 ret = obj_reloc_dangerous;
1076 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1077 ret = obj_reloc_overflow;
1079 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1085 struct mips_hi16 *n;
1087 /* We cannot relocate this one now because we don't know the value
1088 of the carry we need to add. Save the information, and let LO16
1089 do the actual relocation. */
1090 n = xmalloc(sizeof *n);
1093 n->next = ifile->mips_hi16_list;
1094 ifile->mips_hi16_list = n;
1100 unsigned long insnlo = *loc;
1101 ElfW(Addr) val, vallo;
1103 /* Sign extend the addend we extract from the lo insn. */
1104 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1106 if (ifile->mips_hi16_list != NULL) {
1107 struct mips_hi16 *l;
1109 l = ifile->mips_hi16_list;
1111 struct mips_hi16 *next;
1114 /* Do the HI16 relocation. Note that we actually don't
1115 need to know anything about the LO16 itself, except where
1116 to find the low 16 bits of the addend needed by the LO16. */
1119 ((insn & 0xffff) << 16) +
1123 /* Account for the sign extension that will happen in the
1130 insn = (insn & ~0xffff) | val;
1138 ifile->mips_hi16_list = NULL;
1141 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1143 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1148 #elif defined(__nios2__)
1153 case R_NIOS2_BFD_RELOC_32:
1157 case R_NIOS2_BFD_RELOC_16:
1159 ret = obj_reloc_overflow;
1164 case R_NIOS2_BFD_RELOC_8:
1166 ret = obj_reloc_overflow;
1175 if ((Elf32_Sword)v > 0x7fff
1176 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1178 ret = obj_reloc_overflow;
1182 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1192 ret = obj_reloc_overflow;
1196 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1201 case R_NIOS2_PCREL16:
1206 if ((Elf32_Sword)v > 0x7fff
1207 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1209 ret = obj_reloc_overflow;
1213 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1219 Elf32_Addr word, gp;
1221 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1223 if ((Elf32_Sword)v > 0x7fff
1224 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1226 ret = obj_reloc_overflow;
1230 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1234 case R_NIOS2_CALL26:
1236 ret = obj_reloc_dangerous;
1237 if ((v >> 28) != (dot >> 28))
1238 ret = obj_reloc_overflow;
1239 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1247 ret = obj_reloc_overflow;
1250 word = *loc & ~0x7c0;
1251 *loc = word | ((v & 0x1f) << 6);
1260 ret = obj_reloc_overflow;
1263 word = *loc & ~0xfc0;
1264 *loc = word | ((v & 0x3f) << 6);
1273 ret = obj_reloc_overflow;
1276 word = *loc & ~0x3fc0;
1277 *loc = word | ((v & 0xff) << 6);
1286 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1296 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1301 case R_NIOS2_HIADJ16:
1303 Elf32_Addr word1, word2;
1306 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1307 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1312 #elif defined(__powerpc64__)
1313 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1315 #elif defined(__powerpc__)
1317 case R_PPC_ADDR16_HA:
1318 *(unsigned short *)loc = (v + 0x8000) >> 16;
1321 case R_PPC_ADDR16_HI:
1322 *(unsigned short *)loc = v >> 16;
1325 case R_PPC_ADDR16_LO:
1326 *(unsigned short *)loc = v;
1340 #elif defined(__s390__)
1343 *(unsigned int *) loc += v;
1346 *(unsigned short *) loc += v;
1349 *(unsigned char *) loc += v;
1353 *(unsigned int *) loc += v - dot;
1356 *(unsigned short *) loc += (v - dot) >> 1;
1359 *(unsigned short *) loc += v - dot;
1363 case R_390_PLT16DBL:
1364 /* find the plt entry and initialize it. */
1365 pe = (struct arch_single_entry *) &isym->pltent;
1366 if (pe->inited == 0) {
1367 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1368 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1370 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1377 /* Insert relative distance to target. */
1378 v = plt + pe->offset - dot;
1379 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1380 *(unsigned int *) loc = (unsigned int) v;
1381 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1382 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1385 case R_390_GLOB_DAT:
1386 case R_390_JMP_SLOT:
1390 case R_390_RELATIVE:
1391 *loc += f->baseaddr;
1395 *(unsigned long *) loc += got - dot;
1401 if (!isym->gotent.inited)
1403 isym->gotent.inited = 1;
1404 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1406 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1407 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1408 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1409 *(unsigned short *) loc += isym->gotent.offset;
1410 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1411 *(unsigned int *) loc += isym->gotent.offset;
1414 # ifndef R_390_GOTOFF32
1415 # define R_390_GOTOFF32 R_390_GOTOFF
1417 case R_390_GOTOFF32:
1421 #elif defined(__sh__)
1444 *loc = f->baseaddr + rel->r_addend;
1448 *loc = got - dot + rel->r_addend;
1458 # if defined(__SH5__)
1459 case R_SH_IMM_MEDLOW16:
1460 case R_SH_IMM_LOW16:
1464 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1468 * movi and shori have the format:
1470 * | op | imm | reg | reserved |
1471 * 31..26 25..10 9.. 4 3 .. 0
1473 * so we simply mask and or in imm.
1475 word = *loc & ~0x3fffc00;
1476 word |= (v & 0xffff) << 10;
1483 case R_SH_IMM_MEDLOW16_PCREL:
1484 case R_SH_IMM_LOW16_PCREL:
1488 word = *loc & ~0x3fffc00;
1492 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1495 word |= (v & 0xffff) << 10;
1501 # endif /* __SH5__ */
1503 #elif defined(__v850e__)
1509 /* We write two shorts instead of a long because even
1510 32-bit insns only need half-word alignment, but
1511 32-bit data needs to be long-word aligned. */
1512 v += ((unsigned short *)loc)[0];
1513 v += ((unsigned short *)loc)[1] << 16;
1514 ((unsigned short *)loc)[0] = v & 0xffff;
1515 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1518 case R_V850_22_PCREL:
1521 #elif defined(__x86_64__)
1531 *(unsigned int *) loc += v;
1534 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1535 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1540 *(signed int *) loc += v;
1544 *(unsigned short *) loc += v;
1548 *(unsigned char *) loc += v;
1552 *(unsigned int *) loc += v - dot;
1556 *(unsigned short *) loc += v - dot;
1560 *(unsigned char *) loc += v - dot;
1563 case R_X86_64_GLOB_DAT:
1564 case R_X86_64_JUMP_SLOT:
1568 case R_X86_64_RELATIVE:
1569 *loc += f->baseaddr;
1572 case R_X86_64_GOT32:
1573 case R_X86_64_GOTPCREL:
1576 if (!isym->gotent.reloc_done)
1578 isym->gotent.reloc_done = 1;
1579 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1581 /* XXX are these really correct? */
1582 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1583 *(unsigned int *) loc += v + isym->gotent.offset;
1585 *loc += isym->gotent.offset;
1590 # warning "no idea how to handle relocations on your arch"
1594 printf("Warning: unhandled reloc %d\n", (int)ELF_R_TYPE(rel->r_info));
1595 ret = obj_reloc_unhandled;
1598 #if defined(USE_PLT_ENTRIES)
1602 /* find the plt entry and initialize it if necessary */
1604 #if defined(USE_PLT_LIST)
1605 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1612 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1614 /* generate some machine code */
1616 #if defined(__arm__)
1617 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1618 ip[1] = v; /* sym@ */
1620 #if defined(__powerpc__)
1621 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1622 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1623 ip[2] = 0x7d6903a6; /* mtctr r11 */
1624 ip[3] = 0x4e800420; /* bctr */
1626 #if defined(__v850e__)
1627 /* We have to trash a register, so we assume that any control
1628 transfer more than 21-bits away must be a function call
1629 (so we can use a call-clobbered register). */
1630 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1631 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1636 /* relative distance to target */
1638 /* if the target is too far away.... */
1639 #if defined(__arm__) || defined(__powerpc__)
1640 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1641 #elif defined(__v850e__)
1642 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1644 /* go via the plt */
1645 v = plt + pe->offset - dot;
1647 #if defined(__v850e__)
1652 ret = obj_reloc_dangerous;
1654 /* merge the offset into the instruction. */
1655 #if defined(__arm__)
1656 /* Convert to words. */
1659 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1661 #if defined(__powerpc__)
1662 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1664 #if defined(__v850e__)
1665 /* We write two shorts instead of a long because even 32-bit insns
1666 only need half-word alignment, but the 32-bit data write needs
1667 to be long-word aligned. */
1668 ((unsigned short *)loc)[0] =
1669 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1670 | ((v >> 16) & 0x3f); /* offs high part */
1671 ((unsigned short *)loc)[1] =
1672 (v & 0xffff); /* offs low part */
1675 #endif /* USE_PLT_ENTRIES */
1677 #if defined(USE_GOT_ENTRIES)
1680 /* needs an entry in the .got: set it, once */
1681 if (!isym->gotent.inited) {
1682 isym->gotent.inited = 1;
1683 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1685 /* make the reloc with_respect_to_.got */
1687 *loc += isym->gotent.offset + rel->r_addend;
1688 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1689 *loc += isym->gotent.offset;
1693 #endif /* USE_GOT_ENTRIES */
1700 #if defined(USE_LIST)
1702 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1703 int offset, int size)
1705 struct arch_list_entry *pe;
1707 for (pe = *list; pe != NULL; pe = pe->next) {
1708 if (pe->addend == rel->r_addend) {
1714 pe = xzalloc(sizeof(struct arch_list_entry));
1716 pe->addend = rel->r_addend;
1717 pe->offset = offset;
1727 #if defined(USE_SINGLE)
1729 static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
1730 int offset, int size)
1732 if (single->allocated == 0) {
1733 single->allocated = 1;
1734 single->offset = offset;
1743 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1745 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
1746 int offset, int size)
1748 struct obj_section *myrelsec = obj_find_section(f, name);
1755 obj_extend_section(myrelsec, offset);
1757 myrelsec = obj_create_alloced_section(f, name,
1766 static void arch_create_got(struct obj_file *f)
1768 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1769 struct arch_file *ifile = (struct arch_file *) f;
1771 #if defined(USE_GOT_ENTRIES)
1772 int got_offset = 0, got_needed = 0, got_allocate;
1774 #if defined(USE_PLT_ENTRIES)
1775 int plt_offset = 0, plt_needed = 0, plt_allocate;
1777 struct obj_section *relsec, *symsec, *strsec;
1778 ElfW(RelM) *rel, *relend;
1779 ElfW(Sym) *symtab, *extsym;
1780 const char *strtab, *name;
1781 struct arch_symbol *intsym;
1783 for (i = 0; i < f->header.e_shnum; ++i) {
1784 relsec = f->sections[i];
1785 if (relsec->header.sh_type != SHT_RELM)
1788 symsec = f->sections[relsec->header.sh_link];
1789 strsec = f->sections[symsec->header.sh_link];
1791 rel = (ElfW(RelM) *) relsec->contents;
1792 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1793 symtab = (ElfW(Sym) *) symsec->contents;
1794 strtab = (const char *) strsec->contents;
1796 for (; rel < relend; ++rel) {
1797 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1799 #if defined(USE_GOT_ENTRIES)
1802 #if defined(USE_PLT_ENTRIES)
1806 switch (ELF_R_TYPE(rel->r_info)) {
1807 #if defined(__arm__)
1822 #elif defined(__i386__)
1832 #elif defined(__powerpc__)
1837 #elif defined(__mc68000__)
1848 #elif defined(__sh__)
1858 #elif defined(__v850e__)
1859 case R_V850_22_PCREL:
1868 if (extsym->st_name != 0) {
1869 name = strtab + extsym->st_name;
1871 name = f->sections[extsym->st_shndx]->name;
1873 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1874 #if defined(USE_GOT_ENTRIES)
1876 got_offset += arch_single_init(
1877 /*rel,*/ &intsym->gotent,
1878 got_offset, GOT_ENTRY_SIZE);
1883 #if defined(USE_PLT_ENTRIES)
1885 #if defined(USE_PLT_LIST)
1886 plt_offset += arch_list_add(
1887 rel, &intsym->pltent,
1888 plt_offset, PLT_ENTRY_SIZE);
1890 plt_offset += arch_single_init(
1891 /*rel,*/ &intsym->pltent,
1892 plt_offset, PLT_ENTRY_SIZE);
1900 #if defined(USE_GOT_ENTRIES)
1902 ifile->got = arch_xsect_init(f, ".got", got_offset,
1907 #if defined(USE_PLT_ENTRIES)
1909 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1914 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1917 /*======================================================================*/
1919 /* Standard ELF hash function. */
1920 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1922 unsigned long h = 0;
1929 g = (h & 0xf0000000);
1939 static unsigned long FAST_FUNC obj_elf_hash(const char *name)
1941 return obj_elf_hash_n(name, strlen(name));
1944 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1945 /* String comparison for non-co-versioned kernel and module. */
1947 static int ncv_strcmp(const char *a, const char *b)
1949 size_t alen = strlen(a), blen = strlen(b);
1951 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1952 return strncmp(a, b, alen);
1953 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1954 return strncmp(a, b, blen);
1956 return strcmp(a, b);
1959 /* String hashing for non-co-versioned kernel and module. Here
1960 we are simply forced to drop the crc from the hash. */
1962 static unsigned long FAST_FUNC ncv_symbol_hash(const char *str)
1964 size_t len = strlen(str);
1965 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1967 return obj_elf_hash_n(str, len);
1971 obj_set_symbol_compare(struct obj_file *f,
1972 int (*cmp) (const char *, const char *),
1973 unsigned long (*hash) (const char *) FAST_FUNC)
1976 f->symbol_cmp = cmp;
1978 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1981 f->symbol_hash = hash;
1983 memcpy(tmptab, f->symtab, sizeof(tmptab));
1984 memset(f->symtab, 0, sizeof(f->symtab));
1986 for (i = 0; i < HASH_BUCKETS; ++i) {
1987 for (sym = tmptab[i]; sym; sym = next) {
1988 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1990 sym->next = f->symtab[h];
1997 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
1999 static struct obj_symbol *
2000 obj_add_symbol(struct obj_file *f, const char *name,
2001 unsigned long symidx, int info,
2002 int secidx, ElfW(Addr) value,
2005 struct obj_symbol *sym;
2006 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2007 int n_type = ELF_ST_TYPE(info);
2008 int n_binding = ELF_ST_BIND(info);
2010 for (sym = f->symtab[hash]; sym; sym = sym->next) {
2011 if (f->symbol_cmp(sym->name, name) == 0) {
2012 int o_secidx = sym->secidx;
2013 int o_info = sym->info;
2014 int o_type = ELF_ST_TYPE(o_info);
2015 int o_binding = ELF_ST_BIND(o_info);
2017 /* A redefinition! Is it legal? */
2019 if (secidx == SHN_UNDEF)
2021 else if (o_secidx == SHN_UNDEF)
2023 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2024 /* Cope with local and global symbols of the same name
2025 in the same object file, as might have been created
2026 by ld -r. The only reason locals are now seen at this
2027 level at all is so that we can do semi-sensible things
2030 struct obj_symbol *nsym, **p;
2032 nsym = arch_new_symbol();
2033 nsym->next = sym->next;
2036 /* Excise the old (local) symbol from the hash chain. */
2037 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2041 } else if (n_binding == STB_LOCAL) {
2042 /* Another symbol of the same name has already been defined.
2043 Just add this to the local table. */
2044 sym = arch_new_symbol();
2047 f->local_symtab[symidx] = sym;
2049 } else if (n_binding == STB_WEAK)
2051 else if (o_binding == STB_WEAK)
2053 /* Don't unify COMMON symbols with object types the programmer
2055 else if (secidx == SHN_COMMON
2056 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2058 else if (o_secidx == SHN_COMMON
2059 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2062 /* Don't report an error if the symbol is coming from
2063 the kernel or some external module. */
2064 if (secidx <= SHN_HIRESERVE)
2065 bb_error_msg("%s multiply defined", name);
2071 /* Completely new symbol. */
2072 sym = arch_new_symbol();
2073 sym->next = f->symtab[hash];
2074 f->symtab[hash] = sym;
2076 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
2077 if (symidx >= f->local_symtab_size)
2078 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2079 name, (long) symidx, (long) f->local_symtab_size);
2081 f->local_symtab[symidx] = sym;
2088 sym->secidx = secidx;
2094 static struct obj_symbol *
2095 obj_find_symbol(struct obj_file *f, const char *name)
2097 struct obj_symbol *sym;
2098 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2100 for (sym = f->symtab[hash]; sym; sym = sym->next)
2101 if (f->symbol_cmp(sym->name, name) == 0)
2106 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2109 if (sym->secidx >= SHN_LORESERVE)
2111 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2113 /* As a special case, a NULL sym has value zero. */
2117 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2119 int i, n = f->header.e_shnum;
2121 for (i = 0; i < n; ++i)
2122 if (strcmp(f->sections[i]->name, name) == 0)
2123 return f->sections[i];
2127 static int obj_load_order_prio(struct obj_section *a)
2129 unsigned long af, ac;
2131 af = a->header.sh_flags;
2134 if (a->name[0] != '.' || strlen(a->name) != 10
2135 || strcmp(a->name + 5, ".init") != 0
2141 if (!(af & SHF_WRITE))
2143 if (af & SHF_EXECINSTR)
2145 if (a->header.sh_type != SHT_NOBITS)
2152 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2154 struct obj_section **p;
2155 int prio = obj_load_order_prio(sec);
2156 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2157 if (obj_load_order_prio(*p) < prio)
2159 sec->load_next = *p;
2163 static struct obj_section *helper_create_alloced_section(struct obj_file *f,
2165 unsigned long align,
2168 int newidx = f->header.e_shnum++;
2169 struct obj_section *sec;
2171 f->sections = xrealloc_vector(f->sections, 2, newidx);
2172 f->sections[newidx] = sec = arch_new_section();
2174 sec->header.sh_type = SHT_PROGBITS;
2175 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2176 sec->header.sh_size = size;
2177 sec->header.sh_addralign = align;
2181 sec->contents = xzalloc(size);
2186 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2188 unsigned long align,
2191 struct obj_section *sec;
2193 sec = helper_create_alloced_section(f, name, align, size);
2194 obj_insert_section_load_order(f, sec);
2198 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2200 unsigned long align,
2203 struct obj_section *sec;
2205 sec = helper_create_alloced_section(f, name, align, size);
2206 sec->load_next = f->load_order;
2207 f->load_order = sec;
2208 if (f->load_order_search_start == &f->load_order)
2209 f->load_order_search_start = &sec->load_next;
2214 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2216 unsigned long oldsize = sec->header.sh_size;
2218 sec->header.sh_size += more;
2219 sec->contents = xrealloc(sec->contents, sec->header.sh_size);
2221 return sec->contents + oldsize;
2225 /* Conditionally add the symbols from the given symbol set to the
2228 static int add_symbols_from(struct obj_file *f,
2230 struct new_module_symbol *syms,
2233 struct new_module_symbol *s;
2236 #ifdef SYMBOL_PREFIX
2237 char *name_buf = NULL;
2238 size_t name_alloced_size = 0;
2240 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2243 gpl = obj_gpl_license(f, NULL) == 0;
2245 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2246 /* Only add symbols that are already marked external.
2247 If we override locals we may cause problems for
2248 argument initialization. We will also create a false
2249 dependency on the module. */
2250 struct obj_symbol *sym;
2253 /* GPL licensed modules can use symbols exported with
2254 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2255 * exported names. Non-GPL modules never see any GPLONLY_
2256 * symbols so they cannot fudge it by adding the prefix on
2259 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2260 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2267 name = (char *)s->name;
2269 #ifdef SYMBOL_PREFIX
2270 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2271 kernel exports `C names', but module object files
2272 reference `linker names'). */
2273 size_t extra = sizeof SYMBOL_PREFIX;
2274 size_t name_size = strlen(name) + extra;
2275 if (name_size > name_alloced_size) {
2276 name_alloced_size = name_size * 2;
2277 name_buf = alloca(name_alloced_size);
2279 strcpy(name_buf, SYMBOL_PREFIX);
2280 strcpy(name_buf + extra - 1, name);
2284 sym = obj_find_symbol(f, name);
2285 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2286 #ifdef SYMBOL_PREFIX
2287 /* Put NAME_BUF into more permanent storage. */
2288 name = xmalloc(name_size);
2289 strcpy(name, name_buf);
2291 sym = obj_add_symbol(f, name, -1,
2292 ELF_ST_INFO(STB_GLOBAL,
2295 /* Did our symbol just get installed? If so, mark the
2296 module as "used". */
2297 if (sym->secidx == idx)
2305 static void add_kernel_symbols(struct obj_file *f)
2307 struct external_module *m;
2310 /* Add module symbols first. */
2312 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
2314 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
2321 n_ext_modules_used = nused;
2323 /* And finally the symbols from the kernel proper. */
2326 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2329 static char *get_modinfo_value(struct obj_file *f, const char *key)
2331 struct obj_section *sec;
2332 char *p, *v, *n, *ep;
2333 size_t klen = strlen(key);
2335 sec = obj_find_section(f, ".modinfo");
2339 ep = p + sec->header.sh_size;
2342 n = strchr(p, '\0');
2344 if (p + klen == v && strncmp(p, key, klen) == 0)
2347 if (p + klen == n && strcmp(p, key) == 0)
2357 /*======================================================================*/
2358 /* Functions relating to module loading after 2.1.18. */
2360 /* From Linux-2.6 sources */
2361 /* You can use " around spaces, but can't escape ". */
2362 /* Hyphens and underscores equivalent in parameter names. */
2363 static char *next_arg(char *args, char **param, char **val)
2365 unsigned int i, equals = 0;
2366 int in_quote = 0, quoted = 0;
2375 for (i = 0; args[i]; i++) {
2376 if (args[i] == ' ' && !in_quote)
2383 in_quote = !in_quote;
2390 args[equals] = '\0';
2391 *val = args + equals + 1;
2393 /* Don't include quotes in value. */
2396 if (args[i-1] == '"')
2399 if (quoted && args[i-1] == '"')
2405 next = args + i + 1;
2409 /* Chew up trailing spaces. */
2410 return skip_whitespace(next);
2414 new_process_module_arguments(struct obj_file *f, const char *options)
2416 char *xoptions, *pos;
2419 xoptions = pos = xstrdup(skip_whitespace(options));
2421 unsigned long charssize = 0;
2422 char *tmp, *contents, *loc, *pinfo, *p;
2423 struct obj_symbol *sym;
2424 int min, max, n, len;
2426 pos = next_arg(pos, ¶m, &val);
2428 tmp = xasprintf("parm_%s", param);
2429 pinfo = get_modinfo_value(f, tmp);
2432 bb_error_msg_and_die("invalid parameter %s", param);
2434 #ifdef SYMBOL_PREFIX
2435 tmp = xasprintf(SYMBOL_PREFIX "%s", param);
2436 sym = obj_find_symbol(f, tmp);
2439 sym = obj_find_symbol(f, param);
2442 /* Also check that the parameter was not resolved from the kernel. */
2443 if (sym == NULL || sym->secidx > SHN_HIRESERVE)
2444 bb_error_msg_and_die("symbol for parameter %s not found", param);
2446 /* Number of parameters */
2448 if (isdigit(*pinfo)) {
2449 min = max = strtoul(pinfo, &pinfo, 10);
2451 max = strtoul(pinfo + 1, &pinfo, 10);
2454 contents = f->sections[sym->secidx]->contents;
2455 loc = contents + sym->value;
2457 if (*pinfo == 'c') {
2458 if (!isdigit(pinfo[1])) {
2459 bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
2460 " the maximum size", param);
2462 charssize = strtoul(pinfo + 1, NULL, 10);
2467 bb_error_msg_and_die("argument expected for parameter %s", param);
2471 /* Parse parameter values */
2479 bb_error_msg_and_die("too many values for %s (max %d)", param, max);
2483 len = strcspn(p, ",");
2486 obj_string_patch(f, sym->secidx,
2488 loc += tgt_sizeof_char_p;
2493 len = strcspn(p, ",");
2496 if (len >= charssize)
2497 bb_error_msg_and_die("string too long for %s (max %ld)", param,
2499 strcpy((char *) loc, p);
2505 *loc++ = strtoul(p, &endp, 0);
2506 p = endp; /* gcc likes temp var for &endp */
2509 *(short *) loc = strtoul(p, &endp, 0);
2510 loc += tgt_sizeof_short;
2514 *(int *) loc = strtoul(p, &endp, 0);
2515 loc += tgt_sizeof_int;
2519 *(long *) loc = strtoul(p, &endp, 0);
2520 loc += tgt_sizeof_long;
2524 bb_error_msg_and_die("unknown parameter type '%c' for %s",
2528 p = skip_whitespace(p);
2531 p = skip_whitespace(p + 1);
2535 bb_error_msg_and_die("parameter %s requires at least %d arguments", param, min);
2537 bb_error_msg_and_die("invalid argument syntax for %s", param);
2543 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2544 static int new_is_module_checksummed(struct obj_file *f)
2546 const char *p = get_modinfo_value(f, "using_checksums");
2552 /* Get the module's kernel version in the canonical integer form. */
2555 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2560 p = get_modinfo_value(f, "kernel_version");
2563 safe_strncpy(str, p, STRVERSIONLEN);
2565 a = strtoul(p, &p, 10);
2568 b = strtoul(p + 1, &p, 10);
2571 c = strtoul(p + 1, &q, 10);
2575 return a << 16 | b << 8 | c;
2578 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2581 /* Fetch the loaded modules, and all currently exported symbols. */
2583 static void new_get_kernel_symbols(void)
2585 char *module_names, *mn;
2586 struct external_module *modules, *m;
2587 struct new_module_symbol *syms, *s;
2588 size_t ret, bufsize, nmod, nsyms, i, j;
2590 /* Collect the loaded modules. */
2593 module_names = xmalloc(bufsize);
2596 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2597 if (errno == ENOSPC && bufsize < ret) {
2599 module_names = xrealloc(module_names, bufsize);
2600 goto retry_modules_load;
2602 bb_perror_msg_and_die("QM_MODULES");
2605 n_ext_modules = nmod = ret;
2607 /* Collect the modules' symbols. */
2610 ext_modules = modules = xzalloc(nmod * sizeof(*modules));
2611 for (i = 0, mn = module_names, m = modules;
2612 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2613 struct new_module_info info;
2615 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2616 if (errno == ENOENT) {
2617 /* The module was removed out from underneath us. */
2620 bb_perror_msg_and_die("query_module: QM_INFO: %s", mn);
2624 syms = xmalloc(bufsize);
2626 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2630 syms = xrealloc(syms, bufsize);
2631 goto retry_mod_sym_load;
2633 /* The module was removed out from underneath us. */
2636 bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn);
2642 m->addr = info.addr;
2646 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2647 s->name += (unsigned long) syms;
2652 /* Collect the kernel's symbols. */
2654 bufsize = 16 * 1024;
2655 syms = xmalloc(bufsize);
2656 retry_kern_sym_load:
2657 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2658 if (errno == ENOSPC && bufsize < ret) {
2660 syms = xrealloc(syms, bufsize);
2661 goto retry_kern_sym_load;
2663 bb_perror_msg_and_die("kernel: QM_SYMBOLS");
2665 nksyms = nsyms = ret;
2668 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2669 s->name += (unsigned long) syms;
2674 /* Return the kernel symbol checksum version, or zero if not used. */
2676 static int new_is_kernel_checksummed(void)
2678 struct new_module_symbol *s;
2681 /* Using_Versions is not the first symbol, but it should be in there. */
2683 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2684 if (strcmp((char *) s->name, "Using_Versions") == 0)
2691 static void new_create_this_module(struct obj_file *f, const char *m_name)
2693 struct obj_section *sec;
2695 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2696 sizeof(struct new_module));
2697 /* done by obj_create_alloced_section_first: */
2698 /*memset(sec->contents, 0, sizeof(struct new_module));*/
2700 obj_add_symbol(f, SPFX "__this_module", -1,
2701 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2702 sizeof(struct new_module));
2704 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2708 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2709 /* add an entry to the __ksymtab section, creating it if necessary */
2710 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2712 struct obj_section *sec;
2715 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2716 * If __ksymtab is defined but not marked alloc, x out the first character
2717 * (no obj_delete routine) and create a new __ksymtab with the correct
2720 sec = obj_find_section(f, "__ksymtab");
2721 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2722 *((char *)(sec->name)) = 'x'; /* override const */
2726 sec = obj_create_alloced_section(f, "__ksymtab",
2727 tgt_sizeof_void_p, 0);
2730 sec->header.sh_flags |= SHF_ALLOC;
2731 /* Empty section might be byte-aligned */
2732 sec->header.sh_addralign = tgt_sizeof_void_p;
2733 ofs = sec->header.sh_size;
2734 obj_symbol_patch(f, sec->idx, ofs, sym);
2735 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2736 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2738 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2740 static int new_create_module_ksymtab(struct obj_file *f)
2742 struct obj_section *sec;
2745 /* We must always add the module references. */
2747 if (n_ext_modules_used) {
2748 struct new_module_ref *dep;
2749 struct obj_symbol *tm;
2751 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2752 (sizeof(struct new_module_ref)
2753 * n_ext_modules_used));
2757 tm = obj_find_symbol(f, SPFX "__this_module");
2758 dep = (struct new_module_ref *) sec->contents;
2759 for (i = 0; i < n_ext_modules; ++i)
2760 if (ext_modules[i].used) {
2761 dep->dep = ext_modules[i].addr;
2762 obj_symbol_patch(f, sec->idx,
2763 (char *) &dep->ref - sec->contents, tm);
2769 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2773 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2775 /* We don't want to export symbols residing in sections that
2776 aren't loaded. There are a number of these created so that
2777 we make sure certain module options don't appear twice. */
2778 i = f->header.e_shnum;
2779 loaded = alloca(sizeof(int) * i);
2781 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2783 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2784 struct obj_symbol *sym;
2785 for (sym = f->symtab[i]; sym; sym = sym->next) {
2786 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2787 && sym->secidx <= SHN_HIRESERVE
2788 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
2790 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2792 obj_symbol_patch(f, sec->idx, ofs, sym);
2793 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2800 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2808 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2810 struct new_module *module;
2811 struct obj_section *sec;
2816 sec = obj_find_section(f, ".this");
2817 if (!sec || !sec->contents) {
2818 bb_perror_msg_and_die("corrupt module %s?", m_name);
2820 module = (struct new_module *) sec->contents;
2821 m_addr = sec->header.sh_addr;
2823 module->size_of_struct = sizeof(*module);
2824 module->size = m_size;
2825 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2827 sec = obj_find_section(f, "__ksymtab");
2828 if (sec && sec->header.sh_size) {
2829 module->syms = sec->header.sh_addr;
2830 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2833 if (n_ext_modules_used) {
2834 sec = obj_find_section(f, ".kmodtab");
2835 module->deps = sec->header.sh_addr;
2836 module->ndeps = n_ext_modules_used;
2839 module->init = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2840 module->cleanup = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2842 sec = obj_find_section(f, "__ex_table");
2844 module->ex_table_start = sec->header.sh_addr;
2845 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2848 sec = obj_find_section(f, ".text.init");
2850 module->runsize = sec->header.sh_addr - m_addr;
2852 sec = obj_find_section(f, ".data.init");
2854 if (!module->runsize
2855 || module->runsize > sec->header.sh_addr - m_addr
2857 module->runsize = sec->header.sh_addr - m_addr;
2860 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2861 if (sec && sec->header.sh_size) {
2862 module->archdata_start = (void*)sec->header.sh_addr;
2863 module->archdata_end = module->archdata_start + sec->header.sh_size;
2865 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2866 if (sec && sec->header.sh_size) {
2867 module->kallsyms_start = (void*)sec->header.sh_addr;
2868 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2871 /* Whew! All of the initialization is complete. Collect the final
2872 module image and give it to the kernel. */
2874 image = xmalloc(m_size);
2875 obj_create_image(f, image);
2877 ret = init_module(m_name, (struct new_module *) image);
2879 bb_perror_msg("init_module: %s", m_name);
2887 /*======================================================================*/
2890 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2893 struct obj_string_patch *p;
2894 struct obj_section *strsec;
2895 size_t len = strlen(string) + 1;
2898 p = xzalloc(sizeof(*p));
2899 p->next = f->string_patches;
2900 p->reloc_secidx = secidx;
2901 p->reloc_offset = offset;
2902 f->string_patches = p;
2904 strsec = obj_find_section(f, ".kstrtab");
2905 if (strsec == NULL) {
2906 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2907 /*p->string_offset = 0;*/
2908 loc = strsec->contents;
2910 p->string_offset = strsec->header.sh_size;
2911 loc = obj_extend_section(strsec, len);
2913 memcpy(loc, string, len);
2917 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2918 struct obj_symbol *sym)
2920 struct obj_symbol_patch *p;
2922 p = xmalloc(sizeof(*p));
2923 p->next = f->symbol_patches;
2924 p->reloc_secidx = secidx;
2925 p->reloc_offset = offset;
2927 f->symbol_patches = p;
2930 static void obj_check_undefineds(struct obj_file *f)
2934 for (i = 0; i < HASH_BUCKETS; ++i) {
2935 struct obj_symbol *sym;
2936 for (sym = f->symtab[i]; sym; sym = sym->next) {
2937 if (sym->secidx == SHN_UNDEF) {
2938 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
2939 sym->secidx = SHN_ABS;
2943 bb_error_msg_and_die("unresolved symbol %s", sym->name);
2950 static void obj_allocate_commons(struct obj_file *f)
2952 struct common_entry {
2953 struct common_entry *next;
2954 struct obj_symbol *sym;
2955 } *common_head = NULL;
2959 for (i = 0; i < HASH_BUCKETS; ++i) {
2960 struct obj_symbol *sym;
2961 for (sym = f->symtab[i]; sym; sym = sym->next) {
2962 if (sym->secidx == SHN_COMMON) {
2963 /* Collect all COMMON symbols and sort them by size so as to
2964 minimize space wasted by alignment requirements. */
2965 struct common_entry **p, *n;
2966 for (p = &common_head; *p; p = &(*p)->next)
2967 if (sym->size <= (*p)->sym->size)
2969 n = alloca(sizeof(*n));
2977 for (i = 1; i < f->local_symtab_size; ++i) {
2978 struct obj_symbol *sym = f->local_symtab[i];
2979 if (sym && sym->secidx == SHN_COMMON) {
2980 struct common_entry **p, *n;
2981 for (p = &common_head; *p; p = &(*p)->next) {
2982 if (sym == (*p)->sym)
2984 if (sym->size < (*p)->sym->size) {
2985 n = alloca(sizeof(*n));
2996 /* Find the bss section. */
2997 for (i = 0; i < f->header.e_shnum; ++i)
2998 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3001 /* If for some reason there hadn't been one, create one. */
3002 if (i == f->header.e_shnum) {
3003 struct obj_section *sec;
3005 f->header.e_shnum++;
3006 f->sections = xrealloc_vector(f->sections, 2, i);
3007 f->sections[i] = sec = arch_new_section();
3009 sec->header.sh_type = SHT_PROGBITS;
3010 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3015 /* Allocate the COMMONS. */
3017 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3018 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3019 struct common_entry *c;
3021 for (c = common_head; c; c = c->next) {
3022 ElfW(Addr) align = c->sym->value;
3024 if (align > max_align)
3026 if (bss_size & (align - 1))
3027 bss_size = (bss_size | (align - 1)) + 1;
3030 c->sym->value = bss_size;
3032 bss_size += c->sym->size;
3035 f->sections[i]->header.sh_size = bss_size;
3036 f->sections[i]->header.sh_addralign = max_align;
3040 /* For the sake of patch relocation and parameter initialization,
3041 allocate zeroed data for NOBITS sections now. Note that after
3042 this we cannot assume NOBITS are really empty. */
3043 for (i = 0; i < f->header.e_shnum; ++i) {
3044 struct obj_section *s = f->sections[i];
3045 if (s->header.sh_type == SHT_NOBITS) {
3047 if (s->header.sh_size != 0)
3048 s->contents = xzalloc(s->header.sh_size);
3049 s->header.sh_type = SHT_PROGBITS;
3054 static unsigned long obj_load_size(struct obj_file *f)
3056 unsigned long dot = 0;
3057 struct obj_section *sec;
3059 /* Finalize the positions of the sections relative to one another. */
3061 for (sec = f->load_order; sec; sec = sec->load_next) {
3064 align = sec->header.sh_addralign;
3065 if (align && (dot & (align - 1)))
3066 dot = (dot | (align - 1)) + 1;
3068 sec->header.sh_addr = dot;
3069 dot += sec->header.sh_size;
3075 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3077 int i, n = f->header.e_shnum;
3080 /* Finalize the addresses of the sections. */
3083 for (i = 0; i < n; ++i)
3084 f->sections[i]->header.sh_addr += base;
3086 /* And iterate over all of the relocations. */
3088 for (i = 0; i < n; ++i) {
3089 struct obj_section *relsec, *symsec, *targsec, *strsec;
3090 ElfW(RelM) * rel, *relend;
3094 relsec = f->sections[i];
3095 if (relsec->header.sh_type != SHT_RELM)
3098 symsec = f->sections[relsec->header.sh_link];
3099 targsec = f->sections[relsec->header.sh_info];
3100 strsec = f->sections[symsec->header.sh_link];
3102 rel = (ElfW(RelM) *) relsec->contents;
3103 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3104 symtab = (ElfW(Sym) *) symsec->contents;
3105 strtab = (const char *) strsec->contents;
3107 for (; rel < relend; ++rel) {
3108 ElfW(Addr) value = 0;
3109 struct obj_symbol *intsym = NULL;
3110 unsigned long symndx;
3111 ElfW(Sym) *extsym = NULL;
3114 /* Attempt to find a value to use for this relocation. */
3116 symndx = ELF_R_SYM(rel->r_info);
3118 /* Note we've already checked for undefined symbols. */
3120 extsym = &symtab[symndx];
3121 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3122 /* Local symbols we look up in the local table to be sure
3123 we get the one that is really intended. */
3124 intsym = f->local_symtab[symndx];
3126 /* Others we look up in the hash table. */
3128 if (extsym->st_name)
3129 name = strtab + extsym->st_name;
3131 name = f->sections[extsym->st_shndx]->name;
3132 intsym = obj_find_symbol(f, name);
3135 value = obj_symbol_final_value(f, intsym);
3136 intsym->referenced = 1;
3138 #if SHT_RELM == SHT_RELA
3139 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3140 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3141 if (!extsym || !extsym->st_name
3142 || ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3144 value += rel->r_addend;
3148 switch (arch_apply_relocation
3149 (f, targsec, /*symsec,*/ intsym, rel, value)
3154 case obj_reloc_overflow:
3155 errmsg = "Relocation overflow";
3157 case obj_reloc_dangerous:
3158 errmsg = "Dangerous relocation";
3160 case obj_reloc_unhandled:
3161 errmsg = "Unhandled relocation";
3164 bb_error_msg("%s of type %ld for %s", errmsg,
3165 (long) ELF_R_TYPE(rel->r_info),
3166 strtab + extsym->st_name);
3168 bb_error_msg("%s of type %ld", errmsg,
3169 (long) ELF_R_TYPE(rel->r_info));
3177 /* Finally, take care of the patches. */
3179 if (f->string_patches) {
3180 struct obj_string_patch *p;
3181 struct obj_section *strsec;
3182 ElfW(Addr) strsec_base;
3183 strsec = obj_find_section(f, ".kstrtab");
3184 strsec_base = strsec->header.sh_addr;
3186 for (p = f->string_patches; p; p = p->next) {
3187 struct obj_section *targsec = f->sections[p->reloc_secidx];
3188 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3189 = strsec_base + p->string_offset;
3193 if (f->symbol_patches) {
3194 struct obj_symbol_patch *p;
3196 for (p = f->symbol_patches; p; p = p->next) {
3197 struct obj_section *targsec = f->sections[p->reloc_secidx];
3198 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3199 = obj_symbol_final_value(f, p->sym);
3206 static int obj_create_image(struct obj_file *f, char *image)
3208 struct obj_section *sec;
3209 ElfW(Addr) base = f->baseaddr;
3211 for (sec = f->load_order; sec; sec = sec->load_next) {
3214 if (sec->contents == 0 || sec->header.sh_size == 0)
3217 secimg = image + (sec->header.sh_addr - base);
3219 /* Note that we allocated data for NOBITS sections earlier. */
3220 memcpy(secimg, sec->contents, sec->header.sh_size);
3226 /*======================================================================*/
3228 static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits)
3230 typedef uint32_t aliased_uint32_t FIX_ALIASING;
3231 #if BB_LITTLE_ENDIAN
3232 # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3)))))
3234 # define ELFMAG_U32 ((uint32_t)((((ELFMAG0 * 0x100) + ELFMAG1) * 0x100 + ELFMAG2) * 0x100 + ELFMAG3))
3237 ElfW(Shdr) * section_headers;
3241 /* Read the file header. */
3243 f = arch_new_file();
3244 f->symbol_cmp = strcmp;
3245 f->symbol_hash = obj_elf_hash;
3246 f->load_order_search_start = &f->load_order;
3248 if (image_size < sizeof(f->header))
3249 bb_error_msg_and_die("error while loading ELF header");
3250 memcpy(&f->header, image, sizeof(f->header));
3252 if (*(aliased_uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
3253 bb_error_msg_and_die("not an ELF file");
3255 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3256 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB)
3257 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3258 || !MATCH_MACHINE(f->header.e_machine)
3260 bb_error_msg_and_die("ELF file not for this architecture");
3262 if (f->header.e_type != ET_REL) {
3263 bb_error_msg_and_die("ELF file not a relocatable object");
3266 /* Read the section headers. */
3268 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3269 bb_error_msg_and_die("section header size mismatch: %lu != %lu",
3270 (unsigned long) f->header.e_shentsize,
3271 (unsigned long) sizeof(ElfW(Shdr)));
3274 shnum = f->header.e_shnum;
3275 /* Growth of ->sections vector will be done by
3276 * xrealloc_vector(..., 2, ...), therefore we must allocate
3277 * at least 2^2 = 4 extra elements here. */
3278 f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
3280 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3281 if (image_size < f->header.e_shoff + sizeof(ElfW(Shdr)) * shnum)
3282 bb_error_msg_and_die("error while loading section headers");
3283 memcpy(section_headers, image + f->header.e_shoff, sizeof(ElfW(Shdr)) * shnum);
3285 /* Read the section data. */
3287 for (i = 0; i < shnum; ++i) {
3288 struct obj_section *sec;
3290 f->sections[i] = sec = arch_new_section();
3292 sec->header = section_headers[i];
3295 if (sec->header.sh_size) {
3296 switch (sec->header.sh_type) {
3304 if (!loadprogbits) {
3305 sec->contents = NULL;
3312 #if defined(__mips__)
3313 case SHT_MIPS_DWARF:
3315 sec->contents = NULL;
3316 if (sec->header.sh_size > 0) {
3317 sec->contents = xmalloc(sec->header.sh_size);
3318 if (image_size < (sec->header.sh_offset + sec->header.sh_size))
3319 bb_error_msg_and_die("error while loading section data");
3320 memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
3323 #if SHT_RELM == SHT_REL
3325 bb_error_msg_and_die("RELA relocations not supported on this architecture");
3328 bb_error_msg_and_die("REL relocations not supported on this architecture");
3331 if (sec->header.sh_type >= SHT_LOPROC) {
3332 /* Assume processor specific section types are debug
3333 info and can safely be ignored. If this is ever not
3334 the case (Hello MIPS?), don't put ifdefs here but
3335 create an arch_load_proc_section(). */
3339 bb_error_msg_and_die("can't handle sections of type %ld",
3340 (long) sec->header.sh_type);
3345 /* Do what sort of interpretation as needed by each section. */
3347 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3349 for (i = 0; i < shnum; ++i) {
3350 struct obj_section *sec = f->sections[i];
3351 sec->name = shstrtab + sec->header.sh_name;
3354 for (i = 0; i < shnum; ++i) {
3355 struct obj_section *sec = f->sections[i];
3357 /* .modinfo should be contents only but gcc has no attribute for that.
3358 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3360 if (strcmp(sec->name, ".modinfo") == 0)
3361 sec->header.sh_flags &= ~SHF_ALLOC;
3363 if (sec->header.sh_flags & SHF_ALLOC)
3364 obj_insert_section_load_order(f, sec);
3366 switch (sec->header.sh_type) {
3369 unsigned long nsym, j;
3373 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3374 bb_error_msg_and_die("symbol size mismatch: %lu != %lu",
3375 (unsigned long) sec->header.sh_entsize,
3376 (unsigned long) sizeof(ElfW(Sym)));
3379 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3380 strtab = f->sections[sec->header.sh_link]->contents;
3381 sym = (ElfW(Sym) *) sec->contents;
3383 /* Allocate space for a table of local symbols. */
3384 j = f->local_symtab_size = sec->header.sh_info;
3385 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3387 /* Insert all symbols into the hash table. */
3388 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3389 ElfW(Addr) val = sym->st_value;
3392 name = strtab + sym->st_name;
3393 else if (sym->st_shndx < shnum)
3394 name = f->sections[sym->st_shndx]->name;
3397 #if defined(__SH5__)
3399 * For sh64 it is possible that the target of a branch
3400 * requires a mode switch (32 to 16 and back again).
3402 * This is implied by the lsb being set in the target
3403 * address for SHmedia mode and clear for SHcompact.
3405 val |= sym->st_other & 4;
3407 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3414 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3415 bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu",
3416 (unsigned long) sec->header.sh_entsize,
3417 (unsigned long) sizeof(ElfW(RelM)));
3420 /* XXX Relocation code from modutils-2.3.19 is not here.
3421 * Why? That's about 20 lines of code from obj/obj_load.c,
3422 * which gets done in a second pass through the sections.
3423 * This BusyBox insmod does similar work in obj_relocate(). */
3430 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3432 * load the unloaded sections directly into the memory allocated by
3433 * kernel for the module
3436 static int obj_load_progbits(char *image, size_t image_size, struct obj_file *f, char *imagebase)
3438 ElfW(Addr) base = f->baseaddr;
3439 struct obj_section* sec;
3441 for (sec = f->load_order; sec; sec = sec->load_next) {
3442 /* section already loaded? */
3443 if (sec->contents != NULL)
3445 if (sec->header.sh_size == 0)
3447 sec->contents = imagebase + (sec->header.sh_addr - base);
3448 if (image_size < (sec->header.sh_offset + sec->header.sh_size)) {
3449 bb_error_msg("error reading ELF section data");
3450 return 0; /* need to delete half-loaded module! */
3452 memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
3458 static void hide_special_symbols(struct obj_file *f)
3460 static const char *const specials[] = {
3461 SPFX "cleanup_module",
3463 SPFX "kernel_version",
3467 struct obj_symbol *sym;
3468 const char *const *p;
3470 for (p = specials; *p; ++p) {
3471 sym = obj_find_symbol(f, *p);
3473 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3478 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3479 static int obj_gpl_license(struct obj_file *f, const char **license)
3481 struct obj_section *sec;
3482 /* This list must match *exactly* the list of allowable licenses in
3483 * linux/include/linux/module.h. Checking for leading "GPL" will not
3484 * work, somebody will use "GPL sucks, this is proprietary".
3486 static const char *const gpl_licenses[] = {
3489 "GPL and additional rights",
3494 sec = obj_find_section(f, ".modinfo");
3496 const char *value, *ptr, *endptr;
3497 ptr = sec->contents;
3498 endptr = ptr + sec->header.sh_size;
3499 while (ptr < endptr) {
3500 value = strchr(ptr, '=');
3501 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3505 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
3506 if (strcmp(value+1, gpl_licenses[i]) == 0)
3511 ptr = strchr(ptr, '\0');
3521 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3522 #define TAINT_PROPRIETORY_MODULE (1 << 0)
3523 #define TAINT_FORCED_MODULE (1 << 1)
3524 #define TAINT_UNSAFE_SMP (1 << 2)
3525 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3527 static void set_tainted(int fd, const char *m_name,
3528 int kernel_has_tainted, int taint,
3529 const char *text1, const char *text2)
3531 static smallint printed_info;
3536 if (fd < 0 && !kernel_has_tainted)
3537 return; /* New modutils on old kernel */
3538 printf("Warning: loading %s will taint the kernel: %s%s\n",
3539 m_name, text1, text2);
3540 if (!printed_info) {
3541 printf(" See %s for information about tainted modules\n", TAINT_URL);
3545 read(fd, buf, sizeof(buf)-1);
3546 buf[sizeof(buf)-1] = '\0';
3547 oldval = strtoul(buf, NULL, 10);
3548 sprintf(buf, "%d\n", oldval | taint);
3549 xwrite_str(fd, buf);
3553 /* Check if loading this module will taint the kernel. */
3554 static void check_tainted_module(struct obj_file *f, const char *m_name)
3556 int fd, kernel_has_tainted;
3559 kernel_has_tainted = 1;
3560 fd = open(TAINT_FILENAME, O_RDWR);
3562 if (errno == ENOENT)
3563 kernel_has_tainted = 0;
3564 else if (errno == EACCES)
3565 kernel_has_tainted = 1;
3567 bb_simple_perror_msg(TAINT_FILENAME);
3568 kernel_has_tainted = 0;
3572 switch (obj_gpl_license(f, &ptr)) {
3576 set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3578 default: /* case 2: */
3579 /* The module has a non-GPL license so we pretend that the
3580 * kernel always has a taint flag to get a warning even on
3581 * kernels without the proc flag.
3583 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3587 if (flag_force_load)
3588 set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3593 #else /* !FEATURE_CHECK_TAINTED_MODULE */
3594 #define check_tainted_module(x, y) do { } while (0);
3597 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3598 /* add module source, timestamp, kernel version and a symbol for the
3599 * start of some sections. this info is used by ksymoops to do better
3602 #if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3603 #define get_module_version(f, str) get_module_version(str)
3606 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3608 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3609 return new_get_module_version(f, str);
3611 strncpy(str, "???", sizeof(str));
3616 /* add module source, timestamp, kernel version and a symbol for the
3617 * start of some sections. this info is used by ksymoops to do better
3621 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3624 static const char symprefix[] ALIGN1 = "__insmod_";
3625 static const char section_names[][8] = {
3633 struct obj_section *sec;
3634 struct obj_symbol *sym;
3635 char *name, *absolute_filename;
3636 char str[STRVERSIONLEN];
3638 int lm_name, lfilename, use_ksymtab, version;
3639 struct stat statbuf;
3641 /* WARNING: was using realpath, but replaced by readlink to stop using
3642 * lots of stack. But here it seems to be able to cause problems? */
3643 absolute_filename = xmalloc_readlink(filename);
3644 if (!absolute_filename)
3645 absolute_filename = xstrdup(filename);
3647 lm_name = strlen(m_name);
3648 lfilename = strlen(absolute_filename);
3650 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3651 * are not to be exported. otherwise leave ksymtab alone for now, the
3652 * "export all symbols" compatibility code will export these symbols later.
3654 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3656 sec = obj_find_section(f, ".this");
3658 /* tag the module header with the object name, last modified
3659 * timestamp and module version. worst case for module version
3660 * is 0xffffff, decimal 16777215. putting all three fields in
3661 * one symbol is less readable but saves kernel space.
3663 if (stat(absolute_filename, &statbuf) != 0)
3664 statbuf.st_mtime = 0;
3665 version = get_module_version(f, str); /* -1 if not found */
3666 name = xasprintf("%s%s_O%s_M%0*lX_V%d",
3667 symprefix, m_name, absolute_filename,
3668 (int)(2 * sizeof(statbuf.st_mtime)),
3669 (long)statbuf.st_mtime,
3671 sym = obj_add_symbol(f, name, -1,
3672 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3673 sec->idx, sec->header.sh_addr, 0);
3675 new_add_ksymtab(f, sym);
3677 free(absolute_filename);
3678 #ifdef _NOT_SUPPORTED_
3679 /* record where the persistent data is going, same address as previous symbol */
3681 name = xasprintf("%s%s_P%s",
3682 symprefix, m_name, f->persist);
3683 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3684 sec->idx, sec->header.sh_addr, 0);
3686 new_add_ksymtab(f, sym);
3689 /* tag the desired sections if size is non-zero */
3690 for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
3691 sec = obj_find_section(f, section_names[i]);
3692 if (sec && sec->header.sh_size) {
3693 name = xasprintf("%s%s_S%s_L%ld",
3694 symprefix, m_name, sec->name,
3695 (long)sec->header.sh_size);
3696 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3697 sec->idx, sec->header.sh_addr, 0);
3699 new_add_ksymtab(f, sym);
3703 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3705 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3706 static void print_load_map(struct obj_file *f)
3708 struct obj_section *sec;
3709 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3710 struct obj_symbol **all, **p;
3712 char *loaded; /* array of booleans */
3713 struct obj_symbol *sym;
3715 /* Report on the section layout. */
3716 printf("Sections: Size %-*s Align\n",
3717 (int) (2 * sizeof(void *)), "Address");
3719 for (sec = f->load_order; sec; sec = sec->load_next) {
3723 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3728 printf("%-15s %08lx %0*lx 2**%d\n",
3730 (long)sec->header.sh_size,
3731 (int) (2 * sizeof(void *)),
3732 (long)sec->header.sh_addr,
3735 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3736 /* Quick reference which section indices are loaded. */
3737 i = f->header.e_shnum;
3738 loaded = alloca(i * sizeof(loaded[0]));
3740 loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
3742 /* Collect the symbols we'll be listing. */
3743 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3744 for (sym = f->symtab[i]; sym; sym = sym->next)
3745 if (sym->secidx <= SHN_HIRESERVE
3746 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3751 all = alloca(nsyms * sizeof(all[0]));
3753 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3754 for (sym = f->symtab[i]; sym; sym = sym->next)
3755 if (sym->secidx <= SHN_HIRESERVE
3756 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3761 /* And list them. */
3762 printf("\nSymbols:\n");
3763 for (p = all; p < all + nsyms; ++p) {
3765 unsigned long value;
3768 if (sym->secidx == SHN_ABS) {
3771 } else if (sym->secidx == SHN_UNDEF) {
3775 sec = f->sections[sym->secidx];
3777 if (sec->header.sh_type == SHT_NOBITS)
3779 else if (sec->header.sh_flags & SHF_ALLOC) {
3780 if (sec->header.sh_flags & SHF_EXECINSTR)
3782 else if (sec->header.sh_flags & SHF_WRITE)
3787 value = sym->value + sec->header.sh_addr;
3790 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3791 type |= 0x20; /* tolower. safe for '?' too */
3793 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3798 #else /* !FEATURE_INSMOD_LOAD_MAP */
3799 static void print_load_map(struct obj_file *f UNUSED_PARAM)
3804 int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
3807 unsigned long m_size;
3810 int exit_status = EXIT_FAILURE;
3812 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3819 image_size = INT_MAX - 4095;
3821 image = try_to_mmap_module(m_filename, &image_size);
3825 /* Load module into memory and unzip if compressed */
3826 image = xmalloc_open_zipped_read_close(m_filename, &image_size);
3828 return EXIT_FAILURE;
3831 m_name = xstrdup(bb_basename(m_filename));
3832 /* "module.o[.gz]" -> "module" */
3833 *strchrnul(m_name, '.') = '\0';
3835 f = obj_load(image, image_size, LOADBITS);
3837 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3838 /* Version correspondence? */
3839 m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
3841 char m_strversion[STRVERSIONLEN];
3844 if (m_has_modinfo) {
3845 int m_version = new_get_module_version(f, m_strversion);
3846 if (m_version == -1) {
3847 bb_error_msg_and_die("can't find the kernel version "
3848 "the module was compiled for");
3853 if (strncmp(uts.release, m_strversion, STRVERSIONLEN) != 0) {
3854 bb_error_msg("%skernel-module version mismatch\n"
3855 "\t%s was compiled for kernel version %s\n"
3856 "\twhile this kernel is version %s",
3857 flag_force_load ? "warning: " : "",
3858 m_name, m_strversion, uts.release);
3859 if (!flag_force_load)
3865 if (query_module(NULL, 0, NULL, 0, NULL))
3866 bb_error_msg_and_die("old (unsupported) kernel");
3867 new_get_kernel_symbols();
3868 k_crcs = new_is_kernel_checksummed();
3870 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3874 m_crcs = new_is_module_checksummed(f);
3875 if (m_crcs != k_crcs)
3876 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3880 /* Let the module know about the kernel symbols. */
3881 add_kernel_symbols(f);
3883 /* Allocate common symbols, symbol tables, and string tables. */
3884 new_create_this_module(f, m_name);
3885 obj_check_undefineds(f);
3886 obj_allocate_commons(f);
3887 check_tainted_module(f, m_name);
3889 /* Done with the module name, on to the optional var=value arguments */
3890 new_process_module_arguments(f, options);
3893 hide_special_symbols(f);
3895 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3896 add_ksymoops_symbols(f, m_filename, m_name);
3899 new_create_module_ksymtab(f);
3901 /* Find current size of the module */
3902 m_size = obj_load_size(f);
3904 m_addr = create_module(m_name, m_size);
3905 if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
3907 bb_error_msg_and_die("a module named %s already exists", m_name);
3909 bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes",
3912 bb_perror_msg_and_die("create_module: %s", m_name);
3917 * the PROGBITS section was not loaded by the obj_load
3918 * now we can load them directly into the kernel memory
3920 if (!obj_load_progbits(image, image_size, f, (char*)m_addr)) {
3921 delete_module(m_name, 0);
3926 if (!obj_relocate(f, m_addr)) {
3927 delete_module(m_name, 0);
3931 if (!new_init_module(m_name, f, m_size)) {
3932 delete_module(m_name, 0);
3936 if (flag_print_load_map)
3939 exit_status = EXIT_SUCCESS;
3943 munmap(image, image_size);