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.
72 #include <sys/utsname.h>
75 #if !defined(CONFIG_FEATURE_2_4_MODULES) && \
76 !defined(CONFIG_FEATURE_2_6_MODULES)
77 #define CONFIG_FEATURE_2_4_MODULES
80 #if !defined(CONFIG_FEATURE_2_4_MODULES)
81 #define insmod_ng_main insmod_main
84 #if defined(CONFIG_FEATURE_2_6_MODULES)
85 extern int insmod_ng_main( int argc, char **argv);
89 #if defined(CONFIG_FEATURE_2_4_MODULES)
92 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
100 #if defined(__alpha__)
101 #define MATCH_MACHINE(x) (x == EM_ALPHA)
102 #define SHT_RELM SHT_RELA
103 #define Elf64_RelM Elf64_Rela
104 #define ELFCLASSM ELFCLASS64
109 #define MATCH_MACHINE(x) (x == EM_ARM)
110 #define SHT_RELM SHT_REL
111 #define Elf32_RelM Elf32_Rel
112 #define ELFCLASSM ELFCLASS32
113 #define CONFIG_USE_PLT_ENTRIES
114 #define CONFIG_PLT_ENTRY_SIZE 8
115 #define CONFIG_USE_GOT_ENTRIES
116 #define CONFIG_GOT_ENTRY_SIZE 8
117 #define CONFIG_USE_SINGLE
121 #if defined(__cris__)
122 #define MATCH_MACHINE(x) (x == EM_CRIS)
123 #define SHT_RELM SHT_RELA
124 #define Elf32_RelM Elf32_Rela
125 #define ELFCLASSM ELFCLASS32
128 #define R_CRIS_NONE 0
134 #if defined(__H8300H__) || defined(__H8300S__)
135 #define MATCH_MACHINE(x) (x == EM_H8_300)
136 #define SHT_RELM SHT_RELA
137 #define Elf32_RelM Elf32_Rela
138 #define ELFCLASSM ELFCLASS32
139 #define CONFIG_USE_SINGLE
140 #define SYMBOL_PREFIX "_"
143 /* PA-RISC / HP-PA */
144 #if defined(__hppa__)
145 #define MATCH_MACHINE(x) (x == EM_PARISC)
146 #define SHT_RELM SHT_RELA
147 #if defined(__LP64__)
148 #define Elf64_RelM Elf64_Rela
149 #define ELFCLASSM ELFCLASS64
151 #define Elf32_RelM Elf32_Rela
152 #define ELFCLASSM ELFCLASS32
157 #if defined(__i386__)
159 #define MATCH_MACHINE(x) (x == EM_386)
161 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
163 #define SHT_RELM SHT_REL
164 #define Elf32_RelM Elf32_Rel
165 #define ELFCLASSM ELFCLASS32
166 #define CONFIG_USE_GOT_ENTRIES
167 #define CONFIG_GOT_ENTRY_SIZE 4
168 #define CONFIG_USE_SINGLE
171 /* IA64, aka Itanium */
172 #if defined(__ia64__)
173 #define MATCH_MACHINE(x) (x == EM_IA_64)
174 #define SHT_RELM SHT_RELA
175 #define Elf64_RelM Elf64_Rela
176 #define ELFCLASSM ELFCLASS64
180 #if defined(__mc68000__)
181 #define MATCH_MACHINE(x) (x == EM_68K)
182 #define SHT_RELM SHT_RELA
183 #define Elf32_RelM Elf32_Rela
184 #define ELFCLASSM ELFCLASS32
185 #define CONFIG_USE_GOT_ENTRIES
186 #define CONFIG_GOT_ENTRY_SIZE 4
187 #define CONFIG_USE_SINGLE
191 #if defined(__mips__)
192 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
193 #define SHT_RELM SHT_REL
194 #define Elf32_RelM Elf32_Rel
195 #define ELFCLASSM ELFCLASS32
196 /* Account for ELF spec changes. */
197 #ifndef EM_MIPS_RS3_LE
198 #ifdef EM_MIPS_RS4_BE
199 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
201 #define EM_MIPS_RS3_LE 10
203 #endif /* !EM_MIPS_RS3_LE */
204 #define ARCHDATAM "__dbe_table"
208 #if defined(__nios2__)
209 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
210 #define SHT_RELM SHT_RELA
211 #define Elf32_RelM Elf32_Rela
212 #define ELFCLASSM ELFCLASS32
216 #if defined(__powerpc64__)
217 #define MATCH_MACHINE(x) (x == EM_PPC64)
218 #define SHT_RELM SHT_RELA
219 #define Elf64_RelM Elf64_Rela
220 #define ELFCLASSM ELFCLASS64
221 #elif defined(__powerpc__)
222 #define MATCH_MACHINE(x) (x == EM_PPC)
223 #define SHT_RELM SHT_RELA
224 #define Elf32_RelM Elf32_Rela
225 #define ELFCLASSM ELFCLASS32
226 #define CONFIG_USE_PLT_ENTRIES
227 #define CONFIG_PLT_ENTRY_SIZE 16
228 #define CONFIG_USE_PLT_LIST
229 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
230 #define CONFIG_USE_LIST
231 #define ARCHDATAM "__ftr_fixup"
235 #if defined(__s390__)
236 #define MATCH_MACHINE(x) (x == EM_S390)
237 #define SHT_RELM SHT_RELA
238 #define Elf32_RelM Elf32_Rela
239 #define ELFCLASSM ELFCLASS32
240 #define CONFIG_USE_PLT_ENTRIES
241 #define CONFIG_PLT_ENTRY_SIZE 8
242 #define CONFIG_USE_GOT_ENTRIES
243 #define CONFIG_GOT_ENTRY_SIZE 8
244 #define CONFIG_USE_SINGLE
249 #define MATCH_MACHINE(x) (x == EM_SH)
250 #define SHT_RELM SHT_RELA
251 #define Elf32_RelM Elf32_Rela
252 #define ELFCLASSM ELFCLASS32
253 #define CONFIG_USE_GOT_ENTRIES
254 #define CONFIG_GOT_ENTRY_SIZE 4
255 #define CONFIG_USE_SINGLE
256 /* the SH changes have only been tested in =little endian= mode */
257 /* I'm not sure about big endian, so let's warn: */
258 #if defined(__sh__) && BB_BIG_ENDIAN
259 # error insmod.c may require changes for use on big endian SH
261 /* it may or may not work on the SH1/SH2... Error on those also */
262 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
263 #error insmod.c may require changes for SH1 or SH2 use
268 #if defined(__sparc__)
269 #define MATCH_MACHINE(x) (x == EM_SPARC)
270 #define SHT_RELM SHT_RELA
271 #define Elf32_RelM Elf32_Rela
272 #define ELFCLASSM ELFCLASS32
276 #if defined (__v850e__)
277 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
278 #define SHT_RELM SHT_RELA
279 #define Elf32_RelM Elf32_Rela
280 #define ELFCLASSM ELFCLASS32
281 #define CONFIG_USE_PLT_ENTRIES
282 #define CONFIG_PLT_ENTRY_SIZE 8
283 #define CONFIG_USE_SINGLE
284 #ifndef EM_CYGNUS_V850 /* grumble */
285 #define EM_CYGNUS_V850 0x9080
287 #define SYMBOL_PREFIX "_"
291 #if defined(__x86_64__)
292 #define MATCH_MACHINE(x) (x == EM_X86_64)
293 #define SHT_RELM SHT_RELA
294 #define CONFIG_USE_GOT_ENTRIES
295 #define CONFIG_GOT_ENTRY_SIZE 8
296 #define CONFIG_USE_SINGLE
297 #define Elf64_RelM Elf64_Rela
298 #define ELFCLASSM ELFCLASS64
302 #error Sorry, but insmod.c does not yet support this architecture...
306 //----------------------------------------------------------------------------
307 //--------modutils module.h, lines 45-242
308 //----------------------------------------------------------------------------
310 /* Definitions for the Linux module syscall interface.
311 Copyright 1996, 1997 Linux International.
313 Contributed by Richard Henderson <rth@tamu.edu>
315 This file is part of the Linux modutils.
317 This program is free software; you can redistribute it and/or modify it
318 under the terms of the GNU General Public License as published by the
319 Free Software Foundation; either version 2 of the License, or (at your
320 option) any later version.
322 This program is distributed in the hope that it will be useful, but
323 WITHOUT ANY WARRANTY; without even the implied warranty of
324 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
325 General Public License for more details.
327 You should have received a copy of the GNU General Public License
328 along with this program; if not, write to the Free Software Foundation,
329 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
332 #ifndef MODUTILS_MODULE_H
333 /* Why? static const int MODUTILS_MODULE_H = 1;*/
335 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
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
372 struct new_module_persist;
374 struct new_module_ref
376 unsigned tgt_long dep; /* kernel addresses */
377 unsigned tgt_long ref;
378 unsigned tgt_long next_ref;
383 unsigned tgt_long size_of_struct; /* == sizeof(module) */
384 unsigned tgt_long next;
385 unsigned tgt_long name;
386 unsigned tgt_long size;
389 unsigned tgt_long flags; /* AUTOCLEAN et al */
394 unsigned tgt_long syms;
395 unsigned tgt_long deps;
396 unsigned tgt_long refs;
397 unsigned tgt_long init;
398 unsigned tgt_long cleanup;
399 unsigned tgt_long ex_table_start;
400 unsigned tgt_long ex_table_end;
402 unsigned tgt_long gp;
404 /* Everything after here is extension. */
405 unsigned tgt_long persist_start;
406 unsigned tgt_long persist_end;
407 unsigned tgt_long can_unload;
408 unsigned tgt_long runsize;
409 const char *kallsyms_start; /* All symbols for kernel debugging */
410 const char *kallsyms_end;
411 const char *archdata_start; /* arch specific data for module */
412 const char *archdata_end;
413 const char *kernel_data; /* Reserved for kernel internal use */
417 #define ARCHDATA_SEC_NAME ARCHDATAM
419 #define ARCHDATA_SEC_NAME "__archdata"
421 #define KALLSYMS_SEC_NAME "__kallsyms"
424 struct new_module_info
432 /* Bits of module.flags. */
436 NEW_MOD_AUTOCLEAN = 4,
438 NEW_MOD_USED_ONCE = 16
441 int init_module(const char *name, const struct new_module *);
442 int query_module(const char *name, int which, void *buf,
443 size_t bufsize, size_t *ret);
445 /* Values for query_module's which. */
454 /*======================================================================*/
455 /* The system calls unchanged between 2.0 and 2.1. */
457 unsigned long create_module(const char *, size_t);
458 int delete_module(const char *);
461 #endif /* module.h */
463 //----------------------------------------------------------------------------
464 //--------end of modutils module.h
465 //----------------------------------------------------------------------------
469 //----------------------------------------------------------------------------
470 //--------modutils obj.h, lines 253-462
471 //----------------------------------------------------------------------------
473 /* Elf object file loading and relocation routines.
474 Copyright 1996, 1997 Linux International.
476 Contributed by Richard Henderson <rth@tamu.edu>
478 This file is part of the Linux modutils.
480 This program is free software; you can redistribute it and/or modify it
481 under the terms of the GNU General Public License as published by the
482 Free Software Foundation; either version 2 of the License, or (at your
483 option) any later version.
485 This program is distributed in the hope that it will be useful, but
486 WITHOUT ANY WARRANTY; without even the implied warranty of
487 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
488 General Public License for more details.
490 You should have received a copy of the GNU General Public License
491 along with this program; if not, write to the Free Software Foundation,
492 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
495 #ifndef MODUTILS_OBJ_H
496 /* Why? static const int MODUTILS_OBJ_H = 1; */
498 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
500 /* The relocatable object is manipulated using elfin types. */
507 # if ELFCLASSM == ELFCLASS32
508 # define ElfW(x) Elf32_ ## x
509 # define ELFW(x) ELF32_ ## x
511 # define ElfW(x) Elf64_ ## x
512 # define ELFW(x) ELF64_ ## x
516 /* For some reason this is missing from some ancient C libraries.... */
517 #ifndef ELF32_ST_INFO
518 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
521 #ifndef ELF64_ST_INFO
522 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
525 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
526 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
527 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
528 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
529 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
531 struct obj_string_patch;
532 struct obj_symbol_patch;
539 struct obj_section *load_next;
545 struct obj_symbol *next; /* hash table link */
549 int secidx; /* the defining section index/module */
551 int ksymidx; /* for export to the kernel symtab */
552 int referenced; /* actually used in the link */
555 /* Hardcode the hash table size. We shouldn't be needing so many
556 symbols that we begin to degrade performance, and we get a big win
557 by giving the compiler a constant divisor. */
559 #define HASH_BUCKETS 521
565 struct obj_section **sections;
566 struct obj_section *load_order;
567 struct obj_section **load_order_search_start;
568 struct obj_string_patch *string_patches;
569 struct obj_symbol_patch *symbol_patches;
570 int (*symbol_cmp)(const char *, const char *);
571 unsigned long (*symbol_hash)(const char *);
572 unsigned long local_symtab_size;
573 struct obj_symbol **local_symtab;
574 struct obj_symbol *symtab[HASH_BUCKETS];
585 struct obj_string_patch
587 struct obj_string_patch *next;
589 ElfW(Addr) reloc_offset;
590 ElfW(Addr) string_offset;
593 struct obj_symbol_patch
595 struct obj_symbol_patch *next;
597 ElfW(Addr) reloc_offset;
598 struct obj_symbol *sym;
602 /* Generic object manipulation routines. */
604 static unsigned long obj_elf_hash(const char *);
606 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
608 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
611 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
612 struct obj_symbol *sym);
614 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
615 static void obj_set_symbol_compare(struct obj_file *f,
616 int (*cmp)(const char *, const char *),
617 unsigned long (*hash)(const char *));
620 static struct obj_section *obj_find_section (struct obj_file *f,
623 static void obj_insert_section_load_order (struct obj_file *f,
624 struct obj_section *sec);
626 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
631 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
636 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
638 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
641 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
642 struct obj_symbol *sym);
644 static int obj_check_undefineds(struct obj_file *f);
646 static void obj_allocate_commons(struct obj_file *f);
648 static unsigned long obj_load_size (struct obj_file *f);
650 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
652 static struct obj_file *obj_load(FILE *f, int loadprogbits);
654 static int obj_create_image (struct obj_file *f, char *image);
656 /* Architecture specific manipulation routines. */
658 static struct obj_file *arch_new_file (void);
660 static struct obj_section *arch_new_section (void);
662 static struct obj_symbol *arch_new_symbol (void);
664 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
665 struct obj_section *targsec,
666 struct obj_section *symsec,
667 struct obj_symbol *sym,
668 ElfW(RelM) *rel, ElfW(Addr) value);
670 static void arch_create_got (struct obj_file *f);
671 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
672 static int obj_gpl_license(struct obj_file *f, const char **license);
673 #endif /* ENABLE_FEATURE_CHECK_TAINTED_MODULE */
675 //----------------------------------------------------------------------------
676 //--------end of modutils obj.h
677 //----------------------------------------------------------------------------
680 /* SPFX is always a string, so it can be concatenated to string constants. */
682 #define SPFX SYMBOL_PREFIX
688 #define _PATH_MODULES "/lib/modules"
689 enum { STRVERSIONLEN = 32 };
691 /*======================================================================*/
693 static int flag_force_load = 0;
694 static int flag_autoclean = 0;
695 static int flag_verbose = 0;
696 static int flag_quiet = 0;
697 static int flag_export = 1;
700 /*======================================================================*/
702 #if defined(CONFIG_USE_LIST)
704 struct arch_list_entry
706 struct arch_list_entry *next;
707 CONFIG_LIST_ARCHTYPE addend;
714 #if defined(CONFIG_USE_SINGLE)
716 struct arch_single_entry
725 #if defined(__mips__)
728 struct mips_hi16 *next;
735 struct obj_file root;
736 #if defined(CONFIG_USE_PLT_ENTRIES)
737 struct obj_section *plt;
739 #if defined(CONFIG_USE_GOT_ENTRIES)
740 struct obj_section *got;
742 #if defined(__mips__)
743 struct mips_hi16 *mips_hi16_list;
748 struct obj_symbol root;
749 #if defined(CONFIG_USE_PLT_ENTRIES)
750 #if defined(CONFIG_USE_PLT_LIST)
751 struct arch_list_entry *pltent;
753 struct arch_single_entry pltent;
756 #if defined(CONFIG_USE_GOT_ENTRIES)
757 struct arch_single_entry gotent;
762 struct external_module {
767 struct new_module_symbol *syms;
770 static struct new_module_symbol *ksyms;
771 static size_t nksyms;
773 static struct external_module *ext_modules;
774 static int n_ext_modules;
775 static int n_ext_modules_used;
776 extern int delete_module(const char *);
778 static char *m_filename;
779 static char *m_fullName;
783 /*======================================================================*/
786 static int check_module_name_match(const char *filename, struct stat *statbuf,
789 char *fullname = (char *) userdata;
791 if (fullname[0] == '\0')
794 char *tmp, *tmp1 = bb_xstrdup(filename);
795 tmp = bb_get_last_path_component(tmp1);
796 if (strcmp(tmp, fullname) == 0) {
798 /* Stop searching if we find a match */
799 m_filename = bb_xstrdup(filename);
808 /*======================================================================*/
810 static struct obj_file *arch_new_file(void)
813 f = xmalloc(sizeof(*f));
815 memset(f, 0, sizeof(*f));
820 static struct obj_section *arch_new_section(void)
822 return xmalloc(sizeof(struct obj_section));
825 static struct obj_symbol *arch_new_symbol(void)
827 struct arch_symbol *sym;
828 sym = xmalloc(sizeof(*sym));
830 memset(sym, 0, sizeof(*sym));
835 static enum obj_reloc
836 arch_apply_relocation(struct obj_file *f,
837 struct obj_section *targsec,
838 struct obj_section *symsec,
839 struct obj_symbol *sym,
840 ElfW(RelM) *rel, ElfW(Addr) v)
842 struct arch_file *ifile = (struct arch_file *) f;
843 enum obj_reloc ret = obj_reloc_ok;
844 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
845 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
846 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
847 struct arch_symbol *isym = (struct arch_symbol *) sym;
849 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
850 #if defined(CONFIG_USE_GOT_ENTRIES)
851 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
854 #if defined(CONFIG_USE_PLT_ENTRIES)
855 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
857 # if defined(CONFIG_USE_PLT_LIST)
858 struct arch_list_entry *pe;
860 struct arch_single_entry *pe;
864 switch (ELF_R_TYPE(rel->r_info)) {
879 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
880 * (which is .got) similar to branch,
881 * but is full 32 bits relative */
891 case R_ARM_GOTOFF: /* address relative to the got */
896 #elif defined(__cris__)
902 /* CRIS keeps the relocation value in the r_addend field and
903 * should not use whats in *loc at all
908 #elif defined(__H8300H__) || defined(__H8300S__)
911 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
912 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
923 if ((ElfW(Sword))v > 0x7fff ||
924 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
925 ret = obj_reloc_overflow;
927 *(unsigned short *)loc = v;
931 if ((ElfW(Sword))v > 0x7f ||
932 (ElfW(Sword))v < -(ElfW(Sword))0x80)
933 ret = obj_reloc_overflow;
935 *(unsigned char *)loc = v;
938 #elif defined(__i386__)
974 #elif defined(__mc68000__)
985 ret = obj_reloc_overflow;
992 ret = obj_reloc_overflow;
999 if ((ElfW(Sword))v > 0x7f ||
1000 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1001 ret = obj_reloc_overflow;
1008 if ((ElfW(Sword))v > 0x7fff ||
1009 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1010 ret = obj_reloc_overflow;
1016 *(int *)loc = v - dot;
1019 case R_68K_GLOB_DAT:
1020 case R_68K_JMP_SLOT:
1024 case R_68K_RELATIVE:
1025 *(int *)loc += f->baseaddr;
1031 # ifdef R_68K_GOTOFF
1038 #elif defined(__mips__)
1049 ret = obj_reloc_dangerous;
1050 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1051 ret = obj_reloc_overflow;
1053 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1059 struct mips_hi16 *n;
1061 /* We cannot relocate this one now because we don't know the value
1062 of the carry we need to add. Save the information, and let LO16
1063 do the actual relocation. */
1064 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1067 n->next = ifile->mips_hi16_list;
1068 ifile->mips_hi16_list = n;
1074 unsigned long insnlo = *loc;
1075 ElfW(Addr) val, vallo;
1077 /* Sign extend the addend we extract from the lo insn. */
1078 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1080 if (ifile->mips_hi16_list != NULL) {
1081 struct mips_hi16 *l;
1083 l = ifile->mips_hi16_list;
1085 struct mips_hi16 *next;
1088 /* The value for the HI16 had best be the same. */
1089 assert(v == l->value);
1091 /* Do the HI16 relocation. Note that we actually don't
1092 need to know anything about the LO16 itself, except where
1093 to find the low 16 bits of the addend needed by the LO16. */
1096 ((insn & 0xffff) << 16) +
1100 /* Account for the sign extension that will happen in the
1107 insn = (insn & ~0xffff) | val;
1115 ifile->mips_hi16_list = NULL;
1118 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1120 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1125 #elif defined(__nios2__)
1130 case R_NIOS2_BFD_RELOC_32:
1134 case R_NIOS2_BFD_RELOC_16:
1136 ret = obj_reloc_overflow;
1141 case R_NIOS2_BFD_RELOC_8:
1143 ret = obj_reloc_overflow;
1152 if ((Elf32_Sword)v > 0x7fff ||
1153 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1154 ret = obj_reloc_overflow;
1158 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1168 ret = obj_reloc_overflow;
1172 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1177 case R_NIOS2_PCREL16:
1182 if ((Elf32_Sword)v > 0x7fff ||
1183 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1184 ret = obj_reloc_overflow;
1188 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1194 Elf32_Addr word, gp;
1196 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1198 if ((Elf32_Sword)v > 0x7fff ||
1199 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1200 ret = obj_reloc_overflow;
1204 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1208 case R_NIOS2_CALL26:
1210 ret = obj_reloc_dangerous;
1211 if ((v >> 28) != (dot >> 28))
1212 ret = obj_reloc_overflow;
1213 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1221 ret = obj_reloc_overflow;
1224 word = *loc & ~0x7c0;
1225 *loc = word | ((v & 0x1f) << 6);
1234 ret = obj_reloc_overflow;
1237 word = *loc & ~0xfc0;
1238 *loc = word | ((v & 0x3f) << 6);
1247 ret = obj_reloc_overflow;
1250 word = *loc & ~0x3fc0;
1251 *loc = word | ((v & 0xff) << 6);
1260 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1270 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1275 case R_NIOS2_HIADJ16:
1277 Elf32_Addr word1, word2;
1280 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1281 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1286 #elif defined(__powerpc64__)
1287 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1289 #elif defined(__powerpc__)
1291 case R_PPC_ADDR16_HA:
1292 *(unsigned short *)loc = (v + 0x8000) >> 16;
1295 case R_PPC_ADDR16_HI:
1296 *(unsigned short *)loc = v >> 16;
1299 case R_PPC_ADDR16_LO:
1300 *(unsigned short *)loc = v;
1314 #elif defined(__s390__)
1317 *(unsigned int *) loc += v;
1320 *(unsigned short *) loc += v;
1323 *(unsigned char *) loc += v;
1327 *(unsigned int *) loc += v - dot;
1330 *(unsigned short *) loc += (v - dot) >> 1;
1333 *(unsigned short *) loc += v - dot;
1337 case R_390_PLT16DBL:
1338 /* find the plt entry and initialize it. */
1339 assert(isym != NULL);
1340 pe = (struct arch_single_entry *) &isym->pltent;
1341 assert(pe->allocated);
1342 if (pe->inited == 0) {
1343 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1344 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1346 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1353 /* Insert relative distance to target. */
1354 v = plt + pe->offset - dot;
1355 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1356 *(unsigned int *) loc = (unsigned int) v;
1357 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1358 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1361 case R_390_GLOB_DAT:
1362 case R_390_JMP_SLOT:
1366 case R_390_RELATIVE:
1367 *loc += f->baseaddr;
1372 *(unsigned long *) loc += got - dot;
1378 assert(isym != NULL);
1380 if (!isym->gotent.inited)
1382 isym->gotent.inited = 1;
1383 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1385 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1386 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1387 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1388 *(unsigned short *) loc += isym->gotent.offset;
1389 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1390 *(unsigned int *) loc += isym->gotent.offset;
1393 # ifndef R_390_GOTOFF32
1394 # define R_390_GOTOFF32 R_390_GOTOFF
1396 case R_390_GOTOFF32:
1401 #elif defined(__sh__)
1424 *loc = f->baseaddr + rel->r_addend;
1429 *loc = got - dot + rel->r_addend;
1440 # if defined(__SH5__)
1441 case R_SH_IMM_MEDLOW16:
1442 case R_SH_IMM_LOW16:
1446 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1450 * movi and shori have the format:
1452 * | op | imm | reg | reserved |
1453 * 31..26 25..10 9.. 4 3 .. 0
1455 * so we simply mask and or in imm.
1457 word = *loc & ~0x3fffc00;
1458 word |= (v & 0xffff) << 10;
1465 case R_SH_IMM_MEDLOW16_PCREL:
1466 case R_SH_IMM_LOW16_PCREL:
1470 word = *loc & ~0x3fffc00;
1474 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1477 word |= (v & 0xffff) << 10;
1483 # endif /* __SH5__ */
1485 #elif defined (__v850e__)
1491 /* We write two shorts instead of a long because even
1492 32-bit insns only need half-word alignment, but
1493 32-bit data needs to be long-word aligned. */
1494 v += ((unsigned short *)loc)[0];
1495 v += ((unsigned short *)loc)[1] << 16;
1496 ((unsigned short *)loc)[0] = v & 0xffff;
1497 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1500 case R_V850_22_PCREL:
1503 #elif defined(__x86_64__)
1513 *(unsigned int *) loc += v;
1516 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1517 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1522 *(signed int *) loc += v;
1526 *(unsigned short *) loc += v;
1530 *(unsigned char *) loc += v;
1534 *(unsigned int *) loc += v - dot;
1538 *(unsigned short *) loc += v - dot;
1542 *(unsigned char *) loc += v - dot;
1545 case R_X86_64_GLOB_DAT:
1546 case R_X86_64_JUMP_SLOT:
1550 case R_X86_64_RELATIVE:
1551 *loc += f->baseaddr;
1554 case R_X86_64_GOT32:
1555 case R_X86_64_GOTPCREL:
1558 assert(isym != NULL);
1559 if (!isym->gotent.reloc_done)
1561 isym->gotent.reloc_done = 1;
1562 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1564 /* XXX are these really correct? */
1565 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1566 *(unsigned int *) loc += v + isym->gotent.offset;
1568 *loc += isym->gotent.offset;
1573 # warning "no idea how to handle relocations on your arch"
1577 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1578 ret = obj_reloc_unhandled;
1581 #if defined(CONFIG_USE_PLT_ENTRIES)
1585 /* find the plt entry and initialize it if necessary */
1586 assert(isym != NULL);
1588 #if defined(CONFIG_USE_PLT_LIST)
1589 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1597 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1599 /* generate some machine code */
1601 #if defined(__arm__)
1602 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1603 ip[1] = v; /* sym@ */
1605 #if defined(__powerpc__)
1606 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1607 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1608 ip[2] = 0x7d6903a6; /* mtctr r11 */
1609 ip[3] = 0x4e800420; /* bctr */
1611 #if defined (__v850e__)
1612 /* We have to trash a register, so we assume that any control
1613 transfer more than 21-bits away must be a function call
1614 (so we can use a call-clobbered register). */
1615 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1616 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1621 /* relative distance to target */
1623 /* if the target is too far away.... */
1624 #if defined (__arm__) || defined (__powerpc__)
1625 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1626 #elif defined (__v850e__)
1627 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1629 /* go via the plt */
1630 v = plt + pe->offset - dot;
1632 #if defined (__v850e__)
1637 ret = obj_reloc_dangerous;
1639 /* merge the offset into the instruction. */
1640 #if defined(__arm__)
1641 /* Convert to words. */
1644 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1646 #if defined(__powerpc__)
1647 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1649 #if defined (__v850e__)
1650 /* We write two shorts instead of a long because even 32-bit insns
1651 only need half-word alignment, but the 32-bit data write needs
1652 to be long-word aligned. */
1653 ((unsigned short *)loc)[0] =
1654 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1655 | ((v >> 16) & 0x3f); /* offs high part */
1656 ((unsigned short *)loc)[1] =
1657 (v & 0xffff); /* offs low part */
1660 #endif /* CONFIG_USE_PLT_ENTRIES */
1662 #if defined(CONFIG_USE_GOT_ENTRIES)
1665 assert(isym != NULL);
1666 /* needs an entry in the .got: set it, once */
1667 if (!isym->gotent.inited) {
1668 isym->gotent.inited = 1;
1669 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1671 /* make the reloc with_respect_to_.got */
1673 *loc += isym->gotent.offset + rel->r_addend;
1674 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1675 *loc += isym->gotent.offset;
1679 #endif /* CONFIG_USE_GOT_ENTRIES */
1686 #if defined(CONFIG_USE_LIST)
1688 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1689 int offset, int size)
1691 struct arch_list_entry *pe;
1693 for (pe = *list; pe != NULL; pe = pe->next) {
1694 if (pe->addend == rel->r_addend) {
1700 pe = xmalloc(sizeof(struct arch_list_entry));
1702 pe->addend = rel->r_addend;
1703 pe->offset = offset;
1713 #if defined(CONFIG_USE_SINGLE)
1715 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1716 int offset, int size)
1718 if (single->allocated == 0) {
1719 single->allocated = 1;
1720 single->offset = offset;
1729 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1731 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1732 int offset, int size)
1734 struct obj_section *myrelsec = obj_find_section(f, name);
1741 obj_extend_section(myrelsec, offset);
1743 myrelsec = obj_create_alloced_section(f, name,
1753 static void arch_create_got(struct obj_file *f)
1755 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1756 struct arch_file *ifile = (struct arch_file *) f;
1758 #if defined(CONFIG_USE_GOT_ENTRIES)
1759 int got_offset = 0, got_needed = 0, got_allocate;
1761 #if defined(CONFIG_USE_PLT_ENTRIES)
1762 int plt_offset = 0, plt_needed = 0, plt_allocate;
1764 struct obj_section *relsec, *symsec, *strsec;
1765 ElfW(RelM) *rel, *relend;
1766 ElfW(Sym) *symtab, *extsym;
1767 const char *strtab, *name;
1768 struct arch_symbol *intsym;
1770 for (i = 0; i < f->header.e_shnum; ++i) {
1771 relsec = f->sections[i];
1772 if (relsec->header.sh_type != SHT_RELM)
1775 symsec = f->sections[relsec->header.sh_link];
1776 strsec = f->sections[symsec->header.sh_link];
1778 rel = (ElfW(RelM) *) relsec->contents;
1779 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1780 symtab = (ElfW(Sym) *) symsec->contents;
1781 strtab = (const char *) strsec->contents;
1783 for (; rel < relend; ++rel) {
1784 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1786 #if defined(CONFIG_USE_GOT_ENTRIES)
1789 #if defined(CONFIG_USE_PLT_ENTRIES)
1793 switch (ELF_R_TYPE(rel->r_info)) {
1794 #if defined(__arm__)
1809 #elif defined(__i386__)
1819 #elif defined(__powerpc__)
1824 #elif defined(__mc68000__)
1835 #elif defined(__sh__)
1845 #elif defined (__v850e__)
1846 case R_V850_22_PCREL:
1855 if (extsym->st_name != 0) {
1856 name = strtab + extsym->st_name;
1858 name = f->sections[extsym->st_shndx]->name;
1860 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1861 #if defined(CONFIG_USE_GOT_ENTRIES)
1863 got_offset += arch_single_init(
1864 rel, &intsym->gotent,
1865 got_offset, CONFIG_GOT_ENTRY_SIZE);
1870 #if defined(CONFIG_USE_PLT_ENTRIES)
1872 #if defined(CONFIG_USE_PLT_LIST)
1873 plt_offset += arch_list_add(
1874 rel, &intsym->pltent,
1875 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1877 plt_offset += arch_single_init(
1878 rel, &intsym->pltent,
1879 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1887 #if defined(CONFIG_USE_GOT_ENTRIES)
1889 ifile->got = arch_xsect_init(f, ".got", got_offset,
1890 CONFIG_GOT_ENTRY_SIZE);
1894 #if defined(CONFIG_USE_PLT_ENTRIES)
1896 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1897 CONFIG_PLT_ENTRY_SIZE);
1901 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1904 /*======================================================================*/
1906 /* Standard ELF hash function. */
1907 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1909 unsigned long h = 0;
1916 if ((g = (h & 0xf0000000)) != 0) {
1925 static unsigned long obj_elf_hash(const char *name)
1927 return obj_elf_hash_n(name, strlen(name));
1930 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1931 /* String comparison for non-co-versioned kernel and module. */
1933 static int ncv_strcmp(const char *a, const char *b)
1935 size_t alen = strlen(a), blen = strlen(b);
1937 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1938 return strncmp(a, b, alen);
1939 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1940 return strncmp(a, b, blen);
1942 return strcmp(a, b);
1945 /* String hashing for non-co-versioned kernel and module. Here
1946 we are simply forced to drop the crc from the hash. */
1948 static unsigned long ncv_symbol_hash(const char *str)
1950 size_t len = strlen(str);
1951 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1953 return obj_elf_hash_n(str, len);
1957 obj_set_symbol_compare(struct obj_file *f,
1958 int (*cmp) (const char *, const char *),
1959 unsigned long (*hash) (const char *))
1962 f->symbol_cmp = cmp;
1964 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1967 f->symbol_hash = hash;
1969 memcpy(tmptab, f->symtab, sizeof(tmptab));
1970 memset(f->symtab, 0, sizeof(f->symtab));
1972 for (i = 0; i < HASH_BUCKETS; ++i)
1973 for (sym = tmptab[i]; sym; sym = next) {
1974 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1976 sym->next = f->symtab[h];
1982 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1984 static struct obj_symbol *
1985 obj_add_symbol(struct obj_file *f, const char *name,
1986 unsigned long symidx, int info,
1987 int secidx, ElfW(Addr) value,
1990 struct obj_symbol *sym;
1991 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1992 int n_type = ELF_ST_TYPE(info);
1993 int n_binding = ELF_ST_BIND(info);
1995 for (sym = f->symtab[hash]; sym; sym = sym->next)
1996 if (f->symbol_cmp(sym->name, name) == 0) {
1997 int o_secidx = sym->secidx;
1998 int o_info = sym->info;
1999 int o_type = ELF_ST_TYPE(o_info);
2000 int o_binding = ELF_ST_BIND(o_info);
2002 /* A redefinition! Is it legal? */
2004 if (secidx == SHN_UNDEF)
2006 else if (o_secidx == SHN_UNDEF)
2008 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2009 /* Cope with local and global symbols of the same name
2010 in the same object file, as might have been created
2011 by ld -r. The only reason locals are now seen at this
2012 level at all is so that we can do semi-sensible things
2015 struct obj_symbol *nsym, **p;
2017 nsym = arch_new_symbol();
2018 nsym->next = sym->next;
2021 /* Excise the old (local) symbol from the hash chain. */
2022 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2026 } else if (n_binding == STB_LOCAL) {
2027 /* Another symbol of the same name has already been defined.
2028 Just add this to the local table. */
2029 sym = arch_new_symbol();
2032 f->local_symtab[symidx] = sym;
2034 } else if (n_binding == STB_WEAK)
2036 else if (o_binding == STB_WEAK)
2038 /* Don't unify COMMON symbols with object types the programmer
2040 else if (secidx == SHN_COMMON
2041 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2043 else if (o_secidx == SHN_COMMON
2044 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2047 /* Don't report an error if the symbol is coming from
2048 the kernel or some external module. */
2049 if (secidx <= SHN_HIRESERVE)
2050 bb_error_msg("%s multiply defined", name);
2055 /* Completely new symbol. */
2056 sym = arch_new_symbol();
2057 sym->next = f->symtab[hash];
2058 f->symtab[hash] = sym;
2061 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2062 if (symidx >= f->local_symtab_size)
2063 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2064 name, (long) symidx, (long) f->local_symtab_size);
2066 f->local_symtab[symidx] = sym;
2073 sym->secidx = secidx;
2079 static struct obj_symbol *
2080 obj_find_symbol(struct obj_file *f, const char *name)
2082 struct obj_symbol *sym;
2083 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2085 for (sym = f->symtab[hash]; sym; sym = sym->next)
2086 if (f->symbol_cmp(sym->name, name) == 0)
2093 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2096 if (sym->secidx >= SHN_LORESERVE)
2099 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2101 /* As a special case, a NULL sym has value zero. */
2106 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2108 int i, n = f->header.e_shnum;
2110 for (i = 0; i < n; ++i)
2111 if (strcmp(f->sections[i]->name, name) == 0)
2112 return f->sections[i];
2117 static int obj_load_order_prio(struct obj_section *a)
2119 unsigned long af, ac;
2121 af = a->header.sh_flags;
2124 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2125 strcmp(a->name + 5, ".init"))
2129 if (!(af & SHF_WRITE))
2131 if (af & SHF_EXECINSTR)
2133 if (a->header.sh_type != SHT_NOBITS)
2140 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2142 struct obj_section **p;
2143 int prio = obj_load_order_prio(sec);
2144 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2145 if (obj_load_order_prio(*p) < prio)
2147 sec->load_next = *p;
2151 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2153 unsigned long align,
2156 int newidx = f->header.e_shnum++;
2157 struct obj_section *sec;
2159 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2160 f->sections[newidx] = sec = arch_new_section();
2162 memset(sec, 0, sizeof(*sec));
2163 sec->header.sh_type = SHT_PROGBITS;
2164 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2165 sec->header.sh_size = size;
2166 sec->header.sh_addralign = align;
2170 sec->contents = xmalloc(size);
2172 obj_insert_section_load_order(f, sec);
2177 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2179 unsigned long align,
2182 int newidx = f->header.e_shnum++;
2183 struct obj_section *sec;
2185 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2186 f->sections[newidx] = sec = arch_new_section();
2188 memset(sec, 0, sizeof(*sec));
2189 sec->header.sh_type = SHT_PROGBITS;
2190 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2191 sec->header.sh_size = size;
2192 sec->header.sh_addralign = align;
2196 sec->contents = xmalloc(size);
2198 sec->load_next = f->load_order;
2199 f->load_order = sec;
2200 if (f->load_order_search_start == &f->load_order)
2201 f->load_order_search_start = &sec->load_next;
2206 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2208 unsigned long oldsize = sec->header.sh_size;
2210 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2212 return sec->contents + oldsize;
2216 /* Conditionally add the symbols from the given symbol set to the
2220 add_symbols_from( struct obj_file *f,
2221 int idx, struct new_module_symbol *syms, size_t nsyms)
2223 struct new_module_symbol *s;
2226 #ifdef SYMBOL_PREFIX
2228 size_t name_alloced_size = 0;
2230 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2233 gpl = obj_gpl_license(f, NULL) == 0;
2235 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2236 /* Only add symbols that are already marked external.
2237 If we override locals we may cause problems for
2238 argument initialization. We will also create a false
2239 dependency on the module. */
2240 struct obj_symbol *sym;
2243 /* GPL licensed modules can use symbols exported with
2244 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2245 * exported names. Non-GPL modules never see any GPLONLY_
2246 * symbols so they cannot fudge it by adding the prefix on
2249 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2250 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2257 name = (char *)s->name;
2259 #ifdef SYMBOL_PREFIX
2260 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2261 kernel exports `C names', but module object files
2262 reference `linker names'). */
2263 size_t extra = sizeof SYMBOL_PREFIX;
2264 size_t name_size = strlen (name) + extra;
2265 if (name_size > name_alloced_size) {
2266 name_alloced_size = name_size * 2;
2267 name_buf = alloca (name_alloced_size);
2269 strcpy (name_buf, SYMBOL_PREFIX);
2270 strcpy (name_buf + extra - 1, name);
2272 #endif /* SYMBOL_PREFIX */
2274 sym = obj_find_symbol(f, name);
2275 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2276 #ifdef SYMBOL_PREFIX
2277 /* Put NAME_BUF into more permanent storage. */
2278 name = xmalloc (name_size);
2279 strcpy (name, name_buf);
2281 sym = obj_add_symbol(f, name, -1,
2282 ELF_ST_INFO(STB_GLOBAL,
2285 /* Did our symbol just get installed? If so, mark the
2286 module as "used". */
2287 if (sym->secidx == idx)
2295 static void add_kernel_symbols(struct obj_file *f)
2297 struct external_module *m;
2300 /* Add module symbols first. */
2302 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2304 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2305 m->nsyms)) m->used = 1, ++nused;
2307 n_ext_modules_used = nused;
2309 /* And finally the symbols from the kernel proper. */
2312 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2315 static char *get_modinfo_value(struct obj_file *f, const char *key)
2317 struct obj_section *sec;
2318 char *p, *v, *n, *ep;
2319 size_t klen = strlen(key);
2321 sec = obj_find_section(f, ".modinfo");
2325 ep = p + sec->header.sh_size;
2328 n = strchr(p, '\0');
2330 if (p + klen == v && strncmp(p, key, klen) == 0)
2333 if (p + klen == n && strcmp(p, key) == 0)
2343 /*======================================================================*/
2344 /* Functions relating to module loading after 2.1.18. */
2347 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2350 char *p, *q, *key, *sym_name;
2351 struct obj_symbol *sym;
2352 char *contents, *loc;
2356 if ((q = strchr(p, '=')) == NULL) {
2361 key = alloca(q - p + 6);
2362 memcpy(key, "parm_", 5);
2363 memcpy(key + 5, p, q - p);
2366 p = get_modinfo_value(f, key);
2369 bb_error_msg("invalid parameter %s", key);
2373 #ifdef SYMBOL_PREFIX
2374 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2375 strcpy (sym_name, SYMBOL_PREFIX);
2376 strcat (sym_name, key);
2380 sym = obj_find_symbol(f, sym_name);
2382 /* Also check that the parameter was not resolved from the kernel. */
2383 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2384 bb_error_msg("symbol for parameter %s not found", key);
2389 min = strtoul(p, &p, 10);
2391 max = strtoul(p + 1, &p, 10);
2397 contents = f->sections[sym->secidx]->contents;
2398 loc = contents + sym->value;
2402 if ((*p == 's') || (*p == 'c')) {
2405 /* Do C quoting if we begin with a ", else slurp the lot. */
2409 str = alloca(strlen(q));
2410 for (r = str, q++; *q != '"'; ++q, ++r) {
2412 bb_error_msg("improperly terminated string argument for %s",
2415 } else if (*q == '\\')
2449 if (q[1] >= '0' && q[1] <= '7') {
2450 c = (c * 8) + *++q - '0';
2451 if (q[1] >= '0' && q[1] <= '7')
2452 c = (c * 8) + *++q - '0';
2469 /* In this case, the string is not quoted. We will break
2470 it using the coma (like for ints). If the user wants to
2471 include comas in a string, he just has to quote it */
2473 /* Search the next coma */
2477 if (r != (char *) NULL) {
2478 /* Recopy the current field */
2479 str = alloca(r - q + 1);
2480 memcpy(str, q, r - q);
2482 /* I don't know if it is useful, as the previous case
2483 doesn't nul terminate the string ??? */
2486 /* Keep next fields */
2497 obj_string_patch(f, sym->secidx, loc - contents, str);
2498 loc += tgt_sizeof_char_p;
2500 /* Array of chars (in fact, matrix !) */
2501 unsigned long charssize; /* size of each member */
2503 /* Get the size of each member */
2504 /* Probably we should do that outside the loop ? */
2505 if (!isdigit(*(p + 1))) {
2506 bb_error_msg("parameter type 'c' for %s must be followed by"
2507 " the maximum size", key);
2510 charssize = strtoul(p + 1, (char **) NULL, 10);
2513 if (strlen(str) >= charssize) {
2514 bb_error_msg("string too long for %s (max %ld)", key,
2519 /* Copy to location */
2520 strcpy((char *) loc, str);
2524 long v = strtoul(q, &q, 0);
2531 loc += tgt_sizeof_short;
2535 loc += tgt_sizeof_int;
2539 loc += tgt_sizeof_long;
2543 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2558 goto retry_end_of_value;
2562 bb_error_msg("too many values for %s (max %d)", key, max);
2569 bb_error_msg("invalid argument syntax for %s", key);
2576 bb_error_msg("too few values for %s (min %d)", key, min);
2586 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2587 static int new_is_module_checksummed(struct obj_file *f)
2589 const char *p = get_modinfo_value(f, "using_checksums");
2596 /* Get the module's kernel version in the canonical integer form. */
2599 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2604 p = get_modinfo_value(f, "kernel_version");
2607 safe_strncpy(str, p, STRVERSIONLEN);
2609 a = strtoul(p, &p, 10);
2612 b = strtoul(p + 1, &p, 10);
2615 c = strtoul(p + 1, &q, 10);
2619 return a << 16 | b << 8 | c;
2622 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2625 /* Fetch the loaded modules, and all currently exported symbols. */
2627 static int new_get_kernel_symbols(void)
2629 char *module_names, *mn;
2630 struct external_module *modules, *m;
2631 struct new_module_symbol *syms, *s;
2632 size_t ret, bufsize, nmod, nsyms, i, j;
2634 /* Collect the loaded modules. */
2636 module_names = xmalloc(bufsize = 256);
2638 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2639 if (errno == ENOSPC && bufsize < ret) {
2640 module_names = xrealloc(module_names, bufsize = ret);
2641 goto retry_modules_load;
2643 bb_perror_msg("QM_MODULES");
2647 n_ext_modules = nmod = ret;
2649 /* Collect the modules' symbols. */
2652 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2653 memset(modules, 0, nmod * sizeof(*modules));
2654 for (i = 0, mn = module_names, m = modules;
2655 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2656 struct new_module_info info;
2658 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2659 if (errno == ENOENT) {
2660 /* The module was removed out from underneath us. */
2663 bb_perror_msg("query_module: QM_INFO: %s", mn);
2667 syms = xmalloc(bufsize = 1024);
2669 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2672 syms = xrealloc(syms, bufsize = ret);
2673 goto retry_mod_sym_load;
2675 /* The module was removed out from underneath us. */
2678 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2685 m->addr = info.addr;
2689 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2690 s->name += (unsigned long) syms;
2695 /* Collect the kernel's symbols. */
2697 syms = xmalloc(bufsize = 16 * 1024);
2698 retry_kern_sym_load:
2699 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2700 if (errno == ENOSPC && bufsize < ret) {
2701 syms = xrealloc(syms, bufsize = ret);
2702 goto retry_kern_sym_load;
2704 bb_perror_msg("kernel: QM_SYMBOLS");
2707 nksyms = nsyms = ret;
2710 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2711 s->name += (unsigned long) syms;
2717 /* Return the kernel symbol checksum version, or zero if not used. */
2719 static int new_is_kernel_checksummed(void)
2721 struct new_module_symbol *s;
2724 /* Using_Versions is not the first symbol, but it should be in there. */
2726 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2727 if (strcmp((char *) s->name, "Using_Versions") == 0)
2734 static int new_create_this_module(struct obj_file *f, const char *m_name)
2736 struct obj_section *sec;
2738 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2739 sizeof(struct new_module));
2740 memset(sec->contents, 0, sizeof(struct new_module));
2742 obj_add_symbol(f, SPFX "__this_module", -1,
2743 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2744 sizeof(struct new_module));
2746 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2752 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2753 /* add an entry to the __ksymtab section, creating it if necessary */
2754 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2756 struct obj_section *sec;
2759 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2760 * If __ksymtab is defined but not marked alloc, x out the first character
2761 * (no obj_delete routine) and create a new __ksymtab with the correct
2764 sec = obj_find_section(f, "__ksymtab");
2765 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2766 *((char *)(sec->name)) = 'x'; /* override const */
2770 sec = obj_create_alloced_section(f, "__ksymtab",
2771 tgt_sizeof_void_p, 0);
2774 sec->header.sh_flags |= SHF_ALLOC;
2775 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2777 ofs = sec->header.sh_size;
2778 obj_symbol_patch(f, sec->idx, ofs, sym);
2779 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2780 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2782 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2784 static int new_create_module_ksymtab(struct obj_file *f)
2786 struct obj_section *sec;
2789 /* We must always add the module references. */
2791 if (n_ext_modules_used) {
2792 struct new_module_ref *dep;
2793 struct obj_symbol *tm;
2795 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2796 (sizeof(struct new_module_ref)
2797 * n_ext_modules_used));
2801 tm = obj_find_symbol(f, SPFX "__this_module");
2802 dep = (struct new_module_ref *) sec->contents;
2803 for (i = 0; i < n_ext_modules; ++i)
2804 if (ext_modules[i].used) {
2805 dep->dep = ext_modules[i].addr;
2806 obj_symbol_patch(f, sec->idx,
2807 (char *) &dep->ref - sec->contents, tm);
2813 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2818 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2821 /* We don't want to export symbols residing in sections that
2822 aren't loaded. There are a number of these created so that
2823 we make sure certain module options don't appear twice. */
2825 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2827 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2829 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2830 struct obj_symbol *sym;
2831 for (sym = f->symtab[i]; sym; sym = sym->next)
2832 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2833 && sym->secidx <= SHN_HIRESERVE
2834 && (sym->secidx >= SHN_LORESERVE
2835 || loaded[sym->secidx])) {
2836 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2838 obj_symbol_patch(f, sec->idx, ofs, sym);
2839 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2846 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2854 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2856 struct new_module *module;
2857 struct obj_section *sec;
2862 sec = obj_find_section(f, ".this");
2863 if (!sec || !sec->contents) {
2864 bb_perror_msg_and_die("corrupt module %s?",m_name);
2866 module = (struct new_module *) sec->contents;
2867 m_addr = sec->header.sh_addr;
2869 module->size_of_struct = sizeof(*module);
2870 module->size = m_size;
2871 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2873 sec = obj_find_section(f, "__ksymtab");
2874 if (sec && sec->header.sh_size) {
2875 module->syms = sec->header.sh_addr;
2876 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2879 if (n_ext_modules_used) {
2880 sec = obj_find_section(f, ".kmodtab");
2881 module->deps = sec->header.sh_addr;
2882 module->ndeps = n_ext_modules_used;
2886 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2888 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2890 sec = obj_find_section(f, "__ex_table");
2892 module->ex_table_start = sec->header.sh_addr;
2893 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2896 sec = obj_find_section(f, ".text.init");
2898 module->runsize = sec->header.sh_addr - m_addr;
2900 sec = obj_find_section(f, ".data.init");
2902 if (!module->runsize ||
2903 module->runsize > sec->header.sh_addr - m_addr)
2904 module->runsize = sec->header.sh_addr - m_addr;
2906 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2907 if (sec && sec->header.sh_size) {
2908 module->archdata_start = (void*)sec->header.sh_addr;
2909 module->archdata_end = module->archdata_start + sec->header.sh_size;
2911 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2912 if (sec && sec->header.sh_size) {
2913 module->kallsyms_start = (void*)sec->header.sh_addr;
2914 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2917 /* Whew! All of the initialization is complete. Collect the final
2918 module image and give it to the kernel. */
2920 image = xmalloc(m_size);
2921 obj_create_image(f, image);
2923 ret = init_module(m_name, (struct new_module *) image);
2925 bb_perror_msg("init_module: %s", m_name);
2933 /*======================================================================*/
2936 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2939 struct obj_string_patch *p;
2940 struct obj_section *strsec;
2941 size_t len = strlen(string) + 1;
2944 p = xmalloc(sizeof(*p));
2945 p->next = f->string_patches;
2946 p->reloc_secidx = secidx;
2947 p->reloc_offset = offset;
2948 f->string_patches = p;
2950 strsec = obj_find_section(f, ".kstrtab");
2951 if (strsec == NULL) {
2952 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2953 p->string_offset = 0;
2954 loc = strsec->contents;
2956 p->string_offset = strsec->header.sh_size;
2957 loc = obj_extend_section(strsec, len);
2959 memcpy(loc, string, len);
2965 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2966 struct obj_symbol *sym)
2968 struct obj_symbol_patch *p;
2970 p = xmalloc(sizeof(*p));
2971 p->next = f->symbol_patches;
2972 p->reloc_secidx = secidx;
2973 p->reloc_offset = offset;
2975 f->symbol_patches = p;
2980 static int obj_check_undefineds(struct obj_file *f)
2985 for (i = 0; i < HASH_BUCKETS; ++i) {
2986 struct obj_symbol *sym;
2987 for (sym = f->symtab[i]; sym; sym = sym->next)
2988 if (sym->secidx == SHN_UNDEF) {
2989 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
2990 sym->secidx = SHN_ABS;
2994 bb_error_msg("unresolved symbol %s", sym->name);
3004 static void obj_allocate_commons(struct obj_file *f)
3006 struct common_entry {
3007 struct common_entry *next;
3008 struct obj_symbol *sym;
3009 } *common_head = NULL;
3013 for (i = 0; i < HASH_BUCKETS; ++i) {
3014 struct obj_symbol *sym;
3015 for (sym = f->symtab[i]; sym; sym = sym->next)
3016 if (sym->secidx == SHN_COMMON) {
3017 /* Collect all COMMON symbols and sort them by size so as to
3018 minimize space wasted by alignment requirements. */
3020 struct common_entry **p, *n;
3021 for (p = &common_head; *p; p = &(*p)->next)
3022 if (sym->size <= (*p)->sym->size)
3025 n = alloca(sizeof(*n));
3033 for (i = 1; i < f->local_symtab_size; ++i) {
3034 struct obj_symbol *sym = f->local_symtab[i];
3035 if (sym && sym->secidx == SHN_COMMON) {
3036 struct common_entry **p, *n;
3037 for (p = &common_head; *p; p = &(*p)->next)
3038 if (sym == (*p)->sym)
3040 else if (sym->size < (*p)->sym->size) {
3041 n = alloca(sizeof(*n));
3051 /* Find the bss section. */
3052 for (i = 0; i < f->header.e_shnum; ++i)
3053 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3056 /* If for some reason there hadn't been one, create one. */
3057 if (i == f->header.e_shnum) {
3058 struct obj_section *sec;
3060 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3061 f->sections[i] = sec = arch_new_section();
3062 f->header.e_shnum = i + 1;
3064 memset(sec, 0, sizeof(*sec));
3065 sec->header.sh_type = SHT_PROGBITS;
3066 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3071 /* Allocate the COMMONS. */
3073 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3074 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3075 struct common_entry *c;
3077 for (c = common_head; c; c = c->next) {
3078 ElfW(Addr) align = c->sym->value;
3080 if (align > max_align)
3082 if (bss_size & (align - 1))
3083 bss_size = (bss_size | (align - 1)) + 1;
3086 c->sym->value = bss_size;
3088 bss_size += c->sym->size;
3091 f->sections[i]->header.sh_size = bss_size;
3092 f->sections[i]->header.sh_addralign = max_align;
3096 /* For the sake of patch relocation and parameter initialization,
3097 allocate zeroed data for NOBITS sections now. Note that after
3098 this we cannot assume NOBITS are really empty. */
3099 for (i = 0; i < f->header.e_shnum; ++i) {
3100 struct obj_section *s = f->sections[i];
3101 if (s->header.sh_type == SHT_NOBITS) {
3102 if (s->header.sh_size != 0)
3103 s->contents = memset(xmalloc(s->header.sh_size),
3104 0, s->header.sh_size);
3108 s->header.sh_type = SHT_PROGBITS;
3113 static unsigned long obj_load_size(struct obj_file *f)
3115 unsigned long dot = 0;
3116 struct obj_section *sec;
3118 /* Finalize the positions of the sections relative to one another. */
3120 for (sec = f->load_order; sec; sec = sec->load_next) {
3123 align = sec->header.sh_addralign;
3124 if (align && (dot & (align - 1)))
3125 dot = (dot | (align - 1)) + 1;
3127 sec->header.sh_addr = dot;
3128 dot += sec->header.sh_size;
3134 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3136 int i, n = f->header.e_shnum;
3139 /* Finalize the addresses of the sections. */
3142 for (i = 0; i < n; ++i)
3143 f->sections[i]->header.sh_addr += base;
3145 /* And iterate over all of the relocations. */
3147 for (i = 0; i < n; ++i) {
3148 struct obj_section *relsec, *symsec, *targsec, *strsec;
3149 ElfW(RelM) * rel, *relend;
3153 relsec = f->sections[i];
3154 if (relsec->header.sh_type != SHT_RELM)
3157 symsec = f->sections[relsec->header.sh_link];
3158 targsec = f->sections[relsec->header.sh_info];
3159 strsec = f->sections[symsec->header.sh_link];
3161 rel = (ElfW(RelM) *) relsec->contents;
3162 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3163 symtab = (ElfW(Sym) *) symsec->contents;
3164 strtab = (const char *) strsec->contents;
3166 for (; rel < relend; ++rel) {
3167 ElfW(Addr) value = 0;
3168 struct obj_symbol *intsym = NULL;
3169 unsigned long symndx;
3170 ElfW(Sym) * extsym = 0;
3173 /* Attempt to find a value to use for this relocation. */
3175 symndx = ELF_R_SYM(rel->r_info);
3177 /* Note we've already checked for undefined symbols. */
3179 extsym = &symtab[symndx];
3180 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3181 /* Local symbols we look up in the local table to be sure
3182 we get the one that is really intended. */
3183 intsym = f->local_symtab[symndx];
3185 /* Others we look up in the hash table. */
3187 if (extsym->st_name)
3188 name = strtab + extsym->st_name;
3190 name = f->sections[extsym->st_shndx]->name;
3191 intsym = obj_find_symbol(f, name);
3194 value = obj_symbol_final_value(f, intsym);
3195 intsym->referenced = 1;
3197 #if SHT_RELM == SHT_RELA
3198 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3199 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3200 if (!extsym || !extsym->st_name ||
3201 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3203 value += rel->r_addend;
3207 switch (arch_apply_relocation
3208 (f, targsec, symsec, intsym, rel, value)) {
3212 case obj_reloc_overflow:
3213 errmsg = "Relocation overflow";
3215 case obj_reloc_dangerous:
3216 errmsg = "Dangerous relocation";
3218 case obj_reloc_unhandled:
3219 errmsg = "Unhandled relocation";
3222 bb_error_msg("%s of type %ld for %s", errmsg,
3223 (long) ELF_R_TYPE(rel->r_info),
3224 strtab + extsym->st_name);
3226 bb_error_msg("%s of type %ld", errmsg,
3227 (long) ELF_R_TYPE(rel->r_info));
3235 /* Finally, take care of the patches. */
3237 if (f->string_patches) {
3238 struct obj_string_patch *p;
3239 struct obj_section *strsec;
3240 ElfW(Addr) strsec_base;
3241 strsec = obj_find_section(f, ".kstrtab");
3242 strsec_base = strsec->header.sh_addr;
3244 for (p = f->string_patches; p; p = p->next) {
3245 struct obj_section *targsec = f->sections[p->reloc_secidx];
3246 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3247 = strsec_base + p->string_offset;
3251 if (f->symbol_patches) {
3252 struct obj_symbol_patch *p;
3254 for (p = f->symbol_patches; p; p = p->next) {
3255 struct obj_section *targsec = f->sections[p->reloc_secidx];
3256 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3257 = obj_symbol_final_value(f, p->sym);
3264 static int obj_create_image(struct obj_file *f, char *image)
3266 struct obj_section *sec;
3267 ElfW(Addr) base = f->baseaddr;
3269 for (sec = f->load_order; sec; sec = sec->load_next) {
3272 if (sec->contents == 0 || sec->header.sh_size == 0)
3275 secimg = image + (sec->header.sh_addr - base);
3277 /* Note that we allocated data for NOBITS sections earlier. */
3278 memcpy(secimg, sec->contents, sec->header.sh_size);
3284 /*======================================================================*/
3286 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3289 ElfW(Shdr) * section_headers;
3293 /* Read the file header. */
3295 f = arch_new_file();
3296 memset(f, 0, sizeof(*f));
3297 f->symbol_cmp = strcmp;
3298 f->symbol_hash = obj_elf_hash;
3299 f->load_order_search_start = &f->load_order;
3301 fseek(fp, 0, SEEK_SET);
3302 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3303 bb_perror_msg("error reading ELF header");
3307 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3308 || f->header.e_ident[EI_MAG1] != ELFMAG1
3309 || f->header.e_ident[EI_MAG2] != ELFMAG2
3310 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3311 bb_error_msg("not an ELF file");
3314 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3315 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3316 ? ELFDATA2MSB : ELFDATA2LSB)
3317 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3318 || !MATCH_MACHINE(f->header.e_machine)) {
3319 bb_error_msg("ELF file not for this architecture");
3322 if (f->header.e_type != ET_REL) {
3323 bb_error_msg("ELF file not a relocatable object");
3327 /* Read the section headers. */
3329 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3330 bb_error_msg("section header size mismatch: %lu != %lu",
3331 (unsigned long) f->header.e_shentsize,
3332 (unsigned long) sizeof(ElfW(Shdr)));
3336 shnum = f->header.e_shnum;
3337 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3338 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3340 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3341 fseek(fp, f->header.e_shoff, SEEK_SET);
3342 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3343 bb_perror_msg("error reading ELF section headers");
3347 /* Read the section data. */
3349 for (i = 0; i < shnum; ++i) {
3350 struct obj_section *sec;
3352 f->sections[i] = sec = arch_new_section();
3353 memset(sec, 0, sizeof(*sec));
3355 sec->header = section_headers[i];
3358 if(sec->header.sh_size) switch (sec->header.sh_type) {
3367 if (!loadprogbits) {
3368 sec->contents = NULL;
3375 if (sec->header.sh_size > 0) {
3376 sec->contents = xmalloc(sec->header.sh_size);
3377 fseek(fp, sec->header.sh_offset, SEEK_SET);
3378 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3379 bb_perror_msg("error reading ELF section data");
3383 sec->contents = NULL;
3387 #if SHT_RELM == SHT_REL
3389 bb_error_msg("RELA relocations not supported on this architecture");
3393 bb_error_msg("REL relocations not supported on this architecture");
3398 if (sec->header.sh_type >= SHT_LOPROC) {
3399 /* Assume processor specific section types are debug
3400 info and can safely be ignored. If this is ever not
3401 the case (Hello MIPS?), don't put ifdefs here but
3402 create an arch_load_proc_section(). */
3406 bb_error_msg("can't handle sections of type %ld",
3407 (long) sec->header.sh_type);
3412 /* Do what sort of interpretation as needed by each section. */
3414 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3416 for (i = 0; i < shnum; ++i) {
3417 struct obj_section *sec = f->sections[i];
3418 sec->name = shstrtab + sec->header.sh_name;
3421 for (i = 0; i < shnum; ++i) {
3422 struct obj_section *sec = f->sections[i];
3424 /* .modinfo should be contents only but gcc has no attribute for that.
3425 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3427 if (strcmp(sec->name, ".modinfo") == 0)
3428 sec->header.sh_flags &= ~SHF_ALLOC;
3430 if (sec->header.sh_flags & SHF_ALLOC)
3431 obj_insert_section_load_order(f, sec);
3433 switch (sec->header.sh_type) {
3436 unsigned long nsym, j;
3440 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3441 bb_error_msg("symbol size mismatch: %lu != %lu",
3442 (unsigned long) sec->header.sh_entsize,
3443 (unsigned long) sizeof(ElfW(Sym)));
3447 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3448 strtab = f->sections[sec->header.sh_link]->contents;
3449 sym = (ElfW(Sym) *) sec->contents;
3451 /* Allocate space for a table of local symbols. */
3452 j = f->local_symtab_size = sec->header.sh_info;
3453 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3455 /* Insert all symbols into the hash table. */
3456 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3457 ElfW(Addr) val = sym->st_value;
3460 name = strtab + sym->st_name;
3461 else if (sym->st_shndx < shnum)
3462 name = f->sections[sym->st_shndx]->name;
3466 #if defined(__SH5__)
3468 * For sh64 it is possible that the target of a branch
3469 * requires a mode switch (32 to 16 and back again).
3471 * This is implied by the lsb being set in the target
3472 * address for SHmedia mode and clear for SHcompact.
3474 val |= sym->st_other & 4;
3477 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3484 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3485 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3486 (unsigned long) sec->header.sh_entsize,
3487 (unsigned long) sizeof(ElfW(RelM)));
3491 /* XXX Relocation code from modutils-2.3.19 is not here.
3492 * Why? That's about 20 lines of code from obj/obj_load.c,
3493 * which gets done in a second pass through the sections.
3494 * This BusyBox insmod does similar work in obj_relocate(). */
3501 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3503 * load the unloaded sections directly into the memory allocated by
3504 * kernel for the module
3507 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3509 ElfW(Addr) base = f->baseaddr;
3510 struct obj_section* sec;
3512 for (sec = f->load_order; sec; sec = sec->load_next) {
3514 /* section already loaded? */
3515 if (sec->contents != NULL)
3518 if (sec->header.sh_size == 0)
3521 sec->contents = imagebase + (sec->header.sh_addr - base);
3522 fseek(fp, sec->header.sh_offset, SEEK_SET);
3523 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3524 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3533 static void hide_special_symbols(struct obj_file *f)
3535 static const char *const specials[] = {
3536 SPFX "cleanup_module",
3538 SPFX "kernel_version",
3542 struct obj_symbol *sym;
3543 const char *const *p;
3545 for (p = specials; *p; ++p)
3546 if ((sym = obj_find_symbol(f, *p)) != NULL)
3548 ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3552 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3553 static int obj_gpl_license(struct obj_file *f, const char **license)
3555 struct obj_section *sec;
3556 /* This list must match *exactly* the list of allowable licenses in
3557 * linux/include/linux/module.h. Checking for leading "GPL" will not
3558 * work, somebody will use "GPL sucks, this is proprietary".
3560 static const char * const gpl_licenses[] = {
3563 "GPL and additional rights",
3568 if ((sec = obj_find_section(f, ".modinfo"))) {
3569 const char *value, *ptr, *endptr;
3570 ptr = sec->contents;
3571 endptr = ptr + sec->header.sh_size;
3572 while (ptr < endptr) {
3573 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3577 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3578 if (strcmp(value+1, gpl_licenses[i]) == 0)
3583 if (strchr(ptr, '\0'))
3584 ptr = strchr(ptr, '\0') + 1;
3592 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3593 #define TAINT_PROPRIETORY_MODULE (1<<0)
3594 #define TAINT_FORCED_MODULE (1<<1)
3595 #define TAINT_UNSAFE_SMP (1<<2)
3596 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3598 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3599 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3603 static int first = 1;
3604 if (fd < 0 && !kernel_has_tainted)
3605 return; /* New modutils on old kernel */
3606 printf("Warning: loading %s will taint the kernel: %s%s\n",
3607 m_name, text1, text2);
3609 printf(" See %s for information about tainted modules\n", TAINT_URL);
3613 read(fd, buf, sizeof(buf)-1);
3614 buf[sizeof(buf)-1] = '\0';
3615 oldval = strtoul(buf, NULL, 10);
3616 sprintf(buf, "%d\n", oldval | taint);
3617 write(fd, buf, strlen(buf));
3621 /* Check if loading this module will taint the kernel. */
3622 static void check_tainted_module(struct obj_file *f, char *m_name)
3624 static const char tainted_file[] = TAINT_FILENAME;
3625 int fd, kernel_has_tainted;
3628 kernel_has_tainted = 1;
3629 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3630 if (errno == ENOENT)
3631 kernel_has_tainted = 0;
3632 else if (errno == EACCES)
3633 kernel_has_tainted = 1;
3635 perror(tainted_file);
3636 kernel_has_tainted = 0;
3640 switch (obj_gpl_license(f, &ptr)) {
3644 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3647 /* The module has a non-GPL license so we pretend that the
3648 * kernel always has a taint flag to get a warning even on
3649 * kernels without the proc flag.
3651 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3654 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3658 if (flag_force_load)
3659 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3664 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3665 #define check_tainted_module(x, y) do { } while(0);
3666 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3668 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3669 /* add module source, timestamp, kernel version and a symbol for the
3670 * start of some sections. this info is used by ksymoops to do better
3674 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3676 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3677 return new_get_module_version(f, str);
3678 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3679 strncpy(str, "???", sizeof(str));
3681 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3684 /* add module source, timestamp, kernel version and a symbol for the
3685 * start of some sections. this info is used by ksymoops to do better
3689 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3692 static const char symprefix[] = "__insmod_";
3693 struct obj_section *sec;
3694 struct obj_symbol *sym;
3695 char *name, *absolute_filename;
3696 char str[STRVERSIONLEN], real[PATH_MAX];
3697 int i, l, lm_name, lfilename, use_ksymtab, version;
3698 struct stat statbuf;
3700 static const char *section_names[] = {
3708 if (realpath(filename, real)) {
3709 absolute_filename = bb_xstrdup(real);
3712 int save_errno = errno;
3713 bb_error_msg("cannot get realpath for %s", filename);
3716 absolute_filename = bb_xstrdup(filename);
3719 lm_name = strlen(m_name);
3720 lfilename = strlen(absolute_filename);
3722 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3723 * are not to be exported. otherwise leave ksymtab alone for now, the
3724 * "export all symbols" compatibility code will export these symbols later.
3726 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3728 if ((sec = obj_find_section(f, ".this"))) {
3729 /* tag the module header with the object name, last modified
3730 * timestamp and module version. worst case for module version
3731 * is 0xffffff, decimal 16777215. putting all three fields in
3732 * one symbol is less readable but saves kernel space.
3734 l = sizeof(symprefix)+ /* "__insmod_" */
3735 lm_name+ /* module name */
3737 lfilename+ /* object filename */
3739 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3741 8+ /* version in dec */
3744 if (stat(absolute_filename, &statbuf) != 0)
3745 statbuf.st_mtime = 0;
3746 version = get_module_version(f, str); /* -1 if not found */
3747 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3748 symprefix, m_name, absolute_filename,
3749 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3751 sym = obj_add_symbol(f, name, -1,
3752 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3753 sec->idx, sec->header.sh_addr, 0);
3755 new_add_ksymtab(f, sym);
3757 free(absolute_filename);
3758 #ifdef _NOT_SUPPORTED_
3759 /* record where the persistent data is going, same address as previous symbol */
3762 l = sizeof(symprefix)+ /* "__insmod_" */
3763 lm_name+ /* module name */
3765 strlen(f->persist)+ /* data store */
3768 snprintf(name, l, "%s%s_P%s",
3769 symprefix, m_name, f->persist);
3770 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3771 sec->idx, sec->header.sh_addr, 0);
3773 new_add_ksymtab(f, sym);
3775 #endif /* _NOT_SUPPORTED_ */
3776 /* tag the desired sections if size is non-zero */
3778 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3779 if ((sec = obj_find_section(f, section_names[i])) &&
3780 sec->header.sh_size) {
3781 l = sizeof(symprefix)+ /* "__insmod_" */
3782 lm_name+ /* module name */
3784 strlen(sec->name)+ /* section name */
3786 8+ /* length in dec */
3789 snprintf(name, l, "%s%s_S%s_L%ld",
3790 symprefix, m_name, sec->name,
3791 (long)sec->header.sh_size);
3792 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3793 sec->idx, sec->header.sh_addr, 0);
3795 new_add_ksymtab(f, sym);
3799 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3801 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3802 static void print_load_map(struct obj_file *f)
3804 struct obj_symbol *sym;
3805 struct obj_symbol **all, **p;
3806 struct obj_section *sec;
3807 int i, nsyms, *loaded;
3809 /* Report on the section layout. */
3811 printf("Sections: Size %-*s Align\n",
3812 (int) (2 * sizeof(void *)), "Address");
3814 for (sec = f->load_order; sec; sec = sec->load_next) {
3818 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3823 printf("%-15s %08lx %0*lx 2**%d\n",
3825 (long)sec->header.sh_size,
3826 (int) (2 * sizeof(void *)),
3827 (long)sec->header.sh_addr,
3830 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3831 /* Quick reference which section indicies are loaded. */
3833 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3835 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3837 /* Collect the symbols we'll be listing. */
3839 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3840 for (sym = f->symtab[i]; sym; sym = sym->next)
3841 if (sym->secidx <= SHN_HIRESERVE
3842 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3845 all = alloca(nsyms * sizeof(struct obj_symbol *));
3847 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3848 for (sym = f->symtab[i]; sym; sym = sym->next)
3849 if (sym->secidx <= SHN_HIRESERVE
3850 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3853 /* And list them. */
3854 printf("\nSymbols:\n");
3855 for (p = all; p < all + nsyms; ++p) {
3857 unsigned long value;
3860 if (sym->secidx == SHN_ABS) {
3863 } else if (sym->secidx == SHN_UNDEF) {
3867 sec = f->sections[sym->secidx];
3869 if (sec->header.sh_type == SHT_NOBITS)
3871 else if (sec->header.sh_flags & SHF_ALLOC) {
3872 if (sec->header.sh_flags & SHF_EXECINSTR)
3874 else if (sec->header.sh_flags & SHF_WRITE)
3879 value = sym->value + sec->header.sh_addr;
3882 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3883 type = tolower(type);
3885 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3893 int insmod_main( int argc, char **argv)
3899 unsigned long m_size;
3904 int exit_status = EXIT_FAILURE;
3906 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3907 struct utsname uts_info;
3908 char m_strversion[STRVERSIONLEN];
3909 int m_version, m_crcs;
3911 #ifdef CONFIG_FEATURE_CLEAN_UP
3916 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3917 int flag_print_load_map = 0;
3920 struct utsname myuname;
3922 /* Parse any options */
3923 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3924 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
3926 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
3930 case 'f': /* force loading */
3931 flag_force_load = 1;
3933 case 'k': /* module loaded by kerneld, auto-cleanable */
3936 case 's': /* log to syslog */
3937 /* log to syslog -- not supported */
3938 /* but kernel needs this for request_module(), */
3939 /* as this calls: modprobe -k -s -- <module> */
3940 /* so silently ignore this flag */
3942 case 'v': /* verbose output */
3945 case 'q': /* silent */
3948 case 'x': /* do not export externs */
3951 case 'o': /* name the output module */
3953 m_name = bb_xstrdup(optarg);
3955 case 'L': /* Stub warning */
3956 /* This is needed for compatibility with modprobe.
3957 * In theory, this does locking, but we don't do
3958 * that. So be careful and plan your life around not
3959 * loading the same module 50 times concurrently. */
3961 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3962 case 'm': /* print module load map */
3963 flag_print_load_map = 1;
3971 if (argv[optind] == NULL) {
3975 /* Grab the module name */
3976 tmp1 = bb_xstrdup(argv[optind]);
3977 tmp = basename(tmp1);
3980 if (uname(&myuname) == 0) {
3981 if (myuname.release[0] == '2') {
3982 k_version = myuname.release[2] - '0';
3986 #if defined(CONFIG_FEATURE_2_6_MODULES)
3987 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
3988 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
3994 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4000 #if defined(CONFIG_FEATURE_2_6_MODULES)
4002 m_fullName = bb_xasprintf("%s.ko", tmp);
4005 m_fullName = bb_xasprintf("%s.o", tmp);
4011 tmp1 = 0; /* flag for free(m_name) before exit() */
4014 /* Get a filedesc for the module. Check we we have a complete path */
4015 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
4016 (fp = fopen(argv[optind], "r")) == NULL) {
4017 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4018 * but do not error out yet if we fail to find it... */
4019 if (k_version) { /* uname succeedd */
4022 char real_module_dir[FILENAME_MAX];
4024 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4025 /* Jump through hoops in case /lib/modules/`uname -r`
4026 * is a symlink. We do not want recursive_action to
4027 * follow symlinks, but we do want to follow the
4028 * /lib/modules/`uname -r` dir, So resolve it ourselves
4029 * if it is a link... */
4030 if (realpath (tmdn, real_module_dir) == NULL)
4033 module_dir = real_module_dir;
4034 recursive_action(module_dir, TRUE, FALSE, FALSE,
4035 check_module_name_match, 0, m_fullName);
4039 /* Check if we have found anything yet */
4040 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4042 char module_dir[FILENAME_MAX];
4046 if (realpath (_PATH_MODULES, module_dir) == NULL)
4047 strcpy(module_dir, _PATH_MODULES);
4048 /* No module found under /lib/modules/`uname -r`, this
4049 * time cast the net a bit wider. Search /lib/modules/ */
4050 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4051 check_module_name_match, 0, m_fullName))
4054 || ((fp = fopen(m_filename, "r")) == NULL))
4056 bb_error_msg("%s: no module by that name found", m_fullName);
4060 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4063 m_filename = bb_xstrdup(argv[optind]);
4066 printf("Using %s\n", m_filename);
4068 #ifdef CONFIG_FEATURE_2_6_MODULES
4072 argv[optind + 1] = m_filename;
4073 return insmod_ng_main(argc - optind, argv + optind);
4077 if ((f = obj_load(fp, LOADBITS)) == NULL)
4078 bb_perror_msg_and_die("Could not load the module");
4080 if (get_modinfo_value(f, "kernel_version") == NULL)
4085 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4086 /* Version correspondence? */
4088 if (uname(&uts_info) < 0)
4089 uts_info.release[0] = '\0';
4090 if (m_has_modinfo) {
4091 m_version = new_get_module_version(f, m_strversion);
4092 if (m_version == -1) {
4093 bb_error_msg("couldn't find the kernel version the module was "
4099 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4100 if (flag_force_load) {
4101 bb_error_msg("Warning: kernel-module version mismatch\n"
4102 "\t%s was compiled for kernel version %s\n"
4103 "\twhile this kernel is version %s",
4104 m_filename, m_strversion, uts_info.release);
4106 bb_error_msg("kernel-module version mismatch\n"
4107 "\t%s was compiled for kernel version %s\n"
4108 "\twhile this kernel is version %s.",
4109 m_filename, m_strversion, uts_info.release);
4115 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4117 if (!query_module(NULL, 0, NULL, 0, NULL)) {
4118 if (!new_get_kernel_symbols())
4120 k_crcs = new_is_kernel_checksummed();
4122 bb_error_msg("Not configured to support old kernels");
4126 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4129 m_crcs = new_is_module_checksummed(f);
4131 if (m_crcs != k_crcs)
4132 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4133 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4135 /* Let the module know about the kernel symbols. */
4136 add_kernel_symbols(f);
4138 /* Allocate common symbols, symbol tables, and string tables. */
4140 if (!new_create_this_module(f, m_name))
4145 if (!obj_check_undefineds(f)) {
4148 obj_allocate_commons(f);
4149 check_tainted_module(f, m_name);
4151 /* done with the module name, on to the optional var=value arguments */
4154 if (optind < argc) {
4155 if (!new_process_module_arguments(f, argc - optind, argv + optind))
4162 hide_special_symbols(f);
4164 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4165 add_ksymoops_symbols(f, m_filename, m_name);
4166 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4168 new_create_module_ksymtab(f);
4170 /* Find current size of the module */
4171 m_size = obj_load_size(f);
4174 m_addr = create_module(m_name, m_size);
4175 if (m_addr == -1) switch (errno) {
4177 bb_error_msg("A module named %s already exists", m_name);
4180 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4184 bb_perror_msg("create_module: %s", m_name);
4190 * the PROGBITS section was not loaded by the obj_load
4191 * now we can load them directly into the kernel memory
4193 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4194 delete_module(m_name);
4199 if (!obj_relocate(f, m_addr)) {
4200 delete_module(m_name);
4204 if (!new_init_module(m_name, f, m_size))
4206 delete_module(m_name);
4210 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4211 if(flag_print_load_map)
4215 exit_status = EXIT_SUCCESS;
4218 #ifdef CONFIG_FEATURE_CLEAN_UP
4227 return(exit_status);
4234 #ifdef CONFIG_FEATURE_2_6_MODULES
4236 #include <sys/mman.h>
4237 #include <asm/unistd.h>
4238 #include <sys/syscall.h>
4240 /* We use error numbers in a loose translation... */
4241 static const char *moderror(int err)
4245 return "Invalid module format";
4247 return "Unknown symbol in module";
4249 return "Module has wrong symbol version";
4251 return "Invalid parameters";
4253 return strerror(err);
4257 int insmod_ng_main( int argc, char **argv)
4265 char *filename, *options = bb_xstrdup("");
4273 /* Rest is options */
4274 for (i = 2; i < argc; i++) {
4275 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4276 /* Spaces handled by "" pairs, but no way of escaping quotes */
4277 if (strchr(argv[i], ' ')) {
4278 strcat(options, "\"");
4279 strcat(options, argv[i]);
4280 strcat(options, "\"");
4282 strcat(options, argv[i]);
4284 strcat(options, " ");
4287 fd = bb_xopen3(filename, O_RDONLY, 0);
4291 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4292 if (map == MAP_FAILED) {
4293 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4296 ret = syscall(__NR_init_module, map, len, options);
4298 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4299 filename, moderror(errno), ret);