1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * This version of insmod supports x86, x86_64, 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 * 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 * This program is free software; you can redistribute it and/or modify
59 * it under the terms of the GNU General Public License as published by
60 * the Free Software Foundation; either version 2 of the License, or
61 * (at your option) any later version.
63 * This program is distributed in the hope that it will be useful,
64 * but WITHOUT ANY WARRANTY; without even the implied warranty of
65 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
66 * General Public License for more details.
68 * You should have received a copy of the GNU General Public License
69 * along with this program; if not, write to the Free Software
70 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
85 #include <sys/utsname.h>
88 #if !defined(CONFIG_FEATURE_2_4_MODULES) && \
89 !defined(CONFIG_FEATURE_2_6_MODULES)
90 #define CONFIG_FEATURE_2_4_MODULES
93 #if !defined(CONFIG_FEATURE_2_4_MODULES)
94 #define insmod_ng_main insmod_main
97 #if defined(CONFIG_FEATURE_2_6_MODULES)
98 extern int insmod_ng_main( int argc, char **argv);
102 #if defined(CONFIG_FEATURE_2_4_MODULES)
105 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
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
119 #define MATCH_MACHINE(x) (x == EM_ARM)
120 #define SHT_RELM SHT_REL
121 #define Elf32_RelM Elf32_Rel
122 #define ELFCLASSM ELFCLASS32
125 #if defined(__s390__)
126 #define CONFIG_USE_PLT_ENTRIES
127 #define CONFIG_PLT_ENTRY_SIZE 8
128 #define CONFIG_USE_GOT_ENTRIES
129 #define CONFIG_GOT_ENTRY_SIZE 8
130 #define CONFIG_USE_SINGLE
132 #define MATCH_MACHINE(x) (x == EM_S390)
133 #define SHT_RELM SHT_RELA
134 #define Elf32_RelM Elf32_Rela
135 #define ELFCLASSM ELFCLASS32
138 #if defined(__i386__)
139 #define CONFIG_USE_GOT_ENTRIES
140 #define CONFIG_GOT_ENTRY_SIZE 4
141 #define CONFIG_USE_SINGLE
144 #define MATCH_MACHINE(x) (x == EM_386)
146 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
149 #define SHT_RELM SHT_REL
150 #define Elf32_RelM Elf32_Rel
151 #define ELFCLASSM ELFCLASS32
154 #if defined(__x86_64__)
155 #define MATCH_MACHINE(x) (x == EM_X86_64)
156 #define SHT_RELM SHT_REL
157 #define Elf64_RelM Elf64_Rel
158 #define ELFCLASSM ELFCLASS64
161 #if defined(__mc68000__)
162 #define CONFIG_USE_GOT_ENTRIES
163 #define CONFIG_GOT_ENTRY_SIZE 4
164 #define CONFIG_USE_SINGLE
166 #define MATCH_MACHINE(x) (x == EM_68K)
167 #define SHT_RELM SHT_RELA
168 #define Elf32_RelM Elf32_Rela
169 #define ELFCLASSM ELFCLASS32
172 #if defined(__mips__)
173 /* Account for ELF spec changes. */
174 #ifndef EM_MIPS_RS3_LE
175 #ifdef EM_MIPS_RS4_BE
176 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
178 #define EM_MIPS_RS3_LE 10
180 #endif /* !EM_MIPS_RS3_LE */
182 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
183 #define SHT_RELM SHT_REL
184 #define Elf32_RelM Elf32_Rel
185 #define ELFCLASSM ELFCLASS32
186 #define ARCHDATAM "__dbe_table"
189 #if defined(__powerpc__)
190 #define CONFIG_USE_PLT_ENTRIES
191 #define CONFIG_PLT_ENTRY_SIZE 16
192 #define CONFIG_USE_PLT_LIST
193 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
194 #define CONFIG_USE_LIST
196 #define MATCH_MACHINE(x) (x == EM_PPC)
197 #define SHT_RELM SHT_RELA
198 #define Elf32_RelM Elf32_Rela
199 #define ELFCLASSM ELFCLASS32
200 #define ARCHDATAM "__ftr_fixup"
204 #define CONFIG_USE_GOT_ENTRIES
205 #define CONFIG_GOT_ENTRY_SIZE 4
206 #define CONFIG_USE_SINGLE
208 #define MATCH_MACHINE(x) (x == EM_SH)
209 #define SHT_RELM SHT_RELA
210 #define Elf32_RelM Elf32_Rela
211 #define ELFCLASSM ELFCLASS32
213 /* the SH changes have only been tested in =little endian= mode */
214 /* I'm not sure about big endian, so let's warn: */
216 #if defined(__sh__) && defined(__BIG_ENDIAN__)
217 #error insmod.c may require changes for use on big endian SH
220 /* it may or may not work on the SH1/SH2... So let's error on those
222 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && \
224 #error insmod.c may require changes for SH1 or SH2 use
228 #if defined (__v850e__)
229 #define CONFIG_USE_PLT_ENTRIES
230 #define CONFIG_PLT_ENTRY_SIZE 8
231 #define CONFIG_USE_SINGLE
233 #ifndef EM_CYGNUS_V850 /* grumble */
234 #define EM_CYGNUS_V850 0x9080
237 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
238 #define SHT_RELM SHT_RELA
239 #define Elf32_RelM Elf32_Rela
240 #define ELFCLASSM ELFCLASS32
242 #define SYMBOL_PREFIX "_"
245 #if defined(__cris__)
248 #define R_CRIS_NONE 0
252 #define MATCH_MACHINE(x) (x == EM_CRIS)
253 #define SHT_RELM SHT_RELA
254 #define Elf32_RelM Elf32_Rela
255 #define ELFCLASSM ELFCLASS32
258 #if defined(__H8300H__) || defined(__H8300S__)
259 #define CONFIG_USE_SINGLE
261 #define MATCH_MACHINE(x) (x == EM_H8_300)
262 #define SHT_RELM SHT_RELA
263 #define Elf32_RelM Elf32_Rela
265 #define ELFCLASSM ELFCLASS32
266 #define SYMBOL_PREFIX "_"
270 #error Sorry, but insmod.c does not yet support this architecture...
274 //----------------------------------------------------------------------------
275 //--------modutils module.h, lines 45-242
276 //----------------------------------------------------------------------------
278 /* Definitions for the Linux module syscall interface.
279 Copyright 1996, 1997 Linux International.
281 Contributed by Richard Henderson <rth@tamu.edu>
283 This file is part of the Linux modutils.
285 This program is free software; you can redistribute it and/or modify it
286 under the terms of the GNU General Public License as published by the
287 Free Software Foundation; either version 2 of the License, or (at your
288 option) any later version.
290 This program is distributed in the hope that it will be useful, but
291 WITHOUT ANY WARRANTY; without even the implied warranty of
292 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
293 General Public License for more details.
295 You should have received a copy of the GNU General Public License
296 along with this program; if not, write to the Free Software Foundation,
297 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
300 #ifndef MODUTILS_MODULE_H
301 static const int MODUTILS_MODULE_H = 1;
303 #ident "$Id: insmod.c,v 1.124 2004/08/28 00:43:06 andersen Exp $"
305 /*======================================================================*/
306 /* For sizeof() which are related to the module platform and not to the
307 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
309 #define tgt_sizeof_char sizeof(char)
310 #define tgt_sizeof_short sizeof(short)
311 #define tgt_sizeof_int sizeof(int)
312 #define tgt_sizeof_long sizeof(long)
313 #define tgt_sizeof_char_p sizeof(char *)
314 #define tgt_sizeof_void_p sizeof(void *)
315 #define tgt_long long
317 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
318 #undef tgt_sizeof_long
319 #undef tgt_sizeof_char_p
320 #undef tgt_sizeof_void_p
322 static const int tgt_sizeof_long = 8;
323 static const int tgt_sizeof_char_p = 8;
324 static const int tgt_sizeof_void_p = 8;
325 #define tgt_long long long
328 /*======================================================================*/
329 /* The structures used in Linux 2.1. */
331 /* Note: new_module_symbol does not use tgt_long intentionally */
332 struct new_module_symbol
338 struct new_module_persist;
340 struct new_module_ref
342 unsigned tgt_long dep; /* kernel addresses */
343 unsigned tgt_long ref;
344 unsigned tgt_long next_ref;
349 unsigned tgt_long size_of_struct; /* == sizeof(module) */
350 unsigned tgt_long next;
351 unsigned tgt_long name;
352 unsigned tgt_long size;
355 unsigned tgt_long flags; /* AUTOCLEAN et al */
360 unsigned tgt_long syms;
361 unsigned tgt_long deps;
362 unsigned tgt_long refs;
363 unsigned tgt_long init;
364 unsigned tgt_long cleanup;
365 unsigned tgt_long ex_table_start;
366 unsigned tgt_long ex_table_end;
368 unsigned tgt_long gp;
370 /* Everything after here is extension. */
371 unsigned tgt_long persist_start;
372 unsigned tgt_long persist_end;
373 unsigned tgt_long can_unload;
374 unsigned tgt_long runsize;
375 const char *kallsyms_start; /* All symbols for kernel debugging */
376 const char *kallsyms_end;
377 const char *archdata_start; /* arch specific data for module */
378 const char *archdata_end;
379 const char *kernel_data; /* Reserved for kernel internal use */
383 #define ARCHDATA_SEC_NAME ARCHDATAM
385 #define ARCHDATA_SEC_NAME "__archdata"
387 #define KALLSYMS_SEC_NAME "__kallsyms"
390 struct new_module_info
398 /* Bits of module.flags. */
399 static const int NEW_MOD_RUNNING = 1;
400 static const int NEW_MOD_DELETED = 2;
401 static const int NEW_MOD_AUTOCLEAN = 4;
402 static const int NEW_MOD_VISITED = 8;
403 static const int NEW_MOD_USED_ONCE = 16;
405 int init_module(const char *name, const struct new_module *);
406 int query_module(const char *name, int which, void *buf,
407 size_t bufsize, size_t *ret);
409 /* Values for query_module's which. */
411 static const int QM_MODULES = 1;
412 static const int QM_DEPS = 2;
413 static const int QM_REFS = 3;
414 static const int QM_SYMBOLS = 4;
415 static const int QM_INFO = 5;
417 /*======================================================================*/
418 /* The system calls unchanged between 2.0 and 2.1. */
420 unsigned long create_module(const char *, size_t);
421 int delete_module(const char *);
424 #endif /* module.h */
426 //----------------------------------------------------------------------------
427 //--------end of modutils module.h
428 //----------------------------------------------------------------------------
432 //----------------------------------------------------------------------------
433 //--------modutils obj.h, lines 253-462
434 //----------------------------------------------------------------------------
436 /* Elf object file loading and relocation routines.
437 Copyright 1996, 1997 Linux International.
439 Contributed by Richard Henderson <rth@tamu.edu>
441 This file is part of the Linux modutils.
443 This program is free software; you can redistribute it and/or modify it
444 under the terms of the GNU General Public License as published by the
445 Free Software Foundation; either version 2 of the License, or (at your
446 option) any later version.
448 This program is distributed in the hope that it will be useful, but
449 WITHOUT ANY WARRANTY; without even the implied warranty of
450 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
451 General Public License for more details.
453 You should have received a copy of the GNU General Public License
454 along with this program; if not, write to the Free Software Foundation,
455 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
458 #ifndef MODUTILS_OBJ_H
459 static const int MODUTILS_OBJ_H = 1;
461 #ident "$Id: insmod.c,v 1.124 2004/08/28 00:43:06 andersen Exp $"
463 /* The relocatable object is manipulated using elfin types. */
469 #if __BYTE_ORDER == __LITTLE_ENDIAN
470 #define ELFDATAM ELFDATA2LSB
471 #elif __BYTE_ORDER == __BIG_ENDIAN
472 #define ELFDATAM ELFDATA2MSB
476 # if ELFCLASSM == ELFCLASS32
477 # define ElfW(x) Elf32_ ## x
478 # define ELFW(x) ELF32_ ## x
480 # define ElfW(x) Elf64_ ## x
481 # define ELFW(x) ELF64_ ## x
485 /* For some reason this is missing from some ancient C libraries.... */
486 #ifndef ELF32_ST_INFO
487 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
490 #ifndef ELF64_ST_INFO
491 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
494 struct obj_string_patch;
495 struct obj_symbol_patch;
502 struct obj_section *load_next;
508 struct obj_symbol *next; /* hash table link */
512 int secidx; /* the defining section index/module */
514 int ksymidx; /* for export to the kernel symtab */
515 int referenced; /* actually used in the link */
518 /* Hardcode the hash table size. We shouldn't be needing so many
519 symbols that we begin to degrade performance, and we get a big win
520 by giving the compiler a constant divisor. */
522 #define HASH_BUCKETS 521
528 struct obj_section **sections;
529 struct obj_section *load_order;
530 struct obj_section **load_order_search_start;
531 struct obj_string_patch *string_patches;
532 struct obj_symbol_patch *symbol_patches;
533 int (*symbol_cmp)(const char *, const char *);
534 unsigned long (*symbol_hash)(const char *);
535 unsigned long local_symtab_size;
536 struct obj_symbol **local_symtab;
537 struct obj_symbol *symtab[HASH_BUCKETS];
548 struct obj_string_patch
550 struct obj_string_patch *next;
552 ElfW(Addr) reloc_offset;
553 ElfW(Addr) string_offset;
556 struct obj_symbol_patch
558 struct obj_symbol_patch *next;
560 ElfW(Addr) reloc_offset;
561 struct obj_symbol *sym;
565 /* Generic object manipulation routines. */
567 static unsigned long obj_elf_hash(const char *);
569 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
571 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
574 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
575 struct obj_symbol *sym);
577 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
578 static void obj_set_symbol_compare(struct obj_file *f,
579 int (*cmp)(const char *, const char *),
580 unsigned long (*hash)(const char *));
583 static struct obj_section *obj_find_section (struct obj_file *f,
586 static void obj_insert_section_load_order (struct obj_file *f,
587 struct obj_section *sec);
589 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
594 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
599 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
601 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
604 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
605 struct obj_symbol *sym);
607 static int obj_check_undefineds(struct obj_file *f);
609 static void obj_allocate_commons(struct obj_file *f);
611 static unsigned long obj_load_size (struct obj_file *f);
613 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
615 static struct obj_file *obj_load(FILE *f, int loadprogbits);
617 static int obj_create_image (struct obj_file *f, char *image);
619 /* Architecture specific manipulation routines. */
621 static struct obj_file *arch_new_file (void);
623 static struct obj_section *arch_new_section (void);
625 static struct obj_symbol *arch_new_symbol (void);
627 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
628 struct obj_section *targsec,
629 struct obj_section *symsec,
630 struct obj_symbol *sym,
631 ElfW(RelM) *rel, ElfW(Addr) value);
633 static void arch_create_got (struct obj_file *f);
635 static int obj_gpl_license(struct obj_file *f, const char **license);
638 //----------------------------------------------------------------------------
639 //--------end of modutils obj.h
640 //----------------------------------------------------------------------------
643 /* SPFX is always a string, so it can be concatenated to string constants. */
645 #define SPFX SYMBOL_PREFIX
651 #define _PATH_MODULES "/lib/modules"
652 static const int STRVERSIONLEN = 32;
654 /*======================================================================*/
656 static int flag_force_load = 0;
657 static int flag_autoclean = 0;
658 static int flag_verbose = 0;
659 static int flag_quiet = 0;
660 static int flag_export = 1;
663 /*======================================================================*/
665 #if defined(CONFIG_USE_LIST)
667 struct arch_list_entry
669 struct arch_list_entry *next;
670 CONFIG_LIST_ARCHTYPE addend;
677 #if defined(CONFIG_USE_SINGLE)
679 struct arch_single_entry
688 #if defined(__mips__)
691 struct mips_hi16 *next;
698 struct obj_file root;
699 #if defined(CONFIG_USE_PLT_ENTRIES)
700 struct obj_section *plt;
702 #if defined(CONFIG_USE_GOT_ENTRIES)
703 struct obj_section *got;
705 #if defined(__mips__)
706 struct mips_hi16 *mips_hi16_list;
711 struct obj_symbol root;
712 #if defined(CONFIG_USE_PLT_ENTRIES)
713 #if defined(CONFIG_USE_PLT_LIST)
714 struct arch_list_entry *pltent;
716 struct arch_single_entry pltent;
719 #if defined(CONFIG_USE_GOT_ENTRIES)
720 struct arch_single_entry gotent;
725 struct external_module {
730 struct new_module_symbol *syms;
733 static struct new_module_symbol *ksyms;
734 static size_t nksyms;
736 static struct external_module *ext_modules;
737 static int n_ext_modules;
738 static int n_ext_modules_used;
739 extern int delete_module(const char *);
741 static char *m_filename;
742 static char *m_fullName;
746 /*======================================================================*/
749 static int check_module_name_match(const char *filename, struct stat *statbuf,
752 char *fullname = (char *) userdata;
754 if (fullname[0] == '\0')
757 char *tmp, *tmp1 = bb_xstrdup(filename);
758 tmp = bb_get_last_path_component(tmp1);
759 if (strcmp(tmp, fullname) == 0) {
761 /* Stop searching if we find a match */
762 m_filename = bb_xstrdup(filename);
771 /*======================================================================*/
773 static struct obj_file *arch_new_file(void)
776 f = xmalloc(sizeof(*f));
778 memset(f, 0, sizeof(*f));
783 static struct obj_section *arch_new_section(void)
785 return xmalloc(sizeof(struct obj_section));
788 static struct obj_symbol *arch_new_symbol(void)
790 struct arch_symbol *sym;
791 sym = xmalloc(sizeof(*sym));
793 memset(sym, 0, sizeof(*sym));
798 static enum obj_reloc
799 arch_apply_relocation(struct obj_file *f,
800 struct obj_section *targsec,
801 struct obj_section *symsec,
802 struct obj_symbol *sym,
803 ElfW(RelM) *rel, ElfW(Addr) v)
805 struct arch_file *ifile = (struct arch_file *) f;
806 enum obj_reloc ret = obj_reloc_ok;
807 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
808 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
809 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
810 struct arch_symbol *isym = (struct arch_symbol *) sym;
812 #if defined(CONFIG_USE_GOT_ENTRIES)
813 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
815 #if defined(CONFIG_USE_PLT_ENTRIES)
816 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
818 #if defined(CONFIG_USE_PLT_LIST)
819 struct arch_list_entry *pe;
821 struct arch_single_entry *pe;
825 switch (ELF32_R_TYPE(rel->r_info)) {
840 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
841 * (which is .got) similar to branch,
842 * but is full 32 bits relative */
852 case R_ARM_GOTOFF: /* address relative to the got */
857 #elif defined(__s390__)
859 *(unsigned int *) loc += v;
862 *(unsigned short *) loc += v;
865 *(unsigned char *) loc += v;
869 *(unsigned int *) loc += v - dot;
872 *(unsigned short *) loc += (v - dot) >> 1;
875 *(unsigned short *) loc += v - dot;
880 /* find the plt entry and initialize it. */
881 assert(isym != NULL);
882 pe = (struct arch_single_entry *) &isym->pltent;
883 assert(pe->allocated);
884 if (pe->inited == 0) {
885 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
886 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
888 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
895 /* Insert relative distance to target. */
896 v = plt + pe->offset - dot;
897 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
898 *(unsigned int *) loc = (unsigned int) v;
899 else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
900 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
914 *(unsigned long *) loc += got - dot;
920 assert(isym != NULL);
922 if (!isym->gotent.inited)
924 isym->gotent.inited = 1;
925 *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
927 if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
928 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
929 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
930 *(unsigned short *) loc += isym->gotent.offset;
931 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
932 *(unsigned int *) loc += isym->gotent.offset;
935 #ifndef R_390_GOTOFF32
936 #define R_390_GOTOFF32 R_390_GOTOFF
943 #elif defined(__i386__)
979 #elif defined(__mc68000__)
990 ret = obj_reloc_overflow;
997 ret = obj_reloc_overflow;
1004 if ((Elf32_Sword)v > 0x7f ||
1005 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
1006 ret = obj_reloc_overflow;
1013 if ((Elf32_Sword)v > 0x7fff ||
1014 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1015 ret = obj_reloc_overflow;
1021 *(int *)loc = v - dot;
1024 case R_68K_GLOB_DAT:
1025 case R_68K_JMP_SLOT:
1029 case R_68K_RELATIVE:
1030 *(int *)loc += f->baseaddr;
1043 #elif defined(__mips__)
1054 ret = obj_reloc_dangerous;
1055 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1056 ret = obj_reloc_overflow;
1058 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1064 struct mips_hi16 *n;
1066 /* We cannot relocate this one now because we don't know the value
1067 of the carry we need to add. Save the information, and let LO16
1068 do the actual relocation. */
1069 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1072 n->next = ifile->mips_hi16_list;
1073 ifile->mips_hi16_list = n;
1079 unsigned long insnlo = *loc;
1080 Elf32_Addr val, vallo;
1082 /* Sign extend the addend we extract from the lo insn. */
1083 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1085 if (ifile->mips_hi16_list != NULL) {
1086 struct mips_hi16 *l;
1088 l = ifile->mips_hi16_list;
1090 struct mips_hi16 *next;
1093 /* The value for the HI16 had best be the same. */
1094 assert(v == l->value);
1096 /* Do the HI16 relocation. Note that we actually don't
1097 need to know anything about the LO16 itself, except where
1098 to find the low 16 bits of the addend needed by the LO16. */
1101 ((insn & 0xffff) << 16) +
1105 /* Account for the sign extension that will happen in the
1112 insn = (insn & ~0xffff) | val;
1120 ifile->mips_hi16_list = NULL;
1123 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1125 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1130 #elif defined(__powerpc__)
1132 case R_PPC_ADDR16_HA:
1133 *(unsigned short *)loc = (v + 0x8000) >> 16;
1136 case R_PPC_ADDR16_HI:
1137 *(unsigned short *)loc = v >> 16;
1140 case R_PPC_ADDR16_LO:
1141 *(unsigned short *)loc = v;
1155 #elif defined(__sh__)
1178 *loc = f->baseaddr + rel->r_addend;
1183 *loc = got - dot + rel->r_addend;
1194 #if defined(__SH5__)
1195 case R_SH_IMM_MEDLOW16:
1196 case R_SH_IMM_LOW16:
1200 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1204 * movi and shori have the format:
1206 * | op | imm | reg | reserved |
1207 * 31..26 25..10 9.. 4 3 .. 0
1209 * so we simply mask and or in imm.
1211 word = *loc & ~0x3fffc00;
1212 word |= (v & 0xffff) << 10;
1219 case R_SH_IMM_MEDLOW16_PCREL:
1220 case R_SH_IMM_LOW16_PCREL:
1224 word = *loc & ~0x3fffc00;
1228 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1231 word |= (v & 0xffff) << 10;
1237 #endif /* __SH5__ */
1241 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1242 ret = obj_reloc_unhandled;
1245 #if defined (__v850e__)
1250 /* We write two shorts instead of a long because even
1251 32-bit insns only need half-word alignment, but
1252 32-bit data needs to be long-word aligned. */
1253 v += ((unsigned short *)loc)[0];
1254 v += ((unsigned short *)loc)[1] << 16;
1255 ((unsigned short *)loc)[0] = v & 0xffff;
1256 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1259 case R_V850_22_PCREL:
1263 #if defined (__cris__)
1268 /* CRIS keeps the relocation value in the r_addend field and
1269 * should not use whats in *loc at all
1275 #if defined(__H8300H__) || defined(__H8300S__)
1277 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
1278 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
1289 if ((Elf32_Sword)v > 0x7fff ||
1290 (Elf32_Sword)v < -(Elf32_Sword)0x8000)
1291 ret = obj_reloc_overflow;
1293 *(unsigned short *)loc = v;
1297 if ((Elf32_Sword)v > 0x7f ||
1298 (Elf32_Sword)v < -(Elf32_Sword)0x80)
1299 ret = obj_reloc_overflow;
1301 *(unsigned char *)loc = v;
1305 #if defined(CONFIG_USE_PLT_ENTRIES)
1309 /* find the plt entry and initialize it if necessary */
1310 assert(isym != NULL);
1312 #if defined(CONFIG_USE_PLT_LIST)
1313 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1321 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1323 /* generate some machine code */
1325 #if defined(__arm__)
1326 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1327 ip[1] = v; /* sym@ */
1329 #if defined(__powerpc__)
1330 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1331 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1332 ip[2] = 0x7d6903a6; /* mtctr r11 */
1333 ip[3] = 0x4e800420; /* bctr */
1335 #if defined (__v850e__)
1336 /* We have to trash a register, so we assume that any control
1337 transfer more than 21-bits away must be a function call
1338 (so we can use a call-clobbered register). */
1339 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1340 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1345 /* relative distance to target */
1347 /* if the target is too far away.... */
1348 #if defined (__arm__) || defined (__powerpc__)
1349 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1350 #elif defined (__v850e__)
1351 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1353 /* go via the plt */
1354 v = plt + pe->offset - dot;
1356 #if defined (__v850e__)
1361 ret = obj_reloc_dangerous;
1363 /* merge the offset into the instruction. */
1364 #if defined(__arm__)
1365 /* Convert to words. */
1368 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1370 #if defined(__powerpc__)
1371 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1373 #if defined (__v850e__)
1374 /* We write two shorts instead of a long because even 32-bit insns
1375 only need half-word alignment, but the 32-bit data write needs
1376 to be long-word aligned. */
1377 ((unsigned short *)loc)[0] =
1378 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1379 | ((v >> 16) & 0x3f); /* offs high part */
1380 ((unsigned short *)loc)[1] =
1381 (v & 0xffff); /* offs low part */
1384 #endif /* CONFIG_USE_PLT_ENTRIES */
1386 #if defined(CONFIG_USE_GOT_ENTRIES)
1389 assert(isym != NULL);
1390 /* needs an entry in the .got: set it, once */
1391 if (!isym->gotent.inited) {
1392 isym->gotent.inited = 1;
1393 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1395 /* make the reloc with_respect_to_.got */
1397 *loc += isym->gotent.offset + rel->r_addend;
1398 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1399 *loc += isym->gotent.offset;
1403 #endif /* CONFIG_USE_GOT_ENTRIES */
1410 #if defined(CONFIG_USE_LIST)
1412 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1413 int offset, int size)
1415 struct arch_list_entry *pe;
1417 for (pe = *list; pe != NULL; pe = pe->next) {
1418 if (pe->addend == rel->r_addend) {
1424 pe = xmalloc(sizeof(struct arch_list_entry));
1426 pe->addend = rel->r_addend;
1427 pe->offset = offset;
1437 #if defined(CONFIG_USE_SINGLE)
1439 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1440 int offset, int size)
1442 if (single->allocated == 0) {
1443 single->allocated = 1;
1444 single->offset = offset;
1453 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1455 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1456 int offset, int size)
1458 struct obj_section *myrelsec = obj_find_section(f, name);
1465 obj_extend_section(myrelsec, offset);
1467 myrelsec = obj_create_alloced_section(f, name,
1477 static void arch_create_got(struct obj_file *f)
1479 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1480 struct arch_file *ifile = (struct arch_file *) f;
1482 #if defined(CONFIG_USE_GOT_ENTRIES)
1483 int got_offset = 0, got_needed = 0, got_allocate;
1485 #if defined(CONFIG_USE_PLT_ENTRIES)
1486 int plt_offset = 0, plt_needed = 0, plt_allocate;
1488 struct obj_section *relsec, *symsec, *strsec;
1489 ElfW(RelM) *rel, *relend;
1490 ElfW(Sym) *symtab, *extsym;
1491 const char *strtab, *name;
1492 struct arch_symbol *intsym;
1494 for (i = 0; i < f->header.e_shnum; ++i) {
1495 relsec = f->sections[i];
1496 if (relsec->header.sh_type != SHT_RELM)
1499 symsec = f->sections[relsec->header.sh_link];
1500 strsec = f->sections[symsec->header.sh_link];
1502 rel = (ElfW(RelM) *) relsec->contents;
1503 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1504 symtab = (ElfW(Sym) *) symsec->contents;
1505 strtab = (const char *) strsec->contents;
1507 for (; rel < relend; ++rel) {
1508 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1510 #if defined(CONFIG_USE_GOT_ENTRIES)
1513 #if defined(CONFIG_USE_PLT_ENTRIES)
1517 switch (ELF32_R_TYPE(rel->r_info)) {
1518 #if defined(__arm__)
1533 #elif defined(__i386__)
1543 #elif defined(__powerpc__)
1548 #elif defined(__mc68000__)
1559 #elif defined(__sh__)
1569 #elif defined (__v850e__)
1570 case R_V850_22_PCREL:
1579 if (extsym->st_name != 0) {
1580 name = strtab + extsym->st_name;
1582 name = f->sections[extsym->st_shndx]->name;
1584 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1585 #if defined(CONFIG_USE_GOT_ENTRIES)
1587 got_offset += arch_single_init(
1588 rel, &intsym->gotent,
1589 got_offset, CONFIG_GOT_ENTRY_SIZE);
1594 #if defined(CONFIG_USE_PLT_ENTRIES)
1596 #if defined(CONFIG_USE_PLT_LIST)
1597 plt_offset += arch_list_add(
1598 rel, &intsym->pltent,
1599 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1601 plt_offset += arch_single_init(
1602 rel, &intsym->pltent,
1603 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1611 #if defined(CONFIG_USE_GOT_ENTRIES)
1613 ifile->got = arch_xsect_init(f, ".got", got_offset,
1614 CONFIG_GOT_ENTRY_SIZE);
1618 #if defined(CONFIG_USE_PLT_ENTRIES)
1620 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1621 CONFIG_PLT_ENTRY_SIZE);
1625 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1628 /*======================================================================*/
1630 /* Standard ELF hash function. */
1631 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1633 unsigned long h = 0;
1640 if ((g = (h & 0xf0000000)) != 0) {
1649 static unsigned long obj_elf_hash(const char *name)
1651 return obj_elf_hash_n(name, strlen(name));
1654 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1655 /* String comparison for non-co-versioned kernel and module. */
1657 static int ncv_strcmp(const char *a, const char *b)
1659 size_t alen = strlen(a), blen = strlen(b);
1661 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1662 return strncmp(a, b, alen);
1663 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1664 return strncmp(a, b, blen);
1666 return strcmp(a, b);
1669 /* String hashing for non-co-versioned kernel and module. Here
1670 we are simply forced to drop the crc from the hash. */
1672 static unsigned long ncv_symbol_hash(const char *str)
1674 size_t len = strlen(str);
1675 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1677 return obj_elf_hash_n(str, len);
1681 obj_set_symbol_compare(struct obj_file *f,
1682 int (*cmp) (const char *, const char *),
1683 unsigned long (*hash) (const char *))
1686 f->symbol_cmp = cmp;
1688 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1691 f->symbol_hash = hash;
1693 memcpy(tmptab, f->symtab, sizeof(tmptab));
1694 memset(f->symtab, 0, sizeof(f->symtab));
1696 for (i = 0; i < HASH_BUCKETS; ++i)
1697 for (sym = tmptab[i]; sym; sym = next) {
1698 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1700 sym->next = f->symtab[h];
1706 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1708 static struct obj_symbol *
1709 obj_add_symbol(struct obj_file *f, const char *name,
1710 unsigned long symidx, int info,
1711 int secidx, ElfW(Addr) value,
1714 struct obj_symbol *sym;
1715 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1716 int n_type = ELFW(ST_TYPE) (info);
1717 int n_binding = ELFW(ST_BIND) (info);
1719 for (sym = f->symtab[hash]; sym; sym = sym->next)
1720 if (f->symbol_cmp(sym->name, name) == 0) {
1721 int o_secidx = sym->secidx;
1722 int o_info = sym->info;
1723 int o_type = ELFW(ST_TYPE) (o_info);
1724 int o_binding = ELFW(ST_BIND) (o_info);
1726 /* A redefinition! Is it legal? */
1728 if (secidx == SHN_UNDEF)
1730 else if (o_secidx == SHN_UNDEF)
1732 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1733 /* Cope with local and global symbols of the same name
1734 in the same object file, as might have been created
1735 by ld -r. The only reason locals are now seen at this
1736 level at all is so that we can do semi-sensible things
1739 struct obj_symbol *nsym, **p;
1741 nsym = arch_new_symbol();
1742 nsym->next = sym->next;
1745 /* Excise the old (local) symbol from the hash chain. */
1746 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1750 } else if (n_binding == STB_LOCAL) {
1751 /* Another symbol of the same name has already been defined.
1752 Just add this to the local table. */
1753 sym = arch_new_symbol();
1756 f->local_symtab[symidx] = sym;
1758 } else if (n_binding == STB_WEAK)
1760 else if (o_binding == STB_WEAK)
1762 /* Don't unify COMMON symbols with object types the programmer
1764 else if (secidx == SHN_COMMON
1765 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1767 else if (o_secidx == SHN_COMMON
1768 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1771 /* Don't report an error if the symbol is coming from
1772 the kernel or some external module. */
1773 if (secidx <= SHN_HIRESERVE)
1774 bb_error_msg("%s multiply defined", name);
1779 /* Completely new symbol. */
1780 sym = arch_new_symbol();
1781 sym->next = f->symtab[hash];
1782 f->symtab[hash] = sym;
1785 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1786 if (symidx >= f->local_symtab_size)
1787 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1788 name, (long) symidx, (long) f->local_symtab_size);
1790 f->local_symtab[symidx] = sym;
1797 sym->secidx = secidx;
1803 static struct obj_symbol *
1804 obj_find_symbol(struct obj_file *f, const char *name)
1806 struct obj_symbol *sym;
1807 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1809 for (sym = f->symtab[hash]; sym; sym = sym->next)
1810 if (f->symbol_cmp(sym->name, name) == 0)
1817 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1820 if (sym->secidx >= SHN_LORESERVE)
1823 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1825 /* As a special case, a NULL sym has value zero. */
1830 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1832 int i, n = f->header.e_shnum;
1834 for (i = 0; i < n; ++i)
1835 if (strcmp(f->sections[i]->name, name) == 0)
1836 return f->sections[i];
1841 static int obj_load_order_prio(struct obj_section *a)
1843 unsigned long af, ac;
1845 af = a->header.sh_flags;
1848 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1849 strcmp(a->name + 5, ".init"))
1853 if (!(af & SHF_WRITE))
1855 if (af & SHF_EXECINSTR)
1857 if (a->header.sh_type != SHT_NOBITS)
1864 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1866 struct obj_section **p;
1867 int prio = obj_load_order_prio(sec);
1868 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1869 if (obj_load_order_prio(*p) < prio)
1871 sec->load_next = *p;
1875 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1877 unsigned long align,
1880 int newidx = f->header.e_shnum++;
1881 struct obj_section *sec;
1883 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1884 f->sections[newidx] = sec = arch_new_section();
1886 memset(sec, 0, sizeof(*sec));
1887 sec->header.sh_type = SHT_PROGBITS;
1888 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1889 sec->header.sh_size = size;
1890 sec->header.sh_addralign = align;
1894 sec->contents = xmalloc(size);
1896 obj_insert_section_load_order(f, sec);
1901 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1903 unsigned long align,
1906 int newidx = f->header.e_shnum++;
1907 struct obj_section *sec;
1909 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1910 f->sections[newidx] = sec = arch_new_section();
1912 memset(sec, 0, sizeof(*sec));
1913 sec->header.sh_type = SHT_PROGBITS;
1914 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1915 sec->header.sh_size = size;
1916 sec->header.sh_addralign = align;
1920 sec->contents = xmalloc(size);
1922 sec->load_next = f->load_order;
1923 f->load_order = sec;
1924 if (f->load_order_search_start == &f->load_order)
1925 f->load_order_search_start = &sec->load_next;
1930 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1932 unsigned long oldsize = sec->header.sh_size;
1934 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1936 return sec->contents + oldsize;
1940 /* Conditionally add the symbols from the given symbol set to the
1944 add_symbols_from( struct obj_file *f,
1945 int idx, struct new_module_symbol *syms, size_t nsyms)
1947 struct new_module_symbol *s;
1950 #ifdef SYMBOL_PREFIX
1952 size_t name_alloced_size = 0;
1954 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1957 gpl = obj_gpl_license(f, NULL) == 0;
1959 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1960 /* Only add symbols that are already marked external.
1961 If we override locals we may cause problems for
1962 argument initialization. We will also create a false
1963 dependency on the module. */
1964 struct obj_symbol *sym;
1967 /* GPL licensed modules can use symbols exported with
1968 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
1969 * exported names. Non-GPL modules never see any GPLONLY_
1970 * symbols so they cannot fudge it by adding the prefix on
1973 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
1974 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1981 name = (char *)s->name;
1983 #ifdef SYMBOL_PREFIX
1984 /* Prepend SYMBOL_PREFIX to the symbol's name (the
1985 kernel exports `C names', but module object files
1986 reference `linker names'). */
1987 size_t extra = sizeof SYMBOL_PREFIX;
1988 size_t name_size = strlen (name) + extra;
1989 if (name_size > name_alloced_size) {
1990 name_alloced_size = name_size * 2;
1991 name_buf = alloca (name_alloced_size);
1993 strcpy (name_buf, SYMBOL_PREFIX);
1994 strcpy (name_buf + extra - 1, name);
1996 #endif /* SYMBOL_PREFIX */
1998 sym = obj_find_symbol(f, name);
1999 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
2000 #ifdef SYMBOL_PREFIX
2001 /* Put NAME_BUF into more permanent storage. */
2002 name = xmalloc (name_size);
2003 strcpy (name, name_buf);
2005 sym = obj_add_symbol(f, name, -1,
2006 ELFW(ST_INFO) (STB_GLOBAL,
2009 /* Did our symbol just get installed? If so, mark the
2010 module as "used". */
2011 if (sym->secidx == idx)
2019 static void add_kernel_symbols(struct obj_file *f)
2021 struct external_module *m;
2024 /* Add module symbols first. */
2026 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2028 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2029 m->nsyms)) m->used = 1, ++nused;
2031 n_ext_modules_used = nused;
2033 /* And finally the symbols from the kernel proper. */
2036 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2039 static char *get_modinfo_value(struct obj_file *f, const char *key)
2041 struct obj_section *sec;
2042 char *p, *v, *n, *ep;
2043 size_t klen = strlen(key);
2045 sec = obj_find_section(f, ".modinfo");
2049 ep = p + sec->header.sh_size;
2052 n = strchr(p, '\0');
2054 if (p + klen == v && strncmp(p, key, klen) == 0)
2057 if (p + klen == n && strcmp(p, key) == 0)
2067 /*======================================================================*/
2068 /* Functions relating to module loading after 2.1.18. */
2071 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2074 char *p, *q, *key, *sym_name;
2075 struct obj_symbol *sym;
2076 char *contents, *loc;
2080 if ((q = strchr(p, '=')) == NULL) {
2085 key = alloca(q - p + 6);
2086 memcpy(key, "parm_", 5);
2087 memcpy(key + 5, p, q - p);
2090 p = get_modinfo_value(f, key);
2093 bb_error_msg("invalid parameter %s", key);
2097 #ifdef SYMBOL_PREFIX
2098 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2099 strcpy (sym_name, SYMBOL_PREFIX);
2100 strcat (sym_name, key);
2104 sym = obj_find_symbol(f, sym_name);
2106 /* Also check that the parameter was not resolved from the kernel. */
2107 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2108 bb_error_msg("symbol for parameter %s not found", key);
2113 min = strtoul(p, &p, 10);
2115 max = strtoul(p + 1, &p, 10);
2121 contents = f->sections[sym->secidx]->contents;
2122 loc = contents + sym->value;
2126 if ((*p == 's') || (*p == 'c')) {
2129 /* Do C quoting if we begin with a ", else slurp the lot. */
2133 str = alloca(strlen(q));
2134 for (r = str, q++; *q != '"'; ++q, ++r) {
2136 bb_error_msg("improperly terminated string argument for %s",
2139 } else if (*q == '\\')
2173 if (q[1] >= '0' && q[1] <= '7') {
2174 c = (c * 8) + *++q - '0';
2175 if (q[1] >= '0' && q[1] <= '7')
2176 c = (c * 8) + *++q - '0';
2193 /* In this case, the string is not quoted. We will break
2194 it using the coma (like for ints). If the user wants to
2195 include comas in a string, he just has to quote it */
2197 /* Search the next coma */
2201 if (r != (char *) NULL) {
2202 /* Recopy the current field */
2203 str = alloca(r - q + 1);
2204 memcpy(str, q, r - q);
2206 /* I don't know if it is useful, as the previous case
2207 doesn't nul terminate the string ??? */
2210 /* Keep next fields */
2221 obj_string_patch(f, sym->secidx, loc - contents, str);
2222 loc += tgt_sizeof_char_p;
2224 /* Array of chars (in fact, matrix !) */
2225 unsigned long charssize; /* size of each member */
2227 /* Get the size of each member */
2228 /* Probably we should do that outside the loop ? */
2229 if (!isdigit(*(p + 1))) {
2230 bb_error_msg("parameter type 'c' for %s must be followed by"
2231 " the maximum size", key);
2234 charssize = strtoul(p + 1, (char **) NULL, 10);
2237 if (strlen(str) >= charssize) {
2238 bb_error_msg("string too long for %s (max %ld)", key,
2243 /* Copy to location */
2244 strcpy((char *) loc, str);
2248 long v = strtoul(q, &q, 0);
2255 loc += tgt_sizeof_short;
2259 loc += tgt_sizeof_int;
2263 loc += tgt_sizeof_long;
2267 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2282 goto retry_end_of_value;
2286 bb_error_msg("too many values for %s (max %d)", key, max);
2293 bb_error_msg("invalid argument syntax for %s", key);
2300 bb_error_msg("too few values for %s (min %d)", key, min);
2310 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2311 static int new_is_module_checksummed(struct obj_file *f)
2313 const char *p = get_modinfo_value(f, "using_checksums");
2320 /* Get the module's kernel version in the canonical integer form. */
2323 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2328 p = get_modinfo_value(f, "kernel_version");
2331 safe_strncpy(str, p, STRVERSIONLEN);
2333 a = strtoul(p, &p, 10);
2336 b = strtoul(p + 1, &p, 10);
2339 c = strtoul(p + 1, &q, 10);
2343 return a << 16 | b << 8 | c;
2346 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2349 /* Fetch the loaded modules, and all currently exported symbols. */
2351 static int new_get_kernel_symbols(void)
2353 char *module_names, *mn;
2354 struct external_module *modules, *m;
2355 struct new_module_symbol *syms, *s;
2356 size_t ret, bufsize, nmod, nsyms, i, j;
2358 /* Collect the loaded modules. */
2360 module_names = xmalloc(bufsize = 256);
2362 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2363 if (errno == ENOSPC && bufsize < ret) {
2364 module_names = xrealloc(module_names, bufsize = ret);
2365 goto retry_modules_load;
2367 bb_perror_msg("QM_MODULES");
2371 n_ext_modules = nmod = ret;
2373 /* Collect the modules' symbols. */
2376 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2377 memset(modules, 0, nmod * sizeof(*modules));
2378 for (i = 0, mn = module_names, m = modules;
2379 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2380 struct new_module_info info;
2382 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2383 if (errno == ENOENT) {
2384 /* The module was removed out from underneath us. */
2387 bb_perror_msg("query_module: QM_INFO: %s", mn);
2391 syms = xmalloc(bufsize = 1024);
2393 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2396 syms = xrealloc(syms, bufsize = ret);
2397 goto retry_mod_sym_load;
2399 /* The module was removed out from underneath us. */
2402 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2409 m->addr = info.addr;
2413 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2414 s->name += (unsigned long) syms;
2419 /* Collect the kernel's symbols. */
2421 syms = xmalloc(bufsize = 16 * 1024);
2422 retry_kern_sym_load:
2423 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2424 if (errno == ENOSPC && bufsize < ret) {
2425 syms = xrealloc(syms, bufsize = ret);
2426 goto retry_kern_sym_load;
2428 bb_perror_msg("kernel: QM_SYMBOLS");
2431 nksyms = nsyms = ret;
2434 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2435 s->name += (unsigned long) syms;
2441 /* Return the kernel symbol checksum version, or zero if not used. */
2443 static int new_is_kernel_checksummed(void)
2445 struct new_module_symbol *s;
2448 /* Using_Versions is not the first symbol, but it should be in there. */
2450 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2451 if (strcmp((char *) s->name, "Using_Versions") == 0)
2458 static int new_create_this_module(struct obj_file *f, const char *m_name)
2460 struct obj_section *sec;
2462 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2463 sizeof(struct new_module));
2464 memset(sec->contents, 0, sizeof(struct new_module));
2466 obj_add_symbol(f, SPFX "__this_module", -1,
2467 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2468 sizeof(struct new_module));
2470 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2476 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2477 /* add an entry to the __ksymtab section, creating it if necessary */
2478 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2480 struct obj_section *sec;
2483 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2484 * If __ksymtab is defined but not marked alloc, x out the first character
2485 * (no obj_delete routine) and create a new __ksymtab with the correct
2488 sec = obj_find_section(f, "__ksymtab");
2489 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2490 *((char *)(sec->name)) = 'x'; /* override const */
2494 sec = obj_create_alloced_section(f, "__ksymtab",
2495 tgt_sizeof_void_p, 0);
2498 sec->header.sh_flags |= SHF_ALLOC;
2499 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2501 ofs = sec->header.sh_size;
2502 obj_symbol_patch(f, sec->idx, ofs, sym);
2503 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2504 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2506 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2508 static int new_create_module_ksymtab(struct obj_file *f)
2510 struct obj_section *sec;
2513 /* We must always add the module references. */
2515 if (n_ext_modules_used) {
2516 struct new_module_ref *dep;
2517 struct obj_symbol *tm;
2519 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2520 (sizeof(struct new_module_ref)
2521 * n_ext_modules_used));
2525 tm = obj_find_symbol(f, SPFX "__this_module");
2526 dep = (struct new_module_ref *) sec->contents;
2527 for (i = 0; i < n_ext_modules; ++i)
2528 if (ext_modules[i].used) {
2529 dep->dep = ext_modules[i].addr;
2530 obj_symbol_patch(f, sec->idx,
2531 (char *) &dep->ref - sec->contents, tm);
2537 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2542 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2545 /* We don't want to export symbols residing in sections that
2546 aren't loaded. There are a number of these created so that
2547 we make sure certain module options don't appear twice. */
2549 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2551 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2553 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2554 struct obj_symbol *sym;
2555 for (sym = f->symtab[i]; sym; sym = sym->next)
2556 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2557 && sym->secidx <= SHN_HIRESERVE
2558 && (sym->secidx >= SHN_LORESERVE
2559 || loaded[sym->secidx])) {
2560 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2562 obj_symbol_patch(f, sec->idx, ofs, sym);
2563 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2570 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2578 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2580 struct new_module *module;
2581 struct obj_section *sec;
2586 sec = obj_find_section(f, ".this");
2587 if (!sec || !sec->contents) {
2588 bb_perror_msg_and_die("corrupt module %s?",m_name);
2590 module = (struct new_module *) sec->contents;
2591 m_addr = sec->header.sh_addr;
2593 module->size_of_struct = sizeof(*module);
2594 module->size = m_size;
2595 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2597 sec = obj_find_section(f, "__ksymtab");
2598 if (sec && sec->header.sh_size) {
2599 module->syms = sec->header.sh_addr;
2600 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2603 if (n_ext_modules_used) {
2604 sec = obj_find_section(f, ".kmodtab");
2605 module->deps = sec->header.sh_addr;
2606 module->ndeps = n_ext_modules_used;
2610 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2612 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2614 sec = obj_find_section(f, "__ex_table");
2616 module->ex_table_start = sec->header.sh_addr;
2617 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2620 sec = obj_find_section(f, ".text.init");
2622 module->runsize = sec->header.sh_addr - m_addr;
2624 sec = obj_find_section(f, ".data.init");
2626 if (!module->runsize ||
2627 module->runsize > sec->header.sh_addr - m_addr)
2628 module->runsize = sec->header.sh_addr - m_addr;
2630 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2631 if (sec && sec->header.sh_size) {
2632 module->archdata_start = (void*)sec->header.sh_addr;
2633 module->archdata_end = module->archdata_start + sec->header.sh_size;
2635 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2636 if (sec && sec->header.sh_size) {
2637 module->kallsyms_start = (void*)sec->header.sh_addr;
2638 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2641 /* Whew! All of the initialization is complete. Collect the final
2642 module image and give it to the kernel. */
2644 image = xmalloc(m_size);
2645 obj_create_image(f, image);
2647 ret = init_module(m_name, (struct new_module *) image);
2649 bb_perror_msg("init_module: %s", m_name);
2657 /*======================================================================*/
2660 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2663 struct obj_string_patch *p;
2664 struct obj_section *strsec;
2665 size_t len = strlen(string) + 1;
2668 p = xmalloc(sizeof(*p));
2669 p->next = f->string_patches;
2670 p->reloc_secidx = secidx;
2671 p->reloc_offset = offset;
2672 f->string_patches = p;
2674 strsec = obj_find_section(f, ".kstrtab");
2675 if (strsec == NULL) {
2676 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2677 p->string_offset = 0;
2678 loc = strsec->contents;
2680 p->string_offset = strsec->header.sh_size;
2681 loc = obj_extend_section(strsec, len);
2683 memcpy(loc, string, len);
2689 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2690 struct obj_symbol *sym)
2692 struct obj_symbol_patch *p;
2694 p = xmalloc(sizeof(*p));
2695 p->next = f->symbol_patches;
2696 p->reloc_secidx = secidx;
2697 p->reloc_offset = offset;
2699 f->symbol_patches = p;
2704 static int obj_check_undefineds(struct obj_file *f)
2709 for (i = 0; i < HASH_BUCKETS; ++i) {
2710 struct obj_symbol *sym;
2711 for (sym = f->symtab[i]; sym; sym = sym->next)
2712 if (sym->secidx == SHN_UNDEF) {
2713 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2714 sym->secidx = SHN_ABS;
2718 bb_error_msg("unresolved symbol %s", sym->name);
2728 static void obj_allocate_commons(struct obj_file *f)
2730 struct common_entry {
2731 struct common_entry *next;
2732 struct obj_symbol *sym;
2733 } *common_head = NULL;
2737 for (i = 0; i < HASH_BUCKETS; ++i) {
2738 struct obj_symbol *sym;
2739 for (sym = f->symtab[i]; sym; sym = sym->next)
2740 if (sym->secidx == SHN_COMMON) {
2741 /* Collect all COMMON symbols and sort them by size so as to
2742 minimize space wasted by alignment requirements. */
2744 struct common_entry **p, *n;
2745 for (p = &common_head; *p; p = &(*p)->next)
2746 if (sym->size <= (*p)->sym->size)
2749 n = alloca(sizeof(*n));
2757 for (i = 1; i < f->local_symtab_size; ++i) {
2758 struct obj_symbol *sym = f->local_symtab[i];
2759 if (sym && sym->secidx == SHN_COMMON) {
2760 struct common_entry **p, *n;
2761 for (p = &common_head; *p; p = &(*p)->next)
2762 if (sym == (*p)->sym)
2764 else if (sym->size < (*p)->sym->size) {
2765 n = alloca(sizeof(*n));
2775 /* Find the bss section. */
2776 for (i = 0; i < f->header.e_shnum; ++i)
2777 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2780 /* If for some reason there hadn't been one, create one. */
2781 if (i == f->header.e_shnum) {
2782 struct obj_section *sec;
2784 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2785 f->sections[i] = sec = arch_new_section();
2786 f->header.e_shnum = i + 1;
2788 memset(sec, 0, sizeof(*sec));
2789 sec->header.sh_type = SHT_PROGBITS;
2790 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2795 /* Allocate the COMMONS. */
2797 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2798 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2799 struct common_entry *c;
2801 for (c = common_head; c; c = c->next) {
2802 ElfW(Addr) align = c->sym->value;
2804 if (align > max_align)
2806 if (bss_size & (align - 1))
2807 bss_size = (bss_size | (align - 1)) + 1;
2810 c->sym->value = bss_size;
2812 bss_size += c->sym->size;
2815 f->sections[i]->header.sh_size = bss_size;
2816 f->sections[i]->header.sh_addralign = max_align;
2820 /* For the sake of patch relocation and parameter initialization,
2821 allocate zeroed data for NOBITS sections now. Note that after
2822 this we cannot assume NOBITS are really empty. */
2823 for (i = 0; i < f->header.e_shnum; ++i) {
2824 struct obj_section *s = f->sections[i];
2825 if (s->header.sh_type == SHT_NOBITS) {
2826 if (s->header.sh_size != 0)
2827 s->contents = memset(xmalloc(s->header.sh_size),
2828 0, s->header.sh_size);
2832 s->header.sh_type = SHT_PROGBITS;
2837 static unsigned long obj_load_size(struct obj_file *f)
2839 unsigned long dot = 0;
2840 struct obj_section *sec;
2842 /* Finalize the positions of the sections relative to one another. */
2844 for (sec = f->load_order; sec; sec = sec->load_next) {
2847 align = sec->header.sh_addralign;
2848 if (align && (dot & (align - 1)))
2849 dot = (dot | (align - 1)) + 1;
2851 sec->header.sh_addr = dot;
2852 dot += sec->header.sh_size;
2858 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2860 int i, n = f->header.e_shnum;
2863 /* Finalize the addresses of the sections. */
2866 for (i = 0; i < n; ++i)
2867 f->sections[i]->header.sh_addr += base;
2869 /* And iterate over all of the relocations. */
2871 for (i = 0; i < n; ++i) {
2872 struct obj_section *relsec, *symsec, *targsec, *strsec;
2873 ElfW(RelM) * rel, *relend;
2877 relsec = f->sections[i];
2878 if (relsec->header.sh_type != SHT_RELM)
2881 symsec = f->sections[relsec->header.sh_link];
2882 targsec = f->sections[relsec->header.sh_info];
2883 strsec = f->sections[symsec->header.sh_link];
2885 rel = (ElfW(RelM) *) relsec->contents;
2886 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2887 symtab = (ElfW(Sym) *) symsec->contents;
2888 strtab = (const char *) strsec->contents;
2890 for (; rel < relend; ++rel) {
2891 ElfW(Addr) value = 0;
2892 struct obj_symbol *intsym = NULL;
2893 unsigned long symndx;
2894 ElfW(Sym) * extsym = 0;
2897 /* Attempt to find a value to use for this relocation. */
2899 symndx = ELFW(R_SYM) (rel->r_info);
2901 /* Note we've already checked for undefined symbols. */
2903 extsym = &symtab[symndx];
2904 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2905 /* Local symbols we look up in the local table to be sure
2906 we get the one that is really intended. */
2907 intsym = f->local_symtab[symndx];
2909 /* Others we look up in the hash table. */
2911 if (extsym->st_name)
2912 name = strtab + extsym->st_name;
2914 name = f->sections[extsym->st_shndx]->name;
2915 intsym = obj_find_symbol(f, name);
2918 value = obj_symbol_final_value(f, intsym);
2919 intsym->referenced = 1;
2921 #if SHT_RELM == SHT_RELA
2922 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2923 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2924 if (!extsym || !extsym->st_name ||
2925 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2927 value += rel->r_addend;
2931 switch (arch_apply_relocation
2932 (f, targsec, symsec, intsym, rel, value)) {
2936 case obj_reloc_overflow:
2937 errmsg = "Relocation overflow";
2939 case obj_reloc_dangerous:
2940 errmsg = "Dangerous relocation";
2942 case obj_reloc_unhandled:
2943 errmsg = "Unhandled relocation";
2946 bb_error_msg("%s of type %ld for %s", errmsg,
2947 (long) ELFW(R_TYPE) (rel->r_info),
2948 strtab + extsym->st_name);
2950 bb_error_msg("%s of type %ld", errmsg,
2951 (long) ELFW(R_TYPE) (rel->r_info));
2959 /* Finally, take care of the patches. */
2961 if (f->string_patches) {
2962 struct obj_string_patch *p;
2963 struct obj_section *strsec;
2964 ElfW(Addr) strsec_base;
2965 strsec = obj_find_section(f, ".kstrtab");
2966 strsec_base = strsec->header.sh_addr;
2968 for (p = f->string_patches; p; p = p->next) {
2969 struct obj_section *targsec = f->sections[p->reloc_secidx];
2970 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2971 = strsec_base + p->string_offset;
2975 if (f->symbol_patches) {
2976 struct obj_symbol_patch *p;
2978 for (p = f->symbol_patches; p; p = p->next) {
2979 struct obj_section *targsec = f->sections[p->reloc_secidx];
2980 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2981 = obj_symbol_final_value(f, p->sym);
2988 static int obj_create_image(struct obj_file *f, char *image)
2990 struct obj_section *sec;
2991 ElfW(Addr) base = f->baseaddr;
2993 for (sec = f->load_order; sec; sec = sec->load_next) {
2996 if (sec->contents == 0 || sec->header.sh_size == 0)
2999 secimg = image + (sec->header.sh_addr - base);
3001 /* Note that we allocated data for NOBITS sections earlier. */
3002 memcpy(secimg, sec->contents, sec->header.sh_size);
3008 /*======================================================================*/
3010 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3013 ElfW(Shdr) * section_headers;
3017 /* Read the file header. */
3019 f = arch_new_file();
3020 memset(f, 0, sizeof(*f));
3021 f->symbol_cmp = strcmp;
3022 f->symbol_hash = obj_elf_hash;
3023 f->load_order_search_start = &f->load_order;
3025 fseek(fp, 0, SEEK_SET);
3026 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3027 bb_perror_msg("error reading ELF header");
3031 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3032 || f->header.e_ident[EI_MAG1] != ELFMAG1
3033 || f->header.e_ident[EI_MAG2] != ELFMAG2
3034 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3035 bb_error_msg("not an ELF file");
3038 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3039 || f->header.e_ident[EI_DATA] != ELFDATAM
3040 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3041 || !MATCH_MACHINE(f->header.e_machine)) {
3042 bb_error_msg("ELF file not for this architecture");
3045 if (f->header.e_type != ET_REL) {
3046 bb_error_msg("ELF file not a relocatable object");
3050 /* Read the section headers. */
3052 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3053 bb_error_msg("section header size mismatch: %lu != %lu",
3054 (unsigned long) f->header.e_shentsize,
3055 (unsigned long) sizeof(ElfW(Shdr)));
3059 shnum = f->header.e_shnum;
3060 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3061 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3063 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3064 fseek(fp, f->header.e_shoff, SEEK_SET);
3065 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3066 bb_perror_msg("error reading ELF section headers");
3070 /* Read the section data. */
3072 for (i = 0; i < shnum; ++i) {
3073 struct obj_section *sec;
3075 f->sections[i] = sec = arch_new_section();
3076 memset(sec, 0, sizeof(*sec));
3078 sec->header = section_headers[i];
3081 if(sec->header.sh_size) switch (sec->header.sh_type) {
3090 if (!loadprogbits) {
3091 sec->contents = NULL;
3098 if (sec->header.sh_size > 0) {
3099 sec->contents = xmalloc(sec->header.sh_size);
3100 fseek(fp, sec->header.sh_offset, SEEK_SET);
3101 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3102 bb_perror_msg("error reading ELF section data");
3106 sec->contents = NULL;
3110 #if SHT_RELM == SHT_REL
3112 bb_error_msg("RELA relocations not supported on this architecture");
3116 bb_error_msg("REL relocations not supported on this architecture");
3121 if (sec->header.sh_type >= SHT_LOPROC) {
3122 /* Assume processor specific section types are debug
3123 info and can safely be ignored. If this is ever not
3124 the case (Hello MIPS?), don't put ifdefs here but
3125 create an arch_load_proc_section(). */
3129 bb_error_msg("can't handle sections of type %ld",
3130 (long) sec->header.sh_type);
3135 /* Do what sort of interpretation as needed by each section. */
3137 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3139 for (i = 0; i < shnum; ++i) {
3140 struct obj_section *sec = f->sections[i];
3141 sec->name = shstrtab + sec->header.sh_name;
3144 for (i = 0; i < shnum; ++i) {
3145 struct obj_section *sec = f->sections[i];
3147 /* .modinfo should be contents only but gcc has no attribute for that.
3148 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3150 if (strcmp(sec->name, ".modinfo") == 0)
3151 sec->header.sh_flags &= ~SHF_ALLOC;
3153 if (sec->header.sh_flags & SHF_ALLOC)
3154 obj_insert_section_load_order(f, sec);
3156 switch (sec->header.sh_type) {
3159 unsigned long nsym, j;
3163 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3164 bb_error_msg("symbol size mismatch: %lu != %lu",
3165 (unsigned long) sec->header.sh_entsize,
3166 (unsigned long) sizeof(ElfW(Sym)));
3170 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3171 strtab = f->sections[sec->header.sh_link]->contents;
3172 sym = (ElfW(Sym) *) sec->contents;
3174 /* Allocate space for a table of local symbols. */
3175 j = f->local_symtab_size = sec->header.sh_info;
3176 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3178 /* Insert all symbols into the hash table. */
3179 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3180 ElfW(Addr) val = sym->st_value;
3183 name = strtab + sym->st_name;
3184 else if (sym->st_shndx < shnum)
3185 name = f->sections[sym->st_shndx]->name;
3189 #if defined(__SH5__)
3191 * For sh64 it is possible that the target of a branch
3192 * requires a mode switch (32 to 16 and back again).
3194 * This is implied by the lsb being set in the target
3195 * address for SHmedia mode and clear for SHcompact.
3197 val |= sym->st_other & 4;
3200 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3207 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3208 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3209 (unsigned long) sec->header.sh_entsize,
3210 (unsigned long) sizeof(ElfW(RelM)));
3214 /* XXX Relocation code from modutils-2.3.19 is not here.
3215 * Why? That's about 20 lines of code from obj/obj_load.c,
3216 * which gets done in a second pass through the sections.
3217 * This BusyBox insmod does similar work in obj_relocate(). */
3224 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3226 * load the unloaded sections directly into the memory allocated by
3227 * kernel for the module
3230 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3232 ElfW(Addr) base = f->baseaddr;
3233 struct obj_section* sec;
3235 for (sec = f->load_order; sec; sec = sec->load_next) {
3237 /* section already loaded? */
3238 if (sec->contents != NULL)
3241 if (sec->header.sh_size == 0)
3244 sec->contents = imagebase + (sec->header.sh_addr - base);
3245 fseek(fp, sec->header.sh_offset, SEEK_SET);
3246 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3247 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3256 static void hide_special_symbols(struct obj_file *f)
3258 static const char *const specials[] = {
3259 SPFX "cleanup_module",
3261 SPFX "kernel_version",
3265 struct obj_symbol *sym;
3266 const char *const *p;
3268 for (p = specials; *p; ++p)
3269 if ((sym = obj_find_symbol(f, *p)) != NULL)
3271 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3275 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3276 static int obj_gpl_license(struct obj_file *f, const char **license)
3278 struct obj_section *sec;
3279 /* This list must match *exactly* the list of allowable licenses in
3280 * linux/include/linux/module.h. Checking for leading "GPL" will not
3281 * work, somebody will use "GPL sucks, this is proprietary".
3283 static const char *gpl_licenses[] = {
3286 "GPL and additional rights",
3291 if ((sec = obj_find_section(f, ".modinfo"))) {
3292 const char *value, *ptr, *endptr;
3293 ptr = sec->contents;
3294 endptr = ptr + sec->header.sh_size;
3295 while (ptr < endptr) {
3296 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3300 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3301 if (strcmp(value+1, gpl_licenses[i]) == 0)
3306 if (strchr(ptr, '\0'))
3307 ptr = strchr(ptr, '\0') + 1;
3315 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3316 #define TAINT_PROPRIETORY_MODULE (1<<0)
3317 #define TAINT_FORCED_MODULE (1<<1)
3318 #define TAINT_UNSAFE_SMP (1<<2)
3319 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3321 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3322 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3326 static int first = 1;
3327 if (fd < 0 && !kernel_has_tainted)
3328 return; /* New modutils on old kernel */
3329 printf("Warning: loading %s will taint the kernel: %s%s\n",
3330 m_name, text1, text2);
3332 printf(" See %s for information about tainted modules\n", TAINT_URL);
3336 read(fd, buf, sizeof(buf)-1);
3337 buf[sizeof(buf)-1] = '\0';
3338 oldval = strtoul(buf, NULL, 10);
3339 sprintf(buf, "%d\n", oldval | taint);
3340 write(fd, buf, strlen(buf));
3344 /* Check if loading this module will taint the kernel. */
3345 static void check_tainted_module(struct obj_file *f, char *m_name)
3347 static const char tainted_file[] = TAINT_FILENAME;
3348 int fd, kernel_has_tainted;
3351 kernel_has_tainted = 1;
3352 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3353 if (errno == ENOENT)
3354 kernel_has_tainted = 0;
3355 else if (errno == EACCES)
3356 kernel_has_tainted = 1;
3358 perror(tainted_file);
3359 kernel_has_tainted = 0;
3363 switch (obj_gpl_license(f, &ptr)) {
3367 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3370 /* The module has a non-GPL license so we pretend that the
3371 * kernel always has a taint flag to get a warning even on
3372 * kernels without the proc flag.
3374 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3377 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3381 if (flag_force_load)
3382 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3387 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3388 #define check_tainted_module(x, y) do { } while(0);
3389 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3391 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3392 /* add module source, timestamp, kernel version and a symbol for the
3393 * start of some sections. this info is used by ksymoops to do better
3397 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3399 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3400 return new_get_module_version(f, str);
3401 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3402 strncpy(str, "???", sizeof(str));
3404 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3407 /* add module source, timestamp, kernel version and a symbol for the
3408 * start of some sections. this info is used by ksymoops to do better
3412 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3415 static const char symprefix[] = "__insmod_";
3416 struct obj_section *sec;
3417 struct obj_symbol *sym;
3418 char *name, *absolute_filename;
3419 char str[STRVERSIONLEN], real[PATH_MAX];
3420 int i, l, lm_name, lfilename, use_ksymtab, version;
3421 struct stat statbuf;
3423 static const char *section_names[] = {
3431 if (realpath(filename, real)) {
3432 absolute_filename = bb_xstrdup(real);
3435 int save_errno = errno;
3436 bb_error_msg("cannot get realpath for %s", filename);
3439 absolute_filename = bb_xstrdup(filename);
3442 lm_name = strlen(m_name);
3443 lfilename = strlen(absolute_filename);
3445 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3446 * are not to be exported. otherwise leave ksymtab alone for now, the
3447 * "export all symbols" compatibility code will export these symbols later.
3449 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3451 if ((sec = obj_find_section(f, ".this"))) {
3452 /* tag the module header with the object name, last modified
3453 * timestamp and module version. worst case for module version
3454 * is 0xffffff, decimal 16777215. putting all three fields in
3455 * one symbol is less readable but saves kernel space.
3457 l = sizeof(symprefix)+ /* "__insmod_" */
3458 lm_name+ /* module name */
3460 lfilename+ /* object filename */
3462 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3464 8+ /* version in dec */
3467 if (stat(absolute_filename, &statbuf) != 0)
3468 statbuf.st_mtime = 0;
3469 version = get_module_version(f, str); /* -1 if not found */
3470 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3471 symprefix, m_name, absolute_filename,
3472 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3474 sym = obj_add_symbol(f, name, -1,
3475 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3476 sec->idx, sec->header.sh_addr, 0);
3478 new_add_ksymtab(f, sym);
3480 free(absolute_filename);
3481 #ifdef _NOT_SUPPORTED_
3482 /* record where the persistent data is going, same address as previous symbol */
3485 l = sizeof(symprefix)+ /* "__insmod_" */
3486 lm_name+ /* module name */
3488 strlen(f->persist)+ /* data store */
3491 snprintf(name, l, "%s%s_P%s",
3492 symprefix, m_name, f->persist);
3493 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3494 sec->idx, sec->header.sh_addr, 0);
3496 new_add_ksymtab(f, sym);
3498 #endif /* _NOT_SUPPORTED_ */
3499 /* tag the desired sections if size is non-zero */
3501 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3502 if ((sec = obj_find_section(f, section_names[i])) &&
3503 sec->header.sh_size) {
3504 l = sizeof(symprefix)+ /* "__insmod_" */
3505 lm_name+ /* module name */
3507 strlen(sec->name)+ /* section name */
3509 8+ /* length in dec */
3512 snprintf(name, l, "%s%s_S%s_L%ld",
3513 symprefix, m_name, sec->name,
3514 (long)sec->header.sh_size);
3515 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3516 sec->idx, sec->header.sh_addr, 0);
3518 new_add_ksymtab(f, sym);
3522 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3524 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3525 static void print_load_map(struct obj_file *f)
3527 struct obj_symbol *sym;
3528 struct obj_symbol **all, **p;
3529 struct obj_section *sec;
3530 int i, nsyms, *loaded;
3532 /* Report on the section layout. */
3534 printf("Sections: Size %-*s Align\n",
3535 (int) (2 * sizeof(void *)), "Address");
3537 for (sec = f->load_order; sec; sec = sec->load_next) {
3541 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3546 printf("%-15s %08lx %0*lx 2**%d\n",
3548 (long)sec->header.sh_size,
3549 (int) (2 * sizeof(void *)),
3550 (long)sec->header.sh_addr,
3553 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3554 /* Quick reference which section indicies are loaded. */
3556 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3558 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3560 /* Collect the symbols we'll be listing. */
3562 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3563 for (sym = f->symtab[i]; sym; sym = sym->next)
3564 if (sym->secidx <= SHN_HIRESERVE
3565 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3568 all = alloca(nsyms * sizeof(struct obj_symbol *));
3570 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3571 for (sym = f->symtab[i]; sym; sym = sym->next)
3572 if (sym->secidx <= SHN_HIRESERVE
3573 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3576 /* And list them. */
3577 printf("\nSymbols:\n");
3578 for (p = all; p < all + nsyms; ++p) {
3580 unsigned long value;
3583 if (sym->secidx == SHN_ABS) {
3586 } else if (sym->secidx == SHN_UNDEF) {
3590 sec = f->sections[sym->secidx];
3592 if (sec->header.sh_type == SHT_NOBITS)
3594 else if (sec->header.sh_flags & SHF_ALLOC) {
3595 if (sec->header.sh_flags & SHF_EXECINSTR)
3597 else if (sec->header.sh_flags & SHF_WRITE)
3602 value = sym->value + sec->header.sh_addr;
3605 if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
3606 type = tolower(type);
3608 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3616 extern int insmod_main( int argc, char **argv)
3622 unsigned long m_size;
3627 int exit_status = EXIT_FAILURE;
3629 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3630 struct utsname uts_info;
3631 char m_strversion[STRVERSIONLEN];
3632 int m_version, m_crcs;
3634 #ifdef CONFIG_FEATURE_CLEAN_UP
3639 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3640 int flag_print_load_map = 0;
3643 struct utsname myuname;
3645 /* Parse any options */
3646 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3647 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
3649 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
3653 case 'f': /* force loading */
3654 flag_force_load = 1;
3656 case 'k': /* module loaded by kerneld, auto-cleanable */
3659 case 's': /* log to syslog */
3660 /* log to syslog -- not supported */
3661 /* but kernel needs this for request_module(), */
3662 /* as this calls: modprobe -k -s -- <module> */
3663 /* so silently ignore this flag */
3665 case 'v': /* verbose output */
3668 case 'q': /* silent */
3671 case 'x': /* do not export externs */
3674 case 'o': /* name the output module */
3676 m_name = bb_xstrdup(optarg);
3678 case 'L': /* Stub warning */
3679 /* This is needed for compatibility with modprobe.
3680 * In theory, this does locking, but we don't do
3681 * that. So be careful and plan your life around not
3682 * loading the same module 50 times concurrently. */
3684 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3685 case 'm': /* print module load map */
3686 flag_print_load_map = 1;
3694 if (argv[optind] == NULL) {
3698 /* Grab the module name */
3699 tmp1 = bb_xstrdup(argv[optind]);
3700 tmp = basename(tmp1);
3703 if (uname(&myuname) == 0) {
3704 if (myuname.release[0] == '2') {
3705 k_version = myuname.release[2] - '0';
3709 #if defined(CONFIG_FEATURE_2_6_MODULES)
3710 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
3711 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
3717 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3723 #if defined(CONFIG_FEATURE_2_6_MODULES)
3725 bb_xasprintf(&m_fullName, "%s.ko", tmp);
3728 bb_xasprintf(&m_fullName, "%s.o", tmp);
3734 tmp1 = 0; /* flag for free(m_name) before exit() */
3737 /* Get a filedesc for the module. Check we we have a complete path */
3738 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3739 (fp = fopen(argv[optind], "r")) == NULL) {
3740 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3741 * but do not error out yet if we fail to find it... */
3742 if (k_version) { /* uname succeedd */
3745 char real_module_dir[FILENAME_MAX];
3747 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
3748 /* Jump through hoops in case /lib/modules/`uname -r`
3749 * is a symlink. We do not want recursive_action to
3750 * follow symlinks, but we do want to follow the
3751 * /lib/modules/`uname -r` dir, So resolve it ourselves
3752 * if it is a link... */
3753 if (realpath (tmdn, real_module_dir) == NULL)
3756 module_dir = real_module_dir;
3757 recursive_action(module_dir, TRUE, FALSE, FALSE,
3758 check_module_name_match, 0, m_fullName);
3762 /* Check if we have found anything yet */
3763 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
3765 char module_dir[FILENAME_MAX];
3769 if (realpath (_PATH_MODULES, module_dir) == NULL)
3770 strcpy(module_dir, _PATH_MODULES);
3771 /* No module found under /lib/modules/`uname -r`, this
3772 * time cast the net a bit wider. Search /lib/modules/ */
3773 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3774 check_module_name_match, 0, m_fullName))
3777 || ((fp = fopen(m_filename, "r")) == NULL))
3779 bb_error_msg("%s: no module by that name found", m_fullName);
3783 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
3786 m_filename = bb_xstrdup(argv[optind]);
3789 printf("Using %s\n", m_filename);
3791 #ifdef CONFIG_FEATURE_2_6_MODULES
3795 argv[optind + 1] = m_filename;
3796 return insmod_ng_main(argc - optind, argv + optind);
3800 if ((f = obj_load(fp, LOADBITS)) == NULL)
3801 bb_perror_msg_and_die("Could not load the module");
3803 if (get_modinfo_value(f, "kernel_version") == NULL)
3808 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3809 /* Version correspondence? */
3811 if (uname(&uts_info) < 0)
3812 uts_info.release[0] = '\0';
3813 if (m_has_modinfo) {
3814 m_version = new_get_module_version(f, m_strversion);
3815 if (m_version == -1) {
3816 bb_error_msg("couldn't find the kernel version the module was "
3822 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3823 if (flag_force_load) {
3824 bb_error_msg("Warning: kernel-module version mismatch\n"
3825 "\t%s was compiled for kernel version %s\n"
3826 "\twhile this kernel is version %s",
3827 m_filename, m_strversion, uts_info.release);
3829 bb_error_msg("kernel-module version mismatch\n"
3830 "\t%s was compiled for kernel version %s\n"
3831 "\twhile this kernel is version %s.",
3832 m_filename, m_strversion, uts_info.release);
3838 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3840 if (!query_module(NULL, 0, NULL, 0, NULL)) {
3841 if (!new_get_kernel_symbols())
3843 k_crcs = new_is_kernel_checksummed();
3845 bb_error_msg("Not configured to support old kernels");
3849 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3852 m_crcs = new_is_module_checksummed(f);
3854 if (m_crcs != k_crcs)
3855 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3856 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3858 /* Let the module know about the kernel symbols. */
3859 add_kernel_symbols(f);
3861 /* Allocate common symbols, symbol tables, and string tables. */
3863 if (!new_create_this_module(f, m_name))
3868 if (!obj_check_undefineds(f)) {
3871 obj_allocate_commons(f);
3872 check_tainted_module(f, m_name);
3874 /* done with the module name, on to the optional var=value arguments */
3877 if (optind < argc) {
3878 if (!new_process_module_arguments(f, argc - optind, argv + optind))
3885 hide_special_symbols(f);
3887 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3888 add_ksymoops_symbols(f, m_filename, m_name);
3889 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3891 new_create_module_ksymtab(f);
3893 /* Find current size of the module */
3894 m_size = obj_load_size(f);
3897 m_addr = create_module(m_name, m_size);
3898 if (m_addr == -1) switch (errno) {
3900 bb_error_msg("A module named %s already exists", m_name);
3903 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3907 bb_perror_msg("create_module: %s", m_name);
3913 * the PROGBITS section was not loaded by the obj_load
3914 * now we can load them directly into the kernel memory
3916 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3917 delete_module(m_name);
3922 if (!obj_relocate(f, m_addr)) {
3923 delete_module(m_name);
3927 if (!new_init_module(m_name, f, m_size))
3929 delete_module(m_name);
3933 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3934 if(flag_print_load_map)
3938 exit_status = EXIT_SUCCESS;
3941 #ifdef CONFIG_FEATURE_CLEAN_UP
3951 return(exit_status);
3958 #ifdef CONFIG_FEATURE_2_6_MODULES
3960 #include <sys/mman.h>
3961 #include <asm/unistd.h>
3962 #include <sys/syscall.h>
3964 /* We use error numbers in a loose translation... */
3965 static const char *moderror(int err)
3969 return "Invalid module format";
3971 return "Unknown symbol in module";
3973 return "Module has wrong symbol version";
3975 return "Invalid parameters";
3977 return strerror(err);
3981 extern int insmod_ng_main( int argc, char **argv)
3989 char *filename, *options = bb_xstrdup("");
3997 /* Rest is options */
3998 for (i = 2; i < argc; i++) {
3999 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4000 /* Spaces handled by "" pairs, but no way of escaping quotes */
4001 if (strchr(argv[i], ' ')) {
4002 strcat(options, "\"");
4003 strcat(options, argv[i]);
4004 strcat(options, "\"");
4006 strcat(options, argv[i]);
4008 strcat(options, " ");
4011 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4012 bb_perror_msg_and_die("cannot open module `%s'", filename);
4017 map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
4018 if (map == MAP_FAILED) {
4019 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4022 ret = syscall(__NR_init_module, map, len, options);
4024 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4025 filename, moderror(errno), ret);