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);
629 #define obj_load(image, image_size, loadprogbits) \
630 obj_load(image, image_size)
632 static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits);
634 static int obj_create_image(struct obj_file *f, char *image);
636 /* Architecture specific manipulation routines. */
638 static struct obj_file *arch_new_file(void);
640 static struct obj_section *arch_new_section(void);
642 static struct obj_symbol *arch_new_symbol(void);
644 static enum obj_reloc arch_apply_relocation(struct obj_file *f,
645 struct obj_section *targsec,
646 /*struct obj_section *symsec,*/
647 struct obj_symbol *sym,
648 ElfW(RelM) *rel, ElfW(Addr) value);
650 static void arch_create_got(struct obj_file *f);
651 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
652 static int obj_gpl_license(struct obj_file *f, const char **license);
655 //----------------------------------------------------------------------------
656 //--------end of modutils obj.h
657 //----------------------------------------------------------------------------
660 /* SPFX is always a string, so it can be concatenated to string constants. */
662 #define SPFX SYMBOL_PREFIX
667 enum { STRVERSIONLEN = 64 };
669 /*======================================================================*/
671 #define flag_force_load (option_mask32 & INSMOD_OPT_FORCE)
672 #define flag_autoclean (option_mask32 & INSMOD_OPT_KERNELD)
673 #define flag_verbose (option_mask32 & INSMOD_OPT_VERBOSE)
674 #define flag_quiet (option_mask32 & INSMOD_OPT_SILENT)
675 #define flag_noexport (option_mask32 & INSMOD_OPT_NO_EXPORT)
676 #define flag_print_load_map (option_mask32 & INSMOD_OPT_PRINT_MAP)
678 /*======================================================================*/
680 #if defined(USE_LIST)
682 struct arch_list_entry
684 struct arch_list_entry *next;
685 LIST_ARCHTYPE addend;
692 #if defined(USE_SINGLE)
694 struct arch_single_entry
703 #if defined(__mips__)
706 struct mips_hi16 *next;
713 struct obj_file root;
714 #if defined(USE_PLT_ENTRIES)
715 struct obj_section *plt;
717 #if defined(USE_GOT_ENTRIES)
718 struct obj_section *got;
720 #if defined(__mips__)
721 struct mips_hi16 *mips_hi16_list;
726 struct obj_symbol root;
727 #if defined(USE_PLT_ENTRIES)
728 #if defined(USE_PLT_LIST)
729 struct arch_list_entry *pltent;
731 struct arch_single_entry pltent;
734 #if defined(USE_GOT_ENTRIES)
735 struct arch_single_entry gotent;
740 struct external_module {
745 struct new_module_symbol *syms;
748 static struct new_module_symbol *ksyms;
749 static size_t nksyms;
751 static struct external_module *ext_modules;
752 static int n_ext_modules;
753 static int n_ext_modules_used;
755 /*======================================================================*/
758 static struct obj_file *arch_new_file(void)
761 f = xzalloc(sizeof(*f));
762 return &f->root; /* it's a first member */
765 static struct obj_section *arch_new_section(void)
767 return xzalloc(sizeof(struct obj_section));
770 static struct obj_symbol *arch_new_symbol(void)
772 struct arch_symbol *sym;
773 sym = xzalloc(sizeof(*sym));
777 static enum obj_reloc
778 arch_apply_relocation(struct obj_file *f,
779 struct obj_section *targsec,
780 /*struct obj_section *symsec,*/
781 struct obj_symbol *sym,
782 ElfW(RelM) *rel, ElfW(Addr) v)
784 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
785 || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \
786 || defined(__powerpc__) || defined(__mips__)
787 struct arch_file *ifile = (struct arch_file *) f;
789 enum obj_reloc ret = obj_reloc_ok;
790 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
791 #if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
792 || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
793 || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
794 || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
795 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
797 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
798 struct arch_symbol *isym = (struct arch_symbol *) sym;
800 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
801 || defined(__sh__) || defined(__s390__)
802 #if defined(USE_GOT_ENTRIES)
803 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
806 #if defined(USE_PLT_ENTRIES)
807 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
809 # if defined(USE_PLT_LIST)
810 struct arch_list_entry *pe;
812 struct arch_single_entry *pe;
816 switch (ELF_R_TYPE(rel->r_info)) {
831 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
832 * (which is .got) similar to branch,
833 * but is full 32 bits relative */
842 case R_ARM_GOTOFF: /* address relative to the got */
846 #elif defined(__cris__)
852 /* CRIS keeps the relocation value in the r_addend field and
853 * should not use whats in *loc at all
858 #elif defined(__H8300H__) || defined(__H8300S__)
861 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
862 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
873 if ((ElfW(Sword))v > 0x7fff ||
874 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
875 ret = obj_reloc_overflow;
877 *(unsigned short *)loc = v;
881 if ((ElfW(Sword))v > 0x7f ||
882 (ElfW(Sword))v < -(ElfW(Sword))0x80)
883 ret = obj_reloc_overflow;
885 *(unsigned char *)loc = v;
888 #elif defined(__i386__)
920 #elif defined(__microblaze__)
921 case R_MICROBLAZE_NONE:
922 case R_MICROBLAZE_64_NONE:
923 case R_MICROBLAZE_32_SYM_OP_SYM:
924 case R_MICROBLAZE_32_PCREL:
927 case R_MICROBLAZE_64_PCREL: {
928 /* dot is the address of the current instruction.
929 * v is the target symbol address.
930 * So we need to extract the offset in the code,
931 * adding v, then subtrating the current address
932 * of this instruction.
933 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
936 /* Get split offset stored in code */
937 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
940 /* Adjust relative offset. -4 adjustment required
941 * because dot points to the IMM insn, but branch
942 * is computed relative to the branch instruction itself.
946 /* Store back into code */
947 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
948 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
953 case R_MICROBLAZE_32:
957 case R_MICROBLAZE_64: {
958 /* Get split pointer stored in code */
959 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
962 /* Add reloc offset */
965 /* Store back into code */
966 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
967 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
972 case R_MICROBLAZE_32_PCREL_LO:
973 case R_MICROBLAZE_32_LO:
974 case R_MICROBLAZE_SRO32:
975 case R_MICROBLAZE_SRW32:
976 ret = obj_reloc_unhandled;
979 #elif defined(__mc68000__)
990 ret = obj_reloc_overflow;
997 ret = obj_reloc_overflow;
1004 if ((ElfW(Sword))v > 0x7f
1005 || (ElfW(Sword))v < -(ElfW(Sword))0x80
1007 ret = obj_reloc_overflow;
1014 if ((ElfW(Sword))v > 0x7fff
1015 || (ElfW(Sword))v < -(ElfW(Sword))0x8000
1017 ret = obj_reloc_overflow;
1023 *(int *)loc = v - dot;
1026 case R_68K_GLOB_DAT:
1027 case R_68K_JMP_SLOT:
1031 case R_68K_RELATIVE:
1032 *(int *)loc += f->baseaddr;
1038 # ifdef R_68K_GOTOFF
1044 #elif defined(__mips__)
1055 ret = obj_reloc_dangerous;
1056 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1057 ret = obj_reloc_overflow;
1059 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1065 struct mips_hi16 *n;
1067 /* We cannot relocate this one now because we don't know the value
1068 of the carry we need to add. Save the information, and let LO16
1069 do the actual relocation. */
1070 n = xmalloc(sizeof *n);
1073 n->next = ifile->mips_hi16_list;
1074 ifile->mips_hi16_list = n;
1080 unsigned long insnlo = *loc;
1081 ElfW(Addr) val, vallo;
1083 /* Sign extend the addend we extract from the lo insn. */
1084 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1086 if (ifile->mips_hi16_list != NULL) {
1087 struct mips_hi16 *l;
1089 l = ifile->mips_hi16_list;
1091 struct mips_hi16 *next;
1094 /* Do the HI16 relocation. Note that we actually don't
1095 need to know anything about the LO16 itself, except where
1096 to find the low 16 bits of the addend needed by the LO16. */
1099 ((insn & 0xffff) << 16) +
1103 /* Account for the sign extension that will happen in the
1110 insn = (insn & ~0xffff) | val;
1118 ifile->mips_hi16_list = NULL;
1121 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1123 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1128 #elif defined(__nios2__)
1133 case R_NIOS2_BFD_RELOC_32:
1137 case R_NIOS2_BFD_RELOC_16:
1139 ret = obj_reloc_overflow;
1144 case R_NIOS2_BFD_RELOC_8:
1146 ret = obj_reloc_overflow;
1155 if ((Elf32_Sword)v > 0x7fff
1156 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1158 ret = obj_reloc_overflow;
1162 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1172 ret = obj_reloc_overflow;
1176 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1181 case R_NIOS2_PCREL16:
1186 if ((Elf32_Sword)v > 0x7fff
1187 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1189 ret = obj_reloc_overflow;
1193 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1199 Elf32_Addr word, gp;
1201 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1203 if ((Elf32_Sword)v > 0x7fff
1204 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1206 ret = obj_reloc_overflow;
1210 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1214 case R_NIOS2_CALL26:
1216 ret = obj_reloc_dangerous;
1217 if ((v >> 28) != (dot >> 28))
1218 ret = obj_reloc_overflow;
1219 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1227 ret = obj_reloc_overflow;
1230 word = *loc & ~0x7c0;
1231 *loc = word | ((v & 0x1f) << 6);
1240 ret = obj_reloc_overflow;
1243 word = *loc & ~0xfc0;
1244 *loc = word | ((v & 0x3f) << 6);
1253 ret = obj_reloc_overflow;
1256 word = *loc & ~0x3fc0;
1257 *loc = word | ((v & 0xff) << 6);
1266 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1276 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1281 case R_NIOS2_HIADJ16:
1283 Elf32_Addr word1, word2;
1286 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1287 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1292 #elif defined(__powerpc64__)
1293 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1295 #elif defined(__powerpc__)
1297 case R_PPC_ADDR16_HA:
1298 *(unsigned short *)loc = (v + 0x8000) >> 16;
1301 case R_PPC_ADDR16_HI:
1302 *(unsigned short *)loc = v >> 16;
1305 case R_PPC_ADDR16_LO:
1306 *(unsigned short *)loc = v;
1320 #elif defined(__s390__)
1323 *(unsigned int *) loc += v;
1326 *(unsigned short *) loc += v;
1329 *(unsigned char *) loc += v;
1333 *(unsigned int *) loc += v - dot;
1336 *(unsigned short *) loc += (v - dot) >> 1;
1339 *(unsigned short *) loc += v - dot;
1343 case R_390_PLT16DBL:
1344 /* find the plt entry and initialize it. */
1345 pe = (struct arch_single_entry *) &isym->pltent;
1346 if (pe->inited == 0) {
1347 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1348 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1350 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1357 /* Insert relative distance to target. */
1358 v = plt + pe->offset - dot;
1359 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1360 *(unsigned int *) loc = (unsigned int) v;
1361 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1362 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1365 case R_390_GLOB_DAT:
1366 case R_390_JMP_SLOT:
1370 case R_390_RELATIVE:
1371 *loc += f->baseaddr;
1375 *(unsigned long *) loc += got - dot;
1381 if (!isym->gotent.inited)
1383 isym->gotent.inited = 1;
1384 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1386 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1387 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1388 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1389 *(unsigned short *) loc += isym->gotent.offset;
1390 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1391 *(unsigned int *) loc += isym->gotent.offset;
1394 # ifndef R_390_GOTOFF32
1395 # define R_390_GOTOFF32 R_390_GOTOFF
1397 case R_390_GOTOFF32:
1401 #elif defined(__sh__)
1424 *loc = f->baseaddr + rel->r_addend;
1428 *loc = got - dot + rel->r_addend;
1438 # if defined(__SH5__)
1439 case R_SH_IMM_MEDLOW16:
1440 case R_SH_IMM_LOW16:
1444 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1448 * movi and shori have the format:
1450 * | op | imm | reg | reserved |
1451 * 31..26 25..10 9.. 4 3 .. 0
1453 * so we simply mask and or in imm.
1455 word = *loc & ~0x3fffc00;
1456 word |= (v & 0xffff) << 10;
1463 case R_SH_IMM_MEDLOW16_PCREL:
1464 case R_SH_IMM_LOW16_PCREL:
1468 word = *loc & ~0x3fffc00;
1472 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1475 word |= (v & 0xffff) << 10;
1481 # endif /* __SH5__ */
1483 #elif defined(__v850e__)
1489 /* We write two shorts instead of a long because even
1490 32-bit insns only need half-word alignment, but
1491 32-bit data needs to be long-word aligned. */
1492 v += ((unsigned short *)loc)[0];
1493 v += ((unsigned short *)loc)[1] << 16;
1494 ((unsigned short *)loc)[0] = v & 0xffff;
1495 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1498 case R_V850_22_PCREL:
1501 #elif defined(__x86_64__)
1511 *(unsigned int *) loc += v;
1514 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1515 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1520 *(signed int *) loc += v;
1524 *(unsigned short *) loc += v;
1528 *(unsigned char *) loc += v;
1532 *(unsigned int *) loc += v - dot;
1536 *(unsigned short *) loc += v - dot;
1540 *(unsigned char *) loc += v - dot;
1543 case R_X86_64_GLOB_DAT:
1544 case R_X86_64_JUMP_SLOT:
1548 case R_X86_64_RELATIVE:
1549 *loc += f->baseaddr;
1552 case R_X86_64_GOT32:
1553 case R_X86_64_GOTPCREL:
1556 if (!isym->gotent.reloc_done)
1558 isym->gotent.reloc_done = 1;
1559 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1561 /* XXX are these really correct? */
1562 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1563 *(unsigned int *) loc += v + isym->gotent.offset;
1565 *loc += isym->gotent.offset;
1570 # warning "no idea how to handle relocations on your arch"
1574 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1575 ret = obj_reloc_unhandled;
1578 #if defined(USE_PLT_ENTRIES)
1582 /* find the plt entry and initialize it if necessary */
1584 #if defined(USE_PLT_LIST)
1585 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1592 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1594 /* generate some machine code */
1596 #if defined(__arm__)
1597 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1598 ip[1] = v; /* sym@ */
1600 #if defined(__powerpc__)
1601 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1602 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1603 ip[2] = 0x7d6903a6; /* mtctr r11 */
1604 ip[3] = 0x4e800420; /* bctr */
1606 #if defined(__v850e__)
1607 /* We have to trash a register, so we assume that any control
1608 transfer more than 21-bits away must be a function call
1609 (so we can use a call-clobbered register). */
1610 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1611 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1616 /* relative distance to target */
1618 /* if the target is too far away.... */
1619 #if defined(__arm__) || defined(__powerpc__)
1620 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1621 #elif defined(__v850e__)
1622 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1624 /* go via the plt */
1625 v = plt + pe->offset - dot;
1627 #if defined(__v850e__)
1632 ret = obj_reloc_dangerous;
1634 /* merge the offset into the instruction. */
1635 #if defined(__arm__)
1636 /* Convert to words. */
1639 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1641 #if defined(__powerpc__)
1642 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1644 #if defined(__v850e__)
1645 /* We write two shorts instead of a long because even 32-bit insns
1646 only need half-word alignment, but the 32-bit data write needs
1647 to be long-word aligned. */
1648 ((unsigned short *)loc)[0] =
1649 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1650 | ((v >> 16) & 0x3f); /* offs high part */
1651 ((unsigned short *)loc)[1] =
1652 (v & 0xffff); /* offs low part */
1655 #endif /* USE_PLT_ENTRIES */
1657 #if defined(USE_GOT_ENTRIES)
1660 /* needs an entry in the .got: set it, once */
1661 if (!isym->gotent.inited) {
1662 isym->gotent.inited = 1;
1663 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1665 /* make the reloc with_respect_to_.got */
1667 *loc += isym->gotent.offset + rel->r_addend;
1668 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1669 *loc += isym->gotent.offset;
1673 #endif /* USE_GOT_ENTRIES */
1680 #if defined(USE_LIST)
1682 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1683 int offset, int size)
1685 struct arch_list_entry *pe;
1687 for (pe = *list; pe != NULL; pe = pe->next) {
1688 if (pe->addend == rel->r_addend) {
1694 pe = xzalloc(sizeof(struct arch_list_entry));
1696 pe->addend = rel->r_addend;
1697 pe->offset = offset;
1707 #if defined(USE_SINGLE)
1709 static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
1710 int offset, int size)
1712 if (single->allocated == 0) {
1713 single->allocated = 1;
1714 single->offset = offset;
1723 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1725 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
1726 int offset, int size)
1728 struct obj_section *myrelsec = obj_find_section(f, name);
1735 obj_extend_section(myrelsec, offset);
1737 myrelsec = obj_create_alloced_section(f, name,
1746 static void arch_create_got(struct obj_file *f)
1748 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1749 struct arch_file *ifile = (struct arch_file *) f;
1751 #if defined(USE_GOT_ENTRIES)
1752 int got_offset = 0, got_needed = 0, got_allocate;
1754 #if defined(USE_PLT_ENTRIES)
1755 int plt_offset = 0, plt_needed = 0, plt_allocate;
1757 struct obj_section *relsec, *symsec, *strsec;
1758 ElfW(RelM) *rel, *relend;
1759 ElfW(Sym) *symtab, *extsym;
1760 const char *strtab, *name;
1761 struct arch_symbol *intsym;
1763 for (i = 0; i < f->header.e_shnum; ++i) {
1764 relsec = f->sections[i];
1765 if (relsec->header.sh_type != SHT_RELM)
1768 symsec = f->sections[relsec->header.sh_link];
1769 strsec = f->sections[symsec->header.sh_link];
1771 rel = (ElfW(RelM) *) relsec->contents;
1772 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1773 symtab = (ElfW(Sym) *) symsec->contents;
1774 strtab = (const char *) strsec->contents;
1776 for (; rel < relend; ++rel) {
1777 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1779 #if defined(USE_GOT_ENTRIES)
1782 #if defined(USE_PLT_ENTRIES)
1786 switch (ELF_R_TYPE(rel->r_info)) {
1787 #if defined(__arm__)
1802 #elif defined(__i386__)
1812 #elif defined(__powerpc__)
1817 #elif defined(__mc68000__)
1828 #elif defined(__sh__)
1838 #elif defined(__v850e__)
1839 case R_V850_22_PCREL:
1848 if (extsym->st_name != 0) {
1849 name = strtab + extsym->st_name;
1851 name = f->sections[extsym->st_shndx]->name;
1853 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1854 #if defined(USE_GOT_ENTRIES)
1856 got_offset += arch_single_init(
1857 /*rel,*/ &intsym->gotent,
1858 got_offset, GOT_ENTRY_SIZE);
1863 #if defined(USE_PLT_ENTRIES)
1865 #if defined(USE_PLT_LIST)
1866 plt_offset += arch_list_add(
1867 rel, &intsym->pltent,
1868 plt_offset, PLT_ENTRY_SIZE);
1870 plt_offset += arch_single_init(
1871 /*rel,*/ &intsym->pltent,
1872 plt_offset, PLT_ENTRY_SIZE);
1880 #if defined(USE_GOT_ENTRIES)
1882 ifile->got = arch_xsect_init(f, ".got", got_offset,
1887 #if defined(USE_PLT_ENTRIES)
1889 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1894 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1897 /*======================================================================*/
1899 /* Standard ELF hash function. */
1900 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1902 unsigned long h = 0;
1909 g = (h & 0xf0000000);
1919 static unsigned long obj_elf_hash(const char *name)
1921 return obj_elf_hash_n(name, strlen(name));
1924 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1925 /* String comparison for non-co-versioned kernel and module. */
1927 static int ncv_strcmp(const char *a, const char *b)
1929 size_t alen = strlen(a), blen = strlen(b);
1931 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1932 return strncmp(a, b, alen);
1933 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1934 return strncmp(a, b, blen);
1936 return strcmp(a, b);
1939 /* String hashing for non-co-versioned kernel and module. Here
1940 we are simply forced to drop the crc from the hash. */
1942 static unsigned long ncv_symbol_hash(const char *str)
1944 size_t len = strlen(str);
1945 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1947 return obj_elf_hash_n(str, len);
1951 obj_set_symbol_compare(struct obj_file *f,
1952 int (*cmp) (const char *, const char *),
1953 unsigned long (*hash) (const char *))
1956 f->symbol_cmp = cmp;
1958 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1961 f->symbol_hash = hash;
1963 memcpy(tmptab, f->symtab, sizeof(tmptab));
1964 memset(f->symtab, 0, sizeof(f->symtab));
1966 for (i = 0; i < HASH_BUCKETS; ++i)
1967 for (sym = tmptab[i]; sym; sym = next) {
1968 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1970 sym->next = f->symtab[h];
1976 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
1978 static struct obj_symbol *
1979 obj_add_symbol(struct obj_file *f, const char *name,
1980 unsigned long symidx, int info,
1981 int secidx, ElfW(Addr) value,
1984 struct obj_symbol *sym;
1985 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1986 int n_type = ELF_ST_TYPE(info);
1987 int n_binding = ELF_ST_BIND(info);
1989 for (sym = f->symtab[hash]; sym; sym = sym->next) {
1990 if (f->symbol_cmp(sym->name, name) == 0) {
1991 int o_secidx = sym->secidx;
1992 int o_info = sym->info;
1993 int o_type = ELF_ST_TYPE(o_info);
1994 int o_binding = ELF_ST_BIND(o_info);
1996 /* A redefinition! Is it legal? */
1998 if (secidx == SHN_UNDEF)
2000 else if (o_secidx == SHN_UNDEF)
2002 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2003 /* Cope with local and global symbols of the same name
2004 in the same object file, as might have been created
2005 by ld -r. The only reason locals are now seen at this
2006 level at all is so that we can do semi-sensible things
2009 struct obj_symbol *nsym, **p;
2011 nsym = arch_new_symbol();
2012 nsym->next = sym->next;
2015 /* Excise the old (local) symbol from the hash chain. */
2016 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2020 } else if (n_binding == STB_LOCAL) {
2021 /* Another symbol of the same name has already been defined.
2022 Just add this to the local table. */
2023 sym = arch_new_symbol();
2026 f->local_symtab[symidx] = sym;
2028 } else if (n_binding == STB_WEAK)
2030 else if (o_binding == STB_WEAK)
2032 /* Don't unify COMMON symbols with object types the programmer
2034 else if (secidx == SHN_COMMON
2035 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2037 else if (o_secidx == SHN_COMMON
2038 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2041 /* Don't report an error if the symbol is coming from
2042 the kernel or some external module. */
2043 if (secidx <= SHN_HIRESERVE)
2044 bb_error_msg("%s multiply defined", name);
2050 /* Completely new symbol. */
2051 sym = arch_new_symbol();
2052 sym->next = f->symtab[hash];
2053 f->symtab[hash] = sym;
2055 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
2056 if (symidx >= f->local_symtab_size)
2057 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2058 name, (long) symidx, (long) f->local_symtab_size);
2060 f->local_symtab[symidx] = sym;
2067 sym->secidx = secidx;
2073 static struct obj_symbol *
2074 obj_find_symbol(struct obj_file *f, const char *name)
2076 struct obj_symbol *sym;
2077 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2079 for (sym = f->symtab[hash]; sym; sym = sym->next)
2080 if (f->symbol_cmp(sym->name, name) == 0)
2085 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2088 if (sym->secidx >= SHN_LORESERVE)
2090 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2092 /* As a special case, a NULL sym has value zero. */
2096 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2098 int i, n = f->header.e_shnum;
2100 for (i = 0; i < n; ++i)
2101 if (strcmp(f->sections[i]->name, name) == 0)
2102 return f->sections[i];
2106 static int obj_load_order_prio(struct obj_section *a)
2108 unsigned long af, ac;
2110 af = a->header.sh_flags;
2113 if (a->name[0] != '.' || strlen(a->name) != 10
2114 || strcmp(a->name + 5, ".init") != 0
2120 if (!(af & SHF_WRITE))
2122 if (af & SHF_EXECINSTR)
2124 if (a->header.sh_type != SHT_NOBITS)
2131 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2133 struct obj_section **p;
2134 int prio = obj_load_order_prio(sec);
2135 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2136 if (obj_load_order_prio(*p) < prio)
2138 sec->load_next = *p;
2142 static struct obj_section *helper_create_alloced_section(struct obj_file *f,
2144 unsigned long align,
2147 int newidx = f->header.e_shnum++;
2148 struct obj_section *sec;
2150 f->sections = xrealloc_vector(f->sections, 2, newidx);
2151 f->sections[newidx] = sec = arch_new_section();
2153 sec->header.sh_type = SHT_PROGBITS;
2154 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2155 sec->header.sh_size = size;
2156 sec->header.sh_addralign = align;
2160 sec->contents = xzalloc(size);
2165 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2167 unsigned long align,
2170 struct obj_section *sec;
2172 sec = helper_create_alloced_section(f, name, align, size);
2173 obj_insert_section_load_order(f, sec);
2177 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2179 unsigned long align,
2182 struct obj_section *sec;
2184 sec = helper_create_alloced_section(f, name, align, size);
2185 sec->load_next = f->load_order;
2186 f->load_order = sec;
2187 if (f->load_order_search_start == &f->load_order)
2188 f->load_order_search_start = &sec->load_next;
2193 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2195 unsigned long oldsize = sec->header.sh_size;
2197 sec->header.sh_size += more;
2198 sec->contents = xrealloc(sec->contents, sec->header.sh_size);
2200 return sec->contents + oldsize;
2204 /* Conditionally add the symbols from the given symbol set to the
2207 static int add_symbols_from(struct obj_file *f,
2209 struct new_module_symbol *syms,
2212 struct new_module_symbol *s;
2215 #ifdef SYMBOL_PREFIX
2216 char *name_buf = NULL;
2217 size_t name_alloced_size = 0;
2219 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2222 gpl = obj_gpl_license(f, NULL) == 0;
2224 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2225 /* Only add symbols that are already marked external.
2226 If we override locals we may cause problems for
2227 argument initialization. We will also create a false
2228 dependency on the module. */
2229 struct obj_symbol *sym;
2232 /* GPL licensed modules can use symbols exported with
2233 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2234 * exported names. Non-GPL modules never see any GPLONLY_
2235 * symbols so they cannot fudge it by adding the prefix on
2238 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2239 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2246 name = (char *)s->name;
2248 #ifdef SYMBOL_PREFIX
2249 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2250 kernel exports `C names', but module object files
2251 reference `linker names'). */
2252 size_t extra = sizeof SYMBOL_PREFIX;
2253 size_t name_size = strlen(name) + extra;
2254 if (name_size > name_alloced_size) {
2255 name_alloced_size = name_size * 2;
2256 name_buf = alloca(name_alloced_size);
2258 strcpy(name_buf, SYMBOL_PREFIX);
2259 strcpy(name_buf + extra - 1, name);
2263 sym = obj_find_symbol(f, name);
2264 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2265 #ifdef SYMBOL_PREFIX
2266 /* Put NAME_BUF into more permanent storage. */
2267 name = xmalloc(name_size);
2268 strcpy(name, name_buf);
2270 sym = obj_add_symbol(f, name, -1,
2271 ELF_ST_INFO(STB_GLOBAL,
2274 /* Did our symbol just get installed? If so, mark the
2275 module as "used". */
2276 if (sym->secidx == idx)
2284 static void add_kernel_symbols(struct obj_file *f)
2286 struct external_module *m;
2289 /* Add module symbols first. */
2291 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
2293 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
2300 n_ext_modules_used = nused;
2302 /* And finally the symbols from the kernel proper. */
2305 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2308 static char *get_modinfo_value(struct obj_file *f, const char *key)
2310 struct obj_section *sec;
2311 char *p, *v, *n, *ep;
2312 size_t klen = strlen(key);
2314 sec = obj_find_section(f, ".modinfo");
2318 ep = p + sec->header.sh_size;
2321 n = strchr(p, '\0');
2323 if (p + klen == v && strncmp(p, key, klen) == 0)
2326 if (p + klen == n && strcmp(p, key) == 0)
2336 /*======================================================================*/
2337 /* Functions relating to module loading after 2.1.18. */
2339 /* From Linux-2.6 sources */
2340 /* You can use " around spaces, but can't escape ". */
2341 /* Hyphens and underscores equivalent in parameter names. */
2342 static char *next_arg(char *args, char **param, char **val)
2344 unsigned int i, equals = 0;
2345 int in_quote = 0, quoted = 0;
2354 for (i = 0; args[i]; i++) {
2355 if (args[i] == ' ' && !in_quote)
2362 in_quote = !in_quote;
2369 args[equals] = '\0';
2370 *val = args + equals + 1;
2372 /* Don't include quotes in value. */
2375 if (args[i-1] == '"')
2378 if (quoted && args[i-1] == '"')
2384 next = args + i + 1;
2388 /* Chew up trailing spaces. */
2389 return skip_whitespace(next);
2393 new_process_module_arguments(struct obj_file *f, const char *options)
2395 char *xoptions, *pos;
2398 xoptions = pos = xstrdup(skip_whitespace(options));
2400 unsigned long charssize = 0;
2401 char *tmp, *contents, *loc, *pinfo, *p;
2402 struct obj_symbol *sym;
2403 int min, max, n, len;
2405 pos = next_arg(pos, ¶m, &val);
2407 tmp = xasprintf("parm_%s", param);
2408 pinfo = get_modinfo_value(f, tmp);
2411 bb_error_msg_and_die("invalid parameter %s", param);
2413 #ifdef SYMBOL_PREFIX
2414 tmp = xasprintf(SYMBOL_PREFIX "%s", param);
2415 sym = obj_find_symbol(f, tmp);
2418 sym = obj_find_symbol(f, param);
2421 /* Also check that the parameter was not resolved from the kernel. */
2422 if (sym == NULL || sym->secidx > SHN_HIRESERVE)
2423 bb_error_msg_and_die("symbol for parameter %s not found", param);
2425 /* Number of parameters */
2426 if (isdigit(*pinfo)) {
2427 min = strtoul(pinfo, &pinfo, 10);
2429 max = strtoul(pinfo + 1, &pinfo, 10);
2435 contents = f->sections[sym->secidx]->contents;
2436 loc = contents + sym->value;
2438 if (*pinfo == 'c') {
2439 if (!isdigit(*(pinfo + 1))) {
2440 bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
2441 " the maximum size", param);
2443 charssize = strtoul(pinfo + 1, (char **) NULL, 10);
2448 bb_error_msg_and_die("argument expected for parameter %s", param);
2452 /* Parse parameter values */
2457 bb_error_msg_and_die("too many values for %s (max %d)", param, max);
2461 len = strcspn(p, ",");
2463 obj_string_patch(f, sym->secidx,
2465 loc += tgt_sizeof_char_p;
2469 len = strcspn(p, ",");
2471 if (len >= charssize)
2472 bb_error_msg_and_die("string too long for %s (max %ld)", param,
2474 strcpy((char *) loc, p);
2479 *loc++ = strtoul(p, &p, 0);
2482 *(short *) loc = strtoul(p, &p, 0);
2483 loc += tgt_sizeof_short;
2486 *(int *) loc = strtoul(p, &p, 0);
2487 loc += tgt_sizeof_int;
2490 *(long *) loc = strtoul(p, &p, 0);
2491 loc += tgt_sizeof_long;
2494 bb_error_msg_and_die("unknown parameter type '%c' for %s",
2498 p = skip_whitespace(p);
2501 p = skip_whitespace(p + 1);
2505 bb_error_msg_and_die("parameter %s requires at least %d arguments", param, min);
2507 bb_error_msg_and_die("invalid argument syntax for %s", param);
2513 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2514 static int new_is_module_checksummed(struct obj_file *f)
2516 const char *p = get_modinfo_value(f, "using_checksums");
2522 /* Get the module's kernel version in the canonical integer form. */
2525 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2530 p = get_modinfo_value(f, "kernel_version");
2533 safe_strncpy(str, p, STRVERSIONLEN);
2535 a = strtoul(p, &p, 10);
2538 b = strtoul(p + 1, &p, 10);
2541 c = strtoul(p + 1, &q, 10);
2545 return a << 16 | b << 8 | c;
2548 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2551 /* Fetch the loaded modules, and all currently exported symbols. */
2553 static void new_get_kernel_symbols(void)
2555 char *module_names, *mn;
2556 struct external_module *modules, *m;
2557 struct new_module_symbol *syms, *s;
2558 size_t ret, bufsize, nmod, nsyms, i, j;
2560 /* Collect the loaded modules. */
2563 module_names = xmalloc(bufsize);
2566 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2567 if (errno == ENOSPC && bufsize < ret) {
2569 module_names = xrealloc(module_names, bufsize);
2570 goto retry_modules_load;
2572 bb_perror_msg_and_die("QM_MODULES");
2575 n_ext_modules = nmod = ret;
2577 /* Collect the modules' symbols. */
2580 ext_modules = modules = xzalloc(nmod * sizeof(*modules));
2581 for (i = 0, mn = module_names, m = modules;
2582 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2583 struct new_module_info info;
2585 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2586 if (errno == ENOENT) {
2587 /* The module was removed out from underneath us. */
2590 bb_perror_msg_and_die("query_module: QM_INFO: %s", mn);
2594 syms = xmalloc(bufsize);
2596 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2600 syms = xrealloc(syms, bufsize);
2601 goto retry_mod_sym_load;
2603 /* The module was removed out from underneath us. */
2606 bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn);
2612 m->addr = info.addr;
2616 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2617 s->name += (unsigned long) syms;
2622 /* Collect the kernel's symbols. */
2624 bufsize = 16 * 1024;
2625 syms = xmalloc(bufsize);
2626 retry_kern_sym_load:
2627 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2628 if (errno == ENOSPC && bufsize < ret) {
2630 syms = xrealloc(syms, bufsize);
2631 goto retry_kern_sym_load;
2633 bb_perror_msg_and_die("kernel: QM_SYMBOLS");
2635 nksyms = nsyms = ret;
2638 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2639 s->name += (unsigned long) syms;
2644 /* Return the kernel symbol checksum version, or zero if not used. */
2646 static int new_is_kernel_checksummed(void)
2648 struct new_module_symbol *s;
2651 /* Using_Versions is not the first symbol, but it should be in there. */
2653 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2654 if (strcmp((char *) s->name, "Using_Versions") == 0)
2661 static void new_create_this_module(struct obj_file *f, const char *m_name)
2663 struct obj_section *sec;
2665 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2666 sizeof(struct new_module));
2667 /* done by obj_create_alloced_section_first: */
2668 /*memset(sec->contents, 0, sizeof(struct new_module));*/
2670 obj_add_symbol(f, SPFX "__this_module", -1,
2671 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2672 sizeof(struct new_module));
2674 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2678 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2679 /* add an entry to the __ksymtab section, creating it if necessary */
2680 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2682 struct obj_section *sec;
2685 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2686 * If __ksymtab is defined but not marked alloc, x out the first character
2687 * (no obj_delete routine) and create a new __ksymtab with the correct
2690 sec = obj_find_section(f, "__ksymtab");
2691 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2692 *((char *)(sec->name)) = 'x'; /* override const */
2696 sec = obj_create_alloced_section(f, "__ksymtab",
2697 tgt_sizeof_void_p, 0);
2700 sec->header.sh_flags |= SHF_ALLOC;
2701 /* Empty section might be byte-aligned */
2702 sec->header.sh_addralign = tgt_sizeof_void_p;
2703 ofs = sec->header.sh_size;
2704 obj_symbol_patch(f, sec->idx, ofs, sym);
2705 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2706 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2708 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2710 static int new_create_module_ksymtab(struct obj_file *f)
2712 struct obj_section *sec;
2715 /* We must always add the module references. */
2717 if (n_ext_modules_used) {
2718 struct new_module_ref *dep;
2719 struct obj_symbol *tm;
2721 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2722 (sizeof(struct new_module_ref)
2723 * n_ext_modules_used));
2727 tm = obj_find_symbol(f, SPFX "__this_module");
2728 dep = (struct new_module_ref *) sec->contents;
2729 for (i = 0; i < n_ext_modules; ++i)
2730 if (ext_modules[i].used) {
2731 dep->dep = ext_modules[i].addr;
2732 obj_symbol_patch(f, sec->idx,
2733 (char *) &dep->ref - sec->contents, tm);
2739 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2743 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2745 /* We don't want to export symbols residing in sections that
2746 aren't loaded. There are a number of these created so that
2747 we make sure certain module options don't appear twice. */
2748 i = f->header.e_shnum;
2749 loaded = alloca(sizeof(int) * i);
2751 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2753 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2754 struct obj_symbol *sym;
2755 for (sym = f->symtab[i]; sym; sym = sym->next) {
2756 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2757 && sym->secidx <= SHN_HIRESERVE
2758 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
2760 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2762 obj_symbol_patch(f, sec->idx, ofs, sym);
2763 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2770 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2778 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2780 struct new_module *module;
2781 struct obj_section *sec;
2786 sec = obj_find_section(f, ".this");
2787 if (!sec || !sec->contents) {
2788 bb_perror_msg_and_die("corrupt module %s?", m_name);
2790 module = (struct new_module *) sec->contents;
2791 m_addr = sec->header.sh_addr;
2793 module->size_of_struct = sizeof(*module);
2794 module->size = m_size;
2795 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2797 sec = obj_find_section(f, "__ksymtab");
2798 if (sec && sec->header.sh_size) {
2799 module->syms = sec->header.sh_addr;
2800 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2803 if (n_ext_modules_used) {
2804 sec = obj_find_section(f, ".kmodtab");
2805 module->deps = sec->header.sh_addr;
2806 module->ndeps = n_ext_modules_used;
2809 module->init = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2810 module->cleanup = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2812 sec = obj_find_section(f, "__ex_table");
2814 module->ex_table_start = sec->header.sh_addr;
2815 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2818 sec = obj_find_section(f, ".text.init");
2820 module->runsize = sec->header.sh_addr - m_addr;
2822 sec = obj_find_section(f, ".data.init");
2824 if (!module->runsize
2825 || module->runsize > sec->header.sh_addr - m_addr
2827 module->runsize = sec->header.sh_addr - m_addr;
2830 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2831 if (sec && sec->header.sh_size) {
2832 module->archdata_start = (void*)sec->header.sh_addr;
2833 module->archdata_end = module->archdata_start + sec->header.sh_size;
2835 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2836 if (sec && sec->header.sh_size) {
2837 module->kallsyms_start = (void*)sec->header.sh_addr;
2838 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2841 /* Whew! All of the initialization is complete. Collect the final
2842 module image and give it to the kernel. */
2844 image = xmalloc(m_size);
2845 obj_create_image(f, image);
2847 ret = init_module(m_name, (struct new_module *) image);
2849 bb_perror_msg("init_module: %s", m_name);
2857 /*======================================================================*/
2860 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2863 struct obj_string_patch *p;
2864 struct obj_section *strsec;
2865 size_t len = strlen(string) + 1;
2868 p = xzalloc(sizeof(*p));
2869 p->next = f->string_patches;
2870 p->reloc_secidx = secidx;
2871 p->reloc_offset = offset;
2872 f->string_patches = p;
2874 strsec = obj_find_section(f, ".kstrtab");
2875 if (strsec == NULL) {
2876 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2877 /*p->string_offset = 0;*/
2878 loc = strsec->contents;
2880 p->string_offset = strsec->header.sh_size;
2881 loc = obj_extend_section(strsec, len);
2883 memcpy(loc, string, len);
2887 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2888 struct obj_symbol *sym)
2890 struct obj_symbol_patch *p;
2892 p = xmalloc(sizeof(*p));
2893 p->next = f->symbol_patches;
2894 p->reloc_secidx = secidx;
2895 p->reloc_offset = offset;
2897 f->symbol_patches = p;
2900 static void obj_check_undefineds(struct obj_file *f)
2904 for (i = 0; i < HASH_BUCKETS; ++i) {
2905 struct obj_symbol *sym;
2906 for (sym = f->symtab[i]; sym; sym = sym->next) {
2907 if (sym->secidx == SHN_UNDEF) {
2908 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
2909 sym->secidx = SHN_ABS;
2913 bb_error_msg_and_die("unresolved symbol %s", sym->name);
2920 static void obj_allocate_commons(struct obj_file *f)
2922 struct common_entry {
2923 struct common_entry *next;
2924 struct obj_symbol *sym;
2925 } *common_head = NULL;
2929 for (i = 0; i < HASH_BUCKETS; ++i) {
2930 struct obj_symbol *sym;
2931 for (sym = f->symtab[i]; sym; sym = sym->next) {
2932 if (sym->secidx == SHN_COMMON) {
2933 /* Collect all COMMON symbols and sort them by size so as to
2934 minimize space wasted by alignment requirements. */
2935 struct common_entry **p, *n;
2936 for (p = &common_head; *p; p = &(*p)->next)
2937 if (sym->size <= (*p)->sym->size)
2939 n = alloca(sizeof(*n));
2947 for (i = 1; i < f->local_symtab_size; ++i) {
2948 struct obj_symbol *sym = f->local_symtab[i];
2949 if (sym && sym->secidx == SHN_COMMON) {
2950 struct common_entry **p, *n;
2951 for (p = &common_head; *p; p = &(*p)->next) {
2952 if (sym == (*p)->sym)
2954 if (sym->size < (*p)->sym->size) {
2955 n = alloca(sizeof(*n));
2966 /* Find the bss section. */
2967 for (i = 0; i < f->header.e_shnum; ++i)
2968 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2971 /* If for some reason there hadn't been one, create one. */
2972 if (i == f->header.e_shnum) {
2973 struct obj_section *sec;
2975 f->header.e_shnum++;
2976 f->sections = xrealloc_vector(f->sections, 2, i);
2977 f->sections[i] = sec = arch_new_section();
2979 sec->header.sh_type = SHT_PROGBITS;
2980 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2985 /* Allocate the COMMONS. */
2987 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2988 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2989 struct common_entry *c;
2991 for (c = common_head; c; c = c->next) {
2992 ElfW(Addr) align = c->sym->value;
2994 if (align > max_align)
2996 if (bss_size & (align - 1))
2997 bss_size = (bss_size | (align - 1)) + 1;
3000 c->sym->value = bss_size;
3002 bss_size += c->sym->size;
3005 f->sections[i]->header.sh_size = bss_size;
3006 f->sections[i]->header.sh_addralign = max_align;
3010 /* For the sake of patch relocation and parameter initialization,
3011 allocate zeroed data for NOBITS sections now. Note that after
3012 this we cannot assume NOBITS are really empty. */
3013 for (i = 0; i < f->header.e_shnum; ++i) {
3014 struct obj_section *s = f->sections[i];
3015 if (s->header.sh_type == SHT_NOBITS) {
3017 if (s->header.sh_size != 0)
3018 s->contents = xzalloc(s->header.sh_size);
3019 s->header.sh_type = SHT_PROGBITS;
3024 static unsigned long obj_load_size(struct obj_file *f)
3026 unsigned long dot = 0;
3027 struct obj_section *sec;
3029 /* Finalize the positions of the sections relative to one another. */
3031 for (sec = f->load_order; sec; sec = sec->load_next) {
3034 align = sec->header.sh_addralign;
3035 if (align && (dot & (align - 1)))
3036 dot = (dot | (align - 1)) + 1;
3038 sec->header.sh_addr = dot;
3039 dot += sec->header.sh_size;
3045 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3047 int i, n = f->header.e_shnum;
3050 /* Finalize the addresses of the sections. */
3053 for (i = 0; i < n; ++i)
3054 f->sections[i]->header.sh_addr += base;
3056 /* And iterate over all of the relocations. */
3058 for (i = 0; i < n; ++i) {
3059 struct obj_section *relsec, *symsec, *targsec, *strsec;
3060 ElfW(RelM) * rel, *relend;
3064 relsec = f->sections[i];
3065 if (relsec->header.sh_type != SHT_RELM)
3068 symsec = f->sections[relsec->header.sh_link];
3069 targsec = f->sections[relsec->header.sh_info];
3070 strsec = f->sections[symsec->header.sh_link];
3072 rel = (ElfW(RelM) *) relsec->contents;
3073 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3074 symtab = (ElfW(Sym) *) symsec->contents;
3075 strtab = (const char *) strsec->contents;
3077 for (; rel < relend; ++rel) {
3078 ElfW(Addr) value = 0;
3079 struct obj_symbol *intsym = NULL;
3080 unsigned long symndx;
3081 ElfW(Sym) *extsym = NULL;
3084 /* Attempt to find a value to use for this relocation. */
3086 symndx = ELF_R_SYM(rel->r_info);
3088 /* Note we've already checked for undefined symbols. */
3090 extsym = &symtab[symndx];
3091 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3092 /* Local symbols we look up in the local table to be sure
3093 we get the one that is really intended. */
3094 intsym = f->local_symtab[symndx];
3096 /* Others we look up in the hash table. */
3098 if (extsym->st_name)
3099 name = strtab + extsym->st_name;
3101 name = f->sections[extsym->st_shndx]->name;
3102 intsym = obj_find_symbol(f, name);
3105 value = obj_symbol_final_value(f, intsym);
3106 intsym->referenced = 1;
3108 #if SHT_RELM == SHT_RELA
3109 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3110 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3111 if (!extsym || !extsym->st_name
3112 || ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3114 value += rel->r_addend;
3118 switch (arch_apply_relocation
3119 (f, targsec, /*symsec,*/ intsym, rel, value)
3124 case obj_reloc_overflow:
3125 errmsg = "Relocation overflow";
3127 case obj_reloc_dangerous:
3128 errmsg = "Dangerous relocation";
3130 case obj_reloc_unhandled:
3131 errmsg = "Unhandled relocation";
3134 bb_error_msg("%s of type %ld for %s", errmsg,
3135 (long) ELF_R_TYPE(rel->r_info),
3136 strtab + extsym->st_name);
3138 bb_error_msg("%s of type %ld", errmsg,
3139 (long) ELF_R_TYPE(rel->r_info));
3147 /* Finally, take care of the patches. */
3149 if (f->string_patches) {
3150 struct obj_string_patch *p;
3151 struct obj_section *strsec;
3152 ElfW(Addr) strsec_base;
3153 strsec = obj_find_section(f, ".kstrtab");
3154 strsec_base = strsec->header.sh_addr;
3156 for (p = f->string_patches; p; p = p->next) {
3157 struct obj_section *targsec = f->sections[p->reloc_secidx];
3158 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3159 = strsec_base + p->string_offset;
3163 if (f->symbol_patches) {
3164 struct obj_symbol_patch *p;
3166 for (p = f->symbol_patches; p; p = p->next) {
3167 struct obj_section *targsec = f->sections[p->reloc_secidx];
3168 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3169 = obj_symbol_final_value(f, p->sym);
3176 static int obj_create_image(struct obj_file *f, char *image)
3178 struct obj_section *sec;
3179 ElfW(Addr) base = f->baseaddr;
3181 for (sec = f->load_order; sec; sec = sec->load_next) {
3184 if (sec->contents == 0 || sec->header.sh_size == 0)
3187 secimg = image + (sec->header.sh_addr - base);
3189 /* Note that we allocated data for NOBITS sections earlier. */
3190 memcpy(secimg, sec->contents, sec->header.sh_size);
3196 /*======================================================================*/
3198 static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits)
3200 #if BB_LITTLE_ENDIAN
3201 # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3)))))
3203 # define ELFMAG_U32 ((uint32_t)((((ELFMAG0 * 0x100) + ELFMAG1) * 0x100 + ELFMAG2) * 0x100 + ELFMAG3))
3206 ElfW(Shdr) * section_headers;
3210 /* Read the file header. */
3212 f = arch_new_file();
3213 f->symbol_cmp = strcmp;
3214 f->symbol_hash = obj_elf_hash;
3215 f->load_order_search_start = &f->load_order;
3217 if (image_size < sizeof(f->header))
3218 bb_error_msg_and_die("error while loading ELF header");
3219 memcpy(&f->header, image, sizeof(f->header));
3221 if (*(uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
3222 bb_error_msg_and_die("not an ELF file");
3224 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3225 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB)
3226 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3227 || !MATCH_MACHINE(f->header.e_machine)
3229 bb_error_msg_and_die("ELF file not for this architecture");
3231 if (f->header.e_type != ET_REL) {
3232 bb_error_msg_and_die("ELF file not a relocatable object");
3235 /* Read the section headers. */
3237 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3238 bb_error_msg_and_die("section header size mismatch: %lu != %lu",
3239 (unsigned long) f->header.e_shentsize,
3240 (unsigned long) sizeof(ElfW(Shdr)));
3243 shnum = f->header.e_shnum;
3244 /* Growth of ->sections vector will be done by
3245 * xrealloc_vector(..., 2, ...), therefore we must allocate
3246 * at least 2^2 = 4 extra elements here. */
3247 f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
3249 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3250 if (image_size < f->header.e_shoff + sizeof(ElfW(Shdr)) * shnum)
3251 bb_error_msg_and_die("error while loading section headers");
3252 memcpy(section_headers, image + f->header.e_shoff, sizeof(ElfW(Shdr)) * shnum);
3254 /* Read the section data. */
3256 for (i = 0; i < shnum; ++i) {
3257 struct obj_section *sec;
3259 f->sections[i] = sec = arch_new_section();
3261 sec->header = section_headers[i];
3264 if (sec->header.sh_size) {
3265 switch (sec->header.sh_type) {
3273 if (!loadprogbits) {
3274 sec->contents = NULL;
3281 sec->contents = NULL;
3282 if (sec->header.sh_size > 0) {
3283 sec->contents = xmalloc(sec->header.sh_size);
3284 if (image_size < (sec->header.sh_offset + sec->header.sh_size))
3285 bb_error_msg_and_die("error while loading section data");
3286 memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
3289 #if SHT_RELM == SHT_REL
3291 bb_error_msg_and_die("RELA relocations not supported on this architecture");
3294 bb_error_msg_and_die("REL relocations not supported on this architecture");
3297 if (sec->header.sh_type >= SHT_LOPROC) {
3298 /* Assume processor specific section types are debug
3299 info and can safely be ignored. If this is ever not
3300 the case (Hello MIPS?), don't put ifdefs here but
3301 create an arch_load_proc_section(). */
3305 bb_error_msg_and_die("can't handle sections of type %ld",
3306 (long) sec->header.sh_type);
3311 /* Do what sort of interpretation as needed by each section. */
3313 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3315 for (i = 0; i < shnum; ++i) {
3316 struct obj_section *sec = f->sections[i];
3317 sec->name = shstrtab + sec->header.sh_name;
3320 for (i = 0; i < shnum; ++i) {
3321 struct obj_section *sec = f->sections[i];
3323 /* .modinfo should be contents only but gcc has no attribute for that.
3324 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3326 if (strcmp(sec->name, ".modinfo") == 0)
3327 sec->header.sh_flags &= ~SHF_ALLOC;
3329 if (sec->header.sh_flags & SHF_ALLOC)
3330 obj_insert_section_load_order(f, sec);
3332 switch (sec->header.sh_type) {
3335 unsigned long nsym, j;
3339 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3340 bb_error_msg_and_die("symbol size mismatch: %lu != %lu",
3341 (unsigned long) sec->header.sh_entsize,
3342 (unsigned long) sizeof(ElfW(Sym)));
3345 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3346 strtab = f->sections[sec->header.sh_link]->contents;
3347 sym = (ElfW(Sym) *) sec->contents;
3349 /* Allocate space for a table of local symbols. */
3350 j = f->local_symtab_size = sec->header.sh_info;
3351 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3353 /* Insert all symbols into the hash table. */
3354 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3355 ElfW(Addr) val = sym->st_value;
3358 name = strtab + sym->st_name;
3359 else if (sym->st_shndx < shnum)
3360 name = f->sections[sym->st_shndx]->name;
3363 #if defined(__SH5__)
3365 * For sh64 it is possible that the target of a branch
3366 * requires a mode switch (32 to 16 and back again).
3368 * This is implied by the lsb being set in the target
3369 * address for SHmedia mode and clear for SHcompact.
3371 val |= sym->st_other & 4;
3373 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3380 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3381 bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu",
3382 (unsigned long) sec->header.sh_entsize,
3383 (unsigned long) sizeof(ElfW(RelM)));
3386 /* XXX Relocation code from modutils-2.3.19 is not here.
3387 * Why? That's about 20 lines of code from obj/obj_load.c,
3388 * which gets done in a second pass through the sections.
3389 * This BusyBox insmod does similar work in obj_relocate(). */
3396 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3398 * load the unloaded sections directly into the memory allocated by
3399 * kernel for the module
3402 static int obj_load_progbits(char *image, size_t image_size, struct obj_file *f, char *imagebase)
3404 ElfW(Addr) base = f->baseaddr;
3405 struct obj_section* sec;
3407 for (sec = f->load_order; sec; sec = sec->load_next) {
3408 /* section already loaded? */
3409 if (sec->contents != NULL)
3411 if (sec->header.sh_size == 0)
3413 sec->contents = imagebase + (sec->header.sh_addr - base);
3414 if (image_size < (sec->header.sh_offset + sec->header.sh_size)) {
3415 bb_error_msg("error reading ELF section data");
3416 return 0; /* need to delete half-loaded module! */
3418 memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
3424 static void hide_special_symbols(struct obj_file *f)
3426 static const char *const specials[] = {
3427 SPFX "cleanup_module",
3429 SPFX "kernel_version",
3433 struct obj_symbol *sym;
3434 const char *const *p;
3436 for (p = specials; *p; ++p) {
3437 sym = obj_find_symbol(f, *p);
3439 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3444 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3445 static int obj_gpl_license(struct obj_file *f, const char **license)
3447 struct obj_section *sec;
3448 /* This list must match *exactly* the list of allowable licenses in
3449 * linux/include/linux/module.h. Checking for leading "GPL" will not
3450 * work, somebody will use "GPL sucks, this is proprietary".
3452 static const char *const gpl_licenses[] = {
3455 "GPL and additional rights",
3460 sec = obj_find_section(f, ".modinfo");
3462 const char *value, *ptr, *endptr;
3463 ptr = sec->contents;
3464 endptr = ptr + sec->header.sh_size;
3465 while (ptr < endptr) {
3466 value = strchr(ptr, '=');
3467 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3471 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
3472 if (strcmp(value+1, gpl_licenses[i]) == 0)
3477 ptr = strchr(ptr, '\0');
3487 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3488 #define TAINT_PROPRIETORY_MODULE (1 << 0)
3489 #define TAINT_FORCED_MODULE (1 << 1)
3490 #define TAINT_UNSAFE_SMP (1 << 2)
3491 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3493 static void set_tainted(int fd, const char *m_name,
3494 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3496 static smallint printed_info;
3501 if (fd < 0 && !kernel_has_tainted)
3502 return; /* New modutils on old kernel */
3503 printf("Warning: loading %s will taint the kernel: %s%s\n",
3504 m_name, text1, text2);
3505 if (!printed_info) {
3506 printf(" See %s for information about tainted modules\n", TAINT_URL);
3510 read(fd, buf, sizeof(buf)-1);
3511 buf[sizeof(buf)-1] = '\0';
3512 oldval = strtoul(buf, NULL, 10);
3513 sprintf(buf, "%d\n", oldval | taint);
3514 xwrite_str(fd, buf);
3518 /* Check if loading this module will taint the kernel. */
3519 static void check_tainted_module(struct obj_file *f, const char *m_name)
3521 static const char tainted_file[] ALIGN1 = TAINT_FILENAME;
3523 int fd, kernel_has_tainted;
3526 kernel_has_tainted = 1;
3527 fd = open(tainted_file, O_RDWR);
3529 if (errno == ENOENT)
3530 kernel_has_tainted = 0;
3531 else if (errno == EACCES)
3532 kernel_has_tainted = 1;
3534 perror(tainted_file);
3535 kernel_has_tainted = 0;
3539 switch (obj_gpl_license(f, &ptr)) {
3543 set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3545 default: /* case 2: */
3546 /* The module has a non-GPL license so we pretend that the
3547 * kernel always has a taint flag to get a warning even on
3548 * kernels without the proc flag.
3550 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3554 if (flag_force_load)
3555 set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3560 #else /* !FEATURE_CHECK_TAINTED_MODULE */
3561 #define check_tainted_module(x, y) do { } while (0);
3564 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3565 /* add module source, timestamp, kernel version and a symbol for the
3566 * start of some sections. this info is used by ksymoops to do better
3569 #if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3570 #define get_module_version(f, str) get_module_version(str)
3573 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3575 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3576 return new_get_module_version(f, str);
3578 strncpy(str, "???", sizeof(str));
3583 /* add module source, timestamp, kernel version and a symbol for the
3584 * start of some sections. this info is used by ksymoops to do better
3588 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3591 static const char symprefix[] ALIGN1 = "__insmod_";
3592 static const char section_names[][8] = {
3600 struct obj_section *sec;
3601 struct obj_symbol *sym;
3602 char *name, *absolute_filename;
3603 char str[STRVERSIONLEN];
3605 int lm_name, lfilename, use_ksymtab, version;
3606 struct stat statbuf;
3608 /* WARNING: was using realpath, but replaced by readlink to stop using
3609 * lots of stack. But here it seems to be able to cause problems? */
3610 absolute_filename = xmalloc_readlink(filename);
3611 if (!absolute_filename)
3612 absolute_filename = xstrdup(filename);
3614 lm_name = strlen(m_name);
3615 lfilename = strlen(absolute_filename);
3617 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3618 * are not to be exported. otherwise leave ksymtab alone for now, the
3619 * "export all symbols" compatibility code will export these symbols later.
3621 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3623 sec = obj_find_section(f, ".this");
3625 /* tag the module header with the object name, last modified
3626 * timestamp and module version. worst case for module version
3627 * is 0xffffff, decimal 16777215. putting all three fields in
3628 * one symbol is less readable but saves kernel space.
3630 if (stat(absolute_filename, &statbuf) != 0)
3631 statbuf.st_mtime = 0;
3632 version = get_module_version(f, str); /* -1 if not found */
3633 name = xasprintf("%s%s_O%s_M%0*lX_V%d",
3634 symprefix, m_name, absolute_filename,
3635 (int)(2 * sizeof(statbuf.st_mtime)),
3636 (long)statbuf.st_mtime,
3638 sym = obj_add_symbol(f, name, -1,
3639 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3640 sec->idx, sec->header.sh_addr, 0);
3642 new_add_ksymtab(f, sym);
3644 free(absolute_filename);
3645 #ifdef _NOT_SUPPORTED_
3646 /* record where the persistent data is going, same address as previous symbol */
3648 name = xasprintf("%s%s_P%s",
3649 symprefix, m_name, f->persist);
3650 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3651 sec->idx, sec->header.sh_addr, 0);
3653 new_add_ksymtab(f, sym);
3656 /* tag the desired sections if size is non-zero */
3657 for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
3658 sec = obj_find_section(f, section_names[i]);
3659 if (sec && sec->header.sh_size) {
3660 name = xasprintf("%s%s_S%s_L%ld",
3661 symprefix, m_name, sec->name,
3662 (long)sec->header.sh_size);
3663 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3664 sec->idx, sec->header.sh_addr, 0);
3666 new_add_ksymtab(f, sym);
3670 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3672 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3673 static void print_load_map(struct obj_file *f)
3675 struct obj_section *sec;
3676 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3677 struct obj_symbol **all, **p;
3679 char *loaded; /* array of booleans */
3680 struct obj_symbol *sym;
3682 /* Report on the section layout. */
3683 printf("Sections: Size %-*s Align\n",
3684 (int) (2 * sizeof(void *)), "Address");
3686 for (sec = f->load_order; sec; sec = sec->load_next) {
3690 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3695 printf("%-15s %08lx %0*lx 2**%d\n",
3697 (long)sec->header.sh_size,
3698 (int) (2 * sizeof(void *)),
3699 (long)sec->header.sh_addr,
3702 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3703 /* Quick reference which section indices are loaded. */
3704 i = f->header.e_shnum;
3705 loaded = alloca(i * sizeof(loaded[0]));
3707 loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
3709 /* Collect the symbols we'll be listing. */
3710 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3711 for (sym = f->symtab[i]; sym; sym = sym->next)
3712 if (sym->secidx <= SHN_HIRESERVE
3713 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3718 all = alloca(nsyms * sizeof(all[0]));
3720 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3721 for (sym = f->symtab[i]; sym; sym = sym->next)
3722 if (sym->secidx <= SHN_HIRESERVE
3723 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3728 /* And list them. */
3729 printf("\nSymbols:\n");
3730 for (p = all; p < all + nsyms; ++p) {
3732 unsigned long value;
3735 if (sym->secidx == SHN_ABS) {
3738 } else if (sym->secidx == SHN_UNDEF) {
3742 sec = f->sections[sym->secidx];
3744 if (sec->header.sh_type == SHT_NOBITS)
3746 else if (sec->header.sh_flags & SHF_ALLOC) {
3747 if (sec->header.sh_flags & SHF_EXECINSTR)
3749 else if (sec->header.sh_flags & SHF_WRITE)
3754 value = sym->value + sec->header.sh_addr;
3757 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3758 type |= 0x20; /* tolower. safe for '?' too */
3760 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3765 #else /* !FEATURE_INSMOD_LOAD_MAP */
3766 static void print_load_map(struct obj_file *f UNUSED_PARAM)
3771 int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
3774 unsigned long m_size;
3777 int exit_status = EXIT_FAILURE;
3779 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3783 size_t image_size = 64 * 1024 * 1024;
3785 /* Load module into memory and unzip if compressed */
3786 image = xmalloc_open_zipped_read_close(m_filename, &image_size);
3788 return EXIT_FAILURE;
3790 m_name = xstrdup(bb_basename(m_filename));
3791 /* "module.o[.gz]" -> "module" */
3792 *strchrnul(m_name, '.') = '\0';
3794 f = obj_load(image, image_size, LOADBITS);
3796 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3797 /* Version correspondence? */
3798 m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
3800 char m_strversion[STRVERSIONLEN];
3803 if (m_has_modinfo) {
3804 int m_version = new_get_module_version(f, m_strversion);
3805 if (m_version == -1) {
3806 bb_error_msg_and_die("can't find the kernel version "
3807 "the module was compiled for");
3812 if (strncmp(uts.release, m_strversion, STRVERSIONLEN) != 0) {
3813 bb_error_msg("%skernel-module version mismatch\n"
3814 "\t%s was compiled for kernel version %s\n"
3815 "\twhile this kernel is version %s",
3816 flag_force_load ? "warning: " : "",
3817 m_name, m_strversion, uts.release);
3818 if (!flag_force_load)
3824 if (query_module(NULL, 0, NULL, 0, NULL))
3825 bb_error_msg_and_die("old (unsupported) kernel");
3826 new_get_kernel_symbols();
3827 k_crcs = new_is_kernel_checksummed();
3829 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3833 m_crcs = new_is_module_checksummed(f);
3834 if (m_crcs != k_crcs)
3835 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3839 /* Let the module know about the kernel symbols. */
3840 add_kernel_symbols(f);
3842 /* Allocate common symbols, symbol tables, and string tables. */
3843 new_create_this_module(f, m_name);
3844 obj_check_undefineds(f);
3845 obj_allocate_commons(f);
3846 check_tainted_module(f, m_name);
3848 /* Done with the module name, on to the optional var=value arguments */
3849 new_process_module_arguments(f, options);
3852 hide_special_symbols(f);
3854 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3855 add_ksymoops_symbols(f, m_filename, m_name);
3858 new_create_module_ksymtab(f);
3860 /* Find current size of the module */
3861 m_size = obj_load_size(f);
3863 m_addr = create_module(m_name, m_size);
3864 if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
3866 bb_error_msg_and_die("a module named %s already exists", m_name);
3868 bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes",
3871 bb_perror_msg_and_die("create_module: %s", m_name);
3876 * the PROGBITS section was not loaded by the obj_load
3877 * now we can load them directly into the kernel memory
3879 if (!obj_load_progbits(image, image_size, f, (char*)m_addr)) {
3880 delete_module(m_name, 0);
3885 if (!obj_relocate(f, m_addr)) {
3886 delete_module(m_name, 0);
3890 if (!new_init_module(m_name, f, m_size)) {
3891 delete_module(m_name, 0);
3895 if (flag_print_load_map)
3898 exit_status = EXIT_SUCCESS;