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(__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)) {
882 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
883 * (which is .got) similar to branch,
884 * but is full 32 bits relative */
894 case R_ARM_GOTOFF: /* address relative to the got */
899 #elif defined(__s390__)
901 *(unsigned int *) loc += v;
904 *(unsigned short *) loc += v;
907 *(unsigned char *) loc += v;
911 *(unsigned int *) loc += v - dot;
914 *(unsigned short *) loc += (v - dot) >> 1;
917 *(unsigned short *) loc += v - dot;
922 /* find the plt entry and initialize it. */
923 assert(isym != NULL);
924 pe = (struct arch_single_entry *) &isym->pltent;
925 assert(pe->allocated);
926 if (pe->inited == 0) {
927 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
928 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
930 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
937 /* Insert relative distance to target. */
938 v = plt + pe->offset - dot;
939 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
940 *(unsigned int *) loc = (unsigned int) v;
941 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
942 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
956 *(unsigned long *) loc += got - dot;
962 assert(isym != NULL);
964 if (!isym->gotent.inited)
966 isym->gotent.inited = 1;
967 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
969 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
970 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
971 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
972 *(unsigned short *) loc += isym->gotent.offset;
973 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
974 *(unsigned int *) loc += isym->gotent.offset;
977 #ifndef R_390_GOTOFF32
978 #define R_390_GOTOFF32 R_390_GOTOFF
985 #elif defined(__i386__)
1000 case R_386_JMP_SLOT:
1004 case R_386_RELATIVE:
1005 *loc += f->baseaddr;
1021 #elif defined(__x86_64__)
1031 *(unsigned int *) loc += v;
1034 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1035 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1040 *(signed int *) loc += v;
1044 *(unsigned short *) loc += v;
1048 *(unsigned char *) loc += v;
1052 *(unsigned int *) loc += v - dot;
1056 *(unsigned short *) loc += v - dot;
1060 *(unsigned char *) loc += v - dot;
1063 case R_X86_64_GLOB_DAT:
1064 case R_X86_64_JUMP_SLOT:
1068 case R_X86_64_RELATIVE:
1069 *loc += f->baseaddr;
1072 case R_X86_64_GOT32:
1073 case R_X86_64_GOTPCREL:
1076 assert(isym != NULL);
1077 if (!isym->gotent.reloc_done)
1079 isym->gotent.reloc_done = 1;
1080 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1082 /* XXX are these really correct? */
1083 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1084 *(unsigned int *) loc += v + isym->gotent.offset;
1086 *loc += isym->gotent.offset;
1090 #elif defined(__mc68000__)
1101 ret = obj_reloc_overflow;
1108 ret = obj_reloc_overflow;
1115 if ((ElfW(Sword))v > 0x7f ||
1116 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1117 ret = obj_reloc_overflow;
1124 if ((ElfW(Sword))v > 0x7fff ||
1125 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1126 ret = obj_reloc_overflow;
1132 *(int *)loc = v - dot;
1135 case R_68K_GLOB_DAT:
1136 case R_68K_JMP_SLOT:
1140 case R_68K_RELATIVE:
1141 *(int *)loc += f->baseaddr;
1154 #elif defined(__mips__)
1165 ret = obj_reloc_dangerous;
1166 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1167 ret = obj_reloc_overflow;
1169 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1175 struct mips_hi16 *n;
1177 /* We cannot relocate this one now because we don't know the value
1178 of the carry we need to add. Save the information, and let LO16
1179 do the actual relocation. */
1180 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1183 n->next = ifile->mips_hi16_list;
1184 ifile->mips_hi16_list = n;
1190 unsigned long insnlo = *loc;
1191 ElfW(Addr) val, vallo;
1193 /* Sign extend the addend we extract from the lo insn. */
1194 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1196 if (ifile->mips_hi16_list != NULL) {
1197 struct mips_hi16 *l;
1199 l = ifile->mips_hi16_list;
1201 struct mips_hi16 *next;
1204 /* The value for the HI16 had best be the same. */
1205 assert(v == l->value);
1207 /* Do the HI16 relocation. Note that we actually don't
1208 need to know anything about the LO16 itself, except where
1209 to find the low 16 bits of the addend needed by the LO16. */
1212 ((insn & 0xffff) << 16) +
1216 /* Account for the sign extension that will happen in the
1223 insn = (insn & ~0xffff) | val;
1231 ifile->mips_hi16_list = NULL;
1234 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1236 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1241 #elif defined(__powerpc__)
1243 case R_PPC_ADDR16_HA:
1244 *(unsigned short *)loc = (v + 0x8000) >> 16;
1247 case R_PPC_ADDR16_HI:
1248 *(unsigned short *)loc = v >> 16;
1251 case R_PPC_ADDR16_LO:
1252 *(unsigned short *)loc = v;
1266 #elif defined(__sh__)
1289 *loc = f->baseaddr + rel->r_addend;
1294 *loc = got - dot + rel->r_addend;
1305 #if defined(__SH5__)
1306 case R_SH_IMM_MEDLOW16:
1307 case R_SH_IMM_LOW16:
1311 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1315 * movi and shori have the format:
1317 * | op | imm | reg | reserved |
1318 * 31..26 25..10 9.. 4 3 .. 0
1320 * so we simply mask and or in imm.
1322 word = *loc & ~0x3fffc00;
1323 word |= (v & 0xffff) << 10;
1330 case R_SH_IMM_MEDLOW16_PCREL:
1331 case R_SH_IMM_LOW16_PCREL:
1335 word = *loc & ~0x3fffc00;
1339 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1342 word |= (v & 0xffff) << 10;
1348 #endif /* __SH5__ */
1352 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1353 ret = obj_reloc_unhandled;
1356 #if defined (__v850e__)
1361 /* We write two shorts instead of a long because even
1362 32-bit insns only need half-word alignment, but
1363 32-bit data needs to be long-word aligned. */
1364 v += ((unsigned short *)loc)[0];
1365 v += ((unsigned short *)loc)[1] << 16;
1366 ((unsigned short *)loc)[0] = v & 0xffff;
1367 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1370 case R_V850_22_PCREL:
1374 #if defined (__cris__)
1379 /* CRIS keeps the relocation value in the r_addend field and
1380 * should not use whats in *loc at all
1386 #if defined(__H8300H__) || defined(__H8300S__)
1388 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
1389 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
1400 if ((ElfW(Sword))v > 0x7fff ||
1401 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
1402 ret = obj_reloc_overflow;
1404 *(unsigned short *)loc = v;
1408 if ((ElfW(Sword))v > 0x7f ||
1409 (ElfW(Sword))v < -(ElfW(Sword))0x80)
1410 ret = obj_reloc_overflow;
1412 *(unsigned char *)loc = v;
1416 #if defined(CONFIG_USE_PLT_ENTRIES)
1420 /* find the plt entry and initialize it if necessary */
1421 assert(isym != NULL);
1423 #if defined(CONFIG_USE_PLT_LIST)
1424 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1432 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1434 /* generate some machine code */
1436 #if defined(__arm__)
1437 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1438 ip[1] = v; /* sym@ */
1440 #if defined(__powerpc__)
1441 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1442 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1443 ip[2] = 0x7d6903a6; /* mtctr r11 */
1444 ip[3] = 0x4e800420; /* bctr */
1446 #if defined (__v850e__)
1447 /* We have to trash a register, so we assume that any control
1448 transfer more than 21-bits away must be a function call
1449 (so we can use a call-clobbered register). */
1450 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1451 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1456 /* relative distance to target */
1458 /* if the target is too far away.... */
1459 #if defined (__arm__) || defined (__powerpc__)
1460 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1461 #elif defined (__v850e__)
1462 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1464 /* go via the plt */
1465 v = plt + pe->offset - dot;
1467 #if defined (__v850e__)
1472 ret = obj_reloc_dangerous;
1474 /* merge the offset into the instruction. */
1475 #if defined(__arm__)
1476 /* Convert to words. */
1479 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1481 #if defined(__powerpc__)
1482 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1484 #if defined (__v850e__)
1485 /* We write two shorts instead of a long because even 32-bit insns
1486 only need half-word alignment, but the 32-bit data write needs
1487 to be long-word aligned. */
1488 ((unsigned short *)loc)[0] =
1489 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1490 | ((v >> 16) & 0x3f); /* offs high part */
1491 ((unsigned short *)loc)[1] =
1492 (v & 0xffff); /* offs low part */
1495 #endif /* CONFIG_USE_PLT_ENTRIES */
1497 #if defined(CONFIG_USE_GOT_ENTRIES)
1500 assert(isym != NULL);
1501 /* needs an entry in the .got: set it, once */
1502 if (!isym->gotent.inited) {
1503 isym->gotent.inited = 1;
1504 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1506 /* make the reloc with_respect_to_.got */
1508 *loc += isym->gotent.offset + rel->r_addend;
1509 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1510 *loc += isym->gotent.offset;
1514 #endif /* CONFIG_USE_GOT_ENTRIES */
1521 #if defined(CONFIG_USE_LIST)
1523 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1524 int offset, int size)
1526 struct arch_list_entry *pe;
1528 for (pe = *list; pe != NULL; pe = pe->next) {
1529 if (pe->addend == rel->r_addend) {
1535 pe = xmalloc(sizeof(struct arch_list_entry));
1537 pe->addend = rel->r_addend;
1538 pe->offset = offset;
1548 #if defined(CONFIG_USE_SINGLE)
1550 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1551 int offset, int size)
1553 if (single->allocated == 0) {
1554 single->allocated = 1;
1555 single->offset = offset;
1564 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1566 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1567 int offset, int size)
1569 struct obj_section *myrelsec = obj_find_section(f, name);
1576 obj_extend_section(myrelsec, offset);
1578 myrelsec = obj_create_alloced_section(f, name,
1588 static void arch_create_got(struct obj_file *f)
1590 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1591 struct arch_file *ifile = (struct arch_file *) f;
1593 #if defined(CONFIG_USE_GOT_ENTRIES)
1594 int got_offset = 0, got_needed = 0, got_allocate;
1596 #if defined(CONFIG_USE_PLT_ENTRIES)
1597 int plt_offset = 0, plt_needed = 0, plt_allocate;
1599 struct obj_section *relsec, *symsec, *strsec;
1600 ElfW(RelM) *rel, *relend;
1601 ElfW(Sym) *symtab, *extsym;
1602 const char *strtab, *name;
1603 struct arch_symbol *intsym;
1605 for (i = 0; i < f->header.e_shnum; ++i) {
1606 relsec = f->sections[i];
1607 if (relsec->header.sh_type != SHT_RELM)
1610 symsec = f->sections[relsec->header.sh_link];
1611 strsec = f->sections[symsec->header.sh_link];
1613 rel = (ElfW(RelM) *) relsec->contents;
1614 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1615 symtab = (ElfW(Sym) *) symsec->contents;
1616 strtab = (const char *) strsec->contents;
1618 for (; rel < relend; ++rel) {
1619 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1621 #if defined(CONFIG_USE_GOT_ENTRIES)
1624 #if defined(CONFIG_USE_PLT_ENTRIES)
1628 switch (ELF_R_TYPE(rel->r_info)) {
1629 #if defined(__arm__)
1644 #elif defined(__i386__)
1654 #elif defined(__powerpc__)
1659 #elif defined(__mc68000__)
1670 #elif defined(__sh__)
1680 #elif defined (__v850e__)
1681 case R_V850_22_PCREL:
1690 if (extsym->st_name != 0) {
1691 name = strtab + extsym->st_name;
1693 name = f->sections[extsym->st_shndx]->name;
1695 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1696 #if defined(CONFIG_USE_GOT_ENTRIES)
1698 got_offset += arch_single_init(
1699 rel, &intsym->gotent,
1700 got_offset, CONFIG_GOT_ENTRY_SIZE);
1705 #if defined(CONFIG_USE_PLT_ENTRIES)
1707 #if defined(CONFIG_USE_PLT_LIST)
1708 plt_offset += arch_list_add(
1709 rel, &intsym->pltent,
1710 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1712 plt_offset += arch_single_init(
1713 rel, &intsym->pltent,
1714 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1722 #if defined(CONFIG_USE_GOT_ENTRIES)
1724 ifile->got = arch_xsect_init(f, ".got", got_offset,
1725 CONFIG_GOT_ENTRY_SIZE);
1729 #if defined(CONFIG_USE_PLT_ENTRIES)
1731 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1732 CONFIG_PLT_ENTRY_SIZE);
1736 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1739 /*======================================================================*/
1741 /* Standard ELF hash function. */
1742 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1744 unsigned long h = 0;
1751 if ((g = (h & 0xf0000000)) != 0) {
1760 static unsigned long obj_elf_hash(const char *name)
1762 return obj_elf_hash_n(name, strlen(name));
1765 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1766 /* String comparison for non-co-versioned kernel and module. */
1768 static int ncv_strcmp(const char *a, const char *b)
1770 size_t alen = strlen(a), blen = strlen(b);
1772 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1773 return strncmp(a, b, alen);
1774 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1775 return strncmp(a, b, blen);
1777 return strcmp(a, b);
1780 /* String hashing for non-co-versioned kernel and module. Here
1781 we are simply forced to drop the crc from the hash. */
1783 static unsigned long ncv_symbol_hash(const char *str)
1785 size_t len = strlen(str);
1786 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1788 return obj_elf_hash_n(str, len);
1792 obj_set_symbol_compare(struct obj_file *f,
1793 int (*cmp) (const char *, const char *),
1794 unsigned long (*hash) (const char *))
1797 f->symbol_cmp = cmp;
1799 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1802 f->symbol_hash = hash;
1804 memcpy(tmptab, f->symtab, sizeof(tmptab));
1805 memset(f->symtab, 0, sizeof(f->symtab));
1807 for (i = 0; i < HASH_BUCKETS; ++i)
1808 for (sym = tmptab[i]; sym; sym = next) {
1809 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1811 sym->next = f->symtab[h];
1817 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1819 static struct obj_symbol *
1820 obj_add_symbol(struct obj_file *f, const char *name,
1821 unsigned long symidx, int info,
1822 int secidx, ElfW(Addr) value,
1825 struct obj_symbol *sym;
1826 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1827 int n_type = ELF_ST_TYPE(info);
1828 int n_binding = ELF_ST_BIND(info);
1830 for (sym = f->symtab[hash]; sym; sym = sym->next)
1831 if (f->symbol_cmp(sym->name, name) == 0) {
1832 int o_secidx = sym->secidx;
1833 int o_info = sym->info;
1834 int o_type = ELF_ST_TYPE(o_info);
1835 int o_binding = ELF_ST_BIND(o_info);
1837 /* A redefinition! Is it legal? */
1839 if (secidx == SHN_UNDEF)
1841 else if (o_secidx == SHN_UNDEF)
1843 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1844 /* Cope with local and global symbols of the same name
1845 in the same object file, as might have been created
1846 by ld -r. The only reason locals are now seen at this
1847 level at all is so that we can do semi-sensible things
1850 struct obj_symbol *nsym, **p;
1852 nsym = arch_new_symbol();
1853 nsym->next = sym->next;
1856 /* Excise the old (local) symbol from the hash chain. */
1857 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1861 } else if (n_binding == STB_LOCAL) {
1862 /* Another symbol of the same name has already been defined.
1863 Just add this to the local table. */
1864 sym = arch_new_symbol();
1867 f->local_symtab[symidx] = sym;
1869 } else if (n_binding == STB_WEAK)
1871 else if (o_binding == STB_WEAK)
1873 /* Don't unify COMMON symbols with object types the programmer
1875 else if (secidx == SHN_COMMON
1876 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1878 else if (o_secidx == SHN_COMMON
1879 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1882 /* Don't report an error if the symbol is coming from
1883 the kernel or some external module. */
1884 if (secidx <= SHN_HIRESERVE)
1885 bb_error_msg("%s multiply defined", name);
1890 /* Completely new symbol. */
1891 sym = arch_new_symbol();
1892 sym->next = f->symtab[hash];
1893 f->symtab[hash] = sym;
1896 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
1897 if (symidx >= f->local_symtab_size)
1898 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1899 name, (long) symidx, (long) f->local_symtab_size);
1901 f->local_symtab[symidx] = sym;
1908 sym->secidx = secidx;
1914 static struct obj_symbol *
1915 obj_find_symbol(struct obj_file *f, const char *name)
1917 struct obj_symbol *sym;
1918 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1920 for (sym = f->symtab[hash]; sym; sym = sym->next)
1921 if (f->symbol_cmp(sym->name, name) == 0)
1928 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1931 if (sym->secidx >= SHN_LORESERVE)
1934 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1936 /* As a special case, a NULL sym has value zero. */
1941 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1943 int i, n = f->header.e_shnum;
1945 for (i = 0; i < n; ++i)
1946 if (strcmp(f->sections[i]->name, name) == 0)
1947 return f->sections[i];
1952 static int obj_load_order_prio(struct obj_section *a)
1954 unsigned long af, ac;
1956 af = a->header.sh_flags;
1959 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1960 strcmp(a->name + 5, ".init"))
1964 if (!(af & SHF_WRITE))
1966 if (af & SHF_EXECINSTR)
1968 if (a->header.sh_type != SHT_NOBITS)
1975 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1977 struct obj_section **p;
1978 int prio = obj_load_order_prio(sec);
1979 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1980 if (obj_load_order_prio(*p) < prio)
1982 sec->load_next = *p;
1986 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1988 unsigned long align,
1991 int newidx = f->header.e_shnum++;
1992 struct obj_section *sec;
1994 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1995 f->sections[newidx] = sec = arch_new_section();
1997 memset(sec, 0, sizeof(*sec));
1998 sec->header.sh_type = SHT_PROGBITS;
1999 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2000 sec->header.sh_size = size;
2001 sec->header.sh_addralign = align;
2005 sec->contents = xmalloc(size);
2007 obj_insert_section_load_order(f, sec);
2012 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2014 unsigned long align,
2017 int newidx = f->header.e_shnum++;
2018 struct obj_section *sec;
2020 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2021 f->sections[newidx] = sec = arch_new_section();
2023 memset(sec, 0, sizeof(*sec));
2024 sec->header.sh_type = SHT_PROGBITS;
2025 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2026 sec->header.sh_size = size;
2027 sec->header.sh_addralign = align;
2031 sec->contents = xmalloc(size);
2033 sec->load_next = f->load_order;
2034 f->load_order = sec;
2035 if (f->load_order_search_start == &f->load_order)
2036 f->load_order_search_start = &sec->load_next;
2041 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2043 unsigned long oldsize = sec->header.sh_size;
2045 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2047 return sec->contents + oldsize;
2051 /* Conditionally add the symbols from the given symbol set to the
2055 add_symbols_from( struct obj_file *f,
2056 int idx, struct new_module_symbol *syms, size_t nsyms)
2058 struct new_module_symbol *s;
2061 #ifdef SYMBOL_PREFIX
2063 size_t name_alloced_size = 0;
2065 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2068 gpl = obj_gpl_license(f, NULL) == 0;
2070 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2071 /* Only add symbols that are already marked external.
2072 If we override locals we may cause problems for
2073 argument initialization. We will also create a false
2074 dependency on the module. */
2075 struct obj_symbol *sym;
2078 /* GPL licensed modules can use symbols exported with
2079 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2080 * exported names. Non-GPL modules never see any GPLONLY_
2081 * symbols so they cannot fudge it by adding the prefix on
2084 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2085 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2092 name = (char *)s->name;
2094 #ifdef SYMBOL_PREFIX
2095 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2096 kernel exports `C names', but module object files
2097 reference `linker names'). */
2098 size_t extra = sizeof SYMBOL_PREFIX;
2099 size_t name_size = strlen (name) + extra;
2100 if (name_size > name_alloced_size) {
2101 name_alloced_size = name_size * 2;
2102 name_buf = alloca (name_alloced_size);
2104 strcpy (name_buf, SYMBOL_PREFIX);
2105 strcpy (name_buf + extra - 1, name);
2107 #endif /* SYMBOL_PREFIX */
2109 sym = obj_find_symbol(f, name);
2110 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2111 #ifdef SYMBOL_PREFIX
2112 /* Put NAME_BUF into more permanent storage. */
2113 name = xmalloc (name_size);
2114 strcpy (name, name_buf);
2116 sym = obj_add_symbol(f, name, -1,
2117 ELF_ST_INFO(STB_GLOBAL,
2120 /* Did our symbol just get installed? If so, mark the
2121 module as "used". */
2122 if (sym->secidx == idx)
2130 static void add_kernel_symbols(struct obj_file *f)
2132 struct external_module *m;
2135 /* Add module symbols first. */
2137 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2139 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2140 m->nsyms)) m->used = 1, ++nused;
2142 n_ext_modules_used = nused;
2144 /* And finally the symbols from the kernel proper. */
2147 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2150 static char *get_modinfo_value(struct obj_file *f, const char *key)
2152 struct obj_section *sec;
2153 char *p, *v, *n, *ep;
2154 size_t klen = strlen(key);
2156 sec = obj_find_section(f, ".modinfo");
2160 ep = p + sec->header.sh_size;
2163 n = strchr(p, '\0');
2165 if (p + klen == v && strncmp(p, key, klen) == 0)
2168 if (p + klen == n && strcmp(p, key) == 0)
2178 /*======================================================================*/
2179 /* Functions relating to module loading after 2.1.18. */
2182 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2185 char *p, *q, *key, *sym_name;
2186 struct obj_symbol *sym;
2187 char *contents, *loc;
2191 if ((q = strchr(p, '=')) == NULL) {
2196 key = alloca(q - p + 6);
2197 memcpy(key, "parm_", 5);
2198 memcpy(key + 5, p, q - p);
2201 p = get_modinfo_value(f, key);
2204 bb_error_msg("invalid parameter %s", key);
2208 #ifdef SYMBOL_PREFIX
2209 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2210 strcpy (sym_name, SYMBOL_PREFIX);
2211 strcat (sym_name, key);
2215 sym = obj_find_symbol(f, sym_name);
2217 /* Also check that the parameter was not resolved from the kernel. */
2218 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2219 bb_error_msg("symbol for parameter %s not found", key);
2224 min = strtoul(p, &p, 10);
2226 max = strtoul(p + 1, &p, 10);
2232 contents = f->sections[sym->secidx]->contents;
2233 loc = contents + sym->value;
2237 if ((*p == 's') || (*p == 'c')) {
2240 /* Do C quoting if we begin with a ", else slurp the lot. */
2244 str = alloca(strlen(q));
2245 for (r = str, q++; *q != '"'; ++q, ++r) {
2247 bb_error_msg("improperly terminated string argument for %s",
2250 } else if (*q == '\\')
2284 if (q[1] >= '0' && q[1] <= '7') {
2285 c = (c * 8) + *++q - '0';
2286 if (q[1] >= '0' && q[1] <= '7')
2287 c = (c * 8) + *++q - '0';
2304 /* In this case, the string is not quoted. We will break
2305 it using the coma (like for ints). If the user wants to
2306 include comas in a string, he just has to quote it */
2308 /* Search the next coma */
2312 if (r != (char *) NULL) {
2313 /* Recopy the current field */
2314 str = alloca(r - q + 1);
2315 memcpy(str, q, r - q);
2317 /* I don't know if it is useful, as the previous case
2318 doesn't nul terminate the string ??? */
2321 /* Keep next fields */
2332 obj_string_patch(f, sym->secidx, loc - contents, str);
2333 loc += tgt_sizeof_char_p;
2335 /* Array of chars (in fact, matrix !) */
2336 unsigned long charssize; /* size of each member */
2338 /* Get the size of each member */
2339 /* Probably we should do that outside the loop ? */
2340 if (!isdigit(*(p + 1))) {
2341 bb_error_msg("parameter type 'c' for %s must be followed by"
2342 " the maximum size", key);
2345 charssize = strtoul(p + 1, (char **) NULL, 10);
2348 if (strlen(str) >= charssize) {
2349 bb_error_msg("string too long for %s (max %ld)", key,
2354 /* Copy to location */
2355 strcpy((char *) loc, str);
2359 long v = strtoul(q, &q, 0);
2366 loc += tgt_sizeof_short;
2370 loc += tgt_sizeof_int;
2374 loc += tgt_sizeof_long;
2378 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2393 goto retry_end_of_value;
2397 bb_error_msg("too many values for %s (max %d)", key, max);
2404 bb_error_msg("invalid argument syntax for %s", key);
2411 bb_error_msg("too few values for %s (min %d)", key, min);
2421 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2422 static int new_is_module_checksummed(struct obj_file *f)
2424 const char *p = get_modinfo_value(f, "using_checksums");
2431 /* Get the module's kernel version in the canonical integer form. */
2434 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2439 p = get_modinfo_value(f, "kernel_version");
2442 safe_strncpy(str, p, STRVERSIONLEN);
2444 a = strtoul(p, &p, 10);
2447 b = strtoul(p + 1, &p, 10);
2450 c = strtoul(p + 1, &q, 10);
2454 return a << 16 | b << 8 | c;
2457 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2460 /* Fetch the loaded modules, and all currently exported symbols. */
2462 static int new_get_kernel_symbols(void)
2464 char *module_names, *mn;
2465 struct external_module *modules, *m;
2466 struct new_module_symbol *syms, *s;
2467 size_t ret, bufsize, nmod, nsyms, i, j;
2469 /* Collect the loaded modules. */
2471 module_names = xmalloc(bufsize = 256);
2473 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2474 if (errno == ENOSPC && bufsize < ret) {
2475 module_names = xrealloc(module_names, bufsize = ret);
2476 goto retry_modules_load;
2478 bb_perror_msg("QM_MODULES");
2482 n_ext_modules = nmod = ret;
2484 /* Collect the modules' symbols. */
2487 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2488 memset(modules, 0, nmod * sizeof(*modules));
2489 for (i = 0, mn = module_names, m = modules;
2490 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2491 struct new_module_info info;
2493 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2494 if (errno == ENOENT) {
2495 /* The module was removed out from underneath us. */
2498 bb_perror_msg("query_module: QM_INFO: %s", mn);
2502 syms = xmalloc(bufsize = 1024);
2504 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2507 syms = xrealloc(syms, bufsize = ret);
2508 goto retry_mod_sym_load;
2510 /* The module was removed out from underneath us. */
2513 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2520 m->addr = info.addr;
2524 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2525 s->name += (unsigned long) syms;
2530 /* Collect the kernel's symbols. */
2532 syms = xmalloc(bufsize = 16 * 1024);
2533 retry_kern_sym_load:
2534 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2535 if (errno == ENOSPC && bufsize < ret) {
2536 syms = xrealloc(syms, bufsize = ret);
2537 goto retry_kern_sym_load;
2539 bb_perror_msg("kernel: QM_SYMBOLS");
2542 nksyms = nsyms = ret;
2545 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2546 s->name += (unsigned long) syms;
2552 /* Return the kernel symbol checksum version, or zero if not used. */
2554 static int new_is_kernel_checksummed(void)
2556 struct new_module_symbol *s;
2559 /* Using_Versions is not the first symbol, but it should be in there. */
2561 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2562 if (strcmp((char *) s->name, "Using_Versions") == 0)
2569 static int new_create_this_module(struct obj_file *f, const char *m_name)
2571 struct obj_section *sec;
2573 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2574 sizeof(struct new_module));
2575 memset(sec->contents, 0, sizeof(struct new_module));
2577 obj_add_symbol(f, SPFX "__this_module", -1,
2578 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2579 sizeof(struct new_module));
2581 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2587 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2588 /* add an entry to the __ksymtab section, creating it if necessary */
2589 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2591 struct obj_section *sec;
2594 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2595 * If __ksymtab is defined but not marked alloc, x out the first character
2596 * (no obj_delete routine) and create a new __ksymtab with the correct
2599 sec = obj_find_section(f, "__ksymtab");
2600 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2601 *((char *)(sec->name)) = 'x'; /* override const */
2605 sec = obj_create_alloced_section(f, "__ksymtab",
2606 tgt_sizeof_void_p, 0);
2609 sec->header.sh_flags |= SHF_ALLOC;
2610 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2612 ofs = sec->header.sh_size;
2613 obj_symbol_patch(f, sec->idx, ofs, sym);
2614 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2615 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2617 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2619 static int new_create_module_ksymtab(struct obj_file *f)
2621 struct obj_section *sec;
2624 /* We must always add the module references. */
2626 if (n_ext_modules_used) {
2627 struct new_module_ref *dep;
2628 struct obj_symbol *tm;
2630 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2631 (sizeof(struct new_module_ref)
2632 * n_ext_modules_used));
2636 tm = obj_find_symbol(f, SPFX "__this_module");
2637 dep = (struct new_module_ref *) sec->contents;
2638 for (i = 0; i < n_ext_modules; ++i)
2639 if (ext_modules[i].used) {
2640 dep->dep = ext_modules[i].addr;
2641 obj_symbol_patch(f, sec->idx,
2642 (char *) &dep->ref - sec->contents, tm);
2648 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2653 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2656 /* We don't want to export symbols residing in sections that
2657 aren't loaded. There are a number of these created so that
2658 we make sure certain module options don't appear twice. */
2660 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2662 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2664 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2665 struct obj_symbol *sym;
2666 for (sym = f->symtab[i]; sym; sym = sym->next)
2667 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2668 && sym->secidx <= SHN_HIRESERVE
2669 && (sym->secidx >= SHN_LORESERVE
2670 || loaded[sym->secidx])) {
2671 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2673 obj_symbol_patch(f, sec->idx, ofs, sym);
2674 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2681 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2689 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2691 struct new_module *module;
2692 struct obj_section *sec;
2697 sec = obj_find_section(f, ".this");
2698 if (!sec || !sec->contents) {
2699 bb_perror_msg_and_die("corrupt module %s?",m_name);
2701 module = (struct new_module *) sec->contents;
2702 m_addr = sec->header.sh_addr;
2704 module->size_of_struct = sizeof(*module);
2705 module->size = m_size;
2706 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2708 sec = obj_find_section(f, "__ksymtab");
2709 if (sec && sec->header.sh_size) {
2710 module->syms = sec->header.sh_addr;
2711 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2714 if (n_ext_modules_used) {
2715 sec = obj_find_section(f, ".kmodtab");
2716 module->deps = sec->header.sh_addr;
2717 module->ndeps = n_ext_modules_used;
2721 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2723 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2725 sec = obj_find_section(f, "__ex_table");
2727 module->ex_table_start = sec->header.sh_addr;
2728 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2731 sec = obj_find_section(f, ".text.init");
2733 module->runsize = sec->header.sh_addr - m_addr;
2735 sec = obj_find_section(f, ".data.init");
2737 if (!module->runsize ||
2738 module->runsize > sec->header.sh_addr - m_addr)
2739 module->runsize = sec->header.sh_addr - m_addr;
2741 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2742 if (sec && sec->header.sh_size) {
2743 module->archdata_start = (void*)sec->header.sh_addr;
2744 module->archdata_end = module->archdata_start + sec->header.sh_size;
2746 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2747 if (sec && sec->header.sh_size) {
2748 module->kallsyms_start = (void*)sec->header.sh_addr;
2749 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2752 /* Whew! All of the initialization is complete. Collect the final
2753 module image and give it to the kernel. */
2755 image = xmalloc(m_size);
2756 obj_create_image(f, image);
2758 ret = init_module(m_name, (struct new_module *) image);
2760 bb_perror_msg("init_module: %s", m_name);
2768 /*======================================================================*/
2771 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2774 struct obj_string_patch *p;
2775 struct obj_section *strsec;
2776 size_t len = strlen(string) + 1;
2779 p = xmalloc(sizeof(*p));
2780 p->next = f->string_patches;
2781 p->reloc_secidx = secidx;
2782 p->reloc_offset = offset;
2783 f->string_patches = p;
2785 strsec = obj_find_section(f, ".kstrtab");
2786 if (strsec == NULL) {
2787 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2788 p->string_offset = 0;
2789 loc = strsec->contents;
2791 p->string_offset = strsec->header.sh_size;
2792 loc = obj_extend_section(strsec, len);
2794 memcpy(loc, string, len);
2800 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2801 struct obj_symbol *sym)
2803 struct obj_symbol_patch *p;
2805 p = xmalloc(sizeof(*p));
2806 p->next = f->symbol_patches;
2807 p->reloc_secidx = secidx;
2808 p->reloc_offset = offset;
2810 f->symbol_patches = p;
2815 static int obj_check_undefineds(struct obj_file *f)
2820 for (i = 0; i < HASH_BUCKETS; ++i) {
2821 struct obj_symbol *sym;
2822 for (sym = f->symtab[i]; sym; sym = sym->next)
2823 if (sym->secidx == SHN_UNDEF) {
2824 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
2825 sym->secidx = SHN_ABS;
2829 bb_error_msg("unresolved symbol %s", sym->name);
2839 static void obj_allocate_commons(struct obj_file *f)
2841 struct common_entry {
2842 struct common_entry *next;
2843 struct obj_symbol *sym;
2844 } *common_head = NULL;
2848 for (i = 0; i < HASH_BUCKETS; ++i) {
2849 struct obj_symbol *sym;
2850 for (sym = f->symtab[i]; sym; sym = sym->next)
2851 if (sym->secidx == SHN_COMMON) {
2852 /* Collect all COMMON symbols and sort them by size so as to
2853 minimize space wasted by alignment requirements. */
2855 struct common_entry **p, *n;
2856 for (p = &common_head; *p; p = &(*p)->next)
2857 if (sym->size <= (*p)->sym->size)
2860 n = alloca(sizeof(*n));
2868 for (i = 1; i < f->local_symtab_size; ++i) {
2869 struct obj_symbol *sym = f->local_symtab[i];
2870 if (sym && sym->secidx == SHN_COMMON) {
2871 struct common_entry **p, *n;
2872 for (p = &common_head; *p; p = &(*p)->next)
2873 if (sym == (*p)->sym)
2875 else if (sym->size < (*p)->sym->size) {
2876 n = alloca(sizeof(*n));
2886 /* Find the bss section. */
2887 for (i = 0; i < f->header.e_shnum; ++i)
2888 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2891 /* If for some reason there hadn't been one, create one. */
2892 if (i == f->header.e_shnum) {
2893 struct obj_section *sec;
2895 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2896 f->sections[i] = sec = arch_new_section();
2897 f->header.e_shnum = i + 1;
2899 memset(sec, 0, sizeof(*sec));
2900 sec->header.sh_type = SHT_PROGBITS;
2901 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2906 /* Allocate the COMMONS. */
2908 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2909 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2910 struct common_entry *c;
2912 for (c = common_head; c; c = c->next) {
2913 ElfW(Addr) align = c->sym->value;
2915 if (align > max_align)
2917 if (bss_size & (align - 1))
2918 bss_size = (bss_size | (align - 1)) + 1;
2921 c->sym->value = bss_size;
2923 bss_size += c->sym->size;
2926 f->sections[i]->header.sh_size = bss_size;
2927 f->sections[i]->header.sh_addralign = max_align;
2931 /* For the sake of patch relocation and parameter initialization,
2932 allocate zeroed data for NOBITS sections now. Note that after
2933 this we cannot assume NOBITS are really empty. */
2934 for (i = 0; i < f->header.e_shnum; ++i) {
2935 struct obj_section *s = f->sections[i];
2936 if (s->header.sh_type == SHT_NOBITS) {
2937 if (s->header.sh_size != 0)
2938 s->contents = memset(xmalloc(s->header.sh_size),
2939 0, s->header.sh_size);
2943 s->header.sh_type = SHT_PROGBITS;
2948 static unsigned long obj_load_size(struct obj_file *f)
2950 unsigned long dot = 0;
2951 struct obj_section *sec;
2953 /* Finalize the positions of the sections relative to one another. */
2955 for (sec = f->load_order; sec; sec = sec->load_next) {
2958 align = sec->header.sh_addralign;
2959 if (align && (dot & (align - 1)))
2960 dot = (dot | (align - 1)) + 1;
2962 sec->header.sh_addr = dot;
2963 dot += sec->header.sh_size;
2969 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2971 int i, n = f->header.e_shnum;
2974 /* Finalize the addresses of the sections. */
2977 for (i = 0; i < n; ++i)
2978 f->sections[i]->header.sh_addr += base;
2980 /* And iterate over all of the relocations. */
2982 for (i = 0; i < n; ++i) {
2983 struct obj_section *relsec, *symsec, *targsec, *strsec;
2984 ElfW(RelM) * rel, *relend;
2988 relsec = f->sections[i];
2989 if (relsec->header.sh_type != SHT_RELM)
2992 symsec = f->sections[relsec->header.sh_link];
2993 targsec = f->sections[relsec->header.sh_info];
2994 strsec = f->sections[symsec->header.sh_link];
2996 rel = (ElfW(RelM) *) relsec->contents;
2997 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2998 symtab = (ElfW(Sym) *) symsec->contents;
2999 strtab = (const char *) strsec->contents;
3001 for (; rel < relend; ++rel) {
3002 ElfW(Addr) value = 0;
3003 struct obj_symbol *intsym = NULL;
3004 unsigned long symndx;
3005 ElfW(Sym) * extsym = 0;
3008 /* Attempt to find a value to use for this relocation. */
3010 symndx = ELF_R_SYM(rel->r_info);
3012 /* Note we've already checked for undefined symbols. */
3014 extsym = &symtab[symndx];
3015 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3016 /* Local symbols we look up in the local table to be sure
3017 we get the one that is really intended. */
3018 intsym = f->local_symtab[symndx];
3020 /* Others we look up in the hash table. */
3022 if (extsym->st_name)
3023 name = strtab + extsym->st_name;
3025 name = f->sections[extsym->st_shndx]->name;
3026 intsym = obj_find_symbol(f, name);
3029 value = obj_symbol_final_value(f, intsym);
3030 intsym->referenced = 1;
3032 #if SHT_RELM == SHT_RELA
3033 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3034 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3035 if (!extsym || !extsym->st_name ||
3036 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3038 value += rel->r_addend;
3042 switch (arch_apply_relocation
3043 (f, targsec, symsec, intsym, rel, value)) {
3047 case obj_reloc_overflow:
3048 errmsg = "Relocation overflow";
3050 case obj_reloc_dangerous:
3051 errmsg = "Dangerous relocation";
3053 case obj_reloc_unhandled:
3054 errmsg = "Unhandled relocation";
3057 bb_error_msg("%s of type %ld for %s", errmsg,
3058 (long) ELF_R_TYPE(rel->r_info),
3059 strtab + extsym->st_name);
3061 bb_error_msg("%s of type %ld", errmsg,
3062 (long) ELF_R_TYPE(rel->r_info));
3070 /* Finally, take care of the patches. */
3072 if (f->string_patches) {
3073 struct obj_string_patch *p;
3074 struct obj_section *strsec;
3075 ElfW(Addr) strsec_base;
3076 strsec = obj_find_section(f, ".kstrtab");
3077 strsec_base = strsec->header.sh_addr;
3079 for (p = f->string_patches; p; p = p->next) {
3080 struct obj_section *targsec = f->sections[p->reloc_secidx];
3081 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3082 = strsec_base + p->string_offset;
3086 if (f->symbol_patches) {
3087 struct obj_symbol_patch *p;
3089 for (p = f->symbol_patches; p; p = p->next) {
3090 struct obj_section *targsec = f->sections[p->reloc_secidx];
3091 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3092 = obj_symbol_final_value(f, p->sym);
3099 static int obj_create_image(struct obj_file *f, char *image)
3101 struct obj_section *sec;
3102 ElfW(Addr) base = f->baseaddr;
3104 for (sec = f->load_order; sec; sec = sec->load_next) {
3107 if (sec->contents == 0 || sec->header.sh_size == 0)
3110 secimg = image + (sec->header.sh_addr - base);
3112 /* Note that we allocated data for NOBITS sections earlier. */
3113 memcpy(secimg, sec->contents, sec->header.sh_size);
3119 /*======================================================================*/
3121 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3124 ElfW(Shdr) * section_headers;
3128 /* Read the file header. */
3130 f = arch_new_file();
3131 memset(f, 0, sizeof(*f));
3132 f->symbol_cmp = strcmp;
3133 f->symbol_hash = obj_elf_hash;
3134 f->load_order_search_start = &f->load_order;
3136 fseek(fp, 0, SEEK_SET);
3137 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3138 bb_perror_msg("error reading ELF header");
3142 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3143 || f->header.e_ident[EI_MAG1] != ELFMAG1
3144 || f->header.e_ident[EI_MAG2] != ELFMAG2
3145 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3146 bb_error_msg("not an ELF file");
3149 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3150 || f->header.e_ident[EI_DATA] != ELFDATAM
3151 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3152 || !MATCH_MACHINE(f->header.e_machine)) {
3153 bb_error_msg("ELF file not for this architecture");
3156 if (f->header.e_type != ET_REL) {
3157 bb_error_msg("ELF file not a relocatable object");
3161 /* Read the section headers. */
3163 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3164 bb_error_msg("section header size mismatch: %lu != %lu",
3165 (unsigned long) f->header.e_shentsize,
3166 (unsigned long) sizeof(ElfW(Shdr)));
3170 shnum = f->header.e_shnum;
3171 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3172 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3174 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3175 fseek(fp, f->header.e_shoff, SEEK_SET);
3176 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3177 bb_perror_msg("error reading ELF section headers");
3181 /* Read the section data. */
3183 for (i = 0; i < shnum; ++i) {
3184 struct obj_section *sec;
3186 f->sections[i] = sec = arch_new_section();
3187 memset(sec, 0, sizeof(*sec));
3189 sec->header = section_headers[i];
3192 if(sec->header.sh_size) switch (sec->header.sh_type) {
3201 if (!loadprogbits) {
3202 sec->contents = NULL;
3209 if (sec->header.sh_size > 0) {
3210 sec->contents = xmalloc(sec->header.sh_size);
3211 fseek(fp, sec->header.sh_offset, SEEK_SET);
3212 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3213 bb_perror_msg("error reading ELF section data");
3217 sec->contents = NULL;
3221 #if SHT_RELM == SHT_REL
3223 bb_error_msg("RELA relocations not supported on this architecture");
3227 bb_error_msg("REL relocations not supported on this architecture");
3232 if (sec->header.sh_type >= SHT_LOPROC) {
3233 /* Assume processor specific section types are debug
3234 info and can safely be ignored. If this is ever not
3235 the case (Hello MIPS?), don't put ifdefs here but
3236 create an arch_load_proc_section(). */
3240 bb_error_msg("can't handle sections of type %ld",
3241 (long) sec->header.sh_type);
3246 /* Do what sort of interpretation as needed by each section. */
3248 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3250 for (i = 0; i < shnum; ++i) {
3251 struct obj_section *sec = f->sections[i];
3252 sec->name = shstrtab + sec->header.sh_name;
3255 for (i = 0; i < shnum; ++i) {
3256 struct obj_section *sec = f->sections[i];
3258 /* .modinfo should be contents only but gcc has no attribute for that.
3259 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3261 if (strcmp(sec->name, ".modinfo") == 0)
3262 sec->header.sh_flags &= ~SHF_ALLOC;
3264 if (sec->header.sh_flags & SHF_ALLOC)
3265 obj_insert_section_load_order(f, sec);
3267 switch (sec->header.sh_type) {
3270 unsigned long nsym, j;
3274 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3275 bb_error_msg("symbol size mismatch: %lu != %lu",
3276 (unsigned long) sec->header.sh_entsize,
3277 (unsigned long) sizeof(ElfW(Sym)));
3281 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3282 strtab = f->sections[sec->header.sh_link]->contents;
3283 sym = (ElfW(Sym) *) sec->contents;
3285 /* Allocate space for a table of local symbols. */
3286 j = f->local_symtab_size = sec->header.sh_info;
3287 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3289 /* Insert all symbols into the hash table. */
3290 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3291 ElfW(Addr) val = sym->st_value;
3294 name = strtab + sym->st_name;
3295 else if (sym->st_shndx < shnum)
3296 name = f->sections[sym->st_shndx]->name;
3300 #if defined(__SH5__)
3302 * For sh64 it is possible that the target of a branch
3303 * requires a mode switch (32 to 16 and back again).
3305 * This is implied by the lsb being set in the target
3306 * address for SHmedia mode and clear for SHcompact.
3308 val |= sym->st_other & 4;
3311 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3318 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3319 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3320 (unsigned long) sec->header.sh_entsize,
3321 (unsigned long) sizeof(ElfW(RelM)));
3325 /* XXX Relocation code from modutils-2.3.19 is not here.
3326 * Why? That's about 20 lines of code from obj/obj_load.c,
3327 * which gets done in a second pass through the sections.
3328 * This BusyBox insmod does similar work in obj_relocate(). */
3335 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3337 * load the unloaded sections directly into the memory allocated by
3338 * kernel for the module
3341 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3343 ElfW(Addr) base = f->baseaddr;
3344 struct obj_section* sec;
3346 for (sec = f->load_order; sec; sec = sec->load_next) {
3348 /* section already loaded? */
3349 if (sec->contents != NULL)
3352 if (sec->header.sh_size == 0)
3355 sec->contents = imagebase + (sec->header.sh_addr - base);
3356 fseek(fp, sec->header.sh_offset, SEEK_SET);
3357 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3358 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3367 static void hide_special_symbols(struct obj_file *f)
3369 static const char *const specials[] = {
3370 SPFX "cleanup_module",
3372 SPFX "kernel_version",
3376 struct obj_symbol *sym;
3377 const char *const *p;
3379 for (p = specials; *p; ++p)
3380 if ((sym = obj_find_symbol(f, *p)) != NULL)
3382 ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3386 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3387 static int obj_gpl_license(struct obj_file *f, const char **license)
3389 struct obj_section *sec;
3390 /* This list must match *exactly* the list of allowable licenses in
3391 * linux/include/linux/module.h. Checking for leading "GPL" will not
3392 * work, somebody will use "GPL sucks, this is proprietary".
3394 static const char *gpl_licenses[] = {
3397 "GPL and additional rights",
3402 if ((sec = obj_find_section(f, ".modinfo"))) {
3403 const char *value, *ptr, *endptr;
3404 ptr = sec->contents;
3405 endptr = ptr + sec->header.sh_size;
3406 while (ptr < endptr) {
3407 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3411 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3412 if (strcmp(value+1, gpl_licenses[i]) == 0)
3417 if (strchr(ptr, '\0'))
3418 ptr = strchr(ptr, '\0') + 1;
3426 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3427 #define TAINT_PROPRIETORY_MODULE (1<<0)
3428 #define TAINT_FORCED_MODULE (1<<1)
3429 #define TAINT_UNSAFE_SMP (1<<2)
3430 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3432 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3433 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3437 static int first = 1;
3438 if (fd < 0 && !kernel_has_tainted)
3439 return; /* New modutils on old kernel */
3440 printf("Warning: loading %s will taint the kernel: %s%s\n",
3441 m_name, text1, text2);
3443 printf(" See %s for information about tainted modules\n", TAINT_URL);
3447 read(fd, buf, sizeof(buf)-1);
3448 buf[sizeof(buf)-1] = '\0';
3449 oldval = strtoul(buf, NULL, 10);
3450 sprintf(buf, "%d\n", oldval | taint);
3451 write(fd, buf, strlen(buf));
3455 /* Check if loading this module will taint the kernel. */
3456 static void check_tainted_module(struct obj_file *f, char *m_name)
3458 static const char tainted_file[] = TAINT_FILENAME;
3459 int fd, kernel_has_tainted;
3462 kernel_has_tainted = 1;
3463 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3464 if (errno == ENOENT)
3465 kernel_has_tainted = 0;
3466 else if (errno == EACCES)
3467 kernel_has_tainted = 1;
3469 perror(tainted_file);
3470 kernel_has_tainted = 0;
3474 switch (obj_gpl_license(f, &ptr)) {
3478 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3481 /* The module has a non-GPL license so we pretend that the
3482 * kernel always has a taint flag to get a warning even on
3483 * kernels without the proc flag.
3485 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3488 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3492 if (flag_force_load)
3493 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3498 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3499 #define check_tainted_module(x, y) do { } while(0);
3500 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3502 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3503 /* add module source, timestamp, kernel version and a symbol for the
3504 * start of some sections. this info is used by ksymoops to do better
3508 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3510 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3511 return new_get_module_version(f, str);
3512 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3513 strncpy(str, "???", sizeof(str));
3515 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3518 /* add module source, timestamp, kernel version and a symbol for the
3519 * start of some sections. this info is used by ksymoops to do better
3523 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3526 static const char symprefix[] = "__insmod_";
3527 struct obj_section *sec;
3528 struct obj_symbol *sym;
3529 char *name, *absolute_filename;
3530 char str[STRVERSIONLEN], real[PATH_MAX];
3531 int i, l, lm_name, lfilename, use_ksymtab, version;
3532 struct stat statbuf;
3534 static const char *section_names[] = {
3542 if (realpath(filename, real)) {
3543 absolute_filename = bb_xstrdup(real);
3546 int save_errno = errno;
3547 bb_error_msg("cannot get realpath for %s", filename);
3550 absolute_filename = bb_xstrdup(filename);
3553 lm_name = strlen(m_name);
3554 lfilename = strlen(absolute_filename);
3556 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3557 * are not to be exported. otherwise leave ksymtab alone for now, the
3558 * "export all symbols" compatibility code will export these symbols later.
3560 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3562 if ((sec = obj_find_section(f, ".this"))) {
3563 /* tag the module header with the object name, last modified
3564 * timestamp and module version. worst case for module version
3565 * is 0xffffff, decimal 16777215. putting all three fields in
3566 * one symbol is less readable but saves kernel space.
3568 l = sizeof(symprefix)+ /* "__insmod_" */
3569 lm_name+ /* module name */
3571 lfilename+ /* object filename */
3573 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3575 8+ /* version in dec */
3578 if (stat(absolute_filename, &statbuf) != 0)
3579 statbuf.st_mtime = 0;
3580 version = get_module_version(f, str); /* -1 if not found */
3581 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3582 symprefix, m_name, absolute_filename,
3583 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3585 sym = obj_add_symbol(f, name, -1,
3586 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3587 sec->idx, sec->header.sh_addr, 0);
3589 new_add_ksymtab(f, sym);
3591 free(absolute_filename);
3592 #ifdef _NOT_SUPPORTED_
3593 /* record where the persistent data is going, same address as previous symbol */
3596 l = sizeof(symprefix)+ /* "__insmod_" */
3597 lm_name+ /* module name */
3599 strlen(f->persist)+ /* data store */
3602 snprintf(name, l, "%s%s_P%s",
3603 symprefix, m_name, f->persist);
3604 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3605 sec->idx, sec->header.sh_addr, 0);
3607 new_add_ksymtab(f, sym);
3609 #endif /* _NOT_SUPPORTED_ */
3610 /* tag the desired sections if size is non-zero */
3612 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3613 if ((sec = obj_find_section(f, section_names[i])) &&
3614 sec->header.sh_size) {
3615 l = sizeof(symprefix)+ /* "__insmod_" */
3616 lm_name+ /* module name */
3618 strlen(sec->name)+ /* section name */
3620 8+ /* length in dec */
3623 snprintf(name, l, "%s%s_S%s_L%ld",
3624 symprefix, m_name, sec->name,
3625 (long)sec->header.sh_size);
3626 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3627 sec->idx, sec->header.sh_addr, 0);
3629 new_add_ksymtab(f, sym);
3633 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3635 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3636 static void print_load_map(struct obj_file *f)
3638 struct obj_symbol *sym;
3639 struct obj_symbol **all, **p;
3640 struct obj_section *sec;
3641 int i, nsyms, *loaded;
3643 /* Report on the section layout. */
3645 printf("Sections: Size %-*s Align\n",
3646 (int) (2 * sizeof(void *)), "Address");
3648 for (sec = f->load_order; sec; sec = sec->load_next) {
3652 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3657 printf("%-15s %08lx %0*lx 2**%d\n",
3659 (long)sec->header.sh_size,
3660 (int) (2 * sizeof(void *)),
3661 (long)sec->header.sh_addr,
3664 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3665 /* Quick reference which section indicies are loaded. */
3667 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3669 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3671 /* Collect the symbols we'll be listing. */
3673 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3674 for (sym = f->symtab[i]; sym; sym = sym->next)
3675 if (sym->secidx <= SHN_HIRESERVE
3676 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3679 all = alloca(nsyms * sizeof(struct obj_symbol *));
3681 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3682 for (sym = f->symtab[i]; sym; sym = sym->next)
3683 if (sym->secidx <= SHN_HIRESERVE
3684 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3687 /* And list them. */
3688 printf("\nSymbols:\n");
3689 for (p = all; p < all + nsyms; ++p) {
3691 unsigned long value;
3694 if (sym->secidx == SHN_ABS) {
3697 } else if (sym->secidx == SHN_UNDEF) {
3701 sec = f->sections[sym->secidx];
3703 if (sec->header.sh_type == SHT_NOBITS)
3705 else if (sec->header.sh_flags & SHF_ALLOC) {
3706 if (sec->header.sh_flags & SHF_EXECINSTR)
3708 else if (sec->header.sh_flags & SHF_WRITE)
3713 value = sym->value + sec->header.sh_addr;
3716 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3717 type = tolower(type);
3719 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3727 extern int insmod_main( int argc, char **argv)
3733 unsigned long m_size;
3738 int exit_status = EXIT_FAILURE;
3740 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3741 struct utsname uts_info;
3742 char m_strversion[STRVERSIONLEN];
3743 int m_version, m_crcs;
3745 #ifdef CONFIG_FEATURE_CLEAN_UP
3750 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3751 int flag_print_load_map = 0;
3754 struct utsname myuname;
3756 /* Parse any options */
3757 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3758 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
3760 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
3764 case 'f': /* force loading */
3765 flag_force_load = 1;
3767 case 'k': /* module loaded by kerneld, auto-cleanable */
3770 case 's': /* log to syslog */
3771 /* log to syslog -- not supported */
3772 /* but kernel needs this for request_module(), */
3773 /* as this calls: modprobe -k -s -- <module> */
3774 /* so silently ignore this flag */
3776 case 'v': /* verbose output */
3779 case 'q': /* silent */
3782 case 'x': /* do not export externs */
3785 case 'o': /* name the output module */
3787 m_name = bb_xstrdup(optarg);
3789 case 'L': /* Stub warning */
3790 /* This is needed for compatibility with modprobe.
3791 * In theory, this does locking, but we don't do
3792 * that. So be careful and plan your life around not
3793 * loading the same module 50 times concurrently. */
3795 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3796 case 'm': /* print module load map */
3797 flag_print_load_map = 1;
3805 if (argv[optind] == NULL) {
3809 /* Grab the module name */
3810 tmp1 = bb_xstrdup(argv[optind]);
3811 tmp = basename(tmp1);
3814 if (uname(&myuname) == 0) {
3815 if (myuname.release[0] == '2') {
3816 k_version = myuname.release[2] - '0';
3820 #if defined(CONFIG_FEATURE_2_6_MODULES)
3821 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
3822 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
3828 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3834 #if defined(CONFIG_FEATURE_2_6_MODULES)
3836 m_fullName = bb_xasprintf("%s.ko", tmp);
3839 m_fullName = bb_xasprintf("%s.o", tmp);
3845 tmp1 = 0; /* flag for free(m_name) before exit() */
3848 /* Get a filedesc for the module. Check we we have a complete path */
3849 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3850 (fp = fopen(argv[optind], "r")) == NULL) {
3851 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3852 * but do not error out yet if we fail to find it... */
3853 if (k_version) { /* uname succeedd */
3856 char real_module_dir[FILENAME_MAX];
3858 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
3859 /* Jump through hoops in case /lib/modules/`uname -r`
3860 * is a symlink. We do not want recursive_action to
3861 * follow symlinks, but we do want to follow the
3862 * /lib/modules/`uname -r` dir, So resolve it ourselves
3863 * if it is a link... */
3864 if (realpath (tmdn, real_module_dir) == NULL)
3867 module_dir = real_module_dir;
3868 recursive_action(module_dir, TRUE, FALSE, FALSE,
3869 check_module_name_match, 0, m_fullName);
3873 /* Check if we have found anything yet */
3874 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
3876 char module_dir[FILENAME_MAX];
3880 if (realpath (_PATH_MODULES, module_dir) == NULL)
3881 strcpy(module_dir, _PATH_MODULES);
3882 /* No module found under /lib/modules/`uname -r`, this
3883 * time cast the net a bit wider. Search /lib/modules/ */
3884 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3885 check_module_name_match, 0, m_fullName))
3888 || ((fp = fopen(m_filename, "r")) == NULL))
3890 bb_error_msg("%s: no module by that name found", m_fullName);
3894 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
3897 m_filename = bb_xstrdup(argv[optind]);
3900 printf("Using %s\n", m_filename);
3902 #ifdef CONFIG_FEATURE_2_6_MODULES
3906 argv[optind + 1] = m_filename;
3907 return insmod_ng_main(argc - optind, argv + optind);
3911 if ((f = obj_load(fp, LOADBITS)) == NULL)
3912 bb_perror_msg_and_die("Could not load the module");
3914 if (get_modinfo_value(f, "kernel_version") == NULL)
3919 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3920 /* Version correspondence? */
3922 if (uname(&uts_info) < 0)
3923 uts_info.release[0] = '\0';
3924 if (m_has_modinfo) {
3925 m_version = new_get_module_version(f, m_strversion);
3926 if (m_version == -1) {
3927 bb_error_msg("couldn't find the kernel version the module was "
3933 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3934 if (flag_force_load) {
3935 bb_error_msg("Warning: kernel-module version mismatch\n"
3936 "\t%s was compiled for kernel version %s\n"
3937 "\twhile this kernel is version %s",
3938 m_filename, m_strversion, uts_info.release);
3940 bb_error_msg("kernel-module version mismatch\n"
3941 "\t%s was compiled for kernel version %s\n"
3942 "\twhile this kernel is version %s.",
3943 m_filename, m_strversion, uts_info.release);
3949 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3951 if (!query_module(NULL, 0, NULL, 0, NULL)) {
3952 if (!new_get_kernel_symbols())
3954 k_crcs = new_is_kernel_checksummed();
3956 bb_error_msg("Not configured to support old kernels");
3960 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3963 m_crcs = new_is_module_checksummed(f);
3965 if (m_crcs != k_crcs)
3966 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3967 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3969 /* Let the module know about the kernel symbols. */
3970 add_kernel_symbols(f);
3972 /* Allocate common symbols, symbol tables, and string tables. */
3974 if (!new_create_this_module(f, m_name))
3979 if (!obj_check_undefineds(f)) {
3982 obj_allocate_commons(f);
3983 check_tainted_module(f, m_name);
3985 /* done with the module name, on to the optional var=value arguments */
3988 if (optind < argc) {
3989 if (!new_process_module_arguments(f, argc - optind, argv + optind))
3996 hide_special_symbols(f);
3998 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3999 add_ksymoops_symbols(f, m_filename, m_name);
4000 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4002 new_create_module_ksymtab(f);
4004 /* Find current size of the module */
4005 m_size = obj_load_size(f);
4008 m_addr = create_module(m_name, m_size);
4009 if (m_addr == -1) switch (errno) {
4011 bb_error_msg("A module named %s already exists", m_name);
4014 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4018 bb_perror_msg("create_module: %s", m_name);
4024 * the PROGBITS section was not loaded by the obj_load
4025 * now we can load them directly into the kernel memory
4027 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4028 delete_module(m_name);
4033 if (!obj_relocate(f, m_addr)) {
4034 delete_module(m_name);
4038 if (!new_init_module(m_name, f, m_size))
4040 delete_module(m_name);
4044 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4045 if(flag_print_load_map)
4049 exit_status = EXIT_SUCCESS;
4052 #ifdef CONFIG_FEATURE_CLEAN_UP
4062 return(exit_status);
4069 #ifdef CONFIG_FEATURE_2_6_MODULES
4071 #include <sys/mman.h>
4072 #include <asm/unistd.h>
4073 #include <sys/syscall.h>
4075 /* We use error numbers in a loose translation... */
4076 static const char *moderror(int err)
4080 return "Invalid module format";
4082 return "Unknown symbol in module";
4084 return "Module has wrong symbol version";
4086 return "Invalid parameters";
4088 return strerror(err);
4092 extern int insmod_ng_main( int argc, char **argv)
4100 char *filename, *options = bb_xstrdup("");
4108 /* Rest is options */
4109 for (i = 2; i < argc; i++) {
4110 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4111 /* Spaces handled by "" pairs, but no way of escaping quotes */
4112 if (strchr(argv[i], ' ')) {
4113 strcat(options, "\"");
4114 strcat(options, argv[i]);
4115 strcat(options, "\"");
4117 strcat(options, argv[i]);
4119 strcat(options, " ");
4122 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4123 bb_perror_msg_and_die("cannot open module `%s'", filename);
4128 map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
4129 if (map == MAP_FAILED) {
4130 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4133 ret = syscall(__NR_init_module, map, len, options);
4135 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4136 filename, moderror(errno), ret);