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 CONFIG_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 !defined(CONFIG_FEATURE_2_4_MODULES) && \
66 !defined(CONFIG_FEATURE_2_6_MODULES)
67 #define CONFIG_FEATURE_2_4_MODULES
70 #if !defined(CONFIG_FEATURE_2_4_MODULES)
71 #define insmod_ng_main insmod_main
74 #if defined(CONFIG_FEATURE_2_6_MODULES)
75 extern int insmod_ng_main( int argc, char **argv);
79 #if defined(CONFIG_FEATURE_2_4_MODULES)
82 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
90 #if defined(__alpha__)
91 #define MATCH_MACHINE(x) (x == EM_ALPHA)
92 #define SHT_RELM SHT_RELA
93 #define Elf64_RelM Elf64_Rela
94 #define ELFCLASSM ELFCLASS64
99 #define MATCH_MACHINE(x) (x == EM_ARM)
100 #define SHT_RELM SHT_REL
101 #define Elf32_RelM Elf32_Rel
102 #define ELFCLASSM ELFCLASS32
103 #define CONFIG_USE_PLT_ENTRIES
104 #define CONFIG_PLT_ENTRY_SIZE 8
105 #define CONFIG_USE_GOT_ENTRIES
106 #define CONFIG_GOT_ENTRY_SIZE 8
107 #define CONFIG_USE_SINGLE
112 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
113 #define SHT_RELM SHT_RELA
114 #define Elf32_RelM Elf32_Rela
115 #define ELFCLASSM ELFCLASS32
119 #if defined(__cris__)
120 #define MATCH_MACHINE(x) (x == EM_CRIS)
121 #define SHT_RELM SHT_RELA
122 #define Elf32_RelM Elf32_Rela
123 #define ELFCLASSM ELFCLASS32
126 #define R_CRIS_NONE 0
132 #if defined(__H8300H__) || defined(__H8300S__)
133 #define MATCH_MACHINE(x) (x == EM_H8_300)
134 #define SHT_RELM SHT_RELA
135 #define Elf32_RelM Elf32_Rela
136 #define ELFCLASSM ELFCLASS32
137 #define CONFIG_USE_SINGLE
138 #define SYMBOL_PREFIX "_"
141 /* PA-RISC / HP-PA */
142 #if defined(__hppa__)
143 #define MATCH_MACHINE(x) (x == EM_PARISC)
144 #define SHT_RELM SHT_RELA
145 #if defined(__LP64__)
146 #define Elf64_RelM Elf64_Rela
147 #define ELFCLASSM ELFCLASS64
149 #define Elf32_RelM Elf32_Rela
150 #define ELFCLASSM ELFCLASS32
155 #if defined(__i386__)
157 #define MATCH_MACHINE(x) (x == EM_386)
159 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
161 #define SHT_RELM SHT_REL
162 #define Elf32_RelM Elf32_Rel
163 #define ELFCLASSM ELFCLASS32
164 #define CONFIG_USE_GOT_ENTRIES
165 #define CONFIG_GOT_ENTRY_SIZE 4
166 #define CONFIG_USE_SINGLE
169 /* IA64, aka Itanium */
170 #if defined(__ia64__)
171 #define MATCH_MACHINE(x) (x == EM_IA_64)
172 #define SHT_RELM SHT_RELA
173 #define Elf64_RelM Elf64_Rela
174 #define ELFCLASSM ELFCLASS64
178 #if defined(__mc68000__)
179 #define MATCH_MACHINE(x) (x == EM_68K)
180 #define SHT_RELM SHT_RELA
181 #define Elf32_RelM Elf32_Rela
182 #define ELFCLASSM ELFCLASS32
183 #define CONFIG_USE_GOT_ENTRIES
184 #define CONFIG_GOT_ENTRY_SIZE 4
185 #define CONFIG_USE_SINGLE
189 #if defined(__microblaze__)
190 #define CONFIG_USE_SINGLE
191 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
192 #define SHT_RELM SHT_RELA
193 #define Elf32_RelM Elf32_Rela
194 #define ELFCLASSM ELFCLASS32
198 #if defined(__mips__)
199 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
200 #define SHT_RELM SHT_REL
201 #define Elf32_RelM Elf32_Rel
202 #define ELFCLASSM ELFCLASS32
203 /* Account for ELF spec changes. */
204 #ifndef EM_MIPS_RS3_LE
205 #ifdef EM_MIPS_RS4_BE
206 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
208 #define EM_MIPS_RS3_LE 10
210 #endif /* !EM_MIPS_RS3_LE */
211 #define ARCHDATAM "__dbe_table"
215 #if defined(__nios2__)
216 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
217 #define SHT_RELM SHT_RELA
218 #define Elf32_RelM Elf32_Rela
219 #define ELFCLASSM ELFCLASS32
223 #if defined(__powerpc64__)
224 #define MATCH_MACHINE(x) (x == EM_PPC64)
225 #define SHT_RELM SHT_RELA
226 #define Elf64_RelM Elf64_Rela
227 #define ELFCLASSM ELFCLASS64
228 #elif defined(__powerpc__)
229 #define MATCH_MACHINE(x) (x == EM_PPC)
230 #define SHT_RELM SHT_RELA
231 #define Elf32_RelM Elf32_Rela
232 #define ELFCLASSM ELFCLASS32
233 #define CONFIG_USE_PLT_ENTRIES
234 #define CONFIG_PLT_ENTRY_SIZE 16
235 #define CONFIG_USE_PLT_LIST
236 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
237 #define CONFIG_USE_LIST
238 #define ARCHDATAM "__ftr_fixup"
242 #if defined(__s390__)
243 #define MATCH_MACHINE(x) (x == EM_S390)
244 #define SHT_RELM SHT_RELA
245 #define Elf32_RelM Elf32_Rela
246 #define ELFCLASSM ELFCLASS32
247 #define CONFIG_USE_PLT_ENTRIES
248 #define CONFIG_PLT_ENTRY_SIZE 8
249 #define CONFIG_USE_GOT_ENTRIES
250 #define CONFIG_GOT_ENTRY_SIZE 8
251 #define CONFIG_USE_SINGLE
256 #define MATCH_MACHINE(x) (x == EM_SH)
257 #define SHT_RELM SHT_RELA
258 #define Elf32_RelM Elf32_Rela
259 #define ELFCLASSM ELFCLASS32
260 #define CONFIG_USE_GOT_ENTRIES
261 #define CONFIG_GOT_ENTRY_SIZE 4
262 #define CONFIG_USE_SINGLE
263 /* the SH changes have only been tested in =little endian= mode */
264 /* I'm not sure about big endian, so let's warn: */
265 #if defined(__sh__) && BB_BIG_ENDIAN
266 # error insmod.c may require changes for use on big endian SH
268 /* it may or may not work on the SH1/SH2... Error on those also */
269 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
270 #error insmod.c may require changes for SH1 or SH2 use
275 #if defined(__sparc__)
276 #define MATCH_MACHINE(x) (x == EM_SPARC)
277 #define SHT_RELM SHT_RELA
278 #define Elf32_RelM Elf32_Rela
279 #define ELFCLASSM ELFCLASS32
283 #if defined (__v850e__)
284 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
285 #define SHT_RELM SHT_RELA
286 #define Elf32_RelM Elf32_Rela
287 #define ELFCLASSM ELFCLASS32
288 #define CONFIG_USE_PLT_ENTRIES
289 #define CONFIG_PLT_ENTRY_SIZE 8
290 #define CONFIG_USE_SINGLE
291 #ifndef EM_CYGNUS_V850 /* grumble */
292 #define EM_CYGNUS_V850 0x9080
294 #define SYMBOL_PREFIX "_"
298 #if defined(__x86_64__)
299 #define MATCH_MACHINE(x) (x == EM_X86_64)
300 #define SHT_RELM SHT_RELA
301 #define CONFIG_USE_GOT_ENTRIES
302 #define CONFIG_GOT_ENTRY_SIZE 8
303 #define CONFIG_USE_SINGLE
304 #define Elf64_RelM Elf64_Rela
305 #define ELFCLASSM ELFCLASS64
309 #error Sorry, but insmod.c does not yet support this architecture...
313 //----------------------------------------------------------------------------
314 //--------modutils module.h, lines 45-242
315 //----------------------------------------------------------------------------
317 /* Definitions for the Linux module syscall interface.
318 Copyright 1996, 1997 Linux International.
320 Contributed by Richard Henderson <rth@tamu.edu>
322 This file is part of the Linux modutils.
324 This program is free software; you can redistribute it and/or modify it
325 under the terms of the GNU General Public License as published by the
326 Free Software Foundation; either version 2 of the License, or (at your
327 option) any later version.
329 This program is distributed in the hope that it will be useful, but
330 WITHOUT ANY WARRANTY; without even the implied warranty of
331 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
332 General Public License for more details.
334 You should have received a copy of the GNU General Public License
335 along with this program; if not, write to the Free Software Foundation,
336 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
339 #ifndef MODUTILS_MODULE_H
340 /* Why? static const int MODUTILS_MODULE_H = 1;*/
342 /*======================================================================*/
343 /* For sizeof() which are related to the module platform and not to the
344 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
346 #define tgt_sizeof_char sizeof(char)
347 #define tgt_sizeof_short sizeof(short)
348 #define tgt_sizeof_int sizeof(int)
349 #define tgt_sizeof_long sizeof(long)
350 #define tgt_sizeof_char_p sizeof(char *)
351 #define tgt_sizeof_void_p sizeof(void *)
352 #define tgt_long long
354 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
355 #undef tgt_sizeof_long
356 #undef tgt_sizeof_char_p
357 #undef tgt_sizeof_void_p
361 tgt_sizeof_char_p = 8,
362 tgt_sizeof_void_p = 8
364 #define tgt_long long long
367 /*======================================================================*/
368 /* The structures used in Linux 2.1. */
370 /* Note: new_module_symbol does not use tgt_long intentionally */
371 struct new_module_symbol
377 struct new_module_persist;
379 struct new_module_ref
381 unsigned tgt_long dep; /* kernel addresses */
382 unsigned tgt_long ref;
383 unsigned tgt_long next_ref;
388 unsigned tgt_long size_of_struct; /* == sizeof(module) */
389 unsigned tgt_long next;
390 unsigned tgt_long name;
391 unsigned tgt_long size;
394 unsigned tgt_long flags; /* AUTOCLEAN et al */
399 unsigned tgt_long syms;
400 unsigned tgt_long deps;
401 unsigned tgt_long refs;
402 unsigned tgt_long init;
403 unsigned tgt_long cleanup;
404 unsigned tgt_long ex_table_start;
405 unsigned tgt_long ex_table_end;
407 unsigned tgt_long gp;
409 /* Everything after here is extension. */
410 unsigned tgt_long persist_start;
411 unsigned tgt_long persist_end;
412 unsigned tgt_long can_unload;
413 unsigned tgt_long runsize;
414 const char *kallsyms_start; /* All symbols for kernel debugging */
415 const char *kallsyms_end;
416 const char *archdata_start; /* arch specific data for module */
417 const char *archdata_end;
418 const char *kernel_data; /* Reserved for kernel internal use */
422 #define ARCHDATA_SEC_NAME ARCHDATAM
424 #define ARCHDATA_SEC_NAME "__archdata"
426 #define KALLSYMS_SEC_NAME "__kallsyms"
429 struct new_module_info
437 /* Bits of module.flags. */
441 NEW_MOD_AUTOCLEAN = 4,
443 NEW_MOD_USED_ONCE = 16
446 int init_module(const char *name, const struct new_module *);
447 int query_module(const char *name, int which, void *buf,
448 size_t bufsize, size_t *ret);
450 /* Values for query_module's which. */
459 /*======================================================================*/
460 /* The system calls unchanged between 2.0 and 2.1. */
462 unsigned long create_module(const char *, size_t);
463 int delete_module(const char *);
466 #endif /* module.h */
468 //----------------------------------------------------------------------------
469 //--------end of modutils module.h
470 //----------------------------------------------------------------------------
474 //----------------------------------------------------------------------------
475 //--------modutils obj.h, lines 253-462
476 //----------------------------------------------------------------------------
478 /* Elf object file loading and relocation routines.
479 Copyright 1996, 1997 Linux International.
481 Contributed by Richard Henderson <rth@tamu.edu>
483 This file is part of the Linux modutils.
485 This program is free software; you can redistribute it and/or modify it
486 under the terms of the GNU General Public License as published by the
487 Free Software Foundation; either version 2 of the License, or (at your
488 option) any later version.
490 This program is distributed in the hope that it will be useful, but
491 WITHOUT ANY WARRANTY; without even the implied warranty of
492 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
493 General Public License for more details.
495 You should have received a copy of the GNU General Public License
496 along with this program; if not, write to the Free Software Foundation,
497 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
500 #ifndef MODUTILS_OBJ_H
501 /* Why? static const int MODUTILS_OBJ_H = 1; */
503 /* The relocatable object is manipulated using elfin types. */
510 # if ELFCLASSM == ELFCLASS32
511 # define ElfW(x) Elf32_ ## x
512 # define ELFW(x) ELF32_ ## x
514 # define ElfW(x) Elf64_ ## x
515 # define ELFW(x) ELF64_ ## x
519 /* For some reason this is missing from some ancient C libraries.... */
520 #ifndef ELF32_ST_INFO
521 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
524 #ifndef ELF64_ST_INFO
525 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
528 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
529 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
530 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
531 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
532 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
534 struct obj_string_patch;
535 struct obj_symbol_patch;
542 struct obj_section *load_next;
548 struct obj_symbol *next; /* hash table link */
552 int secidx; /* the defining section index/module */
554 int ksymidx; /* for export to the kernel symtab */
555 int referenced; /* actually used in the link */
558 /* Hardcode the hash table size. We shouldn't be needing so many
559 symbols that we begin to degrade performance, and we get a big win
560 by giving the compiler a constant divisor. */
562 #define HASH_BUCKETS 521
568 struct obj_section **sections;
569 struct obj_section *load_order;
570 struct obj_section **load_order_search_start;
571 struct obj_string_patch *string_patches;
572 struct obj_symbol_patch *symbol_patches;
573 int (*symbol_cmp)(const char *, const char *);
574 unsigned long (*symbol_hash)(const char *);
575 unsigned long local_symtab_size;
576 struct obj_symbol **local_symtab;
577 struct obj_symbol *symtab[HASH_BUCKETS];
588 struct obj_string_patch
590 struct obj_string_patch *next;
592 ElfW(Addr) reloc_offset;
593 ElfW(Addr) string_offset;
596 struct obj_symbol_patch
598 struct obj_symbol_patch *next;
600 ElfW(Addr) reloc_offset;
601 struct obj_symbol *sym;
605 /* Generic object manipulation routines. */
607 static unsigned long obj_elf_hash(const char *);
609 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
611 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
614 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
615 struct obj_symbol *sym);
617 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
618 static void obj_set_symbol_compare(struct obj_file *f,
619 int (*cmp)(const char *, const char *),
620 unsigned long (*hash)(const char *));
623 static struct obj_section *obj_find_section (struct obj_file *f,
626 static void obj_insert_section_load_order (struct obj_file *f,
627 struct obj_section *sec);
629 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
634 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
639 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
641 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
644 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
645 struct obj_symbol *sym);
647 static int obj_check_undefineds(struct obj_file *f);
649 static void obj_allocate_commons(struct obj_file *f);
651 static unsigned long obj_load_size (struct obj_file *f);
653 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
655 static struct obj_file *obj_load(FILE *f, int loadprogbits);
657 static int obj_create_image (struct obj_file *f, char *image);
659 /* Architecture specific manipulation routines. */
661 static struct obj_file *arch_new_file (void);
663 static struct obj_section *arch_new_section (void);
665 static struct obj_symbol *arch_new_symbol (void);
667 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
668 struct obj_section *targsec,
669 struct obj_section *symsec,
670 struct obj_symbol *sym,
671 ElfW(RelM) *rel, ElfW(Addr) value);
673 static void arch_create_got (struct obj_file *f);
674 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
675 static int obj_gpl_license(struct obj_file *f, const char **license);
676 #endif /* ENABLE_FEATURE_CHECK_TAINTED_MODULE */
678 //----------------------------------------------------------------------------
679 //--------end of modutils obj.h
680 //----------------------------------------------------------------------------
683 /* SPFX is always a string, so it can be concatenated to string constants. */
685 #define SPFX SYMBOL_PREFIX
691 #define _PATH_MODULES "/lib/modules"
692 enum { STRVERSIONLEN = 32 };
694 /*======================================================================*/
696 static int flag_force_load = 0;
697 static int flag_autoclean = 0;
698 static int flag_verbose = 0;
699 static int flag_quiet = 0;
700 static int flag_export = 1;
703 /*======================================================================*/
705 #if defined(CONFIG_USE_LIST)
707 struct arch_list_entry
709 struct arch_list_entry *next;
710 CONFIG_LIST_ARCHTYPE addend;
717 #if defined(CONFIG_USE_SINGLE)
719 struct arch_single_entry
728 #if defined(__mips__)
731 struct mips_hi16 *next;
738 struct obj_file root;
739 #if defined(CONFIG_USE_PLT_ENTRIES)
740 struct obj_section *plt;
742 #if defined(CONFIG_USE_GOT_ENTRIES)
743 struct obj_section *got;
745 #if defined(__mips__)
746 struct mips_hi16 *mips_hi16_list;
751 struct obj_symbol root;
752 #if defined(CONFIG_USE_PLT_ENTRIES)
753 #if defined(CONFIG_USE_PLT_LIST)
754 struct arch_list_entry *pltent;
756 struct arch_single_entry pltent;
759 #if defined(CONFIG_USE_GOT_ENTRIES)
760 struct arch_single_entry gotent;
765 struct external_module {
770 struct new_module_symbol *syms;
773 static struct new_module_symbol *ksyms;
774 static size_t nksyms;
776 static struct external_module *ext_modules;
777 static int n_ext_modules;
778 static int n_ext_modules_used;
779 extern int delete_module(const char *);
781 static char *m_filename;
782 static char *m_fullName;
786 /*======================================================================*/
789 static int check_module_name_match(const char *filename, struct stat *statbuf,
792 char *fullname = (char *) userdata;
794 if (fullname[0] == '\0')
797 char *tmp, *tmp1 = xstrdup(filename);
798 tmp = bb_get_last_path_component(tmp1);
799 if (strcmp(tmp, fullname) == 0) {
801 /* Stop searching if we find a match */
802 m_filename = xstrdup(filename);
811 /*======================================================================*/
813 static struct obj_file *arch_new_file(void)
816 f = xmalloc(sizeof(*f));
818 memset(f, 0, sizeof(*f));
823 static struct obj_section *arch_new_section(void)
825 return xmalloc(sizeof(struct obj_section));
828 static struct obj_symbol *arch_new_symbol(void)
830 struct arch_symbol *sym;
831 sym = xmalloc(sizeof(*sym));
833 memset(sym, 0, sizeof(*sym));
838 static enum obj_reloc
839 arch_apply_relocation(struct obj_file *f,
840 struct obj_section *targsec,
841 struct obj_section *symsec,
842 struct obj_symbol *sym,
843 ElfW(RelM) *rel, ElfW(Addr) v)
845 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 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
849 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
850 struct arch_symbol *isym = (struct arch_symbol *) sym;
852 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
853 #if defined(CONFIG_USE_GOT_ENTRIES)
854 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
857 #if defined(CONFIG_USE_PLT_ENTRIES)
858 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
860 # if defined(CONFIG_USE_PLT_LIST)
861 struct arch_list_entry *pe;
863 struct arch_single_entry *pe;
867 switch (ELF_R_TYPE(rel->r_info)) {
882 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
883 * (which is .got) similar to branch,
884 * but is full 32 bits relative */
893 case R_ARM_GOTOFF: /* address relative to the got */
897 #elif defined(__cris__)
903 /* CRIS keeps the relocation value in the r_addend field and
904 * should not use whats in *loc at all
909 #elif defined(__H8300H__) || defined(__H8300S__)
912 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
913 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
924 if ((ElfW(Sword))v > 0x7fff ||
925 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
926 ret = obj_reloc_overflow;
928 *(unsigned short *)loc = v;
932 if ((ElfW(Sword))v > 0x7f ||
933 (ElfW(Sword))v < -(ElfW(Sword))0x80)
934 ret = obj_reloc_overflow;
936 *(unsigned char *)loc = v;
939 #elif defined(__i386__)
973 #elif defined (__microblaze__)
974 case R_MICROBLAZE_NONE:
975 case R_MICROBLAZE_64_NONE:
976 case R_MICROBLAZE_32_SYM_OP_SYM:
977 case R_MICROBLAZE_32_PCREL:
980 case R_MICROBLAZE_64_PCREL: {
981 /* dot is the address of the current instruction.
982 * v is the target symbol address.
983 * So we need to extract the offset in the code,
984 * adding v, then subtrating the current address
985 * of this instruction.
986 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
989 /* Get split offset stored in code */
990 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
993 /* Adjust relative offset. -4 adjustment required
994 * because dot points to the IMM insn, but branch
995 * is computed relative to the branch instruction itself.
999 /* Store back into code */
1000 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
1001 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
1006 case R_MICROBLAZE_32:
1010 case R_MICROBLAZE_64: {
1011 /* Get split pointer stored in code */
1012 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
1015 /* Add reloc offset */
1018 /* Store back into code */
1019 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
1020 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
1025 case R_MICROBLAZE_32_PCREL_LO:
1026 case R_MICROBLAZE_32_LO:
1027 case R_MICROBLAZE_SRO32:
1028 case R_MICROBLAZE_SRW32:
1029 ret = obj_reloc_unhandled;
1032 #elif defined(__mc68000__)
1043 ret = obj_reloc_overflow;
1050 ret = obj_reloc_overflow;
1057 if ((ElfW(Sword))v > 0x7f ||
1058 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1059 ret = obj_reloc_overflow;
1066 if ((ElfW(Sword))v > 0x7fff ||
1067 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1068 ret = obj_reloc_overflow;
1074 *(int *)loc = v - dot;
1077 case R_68K_GLOB_DAT:
1078 case R_68K_JMP_SLOT:
1082 case R_68K_RELATIVE:
1083 *(int *)loc += f->baseaddr;
1089 # ifdef R_68K_GOTOFF
1095 #elif defined(__mips__)
1106 ret = obj_reloc_dangerous;
1107 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1108 ret = obj_reloc_overflow;
1110 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1116 struct mips_hi16 *n;
1118 /* We cannot relocate this one now because we don't know the value
1119 of the carry we need to add. Save the information, and let LO16
1120 do the actual relocation. */
1121 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1124 n->next = ifile->mips_hi16_list;
1125 ifile->mips_hi16_list = n;
1131 unsigned long insnlo = *loc;
1132 ElfW(Addr) val, vallo;
1134 /* Sign extend the addend we extract from the lo insn. */
1135 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1137 if (ifile->mips_hi16_list != NULL) {
1138 struct mips_hi16 *l;
1140 l = ifile->mips_hi16_list;
1142 struct mips_hi16 *next;
1145 /* Do the HI16 relocation. Note that we actually don't
1146 need to know anything about the LO16 itself, except where
1147 to find the low 16 bits of the addend needed by the LO16. */
1150 ((insn & 0xffff) << 16) +
1154 /* Account for the sign extension that will happen in the
1161 insn = (insn & ~0xffff) | val;
1169 ifile->mips_hi16_list = NULL;
1172 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1174 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1179 #elif defined(__nios2__)
1184 case R_NIOS2_BFD_RELOC_32:
1188 case R_NIOS2_BFD_RELOC_16:
1190 ret = obj_reloc_overflow;
1195 case R_NIOS2_BFD_RELOC_8:
1197 ret = obj_reloc_overflow;
1206 if ((Elf32_Sword)v > 0x7fff ||
1207 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1208 ret = obj_reloc_overflow;
1212 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1222 ret = obj_reloc_overflow;
1226 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1231 case R_NIOS2_PCREL16:
1236 if ((Elf32_Sword)v > 0x7fff ||
1237 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1238 ret = obj_reloc_overflow;
1242 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1248 Elf32_Addr word, gp;
1250 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1252 if ((Elf32_Sword)v > 0x7fff ||
1253 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1254 ret = obj_reloc_overflow;
1258 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1262 case R_NIOS2_CALL26:
1264 ret = obj_reloc_dangerous;
1265 if ((v >> 28) != (dot >> 28))
1266 ret = obj_reloc_overflow;
1267 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1275 ret = obj_reloc_overflow;
1278 word = *loc & ~0x7c0;
1279 *loc = word | ((v & 0x1f) << 6);
1288 ret = obj_reloc_overflow;
1291 word = *loc & ~0xfc0;
1292 *loc = word | ((v & 0x3f) << 6);
1301 ret = obj_reloc_overflow;
1304 word = *loc & ~0x3fc0;
1305 *loc = word | ((v & 0xff) << 6);
1314 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1324 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1329 case R_NIOS2_HIADJ16:
1331 Elf32_Addr word1, word2;
1334 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1335 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1340 #elif defined(__powerpc64__)
1341 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1343 #elif defined(__powerpc__)
1345 case R_PPC_ADDR16_HA:
1346 *(unsigned short *)loc = (v + 0x8000) >> 16;
1349 case R_PPC_ADDR16_HI:
1350 *(unsigned short *)loc = v >> 16;
1353 case R_PPC_ADDR16_LO:
1354 *(unsigned short *)loc = v;
1368 #elif defined(__s390__)
1371 *(unsigned int *) loc += v;
1374 *(unsigned short *) loc += v;
1377 *(unsigned char *) loc += v;
1381 *(unsigned int *) loc += v - dot;
1384 *(unsigned short *) loc += (v - dot) >> 1;
1387 *(unsigned short *) loc += v - dot;
1391 case R_390_PLT16DBL:
1392 /* find the plt entry and initialize it. */
1393 pe = (struct arch_single_entry *) &isym->pltent;
1394 if (pe->inited == 0) {
1395 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1396 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1398 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1405 /* Insert relative distance to target. */
1406 v = plt + pe->offset - dot;
1407 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1408 *(unsigned int *) loc = (unsigned int) v;
1409 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1410 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1413 case R_390_GLOB_DAT:
1414 case R_390_JMP_SLOT:
1418 case R_390_RELATIVE:
1419 *loc += f->baseaddr;
1423 *(unsigned long *) loc += got - dot;
1429 if (!isym->gotent.inited)
1431 isym->gotent.inited = 1;
1432 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1434 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1435 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1436 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1437 *(unsigned short *) loc += isym->gotent.offset;
1438 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1439 *(unsigned int *) loc += isym->gotent.offset;
1442 # ifndef R_390_GOTOFF32
1443 # define R_390_GOTOFF32 R_390_GOTOFF
1445 case R_390_GOTOFF32:
1449 #elif defined(__sh__)
1472 *loc = f->baseaddr + rel->r_addend;
1476 *loc = got - dot + rel->r_addend;
1486 # if defined(__SH5__)
1487 case R_SH_IMM_MEDLOW16:
1488 case R_SH_IMM_LOW16:
1492 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1496 * movi and shori have the format:
1498 * | op | imm | reg | reserved |
1499 * 31..26 25..10 9.. 4 3 .. 0
1501 * so we simply mask and or in imm.
1503 word = *loc & ~0x3fffc00;
1504 word |= (v & 0xffff) << 10;
1511 case R_SH_IMM_MEDLOW16_PCREL:
1512 case R_SH_IMM_LOW16_PCREL:
1516 word = *loc & ~0x3fffc00;
1520 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1523 word |= (v & 0xffff) << 10;
1529 # endif /* __SH5__ */
1531 #elif defined (__v850e__)
1537 /* We write two shorts instead of a long because even
1538 32-bit insns only need half-word alignment, but
1539 32-bit data needs to be long-word aligned. */
1540 v += ((unsigned short *)loc)[0];
1541 v += ((unsigned short *)loc)[1] << 16;
1542 ((unsigned short *)loc)[0] = v & 0xffff;
1543 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1546 case R_V850_22_PCREL:
1549 #elif defined(__x86_64__)
1559 *(unsigned int *) loc += v;
1562 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1563 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1568 *(signed int *) loc += v;
1572 *(unsigned short *) loc += v;
1576 *(unsigned char *) loc += v;
1580 *(unsigned int *) loc += v - dot;
1584 *(unsigned short *) loc += v - dot;
1588 *(unsigned char *) loc += v - dot;
1591 case R_X86_64_GLOB_DAT:
1592 case R_X86_64_JUMP_SLOT:
1596 case R_X86_64_RELATIVE:
1597 *loc += f->baseaddr;
1600 case R_X86_64_GOT32:
1601 case R_X86_64_GOTPCREL:
1604 if (!isym->gotent.reloc_done)
1606 isym->gotent.reloc_done = 1;
1607 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1609 /* XXX are these really correct? */
1610 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1611 *(unsigned int *) loc += v + isym->gotent.offset;
1613 *loc += isym->gotent.offset;
1618 # warning "no idea how to handle relocations on your arch"
1622 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1623 ret = obj_reloc_unhandled;
1626 #if defined(CONFIG_USE_PLT_ENTRIES)
1630 /* find the plt entry and initialize it if necessary */
1632 #if defined(CONFIG_USE_PLT_LIST)
1633 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1640 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1642 /* generate some machine code */
1644 #if defined(__arm__)
1645 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1646 ip[1] = v; /* sym@ */
1648 #if defined(__powerpc__)
1649 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1650 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1651 ip[2] = 0x7d6903a6; /* mtctr r11 */
1652 ip[3] = 0x4e800420; /* bctr */
1654 #if defined (__v850e__)
1655 /* We have to trash a register, so we assume that any control
1656 transfer more than 21-bits away must be a function call
1657 (so we can use a call-clobbered register). */
1658 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1659 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1664 /* relative distance to target */
1666 /* if the target is too far away.... */
1667 #if defined (__arm__) || defined (__powerpc__)
1668 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1669 #elif defined (__v850e__)
1670 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1672 /* go via the plt */
1673 v = plt + pe->offset - dot;
1675 #if defined (__v850e__)
1680 ret = obj_reloc_dangerous;
1682 /* merge the offset into the instruction. */
1683 #if defined(__arm__)
1684 /* Convert to words. */
1687 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1689 #if defined(__powerpc__)
1690 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1692 #if defined (__v850e__)
1693 /* We write two shorts instead of a long because even 32-bit insns
1694 only need half-word alignment, but the 32-bit data write needs
1695 to be long-word aligned. */
1696 ((unsigned short *)loc)[0] =
1697 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1698 | ((v >> 16) & 0x3f); /* offs high part */
1699 ((unsigned short *)loc)[1] =
1700 (v & 0xffff); /* offs low part */
1703 #endif /* CONFIG_USE_PLT_ENTRIES */
1705 #if defined(CONFIG_USE_GOT_ENTRIES)
1708 /* needs an entry in the .got: set it, once */
1709 if (!isym->gotent.inited) {
1710 isym->gotent.inited = 1;
1711 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1713 /* make the reloc with_respect_to_.got */
1715 *loc += isym->gotent.offset + rel->r_addend;
1716 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1717 *loc += isym->gotent.offset;
1721 #endif /* CONFIG_USE_GOT_ENTRIES */
1728 #if defined(CONFIG_USE_LIST)
1730 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1731 int offset, int size)
1733 struct arch_list_entry *pe;
1735 for (pe = *list; pe != NULL; pe = pe->next) {
1736 if (pe->addend == rel->r_addend) {
1742 pe = xmalloc(sizeof(struct arch_list_entry));
1744 pe->addend = rel->r_addend;
1745 pe->offset = offset;
1755 #if defined(CONFIG_USE_SINGLE)
1757 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1758 int offset, int size)
1760 if (single->allocated == 0) {
1761 single->allocated = 1;
1762 single->offset = offset;
1771 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1773 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1774 int offset, int size)
1776 struct obj_section *myrelsec = obj_find_section(f, name);
1783 obj_extend_section(myrelsec, offset);
1785 myrelsec = obj_create_alloced_section(f, name,
1794 static void arch_create_got(struct obj_file *f)
1796 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1797 struct arch_file *ifile = (struct arch_file *) f;
1799 #if defined(CONFIG_USE_GOT_ENTRIES)
1800 int got_offset = 0, got_needed = 0, got_allocate;
1802 #if defined(CONFIG_USE_PLT_ENTRIES)
1803 int plt_offset = 0, plt_needed = 0, plt_allocate;
1805 struct obj_section *relsec, *symsec, *strsec;
1806 ElfW(RelM) *rel, *relend;
1807 ElfW(Sym) *symtab, *extsym;
1808 const char *strtab, *name;
1809 struct arch_symbol *intsym;
1811 for (i = 0; i < f->header.e_shnum; ++i) {
1812 relsec = f->sections[i];
1813 if (relsec->header.sh_type != SHT_RELM)
1816 symsec = f->sections[relsec->header.sh_link];
1817 strsec = f->sections[symsec->header.sh_link];
1819 rel = (ElfW(RelM) *) relsec->contents;
1820 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1821 symtab = (ElfW(Sym) *) symsec->contents;
1822 strtab = (const char *) strsec->contents;
1824 for (; rel < relend; ++rel) {
1825 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1827 #if defined(CONFIG_USE_GOT_ENTRIES)
1830 #if defined(CONFIG_USE_PLT_ENTRIES)
1834 switch (ELF_R_TYPE(rel->r_info)) {
1835 #if defined(__arm__)
1850 #elif defined(__i386__)
1860 #elif defined(__powerpc__)
1865 #elif defined(__mc68000__)
1876 #elif defined(__sh__)
1886 #elif defined (__v850e__)
1887 case R_V850_22_PCREL:
1896 if (extsym->st_name != 0) {
1897 name = strtab + extsym->st_name;
1899 name = f->sections[extsym->st_shndx]->name;
1901 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1902 #if defined(CONFIG_USE_GOT_ENTRIES)
1904 got_offset += arch_single_init(
1905 rel, &intsym->gotent,
1906 got_offset, CONFIG_GOT_ENTRY_SIZE);
1911 #if defined(CONFIG_USE_PLT_ENTRIES)
1913 #if defined(CONFIG_USE_PLT_LIST)
1914 plt_offset += arch_list_add(
1915 rel, &intsym->pltent,
1916 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1918 plt_offset += arch_single_init(
1919 rel, &intsym->pltent,
1920 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1928 #if defined(CONFIG_USE_GOT_ENTRIES)
1930 ifile->got = arch_xsect_init(f, ".got", got_offset,
1931 CONFIG_GOT_ENTRY_SIZE);
1935 #if defined(CONFIG_USE_PLT_ENTRIES)
1937 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1938 CONFIG_PLT_ENTRY_SIZE);
1942 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1945 /*======================================================================*/
1947 /* Standard ELF hash function. */
1948 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1950 unsigned long h = 0;
1957 if ((g = (h & 0xf0000000)) != 0) {
1966 static unsigned long obj_elf_hash(const char *name)
1968 return obj_elf_hash_n(name, strlen(name));
1971 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1972 /* String comparison for non-co-versioned kernel and module. */
1974 static int ncv_strcmp(const char *a, const char *b)
1976 size_t alen = strlen(a), blen = strlen(b);
1978 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1979 return strncmp(a, b, alen);
1980 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1981 return strncmp(a, b, blen);
1983 return strcmp(a, b);
1986 /* String hashing for non-co-versioned kernel and module. Here
1987 we are simply forced to drop the crc from the hash. */
1989 static unsigned long ncv_symbol_hash(const char *str)
1991 size_t len = strlen(str);
1992 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1994 return obj_elf_hash_n(str, len);
1998 obj_set_symbol_compare(struct obj_file *f,
1999 int (*cmp) (const char *, const char *),
2000 unsigned long (*hash) (const char *))
2003 f->symbol_cmp = cmp;
2005 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
2008 f->symbol_hash = hash;
2010 memcpy(tmptab, f->symtab, sizeof(tmptab));
2011 memset(f->symtab, 0, sizeof(f->symtab));
2013 for (i = 0; i < HASH_BUCKETS; ++i)
2014 for (sym = tmptab[i]; sym; sym = next) {
2015 unsigned long h = hash(sym->name) % HASH_BUCKETS;
2017 sym->next = f->symtab[h];
2023 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2025 static struct obj_symbol *
2026 obj_add_symbol(struct obj_file *f, const char *name,
2027 unsigned long symidx, int info,
2028 int secidx, ElfW(Addr) value,
2031 struct obj_symbol *sym;
2032 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2033 int n_type = ELF_ST_TYPE(info);
2034 int n_binding = ELF_ST_BIND(info);
2036 for (sym = f->symtab[hash]; sym; sym = sym->next)
2037 if (f->symbol_cmp(sym->name, name) == 0) {
2038 int o_secidx = sym->secidx;
2039 int o_info = sym->info;
2040 int o_type = ELF_ST_TYPE(o_info);
2041 int o_binding = ELF_ST_BIND(o_info);
2043 /* A redefinition! Is it legal? */
2045 if (secidx == SHN_UNDEF)
2047 else if (o_secidx == SHN_UNDEF)
2049 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2050 /* Cope with local and global symbols of the same name
2051 in the same object file, as might have been created
2052 by ld -r. The only reason locals are now seen at this
2053 level at all is so that we can do semi-sensible things
2056 struct obj_symbol *nsym, **p;
2058 nsym = arch_new_symbol();
2059 nsym->next = sym->next;
2062 /* Excise the old (local) symbol from the hash chain. */
2063 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2067 } else if (n_binding == STB_LOCAL) {
2068 /* Another symbol of the same name has already been defined.
2069 Just add this to the local table. */
2070 sym = arch_new_symbol();
2073 f->local_symtab[symidx] = sym;
2075 } else if (n_binding == STB_WEAK)
2077 else if (o_binding == STB_WEAK)
2079 /* Don't unify COMMON symbols with object types the programmer
2081 else if (secidx == SHN_COMMON
2082 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2084 else if (o_secidx == SHN_COMMON
2085 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2088 /* Don't report an error if the symbol is coming from
2089 the kernel or some external module. */
2090 if (secidx <= SHN_HIRESERVE)
2091 bb_error_msg("%s multiply defined", name);
2096 /* Completely new symbol. */
2097 sym = arch_new_symbol();
2098 sym->next = f->symtab[hash];
2099 f->symtab[hash] = sym;
2102 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2103 if (symidx >= f->local_symtab_size)
2104 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2105 name, (long) symidx, (long) f->local_symtab_size);
2107 f->local_symtab[symidx] = sym;
2114 sym->secidx = secidx;
2120 static struct obj_symbol *
2121 obj_find_symbol(struct obj_file *f, const char *name)
2123 struct obj_symbol *sym;
2124 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2126 for (sym = f->symtab[hash]; sym; sym = sym->next)
2127 if (f->symbol_cmp(sym->name, name) == 0)
2134 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2137 if (sym->secidx >= SHN_LORESERVE)
2140 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2142 /* As a special case, a NULL sym has value zero. */
2147 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2149 int i, n = f->header.e_shnum;
2151 for (i = 0; i < n; ++i)
2152 if (strcmp(f->sections[i]->name, name) == 0)
2153 return f->sections[i];
2158 static int obj_load_order_prio(struct obj_section *a)
2160 unsigned long af, ac;
2162 af = a->header.sh_flags;
2165 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2166 strcmp(a->name + 5, ".init"))
2170 if (!(af & SHF_WRITE))
2172 if (af & SHF_EXECINSTR)
2174 if (a->header.sh_type != SHT_NOBITS)
2181 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2183 struct obj_section **p;
2184 int prio = obj_load_order_prio(sec);
2185 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2186 if (obj_load_order_prio(*p) < prio)
2188 sec->load_next = *p;
2192 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2194 unsigned long align,
2197 int newidx = f->header.e_shnum++;
2198 struct obj_section *sec;
2200 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2201 f->sections[newidx] = sec = arch_new_section();
2203 memset(sec, 0, sizeof(*sec));
2204 sec->header.sh_type = SHT_PROGBITS;
2205 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2206 sec->header.sh_size = size;
2207 sec->header.sh_addralign = align;
2211 sec->contents = xmalloc(size);
2213 obj_insert_section_load_order(f, sec);
2218 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2220 unsigned long align,
2223 int newidx = f->header.e_shnum++;
2224 struct obj_section *sec;
2226 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2227 f->sections[newidx] = sec = arch_new_section();
2229 memset(sec, 0, sizeof(*sec));
2230 sec->header.sh_type = SHT_PROGBITS;
2231 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2232 sec->header.sh_size = size;
2233 sec->header.sh_addralign = align;
2237 sec->contents = xmalloc(size);
2239 sec->load_next = f->load_order;
2240 f->load_order = sec;
2241 if (f->load_order_search_start == &f->load_order)
2242 f->load_order_search_start = &sec->load_next;
2247 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2249 unsigned long oldsize = sec->header.sh_size;
2251 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2253 return sec->contents + oldsize;
2257 /* Conditionally add the symbols from the given symbol set to the
2261 add_symbols_from( struct obj_file *f,
2262 int idx, struct new_module_symbol *syms, size_t nsyms)
2264 struct new_module_symbol *s;
2267 #ifdef SYMBOL_PREFIX
2269 size_t name_alloced_size = 0;
2271 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2274 gpl = obj_gpl_license(f, NULL) == 0;
2276 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2277 /* Only add symbols that are already marked external.
2278 If we override locals we may cause problems for
2279 argument initialization. We will also create a false
2280 dependency on the module. */
2281 struct obj_symbol *sym;
2284 /* GPL licensed modules can use symbols exported with
2285 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2286 * exported names. Non-GPL modules never see any GPLONLY_
2287 * symbols so they cannot fudge it by adding the prefix on
2290 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2291 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2298 name = (char *)s->name;
2300 #ifdef SYMBOL_PREFIX
2301 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2302 kernel exports `C names', but module object files
2303 reference `linker names'). */
2304 size_t extra = sizeof SYMBOL_PREFIX;
2305 size_t name_size = strlen (name) + extra;
2306 if (name_size > name_alloced_size) {
2307 name_alloced_size = name_size * 2;
2308 name_buf = alloca (name_alloced_size);
2310 strcpy (name_buf, SYMBOL_PREFIX);
2311 strcpy (name_buf + extra - 1, name);
2313 #endif /* SYMBOL_PREFIX */
2315 sym = obj_find_symbol(f, name);
2316 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2317 #ifdef SYMBOL_PREFIX
2318 /* Put NAME_BUF into more permanent storage. */
2319 name = xmalloc (name_size);
2320 strcpy (name, name_buf);
2322 sym = obj_add_symbol(f, name, -1,
2323 ELF_ST_INFO(STB_GLOBAL,
2326 /* Did our symbol just get installed? If so, mark the
2327 module as "used". */
2328 if (sym->secidx == idx)
2336 static void add_kernel_symbols(struct obj_file *f)
2338 struct external_module *m;
2341 /* Add module symbols first. */
2343 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2345 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2346 m->nsyms)) m->used = 1, ++nused;
2348 n_ext_modules_used = nused;
2350 /* And finally the symbols from the kernel proper. */
2353 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2356 static char *get_modinfo_value(struct obj_file *f, const char *key)
2358 struct obj_section *sec;
2359 char *p, *v, *n, *ep;
2360 size_t klen = strlen(key);
2362 sec = obj_find_section(f, ".modinfo");
2366 ep = p + sec->header.sh_size;
2369 n = strchr(p, '\0');
2371 if (p + klen == v && strncmp(p, key, klen) == 0)
2374 if (p + klen == n && strcmp(p, key) == 0)
2384 /*======================================================================*/
2385 /* Functions relating to module loading after 2.1.18. */
2388 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2391 char *p, *q, *key, *sym_name;
2392 struct obj_symbol *sym;
2393 char *contents, *loc;
2397 if ((q = strchr(p, '=')) == NULL) {
2402 key = alloca(q - p + 6);
2403 memcpy(key, "parm_", 5);
2404 memcpy(key + 5, p, q - p);
2407 p = get_modinfo_value(f, key);
2410 bb_error_msg("invalid parameter %s", key);
2414 #ifdef SYMBOL_PREFIX
2415 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2416 strcpy (sym_name, SYMBOL_PREFIX);
2417 strcat (sym_name, key);
2421 sym = obj_find_symbol(f, sym_name);
2423 /* Also check that the parameter was not resolved from the kernel. */
2424 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2425 bb_error_msg("symbol for parameter %s not found", key);
2430 min = strtoul(p, &p, 10);
2432 max = strtoul(p + 1, &p, 10);
2438 contents = f->sections[sym->secidx]->contents;
2439 loc = contents + sym->value;
2443 if ((*p == 's') || (*p == 'c')) {
2446 /* Do C quoting if we begin with a ", else slurp the lot. */
2450 str = alloca(strlen(q));
2451 for (r = str, q++; *q != '"'; ++q, ++r) {
2453 bb_error_msg("improperly terminated string argument for %s",
2456 } else if (*q == '\\')
2490 if (q[1] >= '0' && q[1] <= '7') {
2491 c = (c * 8) + *++q - '0';
2492 if (q[1] >= '0' && q[1] <= '7')
2493 c = (c * 8) + *++q - '0';
2510 /* In this case, the string is not quoted. We will break
2511 it using the coma (like for ints). If the user wants to
2512 include comas in a string, he just has to quote it */
2514 /* Search the next coma */
2518 if (r != (char *) NULL) {
2519 /* Recopy the current field */
2520 str = alloca(r - q + 1);
2521 memcpy(str, q, r - q);
2523 /* I don't know if it is useful, as the previous case
2524 doesn't nul terminate the string ??? */
2527 /* Keep next fields */
2538 obj_string_patch(f, sym->secidx, loc - contents, str);
2539 loc += tgt_sizeof_char_p;
2541 /* Array of chars (in fact, matrix !) */
2542 unsigned long charssize; /* size of each member */
2544 /* Get the size of each member */
2545 /* Probably we should do that outside the loop ? */
2546 if (!isdigit(*(p + 1))) {
2547 bb_error_msg("parameter type 'c' for %s must be followed by"
2548 " the maximum size", key);
2551 charssize = strtoul(p + 1, (char **) NULL, 10);
2554 if (strlen(str) >= charssize) {
2555 bb_error_msg("string too long for %s (max %ld)", key,
2560 /* Copy to location */
2561 strcpy((char *) loc, str);
2565 long v = strtoul(q, &q, 0);
2572 loc += tgt_sizeof_short;
2576 loc += tgt_sizeof_int;
2580 loc += tgt_sizeof_long;
2584 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2599 goto retry_end_of_value;
2603 bb_error_msg("too many values for %s (max %d)", key, max);
2610 bb_error_msg("invalid argument syntax for %s", key);
2617 bb_error_msg("too few values for %s (min %d)", key, min);
2627 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2628 static int new_is_module_checksummed(struct obj_file *f)
2630 const char *p = get_modinfo_value(f, "using_checksums");
2637 /* Get the module's kernel version in the canonical integer form. */
2640 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2645 p = get_modinfo_value(f, "kernel_version");
2648 safe_strncpy(str, p, STRVERSIONLEN);
2650 a = strtoul(p, &p, 10);
2653 b = strtoul(p + 1, &p, 10);
2656 c = strtoul(p + 1, &q, 10);
2660 return a << 16 | b << 8 | c;
2663 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2666 /* Fetch the loaded modules, and all currently exported symbols. */
2668 static int new_get_kernel_symbols(void)
2670 char *module_names, *mn;
2671 struct external_module *modules, *m;
2672 struct new_module_symbol *syms, *s;
2673 size_t ret, bufsize, nmod, nsyms, i, j;
2675 /* Collect the loaded modules. */
2677 module_names = xmalloc(bufsize = 256);
2679 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2680 if (errno == ENOSPC && bufsize < ret) {
2681 module_names = xrealloc(module_names, bufsize = ret);
2682 goto retry_modules_load;
2684 bb_perror_msg("QM_MODULES");
2688 n_ext_modules = nmod = ret;
2690 /* Collect the modules' symbols. */
2693 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2694 memset(modules, 0, nmod * sizeof(*modules));
2695 for (i = 0, mn = module_names, m = modules;
2696 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2697 struct new_module_info info;
2699 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2700 if (errno == ENOENT) {
2701 /* The module was removed out from underneath us. */
2704 bb_perror_msg("query_module: QM_INFO: %s", mn);
2708 syms = xmalloc(bufsize = 1024);
2710 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2713 syms = xrealloc(syms, bufsize = ret);
2714 goto retry_mod_sym_load;
2716 /* The module was removed out from underneath us. */
2719 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2726 m->addr = info.addr;
2730 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2731 s->name += (unsigned long) syms;
2736 /* Collect the kernel's symbols. */
2738 syms = xmalloc(bufsize = 16 * 1024);
2739 retry_kern_sym_load:
2740 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2741 if (errno == ENOSPC && bufsize < ret) {
2742 syms = xrealloc(syms, bufsize = ret);
2743 goto retry_kern_sym_load;
2745 bb_perror_msg("kernel: QM_SYMBOLS");
2748 nksyms = nsyms = ret;
2751 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2752 s->name += (unsigned long) syms;
2758 /* Return the kernel symbol checksum version, or zero if not used. */
2760 static int new_is_kernel_checksummed(void)
2762 struct new_module_symbol *s;
2765 /* Using_Versions is not the first symbol, but it should be in there. */
2767 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2768 if (strcmp((char *) s->name, "Using_Versions") == 0)
2775 static int new_create_this_module(struct obj_file *f, const char *m_name)
2777 struct obj_section *sec;
2779 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2780 sizeof(struct new_module));
2781 memset(sec->contents, 0, sizeof(struct new_module));
2783 obj_add_symbol(f, SPFX "__this_module", -1,
2784 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2785 sizeof(struct new_module));
2787 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2793 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2794 /* add an entry to the __ksymtab section, creating it if necessary */
2795 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2797 struct obj_section *sec;
2800 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2801 * If __ksymtab is defined but not marked alloc, x out the first character
2802 * (no obj_delete routine) and create a new __ksymtab with the correct
2805 sec = obj_find_section(f, "__ksymtab");
2806 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2807 *((char *)(sec->name)) = 'x'; /* override const */
2811 sec = obj_create_alloced_section(f, "__ksymtab",
2812 tgt_sizeof_void_p, 0);
2815 sec->header.sh_flags |= SHF_ALLOC;
2816 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2818 ofs = sec->header.sh_size;
2819 obj_symbol_patch(f, sec->idx, ofs, sym);
2820 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2821 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2823 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2825 static int new_create_module_ksymtab(struct obj_file *f)
2827 struct obj_section *sec;
2830 /* We must always add the module references. */
2832 if (n_ext_modules_used) {
2833 struct new_module_ref *dep;
2834 struct obj_symbol *tm;
2836 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2837 (sizeof(struct new_module_ref)
2838 * n_ext_modules_used));
2842 tm = obj_find_symbol(f, SPFX "__this_module");
2843 dep = (struct new_module_ref *) sec->contents;
2844 for (i = 0; i < n_ext_modules; ++i)
2845 if (ext_modules[i].used) {
2846 dep->dep = ext_modules[i].addr;
2847 obj_symbol_patch(f, sec->idx,
2848 (char *) &dep->ref - sec->contents, tm);
2854 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2859 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2862 /* We don't want to export symbols residing in sections that
2863 aren't loaded. There are a number of these created so that
2864 we make sure certain module options don't appear twice. */
2866 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2868 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2870 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2871 struct obj_symbol *sym;
2872 for (sym = f->symtab[i]; sym; sym = sym->next)
2873 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2874 && sym->secidx <= SHN_HIRESERVE
2875 && (sym->secidx >= SHN_LORESERVE
2876 || loaded[sym->secidx])) {
2877 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2879 obj_symbol_patch(f, sec->idx, ofs, sym);
2880 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2887 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2895 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2897 struct new_module *module;
2898 struct obj_section *sec;
2903 sec = obj_find_section(f, ".this");
2904 if (!sec || !sec->contents) {
2905 bb_perror_msg_and_die("corrupt module %s?",m_name);
2907 module = (struct new_module *) sec->contents;
2908 m_addr = sec->header.sh_addr;
2910 module->size_of_struct = sizeof(*module);
2911 module->size = m_size;
2912 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2914 sec = obj_find_section(f, "__ksymtab");
2915 if (sec && sec->header.sh_size) {
2916 module->syms = sec->header.sh_addr;
2917 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2920 if (n_ext_modules_used) {
2921 sec = obj_find_section(f, ".kmodtab");
2922 module->deps = sec->header.sh_addr;
2923 module->ndeps = n_ext_modules_used;
2927 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2929 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2931 sec = obj_find_section(f, "__ex_table");
2933 module->ex_table_start = sec->header.sh_addr;
2934 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2937 sec = obj_find_section(f, ".text.init");
2939 module->runsize = sec->header.sh_addr - m_addr;
2941 sec = obj_find_section(f, ".data.init");
2943 if (!module->runsize ||
2944 module->runsize > sec->header.sh_addr - m_addr)
2945 module->runsize = sec->header.sh_addr - m_addr;
2947 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2948 if (sec && sec->header.sh_size) {
2949 module->archdata_start = (void*)sec->header.sh_addr;
2950 module->archdata_end = module->archdata_start + sec->header.sh_size;
2952 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2953 if (sec && sec->header.sh_size) {
2954 module->kallsyms_start = (void*)sec->header.sh_addr;
2955 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2958 /* Whew! All of the initialization is complete. Collect the final
2959 module image and give it to the kernel. */
2961 image = xmalloc(m_size);
2962 obj_create_image(f, image);
2964 ret = init_module(m_name, (struct new_module *) image);
2966 bb_perror_msg("init_module: %s", m_name);
2974 /*======================================================================*/
2977 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2980 struct obj_string_patch *p;
2981 struct obj_section *strsec;
2982 size_t len = strlen(string) + 1;
2985 p = xmalloc(sizeof(*p));
2986 p->next = f->string_patches;
2987 p->reloc_secidx = secidx;
2988 p->reloc_offset = offset;
2989 f->string_patches = p;
2991 strsec = obj_find_section(f, ".kstrtab");
2992 if (strsec == NULL) {
2993 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2994 p->string_offset = 0;
2995 loc = strsec->contents;
2997 p->string_offset = strsec->header.sh_size;
2998 loc = obj_extend_section(strsec, len);
3000 memcpy(loc, string, len);
3006 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3007 struct obj_symbol *sym)
3009 struct obj_symbol_patch *p;
3011 p = xmalloc(sizeof(*p));
3012 p->next = f->symbol_patches;
3013 p->reloc_secidx = secidx;
3014 p->reloc_offset = offset;
3016 f->symbol_patches = p;
3021 static int obj_check_undefineds(struct obj_file *f)
3026 for (i = 0; i < HASH_BUCKETS; ++i) {
3027 struct obj_symbol *sym;
3028 for (sym = f->symtab[i]; sym; sym = sym->next)
3029 if (sym->secidx == SHN_UNDEF) {
3030 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3031 sym->secidx = SHN_ABS;
3035 bb_error_msg("unresolved symbol %s", sym->name);
3045 static void obj_allocate_commons(struct obj_file *f)
3047 struct common_entry {
3048 struct common_entry *next;
3049 struct obj_symbol *sym;
3050 } *common_head = NULL;
3054 for (i = 0; i < HASH_BUCKETS; ++i) {
3055 struct obj_symbol *sym;
3056 for (sym = f->symtab[i]; sym; sym = sym->next)
3057 if (sym->secidx == SHN_COMMON) {
3058 /* Collect all COMMON symbols and sort them by size so as to
3059 minimize space wasted by alignment requirements. */
3061 struct common_entry **p, *n;
3062 for (p = &common_head; *p; p = &(*p)->next)
3063 if (sym->size <= (*p)->sym->size)
3066 n = alloca(sizeof(*n));
3074 for (i = 1; i < f->local_symtab_size; ++i) {
3075 struct obj_symbol *sym = f->local_symtab[i];
3076 if (sym && sym->secidx == SHN_COMMON) {
3077 struct common_entry **p, *n;
3078 for (p = &common_head; *p; p = &(*p)->next)
3079 if (sym == (*p)->sym)
3081 else if (sym->size < (*p)->sym->size) {
3082 n = alloca(sizeof(*n));
3092 /* Find the bss section. */
3093 for (i = 0; i < f->header.e_shnum; ++i)
3094 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3097 /* If for some reason there hadn't been one, create one. */
3098 if (i == f->header.e_shnum) {
3099 struct obj_section *sec;
3101 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3102 f->sections[i] = sec = arch_new_section();
3103 f->header.e_shnum = i + 1;
3105 memset(sec, 0, sizeof(*sec));
3106 sec->header.sh_type = SHT_PROGBITS;
3107 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3112 /* Allocate the COMMONS. */
3114 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3115 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3116 struct common_entry *c;
3118 for (c = common_head; c; c = c->next) {
3119 ElfW(Addr) align = c->sym->value;
3121 if (align > max_align)
3123 if (bss_size & (align - 1))
3124 bss_size = (bss_size | (align - 1)) + 1;
3127 c->sym->value = bss_size;
3129 bss_size += c->sym->size;
3132 f->sections[i]->header.sh_size = bss_size;
3133 f->sections[i]->header.sh_addralign = max_align;
3137 /* For the sake of patch relocation and parameter initialization,
3138 allocate zeroed data for NOBITS sections now. Note that after
3139 this we cannot assume NOBITS are really empty. */
3140 for (i = 0; i < f->header.e_shnum; ++i) {
3141 struct obj_section *s = f->sections[i];
3142 if (s->header.sh_type == SHT_NOBITS) {
3143 if (s->header.sh_size != 0)
3144 s->contents = memset(xmalloc(s->header.sh_size),
3145 0, s->header.sh_size);
3149 s->header.sh_type = SHT_PROGBITS;
3154 static unsigned long obj_load_size(struct obj_file *f)
3156 unsigned long dot = 0;
3157 struct obj_section *sec;
3159 /* Finalize the positions of the sections relative to one another. */
3161 for (sec = f->load_order; sec; sec = sec->load_next) {
3164 align = sec->header.sh_addralign;
3165 if (align && (dot & (align - 1)))
3166 dot = (dot | (align - 1)) + 1;
3168 sec->header.sh_addr = dot;
3169 dot += sec->header.sh_size;
3175 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3177 int i, n = f->header.e_shnum;
3180 /* Finalize the addresses of the sections. */
3183 for (i = 0; i < n; ++i)
3184 f->sections[i]->header.sh_addr += base;
3186 /* And iterate over all of the relocations. */
3188 for (i = 0; i < n; ++i) {
3189 struct obj_section *relsec, *symsec, *targsec, *strsec;
3190 ElfW(RelM) * rel, *relend;
3194 relsec = f->sections[i];
3195 if (relsec->header.sh_type != SHT_RELM)
3198 symsec = f->sections[relsec->header.sh_link];
3199 targsec = f->sections[relsec->header.sh_info];
3200 strsec = f->sections[symsec->header.sh_link];
3202 rel = (ElfW(RelM) *) relsec->contents;
3203 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3204 symtab = (ElfW(Sym) *) symsec->contents;
3205 strtab = (const char *) strsec->contents;
3207 for (; rel < relend; ++rel) {
3208 ElfW(Addr) value = 0;
3209 struct obj_symbol *intsym = NULL;
3210 unsigned long symndx;
3211 ElfW(Sym) * extsym = 0;
3214 /* Attempt to find a value to use for this relocation. */
3216 symndx = ELF_R_SYM(rel->r_info);
3218 /* Note we've already checked for undefined symbols. */
3220 extsym = &symtab[symndx];
3221 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3222 /* Local symbols we look up in the local table to be sure
3223 we get the one that is really intended. */
3224 intsym = f->local_symtab[symndx];
3226 /* Others we look up in the hash table. */
3228 if (extsym->st_name)
3229 name = strtab + extsym->st_name;
3231 name = f->sections[extsym->st_shndx]->name;
3232 intsym = obj_find_symbol(f, name);
3235 value = obj_symbol_final_value(f, intsym);
3236 intsym->referenced = 1;
3238 #if SHT_RELM == SHT_RELA
3239 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3240 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3241 if (!extsym || !extsym->st_name ||
3242 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3244 value += rel->r_addend;
3248 switch (arch_apply_relocation
3249 (f, targsec, symsec, intsym, rel, value)) {
3253 case obj_reloc_overflow:
3254 errmsg = "Relocation overflow";
3256 case obj_reloc_dangerous:
3257 errmsg = "Dangerous relocation";
3259 case obj_reloc_unhandled:
3260 errmsg = "Unhandled relocation";
3263 bb_error_msg("%s of type %ld for %s", errmsg,
3264 (long) ELF_R_TYPE(rel->r_info),
3265 strtab + extsym->st_name);
3267 bb_error_msg("%s of type %ld", errmsg,
3268 (long) ELF_R_TYPE(rel->r_info));
3276 /* Finally, take care of the patches. */
3278 if (f->string_patches) {
3279 struct obj_string_patch *p;
3280 struct obj_section *strsec;
3281 ElfW(Addr) strsec_base;
3282 strsec = obj_find_section(f, ".kstrtab");
3283 strsec_base = strsec->header.sh_addr;
3285 for (p = f->string_patches; p; p = p->next) {
3286 struct obj_section *targsec = f->sections[p->reloc_secidx];
3287 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3288 = strsec_base + p->string_offset;
3292 if (f->symbol_patches) {
3293 struct obj_symbol_patch *p;
3295 for (p = f->symbol_patches; p; p = p->next) {
3296 struct obj_section *targsec = f->sections[p->reloc_secidx];
3297 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3298 = obj_symbol_final_value(f, p->sym);
3305 static int obj_create_image(struct obj_file *f, char *image)
3307 struct obj_section *sec;
3308 ElfW(Addr) base = f->baseaddr;
3310 for (sec = f->load_order; sec; sec = sec->load_next) {
3313 if (sec->contents == 0 || sec->header.sh_size == 0)
3316 secimg = image + (sec->header.sh_addr - base);
3318 /* Note that we allocated data for NOBITS sections earlier. */
3319 memcpy(secimg, sec->contents, sec->header.sh_size);
3325 /*======================================================================*/
3327 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3330 ElfW(Shdr) * section_headers;
3334 /* Read the file header. */
3336 f = arch_new_file();
3337 memset(f, 0, sizeof(*f));
3338 f->symbol_cmp = strcmp;
3339 f->symbol_hash = obj_elf_hash;
3340 f->load_order_search_start = &f->load_order;
3342 fseek(fp, 0, SEEK_SET);
3343 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3344 bb_perror_msg("error reading ELF header");
3348 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3349 || f->header.e_ident[EI_MAG1] != ELFMAG1
3350 || f->header.e_ident[EI_MAG2] != ELFMAG2
3351 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3352 bb_error_msg("not an ELF file");
3355 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3356 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3357 ? ELFDATA2MSB : ELFDATA2LSB)
3358 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3359 || !MATCH_MACHINE(f->header.e_machine)) {
3360 bb_error_msg("ELF file not for this architecture");
3363 if (f->header.e_type != ET_REL) {
3364 bb_error_msg("ELF file not a relocatable object");
3368 /* Read the section headers. */
3370 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3371 bb_error_msg("section header size mismatch: %lu != %lu",
3372 (unsigned long) f->header.e_shentsize,
3373 (unsigned long) sizeof(ElfW(Shdr)));
3377 shnum = f->header.e_shnum;
3378 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3379 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3381 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3382 fseek(fp, f->header.e_shoff, SEEK_SET);
3383 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3384 bb_perror_msg("error reading ELF section headers");
3388 /* Read the section data. */
3390 for (i = 0; i < shnum; ++i) {
3391 struct obj_section *sec;
3393 f->sections[i] = sec = arch_new_section();
3394 memset(sec, 0, sizeof(*sec));
3396 sec->header = section_headers[i];
3399 if(sec->header.sh_size) switch (sec->header.sh_type) {
3408 if (!loadprogbits) {
3409 sec->contents = NULL;
3416 if (sec->header.sh_size > 0) {
3417 sec->contents = xmalloc(sec->header.sh_size);
3418 fseek(fp, sec->header.sh_offset, SEEK_SET);
3419 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3420 bb_perror_msg("error reading ELF section data");
3424 sec->contents = NULL;
3428 #if SHT_RELM == SHT_REL
3430 bb_error_msg("RELA relocations not supported on this architecture");
3434 bb_error_msg("REL relocations not supported on this architecture");
3439 if (sec->header.sh_type >= SHT_LOPROC) {
3440 /* Assume processor specific section types are debug
3441 info and can safely be ignored. If this is ever not
3442 the case (Hello MIPS?), don't put ifdefs here but
3443 create an arch_load_proc_section(). */
3447 bb_error_msg("can't handle sections of type %ld",
3448 (long) sec->header.sh_type);
3453 /* Do what sort of interpretation as needed by each section. */
3455 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3457 for (i = 0; i < shnum; ++i) {
3458 struct obj_section *sec = f->sections[i];
3459 sec->name = shstrtab + sec->header.sh_name;
3462 for (i = 0; i < shnum; ++i) {
3463 struct obj_section *sec = f->sections[i];
3465 /* .modinfo should be contents only but gcc has no attribute for that.
3466 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3468 if (strcmp(sec->name, ".modinfo") == 0)
3469 sec->header.sh_flags &= ~SHF_ALLOC;
3471 if (sec->header.sh_flags & SHF_ALLOC)
3472 obj_insert_section_load_order(f, sec);
3474 switch (sec->header.sh_type) {
3477 unsigned long nsym, j;
3481 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3482 bb_error_msg("symbol size mismatch: %lu != %lu",
3483 (unsigned long) sec->header.sh_entsize,
3484 (unsigned long) sizeof(ElfW(Sym)));
3488 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3489 strtab = f->sections[sec->header.sh_link]->contents;
3490 sym = (ElfW(Sym) *) sec->contents;
3492 /* Allocate space for a table of local symbols. */
3493 j = f->local_symtab_size = sec->header.sh_info;
3494 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3496 /* Insert all symbols into the hash table. */
3497 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3498 ElfW(Addr) val = sym->st_value;
3501 name = strtab + sym->st_name;
3502 else if (sym->st_shndx < shnum)
3503 name = f->sections[sym->st_shndx]->name;
3507 #if defined(__SH5__)
3509 * For sh64 it is possible that the target of a branch
3510 * requires a mode switch (32 to 16 and back again).
3512 * This is implied by the lsb being set in the target
3513 * address for SHmedia mode and clear for SHcompact.
3515 val |= sym->st_other & 4;
3518 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3525 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3526 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3527 (unsigned long) sec->header.sh_entsize,
3528 (unsigned long) sizeof(ElfW(RelM)));
3532 /* XXX Relocation code from modutils-2.3.19 is not here.
3533 * Why? That's about 20 lines of code from obj/obj_load.c,
3534 * which gets done in a second pass through the sections.
3535 * This BusyBox insmod does similar work in obj_relocate(). */
3542 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3544 * load the unloaded sections directly into the memory allocated by
3545 * kernel for the module
3548 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3550 ElfW(Addr) base = f->baseaddr;
3551 struct obj_section* sec;
3553 for (sec = f->load_order; sec; sec = sec->load_next) {
3555 /* section already loaded? */
3556 if (sec->contents != NULL)
3559 if (sec->header.sh_size == 0)
3562 sec->contents = imagebase + (sec->header.sh_addr - base);
3563 fseek(fp, sec->header.sh_offset, SEEK_SET);
3564 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3565 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3574 static void hide_special_symbols(struct obj_file *f)
3576 static const char *const specials[] = {
3577 SPFX "cleanup_module",
3579 SPFX "kernel_version",
3583 struct obj_symbol *sym;
3584 const char *const *p;
3586 for (p = specials; *p; ++p)
3587 if ((sym = obj_find_symbol(f, *p)) != NULL)
3589 ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3593 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3594 static int obj_gpl_license(struct obj_file *f, const char **license)
3596 struct obj_section *sec;
3597 /* This list must match *exactly* the list of allowable licenses in
3598 * linux/include/linux/module.h. Checking for leading "GPL" will not
3599 * work, somebody will use "GPL sucks, this is proprietary".
3601 static const char * const gpl_licenses[] = {
3604 "GPL and additional rights",
3609 if ((sec = obj_find_section(f, ".modinfo"))) {
3610 const char *value, *ptr, *endptr;
3611 ptr = sec->contents;
3612 endptr = ptr + sec->header.sh_size;
3613 while (ptr < endptr) {
3614 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3618 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3619 if (strcmp(value+1, gpl_licenses[i]) == 0)
3624 if (strchr(ptr, '\0'))
3625 ptr = strchr(ptr, '\0') + 1;
3633 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3634 #define TAINT_PROPRIETORY_MODULE (1<<0)
3635 #define TAINT_FORCED_MODULE (1<<1)
3636 #define TAINT_UNSAFE_SMP (1<<2)
3637 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3639 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3640 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3644 static int first = 1;
3645 if (fd < 0 && !kernel_has_tainted)
3646 return; /* New modutils on old kernel */
3647 printf("Warning: loading %s will taint the kernel: %s%s\n",
3648 m_name, text1, text2);
3650 printf(" See %s for information about tainted modules\n", TAINT_URL);
3654 read(fd, buf, sizeof(buf)-1);
3655 buf[sizeof(buf)-1] = '\0';
3656 oldval = strtoul(buf, NULL, 10);
3657 sprintf(buf, "%d\n", oldval | taint);
3658 write(fd, buf, strlen(buf));
3662 /* Check if loading this module will taint the kernel. */
3663 static void check_tainted_module(struct obj_file *f, char *m_name)
3665 static const char tainted_file[] = TAINT_FILENAME;
3666 int fd, kernel_has_tainted;
3669 kernel_has_tainted = 1;
3670 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3671 if (errno == ENOENT)
3672 kernel_has_tainted = 0;
3673 else if (errno == EACCES)
3674 kernel_has_tainted = 1;
3676 perror(tainted_file);
3677 kernel_has_tainted = 0;
3681 switch (obj_gpl_license(f, &ptr)) {
3685 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3688 /* The module has a non-GPL license so we pretend that the
3689 * kernel always has a taint flag to get a warning even on
3690 * kernels without the proc flag.
3692 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3695 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3699 if (flag_force_load)
3700 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3705 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3706 #define check_tainted_module(x, y) do { } while(0);
3707 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3709 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3710 /* add module source, timestamp, kernel version and a symbol for the
3711 * start of some sections. this info is used by ksymoops to do better
3715 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3717 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3718 return new_get_module_version(f, str);
3719 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3720 strncpy(str, "???", sizeof(str));
3722 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3725 /* add module source, timestamp, kernel version and a symbol for the
3726 * start of some sections. this info is used by ksymoops to do better
3730 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3733 static const char symprefix[] = "__insmod_";
3734 struct obj_section *sec;
3735 struct obj_symbol *sym;
3736 char *name, *absolute_filename;
3737 char str[STRVERSIONLEN], real[PATH_MAX];
3738 int i, l, lm_name, lfilename, use_ksymtab, version;
3739 struct stat statbuf;
3741 static const char *section_names[] = {
3749 if (realpath(filename, real)) {
3750 absolute_filename = xstrdup(real);
3753 int save_errno = errno;
3754 bb_error_msg("cannot get realpath for %s", filename);
3757 absolute_filename = xstrdup(filename);
3760 lm_name = strlen(m_name);
3761 lfilename = strlen(absolute_filename);
3763 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3764 * are not to be exported. otherwise leave ksymtab alone for now, the
3765 * "export all symbols" compatibility code will export these symbols later.
3767 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3769 if ((sec = obj_find_section(f, ".this"))) {
3770 /* tag the module header with the object name, last modified
3771 * timestamp and module version. worst case for module version
3772 * is 0xffffff, decimal 16777215. putting all three fields in
3773 * one symbol is less readable but saves kernel space.
3775 l = sizeof(symprefix)+ /* "__insmod_" */
3776 lm_name+ /* module name */
3778 lfilename+ /* object filename */
3780 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3782 8+ /* version in dec */
3785 if (stat(absolute_filename, &statbuf) != 0)
3786 statbuf.st_mtime = 0;
3787 version = get_module_version(f, str); /* -1 if not found */
3788 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3789 symprefix, m_name, absolute_filename,
3790 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3792 sym = obj_add_symbol(f, name, -1,
3793 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3794 sec->idx, sec->header.sh_addr, 0);
3796 new_add_ksymtab(f, sym);
3798 free(absolute_filename);
3799 #ifdef _NOT_SUPPORTED_
3800 /* record where the persistent data is going, same address as previous symbol */
3803 l = sizeof(symprefix)+ /* "__insmod_" */
3804 lm_name+ /* module name */
3806 strlen(f->persist)+ /* data store */
3809 snprintf(name, l, "%s%s_P%s",
3810 symprefix, m_name, f->persist);
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);
3816 #endif /* _NOT_SUPPORTED_ */
3817 /* tag the desired sections if size is non-zero */
3819 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3820 if ((sec = obj_find_section(f, section_names[i])) &&
3821 sec->header.sh_size) {
3822 l = sizeof(symprefix)+ /* "__insmod_" */
3823 lm_name+ /* module name */
3825 strlen(sec->name)+ /* section name */
3827 8+ /* length in dec */
3830 snprintf(name, l, "%s%s_S%s_L%ld",
3831 symprefix, m_name, sec->name,
3832 (long)sec->header.sh_size);
3833 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3834 sec->idx, sec->header.sh_addr, 0);
3836 new_add_ksymtab(f, sym);
3840 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3842 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3843 static void print_load_map(struct obj_file *f)
3845 struct obj_symbol *sym;
3846 struct obj_symbol **all, **p;
3847 struct obj_section *sec;
3848 int i, nsyms, *loaded;
3850 /* Report on the section layout. */
3852 printf("Sections: Size %-*s Align\n",
3853 (int) (2 * sizeof(void *)), "Address");
3855 for (sec = f->load_order; sec; sec = sec->load_next) {
3859 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3864 printf("%-15s %08lx %0*lx 2**%d\n",
3866 (long)sec->header.sh_size,
3867 (int) (2 * sizeof(void *)),
3868 (long)sec->header.sh_addr,
3871 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3872 /* Quick reference which section indicies are loaded. */
3874 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3876 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3878 /* Collect the symbols we'll be listing. */
3880 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3881 for (sym = f->symtab[i]; sym; sym = sym->next)
3882 if (sym->secidx <= SHN_HIRESERVE
3883 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3886 all = alloca(nsyms * sizeof(struct obj_symbol *));
3888 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3889 for (sym = f->symtab[i]; sym; sym = sym->next)
3890 if (sym->secidx <= SHN_HIRESERVE
3891 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3894 /* And list them. */
3895 printf("\nSymbols:\n");
3896 for (p = all; p < all + nsyms; ++p) {
3898 unsigned long value;
3901 if (sym->secidx == SHN_ABS) {
3904 } else if (sym->secidx == SHN_UNDEF) {
3908 sec = f->sections[sym->secidx];
3910 if (sec->header.sh_type == SHT_NOBITS)
3912 else if (sec->header.sh_flags & SHF_ALLOC) {
3913 if (sec->header.sh_flags & SHF_EXECINSTR)
3915 else if (sec->header.sh_flags & SHF_WRITE)
3920 value = sym->value + sec->header.sh_addr;
3923 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3924 type = tolower(type);
3926 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3934 int insmod_main( int argc, char **argv)
3940 unsigned long m_size;
3945 int exit_status = EXIT_FAILURE;
3947 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3948 struct utsname uts_info;
3949 char m_strversion[STRVERSIONLEN];
3950 int m_version, m_crcs;
3952 #ifdef CONFIG_FEATURE_CLEAN_UP
3957 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3958 int flag_print_load_map = 0;
3961 struct utsname myuname;
3963 /* Parse any options */
3964 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3965 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
3967 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
3971 case 'f': /* force loading */
3972 flag_force_load = 1;
3974 case 'k': /* module loaded by kerneld, auto-cleanable */
3977 case 's': /* log to syslog */
3978 /* log to syslog -- not supported */
3979 /* but kernel needs this for request_module(), */
3980 /* as this calls: modprobe -k -s -- <module> */
3981 /* so silently ignore this flag */
3983 case 'v': /* verbose output */
3986 case 'q': /* silent */
3989 case 'x': /* do not export externs */
3992 case 'o': /* name the output module */
3994 m_name = xstrdup(optarg);
3996 case 'L': /* Stub warning */
3997 /* This is needed for compatibility with modprobe.
3998 * In theory, this does locking, but we don't do
3999 * that. So be careful and plan your life around not
4000 * loading the same module 50 times concurrently. */
4002 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4003 case 'm': /* print module load map */
4004 flag_print_load_map = 1;
4012 if (argv[optind] == NULL) {
4016 /* Grab the module name */
4017 tmp1 = xstrdup(argv[optind]);
4018 tmp = basename(tmp1);
4021 if (uname(&myuname) == 0) {
4022 if (myuname.release[0] == '2') {
4023 k_version = myuname.release[2] - '0';
4027 #if defined(CONFIG_FEATURE_2_6_MODULES)
4028 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
4029 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
4035 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4041 #if defined(CONFIG_FEATURE_2_6_MODULES)
4043 m_fullName = xasprintf("%s.ko", tmp);
4046 m_fullName = xasprintf("%s.o", tmp);
4052 tmp1 = 0; /* flag for free(m_name) before exit() */
4055 /* Get a filedesc for the module. Check we we have a complete path */
4056 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
4057 (fp = fopen(argv[optind], "r")) == NULL) {
4058 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4059 * but do not error out yet if we fail to find it... */
4060 if (k_version) { /* uname succeedd */
4063 char real_module_dir[FILENAME_MAX];
4065 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4066 /* Jump through hoops in case /lib/modules/`uname -r`
4067 * is a symlink. We do not want recursive_action to
4068 * follow symlinks, but we do want to follow the
4069 * /lib/modules/`uname -r` dir, So resolve it ourselves
4070 * if it is a link... */
4071 if (realpath (tmdn, real_module_dir) == NULL)
4074 module_dir = real_module_dir;
4075 recursive_action(module_dir, TRUE, FALSE, FALSE,
4076 check_module_name_match, 0, m_fullName);
4080 /* Check if we have found anything yet */
4081 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4083 char module_dir[FILENAME_MAX];
4087 if (realpath (_PATH_MODULES, module_dir) == NULL)
4088 strcpy(module_dir, _PATH_MODULES);
4089 /* No module found under /lib/modules/`uname -r`, this
4090 * time cast the net a bit wider. Search /lib/modules/ */
4091 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4092 check_module_name_match, 0, m_fullName))
4095 || ((fp = fopen(m_filename, "r")) == NULL))
4097 bb_error_msg("%s: no module by that name found", m_fullName);
4101 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4104 m_filename = xstrdup(argv[optind]);
4107 printf("Using %s\n", m_filename);
4109 #ifdef CONFIG_FEATURE_2_6_MODULES
4113 argv[optind + 1] = m_filename;
4114 return insmod_ng_main(argc - optind, argv + optind);
4118 if ((f = obj_load(fp, LOADBITS)) == NULL)
4119 bb_perror_msg_and_die("Could not load the module");
4121 if (get_modinfo_value(f, "kernel_version") == NULL)
4126 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4127 /* Version correspondence? */
4129 if (uname(&uts_info) < 0)
4130 uts_info.release[0] = '\0';
4131 if (m_has_modinfo) {
4132 m_version = new_get_module_version(f, m_strversion);
4133 if (m_version == -1) {
4134 bb_error_msg("couldn't find the kernel version the module was "
4140 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4141 if (flag_force_load) {
4142 bb_error_msg("Warning: kernel-module version mismatch\n"
4143 "\t%s was compiled for kernel version %s\n"
4144 "\twhile this kernel is version %s",
4145 m_filename, m_strversion, uts_info.release);
4147 bb_error_msg("kernel-module version mismatch\n"
4148 "\t%s was compiled for kernel version %s\n"
4149 "\twhile this kernel is version %s.",
4150 m_filename, m_strversion, uts_info.release);
4156 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4158 if (!query_module(NULL, 0, NULL, 0, NULL)) {
4159 if (!new_get_kernel_symbols())
4161 k_crcs = new_is_kernel_checksummed();
4163 bb_error_msg("Not configured to support old kernels");
4167 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4170 m_crcs = new_is_module_checksummed(f);
4172 if (m_crcs != k_crcs)
4173 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4174 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4176 /* Let the module know about the kernel symbols. */
4177 add_kernel_symbols(f);
4179 /* Allocate common symbols, symbol tables, and string tables. */
4181 if (!new_create_this_module(f, m_name))
4186 if (!obj_check_undefineds(f)) {
4189 obj_allocate_commons(f);
4190 check_tainted_module(f, m_name);
4192 /* done with the module name, on to the optional var=value arguments */
4195 if (optind < argc) {
4196 if (!new_process_module_arguments(f, argc - optind, argv + optind))
4203 hide_special_symbols(f);
4205 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4206 add_ksymoops_symbols(f, m_filename, m_name);
4207 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4209 new_create_module_ksymtab(f);
4211 /* Find current size of the module */
4212 m_size = obj_load_size(f);
4215 m_addr = create_module(m_name, m_size);
4216 if (m_addr == -1) switch (errno) {
4218 bb_error_msg("A module named %s already exists", m_name);
4221 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4225 bb_perror_msg("create_module: %s", m_name);
4231 * the PROGBITS section was not loaded by the obj_load
4232 * now we can load them directly into the kernel memory
4234 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4235 delete_module(m_name);
4240 if (!obj_relocate(f, m_addr)) {
4241 delete_module(m_name);
4245 if (!new_init_module(m_name, f, m_size))
4247 delete_module(m_name);
4251 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4252 if(flag_print_load_map)
4256 exit_status = EXIT_SUCCESS;
4259 #ifdef CONFIG_FEATURE_CLEAN_UP
4268 return(exit_status);
4275 #ifdef CONFIG_FEATURE_2_6_MODULES
4277 #include <sys/mman.h>
4278 #include <asm/unistd.h>
4279 #include <sys/syscall.h>
4281 /* We use error numbers in a loose translation... */
4282 static const char *moderror(int err)
4286 return "Invalid module format";
4288 return "Unknown symbol in module";
4290 return "Module has wrong symbol version";
4292 return "Invalid parameters";
4294 return strerror(err);
4298 int insmod_ng_main( int argc, char **argv)
4306 char *filename, *options = xstrdup("");
4314 /* Rest is options */
4315 for (i = 2; i < argc; i++) {
4316 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4317 /* Spaces handled by "" pairs, but no way of escaping quotes */
4318 if (strchr(argv[i], ' ')) {
4319 strcat(options, "\"");
4320 strcat(options, argv[i]);
4321 strcat(options, "\"");
4323 strcat(options, argv[i]);
4325 strcat(options, " ");
4328 fd = xopen3(filename, O_RDONLY, 0);
4332 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4333 if (map == MAP_FAILED) {
4334 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4337 ret = syscall(__NR_init_module, map, len, options);
4339 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4340 filename, moderror(errno), ret);