1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * This version of insmod supports x86, ARM, SH3/4/5, powerpc, m68k,
6 * MIPS, v850e, and H8/300.
8 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
9 * and Ron Alder <alder@lineo.com>
11 * Miles Bader <miles@gnu.org> added NEC V850E support.
13 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
14 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
16 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
17 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
18 * very minor changes required to also work with StrongArm and presumably
19 * all ARM based systems.
21 * Yoshinori Sato <ysato@users.sourceforge.jp> 19-May-2004.
22 * added Renesas H8/300 support.
24 * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
25 * Integrated support for sh64 (SH-5), from preliminary modutils
26 * patches from Benedict Gaster <benedict.gaster@superh.com>.
27 * Currently limited to support for 32bit ABI.
29 * Magnus Damm <damm@opensource.se> 22-May-2002.
30 * The plt and got code are now using the same structs.
31 * Added generic linked list code to fully support PowerPC.
32 * Replaced the mess in arch_apply_relocation() with architecture blocks.
33 * The arch_create_got() function got cleaned up with architecture blocks.
34 * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
36 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
37 * PowerPC specific code stolen from modutils-2.3.16,
38 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
39 * I've only tested the code on mpc8xx platforms in big-endian mode.
40 * Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
42 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
43 * based on modutils-2.4.2
44 * MIPS specific support for Elf loading and relocation.
45 * Copyright 1996, 1997 Linux International.
46 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
48 * Based almost entirely on the Linux modutils-2.3.11 implementation.
49 * Copyright 1996, 1997 Linux International.
50 * New implementation contributed by Richard Henderson <rth@tamu.edu>
51 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
52 * Restructured (and partly rewritten) by:
53 * Björn Ekwall <bj0rn@blox.se> February 1999
55 * This program is free software; you can redistribute it and/or modify
56 * it under the terms of the GNU General Public License as published by
57 * the Free Software Foundation; either version 2 of the License, or
58 * (at your option) any later version.
60 * This program is distributed in the hope that it will be useful,
61 * but WITHOUT ANY WARRANTY; without even the implied warranty of
62 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
63 * General Public License for more details.
65 * You should have received a copy of the GNU General Public License
66 * along with this program; if not, write to the Free Software
67 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
82 #include <sys/utsname.h>
85 #if !defined(CONFIG_FEATURE_2_4_MODULES) && \
86 !defined(CONFIG_FEATURE_2_6_MODULES)
87 #define CONFIG_FEATURE_2_4_MODULES
90 #if !defined(CONFIG_FEATURE_2_4_MODULES)
91 #define insmod_ng_main insmod_main
94 #if defined(CONFIG_FEATURE_2_6_MODULES)
95 extern int insmod_ng_main( int argc, char **argv);
99 #if defined(CONFIG_FEATURE_2_4_MODULES)
102 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
110 #define CONFIG_USE_PLT_ENTRIES
111 #define CONFIG_PLT_ENTRY_SIZE 8
112 #define CONFIG_USE_GOT_ENTRIES
113 #define CONFIG_GOT_ENTRY_SIZE 8
114 #define CONFIG_USE_SINGLE
116 #define MATCH_MACHINE(x) (x == EM_ARM)
117 #define SHT_RELM SHT_REL
118 #define Elf32_RelM Elf32_Rel
119 #define ELFCLASSM ELFCLASS32
122 #if defined(__s390__)
123 #define CONFIG_USE_PLT_ENTRIES
124 #define CONFIG_PLT_ENTRY_SIZE 8
125 #define CONFIG_USE_GOT_ENTRIES
126 #define CONFIG_GOT_ENTRY_SIZE 8
127 #define CONFIG_USE_SINGLE
129 #define MATCH_MACHINE(x) (x == EM_S390)
130 #define SHT_RELM SHT_RELA
131 #define Elf32_RelM Elf32_Rela
132 #define ELFCLASSM ELFCLASS32
135 #if defined(__i386__)
136 #define CONFIG_USE_GOT_ENTRIES
137 #define CONFIG_GOT_ENTRY_SIZE 4
138 #define CONFIG_USE_SINGLE
141 #define MATCH_MACHINE(x) (x == EM_386)
143 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
146 #define SHT_RELM SHT_REL
147 #define Elf32_RelM Elf32_Rel
148 #define ELFCLASSM ELFCLASS32
151 #if defined(__mc68000__)
152 #define CONFIG_USE_GOT_ENTRIES
153 #define CONFIG_GOT_ENTRY_SIZE 4
154 #define CONFIG_USE_SINGLE
156 #define MATCH_MACHINE(x) (x == EM_68K)
157 #define SHT_RELM SHT_RELA
158 #define Elf32_RelM Elf32_Rela
159 #define ELFCLASSM ELFCLASS32
162 #if defined(__mips__)
163 /* Account for ELF spec changes. */
164 #ifndef EM_MIPS_RS3_LE
165 #ifdef EM_MIPS_RS4_BE
166 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
168 #define EM_MIPS_RS3_LE 10
170 #endif /* !EM_MIPS_RS3_LE */
172 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
173 #define SHT_RELM SHT_REL
174 #define Elf32_RelM Elf32_Rel
175 #define ELFCLASSM ELFCLASS32
176 #define ARCHDATAM "__dbe_table"
179 #if defined(__powerpc__)
180 #define CONFIG_USE_PLT_ENTRIES
181 #define CONFIG_PLT_ENTRY_SIZE 16
182 #define CONFIG_USE_PLT_LIST
183 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
184 #define CONFIG_USE_LIST
186 #define MATCH_MACHINE(x) (x == EM_PPC)
187 #define SHT_RELM SHT_RELA
188 #define Elf32_RelM Elf32_Rela
189 #define ELFCLASSM ELFCLASS32
190 #define ARCHDATAM "__ftr_fixup"
194 #define CONFIG_USE_GOT_ENTRIES
195 #define CONFIG_GOT_ENTRY_SIZE 4
196 #define CONFIG_USE_SINGLE
198 #define MATCH_MACHINE(x) (x == EM_SH)
199 #define SHT_RELM SHT_RELA
200 #define Elf32_RelM Elf32_Rela
201 #define ELFCLASSM ELFCLASS32
203 /* the SH changes have only been tested in =little endian= mode */
204 /* I'm not sure about big endian, so let's warn: */
206 #if defined(__sh__) && defined(__BIG_ENDIAN__)
207 #error insmod.c may require changes for use on big endian SH
210 /* it may or may not work on the SH1/SH2... So let's error on those
212 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && \
214 #error insmod.c may require changes for SH1 or SH2 use
218 #if defined (__v850e__)
219 #define CONFIG_USE_PLT_ENTRIES
220 #define CONFIG_PLT_ENTRY_SIZE 8
221 #define CONFIG_USE_SINGLE
223 #ifndef EM_CYGNUS_V850 /* grumble */
224 #define EM_CYGNUS_V850 0x9080
227 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
228 #define SHT_RELM SHT_RELA
229 #define Elf32_RelM Elf32_Rela
230 #define ELFCLASSM ELFCLASS32
232 #define SYMBOL_PREFIX "_"
235 #if defined(__cris__)
238 #define R_CRIS_NONE 0
242 #define MATCH_MACHINE(x) (x == EM_CRIS)
243 #define SHT_RELM SHT_RELA
244 #define Elf32_RelM Elf32_Rela
245 #define ELFCLASSM ELFCLASS32
248 #if defined(__H8300H__) || defined(__H8300S__)
249 #define CONFIG_USE_SINGLE
251 #define MATCH_MACHINE(x) (x == EM_H8_300)
252 #define SHT_RELM SHT_RELA
253 #define Elf32_RelM Elf32_Rela
255 #define ELFCLASSM ELFCLASS32
256 #define SYMBOL_PREFIX "_"
260 #error Sorry, but insmod.c does not yet support this architecture...
264 //----------------------------------------------------------------------------
265 //--------modutils module.h, lines 45-242
266 //----------------------------------------------------------------------------
268 /* Definitions for the Linux module syscall interface.
269 Copyright 1996, 1997 Linux International.
271 Contributed by Richard Henderson <rth@tamu.edu>
273 This file is part of the Linux modutils.
275 This program is free software; you can redistribute it and/or modify it
276 under the terms of the GNU General Public License as published by the
277 Free Software Foundation; either version 2 of the License, or (at your
278 option) any later version.
280 This program is distributed in the hope that it will be useful, but
281 WITHOUT ANY WARRANTY; without even the implied warranty of
282 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
283 General Public License for more details.
285 You should have received a copy of the GNU General Public License
286 along with this program; if not, write to the Free Software Foundation,
287 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
290 #ifndef MODUTILS_MODULE_H
291 static const int MODUTILS_MODULE_H = 1;
293 #ident "$Id: insmod.c,v 1.121 2004/06/22 20:10:53 andersen Exp $"
295 /*======================================================================*/
296 /* For sizeof() which are related to the module platform and not to the
297 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
299 #define tgt_sizeof_char sizeof(char)
300 #define tgt_sizeof_short sizeof(short)
301 #define tgt_sizeof_int sizeof(int)
302 #define tgt_sizeof_long sizeof(long)
303 #define tgt_sizeof_char_p sizeof(char *)
304 #define tgt_sizeof_void_p sizeof(void *)
305 #define tgt_long long
307 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
308 #undef tgt_sizeof_long
309 #undef tgt_sizeof_char_p
310 #undef tgt_sizeof_void_p
312 static const int tgt_sizeof_long = 8;
313 static const int tgt_sizeof_char_p = 8;
314 static const int tgt_sizeof_void_p = 8;
315 #define tgt_long long long
318 /*======================================================================*/
319 /* The structures used in Linux 2.1. */
321 /* Note: new_module_symbol does not use tgt_long intentionally */
322 struct new_module_symbol
328 struct new_module_persist;
330 struct new_module_ref
332 unsigned tgt_long dep; /* kernel addresses */
333 unsigned tgt_long ref;
334 unsigned tgt_long next_ref;
339 unsigned tgt_long size_of_struct; /* == sizeof(module) */
340 unsigned tgt_long next;
341 unsigned tgt_long name;
342 unsigned tgt_long size;
345 unsigned tgt_long flags; /* AUTOCLEAN et al */
350 unsigned tgt_long syms;
351 unsigned tgt_long deps;
352 unsigned tgt_long refs;
353 unsigned tgt_long init;
354 unsigned tgt_long cleanup;
355 unsigned tgt_long ex_table_start;
356 unsigned tgt_long ex_table_end;
358 unsigned tgt_long gp;
360 /* Everything after here is extension. */
361 unsigned tgt_long persist_start;
362 unsigned tgt_long persist_end;
363 unsigned tgt_long can_unload;
364 unsigned tgt_long runsize;
365 const char *kallsyms_start; /* All symbols for kernel debugging */
366 const char *kallsyms_end;
367 const char *archdata_start; /* arch specific data for module */
368 const char *archdata_end;
369 const char *kernel_data; /* Reserved for kernel internal use */
373 #define ARCHDATA_SEC_NAME ARCHDATAM
375 #define ARCHDATA_SEC_NAME "__archdata"
377 #define KALLSYMS_SEC_NAME "__kallsyms"
380 struct new_module_info
388 /* Bits of module.flags. */
389 static const int NEW_MOD_RUNNING = 1;
390 static const int NEW_MOD_DELETED = 2;
391 static const int NEW_MOD_AUTOCLEAN = 4;
392 static const int NEW_MOD_VISITED = 8;
393 static const int NEW_MOD_USED_ONCE = 16;
395 int init_module(const char *name, const struct new_module *);
396 int query_module(const char *name, int which, void *buf,
397 size_t bufsize, size_t *ret);
399 /* Values for query_module's which. */
401 static const int QM_MODULES = 1;
402 static const int QM_DEPS = 2;
403 static const int QM_REFS = 3;
404 static const int QM_SYMBOLS = 4;
405 static const int QM_INFO = 5;
407 /*======================================================================*/
408 /* The system calls unchanged between 2.0 and 2.1. */
410 unsigned long create_module(const char *, size_t);
411 int delete_module(const char *);
414 #endif /* module.h */
416 //----------------------------------------------------------------------------
417 //--------end of modutils module.h
418 //----------------------------------------------------------------------------
422 //----------------------------------------------------------------------------
423 //--------modutils obj.h, lines 253-462
424 //----------------------------------------------------------------------------
426 /* Elf object file loading and relocation routines.
427 Copyright 1996, 1997 Linux International.
429 Contributed by Richard Henderson <rth@tamu.edu>
431 This file is part of the Linux modutils.
433 This program is free software; you can redistribute it and/or modify it
434 under the terms of the GNU General Public License as published by the
435 Free Software Foundation; either version 2 of the License, or (at your
436 option) any later version.
438 This program is distributed in the hope that it will be useful, but
439 WITHOUT ANY WARRANTY; without even the implied warranty of
440 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
441 General Public License for more details.
443 You should have received a copy of the GNU General Public License
444 along with this program; if not, write to the Free Software Foundation,
445 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
448 #ifndef MODUTILS_OBJ_H
449 static const int MODUTILS_OBJ_H = 1;
451 #ident "$Id: insmod.c,v 1.121 2004/06/22 20:10:53 andersen Exp $"
453 /* The relocatable object is manipulated using elfin types. */
459 #if __BYTE_ORDER == __LITTLE_ENDIAN
460 #define ELFDATAM ELFDATA2LSB
461 #elif __BYTE_ORDER == __BIG_ENDIAN
462 #define ELFDATAM ELFDATA2MSB
466 # if ELFCLASSM == ELFCLASS32
467 # define ElfW(x) Elf32_ ## x
468 # define ELFW(x) ELF32_ ## x
470 # define ElfW(x) Elf64_ ## x
471 # define ELFW(x) ELF64_ ## x
475 /* For some reason this is missing from some ancient C libraries.... */
476 #ifndef ELF32_ST_INFO
477 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
480 #ifndef ELF64_ST_INFO
481 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
484 struct obj_string_patch;
485 struct obj_symbol_patch;
492 struct obj_section *load_next;
498 struct obj_symbol *next; /* hash table link */
502 int secidx; /* the defining section index/module */
504 int ksymidx; /* for export to the kernel symtab */
505 int referenced; /* actually used in the link */
508 /* Hardcode the hash table size. We shouldn't be needing so many
509 symbols that we begin to degrade performance, and we get a big win
510 by giving the compiler a constant divisor. */
512 #define HASH_BUCKETS 521
518 struct obj_section **sections;
519 struct obj_section *load_order;
520 struct obj_section **load_order_search_start;
521 struct obj_string_patch *string_patches;
522 struct obj_symbol_patch *symbol_patches;
523 int (*symbol_cmp)(const char *, const char *);
524 unsigned long (*symbol_hash)(const char *);
525 unsigned long local_symtab_size;
526 struct obj_symbol **local_symtab;
527 struct obj_symbol *symtab[HASH_BUCKETS];
538 struct obj_string_patch
540 struct obj_string_patch *next;
542 ElfW(Addr) reloc_offset;
543 ElfW(Addr) string_offset;
546 struct obj_symbol_patch
548 struct obj_symbol_patch *next;
550 ElfW(Addr) reloc_offset;
551 struct obj_symbol *sym;
555 /* Generic object manipulation routines. */
557 static unsigned long obj_elf_hash(const char *);
559 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
561 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
564 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
565 struct obj_symbol *sym);
567 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
568 static void obj_set_symbol_compare(struct obj_file *f,
569 int (*cmp)(const char *, const char *),
570 unsigned long (*hash)(const char *));
573 static struct obj_section *obj_find_section (struct obj_file *f,
576 static void obj_insert_section_load_order (struct obj_file *f,
577 struct obj_section *sec);
579 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
584 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
589 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
591 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
594 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
595 struct obj_symbol *sym);
597 static int obj_check_undefineds(struct obj_file *f);
599 static void obj_allocate_commons(struct obj_file *f);
601 static unsigned long obj_load_size (struct obj_file *f);
603 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
605 static struct obj_file *obj_load(FILE *f, int loadprogbits);
607 static int obj_create_image (struct obj_file *f, char *image);
609 /* Architecture specific manipulation routines. */
611 static struct obj_file *arch_new_file (void);
613 static struct obj_section *arch_new_section (void);
615 static struct obj_symbol *arch_new_symbol (void);
617 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
618 struct obj_section *targsec,
619 struct obj_section *symsec,
620 struct obj_symbol *sym,
621 ElfW(RelM) *rel, ElfW(Addr) value);
623 static void arch_create_got (struct obj_file *f);
625 static int obj_gpl_license(struct obj_file *f, const char **license);
628 //----------------------------------------------------------------------------
629 //--------end of modutils obj.h
630 //----------------------------------------------------------------------------
633 /* SPFX is always a string, so it can be concatenated to string constants. */
635 #define SPFX SYMBOL_PREFIX
641 #define _PATH_MODULES "/lib/modules"
642 static const int STRVERSIONLEN = 32;
644 /*======================================================================*/
646 static int flag_force_load = 0;
647 static int flag_autoclean = 0;
648 static int flag_verbose = 0;
649 static int flag_quiet = 0;
650 static int flag_export = 1;
653 /*======================================================================*/
655 #if defined(CONFIG_USE_LIST)
657 struct arch_list_entry
659 struct arch_list_entry *next;
660 CONFIG_LIST_ARCHTYPE addend;
667 #if defined(CONFIG_USE_SINGLE)
669 struct arch_single_entry
678 #if defined(__mips__)
681 struct mips_hi16 *next;
688 struct obj_file root;
689 #if defined(CONFIG_USE_PLT_ENTRIES)
690 struct obj_section *plt;
692 #if defined(CONFIG_USE_GOT_ENTRIES)
693 struct obj_section *got;
695 #if defined(__mips__)
696 struct mips_hi16 *mips_hi16_list;
701 struct obj_symbol root;
702 #if defined(CONFIG_USE_PLT_ENTRIES)
703 #if defined(CONFIG_USE_PLT_LIST)
704 struct arch_list_entry *pltent;
706 struct arch_single_entry pltent;
709 #if defined(CONFIG_USE_GOT_ENTRIES)
710 struct arch_single_entry gotent;
715 struct external_module {
720 struct new_module_symbol *syms;
723 static struct new_module_symbol *ksyms;
724 static size_t nksyms;
726 static struct external_module *ext_modules;
727 static int n_ext_modules;
728 static int n_ext_modules_used;
729 extern int delete_module(const char *);
731 static char *m_filename;
732 static char *m_fullName;
736 /*======================================================================*/
739 static int check_module_name_match(const char *filename, struct stat *statbuf,
742 char *fullname = (char *) userdata;
744 if (fullname[0] == '\0')
747 char *tmp, *tmp1 = bb_xstrdup(filename);
748 tmp = bb_get_last_path_component(tmp1);
749 if (strcmp(tmp, fullname) == 0) {
751 /* Stop searching if we find a match */
752 m_filename = bb_xstrdup(filename);
761 /*======================================================================*/
763 static struct obj_file *arch_new_file(void)
766 f = xmalloc(sizeof(*f));
768 memset(f, 0, sizeof(*f));
773 static struct obj_section *arch_new_section(void)
775 return xmalloc(sizeof(struct obj_section));
778 static struct obj_symbol *arch_new_symbol(void)
780 struct arch_symbol *sym;
781 sym = xmalloc(sizeof(*sym));
783 memset(sym, 0, sizeof(*sym));
788 static enum obj_reloc
789 arch_apply_relocation(struct obj_file *f,
790 struct obj_section *targsec,
791 struct obj_section *symsec,
792 struct obj_symbol *sym,
793 ElfW(RelM) *rel, ElfW(Addr) v)
795 struct arch_file *ifile = (struct arch_file *) f;
796 enum obj_reloc ret = obj_reloc_ok;
797 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
798 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
799 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
800 struct arch_symbol *isym = (struct arch_symbol *) sym;
802 #if defined(CONFIG_USE_GOT_ENTRIES)
803 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
805 #if defined(CONFIG_USE_PLT_ENTRIES)
806 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
808 #if defined(CONFIG_USE_PLT_LIST)
809 struct arch_list_entry *pe;
811 struct arch_single_entry *pe;
815 switch (ELF32_R_TYPE(rel->r_info)) {
830 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
831 * (which is .got) similar to branch,
832 * but is full 32 bits relative */
842 case R_ARM_GOTOFF: /* address relative to the got */
847 #elif defined(__s390__)
849 *(unsigned int *) loc += v;
852 *(unsigned short *) loc += v;
855 *(unsigned char *) loc += v;
859 *(unsigned int *) loc += v - dot;
862 *(unsigned short *) loc += (v - dot) >> 1;
865 *(unsigned short *) loc += v - dot;
870 /* find the plt entry and initialize it. */
871 assert(isym != NULL);
872 pe = (struct arch_single_entry *) &isym->pltent;
873 assert(pe->allocated);
874 if (pe->inited == 0) {
875 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
876 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
878 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
885 /* Insert relative distance to target. */
886 v = plt + pe->offset - dot;
887 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
888 *(unsigned int *) loc = (unsigned int) v;
889 else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
890 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
904 *(unsigned long *) loc += got - dot;
910 assert(isym != NULL);
912 if (!isym->gotent.inited)
914 isym->gotent.inited = 1;
915 *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
917 if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
918 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
919 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
920 *(unsigned short *) loc += isym->gotent.offset;
921 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
922 *(unsigned int *) loc += isym->gotent.offset;
925 #ifndef R_390_GOTOFF32
926 #define R_390_GOTOFF32 R_390_GOTOFF
933 #elif defined(__i386__)
969 #elif defined(__mc68000__)
980 ret = obj_reloc_overflow;
987 ret = obj_reloc_overflow;
994 if ((Elf32_Sword)v > 0x7f ||
995 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
996 ret = obj_reloc_overflow;
1003 if ((Elf32_Sword)v > 0x7fff ||
1004 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1005 ret = obj_reloc_overflow;
1011 *(int *)loc = v - dot;
1014 case R_68K_GLOB_DAT:
1015 case R_68K_JMP_SLOT:
1019 case R_68K_RELATIVE:
1020 *(int *)loc += f->baseaddr;
1033 #elif defined(__mips__)
1044 ret = obj_reloc_dangerous;
1045 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1046 ret = obj_reloc_overflow;
1048 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1054 struct mips_hi16 *n;
1056 /* We cannot relocate this one now because we don't know the value
1057 of the carry we need to add. Save the information, and let LO16
1058 do the actual relocation. */
1059 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1062 n->next = ifile->mips_hi16_list;
1063 ifile->mips_hi16_list = n;
1069 unsigned long insnlo = *loc;
1070 Elf32_Addr val, vallo;
1072 /* Sign extend the addend we extract from the lo insn. */
1073 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1075 if (ifile->mips_hi16_list != NULL) {
1076 struct mips_hi16 *l;
1078 l = ifile->mips_hi16_list;
1080 struct mips_hi16 *next;
1083 /* The value for the HI16 had best be the same. */
1084 assert(v == l->value);
1086 /* Do the HI16 relocation. Note that we actually don't
1087 need to know anything about the LO16 itself, except where
1088 to find the low 16 bits of the addend needed by the LO16. */
1091 ((insn & 0xffff) << 16) +
1095 /* Account for the sign extension that will happen in the
1102 insn = (insn & ~0xffff) | val;
1110 ifile->mips_hi16_list = NULL;
1113 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1115 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1120 #elif defined(__powerpc__)
1122 case R_PPC_ADDR16_HA:
1123 *(unsigned short *)loc = (v + 0x8000) >> 16;
1126 case R_PPC_ADDR16_HI:
1127 *(unsigned short *)loc = v >> 16;
1130 case R_PPC_ADDR16_LO:
1131 *(unsigned short *)loc = v;
1145 #elif defined(__sh__)
1168 *loc = f->baseaddr + rel->r_addend;
1173 *loc = got - dot + rel->r_addend;
1184 #if defined(__SH5__)
1185 case R_SH_IMM_MEDLOW16:
1186 case R_SH_IMM_LOW16:
1190 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1194 * movi and shori have the format:
1196 * | op | imm | reg | reserved |
1197 * 31..26 25..10 9.. 4 3 .. 0
1199 * so we simply mask and or in imm.
1201 word = *loc & ~0x3fffc00;
1202 word |= (v & 0xffff) << 10;
1209 case R_SH_IMM_MEDLOW16_PCREL:
1210 case R_SH_IMM_LOW16_PCREL:
1214 word = *loc & ~0x3fffc00;
1218 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1221 word |= (v & 0xffff) << 10;
1227 #endif /* __SH5__ */
1231 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1232 ret = obj_reloc_unhandled;
1235 #if defined (__v850e__)
1240 /* We write two shorts instead of a long because even
1241 32-bit insns only need half-word alignment, but
1242 32-bit data needs to be long-word aligned. */
1243 v += ((unsigned short *)loc)[0];
1244 v += ((unsigned short *)loc)[1] << 16;
1245 ((unsigned short *)loc)[0] = v & 0xffff;
1246 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1249 case R_V850_22_PCREL:
1253 #if defined (__cris__)
1258 /* CRIS keeps the relocation value in the r_addend field and
1259 * should not use whats in *loc at all
1265 #if defined(__H8300H__) || defined(__H8300S__)
1267 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
1268 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
1279 if ((Elf32_Sword)v > 0x7fff ||
1280 (Elf32_Sword)v < -(Elf32_Sword)0x8000)
1281 ret = obj_reloc_overflow;
1283 *(unsigned short *)loc = v;
1287 if ((Elf32_Sword)v > 0x7f ||
1288 (Elf32_Sword)v < -(Elf32_Sword)0x80)
1289 ret = obj_reloc_overflow;
1291 *(unsigned char *)loc = v;
1295 #if defined(CONFIG_USE_PLT_ENTRIES)
1299 /* find the plt entry and initialize it if necessary */
1300 assert(isym != NULL);
1302 #if defined(CONFIG_USE_PLT_LIST)
1303 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1311 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1313 /* generate some machine code */
1315 #if defined(__arm__)
1316 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1317 ip[1] = v; /* sym@ */
1319 #if defined(__powerpc__)
1320 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1321 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1322 ip[2] = 0x7d6903a6; /* mtctr r11 */
1323 ip[3] = 0x4e800420; /* bctr */
1325 #if defined (__v850e__)
1326 /* We have to trash a register, so we assume that any control
1327 transfer more than 21-bits away must be a function call
1328 (so we can use a call-clobbered register). */
1329 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1330 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1335 /* relative distance to target */
1337 /* if the target is too far away.... */
1338 #if defined (__arm__) || defined (__powerpc__)
1339 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1340 #elif defined (__v850e__)
1341 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1343 /* go via the plt */
1344 v = plt + pe->offset - dot;
1346 #if defined (__v850e__)
1351 ret = obj_reloc_dangerous;
1353 /* merge the offset into the instruction. */
1354 #if defined(__arm__)
1355 /* Convert to words. */
1358 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1360 #if defined(__powerpc__)
1361 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1363 #if defined (__v850e__)
1364 /* We write two shorts instead of a long because even 32-bit insns
1365 only need half-word alignment, but the 32-bit data write needs
1366 to be long-word aligned. */
1367 ((unsigned short *)loc)[0] =
1368 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1369 | ((v >> 16) & 0x3f); /* offs high part */
1370 ((unsigned short *)loc)[1] =
1371 (v & 0xffff); /* offs low part */
1374 #endif /* CONFIG_USE_PLT_ENTRIES */
1376 #if defined(CONFIG_USE_GOT_ENTRIES)
1379 assert(isym != NULL);
1380 /* needs an entry in the .got: set it, once */
1381 if (!isym->gotent.inited) {
1382 isym->gotent.inited = 1;
1383 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1385 /* make the reloc with_respect_to_.got */
1387 *loc += isym->gotent.offset + rel->r_addend;
1388 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1389 *loc += isym->gotent.offset;
1393 #endif /* CONFIG_USE_GOT_ENTRIES */
1400 #if defined(CONFIG_USE_LIST)
1402 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1403 int offset, int size)
1405 struct arch_list_entry *pe;
1407 for (pe = *list; pe != NULL; pe = pe->next) {
1408 if (pe->addend == rel->r_addend) {
1414 pe = xmalloc(sizeof(struct arch_list_entry));
1416 pe->addend = rel->r_addend;
1417 pe->offset = offset;
1427 #if defined(CONFIG_USE_SINGLE)
1429 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1430 int offset, int size)
1432 if (single->allocated == 0) {
1433 single->allocated = 1;
1434 single->offset = offset;
1443 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1445 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1446 int offset, int size)
1448 struct obj_section *myrelsec = obj_find_section(f, name);
1455 obj_extend_section(myrelsec, offset);
1457 myrelsec = obj_create_alloced_section(f, name,
1467 static void arch_create_got(struct obj_file *f)
1469 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1470 struct arch_file *ifile = (struct arch_file *) f;
1472 #if defined(CONFIG_USE_GOT_ENTRIES)
1473 int got_offset = 0, got_needed = 0, got_allocate;
1475 #if defined(CONFIG_USE_PLT_ENTRIES)
1476 int plt_offset = 0, plt_needed = 0, plt_allocate;
1478 struct obj_section *relsec, *symsec, *strsec;
1479 ElfW(RelM) *rel, *relend;
1480 ElfW(Sym) *symtab, *extsym;
1481 const char *strtab, *name;
1482 struct arch_symbol *intsym;
1484 for (i = 0; i < f->header.e_shnum; ++i) {
1485 relsec = f->sections[i];
1486 if (relsec->header.sh_type != SHT_RELM)
1489 symsec = f->sections[relsec->header.sh_link];
1490 strsec = f->sections[symsec->header.sh_link];
1492 rel = (ElfW(RelM) *) relsec->contents;
1493 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1494 symtab = (ElfW(Sym) *) symsec->contents;
1495 strtab = (const char *) strsec->contents;
1497 for (; rel < relend; ++rel) {
1498 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1500 #if defined(CONFIG_USE_GOT_ENTRIES)
1503 #if defined(CONFIG_USE_PLT_ENTRIES)
1507 switch (ELF32_R_TYPE(rel->r_info)) {
1508 #if defined(__arm__)
1523 #elif defined(__i386__)
1533 #elif defined(__powerpc__)
1538 #elif defined(__mc68000__)
1549 #elif defined(__sh__)
1559 #elif defined (__v850e__)
1560 case R_V850_22_PCREL:
1569 if (extsym->st_name != 0) {
1570 name = strtab + extsym->st_name;
1572 name = f->sections[extsym->st_shndx]->name;
1574 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1575 #if defined(CONFIG_USE_GOT_ENTRIES)
1577 got_offset += arch_single_init(
1578 rel, &intsym->gotent,
1579 got_offset, CONFIG_GOT_ENTRY_SIZE);
1584 #if defined(CONFIG_USE_PLT_ENTRIES)
1586 #if defined(CONFIG_USE_PLT_LIST)
1587 plt_offset += arch_list_add(
1588 rel, &intsym->pltent,
1589 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1591 plt_offset += arch_single_init(
1592 rel, &intsym->pltent,
1593 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1601 #if defined(CONFIG_USE_GOT_ENTRIES)
1603 ifile->got = arch_xsect_init(f, ".got", got_offset,
1604 CONFIG_GOT_ENTRY_SIZE);
1608 #if defined(CONFIG_USE_PLT_ENTRIES)
1610 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1611 CONFIG_PLT_ENTRY_SIZE);
1615 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1618 /*======================================================================*/
1620 /* Standard ELF hash function. */
1621 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1623 unsigned long h = 0;
1630 if ((g = (h & 0xf0000000)) != 0) {
1639 static unsigned long obj_elf_hash(const char *name)
1641 return obj_elf_hash_n(name, strlen(name));
1644 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1645 /* String comparison for non-co-versioned kernel and module. */
1647 static int ncv_strcmp(const char *a, const char *b)
1649 size_t alen = strlen(a), blen = strlen(b);
1651 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1652 return strncmp(a, b, alen);
1653 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1654 return strncmp(a, b, blen);
1656 return strcmp(a, b);
1659 /* String hashing for non-co-versioned kernel and module. Here
1660 we are simply forced to drop the crc from the hash. */
1662 static unsigned long ncv_symbol_hash(const char *str)
1664 size_t len = strlen(str);
1665 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1667 return obj_elf_hash_n(str, len);
1671 obj_set_symbol_compare(struct obj_file *f,
1672 int (*cmp) (const char *, const char *),
1673 unsigned long (*hash) (const char *))
1676 f->symbol_cmp = cmp;
1678 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1681 f->symbol_hash = hash;
1683 memcpy(tmptab, f->symtab, sizeof(tmptab));
1684 memset(f->symtab, 0, sizeof(f->symtab));
1686 for (i = 0; i < HASH_BUCKETS; ++i)
1687 for (sym = tmptab[i]; sym; sym = next) {
1688 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1690 sym->next = f->symtab[h];
1696 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1698 static struct obj_symbol *
1699 obj_add_symbol(struct obj_file *f, const char *name,
1700 unsigned long symidx, int info,
1701 int secidx, ElfW(Addr) value,
1704 struct obj_symbol *sym;
1705 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1706 int n_type = ELFW(ST_TYPE) (info);
1707 int n_binding = ELFW(ST_BIND) (info);
1709 for (sym = f->symtab[hash]; sym; sym = sym->next)
1710 if (f->symbol_cmp(sym->name, name) == 0) {
1711 int o_secidx = sym->secidx;
1712 int o_info = sym->info;
1713 int o_type = ELFW(ST_TYPE) (o_info);
1714 int o_binding = ELFW(ST_BIND) (o_info);
1716 /* A redefinition! Is it legal? */
1718 if (secidx == SHN_UNDEF)
1720 else if (o_secidx == SHN_UNDEF)
1722 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1723 /* Cope with local and global symbols of the same name
1724 in the same object file, as might have been created
1725 by ld -r. The only reason locals are now seen at this
1726 level at all is so that we can do semi-sensible things
1729 struct obj_symbol *nsym, **p;
1731 nsym = arch_new_symbol();
1732 nsym->next = sym->next;
1735 /* Excise the old (local) symbol from the hash chain. */
1736 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1740 } else if (n_binding == STB_LOCAL) {
1741 /* Another symbol of the same name has already been defined.
1742 Just add this to the local table. */
1743 sym = arch_new_symbol();
1746 f->local_symtab[symidx] = sym;
1748 } else if (n_binding == STB_WEAK)
1750 else if (o_binding == STB_WEAK)
1752 /* Don't unify COMMON symbols with object types the programmer
1754 else if (secidx == SHN_COMMON
1755 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1757 else if (o_secidx == SHN_COMMON
1758 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1761 /* Don't report an error if the symbol is coming from
1762 the kernel or some external module. */
1763 if (secidx <= SHN_HIRESERVE)
1764 bb_error_msg("%s multiply defined", name);
1769 /* Completely new symbol. */
1770 sym = arch_new_symbol();
1771 sym->next = f->symtab[hash];
1772 f->symtab[hash] = sym;
1775 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1776 if (symidx >= f->local_symtab_size)
1777 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1778 name, (long) symidx, (long) f->local_symtab_size);
1780 f->local_symtab[symidx] = sym;
1787 sym->secidx = secidx;
1793 static struct obj_symbol *
1794 obj_find_symbol(struct obj_file *f, const char *name)
1796 struct obj_symbol *sym;
1797 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1799 for (sym = f->symtab[hash]; sym; sym = sym->next)
1800 if (f->symbol_cmp(sym->name, name) == 0)
1807 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1810 if (sym->secidx >= SHN_LORESERVE)
1813 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1815 /* As a special case, a NULL sym has value zero. */
1820 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1822 int i, n = f->header.e_shnum;
1824 for (i = 0; i < n; ++i)
1825 if (strcmp(f->sections[i]->name, name) == 0)
1826 return f->sections[i];
1831 static int obj_load_order_prio(struct obj_section *a)
1833 unsigned long af, ac;
1835 af = a->header.sh_flags;
1838 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1839 strcmp(a->name + 5, ".init"))
1843 if (!(af & SHF_WRITE))
1845 if (af & SHF_EXECINSTR)
1847 if (a->header.sh_type != SHT_NOBITS)
1854 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1856 struct obj_section **p;
1857 int prio = obj_load_order_prio(sec);
1858 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1859 if (obj_load_order_prio(*p) < prio)
1861 sec->load_next = *p;
1865 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1867 unsigned long align,
1870 int newidx = f->header.e_shnum++;
1871 struct obj_section *sec;
1873 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1874 f->sections[newidx] = sec = arch_new_section();
1876 memset(sec, 0, sizeof(*sec));
1877 sec->header.sh_type = SHT_PROGBITS;
1878 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1879 sec->header.sh_size = size;
1880 sec->header.sh_addralign = align;
1884 sec->contents = xmalloc(size);
1886 obj_insert_section_load_order(f, sec);
1891 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1893 unsigned long align,
1896 int newidx = f->header.e_shnum++;
1897 struct obj_section *sec;
1899 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1900 f->sections[newidx] = sec = arch_new_section();
1902 memset(sec, 0, sizeof(*sec));
1903 sec->header.sh_type = SHT_PROGBITS;
1904 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1905 sec->header.sh_size = size;
1906 sec->header.sh_addralign = align;
1910 sec->contents = xmalloc(size);
1912 sec->load_next = f->load_order;
1913 f->load_order = sec;
1914 if (f->load_order_search_start == &f->load_order)
1915 f->load_order_search_start = &sec->load_next;
1920 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1922 unsigned long oldsize = sec->header.sh_size;
1924 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1926 return sec->contents + oldsize;
1930 /* Conditionally add the symbols from the given symbol set to the
1934 add_symbols_from( struct obj_file *f,
1935 int idx, struct new_module_symbol *syms, size_t nsyms)
1937 struct new_module_symbol *s;
1940 #ifdef SYMBOL_PREFIX
1942 size_t name_alloced_size = 0;
1944 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1947 gpl = obj_gpl_license(f, NULL) == 0;
1949 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1950 /* Only add symbols that are already marked external.
1951 If we override locals we may cause problems for
1952 argument initialization. We will also create a false
1953 dependency on the module. */
1954 struct obj_symbol *sym;
1957 /* GPL licensed modules can use symbols exported with
1958 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
1959 * exported names. Non-GPL modules never see any GPLONLY_
1960 * symbols so they cannot fudge it by adding the prefix on
1963 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
1964 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1966 ((char *)s->name) += 8;
1971 name = (char *)s->name;
1973 #ifdef SYMBOL_PREFIX
1974 /* Prepend SYMBOL_PREFIX to the symbol's name (the
1975 kernel exports `C names', but module object files
1976 reference `linker names'). */
1977 size_t extra = sizeof SYMBOL_PREFIX;
1978 size_t name_size = strlen (name) + extra;
1979 if (name_size > name_alloced_size) {
1980 name_alloced_size = name_size * 2;
1981 name_buf = alloca (name_alloced_size);
1983 strcpy (name_buf, SYMBOL_PREFIX);
1984 strcpy (name_buf + extra - 1, name);
1986 #endif /* SYMBOL_PREFIX */
1988 sym = obj_find_symbol(f, name);
1989 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
1990 #ifdef SYMBOL_PREFIX
1991 /* Put NAME_BUF into more permanent storage. */
1992 name = xmalloc (name_size);
1993 strcpy (name, name_buf);
1995 sym = obj_add_symbol(f, name, -1,
1996 ELFW(ST_INFO) (STB_GLOBAL,
1999 /* Did our symbol just get installed? If so, mark the
2000 module as "used". */
2001 if (sym->secidx == idx)
2009 static void add_kernel_symbols(struct obj_file *f)
2011 struct external_module *m;
2014 /* Add module symbols first. */
2016 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2018 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2019 m->nsyms)) m->used = 1, ++nused;
2021 n_ext_modules_used = nused;
2023 /* And finally the symbols from the kernel proper. */
2026 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2029 static char *get_modinfo_value(struct obj_file *f, const char *key)
2031 struct obj_section *sec;
2032 char *p, *v, *n, *ep;
2033 size_t klen = strlen(key);
2035 sec = obj_find_section(f, ".modinfo");
2039 ep = p + sec->header.sh_size;
2042 n = strchr(p, '\0');
2044 if (p + klen == v && strncmp(p, key, klen) == 0)
2047 if (p + klen == n && strcmp(p, key) == 0)
2057 /*======================================================================*/
2058 /* Functions relating to module loading after 2.1.18. */
2061 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2064 char *p, *q, *key, *sym_name;
2065 struct obj_symbol *sym;
2066 char *contents, *loc;
2070 if ((q = strchr(p, '=')) == NULL) {
2075 key = alloca(q - p + 6);
2076 memcpy(key, "parm_", 5);
2077 memcpy(key + 5, p, q - p);
2080 p = get_modinfo_value(f, key);
2083 bb_error_msg("invalid parameter %s", key);
2087 #ifdef SYMBOL_PREFIX
2088 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2089 strcpy (sym_name, SYMBOL_PREFIX);
2090 strcat (sym_name, key);
2094 sym = obj_find_symbol(f, sym_name);
2096 /* Also check that the parameter was not resolved from the kernel. */
2097 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2098 bb_error_msg("symbol for parameter %s not found", key);
2103 min = strtoul(p, &p, 10);
2105 max = strtoul(p + 1, &p, 10);
2111 contents = f->sections[sym->secidx]->contents;
2112 loc = contents + sym->value;
2116 if ((*p == 's') || (*p == 'c')) {
2119 /* Do C quoting if we begin with a ", else slurp the lot. */
2123 str = alloca(strlen(q));
2124 for (r = str, q++; *q != '"'; ++q, ++r) {
2126 bb_error_msg("improperly terminated string argument for %s",
2129 } else if (*q == '\\')
2163 if (q[1] >= '0' && q[1] <= '7') {
2164 c = (c * 8) + *++q - '0';
2165 if (q[1] >= '0' && q[1] <= '7')
2166 c = (c * 8) + *++q - '0';
2183 /* In this case, the string is not quoted. We will break
2184 it using the coma (like for ints). If the user wants to
2185 include comas in a string, he just has to quote it */
2187 /* Search the next coma */
2191 if (r != (char *) NULL) {
2192 /* Recopy the current field */
2193 str = alloca(r - q + 1);
2194 memcpy(str, q, r - q);
2196 /* I don't know if it is useful, as the previous case
2197 doesn't nul terminate the string ??? */
2200 /* Keep next fields */
2211 obj_string_patch(f, sym->secidx, loc - contents, str);
2212 loc += tgt_sizeof_char_p;
2214 /* Array of chars (in fact, matrix !) */
2215 unsigned long charssize; /* size of each member */
2217 /* Get the size of each member */
2218 /* Probably we should do that outside the loop ? */
2219 if (!isdigit(*(p + 1))) {
2220 bb_error_msg("parameter type 'c' for %s must be followed by"
2221 " the maximum size", key);
2224 charssize = strtoul(p + 1, (char **) NULL, 10);
2227 if (strlen(str) >= charssize) {
2228 bb_error_msg("string too long for %s (max %ld)", key,
2233 /* Copy to location */
2234 strcpy((char *) loc, str);
2238 long v = strtoul(q, &q, 0);
2245 loc += tgt_sizeof_short;
2249 loc += tgt_sizeof_int;
2253 loc += tgt_sizeof_long;
2257 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2272 goto retry_end_of_value;
2276 bb_error_msg("too many values for %s (max %d)", key, max);
2283 bb_error_msg("invalid argument syntax for %s", key);
2290 bb_error_msg("too few values for %s (min %d)", key, min);
2300 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2301 static int new_is_module_checksummed(struct obj_file *f)
2303 const char *p = get_modinfo_value(f, "using_checksums");
2310 /* Get the module's kernel version in the canonical integer form. */
2313 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2318 p = get_modinfo_value(f, "kernel_version");
2321 safe_strncpy(str, p, STRVERSIONLEN);
2323 a = strtoul(p, &p, 10);
2326 b = strtoul(p + 1, &p, 10);
2329 c = strtoul(p + 1, &q, 10);
2333 return a << 16 | b << 8 | c;
2336 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2339 /* Fetch the loaded modules, and all currently exported symbols. */
2341 static int new_get_kernel_symbols(void)
2343 char *module_names, *mn;
2344 struct external_module *modules, *m;
2345 struct new_module_symbol *syms, *s;
2346 size_t ret, bufsize, nmod, nsyms, i, j;
2348 /* Collect the loaded modules. */
2350 module_names = xmalloc(bufsize = 256);
2352 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2353 if (errno == ENOSPC && bufsize < ret) {
2354 module_names = xrealloc(module_names, bufsize = ret);
2355 goto retry_modules_load;
2357 bb_perror_msg("QM_MODULES");
2361 n_ext_modules = nmod = ret;
2363 /* Collect the modules' symbols. */
2366 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2367 memset(modules, 0, nmod * sizeof(*modules));
2368 for (i = 0, mn = module_names, m = modules;
2369 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2370 struct new_module_info info;
2372 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2373 if (errno == ENOENT) {
2374 /* The module was removed out from underneath us. */
2377 bb_perror_msg("query_module: QM_INFO: %s", mn);
2381 syms = xmalloc(bufsize = 1024);
2383 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2386 syms = xrealloc(syms, bufsize = ret);
2387 goto retry_mod_sym_load;
2389 /* The module was removed out from underneath us. */
2392 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2399 m->addr = info.addr;
2403 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2404 s->name += (unsigned long) syms;
2409 /* Collect the kernel's symbols. */
2411 syms = xmalloc(bufsize = 16 * 1024);
2412 retry_kern_sym_load:
2413 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2414 if (errno == ENOSPC && bufsize < ret) {
2415 syms = xrealloc(syms, bufsize = ret);
2416 goto retry_kern_sym_load;
2418 bb_perror_msg("kernel: QM_SYMBOLS");
2421 nksyms = nsyms = ret;
2424 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2425 s->name += (unsigned long) syms;
2431 /* Return the kernel symbol checksum version, or zero if not used. */
2433 static int new_is_kernel_checksummed(void)
2435 struct new_module_symbol *s;
2438 /* Using_Versions is not the first symbol, but it should be in there. */
2440 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2441 if (strcmp((char *) s->name, "Using_Versions") == 0)
2448 static int new_create_this_module(struct obj_file *f, const char *m_name)
2450 struct obj_section *sec;
2452 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2453 sizeof(struct new_module));
2454 memset(sec->contents, 0, sizeof(struct new_module));
2456 obj_add_symbol(f, SPFX "__this_module", -1,
2457 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2458 sizeof(struct new_module));
2460 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2466 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2467 /* add an entry to the __ksymtab section, creating it if necessary */
2468 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2470 struct obj_section *sec;
2473 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2474 * If __ksymtab is defined but not marked alloc, x out the first character
2475 * (no obj_delete routine) and create a new __ksymtab with the correct
2478 sec = obj_find_section(f, "__ksymtab");
2479 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2480 *((char *)(sec->name)) = 'x'; /* override const */
2484 sec = obj_create_alloced_section(f, "__ksymtab",
2485 tgt_sizeof_void_p, 0);
2488 sec->header.sh_flags |= SHF_ALLOC;
2489 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2491 ofs = sec->header.sh_size;
2492 obj_symbol_patch(f, sec->idx, ofs, sym);
2493 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2494 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2496 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2498 static int new_create_module_ksymtab(struct obj_file *f)
2500 struct obj_section *sec;
2503 /* We must always add the module references. */
2505 if (n_ext_modules_used) {
2506 struct new_module_ref *dep;
2507 struct obj_symbol *tm;
2509 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2510 (sizeof(struct new_module_ref)
2511 * n_ext_modules_used));
2515 tm = obj_find_symbol(f, SPFX "__this_module");
2516 dep = (struct new_module_ref *) sec->contents;
2517 for (i = 0; i < n_ext_modules; ++i)
2518 if (ext_modules[i].used) {
2519 dep->dep = ext_modules[i].addr;
2520 obj_symbol_patch(f, sec->idx,
2521 (char *) &dep->ref - sec->contents, tm);
2527 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2532 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2535 /* We don't want to export symbols residing in sections that
2536 aren't loaded. There are a number of these created so that
2537 we make sure certain module options don't appear twice. */
2539 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2541 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2543 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2544 struct obj_symbol *sym;
2545 for (sym = f->symtab[i]; sym; sym = sym->next)
2546 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2547 && sym->secidx <= SHN_HIRESERVE
2548 && (sym->secidx >= SHN_LORESERVE
2549 || loaded[sym->secidx])) {
2550 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2552 obj_symbol_patch(f, sec->idx, ofs, sym);
2553 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2560 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2568 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2570 struct new_module *module;
2571 struct obj_section *sec;
2576 sec = obj_find_section(f, ".this");
2577 if (!sec || !sec->contents) {
2578 bb_perror_msg_and_die("corrupt module %s?",m_name);
2580 module = (struct new_module *) sec->contents;
2581 m_addr = sec->header.sh_addr;
2583 module->size_of_struct = sizeof(*module);
2584 module->size = m_size;
2585 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2587 sec = obj_find_section(f, "__ksymtab");
2588 if (sec && sec->header.sh_size) {
2589 module->syms = sec->header.sh_addr;
2590 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2593 if (n_ext_modules_used) {
2594 sec = obj_find_section(f, ".kmodtab");
2595 module->deps = sec->header.sh_addr;
2596 module->ndeps = n_ext_modules_used;
2600 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2602 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2604 sec = obj_find_section(f, "__ex_table");
2606 module->ex_table_start = sec->header.sh_addr;
2607 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2610 sec = obj_find_section(f, ".text.init");
2612 module->runsize = sec->header.sh_addr - m_addr;
2614 sec = obj_find_section(f, ".data.init");
2616 if (!module->runsize ||
2617 module->runsize > sec->header.sh_addr - m_addr)
2618 module->runsize = sec->header.sh_addr - m_addr;
2620 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2621 if (sec && sec->header.sh_size) {
2622 module->archdata_start = (void*)sec->header.sh_addr;
2623 module->archdata_end = module->archdata_start + sec->header.sh_size;
2625 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2626 if (sec && sec->header.sh_size) {
2627 module->kallsyms_start = (void*)sec->header.sh_addr;
2628 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2631 /* Whew! All of the initialization is complete. Collect the final
2632 module image and give it to the kernel. */
2634 image = xmalloc(m_size);
2635 obj_create_image(f, image);
2637 ret = init_module(m_name, (struct new_module *) image);
2639 bb_perror_msg("init_module: %s", m_name);
2647 /*======================================================================*/
2650 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2653 struct obj_string_patch *p;
2654 struct obj_section *strsec;
2655 size_t len = strlen(string) + 1;
2658 p = xmalloc(sizeof(*p));
2659 p->next = f->string_patches;
2660 p->reloc_secidx = secidx;
2661 p->reloc_offset = offset;
2662 f->string_patches = p;
2664 strsec = obj_find_section(f, ".kstrtab");
2665 if (strsec == NULL) {
2666 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2667 p->string_offset = 0;
2668 loc = strsec->contents;
2670 p->string_offset = strsec->header.sh_size;
2671 loc = obj_extend_section(strsec, len);
2673 memcpy(loc, string, len);
2679 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2680 struct obj_symbol *sym)
2682 struct obj_symbol_patch *p;
2684 p = xmalloc(sizeof(*p));
2685 p->next = f->symbol_patches;
2686 p->reloc_secidx = secidx;
2687 p->reloc_offset = offset;
2689 f->symbol_patches = p;
2694 static int obj_check_undefineds(struct obj_file *f)
2699 for (i = 0; i < HASH_BUCKETS; ++i) {
2700 struct obj_symbol *sym;
2701 for (sym = f->symtab[i]; sym; sym = sym->next)
2702 if (sym->secidx == SHN_UNDEF) {
2703 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2704 sym->secidx = SHN_ABS;
2708 bb_error_msg("unresolved symbol %s", sym->name);
2718 static void obj_allocate_commons(struct obj_file *f)
2720 struct common_entry {
2721 struct common_entry *next;
2722 struct obj_symbol *sym;
2723 } *common_head = NULL;
2727 for (i = 0; i < HASH_BUCKETS; ++i) {
2728 struct obj_symbol *sym;
2729 for (sym = f->symtab[i]; sym; sym = sym->next)
2730 if (sym->secidx == SHN_COMMON) {
2731 /* Collect all COMMON symbols and sort them by size so as to
2732 minimize space wasted by alignment requirements. */
2734 struct common_entry **p, *n;
2735 for (p = &common_head; *p; p = &(*p)->next)
2736 if (sym->size <= (*p)->sym->size)
2739 n = alloca(sizeof(*n));
2747 for (i = 1; i < f->local_symtab_size; ++i) {
2748 struct obj_symbol *sym = f->local_symtab[i];
2749 if (sym && sym->secidx == SHN_COMMON) {
2750 struct common_entry **p, *n;
2751 for (p = &common_head; *p; p = &(*p)->next)
2752 if (sym == (*p)->sym)
2754 else if (sym->size < (*p)->sym->size) {
2755 n = alloca(sizeof(*n));
2765 /* Find the bss section. */
2766 for (i = 0; i < f->header.e_shnum; ++i)
2767 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2770 /* If for some reason there hadn't been one, create one. */
2771 if (i == f->header.e_shnum) {
2772 struct obj_section *sec;
2774 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2775 f->sections[i] = sec = arch_new_section();
2776 f->header.e_shnum = i + 1;
2778 memset(sec, 0, sizeof(*sec));
2779 sec->header.sh_type = SHT_PROGBITS;
2780 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2785 /* Allocate the COMMONS. */
2787 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2788 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2789 struct common_entry *c;
2791 for (c = common_head; c; c = c->next) {
2792 ElfW(Addr) align = c->sym->value;
2794 if (align > max_align)
2796 if (bss_size & (align - 1))
2797 bss_size = (bss_size | (align - 1)) + 1;
2800 c->sym->value = bss_size;
2802 bss_size += c->sym->size;
2805 f->sections[i]->header.sh_size = bss_size;
2806 f->sections[i]->header.sh_addralign = max_align;
2810 /* For the sake of patch relocation and parameter initialization,
2811 allocate zeroed data for NOBITS sections now. Note that after
2812 this we cannot assume NOBITS are really empty. */
2813 for (i = 0; i < f->header.e_shnum; ++i) {
2814 struct obj_section *s = f->sections[i];
2815 if (s->header.sh_type == SHT_NOBITS) {
2816 if (s->header.sh_size != 0)
2817 s->contents = memset(xmalloc(s->header.sh_size),
2818 0, s->header.sh_size);
2822 s->header.sh_type = SHT_PROGBITS;
2827 static unsigned long obj_load_size(struct obj_file *f)
2829 unsigned long dot = 0;
2830 struct obj_section *sec;
2832 /* Finalize the positions of the sections relative to one another. */
2834 for (sec = f->load_order; sec; sec = sec->load_next) {
2837 align = sec->header.sh_addralign;
2838 if (align && (dot & (align - 1)))
2839 dot = (dot | (align - 1)) + 1;
2841 sec->header.sh_addr = dot;
2842 dot += sec->header.sh_size;
2848 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2850 int i, n = f->header.e_shnum;
2853 /* Finalize the addresses of the sections. */
2856 for (i = 0; i < n; ++i)
2857 f->sections[i]->header.sh_addr += base;
2859 /* And iterate over all of the relocations. */
2861 for (i = 0; i < n; ++i) {
2862 struct obj_section *relsec, *symsec, *targsec, *strsec;
2863 ElfW(RelM) * rel, *relend;
2867 relsec = f->sections[i];
2868 if (relsec->header.sh_type != SHT_RELM)
2871 symsec = f->sections[relsec->header.sh_link];
2872 targsec = f->sections[relsec->header.sh_info];
2873 strsec = f->sections[symsec->header.sh_link];
2875 rel = (ElfW(RelM) *) relsec->contents;
2876 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2877 symtab = (ElfW(Sym) *) symsec->contents;
2878 strtab = (const char *) strsec->contents;
2880 for (; rel < relend; ++rel) {
2881 ElfW(Addr) value = 0;
2882 struct obj_symbol *intsym = NULL;
2883 unsigned long symndx;
2884 ElfW(Sym) * extsym = 0;
2887 /* Attempt to find a value to use for this relocation. */
2889 symndx = ELFW(R_SYM) (rel->r_info);
2891 /* Note we've already checked for undefined symbols. */
2893 extsym = &symtab[symndx];
2894 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2895 /* Local symbols we look up in the local table to be sure
2896 we get the one that is really intended. */
2897 intsym = f->local_symtab[symndx];
2899 /* Others we look up in the hash table. */
2901 if (extsym->st_name)
2902 name = strtab + extsym->st_name;
2904 name = f->sections[extsym->st_shndx]->name;
2905 intsym = obj_find_symbol(f, name);
2908 value = obj_symbol_final_value(f, intsym);
2909 intsym->referenced = 1;
2911 #if SHT_RELM == SHT_RELA
2912 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2913 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2914 if (!extsym || !extsym->st_name ||
2915 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2917 value += rel->r_addend;
2921 switch (arch_apply_relocation
2922 (f, targsec, symsec, intsym, rel, value)) {
2926 case obj_reloc_overflow:
2927 errmsg = "Relocation overflow";
2929 case obj_reloc_dangerous:
2930 errmsg = "Dangerous relocation";
2932 case obj_reloc_unhandled:
2933 errmsg = "Unhandled relocation";
2936 bb_error_msg("%s of type %ld for %s", errmsg,
2937 (long) ELFW(R_TYPE) (rel->r_info),
2938 strtab + extsym->st_name);
2940 bb_error_msg("%s of type %ld", errmsg,
2941 (long) ELFW(R_TYPE) (rel->r_info));
2949 /* Finally, take care of the patches. */
2951 if (f->string_patches) {
2952 struct obj_string_patch *p;
2953 struct obj_section *strsec;
2954 ElfW(Addr) strsec_base;
2955 strsec = obj_find_section(f, ".kstrtab");
2956 strsec_base = strsec->header.sh_addr;
2958 for (p = f->string_patches; p; p = p->next) {
2959 struct obj_section *targsec = f->sections[p->reloc_secidx];
2960 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2961 = strsec_base + p->string_offset;
2965 if (f->symbol_patches) {
2966 struct obj_symbol_patch *p;
2968 for (p = f->symbol_patches; p; p = p->next) {
2969 struct obj_section *targsec = f->sections[p->reloc_secidx];
2970 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2971 = obj_symbol_final_value(f, p->sym);
2978 static int obj_create_image(struct obj_file *f, char *image)
2980 struct obj_section *sec;
2981 ElfW(Addr) base = f->baseaddr;
2983 for (sec = f->load_order; sec; sec = sec->load_next) {
2986 if (sec->contents == 0 || sec->header.sh_size == 0)
2989 secimg = image + (sec->header.sh_addr - base);
2991 /* Note that we allocated data for NOBITS sections earlier. */
2992 memcpy(secimg, sec->contents, sec->header.sh_size);
2998 /*======================================================================*/
3000 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3003 ElfW(Shdr) * section_headers;
3007 /* Read the file header. */
3009 f = arch_new_file();
3010 memset(f, 0, sizeof(*f));
3011 f->symbol_cmp = strcmp;
3012 f->symbol_hash = obj_elf_hash;
3013 f->load_order_search_start = &f->load_order;
3015 fseek(fp, 0, SEEK_SET);
3016 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3017 bb_perror_msg("error reading ELF header");
3021 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3022 || f->header.e_ident[EI_MAG1] != ELFMAG1
3023 || f->header.e_ident[EI_MAG2] != ELFMAG2
3024 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3025 bb_error_msg("not an ELF file");
3028 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3029 || f->header.e_ident[EI_DATA] != ELFDATAM
3030 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3031 || !MATCH_MACHINE(f->header.e_machine)) {
3032 bb_error_msg("ELF file not for this architecture");
3035 if (f->header.e_type != ET_REL) {
3036 bb_error_msg("ELF file not a relocatable object");
3040 /* Read the section headers. */
3042 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3043 bb_error_msg("section header size mismatch: %lu != %lu",
3044 (unsigned long) f->header.e_shentsize,
3045 (unsigned long) sizeof(ElfW(Shdr)));
3049 shnum = f->header.e_shnum;
3050 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3051 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3053 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3054 fseek(fp, f->header.e_shoff, SEEK_SET);
3055 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3056 bb_perror_msg("error reading ELF section headers");
3060 /* Read the section data. */
3062 for (i = 0; i < shnum; ++i) {
3063 struct obj_section *sec;
3065 f->sections[i] = sec = arch_new_section();
3066 memset(sec, 0, sizeof(*sec));
3068 sec->header = section_headers[i];
3071 if(sec->header.sh_size) switch (sec->header.sh_type) {
3080 if (!loadprogbits) {
3081 sec->contents = NULL;
3088 if (sec->header.sh_size > 0) {
3089 sec->contents = xmalloc(sec->header.sh_size);
3090 fseek(fp, sec->header.sh_offset, SEEK_SET);
3091 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3092 bb_perror_msg("error reading ELF section data");
3096 sec->contents = NULL;
3100 #if SHT_RELM == SHT_REL
3102 bb_error_msg("RELA relocations not supported on this architecture");
3106 bb_error_msg("REL relocations not supported on this architecture");
3111 if (sec->header.sh_type >= SHT_LOPROC) {
3112 /* Assume processor specific section types are debug
3113 info and can safely be ignored. If this is ever not
3114 the case (Hello MIPS?), don't put ifdefs here but
3115 create an arch_load_proc_section(). */
3119 bb_error_msg("can't handle sections of type %ld",
3120 (long) sec->header.sh_type);
3125 /* Do what sort of interpretation as needed by each section. */
3127 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3129 for (i = 0; i < shnum; ++i) {
3130 struct obj_section *sec = f->sections[i];
3131 sec->name = shstrtab + sec->header.sh_name;
3134 for (i = 0; i < shnum; ++i) {
3135 struct obj_section *sec = f->sections[i];
3137 /* .modinfo should be contents only but gcc has no attribute for that.
3138 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3140 if (strcmp(sec->name, ".modinfo") == 0)
3141 sec->header.sh_flags &= ~SHF_ALLOC;
3143 if (sec->header.sh_flags & SHF_ALLOC)
3144 obj_insert_section_load_order(f, sec);
3146 switch (sec->header.sh_type) {
3149 unsigned long nsym, j;
3153 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3154 bb_error_msg("symbol size mismatch: %lu != %lu",
3155 (unsigned long) sec->header.sh_entsize,
3156 (unsigned long) sizeof(ElfW(Sym)));
3160 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3161 strtab = f->sections[sec->header.sh_link]->contents;
3162 sym = (ElfW(Sym) *) sec->contents;
3164 /* Allocate space for a table of local symbols. */
3165 j = f->local_symtab_size = sec->header.sh_info;
3166 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3168 /* Insert all symbols into the hash table. */
3169 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3170 ElfW(Addr) val = sym->st_value;
3173 name = strtab + sym->st_name;
3174 else if (sym->st_shndx < shnum)
3175 name = f->sections[sym->st_shndx]->name;
3179 #if defined(__SH5__)
3181 * For sh64 it is possible that the target of a branch
3182 * requires a mode switch (32 to 16 and back again).
3184 * This is implied by the lsb being set in the target
3185 * address for SHmedia mode and clear for SHcompact.
3187 val |= sym->st_other & 4;
3190 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3197 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3198 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3199 (unsigned long) sec->header.sh_entsize,
3200 (unsigned long) sizeof(ElfW(RelM)));
3204 /* XXX Relocation code from modutils-2.3.19 is not here.
3205 * Why? That's about 20 lines of code from obj/obj_load.c,
3206 * which gets done in a second pass through the sections.
3207 * This BusyBox insmod does similar work in obj_relocate(). */
3214 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3216 * load the unloaded sections directly into the memory allocated by
3217 * kernel for the module
3220 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3222 ElfW(Addr) base = f->baseaddr;
3223 struct obj_section* sec;
3225 for (sec = f->load_order; sec; sec = sec->load_next) {
3227 /* section already loaded? */
3228 if (sec->contents != NULL)
3231 if (sec->header.sh_size == 0)
3234 sec->contents = imagebase + (sec->header.sh_addr - base);
3235 fseek(fp, sec->header.sh_offset, SEEK_SET);
3236 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3237 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3246 static void hide_special_symbols(struct obj_file *f)
3248 static const char *const specials[] = {
3249 SPFX "cleanup_module",
3251 SPFX "kernel_version",
3255 struct obj_symbol *sym;
3256 const char *const *p;
3258 for (p = specials; *p; ++p)
3259 if ((sym = obj_find_symbol(f, *p)) != NULL)
3261 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3265 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3266 static int obj_gpl_license(struct obj_file *f, const char **license)
3268 struct obj_section *sec;
3269 /* This list must match *exactly* the list of allowable licenses in
3270 * linux/include/linux/module.h. Checking for leading "GPL" will not
3271 * work, somebody will use "GPL sucks, this is proprietary".
3273 static const char *gpl_licenses[] = {
3276 "GPL and additional rights",
3281 if ((sec = obj_find_section(f, ".modinfo"))) {
3282 const char *value, *ptr, *endptr;
3283 ptr = sec->contents;
3284 endptr = ptr + sec->header.sh_size;
3285 while (ptr < endptr) {
3286 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3290 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3291 if (strcmp(value+1, gpl_licenses[i]) == 0)
3296 if (strchr(ptr, '\0'))
3297 ptr = strchr(ptr, '\0') + 1;
3305 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3306 #define TAINT_PROPRIETORY_MODULE (1<<0)
3307 #define TAINT_FORCED_MODULE (1<<1)
3308 #define TAINT_UNSAFE_SMP (1<<2)
3309 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3311 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3312 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3316 static int first = 1;
3317 if (fd < 0 && !kernel_has_tainted)
3318 return; /* New modutils on old kernel */
3319 printf("Warning: loading %s will taint the kernel: %s%s\n",
3320 m_name, text1, text2);
3322 printf(" See %s for information about tainted modules\n", TAINT_URL);
3326 read(fd, buf, sizeof(buf)-1);
3327 buf[sizeof(buf)-1] = '\0';
3328 oldval = strtoul(buf, NULL, 10);
3329 sprintf(buf, "%d\n", oldval | taint);
3330 write(fd, buf, strlen(buf));
3334 /* Check if loading this module will taint the kernel. */
3335 static void check_tainted_module(struct obj_file *f, char *m_name)
3337 static const char tainted_file[] = TAINT_FILENAME;
3338 int fd, kernel_has_tainted;
3341 kernel_has_tainted = 1;
3342 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3343 if (errno == ENOENT)
3344 kernel_has_tainted = 0;
3345 else if (errno == EACCES)
3346 kernel_has_tainted = 1;
3348 perror(tainted_file);
3349 kernel_has_tainted = 0;
3353 switch (obj_gpl_license(f, &ptr)) {
3357 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3360 /* The module has a non-GPL license so we pretend that the
3361 * kernel always has a taint flag to get a warning even on
3362 * kernels without the proc flag.
3364 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3367 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3371 if (flag_force_load)
3372 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3377 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3378 #define check_tainted_module(x, y) do { } while(0);
3379 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3381 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3382 /* add module source, timestamp, kernel version and a symbol for the
3383 * start of some sections. this info is used by ksymoops to do better
3387 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3389 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3390 return new_get_module_version(f, str);
3391 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3392 strncpy(str, "???", sizeof(str));
3394 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3397 /* add module source, timestamp, kernel version and a symbol for the
3398 * start of some sections. this info is used by ksymoops to do better
3402 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3405 static const char symprefix[] = "__insmod_";
3406 struct obj_section *sec;
3407 struct obj_symbol *sym;
3408 char *name, *absolute_filename;
3409 char str[STRVERSIONLEN], real[PATH_MAX];
3410 int i, l, lm_name, lfilename, use_ksymtab, version;
3411 struct stat statbuf;
3413 static const char *section_names[] = {
3421 if (realpath(filename, real)) {
3422 absolute_filename = bb_xstrdup(real);
3425 int save_errno = errno;
3426 bb_error_msg("cannot get realpath for %s", filename);
3429 absolute_filename = bb_xstrdup(filename);
3432 lm_name = strlen(m_name);
3433 lfilename = strlen(absolute_filename);
3435 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3436 * are not to be exported. otherwise leave ksymtab alone for now, the
3437 * "export all symbols" compatibility code will export these symbols later.
3439 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3441 if ((sec = obj_find_section(f, ".this"))) {
3442 /* tag the module header with the object name, last modified
3443 * timestamp and module version. worst case for module version
3444 * is 0xffffff, decimal 16777215. putting all three fields in
3445 * one symbol is less readable but saves kernel space.
3447 l = sizeof(symprefix)+ /* "__insmod_" */
3448 lm_name+ /* module name */
3450 lfilename+ /* object filename */
3452 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3454 8+ /* version in dec */
3457 if (stat(absolute_filename, &statbuf) != 0)
3458 statbuf.st_mtime = 0;
3459 version = get_module_version(f, str); /* -1 if not found */
3460 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3461 symprefix, m_name, absolute_filename,
3462 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3464 sym = obj_add_symbol(f, name, -1,
3465 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3466 sec->idx, sec->header.sh_addr, 0);
3468 new_add_ksymtab(f, sym);
3470 free(absolute_filename);
3471 #ifdef _NOT_SUPPORTED_
3472 /* record where the persistent data is going, same address as previous symbol */
3475 l = sizeof(symprefix)+ /* "__insmod_" */
3476 lm_name+ /* module name */
3478 strlen(f->persist)+ /* data store */
3481 snprintf(name, l, "%s%s_P%s",
3482 symprefix, m_name, f->persist);
3483 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3484 sec->idx, sec->header.sh_addr, 0);
3486 new_add_ksymtab(f, sym);
3488 #endif /* _NOT_SUPPORTED_ */
3489 /* tag the desired sections if size is non-zero */
3491 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3492 if ((sec = obj_find_section(f, section_names[i])) &&
3493 sec->header.sh_size) {
3494 l = sizeof(symprefix)+ /* "__insmod_" */
3495 lm_name+ /* module name */
3497 strlen(sec->name)+ /* section name */
3499 8+ /* length in dec */
3502 snprintf(name, l, "%s%s_S%s_L%ld",
3503 symprefix, m_name, sec->name,
3504 (long)sec->header.sh_size);
3505 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3506 sec->idx, sec->header.sh_addr, 0);
3508 new_add_ksymtab(f, sym);
3512 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3514 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3515 static void print_load_map(struct obj_file *f)
3517 struct obj_symbol *sym;
3518 struct obj_symbol **all, **p;
3519 struct obj_section *sec;
3520 int i, nsyms, *loaded;
3522 /* Report on the section layout. */
3524 printf("Sections: Size %-*s Align\n",
3525 (int) (2 * sizeof(void *)), "Address");
3527 for (sec = f->load_order; sec; sec = sec->load_next) {
3531 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3536 printf("%-15s %08lx %0*lx 2**%d\n",
3538 (long)sec->header.sh_size,
3539 (int) (2 * sizeof(void *)),
3540 (long)sec->header.sh_addr,
3543 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3544 /* Quick reference which section indicies are loaded. */
3546 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3548 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3550 /* Collect the symbols we'll be listing. */
3552 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3553 for (sym = f->symtab[i]; sym; sym = sym->next)
3554 if (sym->secidx <= SHN_HIRESERVE
3555 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3558 all = alloca(nsyms * sizeof(struct obj_symbol *));
3560 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3561 for (sym = f->symtab[i]; sym; sym = sym->next)
3562 if (sym->secidx <= SHN_HIRESERVE
3563 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3566 /* And list them. */
3567 printf("\nSymbols:\n");
3568 for (p = all; p < all + nsyms; ++p) {
3570 unsigned long value;
3573 if (sym->secidx == SHN_ABS) {
3576 } else if (sym->secidx == SHN_UNDEF) {
3580 sec = f->sections[sym->secidx];
3582 if (sec->header.sh_type == SHT_NOBITS)
3584 else if (sec->header.sh_flags & SHF_ALLOC) {
3585 if (sec->header.sh_flags & SHF_EXECINSTR)
3587 else if (sec->header.sh_flags & SHF_WRITE)
3592 value = sym->value + sec->header.sh_addr;
3595 if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
3596 type = tolower(type);
3598 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3606 extern int insmod_main( int argc, char **argv)
3612 unsigned long m_size;
3617 int exit_status = EXIT_FAILURE;
3619 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3620 struct utsname uts_info;
3621 char m_strversion[STRVERSIONLEN];
3622 int m_version, m_crcs;
3624 #ifdef CONFIG_FEATURE_CLEAN_UP
3629 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3630 int flag_print_load_map = 0;
3633 struct utsname myuname;
3635 /* Parse any options */
3636 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3637 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
3639 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
3643 case 'f': /* force loading */
3644 flag_force_load = 1;
3646 case 'k': /* module loaded by kerneld, auto-cleanable */
3649 case 's': /* log to syslog */
3650 /* log to syslog -- not supported */
3651 /* but kernel needs this for request_module(), */
3652 /* as this calls: modprobe -k -s -- <module> */
3653 /* so silently ignore this flag */
3655 case 'v': /* verbose output */
3658 case 'q': /* silent */
3661 case 'x': /* do not export externs */
3664 case 'o': /* name the output module */
3666 m_name = bb_xstrdup(optarg);
3668 case 'L': /* Stub warning */
3669 /* This is needed for compatibility with modprobe.
3670 * In theory, this does locking, but we don't do
3671 * that. So be careful and plan your life around not
3672 * loading the same module 50 times concurrently. */
3674 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3675 case 'm': /* print module load map */
3676 flag_print_load_map = 1;
3684 if (argv[optind] == NULL) {
3688 /* Grab the module name */
3689 tmp1 = bb_xstrdup(argv[optind]);
3690 tmp = basename(tmp1);
3693 if (uname(&myuname) == 0) {
3694 if (myuname.release[0] == '2') {
3695 k_version = myuname.release[2] - '0';
3699 #if defined(CONFIG_FEATURE_2_6_MODULES)
3700 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
3701 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
3707 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3713 #if defined(CONFIG_FEATURE_2_6_MODULES)
3715 bb_xasprintf(&m_fullName, "%s.ko", tmp);
3718 bb_xasprintf(&m_fullName, "%s.o", tmp);
3724 tmp1 = 0; /* flag for free(m_name) before exit() */
3727 /* Get a filedesc for the module. Check we we have a complete path */
3728 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3729 (fp = fopen(argv[optind], "r")) == NULL) {
3730 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3731 * but do not error out yet if we fail to find it... */
3732 if (k_version) { /* uname succeedd */
3735 char real_module_dir[FILENAME_MAX];
3737 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
3738 /* Jump through hoops in case /lib/modules/`uname -r`
3739 * is a symlink. We do not want recursive_action to
3740 * follow symlinks, but we do want to follow the
3741 * /lib/modules/`uname -r` dir, So resolve it ourselves
3742 * if it is a link... */
3743 if (realpath (tmdn, real_module_dir) == NULL)
3746 module_dir = real_module_dir;
3747 recursive_action(module_dir, TRUE, FALSE, FALSE,
3748 check_module_name_match, 0, m_fullName);
3752 /* Check if we have found anything yet */
3753 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
3755 char module_dir[FILENAME_MAX];
3759 if (realpath (_PATH_MODULES, module_dir) == NULL)
3760 strcpy(module_dir, _PATH_MODULES);
3761 /* No module found under /lib/modules/`uname -r`, this
3762 * time cast the net a bit wider. Search /lib/modules/ */
3763 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3764 check_module_name_match, 0, m_fullName))
3767 || ((fp = fopen(m_filename, "r")) == NULL))
3769 bb_error_msg("%s: no module by that name found", m_fullName);
3773 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
3776 m_filename = bb_xstrdup(argv[optind]);
3779 printf("Using %s\n", m_filename);
3781 #ifdef CONFIG_FEATURE_2_6_MODULES
3785 argv[optind + 1] = m_filename;
3786 return insmod_ng_main(argc - optind, argv + optind);
3790 if ((f = obj_load(fp, LOADBITS)) == NULL)
3791 bb_perror_msg_and_die("Could not load the module");
3793 if (get_modinfo_value(f, "kernel_version") == NULL)
3798 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3799 /* Version correspondence? */
3801 if (uname(&uts_info) < 0)
3802 uts_info.release[0] = '\0';
3803 if (m_has_modinfo) {
3804 m_version = new_get_module_version(f, m_strversion);
3805 if (m_version == -1) {
3806 bb_error_msg("couldn't find the kernel version the module was "
3812 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3813 if (flag_force_load) {
3814 bb_error_msg("Warning: kernel-module version mismatch\n"
3815 "\t%s was compiled for kernel version %s\n"
3816 "\twhile this kernel is version %s",
3817 m_filename, m_strversion, uts_info.release);
3819 bb_error_msg("kernel-module version mismatch\n"
3820 "\t%s was compiled for kernel version %s\n"
3821 "\twhile this kernel is version %s.",
3822 m_filename, m_strversion, uts_info.release);
3828 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3830 if (!query_module(NULL, 0, NULL, 0, NULL)) {
3831 if (!new_get_kernel_symbols())
3833 k_crcs = new_is_kernel_checksummed();
3835 bb_error_msg("Not configured to support old kernels");
3839 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3842 m_crcs = new_is_module_checksummed(f);
3844 if (m_crcs != k_crcs)
3845 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3846 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3848 /* Let the module know about the kernel symbols. */
3849 add_kernel_symbols(f);
3851 /* Allocate common symbols, symbol tables, and string tables. */
3853 if (!new_create_this_module(f, m_name))
3858 if (!obj_check_undefineds(f)) {
3861 obj_allocate_commons(f);
3862 check_tainted_module(f, m_name);
3864 /* done with the module name, on to the optional var=value arguments */
3867 if (optind < argc) {
3868 if (new_process_module_arguments(f, argc - optind, argv + optind))
3875 hide_special_symbols(f);
3877 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3878 add_ksymoops_symbols(f, m_filename, m_name);
3879 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3881 new_create_module_ksymtab(f);
3883 /* Find current size of the module */
3884 m_size = obj_load_size(f);
3887 m_addr = create_module(m_name, m_size);
3888 if (m_addr == -1) switch (errno) {
3890 bb_error_msg("A module named %s already exists", m_name);
3893 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3897 bb_perror_msg("create_module: %s", m_name);
3903 * the PROGBITS section was not loaded by the obj_load
3904 * now we can load them directly into the kernel memory
3906 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3907 delete_module(m_name);
3912 if (!obj_relocate(f, m_addr)) {
3913 delete_module(m_name);
3917 if (!new_init_module(m_name, f, m_size))
3919 delete_module(m_name);
3923 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3924 if(flag_print_load_map)
3928 exit_status = EXIT_SUCCESS;
3931 #ifdef CONFIG_FEATURE_CLEAN_UP
3941 return(exit_status);
3948 #ifdef CONFIG_FEATURE_2_6_MODULES
3950 #include <sys/mman.h>
3951 #include <asm/unistd.h>
3952 #include <sys/syscall.h>
3954 /* We use error numbers in a loose translation... */
3955 static const char *moderror(int err)
3959 return "Invalid module format";
3961 return "Unknown symbol in module";
3963 return "Module has wrong symbol version";
3965 return "Invalid parameters";
3967 return strerror(err);
3971 extern int insmod_ng_main( int argc, char **argv)
3979 char *filename, *options = bb_xstrdup("");
3987 /* Rest is options */
3988 for (i = 2; i < argc; i++) {
3989 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
3990 /* Spaces handled by "" pairs, but no way of escaping quotes */
3991 if (strchr(argv[i], ' ')) {
3992 strcat(options, "\"");
3993 strcat(options, argv[i]);
3994 strcat(options, "\"");
3996 strcat(options, argv[i]);
3998 strcat(options, " ");
4001 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4002 bb_perror_msg_and_die("cannot open module `%s'", filename);
4007 map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
4008 if (map == MAP_FAILED) {
4009 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4012 ret = syscall(__NR_init_module, map, len, options);
4014 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4015 filename, moderror(errno), ret);