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 tarball for details.
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
95 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
96 #define SHT_RELM SHT_RELA
97 #define Elf32_RelM Elf32_Rela
98 #define ELFCLASSM ELFCLASS32
102 #if defined(__cris__)
103 #define MATCH_MACHINE(x) (x == EM_CRIS)
104 #define SHT_RELM SHT_RELA
105 #define Elf32_RelM Elf32_Rela
106 #define ELFCLASSM ELFCLASS32
109 #define R_CRIS_NONE 0
115 #if defined(__H8300H__) || defined(__H8300S__)
116 #define MATCH_MACHINE(x) (x == EM_H8_300)
117 #define SHT_RELM SHT_RELA
118 #define Elf32_RelM Elf32_Rela
119 #define ELFCLASSM ELFCLASS32
121 #define SYMBOL_PREFIX "_"
124 /* PA-RISC / HP-PA */
125 #if defined(__hppa__)
126 #define MATCH_MACHINE(x) (x == EM_PARISC)
127 #define SHT_RELM SHT_RELA
128 #if defined(__LP64__)
129 #define Elf64_RelM Elf64_Rela
130 #define ELFCLASSM ELFCLASS64
132 #define Elf32_RelM Elf32_Rela
133 #define ELFCLASSM ELFCLASS32
138 #if defined(__i386__)
140 #define MATCH_MACHINE(x) (x == EM_386)
142 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
144 #define SHT_RELM SHT_REL
145 #define Elf32_RelM Elf32_Rel
146 #define ELFCLASSM ELFCLASS32
147 #define USE_GOT_ENTRIES
148 #define GOT_ENTRY_SIZE 4
152 /* IA64, aka Itanium */
153 #if defined(__ia64__)
154 #define MATCH_MACHINE(x) (x == EM_IA_64)
155 #define SHT_RELM SHT_RELA
156 #define Elf64_RelM Elf64_Rela
157 #define ELFCLASSM ELFCLASS64
161 #if defined(__mc68000__)
162 #define MATCH_MACHINE(x) (x == EM_68K)
163 #define SHT_RELM SHT_RELA
164 #define Elf32_RelM Elf32_Rela
165 #define ELFCLASSM ELFCLASS32
166 #define USE_GOT_ENTRIES
167 #define GOT_ENTRY_SIZE 4
172 #if defined(__microblaze__)
174 #include <linux/elf-em.h>
175 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
176 #define SHT_RELM SHT_RELA
177 #define Elf32_RelM Elf32_Rela
178 #define ELFCLASSM ELFCLASS32
182 #if defined(__mips__)
183 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
184 #define SHT_RELM SHT_REL
185 #define Elf32_RelM Elf32_Rel
186 #define ELFCLASSM ELFCLASS32
187 /* Account for ELF spec changes. */
188 #ifndef EM_MIPS_RS3_LE
189 #ifdef EM_MIPS_RS4_BE
190 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
192 #define EM_MIPS_RS3_LE 10
194 #endif /* !EM_MIPS_RS3_LE */
195 #define ARCHDATAM "__dbe_table"
199 #if defined(__nios2__)
200 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
201 #define SHT_RELM SHT_RELA
202 #define Elf32_RelM Elf32_Rela
203 #define ELFCLASSM ELFCLASS32
207 #if defined(__powerpc64__)
208 #define MATCH_MACHINE(x) (x == EM_PPC64)
209 #define SHT_RELM SHT_RELA
210 #define Elf64_RelM Elf64_Rela
211 #define ELFCLASSM ELFCLASS64
212 #elif defined(__powerpc__)
213 #define MATCH_MACHINE(x) (x == EM_PPC)
214 #define SHT_RELM SHT_RELA
215 #define Elf32_RelM Elf32_Rela
216 #define ELFCLASSM ELFCLASS32
217 #define USE_PLT_ENTRIES
218 #define PLT_ENTRY_SIZE 16
220 #define LIST_ARCHTYPE ElfW(Addr)
222 #define ARCHDATAM "__ftr_fixup"
226 #if defined(__s390__)
227 #define MATCH_MACHINE(x) (x == EM_S390)
228 #define SHT_RELM SHT_RELA
229 #define Elf32_RelM Elf32_Rela
230 #define ELFCLASSM ELFCLASS32
231 #define USE_PLT_ENTRIES
232 #define PLT_ENTRY_SIZE 8
233 #define USE_GOT_ENTRIES
234 #define GOT_ENTRY_SIZE 8
240 #define MATCH_MACHINE(x) (x == EM_SH)
241 #define SHT_RELM SHT_RELA
242 #define Elf32_RelM Elf32_Rela
243 #define ELFCLASSM ELFCLASS32
244 #define USE_GOT_ENTRIES
245 #define GOT_ENTRY_SIZE 4
247 /* the SH changes have only been tested in =little endian= mode */
248 /* I'm not sure about big endian, so let's warn: */
249 #if defined(__sh__) && BB_BIG_ENDIAN
250 # error insmod.c may require changes for use on big endian SH
252 /* it may or may not work on the SH1/SH2... Error on those also */
253 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
254 #error insmod.c may require changes for SH1 or SH2 use
259 #if defined(__sparc__)
260 #define MATCH_MACHINE(x) (x == EM_SPARC)
261 #define SHT_RELM SHT_RELA
262 #define Elf32_RelM Elf32_Rela
263 #define ELFCLASSM ELFCLASS32
267 #if defined(__v850e__)
268 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
269 #define SHT_RELM SHT_RELA
270 #define Elf32_RelM Elf32_Rela
271 #define ELFCLASSM ELFCLASS32
272 #define USE_PLT_ENTRIES
273 #define PLT_ENTRY_SIZE 8
275 #ifndef EM_CYGNUS_V850 /* grumble */
276 #define EM_CYGNUS_V850 0x9080
278 #define SYMBOL_PREFIX "_"
282 #if defined(__x86_64__)
283 #define MATCH_MACHINE(x) (x == EM_X86_64)
284 #define SHT_RELM SHT_RELA
285 #define USE_GOT_ENTRIES
286 #define GOT_ENTRY_SIZE 8
288 #define Elf64_RelM Elf64_Rela
289 #define ELFCLASSM ELFCLASS64
293 #error Sorry, but insmod.c does not yet support this architecture...
297 //----------------------------------------------------------------------------
298 //--------modutils module.h, lines 45-242
299 //----------------------------------------------------------------------------
301 /* Definitions for the Linux module syscall interface.
302 Copyright 1996, 1997 Linux International.
304 Contributed by Richard Henderson <rth@tamu.edu>
306 This file is part of the Linux modutils.
308 This program is free software; you can redistribute it and/or modify it
309 under the terms of the GNU General Public License as published by the
310 Free Software Foundation; either version 2 of the License, or (at your
311 option) any later version.
313 This program is distributed in the hope that it will be useful, but
314 WITHOUT ANY WARRANTY; without even the implied warranty of
315 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
316 General Public License for more details.
318 You should have received a copy of the GNU General Public License
319 along with this program; if not, write to the Free Software Foundation,
320 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
323 #ifndef MODUTILS_MODULE_H
325 /*======================================================================*/
326 /* For sizeof() which are related to the module platform and not to the
327 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
329 #define tgt_sizeof_char sizeof(char)
330 #define tgt_sizeof_short sizeof(short)
331 #define tgt_sizeof_int sizeof(int)
332 #define tgt_sizeof_long sizeof(long)
333 #define tgt_sizeof_char_p sizeof(char *)
334 #define tgt_sizeof_void_p sizeof(void *)
335 #define tgt_long long
337 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
338 #undef tgt_sizeof_long
339 #undef tgt_sizeof_char_p
340 #undef tgt_sizeof_void_p
344 tgt_sizeof_char_p = 8,
345 tgt_sizeof_void_p = 8
347 #define tgt_long long long
350 /*======================================================================*/
351 /* The structures used in Linux 2.1. */
353 /* Note: new_module_symbol does not use tgt_long intentionally */
354 struct new_module_symbol {
359 struct new_module_persist;
361 struct new_module_ref {
362 unsigned tgt_long dep; /* kernel addresses */
363 unsigned tgt_long ref;
364 unsigned tgt_long next_ref;
368 unsigned tgt_long size_of_struct; /* == sizeof(module) */
369 unsigned tgt_long next;
370 unsigned tgt_long name;
371 unsigned tgt_long size;
374 unsigned tgt_long flags; /* AUTOCLEAN et al */
379 unsigned tgt_long syms;
380 unsigned tgt_long deps;
381 unsigned tgt_long refs;
382 unsigned tgt_long init;
383 unsigned tgt_long cleanup;
384 unsigned tgt_long ex_table_start;
385 unsigned tgt_long ex_table_end;
387 unsigned tgt_long gp;
389 /* Everything after here is extension. */
390 unsigned tgt_long persist_start;
391 unsigned tgt_long persist_end;
392 unsigned tgt_long can_unload;
393 unsigned tgt_long runsize;
394 const char *kallsyms_start; /* All symbols for kernel debugging */
395 const char *kallsyms_end;
396 const char *archdata_start; /* arch specific data for module */
397 const char *archdata_end;
398 const char *kernel_data; /* Reserved for kernel internal use */
402 #define ARCHDATA_SEC_NAME ARCHDATAM
404 #define ARCHDATA_SEC_NAME "__archdata"
406 #define KALLSYMS_SEC_NAME "__kallsyms"
409 struct new_module_info {
416 /* Bits of module.flags. */
420 NEW_MOD_AUTOCLEAN = 4,
422 NEW_MOD_USED_ONCE = 16
425 int init_module(const char *name, const struct new_module *);
426 int query_module(const char *name, int which, void *buf,
427 size_t bufsize, size_t *ret);
429 /* Values for query_module's which. */
438 /*======================================================================*/
439 /* The system calls unchanged between 2.0 and 2.1. */
441 unsigned long create_module(const char *, size_t);
442 int delete_module(const char *module, unsigned int flags);
445 #endif /* module.h */
447 //----------------------------------------------------------------------------
448 //--------end of modutils module.h
449 //----------------------------------------------------------------------------
453 //----------------------------------------------------------------------------
454 //--------modutils obj.h, lines 253-462
455 //----------------------------------------------------------------------------
457 /* Elf object file loading and relocation routines.
458 Copyright 1996, 1997 Linux International.
460 Contributed by Richard Henderson <rth@tamu.edu>
462 This file is part of the Linux modutils.
464 This program is free software; you can redistribute it and/or modify it
465 under the terms of the GNU General Public License as published by the
466 Free Software Foundation; either version 2 of the License, or (at your
467 option) any later version.
469 This program is distributed in the hope that it will be useful, but
470 WITHOUT ANY WARRANTY; without even the implied warranty of
471 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
472 General Public License for more details.
474 You should have received a copy of the GNU General Public License
475 along with this program; if not, write to the Free Software Foundation,
476 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
479 #ifndef MODUTILS_OBJ_H
481 /* The relocatable object is manipulated using elfin types. */
487 # if ELFCLASSM == ELFCLASS32
488 # define ElfW(x) Elf32_ ## x
489 # define ELFW(x) ELF32_ ## x
491 # define ElfW(x) Elf64_ ## x
492 # define ELFW(x) ELF64_ ## x
496 /* For some reason this is missing from some ancient C libraries.... */
497 #ifndef ELF32_ST_INFO
498 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
501 #ifndef ELF64_ST_INFO
502 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
505 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
506 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
507 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
508 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
509 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
511 struct obj_string_patch;
512 struct obj_symbol_patch;
519 struct obj_section *load_next;
525 struct obj_symbol *next; /* hash table link */
529 int secidx; /* the defining section index/module */
531 int ksymidx; /* for export to the kernel symtab */
532 int referenced; /* actually used in the link */
535 /* Hardcode the hash table size. We shouldn't be needing so many
536 symbols that we begin to degrade performance, and we get a big win
537 by giving the compiler a constant divisor. */
539 #define HASH_BUCKETS 521
544 struct obj_section **sections;
545 struct obj_section *load_order;
546 struct obj_section **load_order_search_start;
547 struct obj_string_patch *string_patches;
548 struct obj_symbol_patch *symbol_patches;
549 int (*symbol_cmp)(const char *, const char *);
550 unsigned long (*symbol_hash)(const char *);
551 unsigned long local_symtab_size;
552 struct obj_symbol **local_symtab;
553 struct obj_symbol *symtab[HASH_BUCKETS];
563 struct obj_string_patch {
564 struct obj_string_patch *next;
566 ElfW(Addr) reloc_offset;
567 ElfW(Addr) string_offset;
570 struct obj_symbol_patch {
571 struct obj_symbol_patch *next;
573 ElfW(Addr) reloc_offset;
574 struct obj_symbol *sym;
578 /* Generic object manipulation routines. */
580 static unsigned long obj_elf_hash(const char *);
582 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
584 static struct obj_symbol *obj_find_symbol(struct obj_file *f,
587 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
588 struct obj_symbol *sym);
590 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
591 static void obj_set_symbol_compare(struct obj_file *f,
592 int (*cmp)(const char *, const char *),
593 unsigned long (*hash)(const char *));
596 static struct obj_section *obj_find_section(struct obj_file *f,
599 static void obj_insert_section_load_order(struct obj_file *f,
600 struct obj_section *sec);
602 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
607 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
612 static void *obj_extend_section(struct obj_section *sec, unsigned long more);
614 static void obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
617 static void obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
618 struct obj_symbol *sym);
620 static void obj_check_undefineds(struct obj_file *f);
622 static void obj_allocate_commons(struct obj_file *f);
624 static unsigned long obj_load_size(struct obj_file *f);
626 static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
628 static struct obj_file *obj_load(int fd, int loadprogbits);
630 static int obj_create_image(struct obj_file *f, char *image);
632 /* Architecture specific manipulation routines. */
634 static struct obj_file *arch_new_file(void);
636 static struct obj_section *arch_new_section(void);
638 static struct obj_symbol *arch_new_symbol(void);
640 static enum obj_reloc arch_apply_relocation(struct obj_file *f,
641 struct obj_section *targsec,
642 /*struct obj_section *symsec,*/
643 struct obj_symbol *sym,
644 ElfW(RelM) *rel, ElfW(Addr) value);
646 static void arch_create_got(struct obj_file *f);
647 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
648 static int obj_gpl_license(struct obj_file *f, const char **license);
651 //----------------------------------------------------------------------------
652 //--------end of modutils obj.h
653 //----------------------------------------------------------------------------
656 /* SPFX is always a string, so it can be concatenated to string constants. */
658 #define SPFX SYMBOL_PREFIX
663 enum { STRVERSIONLEN = 64 };
665 /*======================================================================*/
667 #define flag_force_load (option_mask32 & INSMOD_OPT_FORCE)
668 #define flag_autoclean (option_mask32 & INSMOD_OPT_KERNELD)
669 #define flag_verbose (option_mask32 & INSMOD_OPT_VERBOSE)
670 #define flag_quiet (option_mask32 & INSMOD_OPT_SILENT)
671 #define flag_noexport (option_mask32 & INSMOD_OPT_NO_EXPORT)
672 #define flag_print_load_map (option_mask32 & INSMOD_OPT_PRINT_MAP)
674 /*======================================================================*/
676 #if defined(USE_LIST)
678 struct arch_list_entry
680 struct arch_list_entry *next;
681 LIST_ARCHTYPE addend;
688 #if defined(USE_SINGLE)
690 struct arch_single_entry
699 #if defined(__mips__)
702 struct mips_hi16 *next;
709 struct obj_file root;
710 #if defined(USE_PLT_ENTRIES)
711 struct obj_section *plt;
713 #if defined(USE_GOT_ENTRIES)
714 struct obj_section *got;
716 #if defined(__mips__)
717 struct mips_hi16 *mips_hi16_list;
722 struct obj_symbol root;
723 #if defined(USE_PLT_ENTRIES)
724 #if defined(USE_PLT_LIST)
725 struct arch_list_entry *pltent;
727 struct arch_single_entry pltent;
730 #if defined(USE_GOT_ENTRIES)
731 struct arch_single_entry gotent;
736 struct external_module {
741 struct new_module_symbol *syms;
744 static struct new_module_symbol *ksyms;
745 static size_t nksyms;
747 static struct external_module *ext_modules;
748 static int n_ext_modules;
749 static int n_ext_modules_used;
751 /*======================================================================*/
754 static struct obj_file *arch_new_file(void)
757 f = xzalloc(sizeof(*f));
758 return &f->root; /* it's a first member */
761 static struct obj_section *arch_new_section(void)
763 return xzalloc(sizeof(struct obj_section));
766 static struct obj_symbol *arch_new_symbol(void)
768 struct arch_symbol *sym;
769 sym = xzalloc(sizeof(*sym));
773 static enum obj_reloc
774 arch_apply_relocation(struct obj_file *f,
775 struct obj_section *targsec,
776 /*struct obj_section *symsec,*/
777 struct obj_symbol *sym,
778 ElfW(RelM) *rel, ElfW(Addr) v)
780 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
781 || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \
782 || defined(__powerpc__) || defined(__mips__)
783 struct arch_file *ifile = (struct arch_file *) f;
785 enum obj_reloc ret = obj_reloc_ok;
786 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
787 #if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
788 || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
789 || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
790 || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
791 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
793 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
794 struct arch_symbol *isym = (struct arch_symbol *) sym;
796 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
797 || defined(__sh__) || defined(__s390__)
798 #if defined(USE_GOT_ENTRIES)
799 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
802 #if defined(USE_PLT_ENTRIES)
803 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
805 # if defined(USE_PLT_LIST)
806 struct arch_list_entry *pe;
808 struct arch_single_entry *pe;
812 switch (ELF_R_TYPE(rel->r_info)) {
827 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
828 * (which is .got) similar to branch,
829 * but is full 32 bits relative */
838 case R_ARM_GOTOFF: /* address relative to the got */
842 #elif defined(__cris__)
848 /* CRIS keeps the relocation value in the r_addend field and
849 * should not use whats in *loc at all
854 #elif defined(__H8300H__) || defined(__H8300S__)
857 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
858 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
869 if ((ElfW(Sword))v > 0x7fff ||
870 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
871 ret = obj_reloc_overflow;
873 *(unsigned short *)loc = v;
877 if ((ElfW(Sword))v > 0x7f ||
878 (ElfW(Sword))v < -(ElfW(Sword))0x80)
879 ret = obj_reloc_overflow;
881 *(unsigned char *)loc = v;
884 #elif defined(__i386__)
916 #elif defined(__microblaze__)
917 case R_MICROBLAZE_NONE:
918 case R_MICROBLAZE_64_NONE:
919 case R_MICROBLAZE_32_SYM_OP_SYM:
920 case R_MICROBLAZE_32_PCREL:
923 case R_MICROBLAZE_64_PCREL: {
924 /* dot is the address of the current instruction.
925 * v is the target symbol address.
926 * So we need to extract the offset in the code,
927 * adding v, then subtrating the current address
928 * of this instruction.
929 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
932 /* Get split offset stored in code */
933 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
936 /* Adjust relative offset. -4 adjustment required
937 * because dot points to the IMM insn, but branch
938 * is computed relative to the branch instruction itself.
942 /* Store back into code */
943 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
944 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
949 case R_MICROBLAZE_32:
953 case R_MICROBLAZE_64: {
954 /* Get split pointer stored in code */
955 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
958 /* Add reloc offset */
961 /* Store back into code */
962 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
963 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
968 case R_MICROBLAZE_32_PCREL_LO:
969 case R_MICROBLAZE_32_LO:
970 case R_MICROBLAZE_SRO32:
971 case R_MICROBLAZE_SRW32:
972 ret = obj_reloc_unhandled;
975 #elif defined(__mc68000__)
986 ret = obj_reloc_overflow;
993 ret = obj_reloc_overflow;
1000 if ((ElfW(Sword))v > 0x7f
1001 || (ElfW(Sword))v < -(ElfW(Sword))0x80
1003 ret = obj_reloc_overflow;
1010 if ((ElfW(Sword))v > 0x7fff
1011 || (ElfW(Sword))v < -(ElfW(Sword))0x8000
1013 ret = obj_reloc_overflow;
1019 *(int *)loc = v - dot;
1022 case R_68K_GLOB_DAT:
1023 case R_68K_JMP_SLOT:
1027 case R_68K_RELATIVE:
1028 *(int *)loc += f->baseaddr;
1034 # ifdef R_68K_GOTOFF
1040 #elif defined(__mips__)
1051 ret = obj_reloc_dangerous;
1052 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1053 ret = obj_reloc_overflow;
1055 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1061 struct mips_hi16 *n;
1063 /* We cannot relocate this one now because we don't know the value
1064 of the carry we need to add. Save the information, and let LO16
1065 do the actual relocation. */
1066 n = xmalloc(sizeof *n);
1069 n->next = ifile->mips_hi16_list;
1070 ifile->mips_hi16_list = n;
1076 unsigned long insnlo = *loc;
1077 ElfW(Addr) val, vallo;
1079 /* Sign extend the addend we extract from the lo insn. */
1080 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1082 if (ifile->mips_hi16_list != NULL) {
1083 struct mips_hi16 *l;
1085 l = ifile->mips_hi16_list;
1087 struct mips_hi16 *next;
1090 /* Do the HI16 relocation. Note that we actually don't
1091 need to know anything about the LO16 itself, except where
1092 to find the low 16 bits of the addend needed by the LO16. */
1095 ((insn & 0xffff) << 16) +
1099 /* Account for the sign extension that will happen in the
1106 insn = (insn & ~0xffff) | val;
1114 ifile->mips_hi16_list = NULL;
1117 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1119 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1124 #elif defined(__nios2__)
1129 case R_NIOS2_BFD_RELOC_32:
1133 case R_NIOS2_BFD_RELOC_16:
1135 ret = obj_reloc_overflow;
1140 case R_NIOS2_BFD_RELOC_8:
1142 ret = obj_reloc_overflow;
1151 if ((Elf32_Sword)v > 0x7fff
1152 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1154 ret = obj_reloc_overflow;
1158 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1168 ret = obj_reloc_overflow;
1172 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1177 case R_NIOS2_PCREL16:
1182 if ((Elf32_Sword)v > 0x7fff
1183 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1185 ret = obj_reloc_overflow;
1189 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1195 Elf32_Addr word, gp;
1197 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1199 if ((Elf32_Sword)v > 0x7fff
1200 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1202 ret = obj_reloc_overflow;
1206 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1210 case R_NIOS2_CALL26:
1212 ret = obj_reloc_dangerous;
1213 if ((v >> 28) != (dot >> 28))
1214 ret = obj_reloc_overflow;
1215 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1223 ret = obj_reloc_overflow;
1226 word = *loc & ~0x7c0;
1227 *loc = word | ((v & 0x1f) << 6);
1236 ret = obj_reloc_overflow;
1239 word = *loc & ~0xfc0;
1240 *loc = word | ((v & 0x3f) << 6);
1249 ret = obj_reloc_overflow;
1252 word = *loc & ~0x3fc0;
1253 *loc = word | ((v & 0xff) << 6);
1262 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1272 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1277 case R_NIOS2_HIADJ16:
1279 Elf32_Addr word1, word2;
1282 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1283 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1288 #elif defined(__powerpc64__)
1289 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1291 #elif defined(__powerpc__)
1293 case R_PPC_ADDR16_HA:
1294 *(unsigned short *)loc = (v + 0x8000) >> 16;
1297 case R_PPC_ADDR16_HI:
1298 *(unsigned short *)loc = v >> 16;
1301 case R_PPC_ADDR16_LO:
1302 *(unsigned short *)loc = v;
1316 #elif defined(__s390__)
1319 *(unsigned int *) loc += v;
1322 *(unsigned short *) loc += v;
1325 *(unsigned char *) loc += v;
1329 *(unsigned int *) loc += v - dot;
1332 *(unsigned short *) loc += (v - dot) >> 1;
1335 *(unsigned short *) loc += v - dot;
1339 case R_390_PLT16DBL:
1340 /* find the plt entry and initialize it. */
1341 pe = (struct arch_single_entry *) &isym->pltent;
1342 if (pe->inited == 0) {
1343 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1344 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1346 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1353 /* Insert relative distance to target. */
1354 v = plt + pe->offset - dot;
1355 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1356 *(unsigned int *) loc = (unsigned int) v;
1357 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1358 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1361 case R_390_GLOB_DAT:
1362 case R_390_JMP_SLOT:
1366 case R_390_RELATIVE:
1367 *loc += f->baseaddr;
1371 *(unsigned long *) loc += got - dot;
1377 if (!isym->gotent.inited)
1379 isym->gotent.inited = 1;
1380 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1382 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1383 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1384 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1385 *(unsigned short *) loc += isym->gotent.offset;
1386 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1387 *(unsigned int *) loc += isym->gotent.offset;
1390 # ifndef R_390_GOTOFF32
1391 # define R_390_GOTOFF32 R_390_GOTOFF
1393 case R_390_GOTOFF32:
1397 #elif defined(__sh__)
1420 *loc = f->baseaddr + rel->r_addend;
1424 *loc = got - dot + rel->r_addend;
1434 # if defined(__SH5__)
1435 case R_SH_IMM_MEDLOW16:
1436 case R_SH_IMM_LOW16:
1440 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1444 * movi and shori have the format:
1446 * | op | imm | reg | reserved |
1447 * 31..26 25..10 9.. 4 3 .. 0
1449 * so we simply mask and or in imm.
1451 word = *loc & ~0x3fffc00;
1452 word |= (v & 0xffff) << 10;
1459 case R_SH_IMM_MEDLOW16_PCREL:
1460 case R_SH_IMM_LOW16_PCREL:
1464 word = *loc & ~0x3fffc00;
1468 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1471 word |= (v & 0xffff) << 10;
1477 # endif /* __SH5__ */
1479 #elif defined(__v850e__)
1485 /* We write two shorts instead of a long because even
1486 32-bit insns only need half-word alignment, but
1487 32-bit data needs to be long-word aligned. */
1488 v += ((unsigned short *)loc)[0];
1489 v += ((unsigned short *)loc)[1] << 16;
1490 ((unsigned short *)loc)[0] = v & 0xffff;
1491 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1494 case R_V850_22_PCREL:
1497 #elif defined(__x86_64__)
1507 *(unsigned int *) loc += v;
1510 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1511 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1516 *(signed int *) loc += v;
1520 *(unsigned short *) loc += v;
1524 *(unsigned char *) loc += v;
1528 *(unsigned int *) loc += v - dot;
1532 *(unsigned short *) loc += v - dot;
1536 *(unsigned char *) loc += v - dot;
1539 case R_X86_64_GLOB_DAT:
1540 case R_X86_64_JUMP_SLOT:
1544 case R_X86_64_RELATIVE:
1545 *loc += f->baseaddr;
1548 case R_X86_64_GOT32:
1549 case R_X86_64_GOTPCREL:
1552 if (!isym->gotent.reloc_done)
1554 isym->gotent.reloc_done = 1;
1555 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1557 /* XXX are these really correct? */
1558 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1559 *(unsigned int *) loc += v + isym->gotent.offset;
1561 *loc += isym->gotent.offset;
1566 # warning "no idea how to handle relocations on your arch"
1570 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1571 ret = obj_reloc_unhandled;
1574 #if defined(USE_PLT_ENTRIES)
1578 /* find the plt entry and initialize it if necessary */
1580 #if defined(USE_PLT_LIST)
1581 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1588 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1590 /* generate some machine code */
1592 #if defined(__arm__)
1593 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1594 ip[1] = v; /* sym@ */
1596 #if defined(__powerpc__)
1597 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1598 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1599 ip[2] = 0x7d6903a6; /* mtctr r11 */
1600 ip[3] = 0x4e800420; /* bctr */
1602 #if defined(__v850e__)
1603 /* We have to trash a register, so we assume that any control
1604 transfer more than 21-bits away must be a function call
1605 (so we can use a call-clobbered register). */
1606 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1607 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1612 /* relative distance to target */
1614 /* if the target is too far away.... */
1615 #if defined(__arm__) || defined(__powerpc__)
1616 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1617 #elif defined(__v850e__)
1618 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1620 /* go via the plt */
1621 v = plt + pe->offset - dot;
1623 #if defined(__v850e__)
1628 ret = obj_reloc_dangerous;
1630 /* merge the offset into the instruction. */
1631 #if defined(__arm__)
1632 /* Convert to words. */
1635 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1637 #if defined(__powerpc__)
1638 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1640 #if defined(__v850e__)
1641 /* We write two shorts instead of a long because even 32-bit insns
1642 only need half-word alignment, but the 32-bit data write needs
1643 to be long-word aligned. */
1644 ((unsigned short *)loc)[0] =
1645 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1646 | ((v >> 16) & 0x3f); /* offs high part */
1647 ((unsigned short *)loc)[1] =
1648 (v & 0xffff); /* offs low part */
1651 #endif /* USE_PLT_ENTRIES */
1653 #if defined(USE_GOT_ENTRIES)
1656 /* needs an entry in the .got: set it, once */
1657 if (!isym->gotent.inited) {
1658 isym->gotent.inited = 1;
1659 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1661 /* make the reloc with_respect_to_.got */
1663 *loc += isym->gotent.offset + rel->r_addend;
1664 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1665 *loc += isym->gotent.offset;
1669 #endif /* USE_GOT_ENTRIES */
1676 #if defined(USE_LIST)
1678 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1679 int offset, int size)
1681 struct arch_list_entry *pe;
1683 for (pe = *list; pe != NULL; pe = pe->next) {
1684 if (pe->addend == rel->r_addend) {
1690 pe = xzalloc(sizeof(struct arch_list_entry));
1692 pe->addend = rel->r_addend;
1693 pe->offset = offset;
1703 #if defined(USE_SINGLE)
1705 static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
1706 int offset, int size)
1708 if (single->allocated == 0) {
1709 single->allocated = 1;
1710 single->offset = offset;
1719 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1721 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
1722 int offset, int size)
1724 struct obj_section *myrelsec = obj_find_section(f, name);
1731 obj_extend_section(myrelsec, offset);
1733 myrelsec = obj_create_alloced_section(f, name,
1742 static void arch_create_got(struct obj_file *f)
1744 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1745 struct arch_file *ifile = (struct arch_file *) f;
1747 #if defined(USE_GOT_ENTRIES)
1748 int got_offset = 0, got_needed = 0, got_allocate;
1750 #if defined(USE_PLT_ENTRIES)
1751 int plt_offset = 0, plt_needed = 0, plt_allocate;
1753 struct obj_section *relsec, *symsec, *strsec;
1754 ElfW(RelM) *rel, *relend;
1755 ElfW(Sym) *symtab, *extsym;
1756 const char *strtab, *name;
1757 struct arch_symbol *intsym;
1759 for (i = 0; i < f->header.e_shnum; ++i) {
1760 relsec = f->sections[i];
1761 if (relsec->header.sh_type != SHT_RELM)
1764 symsec = f->sections[relsec->header.sh_link];
1765 strsec = f->sections[symsec->header.sh_link];
1767 rel = (ElfW(RelM) *) relsec->contents;
1768 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1769 symtab = (ElfW(Sym) *) symsec->contents;
1770 strtab = (const char *) strsec->contents;
1772 for (; rel < relend; ++rel) {
1773 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1775 #if defined(USE_GOT_ENTRIES)
1778 #if defined(USE_PLT_ENTRIES)
1782 switch (ELF_R_TYPE(rel->r_info)) {
1783 #if defined(__arm__)
1798 #elif defined(__i386__)
1808 #elif defined(__powerpc__)
1813 #elif defined(__mc68000__)
1824 #elif defined(__sh__)
1834 #elif defined(__v850e__)
1835 case R_V850_22_PCREL:
1844 if (extsym->st_name != 0) {
1845 name = strtab + extsym->st_name;
1847 name = f->sections[extsym->st_shndx]->name;
1849 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1850 #if defined(USE_GOT_ENTRIES)
1852 got_offset += arch_single_init(
1853 /*rel,*/ &intsym->gotent,
1854 got_offset, GOT_ENTRY_SIZE);
1859 #if defined(USE_PLT_ENTRIES)
1861 #if defined(USE_PLT_LIST)
1862 plt_offset += arch_list_add(
1863 rel, &intsym->pltent,
1864 plt_offset, PLT_ENTRY_SIZE);
1866 plt_offset += arch_single_init(
1867 /*rel,*/ &intsym->pltent,
1868 plt_offset, PLT_ENTRY_SIZE);
1876 #if defined(USE_GOT_ENTRIES)
1878 ifile->got = arch_xsect_init(f, ".got", got_offset,
1883 #if defined(USE_PLT_ENTRIES)
1885 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1890 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1893 /*======================================================================*/
1895 /* Standard ELF hash function. */
1896 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1898 unsigned long h = 0;
1905 g = (h & 0xf0000000);
1915 static unsigned long obj_elf_hash(const char *name)
1917 return obj_elf_hash_n(name, strlen(name));
1920 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1921 /* String comparison for non-co-versioned kernel and module. */
1923 static int ncv_strcmp(const char *a, const char *b)
1925 size_t alen = strlen(a), blen = strlen(b);
1927 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1928 return strncmp(a, b, alen);
1929 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1930 return strncmp(a, b, blen);
1932 return strcmp(a, b);
1935 /* String hashing for non-co-versioned kernel and module. Here
1936 we are simply forced to drop the crc from the hash. */
1938 static unsigned long ncv_symbol_hash(const char *str)
1940 size_t len = strlen(str);
1941 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1943 return obj_elf_hash_n(str, len);
1947 obj_set_symbol_compare(struct obj_file *f,
1948 int (*cmp) (const char *, const char *),
1949 unsigned long (*hash) (const char *))
1952 f->symbol_cmp = cmp;
1954 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1957 f->symbol_hash = hash;
1959 memcpy(tmptab, f->symtab, sizeof(tmptab));
1960 memset(f->symtab, 0, sizeof(f->symtab));
1962 for (i = 0; i < HASH_BUCKETS; ++i)
1963 for (sym = tmptab[i]; sym; sym = next) {
1964 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1966 sym->next = f->symtab[h];
1972 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
1974 static struct obj_symbol *
1975 obj_add_symbol(struct obj_file *f, const char *name,
1976 unsigned long symidx, int info,
1977 int secidx, ElfW(Addr) value,
1980 struct obj_symbol *sym;
1981 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1982 int n_type = ELF_ST_TYPE(info);
1983 int n_binding = ELF_ST_BIND(info);
1985 for (sym = f->symtab[hash]; sym; sym = sym->next) {
1986 if (f->symbol_cmp(sym->name, name) == 0) {
1987 int o_secidx = sym->secidx;
1988 int o_info = sym->info;
1989 int o_type = ELF_ST_TYPE(o_info);
1990 int o_binding = ELF_ST_BIND(o_info);
1992 /* A redefinition! Is it legal? */
1994 if (secidx == SHN_UNDEF)
1996 else if (o_secidx == SHN_UNDEF)
1998 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1999 /* Cope with local and global symbols of the same name
2000 in the same object file, as might have been created
2001 by ld -r. The only reason locals are now seen at this
2002 level at all is so that we can do semi-sensible things
2005 struct obj_symbol *nsym, **p;
2007 nsym = arch_new_symbol();
2008 nsym->next = sym->next;
2011 /* Excise the old (local) symbol from the hash chain. */
2012 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2016 } else if (n_binding == STB_LOCAL) {
2017 /* Another symbol of the same name has already been defined.
2018 Just add this to the local table. */
2019 sym = arch_new_symbol();
2022 f->local_symtab[symidx] = sym;
2024 } else if (n_binding == STB_WEAK)
2026 else if (o_binding == STB_WEAK)
2028 /* Don't unify COMMON symbols with object types the programmer
2030 else if (secidx == SHN_COMMON
2031 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2033 else if (o_secidx == SHN_COMMON
2034 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2037 /* Don't report an error if the symbol is coming from
2038 the kernel or some external module. */
2039 if (secidx <= SHN_HIRESERVE)
2040 bb_error_msg("%s multiply defined", name);
2046 /* Completely new symbol. */
2047 sym = arch_new_symbol();
2048 sym->next = f->symtab[hash];
2049 f->symtab[hash] = sym;
2051 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
2052 if (symidx >= f->local_symtab_size)
2053 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2054 name, (long) symidx, (long) f->local_symtab_size);
2056 f->local_symtab[symidx] = sym;
2063 sym->secidx = secidx;
2069 static struct obj_symbol *
2070 obj_find_symbol(struct obj_file *f, const char *name)
2072 struct obj_symbol *sym;
2073 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2075 for (sym = f->symtab[hash]; sym; sym = sym->next)
2076 if (f->symbol_cmp(sym->name, name) == 0)
2081 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2084 if (sym->secidx >= SHN_LORESERVE)
2086 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2088 /* As a special case, a NULL sym has value zero. */
2092 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2094 int i, n = f->header.e_shnum;
2096 for (i = 0; i < n; ++i)
2097 if (strcmp(f->sections[i]->name, name) == 0)
2098 return f->sections[i];
2102 static int obj_load_order_prio(struct obj_section *a)
2104 unsigned long af, ac;
2106 af = a->header.sh_flags;
2109 if (a->name[0] != '.' || strlen(a->name) != 10
2110 || strcmp(a->name + 5, ".init") != 0
2116 if (!(af & SHF_WRITE))
2118 if (af & SHF_EXECINSTR)
2120 if (a->header.sh_type != SHT_NOBITS)
2127 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2129 struct obj_section **p;
2130 int prio = obj_load_order_prio(sec);
2131 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2132 if (obj_load_order_prio(*p) < prio)
2134 sec->load_next = *p;
2138 static struct obj_section *helper_create_alloced_section(struct obj_file *f,
2140 unsigned long align,
2143 int newidx = f->header.e_shnum++;
2144 struct obj_section *sec;
2146 f->sections = xrealloc_vector(f->sections, 2, newidx);
2147 f->sections[newidx] = sec = arch_new_section();
2149 sec->header.sh_type = SHT_PROGBITS;
2150 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2151 sec->header.sh_size = size;
2152 sec->header.sh_addralign = align;
2156 sec->contents = xzalloc(size);
2161 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2163 unsigned long align,
2166 struct obj_section *sec;
2168 sec = helper_create_alloced_section(f, name, align, size);
2169 obj_insert_section_load_order(f, sec);
2173 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2175 unsigned long align,
2178 struct obj_section *sec;
2180 sec = helper_create_alloced_section(f, name, align, size);
2181 sec->load_next = f->load_order;
2182 f->load_order = sec;
2183 if (f->load_order_search_start == &f->load_order)
2184 f->load_order_search_start = &sec->load_next;
2189 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2191 unsigned long oldsize = sec->header.sh_size;
2193 sec->header.sh_size += more;
2194 sec->contents = xrealloc(sec->contents, sec->header.sh_size);
2196 return sec->contents + oldsize;
2200 /* Conditionally add the symbols from the given symbol set to the
2203 static int add_symbols_from(struct obj_file *f,
2205 struct new_module_symbol *syms,
2208 struct new_module_symbol *s;
2211 #ifdef SYMBOL_PREFIX
2212 char *name_buf = NULL;
2213 size_t name_alloced_size = 0;
2215 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2218 gpl = obj_gpl_license(f, NULL) == 0;
2220 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2221 /* Only add symbols that are already marked external.
2222 If we override locals we may cause problems for
2223 argument initialization. We will also create a false
2224 dependency on the module. */
2225 struct obj_symbol *sym;
2228 /* GPL licensed modules can use symbols exported with
2229 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2230 * exported names. Non-GPL modules never see any GPLONLY_
2231 * symbols so they cannot fudge it by adding the prefix on
2234 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2235 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2242 name = (char *)s->name;
2244 #ifdef SYMBOL_PREFIX
2245 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2246 kernel exports `C names', but module object files
2247 reference `linker names'). */
2248 size_t extra = sizeof SYMBOL_PREFIX;
2249 size_t name_size = strlen(name) + extra;
2250 if (name_size > name_alloced_size) {
2251 name_alloced_size = name_size * 2;
2252 name_buf = alloca(name_alloced_size);
2254 strcpy(name_buf, SYMBOL_PREFIX);
2255 strcpy(name_buf + extra - 1, name);
2259 sym = obj_find_symbol(f, name);
2260 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2261 #ifdef SYMBOL_PREFIX
2262 /* Put NAME_BUF into more permanent storage. */
2263 name = xmalloc(name_size);
2264 strcpy(name, name_buf);
2266 sym = obj_add_symbol(f, name, -1,
2267 ELF_ST_INFO(STB_GLOBAL,
2270 /* Did our symbol just get installed? If so, mark the
2271 module as "used". */
2272 if (sym->secidx == idx)
2280 static void add_kernel_symbols(struct obj_file *f)
2282 struct external_module *m;
2285 /* Add module symbols first. */
2287 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
2289 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
2296 n_ext_modules_used = nused;
2298 /* And finally the symbols from the kernel proper. */
2301 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2304 static char *get_modinfo_value(struct obj_file *f, const char *key)
2306 struct obj_section *sec;
2307 char *p, *v, *n, *ep;
2308 size_t klen = strlen(key);
2310 sec = obj_find_section(f, ".modinfo");
2314 ep = p + sec->header.sh_size;
2317 n = strchr(p, '\0');
2319 if (p + klen == v && strncmp(p, key, klen) == 0)
2322 if (p + klen == n && strcmp(p, key) == 0)
2332 /*======================================================================*/
2333 /* Functions relating to module loading after 2.1.18. */
2335 /* From Linux-2.6 sources */
2336 /* You can use " around spaces, but can't escape ". */
2337 /* Hyphens and underscores equivalent in parameter names. */
2338 static char *next_arg(char *args, char **param, char **val)
2340 unsigned int i, equals = 0;
2341 int in_quote = 0, quoted = 0;
2350 for (i = 0; args[i]; i++) {
2351 if (args[i] == ' ' && !in_quote)
2358 in_quote = !in_quote;
2365 args[equals] = '\0';
2366 *val = args + equals + 1;
2368 /* Don't include quotes in value. */
2371 if (args[i-1] == '"')
2374 if (quoted && args[i-1] == '"')
2380 next = args + i + 1;
2384 /* Chew up trailing spaces. */
2385 return skip_whitespace(next);
2389 new_process_module_arguments(struct obj_file *f, const char *options)
2391 char *xoptions, *pos;
2394 xoptions = pos = xstrdup(skip_whitespace(options));
2396 unsigned long charssize = 0;
2397 char *tmp, *contents, *loc, *pinfo, *p;
2398 struct obj_symbol *sym;
2399 int min, max, n, len;
2401 pos = next_arg(pos, ¶m, &val);
2403 tmp = xasprintf("parm_%s", param);
2404 pinfo = get_modinfo_value(f, tmp);
2407 bb_error_msg_and_die("invalid parameter %s", param);
2409 #ifdef SYMBOL_PREFIX
2410 tmp = xasprintf(SYMBOL_PREFIX "%s", param);
2411 sym = obj_find_symbol(f, tmp);
2414 sym = obj_find_symbol(f, param);
2417 /* Also check that the parameter was not resolved from the kernel. */
2418 if (sym == NULL || sym->secidx > SHN_HIRESERVE)
2419 bb_error_msg_and_die("symbol for parameter %s not found", param);
2421 /* Number of parameters */
2422 if (isdigit(*pinfo)) {
2423 min = strtoul(pinfo, &pinfo, 10);
2425 max = strtoul(pinfo + 1, &pinfo, 10);
2431 contents = f->sections[sym->secidx]->contents;
2432 loc = contents + sym->value;
2434 if (*pinfo == 'c') {
2435 if (!isdigit(*(pinfo + 1))) {
2436 bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
2437 " the maximum size", param);
2439 charssize = strtoul(pinfo + 1, (char **) NULL, 10);
2444 bb_error_msg_and_die("argument expected for parameter %s", param);
2448 /* Parse parameter values */
2453 bb_error_msg_and_die("too many values for %s (max %d)", param, max);
2457 len = strcspn(p, ",");
2459 obj_string_patch(f, sym->secidx,
2461 loc += tgt_sizeof_char_p;
2465 len = strcspn(p, ",");
2467 if (len >= charssize)
2468 bb_error_msg_and_die("string too long for %s (max %ld)", param,
2470 strcpy((char *) loc, p);
2475 *loc++ = strtoul(p, &p, 0);
2478 *(short *) loc = strtoul(p, &p, 0);
2479 loc += tgt_sizeof_short;
2482 *(int *) loc = strtoul(p, &p, 0);
2483 loc += tgt_sizeof_int;
2486 *(long *) loc = strtoul(p, &p, 0);
2487 loc += tgt_sizeof_long;
2490 bb_error_msg_and_die("unknown parameter type '%c' for %s",
2494 p = skip_whitespace(p);
2497 p = skip_whitespace(p + 1);
2501 bb_error_msg_and_die("parameter %s requires at least %d arguments", param, min);
2503 bb_error_msg_and_die("invalid argument syntax for %s", param);
2509 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2510 static int new_is_module_checksummed(struct obj_file *f)
2512 const char *p = get_modinfo_value(f, "using_checksums");
2518 /* Get the module's kernel version in the canonical integer form. */
2521 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2526 p = get_modinfo_value(f, "kernel_version");
2529 safe_strncpy(str, p, STRVERSIONLEN);
2531 a = strtoul(p, &p, 10);
2534 b = strtoul(p + 1, &p, 10);
2537 c = strtoul(p + 1, &q, 10);
2541 return a << 16 | b << 8 | c;
2544 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2547 /* Fetch the loaded modules, and all currently exported symbols. */
2549 static void new_get_kernel_symbols(void)
2551 char *module_names, *mn;
2552 struct external_module *modules, *m;
2553 struct new_module_symbol *syms, *s;
2554 size_t ret, bufsize, nmod, nsyms, i, j;
2556 /* Collect the loaded modules. */
2559 module_names = xmalloc(bufsize);
2562 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2563 if (errno == ENOSPC && bufsize < ret) {
2565 module_names = xrealloc(module_names, bufsize);
2566 goto retry_modules_load;
2568 bb_perror_msg_and_die("QM_MODULES");
2571 n_ext_modules = nmod = ret;
2573 /* Collect the modules' symbols. */
2576 ext_modules = modules = xzalloc(nmod * sizeof(*modules));
2577 for (i = 0, mn = module_names, m = modules;
2578 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2579 struct new_module_info info;
2581 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2582 if (errno == ENOENT) {
2583 /* The module was removed out from underneath us. */
2586 bb_perror_msg_and_die("query_module: QM_INFO: %s", mn);
2590 syms = xmalloc(bufsize);
2592 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2596 syms = xrealloc(syms, bufsize);
2597 goto retry_mod_sym_load;
2599 /* The module was removed out from underneath us. */
2602 bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn);
2608 m->addr = info.addr;
2612 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2613 s->name += (unsigned long) syms;
2618 /* Collect the kernel's symbols. */
2620 bufsize = 16 * 1024;
2621 syms = xmalloc(bufsize);
2622 retry_kern_sym_load:
2623 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2624 if (errno == ENOSPC && bufsize < ret) {
2626 syms = xrealloc(syms, bufsize);
2627 goto retry_kern_sym_load;
2629 bb_perror_msg_and_die("kernel: QM_SYMBOLS");
2631 nksyms = nsyms = ret;
2634 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2635 s->name += (unsigned long) syms;
2640 /* Return the kernel symbol checksum version, or zero if not used. */
2642 static int new_is_kernel_checksummed(void)
2644 struct new_module_symbol *s;
2647 /* Using_Versions is not the first symbol, but it should be in there. */
2649 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2650 if (strcmp((char *) s->name, "Using_Versions") == 0)
2657 static void new_create_this_module(struct obj_file *f, const char *m_name)
2659 struct obj_section *sec;
2661 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2662 sizeof(struct new_module));
2663 /* done by obj_create_alloced_section_first: */
2664 /*memset(sec->contents, 0, sizeof(struct new_module));*/
2666 obj_add_symbol(f, SPFX "__this_module", -1,
2667 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2668 sizeof(struct new_module));
2670 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2674 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2675 /* add an entry to the __ksymtab section, creating it if necessary */
2676 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2678 struct obj_section *sec;
2681 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2682 * If __ksymtab is defined but not marked alloc, x out the first character
2683 * (no obj_delete routine) and create a new __ksymtab with the correct
2686 sec = obj_find_section(f, "__ksymtab");
2687 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2688 *((char *)(sec->name)) = 'x'; /* override const */
2692 sec = obj_create_alloced_section(f, "__ksymtab",
2693 tgt_sizeof_void_p, 0);
2696 sec->header.sh_flags |= SHF_ALLOC;
2697 /* Empty section might be byte-aligned */
2698 sec->header.sh_addralign = tgt_sizeof_void_p;
2699 ofs = sec->header.sh_size;
2700 obj_symbol_patch(f, sec->idx, ofs, sym);
2701 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2702 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2704 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2706 static int new_create_module_ksymtab(struct obj_file *f)
2708 struct obj_section *sec;
2711 /* We must always add the module references. */
2713 if (n_ext_modules_used) {
2714 struct new_module_ref *dep;
2715 struct obj_symbol *tm;
2717 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2718 (sizeof(struct new_module_ref)
2719 * n_ext_modules_used));
2723 tm = obj_find_symbol(f, SPFX "__this_module");
2724 dep = (struct new_module_ref *) sec->contents;
2725 for (i = 0; i < n_ext_modules; ++i)
2726 if (ext_modules[i].used) {
2727 dep->dep = ext_modules[i].addr;
2728 obj_symbol_patch(f, sec->idx,
2729 (char *) &dep->ref - sec->contents, tm);
2735 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2739 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2741 /* We don't want to export symbols residing in sections that
2742 aren't loaded. There are a number of these created so that
2743 we make sure certain module options don't appear twice. */
2744 i = f->header.e_shnum;
2745 loaded = alloca(sizeof(int) * i);
2747 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2749 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2750 struct obj_symbol *sym;
2751 for (sym = f->symtab[i]; sym; sym = sym->next) {
2752 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2753 && sym->secidx <= SHN_HIRESERVE
2754 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
2756 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2758 obj_symbol_patch(f, sec->idx, ofs, sym);
2759 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2766 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2774 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2776 struct new_module *module;
2777 struct obj_section *sec;
2782 sec = obj_find_section(f, ".this");
2783 if (!sec || !sec->contents) {
2784 bb_perror_msg_and_die("corrupt module %s?", m_name);
2786 module = (struct new_module *) sec->contents;
2787 m_addr = sec->header.sh_addr;
2789 module->size_of_struct = sizeof(*module);
2790 module->size = m_size;
2791 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2793 sec = obj_find_section(f, "__ksymtab");
2794 if (sec && sec->header.sh_size) {
2795 module->syms = sec->header.sh_addr;
2796 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2799 if (n_ext_modules_used) {
2800 sec = obj_find_section(f, ".kmodtab");
2801 module->deps = sec->header.sh_addr;
2802 module->ndeps = n_ext_modules_used;
2805 module->init = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2806 module->cleanup = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2808 sec = obj_find_section(f, "__ex_table");
2810 module->ex_table_start = sec->header.sh_addr;
2811 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2814 sec = obj_find_section(f, ".text.init");
2816 module->runsize = sec->header.sh_addr - m_addr;
2818 sec = obj_find_section(f, ".data.init");
2820 if (!module->runsize
2821 || module->runsize > sec->header.sh_addr - m_addr
2823 module->runsize = sec->header.sh_addr - m_addr;
2826 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2827 if (sec && sec->header.sh_size) {
2828 module->archdata_start = (void*)sec->header.sh_addr;
2829 module->archdata_end = module->archdata_start + sec->header.sh_size;
2831 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2832 if (sec && sec->header.sh_size) {
2833 module->kallsyms_start = (void*)sec->header.sh_addr;
2834 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2837 /* Whew! All of the initialization is complete. Collect the final
2838 module image and give it to the kernel. */
2840 image = xmalloc(m_size);
2841 obj_create_image(f, image);
2843 ret = init_module(m_name, (struct new_module *) image);
2845 bb_perror_msg("init_module: %s", m_name);
2853 /*======================================================================*/
2856 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2859 struct obj_string_patch *p;
2860 struct obj_section *strsec;
2861 size_t len = strlen(string) + 1;
2864 p = xzalloc(sizeof(*p));
2865 p->next = f->string_patches;
2866 p->reloc_secidx = secidx;
2867 p->reloc_offset = offset;
2868 f->string_patches = p;
2870 strsec = obj_find_section(f, ".kstrtab");
2871 if (strsec == NULL) {
2872 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2873 /*p->string_offset = 0;*/
2874 loc = strsec->contents;
2876 p->string_offset = strsec->header.sh_size;
2877 loc = obj_extend_section(strsec, len);
2879 memcpy(loc, string, len);
2883 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2884 struct obj_symbol *sym)
2886 struct obj_symbol_patch *p;
2888 p = xmalloc(sizeof(*p));
2889 p->next = f->symbol_patches;
2890 p->reloc_secidx = secidx;
2891 p->reloc_offset = offset;
2893 f->symbol_patches = p;
2896 static void obj_check_undefineds(struct obj_file *f)
2900 for (i = 0; i < HASH_BUCKETS; ++i) {
2901 struct obj_symbol *sym;
2902 for (sym = f->symtab[i]; sym; sym = sym->next) {
2903 if (sym->secidx == SHN_UNDEF) {
2904 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
2905 sym->secidx = SHN_ABS;
2909 bb_error_msg_and_die("unresolved symbol %s", sym->name);
2916 static void obj_allocate_commons(struct obj_file *f)
2918 struct common_entry {
2919 struct common_entry *next;
2920 struct obj_symbol *sym;
2921 } *common_head = NULL;
2925 for (i = 0; i < HASH_BUCKETS; ++i) {
2926 struct obj_symbol *sym;
2927 for (sym = f->symtab[i]; sym; sym = sym->next) {
2928 if (sym->secidx == SHN_COMMON) {
2929 /* Collect all COMMON symbols and sort them by size so as to
2930 minimize space wasted by alignment requirements. */
2931 struct common_entry **p, *n;
2932 for (p = &common_head; *p; p = &(*p)->next)
2933 if (sym->size <= (*p)->sym->size)
2935 n = alloca(sizeof(*n));
2943 for (i = 1; i < f->local_symtab_size; ++i) {
2944 struct obj_symbol *sym = f->local_symtab[i];
2945 if (sym && sym->secidx == SHN_COMMON) {
2946 struct common_entry **p, *n;
2947 for (p = &common_head; *p; p = &(*p)->next) {
2948 if (sym == (*p)->sym)
2950 if (sym->size < (*p)->sym->size) {
2951 n = alloca(sizeof(*n));
2962 /* Find the bss section. */
2963 for (i = 0; i < f->header.e_shnum; ++i)
2964 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2967 /* If for some reason there hadn't been one, create one. */
2968 if (i == f->header.e_shnum) {
2969 struct obj_section *sec;
2971 f->header.e_shnum++;
2972 f->sections = xrealloc_vector(f->sections, 2, i);
2973 f->sections[i] = sec = arch_new_section();
2975 sec->header.sh_type = SHT_PROGBITS;
2976 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2981 /* Allocate the COMMONS. */
2983 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2984 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2985 struct common_entry *c;
2987 for (c = common_head; c; c = c->next) {
2988 ElfW(Addr) align = c->sym->value;
2990 if (align > max_align)
2992 if (bss_size & (align - 1))
2993 bss_size = (bss_size | (align - 1)) + 1;
2996 c->sym->value = bss_size;
2998 bss_size += c->sym->size;
3001 f->sections[i]->header.sh_size = bss_size;
3002 f->sections[i]->header.sh_addralign = max_align;
3006 /* For the sake of patch relocation and parameter initialization,
3007 allocate zeroed data for NOBITS sections now. Note that after
3008 this we cannot assume NOBITS are really empty. */
3009 for (i = 0; i < f->header.e_shnum; ++i) {
3010 struct obj_section *s = f->sections[i];
3011 if (s->header.sh_type == SHT_NOBITS) {
3013 if (s->header.sh_size != 0)
3014 s->contents = xzalloc(s->header.sh_size);
3015 s->header.sh_type = SHT_PROGBITS;
3020 static unsigned long obj_load_size(struct obj_file *f)
3022 unsigned long dot = 0;
3023 struct obj_section *sec;
3025 /* Finalize the positions of the sections relative to one another. */
3027 for (sec = f->load_order; sec; sec = sec->load_next) {
3030 align = sec->header.sh_addralign;
3031 if (align && (dot & (align - 1)))
3032 dot = (dot | (align - 1)) + 1;
3034 sec->header.sh_addr = dot;
3035 dot += sec->header.sh_size;
3041 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3043 int i, n = f->header.e_shnum;
3046 /* Finalize the addresses of the sections. */
3049 for (i = 0; i < n; ++i)
3050 f->sections[i]->header.sh_addr += base;
3052 /* And iterate over all of the relocations. */
3054 for (i = 0; i < n; ++i) {
3055 struct obj_section *relsec, *symsec, *targsec, *strsec;
3056 ElfW(RelM) * rel, *relend;
3060 relsec = f->sections[i];
3061 if (relsec->header.sh_type != SHT_RELM)
3064 symsec = f->sections[relsec->header.sh_link];
3065 targsec = f->sections[relsec->header.sh_info];
3066 strsec = f->sections[symsec->header.sh_link];
3068 rel = (ElfW(RelM) *) relsec->contents;
3069 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3070 symtab = (ElfW(Sym) *) symsec->contents;
3071 strtab = (const char *) strsec->contents;
3073 for (; rel < relend; ++rel) {
3074 ElfW(Addr) value = 0;
3075 struct obj_symbol *intsym = NULL;
3076 unsigned long symndx;
3077 ElfW(Sym) *extsym = NULL;
3080 /* Attempt to find a value to use for this relocation. */
3082 symndx = ELF_R_SYM(rel->r_info);
3084 /* Note we've already checked for undefined symbols. */
3086 extsym = &symtab[symndx];
3087 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3088 /* Local symbols we look up in the local table to be sure
3089 we get the one that is really intended. */
3090 intsym = f->local_symtab[symndx];
3092 /* Others we look up in the hash table. */
3094 if (extsym->st_name)
3095 name = strtab + extsym->st_name;
3097 name = f->sections[extsym->st_shndx]->name;
3098 intsym = obj_find_symbol(f, name);
3101 value = obj_symbol_final_value(f, intsym);
3102 intsym->referenced = 1;
3104 #if SHT_RELM == SHT_RELA
3105 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3106 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3107 if (!extsym || !extsym->st_name
3108 || ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3110 value += rel->r_addend;
3114 switch (arch_apply_relocation
3115 (f, targsec, /*symsec,*/ intsym, rel, value)
3120 case obj_reloc_overflow:
3121 errmsg = "Relocation overflow";
3123 case obj_reloc_dangerous:
3124 errmsg = "Dangerous relocation";
3126 case obj_reloc_unhandled:
3127 errmsg = "Unhandled relocation";
3130 bb_error_msg("%s of type %ld for %s", errmsg,
3131 (long) ELF_R_TYPE(rel->r_info),
3132 strtab + extsym->st_name);
3134 bb_error_msg("%s of type %ld", errmsg,
3135 (long) ELF_R_TYPE(rel->r_info));
3143 /* Finally, take care of the patches. */
3145 if (f->string_patches) {
3146 struct obj_string_patch *p;
3147 struct obj_section *strsec;
3148 ElfW(Addr) strsec_base;
3149 strsec = obj_find_section(f, ".kstrtab");
3150 strsec_base = strsec->header.sh_addr;
3152 for (p = f->string_patches; p; p = p->next) {
3153 struct obj_section *targsec = f->sections[p->reloc_secidx];
3154 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3155 = strsec_base + p->string_offset;
3159 if (f->symbol_patches) {
3160 struct obj_symbol_patch *p;
3162 for (p = f->symbol_patches; p; p = p->next) {
3163 struct obj_section *targsec = f->sections[p->reloc_secidx];
3164 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3165 = obj_symbol_final_value(f, p->sym);
3172 static int obj_create_image(struct obj_file *f, char *image)
3174 struct obj_section *sec;
3175 ElfW(Addr) base = f->baseaddr;
3177 for (sec = f->load_order; sec; sec = sec->load_next) {
3180 if (sec->contents == 0 || sec->header.sh_size == 0)
3183 secimg = image + (sec->header.sh_addr - base);
3185 /* Note that we allocated data for NOBITS sections earlier. */
3186 memcpy(secimg, sec->contents, sec->header.sh_size);
3192 /*======================================================================*/
3194 static struct obj_file *obj_load(int fd, int loadprogbits UNUSED_PARAM)
3196 #if BB_LITTLE_ENDIAN
3197 # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3)))))
3199 # define ELFMAG_U32 ((uint32_t)((((ELFMAG0 * 0x100) + ELFMAG1) * 0x100 + ELFMAG2) * 0x100 + ELFMAG3))
3202 ElfW(Shdr) * section_headers;
3206 /* Read the file header. */
3208 f = arch_new_file();
3209 f->symbol_cmp = strcmp;
3210 f->symbol_hash = obj_elf_hash;
3211 f->load_order_search_start = &f->load_order;
3213 xlseek(fd, 0, SEEK_SET);
3214 xread(fd, &f->header, sizeof(f->header));
3216 if (*(uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
3217 bb_error_msg_and_die("not an ELF file");
3219 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3220 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB)
3221 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3222 || !MATCH_MACHINE(f->header.e_machine)
3224 bb_error_msg_and_die("ELF file not for this architecture");
3226 if (f->header.e_type != ET_REL) {
3227 bb_error_msg_and_die("ELF file not a relocatable object");
3230 /* Read the section headers. */
3232 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3233 bb_error_msg_and_die("section header size mismatch: %lu != %lu",
3234 (unsigned long) f->header.e_shentsize,
3235 (unsigned long) sizeof(ElfW(Shdr)));
3238 shnum = f->header.e_shnum;
3239 /* Growth of ->sections vector will be done by
3240 * xrealloc_vector(..., 2, ...), therefore we must allocate
3241 * at least 2^2 = 4 extra elements here. */
3242 f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
3244 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3245 xlseek(fd, f->header.e_shoff, SEEK_SET);
3246 xread(fd, section_headers, sizeof(ElfW(Shdr)) * shnum);
3248 /* Read the section data. */
3250 for (i = 0; i < shnum; ++i) {
3251 struct obj_section *sec;
3253 f->sections[i] = sec = arch_new_section();
3255 sec->header = section_headers[i];
3258 if (sec->header.sh_size) {
3259 switch (sec->header.sh_type) {
3267 if (!loadprogbits) {
3268 sec->contents = NULL;
3275 sec->contents = NULL;
3276 if (sec->header.sh_size > 0) {
3277 sec->contents = xmalloc(sec->header.sh_size);
3278 xlseek(fd, sec->header.sh_offset, SEEK_SET);
3279 xread(fd, sec->contents, sec->header.sh_size);
3282 #if SHT_RELM == SHT_REL
3284 bb_error_msg_and_die("RELA relocations not supported on this architecture");
3287 bb_error_msg_and_die("REL relocations not supported on this architecture");
3290 if (sec->header.sh_type >= SHT_LOPROC) {
3291 /* Assume processor specific section types are debug
3292 info and can safely be ignored. If this is ever not
3293 the case (Hello MIPS?), don't put ifdefs here but
3294 create an arch_load_proc_section(). */
3298 bb_error_msg_and_die("can't handle sections of type %ld",
3299 (long) sec->header.sh_type);
3304 /* Do what sort of interpretation as needed by each section. */
3306 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3308 for (i = 0; i < shnum; ++i) {
3309 struct obj_section *sec = f->sections[i];
3310 sec->name = shstrtab + sec->header.sh_name;
3313 for (i = 0; i < shnum; ++i) {
3314 struct obj_section *sec = f->sections[i];
3316 /* .modinfo should be contents only but gcc has no attribute for that.
3317 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3319 if (strcmp(sec->name, ".modinfo") == 0)
3320 sec->header.sh_flags &= ~SHF_ALLOC;
3322 if (sec->header.sh_flags & SHF_ALLOC)
3323 obj_insert_section_load_order(f, sec);
3325 switch (sec->header.sh_type) {
3328 unsigned long nsym, j;
3332 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3333 bb_error_msg_and_die("symbol size mismatch: %lu != %lu",
3334 (unsigned long) sec->header.sh_entsize,
3335 (unsigned long) sizeof(ElfW(Sym)));
3338 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3339 strtab = f->sections[sec->header.sh_link]->contents;
3340 sym = (ElfW(Sym) *) sec->contents;
3342 /* Allocate space for a table of local symbols. */
3343 j = f->local_symtab_size = sec->header.sh_info;
3344 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3346 /* Insert all symbols into the hash table. */
3347 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3348 ElfW(Addr) val = sym->st_value;
3351 name = strtab + sym->st_name;
3352 else if (sym->st_shndx < shnum)
3353 name = f->sections[sym->st_shndx]->name;
3356 #if defined(__SH5__)
3358 * For sh64 it is possible that the target of a branch
3359 * requires a mode switch (32 to 16 and back again).
3361 * This is implied by the lsb being set in the target
3362 * address for SHmedia mode and clear for SHcompact.
3364 val |= sym->st_other & 4;
3366 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3373 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3374 bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu",
3375 (unsigned long) sec->header.sh_entsize,
3376 (unsigned long) sizeof(ElfW(RelM)));
3379 /* XXX Relocation code from modutils-2.3.19 is not here.
3380 * Why? That's about 20 lines of code from obj/obj_load.c,
3381 * which gets done in a second pass through the sections.
3382 * This BusyBox insmod does similar work in obj_relocate(). */
3389 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3391 * load the unloaded sections directly into the memory allocated by
3392 * kernel for the module
3395 static int obj_load_progbits(int fd, struct obj_file *f, char *imagebase)
3397 ElfW(Addr) base = f->baseaddr;
3398 struct obj_section* sec;
3400 for (sec = f->load_order; sec; sec = sec->load_next) {
3401 /* section already loaded? */
3402 if (sec->contents != NULL)
3404 if (sec->header.sh_size == 0)
3406 sec->contents = imagebase + (sec->header.sh_addr - base);
3407 xlseek(fd, sec->header.sh_offset, SEEK_SET);
3408 errno = 0; /* read may be short without errno being set */
3409 if (full_read(fd, sec->contents, sec->header.sh_size) != sec->header.sh_size) {
3410 bb_perror_msg("error reading ELF section data");
3418 static void hide_special_symbols(struct obj_file *f)
3420 static const char *const specials[] = {
3421 SPFX "cleanup_module",
3423 SPFX "kernel_version",
3427 struct obj_symbol *sym;
3428 const char *const *p;
3430 for (p = specials; *p; ++p) {
3431 sym = obj_find_symbol(f, *p);
3433 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3438 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3439 static int obj_gpl_license(struct obj_file *f, const char **license)
3441 struct obj_section *sec;
3442 /* This list must match *exactly* the list of allowable licenses in
3443 * linux/include/linux/module.h. Checking for leading "GPL" will not
3444 * work, somebody will use "GPL sucks, this is proprietary".
3446 static const char *const gpl_licenses[] = {
3449 "GPL and additional rights",
3454 sec = obj_find_section(f, ".modinfo");
3456 const char *value, *ptr, *endptr;
3457 ptr = sec->contents;
3458 endptr = ptr + sec->header.sh_size;
3459 while (ptr < endptr) {
3460 value = strchr(ptr, '=');
3461 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3465 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
3466 if (strcmp(value+1, gpl_licenses[i]) == 0)
3471 ptr = strchr(ptr, '\0');
3481 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3482 #define TAINT_PROPRIETORY_MODULE (1 << 0)
3483 #define TAINT_FORCED_MODULE (1 << 1)
3484 #define TAINT_UNSAFE_SMP (1 << 2)
3485 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3487 static void set_tainted(int fd, const char *m_name,
3488 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3490 static smallint printed_info;
3495 if (fd < 0 && !kernel_has_tainted)
3496 return; /* New modutils on old kernel */
3497 printf("Warning: loading %s will taint the kernel: %s%s\n",
3498 m_name, text1, text2);
3499 if (!printed_info) {
3500 printf(" See %s for information about tainted modules\n", TAINT_URL);
3504 read(fd, buf, sizeof(buf)-1);
3505 buf[sizeof(buf)-1] = '\0';
3506 oldval = strtoul(buf, NULL, 10);
3507 sprintf(buf, "%d\n", oldval | taint);
3508 xwrite_str(fd, buf);
3512 /* Check if loading this module will taint the kernel. */
3513 static void check_tainted_module(struct obj_file *f, const char *m_name)
3515 static const char tainted_file[] ALIGN1 = TAINT_FILENAME;
3517 int fd, kernel_has_tainted;
3520 kernel_has_tainted = 1;
3521 fd = open(tainted_file, O_RDWR);
3523 if (errno == ENOENT)
3524 kernel_has_tainted = 0;
3525 else if (errno == EACCES)
3526 kernel_has_tainted = 1;
3528 perror(tainted_file);
3529 kernel_has_tainted = 0;
3533 switch (obj_gpl_license(f, &ptr)) {
3537 set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3539 default: /* case 2: */
3540 /* The module has a non-GPL license so we pretend that the
3541 * kernel always has a taint flag to get a warning even on
3542 * kernels without the proc flag.
3544 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3548 if (flag_force_load)
3549 set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3554 #else /* !FEATURE_CHECK_TAINTED_MODULE */
3555 #define check_tainted_module(x, y) do { } while (0);
3558 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3559 /* add module source, timestamp, kernel version and a symbol for the
3560 * start of some sections. this info is used by ksymoops to do better
3563 #if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3564 #define get_module_version(f, str) get_module_version(str)
3567 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3569 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3570 return new_get_module_version(f, str);
3572 strncpy(str, "???", sizeof(str));
3577 /* add module source, timestamp, kernel version and a symbol for the
3578 * start of some sections. this info is used by ksymoops to do better
3582 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3585 static const char symprefix[] ALIGN1 = "__insmod_";
3586 static const char section_names[][8] = {
3594 struct obj_section *sec;
3595 struct obj_symbol *sym;
3596 char *name, *absolute_filename;
3597 char str[STRVERSIONLEN];
3599 int lm_name, lfilename, use_ksymtab, version;
3600 struct stat statbuf;
3602 /* WARNING: was using realpath, but replaced by readlink to stop using
3603 * lots of stack. But here it seems to be able to cause problems? */
3604 absolute_filename = xmalloc_readlink(filename);
3605 if (!absolute_filename)
3606 absolute_filename = xstrdup(filename);
3608 lm_name = strlen(m_name);
3609 lfilename = strlen(absolute_filename);
3611 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3612 * are not to be exported. otherwise leave ksymtab alone for now, the
3613 * "export all symbols" compatibility code will export these symbols later.
3615 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3617 sec = obj_find_section(f, ".this");
3619 /* tag the module header with the object name, last modified
3620 * timestamp and module version. worst case for module version
3621 * is 0xffffff, decimal 16777215. putting all three fields in
3622 * one symbol is less readable but saves kernel space.
3624 if (stat(absolute_filename, &statbuf) != 0)
3625 statbuf.st_mtime = 0;
3626 version = get_module_version(f, str); /* -1 if not found */
3627 name = xasprintf("%s%s_O%s_M%0*lX_V%d",
3628 symprefix, m_name, absolute_filename,
3629 (int)(2 * sizeof(statbuf.st_mtime)),
3630 (long)statbuf.st_mtime,
3632 sym = obj_add_symbol(f, name, -1,
3633 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3634 sec->idx, sec->header.sh_addr, 0);
3636 new_add_ksymtab(f, sym);
3638 free(absolute_filename);
3639 #ifdef _NOT_SUPPORTED_
3640 /* record where the persistent data is going, same address as previous symbol */
3642 name = xasprintf("%s%s_P%s",
3643 symprefix, m_name, f->persist);
3644 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3645 sec->idx, sec->header.sh_addr, 0);
3647 new_add_ksymtab(f, sym);
3650 /* tag the desired sections if size is non-zero */
3651 for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
3652 sec = obj_find_section(f, section_names[i]);
3653 if (sec && sec->header.sh_size) {
3654 name = xasprintf("%s%s_S%s_L%ld",
3655 symprefix, m_name, sec->name,
3656 (long)sec->header.sh_size);
3657 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3658 sec->idx, sec->header.sh_addr, 0);
3660 new_add_ksymtab(f, sym);
3664 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3666 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3667 static void print_load_map(struct obj_file *f)
3669 struct obj_section *sec;
3670 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3671 struct obj_symbol **all, **p;
3673 char *loaded; /* array of booleans */
3674 struct obj_symbol *sym;
3676 /* Report on the section layout. */
3677 printf("Sections: Size %-*s Align\n",
3678 (int) (2 * sizeof(void *)), "Address");
3680 for (sec = f->load_order; sec; sec = sec->load_next) {
3684 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3689 printf("%-15s %08lx %0*lx 2**%d\n",
3691 (long)sec->header.sh_size,
3692 (int) (2 * sizeof(void *)),
3693 (long)sec->header.sh_addr,
3696 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3697 /* Quick reference which section indices are loaded. */
3698 i = f->header.e_shnum;
3699 loaded = alloca(i * sizeof(loaded[0]));
3701 loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
3703 /* Collect the symbols we'll be listing. */
3704 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3705 for (sym = f->symtab[i]; sym; sym = sym->next)
3706 if (sym->secidx <= SHN_HIRESERVE
3707 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3712 all = alloca(nsyms * sizeof(all[0]));
3714 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3715 for (sym = f->symtab[i]; sym; sym = sym->next)
3716 if (sym->secidx <= SHN_HIRESERVE
3717 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3722 /* And list them. */
3723 printf("\nSymbols:\n");
3724 for (p = all; p < all + nsyms; ++p) {
3726 unsigned long value;
3729 if (sym->secidx == SHN_ABS) {
3732 } else if (sym->secidx == SHN_UNDEF) {
3736 sec = f->sections[sym->secidx];
3738 if (sec->header.sh_type == SHT_NOBITS)
3740 else if (sec->header.sh_flags & SHF_ALLOC) {
3741 if (sec->header.sh_flags & SHF_EXECINSTR)
3743 else if (sec->header.sh_flags & SHF_WRITE)
3748 value = sym->value + sec->header.sh_addr;
3751 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3752 type |= 0x20; /* tolower. safe for '?' too */
3754 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3759 #else /* !FEATURE_INSMOD_LOAD_MAP */
3760 static void print_load_map(struct obj_file *f UNUSED_PARAM)
3765 int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
3768 unsigned long m_size;
3772 int exit_status = EXIT_FAILURE;
3775 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3776 char m_strversion[STRVERSIONLEN];
3777 int m_version, m_crcs;
3782 fd = open_or_warn(m_filename, O_RDONLY);
3784 return EXIT_FAILURE;
3786 m_name = xstrdup(bb_basename(m_filename));
3787 p = strrchr(m_name, '.');
3790 f = obj_load(fd, LOADBITS);
3792 m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
3794 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3795 /* Version correspondence? */
3797 if (m_has_modinfo) {
3798 m_version = new_get_module_version(f, m_strversion);
3799 if (m_version == -1) {
3800 bb_error_msg_and_die("cannot find the kernel version the module was "
3805 if (strncmp(uts.release, m_strversion, STRVERSIONLEN) != 0) {
3806 bb_error_msg("%skernel-module version mismatch\n"
3807 "\t%s was compiled for kernel version %s\n"
3808 "\twhile this kernel is version %s",
3809 flag_force_load ? "warning: " : "",
3810 m_name, m_strversion, uts.release);
3811 if (!flag_force_load)
3818 if (query_module(NULL, 0, NULL, 0, NULL))
3819 bb_error_msg_and_die("not configured to support old kernels");
3820 new_get_kernel_symbols();
3821 k_crcs = new_is_kernel_checksummed();
3823 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3826 m_crcs = new_is_module_checksummed(f);
3828 if (m_crcs != k_crcs)
3829 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3832 /* Let the module know about the kernel symbols. */
3833 add_kernel_symbols(f);
3835 /* Allocate common symbols, symbol tables, and string tables. */
3836 new_create_this_module(f, m_name);
3837 obj_check_undefineds(f);
3838 obj_allocate_commons(f);
3839 check_tainted_module(f, m_name);
3841 /* Done with the module name, on to the optional var=value arguments */
3842 new_process_module_arguments(f, options);
3845 hide_special_symbols(f);
3847 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3848 add_ksymoops_symbols(f, m_filename, m_name);
3851 new_create_module_ksymtab(f);
3853 /* Find current size of the module */
3854 m_size = obj_load_size(f);
3856 m_addr = create_module(m_name, m_size);
3857 if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
3859 bb_error_msg_and_die("a module named %s already exists", m_name);
3861 bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes",
3864 bb_perror_msg_and_die("create_module: %s", m_name);
3869 * the PROGBITS section was not loaded by the obj_load
3870 * now we can load them directly into the kernel memory
3872 if (!obj_load_progbits(fd, f, (char*)m_addr)) {
3873 delete_module(m_name, 0);
3878 if (!obj_relocate(f, m_addr)) {
3879 delete_module(m_name, 0);
3883 if (!new_init_module(m_name, f, m_size)) {
3884 delete_module(m_name, 0);
3888 if (flag_print_load_map)
3891 exit_status = EXIT_SUCCESS;