1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * This version of insmod supports ARM, CRIS, H8/300, x86, ia64, x86_64,
6 * m68k, MIPS, PowerPC, S390, SH3/4/5, Sparc, v850e, and x86_64.
8 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
9 * and Ron Alder <alder@lineo.com>
11 * Rodney Radford <rradford@mindspring.com> 17-Aug-2004.
12 * Added x86_64 support.
14 * Miles Bader <miles@gnu.org> added NEC V850E support.
16 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
17 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
19 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
20 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
21 * very minor changes required to also work with StrongArm and presumably
22 * all ARM based systems.
24 * Yoshinori Sato <ysato@users.sourceforge.jp> 19-May-2004.
25 * added Renesas H8/300 support.
27 * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
28 * Integrated support for sh64 (SH-5), from preliminary modutils
29 * patches from Benedict Gaster <benedict.gaster@superh.com>.
30 * Currently limited to support for 32bit ABI.
32 * Magnus Damm <damm@opensource.se> 22-May-2002.
33 * The plt and got code are now using the same structs.
34 * Added generic linked list code to fully support PowerPC.
35 * Replaced the mess in arch_apply_relocation() with architecture blocks.
36 * The arch_create_got() function got cleaned up with architecture blocks.
37 * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
39 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
40 * PowerPC specific code stolen from modutils-2.3.16,
41 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
42 * I've only tested the code on mpc8xx platforms in big-endian mode.
43 * Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
45 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
46 * based on modutils-2.4.2
47 * MIPS specific support for Elf loading and relocation.
48 * Copyright 1996, 1997 Linux International.
49 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
51 * Based almost entirely on the Linux modutils-2.3.11 implementation.
52 * Copyright 1996, 1997 Linux International.
53 * New implementation contributed by Richard Henderson <rth@tamu.edu>
54 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
55 * Restructured (and partly rewritten) by:
56 * Björn Ekwall <bj0rn@blox.se> February 1999
58 * 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
114 #define MATCH_MACHINE(x) (x == EM_ARM)
115 #define SHT_RELM SHT_REL
116 #define Elf32_RelM Elf32_Rel
117 #define ELFCLASSM ELFCLASS32
118 #define CONFIG_USE_PLT_ENTRIES
119 #define CONFIG_PLT_ENTRY_SIZE 8
120 #define CONFIG_USE_GOT_ENTRIES
121 #define CONFIG_GOT_ENTRY_SIZE 8
122 #define CONFIG_USE_SINGLE
126 #if defined(__cris__)
127 #define MATCH_MACHINE(x) (x == EM_CRIS)
128 #define SHT_RELM SHT_RELA
129 #define Elf32_RelM Elf32_Rela
130 #define ELFCLASSM ELFCLASS32
133 #define R_CRIS_NONE 0
139 #if defined(__H8300H__) || defined(__H8300S__)
140 #define MATCH_MACHINE(x) (x == EM_H8_300)
141 #define SHT_RELM SHT_RELA
142 #define Elf32_RelM Elf32_Rela
143 #define ELFCLASSM ELFCLASS32
144 #define CONFIG_USE_SINGLE
145 #define SYMBOL_PREFIX "_"
149 #if defined(__i386__)
151 #define MATCH_MACHINE(x) (x == EM_386)
153 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
155 #define SHT_RELM SHT_REL
156 #define Elf32_RelM Elf32_Rel
157 #define ELFCLASSM ELFCLASS32
158 #define CONFIG_USE_GOT_ENTRIES
159 #define CONFIG_GOT_ENTRY_SIZE 4
160 #define CONFIG_USE_SINGLE
163 /* IA64, aka Itanium */
164 #if defined(__ia64__)
165 #define MATCH_MACHINE(x) (x == EM_IA_64)
166 #define SHT_RELM SHT_RELA
167 #define Elf64_RelM Elf64_Rela
168 #define ELFCLASSM ELFCLASS64
172 #if defined(__mc68000__)
173 #define MATCH_MACHINE(x) (x == EM_68K)
174 #define SHT_RELM SHT_RELA
175 #define Elf32_RelM Elf32_Rela
176 #define ELFCLASSM ELFCLASS32
177 #define CONFIG_USE_GOT_ENTRIES
178 #define CONFIG_GOT_ENTRY_SIZE 4
179 #define CONFIG_USE_SINGLE
183 #if defined(__mips__)
184 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
185 #define SHT_RELM SHT_REL
186 #define Elf32_RelM Elf32_Rel
187 #define ELFCLASSM ELFCLASS32
188 /* Account for ELF spec changes. */
189 #ifndef EM_MIPS_RS3_LE
190 #ifdef EM_MIPS_RS4_BE
191 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
193 #define EM_MIPS_RS3_LE 10
195 #endif /* !EM_MIPS_RS3_LE */
196 #define ARCHDATAM "__dbe_table"
200 #if defined(__powerpc__)
201 #define MATCH_MACHINE(x) (x == EM_PPC)
202 #define SHT_RELM SHT_RELA
203 #define Elf32_RelM Elf32_Rela
204 #define ELFCLASSM ELFCLASS32
205 #define CONFIG_USE_PLT_ENTRIES
206 #define CONFIG_PLT_ENTRY_SIZE 16
207 #define CONFIG_USE_PLT_LIST
208 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
209 #define CONFIG_USE_LIST
210 #define ARCHDATAM "__ftr_fixup"
214 #if defined(__s390__)
215 #define MATCH_MACHINE(x) (x == EM_S390)
216 #define SHT_RELM SHT_RELA
217 #define Elf32_RelM Elf32_Rela
218 #define ELFCLASSM ELFCLASS32
219 #define CONFIG_USE_PLT_ENTRIES
220 #define CONFIG_PLT_ENTRY_SIZE 8
221 #define CONFIG_USE_GOT_ENTRIES
222 #define CONFIG_GOT_ENTRY_SIZE 8
223 #define CONFIG_USE_SINGLE
228 #define MATCH_MACHINE(x) (x == EM_SH)
229 #define SHT_RELM SHT_RELA
230 #define Elf32_RelM Elf32_Rela
231 #define ELFCLASSM ELFCLASS32
232 #define CONFIG_USE_GOT_ENTRIES
233 #define CONFIG_GOT_ENTRY_SIZE 4
234 #define CONFIG_USE_SINGLE
235 /* the SH changes have only been tested in =little endian= mode */
236 /* I'm not sure about big endian, so let's warn: */
237 #if defined(__sh__) && defined(__BIG_ENDIAN__)
238 #error insmod.c may require changes for use on big endian SH
240 /* it may or may not work on the SH1/SH2... Error on those also */
241 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
242 #error insmod.c may require changes for SH1 or SH2 use
247 #if defined(__sparc__)
248 #define MATCH_MACHINE(x) (x == EM_SPARC)
249 #define SHT_RELM SHT_RELA
250 #define Elf32_RelM Elf32_Rela
251 #define ELFCLASSM ELFCLASS32
255 #if defined (__v850e__)
256 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
257 #define SHT_RELM SHT_RELA
258 #define Elf32_RelM Elf32_Rela
259 #define ELFCLASSM ELFCLASS32
260 #define CONFIG_USE_PLT_ENTRIES
261 #define CONFIG_PLT_ENTRY_SIZE 8
262 #define CONFIG_USE_SINGLE
263 #ifndef EM_CYGNUS_V850 /* grumble */
264 #define EM_CYGNUS_V850 0x9080
266 #define SYMBOL_PREFIX "_"
270 #if defined(__x86_64__)
271 #define MATCH_MACHINE(x) (x == EM_X86_64)
272 #define SHT_RELM SHT_REL
273 #define Elf64_RelM Elf64_Rel
274 #define ELFCLASSM ELFCLASS64
278 #error Sorry, but insmod.c does not yet support this architecture...
282 //----------------------------------------------------------------------------
283 //--------modutils module.h, lines 45-242
284 //----------------------------------------------------------------------------
286 /* Definitions for the Linux module syscall interface.
287 Copyright 1996, 1997 Linux International.
289 Contributed by Richard Henderson <rth@tamu.edu>
291 This file is part of the Linux modutils.
293 This program is free software; you can redistribute it and/or modify it
294 under the terms of the GNU General Public License as published by the
295 Free Software Foundation; either version 2 of the License, or (at your
296 option) any later version.
298 This program is distributed in the hope that it will be useful, but
299 WITHOUT ANY WARRANTY; without even the implied warranty of
300 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
301 General Public License for more details.
303 You should have received a copy of the GNU General Public License
304 along with this program; if not, write to the Free Software Foundation,
305 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
308 #ifndef MODUTILS_MODULE_H
309 static const int MODUTILS_MODULE_H = 1;
311 #ident "$Id: insmod.c,v 1.125 2004/09/02 23:03:25 andersen Exp $"
313 /*======================================================================*/
314 /* For sizeof() which are related to the module platform and not to the
315 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
317 #define tgt_sizeof_char sizeof(char)
318 #define tgt_sizeof_short sizeof(short)
319 #define tgt_sizeof_int sizeof(int)
320 #define tgt_sizeof_long sizeof(long)
321 #define tgt_sizeof_char_p sizeof(char *)
322 #define tgt_sizeof_void_p sizeof(void *)
323 #define tgt_long long
325 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
326 #undef tgt_sizeof_long
327 #undef tgt_sizeof_char_p
328 #undef tgt_sizeof_void_p
330 static const int tgt_sizeof_long = 8;
331 static const int tgt_sizeof_char_p = 8;
332 static const int tgt_sizeof_void_p = 8;
333 #define tgt_long long long
336 /*======================================================================*/
337 /* The structures used in Linux 2.1. */
339 /* Note: new_module_symbol does not use tgt_long intentionally */
340 struct new_module_symbol
346 struct new_module_persist;
348 struct new_module_ref
350 unsigned tgt_long dep; /* kernel addresses */
351 unsigned tgt_long ref;
352 unsigned tgt_long next_ref;
357 unsigned tgt_long size_of_struct; /* == sizeof(module) */
358 unsigned tgt_long next;
359 unsigned tgt_long name;
360 unsigned tgt_long size;
363 unsigned tgt_long flags; /* AUTOCLEAN et al */
368 unsigned tgt_long syms;
369 unsigned tgt_long deps;
370 unsigned tgt_long refs;
371 unsigned tgt_long init;
372 unsigned tgt_long cleanup;
373 unsigned tgt_long ex_table_start;
374 unsigned tgt_long ex_table_end;
376 unsigned tgt_long gp;
378 /* Everything after here is extension. */
379 unsigned tgt_long persist_start;
380 unsigned tgt_long persist_end;
381 unsigned tgt_long can_unload;
382 unsigned tgt_long runsize;
383 const char *kallsyms_start; /* All symbols for kernel debugging */
384 const char *kallsyms_end;
385 const char *archdata_start; /* arch specific data for module */
386 const char *archdata_end;
387 const char *kernel_data; /* Reserved for kernel internal use */
391 #define ARCHDATA_SEC_NAME ARCHDATAM
393 #define ARCHDATA_SEC_NAME "__archdata"
395 #define KALLSYMS_SEC_NAME "__kallsyms"
398 struct new_module_info
406 /* Bits of module.flags. */
407 static const int NEW_MOD_RUNNING = 1;
408 static const int NEW_MOD_DELETED = 2;
409 static const int NEW_MOD_AUTOCLEAN = 4;
410 static const int NEW_MOD_VISITED = 8;
411 static const int NEW_MOD_USED_ONCE = 16;
413 int init_module(const char *name, const struct new_module *);
414 int query_module(const char *name, int which, void *buf,
415 size_t bufsize, size_t *ret);
417 /* Values for query_module's which. */
419 static const int QM_MODULES = 1;
420 static const int QM_DEPS = 2;
421 static const int QM_REFS = 3;
422 static const int QM_SYMBOLS = 4;
423 static const int QM_INFO = 5;
425 /*======================================================================*/
426 /* The system calls unchanged between 2.0 and 2.1. */
428 unsigned long create_module(const char *, size_t);
429 int delete_module(const char *);
432 #endif /* module.h */
434 //----------------------------------------------------------------------------
435 //--------end of modutils module.h
436 //----------------------------------------------------------------------------
440 //----------------------------------------------------------------------------
441 //--------modutils obj.h, lines 253-462
442 //----------------------------------------------------------------------------
444 /* Elf object file loading and relocation routines.
445 Copyright 1996, 1997 Linux International.
447 Contributed by Richard Henderson <rth@tamu.edu>
449 This file is part of the Linux modutils.
451 This program is free software; you can redistribute it and/or modify it
452 under the terms of the GNU General Public License as published by the
453 Free Software Foundation; either version 2 of the License, or (at your
454 option) any later version.
456 This program is distributed in the hope that it will be useful, but
457 WITHOUT ANY WARRANTY; without even the implied warranty of
458 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
459 General Public License for more details.
461 You should have received a copy of the GNU General Public License
462 along with this program; if not, write to the Free Software Foundation,
463 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
466 #ifndef MODUTILS_OBJ_H
467 static const int MODUTILS_OBJ_H = 1;
469 #ident "$Id: insmod.c,v 1.125 2004/09/02 23:03:25 andersen Exp $"
471 /* The relocatable object is manipulated using elfin types. */
477 #if __BYTE_ORDER == __LITTLE_ENDIAN
478 #define ELFDATAM ELFDATA2LSB
479 #elif __BYTE_ORDER == __BIG_ENDIAN
480 #define ELFDATAM ELFDATA2MSB
484 # if ELFCLASSM == ELFCLASS32
485 # define ElfW(x) Elf32_ ## x
486 # define ELFW(x) ELF32_ ## x
488 # define ElfW(x) Elf64_ ## x
489 # define ELFW(x) ELF64_ ## x
493 /* For some reason this is missing from some ancient C libraries.... */
494 #ifndef ELF32_ST_INFO
495 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
498 #ifndef ELF64_ST_INFO
499 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
502 struct obj_string_patch;
503 struct obj_symbol_patch;
510 struct obj_section *load_next;
516 struct obj_symbol *next; /* hash table link */
520 int secidx; /* the defining section index/module */
522 int ksymidx; /* for export to the kernel symtab */
523 int referenced; /* actually used in the link */
526 /* Hardcode the hash table size. We shouldn't be needing so many
527 symbols that we begin to degrade performance, and we get a big win
528 by giving the compiler a constant divisor. */
530 #define HASH_BUCKETS 521
536 struct obj_section **sections;
537 struct obj_section *load_order;
538 struct obj_section **load_order_search_start;
539 struct obj_string_patch *string_patches;
540 struct obj_symbol_patch *symbol_patches;
541 int (*symbol_cmp)(const char *, const char *);
542 unsigned long (*symbol_hash)(const char *);
543 unsigned long local_symtab_size;
544 struct obj_symbol **local_symtab;
545 struct obj_symbol *symtab[HASH_BUCKETS];
556 struct obj_string_patch
558 struct obj_string_patch *next;
560 ElfW(Addr) reloc_offset;
561 ElfW(Addr) string_offset;
564 struct obj_symbol_patch
566 struct obj_symbol_patch *next;
568 ElfW(Addr) reloc_offset;
569 struct obj_symbol *sym;
573 /* Generic object manipulation routines. */
575 static unsigned long obj_elf_hash(const char *);
577 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
579 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
582 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
583 struct obj_symbol *sym);
585 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
586 static void obj_set_symbol_compare(struct obj_file *f,
587 int (*cmp)(const char *, const char *),
588 unsigned long (*hash)(const char *));
591 static struct obj_section *obj_find_section (struct obj_file *f,
594 static void obj_insert_section_load_order (struct obj_file *f,
595 struct obj_section *sec);
597 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
602 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
607 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
609 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
612 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
613 struct obj_symbol *sym);
615 static int obj_check_undefineds(struct obj_file *f);
617 static void obj_allocate_commons(struct obj_file *f);
619 static unsigned long obj_load_size (struct obj_file *f);
621 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
623 static struct obj_file *obj_load(FILE *f, int loadprogbits);
625 static int obj_create_image (struct obj_file *f, char *image);
627 /* Architecture specific manipulation routines. */
629 static struct obj_file *arch_new_file (void);
631 static struct obj_section *arch_new_section (void);
633 static struct obj_symbol *arch_new_symbol (void);
635 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
636 struct obj_section *targsec,
637 struct obj_section *symsec,
638 struct obj_symbol *sym,
639 ElfW(RelM) *rel, ElfW(Addr) value);
641 static void arch_create_got (struct obj_file *f);
643 static int obj_gpl_license(struct obj_file *f, const char **license);
646 //----------------------------------------------------------------------------
647 //--------end of modutils obj.h
648 //----------------------------------------------------------------------------
651 /* SPFX is always a string, so it can be concatenated to string constants. */
653 #define SPFX SYMBOL_PREFIX
659 #define _PATH_MODULES "/lib/modules"
660 static const int STRVERSIONLEN = 32;
662 /*======================================================================*/
664 static int flag_force_load = 0;
665 static int flag_autoclean = 0;
666 static int flag_verbose = 0;
667 static int flag_quiet = 0;
668 static int flag_export = 1;
671 /*======================================================================*/
673 #if defined(CONFIG_USE_LIST)
675 struct arch_list_entry
677 struct arch_list_entry *next;
678 CONFIG_LIST_ARCHTYPE addend;
685 #if defined(CONFIG_USE_SINGLE)
687 struct arch_single_entry
696 #if defined(__mips__)
699 struct mips_hi16 *next;
706 struct obj_file root;
707 #if defined(CONFIG_USE_PLT_ENTRIES)
708 struct obj_section *plt;
710 #if defined(CONFIG_USE_GOT_ENTRIES)
711 struct obj_section *got;
713 #if defined(__mips__)
714 struct mips_hi16 *mips_hi16_list;
719 struct obj_symbol root;
720 #if defined(CONFIG_USE_PLT_ENTRIES)
721 #if defined(CONFIG_USE_PLT_LIST)
722 struct arch_list_entry *pltent;
724 struct arch_single_entry pltent;
727 #if defined(CONFIG_USE_GOT_ENTRIES)
728 struct arch_single_entry gotent;
733 struct external_module {
738 struct new_module_symbol *syms;
741 static struct new_module_symbol *ksyms;
742 static size_t nksyms;
744 static struct external_module *ext_modules;
745 static int n_ext_modules;
746 static int n_ext_modules_used;
747 extern int delete_module(const char *);
749 static char *m_filename;
750 static char *m_fullName;
754 /*======================================================================*/
757 static int check_module_name_match(const char *filename, struct stat *statbuf,
760 char *fullname = (char *) userdata;
762 if (fullname[0] == '\0')
765 char *tmp, *tmp1 = bb_xstrdup(filename);
766 tmp = bb_get_last_path_component(tmp1);
767 if (strcmp(tmp, fullname) == 0) {
769 /* Stop searching if we find a match */
770 m_filename = bb_xstrdup(filename);
779 /*======================================================================*/
781 static struct obj_file *arch_new_file(void)
784 f = xmalloc(sizeof(*f));
786 memset(f, 0, sizeof(*f));
791 static struct obj_section *arch_new_section(void)
793 return xmalloc(sizeof(struct obj_section));
796 static struct obj_symbol *arch_new_symbol(void)
798 struct arch_symbol *sym;
799 sym = xmalloc(sizeof(*sym));
801 memset(sym, 0, sizeof(*sym));
806 static enum obj_reloc
807 arch_apply_relocation(struct obj_file *f,
808 struct obj_section *targsec,
809 struct obj_section *symsec,
810 struct obj_symbol *sym,
811 ElfW(RelM) *rel, ElfW(Addr) v)
813 struct arch_file *ifile = (struct arch_file *) f;
814 enum obj_reloc ret = obj_reloc_ok;
815 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
816 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
817 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
818 struct arch_symbol *isym = (struct arch_symbol *) sym;
820 #if defined(CONFIG_USE_GOT_ENTRIES)
821 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
823 #if defined(CONFIG_USE_PLT_ENTRIES)
824 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
826 #if defined(CONFIG_USE_PLT_LIST)
827 struct arch_list_entry *pe;
829 struct arch_single_entry *pe;
833 switch (ELF32_R_TYPE(rel->r_info)) {
848 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
849 * (which is .got) similar to branch,
850 * but is full 32 bits relative */
860 case R_ARM_GOTOFF: /* address relative to the got */
865 #elif defined(__s390__)
867 *(unsigned int *) loc += v;
870 *(unsigned short *) loc += v;
873 *(unsigned char *) loc += v;
877 *(unsigned int *) loc += v - dot;
880 *(unsigned short *) loc += (v - dot) >> 1;
883 *(unsigned short *) loc += v - dot;
888 /* find the plt entry and initialize it. */
889 assert(isym != NULL);
890 pe = (struct arch_single_entry *) &isym->pltent;
891 assert(pe->allocated);
892 if (pe->inited == 0) {
893 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
894 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
896 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
903 /* Insert relative distance to target. */
904 v = plt + pe->offset - dot;
905 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
906 *(unsigned int *) loc = (unsigned int) v;
907 else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
908 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
922 *(unsigned long *) loc += got - dot;
928 assert(isym != NULL);
930 if (!isym->gotent.inited)
932 isym->gotent.inited = 1;
933 *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
935 if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
936 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
937 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
938 *(unsigned short *) loc += isym->gotent.offset;
939 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
940 *(unsigned int *) loc += isym->gotent.offset;
943 #ifndef R_390_GOTOFF32
944 #define R_390_GOTOFF32 R_390_GOTOFF
951 #elif defined(__i386__)
987 #elif defined(__mc68000__)
998 ret = obj_reloc_overflow;
1005 ret = obj_reloc_overflow;
1012 if ((Elf32_Sword)v > 0x7f ||
1013 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
1014 ret = obj_reloc_overflow;
1021 if ((Elf32_Sword)v > 0x7fff ||
1022 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1023 ret = obj_reloc_overflow;
1029 *(int *)loc = v - dot;
1032 case R_68K_GLOB_DAT:
1033 case R_68K_JMP_SLOT:
1037 case R_68K_RELATIVE:
1038 *(int *)loc += f->baseaddr;
1051 #elif defined(__mips__)
1062 ret = obj_reloc_dangerous;
1063 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1064 ret = obj_reloc_overflow;
1066 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1072 struct mips_hi16 *n;
1074 /* We cannot relocate this one now because we don't know the value
1075 of the carry we need to add. Save the information, and let LO16
1076 do the actual relocation. */
1077 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1080 n->next = ifile->mips_hi16_list;
1081 ifile->mips_hi16_list = n;
1087 unsigned long insnlo = *loc;
1088 Elf32_Addr val, vallo;
1090 /* Sign extend the addend we extract from the lo insn. */
1091 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1093 if (ifile->mips_hi16_list != NULL) {
1094 struct mips_hi16 *l;
1096 l = ifile->mips_hi16_list;
1098 struct mips_hi16 *next;
1101 /* The value for the HI16 had best be the same. */
1102 assert(v == l->value);
1104 /* Do the HI16 relocation. Note that we actually don't
1105 need to know anything about the LO16 itself, except where
1106 to find the low 16 bits of the addend needed by the LO16. */
1109 ((insn & 0xffff) << 16) +
1113 /* Account for the sign extension that will happen in the
1120 insn = (insn & ~0xffff) | val;
1128 ifile->mips_hi16_list = NULL;
1131 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1133 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1138 #elif defined(__powerpc__)
1140 case R_PPC_ADDR16_HA:
1141 *(unsigned short *)loc = (v + 0x8000) >> 16;
1144 case R_PPC_ADDR16_HI:
1145 *(unsigned short *)loc = v >> 16;
1148 case R_PPC_ADDR16_LO:
1149 *(unsigned short *)loc = v;
1163 #elif defined(__sh__)
1186 *loc = f->baseaddr + rel->r_addend;
1191 *loc = got - dot + rel->r_addend;
1202 #if defined(__SH5__)
1203 case R_SH_IMM_MEDLOW16:
1204 case R_SH_IMM_LOW16:
1208 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1212 * movi and shori have the format:
1214 * | op | imm | reg | reserved |
1215 * 31..26 25..10 9.. 4 3 .. 0
1217 * so we simply mask and or in imm.
1219 word = *loc & ~0x3fffc00;
1220 word |= (v & 0xffff) << 10;
1227 case R_SH_IMM_MEDLOW16_PCREL:
1228 case R_SH_IMM_LOW16_PCREL:
1232 word = *loc & ~0x3fffc00;
1236 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1239 word |= (v & 0xffff) << 10;
1245 #endif /* __SH5__ */
1249 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1250 ret = obj_reloc_unhandled;
1253 #if defined (__v850e__)
1258 /* We write two shorts instead of a long because even
1259 32-bit insns only need half-word alignment, but
1260 32-bit data needs to be long-word aligned. */
1261 v += ((unsigned short *)loc)[0];
1262 v += ((unsigned short *)loc)[1] << 16;
1263 ((unsigned short *)loc)[0] = v & 0xffff;
1264 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1267 case R_V850_22_PCREL:
1271 #if defined (__cris__)
1276 /* CRIS keeps the relocation value in the r_addend field and
1277 * should not use whats in *loc at all
1283 #if defined(__H8300H__) || defined(__H8300S__)
1285 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
1286 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
1297 if ((Elf32_Sword)v > 0x7fff ||
1298 (Elf32_Sword)v < -(Elf32_Sword)0x8000)
1299 ret = obj_reloc_overflow;
1301 *(unsigned short *)loc = v;
1305 if ((Elf32_Sword)v > 0x7f ||
1306 (Elf32_Sword)v < -(Elf32_Sword)0x80)
1307 ret = obj_reloc_overflow;
1309 *(unsigned char *)loc = v;
1313 #if defined(CONFIG_USE_PLT_ENTRIES)
1317 /* find the plt entry and initialize it if necessary */
1318 assert(isym != NULL);
1320 #if defined(CONFIG_USE_PLT_LIST)
1321 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1329 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1331 /* generate some machine code */
1333 #if defined(__arm__)
1334 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1335 ip[1] = v; /* sym@ */
1337 #if defined(__powerpc__)
1338 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1339 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1340 ip[2] = 0x7d6903a6; /* mtctr r11 */
1341 ip[3] = 0x4e800420; /* bctr */
1343 #if defined (__v850e__)
1344 /* We have to trash a register, so we assume that any control
1345 transfer more than 21-bits away must be a function call
1346 (so we can use a call-clobbered register). */
1347 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1348 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1353 /* relative distance to target */
1355 /* if the target is too far away.... */
1356 #if defined (__arm__) || defined (__powerpc__)
1357 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1358 #elif defined (__v850e__)
1359 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1361 /* go via the plt */
1362 v = plt + pe->offset - dot;
1364 #if defined (__v850e__)
1369 ret = obj_reloc_dangerous;
1371 /* merge the offset into the instruction. */
1372 #if defined(__arm__)
1373 /* Convert to words. */
1376 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1378 #if defined(__powerpc__)
1379 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1381 #if defined (__v850e__)
1382 /* We write two shorts instead of a long because even 32-bit insns
1383 only need half-word alignment, but the 32-bit data write needs
1384 to be long-word aligned. */
1385 ((unsigned short *)loc)[0] =
1386 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1387 | ((v >> 16) & 0x3f); /* offs high part */
1388 ((unsigned short *)loc)[1] =
1389 (v & 0xffff); /* offs low part */
1392 #endif /* CONFIG_USE_PLT_ENTRIES */
1394 #if defined(CONFIG_USE_GOT_ENTRIES)
1397 assert(isym != NULL);
1398 /* needs an entry in the .got: set it, once */
1399 if (!isym->gotent.inited) {
1400 isym->gotent.inited = 1;
1401 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1403 /* make the reloc with_respect_to_.got */
1405 *loc += isym->gotent.offset + rel->r_addend;
1406 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1407 *loc += isym->gotent.offset;
1411 #endif /* CONFIG_USE_GOT_ENTRIES */
1418 #if defined(CONFIG_USE_LIST)
1420 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1421 int offset, int size)
1423 struct arch_list_entry *pe;
1425 for (pe = *list; pe != NULL; pe = pe->next) {
1426 if (pe->addend == rel->r_addend) {
1432 pe = xmalloc(sizeof(struct arch_list_entry));
1434 pe->addend = rel->r_addend;
1435 pe->offset = offset;
1445 #if defined(CONFIG_USE_SINGLE)
1447 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1448 int offset, int size)
1450 if (single->allocated == 0) {
1451 single->allocated = 1;
1452 single->offset = offset;
1461 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1463 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1464 int offset, int size)
1466 struct obj_section *myrelsec = obj_find_section(f, name);
1473 obj_extend_section(myrelsec, offset);
1475 myrelsec = obj_create_alloced_section(f, name,
1485 static void arch_create_got(struct obj_file *f)
1487 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1488 struct arch_file *ifile = (struct arch_file *) f;
1490 #if defined(CONFIG_USE_GOT_ENTRIES)
1491 int got_offset = 0, got_needed = 0, got_allocate;
1493 #if defined(CONFIG_USE_PLT_ENTRIES)
1494 int plt_offset = 0, plt_needed = 0, plt_allocate;
1496 struct obj_section *relsec, *symsec, *strsec;
1497 ElfW(RelM) *rel, *relend;
1498 ElfW(Sym) *symtab, *extsym;
1499 const char *strtab, *name;
1500 struct arch_symbol *intsym;
1502 for (i = 0; i < f->header.e_shnum; ++i) {
1503 relsec = f->sections[i];
1504 if (relsec->header.sh_type != SHT_RELM)
1507 symsec = f->sections[relsec->header.sh_link];
1508 strsec = f->sections[symsec->header.sh_link];
1510 rel = (ElfW(RelM) *) relsec->contents;
1511 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1512 symtab = (ElfW(Sym) *) symsec->contents;
1513 strtab = (const char *) strsec->contents;
1515 for (; rel < relend; ++rel) {
1516 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1518 #if defined(CONFIG_USE_GOT_ENTRIES)
1521 #if defined(CONFIG_USE_PLT_ENTRIES)
1525 switch (ELF32_R_TYPE(rel->r_info)) {
1526 #if defined(__arm__)
1541 #elif defined(__i386__)
1551 #elif defined(__powerpc__)
1556 #elif defined(__mc68000__)
1567 #elif defined(__sh__)
1577 #elif defined (__v850e__)
1578 case R_V850_22_PCREL:
1587 if (extsym->st_name != 0) {
1588 name = strtab + extsym->st_name;
1590 name = f->sections[extsym->st_shndx]->name;
1592 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1593 #if defined(CONFIG_USE_GOT_ENTRIES)
1595 got_offset += arch_single_init(
1596 rel, &intsym->gotent,
1597 got_offset, CONFIG_GOT_ENTRY_SIZE);
1602 #if defined(CONFIG_USE_PLT_ENTRIES)
1604 #if defined(CONFIG_USE_PLT_LIST)
1605 plt_offset += arch_list_add(
1606 rel, &intsym->pltent,
1607 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1609 plt_offset += arch_single_init(
1610 rel, &intsym->pltent,
1611 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1619 #if defined(CONFIG_USE_GOT_ENTRIES)
1621 ifile->got = arch_xsect_init(f, ".got", got_offset,
1622 CONFIG_GOT_ENTRY_SIZE);
1626 #if defined(CONFIG_USE_PLT_ENTRIES)
1628 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1629 CONFIG_PLT_ENTRY_SIZE);
1633 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1636 /*======================================================================*/
1638 /* Standard ELF hash function. */
1639 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1641 unsigned long h = 0;
1648 if ((g = (h & 0xf0000000)) != 0) {
1657 static unsigned long obj_elf_hash(const char *name)
1659 return obj_elf_hash_n(name, strlen(name));
1662 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1663 /* String comparison for non-co-versioned kernel and module. */
1665 static int ncv_strcmp(const char *a, const char *b)
1667 size_t alen = strlen(a), blen = strlen(b);
1669 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1670 return strncmp(a, b, alen);
1671 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1672 return strncmp(a, b, blen);
1674 return strcmp(a, b);
1677 /* String hashing for non-co-versioned kernel and module. Here
1678 we are simply forced to drop the crc from the hash. */
1680 static unsigned long ncv_symbol_hash(const char *str)
1682 size_t len = strlen(str);
1683 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1685 return obj_elf_hash_n(str, len);
1689 obj_set_symbol_compare(struct obj_file *f,
1690 int (*cmp) (const char *, const char *),
1691 unsigned long (*hash) (const char *))
1694 f->symbol_cmp = cmp;
1696 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1699 f->symbol_hash = hash;
1701 memcpy(tmptab, f->symtab, sizeof(tmptab));
1702 memset(f->symtab, 0, sizeof(f->symtab));
1704 for (i = 0; i < HASH_BUCKETS; ++i)
1705 for (sym = tmptab[i]; sym; sym = next) {
1706 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1708 sym->next = f->symtab[h];
1714 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1716 static struct obj_symbol *
1717 obj_add_symbol(struct obj_file *f, const char *name,
1718 unsigned long symidx, int info,
1719 int secidx, ElfW(Addr) value,
1722 struct obj_symbol *sym;
1723 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1724 int n_type = ELFW(ST_TYPE) (info);
1725 int n_binding = ELFW(ST_BIND) (info);
1727 for (sym = f->symtab[hash]; sym; sym = sym->next)
1728 if (f->symbol_cmp(sym->name, name) == 0) {
1729 int o_secidx = sym->secidx;
1730 int o_info = sym->info;
1731 int o_type = ELFW(ST_TYPE) (o_info);
1732 int o_binding = ELFW(ST_BIND) (o_info);
1734 /* A redefinition! Is it legal? */
1736 if (secidx == SHN_UNDEF)
1738 else if (o_secidx == SHN_UNDEF)
1740 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1741 /* Cope with local and global symbols of the same name
1742 in the same object file, as might have been created
1743 by ld -r. The only reason locals are now seen at this
1744 level at all is so that we can do semi-sensible things
1747 struct obj_symbol *nsym, **p;
1749 nsym = arch_new_symbol();
1750 nsym->next = sym->next;
1753 /* Excise the old (local) symbol from the hash chain. */
1754 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1758 } else if (n_binding == STB_LOCAL) {
1759 /* Another symbol of the same name has already been defined.
1760 Just add this to the local table. */
1761 sym = arch_new_symbol();
1764 f->local_symtab[symidx] = sym;
1766 } else if (n_binding == STB_WEAK)
1768 else if (o_binding == STB_WEAK)
1770 /* Don't unify COMMON symbols with object types the programmer
1772 else if (secidx == SHN_COMMON
1773 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1775 else if (o_secidx == SHN_COMMON
1776 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1779 /* Don't report an error if the symbol is coming from
1780 the kernel or some external module. */
1781 if (secidx <= SHN_HIRESERVE)
1782 bb_error_msg("%s multiply defined", name);
1787 /* Completely new symbol. */
1788 sym = arch_new_symbol();
1789 sym->next = f->symtab[hash];
1790 f->symtab[hash] = sym;
1793 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1794 if (symidx >= f->local_symtab_size)
1795 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1796 name, (long) symidx, (long) f->local_symtab_size);
1798 f->local_symtab[symidx] = sym;
1805 sym->secidx = secidx;
1811 static struct obj_symbol *
1812 obj_find_symbol(struct obj_file *f, const char *name)
1814 struct obj_symbol *sym;
1815 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1817 for (sym = f->symtab[hash]; sym; sym = sym->next)
1818 if (f->symbol_cmp(sym->name, name) == 0)
1825 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1828 if (sym->secidx >= SHN_LORESERVE)
1831 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1833 /* As a special case, a NULL sym has value zero. */
1838 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1840 int i, n = f->header.e_shnum;
1842 for (i = 0; i < n; ++i)
1843 if (strcmp(f->sections[i]->name, name) == 0)
1844 return f->sections[i];
1849 static int obj_load_order_prio(struct obj_section *a)
1851 unsigned long af, ac;
1853 af = a->header.sh_flags;
1856 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1857 strcmp(a->name + 5, ".init"))
1861 if (!(af & SHF_WRITE))
1863 if (af & SHF_EXECINSTR)
1865 if (a->header.sh_type != SHT_NOBITS)
1872 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1874 struct obj_section **p;
1875 int prio = obj_load_order_prio(sec);
1876 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1877 if (obj_load_order_prio(*p) < prio)
1879 sec->load_next = *p;
1883 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1885 unsigned long align,
1888 int newidx = f->header.e_shnum++;
1889 struct obj_section *sec;
1891 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1892 f->sections[newidx] = sec = arch_new_section();
1894 memset(sec, 0, sizeof(*sec));
1895 sec->header.sh_type = SHT_PROGBITS;
1896 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1897 sec->header.sh_size = size;
1898 sec->header.sh_addralign = align;
1902 sec->contents = xmalloc(size);
1904 obj_insert_section_load_order(f, sec);
1909 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1911 unsigned long align,
1914 int newidx = f->header.e_shnum++;
1915 struct obj_section *sec;
1917 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1918 f->sections[newidx] = sec = arch_new_section();
1920 memset(sec, 0, sizeof(*sec));
1921 sec->header.sh_type = SHT_PROGBITS;
1922 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1923 sec->header.sh_size = size;
1924 sec->header.sh_addralign = align;
1928 sec->contents = xmalloc(size);
1930 sec->load_next = f->load_order;
1931 f->load_order = sec;
1932 if (f->load_order_search_start == &f->load_order)
1933 f->load_order_search_start = &sec->load_next;
1938 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1940 unsigned long oldsize = sec->header.sh_size;
1942 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1944 return sec->contents + oldsize;
1948 /* Conditionally add the symbols from the given symbol set to the
1952 add_symbols_from( struct obj_file *f,
1953 int idx, struct new_module_symbol *syms, size_t nsyms)
1955 struct new_module_symbol *s;
1958 #ifdef SYMBOL_PREFIX
1960 size_t name_alloced_size = 0;
1962 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1965 gpl = obj_gpl_license(f, NULL) == 0;
1967 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1968 /* Only add symbols that are already marked external.
1969 If we override locals we may cause problems for
1970 argument initialization. We will also create a false
1971 dependency on the module. */
1972 struct obj_symbol *sym;
1975 /* GPL licensed modules can use symbols exported with
1976 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
1977 * exported names. Non-GPL modules never see any GPLONLY_
1978 * symbols so they cannot fudge it by adding the prefix on
1981 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
1982 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1989 name = (char *)s->name;
1991 #ifdef SYMBOL_PREFIX
1992 /* Prepend SYMBOL_PREFIX to the symbol's name (the
1993 kernel exports `C names', but module object files
1994 reference `linker names'). */
1995 size_t extra = sizeof SYMBOL_PREFIX;
1996 size_t name_size = strlen (name) + extra;
1997 if (name_size > name_alloced_size) {
1998 name_alloced_size = name_size * 2;
1999 name_buf = alloca (name_alloced_size);
2001 strcpy (name_buf, SYMBOL_PREFIX);
2002 strcpy (name_buf + extra - 1, name);
2004 #endif /* SYMBOL_PREFIX */
2006 sym = obj_find_symbol(f, name);
2007 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
2008 #ifdef SYMBOL_PREFIX
2009 /* Put NAME_BUF into more permanent storage. */
2010 name = xmalloc (name_size);
2011 strcpy (name, name_buf);
2013 sym = obj_add_symbol(f, name, -1,
2014 ELFW(ST_INFO) (STB_GLOBAL,
2017 /* Did our symbol just get installed? If so, mark the
2018 module as "used". */
2019 if (sym->secidx == idx)
2027 static void add_kernel_symbols(struct obj_file *f)
2029 struct external_module *m;
2032 /* Add module symbols first. */
2034 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2036 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2037 m->nsyms)) m->used = 1, ++nused;
2039 n_ext_modules_used = nused;
2041 /* And finally the symbols from the kernel proper. */
2044 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2047 static char *get_modinfo_value(struct obj_file *f, const char *key)
2049 struct obj_section *sec;
2050 char *p, *v, *n, *ep;
2051 size_t klen = strlen(key);
2053 sec = obj_find_section(f, ".modinfo");
2057 ep = p + sec->header.sh_size;
2060 n = strchr(p, '\0');
2062 if (p + klen == v && strncmp(p, key, klen) == 0)
2065 if (p + klen == n && strcmp(p, key) == 0)
2075 /*======================================================================*/
2076 /* Functions relating to module loading after 2.1.18. */
2079 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2082 char *p, *q, *key, *sym_name;
2083 struct obj_symbol *sym;
2084 char *contents, *loc;
2088 if ((q = strchr(p, '=')) == NULL) {
2093 key = alloca(q - p + 6);
2094 memcpy(key, "parm_", 5);
2095 memcpy(key + 5, p, q - p);
2098 p = get_modinfo_value(f, key);
2101 bb_error_msg("invalid parameter %s", key);
2105 #ifdef SYMBOL_PREFIX
2106 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2107 strcpy (sym_name, SYMBOL_PREFIX);
2108 strcat (sym_name, key);
2112 sym = obj_find_symbol(f, sym_name);
2114 /* Also check that the parameter was not resolved from the kernel. */
2115 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2116 bb_error_msg("symbol for parameter %s not found", key);
2121 min = strtoul(p, &p, 10);
2123 max = strtoul(p + 1, &p, 10);
2129 contents = f->sections[sym->secidx]->contents;
2130 loc = contents + sym->value;
2134 if ((*p == 's') || (*p == 'c')) {
2137 /* Do C quoting if we begin with a ", else slurp the lot. */
2141 str = alloca(strlen(q));
2142 for (r = str, q++; *q != '"'; ++q, ++r) {
2144 bb_error_msg("improperly terminated string argument for %s",
2147 } else if (*q == '\\')
2181 if (q[1] >= '0' && q[1] <= '7') {
2182 c = (c * 8) + *++q - '0';
2183 if (q[1] >= '0' && q[1] <= '7')
2184 c = (c * 8) + *++q - '0';
2201 /* In this case, the string is not quoted. We will break
2202 it using the coma (like for ints). If the user wants to
2203 include comas in a string, he just has to quote it */
2205 /* Search the next coma */
2209 if (r != (char *) NULL) {
2210 /* Recopy the current field */
2211 str = alloca(r - q + 1);
2212 memcpy(str, q, r - q);
2214 /* I don't know if it is useful, as the previous case
2215 doesn't nul terminate the string ??? */
2218 /* Keep next fields */
2229 obj_string_patch(f, sym->secidx, loc - contents, str);
2230 loc += tgt_sizeof_char_p;
2232 /* Array of chars (in fact, matrix !) */
2233 unsigned long charssize; /* size of each member */
2235 /* Get the size of each member */
2236 /* Probably we should do that outside the loop ? */
2237 if (!isdigit(*(p + 1))) {
2238 bb_error_msg("parameter type 'c' for %s must be followed by"
2239 " the maximum size", key);
2242 charssize = strtoul(p + 1, (char **) NULL, 10);
2245 if (strlen(str) >= charssize) {
2246 bb_error_msg("string too long for %s (max %ld)", key,
2251 /* Copy to location */
2252 strcpy((char *) loc, str);
2256 long v = strtoul(q, &q, 0);
2263 loc += tgt_sizeof_short;
2267 loc += tgt_sizeof_int;
2271 loc += tgt_sizeof_long;
2275 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2290 goto retry_end_of_value;
2294 bb_error_msg("too many values for %s (max %d)", key, max);
2301 bb_error_msg("invalid argument syntax for %s", key);
2308 bb_error_msg("too few values for %s (min %d)", key, min);
2318 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2319 static int new_is_module_checksummed(struct obj_file *f)
2321 const char *p = get_modinfo_value(f, "using_checksums");
2328 /* Get the module's kernel version in the canonical integer form. */
2331 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2336 p = get_modinfo_value(f, "kernel_version");
2339 safe_strncpy(str, p, STRVERSIONLEN);
2341 a = strtoul(p, &p, 10);
2344 b = strtoul(p + 1, &p, 10);
2347 c = strtoul(p + 1, &q, 10);
2351 return a << 16 | b << 8 | c;
2354 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2357 /* Fetch the loaded modules, and all currently exported symbols. */
2359 static int new_get_kernel_symbols(void)
2361 char *module_names, *mn;
2362 struct external_module *modules, *m;
2363 struct new_module_symbol *syms, *s;
2364 size_t ret, bufsize, nmod, nsyms, i, j;
2366 /* Collect the loaded modules. */
2368 module_names = xmalloc(bufsize = 256);
2370 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2371 if (errno == ENOSPC && bufsize < ret) {
2372 module_names = xrealloc(module_names, bufsize = ret);
2373 goto retry_modules_load;
2375 bb_perror_msg("QM_MODULES");
2379 n_ext_modules = nmod = ret;
2381 /* Collect the modules' symbols. */
2384 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2385 memset(modules, 0, nmod * sizeof(*modules));
2386 for (i = 0, mn = module_names, m = modules;
2387 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2388 struct new_module_info info;
2390 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2391 if (errno == ENOENT) {
2392 /* The module was removed out from underneath us. */
2395 bb_perror_msg("query_module: QM_INFO: %s", mn);
2399 syms = xmalloc(bufsize = 1024);
2401 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2404 syms = xrealloc(syms, bufsize = ret);
2405 goto retry_mod_sym_load;
2407 /* The module was removed out from underneath us. */
2410 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2417 m->addr = info.addr;
2421 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2422 s->name += (unsigned long) syms;
2427 /* Collect the kernel's symbols. */
2429 syms = xmalloc(bufsize = 16 * 1024);
2430 retry_kern_sym_load:
2431 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2432 if (errno == ENOSPC && bufsize < ret) {
2433 syms = xrealloc(syms, bufsize = ret);
2434 goto retry_kern_sym_load;
2436 bb_perror_msg("kernel: QM_SYMBOLS");
2439 nksyms = nsyms = ret;
2442 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2443 s->name += (unsigned long) syms;
2449 /* Return the kernel symbol checksum version, or zero if not used. */
2451 static int new_is_kernel_checksummed(void)
2453 struct new_module_symbol *s;
2456 /* Using_Versions is not the first symbol, but it should be in there. */
2458 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2459 if (strcmp((char *) s->name, "Using_Versions") == 0)
2466 static int new_create_this_module(struct obj_file *f, const char *m_name)
2468 struct obj_section *sec;
2470 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2471 sizeof(struct new_module));
2472 memset(sec->contents, 0, sizeof(struct new_module));
2474 obj_add_symbol(f, SPFX "__this_module", -1,
2475 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2476 sizeof(struct new_module));
2478 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2484 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2485 /* add an entry to the __ksymtab section, creating it if necessary */
2486 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2488 struct obj_section *sec;
2491 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2492 * If __ksymtab is defined but not marked alloc, x out the first character
2493 * (no obj_delete routine) and create a new __ksymtab with the correct
2496 sec = obj_find_section(f, "__ksymtab");
2497 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2498 *((char *)(sec->name)) = 'x'; /* override const */
2502 sec = obj_create_alloced_section(f, "__ksymtab",
2503 tgt_sizeof_void_p, 0);
2506 sec->header.sh_flags |= SHF_ALLOC;
2507 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2509 ofs = sec->header.sh_size;
2510 obj_symbol_patch(f, sec->idx, ofs, sym);
2511 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2512 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2514 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2516 static int new_create_module_ksymtab(struct obj_file *f)
2518 struct obj_section *sec;
2521 /* We must always add the module references. */
2523 if (n_ext_modules_used) {
2524 struct new_module_ref *dep;
2525 struct obj_symbol *tm;
2527 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2528 (sizeof(struct new_module_ref)
2529 * n_ext_modules_used));
2533 tm = obj_find_symbol(f, SPFX "__this_module");
2534 dep = (struct new_module_ref *) sec->contents;
2535 for (i = 0; i < n_ext_modules; ++i)
2536 if (ext_modules[i].used) {
2537 dep->dep = ext_modules[i].addr;
2538 obj_symbol_patch(f, sec->idx,
2539 (char *) &dep->ref - sec->contents, tm);
2545 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2550 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2553 /* We don't want to export symbols residing in sections that
2554 aren't loaded. There are a number of these created so that
2555 we make sure certain module options don't appear twice. */
2557 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2559 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2561 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2562 struct obj_symbol *sym;
2563 for (sym = f->symtab[i]; sym; sym = sym->next)
2564 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2565 && sym->secidx <= SHN_HIRESERVE
2566 && (sym->secidx >= SHN_LORESERVE
2567 || loaded[sym->secidx])) {
2568 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2570 obj_symbol_patch(f, sec->idx, ofs, sym);
2571 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2578 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2586 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2588 struct new_module *module;
2589 struct obj_section *sec;
2594 sec = obj_find_section(f, ".this");
2595 if (!sec || !sec->contents) {
2596 bb_perror_msg_and_die("corrupt module %s?",m_name);
2598 module = (struct new_module *) sec->contents;
2599 m_addr = sec->header.sh_addr;
2601 module->size_of_struct = sizeof(*module);
2602 module->size = m_size;
2603 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2605 sec = obj_find_section(f, "__ksymtab");
2606 if (sec && sec->header.sh_size) {
2607 module->syms = sec->header.sh_addr;
2608 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2611 if (n_ext_modules_used) {
2612 sec = obj_find_section(f, ".kmodtab");
2613 module->deps = sec->header.sh_addr;
2614 module->ndeps = n_ext_modules_used;
2618 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2620 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2622 sec = obj_find_section(f, "__ex_table");
2624 module->ex_table_start = sec->header.sh_addr;
2625 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2628 sec = obj_find_section(f, ".text.init");
2630 module->runsize = sec->header.sh_addr - m_addr;
2632 sec = obj_find_section(f, ".data.init");
2634 if (!module->runsize ||
2635 module->runsize > sec->header.sh_addr - m_addr)
2636 module->runsize = sec->header.sh_addr - m_addr;
2638 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2639 if (sec && sec->header.sh_size) {
2640 module->archdata_start = (void*)sec->header.sh_addr;
2641 module->archdata_end = module->archdata_start + sec->header.sh_size;
2643 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2644 if (sec && sec->header.sh_size) {
2645 module->kallsyms_start = (void*)sec->header.sh_addr;
2646 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2649 /* Whew! All of the initialization is complete. Collect the final
2650 module image and give it to the kernel. */
2652 image = xmalloc(m_size);
2653 obj_create_image(f, image);
2655 ret = init_module(m_name, (struct new_module *) image);
2657 bb_perror_msg("init_module: %s", m_name);
2665 /*======================================================================*/
2668 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2671 struct obj_string_patch *p;
2672 struct obj_section *strsec;
2673 size_t len = strlen(string) + 1;
2676 p = xmalloc(sizeof(*p));
2677 p->next = f->string_patches;
2678 p->reloc_secidx = secidx;
2679 p->reloc_offset = offset;
2680 f->string_patches = p;
2682 strsec = obj_find_section(f, ".kstrtab");
2683 if (strsec == NULL) {
2684 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2685 p->string_offset = 0;
2686 loc = strsec->contents;
2688 p->string_offset = strsec->header.sh_size;
2689 loc = obj_extend_section(strsec, len);
2691 memcpy(loc, string, len);
2697 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2698 struct obj_symbol *sym)
2700 struct obj_symbol_patch *p;
2702 p = xmalloc(sizeof(*p));
2703 p->next = f->symbol_patches;
2704 p->reloc_secidx = secidx;
2705 p->reloc_offset = offset;
2707 f->symbol_patches = p;
2712 static int obj_check_undefineds(struct obj_file *f)
2717 for (i = 0; i < HASH_BUCKETS; ++i) {
2718 struct obj_symbol *sym;
2719 for (sym = f->symtab[i]; sym; sym = sym->next)
2720 if (sym->secidx == SHN_UNDEF) {
2721 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2722 sym->secidx = SHN_ABS;
2726 bb_error_msg("unresolved symbol %s", sym->name);
2736 static void obj_allocate_commons(struct obj_file *f)
2738 struct common_entry {
2739 struct common_entry *next;
2740 struct obj_symbol *sym;
2741 } *common_head = NULL;
2745 for (i = 0; i < HASH_BUCKETS; ++i) {
2746 struct obj_symbol *sym;
2747 for (sym = f->symtab[i]; sym; sym = sym->next)
2748 if (sym->secidx == SHN_COMMON) {
2749 /* Collect all COMMON symbols and sort them by size so as to
2750 minimize space wasted by alignment requirements. */
2752 struct common_entry **p, *n;
2753 for (p = &common_head; *p; p = &(*p)->next)
2754 if (sym->size <= (*p)->sym->size)
2757 n = alloca(sizeof(*n));
2765 for (i = 1; i < f->local_symtab_size; ++i) {
2766 struct obj_symbol *sym = f->local_symtab[i];
2767 if (sym && sym->secidx == SHN_COMMON) {
2768 struct common_entry **p, *n;
2769 for (p = &common_head; *p; p = &(*p)->next)
2770 if (sym == (*p)->sym)
2772 else if (sym->size < (*p)->sym->size) {
2773 n = alloca(sizeof(*n));
2783 /* Find the bss section. */
2784 for (i = 0; i < f->header.e_shnum; ++i)
2785 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2788 /* If for some reason there hadn't been one, create one. */
2789 if (i == f->header.e_shnum) {
2790 struct obj_section *sec;
2792 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2793 f->sections[i] = sec = arch_new_section();
2794 f->header.e_shnum = i + 1;
2796 memset(sec, 0, sizeof(*sec));
2797 sec->header.sh_type = SHT_PROGBITS;
2798 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2803 /* Allocate the COMMONS. */
2805 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2806 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2807 struct common_entry *c;
2809 for (c = common_head; c; c = c->next) {
2810 ElfW(Addr) align = c->sym->value;
2812 if (align > max_align)
2814 if (bss_size & (align - 1))
2815 bss_size = (bss_size | (align - 1)) + 1;
2818 c->sym->value = bss_size;
2820 bss_size += c->sym->size;
2823 f->sections[i]->header.sh_size = bss_size;
2824 f->sections[i]->header.sh_addralign = max_align;
2828 /* For the sake of patch relocation and parameter initialization,
2829 allocate zeroed data for NOBITS sections now. Note that after
2830 this we cannot assume NOBITS are really empty. */
2831 for (i = 0; i < f->header.e_shnum; ++i) {
2832 struct obj_section *s = f->sections[i];
2833 if (s->header.sh_type == SHT_NOBITS) {
2834 if (s->header.sh_size != 0)
2835 s->contents = memset(xmalloc(s->header.sh_size),
2836 0, s->header.sh_size);
2840 s->header.sh_type = SHT_PROGBITS;
2845 static unsigned long obj_load_size(struct obj_file *f)
2847 unsigned long dot = 0;
2848 struct obj_section *sec;
2850 /* Finalize the positions of the sections relative to one another. */
2852 for (sec = f->load_order; sec; sec = sec->load_next) {
2855 align = sec->header.sh_addralign;
2856 if (align && (dot & (align - 1)))
2857 dot = (dot | (align - 1)) + 1;
2859 sec->header.sh_addr = dot;
2860 dot += sec->header.sh_size;
2866 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2868 int i, n = f->header.e_shnum;
2871 /* Finalize the addresses of the sections. */
2874 for (i = 0; i < n; ++i)
2875 f->sections[i]->header.sh_addr += base;
2877 /* And iterate over all of the relocations. */
2879 for (i = 0; i < n; ++i) {
2880 struct obj_section *relsec, *symsec, *targsec, *strsec;
2881 ElfW(RelM) * rel, *relend;
2885 relsec = f->sections[i];
2886 if (relsec->header.sh_type != SHT_RELM)
2889 symsec = f->sections[relsec->header.sh_link];
2890 targsec = f->sections[relsec->header.sh_info];
2891 strsec = f->sections[symsec->header.sh_link];
2893 rel = (ElfW(RelM) *) relsec->contents;
2894 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2895 symtab = (ElfW(Sym) *) symsec->contents;
2896 strtab = (const char *) strsec->contents;
2898 for (; rel < relend; ++rel) {
2899 ElfW(Addr) value = 0;
2900 struct obj_symbol *intsym = NULL;
2901 unsigned long symndx;
2902 ElfW(Sym) * extsym = 0;
2905 /* Attempt to find a value to use for this relocation. */
2907 symndx = ELFW(R_SYM) (rel->r_info);
2909 /* Note we've already checked for undefined symbols. */
2911 extsym = &symtab[symndx];
2912 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2913 /* Local symbols we look up in the local table to be sure
2914 we get the one that is really intended. */
2915 intsym = f->local_symtab[symndx];
2917 /* Others we look up in the hash table. */
2919 if (extsym->st_name)
2920 name = strtab + extsym->st_name;
2922 name = f->sections[extsym->st_shndx]->name;
2923 intsym = obj_find_symbol(f, name);
2926 value = obj_symbol_final_value(f, intsym);
2927 intsym->referenced = 1;
2929 #if SHT_RELM == SHT_RELA
2930 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2931 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2932 if (!extsym || !extsym->st_name ||
2933 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2935 value += rel->r_addend;
2939 switch (arch_apply_relocation
2940 (f, targsec, symsec, intsym, rel, value)) {
2944 case obj_reloc_overflow:
2945 errmsg = "Relocation overflow";
2947 case obj_reloc_dangerous:
2948 errmsg = "Dangerous relocation";
2950 case obj_reloc_unhandled:
2951 errmsg = "Unhandled relocation";
2954 bb_error_msg("%s of type %ld for %s", errmsg,
2955 (long) ELFW(R_TYPE) (rel->r_info),
2956 strtab + extsym->st_name);
2958 bb_error_msg("%s of type %ld", errmsg,
2959 (long) ELFW(R_TYPE) (rel->r_info));
2967 /* Finally, take care of the patches. */
2969 if (f->string_patches) {
2970 struct obj_string_patch *p;
2971 struct obj_section *strsec;
2972 ElfW(Addr) strsec_base;
2973 strsec = obj_find_section(f, ".kstrtab");
2974 strsec_base = strsec->header.sh_addr;
2976 for (p = f->string_patches; p; p = p->next) {
2977 struct obj_section *targsec = f->sections[p->reloc_secidx];
2978 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2979 = strsec_base + p->string_offset;
2983 if (f->symbol_patches) {
2984 struct obj_symbol_patch *p;
2986 for (p = f->symbol_patches; p; p = p->next) {
2987 struct obj_section *targsec = f->sections[p->reloc_secidx];
2988 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2989 = obj_symbol_final_value(f, p->sym);
2996 static int obj_create_image(struct obj_file *f, char *image)
2998 struct obj_section *sec;
2999 ElfW(Addr) base = f->baseaddr;
3001 for (sec = f->load_order; sec; sec = sec->load_next) {
3004 if (sec->contents == 0 || sec->header.sh_size == 0)
3007 secimg = image + (sec->header.sh_addr - base);
3009 /* Note that we allocated data for NOBITS sections earlier. */
3010 memcpy(secimg, sec->contents, sec->header.sh_size);
3016 /*======================================================================*/
3018 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3021 ElfW(Shdr) * section_headers;
3025 /* Read the file header. */
3027 f = arch_new_file();
3028 memset(f, 0, sizeof(*f));
3029 f->symbol_cmp = strcmp;
3030 f->symbol_hash = obj_elf_hash;
3031 f->load_order_search_start = &f->load_order;
3033 fseek(fp, 0, SEEK_SET);
3034 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3035 bb_perror_msg("error reading ELF header");
3039 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3040 || f->header.e_ident[EI_MAG1] != ELFMAG1
3041 || f->header.e_ident[EI_MAG2] != ELFMAG2
3042 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3043 bb_error_msg("not an ELF file");
3046 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3047 || f->header.e_ident[EI_DATA] != ELFDATAM
3048 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3049 || !MATCH_MACHINE(f->header.e_machine)) {
3050 bb_error_msg("ELF file not for this architecture");
3053 if (f->header.e_type != ET_REL) {
3054 bb_error_msg("ELF file not a relocatable object");
3058 /* Read the section headers. */
3060 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3061 bb_error_msg("section header size mismatch: %lu != %lu",
3062 (unsigned long) f->header.e_shentsize,
3063 (unsigned long) sizeof(ElfW(Shdr)));
3067 shnum = f->header.e_shnum;
3068 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3069 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3071 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3072 fseek(fp, f->header.e_shoff, SEEK_SET);
3073 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3074 bb_perror_msg("error reading ELF section headers");
3078 /* Read the section data. */
3080 for (i = 0; i < shnum; ++i) {
3081 struct obj_section *sec;
3083 f->sections[i] = sec = arch_new_section();
3084 memset(sec, 0, sizeof(*sec));
3086 sec->header = section_headers[i];
3089 if(sec->header.sh_size) switch (sec->header.sh_type) {
3098 if (!loadprogbits) {
3099 sec->contents = NULL;
3106 if (sec->header.sh_size > 0) {
3107 sec->contents = xmalloc(sec->header.sh_size);
3108 fseek(fp, sec->header.sh_offset, SEEK_SET);
3109 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3110 bb_perror_msg("error reading ELF section data");
3114 sec->contents = NULL;
3118 #if SHT_RELM == SHT_REL
3120 bb_error_msg("RELA relocations not supported on this architecture");
3124 bb_error_msg("REL relocations not supported on this architecture");
3129 if (sec->header.sh_type >= SHT_LOPROC) {
3130 /* Assume processor specific section types are debug
3131 info and can safely be ignored. If this is ever not
3132 the case (Hello MIPS?), don't put ifdefs here but
3133 create an arch_load_proc_section(). */
3137 bb_error_msg("can't handle sections of type %ld",
3138 (long) sec->header.sh_type);
3143 /* Do what sort of interpretation as needed by each section. */
3145 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3147 for (i = 0; i < shnum; ++i) {
3148 struct obj_section *sec = f->sections[i];
3149 sec->name = shstrtab + sec->header.sh_name;
3152 for (i = 0; i < shnum; ++i) {
3153 struct obj_section *sec = f->sections[i];
3155 /* .modinfo should be contents only but gcc has no attribute for that.
3156 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3158 if (strcmp(sec->name, ".modinfo") == 0)
3159 sec->header.sh_flags &= ~SHF_ALLOC;
3161 if (sec->header.sh_flags & SHF_ALLOC)
3162 obj_insert_section_load_order(f, sec);
3164 switch (sec->header.sh_type) {
3167 unsigned long nsym, j;
3171 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3172 bb_error_msg("symbol size mismatch: %lu != %lu",
3173 (unsigned long) sec->header.sh_entsize,
3174 (unsigned long) sizeof(ElfW(Sym)));
3178 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3179 strtab = f->sections[sec->header.sh_link]->contents;
3180 sym = (ElfW(Sym) *) sec->contents;
3182 /* Allocate space for a table of local symbols. */
3183 j = f->local_symtab_size = sec->header.sh_info;
3184 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3186 /* Insert all symbols into the hash table. */
3187 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3188 ElfW(Addr) val = sym->st_value;
3191 name = strtab + sym->st_name;
3192 else if (sym->st_shndx < shnum)
3193 name = f->sections[sym->st_shndx]->name;
3197 #if defined(__SH5__)
3199 * For sh64 it is possible that the target of a branch
3200 * requires a mode switch (32 to 16 and back again).
3202 * This is implied by the lsb being set in the target
3203 * address for SHmedia mode and clear for SHcompact.
3205 val |= sym->st_other & 4;
3208 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3215 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3216 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3217 (unsigned long) sec->header.sh_entsize,
3218 (unsigned long) sizeof(ElfW(RelM)));
3222 /* XXX Relocation code from modutils-2.3.19 is not here.
3223 * Why? That's about 20 lines of code from obj/obj_load.c,
3224 * which gets done in a second pass through the sections.
3225 * This BusyBox insmod does similar work in obj_relocate(). */
3232 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3234 * load the unloaded sections directly into the memory allocated by
3235 * kernel for the module
3238 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3240 ElfW(Addr) base = f->baseaddr;
3241 struct obj_section* sec;
3243 for (sec = f->load_order; sec; sec = sec->load_next) {
3245 /* section already loaded? */
3246 if (sec->contents != NULL)
3249 if (sec->header.sh_size == 0)
3252 sec->contents = imagebase + (sec->header.sh_addr - base);
3253 fseek(fp, sec->header.sh_offset, SEEK_SET);
3254 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3255 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3264 static void hide_special_symbols(struct obj_file *f)
3266 static const char *const specials[] = {
3267 SPFX "cleanup_module",
3269 SPFX "kernel_version",
3273 struct obj_symbol *sym;
3274 const char *const *p;
3276 for (p = specials; *p; ++p)
3277 if ((sym = obj_find_symbol(f, *p)) != NULL)
3279 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3283 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3284 static int obj_gpl_license(struct obj_file *f, const char **license)
3286 struct obj_section *sec;
3287 /* This list must match *exactly* the list of allowable licenses in
3288 * linux/include/linux/module.h. Checking for leading "GPL" will not
3289 * work, somebody will use "GPL sucks, this is proprietary".
3291 static const char *gpl_licenses[] = {
3294 "GPL and additional rights",
3299 if ((sec = obj_find_section(f, ".modinfo"))) {
3300 const char *value, *ptr, *endptr;
3301 ptr = sec->contents;
3302 endptr = ptr + sec->header.sh_size;
3303 while (ptr < endptr) {
3304 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3308 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3309 if (strcmp(value+1, gpl_licenses[i]) == 0)
3314 if (strchr(ptr, '\0'))
3315 ptr = strchr(ptr, '\0') + 1;
3323 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3324 #define TAINT_PROPRIETORY_MODULE (1<<0)
3325 #define TAINT_FORCED_MODULE (1<<1)
3326 #define TAINT_UNSAFE_SMP (1<<2)
3327 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3329 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3330 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3334 static int first = 1;
3335 if (fd < 0 && !kernel_has_tainted)
3336 return; /* New modutils on old kernel */
3337 printf("Warning: loading %s will taint the kernel: %s%s\n",
3338 m_name, text1, text2);
3340 printf(" See %s for information about tainted modules\n", TAINT_URL);
3344 read(fd, buf, sizeof(buf)-1);
3345 buf[sizeof(buf)-1] = '\0';
3346 oldval = strtoul(buf, NULL, 10);
3347 sprintf(buf, "%d\n", oldval | taint);
3348 write(fd, buf, strlen(buf));
3352 /* Check if loading this module will taint the kernel. */
3353 static void check_tainted_module(struct obj_file *f, char *m_name)
3355 static const char tainted_file[] = TAINT_FILENAME;
3356 int fd, kernel_has_tainted;
3359 kernel_has_tainted = 1;
3360 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3361 if (errno == ENOENT)
3362 kernel_has_tainted = 0;
3363 else if (errno == EACCES)
3364 kernel_has_tainted = 1;
3366 perror(tainted_file);
3367 kernel_has_tainted = 0;
3371 switch (obj_gpl_license(f, &ptr)) {
3375 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3378 /* The module has a non-GPL license so we pretend that the
3379 * kernel always has a taint flag to get a warning even on
3380 * kernels without the proc flag.
3382 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3385 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3389 if (flag_force_load)
3390 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3395 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3396 #define check_tainted_module(x, y) do { } while(0);
3397 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3399 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3400 /* add module source, timestamp, kernel version and a symbol for the
3401 * start of some sections. this info is used by ksymoops to do better
3405 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3407 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3408 return new_get_module_version(f, str);
3409 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3410 strncpy(str, "???", sizeof(str));
3412 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3415 /* add module source, timestamp, kernel version and a symbol for the
3416 * start of some sections. this info is used by ksymoops to do better
3420 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3423 static const char symprefix[] = "__insmod_";
3424 struct obj_section *sec;
3425 struct obj_symbol *sym;
3426 char *name, *absolute_filename;
3427 char str[STRVERSIONLEN], real[PATH_MAX];
3428 int i, l, lm_name, lfilename, use_ksymtab, version;
3429 struct stat statbuf;
3431 static const char *section_names[] = {
3439 if (realpath(filename, real)) {
3440 absolute_filename = bb_xstrdup(real);
3443 int save_errno = errno;
3444 bb_error_msg("cannot get realpath for %s", filename);
3447 absolute_filename = bb_xstrdup(filename);
3450 lm_name = strlen(m_name);
3451 lfilename = strlen(absolute_filename);
3453 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3454 * are not to be exported. otherwise leave ksymtab alone for now, the
3455 * "export all symbols" compatibility code will export these symbols later.
3457 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3459 if ((sec = obj_find_section(f, ".this"))) {
3460 /* tag the module header with the object name, last modified
3461 * timestamp and module version. worst case for module version
3462 * is 0xffffff, decimal 16777215. putting all three fields in
3463 * one symbol is less readable but saves kernel space.
3465 l = sizeof(symprefix)+ /* "__insmod_" */
3466 lm_name+ /* module name */
3468 lfilename+ /* object filename */
3470 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3472 8+ /* version in dec */
3475 if (stat(absolute_filename, &statbuf) != 0)
3476 statbuf.st_mtime = 0;
3477 version = get_module_version(f, str); /* -1 if not found */
3478 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3479 symprefix, m_name, absolute_filename,
3480 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3482 sym = obj_add_symbol(f, name, -1,
3483 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3484 sec->idx, sec->header.sh_addr, 0);
3486 new_add_ksymtab(f, sym);
3488 free(absolute_filename);
3489 #ifdef _NOT_SUPPORTED_
3490 /* record where the persistent data is going, same address as previous symbol */
3493 l = sizeof(symprefix)+ /* "__insmod_" */
3494 lm_name+ /* module name */
3496 strlen(f->persist)+ /* data store */
3499 snprintf(name, l, "%s%s_P%s",
3500 symprefix, m_name, f->persist);
3501 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3502 sec->idx, sec->header.sh_addr, 0);
3504 new_add_ksymtab(f, sym);
3506 #endif /* _NOT_SUPPORTED_ */
3507 /* tag the desired sections if size is non-zero */
3509 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3510 if ((sec = obj_find_section(f, section_names[i])) &&
3511 sec->header.sh_size) {
3512 l = sizeof(symprefix)+ /* "__insmod_" */
3513 lm_name+ /* module name */
3515 strlen(sec->name)+ /* section name */
3517 8+ /* length in dec */
3520 snprintf(name, l, "%s%s_S%s_L%ld",
3521 symprefix, m_name, sec->name,
3522 (long)sec->header.sh_size);
3523 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3524 sec->idx, sec->header.sh_addr, 0);
3526 new_add_ksymtab(f, sym);
3530 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3532 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3533 static void print_load_map(struct obj_file *f)
3535 struct obj_symbol *sym;
3536 struct obj_symbol **all, **p;
3537 struct obj_section *sec;
3538 int i, nsyms, *loaded;
3540 /* Report on the section layout. */
3542 printf("Sections: Size %-*s Align\n",
3543 (int) (2 * sizeof(void *)), "Address");
3545 for (sec = f->load_order; sec; sec = sec->load_next) {
3549 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3554 printf("%-15s %08lx %0*lx 2**%d\n",
3556 (long)sec->header.sh_size,
3557 (int) (2 * sizeof(void *)),
3558 (long)sec->header.sh_addr,
3561 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3562 /* Quick reference which section indicies are loaded. */
3564 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3566 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3568 /* Collect the symbols we'll be listing. */
3570 for (nsyms = i = 0; 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 all = alloca(nsyms * sizeof(struct obj_symbol *));
3578 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3579 for (sym = f->symtab[i]; sym; sym = sym->next)
3580 if (sym->secidx <= SHN_HIRESERVE
3581 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3584 /* And list them. */
3585 printf("\nSymbols:\n");
3586 for (p = all; p < all + nsyms; ++p) {
3588 unsigned long value;
3591 if (sym->secidx == SHN_ABS) {
3594 } else if (sym->secidx == SHN_UNDEF) {
3598 sec = f->sections[sym->secidx];
3600 if (sec->header.sh_type == SHT_NOBITS)
3602 else if (sec->header.sh_flags & SHF_ALLOC) {
3603 if (sec->header.sh_flags & SHF_EXECINSTR)
3605 else if (sec->header.sh_flags & SHF_WRITE)
3610 value = sym->value + sec->header.sh_addr;
3613 if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
3614 type = tolower(type);
3616 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3624 extern int insmod_main( int argc, char **argv)
3630 unsigned long m_size;
3635 int exit_status = EXIT_FAILURE;
3637 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3638 struct utsname uts_info;
3639 char m_strversion[STRVERSIONLEN];
3640 int m_version, m_crcs;
3642 #ifdef CONFIG_FEATURE_CLEAN_UP
3647 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3648 int flag_print_load_map = 0;
3651 struct utsname myuname;
3653 /* Parse any options */
3654 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3655 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
3657 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
3661 case 'f': /* force loading */
3662 flag_force_load = 1;
3664 case 'k': /* module loaded by kerneld, auto-cleanable */
3667 case 's': /* log to syslog */
3668 /* log to syslog -- not supported */
3669 /* but kernel needs this for request_module(), */
3670 /* as this calls: modprobe -k -s -- <module> */
3671 /* so silently ignore this flag */
3673 case 'v': /* verbose output */
3676 case 'q': /* silent */
3679 case 'x': /* do not export externs */
3682 case 'o': /* name the output module */
3684 m_name = bb_xstrdup(optarg);
3686 case 'L': /* Stub warning */
3687 /* This is needed for compatibility with modprobe.
3688 * In theory, this does locking, but we don't do
3689 * that. So be careful and plan your life around not
3690 * loading the same module 50 times concurrently. */
3692 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3693 case 'm': /* print module load map */
3694 flag_print_load_map = 1;
3702 if (argv[optind] == NULL) {
3706 /* Grab the module name */
3707 tmp1 = bb_xstrdup(argv[optind]);
3708 tmp = basename(tmp1);
3711 if (uname(&myuname) == 0) {
3712 if (myuname.release[0] == '2') {
3713 k_version = myuname.release[2] - '0';
3717 #if defined(CONFIG_FEATURE_2_6_MODULES)
3718 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
3719 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
3725 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3731 #if defined(CONFIG_FEATURE_2_6_MODULES)
3733 bb_xasprintf(&m_fullName, "%s.ko", tmp);
3736 bb_xasprintf(&m_fullName, "%s.o", tmp);
3742 tmp1 = 0; /* flag for free(m_name) before exit() */
3745 /* Get a filedesc for the module. Check we we have a complete path */
3746 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3747 (fp = fopen(argv[optind], "r")) == NULL) {
3748 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3749 * but do not error out yet if we fail to find it... */
3750 if (k_version) { /* uname succeedd */
3753 char real_module_dir[FILENAME_MAX];
3755 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
3756 /* Jump through hoops in case /lib/modules/`uname -r`
3757 * is a symlink. We do not want recursive_action to
3758 * follow symlinks, but we do want to follow the
3759 * /lib/modules/`uname -r` dir, So resolve it ourselves
3760 * if it is a link... */
3761 if (realpath (tmdn, real_module_dir) == NULL)
3764 module_dir = real_module_dir;
3765 recursive_action(module_dir, TRUE, FALSE, FALSE,
3766 check_module_name_match, 0, m_fullName);
3770 /* Check if we have found anything yet */
3771 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
3773 char module_dir[FILENAME_MAX];
3777 if (realpath (_PATH_MODULES, module_dir) == NULL)
3778 strcpy(module_dir, _PATH_MODULES);
3779 /* No module found under /lib/modules/`uname -r`, this
3780 * time cast the net a bit wider. Search /lib/modules/ */
3781 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3782 check_module_name_match, 0, m_fullName))
3785 || ((fp = fopen(m_filename, "r")) == NULL))
3787 bb_error_msg("%s: no module by that name found", m_fullName);
3791 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
3794 m_filename = bb_xstrdup(argv[optind]);
3797 printf("Using %s\n", m_filename);
3799 #ifdef CONFIG_FEATURE_2_6_MODULES
3803 argv[optind + 1] = m_filename;
3804 return insmod_ng_main(argc - optind, argv + optind);
3808 if ((f = obj_load(fp, LOADBITS)) == NULL)
3809 bb_perror_msg_and_die("Could not load the module");
3811 if (get_modinfo_value(f, "kernel_version") == NULL)
3816 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3817 /* Version correspondence? */
3819 if (uname(&uts_info) < 0)
3820 uts_info.release[0] = '\0';
3821 if (m_has_modinfo) {
3822 m_version = new_get_module_version(f, m_strversion);
3823 if (m_version == -1) {
3824 bb_error_msg("couldn't find the kernel version the module was "
3830 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3831 if (flag_force_load) {
3832 bb_error_msg("Warning: kernel-module version mismatch\n"
3833 "\t%s was compiled for kernel version %s\n"
3834 "\twhile this kernel is version %s",
3835 m_filename, m_strversion, uts_info.release);
3837 bb_error_msg("kernel-module version mismatch\n"
3838 "\t%s was compiled for kernel version %s\n"
3839 "\twhile this kernel is version %s.",
3840 m_filename, m_strversion, uts_info.release);
3846 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3848 if (!query_module(NULL, 0, NULL, 0, NULL)) {
3849 if (!new_get_kernel_symbols())
3851 k_crcs = new_is_kernel_checksummed();
3853 bb_error_msg("Not configured to support old kernels");
3857 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3860 m_crcs = new_is_module_checksummed(f);
3862 if (m_crcs != k_crcs)
3863 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3864 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3866 /* Let the module know about the kernel symbols. */
3867 add_kernel_symbols(f);
3869 /* Allocate common symbols, symbol tables, and string tables. */
3871 if (!new_create_this_module(f, m_name))
3876 if (!obj_check_undefineds(f)) {
3879 obj_allocate_commons(f);
3880 check_tainted_module(f, m_name);
3882 /* done with the module name, on to the optional var=value arguments */
3885 if (optind < argc) {
3886 if (!new_process_module_arguments(f, argc - optind, argv + optind))
3893 hide_special_symbols(f);
3895 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3896 add_ksymoops_symbols(f, m_filename, m_name);
3897 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3899 new_create_module_ksymtab(f);
3901 /* Find current size of the module */
3902 m_size = obj_load_size(f);
3905 m_addr = create_module(m_name, m_size);
3906 if (m_addr == -1) switch (errno) {
3908 bb_error_msg("A module named %s already exists", m_name);
3911 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3915 bb_perror_msg("create_module: %s", m_name);
3921 * the PROGBITS section was not loaded by the obj_load
3922 * now we can load them directly into the kernel memory
3924 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3925 delete_module(m_name);
3930 if (!obj_relocate(f, m_addr)) {
3931 delete_module(m_name);
3935 if (!new_init_module(m_name, f, m_size))
3937 delete_module(m_name);
3941 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3942 if(flag_print_load_map)
3946 exit_status = EXIT_SUCCESS;
3949 #ifdef CONFIG_FEATURE_CLEAN_UP
3959 return(exit_status);
3966 #ifdef CONFIG_FEATURE_2_6_MODULES
3968 #include <sys/mman.h>
3969 #include <asm/unistd.h>
3970 #include <sys/syscall.h>
3972 /* We use error numbers in a loose translation... */
3973 static const char *moderror(int err)
3977 return "Invalid module format";
3979 return "Unknown symbol in module";
3981 return "Module has wrong symbol version";
3983 return "Invalid parameters";
3985 return strerror(err);
3989 extern int insmod_ng_main( int argc, char **argv)
3997 char *filename, *options = bb_xstrdup("");
4005 /* Rest is options */
4006 for (i = 2; i < argc; i++) {
4007 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4008 /* Spaces handled by "" pairs, but no way of escaping quotes */
4009 if (strchr(argv[i], ' ')) {
4010 strcat(options, "\"");
4011 strcat(options, argv[i]);
4012 strcat(options, "\"");
4014 strcat(options, argv[i]);
4016 strcat(options, " ");
4019 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4020 bb_perror_msg_and_die("cannot open module `%s'", filename);
4025 map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
4026 if (map == MAP_FAILED) {
4027 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4030 ret = syscall(__NR_init_module, map, len, options);
4032 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4033 filename, moderror(errno), ret);