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
147 #if defined(__H8300H__) || defined(__H8300S__)
148 #define MATCH_MACHINE(x) (x == EM_H8_300)
149 #define SHT_RELM SHT_RELA
150 #define Elf32_RelM Elf32_Rela
151 #define ELFCLASSM ELFCLASS32
152 #define CONFIG_USE_SINGLE
153 #define SYMBOL_PREFIX "_"
156 /* PA-RISC / HP-PA */
157 #if defined(__hppa__)
158 #define MATCH_MACHINE(x) (x == EM_PARISC)
159 #define SHT_RELM SHT_RELA
160 #if defined(__LP64__)
161 #define Elf64_RelM Elf64_Rela
162 #define ELFCLASSM ELFCLASS64
164 #define Elf32_RelM Elf32_Rela
165 #define ELFCLASSM ELFCLASS32
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(__powerpc64__)
222 #define MATCH_MACHINE(x) (x == EM_PPC64)
223 #define SHT_RELM SHT_RELA
224 #define Elf64_RelM Elf64_Rela
225 #define ELFCLASSM ELFCLASS64
226 #elif defined(__powerpc__)
227 #define MATCH_MACHINE(x) (x == EM_PPC)
228 #define SHT_RELM SHT_RELA
229 #define Elf32_RelM Elf32_Rela
230 #define ELFCLASSM ELFCLASS32
231 #define CONFIG_USE_PLT_ENTRIES
232 #define CONFIG_PLT_ENTRY_SIZE 16
233 #define CONFIG_USE_PLT_LIST
234 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
235 #define CONFIG_USE_LIST
236 #define ARCHDATAM "__ftr_fixup"
240 #if defined(__s390__)
241 #define MATCH_MACHINE(x) (x == EM_S390)
242 #define SHT_RELM SHT_RELA
243 #define Elf32_RelM Elf32_Rela
244 #define ELFCLASSM ELFCLASS32
245 #define CONFIG_USE_PLT_ENTRIES
246 #define CONFIG_PLT_ENTRY_SIZE 8
247 #define CONFIG_USE_GOT_ENTRIES
248 #define CONFIG_GOT_ENTRY_SIZE 8
249 #define CONFIG_USE_SINGLE
254 #define MATCH_MACHINE(x) (x == EM_SH)
255 #define SHT_RELM SHT_RELA
256 #define Elf32_RelM Elf32_Rela
257 #define ELFCLASSM ELFCLASS32
258 #define CONFIG_USE_GOT_ENTRIES
259 #define CONFIG_GOT_ENTRY_SIZE 4
260 #define CONFIG_USE_SINGLE
261 /* the SH changes have only been tested in =little endian= mode */
262 /* I'm not sure about big endian, so let's warn: */
263 #if defined(__sh__) && defined(__BIG_ENDIAN__)
264 #error insmod.c may require changes for use on big endian SH
266 /* it may or may not work on the SH1/SH2... Error on those also */
267 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
268 #error insmod.c may require changes for SH1 or SH2 use
273 #if defined(__sparc__)
274 #define MATCH_MACHINE(x) (x == EM_SPARC)
275 #define SHT_RELM SHT_RELA
276 #define Elf32_RelM Elf32_Rela
277 #define ELFCLASSM ELFCLASS32
281 #if defined (__v850e__)
282 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
283 #define SHT_RELM SHT_RELA
284 #define Elf32_RelM Elf32_Rela
285 #define ELFCLASSM ELFCLASS32
286 #define CONFIG_USE_PLT_ENTRIES
287 #define CONFIG_PLT_ENTRY_SIZE 8
288 #define CONFIG_USE_SINGLE
289 #ifndef EM_CYGNUS_V850 /* grumble */
290 #define EM_CYGNUS_V850 0x9080
292 #define SYMBOL_PREFIX "_"
296 #if defined(__x86_64__)
297 #define MATCH_MACHINE(x) (x == EM_X86_64)
298 #define SHT_RELM SHT_RELA
299 #define CONFIG_USE_GOT_ENTRIES
300 #define CONFIG_GOT_ENTRY_SIZE 8
301 #define CONFIG_USE_SINGLE
302 #define Elf64_RelM Elf64_Rela
303 #define ELFCLASSM ELFCLASS64
307 #error Sorry, but insmod.c does not yet support this architecture...
311 //----------------------------------------------------------------------------
312 //--------modutils module.h, lines 45-242
313 //----------------------------------------------------------------------------
315 /* Definitions for the Linux module syscall interface.
316 Copyright 1996, 1997 Linux International.
318 Contributed by Richard Henderson <rth@tamu.edu>
320 This file is part of the Linux modutils.
322 This program is free software; you can redistribute it and/or modify it
323 under the terms of the GNU General Public License as published by the
324 Free Software Foundation; either version 2 of the License, or (at your
325 option) any later version.
327 This program is distributed in the hope that it will be useful, but
328 WITHOUT ANY WARRANTY; without even the implied warranty of
329 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
330 General Public License for more details.
332 You should have received a copy of the GNU General Public License
333 along with this program; if not, write to the Free Software Foundation,
334 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
337 #ifndef MODUTILS_MODULE_H
338 static const int MODUTILS_MODULE_H = 1;
340 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
342 /*======================================================================*/
343 /* For sizeof() which are related to the module platform and not to the
344 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
346 #define tgt_sizeof_char sizeof(char)
347 #define tgt_sizeof_short sizeof(short)
348 #define tgt_sizeof_int sizeof(int)
349 #define tgt_sizeof_long sizeof(long)
350 #define tgt_sizeof_char_p sizeof(char *)
351 #define tgt_sizeof_void_p sizeof(void *)
352 #define tgt_long long
354 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
355 #undef tgt_sizeof_long
356 #undef tgt_sizeof_char_p
357 #undef tgt_sizeof_void_p
359 static const int tgt_sizeof_long = 8;
360 static const int tgt_sizeof_char_p = 8;
361 static const int tgt_sizeof_void_p = 8;
362 #define tgt_long long long
365 /*======================================================================*/
366 /* The structures used in Linux 2.1. */
368 /* Note: new_module_symbol does not use tgt_long intentionally */
369 struct new_module_symbol
375 struct new_module_persist;
377 struct new_module_ref
379 unsigned tgt_long dep; /* kernel addresses */
380 unsigned tgt_long ref;
381 unsigned tgt_long next_ref;
386 unsigned tgt_long size_of_struct; /* == sizeof(module) */
387 unsigned tgt_long next;
388 unsigned tgt_long name;
389 unsigned tgt_long size;
392 unsigned tgt_long flags; /* AUTOCLEAN et al */
397 unsigned tgt_long syms;
398 unsigned tgt_long deps;
399 unsigned tgt_long refs;
400 unsigned tgt_long init;
401 unsigned tgt_long cleanup;
402 unsigned tgt_long ex_table_start;
403 unsigned tgt_long ex_table_end;
405 unsigned tgt_long gp;
407 /* Everything after here is extension. */
408 unsigned tgt_long persist_start;
409 unsigned tgt_long persist_end;
410 unsigned tgt_long can_unload;
411 unsigned tgt_long runsize;
412 const char *kallsyms_start; /* All symbols for kernel debugging */
413 const char *kallsyms_end;
414 const char *archdata_start; /* arch specific data for module */
415 const char *archdata_end;
416 const char *kernel_data; /* Reserved for kernel internal use */
420 #define ARCHDATA_SEC_NAME ARCHDATAM
422 #define ARCHDATA_SEC_NAME "__archdata"
424 #define KALLSYMS_SEC_NAME "__kallsyms"
427 struct new_module_info
435 /* Bits of module.flags. */
436 static const int NEW_MOD_RUNNING = 1;
437 static const int NEW_MOD_DELETED = 2;
438 static const int NEW_MOD_AUTOCLEAN = 4;
439 static const int NEW_MOD_VISITED = 8;
440 static const int NEW_MOD_USED_ONCE = 16;
442 int init_module(const char *name, const struct new_module *);
443 int query_module(const char *name, int which, void *buf,
444 size_t bufsize, size_t *ret);
446 /* Values for query_module's which. */
448 static const int QM_MODULES = 1;
449 static const int QM_DEPS = 2;
450 static const int QM_REFS = 3;
451 static const int QM_SYMBOLS = 4;
452 static const int QM_INFO = 5;
454 /*======================================================================*/
455 /* The system calls unchanged between 2.0 and 2.1. */
457 unsigned long create_module(const char *, size_t);
458 int delete_module(const char *);
461 #endif /* module.h */
463 //----------------------------------------------------------------------------
464 //--------end of modutils module.h
465 //----------------------------------------------------------------------------
469 //----------------------------------------------------------------------------
470 //--------modutils obj.h, lines 253-462
471 //----------------------------------------------------------------------------
473 /* Elf object file loading and relocation routines.
474 Copyright 1996, 1997 Linux International.
476 Contributed by Richard Henderson <rth@tamu.edu>
478 This file is part of the Linux modutils.
480 This program is free software; you can redistribute it and/or modify it
481 under the terms of the GNU General Public License as published by the
482 Free Software Foundation; either version 2 of the License, or (at your
483 option) any later version.
485 This program is distributed in the hope that it will be useful, but
486 WITHOUT ANY WARRANTY; without even the implied warranty of
487 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
488 General Public License for more details.
490 You should have received a copy of the GNU General Public License
491 along with this program; if not, write to the Free Software Foundation,
492 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
495 #ifndef MODUTILS_OBJ_H
496 static const int MODUTILS_OBJ_H = 1;
498 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
500 /* The relocatable object is manipulated using elfin types. */
506 #if __BYTE_ORDER == __LITTLE_ENDIAN
507 #define ELFDATAM ELFDATA2LSB
508 #elif __BYTE_ORDER == __BIG_ENDIAN
509 #define ELFDATAM ELFDATA2MSB
513 # if ELFCLASSM == ELFCLASS32
514 # define ElfW(x) Elf32_ ## x
515 # define ELFW(x) ELF32_ ## x
517 # define ElfW(x) Elf64_ ## x
518 # define ELFW(x) ELF64_ ## x
522 /* For some reason this is missing from some ancient C libraries.... */
523 #ifndef ELF32_ST_INFO
524 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
527 #ifndef ELF64_ST_INFO
528 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
531 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
532 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
533 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
534 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
535 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
537 struct obj_string_patch;
538 struct obj_symbol_patch;
545 struct obj_section *load_next;
551 struct obj_symbol *next; /* hash table link */
555 int secidx; /* the defining section index/module */
557 int ksymidx; /* for export to the kernel symtab */
558 int referenced; /* actually used in the link */
561 /* Hardcode the hash table size. We shouldn't be needing so many
562 symbols that we begin to degrade performance, and we get a big win
563 by giving the compiler a constant divisor. */
565 #define HASH_BUCKETS 521
571 struct obj_section **sections;
572 struct obj_section *load_order;
573 struct obj_section **load_order_search_start;
574 struct obj_string_patch *string_patches;
575 struct obj_symbol_patch *symbol_patches;
576 int (*symbol_cmp)(const char *, const char *);
577 unsigned long (*symbol_hash)(const char *);
578 unsigned long local_symtab_size;
579 struct obj_symbol **local_symtab;
580 struct obj_symbol *symtab[HASH_BUCKETS];
591 struct obj_string_patch
593 struct obj_string_patch *next;
595 ElfW(Addr) reloc_offset;
596 ElfW(Addr) string_offset;
599 struct obj_symbol_patch
601 struct obj_symbol_patch *next;
603 ElfW(Addr) reloc_offset;
604 struct obj_symbol *sym;
608 /* Generic object manipulation routines. */
610 static unsigned long obj_elf_hash(const char *);
612 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
614 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
617 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
618 struct obj_symbol *sym);
620 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
621 static void obj_set_symbol_compare(struct obj_file *f,
622 int (*cmp)(const char *, const char *),
623 unsigned long (*hash)(const char *));
626 static struct obj_section *obj_find_section (struct obj_file *f,
629 static void obj_insert_section_load_order (struct obj_file *f,
630 struct obj_section *sec);
632 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
637 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
642 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
644 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
647 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
648 struct obj_symbol *sym);
650 static int obj_check_undefineds(struct obj_file *f);
652 static void obj_allocate_commons(struct obj_file *f);
654 static unsigned long obj_load_size (struct obj_file *f);
656 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
658 static struct obj_file *obj_load(FILE *f, int loadprogbits);
660 static int obj_create_image (struct obj_file *f, char *image);
662 /* Architecture specific manipulation routines. */
664 static struct obj_file *arch_new_file (void);
666 static struct obj_section *arch_new_section (void);
668 static struct obj_symbol *arch_new_symbol (void);
670 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
671 struct obj_section *targsec,
672 struct obj_section *symsec,
673 struct obj_symbol *sym,
674 ElfW(RelM) *rel, ElfW(Addr) value);
676 static void arch_create_got (struct obj_file *f);
678 static int obj_gpl_license(struct obj_file *f, const char **license);
681 //----------------------------------------------------------------------------
682 //--------end of modutils obj.h
683 //----------------------------------------------------------------------------
686 /* SPFX is always a string, so it can be concatenated to string constants. */
688 #define SPFX SYMBOL_PREFIX
694 #define _PATH_MODULES "/lib/modules"
695 static const int STRVERSIONLEN = 32;
697 /*======================================================================*/
699 static int flag_force_load = 0;
700 static int flag_autoclean = 0;
701 static int flag_verbose = 0;
702 static int flag_quiet = 0;
703 static int flag_export = 1;
706 /*======================================================================*/
708 #if defined(CONFIG_USE_LIST)
710 struct arch_list_entry
712 struct arch_list_entry *next;
713 CONFIG_LIST_ARCHTYPE addend;
720 #if defined(CONFIG_USE_SINGLE)
722 struct arch_single_entry
731 #if defined(__mips__)
734 struct mips_hi16 *next;
741 struct obj_file root;
742 #if defined(CONFIG_USE_PLT_ENTRIES)
743 struct obj_section *plt;
745 #if defined(CONFIG_USE_GOT_ENTRIES)
746 struct obj_section *got;
748 #if defined(__mips__)
749 struct mips_hi16 *mips_hi16_list;
754 struct obj_symbol root;
755 #if defined(CONFIG_USE_PLT_ENTRIES)
756 #if defined(CONFIG_USE_PLT_LIST)
757 struct arch_list_entry *pltent;
759 struct arch_single_entry pltent;
762 #if defined(CONFIG_USE_GOT_ENTRIES)
763 struct arch_single_entry gotent;
768 struct external_module {
773 struct new_module_symbol *syms;
776 static struct new_module_symbol *ksyms;
777 static size_t nksyms;
779 static struct external_module *ext_modules;
780 static int n_ext_modules;
781 static int n_ext_modules_used;
782 extern int delete_module(const char *);
784 static char *m_filename;
785 static char *m_fullName;
789 /*======================================================================*/
792 static int check_module_name_match(const char *filename, struct stat *statbuf,
795 char *fullname = (char *) userdata;
797 if (fullname[0] == '\0')
800 char *tmp, *tmp1 = bb_xstrdup(filename);
801 tmp = bb_get_last_path_component(tmp1);
802 if (strcmp(tmp, fullname) == 0) {
804 /* Stop searching if we find a match */
805 m_filename = bb_xstrdup(filename);
814 /*======================================================================*/
816 static struct obj_file *arch_new_file(void)
819 f = xmalloc(sizeof(*f));
821 memset(f, 0, sizeof(*f));
826 static struct obj_section *arch_new_section(void)
828 return xmalloc(sizeof(struct obj_section));
831 static struct obj_symbol *arch_new_symbol(void)
833 struct arch_symbol *sym;
834 sym = xmalloc(sizeof(*sym));
836 memset(sym, 0, sizeof(*sym));
841 static enum obj_reloc
842 arch_apply_relocation(struct obj_file *f,
843 struct obj_section *targsec,
844 struct obj_section *symsec,
845 struct obj_symbol *sym,
846 ElfW(RelM) *rel, ElfW(Addr) v)
848 struct arch_file *ifile = (struct arch_file *) f;
849 enum obj_reloc ret = obj_reloc_ok;
850 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
851 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
852 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
853 struct arch_symbol *isym = (struct arch_symbol *) sym;
855 #if defined(CONFIG_USE_GOT_ENTRIES)
856 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
858 #if defined(CONFIG_USE_PLT_ENTRIES)
859 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
861 # if defined(CONFIG_USE_PLT_LIST)
862 struct arch_list_entry *pe;
864 struct arch_single_entry *pe;
868 switch (ELF_R_TYPE(rel->r_info)) {
883 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
884 * (which is .got) similar to branch,
885 * but is full 32 bits relative */
895 case R_ARM_GOTOFF: /* address relative to the got */
900 #elif defined(__cris__)
906 /* CRIS keeps the relocation value in the r_addend field and
907 * should not use whats in *loc at all
912 #elif defined(__H8300H__) || defined(__H8300S__)
915 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
916 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
927 if ((ElfW(Sword))v > 0x7fff ||
928 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
929 ret = obj_reloc_overflow;
931 *(unsigned short *)loc = v;
935 if ((ElfW(Sword))v > 0x7f ||
936 (ElfW(Sword))v < -(ElfW(Sword))0x80)
937 ret = obj_reloc_overflow;
939 *(unsigned char *)loc = v;
942 #elif defined(__i386__)
978 #elif defined(__mc68000__)
989 ret = obj_reloc_overflow;
996 ret = obj_reloc_overflow;
1003 if ((ElfW(Sword))v > 0x7f ||
1004 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1005 ret = obj_reloc_overflow;
1012 if ((ElfW(Sword))v > 0x7fff ||
1013 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1014 ret = obj_reloc_overflow;
1020 *(int *)loc = v - dot;
1023 case R_68K_GLOB_DAT:
1024 case R_68K_JMP_SLOT:
1028 case R_68K_RELATIVE:
1029 *(int *)loc += f->baseaddr;
1035 # ifdef R_68K_GOTOFF
1042 #elif defined(__mips__)
1053 ret = obj_reloc_dangerous;
1054 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1055 ret = obj_reloc_overflow;
1057 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1063 struct mips_hi16 *n;
1065 /* We cannot relocate this one now because we don't know the value
1066 of the carry we need to add. Save the information, and let LO16
1067 do the actual relocation. */
1068 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1071 n->next = ifile->mips_hi16_list;
1072 ifile->mips_hi16_list = n;
1078 unsigned long insnlo = *loc;
1079 ElfW(Addr) val, vallo;
1081 /* Sign extend the addend we extract from the lo insn. */
1082 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1084 if (ifile->mips_hi16_list != NULL) {
1085 struct mips_hi16 *l;
1087 l = ifile->mips_hi16_list;
1089 struct mips_hi16 *next;
1092 /* The value for the HI16 had best be the same. */
1093 assert(v == l->value);
1095 /* Do the HI16 relocation. Note that we actually don't
1096 need to know anything about the LO16 itself, except where
1097 to find the low 16 bits of the addend needed by the LO16. */
1100 ((insn & 0xffff) << 16) +
1104 /* Account for the sign extension that will happen in the
1111 insn = (insn & ~0xffff) | val;
1119 ifile->mips_hi16_list = NULL;
1122 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1124 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1129 #elif defined(__powerpc__)
1131 case R_PPC_ADDR16_HA:
1132 *(unsigned short *)loc = (v + 0x8000) >> 16;
1135 case R_PPC_ADDR16_HI:
1136 *(unsigned short *)loc = v >> 16;
1139 case R_PPC_ADDR16_LO:
1140 *(unsigned short *)loc = v;
1154 #elif defined(__s390__)
1157 *(unsigned int *) loc += v;
1160 *(unsigned short *) loc += v;
1163 *(unsigned char *) loc += v;
1167 *(unsigned int *) loc += v - dot;
1170 *(unsigned short *) loc += (v - dot) >> 1;
1173 *(unsigned short *) loc += v - dot;
1177 case R_390_PLT16DBL:
1178 /* find the plt entry and initialize it. */
1179 assert(isym != NULL);
1180 pe = (struct arch_single_entry *) &isym->pltent;
1181 assert(pe->allocated);
1182 if (pe->inited == 0) {
1183 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1184 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1186 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1193 /* Insert relative distance to target. */
1194 v = plt + pe->offset - dot;
1195 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1196 *(unsigned int *) loc = (unsigned int) v;
1197 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1198 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1201 case R_390_GLOB_DAT:
1202 case R_390_JMP_SLOT:
1206 case R_390_RELATIVE:
1207 *loc += f->baseaddr;
1212 *(unsigned long *) loc += got - dot;
1218 assert(isym != NULL);
1220 if (!isym->gotent.inited)
1222 isym->gotent.inited = 1;
1223 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1225 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1226 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1227 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1228 *(unsigned short *) loc += isym->gotent.offset;
1229 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1230 *(unsigned int *) loc += isym->gotent.offset;
1233 # ifndef R_390_GOTOFF32
1234 # define R_390_GOTOFF32 R_390_GOTOFF
1236 case R_390_GOTOFF32:
1241 #elif defined(__sh__)
1264 *loc = f->baseaddr + rel->r_addend;
1269 *loc = got - dot + rel->r_addend;
1280 # if defined(__SH5__)
1281 case R_SH_IMM_MEDLOW16:
1282 case R_SH_IMM_LOW16:
1286 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1290 * movi and shori have the format:
1292 * | op | imm | reg | reserved |
1293 * 31..26 25..10 9.. 4 3 .. 0
1295 * so we simply mask and or in imm.
1297 word = *loc & ~0x3fffc00;
1298 word |= (v & 0xffff) << 10;
1305 case R_SH_IMM_MEDLOW16_PCREL:
1306 case R_SH_IMM_LOW16_PCREL:
1310 word = *loc & ~0x3fffc00;
1314 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1317 word |= (v & 0xffff) << 10;
1323 # endif /* __SH5__ */
1325 #elif defined (__v850e__)
1331 /* We write two shorts instead of a long because even
1332 32-bit insns only need half-word alignment, but
1333 32-bit data needs to be long-word aligned. */
1334 v += ((unsigned short *)loc)[0];
1335 v += ((unsigned short *)loc)[1] << 16;
1336 ((unsigned short *)loc)[0] = v & 0xffff;
1337 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1340 case R_V850_22_PCREL:
1343 #elif defined(__x86_64__)
1353 *(unsigned int *) loc += v;
1356 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1357 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1362 *(signed int *) loc += v;
1366 *(unsigned short *) loc += v;
1370 *(unsigned char *) loc += v;
1374 *(unsigned int *) loc += v - dot;
1378 *(unsigned short *) loc += v - dot;
1382 *(unsigned char *) loc += v - dot;
1385 case R_X86_64_GLOB_DAT:
1386 case R_X86_64_JUMP_SLOT:
1390 case R_X86_64_RELATIVE:
1391 *loc += f->baseaddr;
1394 case R_X86_64_GOT32:
1395 case R_X86_64_GOTPCREL:
1398 assert(isym != NULL);
1399 if (!isym->gotent.reloc_done)
1401 isym->gotent.reloc_done = 1;
1402 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1404 /* XXX are these really correct? */
1405 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1406 *(unsigned int *) loc += v + isym->gotent.offset;
1408 *loc += isym->gotent.offset;
1415 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1416 ret = obj_reloc_unhandled;
1419 #if defined(CONFIG_USE_PLT_ENTRIES)
1423 /* find the plt entry and initialize it if necessary */
1424 assert(isym != NULL);
1426 #if defined(CONFIG_USE_PLT_LIST)
1427 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1435 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1437 /* generate some machine code */
1439 #if defined(__arm__)
1440 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1441 ip[1] = v; /* sym@ */
1443 #if defined(__powerpc__)
1444 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1445 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1446 ip[2] = 0x7d6903a6; /* mtctr r11 */
1447 ip[3] = 0x4e800420; /* bctr */
1449 #if defined (__v850e__)
1450 /* We have to trash a register, so we assume that any control
1451 transfer more than 21-bits away must be a function call
1452 (so we can use a call-clobbered register). */
1453 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1454 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1459 /* relative distance to target */
1461 /* if the target is too far away.... */
1462 #if defined (__arm__) || defined (__powerpc__)
1463 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1464 #elif defined (__v850e__)
1465 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1467 /* go via the plt */
1468 v = plt + pe->offset - dot;
1470 #if defined (__v850e__)
1475 ret = obj_reloc_dangerous;
1477 /* merge the offset into the instruction. */
1478 #if defined(__arm__)
1479 /* Convert to words. */
1482 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1484 #if defined(__powerpc__)
1485 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1487 #if defined (__v850e__)
1488 /* We write two shorts instead of a long because even 32-bit insns
1489 only need half-word alignment, but the 32-bit data write needs
1490 to be long-word aligned. */
1491 ((unsigned short *)loc)[0] =
1492 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1493 | ((v >> 16) & 0x3f); /* offs high part */
1494 ((unsigned short *)loc)[1] =
1495 (v & 0xffff); /* offs low part */
1498 #endif /* CONFIG_USE_PLT_ENTRIES */
1500 #if defined(CONFIG_USE_GOT_ENTRIES)
1503 assert(isym != NULL);
1504 /* needs an entry in the .got: set it, once */
1505 if (!isym->gotent.inited) {
1506 isym->gotent.inited = 1;
1507 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1509 /* make the reloc with_respect_to_.got */
1511 *loc += isym->gotent.offset + rel->r_addend;
1512 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1513 *loc += isym->gotent.offset;
1517 #endif /* CONFIG_USE_GOT_ENTRIES */
1524 #if defined(CONFIG_USE_LIST)
1526 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1527 int offset, int size)
1529 struct arch_list_entry *pe;
1531 for (pe = *list; pe != NULL; pe = pe->next) {
1532 if (pe->addend == rel->r_addend) {
1538 pe = xmalloc(sizeof(struct arch_list_entry));
1540 pe->addend = rel->r_addend;
1541 pe->offset = offset;
1551 #if defined(CONFIG_USE_SINGLE)
1553 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1554 int offset, int size)
1556 if (single->allocated == 0) {
1557 single->allocated = 1;
1558 single->offset = offset;
1567 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1569 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1570 int offset, int size)
1572 struct obj_section *myrelsec = obj_find_section(f, name);
1579 obj_extend_section(myrelsec, offset);
1581 myrelsec = obj_create_alloced_section(f, name,
1591 static void arch_create_got(struct obj_file *f)
1593 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1594 struct arch_file *ifile = (struct arch_file *) f;
1596 #if defined(CONFIG_USE_GOT_ENTRIES)
1597 int got_offset = 0, got_needed = 0, got_allocate;
1599 #if defined(CONFIG_USE_PLT_ENTRIES)
1600 int plt_offset = 0, plt_needed = 0, plt_allocate;
1602 struct obj_section *relsec, *symsec, *strsec;
1603 ElfW(RelM) *rel, *relend;
1604 ElfW(Sym) *symtab, *extsym;
1605 const char *strtab, *name;
1606 struct arch_symbol *intsym;
1608 for (i = 0; i < f->header.e_shnum; ++i) {
1609 relsec = f->sections[i];
1610 if (relsec->header.sh_type != SHT_RELM)
1613 symsec = f->sections[relsec->header.sh_link];
1614 strsec = f->sections[symsec->header.sh_link];
1616 rel = (ElfW(RelM) *) relsec->contents;
1617 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1618 symtab = (ElfW(Sym) *) symsec->contents;
1619 strtab = (const char *) strsec->contents;
1621 for (; rel < relend; ++rel) {
1622 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1624 #if defined(CONFIG_USE_GOT_ENTRIES)
1627 #if defined(CONFIG_USE_PLT_ENTRIES)
1631 switch (ELF_R_TYPE(rel->r_info)) {
1632 #if defined(__arm__)
1647 #elif defined(__i386__)
1657 #elif defined(__powerpc__)
1662 #elif defined(__mc68000__)
1673 #elif defined(__sh__)
1683 #elif defined (__v850e__)
1684 case R_V850_22_PCREL:
1693 if (extsym->st_name != 0) {
1694 name = strtab + extsym->st_name;
1696 name = f->sections[extsym->st_shndx]->name;
1698 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1699 #if defined(CONFIG_USE_GOT_ENTRIES)
1701 got_offset += arch_single_init(
1702 rel, &intsym->gotent,
1703 got_offset, CONFIG_GOT_ENTRY_SIZE);
1708 #if defined(CONFIG_USE_PLT_ENTRIES)
1710 #if defined(CONFIG_USE_PLT_LIST)
1711 plt_offset += arch_list_add(
1712 rel, &intsym->pltent,
1713 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1715 plt_offset += arch_single_init(
1716 rel, &intsym->pltent,
1717 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1725 #if defined(CONFIG_USE_GOT_ENTRIES)
1727 ifile->got = arch_xsect_init(f, ".got", got_offset,
1728 CONFIG_GOT_ENTRY_SIZE);
1732 #if defined(CONFIG_USE_PLT_ENTRIES)
1734 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1735 CONFIG_PLT_ENTRY_SIZE);
1739 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1742 /*======================================================================*/
1744 /* Standard ELF hash function. */
1745 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1747 unsigned long h = 0;
1754 if ((g = (h & 0xf0000000)) != 0) {
1763 static unsigned long obj_elf_hash(const char *name)
1765 return obj_elf_hash_n(name, strlen(name));
1768 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1769 /* String comparison for non-co-versioned kernel and module. */
1771 static int ncv_strcmp(const char *a, const char *b)
1773 size_t alen = strlen(a), blen = strlen(b);
1775 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1776 return strncmp(a, b, alen);
1777 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1778 return strncmp(a, b, blen);
1780 return strcmp(a, b);
1783 /* String hashing for non-co-versioned kernel and module. Here
1784 we are simply forced to drop the crc from the hash. */
1786 static unsigned long ncv_symbol_hash(const char *str)
1788 size_t len = strlen(str);
1789 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1791 return obj_elf_hash_n(str, len);
1795 obj_set_symbol_compare(struct obj_file *f,
1796 int (*cmp) (const char *, const char *),
1797 unsigned long (*hash) (const char *))
1800 f->symbol_cmp = cmp;
1802 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1805 f->symbol_hash = hash;
1807 memcpy(tmptab, f->symtab, sizeof(tmptab));
1808 memset(f->symtab, 0, sizeof(f->symtab));
1810 for (i = 0; i < HASH_BUCKETS; ++i)
1811 for (sym = tmptab[i]; sym; sym = next) {
1812 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1814 sym->next = f->symtab[h];
1820 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1822 static struct obj_symbol *
1823 obj_add_symbol(struct obj_file *f, const char *name,
1824 unsigned long symidx, int info,
1825 int secidx, ElfW(Addr) value,
1828 struct obj_symbol *sym;
1829 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1830 int n_type = ELF_ST_TYPE(info);
1831 int n_binding = ELF_ST_BIND(info);
1833 for (sym = f->symtab[hash]; sym; sym = sym->next)
1834 if (f->symbol_cmp(sym->name, name) == 0) {
1835 int o_secidx = sym->secidx;
1836 int o_info = sym->info;
1837 int o_type = ELF_ST_TYPE(o_info);
1838 int o_binding = ELF_ST_BIND(o_info);
1840 /* A redefinition! Is it legal? */
1842 if (secidx == SHN_UNDEF)
1844 else if (o_secidx == SHN_UNDEF)
1846 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1847 /* Cope with local and global symbols of the same name
1848 in the same object file, as might have been created
1849 by ld -r. The only reason locals are now seen at this
1850 level at all is so that we can do semi-sensible things
1853 struct obj_symbol *nsym, **p;
1855 nsym = arch_new_symbol();
1856 nsym->next = sym->next;
1859 /* Excise the old (local) symbol from the hash chain. */
1860 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1864 } else if (n_binding == STB_LOCAL) {
1865 /* Another symbol of the same name has already been defined.
1866 Just add this to the local table. */
1867 sym = arch_new_symbol();
1870 f->local_symtab[symidx] = sym;
1872 } else if (n_binding == STB_WEAK)
1874 else if (o_binding == STB_WEAK)
1876 /* Don't unify COMMON symbols with object types the programmer
1878 else if (secidx == SHN_COMMON
1879 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1881 else if (o_secidx == SHN_COMMON
1882 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1885 /* Don't report an error if the symbol is coming from
1886 the kernel or some external module. */
1887 if (secidx <= SHN_HIRESERVE)
1888 bb_error_msg("%s multiply defined", name);
1893 /* Completely new symbol. */
1894 sym = arch_new_symbol();
1895 sym->next = f->symtab[hash];
1896 f->symtab[hash] = sym;
1899 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
1900 if (symidx >= f->local_symtab_size)
1901 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1902 name, (long) symidx, (long) f->local_symtab_size);
1904 f->local_symtab[symidx] = sym;
1911 sym->secidx = secidx;
1917 static struct obj_symbol *
1918 obj_find_symbol(struct obj_file *f, const char *name)
1920 struct obj_symbol *sym;
1921 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1923 for (sym = f->symtab[hash]; sym; sym = sym->next)
1924 if (f->symbol_cmp(sym->name, name) == 0)
1931 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1934 if (sym->secidx >= SHN_LORESERVE)
1937 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1939 /* As a special case, a NULL sym has value zero. */
1944 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1946 int i, n = f->header.e_shnum;
1948 for (i = 0; i < n; ++i)
1949 if (strcmp(f->sections[i]->name, name) == 0)
1950 return f->sections[i];
1955 static int obj_load_order_prio(struct obj_section *a)
1957 unsigned long af, ac;
1959 af = a->header.sh_flags;
1962 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1963 strcmp(a->name + 5, ".init"))
1967 if (!(af & SHF_WRITE))
1969 if (af & SHF_EXECINSTR)
1971 if (a->header.sh_type != SHT_NOBITS)
1978 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1980 struct obj_section **p;
1981 int prio = obj_load_order_prio(sec);
1982 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1983 if (obj_load_order_prio(*p) < prio)
1985 sec->load_next = *p;
1989 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1991 unsigned long align,
1994 int newidx = f->header.e_shnum++;
1995 struct obj_section *sec;
1997 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1998 f->sections[newidx] = sec = arch_new_section();
2000 memset(sec, 0, sizeof(*sec));
2001 sec->header.sh_type = SHT_PROGBITS;
2002 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2003 sec->header.sh_size = size;
2004 sec->header.sh_addralign = align;
2008 sec->contents = xmalloc(size);
2010 obj_insert_section_load_order(f, sec);
2015 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2017 unsigned long align,
2020 int newidx = f->header.e_shnum++;
2021 struct obj_section *sec;
2023 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2024 f->sections[newidx] = sec = arch_new_section();
2026 memset(sec, 0, sizeof(*sec));
2027 sec->header.sh_type = SHT_PROGBITS;
2028 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2029 sec->header.sh_size = size;
2030 sec->header.sh_addralign = align;
2034 sec->contents = xmalloc(size);
2036 sec->load_next = f->load_order;
2037 f->load_order = sec;
2038 if (f->load_order_search_start == &f->load_order)
2039 f->load_order_search_start = &sec->load_next;
2044 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2046 unsigned long oldsize = sec->header.sh_size;
2048 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2050 return sec->contents + oldsize;
2054 /* Conditionally add the symbols from the given symbol set to the
2058 add_symbols_from( struct obj_file *f,
2059 int idx, struct new_module_symbol *syms, size_t nsyms)
2061 struct new_module_symbol *s;
2064 #ifdef SYMBOL_PREFIX
2066 size_t name_alloced_size = 0;
2068 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2071 gpl = obj_gpl_license(f, NULL) == 0;
2073 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2074 /* Only add symbols that are already marked external.
2075 If we override locals we may cause problems for
2076 argument initialization. We will also create a false
2077 dependency on the module. */
2078 struct obj_symbol *sym;
2081 /* GPL licensed modules can use symbols exported with
2082 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2083 * exported names. Non-GPL modules never see any GPLONLY_
2084 * symbols so they cannot fudge it by adding the prefix on
2087 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2088 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2095 name = (char *)s->name;
2097 #ifdef SYMBOL_PREFIX
2098 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2099 kernel exports `C names', but module object files
2100 reference `linker names'). */
2101 size_t extra = sizeof SYMBOL_PREFIX;
2102 size_t name_size = strlen (name) + extra;
2103 if (name_size > name_alloced_size) {
2104 name_alloced_size = name_size * 2;
2105 name_buf = alloca (name_alloced_size);
2107 strcpy (name_buf, SYMBOL_PREFIX);
2108 strcpy (name_buf + extra - 1, name);
2110 #endif /* SYMBOL_PREFIX */
2112 sym = obj_find_symbol(f, name);
2113 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2114 #ifdef SYMBOL_PREFIX
2115 /* Put NAME_BUF into more permanent storage. */
2116 name = xmalloc (name_size);
2117 strcpy (name, name_buf);
2119 sym = obj_add_symbol(f, name, -1,
2120 ELF_ST_INFO(STB_GLOBAL,
2123 /* Did our symbol just get installed? If so, mark the
2124 module as "used". */
2125 if (sym->secidx == idx)
2133 static void add_kernel_symbols(struct obj_file *f)
2135 struct external_module *m;
2138 /* Add module symbols first. */
2140 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2142 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2143 m->nsyms)) m->used = 1, ++nused;
2145 n_ext_modules_used = nused;
2147 /* And finally the symbols from the kernel proper. */
2150 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2153 static char *get_modinfo_value(struct obj_file *f, const char *key)
2155 struct obj_section *sec;
2156 char *p, *v, *n, *ep;
2157 size_t klen = strlen(key);
2159 sec = obj_find_section(f, ".modinfo");
2163 ep = p + sec->header.sh_size;
2166 n = strchr(p, '\0');
2168 if (p + klen == v && strncmp(p, key, klen) == 0)
2171 if (p + klen == n && strcmp(p, key) == 0)
2181 /*======================================================================*/
2182 /* Functions relating to module loading after 2.1.18. */
2185 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2188 char *p, *q, *key, *sym_name;
2189 struct obj_symbol *sym;
2190 char *contents, *loc;
2194 if ((q = strchr(p, '=')) == NULL) {
2199 key = alloca(q - p + 6);
2200 memcpy(key, "parm_", 5);
2201 memcpy(key + 5, p, q - p);
2204 p = get_modinfo_value(f, key);
2207 bb_error_msg("invalid parameter %s", key);
2211 #ifdef SYMBOL_PREFIX
2212 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2213 strcpy (sym_name, SYMBOL_PREFIX);
2214 strcat (sym_name, key);
2218 sym = obj_find_symbol(f, sym_name);
2220 /* Also check that the parameter was not resolved from the kernel. */
2221 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2222 bb_error_msg("symbol for parameter %s not found", key);
2227 min = strtoul(p, &p, 10);
2229 max = strtoul(p + 1, &p, 10);
2235 contents = f->sections[sym->secidx]->contents;
2236 loc = contents + sym->value;
2240 if ((*p == 's') || (*p == 'c')) {
2243 /* Do C quoting if we begin with a ", else slurp the lot. */
2247 str = alloca(strlen(q));
2248 for (r = str, q++; *q != '"'; ++q, ++r) {
2250 bb_error_msg("improperly terminated string argument for %s",
2253 } else if (*q == '\\')
2287 if (q[1] >= '0' && q[1] <= '7') {
2288 c = (c * 8) + *++q - '0';
2289 if (q[1] >= '0' && q[1] <= '7')
2290 c = (c * 8) + *++q - '0';
2307 /* In this case, the string is not quoted. We will break
2308 it using the coma (like for ints). If the user wants to
2309 include comas in a string, he just has to quote it */
2311 /* Search the next coma */
2315 if (r != (char *) NULL) {
2316 /* Recopy the current field */
2317 str = alloca(r - q + 1);
2318 memcpy(str, q, r - q);
2320 /* I don't know if it is useful, as the previous case
2321 doesn't nul terminate the string ??? */
2324 /* Keep next fields */
2335 obj_string_patch(f, sym->secidx, loc - contents, str);
2336 loc += tgt_sizeof_char_p;
2338 /* Array of chars (in fact, matrix !) */
2339 unsigned long charssize; /* size of each member */
2341 /* Get the size of each member */
2342 /* Probably we should do that outside the loop ? */
2343 if (!isdigit(*(p + 1))) {
2344 bb_error_msg("parameter type 'c' for %s must be followed by"
2345 " the maximum size", key);
2348 charssize = strtoul(p + 1, (char **) NULL, 10);
2351 if (strlen(str) >= charssize) {
2352 bb_error_msg("string too long for %s (max %ld)", key,
2357 /* Copy to location */
2358 strcpy((char *) loc, str);
2362 long v = strtoul(q, &q, 0);
2369 loc += tgt_sizeof_short;
2373 loc += tgt_sizeof_int;
2377 loc += tgt_sizeof_long;
2381 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2396 goto retry_end_of_value;
2400 bb_error_msg("too many values for %s (max %d)", key, max);
2407 bb_error_msg("invalid argument syntax for %s", key);
2414 bb_error_msg("too few values for %s (min %d)", key, min);
2424 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2425 static int new_is_module_checksummed(struct obj_file *f)
2427 const char *p = get_modinfo_value(f, "using_checksums");
2434 /* Get the module's kernel version in the canonical integer form. */
2437 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2442 p = get_modinfo_value(f, "kernel_version");
2445 safe_strncpy(str, p, STRVERSIONLEN);
2447 a = strtoul(p, &p, 10);
2450 b = strtoul(p + 1, &p, 10);
2453 c = strtoul(p + 1, &q, 10);
2457 return a << 16 | b << 8 | c;
2460 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2463 /* Fetch the loaded modules, and all currently exported symbols. */
2465 static int new_get_kernel_symbols(void)
2467 char *module_names, *mn;
2468 struct external_module *modules, *m;
2469 struct new_module_symbol *syms, *s;
2470 size_t ret, bufsize, nmod, nsyms, i, j;
2472 /* Collect the loaded modules. */
2474 module_names = xmalloc(bufsize = 256);
2476 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2477 if (errno == ENOSPC && bufsize < ret) {
2478 module_names = xrealloc(module_names, bufsize = ret);
2479 goto retry_modules_load;
2481 bb_perror_msg("QM_MODULES");
2485 n_ext_modules = nmod = ret;
2487 /* Collect the modules' symbols. */
2490 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2491 memset(modules, 0, nmod * sizeof(*modules));
2492 for (i = 0, mn = module_names, m = modules;
2493 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2494 struct new_module_info info;
2496 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2497 if (errno == ENOENT) {
2498 /* The module was removed out from underneath us. */
2501 bb_perror_msg("query_module: QM_INFO: %s", mn);
2505 syms = xmalloc(bufsize = 1024);
2507 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2510 syms = xrealloc(syms, bufsize = ret);
2511 goto retry_mod_sym_load;
2513 /* The module was removed out from underneath us. */
2516 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2523 m->addr = info.addr;
2527 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2528 s->name += (unsigned long) syms;
2533 /* Collect the kernel's symbols. */
2535 syms = xmalloc(bufsize = 16 * 1024);
2536 retry_kern_sym_load:
2537 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2538 if (errno == ENOSPC && bufsize < ret) {
2539 syms = xrealloc(syms, bufsize = ret);
2540 goto retry_kern_sym_load;
2542 bb_perror_msg("kernel: QM_SYMBOLS");
2545 nksyms = nsyms = ret;
2548 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2549 s->name += (unsigned long) syms;
2555 /* Return the kernel symbol checksum version, or zero if not used. */
2557 static int new_is_kernel_checksummed(void)
2559 struct new_module_symbol *s;
2562 /* Using_Versions is not the first symbol, but it should be in there. */
2564 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2565 if (strcmp((char *) s->name, "Using_Versions") == 0)
2572 static int new_create_this_module(struct obj_file *f, const char *m_name)
2574 struct obj_section *sec;
2576 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2577 sizeof(struct new_module));
2578 memset(sec->contents, 0, sizeof(struct new_module));
2580 obj_add_symbol(f, SPFX "__this_module", -1,
2581 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2582 sizeof(struct new_module));
2584 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2590 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2591 /* add an entry to the __ksymtab section, creating it if necessary */
2592 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2594 struct obj_section *sec;
2597 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2598 * If __ksymtab is defined but not marked alloc, x out the first character
2599 * (no obj_delete routine) and create a new __ksymtab with the correct
2602 sec = obj_find_section(f, "__ksymtab");
2603 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2604 *((char *)(sec->name)) = 'x'; /* override const */
2608 sec = obj_create_alloced_section(f, "__ksymtab",
2609 tgt_sizeof_void_p, 0);
2612 sec->header.sh_flags |= SHF_ALLOC;
2613 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2615 ofs = sec->header.sh_size;
2616 obj_symbol_patch(f, sec->idx, ofs, sym);
2617 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2618 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2620 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2622 static int new_create_module_ksymtab(struct obj_file *f)
2624 struct obj_section *sec;
2627 /* We must always add the module references. */
2629 if (n_ext_modules_used) {
2630 struct new_module_ref *dep;
2631 struct obj_symbol *tm;
2633 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2634 (sizeof(struct new_module_ref)
2635 * n_ext_modules_used));
2639 tm = obj_find_symbol(f, SPFX "__this_module");
2640 dep = (struct new_module_ref *) sec->contents;
2641 for (i = 0; i < n_ext_modules; ++i)
2642 if (ext_modules[i].used) {
2643 dep->dep = ext_modules[i].addr;
2644 obj_symbol_patch(f, sec->idx,
2645 (char *) &dep->ref - sec->contents, tm);
2651 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2656 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2659 /* We don't want to export symbols residing in sections that
2660 aren't loaded. There are a number of these created so that
2661 we make sure certain module options don't appear twice. */
2663 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2665 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2667 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2668 struct obj_symbol *sym;
2669 for (sym = f->symtab[i]; sym; sym = sym->next)
2670 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2671 && sym->secidx <= SHN_HIRESERVE
2672 && (sym->secidx >= SHN_LORESERVE
2673 || loaded[sym->secidx])) {
2674 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2676 obj_symbol_patch(f, sec->idx, ofs, sym);
2677 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2684 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2692 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2694 struct new_module *module;
2695 struct obj_section *sec;
2700 sec = obj_find_section(f, ".this");
2701 if (!sec || !sec->contents) {
2702 bb_perror_msg_and_die("corrupt module %s?",m_name);
2704 module = (struct new_module *) sec->contents;
2705 m_addr = sec->header.sh_addr;
2707 module->size_of_struct = sizeof(*module);
2708 module->size = m_size;
2709 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2711 sec = obj_find_section(f, "__ksymtab");
2712 if (sec && sec->header.sh_size) {
2713 module->syms = sec->header.sh_addr;
2714 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2717 if (n_ext_modules_used) {
2718 sec = obj_find_section(f, ".kmodtab");
2719 module->deps = sec->header.sh_addr;
2720 module->ndeps = n_ext_modules_used;
2724 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2726 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2728 sec = obj_find_section(f, "__ex_table");
2730 module->ex_table_start = sec->header.sh_addr;
2731 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2734 sec = obj_find_section(f, ".text.init");
2736 module->runsize = sec->header.sh_addr - m_addr;
2738 sec = obj_find_section(f, ".data.init");
2740 if (!module->runsize ||
2741 module->runsize > sec->header.sh_addr - m_addr)
2742 module->runsize = sec->header.sh_addr - m_addr;
2744 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2745 if (sec && sec->header.sh_size) {
2746 module->archdata_start = (void*)sec->header.sh_addr;
2747 module->archdata_end = module->archdata_start + sec->header.sh_size;
2749 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2750 if (sec && sec->header.sh_size) {
2751 module->kallsyms_start = (void*)sec->header.sh_addr;
2752 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2755 /* Whew! All of the initialization is complete. Collect the final
2756 module image and give it to the kernel. */
2758 image = xmalloc(m_size);
2759 obj_create_image(f, image);
2761 ret = init_module(m_name, (struct new_module *) image);
2763 bb_perror_msg("init_module: %s", m_name);
2771 /*======================================================================*/
2774 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2777 struct obj_string_patch *p;
2778 struct obj_section *strsec;
2779 size_t len = strlen(string) + 1;
2782 p = xmalloc(sizeof(*p));
2783 p->next = f->string_patches;
2784 p->reloc_secidx = secidx;
2785 p->reloc_offset = offset;
2786 f->string_patches = p;
2788 strsec = obj_find_section(f, ".kstrtab");
2789 if (strsec == NULL) {
2790 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2791 p->string_offset = 0;
2792 loc = strsec->contents;
2794 p->string_offset = strsec->header.sh_size;
2795 loc = obj_extend_section(strsec, len);
2797 memcpy(loc, string, len);
2803 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2804 struct obj_symbol *sym)
2806 struct obj_symbol_patch *p;
2808 p = xmalloc(sizeof(*p));
2809 p->next = f->symbol_patches;
2810 p->reloc_secidx = secidx;
2811 p->reloc_offset = offset;
2813 f->symbol_patches = p;
2818 static int obj_check_undefineds(struct obj_file *f)
2823 for (i = 0; i < HASH_BUCKETS; ++i) {
2824 struct obj_symbol *sym;
2825 for (sym = f->symtab[i]; sym; sym = sym->next)
2826 if (sym->secidx == SHN_UNDEF) {
2827 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
2828 sym->secidx = SHN_ABS;
2832 bb_error_msg("unresolved symbol %s", sym->name);
2842 static void obj_allocate_commons(struct obj_file *f)
2844 struct common_entry {
2845 struct common_entry *next;
2846 struct obj_symbol *sym;
2847 } *common_head = NULL;
2851 for (i = 0; i < HASH_BUCKETS; ++i) {
2852 struct obj_symbol *sym;
2853 for (sym = f->symtab[i]; sym; sym = sym->next)
2854 if (sym->secidx == SHN_COMMON) {
2855 /* Collect all COMMON symbols and sort them by size so as to
2856 minimize space wasted by alignment requirements. */
2858 struct common_entry **p, *n;
2859 for (p = &common_head; *p; p = &(*p)->next)
2860 if (sym->size <= (*p)->sym->size)
2863 n = alloca(sizeof(*n));
2871 for (i = 1; i < f->local_symtab_size; ++i) {
2872 struct obj_symbol *sym = f->local_symtab[i];
2873 if (sym && sym->secidx == SHN_COMMON) {
2874 struct common_entry **p, *n;
2875 for (p = &common_head; *p; p = &(*p)->next)
2876 if (sym == (*p)->sym)
2878 else if (sym->size < (*p)->sym->size) {
2879 n = alloca(sizeof(*n));
2889 /* Find the bss section. */
2890 for (i = 0; i < f->header.e_shnum; ++i)
2891 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2894 /* If for some reason there hadn't been one, create one. */
2895 if (i == f->header.e_shnum) {
2896 struct obj_section *sec;
2898 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2899 f->sections[i] = sec = arch_new_section();
2900 f->header.e_shnum = i + 1;
2902 memset(sec, 0, sizeof(*sec));
2903 sec->header.sh_type = SHT_PROGBITS;
2904 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2909 /* Allocate the COMMONS. */
2911 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2912 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2913 struct common_entry *c;
2915 for (c = common_head; c; c = c->next) {
2916 ElfW(Addr) align = c->sym->value;
2918 if (align > max_align)
2920 if (bss_size & (align - 1))
2921 bss_size = (bss_size | (align - 1)) + 1;
2924 c->sym->value = bss_size;
2926 bss_size += c->sym->size;
2929 f->sections[i]->header.sh_size = bss_size;
2930 f->sections[i]->header.sh_addralign = max_align;
2934 /* For the sake of patch relocation and parameter initialization,
2935 allocate zeroed data for NOBITS sections now. Note that after
2936 this we cannot assume NOBITS are really empty. */
2937 for (i = 0; i < f->header.e_shnum; ++i) {
2938 struct obj_section *s = f->sections[i];
2939 if (s->header.sh_type == SHT_NOBITS) {
2940 if (s->header.sh_size != 0)
2941 s->contents = memset(xmalloc(s->header.sh_size),
2942 0, s->header.sh_size);
2946 s->header.sh_type = SHT_PROGBITS;
2951 static unsigned long obj_load_size(struct obj_file *f)
2953 unsigned long dot = 0;
2954 struct obj_section *sec;
2956 /* Finalize the positions of the sections relative to one another. */
2958 for (sec = f->load_order; sec; sec = sec->load_next) {
2961 align = sec->header.sh_addralign;
2962 if (align && (dot & (align - 1)))
2963 dot = (dot | (align - 1)) + 1;
2965 sec->header.sh_addr = dot;
2966 dot += sec->header.sh_size;
2972 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2974 int i, n = f->header.e_shnum;
2977 /* Finalize the addresses of the sections. */
2980 for (i = 0; i < n; ++i)
2981 f->sections[i]->header.sh_addr += base;
2983 /* And iterate over all of the relocations. */
2985 for (i = 0; i < n; ++i) {
2986 struct obj_section *relsec, *symsec, *targsec, *strsec;
2987 ElfW(RelM) * rel, *relend;
2991 relsec = f->sections[i];
2992 if (relsec->header.sh_type != SHT_RELM)
2995 symsec = f->sections[relsec->header.sh_link];
2996 targsec = f->sections[relsec->header.sh_info];
2997 strsec = f->sections[symsec->header.sh_link];
2999 rel = (ElfW(RelM) *) relsec->contents;
3000 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3001 symtab = (ElfW(Sym) *) symsec->contents;
3002 strtab = (const char *) strsec->contents;
3004 for (; rel < relend; ++rel) {
3005 ElfW(Addr) value = 0;
3006 struct obj_symbol *intsym = NULL;
3007 unsigned long symndx;
3008 ElfW(Sym) * extsym = 0;
3011 /* Attempt to find a value to use for this relocation. */
3013 symndx = ELF_R_SYM(rel->r_info);
3015 /* Note we've already checked for undefined symbols. */
3017 extsym = &symtab[symndx];
3018 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3019 /* Local symbols we look up in the local table to be sure
3020 we get the one that is really intended. */
3021 intsym = f->local_symtab[symndx];
3023 /* Others we look up in the hash table. */
3025 if (extsym->st_name)
3026 name = strtab + extsym->st_name;
3028 name = f->sections[extsym->st_shndx]->name;
3029 intsym = obj_find_symbol(f, name);
3032 value = obj_symbol_final_value(f, intsym);
3033 intsym->referenced = 1;
3035 #if SHT_RELM == SHT_RELA
3036 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3037 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3038 if (!extsym || !extsym->st_name ||
3039 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3041 value += rel->r_addend;
3045 switch (arch_apply_relocation
3046 (f, targsec, symsec, intsym, rel, value)) {
3050 case obj_reloc_overflow:
3051 errmsg = "Relocation overflow";
3053 case obj_reloc_dangerous:
3054 errmsg = "Dangerous relocation";
3056 case obj_reloc_unhandled:
3057 errmsg = "Unhandled relocation";
3060 bb_error_msg("%s of type %ld for %s", errmsg,
3061 (long) ELF_R_TYPE(rel->r_info),
3062 strtab + extsym->st_name);
3064 bb_error_msg("%s of type %ld", errmsg,
3065 (long) ELF_R_TYPE(rel->r_info));
3073 /* Finally, take care of the patches. */
3075 if (f->string_patches) {
3076 struct obj_string_patch *p;
3077 struct obj_section *strsec;
3078 ElfW(Addr) strsec_base;
3079 strsec = obj_find_section(f, ".kstrtab");
3080 strsec_base = strsec->header.sh_addr;
3082 for (p = f->string_patches; p; p = p->next) {
3083 struct obj_section *targsec = f->sections[p->reloc_secidx];
3084 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3085 = strsec_base + p->string_offset;
3089 if (f->symbol_patches) {
3090 struct obj_symbol_patch *p;
3092 for (p = f->symbol_patches; p; p = p->next) {
3093 struct obj_section *targsec = f->sections[p->reloc_secidx];
3094 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3095 = obj_symbol_final_value(f, p->sym);
3102 static int obj_create_image(struct obj_file *f, char *image)
3104 struct obj_section *sec;
3105 ElfW(Addr) base = f->baseaddr;
3107 for (sec = f->load_order; sec; sec = sec->load_next) {
3110 if (sec->contents == 0 || sec->header.sh_size == 0)
3113 secimg = image + (sec->header.sh_addr - base);
3115 /* Note that we allocated data for NOBITS sections earlier. */
3116 memcpy(secimg, sec->contents, sec->header.sh_size);
3122 /*======================================================================*/
3124 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3127 ElfW(Shdr) * section_headers;
3131 /* Read the file header. */
3133 f = arch_new_file();
3134 memset(f, 0, sizeof(*f));
3135 f->symbol_cmp = strcmp;
3136 f->symbol_hash = obj_elf_hash;
3137 f->load_order_search_start = &f->load_order;
3139 fseek(fp, 0, SEEK_SET);
3140 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3141 bb_perror_msg("error reading ELF header");
3145 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3146 || f->header.e_ident[EI_MAG1] != ELFMAG1
3147 || f->header.e_ident[EI_MAG2] != ELFMAG2
3148 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3149 bb_error_msg("not an ELF file");
3152 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3153 || f->header.e_ident[EI_DATA] != ELFDATAM
3154 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3155 || !MATCH_MACHINE(f->header.e_machine)) {
3156 bb_error_msg("ELF file not for this architecture");
3159 if (f->header.e_type != ET_REL) {
3160 bb_error_msg("ELF file not a relocatable object");
3164 /* Read the section headers. */
3166 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3167 bb_error_msg("section header size mismatch: %lu != %lu",
3168 (unsigned long) f->header.e_shentsize,
3169 (unsigned long) sizeof(ElfW(Shdr)));
3173 shnum = f->header.e_shnum;
3174 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3175 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3177 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3178 fseek(fp, f->header.e_shoff, SEEK_SET);
3179 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3180 bb_perror_msg("error reading ELF section headers");
3184 /* Read the section data. */
3186 for (i = 0; i < shnum; ++i) {
3187 struct obj_section *sec;
3189 f->sections[i] = sec = arch_new_section();
3190 memset(sec, 0, sizeof(*sec));
3192 sec->header = section_headers[i];
3195 if(sec->header.sh_size) switch (sec->header.sh_type) {
3204 if (!loadprogbits) {
3205 sec->contents = NULL;
3212 if (sec->header.sh_size > 0) {
3213 sec->contents = xmalloc(sec->header.sh_size);
3214 fseek(fp, sec->header.sh_offset, SEEK_SET);
3215 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3216 bb_perror_msg("error reading ELF section data");
3220 sec->contents = NULL;
3224 #if SHT_RELM == SHT_REL
3226 bb_error_msg("RELA relocations not supported on this architecture");
3230 bb_error_msg("REL relocations not supported on this architecture");
3235 if (sec->header.sh_type >= SHT_LOPROC) {
3236 /* Assume processor specific section types are debug
3237 info and can safely be ignored. If this is ever not
3238 the case (Hello MIPS?), don't put ifdefs here but
3239 create an arch_load_proc_section(). */
3243 bb_error_msg("can't handle sections of type %ld",
3244 (long) sec->header.sh_type);
3249 /* Do what sort of interpretation as needed by each section. */
3251 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3253 for (i = 0; i < shnum; ++i) {
3254 struct obj_section *sec = f->sections[i];
3255 sec->name = shstrtab + sec->header.sh_name;
3258 for (i = 0; i < shnum; ++i) {
3259 struct obj_section *sec = f->sections[i];
3261 /* .modinfo should be contents only but gcc has no attribute for that.
3262 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3264 if (strcmp(sec->name, ".modinfo") == 0)
3265 sec->header.sh_flags &= ~SHF_ALLOC;
3267 if (sec->header.sh_flags & SHF_ALLOC)
3268 obj_insert_section_load_order(f, sec);
3270 switch (sec->header.sh_type) {
3273 unsigned long nsym, j;
3277 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3278 bb_error_msg("symbol size mismatch: %lu != %lu",
3279 (unsigned long) sec->header.sh_entsize,
3280 (unsigned long) sizeof(ElfW(Sym)));
3284 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3285 strtab = f->sections[sec->header.sh_link]->contents;
3286 sym = (ElfW(Sym) *) sec->contents;
3288 /* Allocate space for a table of local symbols. */
3289 j = f->local_symtab_size = sec->header.sh_info;
3290 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3292 /* Insert all symbols into the hash table. */
3293 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3294 ElfW(Addr) val = sym->st_value;
3297 name = strtab + sym->st_name;
3298 else if (sym->st_shndx < shnum)
3299 name = f->sections[sym->st_shndx]->name;
3303 #if defined(__SH5__)
3305 * For sh64 it is possible that the target of a branch
3306 * requires a mode switch (32 to 16 and back again).
3308 * This is implied by the lsb being set in the target
3309 * address for SHmedia mode and clear for SHcompact.
3311 val |= sym->st_other & 4;
3314 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3321 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3322 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3323 (unsigned long) sec->header.sh_entsize,
3324 (unsigned long) sizeof(ElfW(RelM)));
3328 /* XXX Relocation code from modutils-2.3.19 is not here.
3329 * Why? That's about 20 lines of code from obj/obj_load.c,
3330 * which gets done in a second pass through the sections.
3331 * This BusyBox insmod does similar work in obj_relocate(). */
3338 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3340 * load the unloaded sections directly into the memory allocated by
3341 * kernel for the module
3344 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3346 ElfW(Addr) base = f->baseaddr;
3347 struct obj_section* sec;
3349 for (sec = f->load_order; sec; sec = sec->load_next) {
3351 /* section already loaded? */
3352 if (sec->contents != NULL)
3355 if (sec->header.sh_size == 0)
3358 sec->contents = imagebase + (sec->header.sh_addr - base);
3359 fseek(fp, sec->header.sh_offset, SEEK_SET);
3360 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3361 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3370 static void hide_special_symbols(struct obj_file *f)
3372 static const char *const specials[] = {
3373 SPFX "cleanup_module",
3375 SPFX "kernel_version",
3379 struct obj_symbol *sym;
3380 const char *const *p;
3382 for (p = specials; *p; ++p)
3383 if ((sym = obj_find_symbol(f, *p)) != NULL)
3385 ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3389 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3390 static int obj_gpl_license(struct obj_file *f, const char **license)
3392 struct obj_section *sec;
3393 /* This list must match *exactly* the list of allowable licenses in
3394 * linux/include/linux/module.h. Checking for leading "GPL" will not
3395 * work, somebody will use "GPL sucks, this is proprietary".
3397 static const char *gpl_licenses[] = {
3400 "GPL and additional rights",
3405 if ((sec = obj_find_section(f, ".modinfo"))) {
3406 const char *value, *ptr, *endptr;
3407 ptr = sec->contents;
3408 endptr = ptr + sec->header.sh_size;
3409 while (ptr < endptr) {
3410 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3414 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3415 if (strcmp(value+1, gpl_licenses[i]) == 0)
3420 if (strchr(ptr, '\0'))
3421 ptr = strchr(ptr, '\0') + 1;
3429 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3430 #define TAINT_PROPRIETORY_MODULE (1<<0)
3431 #define TAINT_FORCED_MODULE (1<<1)
3432 #define TAINT_UNSAFE_SMP (1<<2)
3433 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3435 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3436 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3440 static int first = 1;
3441 if (fd < 0 && !kernel_has_tainted)
3442 return; /* New modutils on old kernel */
3443 printf("Warning: loading %s will taint the kernel: %s%s\n",
3444 m_name, text1, text2);
3446 printf(" See %s for information about tainted modules\n", TAINT_URL);
3450 read(fd, buf, sizeof(buf)-1);
3451 buf[sizeof(buf)-1] = '\0';
3452 oldval = strtoul(buf, NULL, 10);
3453 sprintf(buf, "%d\n", oldval | taint);
3454 write(fd, buf, strlen(buf));
3458 /* Check if loading this module will taint the kernel. */
3459 static void check_tainted_module(struct obj_file *f, char *m_name)
3461 static const char tainted_file[] = TAINT_FILENAME;
3462 int fd, kernel_has_tainted;
3465 kernel_has_tainted = 1;
3466 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3467 if (errno == ENOENT)
3468 kernel_has_tainted = 0;
3469 else if (errno == EACCES)
3470 kernel_has_tainted = 1;
3472 perror(tainted_file);
3473 kernel_has_tainted = 0;
3477 switch (obj_gpl_license(f, &ptr)) {
3481 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3484 /* The module has a non-GPL license so we pretend that the
3485 * kernel always has a taint flag to get a warning even on
3486 * kernels without the proc flag.
3488 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3491 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3495 if (flag_force_load)
3496 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3501 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3502 #define check_tainted_module(x, y) do { } while(0);
3503 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3505 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3506 /* add module source, timestamp, kernel version and a symbol for the
3507 * start of some sections. this info is used by ksymoops to do better
3511 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3513 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3514 return new_get_module_version(f, str);
3515 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3516 strncpy(str, "???", sizeof(str));
3518 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3521 /* add module source, timestamp, kernel version and a symbol for the
3522 * start of some sections. this info is used by ksymoops to do better
3526 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3529 static const char symprefix[] = "__insmod_";
3530 struct obj_section *sec;
3531 struct obj_symbol *sym;
3532 char *name, *absolute_filename;
3533 char str[STRVERSIONLEN], real[PATH_MAX];
3534 int i, l, lm_name, lfilename, use_ksymtab, version;
3535 struct stat statbuf;
3537 static const char *section_names[] = {
3545 if (realpath(filename, real)) {
3546 absolute_filename = bb_xstrdup(real);
3549 int save_errno = errno;
3550 bb_error_msg("cannot get realpath for %s", filename);
3553 absolute_filename = bb_xstrdup(filename);
3556 lm_name = strlen(m_name);
3557 lfilename = strlen(absolute_filename);
3559 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3560 * are not to be exported. otherwise leave ksymtab alone for now, the
3561 * "export all symbols" compatibility code will export these symbols later.
3563 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3565 if ((sec = obj_find_section(f, ".this"))) {
3566 /* tag the module header with the object name, last modified
3567 * timestamp and module version. worst case for module version
3568 * is 0xffffff, decimal 16777215. putting all three fields in
3569 * one symbol is less readable but saves kernel space.
3571 l = sizeof(symprefix)+ /* "__insmod_" */
3572 lm_name+ /* module name */
3574 lfilename+ /* object filename */
3576 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3578 8+ /* version in dec */
3581 if (stat(absolute_filename, &statbuf) != 0)
3582 statbuf.st_mtime = 0;
3583 version = get_module_version(f, str); /* -1 if not found */
3584 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3585 symprefix, m_name, absolute_filename,
3586 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3588 sym = obj_add_symbol(f, name, -1,
3589 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3590 sec->idx, sec->header.sh_addr, 0);
3592 new_add_ksymtab(f, sym);
3594 free(absolute_filename);
3595 #ifdef _NOT_SUPPORTED_
3596 /* record where the persistent data is going, same address as previous symbol */
3599 l = sizeof(symprefix)+ /* "__insmod_" */
3600 lm_name+ /* module name */
3602 strlen(f->persist)+ /* data store */
3605 snprintf(name, l, "%s%s_P%s",
3606 symprefix, m_name, f->persist);
3607 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3608 sec->idx, sec->header.sh_addr, 0);
3610 new_add_ksymtab(f, sym);
3612 #endif /* _NOT_SUPPORTED_ */
3613 /* tag the desired sections if size is non-zero */
3615 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3616 if ((sec = obj_find_section(f, section_names[i])) &&
3617 sec->header.sh_size) {
3618 l = sizeof(symprefix)+ /* "__insmod_" */
3619 lm_name+ /* module name */
3621 strlen(sec->name)+ /* section name */
3623 8+ /* length in dec */
3626 snprintf(name, l, "%s%s_S%s_L%ld",
3627 symprefix, m_name, sec->name,
3628 (long)sec->header.sh_size);
3629 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3630 sec->idx, sec->header.sh_addr, 0);
3632 new_add_ksymtab(f, sym);
3636 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3638 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3639 static void print_load_map(struct obj_file *f)
3641 struct obj_symbol *sym;
3642 struct obj_symbol **all, **p;
3643 struct obj_section *sec;
3644 int i, nsyms, *loaded;
3646 /* Report on the section layout. */
3648 printf("Sections: Size %-*s Align\n",
3649 (int) (2 * sizeof(void *)), "Address");
3651 for (sec = f->load_order; sec; sec = sec->load_next) {
3655 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3660 printf("%-15s %08lx %0*lx 2**%d\n",
3662 (long)sec->header.sh_size,
3663 (int) (2 * sizeof(void *)),
3664 (long)sec->header.sh_addr,
3667 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3668 /* Quick reference which section indicies are loaded. */
3670 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3672 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3674 /* Collect the symbols we'll be listing. */
3676 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3677 for (sym = f->symtab[i]; sym; sym = sym->next)
3678 if (sym->secidx <= SHN_HIRESERVE
3679 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3682 all = alloca(nsyms * sizeof(struct obj_symbol *));
3684 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3685 for (sym = f->symtab[i]; sym; sym = sym->next)
3686 if (sym->secidx <= SHN_HIRESERVE
3687 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3690 /* And list them. */
3691 printf("\nSymbols:\n");
3692 for (p = all; p < all + nsyms; ++p) {
3694 unsigned long value;
3697 if (sym->secidx == SHN_ABS) {
3700 } else if (sym->secidx == SHN_UNDEF) {
3704 sec = f->sections[sym->secidx];
3706 if (sec->header.sh_type == SHT_NOBITS)
3708 else if (sec->header.sh_flags & SHF_ALLOC) {
3709 if (sec->header.sh_flags & SHF_EXECINSTR)
3711 else if (sec->header.sh_flags & SHF_WRITE)
3716 value = sym->value + sec->header.sh_addr;
3719 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3720 type = tolower(type);
3722 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3730 extern int insmod_main( int argc, char **argv)
3736 unsigned long m_size;
3741 int exit_status = EXIT_FAILURE;
3743 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3744 struct utsname uts_info;
3745 char m_strversion[STRVERSIONLEN];
3746 int m_version, m_crcs;
3748 #ifdef CONFIG_FEATURE_CLEAN_UP
3753 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3754 int flag_print_load_map = 0;
3757 struct utsname myuname;
3759 /* Parse any options */
3760 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3761 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
3763 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
3767 case 'f': /* force loading */
3768 flag_force_load = 1;
3770 case 'k': /* module loaded by kerneld, auto-cleanable */
3773 case 's': /* log to syslog */
3774 /* log to syslog -- not supported */
3775 /* but kernel needs this for request_module(), */
3776 /* as this calls: modprobe -k -s -- <module> */
3777 /* so silently ignore this flag */
3779 case 'v': /* verbose output */
3782 case 'q': /* silent */
3785 case 'x': /* do not export externs */
3788 case 'o': /* name the output module */
3790 m_name = bb_xstrdup(optarg);
3792 case 'L': /* Stub warning */
3793 /* This is needed for compatibility with modprobe.
3794 * In theory, this does locking, but we don't do
3795 * that. So be careful and plan your life around not
3796 * loading the same module 50 times concurrently. */
3798 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3799 case 'm': /* print module load map */
3800 flag_print_load_map = 1;
3808 if (argv[optind] == NULL) {
3812 /* Grab the module name */
3813 tmp1 = bb_xstrdup(argv[optind]);
3814 tmp = basename(tmp1);
3817 if (uname(&myuname) == 0) {
3818 if (myuname.release[0] == '2') {
3819 k_version = myuname.release[2] - '0';
3823 #if defined(CONFIG_FEATURE_2_6_MODULES)
3824 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
3825 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
3831 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3837 #if defined(CONFIG_FEATURE_2_6_MODULES)
3839 m_fullName = bb_xasprintf("%s.ko", tmp);
3842 m_fullName = bb_xasprintf("%s.o", tmp);
3848 tmp1 = 0; /* flag for free(m_name) before exit() */
3851 /* Get a filedesc for the module. Check we we have a complete path */
3852 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3853 (fp = fopen(argv[optind], "r")) == NULL) {
3854 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3855 * but do not error out yet if we fail to find it... */
3856 if (k_version) { /* uname succeedd */
3859 char real_module_dir[FILENAME_MAX];
3861 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
3862 /* Jump through hoops in case /lib/modules/`uname -r`
3863 * is a symlink. We do not want recursive_action to
3864 * follow symlinks, but we do want to follow the
3865 * /lib/modules/`uname -r` dir, So resolve it ourselves
3866 * if it is a link... */
3867 if (realpath (tmdn, real_module_dir) == NULL)
3870 module_dir = real_module_dir;
3871 recursive_action(module_dir, TRUE, FALSE, FALSE,
3872 check_module_name_match, 0, m_fullName);
3876 /* Check if we have found anything yet */
3877 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
3879 char module_dir[FILENAME_MAX];
3883 if (realpath (_PATH_MODULES, module_dir) == NULL)
3884 strcpy(module_dir, _PATH_MODULES);
3885 /* No module found under /lib/modules/`uname -r`, this
3886 * time cast the net a bit wider. Search /lib/modules/ */
3887 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3888 check_module_name_match, 0, m_fullName))
3891 || ((fp = fopen(m_filename, "r")) == NULL))
3893 bb_error_msg("%s: no module by that name found", m_fullName);
3897 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
3900 m_filename = bb_xstrdup(argv[optind]);
3903 printf("Using %s\n", m_filename);
3905 #ifdef CONFIG_FEATURE_2_6_MODULES
3909 argv[optind + 1] = m_filename;
3910 return insmod_ng_main(argc - optind, argv + optind);
3914 if ((f = obj_load(fp, LOADBITS)) == NULL)
3915 bb_perror_msg_and_die("Could not load the module");
3917 if (get_modinfo_value(f, "kernel_version") == NULL)
3922 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3923 /* Version correspondence? */
3925 if (uname(&uts_info) < 0)
3926 uts_info.release[0] = '\0';
3927 if (m_has_modinfo) {
3928 m_version = new_get_module_version(f, m_strversion);
3929 if (m_version == -1) {
3930 bb_error_msg("couldn't find the kernel version the module was "
3936 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3937 if (flag_force_load) {
3938 bb_error_msg("Warning: kernel-module version mismatch\n"
3939 "\t%s was compiled for kernel version %s\n"
3940 "\twhile this kernel is version %s",
3941 m_filename, m_strversion, uts_info.release);
3943 bb_error_msg("kernel-module version mismatch\n"
3944 "\t%s was compiled for kernel version %s\n"
3945 "\twhile this kernel is version %s.",
3946 m_filename, m_strversion, uts_info.release);
3952 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3954 if (!query_module(NULL, 0, NULL, 0, NULL)) {
3955 if (!new_get_kernel_symbols())
3957 k_crcs = new_is_kernel_checksummed();
3959 bb_error_msg("Not configured to support old kernels");
3963 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3966 m_crcs = new_is_module_checksummed(f);
3968 if (m_crcs != k_crcs)
3969 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3970 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3972 /* Let the module know about the kernel symbols. */
3973 add_kernel_symbols(f);
3975 /* Allocate common symbols, symbol tables, and string tables. */
3977 if (!new_create_this_module(f, m_name))
3982 if (!obj_check_undefineds(f)) {
3985 obj_allocate_commons(f);
3986 check_tainted_module(f, m_name);
3988 /* done with the module name, on to the optional var=value arguments */
3991 if (optind < argc) {
3992 if (!new_process_module_arguments(f, argc - optind, argv + optind))
3999 hide_special_symbols(f);
4001 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4002 add_ksymoops_symbols(f, m_filename, m_name);
4003 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4005 new_create_module_ksymtab(f);
4007 /* Find current size of the module */
4008 m_size = obj_load_size(f);
4011 m_addr = create_module(m_name, m_size);
4012 if (m_addr == -1) switch (errno) {
4014 bb_error_msg("A module named %s already exists", m_name);
4017 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4021 bb_perror_msg("create_module: %s", m_name);
4027 * the PROGBITS section was not loaded by the obj_load
4028 * now we can load them directly into the kernel memory
4030 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4031 delete_module(m_name);
4036 if (!obj_relocate(f, m_addr)) {
4037 delete_module(m_name);
4041 if (!new_init_module(m_name, f, m_size))
4043 delete_module(m_name);
4047 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4048 if(flag_print_load_map)
4052 exit_status = EXIT_SUCCESS;
4055 #ifdef CONFIG_FEATURE_CLEAN_UP
4065 return(exit_status);
4072 #ifdef CONFIG_FEATURE_2_6_MODULES
4074 #include <sys/mman.h>
4075 #include <asm/unistd.h>
4076 #include <sys/syscall.h>
4078 /* We use error numbers in a loose translation... */
4079 static const char *moderror(int err)
4083 return "Invalid module format";
4085 return "Unknown symbol in module";
4087 return "Module has wrong symbol version";
4089 return "Invalid parameters";
4091 return strerror(err);
4095 extern int insmod_ng_main( int argc, char **argv)
4103 char *filename, *options = bb_xstrdup("");
4111 /* Rest is options */
4112 for (i = 2; i < argc; i++) {
4113 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4114 /* Spaces handled by "" pairs, but no way of escaping quotes */
4115 if (strchr(argv[i], ' ')) {
4116 strcat(options, "\"");
4117 strcat(options, argv[i]);
4118 strcat(options, "\"");
4120 strcat(options, argv[i]);
4122 strcat(options, " ");
4125 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4126 bb_perror_msg_and_die("cannot open module `%s'", filename);
4131 map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
4132 if (map == MAP_FAILED) {
4133 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4136 ret = syscall(__NR_init_module, map, len, options);
4138 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4139 filename, moderror(errno), ret);