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 check_module_name_match(const char *filename,
795 struct stat *statbuf ATTRIBUTE_UNUSED,
796 void *userdata, int depth ATTRIBUTE_UNUSED)
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 struct arch_file *ifile = (struct arch_file *) f;
846 enum obj_reloc ret = obj_reloc_ok;
847 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
848 #if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
849 || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
850 || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
851 || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
852 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
854 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
855 struct arch_symbol *isym = (struct arch_symbol *) sym;
857 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
858 || defined(__sh__) || defined(__s390__)
859 #if defined(USE_GOT_ENTRIES)
860 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
863 #if defined(USE_PLT_ENTRIES)
864 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
866 # if defined(USE_PLT_LIST)
867 struct arch_list_entry *pe;
869 struct arch_single_entry *pe;
873 switch (ELF_R_TYPE(rel->r_info)) {
888 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
889 * (which is .got) similar to branch,
890 * but is full 32 bits relative */
899 case R_ARM_GOTOFF: /* address relative to the got */
903 #elif defined(__cris__)
909 /* CRIS keeps the relocation value in the r_addend field and
910 * should not use whats in *loc at all
915 #elif defined(__H8300H__) || defined(__H8300S__)
918 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
919 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
930 if ((ElfW(Sword))v > 0x7fff ||
931 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
932 ret = obj_reloc_overflow;
934 *(unsigned short *)loc = v;
938 if ((ElfW(Sword))v > 0x7f ||
939 (ElfW(Sword))v < -(ElfW(Sword))0x80)
940 ret = obj_reloc_overflow;
942 *(unsigned char *)loc = v;
945 #elif defined(__i386__)
977 #elif defined(__microblaze__)
978 case R_MICROBLAZE_NONE:
979 case R_MICROBLAZE_64_NONE:
980 case R_MICROBLAZE_32_SYM_OP_SYM:
981 case R_MICROBLAZE_32_PCREL:
984 case R_MICROBLAZE_64_PCREL: {
985 /* dot is the address of the current instruction.
986 * v is the target symbol address.
987 * So we need to extract the offset in the code,
988 * adding v, then subtrating the current address
989 * of this instruction.
990 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
993 /* Get split offset stored in code */
994 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
997 /* Adjust relative offset. -4 adjustment required
998 * because dot points to the IMM insn, but branch
999 * is computed relative to the branch instruction itself.
1001 temp += v - dot - 4;
1003 /* Store back into code */
1004 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
1005 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
1010 case R_MICROBLAZE_32:
1014 case R_MICROBLAZE_64: {
1015 /* Get split pointer stored in code */
1016 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
1019 /* Add reloc offset */
1022 /* Store back into code */
1023 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
1024 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
1029 case R_MICROBLAZE_32_PCREL_LO:
1030 case R_MICROBLAZE_32_LO:
1031 case R_MICROBLAZE_SRO32:
1032 case R_MICROBLAZE_SRW32:
1033 ret = obj_reloc_unhandled;
1036 #elif defined(__mc68000__)
1047 ret = obj_reloc_overflow;
1054 ret = obj_reloc_overflow;
1061 if ((ElfW(Sword))v > 0x7f ||
1062 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1063 ret = obj_reloc_overflow;
1070 if ((ElfW(Sword))v > 0x7fff ||
1071 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1072 ret = obj_reloc_overflow;
1078 *(int *)loc = v - dot;
1081 case R_68K_GLOB_DAT:
1082 case R_68K_JMP_SLOT:
1086 case R_68K_RELATIVE:
1087 *(int *)loc += f->baseaddr;
1093 # ifdef R_68K_GOTOFF
1099 #elif defined(__mips__)
1110 ret = obj_reloc_dangerous;
1111 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1112 ret = obj_reloc_overflow;
1114 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1120 struct mips_hi16 *n;
1122 /* We cannot relocate this one now because we don't know the value
1123 of the carry we need to add. Save the information, and let LO16
1124 do the actual relocation. */
1125 n = xmalloc(sizeof *n);
1128 n->next = ifile->mips_hi16_list;
1129 ifile->mips_hi16_list = n;
1135 unsigned long insnlo = *loc;
1136 ElfW(Addr) val, vallo;
1138 /* Sign extend the addend we extract from the lo insn. */
1139 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1141 if (ifile->mips_hi16_list != NULL) {
1142 struct mips_hi16 *l;
1144 l = ifile->mips_hi16_list;
1146 struct mips_hi16 *next;
1149 /* Do the HI16 relocation. Note that we actually don't
1150 need to know anything about the LO16 itself, except where
1151 to find the low 16 bits of the addend needed by the LO16. */
1154 ((insn & 0xffff) << 16) +
1158 /* Account for the sign extension that will happen in the
1165 insn = (insn & ~0xffff) | val;
1173 ifile->mips_hi16_list = NULL;
1176 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1178 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1183 #elif defined(__nios2__)
1188 case R_NIOS2_BFD_RELOC_32:
1192 case R_NIOS2_BFD_RELOC_16:
1194 ret = obj_reloc_overflow;
1199 case R_NIOS2_BFD_RELOC_8:
1201 ret = obj_reloc_overflow;
1210 if ((Elf32_Sword)v > 0x7fff ||
1211 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1212 ret = obj_reloc_overflow;
1216 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1226 ret = obj_reloc_overflow;
1230 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1235 case R_NIOS2_PCREL16:
1240 if ((Elf32_Sword)v > 0x7fff ||
1241 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1242 ret = obj_reloc_overflow;
1246 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1252 Elf32_Addr word, gp;
1254 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1256 if ((Elf32_Sword)v > 0x7fff ||
1257 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1258 ret = obj_reloc_overflow;
1262 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1266 case R_NIOS2_CALL26:
1268 ret = obj_reloc_dangerous;
1269 if ((v >> 28) != (dot >> 28))
1270 ret = obj_reloc_overflow;
1271 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1279 ret = obj_reloc_overflow;
1282 word = *loc & ~0x7c0;
1283 *loc = word | ((v & 0x1f) << 6);
1292 ret = obj_reloc_overflow;
1295 word = *loc & ~0xfc0;
1296 *loc = word | ((v & 0x3f) << 6);
1305 ret = obj_reloc_overflow;
1308 word = *loc & ~0x3fc0;
1309 *loc = word | ((v & 0xff) << 6);
1318 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1328 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1333 case R_NIOS2_HIADJ16:
1335 Elf32_Addr word1, word2;
1338 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1339 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1344 #elif defined(__powerpc64__)
1345 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1347 #elif defined(__powerpc__)
1349 case R_PPC_ADDR16_HA:
1350 *(unsigned short *)loc = (v + 0x8000) >> 16;
1353 case R_PPC_ADDR16_HI:
1354 *(unsigned short *)loc = v >> 16;
1357 case R_PPC_ADDR16_LO:
1358 *(unsigned short *)loc = v;
1372 #elif defined(__s390__)
1375 *(unsigned int *) loc += v;
1378 *(unsigned short *) loc += v;
1381 *(unsigned char *) loc += v;
1385 *(unsigned int *) loc += v - dot;
1388 *(unsigned short *) loc += (v - dot) >> 1;
1391 *(unsigned short *) loc += v - dot;
1395 case R_390_PLT16DBL:
1396 /* find the plt entry and initialize it. */
1397 pe = (struct arch_single_entry *) &isym->pltent;
1398 if (pe->inited == 0) {
1399 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1400 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1402 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1409 /* Insert relative distance to target. */
1410 v = plt + pe->offset - dot;
1411 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1412 *(unsigned int *) loc = (unsigned int) v;
1413 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1414 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1417 case R_390_GLOB_DAT:
1418 case R_390_JMP_SLOT:
1422 case R_390_RELATIVE:
1423 *loc += f->baseaddr;
1427 *(unsigned long *) loc += got - dot;
1433 if (!isym->gotent.inited)
1435 isym->gotent.inited = 1;
1436 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1438 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1439 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1440 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1441 *(unsigned short *) loc += isym->gotent.offset;
1442 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1443 *(unsigned int *) loc += isym->gotent.offset;
1446 # ifndef R_390_GOTOFF32
1447 # define R_390_GOTOFF32 R_390_GOTOFF
1449 case R_390_GOTOFF32:
1453 #elif defined(__sh__)
1476 *loc = f->baseaddr + rel->r_addend;
1480 *loc = got - dot + rel->r_addend;
1490 # if defined(__SH5__)
1491 case R_SH_IMM_MEDLOW16:
1492 case R_SH_IMM_LOW16:
1496 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1500 * movi and shori have the format:
1502 * | op | imm | reg | reserved |
1503 * 31..26 25..10 9.. 4 3 .. 0
1505 * so we simply mask and or in imm.
1507 word = *loc & ~0x3fffc00;
1508 word |= (v & 0xffff) << 10;
1515 case R_SH_IMM_MEDLOW16_PCREL:
1516 case R_SH_IMM_LOW16_PCREL:
1520 word = *loc & ~0x3fffc00;
1524 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1527 word |= (v & 0xffff) << 10;
1533 # endif /* __SH5__ */
1535 #elif defined(__v850e__)
1541 /* We write two shorts instead of a long because even
1542 32-bit insns only need half-word alignment, but
1543 32-bit data needs to be long-word aligned. */
1544 v += ((unsigned short *)loc)[0];
1545 v += ((unsigned short *)loc)[1] << 16;
1546 ((unsigned short *)loc)[0] = v & 0xffff;
1547 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1550 case R_V850_22_PCREL:
1553 #elif defined(__x86_64__)
1563 *(unsigned int *) loc += v;
1566 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1567 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1572 *(signed int *) loc += v;
1576 *(unsigned short *) loc += v;
1580 *(unsigned char *) loc += v;
1584 *(unsigned int *) loc += v - dot;
1588 *(unsigned short *) loc += v - dot;
1592 *(unsigned char *) loc += v - dot;
1595 case R_X86_64_GLOB_DAT:
1596 case R_X86_64_JUMP_SLOT:
1600 case R_X86_64_RELATIVE:
1601 *loc += f->baseaddr;
1604 case R_X86_64_GOT32:
1605 case R_X86_64_GOTPCREL:
1608 if (!isym->gotent.reloc_done)
1610 isym->gotent.reloc_done = 1;
1611 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1613 /* XXX are these really correct? */
1614 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1615 *(unsigned int *) loc += v + isym->gotent.offset;
1617 *loc += isym->gotent.offset;
1622 # warning "no idea how to handle relocations on your arch"
1626 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1627 ret = obj_reloc_unhandled;
1630 #if defined(USE_PLT_ENTRIES)
1634 /* find the plt entry and initialize it if necessary */
1636 #if defined(USE_PLT_LIST)
1637 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1644 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1646 /* generate some machine code */
1648 #if defined(__arm__)
1649 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1650 ip[1] = v; /* sym@ */
1652 #if defined(__powerpc__)
1653 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1654 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1655 ip[2] = 0x7d6903a6; /* mtctr r11 */
1656 ip[3] = 0x4e800420; /* bctr */
1658 #if defined(__v850e__)
1659 /* We have to trash a register, so we assume that any control
1660 transfer more than 21-bits away must be a function call
1661 (so we can use a call-clobbered register). */
1662 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1663 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1668 /* relative distance to target */
1670 /* if the target is too far away.... */
1671 #if defined(__arm__) || defined(__powerpc__)
1672 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1673 #elif defined(__v850e__)
1674 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1676 /* go via the plt */
1677 v = plt + pe->offset - dot;
1679 #if defined(__v850e__)
1684 ret = obj_reloc_dangerous;
1686 /* merge the offset into the instruction. */
1687 #if defined(__arm__)
1688 /* Convert to words. */
1691 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1693 #if defined(__powerpc__)
1694 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1696 #if defined(__v850e__)
1697 /* We write two shorts instead of a long because even 32-bit insns
1698 only need half-word alignment, but the 32-bit data write needs
1699 to be long-word aligned. */
1700 ((unsigned short *)loc)[0] =
1701 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1702 | ((v >> 16) & 0x3f); /* offs high part */
1703 ((unsigned short *)loc)[1] =
1704 (v & 0xffff); /* offs low part */
1707 #endif /* USE_PLT_ENTRIES */
1709 #if defined(USE_GOT_ENTRIES)
1712 /* needs an entry in the .got: set it, once */
1713 if (!isym->gotent.inited) {
1714 isym->gotent.inited = 1;
1715 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1717 /* make the reloc with_respect_to_.got */
1719 *loc += isym->gotent.offset + rel->r_addend;
1720 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1721 *loc += isym->gotent.offset;
1725 #endif /* USE_GOT_ENTRIES */
1732 #if defined(USE_LIST)
1734 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1735 int offset, int size)
1737 struct arch_list_entry *pe;
1739 for (pe = *list; pe != NULL; pe = pe->next) {
1740 if (pe->addend == rel->r_addend) {
1746 pe = xmalloc(sizeof(struct arch_list_entry));
1748 pe->addend = rel->r_addend;
1749 pe->offset = offset;
1759 #if defined(USE_SINGLE)
1761 static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
1762 int offset, int size)
1764 if (single->allocated == 0) {
1765 single->allocated = 1;
1766 single->offset = offset;
1775 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1777 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
1778 int offset, int size)
1780 struct obj_section *myrelsec = obj_find_section(f, name);
1787 obj_extend_section(myrelsec, offset);
1789 myrelsec = obj_create_alloced_section(f, name,
1798 static void arch_create_got(struct obj_file *f)
1800 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1801 struct arch_file *ifile = (struct arch_file *) f;
1803 #if defined(USE_GOT_ENTRIES)
1804 int got_offset = 0, got_needed = 0, got_allocate;
1806 #if defined(USE_PLT_ENTRIES)
1807 int plt_offset = 0, plt_needed = 0, plt_allocate;
1809 struct obj_section *relsec, *symsec, *strsec;
1810 ElfW(RelM) *rel, *relend;
1811 ElfW(Sym) *symtab, *extsym;
1812 const char *strtab, *name;
1813 struct arch_symbol *intsym;
1815 for (i = 0; i < f->header.e_shnum; ++i) {
1816 relsec = f->sections[i];
1817 if (relsec->header.sh_type != SHT_RELM)
1820 symsec = f->sections[relsec->header.sh_link];
1821 strsec = f->sections[symsec->header.sh_link];
1823 rel = (ElfW(RelM) *) relsec->contents;
1824 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1825 symtab = (ElfW(Sym) *) symsec->contents;
1826 strtab = (const char *) strsec->contents;
1828 for (; rel < relend; ++rel) {
1829 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1831 #if defined(USE_GOT_ENTRIES)
1834 #if defined(USE_PLT_ENTRIES)
1838 switch (ELF_R_TYPE(rel->r_info)) {
1839 #if defined(__arm__)
1854 #elif defined(__i386__)
1864 #elif defined(__powerpc__)
1869 #elif defined(__mc68000__)
1880 #elif defined(__sh__)
1890 #elif defined(__v850e__)
1891 case R_V850_22_PCREL:
1900 if (extsym->st_name != 0) {
1901 name = strtab + extsym->st_name;
1903 name = f->sections[extsym->st_shndx]->name;
1905 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1906 #if defined(USE_GOT_ENTRIES)
1908 got_offset += arch_single_init(
1909 /*rel,*/ &intsym->gotent,
1910 got_offset, GOT_ENTRY_SIZE);
1915 #if defined(USE_PLT_ENTRIES)
1917 #if defined(USE_PLT_LIST)
1918 plt_offset += arch_list_add(
1919 rel, &intsym->pltent,
1920 plt_offset, PLT_ENTRY_SIZE);
1922 plt_offset += arch_single_init(
1923 /*rel,*/ &intsym->pltent,
1924 plt_offset, PLT_ENTRY_SIZE);
1932 #if defined(USE_GOT_ENTRIES)
1934 ifile->got = arch_xsect_init(f, ".got", got_offset,
1939 #if defined(USE_PLT_ENTRIES)
1941 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1946 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1949 /*======================================================================*/
1951 /* Standard ELF hash function. */
1952 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1954 unsigned long h = 0;
1961 g = (h & 0xf0000000);
1971 static unsigned long obj_elf_hash(const char *name)
1973 return obj_elf_hash_n(name, strlen(name));
1976 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1977 /* String comparison for non-co-versioned kernel and module. */
1979 static int ncv_strcmp(const char *a, const char *b)
1981 size_t alen = strlen(a), blen = strlen(b);
1983 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1984 return strncmp(a, b, alen);
1985 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1986 return strncmp(a, b, blen);
1988 return strcmp(a, b);
1991 /* String hashing for non-co-versioned kernel and module. Here
1992 we are simply forced to drop the crc from the hash. */
1994 static unsigned long ncv_symbol_hash(const char *str)
1996 size_t len = strlen(str);
1997 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1999 return obj_elf_hash_n(str, len);
2003 obj_set_symbol_compare(struct obj_file *f,
2004 int (*cmp) (const char *, const char *),
2005 unsigned long (*hash) (const char *))
2008 f->symbol_cmp = cmp;
2010 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
2013 f->symbol_hash = hash;
2015 memcpy(tmptab, f->symtab, sizeof(tmptab));
2016 memset(f->symtab, 0, sizeof(f->symtab));
2018 for (i = 0; i < HASH_BUCKETS; ++i)
2019 for (sym = tmptab[i]; sym; sym = next) {
2020 unsigned long h = hash(sym->name) % HASH_BUCKETS;
2022 sym->next = f->symtab[h];
2028 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2030 static struct obj_symbol *
2031 obj_add_symbol(struct obj_file *f, const char *name,
2032 unsigned long symidx, int info,
2033 int secidx, ElfW(Addr) value,
2036 struct obj_symbol *sym;
2037 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2038 int n_type = ELF_ST_TYPE(info);
2039 int n_binding = ELF_ST_BIND(info);
2041 for (sym = f->symtab[hash]; sym; sym = sym->next) {
2042 if (f->symbol_cmp(sym->name, name) == 0) {
2043 int o_secidx = sym->secidx;
2044 int o_info = sym->info;
2045 int o_type = ELF_ST_TYPE(o_info);
2046 int o_binding = ELF_ST_BIND(o_info);
2048 /* A redefinition! Is it legal? */
2050 if (secidx == SHN_UNDEF)
2052 else if (o_secidx == SHN_UNDEF)
2054 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2055 /* Cope with local and global symbols of the same name
2056 in the same object file, as might have been created
2057 by ld -r. The only reason locals are now seen at this
2058 level at all is so that we can do semi-sensible things
2061 struct obj_symbol *nsym, **p;
2063 nsym = arch_new_symbol();
2064 nsym->next = sym->next;
2067 /* Excise the old (local) symbol from the hash chain. */
2068 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2072 } else if (n_binding == STB_LOCAL) {
2073 /* Another symbol of the same name has already been defined.
2074 Just add this to the local table. */
2075 sym = arch_new_symbol();
2078 f->local_symtab[symidx] = sym;
2080 } else if (n_binding == STB_WEAK)
2082 else if (o_binding == STB_WEAK)
2084 /* Don't unify COMMON symbols with object types the programmer
2086 else if (secidx == SHN_COMMON
2087 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2089 else if (o_secidx == SHN_COMMON
2090 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2093 /* Don't report an error if the symbol is coming from
2094 the kernel or some external module. */
2095 if (secidx <= SHN_HIRESERVE)
2096 bb_error_msg("%s multiply defined", name);
2102 /* Completely new symbol. */
2103 sym = arch_new_symbol();
2104 sym->next = f->symtab[hash];
2105 f->symtab[hash] = sym;
2107 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
2108 if (symidx >= f->local_symtab_size)
2109 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2110 name, (long) symidx, (long) f->local_symtab_size);
2112 f->local_symtab[symidx] = sym;
2119 sym->secidx = secidx;
2125 static struct obj_symbol *
2126 obj_find_symbol(struct obj_file *f, const char *name)
2128 struct obj_symbol *sym;
2129 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2131 for (sym = f->symtab[hash]; sym; sym = sym->next)
2132 if (f->symbol_cmp(sym->name, name) == 0)
2138 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2141 if (sym->secidx >= SHN_LORESERVE)
2144 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2146 /* As a special case, a NULL sym has value zero. */
2151 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2153 int i, n = f->header.e_shnum;
2155 for (i = 0; i < n; ++i)
2156 if (strcmp(f->sections[i]->name, name) == 0)
2157 return f->sections[i];
2162 static int obj_load_order_prio(struct obj_section *a)
2164 unsigned long af, ac;
2166 af = a->header.sh_flags;
2169 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2170 strcmp(a->name + 5, ".init"))
2174 if (!(af & SHF_WRITE))
2176 if (af & SHF_EXECINSTR)
2178 if (a->header.sh_type != SHT_NOBITS)
2185 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2187 struct obj_section **p;
2188 int prio = obj_load_order_prio(sec);
2189 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2190 if (obj_load_order_prio(*p) < prio)
2192 sec->load_next = *p;
2196 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2198 unsigned long align,
2201 int newidx = f->header.e_shnum++;
2202 struct obj_section *sec;
2204 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2205 f->sections[newidx] = sec = arch_new_section();
2207 sec->header.sh_type = SHT_PROGBITS;
2208 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2209 sec->header.sh_size = size;
2210 sec->header.sh_addralign = align;
2214 sec->contents = xmalloc(size);
2216 obj_insert_section_load_order(f, sec);
2221 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2223 unsigned long align,
2226 int newidx = f->header.e_shnum++;
2227 struct obj_section *sec;
2229 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2230 f->sections[newidx] = sec = arch_new_section();
2232 sec->header.sh_type = SHT_PROGBITS;
2233 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2234 sec->header.sh_size = size;
2235 sec->header.sh_addralign = align;
2239 sec->contents = xmalloc(size);
2241 sec->load_next = f->load_order;
2242 f->load_order = sec;
2243 if (f->load_order_search_start == &f->load_order)
2244 f->load_order_search_start = &sec->load_next;
2249 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2251 unsigned long oldsize = sec->header.sh_size;
2253 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2255 return sec->contents + oldsize;
2259 /* Conditionally add the symbols from the given symbol set to the
2263 add_symbols_from( struct obj_file *f,
2264 int idx, struct new_module_symbol *syms, size_t nsyms)
2266 struct new_module_symbol *s;
2269 #ifdef SYMBOL_PREFIX
2271 size_t name_alloced_size = 0;
2273 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2276 gpl = obj_gpl_license(f, NULL) == 0;
2278 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2279 /* Only add symbols that are already marked external.
2280 If we override locals we may cause problems for
2281 argument initialization. We will also create a false
2282 dependency on the module. */
2283 struct obj_symbol *sym;
2286 /* GPL licensed modules can use symbols exported with
2287 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2288 * exported names. Non-GPL modules never see any GPLONLY_
2289 * symbols so they cannot fudge it by adding the prefix on
2292 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2293 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2300 name = (char *)s->name;
2302 #ifdef SYMBOL_PREFIX
2303 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2304 kernel exports `C names', but module object files
2305 reference `linker names'). */
2306 size_t extra = sizeof SYMBOL_PREFIX;
2307 size_t name_size = strlen(name) + extra;
2308 if (name_size > name_alloced_size) {
2309 name_alloced_size = name_size * 2;
2310 name_buf = alloca(name_alloced_size);
2312 strcpy(name_buf, SYMBOL_PREFIX);
2313 strcpy(name_buf + extra - 1, name);
2315 #endif /* SYMBOL_PREFIX */
2317 sym = obj_find_symbol(f, name);
2318 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2319 #ifdef SYMBOL_PREFIX
2320 /* Put NAME_BUF into more permanent storage. */
2321 name = xmalloc(name_size);
2322 strcpy(name, name_buf);
2324 sym = obj_add_symbol(f, name, -1,
2325 ELF_ST_INFO(STB_GLOBAL,
2328 /* Did our symbol just get installed? If so, mark the
2329 module as "used". */
2330 if (sym->secidx == idx)
2338 static void add_kernel_symbols(struct obj_file *f)
2340 struct external_module *m;
2343 /* Add module symbols first. */
2345 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
2347 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
2354 n_ext_modules_used = nused;
2356 /* And finally the symbols from the kernel proper. */
2359 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2362 static char *get_modinfo_value(struct obj_file *f, const char *key)
2364 struct obj_section *sec;
2365 char *p, *v, *n, *ep;
2366 size_t klen = strlen(key);
2368 sec = obj_find_section(f, ".modinfo");
2372 ep = p + sec->header.sh_size;
2375 n = strchr(p, '\0');
2377 if (p + klen == v && strncmp(p, key, klen) == 0)
2380 if (p + klen == n && strcmp(p, key) == 0)
2390 /*======================================================================*/
2391 /* Functions relating to module loading after 2.1.18. */
2394 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2397 char *p, *q, *key, *sym_name;
2398 struct obj_symbol *sym;
2399 char *contents, *loc;
2409 key = alloca(q - p + 6);
2410 memcpy(key, "parm_", 5);
2411 memcpy(key + 5, p, q - p);
2414 p = get_modinfo_value(f, key);
2417 bb_error_msg_and_die("invalid parameter %s", key);
2420 #ifdef SYMBOL_PREFIX
2421 sym_name = alloca(strlen(key) + sizeof SYMBOL_PREFIX);
2422 strcpy(sym_name, SYMBOL_PREFIX);
2423 strcat(sym_name, key);
2427 sym = obj_find_symbol(f, sym_name);
2429 /* Also check that the parameter was not resolved from the kernel. */
2430 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2431 bb_error_msg_and_die("symbol for parameter %s not found", key);
2435 min = strtoul(p, &p, 10);
2437 max = strtoul(p + 1, &p, 10);
2443 contents = f->sections[sym->secidx]->contents;
2444 loc = contents + sym->value;
2448 if ((*p == 's') || (*p == 'c')) {
2451 /* Do C quoting if we begin with a ", else slurp the lot. */
2455 str = alloca(strlen(q));
2456 for (r = str, q++; *q != '"'; ++q, ++r) {
2458 bb_error_msg_and_die("improperly terminated string argument for %s",
2494 if (q[1] >= '0' && q[1] <= '7') {
2495 c = (c * 8) + *++q - '0';
2496 if (q[1] >= '0' && q[1] <= '7')
2497 c = (c * 8) + *++q - '0';
2515 /* In this case, the string is not quoted. We will break
2516 it using the coma (like for ints). If the user wants to
2517 include comas in a string, he just has to quote it */
2519 /* Search the next coma */
2523 if (r != (char *) NULL) {
2524 /* Recopy the current field */
2525 str = alloca(r - q + 1);
2526 memcpy(str, q, r - q);
2528 /* I don't know if it is useful, as the previous case
2529 doesn't nul terminate the string ??? */
2532 /* Keep next fields */
2543 obj_string_patch(f, sym->secidx, loc - contents, str);
2544 loc += tgt_sizeof_char_p;
2546 /* Array of chars (in fact, matrix!) */
2547 unsigned long charssize; /* size of each member */
2549 /* Get the size of each member */
2550 /* Probably we should do that outside the loop ? */
2551 if (!isdigit(*(p + 1))) {
2552 bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
2553 " the maximum size", key);
2555 charssize = strtoul(p + 1, (char **) NULL, 10);
2558 if (strlen(str) >= charssize) {
2559 bb_error_msg_and_die("string too long for %s (max %ld)", key,
2563 /* Copy to location */
2564 strcpy((char *) loc, str);
2568 long v = strtoul(q, &q, 0);
2575 loc += tgt_sizeof_short;
2579 loc += tgt_sizeof_int;
2583 loc += tgt_sizeof_long;
2587 bb_error_msg_and_die("unknown parameter type '%c' for %s", *p, key);
2600 goto retry_end_of_value;
2604 bb_error_msg_and_die("too many values for %s (max %d)", key, max);
2610 bb_error_msg_and_die("invalid argument syntax for %s", key);
2615 bb_error_msg_and_die("too few values for %s (min %d)", key, min);
2623 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2624 static int new_is_module_checksummed(struct obj_file *f)
2626 const char *p = get_modinfo_value(f, "using_checksums");
2632 /* Get the module's kernel version in the canonical integer form. */
2635 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2640 p = get_modinfo_value(f, "kernel_version");
2643 safe_strncpy(str, p, STRVERSIONLEN);
2645 a = strtoul(p, &p, 10);
2648 b = strtoul(p + 1, &p, 10);
2651 c = strtoul(p + 1, &q, 10);
2655 return a << 16 | b << 8 | c;
2658 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2661 /* Fetch the loaded modules, and all currently exported symbols. */
2663 static void new_get_kernel_symbols(void)
2665 char *module_names, *mn;
2666 struct external_module *modules, *m;
2667 struct new_module_symbol *syms, *s;
2668 size_t ret, bufsize, nmod, nsyms, i, j;
2670 /* Collect the loaded modules. */
2673 module_names = xmalloc(bufsize);
2676 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2677 if (errno == ENOSPC && bufsize < ret) {
2679 module_names = xrealloc(module_names, bufsize);
2680 goto retry_modules_load;
2682 bb_perror_msg_and_die("QM_MODULES");
2685 n_ext_modules = nmod = ret;
2687 /* Collect the modules' symbols. */
2690 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2691 memset(modules, 0, nmod * sizeof(*modules));
2692 for (i = 0, mn = module_names, m = modules;
2693 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2694 struct new_module_info info;
2696 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2697 if (errno == ENOENT) {
2698 /* The module was removed out from underneath us. */
2701 bb_perror_msg_and_die("query_module: QM_INFO: %s", mn);
2705 syms = xmalloc(bufsize);
2707 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2711 syms = xrealloc(syms, bufsize);
2712 goto retry_mod_sym_load;
2714 /* The module was removed out from underneath us. */
2717 bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn);
2723 m->addr = info.addr;
2727 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2728 s->name += (unsigned long) syms;
2733 /* Collect the kernel's symbols. */
2735 syms = xmalloc(bufsize = 16 * 1024);
2736 retry_kern_sym_load:
2737 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2738 if (errno == ENOSPC && bufsize < ret) {
2739 syms = xrealloc(syms, bufsize = ret);
2740 goto retry_kern_sym_load;
2742 bb_perror_msg_and_die("kernel: QM_SYMBOLS");
2744 nksyms = nsyms = ret;
2747 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2748 s->name += (unsigned long) syms;
2753 /* Return the kernel symbol checksum version, or zero if not used. */
2755 static int new_is_kernel_checksummed(void)
2757 struct new_module_symbol *s;
2760 /* Using_Versions is not the first symbol, but it should be in there. */
2762 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2763 if (strcmp((char *) s->name, "Using_Versions") == 0)
2770 static void new_create_this_module(struct obj_file *f, const char *m_name)
2772 struct obj_section *sec;
2774 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2775 sizeof(struct new_module));
2776 memset(sec->contents, 0, sizeof(struct new_module));
2778 obj_add_symbol(f, SPFX "__this_module", -1,
2779 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2780 sizeof(struct new_module));
2782 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2786 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2787 /* add an entry to the __ksymtab section, creating it if necessary */
2788 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2790 struct obj_section *sec;
2793 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2794 * If __ksymtab is defined but not marked alloc, x out the first character
2795 * (no obj_delete routine) and create a new __ksymtab with the correct
2798 sec = obj_find_section(f, "__ksymtab");
2799 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2800 *((char *)(sec->name)) = 'x'; /* override const */
2804 sec = obj_create_alloced_section(f, "__ksymtab",
2805 tgt_sizeof_void_p, 0);
2808 sec->header.sh_flags |= SHF_ALLOC;
2809 /* Empty section might be byte-aligned */
2810 sec->header.sh_addralign = tgt_sizeof_void_p;
2811 ofs = sec->header.sh_size;
2812 obj_symbol_patch(f, sec->idx, ofs, sym);
2813 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2814 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2816 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2818 static int new_create_module_ksymtab(struct obj_file *f)
2820 struct obj_section *sec;
2823 /* We must always add the module references. */
2825 if (n_ext_modules_used) {
2826 struct new_module_ref *dep;
2827 struct obj_symbol *tm;
2829 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2830 (sizeof(struct new_module_ref)
2831 * n_ext_modules_used));
2835 tm = obj_find_symbol(f, SPFX "__this_module");
2836 dep = (struct new_module_ref *) sec->contents;
2837 for (i = 0; i < n_ext_modules; ++i)
2838 if (ext_modules[i].used) {
2839 dep->dep = ext_modules[i].addr;
2840 obj_symbol_patch(f, sec->idx,
2841 (char *) &dep->ref - sec->contents, tm);
2847 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2851 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2853 /* We don't want to export symbols residing in sections that
2854 aren't loaded. There are a number of these created so that
2855 we make sure certain module options don't appear twice. */
2857 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2859 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2861 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2862 struct obj_symbol *sym;
2863 for (sym = f->symtab[i]; sym; sym = sym->next)
2864 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2865 && sym->secidx <= SHN_HIRESERVE
2866 && (sym->secidx >= SHN_LORESERVE
2867 || loaded[sym->secidx])) {
2868 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2870 obj_symbol_patch(f, sec->idx, ofs, sym);
2871 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2878 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2886 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2888 struct new_module *module;
2889 struct obj_section *sec;
2894 sec = obj_find_section(f, ".this");
2895 if (!sec || !sec->contents) {
2896 bb_perror_msg_and_die("corrupt module %s?",m_name);
2898 module = (struct new_module *) sec->contents;
2899 m_addr = sec->header.sh_addr;
2901 module->size_of_struct = sizeof(*module);
2902 module->size = m_size;
2903 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2905 sec = obj_find_section(f, "__ksymtab");
2906 if (sec && sec->header.sh_size) {
2907 module->syms = sec->header.sh_addr;
2908 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2911 if (n_ext_modules_used) {
2912 sec = obj_find_section(f, ".kmodtab");
2913 module->deps = sec->header.sh_addr;
2914 module->ndeps = n_ext_modules_used;
2918 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2920 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2922 sec = obj_find_section(f, "__ex_table");
2924 module->ex_table_start = sec->header.sh_addr;
2925 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2928 sec = obj_find_section(f, ".text.init");
2930 module->runsize = sec->header.sh_addr - m_addr;
2932 sec = obj_find_section(f, ".data.init");
2934 if (!module->runsize ||
2935 module->runsize > sec->header.sh_addr - m_addr)
2936 module->runsize = sec->header.sh_addr - m_addr;
2938 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2939 if (sec && sec->header.sh_size) {
2940 module->archdata_start = (void*)sec->header.sh_addr;
2941 module->archdata_end = module->archdata_start + sec->header.sh_size;
2943 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2944 if (sec && sec->header.sh_size) {
2945 module->kallsyms_start = (void*)sec->header.sh_addr;
2946 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2949 /* Whew! All of the initialization is complete. Collect the final
2950 module image and give it to the kernel. */
2952 image = xmalloc(m_size);
2953 obj_create_image(f, image);
2955 ret = init_module(m_name, (struct new_module *) image);
2957 bb_perror_msg("init_module: %s", m_name);
2965 /*======================================================================*/
2968 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2971 struct obj_string_patch *p;
2972 struct obj_section *strsec;
2973 size_t len = strlen(string) + 1;
2976 p = xmalloc(sizeof(*p));
2977 p->next = f->string_patches;
2978 p->reloc_secidx = secidx;
2979 p->reloc_offset = offset;
2980 f->string_patches = p;
2982 strsec = obj_find_section(f, ".kstrtab");
2983 if (strsec == NULL) {
2984 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2985 p->string_offset = 0;
2986 loc = strsec->contents;
2988 p->string_offset = strsec->header.sh_size;
2989 loc = obj_extend_section(strsec, len);
2991 memcpy(loc, string, len);
2995 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2996 struct obj_symbol *sym)
2998 struct obj_symbol_patch *p;
3000 p = xmalloc(sizeof(*p));
3001 p->next = f->symbol_patches;
3002 p->reloc_secidx = secidx;
3003 p->reloc_offset = offset;
3005 f->symbol_patches = p;
3008 static void obj_check_undefineds(struct obj_file *f)
3012 for (i = 0; i < HASH_BUCKETS; ++i) {
3013 struct obj_symbol *sym;
3014 for (sym = f->symtab[i]; sym; sym = sym->next)
3015 if (sym->secidx == SHN_UNDEF) {
3016 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3017 sym->secidx = SHN_ABS;
3021 bb_error_msg_and_die("unresolved symbol %s", sym->name);
3027 static void obj_allocate_commons(struct obj_file *f)
3029 struct common_entry {
3030 struct common_entry *next;
3031 struct obj_symbol *sym;
3032 } *common_head = NULL;
3036 for (i = 0; i < HASH_BUCKETS; ++i) {
3037 struct obj_symbol *sym;
3038 for (sym = f->symtab[i]; sym; sym = sym->next)
3039 if (sym->secidx == SHN_COMMON) {
3040 /* Collect all COMMON symbols and sort them by size so as to
3041 minimize space wasted by alignment requirements. */
3043 struct common_entry **p, *n;
3044 for (p = &common_head; *p; p = &(*p)->next)
3045 if (sym->size <= (*p)->sym->size)
3048 n = alloca(sizeof(*n));
3056 for (i = 1; i < f->local_symtab_size; ++i) {
3057 struct obj_symbol *sym = f->local_symtab[i];
3058 if (sym && sym->secidx == SHN_COMMON) {
3059 struct common_entry **p, *n;
3060 for (p = &common_head; *p; p = &(*p)->next)
3061 if (sym == (*p)->sym)
3063 else if (sym->size < (*p)->sym->size) {
3064 n = alloca(sizeof(*n));
3074 /* Find the bss section. */
3075 for (i = 0; i < f->header.e_shnum; ++i)
3076 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3079 /* If for some reason there hadn't been one, create one. */
3080 if (i == f->header.e_shnum) {
3081 struct obj_section *sec;
3083 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3084 f->sections[i] = sec = arch_new_section();
3085 f->header.e_shnum = i + 1;
3087 sec->header.sh_type = SHT_PROGBITS;
3088 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3093 /* Allocate the COMMONS. */
3095 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3096 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3097 struct common_entry *c;
3099 for (c = common_head; c; c = c->next) {
3100 ElfW(Addr) align = c->sym->value;
3102 if (align > max_align)
3104 if (bss_size & (align - 1))
3105 bss_size = (bss_size | (align - 1)) + 1;
3108 c->sym->value = bss_size;
3110 bss_size += c->sym->size;
3113 f->sections[i]->header.sh_size = bss_size;
3114 f->sections[i]->header.sh_addralign = max_align;
3118 /* For the sake of patch relocation and parameter initialization,
3119 allocate zeroed data for NOBITS sections now. Note that after
3120 this we cannot assume NOBITS are really empty. */
3121 for (i = 0; i < f->header.e_shnum; ++i) {
3122 struct obj_section *s = f->sections[i];
3123 if (s->header.sh_type == SHT_NOBITS) {
3124 if (s->header.sh_size != 0)
3125 s->contents = memset(xmalloc(s->header.sh_size),
3126 0, s->header.sh_size);
3130 s->header.sh_type = SHT_PROGBITS;
3135 static unsigned long obj_load_size(struct obj_file *f)
3137 unsigned long dot = 0;
3138 struct obj_section *sec;
3140 /* Finalize the positions of the sections relative to one another. */
3142 for (sec = f->load_order; sec; sec = sec->load_next) {
3145 align = sec->header.sh_addralign;
3146 if (align && (dot & (align - 1)))
3147 dot = (dot | (align - 1)) + 1;
3149 sec->header.sh_addr = dot;
3150 dot += sec->header.sh_size;
3156 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3158 int i, n = f->header.e_shnum;
3161 /* Finalize the addresses of the sections. */
3164 for (i = 0; i < n; ++i)
3165 f->sections[i]->header.sh_addr += base;
3167 /* And iterate over all of the relocations. */
3169 for (i = 0; i < n; ++i) {
3170 struct obj_section *relsec, *symsec, *targsec, *strsec;
3171 ElfW(RelM) * rel, *relend;
3175 relsec = f->sections[i];
3176 if (relsec->header.sh_type != SHT_RELM)
3179 symsec = f->sections[relsec->header.sh_link];
3180 targsec = f->sections[relsec->header.sh_info];
3181 strsec = f->sections[symsec->header.sh_link];
3183 rel = (ElfW(RelM) *) relsec->contents;
3184 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3185 symtab = (ElfW(Sym) *) symsec->contents;
3186 strtab = (const char *) strsec->contents;
3188 for (; rel < relend; ++rel) {
3189 ElfW(Addr) value = 0;
3190 struct obj_symbol *intsym = NULL;
3191 unsigned long symndx;
3192 ElfW(Sym) * extsym = 0;
3195 /* Attempt to find a value to use for this relocation. */
3197 symndx = ELF_R_SYM(rel->r_info);
3199 /* Note we've already checked for undefined symbols. */
3201 extsym = &symtab[symndx];
3202 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3203 /* Local symbols we look up in the local table to be sure
3204 we get the one that is really intended. */
3205 intsym = f->local_symtab[symndx];
3207 /* Others we look up in the hash table. */
3209 if (extsym->st_name)
3210 name = strtab + extsym->st_name;
3212 name = f->sections[extsym->st_shndx]->name;
3213 intsym = obj_find_symbol(f, name);
3216 value = obj_symbol_final_value(f, intsym);
3217 intsym->referenced = 1;
3219 #if SHT_RELM == SHT_RELA
3220 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3221 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3222 if (!extsym || !extsym->st_name ||
3223 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3225 value += rel->r_addend;
3229 switch (arch_apply_relocation
3230 (f, targsec, /*symsec,*/ intsym, rel, value)
3235 case obj_reloc_overflow:
3236 errmsg = "Relocation overflow";
3238 case obj_reloc_dangerous:
3239 errmsg = "Dangerous relocation";
3241 case obj_reloc_unhandled:
3242 errmsg = "Unhandled relocation";
3245 bb_error_msg("%s of type %ld for %s", errmsg,
3246 (long) ELF_R_TYPE(rel->r_info),
3247 strtab + extsym->st_name);
3249 bb_error_msg("%s of type %ld", errmsg,
3250 (long) ELF_R_TYPE(rel->r_info));
3258 /* Finally, take care of the patches. */
3260 if (f->string_patches) {
3261 struct obj_string_patch *p;
3262 struct obj_section *strsec;
3263 ElfW(Addr) strsec_base;
3264 strsec = obj_find_section(f, ".kstrtab");
3265 strsec_base = strsec->header.sh_addr;
3267 for (p = f->string_patches; p; p = p->next) {
3268 struct obj_section *targsec = f->sections[p->reloc_secidx];
3269 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3270 = strsec_base + p->string_offset;
3274 if (f->symbol_patches) {
3275 struct obj_symbol_patch *p;
3277 for (p = f->symbol_patches; p; p = p->next) {
3278 struct obj_section *targsec = f->sections[p->reloc_secidx];
3279 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3280 = obj_symbol_final_value(f, p->sym);
3287 static int obj_create_image(struct obj_file *f, char *image)
3289 struct obj_section *sec;
3290 ElfW(Addr) base = f->baseaddr;
3292 for (sec = f->load_order; sec; sec = sec->load_next) {
3295 if (sec->contents == 0 || sec->header.sh_size == 0)
3298 secimg = image + (sec->header.sh_addr - base);
3300 /* Note that we allocated data for NOBITS sections earlier. */
3301 memcpy(secimg, sec->contents, sec->header.sh_size);
3307 /*======================================================================*/
3309 static struct obj_file *obj_load(FILE * fp, int loadprogbits ATTRIBUTE_UNUSED)
3312 ElfW(Shdr) * section_headers;
3316 /* Read the file header. */
3318 f = arch_new_file();
3319 f->symbol_cmp = strcmp;
3320 f->symbol_hash = obj_elf_hash;
3321 f->load_order_search_start = &f->load_order;
3323 fseek(fp, 0, SEEK_SET);
3324 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3325 bb_perror_msg_and_die("error reading ELF header");
3328 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3329 || f->header.e_ident[EI_MAG1] != ELFMAG1
3330 || f->header.e_ident[EI_MAG2] != ELFMAG2
3331 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3332 bb_error_msg_and_die("not an ELF file");
3334 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3335 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3336 ? ELFDATA2MSB : ELFDATA2LSB)
3337 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3338 || !MATCH_MACHINE(f->header.e_machine)) {
3339 bb_error_msg_and_die("ELF file not for this architecture");
3341 if (f->header.e_type != ET_REL) {
3342 bb_error_msg_and_die("ELF file not a relocatable object");
3345 /* Read the section headers. */
3347 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3348 bb_error_msg_and_die("section header size mismatch: %lu != %lu",
3349 (unsigned long) f->header.e_shentsize,
3350 (unsigned long) sizeof(ElfW(Shdr)));
3353 shnum = f->header.e_shnum;
3354 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3355 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3357 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3358 fseek(fp, f->header.e_shoff, SEEK_SET);
3359 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3360 bb_perror_msg_and_die("error reading ELF section headers");
3363 /* Read the section data. */
3365 for (i = 0; i < shnum; ++i) {
3366 struct obj_section *sec;
3368 f->sections[i] = sec = arch_new_section();
3370 sec->header = section_headers[i];
3373 if (sec->header.sh_size) {
3374 switch (sec->header.sh_type) {
3383 if (!loadprogbits) {
3384 sec->contents = NULL;
3391 if (sec->header.sh_size > 0) {
3392 sec->contents = xmalloc(sec->header.sh_size);
3393 fseek(fp, sec->header.sh_offset, SEEK_SET);
3394 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3395 bb_perror_msg_and_die("error reading ELF section data");
3398 sec->contents = NULL;
3402 #if SHT_RELM == SHT_REL
3404 bb_error_msg_and_die("RELA relocations not supported on this architecture");
3407 bb_error_msg_and_die("REL relocations not supported on this architecture");
3410 if (sec->header.sh_type >= SHT_LOPROC) {
3411 /* Assume processor specific section types are debug
3412 info and can safely be ignored. If this is ever not
3413 the case (Hello MIPS?), don't put ifdefs here but
3414 create an arch_load_proc_section(). */
3418 bb_error_msg_and_die("can't handle sections of type %ld",
3419 (long) sec->header.sh_type);
3424 /* Do what sort of interpretation as needed by each section. */
3426 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3428 for (i = 0; i < shnum; ++i) {
3429 struct obj_section *sec = f->sections[i];
3430 sec->name = shstrtab + sec->header.sh_name;
3433 for (i = 0; i < shnum; ++i) {
3434 struct obj_section *sec = f->sections[i];
3436 /* .modinfo should be contents only but gcc has no attribute for that.
3437 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3439 if (strcmp(sec->name, ".modinfo") == 0)
3440 sec->header.sh_flags &= ~SHF_ALLOC;
3442 if (sec->header.sh_flags & SHF_ALLOC)
3443 obj_insert_section_load_order(f, sec);
3445 switch (sec->header.sh_type) {
3448 unsigned long nsym, j;
3452 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3453 bb_error_msg_and_die("symbol size mismatch: %lu != %lu",
3454 (unsigned long) sec->header.sh_entsize,
3455 (unsigned long) sizeof(ElfW(Sym)));
3458 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3459 strtab = f->sections[sec->header.sh_link]->contents;
3460 sym = (ElfW(Sym) *) sec->contents;
3462 /* Allocate space for a table of local symbols. */
3463 j = f->local_symtab_size = sec->header.sh_info;
3464 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3466 /* Insert all symbols into the hash table. */
3467 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3468 ElfW(Addr) val = sym->st_value;
3471 name = strtab + sym->st_name;
3472 else if (sym->st_shndx < shnum)
3473 name = f->sections[sym->st_shndx]->name;
3476 #if defined(__SH5__)
3478 * For sh64 it is possible that the target of a branch
3479 * requires a mode switch (32 to 16 and back again).
3481 * This is implied by the lsb being set in the target
3482 * address for SHmedia mode and clear for SHcompact.
3484 val |= sym->st_other & 4;
3486 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3493 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3494 bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu",
3495 (unsigned long) sec->header.sh_entsize,
3496 (unsigned long) sizeof(ElfW(RelM)));
3499 /* XXX Relocation code from modutils-2.3.19 is not here.
3500 * Why? That's about 20 lines of code from obj/obj_load.c,
3501 * which gets done in a second pass through the sections.
3502 * This BusyBox insmod does similar work in obj_relocate(). */
3509 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3511 * load the unloaded sections directly into the memory allocated by
3512 * kernel for the module
3515 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3517 ElfW(Addr) base = f->baseaddr;
3518 struct obj_section* sec;
3520 for (sec = f->load_order; sec; sec = sec->load_next) {
3522 /* section already loaded? */
3523 if (sec->contents != NULL)
3526 if (sec->header.sh_size == 0)
3529 sec->contents = imagebase + (sec->header.sh_addr - base);
3530 fseek(fp, sec->header.sh_offset, SEEK_SET);
3531 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3532 bb_perror_msg("error reading ELF section data");
3541 static void hide_special_symbols(struct obj_file *f)
3543 static const char *const specials[] = {
3544 SPFX "cleanup_module",
3546 SPFX "kernel_version",
3550 struct obj_symbol *sym;
3551 const char *const *p;
3553 for (p = specials; *p; ++p) {
3554 sym = obj_find_symbol(f, *p);
3556 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3561 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3562 static int obj_gpl_license(struct obj_file *f, const char **license)
3564 struct obj_section *sec;
3565 /* This list must match *exactly* the list of allowable licenses in
3566 * linux/include/linux/module.h. Checking for leading "GPL" will not
3567 * work, somebody will use "GPL sucks, this is proprietary".
3569 static const char *const gpl_licenses[] = {
3572 "GPL and additional rights",
3577 sec = obj_find_section(f, ".modinfo");
3579 const char *value, *ptr, *endptr;
3580 ptr = sec->contents;
3581 endptr = ptr + sec->header.sh_size;
3582 while (ptr < endptr) {
3583 value = strchr(ptr, '=');
3584 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3588 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
3589 if (strcmp(value+1, gpl_licenses[i]) == 0)
3594 ptr = strchr(ptr, '\0');
3604 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3605 #define TAINT_PROPRIETORY_MODULE (1 << 0)
3606 #define TAINT_FORCED_MODULE (1 << 1)
3607 #define TAINT_UNSAFE_SMP (1 << 2)
3608 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3610 static void set_tainted(int fd, char *m_name,
3611 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3613 static smallint printed_info;
3618 if (fd < 0 && !kernel_has_tainted)
3619 return; /* New modutils on old kernel */
3620 printf("Warning: loading %s will taint the kernel: %s%s\n",
3621 m_name, text1, text2);
3622 if (!printed_info) {
3623 printf(" See %s for information about tainted modules\n", TAINT_URL);
3627 read(fd, buf, sizeof(buf)-1);
3628 buf[sizeof(buf)-1] = '\0';
3629 oldval = strtoul(buf, NULL, 10);
3630 sprintf(buf, "%d\n", oldval | taint);
3631 write(fd, buf, strlen(buf));
3635 /* Check if loading this module will taint the kernel. */
3636 static void check_tainted_module(struct obj_file *f, char *m_name)
3638 static const char tainted_file[] ALIGN1 = TAINT_FILENAME;
3640 int fd, kernel_has_tainted;
3643 kernel_has_tainted = 1;
3644 fd = open(tainted_file, O_RDWR);
3646 if (errno == ENOENT)
3647 kernel_has_tainted = 0;
3648 else if (errno == EACCES)
3649 kernel_has_tainted = 1;
3651 perror(tainted_file);
3652 kernel_has_tainted = 0;
3656 switch (obj_gpl_license(f, &ptr)) {
3660 set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3663 /* The module has a non-GPL license so we pretend that the
3664 * kernel always has a taint flag to get a warning even on
3665 * kernels without the proc flag.
3667 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3670 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3674 if (flag_force_load)
3675 set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3680 #else /* FEATURE_CHECK_TAINTED_MODULE */
3681 #define check_tainted_module(x, y) do { } while (0);
3682 #endif /* FEATURE_CHECK_TAINTED_MODULE */
3684 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3685 /* add module source, timestamp, kernel version and a symbol for the
3686 * start of some sections. this info is used by ksymoops to do better
3689 #if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3690 #define get_module_version(f, str) get_module_version(str)
3693 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3695 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3696 return new_get_module_version(f, str);
3697 #else /* FEATURE_INSMOD_VERSION_CHECKING */
3698 strncpy(str, "???", sizeof(str));
3700 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
3703 /* add module source, timestamp, kernel version and a symbol for the
3704 * start of some sections. this info is used by ksymoops to do better
3708 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3711 static const char symprefix[] ALIGN1 = "__insmod_";
3712 static const char section_names[][8] = {
3720 struct obj_section *sec;
3721 struct obj_symbol *sym;
3722 char *name, *absolute_filename;
3723 char str[STRVERSIONLEN];
3725 int l, lm_name, lfilename, use_ksymtab, version;
3726 struct stat statbuf;
3728 /* WARNING: was using realpath, but replaced by readlink to stop using
3729 * lots of stack. But here it seems to be able to cause problems? */
3730 absolute_filename = xmalloc_readlink(filename);
3731 if (!absolute_filename)
3732 absolute_filename = xstrdup(filename);
3734 lm_name = strlen(m_name);
3735 lfilename = strlen(absolute_filename);
3737 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3738 * are not to be exported. otherwise leave ksymtab alone for now, the
3739 * "export all symbols" compatibility code will export these symbols later.
3741 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3743 sec = obj_find_section(f, ".this");
3745 /* tag the module header with the object name, last modified
3746 * timestamp and module version. worst case for module version
3747 * is 0xffffff, decimal 16777215. putting all three fields in
3748 * one symbol is less readable but saves kernel space.
3750 l = sizeof(symprefix) + /* "__insmod_" */
3751 lm_name + /* module name */
3753 lfilename + /* object filename */
3755 2 * sizeof(statbuf.st_mtime) + /* mtime in hex */
3757 8 + /* version in dec */
3760 if (stat(absolute_filename, &statbuf) != 0)
3761 statbuf.st_mtime = 0;
3762 version = get_module_version(f, str); /* -1 if not found */
3763 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3764 symprefix, m_name, absolute_filename,
3765 (int)(2 * sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3767 sym = obj_add_symbol(f, name, -1,
3768 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3769 sec->idx, sec->header.sh_addr, 0);
3771 new_add_ksymtab(f, sym);
3773 free(absolute_filename);
3774 #ifdef _NOT_SUPPORTED_
3775 /* record where the persistent data is going, same address as previous symbol */
3778 l = sizeof(symprefix) + /* "__insmod_" */
3779 lm_name + /* module name */
3781 strlen(f->persist) + /* data store */
3784 snprintf(name, l, "%s%s_P%s",
3785 symprefix, m_name, f->persist);
3786 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3787 sec->idx, sec->header.sh_addr, 0);
3789 new_add_ksymtab(f, sym);
3791 #endif /* _NOT_SUPPORTED_ */
3792 /* tag the desired sections if size is non-zero */
3794 for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
3795 sec = obj_find_section(f, section_names[i]);
3796 if (sec && sec->header.sh_size) {
3797 l = sizeof(symprefix) + /* "__insmod_" */
3798 lm_name + /* module name */
3800 strlen(sec->name) + /* section name */
3802 8 + /* length in dec */
3805 snprintf(name, l, "%s%s_S%s_L%ld",
3806 symprefix, m_name, sec->name,
3807 (long)sec->header.sh_size);
3808 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3809 sec->idx, sec->header.sh_addr, 0);
3811 new_add_ksymtab(f, sym);
3815 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3817 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3818 static void print_load_map(struct obj_file *f)
3820 struct obj_section *sec;
3821 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3822 struct obj_symbol **all, **p;
3823 int i, nsyms, *loaded;
3824 struct obj_symbol *sym;
3826 /* Report on the section layout. */
3828 printf("Sections: Size %-*s Align\n",
3829 (int) (2 * sizeof(void *)), "Address");
3831 for (sec = f->load_order; sec; sec = sec->load_next) {
3835 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3840 printf("%-15s %08lx %0*lx 2**%d\n",
3842 (long)sec->header.sh_size,
3843 (int) (2 * sizeof(void *)),
3844 (long)sec->header.sh_addr,
3847 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3848 /* Quick reference which section indicies are loaded. */
3850 i = f->header.e_shnum;
3851 loaded = alloca(sizeof(int) * i);
3853 loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
3855 /* Collect the symbols we'll be listing. */
3857 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3858 for (sym = f->symtab[i]; sym; sym = sym->next)
3859 if (sym->secidx <= SHN_HIRESERVE
3860 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3863 all = alloca(nsyms * sizeof(struct obj_symbol *));
3865 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3866 for (sym = f->symtab[i]; sym; sym = sym->next)
3867 if (sym->secidx <= SHN_HIRESERVE
3868 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3871 /* And list them. */
3872 printf("\nSymbols:\n");
3873 for (p = all; p < all + nsyms; ++p) {
3875 unsigned long value;
3878 if (sym->secidx == SHN_ABS) {
3881 } else if (sym->secidx == SHN_UNDEF) {
3885 sec = f->sections[sym->secidx];
3887 if (sec->header.sh_type == SHT_NOBITS)
3889 else if (sec->header.sh_flags & SHF_ALLOC) {
3890 if (sec->header.sh_flags & SHF_EXECINSTR)
3892 else if (sec->header.sh_flags & SHF_WRITE)
3897 value = sym->value + sec->header.sh_addr;
3900 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3901 type = tolower(type);
3903 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3908 #else /* !FEATURE_INSMOD_LOAD_MAP */
3909 void print_load_map(struct obj_file *f);
3912 int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
3913 int insmod_main(int argc, char **argv)
3919 unsigned long m_size;
3923 char *m_name = NULL;
3924 int exit_status = EXIT_FAILURE;
3926 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3927 struct utsname uts_info;
3928 char m_strversion[STRVERSIONLEN];
3929 int m_version, m_crcs;
3931 #if ENABLE_FEATURE_CLEAN_UP
3937 struct utsname myuname;
3939 /* Parse any options */
3940 getopt32(argv, OPTION_STR, &opt_o);
3941 arg1 = argv[optind];
3942 if (option_mask32 & OPT_o) { // -o /* name the output module */
3944 m_name = xstrdup(opt_o);
3951 /* Grab the module name */
3952 tmp1 = xstrdup(arg1);
3953 tmp = basename(tmp1);
3956 if (uname(&myuname) == 0) {
3957 if (myuname.release[0] == '2') {
3958 k_version = myuname.release[2] - '0';
3962 #if ENABLE_FEATURE_2_6_MODULES
3963 if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
3964 && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
3970 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3976 #if ENABLE_FEATURE_2_6_MODULES
3978 m_fullName = xasprintf("%s.ko", tmp);
3981 m_fullName = xasprintf("%s.o", tmp);
3987 tmp1 = NULL; /* flag for free(m_name) before exit() */
3990 /* Get a filedesc for the module. Check that we have a complete path */
3991 if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
3992 || (fp = fopen(arg1, "r")) == NULL
3994 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3995 * but do not error out yet if we fail to find it... */
3996 if (k_version) { /* uname succeedd */
4000 tmdn = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, myuname.release);
4001 /* Jump through hoops in case /lib/modules/`uname -r`
4002 * is a symlink. We do not want recursive_action to
4003 * follow symlinks, but we do want to follow the
4004 * /lib/modules/`uname -r` dir, So resolve it ourselves
4005 * if it is a link... */
4006 module_dir = xmalloc_readlink(tmdn);
4008 module_dir = xstrdup(tmdn);
4009 recursive_action(module_dir, ACTION_RECURSE,
4010 check_module_name_match, NULL, m_fullName, 0);
4015 /* Check if we have found anything yet */
4016 if (!m_filename || ((fp = fopen(m_filename, "r")) == NULL)) {
4022 module_dir = xmalloc_readlink(CONFIG_DEFAULT_MODULES_DIR);
4024 module_dir = xstrdup(CONFIG_DEFAULT_MODULES_DIR);
4025 /* No module found under /lib/modules/`uname -r`, this
4026 * time cast the net a bit wider. Search /lib/modules/ */
4027 r = recursive_action(module_dir, ACTION_RECURSE,
4028 check_module_name_match, NULL, m_fullName, 0);
4030 bb_error_msg_and_die("%s: module not found", m_fullName);
4032 if (m_filename == NULL
4033 || ((fp = fopen(m_filename, "r")) == NULL)
4035 bb_error_msg_and_die("%s: module not found", m_fullName);
4039 m_filename = xstrdup(arg1);
4042 printf("Using %s\n", m_filename);
4044 #if ENABLE_FEATURE_2_6_MODULES
4045 if (k_version > 4) {
4046 argv[optind] = m_filename;
4048 return insmod_ng_main(argc - optind, argv + optind);
4052 f = obj_load(fp, LOADBITS);
4054 if (get_modinfo_value(f, "kernel_version") == NULL)
4059 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4060 /* Version correspondence? */
4062 if (uname(&uts_info) < 0)
4063 uts_info.release[0] = '\0';
4064 if (m_has_modinfo) {
4065 m_version = new_get_module_version(f, m_strversion);
4066 if (m_version == -1) {
4067 bb_error_msg_and_die("cannot find the kernel version the module was "
4072 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4073 bb_error_msg("%skernel-module version mismatch\n"
4074 "\t%s was compiled for kernel version %s\n"
4075 "\twhile this kernel is version %s",
4076 flag_force_load ? "warning: " : "",
4077 m_filename, m_strversion, uts_info.release);
4078 if (!flag_force_load)
4083 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4085 if (query_module(NULL, 0, NULL, 0, NULL))
4086 bb_error_msg_and_die("not configured to support old kernels");
4087 new_get_kernel_symbols();
4088 k_crcs = new_is_kernel_checksummed();
4090 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4093 m_crcs = new_is_module_checksummed(f);
4095 if (m_crcs != k_crcs)
4096 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4097 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4099 /* Let the module know about the kernel symbols. */
4100 add_kernel_symbols(f);
4102 /* Allocate common symbols, symbol tables, and string tables. */
4104 new_create_this_module(f, m_name);
4105 obj_check_undefineds(f);
4106 obj_allocate_commons(f);
4107 check_tainted_module(f, m_name);
4109 /* done with the module name, on to the optional var=value arguments */
4111 if (optind < argc) {
4112 new_process_module_arguments(f, argc - optind, argv + optind);
4116 hide_special_symbols(f);
4118 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4119 add_ksymoops_symbols(f, m_filename, m_name);
4120 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4122 new_create_module_ksymtab(f);
4124 /* Find current size of the module */
4125 m_size = obj_load_size(f);
4127 m_addr = create_module(m_name, m_size);
4128 if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
4130 bb_error_msg_and_die("a module named %s already exists", m_name);
4132 bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes",
4135 bb_perror_msg_and_die("create_module: %s", m_name);
4140 * the PROGBITS section was not loaded by the obj_load
4141 * now we can load them directly into the kernel memory
4143 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4144 delete_module(m_name, 0);
4149 if (!obj_relocate(f, m_addr)) {
4150 delete_module(m_name, 0);
4154 if (!new_init_module(m_name, f, m_size)) {
4155 delete_module(m_name, 0);
4159 if (flag_print_load_map)
4162 exit_status = EXIT_SUCCESS;
4165 #if ENABLE_FEATURE_CLEAN_UP
4176 #endif /* ENABLE_FEATURE_2_4_MODULES */
4178 * End of big piece of 2.4-specific code
4182 #if ENABLE_FEATURE_2_6_MODULES
4184 #include <sys/mman.h>
4186 #if defined __UCLIBC__ && !ENABLE_FEATURE_2_4_MODULES
4187 /* big time suckage. The old prototype above renders our nice fwd-decl wrong */
4188 extern int init_module(void *module, unsigned long len, const char *options);
4190 #include <asm/unistd.h>
4191 #include <sys/syscall.h>
4192 #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
4195 /* We use error numbers in a loose translation... */
4196 static const char *moderror(int err)
4200 return "invalid module format";
4202 return "unknown symbol in module";
4204 return "module has wrong symbol version";
4206 return "invalid parameters";
4208 return strerror(err);
4212 #if !ENABLE_FEATURE_2_4_MODULES
4213 int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
4214 int insmod_main(int argc ATTRIBUTE_UNUSED, char **argv)
4216 static int insmod_ng_main(int argc ATTRIBUTE_UNUSED, char **argv)
4222 char *filename, *options;
4228 /* Rest is options */
4229 options = xzalloc(1);
4232 options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
4233 /* Spaces handled by "" pairs, but no way of escaping quotes */
4234 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
4238 /* Any special reason why mmap? It isn't performace critical... */
4240 /* yes, xmalloc'ing can use *alot* of RAM. Don't forget that there are
4241 * modules out there that are half a megabyte! mmap()ing is way nicer
4242 * for small mem boxes, i guess.
4247 fd = xopen(filename, O_RDONLY);
4250 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4251 if (map == MAP_FAILED) {
4252 bb_perror_msg_and_die("cannot mmap '%s'", filename);
4255 /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
4258 xread(fd, map, len);
4261 len = MAXINT(ssize_t);
4262 map = xmalloc_open_read_close(filename, &len);
4265 if (init_module(map, len, options) != 0)
4266 bb_error_msg_and_die("cannot insert '%s': %s",
4267 filename, moderror(errno));