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 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
188 #define SHT_RELM SHT_RELA
189 #define Elf32_RelM Elf32_Rela
190 #define ELFCLASSM ELFCLASS32
194 #if defined(__mips__)
195 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
196 #define SHT_RELM SHT_REL
197 #define Elf32_RelM Elf32_Rel
198 #define ELFCLASSM ELFCLASS32
199 /* Account for ELF spec changes. */
200 #ifndef EM_MIPS_RS3_LE
201 #ifdef EM_MIPS_RS4_BE
202 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
204 #define EM_MIPS_RS3_LE 10
206 #endif /* !EM_MIPS_RS3_LE */
207 #define ARCHDATAM "__dbe_table"
211 #if defined(__nios2__)
212 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
213 #define SHT_RELM SHT_RELA
214 #define Elf32_RelM Elf32_Rela
215 #define ELFCLASSM ELFCLASS32
219 #if defined(__powerpc64__)
220 #define MATCH_MACHINE(x) (x == EM_PPC64)
221 #define SHT_RELM SHT_RELA
222 #define Elf64_RelM Elf64_Rela
223 #define ELFCLASSM ELFCLASS64
224 #elif defined(__powerpc__)
225 #define MATCH_MACHINE(x) (x == EM_PPC)
226 #define SHT_RELM SHT_RELA
227 #define Elf32_RelM Elf32_Rela
228 #define ELFCLASSM ELFCLASS32
229 #define USE_PLT_ENTRIES
230 #define PLT_ENTRY_SIZE 16
232 #define LIST_ARCHTYPE ElfW(Addr)
234 #define ARCHDATAM "__ftr_fixup"
238 #if defined(__s390__)
239 #define MATCH_MACHINE(x) (x == EM_S390)
240 #define SHT_RELM SHT_RELA
241 #define Elf32_RelM Elf32_Rela
242 #define ELFCLASSM ELFCLASS32
243 #define USE_PLT_ENTRIES
244 #define PLT_ENTRY_SIZE 8
245 #define USE_GOT_ENTRIES
246 #define GOT_ENTRY_SIZE 8
252 #define MATCH_MACHINE(x) (x == EM_SH)
253 #define SHT_RELM SHT_RELA
254 #define Elf32_RelM Elf32_Rela
255 #define ELFCLASSM ELFCLASS32
256 #define USE_GOT_ENTRIES
257 #define GOT_ENTRY_SIZE 4
259 /* the SH changes have only been tested in =little endian= mode */
260 /* I'm not sure about big endian, so let's warn: */
261 #if defined(__sh__) && BB_BIG_ENDIAN
262 # error insmod.c may require changes for use on big endian SH
264 /* it may or may not work on the SH1/SH2... Error on those also */
265 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
266 #error insmod.c may require changes for SH1 or SH2 use
271 #if defined(__sparc__)
272 #define MATCH_MACHINE(x) (x == EM_SPARC)
273 #define SHT_RELM SHT_RELA
274 #define Elf32_RelM Elf32_Rela
275 #define ELFCLASSM ELFCLASS32
279 #if defined(__v850e__)
280 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
281 #define SHT_RELM SHT_RELA
282 #define Elf32_RelM Elf32_Rela
283 #define ELFCLASSM ELFCLASS32
284 #define USE_PLT_ENTRIES
285 #define PLT_ENTRY_SIZE 8
287 #ifndef EM_CYGNUS_V850 /* grumble */
288 #define EM_CYGNUS_V850 0x9080
290 #define SYMBOL_PREFIX "_"
294 #if defined(__x86_64__)
295 #define MATCH_MACHINE(x) (x == EM_X86_64)
296 #define SHT_RELM SHT_RELA
297 #define USE_GOT_ENTRIES
298 #define GOT_ENTRY_SIZE 8
300 #define Elf64_RelM Elf64_Rela
301 #define ELFCLASSM ELFCLASS64
305 #error Sorry, but insmod.c does not yet support this architecture...
309 //----------------------------------------------------------------------------
310 //--------modutils module.h, lines 45-242
311 //----------------------------------------------------------------------------
313 /* Definitions for the Linux module syscall interface.
314 Copyright 1996, 1997 Linux International.
316 Contributed by Richard Henderson <rth@tamu.edu>
318 This file is part of the Linux modutils.
320 This program is free software; you can redistribute it and/or modify it
321 under the terms of the GNU General Public License as published by the
322 Free Software Foundation; either version 2 of the License, or (at your
323 option) any later version.
325 This program is distributed in the hope that it will be useful, but
326 WITHOUT ANY WARRANTY; without even the implied warranty of
327 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
328 General Public License for more details.
330 You should have received a copy of the GNU General Public License
331 along with this program; if not, write to the Free Software Foundation,
332 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
335 #ifndef MODUTILS_MODULE_H
337 /*======================================================================*/
338 /* For sizeof() which are related to the module platform and not to the
339 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
341 #define tgt_sizeof_char sizeof(char)
342 #define tgt_sizeof_short sizeof(short)
343 #define tgt_sizeof_int sizeof(int)
344 #define tgt_sizeof_long sizeof(long)
345 #define tgt_sizeof_char_p sizeof(char *)
346 #define tgt_sizeof_void_p sizeof(void *)
347 #define tgt_long long
349 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
350 #undef tgt_sizeof_long
351 #undef tgt_sizeof_char_p
352 #undef tgt_sizeof_void_p
356 tgt_sizeof_char_p = 8,
357 tgt_sizeof_void_p = 8
359 #define tgt_long long long
362 /*======================================================================*/
363 /* The structures used in Linux 2.1. */
365 /* Note: new_module_symbol does not use tgt_long intentionally */
366 struct new_module_symbol {
371 struct new_module_persist;
373 struct new_module_ref {
374 unsigned tgt_long dep; /* kernel addresses */
375 unsigned tgt_long ref;
376 unsigned tgt_long next_ref;
380 unsigned tgt_long size_of_struct; /* == sizeof(module) */
381 unsigned tgt_long next;
382 unsigned tgt_long name;
383 unsigned tgt_long size;
386 unsigned tgt_long flags; /* AUTOCLEAN et al */
391 unsigned tgt_long syms;
392 unsigned tgt_long deps;
393 unsigned tgt_long refs;
394 unsigned tgt_long init;
395 unsigned tgt_long cleanup;
396 unsigned tgt_long ex_table_start;
397 unsigned tgt_long ex_table_end;
399 unsigned tgt_long gp;
401 /* Everything after here is extension. */
402 unsigned tgt_long persist_start;
403 unsigned tgt_long persist_end;
404 unsigned tgt_long can_unload;
405 unsigned tgt_long runsize;
406 const char *kallsyms_start; /* All symbols for kernel debugging */
407 const char *kallsyms_end;
408 const char *archdata_start; /* arch specific data for module */
409 const char *archdata_end;
410 const char *kernel_data; /* Reserved for kernel internal use */
414 #define ARCHDATA_SEC_NAME ARCHDATAM
416 #define ARCHDATA_SEC_NAME "__archdata"
418 #define KALLSYMS_SEC_NAME "__kallsyms"
421 struct new_module_info {
428 /* Bits of module.flags. */
432 NEW_MOD_AUTOCLEAN = 4,
434 NEW_MOD_USED_ONCE = 16
437 int init_module(const char *name, const struct new_module *);
438 int query_module(const char *name, int which, void *buf,
439 size_t bufsize, size_t *ret);
441 /* Values for query_module's which. */
450 /*======================================================================*/
451 /* The system calls unchanged between 2.0 and 2.1. */
453 unsigned long create_module(const char *, size_t);
454 int delete_module(const char *);
457 #endif /* module.h */
459 //----------------------------------------------------------------------------
460 //--------end of modutils module.h
461 //----------------------------------------------------------------------------
465 //----------------------------------------------------------------------------
466 //--------modutils obj.h, lines 253-462
467 //----------------------------------------------------------------------------
469 /* Elf object file loading and relocation routines.
470 Copyright 1996, 1997 Linux International.
472 Contributed by Richard Henderson <rth@tamu.edu>
474 This file is part of the Linux modutils.
476 This program is free software; you can redistribute it and/or modify it
477 under the terms of the GNU General Public License as published by the
478 Free Software Foundation; either version 2 of the License, or (at your
479 option) any later version.
481 This program is distributed in the hope that it will be useful, but
482 WITHOUT ANY WARRANTY; without even the implied warranty of
483 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
484 General Public License for more details.
486 You should have received a copy of the GNU General Public License
487 along with this program; if not, write to the Free Software Foundation,
488 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
491 #ifndef MODUTILS_OBJ_H
493 /* The relocatable object is manipulated using elfin types. */
499 # if ELFCLASSM == ELFCLASS32
500 # define ElfW(x) Elf32_ ## x
501 # define ELFW(x) ELF32_ ## x
503 # define ElfW(x) Elf64_ ## x
504 # define ELFW(x) ELF64_ ## x
508 /* For some reason this is missing from some ancient C libraries.... */
509 #ifndef ELF32_ST_INFO
510 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
513 #ifndef ELF64_ST_INFO
514 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
517 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
518 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
519 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
520 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
521 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
523 struct obj_string_patch;
524 struct obj_symbol_patch;
531 struct obj_section *load_next;
537 struct obj_symbol *next; /* hash table link */
541 int secidx; /* the defining section index/module */
543 int ksymidx; /* for export to the kernel symtab */
544 int referenced; /* actually used in the link */
547 /* Hardcode the hash table size. We shouldn't be needing so many
548 symbols that we begin to degrade performance, and we get a big win
549 by giving the compiler a constant divisor. */
551 #define HASH_BUCKETS 521
556 struct obj_section **sections;
557 struct obj_section *load_order;
558 struct obj_section **load_order_search_start;
559 struct obj_string_patch *string_patches;
560 struct obj_symbol_patch *symbol_patches;
561 int (*symbol_cmp)(const char *, const char *);
562 unsigned long (*symbol_hash)(const char *);
563 unsigned long local_symtab_size;
564 struct obj_symbol **local_symtab;
565 struct obj_symbol *symtab[HASH_BUCKETS];
575 struct obj_string_patch {
576 struct obj_string_patch *next;
578 ElfW(Addr) reloc_offset;
579 ElfW(Addr) string_offset;
582 struct obj_symbol_patch {
583 struct obj_symbol_patch *next;
585 ElfW(Addr) reloc_offset;
586 struct obj_symbol *sym;
590 /* Generic object manipulation routines. */
592 static unsigned long obj_elf_hash(const char *);
594 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
596 static struct obj_symbol *obj_find_symbol(struct obj_file *f,
599 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
600 struct obj_symbol *sym);
602 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
603 static void obj_set_symbol_compare(struct obj_file *f,
604 int (*cmp)(const char *, const char *),
605 unsigned long (*hash)(const char *));
608 static struct obj_section *obj_find_section(struct obj_file *f,
611 static void obj_insert_section_load_order(struct obj_file *f,
612 struct obj_section *sec);
614 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
619 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
624 static void *obj_extend_section(struct obj_section *sec, unsigned long more);
626 static void obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
629 static void obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
630 struct obj_symbol *sym);
632 static void obj_check_undefineds(struct obj_file *f);
634 static void obj_allocate_commons(struct obj_file *f);
636 static unsigned long obj_load_size(struct obj_file *f);
638 static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
640 static struct obj_file *obj_load(FILE *f, int loadprogbits);
642 static int obj_create_image(struct obj_file *f, char *image);
644 /* Architecture specific manipulation routines. */
646 static struct obj_file *arch_new_file(void);
648 static struct obj_section *arch_new_section(void);
650 static struct obj_symbol *arch_new_symbol(void);
652 static enum obj_reloc arch_apply_relocation(struct obj_file *f,
653 struct obj_section *targsec,
654 /*struct obj_section *symsec,*/
655 struct obj_symbol *sym,
656 ElfW(RelM) *rel, ElfW(Addr) value);
658 static void arch_create_got(struct obj_file *f);
659 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
660 static int obj_gpl_license(struct obj_file *f, const char **license);
661 #endif /* FEATURE_CHECK_TAINTED_MODULE */
663 //----------------------------------------------------------------------------
664 //--------end of modutils obj.h
665 //----------------------------------------------------------------------------
668 /* SPFX is always a string, so it can be concatenated to string constants. */
670 #define SPFX SYMBOL_PREFIX
676 #define _PATH_MODULES "/lib/modules"
677 enum { STRVERSIONLEN = 64 };
679 /*======================================================================*/
681 #define OPTION_STR "sLo:fkvqx" USE_FEATURE_INSMOD_LOAD_MAP("m")
683 OPT_s = 0x1, // -s /* log to syslog */
684 /* Not supported but kernel needs this for request_module(),
685 as this calls: modprobe -k -s -- <module>
686 so silently ignore this flag */
687 OPT_L = 0x2, // -L /* Stub warning */
688 /* Compatibility with modprobe.
689 In theory, this does locking, but we don't do
690 that. So be careful and plan your life around not
691 loading the same module 50 times concurrently. */
692 OPT_o = 0x4, // -o /* name the output module */
693 OPT_f = 0x8, // -f /* force loading */
694 OPT_k = 0x10, // -k /* module loaded by kerneld, auto-cleanable */
695 OPT_v = 0x20, // -v /* verbose output */
696 OPT_q = 0x40, // -q /* silent */
697 OPT_x = 0x80, // -x /* do not export externs */
698 OPT_m = 0x100, // -m /* print module load map */
700 #define flag_force_load (option_mask32 & OPT_f)
701 #define flag_autoclean (option_mask32 & OPT_k)
702 #define flag_verbose (option_mask32 & OPT_v)
703 #define flag_quiet (option_mask32 & OPT_q)
704 #define flag_noexport (option_mask32 & OPT_x)
705 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
706 #define flag_print_load_map (option_mask32 & OPT_m)
708 #define flag_print_load_map 0
711 /*======================================================================*/
713 #if defined(USE_LIST)
715 struct arch_list_entry
717 struct arch_list_entry *next;
718 LIST_ARCHTYPE addend;
725 #if defined(USE_SINGLE)
727 struct arch_single_entry
736 #if defined(__mips__)
739 struct mips_hi16 *next;
746 struct obj_file root;
747 #if defined(USE_PLT_ENTRIES)
748 struct obj_section *plt;
750 #if defined(USE_GOT_ENTRIES)
751 struct obj_section *got;
753 #if defined(__mips__)
754 struct mips_hi16 *mips_hi16_list;
759 struct obj_symbol root;
760 #if defined(USE_PLT_ENTRIES)
761 #if defined(USE_PLT_LIST)
762 struct arch_list_entry *pltent;
764 struct arch_single_entry pltent;
767 #if defined(USE_GOT_ENTRIES)
768 struct arch_single_entry gotent;
773 struct external_module {
778 struct new_module_symbol *syms;
781 static struct new_module_symbol *ksyms;
782 static size_t nksyms;
784 static struct external_module *ext_modules;
785 static int n_ext_modules;
786 static int n_ext_modules_used;
788 static char *m_filename;
789 static char *m_fullName;
792 /*======================================================================*/
795 static int check_module_name_match(const char *filename,
796 struct stat *statbuf ATTRIBUTE_UNUSED,
797 void *userdata, int depth ATTRIBUTE_UNUSED)
799 char *fullname = (char *) userdata;
802 if (fullname[0] == '\0')
805 tmp = bb_get_last_path_component_nostrip(filename);
806 if (strcmp(tmp, fullname) == 0) {
807 /* Stop searching if we find a match */
808 m_filename = xstrdup(filename);
815 /*======================================================================*/
817 static struct obj_file *arch_new_file(void)
820 f = xzalloc(sizeof(*f));
821 return &f->root; /* it's a first member */
824 static struct obj_section *arch_new_section(void)
826 return xzalloc(sizeof(struct obj_section));
829 static struct obj_symbol *arch_new_symbol(void)
831 struct arch_symbol *sym;
832 sym = xzalloc(sizeof(*sym));
836 static enum obj_reloc
837 arch_apply_relocation(struct obj_file *f,
838 struct obj_section *targsec,
839 /*struct obj_section *symsec,*/
840 struct obj_symbol *sym,
841 ElfW(RelM) *rel, ElfW(Addr) v)
843 struct arch_file *ifile = (struct arch_file *) f;
844 enum obj_reloc ret = obj_reloc_ok;
845 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
846 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
847 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
848 struct arch_symbol *isym = (struct arch_symbol *) sym;
850 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
851 #if defined(USE_GOT_ENTRIES)
852 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
855 #if defined(USE_PLT_ENTRIES)
856 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
858 # if defined(USE_PLT_LIST)
859 struct arch_list_entry *pe;
861 struct arch_single_entry *pe;
865 switch (ELF_R_TYPE(rel->r_info)) {
880 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
881 * (which is .got) similar to branch,
882 * but is full 32 bits relative */
891 case R_ARM_GOTOFF: /* address relative to the got */
895 #elif defined(__cris__)
901 /* CRIS keeps the relocation value in the r_addend field and
902 * should not use whats in *loc at all
907 #elif defined(__H8300H__) || defined(__H8300S__)
910 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
911 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
922 if ((ElfW(Sword))v > 0x7fff ||
923 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
924 ret = obj_reloc_overflow;
926 *(unsigned short *)loc = v;
930 if ((ElfW(Sword))v > 0x7f ||
931 (ElfW(Sword))v < -(ElfW(Sword))0x80)
932 ret = obj_reloc_overflow;
934 *(unsigned char *)loc = v;
937 #elif defined(__i386__)
971 #elif defined(__microblaze__)
972 case R_MICROBLAZE_NONE:
973 case R_MICROBLAZE_64_NONE:
974 case R_MICROBLAZE_32_SYM_OP_SYM:
975 case R_MICROBLAZE_32_PCREL:
978 case R_MICROBLAZE_64_PCREL: {
979 /* dot is the address of the current instruction.
980 * v is the target symbol address.
981 * So we need to extract the offset in the code,
982 * adding v, then subtrating the current address
983 * of this instruction.
984 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
987 /* Get split offset stored in code */
988 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
991 /* Adjust relative offset. -4 adjustment required
992 * because dot points to the IMM insn, but branch
993 * is computed relative to the branch instruction itself.
997 /* Store back into code */
998 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
999 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
1004 case R_MICROBLAZE_32:
1008 case R_MICROBLAZE_64: {
1009 /* Get split pointer stored in code */
1010 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
1013 /* Add reloc offset */
1016 /* Store back into code */
1017 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
1018 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
1023 case R_MICROBLAZE_32_PCREL_LO:
1024 case R_MICROBLAZE_32_LO:
1025 case R_MICROBLAZE_SRO32:
1026 case R_MICROBLAZE_SRW32:
1027 ret = obj_reloc_unhandled;
1030 #elif defined(__mc68000__)
1041 ret = obj_reloc_overflow;
1048 ret = obj_reloc_overflow;
1055 if ((ElfW(Sword))v > 0x7f ||
1056 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1057 ret = obj_reloc_overflow;
1064 if ((ElfW(Sword))v > 0x7fff ||
1065 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1066 ret = obj_reloc_overflow;
1072 *(int *)loc = v - dot;
1075 case R_68K_GLOB_DAT:
1076 case R_68K_JMP_SLOT:
1080 case R_68K_RELATIVE:
1081 *(int *)loc += f->baseaddr;
1087 # ifdef R_68K_GOTOFF
1093 #elif defined(__mips__)
1104 ret = obj_reloc_dangerous;
1105 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1106 ret = obj_reloc_overflow;
1108 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1114 struct mips_hi16 *n;
1116 /* We cannot relocate this one now because we don't know the value
1117 of the carry we need to add. Save the information, and let LO16
1118 do the actual relocation. */
1119 n = xmalloc(sizeof *n);
1122 n->next = ifile->mips_hi16_list;
1123 ifile->mips_hi16_list = n;
1129 unsigned long insnlo = *loc;
1130 ElfW(Addr) val, vallo;
1132 /* Sign extend the addend we extract from the lo insn. */
1133 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1135 if (ifile->mips_hi16_list != NULL) {
1136 struct mips_hi16 *l;
1138 l = ifile->mips_hi16_list;
1140 struct mips_hi16 *next;
1143 /* Do the HI16 relocation. Note that we actually don't
1144 need to know anything about the LO16 itself, except where
1145 to find the low 16 bits of the addend needed by the LO16. */
1148 ((insn & 0xffff) << 16) +
1152 /* Account for the sign extension that will happen in the
1159 insn = (insn & ~0xffff) | val;
1167 ifile->mips_hi16_list = NULL;
1170 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1172 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1177 #elif defined(__nios2__)
1182 case R_NIOS2_BFD_RELOC_32:
1186 case R_NIOS2_BFD_RELOC_16:
1188 ret = obj_reloc_overflow;
1193 case R_NIOS2_BFD_RELOC_8:
1195 ret = obj_reloc_overflow;
1204 if ((Elf32_Sword)v > 0x7fff ||
1205 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1206 ret = obj_reloc_overflow;
1210 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1220 ret = obj_reloc_overflow;
1224 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1229 case R_NIOS2_PCREL16:
1234 if ((Elf32_Sword)v > 0x7fff ||
1235 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1236 ret = obj_reloc_overflow;
1240 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1246 Elf32_Addr word, gp;
1248 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1250 if ((Elf32_Sword)v > 0x7fff ||
1251 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1252 ret = obj_reloc_overflow;
1256 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1260 case R_NIOS2_CALL26:
1262 ret = obj_reloc_dangerous;
1263 if ((v >> 28) != (dot >> 28))
1264 ret = obj_reloc_overflow;
1265 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1273 ret = obj_reloc_overflow;
1276 word = *loc & ~0x7c0;
1277 *loc = word | ((v & 0x1f) << 6);
1286 ret = obj_reloc_overflow;
1289 word = *loc & ~0xfc0;
1290 *loc = word | ((v & 0x3f) << 6);
1299 ret = obj_reloc_overflow;
1302 word = *loc & ~0x3fc0;
1303 *loc = word | ((v & 0xff) << 6);
1312 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1322 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1327 case R_NIOS2_HIADJ16:
1329 Elf32_Addr word1, word2;
1332 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1333 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1338 #elif defined(__powerpc64__)
1339 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1341 #elif defined(__powerpc__)
1343 case R_PPC_ADDR16_HA:
1344 *(unsigned short *)loc = (v + 0x8000) >> 16;
1347 case R_PPC_ADDR16_HI:
1348 *(unsigned short *)loc = v >> 16;
1351 case R_PPC_ADDR16_LO:
1352 *(unsigned short *)loc = v;
1366 #elif defined(__s390__)
1369 *(unsigned int *) loc += v;
1372 *(unsigned short *) loc += v;
1375 *(unsigned char *) loc += v;
1379 *(unsigned int *) loc += v - dot;
1382 *(unsigned short *) loc += (v - dot) >> 1;
1385 *(unsigned short *) loc += v - dot;
1389 case R_390_PLT16DBL:
1390 /* find the plt entry and initialize it. */
1391 pe = (struct arch_single_entry *) &isym->pltent;
1392 if (pe->inited == 0) {
1393 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1394 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1396 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1403 /* Insert relative distance to target. */
1404 v = plt + pe->offset - dot;
1405 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1406 *(unsigned int *) loc = (unsigned int) v;
1407 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1408 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1411 case R_390_GLOB_DAT:
1412 case R_390_JMP_SLOT:
1416 case R_390_RELATIVE:
1417 *loc += f->baseaddr;
1421 *(unsigned long *) loc += got - dot;
1427 if (!isym->gotent.inited)
1429 isym->gotent.inited = 1;
1430 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1432 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1433 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1434 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1435 *(unsigned short *) loc += isym->gotent.offset;
1436 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1437 *(unsigned int *) loc += isym->gotent.offset;
1440 # ifndef R_390_GOTOFF32
1441 # define R_390_GOTOFF32 R_390_GOTOFF
1443 case R_390_GOTOFF32:
1447 #elif defined(__sh__)
1470 *loc = f->baseaddr + rel->r_addend;
1474 *loc = got - dot + rel->r_addend;
1484 # if defined(__SH5__)
1485 case R_SH_IMM_MEDLOW16:
1486 case R_SH_IMM_LOW16:
1490 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1494 * movi and shori have the format:
1496 * | op | imm | reg | reserved |
1497 * 31..26 25..10 9.. 4 3 .. 0
1499 * so we simply mask and or in imm.
1501 word = *loc & ~0x3fffc00;
1502 word |= (v & 0xffff) << 10;
1509 case R_SH_IMM_MEDLOW16_PCREL:
1510 case R_SH_IMM_LOW16_PCREL:
1514 word = *loc & ~0x3fffc00;
1518 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1521 word |= (v & 0xffff) << 10;
1527 # endif /* __SH5__ */
1529 #elif defined(__v850e__)
1535 /* We write two shorts instead of a long because even
1536 32-bit insns only need half-word alignment, but
1537 32-bit data needs to be long-word aligned. */
1538 v += ((unsigned short *)loc)[0];
1539 v += ((unsigned short *)loc)[1] << 16;
1540 ((unsigned short *)loc)[0] = v & 0xffff;
1541 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1544 case R_V850_22_PCREL:
1547 #elif defined(__x86_64__)
1557 *(unsigned int *) loc += v;
1560 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1561 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1566 *(signed int *) loc += v;
1570 *(unsigned short *) loc += v;
1574 *(unsigned char *) loc += v;
1578 *(unsigned int *) loc += v - dot;
1582 *(unsigned short *) loc += v - dot;
1586 *(unsigned char *) loc += v - dot;
1589 case R_X86_64_GLOB_DAT:
1590 case R_X86_64_JUMP_SLOT:
1594 case R_X86_64_RELATIVE:
1595 *loc += f->baseaddr;
1598 case R_X86_64_GOT32:
1599 case R_X86_64_GOTPCREL:
1602 if (!isym->gotent.reloc_done)
1604 isym->gotent.reloc_done = 1;
1605 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1607 /* XXX are these really correct? */
1608 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1609 *(unsigned int *) loc += v + isym->gotent.offset;
1611 *loc += isym->gotent.offset;
1616 # warning "no idea how to handle relocations on your arch"
1620 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1621 ret = obj_reloc_unhandled;
1624 #if defined(USE_PLT_ENTRIES)
1628 /* find the plt entry and initialize it if necessary */
1630 #if defined(USE_PLT_LIST)
1631 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1638 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1640 /* generate some machine code */
1642 #if defined(__arm__)
1643 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1644 ip[1] = v; /* sym@ */
1646 #if defined(__powerpc__)
1647 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1648 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1649 ip[2] = 0x7d6903a6; /* mtctr r11 */
1650 ip[3] = 0x4e800420; /* bctr */
1652 #if defined(__v850e__)
1653 /* We have to trash a register, so we assume that any control
1654 transfer more than 21-bits away must be a function call
1655 (so we can use a call-clobbered register). */
1656 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1657 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1662 /* relative distance to target */
1664 /* if the target is too far away.... */
1665 #if defined(__arm__) || defined(__powerpc__)
1666 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1667 #elif defined(__v850e__)
1668 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1670 /* go via the plt */
1671 v = plt + pe->offset - dot;
1673 #if defined(__v850e__)
1678 ret = obj_reloc_dangerous;
1680 /* merge the offset into the instruction. */
1681 #if defined(__arm__)
1682 /* Convert to words. */
1685 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1687 #if defined(__powerpc__)
1688 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1690 #if defined(__v850e__)
1691 /* We write two shorts instead of a long because even 32-bit insns
1692 only need half-word alignment, but the 32-bit data write needs
1693 to be long-word aligned. */
1694 ((unsigned short *)loc)[0] =
1695 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1696 | ((v >> 16) & 0x3f); /* offs high part */
1697 ((unsigned short *)loc)[1] =
1698 (v & 0xffff); /* offs low part */
1701 #endif /* USE_PLT_ENTRIES */
1703 #if defined(USE_GOT_ENTRIES)
1706 /* needs an entry in the .got: set it, once */
1707 if (!isym->gotent.inited) {
1708 isym->gotent.inited = 1;
1709 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1711 /* make the reloc with_respect_to_.got */
1713 *loc += isym->gotent.offset + rel->r_addend;
1714 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1715 *loc += isym->gotent.offset;
1719 #endif /* USE_GOT_ENTRIES */
1726 #if defined(USE_LIST)
1728 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1729 int offset, int size)
1731 struct arch_list_entry *pe;
1733 for (pe = *list; pe != NULL; pe = pe->next) {
1734 if (pe->addend == rel->r_addend) {
1740 pe = xmalloc(sizeof(struct arch_list_entry));
1742 pe->addend = rel->r_addend;
1743 pe->offset = offset;
1753 #if defined(USE_SINGLE)
1755 static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
1756 int offset, int size)
1758 if (single->allocated == 0) {
1759 single->allocated = 1;
1760 single->offset = offset;
1769 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1771 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
1772 int offset, int size)
1774 struct obj_section *myrelsec = obj_find_section(f, name);
1781 obj_extend_section(myrelsec, offset);
1783 myrelsec = obj_create_alloced_section(f, name,
1792 static void arch_create_got(struct obj_file *f)
1794 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
1795 struct arch_file *ifile = (struct arch_file *) f;
1797 #if defined(USE_GOT_ENTRIES)
1798 int got_offset = 0, got_needed = 0, got_allocate;
1800 #if defined(USE_PLT_ENTRIES)
1801 int plt_offset = 0, plt_needed = 0, plt_allocate;
1803 struct obj_section *relsec, *symsec, *strsec;
1804 ElfW(RelM) *rel, *relend;
1805 ElfW(Sym) *symtab, *extsym;
1806 const char *strtab, *name;
1807 struct arch_symbol *intsym;
1809 for (i = 0; i < f->header.e_shnum; ++i) {
1810 relsec = f->sections[i];
1811 if (relsec->header.sh_type != SHT_RELM)
1814 symsec = f->sections[relsec->header.sh_link];
1815 strsec = f->sections[symsec->header.sh_link];
1817 rel = (ElfW(RelM) *) relsec->contents;
1818 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1819 symtab = (ElfW(Sym) *) symsec->contents;
1820 strtab = (const char *) strsec->contents;
1822 for (; rel < relend; ++rel) {
1823 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1825 #if defined(USE_GOT_ENTRIES)
1828 #if defined(USE_PLT_ENTRIES)
1832 switch (ELF_R_TYPE(rel->r_info)) {
1833 #if defined(__arm__)
1848 #elif defined(__i386__)
1858 #elif defined(__powerpc__)
1863 #elif defined(__mc68000__)
1874 #elif defined(__sh__)
1884 #elif defined(__v850e__)
1885 case R_V850_22_PCREL:
1894 if (extsym->st_name != 0) {
1895 name = strtab + extsym->st_name;
1897 name = f->sections[extsym->st_shndx]->name;
1899 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1900 #if defined(USE_GOT_ENTRIES)
1902 got_offset += arch_single_init(
1903 /*rel,*/ &intsym->gotent,
1904 got_offset, GOT_ENTRY_SIZE);
1909 #if defined(USE_PLT_ENTRIES)
1911 #if defined(USE_PLT_LIST)
1912 plt_offset += arch_list_add(
1913 rel, &intsym->pltent,
1914 plt_offset, PLT_ENTRY_SIZE);
1916 plt_offset += arch_single_init(
1917 /*rel,*/ &intsym->pltent,
1918 plt_offset, PLT_ENTRY_SIZE);
1926 #if defined(USE_GOT_ENTRIES)
1928 ifile->got = arch_xsect_init(f, ".got", got_offset,
1933 #if defined(USE_PLT_ENTRIES)
1935 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1940 #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
1943 /*======================================================================*/
1945 /* Standard ELF hash function. */
1946 static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1948 unsigned long h = 0;
1955 g = (h & 0xf0000000);
1965 static unsigned long obj_elf_hash(const char *name)
1967 return obj_elf_hash_n(name, strlen(name));
1970 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
1971 /* String comparison for non-co-versioned kernel and module. */
1973 static int ncv_strcmp(const char *a, const char *b)
1975 size_t alen = strlen(a), blen = strlen(b);
1977 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1978 return strncmp(a, b, alen);
1979 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1980 return strncmp(a, b, blen);
1982 return strcmp(a, b);
1985 /* String hashing for non-co-versioned kernel and module. Here
1986 we are simply forced to drop the crc from the hash. */
1988 static unsigned long ncv_symbol_hash(const char *str)
1990 size_t len = strlen(str);
1991 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1993 return obj_elf_hash_n(str, len);
1997 obj_set_symbol_compare(struct obj_file *f,
1998 int (*cmp) (const char *, const char *),
1999 unsigned long (*hash) (const char *))
2002 f->symbol_cmp = cmp;
2004 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
2007 f->symbol_hash = hash;
2009 memcpy(tmptab, f->symtab, sizeof(tmptab));
2010 memset(f->symtab, 0, sizeof(f->symtab));
2012 for (i = 0; i < HASH_BUCKETS; ++i)
2013 for (sym = tmptab[i]; sym; sym = next) {
2014 unsigned long h = hash(sym->name) % HASH_BUCKETS;
2016 sym->next = f->symtab[h];
2022 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2024 static struct obj_symbol *
2025 obj_add_symbol(struct obj_file *f, const char *name,
2026 unsigned long symidx, int info,
2027 int secidx, ElfW(Addr) value,
2030 struct obj_symbol *sym;
2031 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2032 int n_type = ELF_ST_TYPE(info);
2033 int n_binding = ELF_ST_BIND(info);
2035 for (sym = f->symtab[hash]; sym; sym = sym->next)
2036 if (f->symbol_cmp(sym->name, name) == 0) {
2037 int o_secidx = sym->secidx;
2038 int o_info = sym->info;
2039 int o_type = ELF_ST_TYPE(o_info);
2040 int o_binding = ELF_ST_BIND(o_info);
2042 /* A redefinition! Is it legal? */
2044 if (secidx == SHN_UNDEF)
2046 else if (o_secidx == SHN_UNDEF)
2048 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2049 /* Cope with local and global symbols of the same name
2050 in the same object file, as might have been created
2051 by ld -r. The only reason locals are now seen at this
2052 level at all is so that we can do semi-sensible things
2055 struct obj_symbol *nsym, **p;
2057 nsym = arch_new_symbol();
2058 nsym->next = sym->next;
2061 /* Excise the old (local) symbol from the hash chain. */
2062 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2066 } else if (n_binding == STB_LOCAL) {
2067 /* Another symbol of the same name has already been defined.
2068 Just add this to the local table. */
2069 sym = arch_new_symbol();
2072 f->local_symtab[symidx] = sym;
2074 } else if (n_binding == STB_WEAK)
2076 else if (o_binding == STB_WEAK)
2078 /* Don't unify COMMON symbols with object types the programmer
2080 else if (secidx == SHN_COMMON
2081 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2083 else if (o_secidx == SHN_COMMON
2084 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2087 /* Don't report an error if the symbol is coming from
2088 the kernel or some external module. */
2089 if (secidx <= SHN_HIRESERVE)
2090 bb_error_msg("%s multiply defined", name);
2095 /* Completely new symbol. */
2096 sym = arch_new_symbol();
2097 sym->next = f->symtab[hash];
2098 f->symtab[hash] = sym;
2101 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2102 if (symidx >= f->local_symtab_size)
2103 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2104 name, (long) symidx, (long) f->local_symtab_size);
2106 f->local_symtab[symidx] = sym;
2113 sym->secidx = secidx;
2119 static struct obj_symbol *
2120 obj_find_symbol(struct obj_file *f, const char *name)
2122 struct obj_symbol *sym;
2123 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2125 for (sym = f->symtab[hash]; sym; sym = sym->next)
2126 if (f->symbol_cmp(sym->name, name) == 0)
2132 static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2135 if (sym->secidx >= SHN_LORESERVE)
2138 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2140 /* As a special case, a NULL sym has value zero. */
2145 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2147 int i, n = f->header.e_shnum;
2149 for (i = 0; i < n; ++i)
2150 if (strcmp(f->sections[i]->name, name) == 0)
2151 return f->sections[i];
2156 static int obj_load_order_prio(struct obj_section *a)
2158 unsigned long af, ac;
2160 af = a->header.sh_flags;
2163 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2164 strcmp(a->name + 5, ".init"))
2168 if (!(af & SHF_WRITE))
2170 if (af & SHF_EXECINSTR)
2172 if (a->header.sh_type != SHT_NOBITS)
2179 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2181 struct obj_section **p;
2182 int prio = obj_load_order_prio(sec);
2183 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2184 if (obj_load_order_prio(*p) < prio)
2186 sec->load_next = *p;
2190 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2192 unsigned long align,
2195 int newidx = f->header.e_shnum++;
2196 struct obj_section *sec;
2198 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2199 f->sections[newidx] = sec = arch_new_section();
2201 sec->header.sh_type = SHT_PROGBITS;
2202 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2203 sec->header.sh_size = size;
2204 sec->header.sh_addralign = align;
2208 sec->contents = xmalloc(size);
2210 obj_insert_section_load_order(f, sec);
2215 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2217 unsigned long align,
2220 int newidx = f->header.e_shnum++;
2221 struct obj_section *sec;
2223 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2224 f->sections[newidx] = sec = arch_new_section();
2226 sec->header.sh_type = SHT_PROGBITS;
2227 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2228 sec->header.sh_size = size;
2229 sec->header.sh_addralign = align;
2233 sec->contents = xmalloc(size);
2235 sec->load_next = f->load_order;
2236 f->load_order = sec;
2237 if (f->load_order_search_start == &f->load_order)
2238 f->load_order_search_start = &sec->load_next;
2243 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2245 unsigned long oldsize = sec->header.sh_size;
2247 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2249 return sec->contents + oldsize;
2253 /* Conditionally add the symbols from the given symbol set to the
2257 add_symbols_from( struct obj_file *f,
2258 int idx, struct new_module_symbol *syms, size_t nsyms)
2260 struct new_module_symbol *s;
2263 #ifdef SYMBOL_PREFIX
2265 size_t name_alloced_size = 0;
2267 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2270 gpl = obj_gpl_license(f, NULL) == 0;
2272 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2273 /* Only add symbols that are already marked external.
2274 If we override locals we may cause problems for
2275 argument initialization. We will also create a false
2276 dependency on the module. */
2277 struct obj_symbol *sym;
2280 /* GPL licensed modules can use symbols exported with
2281 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2282 * exported names. Non-GPL modules never see any GPLONLY_
2283 * symbols so they cannot fudge it by adding the prefix on
2286 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2287 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
2294 name = (char *)s->name;
2296 #ifdef SYMBOL_PREFIX
2297 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2298 kernel exports `C names', but module object files
2299 reference `linker names'). */
2300 size_t extra = sizeof SYMBOL_PREFIX;
2301 size_t name_size = strlen(name) + extra;
2302 if (name_size > name_alloced_size) {
2303 name_alloced_size = name_size * 2;
2304 name_buf = alloca(name_alloced_size);
2306 strcpy(name_buf, SYMBOL_PREFIX);
2307 strcpy(name_buf + extra - 1, name);
2309 #endif /* SYMBOL_PREFIX */
2311 sym = obj_find_symbol(f, name);
2312 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2313 #ifdef SYMBOL_PREFIX
2314 /* Put NAME_BUF into more permanent storage. */
2315 name = xmalloc(name_size);
2316 strcpy(name, name_buf);
2318 sym = obj_add_symbol(f, name, -1,
2319 ELF_ST_INFO(STB_GLOBAL,
2322 /* Did our symbol just get installed? If so, mark the
2323 module as "used". */
2324 if (sym->secidx == idx)
2332 static void add_kernel_symbols(struct obj_file *f)
2334 struct external_module *m;
2337 /* Add module symbols first. */
2339 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
2341 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
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;
2403 key = alloca(q - p + 6);
2404 memcpy(key, "parm_", 5);
2405 memcpy(key + 5, p, q - p);
2408 p = get_modinfo_value(f, key);
2411 bb_error_msg_and_die("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_and_die("symbol for parameter %s not found", key);
2429 min = strtoul(p, &p, 10);
2431 max = strtoul(p + 1, &p, 10);
2437 contents = f->sections[sym->secidx]->contents;
2438 loc = contents + sym->value;
2442 if ((*p == 's') || (*p == 'c')) {
2445 /* Do C quoting if we begin with a ", else slurp the lot. */
2449 str = alloca(strlen(q));
2450 for (r = str, q++; *q != '"'; ++q, ++r) {
2452 bb_error_msg_and_die("improperly terminated string argument for %s",
2488 if (q[1] >= '0' && q[1] <= '7') {
2489 c = (c * 8) + *++q - '0';
2490 if (q[1] >= '0' && q[1] <= '7')
2491 c = (c * 8) + *++q - '0';
2509 /* In this case, the string is not quoted. We will break
2510 it using the coma (like for ints). If the user wants to
2511 include comas in a string, he just has to quote it */
2513 /* Search the next coma */
2517 if (r != (char *) NULL) {
2518 /* Recopy the current field */
2519 str = alloca(r - q + 1);
2520 memcpy(str, q, r - q);
2522 /* I don't know if it is useful, as the previous case
2523 doesn't nul terminate the string ??? */
2526 /* Keep next fields */
2537 obj_string_patch(f, sym->secidx, loc - contents, str);
2538 loc += tgt_sizeof_char_p;
2540 /* Array of chars (in fact, matrix!) */
2541 unsigned long charssize; /* size of each member */
2543 /* Get the size of each member */
2544 /* Probably we should do that outside the loop ? */
2545 if (!isdigit(*(p + 1))) {
2546 bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
2547 " the maximum size", key);
2549 charssize = strtoul(p + 1, (char **) NULL, 10);
2552 if (strlen(str) >= charssize) {
2553 bb_error_msg_and_die("string too long for %s (max %ld)", key,
2557 /* Copy to location */
2558 strcpy((char *) loc, str);
2562 long v = strtoul(q, &q, 0);
2569 loc += tgt_sizeof_short;
2573 loc += tgt_sizeof_int;
2577 loc += tgt_sizeof_long;
2581 bb_error_msg_and_die("unknown parameter type '%c' for %s", *p, key);
2594 goto retry_end_of_value;
2598 bb_error_msg_and_die("too many values for %s (max %d)", key, max);
2604 bb_error_msg_and_die("invalid argument syntax for %s", key);
2609 bb_error_msg_and_die("too few values for %s (min %d)", key, min);
2617 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
2618 static int new_is_module_checksummed(struct obj_file *f)
2620 const char *p = get_modinfo_value(f, "using_checksums");
2626 /* Get the module's kernel version in the canonical integer form. */
2629 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2634 p = get_modinfo_value(f, "kernel_version");
2637 safe_strncpy(str, p, STRVERSIONLEN);
2639 a = strtoul(p, &p, 10);
2642 b = strtoul(p + 1, &p, 10);
2645 c = strtoul(p + 1, &q, 10);
2649 return a << 16 | b << 8 | c;
2652 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
2655 /* Fetch the loaded modules, and all currently exported symbols. */
2657 static void new_get_kernel_symbols(void)
2659 char *module_names, *mn;
2660 struct external_module *modules, *m;
2661 struct new_module_symbol *syms, *s;
2662 size_t ret, bufsize, nmod, nsyms, i, j;
2664 /* Collect the loaded modules. */
2667 module_names = xmalloc(bufsize);
2670 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2671 if (errno == ENOSPC && bufsize < ret) {
2673 module_names = xrealloc(module_names, bufsize);
2674 goto retry_modules_load;
2676 bb_perror_msg_and_die("QM_MODULES");
2679 n_ext_modules = nmod = ret;
2681 /* Collect the modules' symbols. */
2684 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2685 memset(modules, 0, nmod * sizeof(*modules));
2686 for (i = 0, mn = module_names, m = modules;
2687 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2688 struct new_module_info info;
2690 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2691 if (errno == ENOENT) {
2692 /* The module was removed out from underneath us. */
2695 bb_perror_msg_and_die("query_module: QM_INFO: %s", mn);
2699 syms = xmalloc(bufsize);
2701 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2705 syms = xrealloc(syms, bufsize);
2706 goto retry_mod_sym_load;
2708 /* The module was removed out from underneath us. */
2711 bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn);
2717 m->addr = info.addr;
2721 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2722 s->name += (unsigned long) syms;
2727 /* Collect the kernel's symbols. */
2729 syms = xmalloc(bufsize = 16 * 1024);
2730 retry_kern_sym_load:
2731 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2732 if (errno == ENOSPC && bufsize < ret) {
2733 syms = xrealloc(syms, bufsize = ret);
2734 goto retry_kern_sym_load;
2736 bb_perror_msg_and_die("kernel: QM_SYMBOLS");
2738 nksyms = nsyms = ret;
2741 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2742 s->name += (unsigned long) syms;
2747 /* Return the kernel symbol checksum version, or zero if not used. */
2749 static int new_is_kernel_checksummed(void)
2751 struct new_module_symbol *s;
2754 /* Using_Versions is not the first symbol, but it should be in there. */
2756 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2757 if (strcmp((char *) s->name, "Using_Versions") == 0)
2764 static void new_create_this_module(struct obj_file *f, const char *m_name)
2766 struct obj_section *sec;
2768 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2769 sizeof(struct new_module));
2770 memset(sec->contents, 0, sizeof(struct new_module));
2772 obj_add_symbol(f, SPFX "__this_module", -1,
2773 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2774 sizeof(struct new_module));
2776 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2780 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2781 /* add an entry to the __ksymtab section, creating it if necessary */
2782 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2784 struct obj_section *sec;
2787 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2788 * If __ksymtab is defined but not marked alloc, x out the first character
2789 * (no obj_delete routine) and create a new __ksymtab with the correct
2792 sec = obj_find_section(f, "__ksymtab");
2793 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2794 *((char *)(sec->name)) = 'x'; /* override const */
2798 sec = obj_create_alloced_section(f, "__ksymtab",
2799 tgt_sizeof_void_p, 0);
2802 sec->header.sh_flags |= SHF_ALLOC;
2803 /* Empty section might be byte-aligned */
2804 sec->header.sh_addralign = tgt_sizeof_void_p;
2805 ofs = sec->header.sh_size;
2806 obj_symbol_patch(f, sec->idx, ofs, sym);
2807 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2808 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2810 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2812 static int new_create_module_ksymtab(struct obj_file *f)
2814 struct obj_section *sec;
2817 /* We must always add the module references. */
2819 if (n_ext_modules_used) {
2820 struct new_module_ref *dep;
2821 struct obj_symbol *tm;
2823 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2824 (sizeof(struct new_module_ref)
2825 * n_ext_modules_used));
2829 tm = obj_find_symbol(f, SPFX "__this_module");
2830 dep = (struct new_module_ref *) sec->contents;
2831 for (i = 0; i < n_ext_modules; ++i)
2832 if (ext_modules[i].used) {
2833 dep->dep = ext_modules[i].addr;
2834 obj_symbol_patch(f, sec->idx,
2835 (char *) &dep->ref - sec->contents, tm);
2841 if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
2845 sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
2847 /* We don't want to export symbols residing in sections that
2848 aren't loaded. There are a number of these created so that
2849 we make sure certain module options don't appear twice. */
2851 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2853 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2855 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2856 struct obj_symbol *sym;
2857 for (sym = f->symtab[i]; sym; sym = sym->next)
2858 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2859 && sym->secidx <= SHN_HIRESERVE
2860 && (sym->secidx >= SHN_LORESERVE
2861 || loaded[sym->secidx])) {
2862 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2864 obj_symbol_patch(f, sec->idx, ofs, sym);
2865 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2872 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2880 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2882 struct new_module *module;
2883 struct obj_section *sec;
2888 sec = obj_find_section(f, ".this");
2889 if (!sec || !sec->contents) {
2890 bb_perror_msg_and_die("corrupt module %s?",m_name);
2892 module = (struct new_module *) sec->contents;
2893 m_addr = sec->header.sh_addr;
2895 module->size_of_struct = sizeof(*module);
2896 module->size = m_size;
2897 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2899 sec = obj_find_section(f, "__ksymtab");
2900 if (sec && sec->header.sh_size) {
2901 module->syms = sec->header.sh_addr;
2902 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2905 if (n_ext_modules_used) {
2906 sec = obj_find_section(f, ".kmodtab");
2907 module->deps = sec->header.sh_addr;
2908 module->ndeps = n_ext_modules_used;
2912 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2914 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2916 sec = obj_find_section(f, "__ex_table");
2918 module->ex_table_start = sec->header.sh_addr;
2919 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2922 sec = obj_find_section(f, ".text.init");
2924 module->runsize = sec->header.sh_addr - m_addr;
2926 sec = obj_find_section(f, ".data.init");
2928 if (!module->runsize ||
2929 module->runsize > sec->header.sh_addr - m_addr)
2930 module->runsize = sec->header.sh_addr - m_addr;
2932 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2933 if (sec && sec->header.sh_size) {
2934 module->archdata_start = (void*)sec->header.sh_addr;
2935 module->archdata_end = module->archdata_start + sec->header.sh_size;
2937 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2938 if (sec && sec->header.sh_size) {
2939 module->kallsyms_start = (void*)sec->header.sh_addr;
2940 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2943 /* Whew! All of the initialization is complete. Collect the final
2944 module image and give it to the kernel. */
2946 image = xmalloc(m_size);
2947 obj_create_image(f, image);
2949 ret = init_module(m_name, (struct new_module *) image);
2951 bb_perror_msg("init_module: %s", m_name);
2959 /*======================================================================*/
2962 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2965 struct obj_string_patch *p;
2966 struct obj_section *strsec;
2967 size_t len = strlen(string) + 1;
2970 p = xmalloc(sizeof(*p));
2971 p->next = f->string_patches;
2972 p->reloc_secidx = secidx;
2973 p->reloc_offset = offset;
2974 f->string_patches = p;
2976 strsec = obj_find_section(f, ".kstrtab");
2977 if (strsec == NULL) {
2978 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2979 p->string_offset = 0;
2980 loc = strsec->contents;
2982 p->string_offset = strsec->header.sh_size;
2983 loc = obj_extend_section(strsec, len);
2985 memcpy(loc, string, len);
2989 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2990 struct obj_symbol *sym)
2992 struct obj_symbol_patch *p;
2994 p = xmalloc(sizeof(*p));
2995 p->next = f->symbol_patches;
2996 p->reloc_secidx = secidx;
2997 p->reloc_offset = offset;
2999 f->symbol_patches = p;
3002 static void obj_check_undefineds(struct obj_file *f)
3006 for (i = 0; i < HASH_BUCKETS; ++i) {
3007 struct obj_symbol *sym;
3008 for (sym = f->symtab[i]; sym; sym = sym->next)
3009 if (sym->secidx == SHN_UNDEF) {
3010 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3011 sym->secidx = SHN_ABS;
3015 bb_error_msg_and_die("unresolved symbol %s", sym->name);
3021 static void obj_allocate_commons(struct obj_file *f)
3023 struct common_entry {
3024 struct common_entry *next;
3025 struct obj_symbol *sym;
3026 } *common_head = NULL;
3030 for (i = 0; i < HASH_BUCKETS; ++i) {
3031 struct obj_symbol *sym;
3032 for (sym = f->symtab[i]; sym; sym = sym->next)
3033 if (sym->secidx == SHN_COMMON) {
3034 /* Collect all COMMON symbols and sort them by size so as to
3035 minimize space wasted by alignment requirements. */
3037 struct common_entry **p, *n;
3038 for (p = &common_head; *p; p = &(*p)->next)
3039 if (sym->size <= (*p)->sym->size)
3042 n = alloca(sizeof(*n));
3050 for (i = 1; i < f->local_symtab_size; ++i) {
3051 struct obj_symbol *sym = f->local_symtab[i];
3052 if (sym && sym->secidx == SHN_COMMON) {
3053 struct common_entry **p, *n;
3054 for (p = &common_head; *p; p = &(*p)->next)
3055 if (sym == (*p)->sym)
3057 else if (sym->size < (*p)->sym->size) {
3058 n = alloca(sizeof(*n));
3068 /* Find the bss section. */
3069 for (i = 0; i < f->header.e_shnum; ++i)
3070 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3073 /* If for some reason there hadn't been one, create one. */
3074 if (i == f->header.e_shnum) {
3075 struct obj_section *sec;
3077 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3078 f->sections[i] = sec = arch_new_section();
3079 f->header.e_shnum = i + 1;
3081 sec->header.sh_type = SHT_PROGBITS;
3082 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3087 /* Allocate the COMMONS. */
3089 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3090 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3091 struct common_entry *c;
3093 for (c = common_head; c; c = c->next) {
3094 ElfW(Addr) align = c->sym->value;
3096 if (align > max_align)
3098 if (bss_size & (align - 1))
3099 bss_size = (bss_size | (align - 1)) + 1;
3102 c->sym->value = bss_size;
3104 bss_size += c->sym->size;
3107 f->sections[i]->header.sh_size = bss_size;
3108 f->sections[i]->header.sh_addralign = max_align;
3112 /* For the sake of patch relocation and parameter initialization,
3113 allocate zeroed data for NOBITS sections now. Note that after
3114 this we cannot assume NOBITS are really empty. */
3115 for (i = 0; i < f->header.e_shnum; ++i) {
3116 struct obj_section *s = f->sections[i];
3117 if (s->header.sh_type == SHT_NOBITS) {
3118 if (s->header.sh_size != 0)
3119 s->contents = memset(xmalloc(s->header.sh_size),
3120 0, s->header.sh_size);
3124 s->header.sh_type = SHT_PROGBITS;
3129 static unsigned long obj_load_size(struct obj_file *f)
3131 unsigned long dot = 0;
3132 struct obj_section *sec;
3134 /* Finalize the positions of the sections relative to one another. */
3136 for (sec = f->load_order; sec; sec = sec->load_next) {
3139 align = sec->header.sh_addralign;
3140 if (align && (dot & (align - 1)))
3141 dot = (dot | (align - 1)) + 1;
3143 sec->header.sh_addr = dot;
3144 dot += sec->header.sh_size;
3150 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3152 int i, n = f->header.e_shnum;
3155 /* Finalize the addresses of the sections. */
3158 for (i = 0; i < n; ++i)
3159 f->sections[i]->header.sh_addr += base;
3161 /* And iterate over all of the relocations. */
3163 for (i = 0; i < n; ++i) {
3164 struct obj_section *relsec, *symsec, *targsec, *strsec;
3165 ElfW(RelM) * rel, *relend;
3169 relsec = f->sections[i];
3170 if (relsec->header.sh_type != SHT_RELM)
3173 symsec = f->sections[relsec->header.sh_link];
3174 targsec = f->sections[relsec->header.sh_info];
3175 strsec = f->sections[symsec->header.sh_link];
3177 rel = (ElfW(RelM) *) relsec->contents;
3178 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3179 symtab = (ElfW(Sym) *) symsec->contents;
3180 strtab = (const char *) strsec->contents;
3182 for (; rel < relend; ++rel) {
3183 ElfW(Addr) value = 0;
3184 struct obj_symbol *intsym = NULL;
3185 unsigned long symndx;
3186 ElfW(Sym) * extsym = 0;
3189 /* Attempt to find a value to use for this relocation. */
3191 symndx = ELF_R_SYM(rel->r_info);
3193 /* Note we've already checked for undefined symbols. */
3195 extsym = &symtab[symndx];
3196 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3197 /* Local symbols we look up in the local table to be sure
3198 we get the one that is really intended. */
3199 intsym = f->local_symtab[symndx];
3201 /* Others we look up in the hash table. */
3203 if (extsym->st_name)
3204 name = strtab + extsym->st_name;
3206 name = f->sections[extsym->st_shndx]->name;
3207 intsym = obj_find_symbol(f, name);
3210 value = obj_symbol_final_value(f, intsym);
3211 intsym->referenced = 1;
3213 #if SHT_RELM == SHT_RELA
3214 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3215 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3216 if (!extsym || !extsym->st_name ||
3217 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3219 value += rel->r_addend;
3223 switch (arch_apply_relocation
3224 (f, targsec, /*symsec,*/ intsym, rel, value)
3229 case obj_reloc_overflow:
3230 errmsg = "Relocation overflow";
3232 case obj_reloc_dangerous:
3233 errmsg = "Dangerous relocation";
3235 case obj_reloc_unhandled:
3236 errmsg = "Unhandled relocation";
3239 bb_error_msg("%s of type %ld for %s", errmsg,
3240 (long) ELF_R_TYPE(rel->r_info),
3241 strtab + extsym->st_name);
3243 bb_error_msg("%s of type %ld", errmsg,
3244 (long) ELF_R_TYPE(rel->r_info));
3252 /* Finally, take care of the patches. */
3254 if (f->string_patches) {
3255 struct obj_string_patch *p;
3256 struct obj_section *strsec;
3257 ElfW(Addr) strsec_base;
3258 strsec = obj_find_section(f, ".kstrtab");
3259 strsec_base = strsec->header.sh_addr;
3261 for (p = f->string_patches; p; p = p->next) {
3262 struct obj_section *targsec = f->sections[p->reloc_secidx];
3263 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3264 = strsec_base + p->string_offset;
3268 if (f->symbol_patches) {
3269 struct obj_symbol_patch *p;
3271 for (p = f->symbol_patches; p; p = p->next) {
3272 struct obj_section *targsec = f->sections[p->reloc_secidx];
3273 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3274 = obj_symbol_final_value(f, p->sym);
3281 static int obj_create_image(struct obj_file *f, char *image)
3283 struct obj_section *sec;
3284 ElfW(Addr) base = f->baseaddr;
3286 for (sec = f->load_order; sec; sec = sec->load_next) {
3289 if (sec->contents == 0 || sec->header.sh_size == 0)
3292 secimg = image + (sec->header.sh_addr - base);
3294 /* Note that we allocated data for NOBITS sections earlier. */
3295 memcpy(secimg, sec->contents, sec->header.sh_size);
3301 /*======================================================================*/
3303 static struct obj_file *obj_load(FILE * fp, int loadprogbits ATTRIBUTE_UNUSED)
3306 ElfW(Shdr) * section_headers;
3310 /* Read the file header. */
3312 f = arch_new_file();
3313 f->symbol_cmp = strcmp;
3314 f->symbol_hash = obj_elf_hash;
3315 f->load_order_search_start = &f->load_order;
3317 fseek(fp, 0, SEEK_SET);
3318 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3319 bb_perror_msg_and_die("error reading ELF header");
3322 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3323 || f->header.e_ident[EI_MAG1] != ELFMAG1
3324 || f->header.e_ident[EI_MAG2] != ELFMAG2
3325 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3326 bb_error_msg_and_die("not an ELF file");
3328 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3329 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3330 ? ELFDATA2MSB : ELFDATA2LSB)
3331 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3332 || !MATCH_MACHINE(f->header.e_machine)) {
3333 bb_error_msg_and_die("ELF file not for this architecture");
3335 if (f->header.e_type != ET_REL) {
3336 bb_error_msg_and_die("ELF file not a relocatable object");
3339 /* Read the section headers. */
3341 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3342 bb_error_msg_and_die("section header size mismatch: %lu != %lu",
3343 (unsigned long) f->header.e_shentsize,
3344 (unsigned long) sizeof(ElfW(Shdr)));
3347 shnum = f->header.e_shnum;
3348 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3349 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3351 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3352 fseek(fp, f->header.e_shoff, SEEK_SET);
3353 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3354 bb_perror_msg_and_die("error reading ELF section headers");
3357 /* Read the section data. */
3359 for (i = 0; i < shnum; ++i) {
3360 struct obj_section *sec;
3362 f->sections[i] = sec = arch_new_section();
3364 sec->header = section_headers[i];
3367 if (sec->header.sh_size) {
3368 switch (sec->header.sh_type) {
3377 if (!loadprogbits) {
3378 sec->contents = NULL;
3385 if (sec->header.sh_size > 0) {
3386 sec->contents = xmalloc(sec->header.sh_size);
3387 fseek(fp, sec->header.sh_offset, SEEK_SET);
3388 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3389 bb_perror_msg_and_die("error reading ELF section data");
3392 sec->contents = NULL;
3396 #if SHT_RELM == SHT_REL
3398 bb_error_msg_and_die("RELA relocations not supported on this architecture");
3401 bb_error_msg_and_die("REL relocations not supported on this architecture");
3404 if (sec->header.sh_type >= SHT_LOPROC) {
3405 /* Assume processor specific section types are debug
3406 info and can safely be ignored. If this is ever not
3407 the case (Hello MIPS?), don't put ifdefs here but
3408 create an arch_load_proc_section(). */
3412 bb_error_msg_and_die("can't handle sections of type %ld",
3413 (long) sec->header.sh_type);
3418 /* Do what sort of interpretation as needed by each section. */
3420 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3422 for (i = 0; i < shnum; ++i) {
3423 struct obj_section *sec = f->sections[i];
3424 sec->name = shstrtab + sec->header.sh_name;
3427 for (i = 0; i < shnum; ++i) {
3428 struct obj_section *sec = f->sections[i];
3430 /* .modinfo should be contents only but gcc has no attribute for that.
3431 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3433 if (strcmp(sec->name, ".modinfo") == 0)
3434 sec->header.sh_flags &= ~SHF_ALLOC;
3436 if (sec->header.sh_flags & SHF_ALLOC)
3437 obj_insert_section_load_order(f, sec);
3439 switch (sec->header.sh_type) {
3442 unsigned long nsym, j;
3446 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3447 bb_error_msg_and_die("symbol size mismatch: %lu != %lu",
3448 (unsigned long) sec->header.sh_entsize,
3449 (unsigned long) sizeof(ElfW(Sym)));
3452 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3453 strtab = f->sections[sec->header.sh_link]->contents;
3454 sym = (ElfW(Sym) *) sec->contents;
3456 /* Allocate space for a table of local symbols. */
3457 j = f->local_symtab_size = sec->header.sh_info;
3458 f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
3460 /* Insert all symbols into the hash table. */
3461 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3462 ElfW(Addr) val = sym->st_value;
3465 name = strtab + sym->st_name;
3466 else if (sym->st_shndx < shnum)
3467 name = f->sections[sym->st_shndx]->name;
3470 #if defined(__SH5__)
3472 * For sh64 it is possible that the target of a branch
3473 * requires a mode switch (32 to 16 and back again).
3475 * This is implied by the lsb being set in the target
3476 * address for SHmedia mode and clear for SHcompact.
3478 val |= sym->st_other & 4;
3480 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3487 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3488 bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu",
3489 (unsigned long) sec->header.sh_entsize,
3490 (unsigned long) sizeof(ElfW(RelM)));
3493 /* XXX Relocation code from modutils-2.3.19 is not here.
3494 * Why? That's about 20 lines of code from obj/obj_load.c,
3495 * which gets done in a second pass through the sections.
3496 * This BusyBox insmod does similar work in obj_relocate(). */
3503 #if ENABLE_FEATURE_INSMOD_LOADINKMEM
3505 * load the unloaded sections directly into the memory allocated by
3506 * kernel for the module
3509 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3511 ElfW(Addr) base = f->baseaddr;
3512 struct obj_section* sec;
3514 for (sec = f->load_order; sec; sec = sec->load_next) {
3516 /* section already loaded? */
3517 if (sec->contents != NULL)
3520 if (sec->header.sh_size == 0)
3523 sec->contents = imagebase + (sec->header.sh_addr - base);
3524 fseek(fp, sec->header.sh_offset, SEEK_SET);
3525 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3526 bb_perror_msg("error reading ELF section data");
3535 static void hide_special_symbols(struct obj_file *f)
3537 static const char *const specials[] = {
3538 SPFX "cleanup_module",
3540 SPFX "kernel_version",
3544 struct obj_symbol *sym;
3545 const char *const *p;
3547 for (p = specials; *p; ++p) {
3548 sym = obj_find_symbol(f, *p);
3550 sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3555 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
3556 static int obj_gpl_license(struct obj_file *f, const char **license)
3558 struct obj_section *sec;
3559 /* This list must match *exactly* the list of allowable licenses in
3560 * linux/include/linux/module.h. Checking for leading "GPL" will not
3561 * work, somebody will use "GPL sucks, this is proprietary".
3563 static const char *const gpl_licenses[] = {
3566 "GPL and additional rights",
3571 sec = obj_find_section(f, ".modinfo");
3573 const char *value, *ptr, *endptr;
3574 ptr = sec->contents;
3575 endptr = ptr + sec->header.sh_size;
3576 while (ptr < endptr) {
3577 value = strchr(ptr, '=');
3578 if (value && strncmp(ptr, "license", value-ptr) == 0) {
3582 for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
3583 if (strcmp(value+1, gpl_licenses[i]) == 0)
3588 ptr = strchr(ptr, '\0');
3598 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3599 #define TAINT_PROPRIETORY_MODULE (1 << 0)
3600 #define TAINT_FORCED_MODULE (1 << 1)
3601 #define TAINT_UNSAFE_SMP (1 << 2)
3602 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3604 static void set_tainted(int fd, char *m_name,
3605 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3607 static smallint printed_info;
3612 if (fd < 0 && !kernel_has_tainted)
3613 return; /* New modutils on old kernel */
3614 printf("Warning: loading %s will taint the kernel: %s%s\n",
3615 m_name, text1, text2);
3616 if (!printed_info) {
3617 printf(" See %s for information about tainted modules\n", TAINT_URL);
3621 read(fd, buf, sizeof(buf)-1);
3622 buf[sizeof(buf)-1] = '\0';
3623 oldval = strtoul(buf, NULL, 10);
3624 sprintf(buf, "%d\n", oldval | taint);
3625 write(fd, buf, strlen(buf));
3629 /* Check if loading this module will taint the kernel. */
3630 static void check_tainted_module(struct obj_file *f, char *m_name)
3632 static const char tainted_file[] ALIGN1 = TAINT_FILENAME;
3634 int fd, kernel_has_tainted;
3637 kernel_has_tainted = 1;
3638 fd = open(tainted_file, O_RDWR);
3640 if (errno == ENOENT)
3641 kernel_has_tainted = 0;
3642 else if (errno == EACCES)
3643 kernel_has_tainted = 1;
3645 perror(tainted_file);
3646 kernel_has_tainted = 0;
3650 switch (obj_gpl_license(f, &ptr)) {
3654 set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3657 /* The module has a non-GPL license so we pretend that the
3658 * kernel always has a taint flag to get a warning even on
3659 * kernels without the proc flag.
3661 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3664 set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3668 if (flag_force_load)
3669 set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3674 #else /* FEATURE_CHECK_TAINTED_MODULE */
3675 #define check_tainted_module(x, y) do { } while (0);
3676 #endif /* FEATURE_CHECK_TAINTED_MODULE */
3678 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3679 /* add module source, timestamp, kernel version and a symbol for the
3680 * start of some sections. this info is used by ksymoops to do better
3683 #if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3684 #define get_module_version(f, str) get_module_version(str)
3687 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3689 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3690 return new_get_module_version(f, str);
3691 #else /* FEATURE_INSMOD_VERSION_CHECKING */
3692 strncpy(str, "???", sizeof(str));
3694 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
3697 /* add module source, timestamp, kernel version and a symbol for the
3698 * start of some sections. this info is used by ksymoops to do better
3702 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3705 static const char symprefix[] ALIGN1 = "__insmod_";
3706 static const char section_names[][8] = {
3714 struct obj_section *sec;
3715 struct obj_symbol *sym;
3716 char *name, *absolute_filename;
3717 char str[STRVERSIONLEN];
3718 int i, l, lm_name, lfilename, use_ksymtab, version;
3719 struct stat statbuf;
3721 /* WARNING: was using realpath, but replaced by readlink to stop using
3722 * lots of stack. But here it seems to be able to cause problems? */
3723 absolute_filename = xmalloc_readlink(filename);
3724 if (!absolute_filename)
3725 absolute_filename = xstrdup(filename);
3727 lm_name = strlen(m_name);
3728 lfilename = strlen(absolute_filename);
3730 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3731 * are not to be exported. otherwise leave ksymtab alone for now, the
3732 * "export all symbols" compatibility code will export these symbols later.
3734 use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
3736 sec = obj_find_section(f, ".this");
3738 /* tag the module header with the object name, last modified
3739 * timestamp and module version. worst case for module version
3740 * is 0xffffff, decimal 16777215. putting all three fields in
3741 * one symbol is less readable but saves kernel space.
3743 l = sizeof(symprefix) + /* "__insmod_" */
3744 lm_name + /* module name */
3746 lfilename + /* object filename */
3748 2 * sizeof(statbuf.st_mtime) + /* mtime in hex */
3750 8 + /* version in dec */
3753 if (stat(absolute_filename, &statbuf) != 0)
3754 statbuf.st_mtime = 0;
3755 version = get_module_version(f, str); /* -1 if not found */
3756 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3757 symprefix, m_name, absolute_filename,
3758 (int)(2 * sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3760 sym = obj_add_symbol(f, name, -1,
3761 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3762 sec->idx, sec->header.sh_addr, 0);
3764 new_add_ksymtab(f, sym);
3766 free(absolute_filename);
3767 #ifdef _NOT_SUPPORTED_
3768 /* record where the persistent data is going, same address as previous symbol */
3771 l = sizeof(symprefix) + /* "__insmod_" */
3772 lm_name + /* module name */
3774 strlen(f->persist) + /* data store */
3777 snprintf(name, l, "%s%s_P%s",
3778 symprefix, m_name, f->persist);
3779 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3780 sec->idx, sec->header.sh_addr, 0);
3782 new_add_ksymtab(f, sym);
3784 #endif /* _NOT_SUPPORTED_ */
3785 /* tag the desired sections if size is non-zero */
3787 for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
3788 sec = obj_find_section(f, section_names[i]);
3789 if (sec && sec->header.sh_size) {
3790 l = sizeof(symprefix) + /* "__insmod_" */
3791 lm_name + /* module name */
3793 strlen(sec->name) + /* section name */
3795 8 + /* length in dec */
3798 snprintf(name, l, "%s%s_S%s_L%ld",
3799 symprefix, m_name, sec->name,
3800 (long)sec->header.sh_size);
3801 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3802 sec->idx, sec->header.sh_addr, 0);
3804 new_add_ksymtab(f, sym);
3808 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3810 #if ENABLE_FEATURE_INSMOD_LOAD_MAP
3811 static void print_load_map(struct obj_file *f)
3813 struct obj_section *sec;
3814 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3815 struct obj_symbol **all, **p;
3816 int i, nsyms, *loaded;
3817 struct obj_symbol *sym;
3819 /* Report on the section layout. */
3821 printf("Sections: Size %-*s Align\n",
3822 (int) (2 * sizeof(void *)), "Address");
3824 for (sec = f->load_order; sec; sec = sec->load_next) {
3828 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3833 printf("%-15s %08lx %0*lx 2**%d\n",
3835 (long)sec->header.sh_size,
3836 (int) (2 * sizeof(void *)),
3837 (long)sec->header.sh_addr,
3840 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
3841 /* Quick reference which section indicies are loaded. */
3843 i = f->header.e_shnum;
3844 loaded = alloca(sizeof(int) * i);
3846 loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
3848 /* Collect the symbols we'll be listing. */
3850 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3851 for (sym = f->symtab[i]; sym; sym = sym->next)
3852 if (sym->secidx <= SHN_HIRESERVE
3853 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3856 all = alloca(nsyms * sizeof(struct obj_symbol *));
3858 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3859 for (sym = f->symtab[i]; sym; sym = sym->next)
3860 if (sym->secidx <= SHN_HIRESERVE
3861 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3864 /* And list them. */
3865 printf("\nSymbols:\n");
3866 for (p = all; p < all + nsyms; ++p) {
3868 unsigned long value;
3871 if (sym->secidx == SHN_ABS) {
3874 } else if (sym->secidx == SHN_UNDEF) {
3878 sec = f->sections[sym->secidx];
3880 if (sec->header.sh_type == SHT_NOBITS)
3882 else if (sec->header.sh_flags & SHF_ALLOC) {
3883 if (sec->header.sh_flags & SHF_EXECINSTR)
3885 else if (sec->header.sh_flags & SHF_WRITE)
3890 value = sym->value + sec->header.sh_addr;
3893 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3894 type = tolower(type);
3896 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3901 #else /* !FEATURE_INSMOD_LOAD_MAP */
3902 void print_load_map(struct obj_file *f);
3905 int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
3906 int insmod_main(int argc, char **argv)
3912 unsigned long m_size;
3916 char *m_name = NULL;
3917 int exit_status = EXIT_FAILURE;
3919 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
3920 struct utsname uts_info;
3921 char m_strversion[STRVERSIONLEN];
3922 int m_version, m_crcs;
3924 #if ENABLE_FEATURE_CLEAN_UP
3930 struct utsname myuname;
3932 /* Parse any options */
3933 getopt32(argv, OPTION_STR, &opt_o);
3934 arg1 = argv[optind];
3935 if (option_mask32 & OPT_o) { // -o /* name the output module */
3937 m_name = xstrdup(opt_o);
3944 /* Grab the module name */
3945 tmp1 = xstrdup(arg1);
3946 tmp = basename(tmp1);
3949 if (uname(&myuname) == 0) {
3950 if (myuname.release[0] == '2') {
3951 k_version = myuname.release[2] - '0';
3955 #if ENABLE_FEATURE_2_6_MODULES
3956 if (k_version > 4 && len > 3 && tmp[len - 3] == '.'
3957 && tmp[len - 2] == 'k' && tmp[len - 1] == 'o'
3963 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3969 #if ENABLE_FEATURE_2_6_MODULES
3971 m_fullName = xasprintf("%s.ko", tmp);
3974 m_fullName = xasprintf("%s.o", tmp);
3980 tmp1 = NULL; /* flag for free(m_name) before exit() */
3983 /* Get a filedesc for the module. Check that we have a complete path */
3984 if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
3985 || (fp = fopen(arg1, "r")) == NULL
3987 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3988 * but do not error out yet if we fail to find it... */
3989 if (k_version) { /* uname succeedd */
3993 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
3994 /* Jump through hoops in case /lib/modules/`uname -r`
3995 * is a symlink. We do not want recursive_action to
3996 * follow symlinks, but we do want to follow the
3997 * /lib/modules/`uname -r` dir, So resolve it ourselves
3998 * if it is a link... */
3999 module_dir = xmalloc_readlink(tmdn);
4001 module_dir = xstrdup(tmdn);
4002 recursive_action(module_dir, ACTION_RECURSE,
4003 check_module_name_match, NULL, m_fullName, 0);
4008 /* Check if we have found anything yet */
4009 if (!m_filename || ((fp = fopen(m_filename, "r")) == NULL)) {
4015 module_dir = xmalloc_readlink(_PATH_MODULES);
4017 module_dir = xstrdup(_PATH_MODULES);
4018 /* No module found under /lib/modules/`uname -r`, this
4019 * time cast the net a bit wider. Search /lib/modules/ */
4020 r = recursive_action(module_dir, ACTION_RECURSE,
4021 check_module_name_match, NULL, m_fullName, 0);
4023 bb_error_msg_and_die("%s: module not found", m_fullName);
4025 if (m_filename == NULL
4026 || ((fp = fopen(m_filename, "r")) == NULL)
4028 bb_error_msg_and_die("%s: module not found", m_fullName);
4032 m_filename = xstrdup(arg1);
4035 printf("Using %s\n", m_filename);
4037 #if ENABLE_FEATURE_2_6_MODULES
4038 if (k_version > 4) {
4039 argv[optind] = m_filename;
4041 return insmod_ng_main(argc - optind, argv + optind);
4045 f = obj_load(fp, LOADBITS);
4047 if (get_modinfo_value(f, "kernel_version") == NULL)
4052 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4053 /* Version correspondence? */
4055 if (uname(&uts_info) < 0)
4056 uts_info.release[0] = '\0';
4057 if (m_has_modinfo) {
4058 m_version = new_get_module_version(f, m_strversion);
4059 if (m_version == -1) {
4060 bb_error_msg_and_die("cannot find the kernel version the module was "
4065 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4066 bb_error_msg("%skernel-module version mismatch\n"
4067 "\t%s was compiled for kernel version %s\n"
4068 "\twhile this kernel is version %s",
4069 flag_force_load ? "warning: " : "",
4070 m_filename, m_strversion, uts_info.release);
4071 if (!flag_force_load)
4076 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4078 if (query_module(NULL, 0, NULL, 0, NULL))
4079 bb_error_msg_and_die("not configured to support old kernels");
4080 new_get_kernel_symbols();
4081 k_crcs = new_is_kernel_checksummed();
4083 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
4086 m_crcs = new_is_module_checksummed(f);
4088 if (m_crcs != k_crcs)
4089 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4090 #endif /* FEATURE_INSMOD_VERSION_CHECKING */
4092 /* Let the module know about the kernel symbols. */
4093 add_kernel_symbols(f);
4095 /* Allocate common symbols, symbol tables, and string tables. */
4097 new_create_this_module(f, m_name);
4098 obj_check_undefineds(f);
4099 obj_allocate_commons(f);
4100 check_tainted_module(f, m_name);
4102 /* done with the module name, on to the optional var=value arguments */
4104 if (optind < argc) {
4105 new_process_module_arguments(f, argc - optind, argv + optind);
4109 hide_special_symbols(f);
4111 #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4112 add_ksymoops_symbols(f, m_filename, m_name);
4113 #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4115 new_create_module_ksymtab(f);
4117 /* Find current size of the module */
4118 m_size = obj_load_size(f);
4120 m_addr = create_module(m_name, m_size);
4121 if (m_addr == -1) switch (errno) {
4123 bb_error_msg_and_die("a module named %s already exists", m_name);
4125 bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes",
4128 bb_perror_msg_and_die("create_module: %s", m_name);
4133 * the PROGBITS section was not loaded by the obj_load
4134 * now we can load them directly into the kernel memory
4136 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4137 delete_module(m_name);
4142 if (!obj_relocate(f, m_addr)) {
4143 delete_module(m_name);
4147 if (!new_init_module(m_name, f, m_size)) {
4148 delete_module(m_name);
4152 if (flag_print_load_map)
4155 exit_status = EXIT_SUCCESS;
4158 #if ENABLE_FEATURE_CLEAN_UP
4169 #endif /* ENABLE_FEATURE_2_4_MODULES */
4171 * End of big piece of 2.4-specific code
4175 #if ENABLE_FEATURE_2_6_MODULES
4177 #include <sys/mman.h>
4178 #include <asm/unistd.h>
4179 #include <sys/syscall.h>
4181 /* We use error numbers in a loose translation... */
4182 static const char *moderror(int err)
4186 return "invalid module format";
4188 return "unknown symbol in module";
4190 return "module has wrong symbol version";
4192 return "invalid parameters";
4194 return strerror(err);
4198 #if !ENABLE_FEATURE_2_4_MODULES
4199 int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
4200 int insmod_main(int argc ATTRIBUTE_UNUSED, char **argv)
4202 static int insmod_ng_main(int argc ATTRIBUTE_UNUSED, char **argv)
4209 char *filename, *options;
4215 /* Rest is options */
4216 options = xzalloc(1);
4219 options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
4220 /* Spaces handled by "" pairs, but no way of escaping quotes */
4221 optlen += sprintf(options + optlen, (strchr(*argv,' ') ? "\"%s\" " : "%s "), *argv);
4225 /* Any special reason why mmap? It isn't performace critical... */
4229 fd = xopen(filename, O_RDONLY);
4232 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4233 if (map == MAP_FAILED) {
4234 bb_perror_msg_and_die("cannot mmap '%s'", filename);
4237 /* map == NULL on Blackfin, probably on other MMU-less systems too. Workaround. */
4240 xread(fd, map, len);
4243 len = MAXINT(ssize_t);
4244 map = xmalloc_open_read_close(filename, &len);
4247 ret = syscall(__NR_init_module, map, len, options);
4249 bb_error_msg_and_die("cannot insert '%s': %s",
4250 filename, moderror(errno));