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
113 #if defined(__alpha__)
114 #define MATCH_MACHINE(x) (x == EM_ALPHA)
115 #define SHT_RELM SHT_RELA
116 #define Elf64_RelM Elf64_Rela
117 #define ELFCLASSM ELFCLASS64
122 #define MATCH_MACHINE(x) (x == EM_ARM)
123 #define SHT_RELM SHT_REL
124 #define Elf32_RelM Elf32_Rel
125 #define ELFCLASSM ELFCLASS32
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
134 #if defined(__cris__)
135 #define MATCH_MACHINE(x) (x == EM_CRIS)
136 #define SHT_RELM SHT_RELA
137 #define Elf32_RelM Elf32_Rela
138 #define ELFCLASSM ELFCLASS32
141 #define R_CRIS_NONE 0
146 /* PA-RISC / HP-PA */
147 #if defined(__hppa__)
148 #define MATCH_MACHINE(x) (x == EM_PARISC)
149 #define SHT_RELM SHT_RELA
150 #if defined(__LP64__)
151 #define Elf64_RelM Elf64_Rela
152 #define ELFCLASSM ELFCLASS64
154 #define Elf32_RelM Elf32_Rela
155 #define ELFCLASSM ELFCLASS32
160 #if defined(__H8300H__) || defined(__H8300S__)
161 #define MATCH_MACHINE(x) (x == EM_H8_300)
162 #define SHT_RELM SHT_RELA
163 #define Elf32_RelM Elf32_Rela
164 #define ELFCLASSM ELFCLASS32
165 #define CONFIG_USE_SINGLE
166 #define SYMBOL_PREFIX "_"
170 #if defined(__i386__)
172 #define MATCH_MACHINE(x) (x == EM_386)
174 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
176 #define SHT_RELM SHT_REL
177 #define Elf32_RelM Elf32_Rel
178 #define ELFCLASSM ELFCLASS32
179 #define CONFIG_USE_GOT_ENTRIES
180 #define CONFIG_GOT_ENTRY_SIZE 4
181 #define CONFIG_USE_SINGLE
184 /* IA64, aka Itanium */
185 #if defined(__ia64__)
186 #define MATCH_MACHINE(x) (x == EM_IA_64)
187 #define SHT_RELM SHT_RELA
188 #define Elf64_RelM Elf64_Rela
189 #define ELFCLASSM ELFCLASS64
193 #if defined(__mc68000__)
194 #define MATCH_MACHINE(x) (x == EM_68K)
195 #define SHT_RELM SHT_RELA
196 #define Elf32_RelM Elf32_Rela
197 #define ELFCLASSM ELFCLASS32
198 #define CONFIG_USE_GOT_ENTRIES
199 #define CONFIG_GOT_ENTRY_SIZE 4
200 #define CONFIG_USE_SINGLE
204 #if defined(__mips__)
205 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
206 #define SHT_RELM SHT_REL
207 #define Elf32_RelM Elf32_Rel
208 #define ELFCLASSM ELFCLASS32
209 /* Account for ELF spec changes. */
210 #ifndef EM_MIPS_RS3_LE
211 #ifdef EM_MIPS_RS4_BE
212 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
214 #define EM_MIPS_RS3_LE 10
216 #endif /* !EM_MIPS_RS3_LE */
217 #define ARCHDATAM "__dbe_table"
221 #if defined(__powerpc__)
222 #define MATCH_MACHINE(x) (x == EM_PPC)
223 #define SHT_RELM SHT_RELA
224 #define Elf32_RelM Elf32_Rela
225 #define ELFCLASSM ELFCLASS32
226 #define CONFIG_USE_PLT_ENTRIES
227 #define CONFIG_PLT_ENTRY_SIZE 16
228 #define CONFIG_USE_PLT_LIST
229 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
230 #define CONFIG_USE_LIST
231 #define ARCHDATAM "__ftr_fixup"
235 #if defined(__s390__)
236 #define MATCH_MACHINE(x) (x == EM_S390)
237 #define SHT_RELM SHT_RELA
238 #define Elf32_RelM Elf32_Rela
239 #define ELFCLASSM ELFCLASS32
240 #define CONFIG_USE_PLT_ENTRIES
241 #define CONFIG_PLT_ENTRY_SIZE 8
242 #define CONFIG_USE_GOT_ENTRIES
243 #define CONFIG_GOT_ENTRY_SIZE 8
244 #define CONFIG_USE_SINGLE
249 #define MATCH_MACHINE(x) (x == EM_SH)
250 #define SHT_RELM SHT_RELA
251 #define Elf32_RelM Elf32_Rela
252 #define ELFCLASSM ELFCLASS32
253 #define CONFIG_USE_GOT_ENTRIES
254 #define CONFIG_GOT_ENTRY_SIZE 4
255 #define CONFIG_USE_SINGLE
256 /* the SH changes have only been tested in =little endian= mode */
257 /* I'm not sure about big endian, so let's warn: */
258 #if defined(__sh__) && defined(__BIG_ENDIAN__)
259 #error insmod.c may require changes for use on big endian SH
261 /* it may or may not work on the SH1/SH2... Error on those also */
262 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
263 #error insmod.c may require changes for SH1 or SH2 use
268 #if defined(__sparc__)
269 #define MATCH_MACHINE(x) (x == EM_SPARC)
270 #define SHT_RELM SHT_RELA
271 #define Elf32_RelM Elf32_Rela
272 #define ELFCLASSM ELFCLASS32
276 #if defined (__v850e__)
277 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
278 #define SHT_RELM SHT_RELA
279 #define Elf32_RelM Elf32_Rela
280 #define ELFCLASSM ELFCLASS32
281 #define CONFIG_USE_PLT_ENTRIES
282 #define CONFIG_PLT_ENTRY_SIZE 8
283 #define CONFIG_USE_SINGLE
284 #ifndef EM_CYGNUS_V850 /* grumble */
285 #define EM_CYGNUS_V850 0x9080
287 #define SYMBOL_PREFIX "_"
291 #if defined(__x86_64__)
292 #define MATCH_MACHINE(x) (x == EM_X86_64)
293 #define SHT_RELM SHT_RELA
294 #define Elf64_RelM Elf64_Rela
295 #define ELFCLASSM ELFCLASS64
299 #error Sorry, but insmod.c does not yet support this architecture...
303 //----------------------------------------------------------------------------
304 //--------modutils module.h, lines 45-242
305 //----------------------------------------------------------------------------
307 /* Definitions for the Linux module syscall interface.
308 Copyright 1996, 1997 Linux International.
310 Contributed by Richard Henderson <rth@tamu.edu>
312 This file is part of the Linux modutils.
314 This program is free software; you can redistribute it and/or modify it
315 under the terms of the GNU General Public License as published by the
316 Free Software Foundation; either version 2 of the License, or (at your
317 option) any later version.
319 This program is distributed in the hope that it will be useful, but
320 WITHOUT ANY WARRANTY; without even the implied warranty of
321 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
322 General Public License for more details.
324 You should have received a copy of the GNU General Public License
325 along with this program; if not, write to the Free Software Foundation,
326 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
329 #ifndef MODUTILS_MODULE_H
330 static const int MODUTILS_MODULE_H = 1;
332 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
334 /*======================================================================*/
335 /* For sizeof() which are related to the module platform and not to the
336 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
338 #define tgt_sizeof_char sizeof(char)
339 #define tgt_sizeof_short sizeof(short)
340 #define tgt_sizeof_int sizeof(int)
341 #define tgt_sizeof_long sizeof(long)
342 #define tgt_sizeof_char_p sizeof(char *)
343 #define tgt_sizeof_void_p sizeof(void *)
344 #define tgt_long long
346 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
347 #undef tgt_sizeof_long
348 #undef tgt_sizeof_char_p
349 #undef tgt_sizeof_void_p
351 static const int tgt_sizeof_long = 8;
352 static const int tgt_sizeof_char_p = 8;
353 static const int tgt_sizeof_void_p = 8;
354 #define tgt_long long long
357 /*======================================================================*/
358 /* The structures used in Linux 2.1. */
360 /* Note: new_module_symbol does not use tgt_long intentionally */
361 struct new_module_symbol
367 struct new_module_persist;
369 struct new_module_ref
371 unsigned tgt_long dep; /* kernel addresses */
372 unsigned tgt_long ref;
373 unsigned tgt_long next_ref;
378 unsigned tgt_long size_of_struct; /* == sizeof(module) */
379 unsigned tgt_long next;
380 unsigned tgt_long name;
381 unsigned tgt_long size;
384 unsigned tgt_long flags; /* AUTOCLEAN et al */
389 unsigned tgt_long syms;
390 unsigned tgt_long deps;
391 unsigned tgt_long refs;
392 unsigned tgt_long init;
393 unsigned tgt_long cleanup;
394 unsigned tgt_long ex_table_start;
395 unsigned tgt_long ex_table_end;
397 unsigned tgt_long gp;
399 /* Everything after here is extension. */
400 unsigned tgt_long persist_start;
401 unsigned tgt_long persist_end;
402 unsigned tgt_long can_unload;
403 unsigned tgt_long runsize;
404 const char *kallsyms_start; /* All symbols for kernel debugging */
405 const char *kallsyms_end;
406 const char *archdata_start; /* arch specific data for module */
407 const char *archdata_end;
408 const char *kernel_data; /* Reserved for kernel internal use */
412 #define ARCHDATA_SEC_NAME ARCHDATAM
414 #define ARCHDATA_SEC_NAME "__archdata"
416 #define KALLSYMS_SEC_NAME "__kallsyms"
419 struct new_module_info
427 /* Bits of module.flags. */
428 static const int NEW_MOD_RUNNING = 1;
429 static const int NEW_MOD_DELETED = 2;
430 static const int NEW_MOD_AUTOCLEAN = 4;
431 static const int NEW_MOD_VISITED = 8;
432 static const int NEW_MOD_USED_ONCE = 16;
434 int init_module(const char *name, const struct new_module *);
435 int query_module(const char *name, int which, void *buf,
436 size_t bufsize, size_t *ret);
438 /* Values for query_module's which. */
440 static const int QM_MODULES = 1;
441 static const int QM_DEPS = 2;
442 static const int QM_REFS = 3;
443 static const int QM_SYMBOLS = 4;
444 static const int QM_INFO = 5;
446 /*======================================================================*/
447 /* The system calls unchanged between 2.0 and 2.1. */
449 unsigned long create_module(const char *, size_t);
450 int delete_module(const char *);
453 #endif /* module.h */
455 //----------------------------------------------------------------------------
456 //--------end of modutils module.h
457 //----------------------------------------------------------------------------
461 //----------------------------------------------------------------------------
462 //--------modutils obj.h, lines 253-462
463 //----------------------------------------------------------------------------
465 /* Elf object file loading and relocation routines.
466 Copyright 1996, 1997 Linux International.
468 Contributed by Richard Henderson <rth@tamu.edu>
470 This file is part of the Linux modutils.
472 This program is free software; you can redistribute it and/or modify it
473 under the terms of the GNU General Public License as published by the
474 Free Software Foundation; either version 2 of the License, or (at your
475 option) any later version.
477 This program is distributed in the hope that it will be useful, but
478 WITHOUT ANY WARRANTY; without even the implied warranty of
479 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
480 General Public License for more details.
482 You should have received a copy of the GNU General Public License
483 along with this program; if not, write to the Free Software Foundation,
484 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
487 #ifndef MODUTILS_OBJ_H
488 static const int MODUTILS_OBJ_H = 1;
490 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
492 /* The relocatable object is manipulated using elfin types. */
498 #if __BYTE_ORDER == __LITTLE_ENDIAN
499 #define ELFDATAM ELFDATA2LSB
500 #elif __BYTE_ORDER == __BIG_ENDIAN
501 #define ELFDATAM ELFDATA2MSB
505 # if ELFCLASSM == ELFCLASS32
506 # define ElfW(x) Elf32_ ## x
507 # define ELFW(x) ELF32_ ## x
509 # define ElfW(x) Elf64_ ## x
510 # define ELFW(x) ELF64_ ## x
514 /* For some reason this is missing from some ancient C libraries.... */
515 #ifndef ELF32_ST_INFO
516 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
519 #ifndef ELF64_ST_INFO
520 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
523 struct obj_string_patch;
524 struct obj_symbol_patch;
531 struct obj_section *load_next;
537 struct obj_symbol *next; /* hash table link */
541 int secidx; /* the defining section index/module */
543 int ksymidx; /* for export to the kernel symtab */
544 int referenced; /* actually used in the link */
547 /* Hardcode the hash table size. We shouldn't be needing so many
548 symbols that we begin to degrade performance, and we get a big win
549 by giving the compiler a constant divisor. */
551 #define HASH_BUCKETS 521
557 struct obj_section **sections;
558 struct obj_section *load_order;
559 struct obj_section **load_order_search_start;
560 struct obj_string_patch *string_patches;
561 struct obj_symbol_patch *symbol_patches;
562 int (*symbol_cmp)(const char *, const char *);
563 unsigned long (*symbol_hash)(const char *);
564 unsigned long local_symtab_size;
565 struct obj_symbol **local_symtab;
566 struct obj_symbol *symtab[HASH_BUCKETS];
577 struct obj_string_patch
579 struct obj_string_patch *next;
581 ElfW(Addr) reloc_offset;
582 ElfW(Addr) string_offset;
585 struct obj_symbol_patch
587 struct obj_symbol_patch *next;
589 ElfW(Addr) reloc_offset;
590 struct obj_symbol *sym;
594 /* Generic object manipulation routines. */
596 static unsigned long obj_elf_hash(const char *);
598 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
600 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
603 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
604 struct obj_symbol *sym);
606 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
607 static void obj_set_symbol_compare(struct obj_file *f,
608 int (*cmp)(const char *, const char *),
609 unsigned long (*hash)(const char *));
612 static struct obj_section *obj_find_section (struct obj_file *f,
615 static void obj_insert_section_load_order (struct obj_file *f,
616 struct obj_section *sec);
618 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
623 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
628 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
630 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
633 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
634 struct obj_symbol *sym);
636 static int obj_check_undefineds(struct obj_file *f);
638 static void obj_allocate_commons(struct obj_file *f);
640 static unsigned long obj_load_size (struct obj_file *f);
642 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
644 static struct obj_file *obj_load(FILE *f, int loadprogbits);
646 static int obj_create_image (struct obj_file *f, char *image);
648 /* Architecture specific manipulation routines. */
650 static struct obj_file *arch_new_file (void);
652 static struct obj_section *arch_new_section (void);
654 static struct obj_symbol *arch_new_symbol (void);
656 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
657 struct obj_section *targsec,
658 struct obj_section *symsec,
659 struct obj_symbol *sym,
660 ElfW(RelM) *rel, ElfW(Addr) value);
662 static void arch_create_got (struct obj_file *f);
664 static int obj_gpl_license(struct obj_file *f, const char **license);
667 //----------------------------------------------------------------------------
668 //--------end of modutils obj.h
669 //----------------------------------------------------------------------------
672 /* SPFX is always a string, so it can be concatenated to string constants. */
674 #define SPFX SYMBOL_PREFIX
680 #define _PATH_MODULES "/lib/modules"
681 static const int STRVERSIONLEN = 32;
683 /*======================================================================*/
685 static int flag_force_load = 0;
686 static int flag_autoclean = 0;
687 static int flag_verbose = 0;
688 static int flag_quiet = 0;
689 static int flag_export = 1;
692 /*======================================================================*/
694 #if defined(CONFIG_USE_LIST)
696 struct arch_list_entry
698 struct arch_list_entry *next;
699 CONFIG_LIST_ARCHTYPE addend;
706 #if defined(CONFIG_USE_SINGLE)
708 struct arch_single_entry
717 #if defined(__mips__)
720 struct mips_hi16 *next;
727 struct obj_file root;
728 #if defined(CONFIG_USE_PLT_ENTRIES)
729 struct obj_section *plt;
731 #if defined(CONFIG_USE_GOT_ENTRIES)
732 struct obj_section *got;
734 #if defined(__mips__)
735 struct mips_hi16 *mips_hi16_list;
740 struct obj_symbol root;
741 #if defined(CONFIG_USE_PLT_ENTRIES)
742 #if defined(CONFIG_USE_PLT_LIST)
743 struct arch_list_entry *pltent;
745 struct arch_single_entry pltent;
748 #if defined(CONFIG_USE_GOT_ENTRIES)
749 struct arch_single_entry gotent;
754 struct external_module {
759 struct new_module_symbol *syms;
762 static struct new_module_symbol *ksyms;
763 static size_t nksyms;
765 static struct external_module *ext_modules;
766 static int n_ext_modules;
767 static int n_ext_modules_used;
768 extern int delete_module(const char *);
770 static char *m_filename;
771 static char *m_fullName;
775 /*======================================================================*/
778 static int check_module_name_match(const char *filename, struct stat *statbuf,
781 char *fullname = (char *) userdata;
783 if (fullname[0] == '\0')
786 char *tmp, *tmp1 = bb_xstrdup(filename);
787 tmp = bb_get_last_path_component(tmp1);
788 if (strcmp(tmp, fullname) == 0) {
790 /* Stop searching if we find a match */
791 m_filename = bb_xstrdup(filename);
800 /*======================================================================*/
802 static struct obj_file *arch_new_file(void)
805 f = xmalloc(sizeof(*f));
807 memset(f, 0, sizeof(*f));
812 static struct obj_section *arch_new_section(void)
814 return xmalloc(sizeof(struct obj_section));
817 static struct obj_symbol *arch_new_symbol(void)
819 struct arch_symbol *sym;
820 sym = xmalloc(sizeof(*sym));
822 memset(sym, 0, sizeof(*sym));
827 static enum obj_reloc
828 arch_apply_relocation(struct obj_file *f,
829 struct obj_section *targsec,
830 struct obj_section *symsec,
831 struct obj_symbol *sym,
832 ElfW(RelM) *rel, ElfW(Addr) v)
834 struct arch_file *ifile = (struct arch_file *) f;
835 enum obj_reloc ret = obj_reloc_ok;
836 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
837 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
838 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
839 struct arch_symbol *isym = (struct arch_symbol *) sym;
841 #if defined(CONFIG_USE_GOT_ENTRIES)
842 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
844 #if defined(CONFIG_USE_PLT_ENTRIES)
845 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
847 #if defined(CONFIG_USE_PLT_LIST)
848 struct arch_list_entry *pe;
850 struct arch_single_entry *pe;
854 switch (ELF32_R_TYPE(rel->r_info)) {
869 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
870 * (which is .got) similar to branch,
871 * but is full 32 bits relative */
881 case R_ARM_GOTOFF: /* address relative to the got */
886 #elif defined(__s390__)
888 *(unsigned int *) loc += v;
891 *(unsigned short *) loc += v;
894 *(unsigned char *) loc += v;
898 *(unsigned int *) loc += v - dot;
901 *(unsigned short *) loc += (v - dot) >> 1;
904 *(unsigned short *) loc += v - dot;
909 /* find the plt entry and initialize it. */
910 assert(isym != NULL);
911 pe = (struct arch_single_entry *) &isym->pltent;
912 assert(pe->allocated);
913 if (pe->inited == 0) {
914 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
915 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
917 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
924 /* Insert relative distance to target. */
925 v = plt + pe->offset - dot;
926 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
927 *(unsigned int *) loc = (unsigned int) v;
928 else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
929 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
943 *(unsigned long *) loc += got - dot;
949 assert(isym != NULL);
951 if (!isym->gotent.inited)
953 isym->gotent.inited = 1;
954 *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
956 if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
957 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
958 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
959 *(unsigned short *) loc += isym->gotent.offset;
960 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
961 *(unsigned int *) loc += isym->gotent.offset;
964 #ifndef R_390_GOTOFF32
965 #define R_390_GOTOFF32 R_390_GOTOFF
972 #elif defined(__i386__)
1008 #elif defined(__mc68000__)
1019 ret = obj_reloc_overflow;
1026 ret = obj_reloc_overflow;
1033 if ((Elf32_Sword)v > 0x7f ||
1034 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
1035 ret = obj_reloc_overflow;
1042 if ((Elf32_Sword)v > 0x7fff ||
1043 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1044 ret = obj_reloc_overflow;
1050 *(int *)loc = v - dot;
1053 case R_68K_GLOB_DAT:
1054 case R_68K_JMP_SLOT:
1058 case R_68K_RELATIVE:
1059 *(int *)loc += f->baseaddr;
1072 #elif defined(__mips__)
1083 ret = obj_reloc_dangerous;
1084 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1085 ret = obj_reloc_overflow;
1087 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1093 struct mips_hi16 *n;
1095 /* We cannot relocate this one now because we don't know the value
1096 of the carry we need to add. Save the information, and let LO16
1097 do the actual relocation. */
1098 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1101 n->next = ifile->mips_hi16_list;
1102 ifile->mips_hi16_list = n;
1108 unsigned long insnlo = *loc;
1109 Elf32_Addr val, vallo;
1111 /* Sign extend the addend we extract from the lo insn. */
1112 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1114 if (ifile->mips_hi16_list != NULL) {
1115 struct mips_hi16 *l;
1117 l = ifile->mips_hi16_list;
1119 struct mips_hi16 *next;
1122 /* The value for the HI16 had best be the same. */
1123 assert(v == l->value);
1125 /* Do the HI16 relocation. Note that we actually don't
1126 need to know anything about the LO16 itself, except where
1127 to find the low 16 bits of the addend needed by the LO16. */
1130 ((insn & 0xffff) << 16) +
1134 /* Account for the sign extension that will happen in the
1141 insn = (insn & ~0xffff) | val;
1149 ifile->mips_hi16_list = NULL;
1152 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1154 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1159 #elif defined(__powerpc__)
1161 case R_PPC_ADDR16_HA:
1162 *(unsigned short *)loc = (v + 0x8000) >> 16;
1165 case R_PPC_ADDR16_HI:
1166 *(unsigned short *)loc = v >> 16;
1169 case R_PPC_ADDR16_LO:
1170 *(unsigned short *)loc = v;
1184 #elif defined(__sh__)
1207 *loc = f->baseaddr + rel->r_addend;
1212 *loc = got - dot + rel->r_addend;
1223 #if defined(__SH5__)
1224 case R_SH_IMM_MEDLOW16:
1225 case R_SH_IMM_LOW16:
1229 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1233 * movi and shori have the format:
1235 * | op | imm | reg | reserved |
1236 * 31..26 25..10 9.. 4 3 .. 0
1238 * so we simply mask and or in imm.
1240 word = *loc & ~0x3fffc00;
1241 word |= (v & 0xffff) << 10;
1248 case R_SH_IMM_MEDLOW16_PCREL:
1249 case R_SH_IMM_LOW16_PCREL:
1253 word = *loc & ~0x3fffc00;
1257 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1260 word |= (v & 0xffff) << 10;
1266 #endif /* __SH5__ */
1270 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1271 ret = obj_reloc_unhandled;
1274 #if defined (__v850e__)
1279 /* We write two shorts instead of a long because even
1280 32-bit insns only need half-word alignment, but
1281 32-bit data needs to be long-word aligned. */
1282 v += ((unsigned short *)loc)[0];
1283 v += ((unsigned short *)loc)[1] << 16;
1284 ((unsigned short *)loc)[0] = v & 0xffff;
1285 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1288 case R_V850_22_PCREL:
1292 #if defined (__cris__)
1297 /* CRIS keeps the relocation value in the r_addend field and
1298 * should not use whats in *loc at all
1304 #if defined(__H8300H__) || defined(__H8300S__)
1306 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
1307 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
1318 if ((Elf32_Sword)v > 0x7fff ||
1319 (Elf32_Sword)v < -(Elf32_Sword)0x8000)
1320 ret = obj_reloc_overflow;
1322 *(unsigned short *)loc = v;
1326 if ((Elf32_Sword)v > 0x7f ||
1327 (Elf32_Sword)v < -(Elf32_Sword)0x80)
1328 ret = obj_reloc_overflow;
1330 *(unsigned char *)loc = v;
1334 #if defined(CONFIG_USE_PLT_ENTRIES)
1338 /* find the plt entry and initialize it if necessary */
1339 assert(isym != NULL);
1341 #if defined(CONFIG_USE_PLT_LIST)
1342 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1350 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1352 /* generate some machine code */
1354 #if defined(__arm__)
1355 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1356 ip[1] = v; /* sym@ */
1358 #if defined(__powerpc__)
1359 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1360 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1361 ip[2] = 0x7d6903a6; /* mtctr r11 */
1362 ip[3] = 0x4e800420; /* bctr */
1364 #if defined (__v850e__)
1365 /* We have to trash a register, so we assume that any control
1366 transfer more than 21-bits away must be a function call
1367 (so we can use a call-clobbered register). */
1368 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1369 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1374 /* relative distance to target */
1376 /* if the target is too far away.... */
1377 #if defined (__arm__) || defined (__powerpc__)
1378 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1379 #elif defined (__v850e__)
1380 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1382 /* go via the plt */
1383 v = plt + pe->offset - dot;
1385 #if defined (__v850e__)
1390 ret = obj_reloc_dangerous;
1392 /* merge the offset into the instruction. */
1393 #if defined(__arm__)
1394 /* Convert to words. */
1397 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1399 #if defined(__powerpc__)
1400 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1402 #if defined (__v850e__)
1403 /* We write two shorts instead of a long because even 32-bit insns
1404 only need half-word alignment, but the 32-bit data write needs
1405 to be long-word aligned. */
1406 ((unsigned short *)loc)[0] =
1407 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1408 | ((v >> 16) & 0x3f); /* offs high part */
1409 ((unsigned short *)loc)[1] =
1410 (v & 0xffff); /* offs low part */
1413 #endif /* CONFIG_USE_PLT_ENTRIES */
1415 #if defined(CONFIG_USE_GOT_ENTRIES)
1418 assert(isym != NULL);
1419 /* needs an entry in the .got: set it, once */
1420 if (!isym->gotent.inited) {
1421 isym->gotent.inited = 1;
1422 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1424 /* make the reloc with_respect_to_.got */
1426 *loc += isym->gotent.offset + rel->r_addend;
1427 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1428 *loc += isym->gotent.offset;
1432 #endif /* CONFIG_USE_GOT_ENTRIES */
1439 #if defined(CONFIG_USE_LIST)
1441 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1442 int offset, int size)
1444 struct arch_list_entry *pe;
1446 for (pe = *list; pe != NULL; pe = pe->next) {
1447 if (pe->addend == rel->r_addend) {
1453 pe = xmalloc(sizeof(struct arch_list_entry));
1455 pe->addend = rel->r_addend;
1456 pe->offset = offset;
1466 #if defined(CONFIG_USE_SINGLE)
1468 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1469 int offset, int size)
1471 if (single->allocated == 0) {
1472 single->allocated = 1;
1473 single->offset = offset;
1482 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1484 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1485 int offset, int size)
1487 struct obj_section *myrelsec = obj_find_section(f, name);
1494 obj_extend_section(myrelsec, offset);
1496 myrelsec = obj_create_alloced_section(f, name,
1506 static void arch_create_got(struct obj_file *f)
1508 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1509 struct arch_file *ifile = (struct arch_file *) f;
1511 #if defined(CONFIG_USE_GOT_ENTRIES)
1512 int got_offset = 0, got_needed = 0, got_allocate;
1514 #if defined(CONFIG_USE_PLT_ENTRIES)
1515 int plt_offset = 0, plt_needed = 0, plt_allocate;
1517 struct obj_section *relsec, *symsec, *strsec;
1518 ElfW(RelM) *rel, *relend;
1519 ElfW(Sym) *symtab, *extsym;
1520 const char *strtab, *name;
1521 struct arch_symbol *intsym;
1523 for (i = 0; i < f->header.e_shnum; ++i) {
1524 relsec = f->sections[i];
1525 if (relsec->header.sh_type != SHT_RELM)
1528 symsec = f->sections[relsec->header.sh_link];
1529 strsec = f->sections[symsec->header.sh_link];
1531 rel = (ElfW(RelM) *) relsec->contents;
1532 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1533 symtab = (ElfW(Sym) *) symsec->contents;
1534 strtab = (const char *) strsec->contents;
1536 for (; rel < relend; ++rel) {
1537 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1539 #if defined(CONFIG_USE_GOT_ENTRIES)
1542 #if defined(CONFIG_USE_PLT_ENTRIES)
1546 switch (ELF32_R_TYPE(rel->r_info)) {
1547 #if defined(__arm__)
1562 #elif defined(__i386__)
1572 #elif defined(__powerpc__)
1577 #elif defined(__mc68000__)
1588 #elif defined(__sh__)
1598 #elif defined (__v850e__)
1599 case R_V850_22_PCREL:
1608 if (extsym->st_name != 0) {
1609 name = strtab + extsym->st_name;
1611 name = f->sections[extsym->st_shndx]->name;
1613 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1614 #if defined(CONFIG_USE_GOT_ENTRIES)
1616 got_offset += arch_single_init(
1617 rel, &intsym->gotent,
1618 got_offset, CONFIG_GOT_ENTRY_SIZE);
1623 #if defined(CONFIG_USE_PLT_ENTRIES)
1625 #if defined(CONFIG_USE_PLT_LIST)
1626 plt_offset += arch_list_add(
1627 rel, &intsym->pltent,
1628 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1630 plt_offset += arch_single_init(
1631 rel, &intsym->pltent,
1632 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1640 #if defined(CONFIG_USE_GOT_ENTRIES)
1642 ifile->got = arch_xsect_init(f, ".got", got_offset,
1643 CONFIG_GOT_ENTRY_SIZE);
1647 #if defined(CONFIG_USE_PLT_ENTRIES)
1649 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1650 CONFIG_PLT_ENTRY_SIZE);
1654 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1657 /*======================================================================*/
1659 /* Standard ELF hash function. */
1660 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1662 unsigned long h = 0;
1669 if ((g = (h & 0xf0000000)) != 0) {
1678 static unsigned long obj_elf_hash(const char *name)
1680 return obj_elf_hash_n(name, strlen(name));
1683 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1684 /* String comparison for non-co-versioned kernel and module. */
1686 static int ncv_strcmp(const char *a, const char *b)
1688 size_t alen = strlen(a), blen = strlen(b);
1690 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1691 return strncmp(a, b, alen);
1692 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1693 return strncmp(a, b, blen);
1695 return strcmp(a, b);
1698 /* String hashing for non-co-versioned kernel and module. Here
1699 we are simply forced to drop the crc from the hash. */
1701 static unsigned long ncv_symbol_hash(const char *str)
1703 size_t len = strlen(str);
1704 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1706 return obj_elf_hash_n(str, len);
1710 obj_set_symbol_compare(struct obj_file *f,
1711 int (*cmp) (const char *, const char *),
1712 unsigned long (*hash) (const char *))
1715 f->symbol_cmp = cmp;
1717 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1720 f->symbol_hash = hash;
1722 memcpy(tmptab, f->symtab, sizeof(tmptab));
1723 memset(f->symtab, 0, sizeof(f->symtab));
1725 for (i = 0; i < HASH_BUCKETS; ++i)
1726 for (sym = tmptab[i]; sym; sym = next) {
1727 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1729 sym->next = f->symtab[h];
1735 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1737 static struct obj_symbol *
1738 obj_add_symbol(struct obj_file *f, const char *name,
1739 unsigned long symidx, int info,
1740 int secidx, ElfW(Addr) value,
1743 struct obj_symbol *sym;
1744 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1745 int n_type = ELFW(ST_TYPE) (info);
1746 int n_binding = ELFW(ST_BIND) (info);
1748 for (sym = f->symtab[hash]; sym; sym = sym->next)
1749 if (f->symbol_cmp(sym->name, name) == 0) {
1750 int o_secidx = sym->secidx;
1751 int o_info = sym->info;
1752 int o_type = ELFW(ST_TYPE) (o_info);
1753 int o_binding = ELFW(ST_BIND) (o_info);
1755 /* A redefinition! Is it legal? */
1757 if (secidx == SHN_UNDEF)
1759 else if (o_secidx == SHN_UNDEF)
1761 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1762 /* Cope with local and global symbols of the same name
1763 in the same object file, as might have been created
1764 by ld -r. The only reason locals are now seen at this
1765 level at all is so that we can do semi-sensible things
1768 struct obj_symbol *nsym, **p;
1770 nsym = arch_new_symbol();
1771 nsym->next = sym->next;
1774 /* Excise the old (local) symbol from the hash chain. */
1775 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1779 } else if (n_binding == STB_LOCAL) {
1780 /* Another symbol of the same name has already been defined.
1781 Just add this to the local table. */
1782 sym = arch_new_symbol();
1785 f->local_symtab[symidx] = sym;
1787 } else if (n_binding == STB_WEAK)
1789 else if (o_binding == STB_WEAK)
1791 /* Don't unify COMMON symbols with object types the programmer
1793 else if (secidx == SHN_COMMON
1794 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1796 else if (o_secidx == SHN_COMMON
1797 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1800 /* Don't report an error if the symbol is coming from
1801 the kernel or some external module. */
1802 if (secidx <= SHN_HIRESERVE)
1803 bb_error_msg("%s multiply defined", name);
1808 /* Completely new symbol. */
1809 sym = arch_new_symbol();
1810 sym->next = f->symtab[hash];
1811 f->symtab[hash] = sym;
1814 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1815 if (symidx >= f->local_symtab_size)
1816 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1817 name, (long) symidx, (long) f->local_symtab_size);
1819 f->local_symtab[symidx] = sym;
1826 sym->secidx = secidx;
1832 static struct obj_symbol *
1833 obj_find_symbol(struct obj_file *f, const char *name)
1835 struct obj_symbol *sym;
1836 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1838 for (sym = f->symtab[hash]; sym; sym = sym->next)
1839 if (f->symbol_cmp(sym->name, name) == 0)
1846 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1849 if (sym->secidx >= SHN_LORESERVE)
1852 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1854 /* As a special case, a NULL sym has value zero. */
1859 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1861 int i, n = f->header.e_shnum;
1863 for (i = 0; i < n; ++i)
1864 if (strcmp(f->sections[i]->name, name) == 0)
1865 return f->sections[i];
1870 static int obj_load_order_prio(struct obj_section *a)
1872 unsigned long af, ac;
1874 af = a->header.sh_flags;
1877 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1878 strcmp(a->name + 5, ".init"))
1882 if (!(af & SHF_WRITE))
1884 if (af & SHF_EXECINSTR)
1886 if (a->header.sh_type != SHT_NOBITS)
1893 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1895 struct obj_section **p;
1896 int prio = obj_load_order_prio(sec);
1897 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1898 if (obj_load_order_prio(*p) < prio)
1900 sec->load_next = *p;
1904 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1906 unsigned long align,
1909 int newidx = f->header.e_shnum++;
1910 struct obj_section *sec;
1912 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1913 f->sections[newidx] = sec = arch_new_section();
1915 memset(sec, 0, sizeof(*sec));
1916 sec->header.sh_type = SHT_PROGBITS;
1917 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1918 sec->header.sh_size = size;
1919 sec->header.sh_addralign = align;
1923 sec->contents = xmalloc(size);
1925 obj_insert_section_load_order(f, sec);
1930 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1932 unsigned long align,
1935 int newidx = f->header.e_shnum++;
1936 struct obj_section *sec;
1938 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1939 f->sections[newidx] = sec = arch_new_section();
1941 memset(sec, 0, sizeof(*sec));
1942 sec->header.sh_type = SHT_PROGBITS;
1943 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1944 sec->header.sh_size = size;
1945 sec->header.sh_addralign = align;
1949 sec->contents = xmalloc(size);
1951 sec->load_next = f->load_order;
1952 f->load_order = sec;
1953 if (f->load_order_search_start == &f->load_order)
1954 f->load_order_search_start = &sec->load_next;
1959 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1961 unsigned long oldsize = sec->header.sh_size;
1963 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1965 return sec->contents + oldsize;
1969 /* Conditionally add the symbols from the given symbol set to the
1973 add_symbols_from( struct obj_file *f,
1974 int idx, struct new_module_symbol *syms, size_t nsyms)
1976 struct new_module_symbol *s;
1979 #ifdef SYMBOL_PREFIX
1981 size_t name_alloced_size = 0;
1983 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
1986 gpl = obj_gpl_license(f, NULL) == 0;
1988 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1989 /* Only add symbols that are already marked external.
1990 If we override locals we may cause problems for
1991 argument initialization. We will also create a false
1992 dependency on the module. */
1993 struct obj_symbol *sym;
1996 /* GPL licensed modules can use symbols exported with
1997 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
1998 * exported names. Non-GPL modules never see any GPLONLY_
1999 * symbols so they cannot fudge it by adding the prefix on
2002 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2003 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2010 name = (char *)s->name;
2012 #ifdef SYMBOL_PREFIX
2013 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2014 kernel exports `C names', but module object files
2015 reference `linker names'). */
2016 size_t extra = sizeof SYMBOL_PREFIX;
2017 size_t name_size = strlen (name) + extra;
2018 if (name_size > name_alloced_size) {
2019 name_alloced_size = name_size * 2;
2020 name_buf = alloca (name_alloced_size);
2022 strcpy (name_buf, SYMBOL_PREFIX);
2023 strcpy (name_buf + extra - 1, name);
2025 #endif /* SYMBOL_PREFIX */
2027 sym = obj_find_symbol(f, name);
2028 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
2029 #ifdef SYMBOL_PREFIX
2030 /* Put NAME_BUF into more permanent storage. */
2031 name = xmalloc (name_size);
2032 strcpy (name, name_buf);
2034 sym = obj_add_symbol(f, name, -1,
2035 ELFW(ST_INFO) (STB_GLOBAL,
2038 /* Did our symbol just get installed? If so, mark the
2039 module as "used". */
2040 if (sym->secidx == idx)
2048 static void add_kernel_symbols(struct obj_file *f)
2050 struct external_module *m;
2053 /* Add module symbols first. */
2055 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2057 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2058 m->nsyms)) m->used = 1, ++nused;
2060 n_ext_modules_used = nused;
2062 /* And finally the symbols from the kernel proper. */
2065 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2068 static char *get_modinfo_value(struct obj_file *f, const char *key)
2070 struct obj_section *sec;
2071 char *p, *v, *n, *ep;
2072 size_t klen = strlen(key);
2074 sec = obj_find_section(f, ".modinfo");
2078 ep = p + sec->header.sh_size;
2081 n = strchr(p, '\0');
2083 if (p + klen == v && strncmp(p, key, klen) == 0)
2086 if (p + klen == n && strcmp(p, key) == 0)
2096 /*======================================================================*/
2097 /* Functions relating to module loading after 2.1.18. */
2100 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2103 char *p, *q, *key, *sym_name;
2104 struct obj_symbol *sym;
2105 char *contents, *loc;
2109 if ((q = strchr(p, '=')) == NULL) {
2114 key = alloca(q - p + 6);
2115 memcpy(key, "parm_", 5);
2116 memcpy(key + 5, p, q - p);
2119 p = get_modinfo_value(f, key);
2122 bb_error_msg("invalid parameter %s", key);
2126 #ifdef SYMBOL_PREFIX
2127 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2128 strcpy (sym_name, SYMBOL_PREFIX);
2129 strcat (sym_name, key);
2133 sym = obj_find_symbol(f, sym_name);
2135 /* Also check that the parameter was not resolved from the kernel. */
2136 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2137 bb_error_msg("symbol for parameter %s not found", key);
2142 min = strtoul(p, &p, 10);
2144 max = strtoul(p + 1, &p, 10);
2150 contents = f->sections[sym->secidx]->contents;
2151 loc = contents + sym->value;
2155 if ((*p == 's') || (*p == 'c')) {
2158 /* Do C quoting if we begin with a ", else slurp the lot. */
2162 str = alloca(strlen(q));
2163 for (r = str, q++; *q != '"'; ++q, ++r) {
2165 bb_error_msg("improperly terminated string argument for %s",
2168 } else if (*q == '\\')
2202 if (q[1] >= '0' && q[1] <= '7') {
2203 c = (c * 8) + *++q - '0';
2204 if (q[1] >= '0' && q[1] <= '7')
2205 c = (c * 8) + *++q - '0';
2222 /* In this case, the string is not quoted. We will break
2223 it using the coma (like for ints). If the user wants to
2224 include comas in a string, he just has to quote it */
2226 /* Search the next coma */
2230 if (r != (char *) NULL) {
2231 /* Recopy the current field */
2232 str = alloca(r - q + 1);
2233 memcpy(str, q, r - q);
2235 /* I don't know if it is useful, as the previous case
2236 doesn't nul terminate the string ??? */
2239 /* Keep next fields */
2250 obj_string_patch(f, sym->secidx, loc - contents, str);
2251 loc += tgt_sizeof_char_p;
2253 /* Array of chars (in fact, matrix !) */
2254 unsigned long charssize; /* size of each member */
2256 /* Get the size of each member */
2257 /* Probably we should do that outside the loop ? */
2258 if (!isdigit(*(p + 1))) {
2259 bb_error_msg("parameter type 'c' for %s must be followed by"
2260 " the maximum size", key);
2263 charssize = strtoul(p + 1, (char **) NULL, 10);
2266 if (strlen(str) >= charssize) {
2267 bb_error_msg("string too long for %s (max %ld)", key,
2272 /* Copy to location */
2273 strcpy((char *) loc, str);
2277 long v = strtoul(q, &q, 0);
2284 loc += tgt_sizeof_short;
2288 loc += tgt_sizeof_int;
2292 loc += tgt_sizeof_long;
2296 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2311 goto retry_end_of_value;
2315 bb_error_msg("too many values for %s (max %d)", key, max);
2322 bb_error_msg("invalid argument syntax for %s", key);
2329 bb_error_msg("too few values for %s (min %d)", key, min);
2339 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2340 static int new_is_module_checksummed(struct obj_file *f)
2342 const char *p = get_modinfo_value(f, "using_checksums");
2349 /* Get the module's kernel version in the canonical integer form. */
2352 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2357 p = get_modinfo_value(f, "kernel_version");
2360 safe_strncpy(str, p, STRVERSIONLEN);
2362 a = strtoul(p, &p, 10);
2365 b = strtoul(p + 1, &p, 10);
2368 c = strtoul(p + 1, &q, 10);
2372 return a << 16 | b << 8 | c;
2375 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2378 /* Fetch the loaded modules, and all currently exported symbols. */
2380 static int new_get_kernel_symbols(void)
2382 char *module_names, *mn;
2383 struct external_module *modules, *m;
2384 struct new_module_symbol *syms, *s;
2385 size_t ret, bufsize, nmod, nsyms, i, j;
2387 /* Collect the loaded modules. */
2389 module_names = xmalloc(bufsize = 256);
2391 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2392 if (errno == ENOSPC && bufsize < ret) {
2393 module_names = xrealloc(module_names, bufsize = ret);
2394 goto retry_modules_load;
2396 bb_perror_msg("QM_MODULES");
2400 n_ext_modules = nmod = ret;
2402 /* Collect the modules' symbols. */
2405 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2406 memset(modules, 0, nmod * sizeof(*modules));
2407 for (i = 0, mn = module_names, m = modules;
2408 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2409 struct new_module_info info;
2411 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2412 if (errno == ENOENT) {
2413 /* The module was removed out from underneath us. */
2416 bb_perror_msg("query_module: QM_INFO: %s", mn);
2420 syms = xmalloc(bufsize = 1024);
2422 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2425 syms = xrealloc(syms, bufsize = ret);
2426 goto retry_mod_sym_load;
2428 /* The module was removed out from underneath us. */
2431 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2438 m->addr = info.addr;
2442 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2443 s->name += (unsigned long) syms;
2448 /* Collect the kernel's symbols. */
2450 syms = xmalloc(bufsize = 16 * 1024);
2451 retry_kern_sym_load:
2452 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2453 if (errno == ENOSPC && bufsize < ret) {
2454 syms = xrealloc(syms, bufsize = ret);
2455 goto retry_kern_sym_load;
2457 bb_perror_msg("kernel: QM_SYMBOLS");
2460 nksyms = nsyms = ret;
2463 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2464 s->name += (unsigned long) syms;
2470 /* Return the kernel symbol checksum version, or zero if not used. */
2472 static int new_is_kernel_checksummed(void)
2474 struct new_module_symbol *s;
2477 /* Using_Versions is not the first symbol, but it should be in there. */
2479 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2480 if (strcmp((char *) s->name, "Using_Versions") == 0)
2487 static int new_create_this_module(struct obj_file *f, const char *m_name)
2489 struct obj_section *sec;
2491 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2492 sizeof(struct new_module));
2493 memset(sec->contents, 0, sizeof(struct new_module));
2495 obj_add_symbol(f, SPFX "__this_module", -1,
2496 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2497 sizeof(struct new_module));
2499 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2505 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2506 /* add an entry to the __ksymtab section, creating it if necessary */
2507 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2509 struct obj_section *sec;
2512 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2513 * If __ksymtab is defined but not marked alloc, x out the first character
2514 * (no obj_delete routine) and create a new __ksymtab with the correct
2517 sec = obj_find_section(f, "__ksymtab");
2518 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2519 *((char *)(sec->name)) = 'x'; /* override const */
2523 sec = obj_create_alloced_section(f, "__ksymtab",
2524 tgt_sizeof_void_p, 0);
2527 sec->header.sh_flags |= SHF_ALLOC;
2528 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2530 ofs = sec->header.sh_size;
2531 obj_symbol_patch(f, sec->idx, ofs, sym);
2532 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2533 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2535 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2537 static int new_create_module_ksymtab(struct obj_file *f)
2539 struct obj_section *sec;
2542 /* We must always add the module references. */
2544 if (n_ext_modules_used) {
2545 struct new_module_ref *dep;
2546 struct obj_symbol *tm;
2548 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2549 (sizeof(struct new_module_ref)
2550 * n_ext_modules_used));
2554 tm = obj_find_symbol(f, SPFX "__this_module");
2555 dep = (struct new_module_ref *) sec->contents;
2556 for (i = 0; i < n_ext_modules; ++i)
2557 if (ext_modules[i].used) {
2558 dep->dep = ext_modules[i].addr;
2559 obj_symbol_patch(f, sec->idx,
2560 (char *) &dep->ref - sec->contents, tm);
2566 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2571 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2574 /* We don't want to export symbols residing in sections that
2575 aren't loaded. There are a number of these created so that
2576 we make sure certain module options don't appear twice. */
2578 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2580 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2582 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2583 struct obj_symbol *sym;
2584 for (sym = f->symtab[i]; sym; sym = sym->next)
2585 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2586 && sym->secidx <= SHN_HIRESERVE
2587 && (sym->secidx >= SHN_LORESERVE
2588 || loaded[sym->secidx])) {
2589 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2591 obj_symbol_patch(f, sec->idx, ofs, sym);
2592 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2599 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2607 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2609 struct new_module *module;
2610 struct obj_section *sec;
2615 sec = obj_find_section(f, ".this");
2616 if (!sec || !sec->contents) {
2617 bb_perror_msg_and_die("corrupt module %s?",m_name);
2619 module = (struct new_module *) sec->contents;
2620 m_addr = sec->header.sh_addr;
2622 module->size_of_struct = sizeof(*module);
2623 module->size = m_size;
2624 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2626 sec = obj_find_section(f, "__ksymtab");
2627 if (sec && sec->header.sh_size) {
2628 module->syms = sec->header.sh_addr;
2629 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2632 if (n_ext_modules_used) {
2633 sec = obj_find_section(f, ".kmodtab");
2634 module->deps = sec->header.sh_addr;
2635 module->ndeps = n_ext_modules_used;
2639 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2641 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2643 sec = obj_find_section(f, "__ex_table");
2645 module->ex_table_start = sec->header.sh_addr;
2646 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2649 sec = obj_find_section(f, ".text.init");
2651 module->runsize = sec->header.sh_addr - m_addr;
2653 sec = obj_find_section(f, ".data.init");
2655 if (!module->runsize ||
2656 module->runsize > sec->header.sh_addr - m_addr)
2657 module->runsize = sec->header.sh_addr - m_addr;
2659 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2660 if (sec && sec->header.sh_size) {
2661 module->archdata_start = (void*)sec->header.sh_addr;
2662 module->archdata_end = module->archdata_start + sec->header.sh_size;
2664 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2665 if (sec && sec->header.sh_size) {
2666 module->kallsyms_start = (void*)sec->header.sh_addr;
2667 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2670 /* Whew! All of the initialization is complete. Collect the final
2671 module image and give it to the kernel. */
2673 image = xmalloc(m_size);
2674 obj_create_image(f, image);
2676 ret = init_module(m_name, (struct new_module *) image);
2678 bb_perror_msg("init_module: %s", m_name);
2686 /*======================================================================*/
2689 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2692 struct obj_string_patch *p;
2693 struct obj_section *strsec;
2694 size_t len = strlen(string) + 1;
2697 p = xmalloc(sizeof(*p));
2698 p->next = f->string_patches;
2699 p->reloc_secidx = secidx;
2700 p->reloc_offset = offset;
2701 f->string_patches = p;
2703 strsec = obj_find_section(f, ".kstrtab");
2704 if (strsec == NULL) {
2705 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2706 p->string_offset = 0;
2707 loc = strsec->contents;
2709 p->string_offset = strsec->header.sh_size;
2710 loc = obj_extend_section(strsec, len);
2712 memcpy(loc, string, len);
2718 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2719 struct obj_symbol *sym)
2721 struct obj_symbol_patch *p;
2723 p = xmalloc(sizeof(*p));
2724 p->next = f->symbol_patches;
2725 p->reloc_secidx = secidx;
2726 p->reloc_offset = offset;
2728 f->symbol_patches = p;
2733 static int obj_check_undefineds(struct obj_file *f)
2738 for (i = 0; i < HASH_BUCKETS; ++i) {
2739 struct obj_symbol *sym;
2740 for (sym = f->symtab[i]; sym; sym = sym->next)
2741 if (sym->secidx == SHN_UNDEF) {
2742 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2743 sym->secidx = SHN_ABS;
2747 bb_error_msg("unresolved symbol %s", sym->name);
2757 static void obj_allocate_commons(struct obj_file *f)
2759 struct common_entry {
2760 struct common_entry *next;
2761 struct obj_symbol *sym;
2762 } *common_head = NULL;
2766 for (i = 0; i < HASH_BUCKETS; ++i) {
2767 struct obj_symbol *sym;
2768 for (sym = f->symtab[i]; sym; sym = sym->next)
2769 if (sym->secidx == SHN_COMMON) {
2770 /* Collect all COMMON symbols and sort them by size so as to
2771 minimize space wasted by alignment requirements. */
2773 struct common_entry **p, *n;
2774 for (p = &common_head; *p; p = &(*p)->next)
2775 if (sym->size <= (*p)->sym->size)
2778 n = alloca(sizeof(*n));
2786 for (i = 1; i < f->local_symtab_size; ++i) {
2787 struct obj_symbol *sym = f->local_symtab[i];
2788 if (sym && sym->secidx == SHN_COMMON) {
2789 struct common_entry **p, *n;
2790 for (p = &common_head; *p; p = &(*p)->next)
2791 if (sym == (*p)->sym)
2793 else if (sym->size < (*p)->sym->size) {
2794 n = alloca(sizeof(*n));
2804 /* Find the bss section. */
2805 for (i = 0; i < f->header.e_shnum; ++i)
2806 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2809 /* If for some reason there hadn't been one, create one. */
2810 if (i == f->header.e_shnum) {
2811 struct obj_section *sec;
2813 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2814 f->sections[i] = sec = arch_new_section();
2815 f->header.e_shnum = i + 1;
2817 memset(sec, 0, sizeof(*sec));
2818 sec->header.sh_type = SHT_PROGBITS;
2819 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2824 /* Allocate the COMMONS. */
2826 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2827 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2828 struct common_entry *c;
2830 for (c = common_head; c; c = c->next) {
2831 ElfW(Addr) align = c->sym->value;
2833 if (align > max_align)
2835 if (bss_size & (align - 1))
2836 bss_size = (bss_size | (align - 1)) + 1;
2839 c->sym->value = bss_size;
2841 bss_size += c->sym->size;
2844 f->sections[i]->header.sh_size = bss_size;
2845 f->sections[i]->header.sh_addralign = max_align;
2849 /* For the sake of patch relocation and parameter initialization,
2850 allocate zeroed data for NOBITS sections now. Note that after
2851 this we cannot assume NOBITS are really empty. */
2852 for (i = 0; i < f->header.e_shnum; ++i) {
2853 struct obj_section *s = f->sections[i];
2854 if (s->header.sh_type == SHT_NOBITS) {
2855 if (s->header.sh_size != 0)
2856 s->contents = memset(xmalloc(s->header.sh_size),
2857 0, s->header.sh_size);
2861 s->header.sh_type = SHT_PROGBITS;
2866 static unsigned long obj_load_size(struct obj_file *f)
2868 unsigned long dot = 0;
2869 struct obj_section *sec;
2871 /* Finalize the positions of the sections relative to one another. */
2873 for (sec = f->load_order; sec; sec = sec->load_next) {
2876 align = sec->header.sh_addralign;
2877 if (align && (dot & (align - 1)))
2878 dot = (dot | (align - 1)) + 1;
2880 sec->header.sh_addr = dot;
2881 dot += sec->header.sh_size;
2887 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2889 int i, n = f->header.e_shnum;
2892 /* Finalize the addresses of the sections. */
2895 for (i = 0; i < n; ++i)
2896 f->sections[i]->header.sh_addr += base;
2898 /* And iterate over all of the relocations. */
2900 for (i = 0; i < n; ++i) {
2901 struct obj_section *relsec, *symsec, *targsec, *strsec;
2902 ElfW(RelM) * rel, *relend;
2906 relsec = f->sections[i];
2907 if (relsec->header.sh_type != SHT_RELM)
2910 symsec = f->sections[relsec->header.sh_link];
2911 targsec = f->sections[relsec->header.sh_info];
2912 strsec = f->sections[symsec->header.sh_link];
2914 rel = (ElfW(RelM) *) relsec->contents;
2915 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2916 symtab = (ElfW(Sym) *) symsec->contents;
2917 strtab = (const char *) strsec->contents;
2919 for (; rel < relend; ++rel) {
2920 ElfW(Addr) value = 0;
2921 struct obj_symbol *intsym = NULL;
2922 unsigned long symndx;
2923 ElfW(Sym) * extsym = 0;
2926 /* Attempt to find a value to use for this relocation. */
2928 symndx = ELFW(R_SYM) (rel->r_info);
2930 /* Note we've already checked for undefined symbols. */
2932 extsym = &symtab[symndx];
2933 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2934 /* Local symbols we look up in the local table to be sure
2935 we get the one that is really intended. */
2936 intsym = f->local_symtab[symndx];
2938 /* Others we look up in the hash table. */
2940 if (extsym->st_name)
2941 name = strtab + extsym->st_name;
2943 name = f->sections[extsym->st_shndx]->name;
2944 intsym = obj_find_symbol(f, name);
2947 value = obj_symbol_final_value(f, intsym);
2948 intsym->referenced = 1;
2950 #if SHT_RELM == SHT_RELA
2951 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2952 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2953 if (!extsym || !extsym->st_name ||
2954 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2956 value += rel->r_addend;
2960 switch (arch_apply_relocation
2961 (f, targsec, symsec, intsym, rel, value)) {
2965 case obj_reloc_overflow:
2966 errmsg = "Relocation overflow";
2968 case obj_reloc_dangerous:
2969 errmsg = "Dangerous relocation";
2971 case obj_reloc_unhandled:
2972 errmsg = "Unhandled relocation";
2975 bb_error_msg("%s of type %ld for %s", errmsg,
2976 (long) ELFW(R_TYPE) (rel->r_info),
2977 strtab + extsym->st_name);
2979 bb_error_msg("%s of type %ld", errmsg,
2980 (long) ELFW(R_TYPE) (rel->r_info));
2988 /* Finally, take care of the patches. */
2990 if (f->string_patches) {
2991 struct obj_string_patch *p;
2992 struct obj_section *strsec;
2993 ElfW(Addr) strsec_base;
2994 strsec = obj_find_section(f, ".kstrtab");
2995 strsec_base = strsec->header.sh_addr;
2997 for (p = f->string_patches; p; p = p->next) {
2998 struct obj_section *targsec = f->sections[p->reloc_secidx];
2999 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3000 = strsec_base + p->string_offset;
3004 if (f->symbol_patches) {
3005 struct obj_symbol_patch *p;
3007 for (p = f->symbol_patches; p; p = p->next) {
3008 struct obj_section *targsec = f->sections[p->reloc_secidx];
3009 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3010 = obj_symbol_final_value(f, p->sym);
3017 static int obj_create_image(struct obj_file *f, char *image)
3019 struct obj_section *sec;
3020 ElfW(Addr) base = f->baseaddr;
3022 for (sec = f->load_order; sec; sec = sec->load_next) {
3025 if (sec->contents == 0 || sec->header.sh_size == 0)
3028 secimg = image + (sec->header.sh_addr - base);
3030 /* Note that we allocated data for NOBITS sections earlier. */
3031 memcpy(secimg, sec->contents, sec->header.sh_size);
3037 /*======================================================================*/
3039 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3042 ElfW(Shdr) * section_headers;
3046 /* Read the file header. */
3048 f = arch_new_file();
3049 memset(f, 0, sizeof(*f));
3050 f->symbol_cmp = strcmp;
3051 f->symbol_hash = obj_elf_hash;
3052 f->load_order_search_start = &f->load_order;
3054 fseek(fp, 0, SEEK_SET);
3055 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3056 bb_perror_msg("error reading ELF header");
3060 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3061 || f->header.e_ident[EI_MAG1] != ELFMAG1
3062 || f->header.e_ident[EI_MAG2] != ELFMAG2
3063 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3064 bb_error_msg("not an ELF file");
3067 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3068 || f->header.e_ident[EI_DATA] != ELFDATAM
3069 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3070 || !MATCH_MACHINE(f->header.e_machine)) {
3071 bb_error_msg("ELF file not for this architecture");
3074 if (f->header.e_type != ET_REL) {
3075 bb_error_msg("ELF file not a relocatable object");
3079 /* Read the section headers. */
3081 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3082 bb_error_msg("section header size mismatch: %lu != %lu",
3083 (unsigned long) f->header.e_shentsize,
3084 (unsigned long) sizeof(ElfW(Shdr)));
3088 shnum = f->header.e_shnum;
3089 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3090 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3092 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3093 fseek(fp, f->header.e_shoff, SEEK_SET);
3094 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3095 bb_perror_msg("error reading ELF section headers");
3099 /* Read the section data. */
3101 for (i = 0; i < shnum; ++i) {
3102 struct obj_section *sec;
3104 f->sections[i] = sec = arch_new_section();
3105 memset(sec, 0, sizeof(*sec));
3107 sec->header = section_headers[i];
3110 if(sec->header.sh_size) switch (sec->header.sh_type) {
3119 if (!loadprogbits) {
3120 sec->contents = NULL;
3127 if (sec->header.sh_size > 0) {
3128 sec->contents = xmalloc(sec->header.sh_size);
3129 fseek(fp, sec->header.sh_offset, SEEK_SET);
3130 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3131 bb_perror_msg("error reading ELF section data");
3135 sec->contents = NULL;
3139 #if SHT_RELM == SHT_REL
3141 bb_error_msg("RELA relocations not supported on this architecture");
3145 bb_error_msg("REL relocations not supported on this architecture");
3150 if (sec->header.sh_type >= SHT_LOPROC) {
3151 /* Assume processor specific section types are debug
3152 info and can safely be ignored. If this is ever not
3153 the case (Hello MIPS?), don't put ifdefs here but
3154 create an arch_load_proc_section(). */
3158 bb_error_msg("can't handle sections of type %ld",
3159 (long) sec->header.sh_type);
3164 /* Do what sort of interpretation as needed by each section. */
3166 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3168 for (i = 0; i < shnum; ++i) {
3169 struct obj_section *sec = f->sections[i];
3170 sec->name = shstrtab + sec->header.sh_name;
3173 for (i = 0; i < shnum; ++i) {
3174 struct obj_section *sec = f->sections[i];
3176 /* .modinfo should be contents only but gcc has no attribute for that.
3177 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3179 if (strcmp(sec->name, ".modinfo") == 0)
3180 sec->header.sh_flags &= ~SHF_ALLOC;
3182 if (sec->header.sh_flags & SHF_ALLOC)
3183 obj_insert_section_load_order(f, sec);
3185 switch (sec->header.sh_type) {
3188 unsigned long nsym, j;
3192 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3193 bb_error_msg("symbol size mismatch: %lu != %lu",
3194 (unsigned long) sec->header.sh_entsize,
3195 (unsigned long) sizeof(ElfW(Sym)));
3199 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3200 strtab = f->sections[sec->header.sh_link]->contents;
3201 sym = (ElfW(Sym) *) sec->contents;
3203 /* Allocate space for a table of local symbols. */
3204 j = f->local_symtab_size = sec->header.sh_info;
3205 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3207 /* Insert all symbols into the hash table. */
3208 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3209 ElfW(Addr) val = sym->st_value;
3212 name = strtab + sym->st_name;
3213 else if (sym->st_shndx < shnum)
3214 name = f->sections[sym->st_shndx]->name;
3218 #if defined(__SH5__)
3220 * For sh64 it is possible that the target of a branch
3221 * requires a mode switch (32 to 16 and back again).
3223 * This is implied by the lsb being set in the target
3224 * address for SHmedia mode and clear for SHcompact.
3226 val |= sym->st_other & 4;
3229 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3236 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3237 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3238 (unsigned long) sec->header.sh_entsize,
3239 (unsigned long) sizeof(ElfW(RelM)));
3243 /* XXX Relocation code from modutils-2.3.19 is not here.
3244 * Why? That's about 20 lines of code from obj/obj_load.c,
3245 * which gets done in a second pass through the sections.
3246 * This BusyBox insmod does similar work in obj_relocate(). */
3253 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3255 * load the unloaded sections directly into the memory allocated by
3256 * kernel for the module
3259 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3261 ElfW(Addr) base = f->baseaddr;
3262 struct obj_section* sec;
3264 for (sec = f->load_order; sec; sec = sec->load_next) {
3266 /* section already loaded? */
3267 if (sec->contents != NULL)
3270 if (sec->header.sh_size == 0)
3273 sec->contents = imagebase + (sec->header.sh_addr - base);
3274 fseek(fp, sec->header.sh_offset, SEEK_SET);
3275 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3276 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3285 static void hide_special_symbols(struct obj_file *f)
3287 static const char *const specials[] = {
3288 SPFX "cleanup_module",
3290 SPFX "kernel_version",
3294 struct obj_symbol *sym;
3295 const char *const *p;
3297 for (p = specials; *p; ++p)
3298 if ((sym = obj_find_symbol(f, *p)) != NULL)
3300 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3304 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3305 static int obj_gpl_license(struct obj_file *f, const char **license)
3307 struct obj_section *sec;
3308 /* This list must match *exactly* the list of allowable licenses in
3309 * linux/include/linux/module.h. Checking for leading "GPL" will not
3310 * work, somebody will use "GPL sucks, this is proprietary".
3312 static const char *gpl_licenses[] = {
3315 "GPL and additional rights",
3320 if ((sec = obj_find_section(f, ".modinfo"))) {
3321 const char *value, *ptr, *endptr;
3322 ptr = sec->contents;
3323 endptr = ptr + sec->header.sh_size;
3324 while (ptr < endptr) {
3325 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3329 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3330 if (strcmp(value+1, gpl_licenses[i]) == 0)
3335 if (strchr(ptr, '\0'))
3336 ptr = strchr(ptr, '\0') + 1;
3344 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3345 #define TAINT_PROPRIETORY_MODULE (1<<0)
3346 #define TAINT_FORCED_MODULE (1<<1)
3347 #define TAINT_UNSAFE_SMP (1<<2)
3348 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3350 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3351 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3355 static int first = 1;
3356 if (fd < 0 && !kernel_has_tainted)
3357 return; /* New modutils on old kernel */
3358 printf("Warning: loading %s will taint the kernel: %s%s\n",
3359 m_name, text1, text2);
3361 printf(" See %s for information about tainted modules\n", TAINT_URL);
3365 read(fd, buf, sizeof(buf)-1);
3366 buf[sizeof(buf)-1] = '\0';
3367 oldval = strtoul(buf, NULL, 10);
3368 sprintf(buf, "%d\n", oldval | taint);
3369 write(fd, buf, strlen(buf));
3373 /* Check if loading this module will taint the kernel. */
3374 static void check_tainted_module(struct obj_file *f, char *m_name)
3376 static const char tainted_file[] = TAINT_FILENAME;
3377 int fd, kernel_has_tainted;
3380 kernel_has_tainted = 1;
3381 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3382 if (errno == ENOENT)
3383 kernel_has_tainted = 0;
3384 else if (errno == EACCES)
3385 kernel_has_tainted = 1;
3387 perror(tainted_file);
3388 kernel_has_tainted = 0;
3392 switch (obj_gpl_license(f, &ptr)) {
3396 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3399 /* The module has a non-GPL license so we pretend that the
3400 * kernel always has a taint flag to get a warning even on
3401 * kernels without the proc flag.
3403 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3406 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3410 if (flag_force_load)
3411 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3416 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3417 #define check_tainted_module(x, y) do { } while(0);
3418 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3420 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3421 /* add module source, timestamp, kernel version and a symbol for the
3422 * start of some sections. this info is used by ksymoops to do better
3426 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3428 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3429 return new_get_module_version(f, str);
3430 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3431 strncpy(str, "???", sizeof(str));
3433 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3436 /* add module source, timestamp, kernel version and a symbol for the
3437 * start of some sections. this info is used by ksymoops to do better
3441 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3444 static const char symprefix[] = "__insmod_";
3445 struct obj_section *sec;
3446 struct obj_symbol *sym;
3447 char *name, *absolute_filename;
3448 char str[STRVERSIONLEN], real[PATH_MAX];
3449 int i, l, lm_name, lfilename, use_ksymtab, version;
3450 struct stat statbuf;
3452 static const char *section_names[] = {
3460 if (realpath(filename, real)) {
3461 absolute_filename = bb_xstrdup(real);
3464 int save_errno = errno;
3465 bb_error_msg("cannot get realpath for %s", filename);
3468 absolute_filename = bb_xstrdup(filename);
3471 lm_name = strlen(m_name);
3472 lfilename = strlen(absolute_filename);
3474 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3475 * are not to be exported. otherwise leave ksymtab alone for now, the
3476 * "export all symbols" compatibility code will export these symbols later.
3478 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3480 if ((sec = obj_find_section(f, ".this"))) {
3481 /* tag the module header with the object name, last modified
3482 * timestamp and module version. worst case for module version
3483 * is 0xffffff, decimal 16777215. putting all three fields in
3484 * one symbol is less readable but saves kernel space.
3486 l = sizeof(symprefix)+ /* "__insmod_" */
3487 lm_name+ /* module name */
3489 lfilename+ /* object filename */
3491 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3493 8+ /* version in dec */
3496 if (stat(absolute_filename, &statbuf) != 0)
3497 statbuf.st_mtime = 0;
3498 version = get_module_version(f, str); /* -1 if not found */
3499 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3500 symprefix, m_name, absolute_filename,
3501 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3503 sym = obj_add_symbol(f, name, -1,
3504 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3505 sec->idx, sec->header.sh_addr, 0);
3507 new_add_ksymtab(f, sym);
3509 free(absolute_filename);
3510 #ifdef _NOT_SUPPORTED_
3511 /* record where the persistent data is going, same address as previous symbol */
3514 l = sizeof(symprefix)+ /* "__insmod_" */
3515 lm_name+ /* module name */
3517 strlen(f->persist)+ /* data store */
3520 snprintf(name, l, "%s%s_P%s",
3521 symprefix, m_name, f->persist);
3522 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3523 sec->idx, sec->header.sh_addr, 0);
3525 new_add_ksymtab(f, sym);
3527 #endif /* _NOT_SUPPORTED_ */
3528 /* tag the desired sections if size is non-zero */
3530 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3531 if ((sec = obj_find_section(f, section_names[i])) &&
3532 sec->header.sh_size) {
3533 l = sizeof(symprefix)+ /* "__insmod_" */
3534 lm_name+ /* module name */
3536 strlen(sec->name)+ /* section name */
3538 8+ /* length in dec */
3541 snprintf(name, l, "%s%s_S%s_L%ld",
3542 symprefix, m_name, sec->name,
3543 (long)sec->header.sh_size);
3544 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3545 sec->idx, sec->header.sh_addr, 0);
3547 new_add_ksymtab(f, sym);
3551 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3553 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3554 static void print_load_map(struct obj_file *f)
3556 struct obj_symbol *sym;
3557 struct obj_symbol **all, **p;
3558 struct obj_section *sec;
3559 int i, nsyms, *loaded;
3561 /* Report on the section layout. */
3563 printf("Sections: Size %-*s Align\n",
3564 (int) (2 * sizeof(void *)), "Address");
3566 for (sec = f->load_order; sec; sec = sec->load_next) {
3570 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3575 printf("%-15s %08lx %0*lx 2**%d\n",
3577 (long)sec->header.sh_size,
3578 (int) (2 * sizeof(void *)),
3579 (long)sec->header.sh_addr,
3582 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3583 /* Quick reference which section indicies are loaded. */
3585 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3587 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3589 /* Collect the symbols we'll be listing. */
3591 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3592 for (sym = f->symtab[i]; sym; sym = sym->next)
3593 if (sym->secidx <= SHN_HIRESERVE
3594 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3597 all = alloca(nsyms * sizeof(struct obj_symbol *));
3599 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3600 for (sym = f->symtab[i]; sym; sym = sym->next)
3601 if (sym->secidx <= SHN_HIRESERVE
3602 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3605 /* And list them. */
3606 printf("\nSymbols:\n");
3607 for (p = all; p < all + nsyms; ++p) {
3609 unsigned long value;
3612 if (sym->secidx == SHN_ABS) {
3615 } else if (sym->secidx == SHN_UNDEF) {
3619 sec = f->sections[sym->secidx];
3621 if (sec->header.sh_type == SHT_NOBITS)
3623 else if (sec->header.sh_flags & SHF_ALLOC) {
3624 if (sec->header.sh_flags & SHF_EXECINSTR)
3626 else if (sec->header.sh_flags & SHF_WRITE)
3631 value = sym->value + sec->header.sh_addr;
3634 if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
3635 type = tolower(type);
3637 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3645 extern int insmod_main( int argc, char **argv)
3651 unsigned long m_size;
3656 int exit_status = EXIT_FAILURE;
3658 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3659 struct utsname uts_info;
3660 char m_strversion[STRVERSIONLEN];
3661 int m_version, m_crcs;
3663 #ifdef CONFIG_FEATURE_CLEAN_UP
3668 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3669 int flag_print_load_map = 0;
3672 struct utsname myuname;
3674 /* Parse any options */
3675 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3676 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
3678 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
3682 case 'f': /* force loading */
3683 flag_force_load = 1;
3685 case 'k': /* module loaded by kerneld, auto-cleanable */
3688 case 's': /* log to syslog */
3689 /* log to syslog -- not supported */
3690 /* but kernel needs this for request_module(), */
3691 /* as this calls: modprobe -k -s -- <module> */
3692 /* so silently ignore this flag */
3694 case 'v': /* verbose output */
3697 case 'q': /* silent */
3700 case 'x': /* do not export externs */
3703 case 'o': /* name the output module */
3705 m_name = bb_xstrdup(optarg);
3707 case 'L': /* Stub warning */
3708 /* This is needed for compatibility with modprobe.
3709 * In theory, this does locking, but we don't do
3710 * that. So be careful and plan your life around not
3711 * loading the same module 50 times concurrently. */
3713 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3714 case 'm': /* print module load map */
3715 flag_print_load_map = 1;
3723 if (argv[optind] == NULL) {
3727 /* Grab the module name */
3728 tmp1 = bb_xstrdup(argv[optind]);
3729 tmp = basename(tmp1);
3732 if (uname(&myuname) == 0) {
3733 if (myuname.release[0] == '2') {
3734 k_version = myuname.release[2] - '0';
3738 #if defined(CONFIG_FEATURE_2_6_MODULES)
3739 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
3740 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
3746 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3752 #if defined(CONFIG_FEATURE_2_6_MODULES)
3754 bb_xasprintf(&m_fullName, "%s.ko", tmp);
3757 bb_xasprintf(&m_fullName, "%s.o", tmp);
3763 tmp1 = 0; /* flag for free(m_name) before exit() */
3766 /* Get a filedesc for the module. Check we we have a complete path */
3767 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3768 (fp = fopen(argv[optind], "r")) == NULL) {
3769 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3770 * but do not error out yet if we fail to find it... */
3771 if (k_version) { /* uname succeedd */
3774 char real_module_dir[FILENAME_MAX];
3776 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
3777 /* Jump through hoops in case /lib/modules/`uname -r`
3778 * is a symlink. We do not want recursive_action to
3779 * follow symlinks, but we do want to follow the
3780 * /lib/modules/`uname -r` dir, So resolve it ourselves
3781 * if it is a link... */
3782 if (realpath (tmdn, real_module_dir) == NULL)
3785 module_dir = real_module_dir;
3786 recursive_action(module_dir, TRUE, FALSE, FALSE,
3787 check_module_name_match, 0, m_fullName);
3791 /* Check if we have found anything yet */
3792 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
3794 char module_dir[FILENAME_MAX];
3798 if (realpath (_PATH_MODULES, module_dir) == NULL)
3799 strcpy(module_dir, _PATH_MODULES);
3800 /* No module found under /lib/modules/`uname -r`, this
3801 * time cast the net a bit wider. Search /lib/modules/ */
3802 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3803 check_module_name_match, 0, m_fullName))
3806 || ((fp = fopen(m_filename, "r")) == NULL))
3808 bb_error_msg("%s: no module by that name found", m_fullName);
3812 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
3815 m_filename = bb_xstrdup(argv[optind]);
3818 printf("Using %s\n", m_filename);
3820 #ifdef CONFIG_FEATURE_2_6_MODULES
3824 argv[optind + 1] = m_filename;
3825 return insmod_ng_main(argc - optind, argv + optind);
3829 if ((f = obj_load(fp, LOADBITS)) == NULL)
3830 bb_perror_msg_and_die("Could not load the module");
3832 if (get_modinfo_value(f, "kernel_version") == NULL)
3837 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3838 /* Version correspondence? */
3840 if (uname(&uts_info) < 0)
3841 uts_info.release[0] = '\0';
3842 if (m_has_modinfo) {
3843 m_version = new_get_module_version(f, m_strversion);
3844 if (m_version == -1) {
3845 bb_error_msg("couldn't find the kernel version the module was "
3851 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3852 if (flag_force_load) {
3853 bb_error_msg("Warning: kernel-module version mismatch\n"
3854 "\t%s was compiled for kernel version %s\n"
3855 "\twhile this kernel is version %s",
3856 m_filename, m_strversion, uts_info.release);
3858 bb_error_msg("kernel-module version mismatch\n"
3859 "\t%s was compiled for kernel version %s\n"
3860 "\twhile this kernel is version %s.",
3861 m_filename, m_strversion, uts_info.release);
3867 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3869 if (!query_module(NULL, 0, NULL, 0, NULL)) {
3870 if (!new_get_kernel_symbols())
3872 k_crcs = new_is_kernel_checksummed();
3874 bb_error_msg("Not configured to support old kernels");
3878 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3881 m_crcs = new_is_module_checksummed(f);
3883 if (m_crcs != k_crcs)
3884 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3885 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3887 /* Let the module know about the kernel symbols. */
3888 add_kernel_symbols(f);
3890 /* Allocate common symbols, symbol tables, and string tables. */
3892 if (!new_create_this_module(f, m_name))
3897 if (!obj_check_undefineds(f)) {
3900 obj_allocate_commons(f);
3901 check_tainted_module(f, m_name);
3903 /* done with the module name, on to the optional var=value arguments */
3906 if (optind < argc) {
3907 if (!new_process_module_arguments(f, argc - optind, argv + optind))
3914 hide_special_symbols(f);
3916 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3917 add_ksymoops_symbols(f, m_filename, m_name);
3918 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3920 new_create_module_ksymtab(f);
3922 /* Find current size of the module */
3923 m_size = obj_load_size(f);
3926 m_addr = create_module(m_name, m_size);
3927 if (m_addr == -1) switch (errno) {
3929 bb_error_msg("A module named %s already exists", m_name);
3932 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3936 bb_perror_msg("create_module: %s", m_name);
3942 * the PROGBITS section was not loaded by the obj_load
3943 * now we can load them directly into the kernel memory
3945 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3946 delete_module(m_name);
3951 if (!obj_relocate(f, m_addr)) {
3952 delete_module(m_name);
3956 if (!new_init_module(m_name, f, m_size))
3958 delete_module(m_name);
3962 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3963 if(flag_print_load_map)
3967 exit_status = EXIT_SUCCESS;
3970 #ifdef CONFIG_FEATURE_CLEAN_UP
3980 return(exit_status);
3987 #ifdef CONFIG_FEATURE_2_6_MODULES
3989 #include <sys/mman.h>
3990 #include <asm/unistd.h>
3991 #include <sys/syscall.h>
3993 /* We use error numbers in a loose translation... */
3994 static const char *moderror(int err)
3998 return "Invalid module format";
4000 return "Unknown symbol in module";
4002 return "Module has wrong symbol version";
4004 return "Invalid parameters";
4006 return strerror(err);
4010 extern int insmod_ng_main( int argc, char **argv)
4018 char *filename, *options = bb_xstrdup("");
4026 /* Rest is options */
4027 for (i = 2; i < argc; i++) {
4028 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4029 /* Spaces handled by "" pairs, but no way of escaping quotes */
4030 if (strchr(argv[i], ' ')) {
4031 strcat(options, "\"");
4032 strcat(options, argv[i]);
4033 strcat(options, "\"");
4035 strcat(options, argv[i]);
4037 strcat(options, " ");
4040 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4041 bb_perror_msg_and_die("cannot open module `%s'", filename);
4046 map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
4047 if (map == MAP_FAILED) {
4048 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4051 ret = syscall(__NR_init_module, map, len, options);
4053 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4054 filename, moderror(errno), ret);