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.
63 #include <sys/utsname.h>
65 #if !ENABLE_FEATURE_2_4_MODULES && !ENABLE_FEATURE_2_6_MODULES
66 #define ENABLE_FEATURE_2_4_MODULES 1
69 #if !ENABLE_FEATURE_2_4_MODULES
70 #define insmod_ng_main insmod_main
73 #if ENABLE_FEATURE_2_6_MODULES
74 extern int insmod_ng_main( int argc, char **argv);
78 #if ENABLE_FEATURE_2_4_MODULES
81 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
89 #if defined(__alpha__)
90 #define MATCH_MACHINE(x) (x == EM_ALPHA)
91 #define SHT_RELM SHT_RELA
92 #define Elf64_RelM Elf64_Rela
93 #define ELFCLASSM ELFCLASS64
98 #define MATCH_MACHINE(x) (x == EM_ARM)
99 #define SHT_RELM SHT_REL
100 #define Elf32_RelM Elf32_Rel
101 #define ELFCLASSM ELFCLASS32
102 #define USE_PLT_ENTRIES
103 #define PLT_ENTRY_SIZE 8
104 #define USE_GOT_ENTRIES
105 #define GOT_ENTRY_SIZE 8
111 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
112 #define SHT_RELM SHT_RELA
113 #define Elf32_RelM Elf32_Rela
114 #define ELFCLASSM ELFCLASS32
118 #if defined(__cris__)
119 #define MATCH_MACHINE(x) (x == EM_CRIS)
120 #define SHT_RELM SHT_RELA
121 #define Elf32_RelM Elf32_Rela
122 #define ELFCLASSM ELFCLASS32
125 #define R_CRIS_NONE 0
131 #if defined(__H8300H__) || defined(__H8300S__)
132 #define MATCH_MACHINE(x) (x == EM_H8_300)
133 #define SHT_RELM SHT_RELA
134 #define Elf32_RelM Elf32_Rela
135 #define ELFCLASSM ELFCLASS32
137 #define SYMBOL_PREFIX "_"
140 /* PA-RISC / HP-PA */
141 #if defined(__hppa__)
142 #define MATCH_MACHINE(x) (x == EM_PARISC)
143 #define SHT_RELM SHT_RELA
144 #if defined(__LP64__)
145 #define Elf64_RelM Elf64_Rela
146 #define ELFCLASSM ELFCLASS64
148 #define Elf32_RelM Elf32_Rela
149 #define ELFCLASSM ELFCLASS32
154 #if defined(__i386__)
156 #define MATCH_MACHINE(x) (x == EM_386)
158 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
160 #define SHT_RELM SHT_REL
161 #define Elf32_RelM Elf32_Rel
162 #define ELFCLASSM ELFCLASS32
163 #define USE_GOT_ENTRIES
164 #define GOT_ENTRY_SIZE 4
168 /* IA64, aka Itanium */
169 #if defined(__ia64__)
170 #define MATCH_MACHINE(x) (x == EM_IA_64)
171 #define SHT_RELM SHT_RELA
172 #define Elf64_RelM Elf64_Rela
173 #define ELFCLASSM ELFCLASS64
177 #if defined(__mc68000__)
178 #define MATCH_MACHINE(x) (x == EM_68K)
179 #define SHT_RELM SHT_RELA
180 #define Elf32_RelM Elf32_Rela
181 #define ELFCLASSM ELFCLASS32
182 #define USE_GOT_ENTRIES
183 #define GOT_ENTRY_SIZE 4
188 #if defined(__microblaze__)
190 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
191 #define SHT_RELM SHT_RELA
192 #define Elf32_RelM Elf32_Rela
193 #define ELFCLASSM ELFCLASS32
197 #if defined(__mips__)
198 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
199 #define SHT_RELM SHT_REL
200 #define Elf32_RelM Elf32_Rel
201 #define ELFCLASSM ELFCLASS32
202 /* Account for ELF spec changes. */
203 #ifndef EM_MIPS_RS3_LE
204 #ifdef EM_MIPS_RS4_BE
205 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
207 #define EM_MIPS_RS3_LE 10
209 #endif /* !EM_MIPS_RS3_LE */
210 #define ARCHDATAM "__dbe_table"
214 #if defined(__nios2__)
215 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
216 #define SHT_RELM SHT_RELA
217 #define Elf32_RelM Elf32_Rela
218 #define ELFCLASSM ELFCLASS32
222 #if defined(__powerpc64__)
223 #define MATCH_MACHINE(x) (x == EM_PPC64)
224 #define SHT_RELM SHT_RELA
225 #define Elf64_RelM Elf64_Rela
226 #define ELFCLASSM ELFCLASS64
227 #elif defined(__powerpc__)
228 #define MATCH_MACHINE(x) (x == EM_PPC)
229 #define SHT_RELM SHT_RELA
230 #define Elf32_RelM Elf32_Rela
231 #define ELFCLASSM ELFCLASS32
232 #define USE_PLT_ENTRIES
233 #define PLT_ENTRY_SIZE 16
235 #define LIST_ARCHTYPE ElfW(Addr)
237 #define ARCHDATAM "__ftr_fixup"
241 #if defined(__s390__)
242 #define MATCH_MACHINE(x) (x == EM_S390)
243 #define SHT_RELM SHT_RELA
244 #define Elf32_RelM Elf32_Rela
245 #define ELFCLASSM ELFCLASS32
246 #define USE_PLT_ENTRIES
247 #define PLT_ENTRY_SIZE 8
248 #define USE_GOT_ENTRIES
249 #define GOT_ENTRY_SIZE 8
255 #define MATCH_MACHINE(x) (x == EM_SH)
256 #define SHT_RELM SHT_RELA
257 #define Elf32_RelM Elf32_Rela
258 #define ELFCLASSM ELFCLASS32
259 #define USE_GOT_ENTRIES
260 #define GOT_ENTRY_SIZE 4
262 /* the SH changes have only been tested in =little endian= mode */
263 /* I'm not sure about big endian, so let's warn: */
264 #if defined(__sh__) && BB_BIG_ENDIAN
265 # error insmod.c may require changes for use on big endian SH
267 /* it may or may not work on the SH1/SH2... Error on those also */
268 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
269 #error insmod.c may require changes for SH1 or SH2 use
274 #if defined(__sparc__)
275 #define MATCH_MACHINE(x) (x == EM_SPARC)
276 #define SHT_RELM SHT_RELA
277 #define Elf32_RelM Elf32_Rela
278 #define ELFCLASSM ELFCLASS32
282 #if defined (__v850e__)
283 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
284 #define SHT_RELM SHT_RELA
285 #define Elf32_RelM Elf32_Rela
286 #define ELFCLASSM ELFCLASS32
287 #define USE_PLT_ENTRIES
288 #define PLT_ENTRY_SIZE 8
290 #ifndef EM_CYGNUS_V850 /* grumble */
291 #define EM_CYGNUS_V850 0x9080
293 #define SYMBOL_PREFIX "_"
297 #if defined(__x86_64__)
298 #define MATCH_MACHINE(x) (x == EM_X86_64)
299 #define SHT_RELM SHT_RELA
300 #define USE_GOT_ENTRIES
301 #define GOT_ENTRY_SIZE 8
303 #define Elf64_RelM Elf64_Rela
304 #define ELFCLASSM ELFCLASS64
308 #error Sorry, but insmod.c does not yet support this architecture...
312 //----------------------------------------------------------------------------
313 //--------modutils module.h, lines 45-242
314 //----------------------------------------------------------------------------
316 /* Definitions for the Linux module syscall interface.
317 Copyright 1996, 1997 Linux International.
319 Contributed by Richard Henderson <rth@tamu.edu>
321 This file is part of the Linux modutils.
323 This program is free software; you can redistribute it and/or modify it
324 under the terms of the GNU General Public License as published by the
325 Free Software Foundation; either version 2 of the License, or (at your
326 option) any later version.
328 This program is distributed in the hope that it will be useful, but
329 WITHOUT ANY WARRANTY; without even the implied warranty of
330 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
331 General Public License for more details.
333 You should have received a copy of the GNU General Public License
334 along with this program; if not, write to the Free Software Foundation,
335 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
338 #ifndef MODUTILS_MODULE_H
339 /* Why? static const int MODUTILS_MODULE_H = 1;*/
341 /*======================================================================*/
342 /* For sizeof() which are related to the module platform and not to the
343 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
345 #define tgt_sizeof_char sizeof(char)
346 #define tgt_sizeof_short sizeof(short)
347 #define tgt_sizeof_int sizeof(int)
348 #define tgt_sizeof_long sizeof(long)
349 #define tgt_sizeof_char_p sizeof(char *)
350 #define tgt_sizeof_void_p sizeof(void *)
351 #define tgt_long long
353 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
354 #undef tgt_sizeof_long
355 #undef tgt_sizeof_char_p
356 #undef tgt_sizeof_void_p
360 tgt_sizeof_char_p = 8,
361 tgt_sizeof_void_p = 8
363 #define tgt_long long long
366 /*======================================================================*/
367 /* The structures used in Linux 2.1. */
369 /* Note: new_module_symbol does not use tgt_long intentionally */
370 struct new_module_symbol {
375 struct new_module_persist;
377 struct new_module_ref {
378 unsigned tgt_long dep; /* kernel addresses */
379 unsigned tgt_long ref;
380 unsigned tgt_long next_ref;
384 unsigned tgt_long size_of_struct; /* == sizeof(module) */
385 unsigned tgt_long next;
386 unsigned tgt_long name;
387 unsigned tgt_long size;
390 unsigned tgt_long flags; /* AUTOCLEAN et al */
395 unsigned tgt_long syms;
396 unsigned tgt_long deps;
397 unsigned tgt_long refs;
398 unsigned tgt_long init;
399 unsigned tgt_long cleanup;
400 unsigned tgt_long ex_table_start;
401 unsigned tgt_long ex_table_end;
403 unsigned tgt_long gp;
405 /* Everything after here is extension. */
406 unsigned tgt_long persist_start;
407 unsigned tgt_long persist_end;
408 unsigned tgt_long can_unload;
409 unsigned tgt_long runsize;
410 const char *kallsyms_start; /* All symbols for kernel debugging */
411 const char *kallsyms_end;
412 const char *archdata_start; /* arch specific data for module */
413 const char *archdata_end;
414 const char *kernel_data; /* Reserved for kernel internal use */
418 #define ARCHDATA_SEC_NAME ARCHDATAM
420 #define ARCHDATA_SEC_NAME "__archdata"
422 #define KALLSYMS_SEC_NAME "__kallsyms"
425 struct new_module_info {
432 /* Bits of module.flags. */
436 NEW_MOD_AUTOCLEAN = 4,
438 NEW_MOD_USED_ONCE = 16
441 int init_module(const char *name, const struct new_module *);
442 int query_module(const char *name, int which, void *buf,
443 size_t bufsize, size_t *ret);
445 /* Values for query_module's which. */
454 /*======================================================================*/
455 /* The system calls unchanged between 2.0 and 2.1. */
457 unsigned long create_module(const char *, size_t);
458 int delete_module(const char *);
461 #endif /* module.h */
463 //----------------------------------------------------------------------------
464 //--------end of modutils module.h
465 //----------------------------------------------------------------------------
469 //----------------------------------------------------------------------------
470 //--------modutils obj.h, lines 253-462
471 //----------------------------------------------------------------------------
473 /* Elf object file loading and relocation routines.
474 Copyright 1996, 1997 Linux International.
476 Contributed by Richard Henderson <rth@tamu.edu>
478 This file is part of the Linux modutils.
480 This program is free software; you can redistribute it and/or modify it
481 under the terms of the GNU General Public License as published by the
482 Free Software Foundation; either version 2 of the License, or (at your
483 option) any later version.
485 This program is distributed in the hope that it will be useful, but
486 WITHOUT ANY WARRANTY; without even the implied warranty of
487 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
488 General Public License for more details.
490 You should have received a copy of the GNU General Public License
491 along with this program; if not, write to the Free Software Foundation,
492 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
495 #ifndef MODUTILS_OBJ_H
496 /* Why? static const int MODUTILS_OBJ_H = 1; */
498 /* The relocatable object is manipulated using elfin types. */
505 # if ELFCLASSM == ELFCLASS32
506 # define ElfW(x) Elf32_ ## x
507 # define ELFW(x) ELF32_ ## x
509 # define ElfW(x) Elf64_ ## x
510 # define ELFW(x) ELF64_ ## x
514 /* For some reason this is missing from some ancient C libraries.... */
515 #ifndef ELF32_ST_INFO
516 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
519 #ifndef ELF64_ST_INFO
520 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
523 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
524 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
525 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
526 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
527 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
529 struct obj_string_patch;
530 struct obj_symbol_patch;
537 struct obj_section *load_next;
543 struct obj_symbol *next; /* hash table link */
547 int secidx; /* the defining section index/module */
549 int ksymidx; /* for export to the kernel symtab */
550 int referenced; /* actually used in the link */
553 /* Hardcode the hash table size. We shouldn't be needing so many
554 symbols that we begin to degrade performance, and we get a big win
555 by giving the compiler a constant divisor. */
557 #define HASH_BUCKETS 521
562 struct obj_section **sections;
563 struct obj_section *load_order;
564 struct obj_section **load_order_search_start;
565 struct obj_string_patch *string_patches;
566 struct obj_symbol_patch *symbol_patches;
567 int (*symbol_cmp)(const char *, const char *);
568 unsigned long (*symbol_hash)(const char *);
569 unsigned long local_symtab_size;
570 struct obj_symbol **local_symtab;
571 struct obj_symbol *symtab[HASH_BUCKETS];
581 struct obj_string_patch {
582 struct obj_string_patch *next;
584 ElfW(Addr) reloc_offset;
585 ElfW(Addr) string_offset;
588 struct obj_symbol_patch {
589 struct obj_symbol_patch *next;
591 ElfW(Addr) reloc_offset;
592 struct obj_symbol *sym;
596 /* Generic object manipulation routines. */
598 static unsigned long obj_elf_hash(const char *);
600 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
602 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
605 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
606 struct obj_symbol *sym);
608 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
609 static void obj_set_symbol_compare(struct obj_file *f,
610 int (*cmp)(const char *, const char *),
611 unsigned long (*hash)(const char *));
614 static struct obj_section *obj_find_section (struct obj_file *f,
617 static void obj_insert_section_load_order (struct obj_file *f,
618 struct obj_section *sec);
620 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
625 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
630 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
632 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
635 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
636 struct obj_symbol *sym);
638 static int obj_check_undefineds(struct obj_file *f);
640 static void obj_allocate_commons(struct obj_file *f);
642 static unsigned long obj_load_size (struct obj_file *f);
644 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
646 static struct obj_file *obj_load(FILE *f, int loadprogbits);
648 static int obj_create_image (struct obj_file *f, char *image);
650 /* Architecture specific manipulation routines. */
652 static struct obj_file *arch_new_file (void);
654 static struct obj_section *arch_new_section (void);
656 static struct obj_symbol *arch_new_symbol (void);
658 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
659 struct obj_section *targsec,
660 struct obj_section *symsec,
661 struct obj_symbol *sym,
662 ElfW(RelM) *rel, ElfW(Addr) value);
664 static void arch_create_got (struct obj_file *f);
665 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
666 static int obj_gpl_license(struct obj_file *f, const char **license);
667 #endif /* FEATURE_CHECK_TAINTED_MODULE */
669 //----------------------------------------------------------------------------
670 //--------end of modutils obj.h
671 //----------------------------------------------------------------------------
674 /* SPFX is always a string, so it can be concatenated to string constants. */
676 #define SPFX SYMBOL_PREFIX
682 #define _PATH_MODULES "/lib/modules"
683 enum { STRVERSIONLEN = 64 };
685 /*======================================================================*/
687 #define OPTION_STR "sLo:fkvqx" USE_FEATURE_INSMOD_LOAD_MAP("m")
689 OPT_s = 0x1, // -s /* log to syslog */
690 /* Not supported but kernel needs this for request_module(),
691 as this calls: modprobe -k -s -- <module>
692 so silently ignore this flag */
693 OPT_L = 0x2, // -L /* Stub warning */
694 /* Compatibility with modprobe.
695 In theory, this does locking, but we don't do
696 that. So be careful and plan your life around not
697 loading the same module 50 times concurrently. */
698 OPT_o = 0x4, // -o /* name the output module */
699 OPT_f = 0x8, // -f /* force loading */
700 OPT_k = 0x10, // -k /* module loaded by kerneld, auto-cleanable */
701 OPT_v = 0x20, // -v /* verbose output */
702 OPT_q = 0x40, // -q /* silent */
703 OPT_x = 0x80, // -x /* do not export externs */
704 OPT_m = 0x100, // -m /* print module load map */
706 #define flag_force_load (option_mask32 & OPT_f)
707 #define flag_autoclean (option_mask32 & OPT_k)
708 #define flag_verbose (option_mask32 & OPT_v)
709 #define flag_quiet (option_mask32 & OPT_q)
710 #define flag_noexport (option_mask32 & OPT_x)
711 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
712 #define flag_print_load_map (option_mask32 & OPT_m)
714 #define flag_print_load_map 0
717 /*======================================================================*/
719 #if defined(USE_LIST)
721 struct arch_list_entry
723 struct arch_list_entry *next;
724 LIST_ARCHTYPE addend;
731 #if defined(USE_SINGLE)
733 struct arch_single_entry
742 #if defined(__mips__)
745 struct mips_hi16 *next;
752 struct obj_file root;
753 #if defined(USE_PLT_ENTRIES)
754 struct obj_section *plt;
756 #if defined(USE_GOT_ENTRIES)
757 struct obj_section *got;
759 #if defined(__mips__)
760 struct mips_hi16 *mips_hi16_list;
765 struct obj_symbol root;
766 #if defined(USE_PLT_ENTRIES)
767 #if defined(USE_PLT_LIST)
768 struct arch_list_entry *pltent;
770 struct arch_single_entry pltent;
773 #if defined(USE_GOT_ENTRIES)
774 struct arch_single_entry gotent;
779 struct external_module {
784 struct new_module_symbol *syms;
787 static struct new_module_symbol *ksyms;
788 static size_t nksyms;
790 static struct external_module *ext_modules;
791 static int n_ext_modules;
792 static int n_ext_modules_used;
793 extern int delete_module(const char *);
795 static char *m_filename;
796 static char *m_fullName;
799 /*======================================================================*/
802 static int check_module_name_match(const char *filename, struct stat *statbuf,
803 void *userdata, int depth)
805 char *fullname = (char *) userdata;
807 if (fullname[0] == '\0')
810 char *tmp, *tmp1 = xstrdup(filename);
811 tmp = bb_get_last_path_component(tmp1);
812 if (strcmp(tmp, fullname) == 0) {
814 /* Stop searching if we find a match */
815 m_filename = xstrdup(filename);
824 /*======================================================================*/
826 static struct obj_file *arch_new_file(void)
829 f = xmalloc(sizeof(*f));
831 memset(f, 0, sizeof(*f));
836 static struct obj_section *arch_new_section(void)
838 return xmalloc(sizeof(struct obj_section));
841 static struct obj_symbol *arch_new_symbol(void)
843 struct arch_symbol *sym;
844 sym = xmalloc(sizeof(*sym));
846 memset(sym, 0, sizeof(*sym));
851 static enum obj_reloc
852 arch_apply_relocation(struct obj_file *f,
853 struct obj_section *targsec,
854 struct obj_section *symsec,
855 struct obj_symbol *sym,
856 ElfW(RelM) *rel, ElfW(Addr) v)
858 struct arch_file *ifile = (struct arch_file *) f;
859 enum obj_reloc ret = obj_reloc_ok;
860 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
861 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
862 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
863 struct arch_symbol *isym = (struct arch_symbol *) sym;
865 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
866 #if defined(USE_GOT_ENTRIES)
867 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
870 #if defined(USE_PLT_ENTRIES)
871 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
873 # if defined(USE_PLT_LIST)
874 struct arch_list_entry *pe;
876 struct arch_single_entry *pe;
880 switch (ELF_R_TYPE(rel->r_info)) {
895 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
896 * (which is .got) similar to branch,
897 * but is full 32 bits relative */
906 case R_ARM_GOTOFF: /* address relative to the got */
910 #elif defined(__cris__)
916 /* CRIS keeps the relocation value in the r_addend field and
917 * should not use whats in *loc at all
922 #elif defined(__H8300H__) || defined(__H8300S__)
925 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
926 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
937 if ((ElfW(Sword))v > 0x7fff ||
938 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
939 ret = obj_reloc_overflow;
941 *(unsigned short *)loc = v;
945 if ((ElfW(Sword))v > 0x7f ||
946 (ElfW(Sword))v < -(ElfW(Sword))0x80)
947 ret = obj_reloc_overflow;
949 *(unsigned char *)loc = v;
952 #elif defined(__i386__)
986 #elif defined (__microblaze__)
987 case R_MICROBLAZE_NONE:
988 case R_MICROBLAZE_64_NONE:
989 case R_MICROBLAZE_32_SYM_OP_SYM:
990 case R_MICROBLAZE_32_PCREL:
993 case R_MICROBLAZE_64_PCREL: {
994 /* dot is the address of the current instruction.
995 * v is the target symbol address.
996 * So we need to extract the offset in the code,
997 * adding v, then subtrating the current address
998 * of this instruction.
999 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
1002 /* Get split offset stored in code */
1003 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
1006 /* Adjust relative offset. -4 adjustment required
1007 * because dot points to the IMM insn, but branch
1008 * is computed relative to the branch instruction itself.
1010 temp += v - dot - 4;
1012 /* Store back into code */
1013 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
1014 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
1019 case R_MICROBLAZE_32:
1023 case R_MICROBLAZE_64: {
1024 /* Get split pointer stored in code */
1025 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
1028 /* Add reloc offset */
1031 /* Store back into code */
1032 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
1033 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
1038 case R_MICROBLAZE_32_PCREL_LO:
1039 case R_MICROBLAZE_32_LO:
1040 case R_MICROBLAZE_SRO32:
1041 case R_MICROBLAZE_SRW32:
1042 ret = obj_reloc_unhandled;
1045 #elif defined(__mc68000__)
1056 ret = obj_reloc_overflow;
1063 ret = obj_reloc_overflow;
1070 if ((ElfW(Sword))v > 0x7f ||
1071 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1072 ret = obj_reloc_overflow;
1079 if ((ElfW(Sword))v > 0x7fff ||
1080 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1081 ret = obj_reloc_overflow;
1087 *(int *)loc = v - dot;
1090 case R_68K_GLOB_DAT:
1091 case R_68K_JMP_SLOT:
1095 case R_68K_RELATIVE:
1096 *(int *)loc += f->baseaddr;
1102 # ifdef R_68K_GOTOFF
1108 #elif defined(__mips__)
1119 ret = obj_reloc_dangerous;
1120 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1121 ret = obj_reloc_overflow;
1123 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1129 struct mips_hi16 *n;
1131 /* We cannot relocate this one now because we don't know the value
1132 of the carry we need to add. Save the information, and let LO16
1133 do the actual relocation. */
1134 n = xmalloc(sizeof *n);
1137 n->next = ifile->mips_hi16_list;
1138 ifile->mips_hi16_list = n;
1144 unsigned long insnlo = *loc;
1145 ElfW(Addr) val, vallo;
1147 /* Sign extend the addend we extract from the lo insn. */
1148 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1150 if (ifile->mips_hi16_list != NULL) {
1151 struct mips_hi16 *l;
1153 l = ifile->mips_hi16_list;
1155 struct mips_hi16 *next;
1158 /* Do the HI16 relocation. Note that we actually don't
1159 need to know anything about the LO16 itself, except where
1160 to find the low 16 bits of the addend needed by the LO16. */
1163 ((insn & 0xffff) << 16) +
1167 /* Account for the sign extension that will happen in the
1174 insn = (insn & ~0xffff) | val;
1182 ifile->mips_hi16_list = NULL;
1185 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1187 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1192 #elif defined(__nios2__)
1197 case R_NIOS2_BFD_RELOC_32:
1201 case R_NIOS2_BFD_RELOC_16:
1203 ret = obj_reloc_overflow;
1208 case R_NIOS2_BFD_RELOC_8:
1210 ret = obj_reloc_overflow;
1219 if ((Elf32_Sword)v > 0x7fff ||
1220 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1221 ret = obj_reloc_overflow;
1225 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1235 ret = obj_reloc_overflow;
1239 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1244 case R_NIOS2_PCREL16:
1249 if ((Elf32_Sword)v > 0x7fff ||
1250 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1251 ret = obj_reloc_overflow;
1255 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1261 Elf32_Addr word, gp;
1263 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1265 if ((Elf32_Sword)v > 0x7fff ||
1266 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1267 ret = obj_reloc_overflow;
1271 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1275 case R_NIOS2_CALL26:
1277 ret = obj_reloc_dangerous;
1278 if ((v >> 28) != (dot >> 28))
1279 ret = obj_reloc_overflow;
1280 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1288 ret = obj_reloc_overflow;
1291 word = *loc & ~0x7c0;
1292 *loc = word | ((v & 0x1f) << 6);
1301 ret = obj_reloc_overflow;
1304 word = *loc & ~0xfc0;
1305 *loc = word | ((v & 0x3f) << 6);
1314 ret = obj_reloc_overflow;
1317 word = *loc & ~0x3fc0;
1318 *loc = word | ((v & 0xff) << 6);
1327 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1337 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1342 case R_NIOS2_HIADJ16:
1344 Elf32_Addr word1, word2;
1347 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1348 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1353 #elif defined(__powerpc64__)
1354 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1356 #elif defined(__powerpc__)
1358 case R_PPC_ADDR16_HA:
1359 *(unsigned short *)loc = (v + 0x8000) >> 16;
1362 case R_PPC_ADDR16_HI:
1363 *(unsigned short *)loc = v >> 16;
1366 case R_PPC_ADDR16_LO:
1367 *(unsigned short *)loc = v;
1381 #elif defined(__s390__)
1384 *(unsigned int *) loc += v;
1387 *(unsigned short *) loc += v;
1390 *(unsigned char *) loc += v;
1394 *(unsigned int *) loc += v - dot;
1397 *(unsigned short *) loc += (v - dot) >> 1;
1400 *(unsigned short *) loc += v - dot;
1404 case R_390_PLT16DBL:
1405 /* find the plt entry and initialize it. */
1406 pe = (struct arch_single_entry *) &isym->pltent;
1407 if (pe->inited == 0) {
1408 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1409 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1411 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1418 /* Insert relative distance to target. */
1419 v = plt + pe->offset - dot;
1420 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1421 *(unsigned int *) loc = (unsigned int) v;
1422 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1423 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1426 case R_390_GLOB_DAT:
1427 case R_390_JMP_SLOT:
1431 case R_390_RELATIVE:
1432 *loc += f->baseaddr;
1436 *(unsigned long *) loc += got - dot;
1442 if (!isym->gotent.inited)
1444 isym->gotent.inited = 1;
1445 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1447 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1448 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1449 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1450 *(unsigned short *) loc += isym->gotent.offset;
1451 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1452 *(unsigned int *) loc += isym->gotent.offset;
1455 # ifndef R_390_GOTOFF32
1456 # define R_390_GOTOFF32 R_390_GOTOFF
1458 case R_390_GOTOFF32:
1462 #elif defined(__sh__)
1485 *loc = f->baseaddr + rel->r_addend;
1489 *loc = got - dot + rel->r_addend;
1499 # if defined(__SH5__)
1500 case R_SH_IMM_MEDLOW16:
1501 case R_SH_IMM_LOW16:
1505 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1509 * movi and shori have the format:
1511 * | op | imm | reg | reserved |
1512 * 31..26 25..10 9.. 4 3 .. 0
1514 * so we simply mask and or in imm.
1516 word = *loc & ~0x3fffc00;
1517 word |= (v & 0xffff) << 10;
1524 case R_SH_IMM_MEDLOW16_PCREL:
1525 case R_SH_IMM_LOW16_PCREL:
1529 word = *loc & ~0x3fffc00;
1533 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1536 word |= (v & 0xffff) << 10;
1542 # endif /* __SH5__ */
1544 #elif defined (__v850e__)
1550 /* We write two shorts instead of a long because even
1551 32-bit insns only need half-word alignment, but
1552 32-bit data needs to be long-word aligned. */
1553 v += ((unsigned short *)loc)[0];
1554 v += ((unsigned short *)loc)[1] << 16;
1555 ((unsigned short *)loc)[0] = v & 0xffff;
1556 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1559 case R_V850_22_PCREL:
1562 #elif defined(__x86_64__)
1572 *(unsigned int *) loc += v;
1575 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1576 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1581 *(signed int *) loc += v;
1585 *(unsigned short *) loc += v;
1589 *(unsigned char *) loc += v;
1593 *(unsigned int *) loc += v - dot;
1597 *(unsigned short *) loc += v - dot;
1601 *(unsigned char *) loc += v - dot;
1604 case R_X86_64_GLOB_DAT:
1605 case R_X86_64_JUMP_SLOT:
1609 case R_X86_64_RELATIVE:
1610 *loc += f->baseaddr;
1613 case R_X86_64_GOT32:
1614 case R_X86_64_GOTPCREL:
1617 if (!isym->gotent.reloc_done)
1619 isym->gotent.reloc_done = 1;
1620 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1622 /* XXX are these really correct? */
1623 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1624 *(unsigned int *) loc += v + isym->gotent.offset;
1626 *loc += isym->gotent.offset;
1631 # warning "no idea how to handle relocations on your arch"
1635 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1636 ret = obj_reloc_unhandled;
1639 #if defined(USE_PLT_ENTRIES)
1643 /* find the plt entry and initialize it if necessary */
1645 #if defined(USE_PLT_LIST)
1646 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1653 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1655 /* generate some machine code */
1657 #if defined(__arm__)
1658 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1659 ip[1] = v; /* sym@ */
1661 #if defined(__powerpc__)
1662 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1663 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1664 ip[2] = 0x7d6903a6; /* mtctr r11 */
1665 ip[3] = 0x4e800420; /* bctr */
1667 #if defined (__v850e__)
1668 /* We have to trash a register, so we assume that any control
1669 transfer more than 21-bits away must be a function call
1670 (so we can use a call-clobbered register). */
1671 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1672 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1677 /* relative distance to target */
1679 /* if the target is too far away.... */
1680 #if defined (__arm__) || defined (__powerpc__)
1681 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1682 #elif defined (__v850e__)
1683 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1685 /* go via the plt */
1686 v = plt + pe->offset - dot;
1688 #if defined (__v850e__)
1693 ret = obj_reloc_dangerous;
1695 /* merge the offset into the instruction. */
1696 #if defined(__arm__)
1697 /* Convert to words. */
1700 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1702 #if defined(__powerpc__)
1703 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1705 #if defined (__v850e__)
1706 /* We write two shorts instead of a long because even 32-bit insns
1707 only need half-word alignment, but the 32-bit data write needs
1708 to be long-word aligned. */
1709 ((unsigned short *)loc)[0] =
1710 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1711 | ((v >> 16) & 0x3f); /* offs high part */
1712 ((unsigned short *)loc)[1] =
1713 (v & 0xffff); /* offs low part */
1716 #endif /* USE_PLT_ENTRIES */
1718 #if defined(USE_GOT_ENTRIES)
1721 /* needs an entry in the .got: set it, once */
1722 if (!isym->gotent.inited) {
1723 isym->gotent.inited = 1;
1724 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1726 /* make the reloc with_respect_to_.got */
1728 *loc += isym->gotent.offset + rel->r_addend;
1729 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1730 *loc += isym->gotent.offset;
1734 #endif /* USE_GOT_ENTRIES */
1741 #if defined(USE_LIST)
1743 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1744 int offset, int size)
1746 struct arch_list_entry *pe;
1748 for (pe = *list; pe != NULL; pe = pe->next) {
1749 if (pe->addend == rel->r_addend) {
1755 pe = xmalloc(sizeof(struct arch_list_entry));
1757 pe->addend = rel->r_addend;
1758 pe->offset = offset;
1768 #if defined(USE_SINGLE)
1770 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1771 int offset, int size)
1773 if (single->allocated == 0) {
1774 single->allocated = 1;
1775 single->offset = offset;
1784 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1786 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1787 int offset, int size)
1789 struct obj_section *myrelsec = obj_find_section(f, name);
1796 obj_extend_section(myrelsec, offset);
1798 myrelsec = obj_create_alloced_section(f, name,
1807 static void arch_create_got(struct obj_file *f)
1809 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1810 struct arch_file *ifile = (struct arch_file *) f;
1812 #if defined(USE_GOT_ENTRIES)
1813 int got_offset = 0, got_needed = 0, got_allocate;
1815 #if defined(USE_PLT_ENTRIES)
1816 int plt_offset = 0, plt_needed = 0, plt_allocate;
1818 struct obj_section *relsec, *symsec, *strsec;
1819 ElfW(RelM) *rel, *relend;
1820 ElfW(Sym) *symtab, *extsym;
1821 const char *strtab, *name;
1822 struct arch_symbol *intsym;
1824 for (i = 0; i < f->header.e_shnum; ++i) {
1825 relsec = f->sections[i];
1826 if (relsec->header.sh_type != SHT_RELM)
1829 symsec = f->sections[relsec->header.sh_link];
1830 strsec = f->sections[symsec->header.sh_link];
1832 rel = (ElfW(RelM) *) relsec->contents;
1833 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1834 symtab = (ElfW(Sym) *) symsec->contents;
1835 strtab = (const char *) strsec->contents;
1837 for (; rel < relend; ++rel) {
1838 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1840 #if defined(USE_GOT_ENTRIES)
1843 #if defined(USE_PLT_ENTRIES)
1847 switch (ELF_R_TYPE(rel->r_info)) {
1848 #if defined(__arm__)
1863 #elif defined(__i386__)
1873 #elif defined(__powerpc__)
1878 #elif defined(__mc68000__)
1889 #elif defined(__sh__)
1899 #elif defined (__v850e__)
1900 case R_V850_22_PCREL:
1909 if (extsym->st_name != 0) {
1910 name = strtab + extsym->st_name;
1912 name = f->sections[extsym->st_shndx]->name;
1914 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1915 #if defined(USE_GOT_ENTRIES)
1917 got_offset += arch_single_init(
1918 rel, &intsym->gotent,
1919 got_offset, GOT_ENTRY_SIZE);
1924 #if defined(USE_PLT_ENTRIES)
1926 #if defined(USE_PLT_LIST)
1927 plt_offset += arch_list_add(
1928 rel, &intsym->pltent,
1929 plt_offset, PLT_ENTRY_SIZE);
1931 plt_offset += arch_single_init(
1932 rel, &intsym->pltent,
1933 plt_offset, PLT_ENTRY_SIZE);
1941 #if defined(USE_GOT_ENTRIES)
1943 ifile->got = arch_xsect_init(f, ".got", got_offset,
1948 #if defined(USE_PLT_ENTRIES)
1950 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1955 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1958 /*======================================================================*/
1960 /* Standard ELF hash function. */
1961 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1963 unsigned long h = 0;
1970 if ((g = (h & 0xf0000000)) != 0) {
1979 static unsigned long obj_elf_hash(const char *name)
1981 return obj_elf_hash_n(name, strlen(name));
1984 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1985 /* String comparison for non-co-versioned kernel and module. */
1987 static int ncv_strcmp(const char *a, const char *b)
1989 size_t alen = strlen(a), blen = strlen(b);
1991 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1992 return strncmp(a, b, alen);
1993 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1994 return strncmp(a, b, blen);
1996 return strcmp(a, b);
1999 /* String hashing for non-co-versioned kernel and module. Here
2000 we are simply forced to drop the crc from the hash. */
2002 static unsigned long ncv_symbol_hash(const char *str)
2004 size_t len = strlen(str);
2005 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
2007 return obj_elf_hash_n(str, len);
2011 obj_set_symbol_compare(struct obj_file *f,
2012 int (*cmp) (const char *, const char *),
2013 unsigned long (*hash) (const char *))
2016 f->symbol_cmp = cmp;
2018 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
2021 f->symbol_hash = hash;
2023 memcpy(tmptab, f->symtab, sizeof(tmptab));
2024 memset(f->symtab, 0, sizeof(f->symtab));
2026 for (i = 0; i < HASH_BUCKETS; ++i)
2027 for (sym = tmptab[i]; sym; sym = next) {
2028 unsigned long h = hash(sym->name) % HASH_BUCKETS;
2030 sym->next = f->symtab[h];
2036 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2038 static struct obj_symbol *
2039 obj_add_symbol(struct obj_file *f, const char *name,
2040 unsigned long symidx, int info,
2041 int secidx, ElfW(Addr) value,
2044 struct obj_symbol *sym;
2045 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2046 int n_type = ELF_ST_TYPE(info);
2047 int n_binding = ELF_ST_BIND(info);
2049 for (sym = f->symtab[hash]; sym; sym = sym->next)
2050 if (f->symbol_cmp(sym->name, name) == 0) {
2051 int o_secidx = sym->secidx;
2052 int o_info = sym->info;
2053 int o_type = ELF_ST_TYPE(o_info);
2054 int o_binding = ELF_ST_BIND(o_info);
2056 /* A redefinition! Is it legal? */
2058 if (secidx == SHN_UNDEF)
2060 else if (o_secidx == SHN_UNDEF)
2062 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2063 /* Cope with local and global symbols of the same name
2064 in the same object file, as might have been created
2065 by ld -r. The only reason locals are now seen at this
2066 level at all is so that we can do semi-sensible things
2069 struct obj_symbol *nsym, **p;
2071 nsym = arch_new_symbol();
2072 nsym->next = sym->next;
2075 /* Excise the old (local) symbol from the hash chain. */
2076 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2080 } else if (n_binding == STB_LOCAL) {
2081 /* Another symbol of the same name has already been defined.
2082 Just add this to the local table. */
2083 sym = arch_new_symbol();
2086 f->local_symtab[symidx] = sym;
2088 } else if (n_binding == STB_WEAK)
2090 else if (o_binding == STB_WEAK)
2092 /* Don't unify COMMON symbols with object types the programmer
2094 else if (secidx == SHN_COMMON
2095 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2097 else if (o_secidx == SHN_COMMON
2098 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2101 /* Don't report an error if the symbol is coming from
2102 the kernel or some external module. */
2103 if (secidx <= SHN_HIRESERVE)
2104 bb_error_msg("%s multiply defined", name);
2109 /* Completely new symbol. */
2110 sym = arch_new_symbol();
2111 sym->next = f->symtab[hash];
2112 f->symtab[hash] = sym;
2115 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2116 if (symidx >= f->local_symtab_size)
2117 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2118 name, (long) symidx, (long) f->local_symtab_size);
2120 f->local_symtab[symidx] = sym;
2127 sym->secidx = secidx;
2133 static struct obj_symbol *
2134 obj_find_symbol(struct obj_file *f, const char *name)
2136 struct obj_symbol *sym;
2137 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2139 for (sym = f->symtab[hash]; sym; sym = sym->next)
2140 if (f->symbol_cmp(sym->name, name) == 0)
2146 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2149 if (sym->secidx >= SHN_LORESERVE)
2152 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2154 /* As a special case, a NULL sym has value zero. */
2159 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2161 int i, n = f->header.e_shnum;
2163 for (i = 0; i < n; ++i)
2164 if (strcmp(f->sections[i]->name, name) == 0)
2165 return f->sections[i];
2170 static int obj_load_order_prio(struct obj_section *a)
2172 unsigned long af, ac;
2174 af = a->header.sh_flags;
2177 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2178 strcmp(a->name + 5, ".init"))
2182 if (!(af & SHF_WRITE))
2184 if (af & SHF_EXECINSTR)
2186 if (a->header.sh_type != SHT_NOBITS)
2193 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2195 struct obj_section **p;
2196 int prio = obj_load_order_prio(sec);
2197 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2198 if (obj_load_order_prio(*p) < prio)
2200 sec->load_next = *p;
2204 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2206 unsigned long align,
2209 int newidx = f->header.e_shnum++;
2210 struct obj_section *sec;
2212 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2213 f->sections[newidx] = sec = arch_new_section();
2215 memset(sec, 0, sizeof(*sec));
2216 sec->header.sh_type = SHT_PROGBITS;
2217 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2218 sec->header.sh_size = size;
2219 sec->header.sh_addralign = align;
2223 sec->contents = xmalloc(size);
2225 obj_insert_section_load_order(f, sec);
2230 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2232 unsigned long align,
2235 int newidx = f->header.e_shnum++;
2236 struct obj_section *sec;
2238 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2239 f->sections[newidx] = sec = arch_new_section();
2241 memset(sec, 0, sizeof(*sec));
2242 sec->header.sh_type = SHT_PROGBITS;
2243 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2244 sec->header.sh_size = size;
2245 sec->header.sh_addralign = align;
2249 sec->contents = xmalloc(size);
2251 sec->load_next = f->load_order;
2252 f->load_order = sec;
2253 if (f->load_order_search_start == &f->load_order)
2254 f->load_order_search_start = &sec->load_next;
2259 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2261 unsigned long oldsize = sec->header.sh_size;
2263 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2265 return sec->contents + oldsize;
2269 /* Conditionally add the symbols from the given symbol set to the
2273 add_symbols_from( struct obj_file *f,
2274 int idx, struct new_module_symbol *syms, size_t nsyms)
2276 struct new_module_symbol *s;
2279 #ifdef SYMBOL_PREFIX
2281 size_t name_alloced_size = 0;
2283 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2286 gpl = obj_gpl_license(f, NULL) == 0;
2288 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2289 /* Only add symbols that are already marked external.
2290 If we override locals we may cause problems for
2291 argument initialization. We will also create a false
2292 dependency on the module. */
2293 struct obj_symbol *sym;
2296 /* GPL licensed modules can use symbols exported with
2297 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2298 * exported names. Non-GPL modules never see any GPLONLY_
2299 * symbols so they cannot fudge it by adding the prefix on
2302 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2303 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2310 name = (char *)s->name;
2312 #ifdef SYMBOL_PREFIX
2313 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2314 kernel exports `C names', but module object files
2315 reference `linker names'). */
2316 size_t extra = sizeof SYMBOL_PREFIX;
2317 size_t name_size = strlen (name) + extra;
2318 if (name_size > name_alloced_size) {
2319 name_alloced_size = name_size * 2;
2320 name_buf = alloca (name_alloced_size);
2322 strcpy (name_buf, SYMBOL_PREFIX);
2323 strcpy (name_buf + extra - 1, name);
2325 #endif /* SYMBOL_PREFIX */
2327 sym = obj_find_symbol(f, name);
2328 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2329 #ifdef SYMBOL_PREFIX
2330 /* Put NAME_BUF into more permanent storage. */
2331 name = xmalloc (name_size);
2332 strcpy (name, name_buf);
2334 sym = obj_add_symbol(f, name, -1,
2335 ELF_ST_INFO(STB_GLOBAL,
2338 /* Did our symbol just get installed? If so, mark the
2339 module as "used". */
2340 if (sym->secidx == idx)
2348 static void add_kernel_symbols(struct obj_file *f)
2350 struct external_module *m;
2353 /* Add module symbols first. */
2355 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2357 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2358 m->nsyms)) m->used = 1, ++nused;
2360 n_ext_modules_used = nused;
2362 /* And finally the symbols from the kernel proper. */
2365 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2368 static char *get_modinfo_value(struct obj_file *f, const char *key)
2370 struct obj_section *sec;
2371 char *p, *v, *n, *ep;
2372 size_t klen = strlen(key);
2374 sec = obj_find_section(f, ".modinfo");
2378 ep = p + sec->header.sh_size;
2381 n = strchr(p, '\0');
2383 if (p + klen == v && strncmp(p, key, klen) == 0)
2386 if (p + klen == n && strcmp(p, key) == 0)
2396 /*======================================================================*/
2397 /* Functions relating to module loading after 2.1.18. */
2400 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2403 char *p, *q, *key, *sym_name;
2404 struct obj_symbol *sym;
2405 char *contents, *loc;
2409 if ((q = strchr(p, '=')) == NULL) {
2414 key = alloca(q - p + 6);
2415 memcpy(key, "parm_", 5);
2416 memcpy(key + 5, p, q - p);
2419 p = get_modinfo_value(f, key);
2422 bb_error_msg("invalid parameter %s", key);
2426 #ifdef SYMBOL_PREFIX
2427 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2428 strcpy (sym_name, SYMBOL_PREFIX);
2429 strcat (sym_name, key);
2433 sym = obj_find_symbol(f, sym_name);
2435 /* Also check that the parameter was not resolved from the kernel. */
2436 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2437 bb_error_msg("symbol for parameter %s not found", key);
2442 min = strtoul(p, &p, 10);
2444 max = strtoul(p + 1, &p, 10);
2450 contents = f->sections[sym->secidx]->contents;
2451 loc = contents + sym->value;
2455 if ((*p == 's') || (*p == 'c')) {
2458 /* Do C quoting if we begin with a ", else slurp the lot. */
2462 str = alloca(strlen(q));
2463 for (r = str, q++; *q != '"'; ++q, ++r) {
2465 bb_error_msg("improperly terminated string argument for %s",
2468 } else if (*q == '\\')
2502 if (q[1] >= '0' && q[1] <= '7') {
2503 c = (c * 8) + *++q - '0';
2504 if (q[1] >= '0' && q[1] <= '7')
2505 c = (c * 8) + *++q - '0';
2522 /* In this case, the string is not quoted. We will break
2523 it using the coma (like for ints). If the user wants to
2524 include comas in a string, he just has to quote it */
2526 /* Search the next coma */
2530 if (r != (char *) NULL) {
2531 /* Recopy the current field */
2532 str = alloca(r - q + 1);
2533 memcpy(str, q, r - q);
2535 /* I don't know if it is useful, as the previous case
2536 doesn't nul terminate the string ??? */
2539 /* Keep next fields */
2550 obj_string_patch(f, sym->secidx, loc - contents, str);
2551 loc += tgt_sizeof_char_p;
2553 /* Array of chars (in fact, matrix !) */
2554 unsigned long charssize; /* size of each member */
2556 /* Get the size of each member */
2557 /* Probably we should do that outside the loop ? */
2558 if (!isdigit(*(p + 1))) {
2559 bb_error_msg("parameter type 'c' for %s must be followed by"
2560 " the maximum size", key);
2563 charssize = strtoul(p + 1, (char **) NULL, 10);
2566 if (strlen(str) >= charssize) {
2567 bb_error_msg("string too long for %s (max %ld)", key,
2572 /* Copy to location */
2573 strcpy((char *) loc, str);
2577 long v = strtoul(q, &q, 0);
2584 loc += tgt_sizeof_short;
2588 loc += tgt_sizeof_int;
2592 loc += tgt_sizeof_long;
2596 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2611 goto retry_end_of_value;
2615 bb_error_msg("too many values for %s (max %d)", key, max);
2622 bb_error_msg("invalid argument syntax for %s", key);
2629 bb_error_msg("too few values for %s (min %d)", key, min);
2639 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2640 static int new_is_module_checksummed(struct obj_file *f)
2642 const char *p = get_modinfo_value(f, "using_checksums");
2649 /* Get the module's kernel version in the canonical integer form. */
2652 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2657 p = get_modinfo_value(f, "kernel_version");
2660 safe_strncpy(str, p, STRVERSIONLEN);
2662 a = strtoul(p, &p, 10);
2665 b = strtoul(p + 1, &p, 10);
2668 c = strtoul(p + 1, &q, 10);
2672 return a << 16 | b << 8 | c;
2675 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2678 /* Fetch the loaded modules, and all currently exported symbols. */
2680 static int new_get_kernel_symbols(void)
2682 char *module_names, *mn;
2683 struct external_module *modules, *m;
2684 struct new_module_symbol *syms, *s;
2685 size_t ret, bufsize, nmod, nsyms, i, j;
2687 /* Collect the loaded modules. */
2689 module_names = xmalloc(bufsize = 256);
2691 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2692 if (errno == ENOSPC && bufsize < ret) {
2693 module_names = xrealloc(module_names, bufsize = ret);
2694 goto retry_modules_load;
2696 bb_perror_msg("QM_MODULES");
2700 n_ext_modules = nmod = ret;
2702 /* Collect the modules' symbols. */
2705 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2706 memset(modules, 0, nmod * sizeof(*modules));
2707 for (i = 0, mn = module_names, m = modules;
2708 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2709 struct new_module_info info;
2711 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2712 if (errno == ENOENT) {
2713 /* The module was removed out from underneath us. */
2716 bb_perror_msg("query_module: QM_INFO: %s", mn);
2720 syms = xmalloc(bufsize = 1024);
2722 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2725 syms = xrealloc(syms, bufsize = ret);
2726 goto retry_mod_sym_load;
2728 /* The module was removed out from underneath us. */
2731 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2738 m->addr = info.addr;
2742 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2743 s->name += (unsigned long) syms;
2748 /* Collect the kernel's symbols. */
2750 syms = xmalloc(bufsize = 16 * 1024);
2751 retry_kern_sym_load:
2752 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2753 if (errno == ENOSPC && bufsize < ret) {
2754 syms = xrealloc(syms, bufsize = ret);
2755 goto retry_kern_sym_load;
2757 bb_perror_msg("kernel: QM_SYMBOLS");
2760 nksyms = nsyms = ret;
2763 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2764 s->name += (unsigned long) syms;
2770 /* Return the kernel symbol checksum version, or zero if not used. */
2772 static int new_is_kernel_checksummed(void)
2774 struct new_module_symbol *s;
2777 /* Using_Versions is not the first symbol, but it should be in there. */
2779 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2780 if (strcmp((char *) s->name, "Using_Versions") == 0)
2787 static int new_create_this_module(struct obj_file *f, const char *m_name)
2789 struct obj_section *sec;
2791 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2792 sizeof(struct new_module));
2793 memset(sec->contents, 0, sizeof(struct new_module));
2795 obj_add_symbol(f, SPFX "__this_module", -1,
2796 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2797 sizeof(struct new_module));
2799 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2805 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2806 /* add an entry to the __ksymtab section, creating it if necessary */
2807 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2809 struct obj_section *sec;
2812 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2813 * If __ksymtab is defined but not marked alloc, x out the first character
2814 * (no obj_delete routine) and create a new __ksymtab with the correct
2817 sec = obj_find_section(f, "__ksymtab");
2818 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2819 *((char *)(sec->name)) = 'x'; /* override const */
2823 sec = obj_create_alloced_section(f, "__ksymtab",
2824 tgt_sizeof_void_p, 0);
2827 sec->header.sh_flags |= SHF_ALLOC;
2828 /* Empty section might be byte-aligned */
2829 sec->header.sh_addralign = tgt_sizeof_void_p;
2830 ofs = sec->header.sh_size;
2831 obj_symbol_patch(f, sec->idx, ofs, sym);
2832 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2833 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2835 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2837 static int new_create_module_ksymtab(struct obj_file *f)
2839 struct obj_section *sec;
2842 /* We must always add the module references. */
2844 if (n_ext_modules_used) {
2845 struct new_module_ref *dep;
2846 struct obj_symbol *tm;
2848 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2849 (sizeof(struct new_module_ref)
2850 * n_ext_modules_used));
2854 tm = obj_find_symbol(f, SPFX "__this_module");
2855 dep = (struct new_module_ref *) sec->contents;
2856 for (i = 0; i < n_ext_modules; ++i)
2857 if (ext_modules[i].used) {
2858 dep->dep = ext_modules[i].addr;
2859 obj_symbol_patch(f, sec->idx,
2860 (char *) &dep->ref - sec->contents, tm);
2866 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2870 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2872 /* We don't want to export symbols residing in sections that
2873 aren't loaded. There are a number of these created so that
2874 we make sure certain module options don't appear twice. */
2876 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2878 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2880 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2881 struct obj_symbol *sym;
2882 for (sym = f->symtab[i]; sym; sym = sym->next)
2883 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2884 && sym->secidx <= SHN_HIRESERVE
2885 && (sym->secidx >= SHN_LORESERVE
2886 || loaded[sym->secidx])) {
2887 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2889 obj_symbol_patch(f, sec->idx, ofs, sym);
2890 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2897 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2905 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2907 struct new_module *module;
2908 struct obj_section *sec;
2913 sec = obj_find_section(f, ".this");
2914 if (!sec || !sec->contents) {
2915 bb_perror_msg_and_die("corrupt module %s?",m_name);
2917 module = (struct new_module *) sec->contents;
2918 m_addr = sec->header.sh_addr;
2920 module->size_of_struct = sizeof(*module);
2921 module->size = m_size;
2922 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2924 sec = obj_find_section(f, "__ksymtab");
2925 if (sec && sec->header.sh_size) {
2926 module->syms = sec->header.sh_addr;
2927 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2930 if (n_ext_modules_used) {
2931 sec = obj_find_section(f, ".kmodtab");
2932 module->deps = sec->header.sh_addr;
2933 module->ndeps = n_ext_modules_used;
2937 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2939 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2941 sec = obj_find_section(f, "__ex_table");
2943 module->ex_table_start = sec->header.sh_addr;
2944 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2947 sec = obj_find_section(f, ".text.init");
2949 module->runsize = sec->header.sh_addr - m_addr;
2951 sec = obj_find_section(f, ".data.init");
2953 if (!module->runsize ||
2954 module->runsize > sec->header.sh_addr - m_addr)
2955 module->runsize = sec->header.sh_addr - m_addr;
2957 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2958 if (sec && sec->header.sh_size) {
2959 module->archdata_start = (void*)sec->header.sh_addr;
2960 module->archdata_end = module->archdata_start + sec->header.sh_size;
2962 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2963 if (sec && sec->header.sh_size) {
2964 module->kallsyms_start = (void*)sec->header.sh_addr;
2965 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2968 /* Whew! All of the initialization is complete. Collect the final
2969 module image and give it to the kernel. */
2971 image = xmalloc(m_size);
2972 obj_create_image(f, image);
2974 ret = init_module(m_name, (struct new_module *) image);
2976 bb_perror_msg("init_module: %s", m_name);
2984 /*======================================================================*/
2987 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2990 struct obj_string_patch *p;
2991 struct obj_section *strsec;
2992 size_t len = strlen(string) + 1;
2995 p = xmalloc(sizeof(*p));
2996 p->next = f->string_patches;
2997 p->reloc_secidx = secidx;
2998 p->reloc_offset = offset;
2999 f->string_patches = p;
3001 strsec = obj_find_section(f, ".kstrtab");
3002 if (strsec == NULL) {
3003 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3004 p->string_offset = 0;
3005 loc = strsec->contents;
3007 p->string_offset = strsec->header.sh_size;
3008 loc = obj_extend_section(strsec, len);
3010 memcpy(loc, string, len);
3016 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3017 struct obj_symbol *sym)
3019 struct obj_symbol_patch *p;
3021 p = xmalloc(sizeof(*p));
3022 p->next = f->symbol_patches;
3023 p->reloc_secidx = secidx;
3024 p->reloc_offset = offset;
3026 f->symbol_patches = p;
3031 static int obj_check_undefineds(struct obj_file *f)
3036 for (i = 0; i < HASH_BUCKETS; ++i) {
3037 struct obj_symbol *sym;
3038 for (sym = f->symtab[i]; sym; sym = sym->next)
3039 if (sym->secidx == SHN_UNDEF) {
3040 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3041 sym->secidx = SHN_ABS;
3045 bb_error_msg("unresolved symbol %s", sym->name);
3055 static void obj_allocate_commons(struct obj_file *f)
3057 struct common_entry {
3058 struct common_entry *next;
3059 struct obj_symbol *sym;
3060 } *common_head = NULL;
3064 for (i = 0; i < HASH_BUCKETS; ++i) {
3065 struct obj_symbol *sym;
3066 for (sym = f->symtab[i]; sym; sym = sym->next)
3067 if (sym->secidx == SHN_COMMON) {
3068 /* Collect all COMMON symbols and sort them by size so as to
3069 minimize space wasted by alignment requirements. */
3071 struct common_entry **p, *n;
3072 for (p = &common_head; *p; p = &(*p)->next)
3073 if (sym->size <= (*p)->sym->size)
3076 n = alloca(sizeof(*n));
3084 for (i = 1; i < f->local_symtab_size; ++i) {
3085 struct obj_symbol *sym = f->local_symtab[i];
3086 if (sym && sym->secidx == SHN_COMMON) {
3087 struct common_entry **p, *n;
3088 for (p = &common_head; *p; p = &(*p)->next)
3089 if (sym == (*p)->sym)
3091 else if (sym->size < (*p)->sym->size) {
3092 n = alloca(sizeof(*n));
3102 /* Find the bss section. */
3103 for (i = 0; i < f->header.e_shnum; ++i)
3104 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3107 /* If for some reason there hadn't been one, create one. */
3108 if (i == f->header.e_shnum) {
3109 struct obj_section *sec;
3111 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3112 f->sections[i] = sec = arch_new_section();
3113 f->header.e_shnum = i + 1;
3115 memset(sec, 0, sizeof(*sec));
3116 sec->header.sh_type = SHT_PROGBITS;
3117 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3122 /* Allocate the COMMONS. */
3124 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3125 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3126 struct common_entry *c;
3128 for (c = common_head; c; c = c->next) {
3129 ElfW(Addr) align = c->sym->value;
3131 if (align > max_align)
3133 if (bss_size & (align - 1))
3134 bss_size = (bss_size | (align - 1)) + 1;
3137 c->sym->value = bss_size;
3139 bss_size += c->sym->size;
3142 f->sections[i]->header.sh_size = bss_size;
3143 f->sections[i]->header.sh_addralign = max_align;
3147 /* For the sake of patch relocation and parameter initialization,
3148 allocate zeroed data for NOBITS sections now. Note that after
3149 this we cannot assume NOBITS are really empty. */
3150 for (i = 0; i < f->header.e_shnum; ++i) {
3151 struct obj_section *s = f->sections[i];
3152 if (s->header.sh_type == SHT_NOBITS) {
3153 if (s->header.sh_size != 0)
3154 s->contents = memset(xmalloc(s->header.sh_size),
3155 0, s->header.sh_size);
3159 s->header.sh_type = SHT_PROGBITS;
3164 static unsigned long obj_load_size(struct obj_file *f)
3166 unsigned long dot = 0;
3167 struct obj_section *sec;
3169 /* Finalize the positions of the sections relative to one another. */
3171 for (sec = f->load_order; sec; sec = sec->load_next) {
3174 align = sec->header.sh_addralign;
3175 if (align && (dot & (align - 1)))
3176 dot = (dot | (align - 1)) + 1;
3178 sec->header.sh_addr = dot;
3179 dot += sec->header.sh_size;
3185 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3187 int i, n = f->header.e_shnum;
3190 /* Finalize the addresses of the sections. */
3193 for (i = 0; i < n; ++i)
3194 f->sections[i]->header.sh_addr += base;
3196 /* And iterate over all of the relocations. */
3198 for (i = 0; i < n; ++i) {
3199 struct obj_section *relsec, *symsec, *targsec, *strsec;
3200 ElfW(RelM) * rel, *relend;
3204 relsec = f->sections[i];
3205 if (relsec->header.sh_type != SHT_RELM)
3208 symsec = f->sections[relsec->header.sh_link];
3209 targsec = f->sections[relsec->header.sh_info];
3210 strsec = f->sections[symsec->header.sh_link];
3212 rel = (ElfW(RelM) *) relsec->contents;
3213 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3214 symtab = (ElfW(Sym) *) symsec->contents;
3215 strtab = (const char *) strsec->contents;
3217 for (; rel < relend; ++rel) {
3218 ElfW(Addr) value = 0;
3219 struct obj_symbol *intsym = NULL;
3220 unsigned long symndx;
3221 ElfW(Sym) * extsym = 0;
3224 /* Attempt to find a value to use for this relocation. */
3226 symndx = ELF_R_SYM(rel->r_info);
3228 /* Note we've already checked for undefined symbols. */
3230 extsym = &symtab[symndx];
3231 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3232 /* Local symbols we look up in the local table to be sure
3233 we get the one that is really intended. */
3234 intsym = f->local_symtab[symndx];
3236 /* Others we look up in the hash table. */
3238 if (extsym->st_name)
3239 name = strtab + extsym->st_name;
3241 name = f->sections[extsym->st_shndx]->name;
3242 intsym = obj_find_symbol(f, name);
3245 value = obj_symbol_final_value(f, intsym);
3246 intsym->referenced = 1;
3248 #if SHT_RELM == SHT_RELA
3249 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3250 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3251 if (!extsym || !extsym->st_name ||
3252 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3254 value += rel->r_addend;
3258 switch (arch_apply_relocation
3259 (f, targsec, symsec, intsym, rel, value)
3264 case obj_reloc_overflow:
3265 errmsg = "Relocation overflow";
3267 case obj_reloc_dangerous:
3268 errmsg = "Dangerous relocation";
3270 case obj_reloc_unhandled:
3271 errmsg = "Unhandled relocation";
3274 bb_error_msg("%s of type %ld for %s", errmsg,
3275 (long) ELF_R_TYPE(rel->r_info),
3276 strtab + extsym->st_name);
3278 bb_error_msg("%s of type %ld", errmsg,
3279 (long) ELF_R_TYPE(rel->r_info));
3287 /* Finally, take care of the patches. */
3289 if (f->string_patches) {
3290 struct obj_string_patch *p;
3291 struct obj_section *strsec;
3292 ElfW(Addr) strsec_base;
3293 strsec = obj_find_section(f, ".kstrtab");
3294 strsec_base = strsec->header.sh_addr;
3296 for (p = f->string_patches; p; p = p->next) {
3297 struct obj_section *targsec = f->sections[p->reloc_secidx];
3298 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3299 = strsec_base + p->string_offset;
3303 if (f->symbol_patches) {
3304 struct obj_symbol_patch *p;
3306 for (p = f->symbol_patches; p; p = p->next) {
3307 struct obj_section *targsec = f->sections[p->reloc_secidx];
3308 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3309 = obj_symbol_final_value(f, p->sym);
3316 static int obj_create_image(struct obj_file *f, char *image)
3318 struct obj_section *sec;
3319 ElfW(Addr) base = f->baseaddr;
3321 for (sec = f->load_order; sec; sec = sec->load_next) {
3324 if (sec->contents == 0 || sec->header.sh_size == 0)
3327 secimg = image + (sec->header.sh_addr - base);
3329 /* Note that we allocated data for NOBITS sections earlier. */
3330 memcpy(secimg, sec->contents, sec->header.sh_size);
3336 /*======================================================================*/
3338 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3341 ElfW(Shdr) * section_headers;
3345 /* Read the file header. */
3347 f = arch_new_file();
3348 memset(f, 0, sizeof(*f));
3349 f->symbol_cmp = strcmp;
3350 f->symbol_hash = obj_elf_hash;
3351 f->load_order_search_start = &f->load_order;
3353 fseek(fp, 0, SEEK_SET);
3354 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3355 bb_perror_msg("error reading ELF header");
3359 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3360 || f->header.e_ident[EI_MAG1] != ELFMAG1
3361 || f->header.e_ident[EI_MAG2] != ELFMAG2
3362 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3363 bb_error_msg("not an ELF file");
3366 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3367 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3368 ? ELFDATA2MSB : ELFDATA2LSB)
3369 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3370 || !MATCH_MACHINE(f->header.e_machine)) {
3371 bb_error_msg("ELF file not for this architecture");
3374 if (f->header.e_type != ET_REL) {
3375 bb_error_msg("ELF file not a relocatable object");
3379 /* Read the section headers. */
3381 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3382 bb_error_msg("section header size mismatch: %lu != %lu",
3383 (unsigned long) f->header.e_shentsize,
3384 (unsigned long) sizeof(ElfW(Shdr)));
3388 shnum = f->header.e_shnum;
3389 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3390 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3392 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3393 fseek(fp, f->header.e_shoff, SEEK_SET);
3394 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3395 bb_perror_msg("error reading ELF section headers");
3399 /* Read the section data. */
3401 for (i = 0; i < shnum; ++i) {
3402 struct obj_section *sec;
3404 f->sections[i] = sec = arch_new_section();
3405 memset(sec, 0, sizeof(*sec));
3407 sec->header = section_headers[i];
3410 if(sec->header.sh_size) {
3411 switch (sec->header.sh_type) {
3420 if (!loadprogbits) {
3421 sec->contents = NULL;
3428 if (sec->header.sh_size > 0) {
3429 sec->contents = xmalloc(sec->header.sh_size);
3430 fseek(fp, sec->header.sh_offset, SEEK_SET);
3431 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3432 bb_perror_msg("error reading ELF section data");
3436 sec->contents = NULL;
3440 #if SHT_RELM == SHT_REL
3442 bb_error_msg("RELA relocations not supported on this architecture");
3446 bb_error_msg("REL relocations not supported on this architecture");
3451 if (sec->header.sh_type >= SHT_LOPROC) {
3452 /* Assume processor specific section types are debug
3453 info and can safely be ignored. If this is ever not
3454 the case (Hello MIPS?), don't put ifdefs here but
3455 create an arch_load_proc_section(). */
3459 bb_error_msg("can't handle sections of type %ld",
3460 (long) sec->header.sh_type);
3466 /* Do what sort of interpretation as needed by each section. */
3468 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3470 for (i = 0; i < shnum; ++i) {
3471 struct obj_section *sec = f->sections[i];
3472 sec->name = shstrtab + sec->header.sh_name;
3475 for (i = 0; i < shnum; ++i) {
3476 struct obj_section *sec = f->sections[i];
3478 /* .modinfo should be contents only but gcc has no attribute for that.
3479 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3481 if (strcmp(sec->name, ".modinfo") == 0)
3482 sec->header.sh_flags &= ~SHF_ALLOC;
3484 if (sec->header.sh_flags & SHF_ALLOC)
3485 obj_insert_section_load_order(f, sec);
3487 switch (sec->header.sh_type) {
3490 unsigned long nsym, j;
3494 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3495 bb_error_msg("symbol size mismatch: %lu != %lu",
3496 (unsigned long) sec->header.sh_entsize,
3497 (unsigned long) sizeof(ElfW(Sym)));
3501 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3502 strtab = f->sections[sec->header.sh_link]->contents;
3503 sym = (ElfW(Sym) *) sec->contents;
3505 /* Allocate space for a table of local symbols. */
3506 j = f->local_symtab_size = sec->header.sh_info;
3507 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3509 /* Insert all symbols into the hash table. */
3510 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3511 ElfW(Addr) val = sym->st_value;
3514 name = strtab + sym->st_name;
3515 else if (sym->st_shndx < shnum)
3516 name = f->sections[sym->st_shndx]->name;
3519 #if defined(__SH5__)
3521 * For sh64 it is possible that the target of a branch
3522 * requires a mode switch (32 to 16 and back again).
3524 * This is implied by the lsb being set in the target
3525 * address for SHmedia mode and clear for SHcompact.
3527 val |= sym->st_other & 4;
3530 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3537 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3538 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3539 (unsigned long) sec->header.sh_entsize,
3540 (unsigned long) sizeof(ElfW(RelM)));
3544 /* XXX Relocation code from modutils-2.3.19 is not here.
3545 * Why? That's about 20 lines of code from obj/obj_load.c,
3546 * which gets done in a second pass through the sections.
3547 * This BusyBox insmod does similar work in obj_relocate(). */
3554 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3556 * load the unloaded sections directly into the memory allocated by
3557 * kernel for the module
3560 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3562 ElfW(Addr) base = f->baseaddr;
3563 struct obj_section* sec;
3565 for (sec = f->load_order; sec; sec = sec->load_next) {
3567 /* section already loaded? */
3568 if (sec->contents != NULL)
3571 if (sec->header.sh_size == 0)
3574 sec->contents = imagebase + (sec->header.sh_addr - base);
3575 fseek(fp, sec->header.sh_offset, SEEK_SET);
3576 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3577 bb_perror_msg("error reading ELF section data");
3586 static void hide_special_symbols(struct obj_file *f)
3588 static const char *const specials[] = {
3589 SPFX "cleanup_module",
3591 SPFX "kernel_version",
3595 struct obj_symbol *sym;
3596 const char *const *p;
3598 for (p = specials; *p; ++p) {
3599 sym = obj_find_symbol(f, *p);
3601 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3606 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3607 static int obj_gpl_license(struct obj_file *f, const char **license)
3609 struct obj_section *sec;
3610 /* This list must match *exactly* the list of allowable licenses in
3611 * linux/include/linux/module.h. Checking for leading "GPL" will not
3612 * work, somebody will use "GPL sucks, this is proprietary".
3614 static const char * const gpl_licenses[] = {
3617 "GPL and additional rights",
3622 sec = obj_find_section(f, ".modinfo");
3624 const char *value, *ptr, *endptr;
3625 ptr = sec->contents;
3626 endptr = ptr + sec->header.sh_size;
3627 while (ptr < endptr) {
3628 value = strchr(ptr, '=');
3629 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3633 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3634 if (strcmp(value+1, gpl_licenses[i]) == 0)
3639 if (strchr(ptr, '\0'))
3640 ptr = strchr(ptr, '\0') + 1;
3648 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3649 #define TAINT_PROPRIETORY_MODULE (1<<0)
3650 #define TAINT_FORCED_MODULE (1<<1)
3651 #define TAINT_UNSAFE_SMP (1<<2)
3652 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3654 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3655 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3659 static int first = 1;
3660 if (fd < 0 && !kernel_has_tainted)
3661 return; /* New modutils on old kernel */
3662 printf("Warning: loading %s will taint the kernel: %s%s\n",
3663 m_name, text1, text2);
3665 printf(" See %s for information about tainted modules\n", TAINT_URL);
3669 read(fd, buf, sizeof(buf)-1);
3670 buf[sizeof(buf)-1] = '\0';
3671 oldval = strtoul(buf, NULL, 10);
3672 sprintf(buf, "%d\n", oldval | taint);
3673 write(fd, buf, strlen(buf));
3677 /* Check if loading this module will taint the kernel. */
3678 static void check_tainted_module(struct obj_file *f, char *m_name)
3680 static const char tainted_file[] = TAINT_FILENAME;
3681 int fd, kernel_has_tainted;
3684 kernel_has_tainted = 1;
3685 fd = open(tainted_file, O_RDWR);
3687 if (errno == ENOENT)
3688 kernel_has_tainted = 0;
3689 else if (errno == EACCES)
3690 kernel_has_tainted = 1;
3692 perror(tainted_file);
3693 kernel_has_tainted = 0;
3697 switch (obj_gpl_license(f, &ptr)) {
3701 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3704 /* The module has a non-GPL license so we pretend that the
3705 * kernel always has a taint flag to get a warning even on
3706 * kernels without the proc flag.
3708 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3711 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3715 if (flag_force_load)
3716 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3721 #else /* FEATURE_CHECK_TAINTED_MODULE */
3722 #define check_tainted_module(x, y) do { } while(0);
3723 #endif /* FEATURE_CHECK_TAINTED_MODULE */
3725 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3726 /* add module source, timestamp, kernel version and a symbol for the
3727 * start of some sections. this info is used by ksymoops to do better
3731 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3733 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3734 return new_get_module_version(f, str);
3735 #else /* FEATURE_INSMOD_VERSION_CHECKING */
3736 strncpy(str, "???", sizeof(str));
3738 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
3741 /* add module source, timestamp, kernel version and a symbol for the
3742 * start of some sections. this info is used by ksymoops to do better
3746 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3749 static const char symprefix[] = "__insmod_";
3750 struct obj_section *sec;
3751 struct obj_symbol *sym;
3752 char *name, *absolute_filename;
3753 char str[STRVERSIONLEN], real[PATH_MAX];
3754 int i, l, lm_name, lfilename, use_ksymtab, version;
3755 struct stat statbuf;
3757 static const char *section_names[] = {
3765 if (realpath(filename, real)) {
3766 absolute_filename = xstrdup(real);
3769 int save_errno = errno;
3770 bb_error_msg("cannot get realpath for %s", filename);
3773 absolute_filename = xstrdup(filename);
3776 lm_name = strlen(m_name);
3777 lfilename = strlen(absolute_filename);
3779 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3780 * are not to be exported. otherwise leave ksymtab alone for now, the
3781 * "export all symbols" compatibility code will export these symbols later.
3783 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3785 if ((sec = obj_find_section(f, ".this"))) {
3786 /* tag the module header with the object name, last modified
3787 * timestamp and module version. worst case for module version
3788 * is 0xffffff, decimal 16777215. putting all three fields in
3789 * one symbol is less readable but saves kernel space.
3791 l = sizeof(symprefix)+ /* "__insmod_" */
3792 lm_name+ /* module name */
3794 lfilename+ /* object filename */
3796 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3798 8+ /* version in dec */
3801 if (stat(absolute_filename, &statbuf) != 0)
3802 statbuf.st_mtime = 0;
3803 version = get_module_version(f, str); /* -1 if not found */
3804 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3805 symprefix, m_name, absolute_filename,
3806 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3808 sym = obj_add_symbol(f, name, -1,
3809 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3810 sec->idx, sec->header.sh_addr, 0);
3812 new_add_ksymtab(f, sym);
3814 free(absolute_filename);
3815 #ifdef _NOT_SUPPORTED_
3816 /* record where the persistent data is going, same address as previous symbol */
3819 l = sizeof(symprefix)+ /* "__insmod_" */
3820 lm_name+ /* module name */
3822 strlen(f->persist)+ /* data store */
3825 snprintf(name, l, "%s%s_P%s",
3826 symprefix, m_name, f->persist);
3827 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3828 sec->idx, sec->header.sh_addr, 0);
3830 new_add_ksymtab(f, sym);
3832 #endif /* _NOT_SUPPORTED_ */
3833 /* tag the desired sections if size is non-zero */
3835 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3836 if ((sec = obj_find_section(f, section_names[i])) &&
3837 sec->header.sh_size) {
3838 l = sizeof(symprefix)+ /* "__insmod_" */
3839 lm_name+ /* module name */
3841 strlen(sec->name)+ /* section name */
3843 8+ /* length in dec */
3846 snprintf(name, l, "%s%s_S%s_L%ld",
3847 symprefix, m_name, sec->name,
3848 (long)sec->header.sh_size);
3849 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3850 sec->idx, sec->header.sh_addr, 0);
3852 new_add_ksymtab(f, sym);
3856 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3858 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3859 static void print_load_map(struct obj_file *f)
3861 struct obj_symbol *sym;
3862 struct obj_symbol **all, **p;
3863 struct obj_section *sec;
3864 int i, nsyms, *loaded;
3866 /* Report on the section layout. */
3868 printf("Sections: Size %-*s Align\n",
3869 (int) (2 * sizeof(void *)), "Address");
3871 for (sec = f->load_order; sec; sec = sec->load_next) {
3875 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3880 printf("%-15s %08lx %0*lx 2**%d\n",
3882 (long)sec->header.sh_size,
3883 (int) (2 * sizeof(void *)),
3884 (long)sec->header.sh_addr,
3887 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3888 /* Quick reference which section indicies are loaded. */
3890 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3892 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3894 /* Collect the symbols we'll be listing. */
3896 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3897 for (sym = f->symtab[i]; sym; sym = sym->next)
3898 if (sym->secidx <= SHN_HIRESERVE
3899 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3902 all = alloca(nsyms * sizeof(struct obj_symbol *));
3904 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3905 for (sym = f->symtab[i]; sym; sym = sym->next)
3906 if (sym->secidx <= SHN_HIRESERVE
3907 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3910 /* And list them. */
3911 printf("\nSymbols:\n");
3912 for (p = all; p < all + nsyms; ++p) {
3914 unsigned long value;
3917 if (sym->secidx == SHN_ABS) {
3920 } else if (sym->secidx == SHN_UNDEF) {
3924 sec = f->sections[sym->secidx];
3926 if (sec->header.sh_type == SHT_NOBITS)
3928 else if (sec->header.sh_flags & SHF_ALLOC) {
3929 if (sec->header.sh_flags & SHF_EXECINSTR)
3931 else if (sec->header.sh_flags & SHF_WRITE)
3936 value = sym->value + sec->header.sh_addr;
3939 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3940 type = tolower(type);
3942 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3947 #else /* !FEATURE_INSMOD_LOAD_MAP */
3948 void print_load_map(struct obj_file *f);
3951 int insmod_main( int argc, char **argv)
3957 unsigned long m_size;
3962 int exit_status = EXIT_FAILURE;
3964 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3965 struct utsname uts_info;
3966 char m_strversion[STRVERSIONLEN];
3967 int m_version, m_crcs;
3969 #if ENABLE_FEATURE_CLEAN_UP
3975 struct utsname myuname;
3977 /* Parse any options */
3978 getopt32(argc, argv, OPTION_STR, &opt_o);
3979 arg1 = argv[optind];
3980 if (option_mask32 & OPT_o) { // -o /* name the output module */
3982 m_name = xstrdup(opt_o);
3989 /* Grab the module name */
3990 tmp1 = xstrdup(arg1);
3991 tmp = basename(tmp1);
3994 if (uname(&myuname) == 0) {
3995 if (myuname.release[0] == '2') {
3996 k_version = myuname.release[2] - '0';
4000 #if ENABLE_FEATURE_2_6_MODULES
4001 if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
4002 && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
4008 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4014 #if ENABLE_FEATURE_2_6_MODULES
4016 m_fullName = xasprintf("%s.ko", tmp);
4019 m_fullName = xasprintf("%s.o", tmp);
4025 tmp1 = 0; /* flag for free(m_name) before exit() */
4028 /* Get a filedesc for the module. Check we we have a complete path */
4029 if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
4030 || (fp = fopen(arg1, "r")) == NULL
4032 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4033 * but do not error out yet if we fail to find it... */
4034 if (k_version) { /* uname succeedd */
4037 char real_module_dir[FILENAME_MAX];
4039 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4040 /* Jump through hoops in case /lib/modules/`uname -r`
4041 * is a symlink. We do not want recursive_action to
4042 * follow symlinks, but we do want to follow the
4043 * /lib/modules/`uname -r` dir, So resolve it ourselves
4044 * if it is a link... */
4045 if (realpath(tmdn, real_module_dir) == NULL)
4048 module_dir = real_module_dir;
4049 recursive_action(module_dir, TRUE, FALSE, FALSE,
4050 check_module_name_match, 0, m_fullName, 0);
4054 /* Check if we have found anything yet */
4055 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) {
4056 char module_dir[FILENAME_MAX];
4060 if (realpath (_PATH_MODULES, module_dir) == NULL)
4061 strcpy(module_dir, _PATH_MODULES);
4062 /* No module found under /lib/modules/`uname -r`, this
4063 * time cast the net a bit wider. Search /lib/modules/ */
4064 if (!recursive_action(module_dir, TRUE, FALSE, FALSE,
4065 check_module_name_match, 0, m_fullName, 0)
4068 || ((fp = fopen(m_filename, "r")) == NULL)
4070 bb_error_msg("%s: no module by that name found", m_fullName);
4074 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4077 m_filename = xstrdup(arg1);
4080 printf("Using %s\n", m_filename);
4082 #if ENABLE_FEATURE_2_6_MODULES
4083 if (k_version > 4) {
4084 argv[optind] = m_filename;
4086 return insmod_ng_main(argc - optind, argv + optind);
4090 f = obj_load(fp, LOADBITS);
4092 bb_perror_msg_and_die("cannot load the module");
4094 if (get_modinfo_value(f, "kernel_version") == NULL)
4099 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4100 /* Version correspondence? */
4102 if (uname(&uts_info) < 0)
4103 uts_info.release[0] = '\0';
4104 if (m_has_modinfo) {
4105 m_version = new_get_module_version(f, m_strversion);
4106 if (m_version == -1) {
4107 bb_error_msg("cannot find the kernel version the module was "
4113 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4114 if (flag_force_load) {
4115 bb_error_msg("warning: kernel-module version mismatch\n"
4116 "\t%s was compiled for kernel version %s\n"
4117 "\twhile this kernel is version %s",
4118 m_filename, m_strversion, uts_info.release);
4120 bb_error_msg("kernel-module version mismatch\n"
4121 "\t%s was compiled for kernel version %s\n"
4122 "\twhile this kernel is version %s.",
4123 m_filename, m_strversion, uts_info.release);
4129 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4131 if (!query_module(NULL, 0, NULL, 0, NULL)) {
4132 if (!new_get_kernel_symbols())
4134 k_crcs = new_is_kernel_checksummed();
4136 bb_error_msg("not configured to support old kernels");
4140 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4143 m_crcs = new_is_module_checksummed(f);
4145 if (m_crcs != k_crcs)
4146 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4147 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4149 /* Let the module know about the kernel symbols. */
4150 add_kernel_symbols(f);
4152 /* Allocate common symbols, symbol tables, and string tables. */
4154 if (!new_create_this_module(f, m_name)) {
4158 if (!obj_check_undefineds(f)) {
4161 obj_allocate_commons(f);
4162 check_tainted_module(f, m_name);
4164 /* done with the module name, on to the optional var=value arguments */
4166 if (optind < argc) {
4167 if (!new_process_module_arguments(f, argc - optind, argv + optind)) {
4173 hide_special_symbols(f);
4175 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4176 add_ksymoops_symbols(f, m_filename, m_name);
4177 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4179 new_create_module_ksymtab(f);
4181 /* Find current size of the module */
4182 m_size = obj_load_size(f);
4185 m_addr = create_module(m_name, m_size);
4186 if (m_addr == -1) switch (errno) {
4188 bb_error_msg("a module named %s already exists", m_name);
4191 bb_error_msg("can't allocate kernel memory for module; needed %lu bytes",
4195 bb_perror_msg("create_module: %s", m_name);
4201 * the PROGBITS section was not loaded by the obj_load
4202 * now we can load them directly into the kernel memory
4204 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4205 delete_module(m_name);
4210 if (!obj_relocate(f, m_addr)) {
4211 delete_module(m_name);
4215 if (!new_init_module(m_name, f, m_size)) {
4216 delete_module(m_name);
4220 if(flag_print_load_map)
4223 exit_status = EXIT_SUCCESS;
4226 #if ENABLE_FEATURE_CLEAN_UP
4241 #if ENABLE_FEATURE_2_6_MODULES
4243 #include <sys/mman.h>
4244 #include <asm/unistd.h>
4245 #include <sys/syscall.h>
4247 /* We use error numbers in a loose translation... */
4248 static const char *moderror(int err)
4252 return "Invalid module format";
4254 return "Unknown symbol in module";
4256 return "Module has wrong symbol version";
4258 return "Invalid parameters";
4260 return strerror(err);
4264 int insmod_ng_main(int argc, char **argv)
4269 char *filename, *options;
4275 /* Rest is options */
4276 options = xstrdup("");
4278 int optlen = strlen(options);
4279 options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
4280 /* Spaces handled by "" pairs, but no way of escaping quotes */
4281 sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
4285 /* Any special reason why mmap? It isn't performace critical... */
4289 fd = xopen(filename, O_RDONLY);
4292 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4293 if (map == MAP_FAILED) {
4294 bb_perror_msg_and_die("cannot mmap '%s'", filename);
4297 /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
4300 xread(fd, map, len);
4303 len = MAXINT(ssize_t);
4304 map = xmalloc_open_read_close(filename, &len);
4307 ret = syscall(__NR_init_module, map, len, options);
4309 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
4310 filename, moderror(errno), ret);