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 #undef ENABLE_FEATURE_2_4_MODULES
67 #define ENABLE_FEATURE_2_4_MODULES 1
70 #if !ENABLE_FEATURE_2_4_MODULES
71 #define insmod_ng_main insmod_main
74 #if ENABLE_FEATURE_2_6_MODULES
75 extern int insmod_ng_main( int argc, char **argv);
79 #if ENABLE_FEATURE_2_4_MODULES
82 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
90 #if defined(__alpha__)
91 #define MATCH_MACHINE(x) (x == EM_ALPHA)
92 #define SHT_RELM SHT_RELA
93 #define Elf64_RelM Elf64_Rela
94 #define ELFCLASSM ELFCLASS64
99 #define MATCH_MACHINE(x) (x == EM_ARM)
100 #define SHT_RELM SHT_REL
101 #define Elf32_RelM Elf32_Rel
102 #define ELFCLASSM ELFCLASS32
103 #define USE_PLT_ENTRIES
104 #define PLT_ENTRY_SIZE 8
105 #define USE_GOT_ENTRIES
106 #define GOT_ENTRY_SIZE 8
112 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
113 #define SHT_RELM SHT_RELA
114 #define Elf32_RelM Elf32_Rela
115 #define ELFCLASSM ELFCLASS32
119 #if defined(__cris__)
120 #define MATCH_MACHINE(x) (x == EM_CRIS)
121 #define SHT_RELM SHT_RELA
122 #define Elf32_RelM Elf32_Rela
123 #define ELFCLASSM ELFCLASS32
126 #define R_CRIS_NONE 0
132 #if defined(__H8300H__) || defined(__H8300S__)
133 #define MATCH_MACHINE(x) (x == EM_H8_300)
134 #define SHT_RELM SHT_RELA
135 #define Elf32_RelM Elf32_Rela
136 #define ELFCLASSM ELFCLASS32
138 #define SYMBOL_PREFIX "_"
141 /* PA-RISC / HP-PA */
142 #if defined(__hppa__)
143 #define MATCH_MACHINE(x) (x == EM_PARISC)
144 #define SHT_RELM SHT_RELA
145 #if defined(__LP64__)
146 #define Elf64_RelM Elf64_Rela
147 #define ELFCLASSM ELFCLASS64
149 #define Elf32_RelM Elf32_Rela
150 #define ELFCLASSM ELFCLASS32
155 #if defined(__i386__)
157 #define MATCH_MACHINE(x) (x == EM_386)
159 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
161 #define SHT_RELM SHT_REL
162 #define Elf32_RelM Elf32_Rel
163 #define ELFCLASSM ELFCLASS32
164 #define USE_GOT_ENTRIES
165 #define GOT_ENTRY_SIZE 4
169 /* IA64, aka Itanium */
170 #if defined(__ia64__)
171 #define MATCH_MACHINE(x) (x == EM_IA_64)
172 #define SHT_RELM SHT_RELA
173 #define Elf64_RelM Elf64_Rela
174 #define ELFCLASSM ELFCLASS64
178 #if defined(__mc68000__)
179 #define MATCH_MACHINE(x) (x == EM_68K)
180 #define SHT_RELM SHT_RELA
181 #define Elf32_RelM Elf32_Rela
182 #define ELFCLASSM ELFCLASS32
183 #define USE_GOT_ENTRIES
184 #define GOT_ENTRY_SIZE 4
189 #if defined(__microblaze__)
191 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
192 #define SHT_RELM SHT_RELA
193 #define Elf32_RelM Elf32_Rela
194 #define ELFCLASSM ELFCLASS32
198 #if defined(__mips__)
199 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
200 #define SHT_RELM SHT_REL
201 #define Elf32_RelM Elf32_Rel
202 #define ELFCLASSM ELFCLASS32
203 /* Account for ELF spec changes. */
204 #ifndef EM_MIPS_RS3_LE
205 #ifdef EM_MIPS_RS4_BE
206 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
208 #define EM_MIPS_RS3_LE 10
210 #endif /* !EM_MIPS_RS3_LE */
211 #define ARCHDATAM "__dbe_table"
215 #if defined(__nios2__)
216 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
217 #define SHT_RELM SHT_RELA
218 #define Elf32_RelM Elf32_Rela
219 #define ELFCLASSM ELFCLASS32
223 #if defined(__powerpc64__)
224 #define MATCH_MACHINE(x) (x == EM_PPC64)
225 #define SHT_RELM SHT_RELA
226 #define Elf64_RelM Elf64_Rela
227 #define ELFCLASSM ELFCLASS64
228 #elif defined(__powerpc__)
229 #define MATCH_MACHINE(x) (x == EM_PPC)
230 #define SHT_RELM SHT_RELA
231 #define Elf32_RelM Elf32_Rela
232 #define ELFCLASSM ELFCLASS32
233 #define USE_PLT_ENTRIES
234 #define PLT_ENTRY_SIZE 16
236 #define LIST_ARCHTYPE ElfW(Addr)
238 #define ARCHDATAM "__ftr_fixup"
242 #if defined(__s390__)
243 #define MATCH_MACHINE(x) (x == EM_S390)
244 #define SHT_RELM SHT_RELA
245 #define Elf32_RelM Elf32_Rela
246 #define ELFCLASSM ELFCLASS32
247 #define USE_PLT_ENTRIES
248 #define PLT_ENTRY_SIZE 8
249 #define USE_GOT_ENTRIES
250 #define GOT_ENTRY_SIZE 8
256 #define MATCH_MACHINE(x) (x == EM_SH)
257 #define SHT_RELM SHT_RELA
258 #define Elf32_RelM Elf32_Rela
259 #define ELFCLASSM ELFCLASS32
260 #define USE_GOT_ENTRIES
261 #define GOT_ENTRY_SIZE 4
263 /* the SH changes have only been tested in =little endian= mode */
264 /* I'm not sure about big endian, so let's warn: */
265 #if defined(__sh__) && BB_BIG_ENDIAN
266 # error insmod.c may require changes for use on big endian SH
268 /* it may or may not work on the SH1/SH2... Error on those also */
269 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
270 #error insmod.c may require changes for SH1 or SH2 use
275 #if defined(__sparc__)
276 #define MATCH_MACHINE(x) (x == EM_SPARC)
277 #define SHT_RELM SHT_RELA
278 #define Elf32_RelM Elf32_Rela
279 #define ELFCLASSM ELFCLASS32
283 #if defined(__v850e__)
284 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
285 #define SHT_RELM SHT_RELA
286 #define Elf32_RelM Elf32_Rela
287 #define ELFCLASSM ELFCLASS32
288 #define USE_PLT_ENTRIES
289 #define PLT_ENTRY_SIZE 8
291 #ifndef EM_CYGNUS_V850 /* grumble */
292 #define EM_CYGNUS_V850 0x9080
294 #define SYMBOL_PREFIX "_"
298 #if defined(__x86_64__)
299 #define MATCH_MACHINE(x) (x == EM_X86_64)
300 #define SHT_RELM SHT_RELA
301 #define USE_GOT_ENTRIES
302 #define GOT_ENTRY_SIZE 8
304 #define Elf64_RelM Elf64_Rela
305 #define ELFCLASSM ELFCLASS64
309 #error Sorry, but insmod.c does not yet support this architecture...
313 //----------------------------------------------------------------------------
314 //--------modutils module.h, lines 45-242
315 //----------------------------------------------------------------------------
317 /* Definitions for the Linux module syscall interface.
318 Copyright 1996, 1997 Linux International.
320 Contributed by Richard Henderson <rth@tamu.edu>
322 This file is part of the Linux modutils.
324 This program is free software; you can redistribute it and/or modify it
325 under the terms of the GNU General Public License as published by the
326 Free Software Foundation; either version 2 of the License, or (at your
327 option) any later version.
329 This program is distributed in the hope that it will be useful, but
330 WITHOUT ANY WARRANTY; without even the implied warranty of
331 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
332 General Public License for more details.
334 You should have received a copy of the GNU General Public License
335 along with this program; if not, write to the Free Software Foundation,
336 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
339 #ifndef MODUTILS_MODULE_H
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
497 /* The relocatable object is manipulated using elfin types. */
503 # if ELFCLASSM == ELFCLASS32
504 # define ElfW(x) Elf32_ ## x
505 # define ELFW(x) ELF32_ ## x
507 # define ElfW(x) Elf64_ ## x
508 # define ELFW(x) ELF64_ ## x
512 /* For some reason this is missing from some ancient C libraries.... */
513 #ifndef ELF32_ST_INFO
514 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
517 #ifndef ELF64_ST_INFO
518 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
521 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
522 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
523 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
524 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
525 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
527 struct obj_string_patch;
528 struct obj_symbol_patch;
535 struct obj_section *load_next;
541 struct obj_symbol *next; /* hash table link */
545 int secidx; /* the defining section index/module */
547 int ksymidx; /* for export to the kernel symtab */
548 int referenced; /* actually used in the link */
551 /* Hardcode the hash table size. We shouldn't be needing so many
552 symbols that we begin to degrade performance, and we get a big win
553 by giving the compiler a constant divisor. */
555 #define HASH_BUCKETS 521
560 struct obj_section **sections;
561 struct obj_section *load_order;
562 struct obj_section **load_order_search_start;
563 struct obj_string_patch *string_patches;
564 struct obj_symbol_patch *symbol_patches;
565 int (*symbol_cmp)(const char *, const char *);
566 unsigned long (*symbol_hash)(const char *);
567 unsigned long local_symtab_size;
568 struct obj_symbol **local_symtab;
569 struct obj_symbol *symtab[HASH_BUCKETS];
579 struct obj_string_patch {
580 struct obj_string_patch *next;
582 ElfW(Addr) reloc_offset;
583 ElfW(Addr) string_offset;
586 struct obj_symbol_patch {
587 struct obj_symbol_patch *next;
589 ElfW(Addr) reloc_offset;
590 struct obj_symbol *sym;
594 /* Generic object manipulation routines. */
596 static unsigned long obj_elf_hash(const char *);
598 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
600 static struct obj_symbol *obj_find_symbol(struct obj_file *f,
603 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
604 struct obj_symbol *sym);
606 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
607 static void obj_set_symbol_compare(struct obj_file *f,
608 int (*cmp)(const char *, const char *),
609 unsigned long (*hash)(const char *));
612 static struct obj_section *obj_find_section(struct obj_file *f,
615 static void obj_insert_section_load_order(struct obj_file *f,
616 struct obj_section *sec);
618 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
623 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
628 static void *obj_extend_section(struct obj_section *sec, unsigned long more);
630 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
633 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
634 struct obj_symbol *sym);
636 static int obj_check_undefineds(struct obj_file *f);
638 static void obj_allocate_commons(struct obj_file *f);
640 static unsigned long obj_load_size(struct obj_file *f);
642 static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
644 static struct obj_file *obj_load(FILE *f, int loadprogbits);
646 static int obj_create_image(struct obj_file *f, char *image);
648 /* Architecture specific manipulation routines. */
650 static struct obj_file *arch_new_file(void);
652 static struct obj_section *arch_new_section(void);
654 static struct obj_symbol *arch_new_symbol(void);
656 static enum obj_reloc arch_apply_relocation(struct obj_file *f,
657 struct obj_section *targsec,
658 struct obj_section *symsec,
659 struct obj_symbol *sym,
660 ElfW(RelM) *rel, ElfW(Addr) value);
662 static void arch_create_got(struct obj_file *f);
663 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
664 static int obj_gpl_license(struct obj_file *f, const char **license);
665 #endif /* FEATURE_CHECK_TAINTED_MODULE */
667 //----------------------------------------------------------------------------
668 //--------end of modutils obj.h
669 //----------------------------------------------------------------------------
672 /* SPFX is always a string, so it can be concatenated to string constants. */
674 #define SPFX SYMBOL_PREFIX
680 #define _PATH_MODULES "/lib/modules"
681 enum { STRVERSIONLEN = 64 };
683 /*======================================================================*/
685 #define OPTION_STR "sLo:fkvqx" USE_FEATURE_INSMOD_LOAD_MAP("m")
687 OPT_s = 0x1, // -s /* log to syslog */
688 /* Not supported but kernel needs this for request_module(),
689 as this calls: modprobe -k -s -- <module>
690 so silently ignore this flag */
691 OPT_L = 0x2, // -L /* Stub warning */
692 /* Compatibility with modprobe.
693 In theory, this does locking, but we don't do
694 that. So be careful and plan your life around not
695 loading the same module 50 times concurrently. */
696 OPT_o = 0x4, // -o /* name the output module */
697 OPT_f = 0x8, // -f /* force loading */
698 OPT_k = 0x10, // -k /* module loaded by kerneld, auto-cleanable */
699 OPT_v = 0x20, // -v /* verbose output */
700 OPT_q = 0x40, // -q /* silent */
701 OPT_x = 0x80, // -x /* do not export externs */
702 OPT_m = 0x100, // -m /* print module load map */
704 #define flag_force_load (option_mask32 & OPT_f)
705 #define flag_autoclean (option_mask32 & OPT_k)
706 #define flag_verbose (option_mask32 & OPT_v)
707 #define flag_quiet (option_mask32 & OPT_q)
708 #define flag_noexport (option_mask32 & OPT_x)
709 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
710 #define flag_print_load_map (option_mask32 & OPT_m)
712 #define flag_print_load_map 0
715 /*======================================================================*/
717 #if defined(USE_LIST)
719 struct arch_list_entry
721 struct arch_list_entry *next;
722 LIST_ARCHTYPE addend;
729 #if defined(USE_SINGLE)
731 struct arch_single_entry
740 #if defined(__mips__)
743 struct mips_hi16 *next;
750 struct obj_file root;
751 #if defined(USE_PLT_ENTRIES)
752 struct obj_section *plt;
754 #if defined(USE_GOT_ENTRIES)
755 struct obj_section *got;
757 #if defined(__mips__)
758 struct mips_hi16 *mips_hi16_list;
763 struct obj_symbol root;
764 #if defined(USE_PLT_ENTRIES)
765 #if defined(USE_PLT_LIST)
766 struct arch_list_entry *pltent;
768 struct arch_single_entry pltent;
771 #if defined(USE_GOT_ENTRIES)
772 struct arch_single_entry gotent;
777 struct external_module {
782 struct new_module_symbol *syms;
785 static struct new_module_symbol *ksyms;
786 static size_t nksyms;
788 static struct external_module *ext_modules;
789 static int n_ext_modules;
790 static int n_ext_modules_used;
791 extern int delete_module(const char *);
793 static char *m_filename;
794 static char *m_fullName;
797 /*======================================================================*/
800 static int check_module_name_match(const char *filename, struct stat *statbuf,
801 void *userdata, int depth)
803 char *fullname = (char *) userdata;
805 if (fullname[0] == '\0')
808 char *tmp, *tmp1 = xstrdup(filename);
809 tmp = bb_get_last_path_component(tmp1);
810 if (strcmp(tmp, fullname) == 0) {
812 /* Stop searching if we find a match */
813 m_filename = xstrdup(filename);
822 /*======================================================================*/
824 static struct obj_file *arch_new_file(void)
827 f = xmalloc(sizeof(*f));
829 memset(f, 0, sizeof(*f));
834 static struct obj_section *arch_new_section(void)
836 return xmalloc(sizeof(struct obj_section));
839 static struct obj_symbol *arch_new_symbol(void)
841 struct arch_symbol *sym;
842 sym = xmalloc(sizeof(*sym));
844 memset(sym, 0, sizeof(*sym));
849 static enum obj_reloc
850 arch_apply_relocation(struct obj_file *f,
851 struct obj_section *targsec,
852 struct obj_section *symsec,
853 struct obj_symbol *sym,
854 ElfW(RelM) *rel, ElfW(Addr) v)
856 struct arch_file *ifile = (struct arch_file *) f;
857 enum obj_reloc ret = obj_reloc_ok;
858 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
859 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
860 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
861 struct arch_symbol *isym = (struct arch_symbol *) sym;
863 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
864 #if defined(USE_GOT_ENTRIES)
865 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
868 #if defined(USE_PLT_ENTRIES)
869 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
871 # if defined(USE_PLT_LIST)
872 struct arch_list_entry *pe;
874 struct arch_single_entry *pe;
878 switch (ELF_R_TYPE(rel->r_info)) {
893 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
894 * (which is .got) similar to branch,
895 * but is full 32 bits relative */
904 case R_ARM_GOTOFF: /* address relative to the got */
908 #elif defined(__cris__)
914 /* CRIS keeps the relocation value in the r_addend field and
915 * should not use whats in *loc at all
920 #elif defined(__H8300H__) || defined(__H8300S__)
923 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
924 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
935 if ((ElfW(Sword))v > 0x7fff ||
936 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
937 ret = obj_reloc_overflow;
939 *(unsigned short *)loc = v;
943 if ((ElfW(Sword))v > 0x7f ||
944 (ElfW(Sword))v < -(ElfW(Sword))0x80)
945 ret = obj_reloc_overflow;
947 *(unsigned char *)loc = v;
950 #elif defined(__i386__)
984 #elif defined(__microblaze__)
985 case R_MICROBLAZE_NONE:
986 case R_MICROBLAZE_64_NONE:
987 case R_MICROBLAZE_32_SYM_OP_SYM:
988 case R_MICROBLAZE_32_PCREL:
991 case R_MICROBLAZE_64_PCREL: {
992 /* dot is the address of the current instruction.
993 * v is the target symbol address.
994 * So we need to extract the offset in the code,
995 * adding v, then subtrating the current address
996 * of this instruction.
997 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
1000 /* Get split offset stored in code */
1001 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
1004 /* Adjust relative offset. -4 adjustment required
1005 * because dot points to the IMM insn, but branch
1006 * is computed relative to the branch instruction itself.
1008 temp += v - dot - 4;
1010 /* Store back into code */
1011 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
1012 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
1017 case R_MICROBLAZE_32:
1021 case R_MICROBLAZE_64: {
1022 /* Get split pointer stored in code */
1023 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
1026 /* Add reloc offset */
1029 /* Store back into code */
1030 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
1031 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
1036 case R_MICROBLAZE_32_PCREL_LO:
1037 case R_MICROBLAZE_32_LO:
1038 case R_MICROBLAZE_SRO32:
1039 case R_MICROBLAZE_SRW32:
1040 ret = obj_reloc_unhandled;
1043 #elif defined(__mc68000__)
1054 ret = obj_reloc_overflow;
1061 ret = obj_reloc_overflow;
1068 if ((ElfW(Sword))v > 0x7f ||
1069 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1070 ret = obj_reloc_overflow;
1077 if ((ElfW(Sword))v > 0x7fff ||
1078 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1079 ret = obj_reloc_overflow;
1085 *(int *)loc = v - dot;
1088 case R_68K_GLOB_DAT:
1089 case R_68K_JMP_SLOT:
1093 case R_68K_RELATIVE:
1094 *(int *)loc += f->baseaddr;
1100 # ifdef R_68K_GOTOFF
1106 #elif defined(__mips__)
1117 ret = obj_reloc_dangerous;
1118 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1119 ret = obj_reloc_overflow;
1121 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1127 struct mips_hi16 *n;
1129 /* We cannot relocate this one now because we don't know the value
1130 of the carry we need to add. Save the information, and let LO16
1131 do the actual relocation. */
1132 n = xmalloc(sizeof *n);
1135 n->next = ifile->mips_hi16_list;
1136 ifile->mips_hi16_list = n;
1142 unsigned long insnlo = *loc;
1143 ElfW(Addr) val, vallo;
1145 /* Sign extend the addend we extract from the lo insn. */
1146 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1148 if (ifile->mips_hi16_list != NULL) {
1149 struct mips_hi16 *l;
1151 l = ifile->mips_hi16_list;
1153 struct mips_hi16 *next;
1156 /* Do the HI16 relocation. Note that we actually don't
1157 need to know anything about the LO16 itself, except where
1158 to find the low 16 bits of the addend needed by the LO16. */
1161 ((insn & 0xffff) << 16) +
1165 /* Account for the sign extension that will happen in the
1172 insn = (insn & ~0xffff) | val;
1180 ifile->mips_hi16_list = NULL;
1183 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1185 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1190 #elif defined(__nios2__)
1195 case R_NIOS2_BFD_RELOC_32:
1199 case R_NIOS2_BFD_RELOC_16:
1201 ret = obj_reloc_overflow;
1206 case R_NIOS2_BFD_RELOC_8:
1208 ret = obj_reloc_overflow;
1217 if ((Elf32_Sword)v > 0x7fff ||
1218 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1219 ret = obj_reloc_overflow;
1223 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1233 ret = obj_reloc_overflow;
1237 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1242 case R_NIOS2_PCREL16:
1247 if ((Elf32_Sword)v > 0x7fff ||
1248 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1249 ret = obj_reloc_overflow;
1253 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1259 Elf32_Addr word, gp;
1261 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1263 if ((Elf32_Sword)v > 0x7fff ||
1264 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1265 ret = obj_reloc_overflow;
1269 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1273 case R_NIOS2_CALL26:
1275 ret = obj_reloc_dangerous;
1276 if ((v >> 28) != (dot >> 28))
1277 ret = obj_reloc_overflow;
1278 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1286 ret = obj_reloc_overflow;
1289 word = *loc & ~0x7c0;
1290 *loc = word | ((v & 0x1f) << 6);
1299 ret = obj_reloc_overflow;
1302 word = *loc & ~0xfc0;
1303 *loc = word | ((v & 0x3f) << 6);
1312 ret = obj_reloc_overflow;
1315 word = *loc & ~0x3fc0;
1316 *loc = word | ((v & 0xff) << 6);
1325 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1335 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1340 case R_NIOS2_HIADJ16:
1342 Elf32_Addr word1, word2;
1345 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1346 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1351 #elif defined(__powerpc64__)
1352 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1354 #elif defined(__powerpc__)
1356 case R_PPC_ADDR16_HA:
1357 *(unsigned short *)loc = (v + 0x8000) >> 16;
1360 case R_PPC_ADDR16_HI:
1361 *(unsigned short *)loc = v >> 16;
1364 case R_PPC_ADDR16_LO:
1365 *(unsigned short *)loc = v;
1379 #elif defined(__s390__)
1382 *(unsigned int *) loc += v;
1385 *(unsigned short *) loc += v;
1388 *(unsigned char *) loc += v;
1392 *(unsigned int *) loc += v - dot;
1395 *(unsigned short *) loc += (v - dot) >> 1;
1398 *(unsigned short *) loc += v - dot;
1402 case R_390_PLT16DBL:
1403 /* find the plt entry and initialize it. */
1404 pe = (struct arch_single_entry *) &isym->pltent;
1405 if (pe->inited == 0) {
1406 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1407 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1409 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1416 /* Insert relative distance to target. */
1417 v = plt + pe->offset - dot;
1418 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1419 *(unsigned int *) loc = (unsigned int) v;
1420 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1421 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1424 case R_390_GLOB_DAT:
1425 case R_390_JMP_SLOT:
1429 case R_390_RELATIVE:
1430 *loc += f->baseaddr;
1434 *(unsigned long *) loc += got - dot;
1440 if (!isym->gotent.inited)
1442 isym->gotent.inited = 1;
1443 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1445 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1446 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1447 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1448 *(unsigned short *) loc += isym->gotent.offset;
1449 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1450 *(unsigned int *) loc += isym->gotent.offset;
1453 # ifndef R_390_GOTOFF32
1454 # define R_390_GOTOFF32 R_390_GOTOFF
1456 case R_390_GOTOFF32:
1460 #elif defined(__sh__)
1483 *loc = f->baseaddr + rel->r_addend;
1487 *loc = got - dot + rel->r_addend;
1497 # if defined(__SH5__)
1498 case R_SH_IMM_MEDLOW16:
1499 case R_SH_IMM_LOW16:
1503 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1507 * movi and shori have the format:
1509 * | op | imm | reg | reserved |
1510 * 31..26 25..10 9.. 4 3 .. 0
1512 * so we simply mask and or in imm.
1514 word = *loc & ~0x3fffc00;
1515 word |= (v & 0xffff) << 10;
1522 case R_SH_IMM_MEDLOW16_PCREL:
1523 case R_SH_IMM_LOW16_PCREL:
1527 word = *loc & ~0x3fffc00;
1531 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1534 word |= (v & 0xffff) << 10;
1540 # endif /* __SH5__ */
1542 #elif defined(__v850e__)
1548 /* We write two shorts instead of a long because even
1549 32-bit insns only need half-word alignment, but
1550 32-bit data needs to be long-word aligned. */
1551 v += ((unsigned short *)loc)[0];
1552 v += ((unsigned short *)loc)[1] << 16;
1553 ((unsigned short *)loc)[0] = v & 0xffff;
1554 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1557 case R_V850_22_PCREL:
1560 #elif defined(__x86_64__)
1570 *(unsigned int *) loc += v;
1573 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1574 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1579 *(signed int *) loc += v;
1583 *(unsigned short *) loc += v;
1587 *(unsigned char *) loc += v;
1591 *(unsigned int *) loc += v - dot;
1595 *(unsigned short *) loc += v - dot;
1599 *(unsigned char *) loc += v - dot;
1602 case R_X86_64_GLOB_DAT:
1603 case R_X86_64_JUMP_SLOT:
1607 case R_X86_64_RELATIVE:
1608 *loc += f->baseaddr;
1611 case R_X86_64_GOT32:
1612 case R_X86_64_GOTPCREL:
1615 if (!isym->gotent.reloc_done)
1617 isym->gotent.reloc_done = 1;
1618 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1620 /* XXX are these really correct? */
1621 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1622 *(unsigned int *) loc += v + isym->gotent.offset;
1624 *loc += isym->gotent.offset;
1629 # warning "no idea how to handle relocations on your arch"
1633 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1634 ret = obj_reloc_unhandled;
1637 #if defined(USE_PLT_ENTRIES)
1641 /* find the plt entry and initialize it if necessary */
1643 #if defined(USE_PLT_LIST)
1644 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1651 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1653 /* generate some machine code */
1655 #if defined(__arm__)
1656 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1657 ip[1] = v; /* sym@ */
1659 #if defined(__powerpc__)
1660 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1661 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1662 ip[2] = 0x7d6903a6; /* mtctr r11 */
1663 ip[3] = 0x4e800420; /* bctr */
1665 #if defined(__v850e__)
1666 /* We have to trash a register, so we assume that any control
1667 transfer more than 21-bits away must be a function call
1668 (so we can use a call-clobbered register). */
1669 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1670 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1675 /* relative distance to target */
1677 /* if the target is too far away.... */
1678 #if defined(__arm__) || defined(__powerpc__)
1679 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1680 #elif defined(__v850e__)
1681 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1683 /* go via the plt */
1684 v = plt + pe->offset - dot;
1686 #if defined(__v850e__)
1691 ret = obj_reloc_dangerous;
1693 /* merge the offset into the instruction. */
1694 #if defined(__arm__)
1695 /* Convert to words. */
1698 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1700 #if defined(__powerpc__)
1701 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1703 #if defined(__v850e__)
1704 /* We write two shorts instead of a long because even 32-bit insns
1705 only need half-word alignment, but the 32-bit data write needs
1706 to be long-word aligned. */
1707 ((unsigned short *)loc)[0] =
1708 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1709 | ((v >> 16) & 0x3f); /* offs high part */
1710 ((unsigned short *)loc)[1] =
1711 (v & 0xffff); /* offs low part */
1714 #endif /* USE_PLT_ENTRIES */
1716 #if defined(USE_GOT_ENTRIES)
1719 /* needs an entry in the .got: set it, once */
1720 if (!isym->gotent.inited) {
1721 isym->gotent.inited = 1;
1722 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1724 /* make the reloc with_respect_to_.got */
1726 *loc += isym->gotent.offset + rel->r_addend;
1727 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1728 *loc += isym->gotent.offset;
1732 #endif /* USE_GOT_ENTRIES */
1739 #if defined(USE_LIST)
1741 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1742 int offset, int size)
1744 struct arch_list_entry *pe;
1746 for (pe = *list; pe != NULL; pe = pe->next) {
1747 if (pe->addend == rel->r_addend) {
1753 pe = xmalloc(sizeof(struct arch_list_entry));
1755 pe->addend = rel->r_addend;
1756 pe->offset = offset;
1766 #if defined(USE_SINGLE)
1768 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1769 int offset, int size)
1771 if (single->allocated == 0) {
1772 single->allocated = 1;
1773 single->offset = offset;
1782 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1784 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
1785 int offset, int size)
1787 struct obj_section *myrelsec = obj_find_section(f, name);
1794 obj_extend_section(myrelsec, offset);
1796 myrelsec = obj_create_alloced_section(f, name,
1805 static void arch_create_got(struct obj_file *f)
1807 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1808 struct arch_file *ifile = (struct arch_file *) f;
1810 #if defined(USE_GOT_ENTRIES)
1811 int got_offset = 0, got_needed = 0, got_allocate;
1813 #if defined(USE_PLT_ENTRIES)
1814 int plt_offset = 0, plt_needed = 0, plt_allocate;
1816 struct obj_section *relsec, *symsec, *strsec;
1817 ElfW(RelM) *rel, *relend;
1818 ElfW(Sym) *symtab, *extsym;
1819 const char *strtab, *name;
1820 struct arch_symbol *intsym;
1822 for (i = 0; i < f->header.e_shnum; ++i) {
1823 relsec = f->sections[i];
1824 if (relsec->header.sh_type != SHT_RELM)
1827 symsec = f->sections[relsec->header.sh_link];
1828 strsec = f->sections[symsec->header.sh_link];
1830 rel = (ElfW(RelM) *) relsec->contents;
1831 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1832 symtab = (ElfW(Sym) *) symsec->contents;
1833 strtab = (const char *) strsec->contents;
1835 for (; rel < relend; ++rel) {
1836 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1838 #if defined(USE_GOT_ENTRIES)
1841 #if defined(USE_PLT_ENTRIES)
1845 switch (ELF_R_TYPE(rel->r_info)) {
1846 #if defined(__arm__)
1861 #elif defined(__i386__)
1871 #elif defined(__powerpc__)
1876 #elif defined(__mc68000__)
1887 #elif defined(__sh__)
1897 #elif defined(__v850e__)
1898 case R_V850_22_PCREL:
1907 if (extsym->st_name != 0) {
1908 name = strtab + extsym->st_name;
1910 name = f->sections[extsym->st_shndx]->name;
1912 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1913 #if defined(USE_GOT_ENTRIES)
1915 got_offset += arch_single_init(
1916 rel, &intsym->gotent,
1917 got_offset, GOT_ENTRY_SIZE);
1922 #if defined(USE_PLT_ENTRIES)
1924 #if defined(USE_PLT_LIST)
1925 plt_offset += arch_list_add(
1926 rel, &intsym->pltent,
1927 plt_offset, PLT_ENTRY_SIZE);
1929 plt_offset += arch_single_init(
1930 rel, &intsym->pltent,
1931 plt_offset, PLT_ENTRY_SIZE);
1939 #if defined(USE_GOT_ENTRIES)
1941 ifile->got = arch_xsect_init(f, ".got", got_offset,
1946 #if defined(USE_PLT_ENTRIES)
1948 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1953 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1956 /*======================================================================*/
1958 /* Standard ELF hash function. */
1959 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1961 unsigned long h = 0;
1968 if ((g = (h & 0xf0000000)) != 0) {
1977 static unsigned long obj_elf_hash(const char *name)
1979 return obj_elf_hash_n(name, strlen(name));
1982 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1983 /* String comparison for non-co-versioned kernel and module. */
1985 static int ncv_strcmp(const char *a, const char *b)
1987 size_t alen = strlen(a), blen = strlen(b);
1989 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1990 return strncmp(a, b, alen);
1991 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1992 return strncmp(a, b, blen);
1994 return strcmp(a, b);
1997 /* String hashing for non-co-versioned kernel and module. Here
1998 we are simply forced to drop the crc from the hash. */
2000 static unsigned long ncv_symbol_hash(const char *str)
2002 size_t len = strlen(str);
2003 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
2005 return obj_elf_hash_n(str, len);
2009 obj_set_symbol_compare(struct obj_file *f,
2010 int (*cmp) (const char *, const char *),
2011 unsigned long (*hash) (const char *))
2014 f->symbol_cmp = cmp;
2016 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
2019 f->symbol_hash = hash;
2021 memcpy(tmptab, f->symtab, sizeof(tmptab));
2022 memset(f->symtab, 0, sizeof(f->symtab));
2024 for (i = 0; i < HASH_BUCKETS; ++i)
2025 for (sym = tmptab[i]; sym; sym = next) {
2026 unsigned long h = hash(sym->name) % HASH_BUCKETS;
2028 sym->next = f->symtab[h];
2034 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2036 static struct obj_symbol *
2037 obj_add_symbol(struct obj_file *f, const char *name,
2038 unsigned long symidx, int info,
2039 int secidx, ElfW(Addr) value,
2042 struct obj_symbol *sym;
2043 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2044 int n_type = ELF_ST_TYPE(info);
2045 int n_binding = ELF_ST_BIND(info);
2047 for (sym = f->symtab[hash]; sym; sym = sym->next)
2048 if (f->symbol_cmp(sym->name, name) == 0) {
2049 int o_secidx = sym->secidx;
2050 int o_info = sym->info;
2051 int o_type = ELF_ST_TYPE(o_info);
2052 int o_binding = ELF_ST_BIND(o_info);
2054 /* A redefinition! Is it legal? */
2056 if (secidx == SHN_UNDEF)
2058 else if (o_secidx == SHN_UNDEF)
2060 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2061 /* Cope with local and global symbols of the same name
2062 in the same object file, as might have been created
2063 by ld -r. The only reason locals are now seen at this
2064 level at all is so that we can do semi-sensible things
2067 struct obj_symbol *nsym, **p;
2069 nsym = arch_new_symbol();
2070 nsym->next = sym->next;
2073 /* Excise the old (local) symbol from the hash chain. */
2074 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2078 } else if (n_binding == STB_LOCAL) {
2079 /* Another symbol of the same name has already been defined.
2080 Just add this to the local table. */
2081 sym = arch_new_symbol();
2084 f->local_symtab[symidx] = sym;
2086 } else if (n_binding == STB_WEAK)
2088 else if (o_binding == STB_WEAK)
2090 /* Don't unify COMMON symbols with object types the programmer
2092 else if (secidx == SHN_COMMON
2093 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2095 else if (o_secidx == SHN_COMMON
2096 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2099 /* Don't report an error if the symbol is coming from
2100 the kernel or some external module. */
2101 if (secidx <= SHN_HIRESERVE)
2102 bb_error_msg("%s multiply defined", name);
2107 /* Completely new symbol. */
2108 sym = arch_new_symbol();
2109 sym->next = f->symtab[hash];
2110 f->symtab[hash] = sym;
2113 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2114 if (symidx >= f->local_symtab_size)
2115 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2116 name, (long) symidx, (long) f->local_symtab_size);
2118 f->local_symtab[symidx] = sym;
2125 sym->secidx = secidx;
2131 static struct obj_symbol *
2132 obj_find_symbol(struct obj_file *f, const char *name)
2134 struct obj_symbol *sym;
2135 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2137 for (sym = f->symtab[hash]; sym; sym = sym->next)
2138 if (f->symbol_cmp(sym->name, name) == 0)
2144 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2147 if (sym->secidx >= SHN_LORESERVE)
2150 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2152 /* As a special case, a NULL sym has value zero. */
2157 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2159 int i, n = f->header.e_shnum;
2161 for (i = 0; i < n; ++i)
2162 if (strcmp(f->sections[i]->name, name) == 0)
2163 return f->sections[i];
2168 static int obj_load_order_prio(struct obj_section *a)
2170 unsigned long af, ac;
2172 af = a->header.sh_flags;
2175 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2176 strcmp(a->name + 5, ".init"))
2180 if (!(af & SHF_WRITE))
2182 if (af & SHF_EXECINSTR)
2184 if (a->header.sh_type != SHT_NOBITS)
2191 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2193 struct obj_section **p;
2194 int prio = obj_load_order_prio(sec);
2195 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2196 if (obj_load_order_prio(*p) < prio)
2198 sec->load_next = *p;
2202 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2204 unsigned long align,
2207 int newidx = f->header.e_shnum++;
2208 struct obj_section *sec;
2210 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2211 f->sections[newidx] = sec = arch_new_section();
2213 memset(sec, 0, sizeof(*sec));
2214 sec->header.sh_type = SHT_PROGBITS;
2215 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2216 sec->header.sh_size = size;
2217 sec->header.sh_addralign = align;
2221 sec->contents = xmalloc(size);
2223 obj_insert_section_load_order(f, sec);
2228 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2230 unsigned long align,
2233 int newidx = f->header.e_shnum++;
2234 struct obj_section *sec;
2236 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2237 f->sections[newidx] = sec = arch_new_section();
2239 memset(sec, 0, sizeof(*sec));
2240 sec->header.sh_type = SHT_PROGBITS;
2241 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2242 sec->header.sh_size = size;
2243 sec->header.sh_addralign = align;
2247 sec->contents = xmalloc(size);
2249 sec->load_next = f->load_order;
2250 f->load_order = sec;
2251 if (f->load_order_search_start == &f->load_order)
2252 f->load_order_search_start = &sec->load_next;
2257 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2259 unsigned long oldsize = sec->header.sh_size;
2261 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2263 return sec->contents + oldsize;
2267 /* Conditionally add the symbols from the given symbol set to the
2271 add_symbols_from( struct obj_file *f,
2272 int idx, struct new_module_symbol *syms, size_t nsyms)
2274 struct new_module_symbol *s;
2277 #ifdef SYMBOL_PREFIX
2279 size_t name_alloced_size = 0;
2281 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2284 gpl = obj_gpl_license(f, NULL) == 0;
2286 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2287 /* Only add symbols that are already marked external.
2288 If we override locals we may cause problems for
2289 argument initialization. We will also create a false
2290 dependency on the module. */
2291 struct obj_symbol *sym;
2294 /* GPL licensed modules can use symbols exported with
2295 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2296 * exported names. Non-GPL modules never see any GPLONLY_
2297 * symbols so they cannot fudge it by adding the prefix on
2300 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2301 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2308 name = (char *)s->name;
2310 #ifdef SYMBOL_PREFIX
2311 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2312 kernel exports `C names', but module object files
2313 reference `linker names'). */
2314 size_t extra = sizeof SYMBOL_PREFIX;
2315 size_t name_size = strlen(name) + extra;
2316 if (name_size > name_alloced_size) {
2317 name_alloced_size = name_size * 2;
2318 name_buf = alloca(name_alloced_size);
2320 strcpy(name_buf, SYMBOL_PREFIX);
2321 strcpy(name_buf + extra - 1, name);
2323 #endif /* SYMBOL_PREFIX */
2325 sym = obj_find_symbol(f, name);
2326 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2327 #ifdef SYMBOL_PREFIX
2328 /* Put NAME_BUF into more permanent storage. */
2329 name = xmalloc(name_size);
2330 strcpy(name, name_buf);
2332 sym = obj_add_symbol(f, name, -1,
2333 ELF_ST_INFO(STB_GLOBAL,
2336 /* Did our symbol just get installed? If so, mark the
2337 module as "used". */
2338 if (sym->secidx == idx)
2346 static void add_kernel_symbols(struct obj_file *f)
2348 struct external_module *m;
2351 /* Add module symbols first. */
2353 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
2355 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
2362 n_ext_modules_used = nused;
2364 /* And finally the symbols from the kernel proper. */
2367 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2370 static char *get_modinfo_value(struct obj_file *f, const char *key)
2372 struct obj_section *sec;
2373 char *p, *v, *n, *ep;
2374 size_t klen = strlen(key);
2376 sec = obj_find_section(f, ".modinfo");
2380 ep = p + sec->header.sh_size;
2383 n = strchr(p, '\0');
2385 if (p + klen == v && strncmp(p, key, klen) == 0)
2388 if (p + klen == n && strcmp(p, key) == 0)
2398 /*======================================================================*/
2399 /* Functions relating to module loading after 2.1.18. */
2402 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2405 char *p, *q, *key, *sym_name;
2406 struct obj_symbol *sym;
2407 char *contents, *loc;
2411 if ((q = strchr(p, '=')) == NULL) {
2416 key = alloca(q - p + 6);
2417 memcpy(key, "parm_", 5);
2418 memcpy(key + 5, p, q - p);
2421 p = get_modinfo_value(f, key);
2424 bb_error_msg("invalid parameter %s", key);
2428 #ifdef SYMBOL_PREFIX
2429 sym_name = alloca(strlen(key) + sizeof SYMBOL_PREFIX);
2430 strcpy(sym_name, SYMBOL_PREFIX);
2431 strcat(sym_name, key);
2435 sym = obj_find_symbol(f, sym_name);
2437 /* Also check that the parameter was not resolved from the kernel. */
2438 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2439 bb_error_msg("symbol for parameter %s not found", key);
2444 min = strtoul(p, &p, 10);
2446 max = strtoul(p + 1, &p, 10);
2452 contents = f->sections[sym->secidx]->contents;
2453 loc = contents + sym->value;
2457 if ((*p == 's') || (*p == 'c')) {
2460 /* Do C quoting if we begin with a ", else slurp the lot. */
2464 str = alloca(strlen(q));
2465 for (r = str, q++; *q != '"'; ++q, ++r) {
2467 bb_error_msg("improperly terminated string argument for %s",
2470 } else if (*q == '\\')
2504 if (q[1] >= '0' && q[1] <= '7') {
2505 c = (c * 8) + *++q - '0';
2506 if (q[1] >= '0' && q[1] <= '7')
2507 c = (c * 8) + *++q - '0';
2524 /* In this case, the string is not quoted. We will break
2525 it using the coma (like for ints). If the user wants to
2526 include comas in a string, he just has to quote it */
2528 /* Search the next coma */
2532 if (r != (char *) NULL) {
2533 /* Recopy the current field */
2534 str = alloca(r - q + 1);
2535 memcpy(str, q, r - q);
2537 /* I don't know if it is useful, as the previous case
2538 doesn't nul terminate the string ??? */
2541 /* Keep next fields */
2552 obj_string_patch(f, sym->secidx, loc - contents, str);
2553 loc += tgt_sizeof_char_p;
2555 /* Array of chars (in fact, matrix!) */
2556 unsigned long charssize; /* size of each member */
2558 /* Get the size of each member */
2559 /* Probably we should do that outside the loop ? */
2560 if (!isdigit(*(p + 1))) {
2561 bb_error_msg("parameter type 'c' for %s must be followed by"
2562 " the maximum size", key);
2565 charssize = strtoul(p + 1, (char **) NULL, 10);
2568 if (strlen(str) >= charssize) {
2569 bb_error_msg("string too long for %s (max %ld)", key,
2574 /* Copy to location */
2575 strcpy((char *) loc, str);
2579 long v = strtoul(q, &q, 0);
2586 loc += tgt_sizeof_short;
2590 loc += tgt_sizeof_int;
2594 loc += tgt_sizeof_long;
2598 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2613 goto retry_end_of_value;
2617 bb_error_msg("too many values for %s (max %d)", key, max);
2624 bb_error_msg("invalid argument syntax for %s", key);
2631 bb_error_msg("too few values for %s (min %d)", key, min);
2641 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2642 static int new_is_module_checksummed(struct obj_file *f)
2644 const char *p = get_modinfo_value(f, "using_checksums");
2651 /* Get the module's kernel version in the canonical integer form. */
2654 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2659 p = get_modinfo_value(f, "kernel_version");
2662 safe_strncpy(str, p, STRVERSIONLEN);
2664 a = strtoul(p, &p, 10);
2667 b = strtoul(p + 1, &p, 10);
2670 c = strtoul(p + 1, &q, 10);
2674 return a << 16 | b << 8 | c;
2677 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2680 /* Fetch the loaded modules, and all currently exported symbols. */
2682 static int new_get_kernel_symbols(void)
2684 char *module_names, *mn;
2685 struct external_module *modules, *m;
2686 struct new_module_symbol *syms, *s;
2687 size_t ret, bufsize, nmod, nsyms, i, j;
2689 /* Collect the loaded modules. */
2691 module_names = xmalloc(bufsize = 256);
2693 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2694 if (errno == ENOSPC && bufsize < ret) {
2695 module_names = xrealloc(module_names, bufsize = ret);
2696 goto retry_modules_load;
2698 bb_perror_msg("QM_MODULES");
2702 n_ext_modules = nmod = ret;
2704 /* Collect the modules' symbols. */
2707 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2708 memset(modules, 0, nmod * sizeof(*modules));
2709 for (i = 0, mn = module_names, m = modules;
2710 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2711 struct new_module_info info;
2713 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2714 if (errno == ENOENT) {
2715 /* The module was removed out from underneath us. */
2718 bb_perror_msg("query_module: QM_INFO: %s", mn);
2722 syms = xmalloc(bufsize = 1024);
2724 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2727 syms = xrealloc(syms, bufsize = ret);
2728 goto retry_mod_sym_load;
2730 /* The module was removed out from underneath us. */
2733 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2740 m->addr = info.addr;
2744 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2745 s->name += (unsigned long) syms;
2750 /* Collect the kernel's symbols. */
2752 syms = xmalloc(bufsize = 16 * 1024);
2753 retry_kern_sym_load:
2754 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2755 if (errno == ENOSPC && bufsize < ret) {
2756 syms = xrealloc(syms, bufsize = ret);
2757 goto retry_kern_sym_load;
2759 bb_perror_msg("kernel: QM_SYMBOLS");
2762 nksyms = nsyms = ret;
2765 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2766 s->name += (unsigned long) syms;
2772 /* Return the kernel symbol checksum version, or zero if not used. */
2774 static int new_is_kernel_checksummed(void)
2776 struct new_module_symbol *s;
2779 /* Using_Versions is not the first symbol, but it should be in there. */
2781 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2782 if (strcmp((char *) s->name, "Using_Versions") == 0)
2789 static int new_create_this_module(struct obj_file *f, const char *m_name)
2791 struct obj_section *sec;
2793 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2794 sizeof(struct new_module));
2795 memset(sec->contents, 0, sizeof(struct new_module));
2797 obj_add_symbol(f, SPFX "__this_module", -1,
2798 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2799 sizeof(struct new_module));
2801 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2807 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2808 /* add an entry to the __ksymtab section, creating it if necessary */
2809 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2811 struct obj_section *sec;
2814 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2815 * If __ksymtab is defined but not marked alloc, x out the first character
2816 * (no obj_delete routine) and create a new __ksymtab with the correct
2819 sec = obj_find_section(f, "__ksymtab");
2820 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2821 *((char *)(sec->name)) = 'x'; /* override const */
2825 sec = obj_create_alloced_section(f, "__ksymtab",
2826 tgt_sizeof_void_p, 0);
2829 sec->header.sh_flags |= SHF_ALLOC;
2830 /* Empty section might be byte-aligned */
2831 sec->header.sh_addralign = tgt_sizeof_void_p;
2832 ofs = sec->header.sh_size;
2833 obj_symbol_patch(f, sec->idx, ofs, sym);
2834 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2835 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2837 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2839 static int new_create_module_ksymtab(struct obj_file *f)
2841 struct obj_section *sec;
2844 /* We must always add the module references. */
2846 if (n_ext_modules_used) {
2847 struct new_module_ref *dep;
2848 struct obj_symbol *tm;
2850 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2851 (sizeof(struct new_module_ref)
2852 * n_ext_modules_used));
2856 tm = obj_find_symbol(f, SPFX "__this_module");
2857 dep = (struct new_module_ref *) sec->contents;
2858 for (i = 0; i < n_ext_modules; ++i)
2859 if (ext_modules[i].used) {
2860 dep->dep = ext_modules[i].addr;
2861 obj_symbol_patch(f, sec->idx,
2862 (char *) &dep->ref - sec->contents, tm);
2868 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2872 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2874 /* We don't want to export symbols residing in sections that
2875 aren't loaded. There are a number of these created so that
2876 we make sure certain module options don't appear twice. */
2878 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2880 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2882 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2883 struct obj_symbol *sym;
2884 for (sym = f->symtab[i]; sym; sym = sym->next)
2885 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2886 && sym->secidx <= SHN_HIRESERVE
2887 && (sym->secidx >= SHN_LORESERVE
2888 || loaded[sym->secidx])) {
2889 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2891 obj_symbol_patch(f, sec->idx, ofs, sym);
2892 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2899 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2907 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2909 struct new_module *module;
2910 struct obj_section *sec;
2915 sec = obj_find_section(f, ".this");
2916 if (!sec || !sec->contents) {
2917 bb_perror_msg_and_die("corrupt module %s?",m_name);
2919 module = (struct new_module *) sec->contents;
2920 m_addr = sec->header.sh_addr;
2922 module->size_of_struct = sizeof(*module);
2923 module->size = m_size;
2924 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2926 sec = obj_find_section(f, "__ksymtab");
2927 if (sec && sec->header.sh_size) {
2928 module->syms = sec->header.sh_addr;
2929 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2932 if (n_ext_modules_used) {
2933 sec = obj_find_section(f, ".kmodtab");
2934 module->deps = sec->header.sh_addr;
2935 module->ndeps = n_ext_modules_used;
2939 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2941 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2943 sec = obj_find_section(f, "__ex_table");
2945 module->ex_table_start = sec->header.sh_addr;
2946 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2949 sec = obj_find_section(f, ".text.init");
2951 module->runsize = sec->header.sh_addr - m_addr;
2953 sec = obj_find_section(f, ".data.init");
2955 if (!module->runsize ||
2956 module->runsize > sec->header.sh_addr - m_addr)
2957 module->runsize = sec->header.sh_addr - m_addr;
2959 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2960 if (sec && sec->header.sh_size) {
2961 module->archdata_start = (void*)sec->header.sh_addr;
2962 module->archdata_end = module->archdata_start + sec->header.sh_size;
2964 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2965 if (sec && sec->header.sh_size) {
2966 module->kallsyms_start = (void*)sec->header.sh_addr;
2967 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2970 /* Whew! All of the initialization is complete. Collect the final
2971 module image and give it to the kernel. */
2973 image = xmalloc(m_size);
2974 obj_create_image(f, image);
2976 ret = init_module(m_name, (struct new_module *) image);
2978 bb_perror_msg("init_module: %s", m_name);
2986 /*======================================================================*/
2989 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2992 struct obj_string_patch *p;
2993 struct obj_section *strsec;
2994 size_t len = strlen(string) + 1;
2997 p = xmalloc(sizeof(*p));
2998 p->next = f->string_patches;
2999 p->reloc_secidx = secidx;
3000 p->reloc_offset = offset;
3001 f->string_patches = p;
3003 strsec = obj_find_section(f, ".kstrtab");
3004 if (strsec == NULL) {
3005 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3006 p->string_offset = 0;
3007 loc = strsec->contents;
3009 p->string_offset = strsec->header.sh_size;
3010 loc = obj_extend_section(strsec, len);
3012 memcpy(loc, string, len);
3018 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3019 struct obj_symbol *sym)
3021 struct obj_symbol_patch *p;
3023 p = xmalloc(sizeof(*p));
3024 p->next = f->symbol_patches;
3025 p->reloc_secidx = secidx;
3026 p->reloc_offset = offset;
3028 f->symbol_patches = p;
3033 static int obj_check_undefineds(struct obj_file *f)
3038 for (i = 0; i < HASH_BUCKETS; ++i) {
3039 struct obj_symbol *sym;
3040 for (sym = f->symtab[i]; sym; sym = sym->next)
3041 if (sym->secidx == SHN_UNDEF) {
3042 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3043 sym->secidx = SHN_ABS;
3047 bb_error_msg("unresolved symbol %s", sym->name);
3057 static void obj_allocate_commons(struct obj_file *f)
3059 struct common_entry {
3060 struct common_entry *next;
3061 struct obj_symbol *sym;
3062 } *common_head = NULL;
3066 for (i = 0; i < HASH_BUCKETS; ++i) {
3067 struct obj_symbol *sym;
3068 for (sym = f->symtab[i]; sym; sym = sym->next)
3069 if (sym->secidx == SHN_COMMON) {
3070 /* Collect all COMMON symbols and sort them by size so as to
3071 minimize space wasted by alignment requirements. */
3073 struct common_entry **p, *n;
3074 for (p = &common_head; *p; p = &(*p)->next)
3075 if (sym->size <= (*p)->sym->size)
3078 n = alloca(sizeof(*n));
3086 for (i = 1; i < f->local_symtab_size; ++i) {
3087 struct obj_symbol *sym = f->local_symtab[i];
3088 if (sym && sym->secidx == SHN_COMMON) {
3089 struct common_entry **p, *n;
3090 for (p = &common_head; *p; p = &(*p)->next)
3091 if (sym == (*p)->sym)
3093 else if (sym->size < (*p)->sym->size) {
3094 n = alloca(sizeof(*n));
3104 /* Find the bss section. */
3105 for (i = 0; i < f->header.e_shnum; ++i)
3106 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3109 /* If for some reason there hadn't been one, create one. */
3110 if (i == f->header.e_shnum) {
3111 struct obj_section *sec;
3113 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3114 f->sections[i] = sec = arch_new_section();
3115 f->header.e_shnum = i + 1;
3117 memset(sec, 0, sizeof(*sec));
3118 sec->header.sh_type = SHT_PROGBITS;
3119 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3124 /* Allocate the COMMONS. */
3126 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3127 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3128 struct common_entry *c;
3130 for (c = common_head; c; c = c->next) {
3131 ElfW(Addr) align = c->sym->value;
3133 if (align > max_align)
3135 if (bss_size & (align - 1))
3136 bss_size = (bss_size | (align - 1)) + 1;
3139 c->sym->value = bss_size;
3141 bss_size += c->sym->size;
3144 f->sections[i]->header.sh_size = bss_size;
3145 f->sections[i]->header.sh_addralign = max_align;
3149 /* For the sake of patch relocation and parameter initialization,
3150 allocate zeroed data for NOBITS sections now. Note that after
3151 this we cannot assume NOBITS are really empty. */
3152 for (i = 0; i < f->header.e_shnum; ++i) {
3153 struct obj_section *s = f->sections[i];
3154 if (s->header.sh_type == SHT_NOBITS) {
3155 if (s->header.sh_size != 0)
3156 s->contents = memset(xmalloc(s->header.sh_size),
3157 0, s->header.sh_size);
3161 s->header.sh_type = SHT_PROGBITS;
3166 static unsigned long obj_load_size(struct obj_file *f)
3168 unsigned long dot = 0;
3169 struct obj_section *sec;
3171 /* Finalize the positions of the sections relative to one another. */
3173 for (sec = f->load_order; sec; sec = sec->load_next) {
3176 align = sec->header.sh_addralign;
3177 if (align && (dot & (align - 1)))
3178 dot = (dot | (align - 1)) + 1;
3180 sec->header.sh_addr = dot;
3181 dot += sec->header.sh_size;
3187 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3189 int i, n = f->header.e_shnum;
3192 /* Finalize the addresses of the sections. */
3195 for (i = 0; i < n; ++i)
3196 f->sections[i]->header.sh_addr += base;
3198 /* And iterate over all of the relocations. */
3200 for (i = 0; i < n; ++i) {
3201 struct obj_section *relsec, *symsec, *targsec, *strsec;
3202 ElfW(RelM) * rel, *relend;
3206 relsec = f->sections[i];
3207 if (relsec->header.sh_type != SHT_RELM)
3210 symsec = f->sections[relsec->header.sh_link];
3211 targsec = f->sections[relsec->header.sh_info];
3212 strsec = f->sections[symsec->header.sh_link];
3214 rel = (ElfW(RelM) *) relsec->contents;
3215 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3216 symtab = (ElfW(Sym) *) symsec->contents;
3217 strtab = (const char *) strsec->contents;
3219 for (; rel < relend; ++rel) {
3220 ElfW(Addr) value = 0;
3221 struct obj_symbol *intsym = NULL;
3222 unsigned long symndx;
3223 ElfW(Sym) * extsym = 0;
3226 /* Attempt to find a value to use for this relocation. */
3228 symndx = ELF_R_SYM(rel->r_info);
3230 /* Note we've already checked for undefined symbols. */
3232 extsym = &symtab[symndx];
3233 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3234 /* Local symbols we look up in the local table to be sure
3235 we get the one that is really intended. */
3236 intsym = f->local_symtab[symndx];
3238 /* Others we look up in the hash table. */
3240 if (extsym->st_name)
3241 name = strtab + extsym->st_name;
3243 name = f->sections[extsym->st_shndx]->name;
3244 intsym = obj_find_symbol(f, name);
3247 value = obj_symbol_final_value(f, intsym);
3248 intsym->referenced = 1;
3250 #if SHT_RELM == SHT_RELA
3251 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3252 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3253 if (!extsym || !extsym->st_name ||
3254 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3256 value += rel->r_addend;
3260 switch (arch_apply_relocation
3261 (f, targsec, symsec, intsym, rel, value)
3266 case obj_reloc_overflow:
3267 errmsg = "Relocation overflow";
3269 case obj_reloc_dangerous:
3270 errmsg = "Dangerous relocation";
3272 case obj_reloc_unhandled:
3273 errmsg = "Unhandled relocation";
3276 bb_error_msg("%s of type %ld for %s", errmsg,
3277 (long) ELF_R_TYPE(rel->r_info),
3278 strtab + extsym->st_name);
3280 bb_error_msg("%s of type %ld", errmsg,
3281 (long) ELF_R_TYPE(rel->r_info));
3289 /* Finally, take care of the patches. */
3291 if (f->string_patches) {
3292 struct obj_string_patch *p;
3293 struct obj_section *strsec;
3294 ElfW(Addr) strsec_base;
3295 strsec = obj_find_section(f, ".kstrtab");
3296 strsec_base = strsec->header.sh_addr;
3298 for (p = f->string_patches; p; p = p->next) {
3299 struct obj_section *targsec = f->sections[p->reloc_secidx];
3300 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3301 = strsec_base + p->string_offset;
3305 if (f->symbol_patches) {
3306 struct obj_symbol_patch *p;
3308 for (p = f->symbol_patches; p; p = p->next) {
3309 struct obj_section *targsec = f->sections[p->reloc_secidx];
3310 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3311 = obj_symbol_final_value(f, p->sym);
3318 static int obj_create_image(struct obj_file *f, char *image)
3320 struct obj_section *sec;
3321 ElfW(Addr) base = f->baseaddr;
3323 for (sec = f->load_order; sec; sec = sec->load_next) {
3326 if (sec->contents == 0 || sec->header.sh_size == 0)
3329 secimg = image + (sec->header.sh_addr - base);
3331 /* Note that we allocated data for NOBITS sections earlier. */
3332 memcpy(secimg, sec->contents, sec->header.sh_size);
3338 /*======================================================================*/
3340 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3343 ElfW(Shdr) * section_headers;
3347 /* Read the file header. */
3349 f = arch_new_file();
3350 memset(f, 0, sizeof(*f));
3351 f->symbol_cmp = strcmp;
3352 f->symbol_hash = obj_elf_hash;
3353 f->load_order_search_start = &f->load_order;
3355 fseek(fp, 0, SEEK_SET);
3356 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3357 bb_perror_msg("error reading ELF header");
3361 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3362 || f->header.e_ident[EI_MAG1] != ELFMAG1
3363 || f->header.e_ident[EI_MAG2] != ELFMAG2
3364 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3365 bb_error_msg("not an ELF file");
3368 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3369 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3370 ? ELFDATA2MSB : ELFDATA2LSB)
3371 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3372 || !MATCH_MACHINE(f->header.e_machine)) {
3373 bb_error_msg("ELF file not for this architecture");
3376 if (f->header.e_type != ET_REL) {
3377 bb_error_msg("ELF file not a relocatable object");
3381 /* Read the section headers. */
3383 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3384 bb_error_msg("section header size mismatch: %lu != %lu",
3385 (unsigned long) f->header.e_shentsize,
3386 (unsigned long) sizeof(ElfW(Shdr)));
3390 shnum = f->header.e_shnum;
3391 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3392 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3394 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3395 fseek(fp, f->header.e_shoff, SEEK_SET);
3396 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3397 bb_perror_msg("error reading ELF section headers");
3401 /* Read the section data. */
3403 for (i = 0; i < shnum; ++i) {
3404 struct obj_section *sec;
3406 f->sections[i] = sec = arch_new_section();
3407 memset(sec, 0, sizeof(*sec));
3409 sec->header = section_headers[i];
3412 if (sec->header.sh_size) {
3413 switch (sec->header.sh_type) {
3422 if (!loadprogbits) {
3423 sec->contents = NULL;
3430 if (sec->header.sh_size > 0) {
3431 sec->contents = xmalloc(sec->header.sh_size);
3432 fseek(fp, sec->header.sh_offset, SEEK_SET);
3433 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3434 bb_perror_msg("error reading ELF section data");
3438 sec->contents = NULL;
3442 #if SHT_RELM == SHT_REL
3444 bb_error_msg("RELA relocations not supported on this architecture");
3448 bb_error_msg("REL relocations not supported on this architecture");
3453 if (sec->header.sh_type >= SHT_LOPROC) {
3454 /* Assume processor specific section types are debug
3455 info and can safely be ignored. If this is ever not
3456 the case (Hello MIPS?), don't put ifdefs here but
3457 create an arch_load_proc_section(). */
3461 bb_error_msg("can't handle sections of type %ld",
3462 (long) sec->header.sh_type);
3468 /* Do what sort of interpretation as needed by each section. */
3470 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3472 for (i = 0; i < shnum; ++i) {
3473 struct obj_section *sec = f->sections[i];
3474 sec->name = shstrtab + sec->header.sh_name;
3477 for (i = 0; i < shnum; ++i) {
3478 struct obj_section *sec = f->sections[i];
3480 /* .modinfo should be contents only but gcc has no attribute for that.
3481 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3483 if (strcmp(sec->name, ".modinfo") == 0)
3484 sec->header.sh_flags &= ~SHF_ALLOC;
3486 if (sec->header.sh_flags & SHF_ALLOC)
3487 obj_insert_section_load_order(f, sec);
3489 switch (sec->header.sh_type) {
3492 unsigned long nsym, j;
3496 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3497 bb_error_msg("symbol size mismatch: %lu != %lu",
3498 (unsigned long) sec->header.sh_entsize,
3499 (unsigned long) sizeof(ElfW(Sym)));
3503 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3504 strtab = f->sections[sec->header.sh_link]->contents;
3505 sym = (ElfW(Sym) *) sec->contents;
3507 /* Allocate space for a table of local symbols. */
3508 j = f->local_symtab_size = sec->header.sh_info;
3509 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3511 /* Insert all symbols into the hash table. */
3512 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3513 ElfW(Addr) val = sym->st_value;
3516 name = strtab + sym->st_name;
3517 else if (sym->st_shndx < shnum)
3518 name = f->sections[sym->st_shndx]->name;
3521 #if defined(__SH5__)
3523 * For sh64 it is possible that the target of a branch
3524 * requires a mode switch (32 to 16 and back again).
3526 * This is implied by the lsb being set in the target
3527 * address for SHmedia mode and clear for SHcompact.
3529 val |= sym->st_other & 4;
3532 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3539 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3540 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3541 (unsigned long) sec->header.sh_entsize,
3542 (unsigned long) sizeof(ElfW(RelM)));
3546 /* XXX Relocation code from modutils-2.3.19 is not here.
3547 * Why? That's about 20 lines of code from obj/obj_load.c,
3548 * which gets done in a second pass through the sections.
3549 * This BusyBox insmod does similar work in obj_relocate(). */
3556 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3558 * load the unloaded sections directly into the memory allocated by
3559 * kernel for the module
3562 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3564 ElfW(Addr) base = f->baseaddr;
3565 struct obj_section* sec;
3567 for (sec = f->load_order; sec; sec = sec->load_next) {
3569 /* section already loaded? */
3570 if (sec->contents != NULL)
3573 if (sec->header.sh_size == 0)
3576 sec->contents = imagebase + (sec->header.sh_addr - base);
3577 fseek(fp, sec->header.sh_offset, SEEK_SET);
3578 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3579 bb_perror_msg("error reading ELF section data");
3588 static void hide_special_symbols(struct obj_file *f)
3590 static const char *const specials[] = {
3591 SPFX "cleanup_module",
3593 SPFX "kernel_version",
3597 struct obj_symbol *sym;
3598 const char *const *p;
3600 for (p = specials; *p; ++p) {
3601 sym = obj_find_symbol(f, *p);
3603 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3608 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3609 static int obj_gpl_license(struct obj_file *f, const char **license)
3611 struct obj_section *sec;
3612 /* This list must match *exactly* the list of allowable licenses in
3613 * linux/include/linux/module.h. Checking for leading "GPL" will not
3614 * work, somebody will use "GPL sucks, this is proprietary".
3616 static const char *const gpl_licenses[] = {
3619 "GPL and additional rights",
3624 sec = obj_find_section(f, ".modinfo");
3626 const char *value, *ptr, *endptr;
3627 ptr = sec->contents;
3628 endptr = ptr + sec->header.sh_size;
3629 while (ptr < endptr) {
3630 value = strchr(ptr, '=');
3631 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3635 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
3636 if (strcmp(value+1, gpl_licenses[i]) == 0)
3641 if (strchr(ptr, '\0'))
3642 ptr = strchr(ptr, '\0') + 1;
3650 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3651 #define TAINT_PROPRIETORY_MODULE (1<<0)
3652 #define TAINT_FORCED_MODULE (1<<1)
3653 #define TAINT_UNSAFE_SMP (1<<2)
3654 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3656 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3657 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3659 static smallint printed_info;
3664 if (fd < 0 && !kernel_has_tainted)
3665 return; /* New modutils on old kernel */
3666 printf("Warning: loading %s will taint the kernel: %s%s\n",
3667 m_name, text1, text2);
3668 if (!printed_info) {
3669 printf(" See %s for information about tainted modules\n", TAINT_URL);
3673 read(fd, buf, sizeof(buf)-1);
3674 buf[sizeof(buf)-1] = '\0';
3675 oldval = strtoul(buf, NULL, 10);
3676 sprintf(buf, "%d\n", oldval | taint);
3677 write(fd, buf, strlen(buf));
3681 /* Check if loading this module will taint the kernel. */
3682 static void check_tainted_module(struct obj_file *f, char *m_name)
3684 static const char tainted_file[] ALIGN1 = TAINT_FILENAME;
3686 int fd, kernel_has_tainted;
3689 kernel_has_tainted = 1;
3690 fd = open(tainted_file, O_RDWR);
3692 if (errno == ENOENT)
3693 kernel_has_tainted = 0;
3694 else if (errno == EACCES)
3695 kernel_has_tainted = 1;
3697 perror(tainted_file);
3698 kernel_has_tainted = 0;
3702 switch (obj_gpl_license(f, &ptr)) {
3706 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3709 /* The module has a non-GPL license so we pretend that the
3710 * kernel always has a taint flag to get a warning even on
3711 * kernels without the proc flag.
3713 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3716 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3720 if (flag_force_load)
3721 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3726 #else /* FEATURE_CHECK_TAINTED_MODULE */
3727 #define check_tainted_module(x, y) do { } while (0);
3728 #endif /* FEATURE_CHECK_TAINTED_MODULE */
3730 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3731 /* add module source, timestamp, kernel version and a symbol for the
3732 * start of some sections. this info is used by ksymoops to do better
3736 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3738 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3739 return new_get_module_version(f, str);
3740 #else /* FEATURE_INSMOD_VERSION_CHECKING */
3741 strncpy(str, "???", sizeof(str));
3743 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
3746 /* add module source, timestamp, kernel version and a symbol for the
3747 * start of some sections. this info is used by ksymoops to do better
3751 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3754 static const char symprefix[] ALIGN1 = "__insmod_";
3756 struct obj_section *sec;
3757 struct obj_symbol *sym;
3758 char *name, *absolute_filename;
3759 char str[STRVERSIONLEN], real[PATH_MAX];
3760 int i, l, lm_name, lfilename, use_ksymtab, version;
3761 struct stat statbuf;
3763 static const char *section_names[] = {
3771 if (realpath(filename, real)) {
3772 absolute_filename = xstrdup(real);
3774 bb_perror_msg("cannot get realpath for %s", filename);
3775 absolute_filename = xstrdup(filename);
3778 lm_name = strlen(m_name);
3779 lfilename = strlen(absolute_filename);
3781 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3782 * are not to be exported. otherwise leave ksymtab alone for now, the
3783 * "export all symbols" compatibility code will export these symbols later.
3785 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3787 sec = obj_find_section(f, ".this");
3789 /* tag the module header with the object name, last modified
3790 * timestamp and module version. worst case for module version
3791 * is 0xffffff, decimal 16777215. putting all three fields in
3792 * one symbol is less readable but saves kernel space.
3794 l = sizeof(symprefix)+ /* "__insmod_" */
3795 lm_name+ /* module name */
3797 lfilename+ /* object filename */
3799 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3801 8+ /* version in dec */
3804 if (stat(absolute_filename, &statbuf) != 0)
3805 statbuf.st_mtime = 0;
3806 version = get_module_version(f, str); /* -1 if not found */
3807 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3808 symprefix, m_name, absolute_filename,
3809 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3811 sym = obj_add_symbol(f, name, -1,
3812 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3813 sec->idx, sec->header.sh_addr, 0);
3815 new_add_ksymtab(f, sym);
3817 free(absolute_filename);
3818 #ifdef _NOT_SUPPORTED_
3819 /* record where the persistent data is going, same address as previous symbol */
3822 l = sizeof(symprefix)+ /* "__insmod_" */
3823 lm_name+ /* module name */
3825 strlen(f->persist)+ /* data store */
3828 snprintf(name, l, "%s%s_P%s",
3829 symprefix, m_name, f->persist);
3830 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3831 sec->idx, sec->header.sh_addr, 0);
3833 new_add_ksymtab(f, sym);
3835 #endif /* _NOT_SUPPORTED_ */
3836 /* tag the desired sections if size is non-zero */
3838 for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
3839 sec = obj_find_section(f, section_names[i]);
3840 if (sec && sec->header.sh_size) {
3841 l = sizeof(symprefix)+ /* "__insmod_" */
3842 lm_name+ /* module name */
3844 strlen(sec->name)+ /* section name */
3846 8+ /* length in dec */
3849 snprintf(name, l, "%s%s_S%s_L%ld",
3850 symprefix, m_name, sec->name,
3851 (long)sec->header.sh_size);
3852 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3853 sec->idx, sec->header.sh_addr, 0);
3855 new_add_ksymtab(f, sym);
3859 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3861 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3862 static void print_load_map(struct obj_file *f)
3864 struct obj_section *sec;
3865 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3866 struct obj_symbol **all, **p;
3867 int i, nsyms, *loaded;
3868 struct obj_symbol *sym;
3870 /* Report on the section layout. */
3872 printf("Sections: Size %-*s Align\n",
3873 (int) (2 * sizeof(void *)), "Address");
3875 for (sec = f->load_order; sec; sec = sec->load_next) {
3879 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3884 printf("%-15s %08lx %0*lx 2**%d\n",
3886 (long)sec->header.sh_size,
3887 (int) (2 * sizeof(void *)),
3888 (long)sec->header.sh_addr,
3891 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3892 /* Quick reference which section indicies are loaded. */
3894 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3896 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3898 /* Collect the symbols we'll be listing. */
3900 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3901 for (sym = f->symtab[i]; sym; sym = sym->next)
3902 if (sym->secidx <= SHN_HIRESERVE
3903 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3906 all = alloca(nsyms * sizeof(struct obj_symbol *));
3908 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3909 for (sym = f->symtab[i]; sym; sym = sym->next)
3910 if (sym->secidx <= SHN_HIRESERVE
3911 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3914 /* And list them. */
3915 printf("\nSymbols:\n");
3916 for (p = all; p < all + nsyms; ++p) {
3918 unsigned long value;
3921 if (sym->secidx == SHN_ABS) {
3924 } else if (sym->secidx == SHN_UNDEF) {
3928 sec = f->sections[sym->secidx];
3930 if (sec->header.sh_type == SHT_NOBITS)
3932 else if (sec->header.sh_flags & SHF_ALLOC) {
3933 if (sec->header.sh_flags & SHF_EXECINSTR)
3935 else if (sec->header.sh_flags & SHF_WRITE)
3940 value = sym->value + sec->header.sh_addr;
3943 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3944 type = tolower(type);
3946 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3951 #else /* !FEATURE_INSMOD_LOAD_MAP */
3952 void print_load_map(struct obj_file *f);
3955 int insmod_main( int argc, char **argv);
3956 int insmod_main( int argc, char **argv)
3962 unsigned long m_size;
3967 int exit_status = EXIT_FAILURE;
3969 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3970 struct utsname uts_info;
3971 char m_strversion[STRVERSIONLEN];
3972 int m_version, m_crcs;
3974 #if ENABLE_FEATURE_CLEAN_UP
3980 struct utsname myuname;
3982 /* Parse any options */
3983 getopt32(argv, OPTION_STR, &opt_o);
3984 arg1 = argv[optind];
3985 if (option_mask32 & OPT_o) { // -o /* name the output module */
3987 m_name = xstrdup(opt_o);
3994 /* Grab the module name */
3995 tmp1 = xstrdup(arg1);
3996 tmp = basename(tmp1);
3999 if (uname(&myuname) == 0) {
4000 if (myuname.release[0] == '2') {
4001 k_version = myuname.release[2] - '0';
4005 #if ENABLE_FEATURE_2_6_MODULES
4006 if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
4007 && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
4013 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4019 #if ENABLE_FEATURE_2_6_MODULES
4021 m_fullName = xasprintf("%s.ko", tmp);
4024 m_fullName = xasprintf("%s.o", tmp);
4030 tmp1 = 0; /* flag for free(m_name) before exit() */
4033 /* Get a filedesc for the module. Check we we have a complete path */
4034 if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
4035 || (fp = fopen(arg1, "r")) == NULL
4037 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4038 * but do not error out yet if we fail to find it... */
4039 if (k_version) { /* uname succeedd */
4042 char real_module_dir[FILENAME_MAX];
4044 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4045 /* Jump through hoops in case /lib/modules/`uname -r`
4046 * is a symlink. We do not want recursive_action to
4047 * follow symlinks, but we do want to follow the
4048 * /lib/modules/`uname -r` dir, So resolve it ourselves
4049 * if it is a link... */
4050 if (realpath(tmdn, real_module_dir) == NULL)
4053 module_dir = real_module_dir;
4054 recursive_action(module_dir, ACTION_RECURSE,
4055 check_module_name_match, 0, m_fullName, 0);
4059 /* Check if we have found anything yet */
4060 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) {
4061 char module_dir[FILENAME_MAX];
4065 if (realpath (_PATH_MODULES, module_dir) == NULL)
4066 strcpy(module_dir, _PATH_MODULES);
4067 /* No module found under /lib/modules/`uname -r`, this
4068 * time cast the net a bit wider. Search /lib/modules/ */
4069 if (!recursive_action(module_dir, ACTION_RECURSE,
4070 check_module_name_match, 0, m_fullName, 0)
4073 || ((fp = fopen(m_filename, "r")) == NULL)
4075 bb_error_msg("%s: no module by that name found", m_fullName);
4079 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4082 m_filename = xstrdup(arg1);
4085 printf("Using %s\n", m_filename);
4087 #if ENABLE_FEATURE_2_6_MODULES
4088 if (k_version > 4) {
4089 argv[optind] = m_filename;
4091 return insmod_ng_main(argc - optind, argv + optind);
4095 f = obj_load(fp, LOADBITS);
4097 bb_perror_msg_and_die("cannot load the module");
4099 if (get_modinfo_value(f, "kernel_version") == NULL)
4104 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4105 /* Version correspondence? */
4107 if (uname(&uts_info) < 0)
4108 uts_info.release[0] = '\0';
4109 if (m_has_modinfo) {
4110 m_version = new_get_module_version(f, m_strversion);
4111 if (m_version == -1) {
4112 bb_error_msg("cannot find the kernel version the module was "
4118 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4119 bb_error_msg("%skernel-module version mismatch\n"
4120 "\t%s was compiled for kernel version %s\n"
4121 "\twhile this kernel is version %s",
4122 flag_force_load ? "warning: " : "",
4123 m_filename, m_strversion, uts_info.release);
4124 if (!flag_force_load)
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);
4265 int insmod_ng_main(int argc, char **argv)
4271 char *filename, *options;
4277 /* Rest is options */
4278 options = xzalloc(1);
4281 options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
4282 /* Spaces handled by "" pairs, but no way of escaping quotes */
4283 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
4287 /* Any special reason why mmap? It isn't performace critical... */
4291 fd = xopen(filename, O_RDONLY);
4294 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4295 if (map == MAP_FAILED) {
4296 bb_perror_msg_and_die("cannot mmap '%s'", filename);
4299 /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
4302 xread(fd, map, len);
4305 len = MAXINT(ssize_t);
4306 map = xmalloc_open_read_close(filename, &len);
4309 ret = syscall(__NR_init_module, map, len, options);
4311 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
4312 filename, moderror(errno), ret);