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
340 /* Why? static const int MODUTILS_MODULE_H = 1;*/
342 /*======================================================================*/
343 /* For sizeof() which are related to the module platform and not to the
344 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
346 #define tgt_sizeof_char sizeof(char)
347 #define tgt_sizeof_short sizeof(short)
348 #define tgt_sizeof_int sizeof(int)
349 #define tgt_sizeof_long sizeof(long)
350 #define tgt_sizeof_char_p sizeof(char *)
351 #define tgt_sizeof_void_p sizeof(void *)
352 #define tgt_long long
354 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
355 #undef tgt_sizeof_long
356 #undef tgt_sizeof_char_p
357 #undef tgt_sizeof_void_p
361 tgt_sizeof_char_p = 8,
362 tgt_sizeof_void_p = 8
364 #define tgt_long long long
367 /*======================================================================*/
368 /* The structures used in Linux 2.1. */
370 /* Note: new_module_symbol does not use tgt_long intentionally */
371 struct new_module_symbol {
376 struct new_module_persist;
378 struct new_module_ref {
379 unsigned tgt_long dep; /* kernel addresses */
380 unsigned tgt_long ref;
381 unsigned tgt_long next_ref;
385 unsigned tgt_long size_of_struct; /* == sizeof(module) */
386 unsigned tgt_long next;
387 unsigned tgt_long name;
388 unsigned tgt_long size;
391 unsigned tgt_long flags; /* AUTOCLEAN et al */
396 unsigned tgt_long syms;
397 unsigned tgt_long deps;
398 unsigned tgt_long refs;
399 unsigned tgt_long init;
400 unsigned tgt_long cleanup;
401 unsigned tgt_long ex_table_start;
402 unsigned tgt_long ex_table_end;
404 unsigned tgt_long gp;
406 /* Everything after here is extension. */
407 unsigned tgt_long persist_start;
408 unsigned tgt_long persist_end;
409 unsigned tgt_long can_unload;
410 unsigned tgt_long runsize;
411 const char *kallsyms_start; /* All symbols for kernel debugging */
412 const char *kallsyms_end;
413 const char *archdata_start; /* arch specific data for module */
414 const char *archdata_end;
415 const char *kernel_data; /* Reserved for kernel internal use */
419 #define ARCHDATA_SEC_NAME ARCHDATAM
421 #define ARCHDATA_SEC_NAME "__archdata"
423 #define KALLSYMS_SEC_NAME "__kallsyms"
426 struct new_module_info {
433 /* Bits of module.flags. */
437 NEW_MOD_AUTOCLEAN = 4,
439 NEW_MOD_USED_ONCE = 16
442 int init_module(const char *name, const struct new_module *);
443 int query_module(const char *name, int which, void *buf,
444 size_t bufsize, size_t *ret);
446 /* Values for query_module's which. */
455 /*======================================================================*/
456 /* The system calls unchanged between 2.0 and 2.1. */
458 unsigned long create_module(const char *, size_t);
459 int delete_module(const char *);
462 #endif /* module.h */
464 //----------------------------------------------------------------------------
465 //--------end of modutils module.h
466 //----------------------------------------------------------------------------
470 //----------------------------------------------------------------------------
471 //--------modutils obj.h, lines 253-462
472 //----------------------------------------------------------------------------
474 /* Elf object file loading and relocation routines.
475 Copyright 1996, 1997 Linux International.
477 Contributed by Richard Henderson <rth@tamu.edu>
479 This file is part of the Linux modutils.
481 This program is free software; you can redistribute it and/or modify it
482 under the terms of the GNU General Public License as published by the
483 Free Software Foundation; either version 2 of the License, or (at your
484 option) any later version.
486 This program is distributed in the hope that it will be useful, but
487 WITHOUT ANY WARRANTY; without even the implied warranty of
488 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
489 General Public License for more details.
491 You should have received a copy of the GNU General Public License
492 along with this program; if not, write to the Free Software Foundation,
493 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
496 #ifndef MODUTILS_OBJ_H
497 /* Why? static const int MODUTILS_OBJ_H = 1; */
499 /* The relocatable object is manipulated using elfin types. */
506 # if ELFCLASSM == ELFCLASS32
507 # define ElfW(x) Elf32_ ## x
508 # define ELFW(x) ELF32_ ## x
510 # define ElfW(x) Elf64_ ## x
511 # define ELFW(x) ELF64_ ## x
515 /* For some reason this is missing from some ancient C libraries.... */
516 #ifndef ELF32_ST_INFO
517 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
520 #ifndef ELF64_ST_INFO
521 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
524 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
525 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
526 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
527 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
528 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
530 struct obj_string_patch;
531 struct obj_symbol_patch;
538 struct obj_section *load_next;
544 struct obj_symbol *next; /* hash table link */
548 int secidx; /* the defining section index/module */
550 int ksymidx; /* for export to the kernel symtab */
551 int referenced; /* actually used in the link */
554 /* Hardcode the hash table size. We shouldn't be needing so many
555 symbols that we begin to degrade performance, and we get a big win
556 by giving the compiler a constant divisor. */
558 #define HASH_BUCKETS 521
563 struct obj_section **sections;
564 struct obj_section *load_order;
565 struct obj_section **load_order_search_start;
566 struct obj_string_patch *string_patches;
567 struct obj_symbol_patch *symbol_patches;
568 int (*symbol_cmp)(const char *, const char *);
569 unsigned long (*symbol_hash)(const char *);
570 unsigned long local_symtab_size;
571 struct obj_symbol **local_symtab;
572 struct obj_symbol *symtab[HASH_BUCKETS];
582 struct obj_string_patch {
583 struct obj_string_patch *next;
585 ElfW(Addr) reloc_offset;
586 ElfW(Addr) string_offset;
589 struct obj_symbol_patch {
590 struct obj_symbol_patch *next;
592 ElfW(Addr) reloc_offset;
593 struct obj_symbol *sym;
597 /* Generic object manipulation routines. */
599 static unsigned long obj_elf_hash(const char *);
601 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
603 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
606 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
607 struct obj_symbol *sym);
609 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
610 static void obj_set_symbol_compare(struct obj_file *f,
611 int (*cmp)(const char *, const char *),
612 unsigned long (*hash)(const char *));
615 static struct obj_section *obj_find_section (struct obj_file *f,
618 static void obj_insert_section_load_order (struct obj_file *f,
619 struct obj_section *sec);
621 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
626 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
631 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
633 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
636 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
637 struct obj_symbol *sym);
639 static int obj_check_undefineds(struct obj_file *f);
641 static void obj_allocate_commons(struct obj_file *f);
643 static unsigned long obj_load_size (struct obj_file *f);
645 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
647 static struct obj_file *obj_load(FILE *f, int loadprogbits);
649 static int obj_create_image (struct obj_file *f, char *image);
651 /* Architecture specific manipulation routines. */
653 static struct obj_file *arch_new_file (void);
655 static struct obj_section *arch_new_section (void);
657 static struct obj_symbol *arch_new_symbol (void);
659 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
660 struct obj_section *targsec,
661 struct obj_section *symsec,
662 struct obj_symbol *sym,
663 ElfW(RelM) *rel, ElfW(Addr) value);
665 static void arch_create_got (struct obj_file *f);
666 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
667 static int obj_gpl_license(struct obj_file *f, const char **license);
668 #endif /* FEATURE_CHECK_TAINTED_MODULE */
670 //----------------------------------------------------------------------------
671 //--------end of modutils obj.h
672 //----------------------------------------------------------------------------
675 /* SPFX is always a string, so it can be concatenated to string constants. */
677 #define SPFX SYMBOL_PREFIX
683 #define _PATH_MODULES "/lib/modules"
684 enum { STRVERSIONLEN = 64 };
686 /*======================================================================*/
688 #define OPTION_STR "sLo:fkvqx" USE_FEATURE_INSMOD_LOAD_MAP("m")
690 OPT_s = 0x1, // -s /* log to syslog */
691 /* Not supported but kernel needs this for request_module(),
692 as this calls: modprobe -k -s -- <module>
693 so silently ignore this flag */
694 OPT_L = 0x2, // -L /* Stub warning */
695 /* Compatibility with modprobe.
696 In theory, this does locking, but we don't do
697 that. So be careful and plan your life around not
698 loading the same module 50 times concurrently. */
699 OPT_o = 0x4, // -o /* name the output module */
700 OPT_f = 0x8, // -f /* force loading */
701 OPT_k = 0x10, // -k /* module loaded by kerneld, auto-cleanable */
702 OPT_v = 0x20, // -v /* verbose output */
703 OPT_q = 0x40, // -q /* silent */
704 OPT_x = 0x80, // -x /* do not export externs */
705 OPT_m = 0x100, // -m /* print module load map */
707 #define flag_force_load (option_mask32 & OPT_f)
708 #define flag_autoclean (option_mask32 & OPT_k)
709 #define flag_verbose (option_mask32 & OPT_v)
710 #define flag_quiet (option_mask32 & OPT_q)
711 #define flag_noexport (option_mask32 & OPT_x)
712 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
713 #define flag_print_load_map (option_mask32 & OPT_m)
715 #define flag_print_load_map 0
718 /*======================================================================*/
720 #if defined(USE_LIST)
722 struct arch_list_entry
724 struct arch_list_entry *next;
725 LIST_ARCHTYPE addend;
732 #if defined(USE_SINGLE)
734 struct arch_single_entry
743 #if defined(__mips__)
746 struct mips_hi16 *next;
753 struct obj_file root;
754 #if defined(USE_PLT_ENTRIES)
755 struct obj_section *plt;
757 #if defined(USE_GOT_ENTRIES)
758 struct obj_section *got;
760 #if defined(__mips__)
761 struct mips_hi16 *mips_hi16_list;
766 struct obj_symbol root;
767 #if defined(USE_PLT_ENTRIES)
768 #if defined(USE_PLT_LIST)
769 struct arch_list_entry *pltent;
771 struct arch_single_entry pltent;
774 #if defined(USE_GOT_ENTRIES)
775 struct arch_single_entry gotent;
780 struct external_module {
785 struct new_module_symbol *syms;
788 static struct new_module_symbol *ksyms;
789 static size_t nksyms;
791 static struct external_module *ext_modules;
792 static int n_ext_modules;
793 static int n_ext_modules_used;
794 extern int delete_module(const char *);
796 static char *m_filename;
797 static char *m_fullName;
800 /*======================================================================*/
803 static int check_module_name_match(const char *filename, struct stat *statbuf,
804 void *userdata, int depth)
806 char *fullname = (char *) userdata;
808 if (fullname[0] == '\0')
811 char *tmp, *tmp1 = xstrdup(filename);
812 tmp = bb_get_last_path_component(tmp1);
813 if (strcmp(tmp, fullname) == 0) {
815 /* Stop searching if we find a match */
816 m_filename = xstrdup(filename);
825 /*======================================================================*/
827 static struct obj_file *arch_new_file(void)
830 f = xmalloc(sizeof(*f));
832 memset(f, 0, sizeof(*f));
837 static struct obj_section *arch_new_section(void)
839 return xmalloc(sizeof(struct obj_section));
842 static struct obj_symbol *arch_new_symbol(void)
844 struct arch_symbol *sym;
845 sym = xmalloc(sizeof(*sym));
847 memset(sym, 0, sizeof(*sym));
852 static enum obj_reloc
853 arch_apply_relocation(struct obj_file *f,
854 struct obj_section *targsec,
855 struct obj_section *symsec,
856 struct obj_symbol *sym,
857 ElfW(RelM) *rel, ElfW(Addr) v)
859 struct arch_file *ifile = (struct arch_file *) f;
860 enum obj_reloc ret = obj_reloc_ok;
861 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
862 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
863 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
864 struct arch_symbol *isym = (struct arch_symbol *) sym;
866 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
867 #if defined(USE_GOT_ENTRIES)
868 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
871 #if defined(USE_PLT_ENTRIES)
872 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
874 # if defined(USE_PLT_LIST)
875 struct arch_list_entry *pe;
877 struct arch_single_entry *pe;
881 switch (ELF_R_TYPE(rel->r_info)) {
896 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
897 * (which is .got) similar to branch,
898 * but is full 32 bits relative */
907 case R_ARM_GOTOFF: /* address relative to the got */
911 #elif defined(__cris__)
917 /* CRIS keeps the relocation value in the r_addend field and
918 * should not use whats in *loc at all
923 #elif defined(__H8300H__) || defined(__H8300S__)
926 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
927 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
938 if ((ElfW(Sword))v > 0x7fff ||
939 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
940 ret = obj_reloc_overflow;
942 *(unsigned short *)loc = v;
946 if ((ElfW(Sword))v > 0x7f ||
947 (ElfW(Sword))v < -(ElfW(Sword))0x80)
948 ret = obj_reloc_overflow;
950 *(unsigned char *)loc = v;
953 #elif defined(__i386__)
987 #elif defined (__microblaze__)
988 case R_MICROBLAZE_NONE:
989 case R_MICROBLAZE_64_NONE:
990 case R_MICROBLAZE_32_SYM_OP_SYM:
991 case R_MICROBLAZE_32_PCREL:
994 case R_MICROBLAZE_64_PCREL: {
995 /* dot is the address of the current instruction.
996 * v is the target symbol address.
997 * So we need to extract the offset in the code,
998 * adding v, then subtrating the current address
999 * of this instruction.
1000 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
1003 /* Get split offset stored in code */
1004 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
1007 /* Adjust relative offset. -4 adjustment required
1008 * because dot points to the IMM insn, but branch
1009 * is computed relative to the branch instruction itself.
1011 temp += v - dot - 4;
1013 /* Store back into code */
1014 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
1015 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
1020 case R_MICROBLAZE_32:
1024 case R_MICROBLAZE_64: {
1025 /* Get split pointer stored in code */
1026 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
1029 /* Add reloc offset */
1032 /* Store back into code */
1033 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
1034 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
1039 case R_MICROBLAZE_32_PCREL_LO:
1040 case R_MICROBLAZE_32_LO:
1041 case R_MICROBLAZE_SRO32:
1042 case R_MICROBLAZE_SRW32:
1043 ret = obj_reloc_unhandled;
1046 #elif defined(__mc68000__)
1057 ret = obj_reloc_overflow;
1064 ret = obj_reloc_overflow;
1071 if ((ElfW(Sword))v > 0x7f ||
1072 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1073 ret = obj_reloc_overflow;
1080 if ((ElfW(Sword))v > 0x7fff ||
1081 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1082 ret = obj_reloc_overflow;
1088 *(int *)loc = v - dot;
1091 case R_68K_GLOB_DAT:
1092 case R_68K_JMP_SLOT:
1096 case R_68K_RELATIVE:
1097 *(int *)loc += f->baseaddr;
1103 # ifdef R_68K_GOTOFF
1109 #elif defined(__mips__)
1120 ret = obj_reloc_dangerous;
1121 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1122 ret = obj_reloc_overflow;
1124 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1130 struct mips_hi16 *n;
1132 /* We cannot relocate this one now because we don't know the value
1133 of the carry we need to add. Save the information, and let LO16
1134 do the actual relocation. */
1135 n = xmalloc(sizeof *n);
1138 n->next = ifile->mips_hi16_list;
1139 ifile->mips_hi16_list = n;
1145 unsigned long insnlo = *loc;
1146 ElfW(Addr) val, vallo;
1148 /* Sign extend the addend we extract from the lo insn. */
1149 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1151 if (ifile->mips_hi16_list != NULL) {
1152 struct mips_hi16 *l;
1154 l = ifile->mips_hi16_list;
1156 struct mips_hi16 *next;
1159 /* Do the HI16 relocation. Note that we actually don't
1160 need to know anything about the LO16 itself, except where
1161 to find the low 16 bits of the addend needed by the LO16. */
1164 ((insn & 0xffff) << 16) +
1168 /* Account for the sign extension that will happen in the
1175 insn = (insn & ~0xffff) | val;
1183 ifile->mips_hi16_list = NULL;
1186 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1188 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1193 #elif defined(__nios2__)
1198 case R_NIOS2_BFD_RELOC_32:
1202 case R_NIOS2_BFD_RELOC_16:
1204 ret = obj_reloc_overflow;
1209 case R_NIOS2_BFD_RELOC_8:
1211 ret = obj_reloc_overflow;
1220 if ((Elf32_Sword)v > 0x7fff ||
1221 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1222 ret = obj_reloc_overflow;
1226 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1236 ret = obj_reloc_overflow;
1240 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1245 case R_NIOS2_PCREL16:
1250 if ((Elf32_Sword)v > 0x7fff ||
1251 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1252 ret = obj_reloc_overflow;
1256 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1262 Elf32_Addr word, gp;
1264 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1266 if ((Elf32_Sword)v > 0x7fff ||
1267 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1268 ret = obj_reloc_overflow;
1272 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1276 case R_NIOS2_CALL26:
1278 ret = obj_reloc_dangerous;
1279 if ((v >> 28) != (dot >> 28))
1280 ret = obj_reloc_overflow;
1281 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1289 ret = obj_reloc_overflow;
1292 word = *loc & ~0x7c0;
1293 *loc = word | ((v & 0x1f) << 6);
1302 ret = obj_reloc_overflow;
1305 word = *loc & ~0xfc0;
1306 *loc = word | ((v & 0x3f) << 6);
1315 ret = obj_reloc_overflow;
1318 word = *loc & ~0x3fc0;
1319 *loc = word | ((v & 0xff) << 6);
1328 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1338 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1343 case R_NIOS2_HIADJ16:
1345 Elf32_Addr word1, word2;
1348 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1349 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1354 #elif defined(__powerpc64__)
1355 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1357 #elif defined(__powerpc__)
1359 case R_PPC_ADDR16_HA:
1360 *(unsigned short *)loc = (v + 0x8000) >> 16;
1363 case R_PPC_ADDR16_HI:
1364 *(unsigned short *)loc = v >> 16;
1367 case R_PPC_ADDR16_LO:
1368 *(unsigned short *)loc = v;
1382 #elif defined(__s390__)
1385 *(unsigned int *) loc += v;
1388 *(unsigned short *) loc += v;
1391 *(unsigned char *) loc += v;
1395 *(unsigned int *) loc += v - dot;
1398 *(unsigned short *) loc += (v - dot) >> 1;
1401 *(unsigned short *) loc += v - dot;
1405 case R_390_PLT16DBL:
1406 /* find the plt entry and initialize it. */
1407 pe = (struct arch_single_entry *) &isym->pltent;
1408 if (pe->inited == 0) {
1409 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1410 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1412 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1419 /* Insert relative distance to target. */
1420 v = plt + pe->offset - dot;
1421 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1422 *(unsigned int *) loc = (unsigned int) v;
1423 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1424 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1427 case R_390_GLOB_DAT:
1428 case R_390_JMP_SLOT:
1432 case R_390_RELATIVE:
1433 *loc += f->baseaddr;
1437 *(unsigned long *) loc += got - dot;
1443 if (!isym->gotent.inited)
1445 isym->gotent.inited = 1;
1446 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1448 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1449 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1450 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1451 *(unsigned short *) loc += isym->gotent.offset;
1452 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1453 *(unsigned int *) loc += isym->gotent.offset;
1456 # ifndef R_390_GOTOFF32
1457 # define R_390_GOTOFF32 R_390_GOTOFF
1459 case R_390_GOTOFF32:
1463 #elif defined(__sh__)
1486 *loc = f->baseaddr + rel->r_addend;
1490 *loc = got - dot + rel->r_addend;
1500 # if defined(__SH5__)
1501 case R_SH_IMM_MEDLOW16:
1502 case R_SH_IMM_LOW16:
1506 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1510 * movi and shori have the format:
1512 * | op | imm | reg | reserved |
1513 * 31..26 25..10 9.. 4 3 .. 0
1515 * so we simply mask and or in imm.
1517 word = *loc & ~0x3fffc00;
1518 word |= (v & 0xffff) << 10;
1525 case R_SH_IMM_MEDLOW16_PCREL:
1526 case R_SH_IMM_LOW16_PCREL:
1530 word = *loc & ~0x3fffc00;
1534 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1537 word |= (v & 0xffff) << 10;
1543 # endif /* __SH5__ */
1545 #elif defined (__v850e__)
1551 /* We write two shorts instead of a long because even
1552 32-bit insns only need half-word alignment, but
1553 32-bit data needs to be long-word aligned. */
1554 v += ((unsigned short *)loc)[0];
1555 v += ((unsigned short *)loc)[1] << 16;
1556 ((unsigned short *)loc)[0] = v & 0xffff;
1557 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1560 case R_V850_22_PCREL:
1563 #elif defined(__x86_64__)
1573 *(unsigned int *) loc += v;
1576 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1577 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1582 *(signed int *) loc += v;
1586 *(unsigned short *) loc += v;
1590 *(unsigned char *) loc += v;
1594 *(unsigned int *) loc += v - dot;
1598 *(unsigned short *) loc += v - dot;
1602 *(unsigned char *) loc += v - dot;
1605 case R_X86_64_GLOB_DAT:
1606 case R_X86_64_JUMP_SLOT:
1610 case R_X86_64_RELATIVE:
1611 *loc += f->baseaddr;
1614 case R_X86_64_GOT32:
1615 case R_X86_64_GOTPCREL:
1618 if (!isym->gotent.reloc_done)
1620 isym->gotent.reloc_done = 1;
1621 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1623 /* XXX are these really correct? */
1624 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1625 *(unsigned int *) loc += v + isym->gotent.offset;
1627 *loc += isym->gotent.offset;
1632 # warning "no idea how to handle relocations on your arch"
1636 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1637 ret = obj_reloc_unhandled;
1640 #if defined(USE_PLT_ENTRIES)
1644 /* find the plt entry and initialize it if necessary */
1646 #if defined(USE_PLT_LIST)
1647 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1654 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1656 /* generate some machine code */
1658 #if defined(__arm__)
1659 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1660 ip[1] = v; /* sym@ */
1662 #if defined(__powerpc__)
1663 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1664 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1665 ip[2] = 0x7d6903a6; /* mtctr r11 */
1666 ip[3] = 0x4e800420; /* bctr */
1668 #if defined (__v850e__)
1669 /* We have to trash a register, so we assume that any control
1670 transfer more than 21-bits away must be a function call
1671 (so we can use a call-clobbered register). */
1672 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1673 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1678 /* relative distance to target */
1680 /* if the target is too far away.... */
1681 #if defined (__arm__) || defined (__powerpc__)
1682 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1683 #elif defined (__v850e__)
1684 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1686 /* go via the plt */
1687 v = plt + pe->offset - dot;
1689 #if defined (__v850e__)
1694 ret = obj_reloc_dangerous;
1696 /* merge the offset into the instruction. */
1697 #if defined(__arm__)
1698 /* Convert to words. */
1701 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1703 #if defined(__powerpc__)
1704 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1706 #if defined (__v850e__)
1707 /* We write two shorts instead of a long because even 32-bit insns
1708 only need half-word alignment, but the 32-bit data write needs
1709 to be long-word aligned. */
1710 ((unsigned short *)loc)[0] =
1711 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1712 | ((v >> 16) & 0x3f); /* offs high part */
1713 ((unsigned short *)loc)[1] =
1714 (v & 0xffff); /* offs low part */
1717 #endif /* USE_PLT_ENTRIES */
1719 #if defined(USE_GOT_ENTRIES)
1722 /* needs an entry in the .got: set it, once */
1723 if (!isym->gotent.inited) {
1724 isym->gotent.inited = 1;
1725 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1727 /* make the reloc with_respect_to_.got */
1729 *loc += isym->gotent.offset + rel->r_addend;
1730 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1731 *loc += isym->gotent.offset;
1735 #endif /* USE_GOT_ENTRIES */
1742 #if defined(USE_LIST)
1744 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1745 int offset, int size)
1747 struct arch_list_entry *pe;
1749 for (pe = *list; pe != NULL; pe = pe->next) {
1750 if (pe->addend == rel->r_addend) {
1756 pe = xmalloc(sizeof(struct arch_list_entry));
1758 pe->addend = rel->r_addend;
1759 pe->offset = offset;
1769 #if defined(USE_SINGLE)
1771 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1772 int offset, int size)
1774 if (single->allocated == 0) {
1775 single->allocated = 1;
1776 single->offset = offset;
1785 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1787 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1788 int offset, int size)
1790 struct obj_section *myrelsec = obj_find_section(f, name);
1797 obj_extend_section(myrelsec, offset);
1799 myrelsec = obj_create_alloced_section(f, name,
1808 static void arch_create_got(struct obj_file *f)
1810 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1811 struct arch_file *ifile = (struct arch_file *) f;
1813 #if defined(USE_GOT_ENTRIES)
1814 int got_offset = 0, got_needed = 0, got_allocate;
1816 #if defined(USE_PLT_ENTRIES)
1817 int plt_offset = 0, plt_needed = 0, plt_allocate;
1819 struct obj_section *relsec, *symsec, *strsec;
1820 ElfW(RelM) *rel, *relend;
1821 ElfW(Sym) *symtab, *extsym;
1822 const char *strtab, *name;
1823 struct arch_symbol *intsym;
1825 for (i = 0; i < f->header.e_shnum; ++i) {
1826 relsec = f->sections[i];
1827 if (relsec->header.sh_type != SHT_RELM)
1830 symsec = f->sections[relsec->header.sh_link];
1831 strsec = f->sections[symsec->header.sh_link];
1833 rel = (ElfW(RelM) *) relsec->contents;
1834 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1835 symtab = (ElfW(Sym) *) symsec->contents;
1836 strtab = (const char *) strsec->contents;
1838 for (; rel < relend; ++rel) {
1839 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1841 #if defined(USE_GOT_ENTRIES)
1844 #if defined(USE_PLT_ENTRIES)
1848 switch (ELF_R_TYPE(rel->r_info)) {
1849 #if defined(__arm__)
1864 #elif defined(__i386__)
1874 #elif defined(__powerpc__)
1879 #elif defined(__mc68000__)
1890 #elif defined(__sh__)
1900 #elif defined (__v850e__)
1901 case R_V850_22_PCREL:
1910 if (extsym->st_name != 0) {
1911 name = strtab + extsym->st_name;
1913 name = f->sections[extsym->st_shndx]->name;
1915 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1916 #if defined(USE_GOT_ENTRIES)
1918 got_offset += arch_single_init(
1919 rel, &intsym->gotent,
1920 got_offset, GOT_ENTRY_SIZE);
1925 #if defined(USE_PLT_ENTRIES)
1927 #if defined(USE_PLT_LIST)
1928 plt_offset += arch_list_add(
1929 rel, &intsym->pltent,
1930 plt_offset, PLT_ENTRY_SIZE);
1932 plt_offset += arch_single_init(
1933 rel, &intsym->pltent,
1934 plt_offset, PLT_ENTRY_SIZE);
1942 #if defined(USE_GOT_ENTRIES)
1944 ifile->got = arch_xsect_init(f, ".got", got_offset,
1949 #if defined(USE_PLT_ENTRIES)
1951 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1956 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1959 /*======================================================================*/
1961 /* Standard ELF hash function. */
1962 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1964 unsigned long h = 0;
1971 if ((g = (h & 0xf0000000)) != 0) {
1980 static unsigned long obj_elf_hash(const char *name)
1982 return obj_elf_hash_n(name, strlen(name));
1985 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1986 /* String comparison for non-co-versioned kernel and module. */
1988 static int ncv_strcmp(const char *a, const char *b)
1990 size_t alen = strlen(a), blen = strlen(b);
1992 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1993 return strncmp(a, b, alen);
1994 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1995 return strncmp(a, b, blen);
1997 return strcmp(a, b);
2000 /* String hashing for non-co-versioned kernel and module. Here
2001 we are simply forced to drop the crc from the hash. */
2003 static unsigned long ncv_symbol_hash(const char *str)
2005 size_t len = strlen(str);
2006 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
2008 return obj_elf_hash_n(str, len);
2012 obj_set_symbol_compare(struct obj_file *f,
2013 int (*cmp) (const char *, const char *),
2014 unsigned long (*hash) (const char *))
2017 f->symbol_cmp = cmp;
2019 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
2022 f->symbol_hash = hash;
2024 memcpy(tmptab, f->symtab, sizeof(tmptab));
2025 memset(f->symtab, 0, sizeof(f->symtab));
2027 for (i = 0; i < HASH_BUCKETS; ++i)
2028 for (sym = tmptab[i]; sym; sym = next) {
2029 unsigned long h = hash(sym->name) % HASH_BUCKETS;
2031 sym->next = f->symtab[h];
2037 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2039 static struct obj_symbol *
2040 obj_add_symbol(struct obj_file *f, const char *name,
2041 unsigned long symidx, int info,
2042 int secidx, ElfW(Addr) value,
2045 struct obj_symbol *sym;
2046 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2047 int n_type = ELF_ST_TYPE(info);
2048 int n_binding = ELF_ST_BIND(info);
2050 for (sym = f->symtab[hash]; sym; sym = sym->next)
2051 if (f->symbol_cmp(sym->name, name) == 0) {
2052 int o_secidx = sym->secidx;
2053 int o_info = sym->info;
2054 int o_type = ELF_ST_TYPE(o_info);
2055 int o_binding = ELF_ST_BIND(o_info);
2057 /* A redefinition! Is it legal? */
2059 if (secidx == SHN_UNDEF)
2061 else if (o_secidx == SHN_UNDEF)
2063 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2064 /* Cope with local and global symbols of the same name
2065 in the same object file, as might have been created
2066 by ld -r. The only reason locals are now seen at this
2067 level at all is so that we can do semi-sensible things
2070 struct obj_symbol *nsym, **p;
2072 nsym = arch_new_symbol();
2073 nsym->next = sym->next;
2076 /* Excise the old (local) symbol from the hash chain. */
2077 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2081 } else if (n_binding == STB_LOCAL) {
2082 /* Another symbol of the same name has already been defined.
2083 Just add this to the local table. */
2084 sym = arch_new_symbol();
2087 f->local_symtab[symidx] = sym;
2089 } else if (n_binding == STB_WEAK)
2091 else if (o_binding == STB_WEAK)
2093 /* Don't unify COMMON symbols with object types the programmer
2095 else if (secidx == SHN_COMMON
2096 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2098 else if (o_secidx == SHN_COMMON
2099 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2102 /* Don't report an error if the symbol is coming from
2103 the kernel or some external module. */
2104 if (secidx <= SHN_HIRESERVE)
2105 bb_error_msg("%s multiply defined", name);
2110 /* Completely new symbol. */
2111 sym = arch_new_symbol();
2112 sym->next = f->symtab[hash];
2113 f->symtab[hash] = sym;
2116 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2117 if (symidx >= f->local_symtab_size)
2118 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2119 name, (long) symidx, (long) f->local_symtab_size);
2121 f->local_symtab[symidx] = sym;
2128 sym->secidx = secidx;
2134 static struct obj_symbol *
2135 obj_find_symbol(struct obj_file *f, const char *name)
2137 struct obj_symbol *sym;
2138 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2140 for (sym = f->symtab[hash]; sym; sym = sym->next)
2141 if (f->symbol_cmp(sym->name, name) == 0)
2147 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2150 if (sym->secidx >= SHN_LORESERVE)
2153 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2155 /* As a special case, a NULL sym has value zero. */
2160 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2162 int i, n = f->header.e_shnum;
2164 for (i = 0; i < n; ++i)
2165 if (strcmp(f->sections[i]->name, name) == 0)
2166 return f->sections[i];
2171 static int obj_load_order_prio(struct obj_section *a)
2173 unsigned long af, ac;
2175 af = a->header.sh_flags;
2178 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2179 strcmp(a->name + 5, ".init"))
2183 if (!(af & SHF_WRITE))
2185 if (af & SHF_EXECINSTR)
2187 if (a->header.sh_type != SHT_NOBITS)
2194 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2196 struct obj_section **p;
2197 int prio = obj_load_order_prio(sec);
2198 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2199 if (obj_load_order_prio(*p) < prio)
2201 sec->load_next = *p;
2205 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2207 unsigned long align,
2210 int newidx = f->header.e_shnum++;
2211 struct obj_section *sec;
2213 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2214 f->sections[newidx] = sec = arch_new_section();
2216 memset(sec, 0, sizeof(*sec));
2217 sec->header.sh_type = SHT_PROGBITS;
2218 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2219 sec->header.sh_size = size;
2220 sec->header.sh_addralign = align;
2224 sec->contents = xmalloc(size);
2226 obj_insert_section_load_order(f, sec);
2231 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2233 unsigned long align,
2236 int newidx = f->header.e_shnum++;
2237 struct obj_section *sec;
2239 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2240 f->sections[newidx] = sec = arch_new_section();
2242 memset(sec, 0, sizeof(*sec));
2243 sec->header.sh_type = SHT_PROGBITS;
2244 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2245 sec->header.sh_size = size;
2246 sec->header.sh_addralign = align;
2250 sec->contents = xmalloc(size);
2252 sec->load_next = f->load_order;
2253 f->load_order = sec;
2254 if (f->load_order_search_start == &f->load_order)
2255 f->load_order_search_start = &sec->load_next;
2260 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2262 unsigned long oldsize = sec->header.sh_size;
2264 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2266 return sec->contents + oldsize;
2270 /* Conditionally add the symbols from the given symbol set to the
2274 add_symbols_from( struct obj_file *f,
2275 int idx, struct new_module_symbol *syms, size_t nsyms)
2277 struct new_module_symbol *s;
2280 #ifdef SYMBOL_PREFIX
2282 size_t name_alloced_size = 0;
2284 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2287 gpl = obj_gpl_license(f, NULL) == 0;
2289 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2290 /* Only add symbols that are already marked external.
2291 If we override locals we may cause problems for
2292 argument initialization. We will also create a false
2293 dependency on the module. */
2294 struct obj_symbol *sym;
2297 /* GPL licensed modules can use symbols exported with
2298 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2299 * exported names. Non-GPL modules never see any GPLONLY_
2300 * symbols so they cannot fudge it by adding the prefix on
2303 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2304 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2311 name = (char *)s->name;
2313 #ifdef SYMBOL_PREFIX
2314 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2315 kernel exports `C names', but module object files
2316 reference `linker names'). */
2317 size_t extra = sizeof SYMBOL_PREFIX;
2318 size_t name_size = strlen (name) + extra;
2319 if (name_size > name_alloced_size) {
2320 name_alloced_size = name_size * 2;
2321 name_buf = alloca (name_alloced_size);
2323 strcpy (name_buf, SYMBOL_PREFIX);
2324 strcpy (name_buf + extra - 1, name);
2326 #endif /* SYMBOL_PREFIX */
2328 sym = obj_find_symbol(f, name);
2329 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2330 #ifdef SYMBOL_PREFIX
2331 /* Put NAME_BUF into more permanent storage. */
2332 name = xmalloc (name_size);
2333 strcpy (name, name_buf);
2335 sym = obj_add_symbol(f, name, -1,
2336 ELF_ST_INFO(STB_GLOBAL,
2339 /* Did our symbol just get installed? If so, mark the
2340 module as "used". */
2341 if (sym->secidx == idx)
2349 static void add_kernel_symbols(struct obj_file *f)
2351 struct external_module *m;
2354 /* Add module symbols first. */
2356 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2358 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2359 m->nsyms)) m->used = 1, ++nused;
2361 n_ext_modules_used = nused;
2363 /* And finally the symbols from the kernel proper. */
2366 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2369 static char *get_modinfo_value(struct obj_file *f, const char *key)
2371 struct obj_section *sec;
2372 char *p, *v, *n, *ep;
2373 size_t klen = strlen(key);
2375 sec = obj_find_section(f, ".modinfo");
2379 ep = p + sec->header.sh_size;
2382 n = strchr(p, '\0');
2384 if (p + klen == v && strncmp(p, key, klen) == 0)
2387 if (p + klen == n && strcmp(p, key) == 0)
2397 /*======================================================================*/
2398 /* Functions relating to module loading after 2.1.18. */
2401 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2404 char *p, *q, *key, *sym_name;
2405 struct obj_symbol *sym;
2406 char *contents, *loc;
2410 if ((q = strchr(p, '=')) == NULL) {
2415 key = alloca(q - p + 6);
2416 memcpy(key, "parm_", 5);
2417 memcpy(key + 5, p, q - p);
2420 p = get_modinfo_value(f, key);
2423 bb_error_msg("invalid parameter %s", key);
2427 #ifdef SYMBOL_PREFIX
2428 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2429 strcpy (sym_name, SYMBOL_PREFIX);
2430 strcat (sym_name, key);
2434 sym = obj_find_symbol(f, sym_name);
2436 /* Also check that the parameter was not resolved from the kernel. */
2437 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2438 bb_error_msg("symbol for parameter %s not found", key);
2443 min = strtoul(p, &p, 10);
2445 max = strtoul(p + 1, &p, 10);
2451 contents = f->sections[sym->secidx]->contents;
2452 loc = contents + sym->value;
2456 if ((*p == 's') || (*p == 'c')) {
2459 /* Do C quoting if we begin with a ", else slurp the lot. */
2463 str = alloca(strlen(q));
2464 for (r = str, q++; *q != '"'; ++q, ++r) {
2466 bb_error_msg("improperly terminated string argument for %s",
2469 } else if (*q == '\\')
2503 if (q[1] >= '0' && q[1] <= '7') {
2504 c = (c * 8) + *++q - '0';
2505 if (q[1] >= '0' && q[1] <= '7')
2506 c = (c * 8) + *++q - '0';
2523 /* In this case, the string is not quoted. We will break
2524 it using the coma (like for ints). If the user wants to
2525 include comas in a string, he just has to quote it */
2527 /* Search the next coma */
2531 if (r != (char *) NULL) {
2532 /* Recopy the current field */
2533 str = alloca(r - q + 1);
2534 memcpy(str, q, r - q);
2536 /* I don't know if it is useful, as the previous case
2537 doesn't nul terminate the string ??? */
2540 /* Keep next fields */
2551 obj_string_patch(f, sym->secidx, loc - contents, str);
2552 loc += tgt_sizeof_char_p;
2554 /* Array of chars (in fact, matrix !) */
2555 unsigned long charssize; /* size of each member */
2557 /* Get the size of each member */
2558 /* Probably we should do that outside the loop ? */
2559 if (!isdigit(*(p + 1))) {
2560 bb_error_msg("parameter type 'c' for %s must be followed by"
2561 " the maximum size", key);
2564 charssize = strtoul(p + 1, (char **) NULL, 10);
2567 if (strlen(str) >= charssize) {
2568 bb_error_msg("string too long for %s (max %ld)", key,
2573 /* Copy to location */
2574 strcpy((char *) loc, str);
2578 long v = strtoul(q, &q, 0);
2585 loc += tgt_sizeof_short;
2589 loc += tgt_sizeof_int;
2593 loc += tgt_sizeof_long;
2597 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2612 goto retry_end_of_value;
2616 bb_error_msg("too many values for %s (max %d)", key, max);
2623 bb_error_msg("invalid argument syntax for %s", key);
2630 bb_error_msg("too few values for %s (min %d)", key, min);
2640 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2641 static int new_is_module_checksummed(struct obj_file *f)
2643 const char *p = get_modinfo_value(f, "using_checksums");
2650 /* Get the module's kernel version in the canonical integer form. */
2653 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2658 p = get_modinfo_value(f, "kernel_version");
2661 safe_strncpy(str, p, STRVERSIONLEN);
2663 a = strtoul(p, &p, 10);
2666 b = strtoul(p + 1, &p, 10);
2669 c = strtoul(p + 1, &q, 10);
2673 return a << 16 | b << 8 | c;
2676 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2679 /* Fetch the loaded modules, and all currently exported symbols. */
2681 static int new_get_kernel_symbols(void)
2683 char *module_names, *mn;
2684 struct external_module *modules, *m;
2685 struct new_module_symbol *syms, *s;
2686 size_t ret, bufsize, nmod, nsyms, i, j;
2688 /* Collect the loaded modules. */
2690 module_names = xmalloc(bufsize = 256);
2692 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2693 if (errno == ENOSPC && bufsize < ret) {
2694 module_names = xrealloc(module_names, bufsize = ret);
2695 goto retry_modules_load;
2697 bb_perror_msg("QM_MODULES");
2701 n_ext_modules = nmod = ret;
2703 /* Collect the modules' symbols. */
2706 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2707 memset(modules, 0, nmod * sizeof(*modules));
2708 for (i = 0, mn = module_names, m = modules;
2709 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2710 struct new_module_info info;
2712 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2713 if (errno == ENOENT) {
2714 /* The module was removed out from underneath us. */
2717 bb_perror_msg("query_module: QM_INFO: %s", mn);
2721 syms = xmalloc(bufsize = 1024);
2723 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2726 syms = xrealloc(syms, bufsize = ret);
2727 goto retry_mod_sym_load;
2729 /* The module was removed out from underneath us. */
2732 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2739 m->addr = info.addr;
2743 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2744 s->name += (unsigned long) syms;
2749 /* Collect the kernel's symbols. */
2751 syms = xmalloc(bufsize = 16 * 1024);
2752 retry_kern_sym_load:
2753 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2754 if (errno == ENOSPC && bufsize < ret) {
2755 syms = xrealloc(syms, bufsize = ret);
2756 goto retry_kern_sym_load;
2758 bb_perror_msg("kernel: QM_SYMBOLS");
2761 nksyms = nsyms = ret;
2764 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2765 s->name += (unsigned long) syms;
2771 /* Return the kernel symbol checksum version, or zero if not used. */
2773 static int new_is_kernel_checksummed(void)
2775 struct new_module_symbol *s;
2778 /* Using_Versions is not the first symbol, but it should be in there. */
2780 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2781 if (strcmp((char *) s->name, "Using_Versions") == 0)
2788 static int new_create_this_module(struct obj_file *f, const char *m_name)
2790 struct obj_section *sec;
2792 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2793 sizeof(struct new_module));
2794 memset(sec->contents, 0, sizeof(struct new_module));
2796 obj_add_symbol(f, SPFX "__this_module", -1,
2797 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2798 sizeof(struct new_module));
2800 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2806 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2807 /* add an entry to the __ksymtab section, creating it if necessary */
2808 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2810 struct obj_section *sec;
2813 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2814 * If __ksymtab is defined but not marked alloc, x out the first character
2815 * (no obj_delete routine) and create a new __ksymtab with the correct
2818 sec = obj_find_section(f, "__ksymtab");
2819 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2820 *((char *)(sec->name)) = 'x'; /* override const */
2824 sec = obj_create_alloced_section(f, "__ksymtab",
2825 tgt_sizeof_void_p, 0);
2828 sec->header.sh_flags |= SHF_ALLOC;
2829 /* Empty section might be byte-aligned */
2830 sec->header.sh_addralign = tgt_sizeof_void_p;
2831 ofs = sec->header.sh_size;
2832 obj_symbol_patch(f, sec->idx, ofs, sym);
2833 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2834 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2836 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2838 static int new_create_module_ksymtab(struct obj_file *f)
2840 struct obj_section *sec;
2843 /* We must always add the module references. */
2845 if (n_ext_modules_used) {
2846 struct new_module_ref *dep;
2847 struct obj_symbol *tm;
2849 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2850 (sizeof(struct new_module_ref)
2851 * n_ext_modules_used));
2855 tm = obj_find_symbol(f, SPFX "__this_module");
2856 dep = (struct new_module_ref *) sec->contents;
2857 for (i = 0; i < n_ext_modules; ++i)
2858 if (ext_modules[i].used) {
2859 dep->dep = ext_modules[i].addr;
2860 obj_symbol_patch(f, sec->idx,
2861 (char *) &dep->ref - sec->contents, tm);
2867 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2871 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2873 /* We don't want to export symbols residing in sections that
2874 aren't loaded. There are a number of these created so that
2875 we make sure certain module options don't appear twice. */
2877 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2879 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2881 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2882 struct obj_symbol *sym;
2883 for (sym = f->symtab[i]; sym; sym = sym->next)
2884 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2885 && sym->secidx <= SHN_HIRESERVE
2886 && (sym->secidx >= SHN_LORESERVE
2887 || loaded[sym->secidx])) {
2888 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2890 obj_symbol_patch(f, sec->idx, ofs, sym);
2891 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2898 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2906 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2908 struct new_module *module;
2909 struct obj_section *sec;
2914 sec = obj_find_section(f, ".this");
2915 if (!sec || !sec->contents) {
2916 bb_perror_msg_and_die("corrupt module %s?",m_name);
2918 module = (struct new_module *) sec->contents;
2919 m_addr = sec->header.sh_addr;
2921 module->size_of_struct = sizeof(*module);
2922 module->size = m_size;
2923 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2925 sec = obj_find_section(f, "__ksymtab");
2926 if (sec && sec->header.sh_size) {
2927 module->syms = sec->header.sh_addr;
2928 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2931 if (n_ext_modules_used) {
2932 sec = obj_find_section(f, ".kmodtab");
2933 module->deps = sec->header.sh_addr;
2934 module->ndeps = n_ext_modules_used;
2938 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2940 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2942 sec = obj_find_section(f, "__ex_table");
2944 module->ex_table_start = sec->header.sh_addr;
2945 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2948 sec = obj_find_section(f, ".text.init");
2950 module->runsize = sec->header.sh_addr - m_addr;
2952 sec = obj_find_section(f, ".data.init");
2954 if (!module->runsize ||
2955 module->runsize > sec->header.sh_addr - m_addr)
2956 module->runsize = sec->header.sh_addr - m_addr;
2958 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2959 if (sec && sec->header.sh_size) {
2960 module->archdata_start = (void*)sec->header.sh_addr;
2961 module->archdata_end = module->archdata_start + sec->header.sh_size;
2963 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2964 if (sec && sec->header.sh_size) {
2965 module->kallsyms_start = (void*)sec->header.sh_addr;
2966 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2969 /* Whew! All of the initialization is complete. Collect the final
2970 module image and give it to the kernel. */
2972 image = xmalloc(m_size);
2973 obj_create_image(f, image);
2975 ret = init_module(m_name, (struct new_module *) image);
2977 bb_perror_msg("init_module: %s", m_name);
2985 /*======================================================================*/
2988 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2991 struct obj_string_patch *p;
2992 struct obj_section *strsec;
2993 size_t len = strlen(string) + 1;
2996 p = xmalloc(sizeof(*p));
2997 p->next = f->string_patches;
2998 p->reloc_secidx = secidx;
2999 p->reloc_offset = offset;
3000 f->string_patches = p;
3002 strsec = obj_find_section(f, ".kstrtab");
3003 if (strsec == NULL) {
3004 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3005 p->string_offset = 0;
3006 loc = strsec->contents;
3008 p->string_offset = strsec->header.sh_size;
3009 loc = obj_extend_section(strsec, len);
3011 memcpy(loc, string, len);
3017 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3018 struct obj_symbol *sym)
3020 struct obj_symbol_patch *p;
3022 p = xmalloc(sizeof(*p));
3023 p->next = f->symbol_patches;
3024 p->reloc_secidx = secidx;
3025 p->reloc_offset = offset;
3027 f->symbol_patches = p;
3032 static int obj_check_undefineds(struct obj_file *f)
3037 for (i = 0; i < HASH_BUCKETS; ++i) {
3038 struct obj_symbol *sym;
3039 for (sym = f->symtab[i]; sym; sym = sym->next)
3040 if (sym->secidx == SHN_UNDEF) {
3041 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3042 sym->secidx = SHN_ABS;
3046 bb_error_msg("unresolved symbol %s", sym->name);
3056 static void obj_allocate_commons(struct obj_file *f)
3058 struct common_entry {
3059 struct common_entry *next;
3060 struct obj_symbol *sym;
3061 } *common_head = NULL;
3065 for (i = 0; i < HASH_BUCKETS; ++i) {
3066 struct obj_symbol *sym;
3067 for (sym = f->symtab[i]; sym; sym = sym->next)
3068 if (sym->secidx == SHN_COMMON) {
3069 /* Collect all COMMON symbols and sort them by size so as to
3070 minimize space wasted by alignment requirements. */
3072 struct common_entry **p, *n;
3073 for (p = &common_head; *p; p = &(*p)->next)
3074 if (sym->size <= (*p)->sym->size)
3077 n = alloca(sizeof(*n));
3085 for (i = 1; i < f->local_symtab_size; ++i) {
3086 struct obj_symbol *sym = f->local_symtab[i];
3087 if (sym && sym->secidx == SHN_COMMON) {
3088 struct common_entry **p, *n;
3089 for (p = &common_head; *p; p = &(*p)->next)
3090 if (sym == (*p)->sym)
3092 else if (sym->size < (*p)->sym->size) {
3093 n = alloca(sizeof(*n));
3103 /* Find the bss section. */
3104 for (i = 0; i < f->header.e_shnum; ++i)
3105 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3108 /* If for some reason there hadn't been one, create one. */
3109 if (i == f->header.e_shnum) {
3110 struct obj_section *sec;
3112 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3113 f->sections[i] = sec = arch_new_section();
3114 f->header.e_shnum = i + 1;
3116 memset(sec, 0, sizeof(*sec));
3117 sec->header.sh_type = SHT_PROGBITS;
3118 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3123 /* Allocate the COMMONS. */
3125 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3126 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3127 struct common_entry *c;
3129 for (c = common_head; c; c = c->next) {
3130 ElfW(Addr) align = c->sym->value;
3132 if (align > max_align)
3134 if (bss_size & (align - 1))
3135 bss_size = (bss_size | (align - 1)) + 1;
3138 c->sym->value = bss_size;
3140 bss_size += c->sym->size;
3143 f->sections[i]->header.sh_size = bss_size;
3144 f->sections[i]->header.sh_addralign = max_align;
3148 /* For the sake of patch relocation and parameter initialization,
3149 allocate zeroed data for NOBITS sections now. Note that after
3150 this we cannot assume NOBITS are really empty. */
3151 for (i = 0; i < f->header.e_shnum; ++i) {
3152 struct obj_section *s = f->sections[i];
3153 if (s->header.sh_type == SHT_NOBITS) {
3154 if (s->header.sh_size != 0)
3155 s->contents = memset(xmalloc(s->header.sh_size),
3156 0, s->header.sh_size);
3160 s->header.sh_type = SHT_PROGBITS;
3165 static unsigned long obj_load_size(struct obj_file *f)
3167 unsigned long dot = 0;
3168 struct obj_section *sec;
3170 /* Finalize the positions of the sections relative to one another. */
3172 for (sec = f->load_order; sec; sec = sec->load_next) {
3175 align = sec->header.sh_addralign;
3176 if (align && (dot & (align - 1)))
3177 dot = (dot | (align - 1)) + 1;
3179 sec->header.sh_addr = dot;
3180 dot += sec->header.sh_size;
3186 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3188 int i, n = f->header.e_shnum;
3191 /* Finalize the addresses of the sections. */
3194 for (i = 0; i < n; ++i)
3195 f->sections[i]->header.sh_addr += base;
3197 /* And iterate over all of the relocations. */
3199 for (i = 0; i < n; ++i) {
3200 struct obj_section *relsec, *symsec, *targsec, *strsec;
3201 ElfW(RelM) * rel, *relend;
3205 relsec = f->sections[i];
3206 if (relsec->header.sh_type != SHT_RELM)
3209 symsec = f->sections[relsec->header.sh_link];
3210 targsec = f->sections[relsec->header.sh_info];
3211 strsec = f->sections[symsec->header.sh_link];
3213 rel = (ElfW(RelM) *) relsec->contents;
3214 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3215 symtab = (ElfW(Sym) *) symsec->contents;
3216 strtab = (const char *) strsec->contents;
3218 for (; rel < relend; ++rel) {
3219 ElfW(Addr) value = 0;
3220 struct obj_symbol *intsym = NULL;
3221 unsigned long symndx;
3222 ElfW(Sym) * extsym = 0;
3225 /* Attempt to find a value to use for this relocation. */
3227 symndx = ELF_R_SYM(rel->r_info);
3229 /* Note we've already checked for undefined symbols. */
3231 extsym = &symtab[symndx];
3232 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3233 /* Local symbols we look up in the local table to be sure
3234 we get the one that is really intended. */
3235 intsym = f->local_symtab[symndx];
3237 /* Others we look up in the hash table. */
3239 if (extsym->st_name)
3240 name = strtab + extsym->st_name;
3242 name = f->sections[extsym->st_shndx]->name;
3243 intsym = obj_find_symbol(f, name);
3246 value = obj_symbol_final_value(f, intsym);
3247 intsym->referenced = 1;
3249 #if SHT_RELM == SHT_RELA
3250 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3251 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3252 if (!extsym || !extsym->st_name ||
3253 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3255 value += rel->r_addend;
3259 switch (arch_apply_relocation
3260 (f, targsec, symsec, intsym, rel, value)
3265 case obj_reloc_overflow:
3266 errmsg = "Relocation overflow";
3268 case obj_reloc_dangerous:
3269 errmsg = "Dangerous relocation";
3271 case obj_reloc_unhandled:
3272 errmsg = "Unhandled relocation";
3275 bb_error_msg("%s of type %ld for %s", errmsg,
3276 (long) ELF_R_TYPE(rel->r_info),
3277 strtab + extsym->st_name);
3279 bb_error_msg("%s of type %ld", errmsg,
3280 (long) ELF_R_TYPE(rel->r_info));
3288 /* Finally, take care of the patches. */
3290 if (f->string_patches) {
3291 struct obj_string_patch *p;
3292 struct obj_section *strsec;
3293 ElfW(Addr) strsec_base;
3294 strsec = obj_find_section(f, ".kstrtab");
3295 strsec_base = strsec->header.sh_addr;
3297 for (p = f->string_patches; p; p = p->next) {
3298 struct obj_section *targsec = f->sections[p->reloc_secidx];
3299 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3300 = strsec_base + p->string_offset;
3304 if (f->symbol_patches) {
3305 struct obj_symbol_patch *p;
3307 for (p = f->symbol_patches; p; p = p->next) {
3308 struct obj_section *targsec = f->sections[p->reloc_secidx];
3309 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3310 = obj_symbol_final_value(f, p->sym);
3317 static int obj_create_image(struct obj_file *f, char *image)
3319 struct obj_section *sec;
3320 ElfW(Addr) base = f->baseaddr;
3322 for (sec = f->load_order; sec; sec = sec->load_next) {
3325 if (sec->contents == 0 || sec->header.sh_size == 0)
3328 secimg = image + (sec->header.sh_addr - base);
3330 /* Note that we allocated data for NOBITS sections earlier. */
3331 memcpy(secimg, sec->contents, sec->header.sh_size);
3337 /*======================================================================*/
3339 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3342 ElfW(Shdr) * section_headers;
3346 /* Read the file header. */
3348 f = arch_new_file();
3349 memset(f, 0, sizeof(*f));
3350 f->symbol_cmp = strcmp;
3351 f->symbol_hash = obj_elf_hash;
3352 f->load_order_search_start = &f->load_order;
3354 fseek(fp, 0, SEEK_SET);
3355 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3356 bb_perror_msg("error reading ELF header");
3360 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3361 || f->header.e_ident[EI_MAG1] != ELFMAG1
3362 || f->header.e_ident[EI_MAG2] != ELFMAG2
3363 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3364 bb_error_msg("not an ELF file");
3367 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3368 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3369 ? ELFDATA2MSB : ELFDATA2LSB)
3370 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3371 || !MATCH_MACHINE(f->header.e_machine)) {
3372 bb_error_msg("ELF file not for this architecture");
3375 if (f->header.e_type != ET_REL) {
3376 bb_error_msg("ELF file not a relocatable object");
3380 /* Read the section headers. */
3382 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3383 bb_error_msg("section header size mismatch: %lu != %lu",
3384 (unsigned long) f->header.e_shentsize,
3385 (unsigned long) sizeof(ElfW(Shdr)));
3389 shnum = f->header.e_shnum;
3390 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3391 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3393 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3394 fseek(fp, f->header.e_shoff, SEEK_SET);
3395 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3396 bb_perror_msg("error reading ELF section headers");
3400 /* Read the section data. */
3402 for (i = 0; i < shnum; ++i) {
3403 struct obj_section *sec;
3405 f->sections[i] = sec = arch_new_section();
3406 memset(sec, 0, sizeof(*sec));
3408 sec->header = section_headers[i];
3411 if(sec->header.sh_size) {
3412 switch (sec->header.sh_type) {
3421 if (!loadprogbits) {
3422 sec->contents = NULL;
3429 if (sec->header.sh_size > 0) {
3430 sec->contents = xmalloc(sec->header.sh_size);
3431 fseek(fp, sec->header.sh_offset, SEEK_SET);
3432 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3433 bb_perror_msg("error reading ELF section data");
3437 sec->contents = NULL;
3441 #if SHT_RELM == SHT_REL
3443 bb_error_msg("RELA relocations not supported on this architecture");
3447 bb_error_msg("REL relocations not supported on this architecture");
3452 if (sec->header.sh_type >= SHT_LOPROC) {
3453 /* Assume processor specific section types are debug
3454 info and can safely be ignored. If this is ever not
3455 the case (Hello MIPS?), don't put ifdefs here but
3456 create an arch_load_proc_section(). */
3460 bb_error_msg("can't handle sections of type %ld",
3461 (long) sec->header.sh_type);
3467 /* Do what sort of interpretation as needed by each section. */
3469 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3471 for (i = 0; i < shnum; ++i) {
3472 struct obj_section *sec = f->sections[i];
3473 sec->name = shstrtab + sec->header.sh_name;
3476 for (i = 0; i < shnum; ++i) {
3477 struct obj_section *sec = f->sections[i];
3479 /* .modinfo should be contents only but gcc has no attribute for that.
3480 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3482 if (strcmp(sec->name, ".modinfo") == 0)
3483 sec->header.sh_flags &= ~SHF_ALLOC;
3485 if (sec->header.sh_flags & SHF_ALLOC)
3486 obj_insert_section_load_order(f, sec);
3488 switch (sec->header.sh_type) {
3491 unsigned long nsym, j;
3495 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3496 bb_error_msg("symbol size mismatch: %lu != %lu",
3497 (unsigned long) sec->header.sh_entsize,
3498 (unsigned long) sizeof(ElfW(Sym)));
3502 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3503 strtab = f->sections[sec->header.sh_link]->contents;
3504 sym = (ElfW(Sym) *) sec->contents;
3506 /* Allocate space for a table of local symbols. */
3507 j = f->local_symtab_size = sec->header.sh_info;
3508 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3510 /* Insert all symbols into the hash table. */
3511 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3512 ElfW(Addr) val = sym->st_value;
3515 name = strtab + sym->st_name;
3516 else if (sym->st_shndx < shnum)
3517 name = f->sections[sym->st_shndx]->name;
3520 #if defined(__SH5__)
3522 * For sh64 it is possible that the target of a branch
3523 * requires a mode switch (32 to 16 and back again).
3525 * This is implied by the lsb being set in the target
3526 * address for SHmedia mode and clear for SHcompact.
3528 val |= sym->st_other & 4;
3531 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3538 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3539 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3540 (unsigned long) sec->header.sh_entsize,
3541 (unsigned long) sizeof(ElfW(RelM)));
3545 /* XXX Relocation code from modutils-2.3.19 is not here.
3546 * Why? That's about 20 lines of code from obj/obj_load.c,
3547 * which gets done in a second pass through the sections.
3548 * This BusyBox insmod does similar work in obj_relocate(). */
3555 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3557 * load the unloaded sections directly into the memory allocated by
3558 * kernel for the module
3561 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3563 ElfW(Addr) base = f->baseaddr;
3564 struct obj_section* sec;
3566 for (sec = f->load_order; sec; sec = sec->load_next) {
3568 /* section already loaded? */
3569 if (sec->contents != NULL)
3572 if (sec->header.sh_size == 0)
3575 sec->contents = imagebase + (sec->header.sh_addr - base);
3576 fseek(fp, sec->header.sh_offset, SEEK_SET);
3577 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3578 bb_perror_msg("error reading ELF section data");
3587 static void hide_special_symbols(struct obj_file *f)
3589 static const char *const specials[] = {
3590 SPFX "cleanup_module",
3592 SPFX "kernel_version",
3596 struct obj_symbol *sym;
3597 const char *const *p;
3599 for (p = specials; *p; ++p) {
3600 sym = obj_find_symbol(f, *p);
3602 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3607 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3608 static int obj_gpl_license(struct obj_file *f, const char **license)
3610 struct obj_section *sec;
3611 /* This list must match *exactly* the list of allowable licenses in
3612 * linux/include/linux/module.h. Checking for leading "GPL" will not
3613 * work, somebody will use "GPL sucks, this is proprietary".
3615 static const char * const gpl_licenses[] = {
3618 "GPL and additional rights",
3623 sec = obj_find_section(f, ".modinfo");
3625 const char *value, *ptr, *endptr;
3626 ptr = sec->contents;
3627 endptr = ptr + sec->header.sh_size;
3628 while (ptr < endptr) {
3629 value = strchr(ptr, '=');
3630 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3634 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3635 if (strcmp(value+1, gpl_licenses[i]) == 0)
3640 if (strchr(ptr, '\0'))
3641 ptr = strchr(ptr, '\0') + 1;
3649 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3650 #define TAINT_PROPRIETORY_MODULE (1<<0)
3651 #define TAINT_FORCED_MODULE (1<<1)
3652 #define TAINT_UNSAFE_SMP (1<<2)
3653 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3655 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3656 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3660 static int first = 1;
3661 if (fd < 0 && !kernel_has_tainted)
3662 return; /* New modutils on old kernel */
3663 printf("Warning: loading %s will taint the kernel: %s%s\n",
3664 m_name, text1, text2);
3666 printf(" See %s for information about tainted modules\n", TAINT_URL);
3670 read(fd, buf, sizeof(buf)-1);
3671 buf[sizeof(buf)-1] = '\0';
3672 oldval = strtoul(buf, NULL, 10);
3673 sprintf(buf, "%d\n", oldval | taint);
3674 write(fd, buf, strlen(buf));
3678 /* Check if loading this module will taint the kernel. */
3679 static void check_tainted_module(struct obj_file *f, char *m_name)
3681 static const char tainted_file[] = TAINT_FILENAME;
3682 int fd, kernel_has_tainted;
3685 kernel_has_tainted = 1;
3686 fd = open(tainted_file, O_RDWR);
3688 if (errno == ENOENT)
3689 kernel_has_tainted = 0;
3690 else if (errno == EACCES)
3691 kernel_has_tainted = 1;
3693 perror(tainted_file);
3694 kernel_has_tainted = 0;
3698 switch (obj_gpl_license(f, &ptr)) {
3702 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3705 /* The module has a non-GPL license so we pretend that the
3706 * kernel always has a taint flag to get a warning even on
3707 * kernels without the proc flag.
3709 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3712 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3716 if (flag_force_load)
3717 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3722 #else /* FEATURE_CHECK_TAINTED_MODULE */
3723 #define check_tainted_module(x, y) do { } while (0);
3724 #endif /* FEATURE_CHECK_TAINTED_MODULE */
3726 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3727 /* add module source, timestamp, kernel version and a symbol for the
3728 * start of some sections. this info is used by ksymoops to do better
3732 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3734 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3735 return new_get_module_version(f, str);
3736 #else /* FEATURE_INSMOD_VERSION_CHECKING */
3737 strncpy(str, "???", sizeof(str));
3739 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
3742 /* add module source, timestamp, kernel version and a symbol for the
3743 * start of some sections. this info is used by ksymoops to do better
3747 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3750 static const char symprefix[] = "__insmod_";
3751 struct obj_section *sec;
3752 struct obj_symbol *sym;
3753 char *name, *absolute_filename;
3754 char str[STRVERSIONLEN], real[PATH_MAX];
3755 int i, l, lm_name, lfilename, use_ksymtab, version;
3756 struct stat statbuf;
3758 static const char *section_names[] = {
3766 if (realpath(filename, real)) {
3767 absolute_filename = xstrdup(real);
3769 bb_perror_msg("cannot get realpath for %s", filename);
3770 absolute_filename = xstrdup(filename);
3773 lm_name = strlen(m_name);
3774 lfilename = strlen(absolute_filename);
3776 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3777 * are not to be exported. otherwise leave ksymtab alone for now, the
3778 * "export all symbols" compatibility code will export these symbols later.
3780 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3782 sec = obj_find_section(f, ".this");
3784 /* tag the module header with the object name, last modified
3785 * timestamp and module version. worst case for module version
3786 * is 0xffffff, decimal 16777215. putting all three fields in
3787 * one symbol is less readable but saves kernel space.
3789 l = sizeof(symprefix)+ /* "__insmod_" */
3790 lm_name+ /* module name */
3792 lfilename+ /* object filename */
3794 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3796 8+ /* version in dec */
3799 if (stat(absolute_filename, &statbuf) != 0)
3800 statbuf.st_mtime = 0;
3801 version = get_module_version(f, str); /* -1 if not found */
3802 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3803 symprefix, m_name, absolute_filename,
3804 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3806 sym = obj_add_symbol(f, name, -1,
3807 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3808 sec->idx, sec->header.sh_addr, 0);
3810 new_add_ksymtab(f, sym);
3812 free(absolute_filename);
3813 #ifdef _NOT_SUPPORTED_
3814 /* record where the persistent data is going, same address as previous symbol */
3817 l = sizeof(symprefix)+ /* "__insmod_" */
3818 lm_name+ /* module name */
3820 strlen(f->persist)+ /* data store */
3823 snprintf(name, l, "%s%s_P%s",
3824 symprefix, m_name, f->persist);
3825 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3826 sec->idx, sec->header.sh_addr, 0);
3828 new_add_ksymtab(f, sym);
3830 #endif /* _NOT_SUPPORTED_ */
3831 /* tag the desired sections if size is non-zero */
3833 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3834 sec = obj_find_section(f, section_names[i]);
3835 if (sec && sec->header.sh_size) {
3836 l = sizeof(symprefix)+ /* "__insmod_" */
3837 lm_name+ /* module name */
3839 strlen(sec->name)+ /* section name */
3841 8+ /* length in dec */
3844 snprintf(name, l, "%s%s_S%s_L%ld",
3845 symprefix, m_name, sec->name,
3846 (long)sec->header.sh_size);
3847 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3848 sec->idx, sec->header.sh_addr, 0);
3850 new_add_ksymtab(f, sym);
3854 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3856 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3857 static void print_load_map(struct obj_file *f)
3859 struct obj_section *sec;
3860 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3861 struct obj_symbol **all, **p;
3862 int i, nsyms, *loaded;
3863 struct obj_symbol *sym;
3865 /* Report on the section layout. */
3867 printf("Sections: Size %-*s Align\n",
3868 (int) (2 * sizeof(void *)), "Address");
3870 for (sec = f->load_order; sec; sec = sec->load_next) {
3874 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3879 printf("%-15s %08lx %0*lx 2**%d\n",
3881 (long)sec->header.sh_size,
3882 (int) (2 * sizeof(void *)),
3883 (long)sec->header.sh_addr,
3886 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3887 /* Quick reference which section indicies are loaded. */
3889 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3891 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3893 /* Collect the symbols we'll be listing. */
3895 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3896 for (sym = f->symtab[i]; sym; sym = sym->next)
3897 if (sym->secidx <= SHN_HIRESERVE
3898 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3901 all = alloca(nsyms * sizeof(struct obj_symbol *));
3903 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3904 for (sym = f->symtab[i]; sym; sym = sym->next)
3905 if (sym->secidx <= SHN_HIRESERVE
3906 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3909 /* And list them. */
3910 printf("\nSymbols:\n");
3911 for (p = all; p < all + nsyms; ++p) {
3913 unsigned long value;
3916 if (sym->secidx == SHN_ABS) {
3919 } else if (sym->secidx == SHN_UNDEF) {
3923 sec = f->sections[sym->secidx];
3925 if (sec->header.sh_type == SHT_NOBITS)
3927 else if (sec->header.sh_flags & SHF_ALLOC) {
3928 if (sec->header.sh_flags & SHF_EXECINSTR)
3930 else if (sec->header.sh_flags & SHF_WRITE)
3935 value = sym->value + sec->header.sh_addr;
3938 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3939 type = tolower(type);
3941 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3946 #else /* !FEATURE_INSMOD_LOAD_MAP */
3947 void print_load_map(struct obj_file *f);
3950 int insmod_main( int argc, char **argv)
3956 unsigned long m_size;
3961 int exit_status = EXIT_FAILURE;
3963 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3964 struct utsname uts_info;
3965 char m_strversion[STRVERSIONLEN];
3966 int m_version, m_crcs;
3968 #if ENABLE_FEATURE_CLEAN_UP
3974 struct utsname myuname;
3976 /* Parse any options */
3977 getopt32(argc, argv, OPTION_STR, &opt_o);
3978 arg1 = argv[optind];
3979 if (option_mask32 & OPT_o) { // -o /* name the output module */
3981 m_name = xstrdup(opt_o);
3988 /* Grab the module name */
3989 tmp1 = xstrdup(arg1);
3990 tmp = basename(tmp1);
3993 if (uname(&myuname) == 0) {
3994 if (myuname.release[0] == '2') {
3995 k_version = myuname.release[2] - '0';
3999 #if ENABLE_FEATURE_2_6_MODULES
4000 if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
4001 && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
4007 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4013 #if ENABLE_FEATURE_2_6_MODULES
4015 m_fullName = xasprintf("%s.ko", tmp);
4018 m_fullName = xasprintf("%s.o", tmp);
4024 tmp1 = 0; /* flag for free(m_name) before exit() */
4027 /* Get a filedesc for the module. Check we we have a complete path */
4028 if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
4029 || (fp = fopen(arg1, "r")) == NULL
4031 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4032 * but do not error out yet if we fail to find it... */
4033 if (k_version) { /* uname succeedd */
4036 char real_module_dir[FILENAME_MAX];
4038 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4039 /* Jump through hoops in case /lib/modules/`uname -r`
4040 * is a symlink. We do not want recursive_action to
4041 * follow symlinks, but we do want to follow the
4042 * /lib/modules/`uname -r` dir, So resolve it ourselves
4043 * if it is a link... */
4044 if (realpath(tmdn, real_module_dir) == NULL)
4047 module_dir = real_module_dir;
4048 recursive_action(module_dir, TRUE, FALSE, FALSE,
4049 check_module_name_match, 0, m_fullName, 0);
4053 /* Check if we have found anything yet */
4054 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) {
4055 char module_dir[FILENAME_MAX];
4059 if (realpath (_PATH_MODULES, module_dir) == NULL)
4060 strcpy(module_dir, _PATH_MODULES);
4061 /* No module found under /lib/modules/`uname -r`, this
4062 * time cast the net a bit wider. Search /lib/modules/ */
4063 if (!recursive_action(module_dir, TRUE, FALSE, FALSE,
4064 check_module_name_match, 0, m_fullName, 0)
4067 || ((fp = fopen(m_filename, "r")) == NULL)
4069 bb_error_msg("%s: no module by that name found", m_fullName);
4073 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4076 m_filename = xstrdup(arg1);
4079 printf("Using %s\n", m_filename);
4081 #if ENABLE_FEATURE_2_6_MODULES
4082 if (k_version > 4) {
4083 argv[optind] = m_filename;
4085 return insmod_ng_main(argc - optind, argv + optind);
4089 f = obj_load(fp, LOADBITS);
4091 bb_perror_msg_and_die("cannot load the module");
4093 if (get_modinfo_value(f, "kernel_version") == NULL)
4098 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4099 /* Version correspondence? */
4101 if (uname(&uts_info) < 0)
4102 uts_info.release[0] = '\0';
4103 if (m_has_modinfo) {
4104 m_version = new_get_module_version(f, m_strversion);
4105 if (m_version == -1) {
4106 bb_error_msg("cannot find the kernel version the module was "
4112 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4113 if (flag_force_load) {
4114 bb_error_msg("warning: kernel-module version mismatch\n"
4115 "\t%s was compiled for kernel version %s\n"
4116 "\twhile this kernel is version %s",
4117 m_filename, m_strversion, uts_info.release);
4119 bb_error_msg("kernel-module version mismatch\n"
4120 "\t%s was compiled for kernel version %s\n"
4121 "\twhile this kernel is version %s.",
4122 m_filename, m_strversion, uts_info.release);
4128 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4130 if (!query_module(NULL, 0, NULL, 0, NULL)) {
4131 if (!new_get_kernel_symbols())
4133 k_crcs = new_is_kernel_checksummed();
4135 bb_error_msg("not configured to support old kernels");
4139 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4142 m_crcs = new_is_module_checksummed(f);
4144 if (m_crcs != k_crcs)
4145 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4146 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4148 /* Let the module know about the kernel symbols. */
4149 add_kernel_symbols(f);
4151 /* Allocate common symbols, symbol tables, and string tables. */
4153 if (!new_create_this_module(f, m_name)) {
4157 if (!obj_check_undefineds(f)) {
4160 obj_allocate_commons(f);
4161 check_tainted_module(f, m_name);
4163 /* done with the module name, on to the optional var=value arguments */
4165 if (optind < argc) {
4166 if (!new_process_module_arguments(f, argc - optind, argv + optind)) {
4172 hide_special_symbols(f);
4174 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4175 add_ksymoops_symbols(f, m_filename, m_name);
4176 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4178 new_create_module_ksymtab(f);
4180 /* Find current size of the module */
4181 m_size = obj_load_size(f);
4184 m_addr = create_module(m_name, m_size);
4185 if (m_addr == -1) switch (errno) {
4187 bb_error_msg("a module named %s already exists", m_name);
4190 bb_error_msg("can't allocate kernel memory for module; needed %lu bytes",
4194 bb_perror_msg("create_module: %s", m_name);
4200 * the PROGBITS section was not loaded by the obj_load
4201 * now we can load them directly into the kernel memory
4203 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4204 delete_module(m_name);
4209 if (!obj_relocate(f, m_addr)) {
4210 delete_module(m_name);
4214 if (!new_init_module(m_name, f, m_size)) {
4215 delete_module(m_name);
4219 if(flag_print_load_map)
4222 exit_status = EXIT_SUCCESS;
4225 #if ENABLE_FEATURE_CLEAN_UP
4240 #if ENABLE_FEATURE_2_6_MODULES
4242 #include <sys/mman.h>
4243 #include <asm/unistd.h>
4244 #include <sys/syscall.h>
4246 /* We use error numbers in a loose translation... */
4247 static const char *moderror(int err)
4251 return "Invalid module format";
4253 return "Unknown symbol in module";
4255 return "Module has wrong symbol version";
4257 return "Invalid parameters";
4259 return strerror(err);
4263 int insmod_ng_main(int argc, char **argv)
4268 char *filename, *options;
4274 /* Rest is options */
4275 options = xstrdup("");
4277 int optlen = strlen(options);
4278 options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
4279 /* Spaces handled by "" pairs, but no way of escaping quotes */
4280 sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
4284 /* Any special reason why mmap? It isn't performace critical... */
4288 fd = xopen(filename, O_RDONLY);
4291 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4292 if (map == MAP_FAILED) {
4293 bb_perror_msg_and_die("cannot mmap '%s'", filename);
4296 /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
4299 xread(fd, map, len);
4302 len = MAXINT(ssize_t);
4303 map = xmalloc_open_read_close(filename, &len);
4306 ret = syscall(__NR_init_module, map, len, options);
4308 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
4309 filename, moderror(errno), ret);