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.
63 #include <sys/utsname.h>
65 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
72 #if defined(__alpha__)
73 #define MATCH_MACHINE(x) (x == EM_ALPHA)
74 #define SHT_RELM SHT_RELA
75 #define Elf64_RelM Elf64_Rela
76 #define ELFCLASSM ELFCLASS64
81 #define MATCH_MACHINE(x) (x == EM_ARM)
82 #define SHT_RELM SHT_REL
83 #define Elf32_RelM Elf32_Rel
84 #define ELFCLASSM ELFCLASS32
85 #define USE_PLT_ENTRIES
86 #define PLT_ENTRY_SIZE 8
87 #define USE_GOT_ENTRIES
88 #define GOT_ENTRY_SIZE 8
93 #if defined(__nds32__) || defined(__NDS32__)
94 #define CONFIG_USE_GOT_ENTRIES
95 #define CONFIG_GOT_ENTRY_SIZE 4
96 #define CONFIG_USE_SINGLE
98 #if defined(__NDS32_EB__)
99 #define MATCH_MACHINE(x) (x == EM_NDS32)
100 #define SHT_RELM SHT_RELA
101 #define Elf32_RelM Elf32_Rela
102 #define ELFCLASSM ELFCLASS32
105 #if defined(__NDS32_EL__)
106 #define MATCH_MACHINE(x) (x == EM_NDS32)
107 #define SHT_RELM SHT_RELA
108 #define Elf32_RelM Elf32_Rela
109 #define ELFCLASSM ELFCLASS32
115 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
116 #define SHT_RELM SHT_RELA
117 #define Elf32_RelM Elf32_Rela
118 #define ELFCLASSM ELFCLASS32
122 #if defined(__cris__)
123 #define MATCH_MACHINE(x) (x == EM_CRIS)
124 #define SHT_RELM SHT_RELA
125 #define Elf32_RelM Elf32_Rela
126 #define ELFCLASSM ELFCLASS32
129 #define R_CRIS_NONE 0
135 #if defined(__H8300H__) || defined(__H8300S__)
136 #define MATCH_MACHINE(x) (x == EM_H8_300)
137 #define SHT_RELM SHT_RELA
138 #define Elf32_RelM Elf32_Rela
139 #define ELFCLASSM ELFCLASS32
141 #define SYMBOL_PREFIX "_"
144 /* PA-RISC / HP-PA */
145 #if defined(__hppa__)
146 #define MATCH_MACHINE(x) (x == EM_PARISC)
147 #define SHT_RELM SHT_RELA
148 #if defined(__LP64__)
149 #define Elf64_RelM Elf64_Rela
150 #define ELFCLASSM ELFCLASS64
152 #define Elf32_RelM Elf32_Rela
153 #define ELFCLASSM ELFCLASS32
158 #if defined(__i386__)
160 #define MATCH_MACHINE(x) (x == EM_386)
162 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
164 #define SHT_RELM SHT_REL
165 #define Elf32_RelM Elf32_Rel
166 #define ELFCLASSM ELFCLASS32
167 #define USE_GOT_ENTRIES
168 #define GOT_ENTRY_SIZE 4
172 /* IA64, aka Itanium */
173 #if defined(__ia64__)
174 #define MATCH_MACHINE(x) (x == EM_IA_64)
175 #define SHT_RELM SHT_RELA
176 #define Elf64_RelM Elf64_Rela
177 #define ELFCLASSM ELFCLASS64
181 #if defined(__mc68000__)
182 #define MATCH_MACHINE(x) (x == EM_68K)
183 #define SHT_RELM SHT_RELA
184 #define Elf32_RelM Elf32_Rela
185 #define ELFCLASSM ELFCLASS32
186 #define USE_GOT_ENTRIES
187 #define GOT_ENTRY_SIZE 4
192 #if defined(__microblaze__)
194 #include <linux/elf-em.h>
195 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
196 #define SHT_RELM SHT_RELA
197 #define Elf32_RelM Elf32_Rela
198 #define ELFCLASSM ELFCLASS32
202 #if defined(__mips__)
203 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
204 #define SHT_RELM SHT_REL
205 #define Elf32_RelM Elf32_Rel
206 #define ELFCLASSM ELFCLASS32
207 /* Account for ELF spec changes. */
208 #ifndef EM_MIPS_RS3_LE
209 #ifdef EM_MIPS_RS4_BE
210 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
212 #define EM_MIPS_RS3_LE 10
214 #endif /* !EM_MIPS_RS3_LE */
215 #define ARCHDATAM "__dbe_table"
219 #if defined(__nios2__)
220 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
221 #define SHT_RELM SHT_RELA
222 #define Elf32_RelM Elf32_Rela
223 #define ELFCLASSM ELFCLASS32
227 #if defined(__powerpc64__)
228 #define MATCH_MACHINE(x) (x == EM_PPC64)
229 #define SHT_RELM SHT_RELA
230 #define Elf64_RelM Elf64_Rela
231 #define ELFCLASSM ELFCLASS64
232 #elif defined(__powerpc__)
233 #define MATCH_MACHINE(x) (x == EM_PPC)
234 #define SHT_RELM SHT_RELA
235 #define Elf32_RelM Elf32_Rela
236 #define ELFCLASSM ELFCLASS32
237 #define USE_PLT_ENTRIES
238 #define PLT_ENTRY_SIZE 16
240 #define LIST_ARCHTYPE ElfW(Addr)
242 #define ARCHDATAM "__ftr_fixup"
246 #if defined(__s390__)
247 #define MATCH_MACHINE(x) (x == EM_S390)
248 #define SHT_RELM SHT_RELA
249 #define Elf32_RelM Elf32_Rela
250 #define ELFCLASSM ELFCLASS32
251 #define USE_PLT_ENTRIES
252 #define PLT_ENTRY_SIZE 8
253 #define USE_GOT_ENTRIES
254 #define GOT_ENTRY_SIZE 8
260 #define MATCH_MACHINE(x) (x == EM_SH)
261 #define SHT_RELM SHT_RELA
262 #define Elf32_RelM Elf32_Rela
263 #define ELFCLASSM ELFCLASS32
264 #define USE_GOT_ENTRIES
265 #define GOT_ENTRY_SIZE 4
267 /* the SH changes have only been tested in =little endian= mode */
268 /* I'm not sure about big endian, so let's warn: */
269 #if defined(__sh__) && BB_BIG_ENDIAN
270 # error insmod.c may require changes for use on big endian SH
272 /* it may or may not work on the SH1/SH2... Error on those also */
273 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
274 #error insmod.c may require changes for SH1 or SH2 use
279 #if defined(__sparc__)
280 #define MATCH_MACHINE(x) (x == EM_SPARC)
281 #define SHT_RELM SHT_RELA
282 #define Elf32_RelM Elf32_Rela
283 #define ELFCLASSM ELFCLASS32
287 #if defined(__v850e__)
288 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
289 #define SHT_RELM SHT_RELA
290 #define Elf32_RelM Elf32_Rela
291 #define ELFCLASSM ELFCLASS32
292 #define USE_PLT_ENTRIES
293 #define PLT_ENTRY_SIZE 8
295 #ifndef EM_CYGNUS_V850 /* grumble */
296 #define EM_CYGNUS_V850 0x9080
298 #define SYMBOL_PREFIX "_"
302 #if defined(__x86_64__)
303 #define MATCH_MACHINE(x) (x == EM_X86_64)
304 #define SHT_RELM SHT_RELA
305 #define USE_GOT_ENTRIES
306 #define GOT_ENTRY_SIZE 8
308 #define Elf64_RelM Elf64_Rela
309 #define ELFCLASSM ELFCLASS64
313 #error Sorry, but insmod.c does not yet support this architecture...
317 //----------------------------------------------------------------------------
318 //--------modutils module.h, lines 45-242
319 //----------------------------------------------------------------------------
321 /* Definitions for the Linux module syscall interface.
322 Copyright 1996, 1997 Linux International.
324 Contributed by Richard Henderson <rth@tamu.edu>
326 This file is part of the Linux modutils.
328 This program is free software; you can redistribute it and/or modify it
329 under the terms of the GNU General Public License as published by the
330 Free Software Foundation; either version 2 of the License, or (at your
331 option) any later version.
333 This program is distributed in the hope that it will be useful, but
334 WITHOUT ANY WARRANTY; without even the implied warranty of
335 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
336 General Public License for more details.
338 You should have received a copy of the GNU General Public License
339 along with this program; if not, write to the Free Software Foundation,
340 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
343 #ifndef MODUTILS_MODULE_H
345 /*======================================================================*/
346 /* For sizeof() which are related to the module platform and not to the
347 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
349 #define tgt_sizeof_char sizeof(char)
350 #define tgt_sizeof_short sizeof(short)
351 #define tgt_sizeof_int sizeof(int)
352 #define tgt_sizeof_long sizeof(long)
353 #define tgt_sizeof_char_p sizeof(char *)
354 #define tgt_sizeof_void_p sizeof(void *)
355 #define tgt_long long
357 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
358 #undef tgt_sizeof_long
359 #undef tgt_sizeof_char_p
360 #undef tgt_sizeof_void_p
364 tgt_sizeof_char_p = 8,
365 tgt_sizeof_void_p = 8
367 #define tgt_long long long
370 /*======================================================================*/
371 /* The structures used in Linux 2.1. */
373 /* Note: new_module_symbol does not use tgt_long intentionally */
374 struct new_module_symbol {
379 struct new_module_persist;
381 struct new_module_ref {
382 unsigned tgt_long dep; /* kernel addresses */
383 unsigned tgt_long ref;
384 unsigned tgt_long next_ref;
388 unsigned tgt_long size_of_struct; /* == sizeof(module) */
389 unsigned tgt_long next;
390 unsigned tgt_long name;
391 unsigned tgt_long size;
394 unsigned tgt_long flags; /* AUTOCLEAN et al */
399 unsigned tgt_long syms;
400 unsigned tgt_long deps;
401 unsigned tgt_long refs;
402 unsigned tgt_long init;
403 unsigned tgt_long cleanup;
404 unsigned tgt_long ex_table_start;
405 unsigned tgt_long ex_table_end;
407 unsigned tgt_long gp;
409 /* Everything after here is extension. */
410 unsigned tgt_long persist_start;
411 unsigned tgt_long persist_end;
412 unsigned tgt_long can_unload;
413 unsigned tgt_long runsize;
414 const char *kallsyms_start; /* All symbols for kernel debugging */
415 const char *kallsyms_end;
416 const char *archdata_start; /* arch specific data for module */
417 const char *archdata_end;
418 const char *kernel_data; /* Reserved for kernel internal use */
422 #define ARCHDATA_SEC_NAME ARCHDATAM
424 #define ARCHDATA_SEC_NAME "__archdata"
426 #define KALLSYMS_SEC_NAME "__kallsyms"
429 struct new_module_info {
436 /* Bits of module.flags. */
440 NEW_MOD_AUTOCLEAN = 4,
442 NEW_MOD_USED_ONCE = 16
445 int init_module(const char *name, const struct new_module *);
446 int query_module(const char *name, int which, void *buf,
447 size_t bufsize, size_t *ret);
449 /* Values for query_module's which. */
458 /*======================================================================*/
459 /* The system calls unchanged between 2.0 and 2.1. */
461 unsigned long create_module(const char *, size_t);
462 int delete_module(const char *module, unsigned int flags);
465 #endif /* module.h */
467 //----------------------------------------------------------------------------
468 //--------end of modutils module.h
469 //----------------------------------------------------------------------------
473 //----------------------------------------------------------------------------
474 //--------modutils obj.h, lines 253-462
475 //----------------------------------------------------------------------------
477 /* Elf object file loading and relocation routines.
478 Copyright 1996, 1997 Linux International.
480 Contributed by Richard Henderson <rth@tamu.edu>
482 This file is part of the Linux modutils.
484 This program is free software; you can redistribute it and/or modify it
485 under the terms of the GNU General Public License as published by the
486 Free Software Foundation; either version 2 of the License, or (at your
487 option) any later version.
489 This program is distributed in the hope that it will be useful, but
490 WITHOUT ANY WARRANTY; without even the implied warranty of
491 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
492 General Public License for more details.
494 You should have received a copy of the GNU General Public License
495 along with this program; if not, write to the Free Software Foundation,
496 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
499 #ifndef MODUTILS_OBJ_H
501 /* The relocatable object is manipulated using elfin types. */
507 # if ELFCLASSM == ELFCLASS32
508 # define ElfW(x) Elf32_ ## x
509 # define ELFW(x) ELF32_ ## x
511 # define ElfW(x) Elf64_ ## x
512 # define ELFW(x) ELF64_ ## x
516 /* For some reason this is missing from some ancient C libraries.... */
517 #ifndef ELF32_ST_INFO
518 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
521 #ifndef ELF64_ST_INFO
522 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
525 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
526 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
527 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
528 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
529 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
531 struct obj_string_patch;
532 struct obj_symbol_patch;
538 struct obj_section *load_next;
543 struct obj_symbol *next; /* hash table link */
547 int secidx; /* the defining section index/module */
549 int ksymidx; /* for export to the kernel symtab */
550 int referenced; /* actually used in the link */
553 /* Hardcode the hash table size. We shouldn't be needing so many
554 symbols that we begin to degrade performance, and we get a big win
555 by giving the compiler a constant divisor. */
557 #define HASH_BUCKETS 521
562 struct obj_section **sections;
563 struct obj_section *load_order;
564 struct obj_section **load_order_search_start;
565 struct obj_string_patch *string_patches;
566 struct obj_symbol_patch *symbol_patches;
567 int (*symbol_cmp)(const char *, const char *); /* cant be FAST_FUNC */
568 unsigned long (*symbol_hash)(const char *) FAST_FUNC;
569 unsigned long local_symtab_size;
570 struct obj_symbol **local_symtab;
571 struct obj_symbol *symtab[HASH_BUCKETS];
581 struct obj_string_patch {
582 struct obj_string_patch *next;
584 ElfW(Addr) reloc_offset;
585 ElfW(Addr) string_offset;
588 struct obj_symbol_patch {
589 struct obj_symbol_patch *next;
591 ElfW(Addr) reloc_offset;
592 struct obj_symbol *sym;
596 /* Generic object manipulation routines. */
598 static unsigned long FAST_FUNC obj_elf_hash(const char *);
600 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
602 static struct obj_symbol *obj_find_symbol(struct obj_file *f,
605 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
606 struct obj_symbol *sym);
608 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
609 static void obj_set_symbol_compare(struct obj_file *f,
610 int (*cmp)(const char *, const char *),
611 unsigned long (*hash)(const char *) FAST_FUNC);
614 static struct obj_section *obj_find_section(struct obj_file *f,
617 static void obj_insert_section_load_order(struct obj_file *f,
618 struct obj_section *sec);
620 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
625 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
630 static void *obj_extend_section(struct obj_section *sec, unsigned long more);
632 static void obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
635 static void obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
636 struct obj_symbol *sym);
638 static void obj_check_undefineds(struct obj_file *f);
640 static void obj_allocate_commons(struct obj_file *f);
642 static unsigned long obj_load_size(struct obj_file *f);
644 static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
647 #define obj_load(image, image_size, loadprogbits) \
648 obj_load(image, image_size)
650 static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits);
652 static int obj_create_image(struct obj_file *f, char *image);
654 /* Architecture specific manipulation routines. */
656 static struct obj_file *arch_new_file(void);
658 static struct obj_section *arch_new_section(void);
660 static struct obj_symbol *arch_new_symbol(void);
662 static enum obj_reloc arch_apply_relocation(struct obj_file *f,
663 struct obj_section *targsec,
664 /*struct obj_section *symsec,*/
665 struct obj_symbol *sym,
666 ElfW(RelM) *rel, ElfW(Addr) value);
668 static void arch_create_got(struct obj_file *f);
669 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
670 static int obj_gpl_license(struct obj_file *f, const char **license);
673 //----------------------------------------------------------------------------
674 //--------end of modutils obj.h
675 //----------------------------------------------------------------------------
678 /* SPFX is always a string, so it can be concatenated to string constants. */
680 #define SPFX SYMBOL_PREFIX
685 enum { STRVERSIONLEN = 64 };
687 /*======================================================================*/
689 #define flag_force_load (option_mask32 & INSMOD_OPT_FORCE)
690 #define flag_autoclean (option_mask32 & INSMOD_OPT_KERNELD)
691 #define flag_verbose (option_mask32 & INSMOD_OPT_VERBOSE)
692 #define flag_quiet (option_mask32 & INSMOD_OPT_SILENT)
693 #define flag_noexport (option_mask32 & INSMOD_OPT_NO_EXPORT)
694 #define flag_print_load_map (option_mask32 & INSMOD_OPT_PRINT_MAP)
696 /*======================================================================*/
698 #if defined(USE_LIST)
700 struct arch_list_entry {
701 struct arch_list_entry *next;
702 LIST_ARCHTYPE addend;
709 #if defined(USE_SINGLE)
711 struct arch_single_entry {
719 #if defined(__mips__)
721 struct mips_hi16 *next;
728 struct obj_file root;
729 #if defined(USE_PLT_ENTRIES)
730 struct obj_section *plt;
732 #if defined(USE_GOT_ENTRIES)
733 struct obj_section *got;
735 #if defined(__mips__)
736 struct mips_hi16 *mips_hi16_list;
741 struct obj_symbol root;
742 #if defined(USE_PLT_ENTRIES)
743 #if defined(USE_PLT_LIST)
744 struct arch_list_entry *pltent;
746 struct arch_single_entry pltent;
749 #if defined(USE_GOT_ENTRIES)
750 struct arch_single_entry gotent;
755 struct external_module {
760 struct new_module_symbol *syms;
763 static struct new_module_symbol *ksyms;
764 static size_t nksyms;
766 static struct external_module *ext_modules;
767 static int n_ext_modules;
768 static int n_ext_modules_used;
770 /*======================================================================*/
773 static struct obj_file *arch_new_file(void)
776 f = xzalloc(sizeof(*f));
777 return &f->root; /* it's a first member */
780 static struct obj_section *arch_new_section(void)
782 return xzalloc(sizeof(struct obj_section));
785 static struct obj_symbol *arch_new_symbol(void)
787 struct arch_symbol *sym;
788 sym = xzalloc(sizeof(*sym));
792 static enum obj_reloc
793 arch_apply_relocation(struct obj_file *f,
794 struct obj_section *targsec,
795 /*struct obj_section *symsec,*/
796 struct obj_symbol *sym,
797 ElfW(RelM) *rel, ElfW(Addr) v)
799 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
800 || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \
801 || defined(__powerpc__) || defined(__mips__)
802 struct arch_file *ifile = (struct arch_file *) f;
804 enum obj_reloc ret = obj_reloc_ok;
805 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
806 #if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
807 || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
808 || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
809 || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
810 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
812 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
813 struct arch_symbol *isym = (struct arch_symbol *) sym;
815 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
816 || defined(__sh__) || defined(__s390__)
817 #if defined(USE_GOT_ENTRIES)
818 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
821 #if defined(USE_PLT_ENTRIES)
822 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
824 # if defined(USE_PLT_LIST)
825 struct arch_list_entry *pe;
827 struct arch_single_entry *pe;
831 switch (ELF_R_TYPE(rel->r_info)) {
846 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
847 * (which is .got) similar to branch,
848 * but is full 32 bits relative */
857 case R_ARM_GOTOFF: /* address relative to the got */
861 #elif defined(__cris__)
867 /* CRIS keeps the relocation value in the r_addend field and
868 * should not use whats in *loc at all
873 #elif defined(__H8300H__) || defined(__H8300S__)
876 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
877 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
888 if ((ElfW(Sword))v > 0x7fff
889 || (ElfW(Sword))v < -(ElfW(Sword))0x8000
891 ret = obj_reloc_overflow;
893 *(unsigned short *)loc = v;
898 if ((ElfW(Sword))v > 0x7f
899 || (ElfW(Sword))v < -(ElfW(Sword))0x80
901 ret = obj_reloc_overflow;
903 *(unsigned char *)loc = v;
907 #elif defined(__i386__)
939 #elif defined(__microblaze__)
940 case R_MICROBLAZE_NONE:
941 case R_MICROBLAZE_64_NONE:
942 case R_MICROBLAZE_32_SYM_OP_SYM:
943 case R_MICROBLAZE_32_PCREL:
946 case R_MICROBLAZE_64_PCREL: {
947 /* dot is the address of the current instruction.
948 * v is the target symbol address.
949 * So we need to extract the offset in the code,
950 * adding v, then subtrating the current address
951 * of this instruction.
952 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
955 /* Get split offset stored in code */
956 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
959 /* Adjust relative offset. -4 adjustment required
960 * because dot points to the IMM insn, but branch
961 * is computed relative to the branch instruction itself.
965 /* Store back into code */
966 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
967 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
972 case R_MICROBLAZE_32:
976 case R_MICROBLAZE_64: {
977 /* Get split pointer stored in code */
978 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
981 /* Add reloc offset */
984 /* Store back into code */
985 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
986 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
991 case R_MICROBLAZE_32_PCREL_LO:
992 case R_MICROBLAZE_32_LO:
993 case R_MICROBLAZE_SRO32:
994 case R_MICROBLAZE_SRW32:
995 ret = obj_reloc_unhandled;
998 #elif defined(__mc68000__)
1009 ret = obj_reloc_overflow;
1016 ret = obj_reloc_overflow;
1023 if ((ElfW(Sword))v > 0x7f
1024 || (ElfW(Sword))v < -(ElfW(Sword))0x80
1026 ret = obj_reloc_overflow;
1033 if ((ElfW(Sword))v > 0x7fff
1034 || (ElfW(Sword))v < -(ElfW(Sword))0x8000
1036 ret = obj_reloc_overflow;
1042 *(int *)loc = v - dot;
1045 case R_68K_GLOB_DAT:
1046 case R_68K_JMP_SLOT:
1050 case R_68K_RELATIVE:
1051 *(int *)loc += f->baseaddr;
1057 # ifdef R_68K_GOTOFF
1063 #elif defined(__mips__)
1074 ret = obj_reloc_dangerous;
1075 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1076 ret = obj_reloc_overflow;
1078 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1084 struct mips_hi16 *n;
1086 /* We cannot relocate this one now because we don't know the value
1087 of the carry we need to add. Save the information, and let LO16
1088 do the actual relocation. */
1089 n = xmalloc(sizeof *n);
1092 n->next = ifile->mips_hi16_list;
1093 ifile->mips_hi16_list = n;
1099 unsigned long insnlo = *loc;
1100 ElfW(Addr) val, vallo;
1102 /* Sign extend the addend we extract from the lo insn. */
1103 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1105 if (ifile->mips_hi16_list != NULL) {
1106 struct mips_hi16 *l;
1108 l = ifile->mips_hi16_list;
1110 struct mips_hi16 *next;
1113 /* Do the HI16 relocation. Note that we actually don't
1114 need to know anything about the LO16 itself, except where
1115 to find the low 16 bits of the addend needed by the LO16. */
1118 ((insn & 0xffff) << 16) +
1122 /* Account for the sign extension that will happen in the
1129 insn = (insn & ~0xffff) | val;
1137 ifile->mips_hi16_list = NULL;
1140 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1142 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1147 #elif defined(__nios2__)
1152 case R_NIOS2_BFD_RELOC_32:
1156 case R_NIOS2_BFD_RELOC_16:
1158 ret = obj_reloc_overflow;
1163 case R_NIOS2_BFD_RELOC_8:
1165 ret = obj_reloc_overflow;
1174 if ((Elf32_Sword)v > 0x7fff
1175 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1177 ret = obj_reloc_overflow;
1181 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1191 ret = obj_reloc_overflow;
1195 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1200 case R_NIOS2_PCREL16:
1205 if ((Elf32_Sword)v > 0x7fff
1206 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1208 ret = obj_reloc_overflow;
1212 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1218 Elf32_Addr word, gp;
1220 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1222 if ((Elf32_Sword)v > 0x7fff
1223 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1225 ret = obj_reloc_overflow;
1229 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1233 case R_NIOS2_CALL26:
1235 ret = obj_reloc_dangerous;
1236 if ((v >> 28) != (dot >> 28))
1237 ret = obj_reloc_overflow;
1238 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1246 ret = obj_reloc_overflow;
1249 word = *loc & ~0x7c0;
1250 *loc = word | ((v & 0x1f) << 6);
1259 ret = obj_reloc_overflow;
1262 word = *loc & ~0xfc0;
1263 *loc = word | ((v & 0x3f) << 6);
1272 ret = obj_reloc_overflow;
1275 word = *loc & ~0x3fc0;
1276 *loc = word | ((v & 0xff) << 6);
1285 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1295 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1300 case R_NIOS2_HIADJ16:
1302 Elf32_Addr word1, word2;
1305 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1306 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1311 #elif defined(__powerpc64__)
1312 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1314 #elif defined(__powerpc__)
1316 case R_PPC_ADDR16_HA:
1317 *(unsigned short *)loc = (v + 0x8000) >> 16;
1320 case R_PPC_ADDR16_HI:
1321 *(unsigned short *)loc = v >> 16;
1324 case R_PPC_ADDR16_LO:
1325 *(unsigned short *)loc = v;
1339 #elif defined(__s390__)
1342 *(unsigned int *) loc += v;
1345 *(unsigned short *) loc += v;
1348 *(unsigned char *) loc += v;
1352 *(unsigned int *) loc += v - dot;
1355 *(unsigned short *) loc += (v - dot) >> 1;
1358 *(unsigned short *) loc += v - dot;
1362 case R_390_PLT16DBL:
1363 /* find the plt entry and initialize it. */
1364 pe = (struct arch_single_entry *) &isym->pltent;
1365 if (pe->inited == 0) {
1366 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1367 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1369 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1376 /* Insert relative distance to target. */
1377 v = plt + pe->offset - dot;
1378 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1379 *(unsigned int *) loc = (unsigned int) v;
1380 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1381 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1384 case R_390_GLOB_DAT:
1385 case R_390_JMP_SLOT:
1389 case R_390_RELATIVE:
1390 *loc += f->baseaddr;
1394 *(unsigned long *) loc += got - dot;
1400 if (!isym->gotent.inited)
1402 isym->gotent.inited = 1;
1403 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1405 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1406 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1407 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1408 *(unsigned short *) loc += isym->gotent.offset;
1409 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1410 *(unsigned int *) loc += isym->gotent.offset;
1413 # ifndef R_390_GOTOFF32
1414 # define R_390_GOTOFF32 R_390_GOTOFF
1416 case R_390_GOTOFF32:
1420 #elif defined(__sh__)
1443 *loc = f->baseaddr + rel->r_addend;
1447 *loc = got - dot + rel->r_addend;
1457 # if defined(__SH5__)
1458 case R_SH_IMM_MEDLOW16:
1459 case R_SH_IMM_LOW16:
1463 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1467 * movi and shori have the format:
1469 * | op | imm | reg | reserved |
1470 * 31..26 25..10 9.. 4 3 .. 0
1472 * so we simply mask and or in imm.
1474 word = *loc & ~0x3fffc00;
1475 word |= (v & 0xffff) << 10;
1482 case R_SH_IMM_MEDLOW16_PCREL:
1483 case R_SH_IMM_LOW16_PCREL:
1487 word = *loc & ~0x3fffc00;
1491 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1494 word |= (v & 0xffff) << 10;
1500 # endif /* __SH5__ */
1502 #elif defined(__v850e__)
1508 /* We write two shorts instead of a long because even
1509 32-bit insns only need half-word alignment, but
1510 32-bit data needs to be long-word aligned. */
1511 v += ((unsigned short *)loc)[0];
1512 v += ((unsigned short *)loc)[1] << 16;
1513 ((unsigned short *)loc)[0] = v & 0xffff;
1514 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1517 case R_V850_22_PCREL:
1520 #elif defined(__x86_64__)
1530 *(unsigned int *) loc += v;
1533 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1534 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1539 *(signed int *) loc += v;
1543 *(unsigned short *) loc += v;
1547 *(unsigned char *) loc += v;
1551 *(unsigned int *) loc += v - dot;
1555 *(unsigned short *) loc += v - dot;
1559 *(unsigned char *) loc += v - dot;
1562 case R_X86_64_GLOB_DAT:
1563 case R_X86_64_JUMP_SLOT:
1567 case R_X86_64_RELATIVE:
1568 *loc += f->baseaddr;
1571 case R_X86_64_GOT32:
1572 case R_X86_64_GOTPCREL:
1575 if (!isym->gotent.reloc_done)
1577 isym->gotent.reloc_done = 1;
1578 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1580 /* XXX are these really correct? */
1581 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1582 *(unsigned int *) loc += v + isym->gotent.offset;
1584 *loc += isym->gotent.offset;
1589 # warning "no idea how to handle relocations on your arch"
1593 printf("Warning: unhandled reloc %d\n", (int)ELF_R_TYPE(rel->r_info));
1594 ret = obj_reloc_unhandled;
1597 #if defined(USE_PLT_ENTRIES)
1601 /* find the plt entry and initialize it if necessary */
1603 #if defined(USE_PLT_LIST)
1604 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1611 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1613 /* generate some machine code */
1615 #if defined(__arm__)
1616 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1617 ip[1] = v; /* sym@ */
1619 #if defined(__powerpc__)
1620 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1621 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1622 ip[2] = 0x7d6903a6; /* mtctr r11 */
1623 ip[3] = 0x4e800420; /* bctr */
1625 #if defined(__v850e__)
1626 /* We have to trash a register, so we assume that any control
1627 transfer more than 21-bits away must be a function call
1628 (so we can use a call-clobbered register). */
1629 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1630 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1635 /* relative distance to target */
1637 /* if the target is too far away.... */
1638 #if defined(__arm__) || defined(__powerpc__)
1639 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1640 #elif defined(__v850e__)
1641 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1643 /* go via the plt */
1644 v = plt + pe->offset - dot;
1646 #if defined(__v850e__)
1651 ret = obj_reloc_dangerous;
1653 /* merge the offset into the instruction. */
1654 #if defined(__arm__)
1655 /* Convert to words. */
1658 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1660 #if defined(__powerpc__)
1661 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1663 #if defined(__v850e__)
1664 /* We write two shorts instead of a long because even 32-bit insns
1665 only need half-word alignment, but the 32-bit data write needs
1666 to be long-word aligned. */
1667 ((unsigned short *)loc)[0] =
1668 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1669 | ((v >> 16) & 0x3f); /* offs high part */
1670 ((unsigned short *)loc)[1] =
1671 (v & 0xffff); /* offs low part */
1674 #endif /* USE_PLT_ENTRIES */
1676 #if defined(USE_GOT_ENTRIES)
1679 /* needs an entry in the .got: set it, once */
1680 if (!isym->gotent.inited) {
1681 isym->gotent.inited = 1;
1682 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1684 /* make the reloc with_respect_to_.got */
1686 *loc += isym->gotent.offset + rel->r_addend;
1687 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1688 *loc += isym->gotent.offset;
1692 #endif /* USE_GOT_ENTRIES */
1699 #if defined(USE_LIST)
1701 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1702 int offset, int size)
1704 struct arch_list_entry *pe;
1706 for (pe = *list; pe != NULL; pe = pe->next) {
1707 if (pe->addend == rel->r_addend) {
1713 pe = xzalloc(sizeof(struct arch_list_entry));
1715 pe->addend = rel->r_addend;
1716 pe->offset = offset;
1726 #if defined(USE_SINGLE)
1728 static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
1729 int offset, int size)
1731 if (single->allocated == 0) {
1732 single->allocated = 1;
1733 single->offset = offset;
1742 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1744 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
1745 int offset, int size)
1747 struct obj_section *myrelsec = obj_find_section(f, name);
1754 obj_extend_section(myrelsec, offset);
1756 myrelsec = obj_create_alloced_section(f, name,
1765 static void arch_create_got(struct obj_file *f)
1767 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1768 struct arch_file *ifile = (struct arch_file *) f;
1770 #if defined(USE_GOT_ENTRIES)
1771 int got_offset = 0, got_needed = 0, got_allocate;
1773 #if defined(USE_PLT_ENTRIES)
1774 int plt_offset = 0, plt_needed = 0, plt_allocate;
1776 struct obj_section *relsec, *symsec, *strsec;
1777 ElfW(RelM) *rel, *relend;
1778 ElfW(Sym) *symtab, *extsym;
1779 const char *strtab, *name;
1780 struct arch_symbol *intsym;
1782 for (i = 0; i < f->header.e_shnum; ++i) {
1783 relsec = f->sections[i];
1784 if (relsec->header.sh_type != SHT_RELM)
1787 symsec = f->sections[relsec->header.sh_link];
1788 strsec = f->sections[symsec->header.sh_link];
1790 rel = (ElfW(RelM) *) relsec->contents;
1791 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1792 symtab = (ElfW(Sym) *) symsec->contents;
1793 strtab = (const char *) strsec->contents;
1795 for (; rel < relend; ++rel) {
1796 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1798 #if defined(USE_GOT_ENTRIES)
1801 #if defined(USE_PLT_ENTRIES)
1805 switch (ELF_R_TYPE(rel->r_info)) {
1806 #if defined(__arm__)
1821 #elif defined(__i386__)
1831 #elif defined(__powerpc__)
1836 #elif defined(__mc68000__)
1847 #elif defined(__sh__)
1857 #elif defined(__v850e__)
1858 case R_V850_22_PCREL:
1867 if (extsym->st_name != 0) {
1868 name = strtab + extsym->st_name;
1870 name = f->sections[extsym->st_shndx]->name;
1872 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1873 #if defined(USE_GOT_ENTRIES)
1875 got_offset += arch_single_init(
1876 /*rel,*/ &intsym->gotent,
1877 got_offset, GOT_ENTRY_SIZE);
1882 #if defined(USE_PLT_ENTRIES)
1884 #if defined(USE_PLT_LIST)
1885 plt_offset += arch_list_add(
1886 rel, &intsym->pltent,
1887 plt_offset, PLT_ENTRY_SIZE);
1889 plt_offset += arch_single_init(
1890 /*rel,*/ &intsym->pltent,
1891 plt_offset, PLT_ENTRY_SIZE);
1899 #if defined(USE_GOT_ENTRIES)
1901 ifile->got = arch_xsect_init(f, ".got", got_offset,
1906 #if defined(USE_PLT_ENTRIES)
1908 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1913 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1916 /*======================================================================*/
1918 /* Standard ELF hash function. */
1919 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1921 unsigned long h = 0;
1928 g = (h & 0xf0000000);
1938 static unsigned long FAST_FUNC obj_elf_hash(const char *name)
1940 return obj_elf_hash_n(name, strlen(name));
1943 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1944 /* String comparison for non-co-versioned kernel and module. */
1946 static int ncv_strcmp(const char *a, const char *b)
1948 size_t alen = strlen(a), blen = strlen(b);
1950 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1951 return strncmp(a, b, alen);
1952 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1953 return strncmp(a, b, blen);
1955 return strcmp(a, b);
1958 /* String hashing for non-co-versioned kernel and module. Here
1959 we are simply forced to drop the crc from the hash. */
1961 static unsigned long FAST_FUNC ncv_symbol_hash(const char *str)
1963 size_t len = strlen(str);
1964 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1966 return obj_elf_hash_n(str, len);
1970 obj_set_symbol_compare(struct obj_file *f,
1971 int (*cmp) (const char *, const char *),
1972 unsigned long (*hash) (const char *) FAST_FUNC)
1975 f->symbol_cmp = cmp;
1977 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1980 f->symbol_hash = hash;
1982 memcpy(tmptab, f->symtab, sizeof(tmptab));
1983 memset(f->symtab, 0, sizeof(f->symtab));
1985 for (i = 0; i < HASH_BUCKETS; ++i) {
1986 for (sym = tmptab[i]; sym; sym = next) {
1987 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1989 sym->next = f->symtab[h];
1996 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
1998 static struct obj_symbol *
1999 obj_add_symbol(struct obj_file *f, const char *name,
2000 unsigned long symidx, int info,
2001 int secidx, ElfW(Addr) value,
2004 struct obj_symbol *sym;
2005 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2006 int n_type = ELF_ST_TYPE(info);
2007 int n_binding = ELF_ST_BIND(info);
2009 for (sym = f->symtab[hash]; sym; sym = sym->next) {
2010 if (f->symbol_cmp(sym->name, name) == 0) {
2011 int o_secidx = sym->secidx;
2012 int o_info = sym->info;
2013 int o_type = ELF_ST_TYPE(o_info);
2014 int o_binding = ELF_ST_BIND(o_info);
2016 /* A redefinition! Is it legal? */
2018 if (secidx == SHN_UNDEF)
2020 else if (o_secidx == SHN_UNDEF)
2022 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2023 /* Cope with local and global symbols of the same name
2024 in the same object file, as might have been created
2025 by ld -r. The only reason locals are now seen at this
2026 level at all is so that we can do semi-sensible things
2029 struct obj_symbol *nsym, **p;
2031 nsym = arch_new_symbol();
2032 nsym->next = sym->next;
2035 /* Excise the old (local) symbol from the hash chain. */
2036 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2040 } else if (n_binding == STB_LOCAL) {
2041 /* Another symbol of the same name has already been defined.
2042 Just add this to the local table. */
2043 sym = arch_new_symbol();
2046 f->local_symtab[symidx] = sym;
2048 } else if (n_binding == STB_WEAK)
2050 else if (o_binding == STB_WEAK)
2052 /* Don't unify COMMON symbols with object types the programmer
2054 else if (secidx == SHN_COMMON
2055 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2057 else if (o_secidx == SHN_COMMON
2058 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2061 /* Don't report an error if the symbol is coming from
2062 the kernel or some external module. */
2063 if (secidx <= SHN_HIRESERVE)
2064 bb_error_msg("%s multiply defined", name);
2070 /* Completely new symbol. */
2071 sym = arch_new_symbol();
2072 sym->next = f->symtab[hash];
2073 f->symtab[hash] = sym;
2075 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
2076 if (symidx >= f->local_symtab_size)
2077 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2078 name, (long) symidx, (long) f->local_symtab_size);
2080 f->local_symtab[symidx] = sym;
2087 sym->secidx = secidx;
2093 static struct obj_symbol *
2094 obj_find_symbol(struct obj_file *f, const char *name)
2096 struct obj_symbol *sym;
2097 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2099 for (sym = f->symtab[hash]; sym; sym = sym->next)
2100 if (f->symbol_cmp(sym->name, name) == 0)
2105 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2108 if (sym->secidx >= SHN_LORESERVE)
2110 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2112 /* As a special case, a NULL sym has value zero. */
2116 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2118 int i, n = f->header.e_shnum;
2120 for (i = 0; i < n; ++i)
2121 if (strcmp(f->sections[i]->name, name) == 0)
2122 return f->sections[i];
2126 static int obj_load_order_prio(struct obj_section *a)
2128 unsigned long af, ac;
2130 af = a->header.sh_flags;
2133 if (a->name[0] != '.' || strlen(a->name) != 10
2134 || strcmp(a->name + 5, ".init") != 0
2140 if (!(af & SHF_WRITE))
2142 if (af & SHF_EXECINSTR)
2144 if (a->header.sh_type != SHT_NOBITS)
2151 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2153 struct obj_section **p;
2154 int prio = obj_load_order_prio(sec);
2155 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2156 if (obj_load_order_prio(*p) < prio)
2158 sec->load_next = *p;
2162 static struct obj_section *helper_create_alloced_section(struct obj_file *f,
2164 unsigned long align,
2167 int newidx = f->header.e_shnum++;
2168 struct obj_section *sec;
2170 f->sections = xrealloc_vector(f->sections, 2, newidx);
2171 f->sections[newidx] = sec = arch_new_section();
2173 sec->header.sh_type = SHT_PROGBITS;
2174 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2175 sec->header.sh_size = size;
2176 sec->header.sh_addralign = align;
2180 sec->contents = xzalloc(size);
2185 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2187 unsigned long align,
2190 struct obj_section *sec;
2192 sec = helper_create_alloced_section(f, name, align, size);
2193 obj_insert_section_load_order(f, sec);
2197 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2199 unsigned long align,
2202 struct obj_section *sec;
2204 sec = helper_create_alloced_section(f, name, align, size);
2205 sec->load_next = f->load_order;
2206 f->load_order = sec;
2207 if (f->load_order_search_start == &f->load_order)
2208 f->load_order_search_start = &sec->load_next;
2213 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2215 unsigned long oldsize = sec->header.sh_size;
2217 sec->header.sh_size += more;
2218 sec->contents = xrealloc(sec->contents, sec->header.sh_size);
2220 return sec->contents + oldsize;
2224 /* Conditionally add the symbols from the given symbol set to the
2227 static int add_symbols_from(struct obj_file *f,
2229 struct new_module_symbol *syms,
2232 struct new_module_symbol *s;
2235 #ifdef SYMBOL_PREFIX
2236 char *name_buf = NULL;
2237 size_t name_alloced_size = 0;
2239 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2242 gpl = obj_gpl_license(f, NULL) == 0;
2244 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2245 /* Only add symbols that are already marked external.
2246 If we override locals we may cause problems for
2247 argument initialization. We will also create a false
2248 dependency on the module. */
2249 struct obj_symbol *sym;
2252 /* GPL licensed modules can use symbols exported with
2253 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2254 * exported names. Non-GPL modules never see any GPLONLY_
2255 * symbols so they cannot fudge it by adding the prefix on
2258 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2259 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2266 name = (char *)s->name;
2268 #ifdef SYMBOL_PREFIX
2269 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2270 kernel exports `C names', but module object files
2271 reference `linker names'). */
2272 size_t extra = sizeof SYMBOL_PREFIX;
2273 size_t name_size = strlen(name) + extra;
2274 if (name_size > name_alloced_size) {
2275 name_alloced_size = name_size * 2;
2276 name_buf = alloca(name_alloced_size);
2278 strcpy(name_buf, SYMBOL_PREFIX);
2279 strcpy(name_buf + extra - 1, name);
2283 sym = obj_find_symbol(f, name);
2284 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2285 #ifdef SYMBOL_PREFIX
2286 /* Put NAME_BUF into more permanent storage. */
2287 name = xmalloc(name_size);
2288 strcpy(name, name_buf);
2290 sym = obj_add_symbol(f, name, -1,
2291 ELF_ST_INFO(STB_GLOBAL,
2294 /* Did our symbol just get installed? If so, mark the
2295 module as "used". */
2296 if (sym->secidx == idx)
2304 static void add_kernel_symbols(struct obj_file *f)
2306 struct external_module *m;
2309 /* Add module symbols first. */
2311 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
2313 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
2320 n_ext_modules_used = nused;
2322 /* And finally the symbols from the kernel proper. */
2325 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2328 static char *get_modinfo_value(struct obj_file *f, const char *key)
2330 struct obj_section *sec;
2331 char *p, *v, *n, *ep;
2332 size_t klen = strlen(key);
2334 sec = obj_find_section(f, ".modinfo");
2338 ep = p + sec->header.sh_size;
2341 n = strchr(p, '\0');
2343 if (p + klen == v && strncmp(p, key, klen) == 0)
2346 if (p + klen == n && strcmp(p, key) == 0)
2356 /*======================================================================*/
2357 /* Functions relating to module loading after 2.1.18. */
2359 /* From Linux-2.6 sources */
2360 /* You can use " around spaces, but can't escape ". */
2361 /* Hyphens and underscores equivalent in parameter names. */
2362 static char *next_arg(char *args, char **param, char **val)
2364 unsigned int i, equals = 0;
2365 int in_quote = 0, quoted = 0;
2374 for (i = 0; args[i]; i++) {
2375 if (args[i] == ' ' && !in_quote)
2382 in_quote = !in_quote;
2389 args[equals] = '\0';
2390 *val = args + equals + 1;
2392 /* Don't include quotes in value. */
2395 if (args[i-1] == '"')
2398 if (quoted && args[i-1] == '"')
2404 next = args + i + 1;
2408 /* Chew up trailing spaces. */
2409 return skip_whitespace(next);
2413 new_process_module_arguments(struct obj_file *f, const char *options)
2415 char *xoptions, *pos;
2418 xoptions = pos = xstrdup(skip_whitespace(options));
2420 unsigned long charssize = 0;
2421 char *tmp, *contents, *loc, *pinfo, *p;
2422 struct obj_symbol *sym;
2423 int min, max, n, len;
2425 pos = next_arg(pos, ¶m, &val);
2427 tmp = xasprintf("parm_%s", param);
2428 pinfo = get_modinfo_value(f, tmp);
2431 bb_error_msg_and_die("invalid parameter %s", param);
2433 #ifdef SYMBOL_PREFIX
2434 tmp = xasprintf(SYMBOL_PREFIX "%s", param);
2435 sym = obj_find_symbol(f, tmp);
2438 sym = obj_find_symbol(f, param);
2441 /* Also check that the parameter was not resolved from the kernel. */
2442 if (sym == NULL || sym->secidx > SHN_HIRESERVE)
2443 bb_error_msg_and_die("symbol for parameter %s not found", param);
2445 /* Number of parameters */
2447 if (isdigit(*pinfo)) {
2448 min = max = strtoul(pinfo, &pinfo, 10);
2450 max = strtoul(pinfo + 1, &pinfo, 10);
2453 contents = f->sections[sym->secidx]->contents;
2454 loc = contents + sym->value;
2456 if (*pinfo == 'c') {
2457 if (!isdigit(pinfo[1])) {
2458 bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
2459 " the maximum size", param);
2461 charssize = strtoul(pinfo + 1, NULL, 10);
2466 bb_error_msg_and_die("argument expected for parameter %s", param);
2470 /* Parse parameter values */
2478 bb_error_msg_and_die("too many values for %s (max %d)", param, max);
2482 len = strcspn(p, ",");
2485 obj_string_patch(f, sym->secidx,
2487 loc += tgt_sizeof_char_p;
2492 len = strcspn(p, ",");
2495 if (len >= charssize)
2496 bb_error_msg_and_die("string too long for %s (max %ld)", param,
2498 strcpy((char *) loc, p);
2504 *loc++ = strtoul(p, &endp, 0);
2505 p = endp; /* gcc likes temp var for &endp */
2508 *(short *) loc = strtoul(p, &endp, 0);
2509 loc += tgt_sizeof_short;
2513 *(int *) loc = strtoul(p, &endp, 0);
2514 loc += tgt_sizeof_int;
2518 *(long *) loc = strtoul(p, &endp, 0);
2519 loc += tgt_sizeof_long;
2523 bb_error_msg_and_die("unknown parameter type '%c' for %s",
2527 p = skip_whitespace(p);
2530 p = skip_whitespace(p + 1);
2534 bb_error_msg_and_die("parameter %s requires at least %d arguments", param, min);
2536 bb_error_msg_and_die("invalid argument syntax for %s", param);
2542 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2543 static int new_is_module_checksummed(struct obj_file *f)
2545 const char *p = get_modinfo_value(f, "using_checksums");
2551 /* Get the module's kernel version in the canonical integer form. */
2554 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2559 p = get_modinfo_value(f, "kernel_version");
2562 safe_strncpy(str, p, STRVERSIONLEN);
2564 a = strtoul(p, &p, 10);
2567 b = strtoul(p + 1, &p, 10);
2570 c = strtoul(p + 1, &q, 10);
2574 return a << 16 | b << 8 | c;
2577 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2580 /* Fetch the loaded modules, and all currently exported symbols. */
2582 static void new_get_kernel_symbols(void)
2584 char *module_names, *mn;
2585 struct external_module *modules, *m;
2586 struct new_module_symbol *syms, *s;
2587 size_t ret, bufsize, nmod, nsyms, i, j;
2589 /* Collect the loaded modules. */
2592 module_names = xmalloc(bufsize);
2595 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2596 if (errno == ENOSPC && bufsize < ret) {
2598 module_names = xrealloc(module_names, bufsize);
2599 goto retry_modules_load;
2601 bb_perror_msg_and_die("QM_MODULES");
2604 n_ext_modules = nmod = ret;
2606 /* Collect the modules' symbols. */
2609 ext_modules = modules = xzalloc(nmod * sizeof(*modules));
2610 for (i = 0, mn = module_names, m = modules;
2611 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2612 struct new_module_info info;
2614 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2615 if (errno == ENOENT) {
2616 /* The module was removed out from underneath us. */
2619 bb_perror_msg_and_die("query_module: QM_INFO: %s", mn);
2623 syms = xmalloc(bufsize);
2625 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2629 syms = xrealloc(syms, bufsize);
2630 goto retry_mod_sym_load;
2632 /* The module was removed out from underneath us. */
2635 bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn);
2641 m->addr = info.addr;
2645 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2646 s->name += (unsigned long) syms;
2651 /* Collect the kernel's symbols. */
2653 bufsize = 16 * 1024;
2654 syms = xmalloc(bufsize);
2655 retry_kern_sym_load:
2656 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2657 if (errno == ENOSPC && bufsize < ret) {
2659 syms = xrealloc(syms, bufsize);
2660 goto retry_kern_sym_load;
2662 bb_perror_msg_and_die("kernel: QM_SYMBOLS");
2664 nksyms = nsyms = ret;
2667 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2668 s->name += (unsigned long) syms;
2673 /* Return the kernel symbol checksum version, or zero if not used. */
2675 static int new_is_kernel_checksummed(void)
2677 struct new_module_symbol *s;
2680 /* Using_Versions is not the first symbol, but it should be in there. */
2682 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2683 if (strcmp((char *) s->name, "Using_Versions") == 0)
2690 static void new_create_this_module(struct obj_file *f, const char *m_name)
2692 struct obj_section *sec;
2694 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2695 sizeof(struct new_module));
2696 /* done by obj_create_alloced_section_first: */
2697 /*memset(sec->contents, 0, sizeof(struct new_module));*/
2699 obj_add_symbol(f, SPFX "__this_module", -1,
2700 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2701 sizeof(struct new_module));
2703 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2707 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2708 /* add an entry to the __ksymtab section, creating it if necessary */
2709 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2711 struct obj_section *sec;
2714 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2715 * If __ksymtab is defined but not marked alloc, x out the first character
2716 * (no obj_delete routine) and create a new __ksymtab with the correct
2719 sec = obj_find_section(f, "__ksymtab");
2720 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2721 *((char *)(sec->name)) = 'x'; /* override const */
2725 sec = obj_create_alloced_section(f, "__ksymtab",
2726 tgt_sizeof_void_p, 0);
2729 sec->header.sh_flags |= SHF_ALLOC;
2730 /* Empty section might be byte-aligned */
2731 sec->header.sh_addralign = tgt_sizeof_void_p;
2732 ofs = sec->header.sh_size;
2733 obj_symbol_patch(f, sec->idx, ofs, sym);
2734 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2735 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2737 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2739 static int new_create_module_ksymtab(struct obj_file *f)
2741 struct obj_section *sec;
2744 /* We must always add the module references. */
2746 if (n_ext_modules_used) {
2747 struct new_module_ref *dep;
2748 struct obj_symbol *tm;
2750 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2751 (sizeof(struct new_module_ref)
2752 * n_ext_modules_used));
2756 tm = obj_find_symbol(f, SPFX "__this_module");
2757 dep = (struct new_module_ref *) sec->contents;
2758 for (i = 0; i < n_ext_modules; ++i)
2759 if (ext_modules[i].used) {
2760 dep->dep = ext_modules[i].addr;
2761 obj_symbol_patch(f, sec->idx,
2762 (char *) &dep->ref - sec->contents, tm);
2768 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2772 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2774 /* We don't want to export symbols residing in sections that
2775 aren't loaded. There are a number of these created so that
2776 we make sure certain module options don't appear twice. */
2777 i = f->header.e_shnum;
2778 loaded = alloca(sizeof(int) * i);
2780 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2782 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2783 struct obj_symbol *sym;
2784 for (sym = f->symtab[i]; sym; sym = sym->next) {
2785 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2786 && sym->secidx <= SHN_HIRESERVE
2787 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
2789 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2791 obj_symbol_patch(f, sec->idx, ofs, sym);
2792 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2799 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2807 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2809 struct new_module *module;
2810 struct obj_section *sec;
2815 sec = obj_find_section(f, ".this");
2816 if (!sec || !sec->contents) {
2817 bb_perror_msg_and_die("corrupt module %s?", m_name);
2819 module = (struct new_module *) sec->contents;
2820 m_addr = sec->header.sh_addr;
2822 module->size_of_struct = sizeof(*module);
2823 module->size = m_size;
2824 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2826 sec = obj_find_section(f, "__ksymtab");
2827 if (sec && sec->header.sh_size) {
2828 module->syms = sec->header.sh_addr;
2829 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2832 if (n_ext_modules_used) {
2833 sec = obj_find_section(f, ".kmodtab");
2834 module->deps = sec->header.sh_addr;
2835 module->ndeps = n_ext_modules_used;
2838 module->init = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2839 module->cleanup = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2841 sec = obj_find_section(f, "__ex_table");
2843 module->ex_table_start = sec->header.sh_addr;
2844 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2847 sec = obj_find_section(f, ".text.init");
2849 module->runsize = sec->header.sh_addr - m_addr;
2851 sec = obj_find_section(f, ".data.init");
2853 if (!module->runsize
2854 || module->runsize > sec->header.sh_addr - m_addr
2856 module->runsize = sec->header.sh_addr - m_addr;
2859 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2860 if (sec && sec->header.sh_size) {
2861 module->archdata_start = (void*)sec->header.sh_addr;
2862 module->archdata_end = module->archdata_start + sec->header.sh_size;
2864 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2865 if (sec && sec->header.sh_size) {
2866 module->kallsyms_start = (void*)sec->header.sh_addr;
2867 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2870 /* Whew! All of the initialization is complete. Collect the final
2871 module image and give it to the kernel. */
2873 image = xmalloc(m_size);
2874 obj_create_image(f, image);
2876 ret = init_module(m_name, (struct new_module *) image);
2878 bb_perror_msg("init_module: %s", m_name);
2886 /*======================================================================*/
2889 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2892 struct obj_string_patch *p;
2893 struct obj_section *strsec;
2894 size_t len = strlen(string) + 1;
2897 p = xzalloc(sizeof(*p));
2898 p->next = f->string_patches;
2899 p->reloc_secidx = secidx;
2900 p->reloc_offset = offset;
2901 f->string_patches = p;
2903 strsec = obj_find_section(f, ".kstrtab");
2904 if (strsec == NULL) {
2905 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2906 /*p->string_offset = 0;*/
2907 loc = strsec->contents;
2909 p->string_offset = strsec->header.sh_size;
2910 loc = obj_extend_section(strsec, len);
2912 memcpy(loc, string, len);
2916 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2917 struct obj_symbol *sym)
2919 struct obj_symbol_patch *p;
2921 p = xmalloc(sizeof(*p));
2922 p->next = f->symbol_patches;
2923 p->reloc_secidx = secidx;
2924 p->reloc_offset = offset;
2926 f->symbol_patches = p;
2929 static void obj_check_undefineds(struct obj_file *f)
2933 for (i = 0; i < HASH_BUCKETS; ++i) {
2934 struct obj_symbol *sym;
2935 for (sym = f->symtab[i]; sym; sym = sym->next) {
2936 if (sym->secidx == SHN_UNDEF) {
2937 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
2938 sym->secidx = SHN_ABS;
2942 bb_error_msg_and_die("unresolved symbol %s", sym->name);
2949 static void obj_allocate_commons(struct obj_file *f)
2951 struct common_entry {
2952 struct common_entry *next;
2953 struct obj_symbol *sym;
2954 } *common_head = NULL;
2958 for (i = 0; i < HASH_BUCKETS; ++i) {
2959 struct obj_symbol *sym;
2960 for (sym = f->symtab[i]; sym; sym = sym->next) {
2961 if (sym->secidx == SHN_COMMON) {
2962 /* Collect all COMMON symbols and sort them by size so as to
2963 minimize space wasted by alignment requirements. */
2964 struct common_entry **p, *n;
2965 for (p = &common_head; *p; p = &(*p)->next)
2966 if (sym->size <= (*p)->sym->size)
2968 n = alloca(sizeof(*n));
2976 for (i = 1; i < f->local_symtab_size; ++i) {
2977 struct obj_symbol *sym = f->local_symtab[i];
2978 if (sym && sym->secidx == SHN_COMMON) {
2979 struct common_entry **p, *n;
2980 for (p = &common_head; *p; p = &(*p)->next) {
2981 if (sym == (*p)->sym)
2983 if (sym->size < (*p)->sym->size) {
2984 n = alloca(sizeof(*n));
2995 /* Find the bss section. */
2996 for (i = 0; i < f->header.e_shnum; ++i)
2997 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3000 /* If for some reason there hadn't been one, create one. */
3001 if (i == f->header.e_shnum) {
3002 struct obj_section *sec;
3004 f->header.e_shnum++;
3005 f->sections = xrealloc_vector(f->sections, 2, i);
3006 f->sections[i] = sec = arch_new_section();
3008 sec->header.sh_type = SHT_PROGBITS;
3009 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3014 /* Allocate the COMMONS. */
3016 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3017 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3018 struct common_entry *c;
3020 for (c = common_head; c; c = c->next) {
3021 ElfW(Addr) align = c->sym->value;
3023 if (align > max_align)
3025 if (bss_size & (align - 1))
3026 bss_size = (bss_size | (align - 1)) + 1;
3029 c->sym->value = bss_size;
3031 bss_size += c->sym->size;
3034 f->sections[i]->header.sh_size = bss_size;
3035 f->sections[i]->header.sh_addralign = max_align;
3039 /* For the sake of patch relocation and parameter initialization,
3040 allocate zeroed data for NOBITS sections now. Note that after
3041 this we cannot assume NOBITS are really empty. */
3042 for (i = 0; i < f->header.e_shnum; ++i) {
3043 struct obj_section *s = f->sections[i];
3044 if (s->header.sh_type == SHT_NOBITS) {
3046 if (s->header.sh_size != 0)
3047 s->contents = xzalloc(s->header.sh_size);
3048 s->header.sh_type = SHT_PROGBITS;
3053 static unsigned long obj_load_size(struct obj_file *f)
3055 unsigned long dot = 0;
3056 struct obj_section *sec;
3058 /* Finalize the positions of the sections relative to one another. */
3060 for (sec = f->load_order; sec; sec = sec->load_next) {
3063 align = sec->header.sh_addralign;
3064 if (align && (dot & (align - 1)))
3065 dot = (dot | (align - 1)) + 1;
3067 sec->header.sh_addr = dot;
3068 dot += sec->header.sh_size;
3074 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3076 int i, n = f->header.e_shnum;
3079 /* Finalize the addresses of the sections. */
3082 for (i = 0; i < n; ++i)
3083 f->sections[i]->header.sh_addr += base;
3085 /* And iterate over all of the relocations. */
3087 for (i = 0; i < n; ++i) {
3088 struct obj_section *relsec, *symsec, *targsec, *strsec;
3089 ElfW(RelM) * rel, *relend;
3093 relsec = f->sections[i];
3094 if (relsec->header.sh_type != SHT_RELM)
3097 symsec = f->sections[relsec->header.sh_link];
3098 targsec = f->sections[relsec->header.sh_info];
3099 strsec = f->sections[symsec->header.sh_link];
3101 rel = (ElfW(RelM) *) relsec->contents;
3102 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3103 symtab = (ElfW(Sym) *) symsec->contents;
3104 strtab = (const char *) strsec->contents;
3106 for (; rel < relend; ++rel) {
3107 ElfW(Addr) value = 0;
3108 struct obj_symbol *intsym = NULL;
3109 unsigned long symndx;
3110 ElfW(Sym) *extsym = NULL;
3113 /* Attempt to find a value to use for this relocation. */
3115 symndx = ELF_R_SYM(rel->r_info);
3117 /* Note we've already checked for undefined symbols. */
3119 extsym = &symtab[symndx];
3120 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3121 /* Local symbols we look up in the local table to be sure
3122 we get the one that is really intended. */
3123 intsym = f->local_symtab[symndx];
3125 /* Others we look up in the hash table. */
3127 if (extsym->st_name)
3128 name = strtab + extsym->st_name;
3130 name = f->sections[extsym->st_shndx]->name;
3131 intsym = obj_find_symbol(f, name);
3134 value = obj_symbol_final_value(f, intsym);
3135 intsym->referenced = 1;
3137 #if SHT_RELM == SHT_RELA
3138 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3139 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3140 if (!extsym || !extsym->st_name
3141 || ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3143 value += rel->r_addend;
3147 switch (arch_apply_relocation
3148 (f, targsec, /*symsec,*/ intsym, rel, value)
3153 case obj_reloc_overflow:
3154 errmsg = "Relocation overflow";
3156 case obj_reloc_dangerous:
3157 errmsg = "Dangerous relocation";
3159 case obj_reloc_unhandled:
3160 errmsg = "Unhandled relocation";
3163 bb_error_msg("%s of type %ld for %s", errmsg,
3164 (long) ELF_R_TYPE(rel->r_info),
3165 strtab + extsym->st_name);
3167 bb_error_msg("%s of type %ld", errmsg,
3168 (long) ELF_R_TYPE(rel->r_info));
3176 /* Finally, take care of the patches. */
3178 if (f->string_patches) {
3179 struct obj_string_patch *p;
3180 struct obj_section *strsec;
3181 ElfW(Addr) strsec_base;
3182 strsec = obj_find_section(f, ".kstrtab");
3183 strsec_base = strsec->header.sh_addr;
3185 for (p = f->string_patches; p; p = p->next) {
3186 struct obj_section *targsec = f->sections[p->reloc_secidx];
3187 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3188 = strsec_base + p->string_offset;
3192 if (f->symbol_patches) {
3193 struct obj_symbol_patch *p;
3195 for (p = f->symbol_patches; p; p = p->next) {
3196 struct obj_section *targsec = f->sections[p->reloc_secidx];
3197 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3198 = obj_symbol_final_value(f, p->sym);
3205 static int obj_create_image(struct obj_file *f, char *image)
3207 struct obj_section *sec;
3208 ElfW(Addr) base = f->baseaddr;
3210 for (sec = f->load_order; sec; sec = sec->load_next) {
3213 if (sec->contents == 0 || sec->header.sh_size == 0)
3216 secimg = image + (sec->header.sh_addr - base);
3218 /* Note that we allocated data for NOBITS sections earlier. */
3219 memcpy(secimg, sec->contents, sec->header.sh_size);
3225 /*======================================================================*/
3227 static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits)
3229 typedef uint32_t aliased_uint32_t FIX_ALIASING;
3230 #if BB_LITTLE_ENDIAN
3231 # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3)))))
3233 # define ELFMAG_U32 ((uint32_t)((((ELFMAG0 * 0x100) + ELFMAG1) * 0x100 + ELFMAG2) * 0x100 + ELFMAG3))
3236 ElfW(Shdr) * section_headers;
3240 /* Read the file header. */
3242 f = arch_new_file();
3243 f->symbol_cmp = strcmp;
3244 f->symbol_hash = obj_elf_hash;
3245 f->load_order_search_start = &f->load_order;
3247 if (image_size < sizeof(f->header))
3248 bb_error_msg_and_die("error while loading ELF header");
3249 memcpy(&f->header, image, sizeof(f->header));
3251 if (*(aliased_uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
3252 bb_error_msg_and_die("not an ELF file");
3254 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3255 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB)
3256 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3257 || !MATCH_MACHINE(f->header.e_machine)
3259 bb_error_msg_and_die("ELF file not for this architecture");
3261 if (f->header.e_type != ET_REL) {
3262 bb_error_msg_and_die("ELF file not a relocatable object");
3265 /* Read the section headers. */
3267 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3268 bb_error_msg_and_die("section header size mismatch: %lu != %lu",
3269 (unsigned long) f->header.e_shentsize,
3270 (unsigned long) sizeof(ElfW(Shdr)));
3273 shnum = f->header.e_shnum;
3274 /* Growth of ->sections vector will be done by
3275 * xrealloc_vector(..., 2, ...), therefore we must allocate
3276 * at least 2^2 = 4 extra elements here. */
3277 f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
3279 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3280 if (image_size < f->header.e_shoff + sizeof(ElfW(Shdr)) * shnum)
3281 bb_error_msg_and_die("error while loading section headers");
3282 memcpy(section_headers, image + f->header.e_shoff, sizeof(ElfW(Shdr)) * shnum);
3284 /* Read the section data. */
3286 for (i = 0; i < shnum; ++i) {
3287 struct obj_section *sec;
3289 f->sections[i] = sec = arch_new_section();
3291 sec->header = section_headers[i];
3294 if (sec->header.sh_size) {
3295 switch (sec->header.sh_type) {
3303 if (!loadprogbits) {
3304 sec->contents = NULL;
3311 #if defined(__mips__)
3312 case SHT_MIPS_DWARF:
3314 sec->contents = NULL;
3315 if (sec->header.sh_size > 0) {
3316 sec->contents = xmalloc(sec->header.sh_size);
3317 if (image_size < (sec->header.sh_offset + sec->header.sh_size))
3318 bb_error_msg_and_die("error while loading section data");
3319 memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
3322 #if SHT_RELM == SHT_REL
3324 bb_error_msg_and_die("RELA relocations not supported on this architecture");
3327 bb_error_msg_and_die("REL relocations not supported on this architecture");
3330 if (sec->header.sh_type >= SHT_LOPROC) {
3331 /* Assume processor specific section types are debug
3332 info and can safely be ignored. If this is ever not
3333 the case (Hello MIPS?), don't put ifdefs here but
3334 create an arch_load_proc_section(). */
3338 bb_error_msg_and_die("can't handle sections of type %ld",
3339 (long) sec->header.sh_type);
3344 /* Do what sort of interpretation as needed by each section. */
3346 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3348 for (i = 0; i < shnum; ++i) {
3349 struct obj_section *sec = f->sections[i];
3350 sec->name = shstrtab + sec->header.sh_name;
3353 for (i = 0; i < shnum; ++i) {
3354 struct obj_section *sec = f->sections[i];
3356 /* .modinfo should be contents only but gcc has no attribute for that.
3357 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3359 if (strcmp(sec->name, ".modinfo") == 0)
3360 sec->header.sh_flags &= ~SHF_ALLOC;
3362 if (sec->header.sh_flags & SHF_ALLOC)
3363 obj_insert_section_load_order(f, sec);
3365 switch (sec->header.sh_type) {
3368 unsigned long nsym, j;
3372 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3373 bb_error_msg_and_die("symbol size mismatch: %lu != %lu",
3374 (unsigned long) sec->header.sh_entsize,
3375 (unsigned long) sizeof(ElfW(Sym)));
3378 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3379 strtab = f->sections[sec->header.sh_link]->contents;
3380 sym = (ElfW(Sym) *) sec->contents;
3382 /* Allocate space for a table of local symbols. */
3383 j = f->local_symtab_size = sec->header.sh_info;
3384 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3386 /* Insert all symbols into the hash table. */
3387 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3388 ElfW(Addr) val = sym->st_value;
3391 name = strtab + sym->st_name;
3392 else if (sym->st_shndx < shnum)
3393 name = f->sections[sym->st_shndx]->name;
3396 #if defined(__SH5__)
3398 * For sh64 it is possible that the target of a branch
3399 * requires a mode switch (32 to 16 and back again).
3401 * This is implied by the lsb being set in the target
3402 * address for SHmedia mode and clear for SHcompact.
3404 val |= sym->st_other & 4;
3406 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3413 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3414 bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu",
3415 (unsigned long) sec->header.sh_entsize,
3416 (unsigned long) sizeof(ElfW(RelM)));
3419 /* XXX Relocation code from modutils-2.3.19 is not here.
3420 * Why? That's about 20 lines of code from obj/obj_load.c,
3421 * which gets done in a second pass through the sections.
3422 * This BusyBox insmod does similar work in obj_relocate(). */
3429 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3431 * load the unloaded sections directly into the memory allocated by
3432 * kernel for the module
3435 static int obj_load_progbits(char *image, size_t image_size, struct obj_file *f, char *imagebase)
3437 ElfW(Addr) base = f->baseaddr;
3438 struct obj_section* sec;
3440 for (sec = f->load_order; sec; sec = sec->load_next) {
3441 /* section already loaded? */
3442 if (sec->contents != NULL)
3444 if (sec->header.sh_size == 0)
3446 sec->contents = imagebase + (sec->header.sh_addr - base);
3447 if (image_size < (sec->header.sh_offset + sec->header.sh_size)) {
3448 bb_error_msg("error reading ELF section data");
3449 return 0; /* need to delete half-loaded module! */
3451 memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
3457 static void hide_special_symbols(struct obj_file *f)
3459 static const char *const specials[] = {
3460 SPFX "cleanup_module",
3462 SPFX "kernel_version",
3466 struct obj_symbol *sym;
3467 const char *const *p;
3469 for (p = specials; *p; ++p) {
3470 sym = obj_find_symbol(f, *p);
3472 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3477 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3478 static int obj_gpl_license(struct obj_file *f, const char **license)
3480 struct obj_section *sec;
3481 /* This list must match *exactly* the list of allowable licenses in
3482 * linux/include/linux/module.h. Checking for leading "GPL" will not
3483 * work, somebody will use "GPL sucks, this is proprietary".
3485 static const char *const gpl_licenses[] = {
3488 "GPL and additional rights",
3493 sec = obj_find_section(f, ".modinfo");
3495 const char *value, *ptr, *endptr;
3496 ptr = sec->contents;
3497 endptr = ptr + sec->header.sh_size;
3498 while (ptr < endptr) {
3499 value = strchr(ptr, '=');
3500 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3504 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
3505 if (strcmp(value+1, gpl_licenses[i]) == 0)
3510 ptr = strchr(ptr, '\0');
3520 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3521 #define TAINT_PROPRIETORY_MODULE (1 << 0)
3522 #define TAINT_FORCED_MODULE (1 << 1)
3523 #define TAINT_UNSAFE_SMP (1 << 2)
3524 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3526 static void set_tainted(int fd, const char *m_name,
3527 int kernel_has_tainted, int taint,
3528 const char *text1, const char *text2)
3530 static smallint printed_info;
3535 if (fd < 0 && !kernel_has_tainted)
3536 return; /* New modutils on old kernel */
3537 printf("Warning: loading %s will taint the kernel: %s%s\n",
3538 m_name, text1, text2);
3539 if (!printed_info) {
3540 printf(" See %s for information about tainted modules\n", TAINT_URL);
3544 read(fd, buf, sizeof(buf)-1);
3545 buf[sizeof(buf)-1] = '\0';
3546 oldval = strtoul(buf, NULL, 10);
3547 sprintf(buf, "%d\n", oldval | taint);
3548 xwrite_str(fd, buf);
3552 /* Check if loading this module will taint the kernel. */
3553 static void check_tainted_module(struct obj_file *f, const char *m_name)
3555 int fd, kernel_has_tainted;
3558 kernel_has_tainted = 1;
3559 fd = open(TAINT_FILENAME, O_RDWR);
3561 if (errno == ENOENT)
3562 kernel_has_tainted = 0;
3563 else if (errno == EACCES)
3564 kernel_has_tainted = 1;
3566 bb_simple_perror_msg(TAINT_FILENAME);
3567 kernel_has_tainted = 0;
3571 switch (obj_gpl_license(f, &ptr)) {
3575 set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3577 default: /* case 2: */
3578 /* The module has a non-GPL license so we pretend that the
3579 * kernel always has a taint flag to get a warning even on
3580 * kernels without the proc flag.
3582 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3586 if (flag_force_load)
3587 set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3592 #else /* !FEATURE_CHECK_TAINTED_MODULE */
3593 #define check_tainted_module(x, y) do { } while (0);
3596 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3597 /* add module source, timestamp, kernel version and a symbol for the
3598 * start of some sections. this info is used by ksymoops to do better
3601 #if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3602 #define get_module_version(f, str) get_module_version(str)
3605 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3607 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3608 return new_get_module_version(f, str);
3610 strncpy(str, "???", sizeof(str));
3615 /* add module source, timestamp, kernel version and a symbol for the
3616 * start of some sections. this info is used by ksymoops to do better
3620 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3623 static const char symprefix[] ALIGN1 = "__insmod_";
3624 static const char section_names[][8] = {
3632 struct obj_section *sec;
3633 struct obj_symbol *sym;
3634 char *name, *absolute_filename;
3635 char str[STRVERSIONLEN];
3637 int lm_name, lfilename, use_ksymtab, version;
3638 struct stat statbuf;
3640 /* WARNING: was using realpath, but replaced by readlink to stop using
3641 * lots of stack. But here it seems to be able to cause problems? */
3642 absolute_filename = xmalloc_readlink(filename);
3643 if (!absolute_filename)
3644 absolute_filename = xstrdup(filename);
3646 lm_name = strlen(m_name);
3647 lfilename = strlen(absolute_filename);
3649 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3650 * are not to be exported. otherwise leave ksymtab alone for now, the
3651 * "export all symbols" compatibility code will export these symbols later.
3653 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3655 sec = obj_find_section(f, ".this");
3657 /* tag the module header with the object name, last modified
3658 * timestamp and module version. worst case for module version
3659 * is 0xffffff, decimal 16777215. putting all three fields in
3660 * one symbol is less readable but saves kernel space.
3662 if (stat(absolute_filename, &statbuf) != 0)
3663 statbuf.st_mtime = 0;
3664 version = get_module_version(f, str); /* -1 if not found */
3665 name = xasprintf("%s%s_O%s_M%0*lX_V%d",
3666 symprefix, m_name, absolute_filename,
3667 (int)(2 * sizeof(statbuf.st_mtime)),
3668 (long)statbuf.st_mtime,
3670 sym = obj_add_symbol(f, name, -1,
3671 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3672 sec->idx, sec->header.sh_addr, 0);
3674 new_add_ksymtab(f, sym);
3676 free(absolute_filename);
3677 #ifdef _NOT_SUPPORTED_
3678 /* record where the persistent data is going, same address as previous symbol */
3680 name = xasprintf("%s%s_P%s",
3681 symprefix, m_name, f->persist);
3682 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3683 sec->idx, sec->header.sh_addr, 0);
3685 new_add_ksymtab(f, sym);
3688 /* tag the desired sections if size is non-zero */
3689 for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
3690 sec = obj_find_section(f, section_names[i]);
3691 if (sec && sec->header.sh_size) {
3692 name = xasprintf("%s%s_S%s_L%ld",
3693 symprefix, m_name, sec->name,
3694 (long)sec->header.sh_size);
3695 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3696 sec->idx, sec->header.sh_addr, 0);
3698 new_add_ksymtab(f, sym);
3702 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3704 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3705 static void print_load_map(struct obj_file *f)
3707 struct obj_section *sec;
3708 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3709 struct obj_symbol **all, **p;
3711 char *loaded; /* array of booleans */
3712 struct obj_symbol *sym;
3714 /* Report on the section layout. */
3715 printf("Sections: Size %-*s Align\n",
3716 (int) (2 * sizeof(void *)), "Address");
3718 for (sec = f->load_order; sec; sec = sec->load_next) {
3722 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3727 printf("%-15s %08lx %0*lx 2**%d\n",
3729 (long)sec->header.sh_size,
3730 (int) (2 * sizeof(void *)),
3731 (long)sec->header.sh_addr,
3734 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3735 /* Quick reference which section indices are loaded. */
3736 i = f->header.e_shnum;
3737 loaded = alloca(i * sizeof(loaded[0]));
3739 loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
3741 /* Collect the symbols we'll be listing. */
3742 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3743 for (sym = f->symtab[i]; sym; sym = sym->next)
3744 if (sym->secidx <= SHN_HIRESERVE
3745 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3750 all = alloca(nsyms * sizeof(all[0]));
3752 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3753 for (sym = f->symtab[i]; sym; sym = sym->next)
3754 if (sym->secidx <= SHN_HIRESERVE
3755 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3760 /* And list them. */
3761 printf("\nSymbols:\n");
3762 for (p = all; p < all + nsyms; ++p) {
3764 unsigned long value;
3767 if (sym->secidx == SHN_ABS) {
3770 } else if (sym->secidx == SHN_UNDEF) {
3774 sec = f->sections[sym->secidx];
3776 if (sec->header.sh_type == SHT_NOBITS)
3778 else if (sec->header.sh_flags & SHF_ALLOC) {
3779 if (sec->header.sh_flags & SHF_EXECINSTR)
3781 else if (sec->header.sh_flags & SHF_WRITE)
3786 value = sym->value + sec->header.sh_addr;
3789 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3790 type |= 0x20; /* tolower. safe for '?' too */
3792 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3797 #else /* !FEATURE_INSMOD_LOAD_MAP */
3798 static void print_load_map(struct obj_file *f UNUSED_PARAM)
3803 int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
3806 unsigned long m_size;
3809 int exit_status = EXIT_FAILURE;
3811 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3818 image_size = INT_MAX - 4095;
3820 image = try_to_mmap_module(m_filename, &image_size);
3824 /* Load module into memory and unzip if compressed */
3825 image = xmalloc_open_zipped_read_close(m_filename, &image_size);
3827 return EXIT_FAILURE;
3830 m_name = xstrdup(bb_basename(m_filename));
3831 /* "module.o[.gz]" -> "module" */
3832 *strchrnul(m_name, '.') = '\0';
3834 f = obj_load(image, image_size, LOADBITS);
3836 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3837 /* Version correspondence? */
3838 m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
3840 char m_strversion[STRVERSIONLEN];
3843 if (m_has_modinfo) {
3844 int m_version = new_get_module_version(f, m_strversion);
3845 if (m_version == -1) {
3846 bb_error_msg_and_die("can't find the kernel version "
3847 "the module was compiled for");
3852 if (strncmp(uts.release, m_strversion, STRVERSIONLEN) != 0) {
3853 bb_error_msg("%skernel-module version mismatch\n"
3854 "\t%s was compiled for kernel version %s\n"
3855 "\twhile this kernel is version %s",
3856 flag_force_load ? "warning: " : "",
3857 m_name, m_strversion, uts.release);
3858 if (!flag_force_load)
3864 if (query_module(NULL, 0, NULL, 0, NULL))
3865 bb_error_msg_and_die("old (unsupported) kernel");
3866 new_get_kernel_symbols();
3867 k_crcs = new_is_kernel_checksummed();
3869 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3873 m_crcs = new_is_module_checksummed(f);
3874 if (m_crcs != k_crcs)
3875 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3879 /* Let the module know about the kernel symbols. */
3880 add_kernel_symbols(f);
3882 /* Allocate common symbols, symbol tables, and string tables. */
3883 new_create_this_module(f, m_name);
3884 obj_check_undefineds(f);
3885 obj_allocate_commons(f);
3886 check_tainted_module(f, m_name);
3888 /* Done with the module name, on to the optional var=value arguments */
3889 new_process_module_arguments(f, options);
3892 hide_special_symbols(f);
3894 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3895 add_ksymoops_symbols(f, m_filename, m_name);
3898 new_create_module_ksymtab(f);
3900 /* Find current size of the module */
3901 m_size = obj_load_size(f);
3903 m_addr = create_module(m_name, m_size);
3904 if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
3906 bb_error_msg_and_die("a module named %s already exists", m_name);
3908 bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes",
3911 bb_perror_msg_and_die("create_module: %s", m_name);
3916 * the PROGBITS section was not loaded by the obj_load
3917 * now we can load them directly into the kernel memory
3919 if (!obj_load_progbits(image, image_size, f, (char*)m_addr)) {
3920 delete_module(m_name, 0);
3925 if (!obj_relocate(f, m_addr)) {
3926 delete_module(m_name, 0);
3930 if (!new_init_module(m_name, f, m_size)) {
3931 delete_module(m_name, 0);
3935 if (flag_print_load_map)
3938 exit_status = EXIT_SUCCESS;
3942 munmap(image, image_size);