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
71 * Big piece of 2.4-specific code
73 #if ENABLE_FEATURE_2_4_MODULES
75 #if ENABLE_FEATURE_2_6_MODULES
76 static int insmod_ng_main(int argc, char **argv);
79 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
86 #if defined(__alpha__)
87 #define MATCH_MACHINE(x) (x == EM_ALPHA)
88 #define SHT_RELM SHT_RELA
89 #define Elf64_RelM Elf64_Rela
90 #define ELFCLASSM ELFCLASS64
95 #define MATCH_MACHINE(x) (x == EM_ARM)
96 #define SHT_RELM SHT_REL
97 #define Elf32_RelM Elf32_Rel
98 #define ELFCLASSM ELFCLASS32
99 #define USE_PLT_ENTRIES
100 #define PLT_ENTRY_SIZE 8
101 #define USE_GOT_ENTRIES
102 #define GOT_ENTRY_SIZE 8
108 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
109 #define SHT_RELM SHT_RELA
110 #define Elf32_RelM Elf32_Rela
111 #define ELFCLASSM ELFCLASS32
115 #if defined(__cris__)
116 #define MATCH_MACHINE(x) (x == EM_CRIS)
117 #define SHT_RELM SHT_RELA
118 #define Elf32_RelM Elf32_Rela
119 #define ELFCLASSM ELFCLASS32
122 #define R_CRIS_NONE 0
128 #if defined(__H8300H__) || defined(__H8300S__)
129 #define MATCH_MACHINE(x) (x == EM_H8_300)
130 #define SHT_RELM SHT_RELA
131 #define Elf32_RelM Elf32_Rela
132 #define ELFCLASSM ELFCLASS32
134 #define SYMBOL_PREFIX "_"
137 /* PA-RISC / HP-PA */
138 #if defined(__hppa__)
139 #define MATCH_MACHINE(x) (x == EM_PARISC)
140 #define SHT_RELM SHT_RELA
141 #if defined(__LP64__)
142 #define Elf64_RelM Elf64_Rela
143 #define ELFCLASSM ELFCLASS64
145 #define Elf32_RelM Elf32_Rela
146 #define ELFCLASSM ELFCLASS32
151 #if defined(__i386__)
153 #define MATCH_MACHINE(x) (x == EM_386)
155 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
157 #define SHT_RELM SHT_REL
158 #define Elf32_RelM Elf32_Rel
159 #define ELFCLASSM ELFCLASS32
160 #define USE_GOT_ENTRIES
161 #define GOT_ENTRY_SIZE 4
165 /* IA64, aka Itanium */
166 #if defined(__ia64__)
167 #define MATCH_MACHINE(x) (x == EM_IA_64)
168 #define SHT_RELM SHT_RELA
169 #define Elf64_RelM Elf64_Rela
170 #define ELFCLASSM ELFCLASS64
174 #if defined(__mc68000__)
175 #define MATCH_MACHINE(x) (x == EM_68K)
176 #define SHT_RELM SHT_RELA
177 #define Elf32_RelM Elf32_Rela
178 #define ELFCLASSM ELFCLASS32
179 #define USE_GOT_ENTRIES
180 #define GOT_ENTRY_SIZE 4
185 #if defined(__microblaze__)
187 #include <linux/elf-em.h>
188 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
189 #define SHT_RELM SHT_RELA
190 #define Elf32_RelM Elf32_Rela
191 #define ELFCLASSM ELFCLASS32
195 #if defined(__mips__)
196 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
197 #define SHT_RELM SHT_REL
198 #define Elf32_RelM Elf32_Rel
199 #define ELFCLASSM ELFCLASS32
200 /* Account for ELF spec changes. */
201 #ifndef EM_MIPS_RS3_LE
202 #ifdef EM_MIPS_RS4_BE
203 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
205 #define EM_MIPS_RS3_LE 10
207 #endif /* !EM_MIPS_RS3_LE */
208 #define ARCHDATAM "__dbe_table"
212 #if defined(__nios2__)
213 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
214 #define SHT_RELM SHT_RELA
215 #define Elf32_RelM Elf32_Rela
216 #define ELFCLASSM ELFCLASS32
220 #if defined(__powerpc64__)
221 #define MATCH_MACHINE(x) (x == EM_PPC64)
222 #define SHT_RELM SHT_RELA
223 #define Elf64_RelM Elf64_Rela
224 #define ELFCLASSM ELFCLASS64
225 #elif defined(__powerpc__)
226 #define MATCH_MACHINE(x) (x == EM_PPC)
227 #define SHT_RELM SHT_RELA
228 #define Elf32_RelM Elf32_Rela
229 #define ELFCLASSM ELFCLASS32
230 #define USE_PLT_ENTRIES
231 #define PLT_ENTRY_SIZE 16
233 #define LIST_ARCHTYPE ElfW(Addr)
235 #define ARCHDATAM "__ftr_fixup"
239 #if defined(__s390__)
240 #define MATCH_MACHINE(x) (x == EM_S390)
241 #define SHT_RELM SHT_RELA
242 #define Elf32_RelM Elf32_Rela
243 #define ELFCLASSM ELFCLASS32
244 #define USE_PLT_ENTRIES
245 #define PLT_ENTRY_SIZE 8
246 #define USE_GOT_ENTRIES
247 #define GOT_ENTRY_SIZE 8
253 #define MATCH_MACHINE(x) (x == EM_SH)
254 #define SHT_RELM SHT_RELA
255 #define Elf32_RelM Elf32_Rela
256 #define ELFCLASSM ELFCLASS32
257 #define USE_GOT_ENTRIES
258 #define GOT_ENTRY_SIZE 4
260 /* the SH changes have only been tested in =little endian= mode */
261 /* I'm not sure about big endian, so let's warn: */
262 #if defined(__sh__) && BB_BIG_ENDIAN
263 # error insmod.c may require changes for use on big endian SH
265 /* it may or may not work on the SH1/SH2... Error on those also */
266 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
267 #error insmod.c may require changes for SH1 or SH2 use
272 #if defined(__sparc__)
273 #define MATCH_MACHINE(x) (x == EM_SPARC)
274 #define SHT_RELM SHT_RELA
275 #define Elf32_RelM Elf32_Rela
276 #define ELFCLASSM ELFCLASS32
280 #if defined(__v850e__)
281 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
282 #define SHT_RELM SHT_RELA
283 #define Elf32_RelM Elf32_Rela
284 #define ELFCLASSM ELFCLASS32
285 #define USE_PLT_ENTRIES
286 #define PLT_ENTRY_SIZE 8
288 #ifndef EM_CYGNUS_V850 /* grumble */
289 #define EM_CYGNUS_V850 0x9080
291 #define SYMBOL_PREFIX "_"
295 #if defined(__x86_64__)
296 #define MATCH_MACHINE(x) (x == EM_X86_64)
297 #define SHT_RELM SHT_RELA
298 #define USE_GOT_ENTRIES
299 #define GOT_ENTRY_SIZE 8
301 #define Elf64_RelM Elf64_Rela
302 #define ELFCLASSM ELFCLASS64
306 #error Sorry, but insmod.c does not yet support this architecture...
310 //----------------------------------------------------------------------------
311 //--------modutils module.h, lines 45-242
312 //----------------------------------------------------------------------------
314 /* Definitions for the Linux module syscall interface.
315 Copyright 1996, 1997 Linux International.
317 Contributed by Richard Henderson <rth@tamu.edu>
319 This file is part of the Linux modutils.
321 This program is free software; you can redistribute it and/or modify it
322 under the terms of the GNU General Public License as published by the
323 Free Software Foundation; either version 2 of the License, or (at your
324 option) any later version.
326 This program is distributed in the hope that it will be useful, but
327 WITHOUT ANY WARRANTY; without even the implied warranty of
328 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
329 General Public License for more details.
331 You should have received a copy of the GNU General Public License
332 along with this program; if not, write to the Free Software Foundation,
333 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
336 #ifndef MODUTILS_MODULE_H
338 /*======================================================================*/
339 /* For sizeof() which are related to the module platform and not to the
340 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
342 #define tgt_sizeof_char sizeof(char)
343 #define tgt_sizeof_short sizeof(short)
344 #define tgt_sizeof_int sizeof(int)
345 #define tgt_sizeof_long sizeof(long)
346 #define tgt_sizeof_char_p sizeof(char *)
347 #define tgt_sizeof_void_p sizeof(void *)
348 #define tgt_long long
350 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
351 #undef tgt_sizeof_long
352 #undef tgt_sizeof_char_p
353 #undef tgt_sizeof_void_p
357 tgt_sizeof_char_p = 8,
358 tgt_sizeof_void_p = 8
360 #define tgt_long long long
363 /*======================================================================*/
364 /* The structures used in Linux 2.1. */
366 /* Note: new_module_symbol does not use tgt_long intentionally */
367 struct new_module_symbol {
372 struct new_module_persist;
374 struct new_module_ref {
375 unsigned tgt_long dep; /* kernel addresses */
376 unsigned tgt_long ref;
377 unsigned tgt_long next_ref;
381 unsigned tgt_long size_of_struct; /* == sizeof(module) */
382 unsigned tgt_long next;
383 unsigned tgt_long name;
384 unsigned tgt_long size;
387 unsigned tgt_long flags; /* AUTOCLEAN et al */
392 unsigned tgt_long syms;
393 unsigned tgt_long deps;
394 unsigned tgt_long refs;
395 unsigned tgt_long init;
396 unsigned tgt_long cleanup;
397 unsigned tgt_long ex_table_start;
398 unsigned tgt_long ex_table_end;
400 unsigned tgt_long gp;
402 /* Everything after here is extension. */
403 unsigned tgt_long persist_start;
404 unsigned tgt_long persist_end;
405 unsigned tgt_long can_unload;
406 unsigned tgt_long runsize;
407 const char *kallsyms_start; /* All symbols for kernel debugging */
408 const char *kallsyms_end;
409 const char *archdata_start; /* arch specific data for module */
410 const char *archdata_end;
411 const char *kernel_data; /* Reserved for kernel internal use */
415 #define ARCHDATA_SEC_NAME ARCHDATAM
417 #define ARCHDATA_SEC_NAME "__archdata"
419 #define KALLSYMS_SEC_NAME "__kallsyms"
422 struct new_module_info {
429 /* Bits of module.flags. */
433 NEW_MOD_AUTOCLEAN = 4,
435 NEW_MOD_USED_ONCE = 16
438 int init_module(const char *name, const struct new_module *);
439 int query_module(const char *name, int which, void *buf,
440 size_t bufsize, size_t *ret);
442 /* Values for query_module's which. */
451 /*======================================================================*/
452 /* The system calls unchanged between 2.0 and 2.1. */
454 unsigned long create_module(const char *, size_t);
455 int delete_module(const char *module, unsigned int flags);
458 #endif /* module.h */
460 //----------------------------------------------------------------------------
461 //--------end of modutils module.h
462 //----------------------------------------------------------------------------
466 //----------------------------------------------------------------------------
467 //--------modutils obj.h, lines 253-462
468 //----------------------------------------------------------------------------
470 /* Elf object file loading and relocation routines.
471 Copyright 1996, 1997 Linux International.
473 Contributed by Richard Henderson <rth@tamu.edu>
475 This file is part of the Linux modutils.
477 This program is free software; you can redistribute it and/or modify it
478 under the terms of the GNU General Public License as published by the
479 Free Software Foundation; either version 2 of the License, or (at your
480 option) any later version.
482 This program is distributed in the hope that it will be useful, but
483 WITHOUT ANY WARRANTY; without even the implied warranty of
484 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
485 General Public License for more details.
487 You should have received a copy of the GNU General Public License
488 along with this program; if not, write to the Free Software Foundation,
489 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
492 #ifndef MODUTILS_OBJ_H
494 /* The relocatable object is manipulated using elfin types. */
500 # if ELFCLASSM == ELFCLASS32
501 # define ElfW(x) Elf32_ ## x
502 # define ELFW(x) ELF32_ ## x
504 # define ElfW(x) Elf64_ ## x
505 # define ELFW(x) ELF64_ ## x
509 /* For some reason this is missing from some ancient C libraries.... */
510 #ifndef ELF32_ST_INFO
511 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
514 #ifndef ELF64_ST_INFO
515 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
518 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
519 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
520 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
521 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
522 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
524 struct obj_string_patch;
525 struct obj_symbol_patch;
532 struct obj_section *load_next;
538 struct obj_symbol *next; /* hash table link */
542 int secidx; /* the defining section index/module */
544 int ksymidx; /* for export to the kernel symtab */
545 int referenced; /* actually used in the link */
548 /* Hardcode the hash table size. We shouldn't be needing so many
549 symbols that we begin to degrade performance, and we get a big win
550 by giving the compiler a constant divisor. */
552 #define HASH_BUCKETS 521
557 struct obj_section **sections;
558 struct obj_section *load_order;
559 struct obj_section **load_order_search_start;
560 struct obj_string_patch *string_patches;
561 struct obj_symbol_patch *symbol_patches;
562 int (*symbol_cmp)(const char *, const char *);
563 unsigned long (*symbol_hash)(const char *);
564 unsigned long local_symtab_size;
565 struct obj_symbol **local_symtab;
566 struct obj_symbol *symtab[HASH_BUCKETS];
576 struct obj_string_patch {
577 struct obj_string_patch *next;
579 ElfW(Addr) reloc_offset;
580 ElfW(Addr) string_offset;
583 struct obj_symbol_patch {
584 struct obj_symbol_patch *next;
586 ElfW(Addr) reloc_offset;
587 struct obj_symbol *sym;
591 /* Generic object manipulation routines. */
593 static unsigned long obj_elf_hash(const char *);
595 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
597 static struct obj_symbol *obj_find_symbol(struct obj_file *f,
600 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
601 struct obj_symbol *sym);
603 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
604 static void obj_set_symbol_compare(struct obj_file *f,
605 int (*cmp)(const char *, const char *),
606 unsigned long (*hash)(const char *));
609 static struct obj_section *obj_find_section(struct obj_file *f,
612 static void obj_insert_section_load_order(struct obj_file *f,
613 struct obj_section *sec);
615 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
620 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
625 static void *obj_extend_section(struct obj_section *sec, unsigned long more);
627 static void obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
630 static void obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
631 struct obj_symbol *sym);
633 static void obj_check_undefineds(struct obj_file *f);
635 static void obj_allocate_commons(struct obj_file *f);
637 static unsigned long obj_load_size(struct obj_file *f);
639 static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
641 static struct obj_file *obj_load(FILE *f, int loadprogbits);
643 static int obj_create_image(struct obj_file *f, char *image);
645 /* Architecture specific manipulation routines. */
647 static struct obj_file *arch_new_file(void);
649 static struct obj_section *arch_new_section(void);
651 static struct obj_symbol *arch_new_symbol(void);
653 static enum obj_reloc arch_apply_relocation(struct obj_file *f,
654 struct obj_section *targsec,
655 /*struct obj_section *symsec,*/
656 struct obj_symbol *sym,
657 ElfW(RelM) *rel, ElfW(Addr) value);
659 static void arch_create_got(struct obj_file *f);
660 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
661 static int obj_gpl_license(struct obj_file *f, const char **license);
662 #endif /* FEATURE_CHECK_TAINTED_MODULE */
664 //----------------------------------------------------------------------------
665 //--------end of modutils obj.h
666 //----------------------------------------------------------------------------
669 /* SPFX is always a string, so it can be concatenated to string constants. */
671 #define SPFX SYMBOL_PREFIX
676 enum { STRVERSIONLEN = 64 };
678 /*======================================================================*/
680 #define OPTION_STR "sLo:fkvqx" USE_FEATURE_INSMOD_LOAD_MAP("m")
682 OPT_s = 0x1, // -s /* log to syslog */
683 /* Not supported but kernel needs this for request_module(),
684 as this calls: modprobe -k -s -- <module>
685 so silently ignore this flag */
686 OPT_L = 0x2, // -L /* Stub warning */
687 /* Compatibility with modprobe.
688 In theory, this does locking, but we don't do
689 that. So be careful and plan your life around not
690 loading the same module 50 times concurrently. */
691 OPT_o = 0x4, // -o /* name the output module */
692 OPT_f = 0x8, // -f /* force loading */
693 OPT_k = 0x10, // -k /* module loaded by kerneld, auto-cleanable */
694 OPT_v = 0x20, // -v /* verbose output */
695 OPT_q = 0x40, // -q /* silent */
696 OPT_x = 0x80, // -x /* do not export externs */
697 OPT_m = 0x100, // -m /* print module load map */
699 #define flag_force_load (option_mask32 & OPT_f)
700 #define flag_autoclean (option_mask32 & OPT_k)
701 #define flag_verbose (option_mask32 & OPT_v)
702 #define flag_quiet (option_mask32 & OPT_q)
703 #define flag_noexport (option_mask32 & OPT_x)
704 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
705 #define flag_print_load_map (option_mask32 & OPT_m)
707 #define flag_print_load_map 0
710 /*======================================================================*/
712 #if defined(USE_LIST)
714 struct arch_list_entry
716 struct arch_list_entry *next;
717 LIST_ARCHTYPE addend;
724 #if defined(USE_SINGLE)
726 struct arch_single_entry
735 #if defined(__mips__)
738 struct mips_hi16 *next;
745 struct obj_file root;
746 #if defined(USE_PLT_ENTRIES)
747 struct obj_section *plt;
749 #if defined(USE_GOT_ENTRIES)
750 struct obj_section *got;
752 #if defined(__mips__)
753 struct mips_hi16 *mips_hi16_list;
758 struct obj_symbol root;
759 #if defined(USE_PLT_ENTRIES)
760 #if defined(USE_PLT_LIST)
761 struct arch_list_entry *pltent;
763 struct arch_single_entry pltent;
766 #if defined(USE_GOT_ENTRIES)
767 struct arch_single_entry gotent;
772 struct external_module {
777 struct new_module_symbol *syms;
780 static struct new_module_symbol *ksyms;
781 static size_t nksyms;
783 static struct external_module *ext_modules;
784 static int n_ext_modules;
785 static int n_ext_modules_used;
787 static char *m_filename;
788 static char *m_fullName;
791 /*======================================================================*/
794 static int FAST_FUNC check_module_name_match(const char *filename,
795 struct stat *statbuf UNUSED_PARAM,
796 void *userdata, int depth UNUSED_PARAM)
798 char *fullname = (char *) userdata;
801 if (fullname[0] == '\0')
804 tmp = bb_get_last_path_component_nostrip(filename);
805 if (strcmp(tmp, fullname) == 0) {
806 /* Stop searching if we find a match */
807 m_filename = xstrdup(filename);
814 /*======================================================================*/
816 static struct obj_file *arch_new_file(void)
819 f = xzalloc(sizeof(*f));
820 return &f->root; /* it's a first member */
823 static struct obj_section *arch_new_section(void)
825 return xzalloc(sizeof(struct obj_section));
828 static struct obj_symbol *arch_new_symbol(void)
830 struct arch_symbol *sym;
831 sym = xzalloc(sizeof(*sym));
835 static enum obj_reloc
836 arch_apply_relocation(struct obj_file *f,
837 struct obj_section *targsec,
838 /*struct obj_section *symsec,*/
839 struct obj_symbol *sym,
840 ElfW(RelM) *rel, ElfW(Addr) v)
842 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
843 || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \
844 || defined(__powerpc__) || defined(__mips__)
845 struct arch_file *ifile = (struct arch_file *) f;
847 enum obj_reloc ret = obj_reloc_ok;
848 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
849 #if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
850 || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
851 || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
852 || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
853 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
855 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
856 struct arch_symbol *isym = (struct arch_symbol *) sym;
858 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
859 || defined(__sh__) || defined(__s390__)
860 #if defined(USE_GOT_ENTRIES)
861 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
864 #if defined(USE_PLT_ENTRIES)
865 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
867 # if defined(USE_PLT_LIST)
868 struct arch_list_entry *pe;
870 struct arch_single_entry *pe;
874 switch (ELF_R_TYPE(rel->r_info)) {
889 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
890 * (which is .got) similar to branch,
891 * but is full 32 bits relative */
900 case R_ARM_GOTOFF: /* address relative to the got */
904 #elif defined(__cris__)
910 /* CRIS keeps the relocation value in the r_addend field and
911 * should not use whats in *loc at all
916 #elif defined(__H8300H__) || defined(__H8300S__)
919 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
920 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
931 if ((ElfW(Sword))v > 0x7fff ||
932 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
933 ret = obj_reloc_overflow;
935 *(unsigned short *)loc = v;
939 if ((ElfW(Sword))v > 0x7f ||
940 (ElfW(Sword))v < -(ElfW(Sword))0x80)
941 ret = obj_reloc_overflow;
943 *(unsigned char *)loc = v;
946 #elif defined(__i386__)
978 #elif defined(__microblaze__)
979 case R_MICROBLAZE_NONE:
980 case R_MICROBLAZE_64_NONE:
981 case R_MICROBLAZE_32_SYM_OP_SYM:
982 case R_MICROBLAZE_32_PCREL:
985 case R_MICROBLAZE_64_PCREL: {
986 /* dot is the address of the current instruction.
987 * v is the target symbol address.
988 * So we need to extract the offset in the code,
989 * adding v, then subtrating the current address
990 * of this instruction.
991 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
994 /* Get split offset stored in code */
995 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
998 /* Adjust relative offset. -4 adjustment required
999 * because dot points to the IMM insn, but branch
1000 * is computed relative to the branch instruction itself.
1002 temp += v - dot - 4;
1004 /* Store back into code */
1005 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
1006 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
1011 case R_MICROBLAZE_32:
1015 case R_MICROBLAZE_64: {
1016 /* Get split pointer stored in code */
1017 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
1020 /* Add reloc offset */
1023 /* Store back into code */
1024 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
1025 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
1030 case R_MICROBLAZE_32_PCREL_LO:
1031 case R_MICROBLAZE_32_LO:
1032 case R_MICROBLAZE_SRO32:
1033 case R_MICROBLAZE_SRW32:
1034 ret = obj_reloc_unhandled;
1037 #elif defined(__mc68000__)
1048 ret = obj_reloc_overflow;
1055 ret = obj_reloc_overflow;
1062 if ((ElfW(Sword))v > 0x7f ||
1063 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1064 ret = obj_reloc_overflow;
1071 if ((ElfW(Sword))v > 0x7fff ||
1072 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1073 ret = obj_reloc_overflow;
1079 *(int *)loc = v - dot;
1082 case R_68K_GLOB_DAT:
1083 case R_68K_JMP_SLOT:
1087 case R_68K_RELATIVE:
1088 *(int *)loc += f->baseaddr;
1094 # ifdef R_68K_GOTOFF
1100 #elif defined(__mips__)
1111 ret = obj_reloc_dangerous;
1112 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1113 ret = obj_reloc_overflow;
1115 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1121 struct mips_hi16 *n;
1123 /* We cannot relocate this one now because we don't know the value
1124 of the carry we need to add. Save the information, and let LO16
1125 do the actual relocation. */
1126 n = xmalloc(sizeof *n);
1129 n->next = ifile->mips_hi16_list;
1130 ifile->mips_hi16_list = n;
1136 unsigned long insnlo = *loc;
1137 ElfW(Addr) val, vallo;
1139 /* Sign extend the addend we extract from the lo insn. */
1140 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1142 if (ifile->mips_hi16_list != NULL) {
1143 struct mips_hi16 *l;
1145 l = ifile->mips_hi16_list;
1147 struct mips_hi16 *next;
1150 /* Do the HI16 relocation. Note that we actually don't
1151 need to know anything about the LO16 itself, except where
1152 to find the low 16 bits of the addend needed by the LO16. */
1155 ((insn & 0xffff) << 16) +
1159 /* Account for the sign extension that will happen in the
1166 insn = (insn & ~0xffff) | val;
1174 ifile->mips_hi16_list = NULL;
1177 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1179 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1184 #elif defined(__nios2__)
1189 case R_NIOS2_BFD_RELOC_32:
1193 case R_NIOS2_BFD_RELOC_16:
1195 ret = obj_reloc_overflow;
1200 case R_NIOS2_BFD_RELOC_8:
1202 ret = obj_reloc_overflow;
1211 if ((Elf32_Sword)v > 0x7fff ||
1212 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1213 ret = obj_reloc_overflow;
1217 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1227 ret = obj_reloc_overflow;
1231 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1236 case R_NIOS2_PCREL16:
1241 if ((Elf32_Sword)v > 0x7fff ||
1242 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1243 ret = obj_reloc_overflow;
1247 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1253 Elf32_Addr word, gp;
1255 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1257 if ((Elf32_Sword)v > 0x7fff ||
1258 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1259 ret = obj_reloc_overflow;
1263 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1267 case R_NIOS2_CALL26:
1269 ret = obj_reloc_dangerous;
1270 if ((v >> 28) != (dot >> 28))
1271 ret = obj_reloc_overflow;
1272 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1280 ret = obj_reloc_overflow;
1283 word = *loc & ~0x7c0;
1284 *loc = word | ((v & 0x1f) << 6);
1293 ret = obj_reloc_overflow;
1296 word = *loc & ~0xfc0;
1297 *loc = word | ((v & 0x3f) << 6);
1306 ret = obj_reloc_overflow;
1309 word = *loc & ~0x3fc0;
1310 *loc = word | ((v & 0xff) << 6);
1319 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1329 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1334 case R_NIOS2_HIADJ16:
1336 Elf32_Addr word1, word2;
1339 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1340 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1345 #elif defined(__powerpc64__)
1346 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1348 #elif defined(__powerpc__)
1350 case R_PPC_ADDR16_HA:
1351 *(unsigned short *)loc = (v + 0x8000) >> 16;
1354 case R_PPC_ADDR16_HI:
1355 *(unsigned short *)loc = v >> 16;
1358 case R_PPC_ADDR16_LO:
1359 *(unsigned short *)loc = v;
1373 #elif defined(__s390__)
1376 *(unsigned int *) loc += v;
1379 *(unsigned short *) loc += v;
1382 *(unsigned char *) loc += v;
1386 *(unsigned int *) loc += v - dot;
1389 *(unsigned short *) loc += (v - dot) >> 1;
1392 *(unsigned short *) loc += v - dot;
1396 case R_390_PLT16DBL:
1397 /* find the plt entry and initialize it. */
1398 pe = (struct arch_single_entry *) &isym->pltent;
1399 if (pe->inited == 0) {
1400 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1401 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1403 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1410 /* Insert relative distance to target. */
1411 v = plt + pe->offset - dot;
1412 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1413 *(unsigned int *) loc = (unsigned int) v;
1414 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1415 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1418 case R_390_GLOB_DAT:
1419 case R_390_JMP_SLOT:
1423 case R_390_RELATIVE:
1424 *loc += f->baseaddr;
1428 *(unsigned long *) loc += got - dot;
1434 if (!isym->gotent.inited)
1436 isym->gotent.inited = 1;
1437 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1439 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1440 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1441 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1442 *(unsigned short *) loc += isym->gotent.offset;
1443 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1444 *(unsigned int *) loc += isym->gotent.offset;
1447 # ifndef R_390_GOTOFF32
1448 # define R_390_GOTOFF32 R_390_GOTOFF
1450 case R_390_GOTOFF32:
1454 #elif defined(__sh__)
1477 *loc = f->baseaddr + rel->r_addend;
1481 *loc = got - dot + rel->r_addend;
1491 # if defined(__SH5__)
1492 case R_SH_IMM_MEDLOW16:
1493 case R_SH_IMM_LOW16:
1497 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1501 * movi and shori have the format:
1503 * | op | imm | reg | reserved |
1504 * 31..26 25..10 9.. 4 3 .. 0
1506 * so we simply mask and or in imm.
1508 word = *loc & ~0x3fffc00;
1509 word |= (v & 0xffff) << 10;
1516 case R_SH_IMM_MEDLOW16_PCREL:
1517 case R_SH_IMM_LOW16_PCREL:
1521 word = *loc & ~0x3fffc00;
1525 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1528 word |= (v & 0xffff) << 10;
1534 # endif /* __SH5__ */
1536 #elif defined(__v850e__)
1542 /* We write two shorts instead of a long because even
1543 32-bit insns only need half-word alignment, but
1544 32-bit data needs to be long-word aligned. */
1545 v += ((unsigned short *)loc)[0];
1546 v += ((unsigned short *)loc)[1] << 16;
1547 ((unsigned short *)loc)[0] = v & 0xffff;
1548 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1551 case R_V850_22_PCREL:
1554 #elif defined(__x86_64__)
1564 *(unsigned int *) loc += v;
1567 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1568 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1573 *(signed int *) loc += v;
1577 *(unsigned short *) loc += v;
1581 *(unsigned char *) loc += v;
1585 *(unsigned int *) loc += v - dot;
1589 *(unsigned short *) loc += v - dot;
1593 *(unsigned char *) loc += v - dot;
1596 case R_X86_64_GLOB_DAT:
1597 case R_X86_64_JUMP_SLOT:
1601 case R_X86_64_RELATIVE:
1602 *loc += f->baseaddr;
1605 case R_X86_64_GOT32:
1606 case R_X86_64_GOTPCREL:
1609 if (!isym->gotent.reloc_done)
1611 isym->gotent.reloc_done = 1;
1612 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1614 /* XXX are these really correct? */
1615 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1616 *(unsigned int *) loc += v + isym->gotent.offset;
1618 *loc += isym->gotent.offset;
1623 # warning "no idea how to handle relocations on your arch"
1627 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1628 ret = obj_reloc_unhandled;
1631 #if defined(USE_PLT_ENTRIES)
1635 /* find the plt entry and initialize it if necessary */
1637 #if defined(USE_PLT_LIST)
1638 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1645 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1647 /* generate some machine code */
1649 #if defined(__arm__)
1650 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1651 ip[1] = v; /* sym@ */
1653 #if defined(__powerpc__)
1654 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1655 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1656 ip[2] = 0x7d6903a6; /* mtctr r11 */
1657 ip[3] = 0x4e800420; /* bctr */
1659 #if defined(__v850e__)
1660 /* We have to trash a register, so we assume that any control
1661 transfer more than 21-bits away must be a function call
1662 (so we can use a call-clobbered register). */
1663 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1664 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1669 /* relative distance to target */
1671 /* if the target is too far away.... */
1672 #if defined(__arm__) || defined(__powerpc__)
1673 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1674 #elif defined(__v850e__)
1675 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1677 /* go via the plt */
1678 v = plt + pe->offset - dot;
1680 #if defined(__v850e__)
1685 ret = obj_reloc_dangerous;
1687 /* merge the offset into the instruction. */
1688 #if defined(__arm__)
1689 /* Convert to words. */
1692 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1694 #if defined(__powerpc__)
1695 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1697 #if defined(__v850e__)
1698 /* We write two shorts instead of a long because even 32-bit insns
1699 only need half-word alignment, but the 32-bit data write needs
1700 to be long-word aligned. */
1701 ((unsigned short *)loc)[0] =
1702 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1703 | ((v >> 16) & 0x3f); /* offs high part */
1704 ((unsigned short *)loc)[1] =
1705 (v & 0xffff); /* offs low part */
1708 #endif /* USE_PLT_ENTRIES */
1710 #if defined(USE_GOT_ENTRIES)
1713 /* needs an entry in the .got: set it, once */
1714 if (!isym->gotent.inited) {
1715 isym->gotent.inited = 1;
1716 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1718 /* make the reloc with_respect_to_.got */
1720 *loc += isym->gotent.offset + rel->r_addend;
1721 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1722 *loc += isym->gotent.offset;
1726 #endif /* USE_GOT_ENTRIES */
1733 #if defined(USE_LIST)
1735 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1736 int offset, int size)
1738 struct arch_list_entry *pe;
1740 for (pe = *list; pe != NULL; pe = pe->next) {
1741 if (pe->addend == rel->r_addend) {
1747 pe = xmalloc(sizeof(struct arch_list_entry));
1749 pe->addend = rel->r_addend;
1750 pe->offset = offset;
1760 #if defined(USE_SINGLE)
1762 static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
1763 int offset, int size)
1765 if (single->allocated == 0) {
1766 single->allocated = 1;
1767 single->offset = offset;
1776 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1778 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
1779 int offset, int size)
1781 struct obj_section *myrelsec = obj_find_section(f, name);
1788 obj_extend_section(myrelsec, offset);
1790 myrelsec = obj_create_alloced_section(f, name,
1799 static void arch_create_got(struct obj_file *f)
1801 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1802 struct arch_file *ifile = (struct arch_file *) f;
1804 #if defined(USE_GOT_ENTRIES)
1805 int got_offset = 0, got_needed = 0, got_allocate;
1807 #if defined(USE_PLT_ENTRIES)
1808 int plt_offset = 0, plt_needed = 0, plt_allocate;
1810 struct obj_section *relsec, *symsec, *strsec;
1811 ElfW(RelM) *rel, *relend;
1812 ElfW(Sym) *symtab, *extsym;
1813 const char *strtab, *name;
1814 struct arch_symbol *intsym;
1816 for (i = 0; i < f->header.e_shnum; ++i) {
1817 relsec = f->sections[i];
1818 if (relsec->header.sh_type != SHT_RELM)
1821 symsec = f->sections[relsec->header.sh_link];
1822 strsec = f->sections[symsec->header.sh_link];
1824 rel = (ElfW(RelM) *) relsec->contents;
1825 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1826 symtab = (ElfW(Sym) *) symsec->contents;
1827 strtab = (const char *) strsec->contents;
1829 for (; rel < relend; ++rel) {
1830 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1832 #if defined(USE_GOT_ENTRIES)
1835 #if defined(USE_PLT_ENTRIES)
1839 switch (ELF_R_TYPE(rel->r_info)) {
1840 #if defined(__arm__)
1855 #elif defined(__i386__)
1865 #elif defined(__powerpc__)
1870 #elif defined(__mc68000__)
1881 #elif defined(__sh__)
1891 #elif defined(__v850e__)
1892 case R_V850_22_PCREL:
1901 if (extsym->st_name != 0) {
1902 name = strtab + extsym->st_name;
1904 name = f->sections[extsym->st_shndx]->name;
1906 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1907 #if defined(USE_GOT_ENTRIES)
1909 got_offset += arch_single_init(
1910 /*rel,*/ &intsym->gotent,
1911 got_offset, GOT_ENTRY_SIZE);
1916 #if defined(USE_PLT_ENTRIES)
1918 #if defined(USE_PLT_LIST)
1919 plt_offset += arch_list_add(
1920 rel, &intsym->pltent,
1921 plt_offset, PLT_ENTRY_SIZE);
1923 plt_offset += arch_single_init(
1924 /*rel,*/ &intsym->pltent,
1925 plt_offset, PLT_ENTRY_SIZE);
1933 #if defined(USE_GOT_ENTRIES)
1935 ifile->got = arch_xsect_init(f, ".got", got_offset,
1940 #if defined(USE_PLT_ENTRIES)
1942 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1947 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1950 /*======================================================================*/
1952 /* Standard ELF hash function. */
1953 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1955 unsigned long h = 0;
1962 g = (h & 0xf0000000);
1972 static unsigned long obj_elf_hash(const char *name)
1974 return obj_elf_hash_n(name, strlen(name));
1977 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1978 /* String comparison for non-co-versioned kernel and module. */
1980 static int ncv_strcmp(const char *a, const char *b)
1982 size_t alen = strlen(a), blen = strlen(b);
1984 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1985 return strncmp(a, b, alen);
1986 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1987 return strncmp(a, b, blen);
1989 return strcmp(a, b);
1992 /* String hashing for non-co-versioned kernel and module. Here
1993 we are simply forced to drop the crc from the hash. */
1995 static unsigned long ncv_symbol_hash(const char *str)
1997 size_t len = strlen(str);
1998 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
2000 return obj_elf_hash_n(str, len);
2004 obj_set_symbol_compare(struct obj_file *f,
2005 int (*cmp) (const char *, const char *),
2006 unsigned long (*hash) (const char *))
2009 f->symbol_cmp = cmp;
2011 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
2014 f->symbol_hash = hash;
2016 memcpy(tmptab, f->symtab, sizeof(tmptab));
2017 memset(f->symtab, 0, sizeof(f->symtab));
2019 for (i = 0; i < HASH_BUCKETS; ++i)
2020 for (sym = tmptab[i]; sym; sym = next) {
2021 unsigned long h = hash(sym->name) % HASH_BUCKETS;
2023 sym->next = f->symtab[h];
2029 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2031 static struct obj_symbol *
2032 obj_add_symbol(struct obj_file *f, const char *name,
2033 unsigned long symidx, int info,
2034 int secidx, ElfW(Addr) value,
2037 struct obj_symbol *sym;
2038 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2039 int n_type = ELF_ST_TYPE(info);
2040 int n_binding = ELF_ST_BIND(info);
2042 for (sym = f->symtab[hash]; sym; sym = sym->next) {
2043 if (f->symbol_cmp(sym->name, name) == 0) {
2044 int o_secidx = sym->secidx;
2045 int o_info = sym->info;
2046 int o_type = ELF_ST_TYPE(o_info);
2047 int o_binding = ELF_ST_BIND(o_info);
2049 /* A redefinition! Is it legal? */
2051 if (secidx == SHN_UNDEF)
2053 else if (o_secidx == SHN_UNDEF)
2055 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2056 /* Cope with local and global symbols of the same name
2057 in the same object file, as might have been created
2058 by ld -r. The only reason locals are now seen at this
2059 level at all is so that we can do semi-sensible things
2062 struct obj_symbol *nsym, **p;
2064 nsym = arch_new_symbol();
2065 nsym->next = sym->next;
2068 /* Excise the old (local) symbol from the hash chain. */
2069 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2073 } else if (n_binding == STB_LOCAL) {
2074 /* Another symbol of the same name has already been defined.
2075 Just add this to the local table. */
2076 sym = arch_new_symbol();
2079 f->local_symtab[symidx] = sym;
2081 } else if (n_binding == STB_WEAK)
2083 else if (o_binding == STB_WEAK)
2085 /* Don't unify COMMON symbols with object types the programmer
2087 else if (secidx == SHN_COMMON
2088 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2090 else if (o_secidx == SHN_COMMON
2091 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2094 /* Don't report an error if the symbol is coming from
2095 the kernel or some external module. */
2096 if (secidx <= SHN_HIRESERVE)
2097 bb_error_msg("%s multiply defined", name);
2103 /* Completely new symbol. */
2104 sym = arch_new_symbol();
2105 sym->next = f->symtab[hash];
2106 f->symtab[hash] = sym;
2108 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
2109 if (symidx >= f->local_symtab_size)
2110 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2111 name, (long) symidx, (long) f->local_symtab_size);
2113 f->local_symtab[symidx] = sym;
2120 sym->secidx = secidx;
2126 static struct obj_symbol *
2127 obj_find_symbol(struct obj_file *f, const char *name)
2129 struct obj_symbol *sym;
2130 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2132 for (sym = f->symtab[hash]; sym; sym = sym->next)
2133 if (f->symbol_cmp(sym->name, name) == 0)
2139 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2142 if (sym->secidx >= SHN_LORESERVE)
2145 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2147 /* As a special case, a NULL sym has value zero. */
2152 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2154 int i, n = f->header.e_shnum;
2156 for (i = 0; i < n; ++i)
2157 if (strcmp(f->sections[i]->name, name) == 0)
2158 return f->sections[i];
2163 static int obj_load_order_prio(struct obj_section *a)
2165 unsigned long af, ac;
2167 af = a->header.sh_flags;
2170 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2171 strcmp(a->name + 5, ".init"))
2175 if (!(af & SHF_WRITE))
2177 if (af & SHF_EXECINSTR)
2179 if (a->header.sh_type != SHT_NOBITS)
2186 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2188 struct obj_section **p;
2189 int prio = obj_load_order_prio(sec);
2190 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2191 if (obj_load_order_prio(*p) < prio)
2193 sec->load_next = *p;
2197 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2199 unsigned long align,
2202 int newidx = f->header.e_shnum++;
2203 struct obj_section *sec;
2205 f->sections = xrealloc_vector(f->sections, 2, newidx);
2206 f->sections[newidx] = sec = arch_new_section();
2208 sec->header.sh_type = SHT_PROGBITS;
2209 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2210 sec->header.sh_size = size;
2211 sec->header.sh_addralign = align;
2215 sec->contents = xmalloc(size);
2217 obj_insert_section_load_order(f, sec);
2222 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2224 unsigned long align,
2227 int newidx = f->header.e_shnum++;
2228 struct obj_section *sec;
2230 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2231 f->sections[newidx] = sec = arch_new_section();
2233 sec->header.sh_type = SHT_PROGBITS;
2234 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2235 sec->header.sh_size = size;
2236 sec->header.sh_addralign = align;
2240 sec->contents = xmalloc(size);
2242 sec->load_next = f->load_order;
2243 f->load_order = sec;
2244 if (f->load_order_search_start == &f->load_order)
2245 f->load_order_search_start = &sec->load_next;
2250 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2252 unsigned long oldsize = sec->header.sh_size;
2254 sec->header.sh_size += more;
2255 sec->contents = xrealloc(sec->contents, sec->header.sh_size);
2257 return sec->contents + oldsize;
2261 /* Conditionally add the symbols from the given symbol set to the
2265 add_symbols_from( struct obj_file *f,
2266 int idx, struct new_module_symbol *syms, size_t nsyms)
2268 struct new_module_symbol *s;
2271 #ifdef SYMBOL_PREFIX
2273 size_t name_alloced_size = 0;
2275 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2278 gpl = obj_gpl_license(f, NULL) == 0;
2280 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2281 /* Only add symbols that are already marked external.
2282 If we override locals we may cause problems for
2283 argument initialization. We will also create a false
2284 dependency on the module. */
2285 struct obj_symbol *sym;
2288 /* GPL licensed modules can use symbols exported with
2289 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2290 * exported names. Non-GPL modules never see any GPLONLY_
2291 * symbols so they cannot fudge it by adding the prefix on
2294 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2295 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2302 name = (char *)s->name;
2304 #ifdef SYMBOL_PREFIX
2305 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2306 kernel exports `C names', but module object files
2307 reference `linker names'). */
2308 size_t extra = sizeof SYMBOL_PREFIX;
2309 size_t name_size = strlen(name) + extra;
2310 if (name_size > name_alloced_size) {
2311 name_alloced_size = name_size * 2;
2312 name_buf = alloca(name_alloced_size);
2314 strcpy(name_buf, SYMBOL_PREFIX);
2315 strcpy(name_buf + extra - 1, name);
2317 #endif /* SYMBOL_PREFIX */
2319 sym = obj_find_symbol(f, name);
2320 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2321 #ifdef SYMBOL_PREFIX
2322 /* Put NAME_BUF into more permanent storage. */
2323 name = xmalloc(name_size);
2324 strcpy(name, name_buf);
2326 sym = obj_add_symbol(f, name, -1,
2327 ELF_ST_INFO(STB_GLOBAL,
2330 /* Did our symbol just get installed? If so, mark the
2331 module as "used". */
2332 if (sym->secidx == idx)
2340 static void add_kernel_symbols(struct obj_file *f)
2342 struct external_module *m;
2345 /* Add module symbols first. */
2347 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
2349 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
2356 n_ext_modules_used = nused;
2358 /* And finally the symbols from the kernel proper. */
2361 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2364 static char *get_modinfo_value(struct obj_file *f, const char *key)
2366 struct obj_section *sec;
2367 char *p, *v, *n, *ep;
2368 size_t klen = strlen(key);
2370 sec = obj_find_section(f, ".modinfo");
2374 ep = p + sec->header.sh_size;
2377 n = strchr(p, '\0');
2379 if (p + klen == v && strncmp(p, key, klen) == 0)
2382 if (p + klen == n && strcmp(p, key) == 0)
2392 /*======================================================================*/
2393 /* Functions relating to module loading after 2.1.18. */
2396 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2399 char *p, *q, *key, *sym_name;
2400 struct obj_symbol *sym;
2401 char *contents, *loc;
2411 key = alloca(q - p + 6);
2412 memcpy(key, "parm_", 5);
2413 memcpy(key + 5, p, q - p);
2416 p = get_modinfo_value(f, key);
2419 bb_error_msg_and_die("invalid parameter %s", key);
2422 #ifdef SYMBOL_PREFIX
2423 sym_name = alloca(strlen(key) + sizeof SYMBOL_PREFIX);
2424 strcpy(sym_name, SYMBOL_PREFIX);
2425 strcat(sym_name, key);
2429 sym = obj_find_symbol(f, sym_name);
2431 /* Also check that the parameter was not resolved from the kernel. */
2432 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2433 bb_error_msg_and_die("symbol for parameter %s not found", key);
2437 min = strtoul(p, &p, 10);
2439 max = strtoul(p + 1, &p, 10);
2445 contents = f->sections[sym->secidx]->contents;
2446 loc = contents + sym->value;
2450 if ((*p == 's') || (*p == 'c')) {
2453 /* Do C quoting if we begin with a ", else slurp the lot. */
2457 str = alloca(strlen(q));
2458 for (r = str, q++; *q != '"'; ++q, ++r) {
2460 bb_error_msg_and_die("improperly terminated string argument for %s",
2496 if (q[1] >= '0' && q[1] <= '7') {
2497 c = (c * 8) + *++q - '0';
2498 if (q[1] >= '0' && q[1] <= '7')
2499 c = (c * 8) + *++q - '0';
2517 /* In this case, the string is not quoted. We will break
2518 it using the coma (like for ints). If the user wants to
2519 include comas in a string, he just has to quote it */
2521 /* Search the next coma */
2525 if (r != (char *) NULL) {
2526 /* Recopy the current field */
2527 str = alloca(r - q + 1);
2528 memcpy(str, q, r - q);
2530 /* I don't know if it is useful, as the previous case
2531 doesn't nul terminate the string ??? */
2534 /* Keep next fields */
2545 obj_string_patch(f, sym->secidx, loc - contents, str);
2546 loc += tgt_sizeof_char_p;
2548 /* Array of chars (in fact, matrix!) */
2549 unsigned long charssize; /* size of each member */
2551 /* Get the size of each member */
2552 /* Probably we should do that outside the loop ? */
2553 if (!isdigit(*(p + 1))) {
2554 bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
2555 " the maximum size", key);
2557 charssize = strtoul(p + 1, (char **) NULL, 10);
2560 if (strlen(str) >= charssize) {
2561 bb_error_msg_and_die("string too long for %s (max %ld)", key,
2565 /* Copy to location */
2566 strcpy((char *) loc, str);
2570 long v = strtoul(q, &q, 0);
2577 loc += tgt_sizeof_short;
2581 loc += tgt_sizeof_int;
2585 loc += tgt_sizeof_long;
2589 bb_error_msg_and_die("unknown parameter type '%c' for %s", *p, key);
2602 goto retry_end_of_value;
2606 bb_error_msg_and_die("too many values for %s (max %d)", key, max);
2612 bb_error_msg_and_die("invalid argument syntax for %s", key);
2617 bb_error_msg_and_die("too few values for %s (min %d)", key, min);
2625 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2626 static int new_is_module_checksummed(struct obj_file *f)
2628 const char *p = get_modinfo_value(f, "using_checksums");
2634 /* Get the module's kernel version in the canonical integer form. */
2637 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2642 p = get_modinfo_value(f, "kernel_version");
2645 safe_strncpy(str, p, STRVERSIONLEN);
2647 a = strtoul(p, &p, 10);
2650 b = strtoul(p + 1, &p, 10);
2653 c = strtoul(p + 1, &q, 10);
2657 return a << 16 | b << 8 | c;
2660 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2663 /* Fetch the loaded modules, and all currently exported symbols. */
2665 static void new_get_kernel_symbols(void)
2667 char *module_names, *mn;
2668 struct external_module *modules, *m;
2669 struct new_module_symbol *syms, *s;
2670 size_t ret, bufsize, nmod, nsyms, i, j;
2672 /* Collect the loaded modules. */
2675 module_names = xmalloc(bufsize);
2678 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2679 if (errno == ENOSPC && bufsize < ret) {
2681 module_names = xrealloc(module_names, bufsize);
2682 goto retry_modules_load;
2684 bb_perror_msg_and_die("QM_MODULES");
2687 n_ext_modules = nmod = ret;
2689 /* Collect the modules' symbols. */
2692 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2693 memset(modules, 0, nmod * sizeof(*modules));
2694 for (i = 0, mn = module_names, m = modules;
2695 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2696 struct new_module_info info;
2698 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2699 if (errno == ENOENT) {
2700 /* The module was removed out from underneath us. */
2703 bb_perror_msg_and_die("query_module: QM_INFO: %s", mn);
2707 syms = xmalloc(bufsize);
2709 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2713 syms = xrealloc(syms, bufsize);
2714 goto retry_mod_sym_load;
2716 /* The module was removed out from underneath us. */
2719 bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn);
2725 m->addr = info.addr;
2729 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2730 s->name += (unsigned long) syms;
2735 /* Collect the kernel's symbols. */
2737 syms = xmalloc(bufsize = 16 * 1024);
2738 retry_kern_sym_load:
2739 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2740 if (errno == ENOSPC && bufsize < ret) {
2742 syms = xrealloc(syms, bufsize);
2743 goto retry_kern_sym_load;
2745 bb_perror_msg_and_die("kernel: QM_SYMBOLS");
2747 nksyms = nsyms = ret;
2750 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2751 s->name += (unsigned long) syms;
2756 /* Return the kernel symbol checksum version, or zero if not used. */
2758 static int new_is_kernel_checksummed(void)
2760 struct new_module_symbol *s;
2763 /* Using_Versions is not the first symbol, but it should be in there. */
2765 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2766 if (strcmp((char *) s->name, "Using_Versions") == 0)
2773 static void new_create_this_module(struct obj_file *f, const char *m_name)
2775 struct obj_section *sec;
2777 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2778 sizeof(struct new_module));
2779 memset(sec->contents, 0, sizeof(struct new_module));
2781 obj_add_symbol(f, SPFX "__this_module", -1,
2782 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2783 sizeof(struct new_module));
2785 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2789 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2790 /* add an entry to the __ksymtab section, creating it if necessary */
2791 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2793 struct obj_section *sec;
2796 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2797 * If __ksymtab is defined but not marked alloc, x out the first character
2798 * (no obj_delete routine) and create a new __ksymtab with the correct
2801 sec = obj_find_section(f, "__ksymtab");
2802 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2803 *((char *)(sec->name)) = 'x'; /* override const */
2807 sec = obj_create_alloced_section(f, "__ksymtab",
2808 tgt_sizeof_void_p, 0);
2811 sec->header.sh_flags |= SHF_ALLOC;
2812 /* Empty section might be byte-aligned */
2813 sec->header.sh_addralign = tgt_sizeof_void_p;
2814 ofs = sec->header.sh_size;
2815 obj_symbol_patch(f, sec->idx, ofs, sym);
2816 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2817 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2819 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2821 static int new_create_module_ksymtab(struct obj_file *f)
2823 struct obj_section *sec;
2826 /* We must always add the module references. */
2828 if (n_ext_modules_used) {
2829 struct new_module_ref *dep;
2830 struct obj_symbol *tm;
2832 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2833 (sizeof(struct new_module_ref)
2834 * n_ext_modules_used));
2838 tm = obj_find_symbol(f, SPFX "__this_module");
2839 dep = (struct new_module_ref *) sec->contents;
2840 for (i = 0; i < n_ext_modules; ++i)
2841 if (ext_modules[i].used) {
2842 dep->dep = ext_modules[i].addr;
2843 obj_symbol_patch(f, sec->idx,
2844 (char *) &dep->ref - sec->contents, tm);
2850 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2854 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2856 /* We don't want to export symbols residing in sections that
2857 aren't loaded. There are a number of these created so that
2858 we make sure certain module options don't appear twice. */
2860 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2862 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2864 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2865 struct obj_symbol *sym;
2866 for (sym = f->symtab[i]; sym; sym = sym->next)
2867 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2868 && sym->secidx <= SHN_HIRESERVE
2869 && (sym->secidx >= SHN_LORESERVE
2870 || loaded[sym->secidx])) {
2871 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2873 obj_symbol_patch(f, sec->idx, ofs, sym);
2874 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2881 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2889 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2891 struct new_module *module;
2892 struct obj_section *sec;
2897 sec = obj_find_section(f, ".this");
2898 if (!sec || !sec->contents) {
2899 bb_perror_msg_and_die("corrupt module %s?", m_name);
2901 module = (struct new_module *) sec->contents;
2902 m_addr = sec->header.sh_addr;
2904 module->size_of_struct = sizeof(*module);
2905 module->size = m_size;
2906 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2908 sec = obj_find_section(f, "__ksymtab");
2909 if (sec && sec->header.sh_size) {
2910 module->syms = sec->header.sh_addr;
2911 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2914 if (n_ext_modules_used) {
2915 sec = obj_find_section(f, ".kmodtab");
2916 module->deps = sec->header.sh_addr;
2917 module->ndeps = n_ext_modules_used;
2921 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2923 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2925 sec = obj_find_section(f, "__ex_table");
2927 module->ex_table_start = sec->header.sh_addr;
2928 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2931 sec = obj_find_section(f, ".text.init");
2933 module->runsize = sec->header.sh_addr - m_addr;
2935 sec = obj_find_section(f, ".data.init");
2937 if (!module->runsize ||
2938 module->runsize > sec->header.sh_addr - m_addr)
2939 module->runsize = sec->header.sh_addr - m_addr;
2941 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2942 if (sec && sec->header.sh_size) {
2943 module->archdata_start = (void*)sec->header.sh_addr;
2944 module->archdata_end = module->archdata_start + sec->header.sh_size;
2946 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2947 if (sec && sec->header.sh_size) {
2948 module->kallsyms_start = (void*)sec->header.sh_addr;
2949 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2952 /* Whew! All of the initialization is complete. Collect the final
2953 module image and give it to the kernel. */
2955 image = xmalloc(m_size);
2956 obj_create_image(f, image);
2958 ret = init_module(m_name, (struct new_module *) image);
2960 bb_perror_msg("init_module: %s", m_name);
2968 /*======================================================================*/
2971 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2974 struct obj_string_patch *p;
2975 struct obj_section *strsec;
2976 size_t len = strlen(string) + 1;
2979 p = xmalloc(sizeof(*p));
2980 p->next = f->string_patches;
2981 p->reloc_secidx = secidx;
2982 p->reloc_offset = offset;
2983 f->string_patches = p;
2985 strsec = obj_find_section(f, ".kstrtab");
2986 if (strsec == NULL) {
2987 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2988 p->string_offset = 0;
2989 loc = strsec->contents;
2991 p->string_offset = strsec->header.sh_size;
2992 loc = obj_extend_section(strsec, len);
2994 memcpy(loc, string, len);
2998 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2999 struct obj_symbol *sym)
3001 struct obj_symbol_patch *p;
3003 p = xmalloc(sizeof(*p));
3004 p->next = f->symbol_patches;
3005 p->reloc_secidx = secidx;
3006 p->reloc_offset = offset;
3008 f->symbol_patches = p;
3011 static void obj_check_undefineds(struct obj_file *f)
3015 for (i = 0; i < HASH_BUCKETS; ++i) {
3016 struct obj_symbol *sym;
3017 for (sym = f->symtab[i]; sym; sym = sym->next)
3018 if (sym->secidx == SHN_UNDEF) {
3019 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3020 sym->secidx = SHN_ABS;
3024 bb_error_msg_and_die("unresolved symbol %s", sym->name);
3030 static void obj_allocate_commons(struct obj_file *f)
3032 struct common_entry {
3033 struct common_entry *next;
3034 struct obj_symbol *sym;
3035 } *common_head = NULL;
3039 for (i = 0; i < HASH_BUCKETS; ++i) {
3040 struct obj_symbol *sym;
3041 for (sym = f->symtab[i]; sym; sym = sym->next)
3042 if (sym->secidx == SHN_COMMON) {
3043 /* Collect all COMMON symbols and sort them by size so as to
3044 minimize space wasted by alignment requirements. */
3046 struct common_entry **p, *n;
3047 for (p = &common_head; *p; p = &(*p)->next)
3048 if (sym->size <= (*p)->sym->size)
3051 n = alloca(sizeof(*n));
3059 for (i = 1; i < f->local_symtab_size; ++i) {
3060 struct obj_symbol *sym = f->local_symtab[i];
3061 if (sym && sym->secidx == SHN_COMMON) {
3062 struct common_entry **p, *n;
3063 for (p = &common_head; *p; p = &(*p)->next)
3064 if (sym == (*p)->sym)
3066 else if (sym->size < (*p)->sym->size) {
3067 n = alloca(sizeof(*n));
3077 /* Find the bss section. */
3078 for (i = 0; i < f->header.e_shnum; ++i)
3079 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3082 /* If for some reason there hadn't been one, create one. */
3083 if (i == f->header.e_shnum) {
3084 struct obj_section *sec;
3086 f->sections = xrealloc_vector(f->sections, 2, i);
3087 f->sections[i] = sec = arch_new_section();
3088 f->header.e_shnum = i + 1;
3090 sec->header.sh_type = SHT_PROGBITS;
3091 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3096 /* Allocate the COMMONS. */
3098 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3099 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3100 struct common_entry *c;
3102 for (c = common_head; c; c = c->next) {
3103 ElfW(Addr) align = c->sym->value;
3105 if (align > max_align)
3107 if (bss_size & (align - 1))
3108 bss_size = (bss_size | (align - 1)) + 1;
3111 c->sym->value = bss_size;
3113 bss_size += c->sym->size;
3116 f->sections[i]->header.sh_size = bss_size;
3117 f->sections[i]->header.sh_addralign = max_align;
3121 /* For the sake of patch relocation and parameter initialization,
3122 allocate zeroed data for NOBITS sections now. Note that after
3123 this we cannot assume NOBITS are really empty. */
3124 for (i = 0; i < f->header.e_shnum; ++i) {
3125 struct obj_section *s = f->sections[i];
3126 if (s->header.sh_type == SHT_NOBITS) {
3127 if (s->header.sh_size != 0)
3128 s->contents = memset(xmalloc(s->header.sh_size),
3129 0, s->header.sh_size);
3133 s->header.sh_type = SHT_PROGBITS;
3138 static unsigned long obj_load_size(struct obj_file *f)
3140 unsigned long dot = 0;
3141 struct obj_section *sec;
3143 /* Finalize the positions of the sections relative to one another. */
3145 for (sec = f->load_order; sec; sec = sec->load_next) {
3148 align = sec->header.sh_addralign;
3149 if (align && (dot & (align - 1)))
3150 dot = (dot | (align - 1)) + 1;
3152 sec->header.sh_addr = dot;
3153 dot += sec->header.sh_size;
3159 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3161 int i, n = f->header.e_shnum;
3164 /* Finalize the addresses of the sections. */
3167 for (i = 0; i < n; ++i)
3168 f->sections[i]->header.sh_addr += base;
3170 /* And iterate over all of the relocations. */
3172 for (i = 0; i < n; ++i) {
3173 struct obj_section *relsec, *symsec, *targsec, *strsec;
3174 ElfW(RelM) * rel, *relend;
3178 relsec = f->sections[i];
3179 if (relsec->header.sh_type != SHT_RELM)
3182 symsec = f->sections[relsec->header.sh_link];
3183 targsec = f->sections[relsec->header.sh_info];
3184 strsec = f->sections[symsec->header.sh_link];
3186 rel = (ElfW(RelM) *) relsec->contents;
3187 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3188 symtab = (ElfW(Sym) *) symsec->contents;
3189 strtab = (const char *) strsec->contents;
3191 for (; rel < relend; ++rel) {
3192 ElfW(Addr) value = 0;
3193 struct obj_symbol *intsym = NULL;
3194 unsigned long symndx;
3195 ElfW(Sym) * extsym = 0;
3198 /* Attempt to find a value to use for this relocation. */
3200 symndx = ELF_R_SYM(rel->r_info);
3202 /* Note we've already checked for undefined symbols. */
3204 extsym = &symtab[symndx];
3205 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3206 /* Local symbols we look up in the local table to be sure
3207 we get the one that is really intended. */
3208 intsym = f->local_symtab[symndx];
3210 /* Others we look up in the hash table. */
3212 if (extsym->st_name)
3213 name = strtab + extsym->st_name;
3215 name = f->sections[extsym->st_shndx]->name;
3216 intsym = obj_find_symbol(f, name);
3219 value = obj_symbol_final_value(f, intsym);
3220 intsym->referenced = 1;
3222 #if SHT_RELM == SHT_RELA
3223 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3224 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3225 if (!extsym || !extsym->st_name ||
3226 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3228 value += rel->r_addend;
3232 switch (arch_apply_relocation
3233 (f, targsec, /*symsec,*/ intsym, rel, value)
3238 case obj_reloc_overflow:
3239 errmsg = "Relocation overflow";
3241 case obj_reloc_dangerous:
3242 errmsg = "Dangerous relocation";
3244 case obj_reloc_unhandled:
3245 errmsg = "Unhandled relocation";
3248 bb_error_msg("%s of type %ld for %s", errmsg,
3249 (long) ELF_R_TYPE(rel->r_info),
3250 strtab + extsym->st_name);
3252 bb_error_msg("%s of type %ld", errmsg,
3253 (long) ELF_R_TYPE(rel->r_info));
3261 /* Finally, take care of the patches. */
3263 if (f->string_patches) {
3264 struct obj_string_patch *p;
3265 struct obj_section *strsec;
3266 ElfW(Addr) strsec_base;
3267 strsec = obj_find_section(f, ".kstrtab");
3268 strsec_base = strsec->header.sh_addr;
3270 for (p = f->string_patches; p; p = p->next) {
3271 struct obj_section *targsec = f->sections[p->reloc_secidx];
3272 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3273 = strsec_base + p->string_offset;
3277 if (f->symbol_patches) {
3278 struct obj_symbol_patch *p;
3280 for (p = f->symbol_patches; p; p = p->next) {
3281 struct obj_section *targsec = f->sections[p->reloc_secidx];
3282 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3283 = obj_symbol_final_value(f, p->sym);
3290 static int obj_create_image(struct obj_file *f, char *image)
3292 struct obj_section *sec;
3293 ElfW(Addr) base = f->baseaddr;
3295 for (sec = f->load_order; sec; sec = sec->load_next) {
3298 if (sec->contents == 0 || sec->header.sh_size == 0)
3301 secimg = image + (sec->header.sh_addr - base);
3303 /* Note that we allocated data for NOBITS sections earlier. */
3304 memcpy(secimg, sec->contents, sec->header.sh_size);
3310 /*======================================================================*/
3312 static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
3315 ElfW(Shdr) * section_headers;
3319 /* Read the file header. */
3321 f = arch_new_file();
3322 f->symbol_cmp = strcmp;
3323 f->symbol_hash = obj_elf_hash;
3324 f->load_order_search_start = &f->load_order;
3326 fseek(fp, 0, SEEK_SET);
3327 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3328 bb_perror_msg_and_die("error reading ELF header");
3331 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3332 || f->header.e_ident[EI_MAG1] != ELFMAG1
3333 || f->header.e_ident[EI_MAG2] != ELFMAG2
3334 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3335 bb_error_msg_and_die("not an ELF file");
3337 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3338 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3339 ? ELFDATA2MSB : ELFDATA2LSB)
3340 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3341 || !MATCH_MACHINE(f->header.e_machine)) {
3342 bb_error_msg_and_die("ELF file not for this architecture");
3344 if (f->header.e_type != ET_REL) {
3345 bb_error_msg_and_die("ELF file not a relocatable object");
3348 /* Read the section headers. */
3350 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3351 bb_error_msg_and_die("section header size mismatch: %lu != %lu",
3352 (unsigned long) f->header.e_shentsize,
3353 (unsigned long) sizeof(ElfW(Shdr)));
3356 shnum = f->header.e_shnum;
3357 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3358 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3360 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3361 fseek(fp, f->header.e_shoff, SEEK_SET);
3362 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3363 bb_perror_msg_and_die("error reading ELF section headers");
3366 /* Read the section data. */
3368 for (i = 0; i < shnum; ++i) {
3369 struct obj_section *sec;
3371 f->sections[i] = sec = arch_new_section();
3373 sec->header = section_headers[i];
3376 if (sec->header.sh_size) {
3377 switch (sec->header.sh_type) {
3386 if (!loadprogbits) {
3387 sec->contents = NULL;
3394 if (sec->header.sh_size > 0) {
3395 sec->contents = xmalloc(sec->header.sh_size);
3396 fseek(fp, sec->header.sh_offset, SEEK_SET);
3397 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3398 bb_perror_msg_and_die("error reading ELF section data");
3401 sec->contents = NULL;
3405 #if SHT_RELM == SHT_REL
3407 bb_error_msg_and_die("RELA relocations not supported on this architecture");
3410 bb_error_msg_and_die("REL relocations not supported on this architecture");
3413 if (sec->header.sh_type >= SHT_LOPROC) {
3414 /* Assume processor specific section types are debug
3415 info and can safely be ignored. If this is ever not
3416 the case (Hello MIPS?), don't put ifdefs here but
3417 create an arch_load_proc_section(). */
3421 bb_error_msg_and_die("can't handle sections of type %ld",
3422 (long) sec->header.sh_type);
3427 /* Do what sort of interpretation as needed by each section. */
3429 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3431 for (i = 0; i < shnum; ++i) {
3432 struct obj_section *sec = f->sections[i];
3433 sec->name = shstrtab + sec->header.sh_name;
3436 for (i = 0; i < shnum; ++i) {
3437 struct obj_section *sec = f->sections[i];
3439 /* .modinfo should be contents only but gcc has no attribute for that.
3440 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3442 if (strcmp(sec->name, ".modinfo") == 0)
3443 sec->header.sh_flags &= ~SHF_ALLOC;
3445 if (sec->header.sh_flags & SHF_ALLOC)
3446 obj_insert_section_load_order(f, sec);
3448 switch (sec->header.sh_type) {
3451 unsigned long nsym, j;
3455 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3456 bb_error_msg_and_die("symbol size mismatch: %lu != %lu",
3457 (unsigned long) sec->header.sh_entsize,
3458 (unsigned long) sizeof(ElfW(Sym)));
3461 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3462 strtab = f->sections[sec->header.sh_link]->contents;
3463 sym = (ElfW(Sym) *) sec->contents;
3465 /* Allocate space for a table of local symbols. */
3466 j = f->local_symtab_size = sec->header.sh_info;
3467 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3469 /* Insert all symbols into the hash table. */
3470 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3471 ElfW(Addr) val = sym->st_value;
3474 name = strtab + sym->st_name;
3475 else if (sym->st_shndx < shnum)
3476 name = f->sections[sym->st_shndx]->name;
3479 #if defined(__SH5__)
3481 * For sh64 it is possible that the target of a branch
3482 * requires a mode switch (32 to 16 and back again).
3484 * This is implied by the lsb being set in the target
3485 * address for SHmedia mode and clear for SHcompact.
3487 val |= sym->st_other & 4;
3489 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3496 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3497 bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu",
3498 (unsigned long) sec->header.sh_entsize,
3499 (unsigned long) sizeof(ElfW(RelM)));
3502 /* XXX Relocation code from modutils-2.3.19 is not here.
3503 * Why? That's about 20 lines of code from obj/obj_load.c,
3504 * which gets done in a second pass through the sections.
3505 * This BusyBox insmod does similar work in obj_relocate(). */
3512 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3514 * load the unloaded sections directly into the memory allocated by
3515 * kernel for the module
3518 static int obj_load_progbits(FILE *fp, struct obj_file *f, char *imagebase)
3520 ElfW(Addr) base = f->baseaddr;
3521 struct obj_section* sec;
3523 for (sec = f->load_order; sec; sec = sec->load_next) {
3525 /* section already loaded? */
3526 if (sec->contents != NULL)
3529 if (sec->header.sh_size == 0)
3532 sec->contents = imagebase + (sec->header.sh_addr - base);
3533 fseek(fp, sec->header.sh_offset, SEEK_SET);
3534 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3535 bb_perror_msg("error reading ELF section data");
3544 static void hide_special_symbols(struct obj_file *f)
3546 static const char *const specials[] = {
3547 SPFX "cleanup_module",
3549 SPFX "kernel_version",
3553 struct obj_symbol *sym;
3554 const char *const *p;
3556 for (p = specials; *p; ++p) {
3557 sym = obj_find_symbol(f, *p);
3559 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3564 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3565 static int obj_gpl_license(struct obj_file *f, const char **license)
3567 struct obj_section *sec;
3568 /* This list must match *exactly* the list of allowable licenses in
3569 * linux/include/linux/module.h. Checking for leading "GPL" will not
3570 * work, somebody will use "GPL sucks, this is proprietary".
3572 static const char *const gpl_licenses[] = {
3575 "GPL and additional rights",
3580 sec = obj_find_section(f, ".modinfo");
3582 const char *value, *ptr, *endptr;
3583 ptr = sec->contents;
3584 endptr = ptr + sec->header.sh_size;
3585 while (ptr < endptr) {
3586 value = strchr(ptr, '=');
3587 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3591 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
3592 if (strcmp(value+1, gpl_licenses[i]) == 0)
3597 ptr = strchr(ptr, '\0');
3607 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3608 #define TAINT_PROPRIETORY_MODULE (1 << 0)
3609 #define TAINT_FORCED_MODULE (1 << 1)
3610 #define TAINT_UNSAFE_SMP (1 << 2)
3611 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3613 static void set_tainted(int fd, char *m_name,
3614 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3616 static smallint printed_info;
3621 if (fd < 0 && !kernel_has_tainted)
3622 return; /* New modutils on old kernel */
3623 printf("Warning: loading %s will taint the kernel: %s%s\n",
3624 m_name, text1, text2);
3625 if (!printed_info) {
3626 printf(" See %s for information about tainted modules\n", TAINT_URL);
3630 read(fd, buf, sizeof(buf)-1);
3631 buf[sizeof(buf)-1] = '\0';
3632 oldval = strtoul(buf, NULL, 10);
3633 sprintf(buf, "%d\n", oldval | taint);
3634 write(fd, buf, strlen(buf));
3638 /* Check if loading this module will taint the kernel. */
3639 static void check_tainted_module(struct obj_file *f, char *m_name)
3641 static const char tainted_file[] ALIGN1 = TAINT_FILENAME;
3643 int fd, kernel_has_tainted;
3646 kernel_has_tainted = 1;
3647 fd = open(tainted_file, O_RDWR);
3649 if (errno == ENOENT)
3650 kernel_has_tainted = 0;
3651 else if (errno == EACCES)
3652 kernel_has_tainted = 1;
3654 perror(tainted_file);
3655 kernel_has_tainted = 0;
3659 switch (obj_gpl_license(f, &ptr)) {
3663 set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3666 /* The module has a non-GPL license so we pretend that the
3667 * kernel always has a taint flag to get a warning even on
3668 * kernels without the proc flag.
3670 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3673 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3677 if (flag_force_load)
3678 set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3683 #else /* FEATURE_CHECK_TAINTED_MODULE */
3684 #define check_tainted_module(x, y) do { } while (0);
3685 #endif /* FEATURE_CHECK_TAINTED_MODULE */
3687 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3688 /* add module source, timestamp, kernel version and a symbol for the
3689 * start of some sections. this info is used by ksymoops to do better
3692 #if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3693 #define get_module_version(f, str) get_module_version(str)
3696 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3698 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3699 return new_get_module_version(f, str);
3700 #else /* FEATURE_INSMOD_VERSION_CHECKING */
3701 strncpy(str, "???", sizeof(str));
3703 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
3706 /* add module source, timestamp, kernel version and a symbol for the
3707 * start of some sections. this info is used by ksymoops to do better
3711 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3714 static const char symprefix[] ALIGN1 = "__insmod_";
3715 static const char section_names[][8] = {
3723 struct obj_section *sec;
3724 struct obj_symbol *sym;
3725 char *name, *absolute_filename;
3726 char str[STRVERSIONLEN];
3728 int l, lm_name, lfilename, use_ksymtab, version;
3729 struct stat statbuf;
3731 /* WARNING: was using realpath, but replaced by readlink to stop using
3732 * lots of stack. But here it seems to be able to cause problems? */
3733 absolute_filename = xmalloc_readlink(filename);
3734 if (!absolute_filename)
3735 absolute_filename = xstrdup(filename);
3737 lm_name = strlen(m_name);
3738 lfilename = strlen(absolute_filename);
3740 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3741 * are not to be exported. otherwise leave ksymtab alone for now, the
3742 * "export all symbols" compatibility code will export these symbols later.
3744 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3746 sec = obj_find_section(f, ".this");
3748 /* tag the module header with the object name, last modified
3749 * timestamp and module version. worst case for module version
3750 * is 0xffffff, decimal 16777215. putting all three fields in
3751 * one symbol is less readable but saves kernel space.
3753 l = sizeof(symprefix) + /* "__insmod_" */
3754 lm_name + /* module name */
3756 lfilename + /* object filename */
3758 2 * sizeof(statbuf.st_mtime) + /* mtime in hex */
3760 8 + /* version in dec */
3763 if (stat(absolute_filename, &statbuf) != 0)
3764 statbuf.st_mtime = 0;
3765 version = get_module_version(f, str); /* -1 if not found */
3766 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3767 symprefix, m_name, absolute_filename,
3768 (int)(2 * sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3770 sym = obj_add_symbol(f, name, -1,
3771 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3772 sec->idx, sec->header.sh_addr, 0);
3774 new_add_ksymtab(f, sym);
3776 free(absolute_filename);
3777 #ifdef _NOT_SUPPORTED_
3778 /* record where the persistent data is going, same address as previous symbol */
3781 l = sizeof(symprefix) + /* "__insmod_" */
3782 lm_name + /* module name */
3784 strlen(f->persist) + /* data store */
3787 snprintf(name, l, "%s%s_P%s",
3788 symprefix, m_name, f->persist);
3789 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3790 sec->idx, sec->header.sh_addr, 0);
3792 new_add_ksymtab(f, sym);
3794 #endif /* _NOT_SUPPORTED_ */
3795 /* tag the desired sections if size is non-zero */
3797 for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
3798 sec = obj_find_section(f, section_names[i]);
3799 if (sec && sec->header.sh_size) {
3800 l = sizeof(symprefix) + /* "__insmod_" */
3801 lm_name + /* module name */
3803 strlen(sec->name) + /* section name */
3805 8 + /* length in dec */
3808 snprintf(name, l, "%s%s_S%s_L%ld",
3809 symprefix, m_name, sec->name,
3810 (long)sec->header.sh_size);
3811 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3812 sec->idx, sec->header.sh_addr, 0);
3814 new_add_ksymtab(f, sym);
3818 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3820 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3821 static void print_load_map(struct obj_file *f)
3823 struct obj_section *sec;
3824 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3825 struct obj_symbol **all, **p;
3826 int i, nsyms, *loaded;
3827 struct obj_symbol *sym;
3829 /* Report on the section layout. */
3831 printf("Sections: Size %-*s Align\n",
3832 (int) (2 * sizeof(void *)), "Address");
3834 for (sec = f->load_order; sec; sec = sec->load_next) {
3838 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3843 printf("%-15s %08lx %0*lx 2**%d\n",
3845 (long)sec->header.sh_size,
3846 (int) (2 * sizeof(void *)),
3847 (long)sec->header.sh_addr,
3850 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3851 /* Quick reference which section indices are loaded. */
3853 i = f->header.e_shnum;
3854 loaded = alloca(sizeof(int) * i);
3856 loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
3858 /* Collect the symbols we'll be listing. */
3860 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3861 for (sym = f->symtab[i]; sym; sym = sym->next)
3862 if (sym->secidx <= SHN_HIRESERVE
3863 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3866 all = alloca(nsyms * sizeof(struct obj_symbol *));
3868 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3869 for (sym = f->symtab[i]; sym; sym = sym->next)
3870 if (sym->secidx <= SHN_HIRESERVE
3871 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3874 /* And list them. */
3875 printf("\nSymbols:\n");
3876 for (p = all; p < all + nsyms; ++p) {
3878 unsigned long value;
3881 if (sym->secidx == SHN_ABS) {
3884 } else if (sym->secidx == SHN_UNDEF) {
3888 sec = f->sections[sym->secidx];
3890 if (sec->header.sh_type == SHT_NOBITS)
3892 else if (sec->header.sh_flags & SHF_ALLOC) {
3893 if (sec->header.sh_flags & SHF_EXECINSTR)
3895 else if (sec->header.sh_flags & SHF_WRITE)
3900 value = sym->value + sec->header.sh_addr;
3903 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3904 type = tolower(type);
3906 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3911 #else /* !FEATURE_INSMOD_LOAD_MAP */
3912 void print_load_map(struct obj_file *f);
3915 int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
3916 int insmod_main(int argc, char **argv)
3922 unsigned long m_size;
3926 char *m_name = NULL;
3927 int exit_status = EXIT_FAILURE;
3929 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3930 struct utsname uts_info;
3931 char m_strversion[STRVERSIONLEN];
3932 int m_version, m_crcs;
3934 #if ENABLE_FEATURE_CLEAN_UP
3940 struct utsname myuname;
3942 /* Parse any options */
3943 getopt32(argv, OPTION_STR, &opt_o);
3944 arg1 = argv[optind];
3945 if (option_mask32 & OPT_o) { // -o /* name the output module */
3947 m_name = xstrdup(opt_o);
3954 /* Grab the module name */
3955 tmp1 = xstrdup(arg1);
3956 tmp = basename(tmp1);
3959 if (uname(&myuname) == 0) {
3960 if (myuname.release[0] == '2') {
3961 k_version = myuname.release[2] - '0';
3965 #if ENABLE_FEATURE_2_6_MODULES
3966 if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
3967 && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
3973 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3979 #if ENABLE_FEATURE_2_6_MODULES
3981 m_fullName = xasprintf("%s.ko", tmp);
3984 m_fullName = xasprintf("%s.o", tmp);
3990 tmp1 = NULL; /* flag for free(m_name) before exit() */
3993 /* Get a filedesc for the module. Check that we have a complete path */
3994 if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
3995 || (fp = fopen_for_read(arg1)) == NULL
3997 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3998 * but do not error out yet if we fail to find it... */
3999 if (k_version) { /* uname succeedd */
4003 tmdn = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, myuname.release);
4004 /* Jump through hoops in case /lib/modules/`uname -r`
4005 * is a symlink. We do not want recursive_action to
4006 * follow symlinks, but we do want to follow the
4007 * /lib/modules/`uname -r` dir, So resolve it ourselves
4008 * if it is a link... */
4009 module_dir = xmalloc_readlink(tmdn);
4011 module_dir = xstrdup(tmdn);
4012 recursive_action(module_dir, ACTION_RECURSE,
4013 check_module_name_match, NULL, m_fullName, 0);
4018 /* Check if we have found anything yet */
4019 if (!m_filename || ((fp = fopen_for_read(m_filename)) == NULL)) {
4025 module_dir = xmalloc_readlink(CONFIG_DEFAULT_MODULES_DIR);
4027 module_dir = xstrdup(CONFIG_DEFAULT_MODULES_DIR);
4028 /* No module found under /lib/modules/`uname -r`, this
4029 * time cast the net a bit wider. Search /lib/modules/ */
4030 r = recursive_action(module_dir, ACTION_RECURSE,
4031 check_module_name_match, NULL, m_fullName, 0);
4033 bb_error_msg_and_die("%s: module not found", m_fullName);
4035 if (m_filename == NULL
4036 || ((fp = fopen_for_read(m_filename)) == NULL)
4038 bb_error_msg_and_die("%s: module not found", m_fullName);
4042 m_filename = xstrdup(arg1);
4045 printf("Using %s\n", m_filename);
4047 #if ENABLE_FEATURE_2_6_MODULES
4048 if (k_version > 4) {
4049 argv[optind] = m_filename;
4051 return insmod_ng_main(argc - optind, argv + optind);
4055 f = obj_load(fp, LOADBITS);
4057 if (get_modinfo_value(f, "kernel_version") == NULL)
4062 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4063 /* Version correspondence? */
4065 if (uname(&uts_info) < 0)
4066 uts_info.release[0] = '\0';
4067 if (m_has_modinfo) {
4068 m_version = new_get_module_version(f, m_strversion);
4069 if (m_version == -1) {
4070 bb_error_msg_and_die("cannot find the kernel version the module was "
4075 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4076 bb_error_msg("%skernel-module version mismatch\n"
4077 "\t%s was compiled for kernel version %s\n"
4078 "\twhile this kernel is version %s",
4079 flag_force_load ? "warning: " : "",
4080 m_filename, m_strversion, uts_info.release);
4081 if (!flag_force_load)
4086 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4088 if (query_module(NULL, 0, NULL, 0, NULL))
4089 bb_error_msg_and_die("not configured to support old kernels");
4090 new_get_kernel_symbols();
4091 k_crcs = new_is_kernel_checksummed();
4093 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4096 m_crcs = new_is_module_checksummed(f);
4098 if (m_crcs != k_crcs)
4099 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4100 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4102 /* Let the module know about the kernel symbols. */
4103 add_kernel_symbols(f);
4105 /* Allocate common symbols, symbol tables, and string tables. */
4107 new_create_this_module(f, m_name);
4108 obj_check_undefineds(f);
4109 obj_allocate_commons(f);
4110 check_tainted_module(f, m_name);
4112 /* done with the module name, on to the optional var=value arguments */
4114 if (optind < argc) {
4115 new_process_module_arguments(f, argc - optind, argv + optind);
4119 hide_special_symbols(f);
4121 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4122 add_ksymoops_symbols(f, m_filename, m_name);
4123 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4125 new_create_module_ksymtab(f);
4127 /* Find current size of the module */
4128 m_size = obj_load_size(f);
4130 m_addr = create_module(m_name, m_size);
4131 if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
4133 bb_error_msg_and_die("a module named %s already exists", m_name);
4135 bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes",
4138 bb_perror_msg_and_die("create_module: %s", m_name);
4143 * the PROGBITS section was not loaded by the obj_load
4144 * now we can load them directly into the kernel memory
4146 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4147 delete_module(m_name, 0);
4152 if (!obj_relocate(f, m_addr)) {
4153 delete_module(m_name, 0);
4157 if (!new_init_module(m_name, f, m_size)) {
4158 delete_module(m_name, 0);
4162 if (flag_print_load_map)
4165 exit_status = EXIT_SUCCESS;
4168 #if ENABLE_FEATURE_CLEAN_UP
4179 #endif /* ENABLE_FEATURE_2_4_MODULES */
4181 * End of big piece of 2.4-specific code
4185 #if ENABLE_FEATURE_2_6_MODULES
4187 #include <sys/mman.h>
4189 #if defined __UCLIBC__ && !ENABLE_FEATURE_2_4_MODULES
4190 /* big time suckage. The old prototype above renders our nice fwd-decl wrong */
4191 extern int init_module(void *module, unsigned long len, const char *options);
4193 #include <asm/unistd.h>
4194 #include <sys/syscall.h>
4195 #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
4198 /* We use error numbers in a loose translation... */
4199 static const char *moderror(int err)
4203 return "invalid module format";
4205 return "unknown symbol in module";
4207 return "module has wrong symbol version";
4209 return "invalid parameters";
4211 return strerror(err);
4215 #if !ENABLE_FEATURE_2_4_MODULES
4216 int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
4217 int insmod_main(int argc UNUSED_PARAM, char **argv)
4219 static int insmod_ng_main(int argc UNUSED_PARAM, char **argv)
4225 char *filename, *options;
4231 /* Rest is options */
4232 options = xzalloc(1);
4235 options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
4236 /* Spaces handled by "" pairs, but no way of escaping quotes */
4237 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
4241 /* Any special reason why mmap? It isn't performance critical. -vda */
4242 /* Yes, xmalloc'ing can use *alot* of RAM. Don't forget that there are
4243 * modules out there that are half a megabyte! mmap()ing is way nicer
4244 * for small mem boxes, i guess. */
4245 /* But after load, these modules will take up that 0.5mb in kernel
4246 * anyway. Using malloc here causes only a transient spike to 1mb,
4247 * after module is loaded, we go back to normal 0.5mb usage
4248 * (in kernel). Also, mmap isn't magic - when we touch mapped data,
4249 * we use memory. -vda */
4253 fd = xopen(filename, O_RDONLY);
4256 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4257 if (map == MAP_FAILED) {
4258 bb_perror_msg_and_die("cannot mmap '%s'", filename);
4261 /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
4264 xread(fd, map, len);
4267 len = MAXINT(ssize_t);
4268 map = xmalloc_open_read_close(filename, &len);
4271 if (init_module(map, len, options) != 0)
4272 bb_error_msg_and_die("cannot insert '%s': %s",
4273 filename, moderror(errno));