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. */
504 # if ELFCLASSM == ELFCLASS32
505 # define ElfW(x) Elf32_ ## x
506 # define ELFW(x) ELF32_ ## x
508 # define ElfW(x) Elf64_ ## x
509 # define ELFW(x) ELF64_ ## x
513 /* For some reason this is missing from some ancient C libraries.... */
514 #ifndef ELF32_ST_INFO
515 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
518 #ifndef ELF64_ST_INFO
519 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
522 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
523 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
524 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
525 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
526 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
528 struct obj_string_patch;
529 struct obj_symbol_patch;
536 struct obj_section *load_next;
542 struct obj_symbol *next; /* hash table link */
546 int secidx; /* the defining section index/module */
548 int ksymidx; /* for export to the kernel symtab */
549 int referenced; /* actually used in the link */
552 /* Hardcode the hash table size. We shouldn't be needing so many
553 symbols that we begin to degrade performance, and we get a big win
554 by giving the compiler a constant divisor. */
556 #define HASH_BUCKETS 521
561 struct obj_section **sections;
562 struct obj_section *load_order;
563 struct obj_section **load_order_search_start;
564 struct obj_string_patch *string_patches;
565 struct obj_symbol_patch *symbol_patches;
566 int (*symbol_cmp)(const char *, const char *);
567 unsigned long (*symbol_hash)(const char *);
568 unsigned long local_symtab_size;
569 struct obj_symbol **local_symtab;
570 struct obj_symbol *symtab[HASH_BUCKETS];
580 struct obj_string_patch {
581 struct obj_string_patch *next;
583 ElfW(Addr) reloc_offset;
584 ElfW(Addr) string_offset;
587 struct obj_symbol_patch {
588 struct obj_symbol_patch *next;
590 ElfW(Addr) reloc_offset;
591 struct obj_symbol *sym;
595 /* Generic object manipulation routines. */
597 static unsigned long obj_elf_hash(const char *);
599 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
601 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
604 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
605 struct obj_symbol *sym);
607 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
608 static void obj_set_symbol_compare(struct obj_file *f,
609 int (*cmp)(const char *, const char *),
610 unsigned long (*hash)(const char *));
613 static struct obj_section *obj_find_section (struct obj_file *f,
616 static void obj_insert_section_load_order (struct obj_file *f,
617 struct obj_section *sec);
619 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
624 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
629 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
631 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
634 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
635 struct obj_symbol *sym);
637 static int obj_check_undefineds(struct obj_file *f);
639 static void obj_allocate_commons(struct obj_file *f);
641 static unsigned long obj_load_size (struct obj_file *f);
643 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
645 static struct obj_file *obj_load(FILE *f, int loadprogbits);
647 static int obj_create_image (struct obj_file *f, char *image);
649 /* Architecture specific manipulation routines. */
651 static struct obj_file *arch_new_file (void);
653 static struct obj_section *arch_new_section (void);
655 static struct obj_symbol *arch_new_symbol (void);
657 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
658 struct obj_section *targsec,
659 struct obj_section *symsec,
660 struct obj_symbol *sym,
661 ElfW(RelM) *rel, ElfW(Addr) value);
663 static void arch_create_got (struct obj_file *f);
664 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
665 static int obj_gpl_license(struct obj_file *f, const char **license);
666 #endif /* FEATURE_CHECK_TAINTED_MODULE */
668 //----------------------------------------------------------------------------
669 //--------end of modutils obj.h
670 //----------------------------------------------------------------------------
673 /* SPFX is always a string, so it can be concatenated to string constants. */
675 #define SPFX SYMBOL_PREFIX
681 #define _PATH_MODULES "/lib/modules"
682 enum { STRVERSIONLEN = 64 };
684 /*======================================================================*/
686 #define OPTION_STR "sLo:fkvqx" USE_FEATURE_INSMOD_LOAD_MAP("m")
688 OPT_s = 0x1, // -s /* log to syslog */
689 /* Not supported but kernel needs this for request_module(),
690 as this calls: modprobe -k -s -- <module>
691 so silently ignore this flag */
692 OPT_L = 0x2, // -L /* Stub warning */
693 /* Compatibility with modprobe.
694 In theory, this does locking, but we don't do
695 that. So be careful and plan your life around not
696 loading the same module 50 times concurrently. */
697 OPT_o = 0x4, // -o /* name the output module */
698 OPT_f = 0x8, // -f /* force loading */
699 OPT_k = 0x10, // -k /* module loaded by kerneld, auto-cleanable */
700 OPT_v = 0x20, // -v /* verbose output */
701 OPT_q = 0x40, // -q /* silent */
702 OPT_x = 0x80, // -x /* do not export externs */
703 OPT_m = 0x100, // -m /* print module load map */
705 #define flag_force_load (option_mask32 & OPT_f)
706 #define flag_autoclean (option_mask32 & OPT_k)
707 #define flag_verbose (option_mask32 & OPT_v)
708 #define flag_quiet (option_mask32 & OPT_q)
709 #define flag_noexport (option_mask32 & OPT_x)
710 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
711 #define flag_print_load_map (option_mask32 & OPT_m)
713 #define flag_print_load_map 0
716 /*======================================================================*/
718 #if defined(USE_LIST)
720 struct arch_list_entry
722 struct arch_list_entry *next;
723 LIST_ARCHTYPE addend;
730 #if defined(USE_SINGLE)
732 struct arch_single_entry
741 #if defined(__mips__)
744 struct mips_hi16 *next;
751 struct obj_file root;
752 #if defined(USE_PLT_ENTRIES)
753 struct obj_section *plt;
755 #if defined(USE_GOT_ENTRIES)
756 struct obj_section *got;
758 #if defined(__mips__)
759 struct mips_hi16 *mips_hi16_list;
764 struct obj_symbol root;
765 #if defined(USE_PLT_ENTRIES)
766 #if defined(USE_PLT_LIST)
767 struct arch_list_entry *pltent;
769 struct arch_single_entry pltent;
772 #if defined(USE_GOT_ENTRIES)
773 struct arch_single_entry gotent;
778 struct external_module {
783 struct new_module_symbol *syms;
786 static struct new_module_symbol *ksyms;
787 static size_t nksyms;
789 static struct external_module *ext_modules;
790 static int n_ext_modules;
791 static int n_ext_modules_used;
792 extern int delete_module(const char *);
794 static char *m_filename;
795 static char *m_fullName;
798 /*======================================================================*/
801 static int check_module_name_match(const char *filename, struct stat *statbuf,
802 void *userdata, int depth)
804 char *fullname = (char *) userdata;
806 if (fullname[0] == '\0')
809 char *tmp, *tmp1 = xstrdup(filename);
810 tmp = bb_get_last_path_component(tmp1);
811 if (strcmp(tmp, fullname) == 0) {
813 /* Stop searching if we find a match */
814 m_filename = xstrdup(filename);
823 /*======================================================================*/
825 static struct obj_file *arch_new_file(void)
828 f = xmalloc(sizeof(*f));
830 memset(f, 0, sizeof(*f));
835 static struct obj_section *arch_new_section(void)
837 return xmalloc(sizeof(struct obj_section));
840 static struct obj_symbol *arch_new_symbol(void)
842 struct arch_symbol *sym;
843 sym = xmalloc(sizeof(*sym));
845 memset(sym, 0, sizeof(*sym));
850 static enum obj_reloc
851 arch_apply_relocation(struct obj_file *f,
852 struct obj_section *targsec,
853 struct obj_section *symsec,
854 struct obj_symbol *sym,
855 ElfW(RelM) *rel, ElfW(Addr) v)
857 struct arch_file *ifile = (struct arch_file *) f;
858 enum obj_reloc ret = obj_reloc_ok;
859 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
860 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
861 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
862 struct arch_symbol *isym = (struct arch_symbol *) sym;
864 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
865 #if defined(USE_GOT_ENTRIES)
866 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
869 #if defined(USE_PLT_ENTRIES)
870 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
872 # if defined(USE_PLT_LIST)
873 struct arch_list_entry *pe;
875 struct arch_single_entry *pe;
879 switch (ELF_R_TYPE(rel->r_info)) {
894 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
895 * (which is .got) similar to branch,
896 * but is full 32 bits relative */
905 case R_ARM_GOTOFF: /* address relative to the got */
909 #elif defined(__cris__)
915 /* CRIS keeps the relocation value in the r_addend field and
916 * should not use whats in *loc at all
921 #elif defined(__H8300H__) || defined(__H8300S__)
924 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
925 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
936 if ((ElfW(Sword))v > 0x7fff ||
937 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
938 ret = obj_reloc_overflow;
940 *(unsigned short *)loc = v;
944 if ((ElfW(Sword))v > 0x7f ||
945 (ElfW(Sword))v < -(ElfW(Sword))0x80)
946 ret = obj_reloc_overflow;
948 *(unsigned char *)loc = v;
951 #elif defined(__i386__)
985 #elif defined (__microblaze__)
986 case R_MICROBLAZE_NONE:
987 case R_MICROBLAZE_64_NONE:
988 case R_MICROBLAZE_32_SYM_OP_SYM:
989 case R_MICROBLAZE_32_PCREL:
992 case R_MICROBLAZE_64_PCREL: {
993 /* dot is the address of the current instruction.
994 * v is the target symbol address.
995 * So we need to extract the offset in the code,
996 * adding v, then subtrating the current address
997 * of this instruction.
998 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
1001 /* Get split offset stored in code */
1002 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
1005 /* Adjust relative offset. -4 adjustment required
1006 * because dot points to the IMM insn, but branch
1007 * is computed relative to the branch instruction itself.
1009 temp += v - dot - 4;
1011 /* Store back into code */
1012 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
1013 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
1018 case R_MICROBLAZE_32:
1022 case R_MICROBLAZE_64: {
1023 /* Get split pointer stored in code */
1024 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
1027 /* Add reloc offset */
1030 /* Store back into code */
1031 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
1032 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
1037 case R_MICROBLAZE_32_PCREL_LO:
1038 case R_MICROBLAZE_32_LO:
1039 case R_MICROBLAZE_SRO32:
1040 case R_MICROBLAZE_SRW32:
1041 ret = obj_reloc_unhandled;
1044 #elif defined(__mc68000__)
1055 ret = obj_reloc_overflow;
1062 ret = obj_reloc_overflow;
1069 if ((ElfW(Sword))v > 0x7f ||
1070 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1071 ret = obj_reloc_overflow;
1078 if ((ElfW(Sword))v > 0x7fff ||
1079 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1080 ret = obj_reloc_overflow;
1086 *(int *)loc = v - dot;
1089 case R_68K_GLOB_DAT:
1090 case R_68K_JMP_SLOT:
1094 case R_68K_RELATIVE:
1095 *(int *)loc += f->baseaddr;
1101 # ifdef R_68K_GOTOFF
1107 #elif defined(__mips__)
1118 ret = obj_reloc_dangerous;
1119 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1120 ret = obj_reloc_overflow;
1122 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1128 struct mips_hi16 *n;
1130 /* We cannot relocate this one now because we don't know the value
1131 of the carry we need to add. Save the information, and let LO16
1132 do the actual relocation. */
1133 n = xmalloc(sizeof *n);
1136 n->next = ifile->mips_hi16_list;
1137 ifile->mips_hi16_list = n;
1143 unsigned long insnlo = *loc;
1144 ElfW(Addr) val, vallo;
1146 /* Sign extend the addend we extract from the lo insn. */
1147 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1149 if (ifile->mips_hi16_list != NULL) {
1150 struct mips_hi16 *l;
1152 l = ifile->mips_hi16_list;
1154 struct mips_hi16 *next;
1157 /* Do the HI16 relocation. Note that we actually don't
1158 need to know anything about the LO16 itself, except where
1159 to find the low 16 bits of the addend needed by the LO16. */
1162 ((insn & 0xffff) << 16) +
1166 /* Account for the sign extension that will happen in the
1173 insn = (insn & ~0xffff) | val;
1181 ifile->mips_hi16_list = NULL;
1184 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1186 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1191 #elif defined(__nios2__)
1196 case R_NIOS2_BFD_RELOC_32:
1200 case R_NIOS2_BFD_RELOC_16:
1202 ret = obj_reloc_overflow;
1207 case R_NIOS2_BFD_RELOC_8:
1209 ret = obj_reloc_overflow;
1218 if ((Elf32_Sword)v > 0x7fff ||
1219 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1220 ret = obj_reloc_overflow;
1224 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1234 ret = obj_reloc_overflow;
1238 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1243 case R_NIOS2_PCREL16:
1248 if ((Elf32_Sword)v > 0x7fff ||
1249 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1250 ret = obj_reloc_overflow;
1254 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1260 Elf32_Addr word, gp;
1262 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1264 if ((Elf32_Sword)v > 0x7fff ||
1265 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1266 ret = obj_reloc_overflow;
1270 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1274 case R_NIOS2_CALL26:
1276 ret = obj_reloc_dangerous;
1277 if ((v >> 28) != (dot >> 28))
1278 ret = obj_reloc_overflow;
1279 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1287 ret = obj_reloc_overflow;
1290 word = *loc & ~0x7c0;
1291 *loc = word | ((v & 0x1f) << 6);
1300 ret = obj_reloc_overflow;
1303 word = *loc & ~0xfc0;
1304 *loc = word | ((v & 0x3f) << 6);
1313 ret = obj_reloc_overflow;
1316 word = *loc & ~0x3fc0;
1317 *loc = word | ((v & 0xff) << 6);
1326 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1336 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1341 case R_NIOS2_HIADJ16:
1343 Elf32_Addr word1, word2;
1346 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1347 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1352 #elif defined(__powerpc64__)
1353 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1355 #elif defined(__powerpc__)
1357 case R_PPC_ADDR16_HA:
1358 *(unsigned short *)loc = (v + 0x8000) >> 16;
1361 case R_PPC_ADDR16_HI:
1362 *(unsigned short *)loc = v >> 16;
1365 case R_PPC_ADDR16_LO:
1366 *(unsigned short *)loc = v;
1380 #elif defined(__s390__)
1383 *(unsigned int *) loc += v;
1386 *(unsigned short *) loc += v;
1389 *(unsigned char *) loc += v;
1393 *(unsigned int *) loc += v - dot;
1396 *(unsigned short *) loc += (v - dot) >> 1;
1399 *(unsigned short *) loc += v - dot;
1403 case R_390_PLT16DBL:
1404 /* find the plt entry and initialize it. */
1405 pe = (struct arch_single_entry *) &isym->pltent;
1406 if (pe->inited == 0) {
1407 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1408 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1410 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1417 /* Insert relative distance to target. */
1418 v = plt + pe->offset - dot;
1419 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1420 *(unsigned int *) loc = (unsigned int) v;
1421 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1422 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1425 case R_390_GLOB_DAT:
1426 case R_390_JMP_SLOT:
1430 case R_390_RELATIVE:
1431 *loc += f->baseaddr;
1435 *(unsigned long *) loc += got - dot;
1441 if (!isym->gotent.inited)
1443 isym->gotent.inited = 1;
1444 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1446 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1447 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1448 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1449 *(unsigned short *) loc += isym->gotent.offset;
1450 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1451 *(unsigned int *) loc += isym->gotent.offset;
1454 # ifndef R_390_GOTOFF32
1455 # define R_390_GOTOFF32 R_390_GOTOFF
1457 case R_390_GOTOFF32:
1461 #elif defined(__sh__)
1484 *loc = f->baseaddr + rel->r_addend;
1488 *loc = got - dot + rel->r_addend;
1498 # if defined(__SH5__)
1499 case R_SH_IMM_MEDLOW16:
1500 case R_SH_IMM_LOW16:
1504 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1508 * movi and shori have the format:
1510 * | op | imm | reg | reserved |
1511 * 31..26 25..10 9.. 4 3 .. 0
1513 * so we simply mask and or in imm.
1515 word = *loc & ~0x3fffc00;
1516 word |= (v & 0xffff) << 10;
1523 case R_SH_IMM_MEDLOW16_PCREL:
1524 case R_SH_IMM_LOW16_PCREL:
1528 word = *loc & ~0x3fffc00;
1532 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1535 word |= (v & 0xffff) << 10;
1541 # endif /* __SH5__ */
1543 #elif defined (__v850e__)
1549 /* We write two shorts instead of a long because even
1550 32-bit insns only need half-word alignment, but
1551 32-bit data needs to be long-word aligned. */
1552 v += ((unsigned short *)loc)[0];
1553 v += ((unsigned short *)loc)[1] << 16;
1554 ((unsigned short *)loc)[0] = v & 0xffff;
1555 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1558 case R_V850_22_PCREL:
1561 #elif defined(__x86_64__)
1571 *(unsigned int *) loc += v;
1574 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1575 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1580 *(signed int *) loc += v;
1584 *(unsigned short *) loc += v;
1588 *(unsigned char *) loc += v;
1592 *(unsigned int *) loc += v - dot;
1596 *(unsigned short *) loc += v - dot;
1600 *(unsigned char *) loc += v - dot;
1603 case R_X86_64_GLOB_DAT:
1604 case R_X86_64_JUMP_SLOT:
1608 case R_X86_64_RELATIVE:
1609 *loc += f->baseaddr;
1612 case R_X86_64_GOT32:
1613 case R_X86_64_GOTPCREL:
1616 if (!isym->gotent.reloc_done)
1618 isym->gotent.reloc_done = 1;
1619 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1621 /* XXX are these really correct? */
1622 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1623 *(unsigned int *) loc += v + isym->gotent.offset;
1625 *loc += isym->gotent.offset;
1630 # warning "no idea how to handle relocations on your arch"
1634 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1635 ret = obj_reloc_unhandled;
1638 #if defined(USE_PLT_ENTRIES)
1642 /* find the plt entry and initialize it if necessary */
1644 #if defined(USE_PLT_LIST)
1645 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1652 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1654 /* generate some machine code */
1656 #if defined(__arm__)
1657 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1658 ip[1] = v; /* sym@ */
1660 #if defined(__powerpc__)
1661 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1662 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1663 ip[2] = 0x7d6903a6; /* mtctr r11 */
1664 ip[3] = 0x4e800420; /* bctr */
1666 #if defined (__v850e__)
1667 /* We have to trash a register, so we assume that any control
1668 transfer more than 21-bits away must be a function call
1669 (so we can use a call-clobbered register). */
1670 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1671 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1676 /* relative distance to target */
1678 /* if the target is too far away.... */
1679 #if defined (__arm__) || defined (__powerpc__)
1680 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1681 #elif defined (__v850e__)
1682 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1684 /* go via the plt */
1685 v = plt + pe->offset - dot;
1687 #if defined (__v850e__)
1692 ret = obj_reloc_dangerous;
1694 /* merge the offset into the instruction. */
1695 #if defined(__arm__)
1696 /* Convert to words. */
1699 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1701 #if defined(__powerpc__)
1702 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1704 #if defined (__v850e__)
1705 /* We write two shorts instead of a long because even 32-bit insns
1706 only need half-word alignment, but the 32-bit data write needs
1707 to be long-word aligned. */
1708 ((unsigned short *)loc)[0] =
1709 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1710 | ((v >> 16) & 0x3f); /* offs high part */
1711 ((unsigned short *)loc)[1] =
1712 (v & 0xffff); /* offs low part */
1715 #endif /* USE_PLT_ENTRIES */
1717 #if defined(USE_GOT_ENTRIES)
1720 /* needs an entry in the .got: set it, once */
1721 if (!isym->gotent.inited) {
1722 isym->gotent.inited = 1;
1723 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1725 /* make the reloc with_respect_to_.got */
1727 *loc += isym->gotent.offset + rel->r_addend;
1728 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1729 *loc += isym->gotent.offset;
1733 #endif /* USE_GOT_ENTRIES */
1740 #if defined(USE_LIST)
1742 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1743 int offset, int size)
1745 struct arch_list_entry *pe;
1747 for (pe = *list; pe != NULL; pe = pe->next) {
1748 if (pe->addend == rel->r_addend) {
1754 pe = xmalloc(sizeof(struct arch_list_entry));
1756 pe->addend = rel->r_addend;
1757 pe->offset = offset;
1767 #if defined(USE_SINGLE)
1769 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1770 int offset, int size)
1772 if (single->allocated == 0) {
1773 single->allocated = 1;
1774 single->offset = offset;
1783 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1785 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1786 int offset, int size)
1788 struct obj_section *myrelsec = obj_find_section(f, name);
1795 obj_extend_section(myrelsec, offset);
1797 myrelsec = obj_create_alloced_section(f, name,
1806 static void arch_create_got(struct obj_file *f)
1808 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1809 struct arch_file *ifile = (struct arch_file *) f;
1811 #if defined(USE_GOT_ENTRIES)
1812 int got_offset = 0, got_needed = 0, got_allocate;
1814 #if defined(USE_PLT_ENTRIES)
1815 int plt_offset = 0, plt_needed = 0, plt_allocate;
1817 struct obj_section *relsec, *symsec, *strsec;
1818 ElfW(RelM) *rel, *relend;
1819 ElfW(Sym) *symtab, *extsym;
1820 const char *strtab, *name;
1821 struct arch_symbol *intsym;
1823 for (i = 0; i < f->header.e_shnum; ++i) {
1824 relsec = f->sections[i];
1825 if (relsec->header.sh_type != SHT_RELM)
1828 symsec = f->sections[relsec->header.sh_link];
1829 strsec = f->sections[symsec->header.sh_link];
1831 rel = (ElfW(RelM) *) relsec->contents;
1832 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1833 symtab = (ElfW(Sym) *) symsec->contents;
1834 strtab = (const char *) strsec->contents;
1836 for (; rel < relend; ++rel) {
1837 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1839 #if defined(USE_GOT_ENTRIES)
1842 #if defined(USE_PLT_ENTRIES)
1846 switch (ELF_R_TYPE(rel->r_info)) {
1847 #if defined(__arm__)
1862 #elif defined(__i386__)
1872 #elif defined(__powerpc__)
1877 #elif defined(__mc68000__)
1888 #elif defined(__sh__)
1898 #elif defined (__v850e__)
1899 case R_V850_22_PCREL:
1908 if (extsym->st_name != 0) {
1909 name = strtab + extsym->st_name;
1911 name = f->sections[extsym->st_shndx]->name;
1913 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1914 #if defined(USE_GOT_ENTRIES)
1916 got_offset += arch_single_init(
1917 rel, &intsym->gotent,
1918 got_offset, GOT_ENTRY_SIZE);
1923 #if defined(USE_PLT_ENTRIES)
1925 #if defined(USE_PLT_LIST)
1926 plt_offset += arch_list_add(
1927 rel, &intsym->pltent,
1928 plt_offset, PLT_ENTRY_SIZE);
1930 plt_offset += arch_single_init(
1931 rel, &intsym->pltent,
1932 plt_offset, PLT_ENTRY_SIZE);
1940 #if defined(USE_GOT_ENTRIES)
1942 ifile->got = arch_xsect_init(f, ".got", got_offset,
1947 #if defined(USE_PLT_ENTRIES)
1949 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1954 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1957 /*======================================================================*/
1959 /* Standard ELF hash function. */
1960 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1962 unsigned long h = 0;
1969 if ((g = (h & 0xf0000000)) != 0) {
1978 static unsigned long obj_elf_hash(const char *name)
1980 return obj_elf_hash_n(name, strlen(name));
1983 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1984 /* String comparison for non-co-versioned kernel and module. */
1986 static int ncv_strcmp(const char *a, const char *b)
1988 size_t alen = strlen(a), blen = strlen(b);
1990 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1991 return strncmp(a, b, alen);
1992 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1993 return strncmp(a, b, blen);
1995 return strcmp(a, b);
1998 /* String hashing for non-co-versioned kernel and module. Here
1999 we are simply forced to drop the crc from the hash. */
2001 static unsigned long ncv_symbol_hash(const char *str)
2003 size_t len = strlen(str);
2004 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
2006 return obj_elf_hash_n(str, len);
2010 obj_set_symbol_compare(struct obj_file *f,
2011 int (*cmp) (const char *, const char *),
2012 unsigned long (*hash) (const char *))
2015 f->symbol_cmp = cmp;
2017 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
2020 f->symbol_hash = hash;
2022 memcpy(tmptab, f->symtab, sizeof(tmptab));
2023 memset(f->symtab, 0, sizeof(f->symtab));
2025 for (i = 0; i < HASH_BUCKETS; ++i)
2026 for (sym = tmptab[i]; sym; sym = next) {
2027 unsigned long h = hash(sym->name) % HASH_BUCKETS;
2029 sym->next = f->symtab[h];
2035 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2037 static struct obj_symbol *
2038 obj_add_symbol(struct obj_file *f, const char *name,
2039 unsigned long symidx, int info,
2040 int secidx, ElfW(Addr) value,
2043 struct obj_symbol *sym;
2044 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2045 int n_type = ELF_ST_TYPE(info);
2046 int n_binding = ELF_ST_BIND(info);
2048 for (sym = f->symtab[hash]; sym; sym = sym->next)
2049 if (f->symbol_cmp(sym->name, name) == 0) {
2050 int o_secidx = sym->secidx;
2051 int o_info = sym->info;
2052 int o_type = ELF_ST_TYPE(o_info);
2053 int o_binding = ELF_ST_BIND(o_info);
2055 /* A redefinition! Is it legal? */
2057 if (secidx == SHN_UNDEF)
2059 else if (o_secidx == SHN_UNDEF)
2061 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2062 /* Cope with local and global symbols of the same name
2063 in the same object file, as might have been created
2064 by ld -r. The only reason locals are now seen at this
2065 level at all is so that we can do semi-sensible things
2068 struct obj_symbol *nsym, **p;
2070 nsym = arch_new_symbol();
2071 nsym->next = sym->next;
2074 /* Excise the old (local) symbol from the hash chain. */
2075 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2079 } else if (n_binding == STB_LOCAL) {
2080 /* Another symbol of the same name has already been defined.
2081 Just add this to the local table. */
2082 sym = arch_new_symbol();
2085 f->local_symtab[symidx] = sym;
2087 } else if (n_binding == STB_WEAK)
2089 else if (o_binding == STB_WEAK)
2091 /* Don't unify COMMON symbols with object types the programmer
2093 else if (secidx == SHN_COMMON
2094 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2096 else if (o_secidx == SHN_COMMON
2097 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2100 /* Don't report an error if the symbol is coming from
2101 the kernel or some external module. */
2102 if (secidx <= SHN_HIRESERVE)
2103 bb_error_msg("%s multiply defined", name);
2108 /* Completely new symbol. */
2109 sym = arch_new_symbol();
2110 sym->next = f->symtab[hash];
2111 f->symtab[hash] = sym;
2114 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2115 if (symidx >= f->local_symtab_size)
2116 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2117 name, (long) symidx, (long) f->local_symtab_size);
2119 f->local_symtab[symidx] = sym;
2126 sym->secidx = secidx;
2132 static struct obj_symbol *
2133 obj_find_symbol(struct obj_file *f, const char *name)
2135 struct obj_symbol *sym;
2136 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2138 for (sym = f->symtab[hash]; sym; sym = sym->next)
2139 if (f->symbol_cmp(sym->name, name) == 0)
2145 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2148 if (sym->secidx >= SHN_LORESERVE)
2151 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2153 /* As a special case, a NULL sym has value zero. */
2158 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2160 int i, n = f->header.e_shnum;
2162 for (i = 0; i < n; ++i)
2163 if (strcmp(f->sections[i]->name, name) == 0)
2164 return f->sections[i];
2169 static int obj_load_order_prio(struct obj_section *a)
2171 unsigned long af, ac;
2173 af = a->header.sh_flags;
2176 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2177 strcmp(a->name + 5, ".init"))
2181 if (!(af & SHF_WRITE))
2183 if (af & SHF_EXECINSTR)
2185 if (a->header.sh_type != SHT_NOBITS)
2192 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2194 struct obj_section **p;
2195 int prio = obj_load_order_prio(sec);
2196 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2197 if (obj_load_order_prio(*p) < prio)
2199 sec->load_next = *p;
2203 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2205 unsigned long align,
2208 int newidx = f->header.e_shnum++;
2209 struct obj_section *sec;
2211 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2212 f->sections[newidx] = sec = arch_new_section();
2214 memset(sec, 0, sizeof(*sec));
2215 sec->header.sh_type = SHT_PROGBITS;
2216 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2217 sec->header.sh_size = size;
2218 sec->header.sh_addralign = align;
2222 sec->contents = xmalloc(size);
2224 obj_insert_section_load_order(f, sec);
2229 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2231 unsigned long align,
2234 int newidx = f->header.e_shnum++;
2235 struct obj_section *sec;
2237 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2238 f->sections[newidx] = sec = arch_new_section();
2240 memset(sec, 0, sizeof(*sec));
2241 sec->header.sh_type = SHT_PROGBITS;
2242 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2243 sec->header.sh_size = size;
2244 sec->header.sh_addralign = align;
2248 sec->contents = xmalloc(size);
2250 sec->load_next = f->load_order;
2251 f->load_order = sec;
2252 if (f->load_order_search_start == &f->load_order)
2253 f->load_order_search_start = &sec->load_next;
2258 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2260 unsigned long oldsize = sec->header.sh_size;
2262 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2264 return sec->contents + oldsize;
2268 /* Conditionally add the symbols from the given symbol set to the
2272 add_symbols_from( struct obj_file *f,
2273 int idx, struct new_module_symbol *syms, size_t nsyms)
2275 struct new_module_symbol *s;
2278 #ifdef SYMBOL_PREFIX
2280 size_t name_alloced_size = 0;
2282 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2285 gpl = obj_gpl_license(f, NULL) == 0;
2287 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2288 /* Only add symbols that are already marked external.
2289 If we override locals we may cause problems for
2290 argument initialization. We will also create a false
2291 dependency on the module. */
2292 struct obj_symbol *sym;
2295 /* GPL licensed modules can use symbols exported with
2296 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2297 * exported names. Non-GPL modules never see any GPLONLY_
2298 * symbols so they cannot fudge it by adding the prefix on
2301 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2302 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2309 name = (char *)s->name;
2311 #ifdef SYMBOL_PREFIX
2312 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2313 kernel exports `C names', but module object files
2314 reference `linker names'). */
2315 size_t extra = sizeof SYMBOL_PREFIX;
2316 size_t name_size = strlen (name) + extra;
2317 if (name_size > name_alloced_size) {
2318 name_alloced_size = name_size * 2;
2319 name_buf = alloca (name_alloced_size);
2321 strcpy (name_buf, SYMBOL_PREFIX);
2322 strcpy (name_buf + extra - 1, name);
2324 #endif /* SYMBOL_PREFIX */
2326 sym = obj_find_symbol(f, name);
2327 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2328 #ifdef SYMBOL_PREFIX
2329 /* Put NAME_BUF into more permanent storage. */
2330 name = xmalloc (name_size);
2331 strcpy (name, name_buf);
2333 sym = obj_add_symbol(f, name, -1,
2334 ELF_ST_INFO(STB_GLOBAL,
2337 /* Did our symbol just get installed? If so, mark the
2338 module as "used". */
2339 if (sym->secidx == idx)
2347 static void add_kernel_symbols(struct obj_file *f)
2349 struct external_module *m;
2352 /* Add module symbols first. */
2354 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2356 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2357 m->nsyms)) m->used = 1, ++nused;
2359 n_ext_modules_used = nused;
2361 /* And finally the symbols from the kernel proper. */
2364 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2367 static char *get_modinfo_value(struct obj_file *f, const char *key)
2369 struct obj_section *sec;
2370 char *p, *v, *n, *ep;
2371 size_t klen = strlen(key);
2373 sec = obj_find_section(f, ".modinfo");
2377 ep = p + sec->header.sh_size;
2380 n = strchr(p, '\0');
2382 if (p + klen == v && strncmp(p, key, klen) == 0)
2385 if (p + klen == n && strcmp(p, key) == 0)
2395 /*======================================================================*/
2396 /* Functions relating to module loading after 2.1.18. */
2399 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2402 char *p, *q, *key, *sym_name;
2403 struct obj_symbol *sym;
2404 char *contents, *loc;
2408 if ((q = strchr(p, '=')) == NULL) {
2413 key = alloca(q - p + 6);
2414 memcpy(key, "parm_", 5);
2415 memcpy(key + 5, p, q - p);
2418 p = get_modinfo_value(f, key);
2421 bb_error_msg("invalid parameter %s", key);
2425 #ifdef SYMBOL_PREFIX
2426 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2427 strcpy (sym_name, SYMBOL_PREFIX);
2428 strcat (sym_name, key);
2432 sym = obj_find_symbol(f, sym_name);
2434 /* Also check that the parameter was not resolved from the kernel. */
2435 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2436 bb_error_msg("symbol for parameter %s not found", key);
2441 min = strtoul(p, &p, 10);
2443 max = strtoul(p + 1, &p, 10);
2449 contents = f->sections[sym->secidx]->contents;
2450 loc = contents + sym->value;
2454 if ((*p == 's') || (*p == 'c')) {
2457 /* Do C quoting if we begin with a ", else slurp the lot. */
2461 str = alloca(strlen(q));
2462 for (r = str, q++; *q != '"'; ++q, ++r) {
2464 bb_error_msg("improperly terminated string argument for %s",
2467 } else if (*q == '\\')
2501 if (q[1] >= '0' && q[1] <= '7') {
2502 c = (c * 8) + *++q - '0';
2503 if (q[1] >= '0' && q[1] <= '7')
2504 c = (c * 8) + *++q - '0';
2521 /* In this case, the string is not quoted. We will break
2522 it using the coma (like for ints). If the user wants to
2523 include comas in a string, he just has to quote it */
2525 /* Search the next coma */
2529 if (r != (char *) NULL) {
2530 /* Recopy the current field */
2531 str = alloca(r - q + 1);
2532 memcpy(str, q, r - q);
2534 /* I don't know if it is useful, as the previous case
2535 doesn't nul terminate the string ??? */
2538 /* Keep next fields */
2549 obj_string_patch(f, sym->secidx, loc - contents, str);
2550 loc += tgt_sizeof_char_p;
2552 /* Array of chars (in fact, matrix !) */
2553 unsigned long charssize; /* size of each member */
2555 /* Get the size of each member */
2556 /* Probably we should do that outside the loop ? */
2557 if (!isdigit(*(p + 1))) {
2558 bb_error_msg("parameter type 'c' for %s must be followed by"
2559 " the maximum size", key);
2562 charssize = strtoul(p + 1, (char **) NULL, 10);
2565 if (strlen(str) >= charssize) {
2566 bb_error_msg("string too long for %s (max %ld)", key,
2571 /* Copy to location */
2572 strcpy((char *) loc, str);
2576 long v = strtoul(q, &q, 0);
2583 loc += tgt_sizeof_short;
2587 loc += tgt_sizeof_int;
2591 loc += tgt_sizeof_long;
2595 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2610 goto retry_end_of_value;
2614 bb_error_msg("too many values for %s (max %d)", key, max);
2621 bb_error_msg("invalid argument syntax for %s", key);
2628 bb_error_msg("too few values for %s (min %d)", key, min);
2638 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2639 static int new_is_module_checksummed(struct obj_file *f)
2641 const char *p = get_modinfo_value(f, "using_checksums");
2648 /* Get the module's kernel version in the canonical integer form. */
2651 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2656 p = get_modinfo_value(f, "kernel_version");
2659 safe_strncpy(str, p, STRVERSIONLEN);
2661 a = strtoul(p, &p, 10);
2664 b = strtoul(p + 1, &p, 10);
2667 c = strtoul(p + 1, &q, 10);
2671 return a << 16 | b << 8 | c;
2674 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2677 /* Fetch the loaded modules, and all currently exported symbols. */
2679 static int new_get_kernel_symbols(void)
2681 char *module_names, *mn;
2682 struct external_module *modules, *m;
2683 struct new_module_symbol *syms, *s;
2684 size_t ret, bufsize, nmod, nsyms, i, j;
2686 /* Collect the loaded modules. */
2688 module_names = xmalloc(bufsize = 256);
2690 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2691 if (errno == ENOSPC && bufsize < ret) {
2692 module_names = xrealloc(module_names, bufsize = ret);
2693 goto retry_modules_load;
2695 bb_perror_msg("QM_MODULES");
2699 n_ext_modules = nmod = ret;
2701 /* Collect the modules' symbols. */
2704 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2705 memset(modules, 0, nmod * sizeof(*modules));
2706 for (i = 0, mn = module_names, m = modules;
2707 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2708 struct new_module_info info;
2710 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2711 if (errno == ENOENT) {
2712 /* The module was removed out from underneath us. */
2715 bb_perror_msg("query_module: QM_INFO: %s", mn);
2719 syms = xmalloc(bufsize = 1024);
2721 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2724 syms = xrealloc(syms, bufsize = ret);
2725 goto retry_mod_sym_load;
2727 /* The module was removed out from underneath us. */
2730 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2737 m->addr = info.addr;
2741 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2742 s->name += (unsigned long) syms;
2747 /* Collect the kernel's symbols. */
2749 syms = xmalloc(bufsize = 16 * 1024);
2750 retry_kern_sym_load:
2751 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2752 if (errno == ENOSPC && bufsize < ret) {
2753 syms = xrealloc(syms, bufsize = ret);
2754 goto retry_kern_sym_load;
2756 bb_perror_msg("kernel: QM_SYMBOLS");
2759 nksyms = nsyms = ret;
2762 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2763 s->name += (unsigned long) syms;
2769 /* Return the kernel symbol checksum version, or zero if not used. */
2771 static int new_is_kernel_checksummed(void)
2773 struct new_module_symbol *s;
2776 /* Using_Versions is not the first symbol, but it should be in there. */
2778 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2779 if (strcmp((char *) s->name, "Using_Versions") == 0)
2786 static int new_create_this_module(struct obj_file *f, const char *m_name)
2788 struct obj_section *sec;
2790 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2791 sizeof(struct new_module));
2792 memset(sec->contents, 0, sizeof(struct new_module));
2794 obj_add_symbol(f, SPFX "__this_module", -1,
2795 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2796 sizeof(struct new_module));
2798 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2804 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2805 /* add an entry to the __ksymtab section, creating it if necessary */
2806 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2808 struct obj_section *sec;
2811 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2812 * If __ksymtab is defined but not marked alloc, x out the first character
2813 * (no obj_delete routine) and create a new __ksymtab with the correct
2816 sec = obj_find_section(f, "__ksymtab");
2817 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2818 *((char *)(sec->name)) = 'x'; /* override const */
2822 sec = obj_create_alloced_section(f, "__ksymtab",
2823 tgt_sizeof_void_p, 0);
2826 sec->header.sh_flags |= SHF_ALLOC;
2827 /* Empty section might be byte-aligned */
2828 sec->header.sh_addralign = tgt_sizeof_void_p;
2829 ofs = sec->header.sh_size;
2830 obj_symbol_patch(f, sec->idx, ofs, sym);
2831 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2832 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2834 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2836 static int new_create_module_ksymtab(struct obj_file *f)
2838 struct obj_section *sec;
2841 /* We must always add the module references. */
2843 if (n_ext_modules_used) {
2844 struct new_module_ref *dep;
2845 struct obj_symbol *tm;
2847 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2848 (sizeof(struct new_module_ref)
2849 * n_ext_modules_used));
2853 tm = obj_find_symbol(f, SPFX "__this_module");
2854 dep = (struct new_module_ref *) sec->contents;
2855 for (i = 0; i < n_ext_modules; ++i)
2856 if (ext_modules[i].used) {
2857 dep->dep = ext_modules[i].addr;
2858 obj_symbol_patch(f, sec->idx,
2859 (char *) &dep->ref - sec->contents, tm);
2865 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2869 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2871 /* We don't want to export symbols residing in sections that
2872 aren't loaded. There are a number of these created so that
2873 we make sure certain module options don't appear twice. */
2875 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2877 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2879 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2880 struct obj_symbol *sym;
2881 for (sym = f->symtab[i]; sym; sym = sym->next)
2882 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2883 && sym->secidx <= SHN_HIRESERVE
2884 && (sym->secidx >= SHN_LORESERVE
2885 || loaded[sym->secidx])) {
2886 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2888 obj_symbol_patch(f, sec->idx, ofs, sym);
2889 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2896 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2904 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2906 struct new_module *module;
2907 struct obj_section *sec;
2912 sec = obj_find_section(f, ".this");
2913 if (!sec || !sec->contents) {
2914 bb_perror_msg_and_die("corrupt module %s?",m_name);
2916 module = (struct new_module *) sec->contents;
2917 m_addr = sec->header.sh_addr;
2919 module->size_of_struct = sizeof(*module);
2920 module->size = m_size;
2921 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2923 sec = obj_find_section(f, "__ksymtab");
2924 if (sec && sec->header.sh_size) {
2925 module->syms = sec->header.sh_addr;
2926 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2929 if (n_ext_modules_used) {
2930 sec = obj_find_section(f, ".kmodtab");
2931 module->deps = sec->header.sh_addr;
2932 module->ndeps = n_ext_modules_used;
2936 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2938 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2940 sec = obj_find_section(f, "__ex_table");
2942 module->ex_table_start = sec->header.sh_addr;
2943 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2946 sec = obj_find_section(f, ".text.init");
2948 module->runsize = sec->header.sh_addr - m_addr;
2950 sec = obj_find_section(f, ".data.init");
2952 if (!module->runsize ||
2953 module->runsize > sec->header.sh_addr - m_addr)
2954 module->runsize = sec->header.sh_addr - m_addr;
2956 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2957 if (sec && sec->header.sh_size) {
2958 module->archdata_start = (void*)sec->header.sh_addr;
2959 module->archdata_end = module->archdata_start + sec->header.sh_size;
2961 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2962 if (sec && sec->header.sh_size) {
2963 module->kallsyms_start = (void*)sec->header.sh_addr;
2964 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2967 /* Whew! All of the initialization is complete. Collect the final
2968 module image and give it to the kernel. */
2970 image = xmalloc(m_size);
2971 obj_create_image(f, image);
2973 ret = init_module(m_name, (struct new_module *) image);
2975 bb_perror_msg("init_module: %s", m_name);
2983 /*======================================================================*/
2986 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2989 struct obj_string_patch *p;
2990 struct obj_section *strsec;
2991 size_t len = strlen(string) + 1;
2994 p = xmalloc(sizeof(*p));
2995 p->next = f->string_patches;
2996 p->reloc_secidx = secidx;
2997 p->reloc_offset = offset;
2998 f->string_patches = p;
3000 strsec = obj_find_section(f, ".kstrtab");
3001 if (strsec == NULL) {
3002 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3003 p->string_offset = 0;
3004 loc = strsec->contents;
3006 p->string_offset = strsec->header.sh_size;
3007 loc = obj_extend_section(strsec, len);
3009 memcpy(loc, string, len);
3015 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3016 struct obj_symbol *sym)
3018 struct obj_symbol_patch *p;
3020 p = xmalloc(sizeof(*p));
3021 p->next = f->symbol_patches;
3022 p->reloc_secidx = secidx;
3023 p->reloc_offset = offset;
3025 f->symbol_patches = p;
3030 static int obj_check_undefineds(struct obj_file *f)
3035 for (i = 0; i < HASH_BUCKETS; ++i) {
3036 struct obj_symbol *sym;
3037 for (sym = f->symtab[i]; sym; sym = sym->next)
3038 if (sym->secidx == SHN_UNDEF) {
3039 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3040 sym->secidx = SHN_ABS;
3044 bb_error_msg("unresolved symbol %s", sym->name);
3054 static void obj_allocate_commons(struct obj_file *f)
3056 struct common_entry {
3057 struct common_entry *next;
3058 struct obj_symbol *sym;
3059 } *common_head = NULL;
3063 for (i = 0; i < HASH_BUCKETS; ++i) {
3064 struct obj_symbol *sym;
3065 for (sym = f->symtab[i]; sym; sym = sym->next)
3066 if (sym->secidx == SHN_COMMON) {
3067 /* Collect all COMMON symbols and sort them by size so as to
3068 minimize space wasted by alignment requirements. */
3070 struct common_entry **p, *n;
3071 for (p = &common_head; *p; p = &(*p)->next)
3072 if (sym->size <= (*p)->sym->size)
3075 n = alloca(sizeof(*n));
3083 for (i = 1; i < f->local_symtab_size; ++i) {
3084 struct obj_symbol *sym = f->local_symtab[i];
3085 if (sym && sym->secidx == SHN_COMMON) {
3086 struct common_entry **p, *n;
3087 for (p = &common_head; *p; p = &(*p)->next)
3088 if (sym == (*p)->sym)
3090 else if (sym->size < (*p)->sym->size) {
3091 n = alloca(sizeof(*n));
3101 /* Find the bss section. */
3102 for (i = 0; i < f->header.e_shnum; ++i)
3103 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3106 /* If for some reason there hadn't been one, create one. */
3107 if (i == f->header.e_shnum) {
3108 struct obj_section *sec;
3110 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3111 f->sections[i] = sec = arch_new_section();
3112 f->header.e_shnum = i + 1;
3114 memset(sec, 0, sizeof(*sec));
3115 sec->header.sh_type = SHT_PROGBITS;
3116 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3121 /* Allocate the COMMONS. */
3123 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3124 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3125 struct common_entry *c;
3127 for (c = common_head; c; c = c->next) {
3128 ElfW(Addr) align = c->sym->value;
3130 if (align > max_align)
3132 if (bss_size & (align - 1))
3133 bss_size = (bss_size | (align - 1)) + 1;
3136 c->sym->value = bss_size;
3138 bss_size += c->sym->size;
3141 f->sections[i]->header.sh_size = bss_size;
3142 f->sections[i]->header.sh_addralign = max_align;
3146 /* For the sake of patch relocation and parameter initialization,
3147 allocate zeroed data for NOBITS sections now. Note that after
3148 this we cannot assume NOBITS are really empty. */
3149 for (i = 0; i < f->header.e_shnum; ++i) {
3150 struct obj_section *s = f->sections[i];
3151 if (s->header.sh_type == SHT_NOBITS) {
3152 if (s->header.sh_size != 0)
3153 s->contents = memset(xmalloc(s->header.sh_size),
3154 0, s->header.sh_size);
3158 s->header.sh_type = SHT_PROGBITS;
3163 static unsigned long obj_load_size(struct obj_file *f)
3165 unsigned long dot = 0;
3166 struct obj_section *sec;
3168 /* Finalize the positions of the sections relative to one another. */
3170 for (sec = f->load_order; sec; sec = sec->load_next) {
3173 align = sec->header.sh_addralign;
3174 if (align && (dot & (align - 1)))
3175 dot = (dot | (align - 1)) + 1;
3177 sec->header.sh_addr = dot;
3178 dot += sec->header.sh_size;
3184 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3186 int i, n = f->header.e_shnum;
3189 /* Finalize the addresses of the sections. */
3192 for (i = 0; i < n; ++i)
3193 f->sections[i]->header.sh_addr += base;
3195 /* And iterate over all of the relocations. */
3197 for (i = 0; i < n; ++i) {
3198 struct obj_section *relsec, *symsec, *targsec, *strsec;
3199 ElfW(RelM) * rel, *relend;
3203 relsec = f->sections[i];
3204 if (relsec->header.sh_type != SHT_RELM)
3207 symsec = f->sections[relsec->header.sh_link];
3208 targsec = f->sections[relsec->header.sh_info];
3209 strsec = f->sections[symsec->header.sh_link];
3211 rel = (ElfW(RelM) *) relsec->contents;
3212 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3213 symtab = (ElfW(Sym) *) symsec->contents;
3214 strtab = (const char *) strsec->contents;
3216 for (; rel < relend; ++rel) {
3217 ElfW(Addr) value = 0;
3218 struct obj_symbol *intsym = NULL;
3219 unsigned long symndx;
3220 ElfW(Sym) * extsym = 0;
3223 /* Attempt to find a value to use for this relocation. */
3225 symndx = ELF_R_SYM(rel->r_info);
3227 /* Note we've already checked for undefined symbols. */
3229 extsym = &symtab[symndx];
3230 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3231 /* Local symbols we look up in the local table to be sure
3232 we get the one that is really intended. */
3233 intsym = f->local_symtab[symndx];
3235 /* Others we look up in the hash table. */
3237 if (extsym->st_name)
3238 name = strtab + extsym->st_name;
3240 name = f->sections[extsym->st_shndx]->name;
3241 intsym = obj_find_symbol(f, name);
3244 value = obj_symbol_final_value(f, intsym);
3245 intsym->referenced = 1;
3247 #if SHT_RELM == SHT_RELA
3248 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3249 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3250 if (!extsym || !extsym->st_name ||
3251 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3253 value += rel->r_addend;
3257 switch (arch_apply_relocation
3258 (f, targsec, symsec, intsym, rel, value)
3263 case obj_reloc_overflow:
3264 errmsg = "Relocation overflow";
3266 case obj_reloc_dangerous:
3267 errmsg = "Dangerous relocation";
3269 case obj_reloc_unhandled:
3270 errmsg = "Unhandled relocation";
3273 bb_error_msg("%s of type %ld for %s", errmsg,
3274 (long) ELF_R_TYPE(rel->r_info),
3275 strtab + extsym->st_name);
3277 bb_error_msg("%s of type %ld", errmsg,
3278 (long) ELF_R_TYPE(rel->r_info));
3286 /* Finally, take care of the patches. */
3288 if (f->string_patches) {
3289 struct obj_string_patch *p;
3290 struct obj_section *strsec;
3291 ElfW(Addr) strsec_base;
3292 strsec = obj_find_section(f, ".kstrtab");
3293 strsec_base = strsec->header.sh_addr;
3295 for (p = f->string_patches; p; p = p->next) {
3296 struct obj_section *targsec = f->sections[p->reloc_secidx];
3297 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3298 = strsec_base + p->string_offset;
3302 if (f->symbol_patches) {
3303 struct obj_symbol_patch *p;
3305 for (p = f->symbol_patches; p; p = p->next) {
3306 struct obj_section *targsec = f->sections[p->reloc_secidx];
3307 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3308 = obj_symbol_final_value(f, p->sym);
3315 static int obj_create_image(struct obj_file *f, char *image)
3317 struct obj_section *sec;
3318 ElfW(Addr) base = f->baseaddr;
3320 for (sec = f->load_order; sec; sec = sec->load_next) {
3323 if (sec->contents == 0 || sec->header.sh_size == 0)
3326 secimg = image + (sec->header.sh_addr - base);
3328 /* Note that we allocated data for NOBITS sections earlier. */
3329 memcpy(secimg, sec->contents, sec->header.sh_size);
3335 /*======================================================================*/
3337 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3340 ElfW(Shdr) * section_headers;
3344 /* Read the file header. */
3346 f = arch_new_file();
3347 memset(f, 0, sizeof(*f));
3348 f->symbol_cmp = strcmp;
3349 f->symbol_hash = obj_elf_hash;
3350 f->load_order_search_start = &f->load_order;
3352 fseek(fp, 0, SEEK_SET);
3353 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3354 bb_perror_msg("error reading ELF header");
3358 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3359 || f->header.e_ident[EI_MAG1] != ELFMAG1
3360 || f->header.e_ident[EI_MAG2] != ELFMAG2
3361 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3362 bb_error_msg("not an ELF file");
3365 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3366 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3367 ? ELFDATA2MSB : ELFDATA2LSB)
3368 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3369 || !MATCH_MACHINE(f->header.e_machine)) {
3370 bb_error_msg("ELF file not for this architecture");
3373 if (f->header.e_type != ET_REL) {
3374 bb_error_msg("ELF file not a relocatable object");
3378 /* Read the section headers. */
3380 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3381 bb_error_msg("section header size mismatch: %lu != %lu",
3382 (unsigned long) f->header.e_shentsize,
3383 (unsigned long) sizeof(ElfW(Shdr)));
3387 shnum = f->header.e_shnum;
3388 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3389 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3391 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3392 fseek(fp, f->header.e_shoff, SEEK_SET);
3393 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3394 bb_perror_msg("error reading ELF section headers");
3398 /* Read the section data. */
3400 for (i = 0; i < shnum; ++i) {
3401 struct obj_section *sec;
3403 f->sections[i] = sec = arch_new_section();
3404 memset(sec, 0, sizeof(*sec));
3406 sec->header = section_headers[i];
3409 if(sec->header.sh_size) {
3410 switch (sec->header.sh_type) {
3419 if (!loadprogbits) {
3420 sec->contents = NULL;
3427 if (sec->header.sh_size > 0) {
3428 sec->contents = xmalloc(sec->header.sh_size);
3429 fseek(fp, sec->header.sh_offset, SEEK_SET);
3430 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3431 bb_perror_msg("error reading ELF section data");
3435 sec->contents = NULL;
3439 #if SHT_RELM == SHT_REL
3441 bb_error_msg("RELA relocations not supported on this architecture");
3445 bb_error_msg("REL relocations not supported on this architecture");
3450 if (sec->header.sh_type >= SHT_LOPROC) {
3451 /* Assume processor specific section types are debug
3452 info and can safely be ignored. If this is ever not
3453 the case (Hello MIPS?), don't put ifdefs here but
3454 create an arch_load_proc_section(). */
3458 bb_error_msg("can't handle sections of type %ld",
3459 (long) sec->header.sh_type);
3465 /* Do what sort of interpretation as needed by each section. */
3467 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3469 for (i = 0; i < shnum; ++i) {
3470 struct obj_section *sec = f->sections[i];
3471 sec->name = shstrtab + sec->header.sh_name;
3474 for (i = 0; i < shnum; ++i) {
3475 struct obj_section *sec = f->sections[i];
3477 /* .modinfo should be contents only but gcc has no attribute for that.
3478 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3480 if (strcmp(sec->name, ".modinfo") == 0)
3481 sec->header.sh_flags &= ~SHF_ALLOC;
3483 if (sec->header.sh_flags & SHF_ALLOC)
3484 obj_insert_section_load_order(f, sec);
3486 switch (sec->header.sh_type) {
3489 unsigned long nsym, j;
3493 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3494 bb_error_msg("symbol size mismatch: %lu != %lu",
3495 (unsigned long) sec->header.sh_entsize,
3496 (unsigned long) sizeof(ElfW(Sym)));
3500 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3501 strtab = f->sections[sec->header.sh_link]->contents;
3502 sym = (ElfW(Sym) *) sec->contents;
3504 /* Allocate space for a table of local symbols. */
3505 j = f->local_symtab_size = sec->header.sh_info;
3506 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3508 /* Insert all symbols into the hash table. */
3509 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3510 ElfW(Addr) val = sym->st_value;
3513 name = strtab + sym->st_name;
3514 else if (sym->st_shndx < shnum)
3515 name = f->sections[sym->st_shndx]->name;
3518 #if defined(__SH5__)
3520 * For sh64 it is possible that the target of a branch
3521 * requires a mode switch (32 to 16 and back again).
3523 * This is implied by the lsb being set in the target
3524 * address for SHmedia mode and clear for SHcompact.
3526 val |= sym->st_other & 4;
3529 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3536 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3537 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3538 (unsigned long) sec->header.sh_entsize,
3539 (unsigned long) sizeof(ElfW(RelM)));
3543 /* XXX Relocation code from modutils-2.3.19 is not here.
3544 * Why? That's about 20 lines of code from obj/obj_load.c,
3545 * which gets done in a second pass through the sections.
3546 * This BusyBox insmod does similar work in obj_relocate(). */
3553 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3555 * load the unloaded sections directly into the memory allocated by
3556 * kernel for the module
3559 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3561 ElfW(Addr) base = f->baseaddr;
3562 struct obj_section* sec;
3564 for (sec = f->load_order; sec; sec = sec->load_next) {
3566 /* section already loaded? */
3567 if (sec->contents != NULL)
3570 if (sec->header.sh_size == 0)
3573 sec->contents = imagebase + (sec->header.sh_addr - base);
3574 fseek(fp, sec->header.sh_offset, SEEK_SET);
3575 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3576 bb_perror_msg("error reading ELF section data");
3585 static void hide_special_symbols(struct obj_file *f)
3587 static const char *const specials[] = {
3588 SPFX "cleanup_module",
3590 SPFX "kernel_version",
3594 struct obj_symbol *sym;
3595 const char *const *p;
3597 for (p = specials; *p; ++p) {
3598 sym = obj_find_symbol(f, *p);
3600 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3605 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3606 static int obj_gpl_license(struct obj_file *f, const char **license)
3608 struct obj_section *sec;
3609 /* This list must match *exactly* the list of allowable licenses in
3610 * linux/include/linux/module.h. Checking for leading "GPL" will not
3611 * work, somebody will use "GPL sucks, this is proprietary".
3613 static const char * const gpl_licenses[] = {
3616 "GPL and additional rights",
3621 sec = obj_find_section(f, ".modinfo");
3623 const char *value, *ptr, *endptr;
3624 ptr = sec->contents;
3625 endptr = ptr + sec->header.sh_size;
3626 while (ptr < endptr) {
3627 value = strchr(ptr, '=');
3628 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3632 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3633 if (strcmp(value+1, gpl_licenses[i]) == 0)
3638 if (strchr(ptr, '\0'))
3639 ptr = strchr(ptr, '\0') + 1;
3647 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3648 #define TAINT_PROPRIETORY_MODULE (1<<0)
3649 #define TAINT_FORCED_MODULE (1<<1)
3650 #define TAINT_UNSAFE_SMP (1<<2)
3651 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3653 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3654 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3658 static int first = 1;
3659 if (fd < 0 && !kernel_has_tainted)
3660 return; /* New modutils on old kernel */
3661 printf("Warning: loading %s will taint the kernel: %s%s\n",
3662 m_name, text1, text2);
3664 printf(" See %s for information about tainted modules\n", TAINT_URL);
3668 read(fd, buf, sizeof(buf)-1);
3669 buf[sizeof(buf)-1] = '\0';
3670 oldval = strtoul(buf, NULL, 10);
3671 sprintf(buf, "%d\n", oldval | taint);
3672 write(fd, buf, strlen(buf));
3676 /* Check if loading this module will taint the kernel. */
3677 static void check_tainted_module(struct obj_file *f, char *m_name)
3679 static const char tainted_file[] = TAINT_FILENAME;
3680 int fd, kernel_has_tainted;
3683 kernel_has_tainted = 1;
3684 fd = open(tainted_file, O_RDWR);
3686 if (errno == ENOENT)
3687 kernel_has_tainted = 0;
3688 else if (errno == EACCES)
3689 kernel_has_tainted = 1;
3691 perror(tainted_file);
3692 kernel_has_tainted = 0;
3696 switch (obj_gpl_license(f, &ptr)) {
3700 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3703 /* The module has a non-GPL license so we pretend that the
3704 * kernel always has a taint flag to get a warning even on
3705 * kernels without the proc flag.
3707 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3710 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3714 if (flag_force_load)
3715 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3720 #else /* FEATURE_CHECK_TAINTED_MODULE */
3721 #define check_tainted_module(x, y) do { } while (0);
3722 #endif /* FEATURE_CHECK_TAINTED_MODULE */
3724 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3725 /* add module source, timestamp, kernel version and a symbol for the
3726 * start of some sections. this info is used by ksymoops to do better
3730 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3732 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3733 return new_get_module_version(f, str);
3734 #else /* FEATURE_INSMOD_VERSION_CHECKING */
3735 strncpy(str, "???", sizeof(str));
3737 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
3740 /* add module source, timestamp, kernel version and a symbol for the
3741 * start of some sections. this info is used by ksymoops to do better
3745 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3748 static const char symprefix[] = "__insmod_";
3749 struct obj_section *sec;
3750 struct obj_symbol *sym;
3751 char *name, *absolute_filename;
3752 char str[STRVERSIONLEN], real[PATH_MAX];
3753 int i, l, lm_name, lfilename, use_ksymtab, version;
3754 struct stat statbuf;
3756 static const char *section_names[] = {
3764 if (realpath(filename, real)) {
3765 absolute_filename = xstrdup(real);
3767 bb_perror_msg("cannot get realpath for %s", filename);
3768 absolute_filename = xstrdup(filename);
3771 lm_name = strlen(m_name);
3772 lfilename = strlen(absolute_filename);
3774 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3775 * are not to be exported. otherwise leave ksymtab alone for now, the
3776 * "export all symbols" compatibility code will export these symbols later.
3778 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3780 sec = obj_find_section(f, ".this");
3782 /* tag the module header with the object name, last modified
3783 * timestamp and module version. worst case for module version
3784 * is 0xffffff, decimal 16777215. putting all three fields in
3785 * one symbol is less readable but saves kernel space.
3787 l = sizeof(symprefix)+ /* "__insmod_" */
3788 lm_name+ /* module name */
3790 lfilename+ /* object filename */
3792 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3794 8+ /* version in dec */
3797 if (stat(absolute_filename, &statbuf) != 0)
3798 statbuf.st_mtime = 0;
3799 version = get_module_version(f, str); /* -1 if not found */
3800 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3801 symprefix, m_name, absolute_filename,
3802 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3804 sym = obj_add_symbol(f, name, -1,
3805 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3806 sec->idx, sec->header.sh_addr, 0);
3808 new_add_ksymtab(f, sym);
3810 free(absolute_filename);
3811 #ifdef _NOT_SUPPORTED_
3812 /* record where the persistent data is going, same address as previous symbol */
3815 l = sizeof(symprefix)+ /* "__insmod_" */
3816 lm_name+ /* module name */
3818 strlen(f->persist)+ /* data store */
3821 snprintf(name, l, "%s%s_P%s",
3822 symprefix, m_name, f->persist);
3823 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3824 sec->idx, sec->header.sh_addr, 0);
3826 new_add_ksymtab(f, sym);
3828 #endif /* _NOT_SUPPORTED_ */
3829 /* tag the desired sections if size is non-zero */
3831 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3832 sec = obj_find_section(f, section_names[i]);
3833 if (sec && sec->header.sh_size) {
3834 l = sizeof(symprefix)+ /* "__insmod_" */
3835 lm_name+ /* module name */
3837 strlen(sec->name)+ /* section name */
3839 8+ /* length in dec */
3842 snprintf(name, l, "%s%s_S%s_L%ld",
3843 symprefix, m_name, sec->name,
3844 (long)sec->header.sh_size);
3845 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3846 sec->idx, sec->header.sh_addr, 0);
3848 new_add_ksymtab(f, sym);
3852 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3854 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3855 static void print_load_map(struct obj_file *f)
3857 struct obj_section *sec;
3858 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3859 struct obj_symbol **all, **p;
3860 int i, nsyms, *loaded;
3861 struct obj_symbol *sym;
3863 /* Report on the section layout. */
3865 printf("Sections: Size %-*s Align\n",
3866 (int) (2 * sizeof(void *)), "Address");
3868 for (sec = f->load_order; sec; sec = sec->load_next) {
3872 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3877 printf("%-15s %08lx %0*lx 2**%d\n",
3879 (long)sec->header.sh_size,
3880 (int) (2 * sizeof(void *)),
3881 (long)sec->header.sh_addr,
3884 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3885 /* Quick reference which section indicies are loaded. */
3887 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3889 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3891 /* Collect the symbols we'll be listing. */
3893 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3894 for (sym = f->symtab[i]; sym; sym = sym->next)
3895 if (sym->secidx <= SHN_HIRESERVE
3896 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3899 all = alloca(nsyms * sizeof(struct obj_symbol *));
3901 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3902 for (sym = f->symtab[i]; sym; sym = sym->next)
3903 if (sym->secidx <= SHN_HIRESERVE
3904 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3907 /* And list them. */
3908 printf("\nSymbols:\n");
3909 for (p = all; p < all + nsyms; ++p) {
3911 unsigned long value;
3914 if (sym->secidx == SHN_ABS) {
3917 } else if (sym->secidx == SHN_UNDEF) {
3921 sec = f->sections[sym->secidx];
3923 if (sec->header.sh_type == SHT_NOBITS)
3925 else if (sec->header.sh_flags & SHF_ALLOC) {
3926 if (sec->header.sh_flags & SHF_EXECINSTR)
3928 else if (sec->header.sh_flags & SHF_WRITE)
3933 value = sym->value + sec->header.sh_addr;
3936 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3937 type = tolower(type);
3939 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3944 #else /* !FEATURE_INSMOD_LOAD_MAP */
3945 void print_load_map(struct obj_file *f);
3948 int insmod_main( int argc, char **argv)
3954 unsigned long m_size;
3959 int exit_status = EXIT_FAILURE;
3961 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3962 struct utsname uts_info;
3963 char m_strversion[STRVERSIONLEN];
3964 int m_version, m_crcs;
3966 #if ENABLE_FEATURE_CLEAN_UP
3972 struct utsname myuname;
3974 /* Parse any options */
3975 getopt32(argc, argv, OPTION_STR, &opt_o);
3976 arg1 = argv[optind];
3977 if (option_mask32 & OPT_o) { // -o /* name the output module */
3979 m_name = xstrdup(opt_o);
3986 /* Grab the module name */
3987 tmp1 = xstrdup(arg1);
3988 tmp = basename(tmp1);
3991 if (uname(&myuname) == 0) {
3992 if (myuname.release[0] == '2') {
3993 k_version = myuname.release[2] - '0';
3997 #if ENABLE_FEATURE_2_6_MODULES
3998 if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
3999 && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
4005 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4011 #if ENABLE_FEATURE_2_6_MODULES
4013 m_fullName = xasprintf("%s.ko", tmp);
4016 m_fullName = xasprintf("%s.o", tmp);
4022 tmp1 = 0; /* flag for free(m_name) before exit() */
4025 /* Get a filedesc for the module. Check we we have a complete path */
4026 if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
4027 || (fp = fopen(arg1, "r")) == NULL
4029 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4030 * but do not error out yet if we fail to find it... */
4031 if (k_version) { /* uname succeedd */
4034 char real_module_dir[FILENAME_MAX];
4036 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4037 /* Jump through hoops in case /lib/modules/`uname -r`
4038 * is a symlink. We do not want recursive_action to
4039 * follow symlinks, but we do want to follow the
4040 * /lib/modules/`uname -r` dir, So resolve it ourselves
4041 * if it is a link... */
4042 if (realpath(tmdn, real_module_dir) == NULL)
4045 module_dir = real_module_dir;
4046 recursive_action(module_dir, TRUE, FALSE, FALSE,
4047 check_module_name_match, 0, m_fullName, 0);
4051 /* Check if we have found anything yet */
4052 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) {
4053 char module_dir[FILENAME_MAX];
4057 if (realpath (_PATH_MODULES, module_dir) == NULL)
4058 strcpy(module_dir, _PATH_MODULES);
4059 /* No module found under /lib/modules/`uname -r`, this
4060 * time cast the net a bit wider. Search /lib/modules/ */
4061 if (!recursive_action(module_dir, TRUE, FALSE, FALSE,
4062 check_module_name_match, 0, m_fullName, 0)
4065 || ((fp = fopen(m_filename, "r")) == NULL)
4067 bb_error_msg("%s: no module by that name found", m_fullName);
4071 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4074 m_filename = xstrdup(arg1);
4077 printf("Using %s\n", m_filename);
4079 #if ENABLE_FEATURE_2_6_MODULES
4080 if (k_version > 4) {
4081 argv[optind] = m_filename;
4083 return insmod_ng_main(argc - optind, argv + optind);
4087 f = obj_load(fp, LOADBITS);
4089 bb_perror_msg_and_die("cannot load the module");
4091 if (get_modinfo_value(f, "kernel_version") == NULL)
4096 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4097 /* Version correspondence? */
4099 if (uname(&uts_info) < 0)
4100 uts_info.release[0] = '\0';
4101 if (m_has_modinfo) {
4102 m_version = new_get_module_version(f, m_strversion);
4103 if (m_version == -1) {
4104 bb_error_msg("cannot find the kernel version the module was "
4110 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4111 if (flag_force_load) {
4112 bb_error_msg("warning: kernel-module version mismatch\n"
4113 "\t%s was compiled for kernel version %s\n"
4114 "\twhile this kernel is version %s",
4115 m_filename, m_strversion, uts_info.release);
4117 bb_error_msg("kernel-module version mismatch\n"
4118 "\t%s was compiled for kernel version %s\n"
4119 "\twhile this kernel is version %s.",
4120 m_filename, m_strversion, uts_info.release);
4126 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4128 if (!query_module(NULL, 0, NULL, 0, NULL)) {
4129 if (!new_get_kernel_symbols())
4131 k_crcs = new_is_kernel_checksummed();
4133 bb_error_msg("not configured to support old kernels");
4137 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4140 m_crcs = new_is_module_checksummed(f);
4142 if (m_crcs != k_crcs)
4143 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4144 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4146 /* Let the module know about the kernel symbols. */
4147 add_kernel_symbols(f);
4149 /* Allocate common symbols, symbol tables, and string tables. */
4151 if (!new_create_this_module(f, m_name)) {
4155 if (!obj_check_undefineds(f)) {
4158 obj_allocate_commons(f);
4159 check_tainted_module(f, m_name);
4161 /* done with the module name, on to the optional var=value arguments */
4163 if (optind < argc) {
4164 if (!new_process_module_arguments(f, argc - optind, argv + optind)) {
4170 hide_special_symbols(f);
4172 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4173 add_ksymoops_symbols(f, m_filename, m_name);
4174 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4176 new_create_module_ksymtab(f);
4178 /* Find current size of the module */
4179 m_size = obj_load_size(f);
4182 m_addr = create_module(m_name, m_size);
4183 if (m_addr == -1) switch (errno) {
4185 bb_error_msg("a module named %s already exists", m_name);
4188 bb_error_msg("can't allocate kernel memory for module; needed %lu bytes",
4192 bb_perror_msg("create_module: %s", m_name);
4198 * the PROGBITS section was not loaded by the obj_load
4199 * now we can load them directly into the kernel memory
4201 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4202 delete_module(m_name);
4207 if (!obj_relocate(f, m_addr)) {
4208 delete_module(m_name);
4212 if (!new_init_module(m_name, f, m_size)) {
4213 delete_module(m_name);
4217 if(flag_print_load_map)
4220 exit_status = EXIT_SUCCESS;
4223 #if ENABLE_FEATURE_CLEAN_UP
4238 #if ENABLE_FEATURE_2_6_MODULES
4240 #include <sys/mman.h>
4241 #include <asm/unistd.h>
4242 #include <sys/syscall.h>
4244 /* We use error numbers in a loose translation... */
4245 static const char *moderror(int err)
4249 return "Invalid module format";
4251 return "Unknown symbol in module";
4253 return "Module has wrong symbol version";
4255 return "Invalid parameters";
4257 return strerror(err);
4261 int insmod_ng_main(int argc, char **argv)
4266 char *filename, *options;
4272 /* Rest is options */
4273 options = xstrdup("");
4275 int optlen = strlen(options);
4276 options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
4277 /* Spaces handled by "" pairs, but no way of escaping quotes */
4278 sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
4282 /* Any special reason why mmap? It isn't performace critical... */
4286 fd = xopen(filename, O_RDONLY);
4289 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4290 if (map == MAP_FAILED) {
4291 bb_perror_msg_and_die("cannot mmap '%s'", filename);
4294 /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
4297 xread(fd, map, len);
4300 len = MAXINT(ssize_t);
4301 map = xmalloc_open_read_close(filename, &len);
4304 ret = syscall(__NR_init_module, map, len, options);
4306 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
4307 filename, moderror(errno), ret);