1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * This version of insmod supports x86, ARM, SH3/4/5, powerpc, m68k,
6 * MIPS, v850e, and H8/300.
8 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
9 * and Ron Alder <alder@lineo.com>
11 * Miles Bader <miles@gnu.org> added NEC V850E support.
13 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
14 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
16 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
17 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
18 * very minor changes required to also work with StrongArm and presumably
19 * all ARM based systems.
21 * Yoshinori Sato <ysato@users.sourceforge.jp> 19-May-2004.
22 * added Renesas H8/300 support.
24 * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
25 * Integrated support for sh64 (SH-5), from preliminary modutils
26 * patches from Benedict Gaster <benedict.gaster@superh.com>.
27 * Currently limited to support for 32bit ABI.
29 * Magnus Damm <damm@opensource.se> 22-May-2002.
30 * The plt and got code are now using the same structs.
31 * Added generic linked list code to fully support PowerPC.
32 * Replaced the mess in arch_apply_relocation() with architecture blocks.
33 * The arch_create_got() function got cleaned up with architecture blocks.
34 * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
36 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
37 * PowerPC specific code stolen from modutils-2.3.16,
38 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
39 * I've only tested the code on mpc8xx platforms in big-endian mode.
40 * Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
42 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
43 * based on modutils-2.4.2
44 * MIPS specific support for Elf loading and relocation.
45 * Copyright 1996, 1997 Linux International.
46 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
48 * Based almost entirely on the Linux modutils-2.3.11 implementation.
49 * Copyright 1996, 1997 Linux International.
50 * New implementation contributed by Richard Henderson <rth@tamu.edu>
51 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
52 * Restructured (and partly rewritten) by:
53 * Björn Ekwall <bj0rn@blox.se> February 1999
55 * This program is free software; you can redistribute it and/or modify
56 * it under the terms of the GNU General Public License as published by
57 * the Free Software Foundation; either version 2 of the License, or
58 * (at your option) any later version.
60 * This program is distributed in the hope that it will be useful,
61 * but WITHOUT ANY WARRANTY; without even the implied warranty of
62 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
63 * General Public License for more details.
65 * You should have received a copy of the GNU General Public License
66 * along with this program; if not, write to the Free Software
67 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
82 #include <sys/utsname.h>
85 #if !defined(CONFIG_FEATURE_2_4_MODULES) && \
86 !defined(CONFIG_FEATURE_2_2_MODULES) && \
87 !defined(CONFIG_FEATURE_2_6_MODULES)
88 #define CONFIG_FEATURE_2_4_MODULES
91 #if !defined(CONFIG_FEATURE_2_4_MODULES) && !defined(CONFIG_FEATURE_2_2_MODULES)
92 #define insmod_ng_main insmod_main
95 #if defined(CONFIG_FEATURE_2_4_MODULES) || defined(CONFIG_FEATURE_2_2_MODULES)
97 #if defined(CONFIG_FEATURE_2_6_MODULES)
98 extern int insmod_ng_main( int argc, char **argv);
101 #ifdef CONFIG_FEATURE_2_4_MODULES
102 # undef CONFIG_FEATURE_2_2_MODULES
103 # define new_sys_init_module init_module
105 # define old_sys_init_module init_module
108 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
116 #define CONFIG_USE_PLT_ENTRIES
117 #define CONFIG_PLT_ENTRY_SIZE 8
118 #define CONFIG_USE_GOT_ENTRIES
119 #define CONFIG_GOT_ENTRY_SIZE 8
120 #define CONFIG_USE_SINGLE
122 #define MATCH_MACHINE(x) (x == EM_ARM)
123 #define SHT_RELM SHT_REL
124 #define Elf32_RelM Elf32_Rel
125 #define ELFCLASSM ELFCLASS32
128 #if defined(__s390__)
129 #define CONFIG_USE_PLT_ENTRIES
130 #define CONFIG_PLT_ENTRY_SIZE 8
131 #define CONFIG_USE_GOT_ENTRIES
132 #define CONFIG_GOT_ENTRY_SIZE 8
133 #define CONFIG_USE_SINGLE
135 #define MATCH_MACHINE(x) (x == EM_S390)
136 #define SHT_RELM SHT_RELA
137 #define Elf32_RelM Elf32_Rela
138 #define ELFCLASSM ELFCLASS32
141 #if defined(__i386__)
142 #define CONFIG_USE_GOT_ENTRIES
143 #define CONFIG_GOT_ENTRY_SIZE 4
144 #define CONFIG_USE_SINGLE
147 #define MATCH_MACHINE(x) (x == EM_386)
149 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
152 #define SHT_RELM SHT_REL
153 #define Elf32_RelM Elf32_Rel
154 #define ELFCLASSM ELFCLASS32
157 #if defined(__mc68000__)
158 #define CONFIG_USE_GOT_ENTRIES
159 #define CONFIG_GOT_ENTRY_SIZE 4
160 #define CONFIG_USE_SINGLE
162 #define MATCH_MACHINE(x) (x == EM_68K)
163 #define SHT_RELM SHT_RELA
164 #define Elf32_RelM Elf32_Rela
165 #define ELFCLASSM ELFCLASS32
168 #if defined(__mips__)
169 /* Account for ELF spec changes. */
170 #ifndef EM_MIPS_RS3_LE
171 #ifdef EM_MIPS_RS4_BE
172 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
174 #define EM_MIPS_RS3_LE 10
176 #endif /* !EM_MIPS_RS3_LE */
178 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
179 #define SHT_RELM SHT_REL
180 #define Elf32_RelM Elf32_Rel
181 #define ELFCLASSM ELFCLASS32
182 #define ARCHDATAM "__dbe_table"
185 #if defined(__powerpc__)
186 #define CONFIG_USE_PLT_ENTRIES
187 #define CONFIG_PLT_ENTRY_SIZE 16
188 #define CONFIG_USE_PLT_LIST
189 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
190 #define CONFIG_USE_LIST
192 #define MATCH_MACHINE(x) (x == EM_PPC)
193 #define SHT_RELM SHT_RELA
194 #define Elf32_RelM Elf32_Rela
195 #define ELFCLASSM ELFCLASS32
196 #define ARCHDATAM "__ftr_fixup"
200 #define CONFIG_USE_GOT_ENTRIES
201 #define CONFIG_GOT_ENTRY_SIZE 4
202 #define CONFIG_USE_SINGLE
204 #define MATCH_MACHINE(x) (x == EM_SH)
205 #define SHT_RELM SHT_RELA
206 #define Elf32_RelM Elf32_Rela
207 #define ELFCLASSM ELFCLASS32
209 /* the SH changes have only been tested in =little endian= mode */
210 /* I'm not sure about big endian, so let's warn: */
212 #if defined(__sh__) && defined(__BIG_ENDIAN__)
213 #error insmod.c may require changes for use on big endian SH
216 /* it may or may not work on the SH1/SH2... So let's error on those
218 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && \
220 #error insmod.c may require changes for SH1 or SH2 use
224 #if defined (__v850e__)
225 #define CONFIG_USE_PLT_ENTRIES
226 #define CONFIG_PLT_ENTRY_SIZE 8
227 #define CONFIG_USE_SINGLE
229 #ifndef EM_CYGNUS_V850 /* grumble */
230 #define EM_CYGNUS_V850 0x9080
233 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
234 #define SHT_RELM SHT_RELA
235 #define Elf32_RelM Elf32_Rela
236 #define ELFCLASSM ELFCLASS32
238 #define SYMBOL_PREFIX "_"
241 #if defined(__cris__)
244 #define R_CRIS_NONE 0
248 #define MATCH_MACHINE(x) (x == EM_CRIS)
249 #define SHT_RELM SHT_RELA
250 #define Elf32_RelM Elf32_Rela
251 #define ELFCLASSM ELFCLASS32
254 #if defined(__H8300H__) || defined(__H8300S__)
255 #define CONFIG_USE_SINGLE
257 #define MATCH_MACHINE(x) (x == EM_H8_300)
258 #define SHT_RELM SHT_RELA
259 #define Elf32_RelM Elf32_Rela
261 #define ELFCLASSM ELFCLASS32
262 #define SYMBOL_PREFIX "_"
266 #error Sorry, but insmod.c does not yet support this architecture...
270 //----------------------------------------------------------------------------
271 //--------modutils module.h, lines 45-242
272 //----------------------------------------------------------------------------
274 /* Definitions for the Linux module syscall interface.
275 Copyright 1996, 1997 Linux International.
277 Contributed by Richard Henderson <rth@tamu.edu>
279 This file is part of the Linux modutils.
281 This program is free software; you can redistribute it and/or modify it
282 under the terms of the GNU General Public License as published by the
283 Free Software Foundation; either version 2 of the License, or (at your
284 option) any later version.
286 This program is distributed in the hope that it will be useful, but
287 WITHOUT ANY WARRANTY; without even the implied warranty of
288 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
289 General Public License for more details.
291 You should have received a copy of the GNU General Public License
292 along with this program; if not, write to the Free Software Foundation,
293 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
296 #ifndef MODUTILS_MODULE_H
297 static const int MODUTILS_MODULE_H = 1;
299 #ident "$Id: insmod.c,v 1.118 2004/05/26 11:38:46 andersen Exp $"
301 /* This file contains the structures used by the 2.0 and 2.1 kernels.
302 We do not use the kernel headers directly because we do not wish
303 to be dependent on a particular kernel version to compile insmod. */
306 /*======================================================================*/
307 /* The structures used by Linux 2.0. */
309 /* The symbol format used by get_kernel_syms(2). */
310 struct old_kernel_sym
316 struct old_module_ref
318 unsigned long module; /* kernel addresses */
322 struct old_module_symbol
328 struct old_symbol_table
330 int size; /* total, including string table!!! */
333 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
334 struct old_module_ref ref[0]; /* actual size defined by n_refs */
337 struct old_mod_routines
340 unsigned long cleanup;
346 unsigned long ref; /* the list of modules that refer to me */
347 unsigned long symtab;
349 int size; /* size of module in pages */
350 unsigned long addr; /* address of module */
352 unsigned long cleanup; /* cleanup routine */
355 /* Sent to init_module(2) or'ed into the code size parameter. */
356 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
358 int get_kernel_syms(struct old_kernel_sym *);
359 int old_sys_init_module(const char *name, char *code, unsigned codesize,
360 struct old_mod_routines *, struct old_symbol_table *);
362 /*======================================================================*/
363 /* For sizeof() which are related to the module platform and not to the
364 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
366 #define tgt_sizeof_char sizeof(char)
367 #define tgt_sizeof_short sizeof(short)
368 #define tgt_sizeof_int sizeof(int)
369 #define tgt_sizeof_long sizeof(long)
370 #define tgt_sizeof_char_p sizeof(char *)
371 #define tgt_sizeof_void_p sizeof(void *)
372 #define tgt_long long
374 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
375 #undef tgt_sizeof_long
376 #undef tgt_sizeof_char_p
377 #undef tgt_sizeof_void_p
379 static const int tgt_sizeof_long = 8;
380 static const int tgt_sizeof_char_p = 8;
381 static const int tgt_sizeof_void_p = 8;
382 #define tgt_long long long
385 /*======================================================================*/
386 /* The structures used in Linux 2.1. */
388 /* Note: new_module_symbol does not use tgt_long intentionally */
389 struct new_module_symbol
395 struct new_module_persist;
397 struct new_module_ref
399 unsigned tgt_long dep; /* kernel addresses */
400 unsigned tgt_long ref;
401 unsigned tgt_long next_ref;
406 unsigned tgt_long size_of_struct; /* == sizeof(module) */
407 unsigned tgt_long next;
408 unsigned tgt_long name;
409 unsigned tgt_long size;
412 unsigned tgt_long flags; /* AUTOCLEAN et al */
417 unsigned tgt_long syms;
418 unsigned tgt_long deps;
419 unsigned tgt_long refs;
420 unsigned tgt_long init;
421 unsigned tgt_long cleanup;
422 unsigned tgt_long ex_table_start;
423 unsigned tgt_long ex_table_end;
425 unsigned tgt_long gp;
427 /* Everything after here is extension. */
428 unsigned tgt_long persist_start;
429 unsigned tgt_long persist_end;
430 unsigned tgt_long can_unload;
431 unsigned tgt_long runsize;
432 #ifdef CONFIG_FEATURE_2_4_MODULES
433 const char *kallsyms_start; /* All symbols for kernel debugging */
434 const char *kallsyms_end;
435 const char *archdata_start; /* arch specific data for module */
436 const char *archdata_end;
437 const char *kernel_data; /* Reserved for kernel internal use */
442 #define ARCHDATA_SEC_NAME ARCHDATAM
444 #define ARCHDATA_SEC_NAME "__archdata"
446 #define KALLSYMS_SEC_NAME "__kallsyms"
449 struct new_module_info
457 /* Bits of module.flags. */
458 static const int NEW_MOD_RUNNING = 1;
459 static const int NEW_MOD_DELETED = 2;
460 static const int NEW_MOD_AUTOCLEAN = 4;
461 static const int NEW_MOD_VISITED = 8;
462 static const int NEW_MOD_USED_ONCE = 16;
464 int new_sys_init_module(const char *name, const struct new_module *);
465 int query_module(const char *name, int which, void *buf, size_t bufsize,
468 /* Values for query_module's which. */
470 static const int QM_MODULES = 1;
471 static const int QM_DEPS = 2;
472 static const int QM_REFS = 3;
473 static const int QM_SYMBOLS = 4;
474 static const int QM_INFO = 5;
476 /*======================================================================*/
477 /* The system calls unchanged between 2.0 and 2.1. */
479 unsigned long create_module(const char *, size_t);
480 int delete_module(const char *);
483 #endif /* module.h */
485 //----------------------------------------------------------------------------
486 //--------end of modutils module.h
487 //----------------------------------------------------------------------------
491 //----------------------------------------------------------------------------
492 //--------modutils obj.h, lines 253-462
493 //----------------------------------------------------------------------------
495 /* Elf object file loading and relocation routines.
496 Copyright 1996, 1997 Linux International.
498 Contributed by Richard Henderson <rth@tamu.edu>
500 This file is part of the Linux modutils.
502 This program is free software; you can redistribute it and/or modify it
503 under the terms of the GNU General Public License as published by the
504 Free Software Foundation; either version 2 of the License, or (at your
505 option) any later version.
507 This program is distributed in the hope that it will be useful, but
508 WITHOUT ANY WARRANTY; without even the implied warranty of
509 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
510 General Public License for more details.
512 You should have received a copy of the GNU General Public License
513 along with this program; if not, write to the Free Software Foundation,
514 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
517 #ifndef MODUTILS_OBJ_H
518 static const int MODUTILS_OBJ_H = 1;
520 #ident "$Id: insmod.c,v 1.118 2004/05/26 11:38:46 andersen Exp $"
522 /* The relocatable object is manipulated using elfin types. */
528 #if __BYTE_ORDER == __LITTLE_ENDIAN
529 #define ELFDATAM ELFDATA2LSB
530 #elif __BYTE_ORDER == __BIG_ENDIAN
531 #define ELFDATAM ELFDATA2MSB
535 # if ELFCLASSM == ELFCLASS32
536 # define ElfW(x) Elf32_ ## x
537 # define ELFW(x) ELF32_ ## x
539 # define ElfW(x) Elf64_ ## x
540 # define ELFW(x) ELF64_ ## x
544 /* For some reason this is missing from some ancient C libraries.... */
545 #ifndef ELF32_ST_INFO
546 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
549 #ifndef ELF64_ST_INFO
550 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
553 struct obj_string_patch;
554 struct obj_symbol_patch;
561 struct obj_section *load_next;
567 struct obj_symbol *next; /* hash table link */
571 int secidx; /* the defining section index/module */
573 int ksymidx; /* for export to the kernel symtab */
574 int referenced; /* actually used in the link */
577 /* Hardcode the hash table size. We shouldn't be needing so many
578 symbols that we begin to degrade performance, and we get a big win
579 by giving the compiler a constant divisor. */
581 #define HASH_BUCKETS 521
587 struct obj_section **sections;
588 struct obj_section *load_order;
589 struct obj_section **load_order_search_start;
590 struct obj_string_patch *string_patches;
591 struct obj_symbol_patch *symbol_patches;
592 int (*symbol_cmp)(const char *, const char *);
593 unsigned long (*symbol_hash)(const char *);
594 unsigned long local_symtab_size;
595 struct obj_symbol **local_symtab;
596 struct obj_symbol *symtab[HASH_BUCKETS];
607 struct obj_string_patch
609 struct obj_string_patch *next;
611 ElfW(Addr) reloc_offset;
612 ElfW(Addr) string_offset;
615 struct obj_symbol_patch
617 struct obj_symbol_patch *next;
619 ElfW(Addr) reloc_offset;
620 struct obj_symbol *sym;
624 /* Generic object manipulation routines. */
626 static unsigned long obj_elf_hash(const char *);
628 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
630 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
633 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
634 struct obj_symbol *sym);
636 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
637 static void obj_set_symbol_compare(struct obj_file *f,
638 int (*cmp)(const char *, const char *),
639 unsigned long (*hash)(const char *));
642 static struct obj_section *obj_find_section (struct obj_file *f,
645 static void obj_insert_section_load_order (struct obj_file *f,
646 struct obj_section *sec);
648 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
653 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
658 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
660 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
663 #ifdef CONFIG_FEATURE_2_4_MODULES
664 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
665 struct obj_symbol *sym);
668 static int obj_check_undefineds(struct obj_file *f);
670 static void obj_allocate_commons(struct obj_file *f);
672 static unsigned long obj_load_size (struct obj_file *f);
674 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
676 static struct obj_file *obj_load(FILE *f, int loadprogbits);
678 static int obj_create_image (struct obj_file *f, char *image);
680 /* Architecture specific manipulation routines. */
682 static struct obj_file *arch_new_file (void);
684 static struct obj_section *arch_new_section (void);
686 static struct obj_symbol *arch_new_symbol (void);
688 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
689 struct obj_section *targsec,
690 struct obj_section *symsec,
691 struct obj_symbol *sym,
692 ElfW(RelM) *rel, ElfW(Addr) value);
694 static void arch_create_got (struct obj_file *f);
696 static int obj_gpl_license(struct obj_file *f, const char **license);
698 #ifdef CONFIG_FEATURE_2_4_MODULES
699 static int arch_init_module (struct obj_file *f, struct new_module *);
703 //----------------------------------------------------------------------------
704 //--------end of modutils obj.h
705 //----------------------------------------------------------------------------
708 /* SPFX is always a string, so it can be concatenated to string constants. */
710 #define SPFX SYMBOL_PREFIX
716 #define _PATH_MODULES "/lib/modules"
717 static const int STRVERSIONLEN = 32;
719 /*======================================================================*/
721 static int flag_force_load = 0;
722 static int flag_autoclean = 0;
723 static int flag_verbose = 0;
724 static int flag_quiet = 0;
725 static int flag_export = 1;
728 /*======================================================================*/
730 #if defined(CONFIG_USE_LIST)
732 struct arch_list_entry
734 struct arch_list_entry *next;
735 CONFIG_LIST_ARCHTYPE addend;
742 #if defined(CONFIG_USE_SINGLE)
744 struct arch_single_entry
753 #if defined(__mips__)
756 struct mips_hi16 *next;
763 struct obj_file root;
764 #if defined(CONFIG_USE_PLT_ENTRIES)
765 struct obj_section *plt;
767 #if defined(CONFIG_USE_GOT_ENTRIES)
768 struct obj_section *got;
770 #if defined(__mips__)
771 struct mips_hi16 *mips_hi16_list;
776 struct obj_symbol root;
777 #if defined(CONFIG_USE_PLT_ENTRIES)
778 #if defined(CONFIG_USE_PLT_LIST)
779 struct arch_list_entry *pltent;
781 struct arch_single_entry pltent;
784 #if defined(CONFIG_USE_GOT_ENTRIES)
785 struct arch_single_entry gotent;
790 struct external_module {
795 struct new_module_symbol *syms;
798 static struct new_module_symbol *ksyms;
799 static size_t nksyms;
801 static struct external_module *ext_modules;
802 static int n_ext_modules;
803 static int n_ext_modules_used;
804 extern int delete_module(const char *);
806 static char *m_filename;
807 static char *m_fullName;
811 /*======================================================================*/
814 static int check_module_name_match(const char *filename, struct stat *statbuf,
817 char *fullname = (char *) userdata;
819 if (fullname[0] == '\0')
822 char *tmp, *tmp1 = bb_xstrdup(filename);
823 tmp = bb_get_last_path_component(tmp1);
824 if (strcmp(tmp, fullname) == 0) {
826 /* Stop searching if we find a match */
827 m_filename = bb_xstrdup(filename);
836 /*======================================================================*/
838 static struct obj_file *arch_new_file(void)
841 f = xmalloc(sizeof(*f));
843 memset(f, 0, sizeof(*f));
848 static struct obj_section *arch_new_section(void)
850 return xmalloc(sizeof(struct obj_section));
853 static struct obj_symbol *arch_new_symbol(void)
855 struct arch_symbol *sym;
856 sym = xmalloc(sizeof(*sym));
858 memset(sym, 0, sizeof(*sym));
863 static enum obj_reloc
864 arch_apply_relocation(struct obj_file *f,
865 struct obj_section *targsec,
866 struct obj_section *symsec,
867 struct obj_symbol *sym,
868 ElfW(RelM) *rel, ElfW(Addr) v)
870 struct arch_file *ifile = (struct arch_file *) f;
871 enum obj_reloc ret = obj_reloc_ok;
872 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
873 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
874 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
875 struct arch_symbol *isym = (struct arch_symbol *) sym;
877 #if defined(CONFIG_USE_GOT_ENTRIES)
878 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
880 #if defined(CONFIG_USE_PLT_ENTRIES)
881 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
883 #if defined(CONFIG_USE_PLT_LIST)
884 struct arch_list_entry *pe;
886 struct arch_single_entry *pe;
890 switch (ELF32_R_TYPE(rel->r_info)) {
905 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
906 * (which is .got) similar to branch,
907 * but is full 32 bits relative */
917 case R_ARM_GOTOFF: /* address relative to the got */
922 #elif defined(__s390__)
924 *(unsigned int *) loc += v;
927 *(unsigned short *) loc += v;
930 *(unsigned char *) loc += v;
934 *(unsigned int *) loc += v - dot;
937 *(unsigned short *) loc += (v - dot) >> 1;
940 *(unsigned short *) loc += v - dot;
945 /* find the plt entry and initialize it. */
946 assert(isym != NULL);
947 pe = (struct arch_single_entry *) &isym->pltent;
948 assert(pe->allocated);
949 if (pe->inited == 0) {
950 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
951 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
953 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
960 /* Insert relative distance to target. */
961 v = plt + pe->offset - dot;
962 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
963 *(unsigned int *) loc = (unsigned int) v;
964 else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
965 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
979 *(unsigned long *) loc += got - dot;
985 assert(isym != NULL);
987 if (!isym->gotent.inited)
989 isym->gotent.inited = 1;
990 *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
992 if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
993 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
994 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
995 *(unsigned short *) loc += isym->gotent.offset;
996 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
997 *(unsigned int *) loc += isym->gotent.offset;
1000 #ifndef R_390_GOTOFF32
1001 #define R_390_GOTOFF32 R_390_GOTOFF
1003 case R_390_GOTOFF32:
1008 #elif defined(__i386__)
1022 case R_386_GLOB_DAT:
1023 case R_386_JMP_SLOT:
1027 case R_386_RELATIVE:
1028 *loc += f->baseaddr;
1044 #elif defined(__mc68000__)
1055 ret = obj_reloc_overflow;
1062 ret = obj_reloc_overflow;
1069 if ((Elf32_Sword)v > 0x7f ||
1070 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
1071 ret = obj_reloc_overflow;
1078 if ((Elf32_Sword)v > 0x7fff ||
1079 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1080 ret = obj_reloc_overflow;
1086 *(int *)loc = v - dot;
1089 case R_68K_GLOB_DAT:
1090 case R_68K_JMP_SLOT:
1094 case R_68K_RELATIVE:
1095 *(int *)loc += f->baseaddr;
1108 #elif defined(__mips__)
1119 ret = obj_reloc_dangerous;
1120 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1121 ret = obj_reloc_overflow;
1123 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1129 struct mips_hi16 *n;
1131 /* We cannot relocate this one now because we don't know the value
1132 of the carry we need to add. Save the information, and let LO16
1133 do the actual relocation. */
1134 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1137 n->next = ifile->mips_hi16_list;
1138 ifile->mips_hi16_list = n;
1144 unsigned long insnlo = *loc;
1145 Elf32_Addr val, vallo;
1147 /* Sign extend the addend we extract from the lo insn. */
1148 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1150 if (ifile->mips_hi16_list != NULL) {
1151 struct mips_hi16 *l;
1153 l = ifile->mips_hi16_list;
1155 struct mips_hi16 *next;
1158 /* The value for the HI16 had best be the same. */
1159 assert(v == l->value);
1161 /* Do the HI16 relocation. Note that we actually don't
1162 need to know anything about the LO16 itself, except where
1163 to find the low 16 bits of the addend needed by the LO16. */
1166 ((insn & 0xffff) << 16) +
1170 /* Account for the sign extension that will happen in the
1177 insn = (insn & ~0xffff) | val;
1185 ifile->mips_hi16_list = NULL;
1188 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1190 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1195 #elif defined(__powerpc__)
1197 case R_PPC_ADDR16_HA:
1198 *(unsigned short *)loc = (v + 0x8000) >> 16;
1201 case R_PPC_ADDR16_HI:
1202 *(unsigned short *)loc = v >> 16;
1205 case R_PPC_ADDR16_LO:
1206 *(unsigned short *)loc = v;
1220 #elif defined(__sh__)
1243 *loc = f->baseaddr + rel->r_addend;
1248 *loc = got - dot + rel->r_addend;
1259 #if defined(__SH5__)
1260 case R_SH_IMM_MEDLOW16:
1261 case R_SH_IMM_LOW16:
1265 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1269 * movi and shori have the format:
1271 * | op | imm | reg | reserved |
1272 * 31..26 25..10 9.. 4 3 .. 0
1274 * so we simply mask and or in imm.
1276 word = *loc & ~0x3fffc00;
1277 word |= (v & 0xffff) << 10;
1284 case R_SH_IMM_MEDLOW16_PCREL:
1285 case R_SH_IMM_LOW16_PCREL:
1289 word = *loc & ~0x3fffc00;
1293 if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1296 word |= (v & 0xffff) << 10;
1302 #endif /* __SH5__ */
1306 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1307 ret = obj_reloc_unhandled;
1310 #if defined (__v850e__)
1315 /* We write two shorts instead of a long because even
1316 32-bit insns only need half-word alignment, but
1317 32-bit data needs to be long-word aligned. */
1318 v += ((unsigned short *)loc)[0];
1319 v += ((unsigned short *)loc)[1] << 16;
1320 ((unsigned short *)loc)[0] = v & 0xffff;
1321 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1324 case R_V850_22_PCREL:
1328 #if defined (__cris__)
1333 /* CRIS keeps the relocation value in the r_addend field and
1334 * should not use whats in *loc at all
1340 #elif defined(__H8300H__) || defined(__H8300S__)
1342 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
1343 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
1354 if ((Elf32_Sword)v > 0x7fff ||
1355 (Elf32_Sword)v < -(Elf32_Sword)0x8000)
1356 ret = obj_reloc_overflow;
1358 *(unsigned short *)loc = v;
1362 if ((Elf32_Sword)v > 0x7f ||
1363 (Elf32_Sword)v < -(Elf32_Sword)0x80)
1364 ret = obj_reloc_overflow;
1366 *(unsigned char *)loc = v;
1370 #if defined(CONFIG_USE_PLT_ENTRIES)
1374 /* find the plt entry and initialize it if necessary */
1375 assert(isym != NULL);
1377 #if defined(CONFIG_USE_PLT_LIST)
1378 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1386 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1388 /* generate some machine code */
1390 #if defined(__arm__)
1391 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1392 ip[1] = v; /* sym@ */
1394 #if defined(__powerpc__)
1395 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1396 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1397 ip[2] = 0x7d6903a6; /* mtctr r11 */
1398 ip[3] = 0x4e800420; /* bctr */
1400 #if defined (__v850e__)
1401 /* We have to trash a register, so we assume that any control
1402 transfer more than 21-bits away must be a function call
1403 (so we can use a call-clobbered register). */
1404 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1405 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1410 /* relative distance to target */
1412 /* if the target is too far away.... */
1413 #if defined (__arm__) || defined (__powerpc__)
1414 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1415 #elif defined (__v850e__)
1416 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1418 /* go via the plt */
1419 v = plt + pe->offset - dot;
1421 #if defined (__v850e__)
1426 ret = obj_reloc_dangerous;
1428 /* merge the offset into the instruction. */
1429 #if defined(__arm__)
1430 /* Convert to words. */
1433 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1435 #if defined(__powerpc__)
1436 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1438 #if defined (__v850e__)
1439 /* We write two shorts instead of a long because even 32-bit insns
1440 only need half-word alignment, but the 32-bit data write needs
1441 to be long-word aligned. */
1442 ((unsigned short *)loc)[0] =
1443 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1444 | ((v >> 16) & 0x3f); /* offs high part */
1445 ((unsigned short *)loc)[1] =
1446 (v & 0xffff); /* offs low part */
1449 #endif /* CONFIG_USE_PLT_ENTRIES */
1451 #if defined(CONFIG_USE_GOT_ENTRIES)
1454 assert(isym != NULL);
1455 /* needs an entry in the .got: set it, once */
1456 if (!isym->gotent.inited) {
1457 isym->gotent.inited = 1;
1458 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1460 /* make the reloc with_respect_to_.got */
1462 *loc += isym->gotent.offset + rel->r_addend;
1463 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1464 *loc += isym->gotent.offset;
1468 #endif /* CONFIG_USE_GOT_ENTRIES */
1475 #if defined(CONFIG_USE_LIST)
1477 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1478 int offset, int size)
1480 struct arch_list_entry *pe;
1482 for (pe = *list; pe != NULL; pe = pe->next) {
1483 if (pe->addend == rel->r_addend) {
1489 pe = xmalloc(sizeof(struct arch_list_entry));
1491 pe->addend = rel->r_addend;
1492 pe->offset = offset;
1502 #if defined(CONFIG_USE_SINGLE)
1504 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1505 int offset, int size)
1507 if (single->allocated == 0) {
1508 single->allocated = 1;
1509 single->offset = offset;
1518 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1520 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1521 int offset, int size)
1523 struct obj_section *myrelsec = obj_find_section(f, name);
1530 obj_extend_section(myrelsec, offset);
1532 myrelsec = obj_create_alloced_section(f, name,
1542 static void arch_create_got(struct obj_file *f)
1544 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1545 struct arch_file *ifile = (struct arch_file *) f;
1547 #if defined(CONFIG_USE_GOT_ENTRIES)
1548 int got_offset = 0, got_needed = 0, got_allocate;
1550 #if defined(CONFIG_USE_PLT_ENTRIES)
1551 int plt_offset = 0, plt_needed = 0, plt_allocate;
1553 struct obj_section *relsec, *symsec, *strsec;
1554 ElfW(RelM) *rel, *relend;
1555 ElfW(Sym) *symtab, *extsym;
1556 const char *strtab, *name;
1557 struct arch_symbol *intsym;
1559 for (i = 0; i < f->header.e_shnum; ++i) {
1560 relsec = f->sections[i];
1561 if (relsec->header.sh_type != SHT_RELM)
1564 symsec = f->sections[relsec->header.sh_link];
1565 strsec = f->sections[symsec->header.sh_link];
1567 rel = (ElfW(RelM) *) relsec->contents;
1568 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1569 symtab = (ElfW(Sym) *) symsec->contents;
1570 strtab = (const char *) strsec->contents;
1572 for (; rel < relend; ++rel) {
1573 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1575 #if defined(CONFIG_USE_GOT_ENTRIES)
1578 #if defined(CONFIG_USE_PLT_ENTRIES)
1582 switch (ELF32_R_TYPE(rel->r_info)) {
1583 #if defined(__arm__)
1598 #elif defined(__i386__)
1608 #elif defined(__powerpc__)
1613 #elif defined(__mc68000__)
1624 #elif defined(__sh__)
1634 #elif defined (__v850e__)
1635 case R_V850_22_PCREL:
1644 if (extsym->st_name != 0) {
1645 name = strtab + extsym->st_name;
1647 name = f->sections[extsym->st_shndx]->name;
1649 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1650 #if defined(CONFIG_USE_GOT_ENTRIES)
1652 got_offset += arch_single_init(
1653 rel, &intsym->gotent,
1654 got_offset, CONFIG_GOT_ENTRY_SIZE);
1659 #if defined(CONFIG_USE_PLT_ENTRIES)
1661 #if defined(CONFIG_USE_PLT_LIST)
1662 plt_offset += arch_list_add(
1663 rel, &intsym->pltent,
1664 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1666 plt_offset += arch_single_init(
1667 rel, &intsym->pltent,
1668 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1676 #if defined(CONFIG_USE_GOT_ENTRIES)
1678 ifile->got = arch_xsect_init(f, ".got", got_offset,
1679 CONFIG_GOT_ENTRY_SIZE);
1683 #if defined(CONFIG_USE_PLT_ENTRIES)
1685 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1686 CONFIG_PLT_ENTRY_SIZE);
1690 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1693 #ifdef CONFIG_FEATURE_2_4_MODULES
1694 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1700 /*======================================================================*/
1702 /* Standard ELF hash function. */
1703 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1705 unsigned long h = 0;
1712 if ((g = (h & 0xf0000000)) != 0) {
1721 static unsigned long obj_elf_hash(const char *name)
1723 return obj_elf_hash_n(name, strlen(name));
1726 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1727 /* String comparison for non-co-versioned kernel and module. */
1729 static int ncv_strcmp(const char *a, const char *b)
1731 size_t alen = strlen(a), blen = strlen(b);
1733 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1734 return strncmp(a, b, alen);
1735 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1736 return strncmp(a, b, blen);
1738 return strcmp(a, b);
1741 /* String hashing for non-co-versioned kernel and module. Here
1742 we are simply forced to drop the crc from the hash. */
1744 static unsigned long ncv_symbol_hash(const char *str)
1746 size_t len = strlen(str);
1747 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1749 return obj_elf_hash_n(str, len);
1753 obj_set_symbol_compare(struct obj_file *f,
1754 int (*cmp) (const char *, const char *),
1755 unsigned long (*hash) (const char *))
1758 f->symbol_cmp = cmp;
1760 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1763 f->symbol_hash = hash;
1765 memcpy(tmptab, f->symtab, sizeof(tmptab));
1766 memset(f->symtab, 0, sizeof(f->symtab));
1768 for (i = 0; i < HASH_BUCKETS; ++i)
1769 for (sym = tmptab[i]; sym; sym = next) {
1770 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1772 sym->next = f->symtab[h];
1778 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1780 static struct obj_symbol *
1781 obj_add_symbol(struct obj_file *f, const char *name,
1782 unsigned long symidx, int info,
1783 int secidx, ElfW(Addr) value,
1786 struct obj_symbol *sym;
1787 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1788 int n_type = ELFW(ST_TYPE) (info);
1789 int n_binding = ELFW(ST_BIND) (info);
1791 for (sym = f->symtab[hash]; sym; sym = sym->next)
1792 if (f->symbol_cmp(sym->name, name) == 0) {
1793 int o_secidx = sym->secidx;
1794 int o_info = sym->info;
1795 int o_type = ELFW(ST_TYPE) (o_info);
1796 int o_binding = ELFW(ST_BIND) (o_info);
1798 /* A redefinition! Is it legal? */
1800 if (secidx == SHN_UNDEF)
1802 else if (o_secidx == SHN_UNDEF)
1804 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1805 /* Cope with local and global symbols of the same name
1806 in the same object file, as might have been created
1807 by ld -r. The only reason locals are now seen at this
1808 level at all is so that we can do semi-sensible things
1811 struct obj_symbol *nsym, **p;
1813 nsym = arch_new_symbol();
1814 nsym->next = sym->next;
1817 /* Excise the old (local) symbol from the hash chain. */
1818 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1822 } else if (n_binding == STB_LOCAL) {
1823 /* Another symbol of the same name has already been defined.
1824 Just add this to the local table. */
1825 sym = arch_new_symbol();
1828 f->local_symtab[symidx] = sym;
1830 } else if (n_binding == STB_WEAK)
1832 else if (o_binding == STB_WEAK)
1834 /* Don't unify COMMON symbols with object types the programmer
1836 else if (secidx == SHN_COMMON
1837 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1839 else if (o_secidx == SHN_COMMON
1840 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1843 /* Don't report an error if the symbol is coming from
1844 the kernel or some external module. */
1845 if (secidx <= SHN_HIRESERVE)
1846 bb_error_msg("%s multiply defined", name);
1851 /* Completely new symbol. */
1852 sym = arch_new_symbol();
1853 sym->next = f->symtab[hash];
1854 f->symtab[hash] = sym;
1857 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1858 if (symidx >= f->local_symtab_size)
1859 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1860 name, (long) symidx, (long) f->local_symtab_size);
1862 f->local_symtab[symidx] = sym;
1869 sym->secidx = secidx;
1875 static struct obj_symbol *
1876 obj_find_symbol(struct obj_file *f, const char *name)
1878 struct obj_symbol *sym;
1879 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1881 for (sym = f->symtab[hash]; sym; sym = sym->next)
1882 if (f->symbol_cmp(sym->name, name) == 0)
1889 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1892 if (sym->secidx >= SHN_LORESERVE)
1895 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1897 /* As a special case, a NULL sym has value zero. */
1902 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1904 int i, n = f->header.e_shnum;
1906 for (i = 0; i < n; ++i)
1907 if (strcmp(f->sections[i]->name, name) == 0)
1908 return f->sections[i];
1913 static int obj_load_order_prio(struct obj_section *a)
1915 unsigned long af, ac;
1917 af = a->header.sh_flags;
1920 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1921 strcmp(a->name + 5, ".init"))
1925 if (!(af & SHF_WRITE))
1927 if (af & SHF_EXECINSTR)
1929 if (a->header.sh_type != SHT_NOBITS)
1936 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1938 struct obj_section **p;
1939 int prio = obj_load_order_prio(sec);
1940 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1941 if (obj_load_order_prio(*p) < prio)
1943 sec->load_next = *p;
1947 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1949 unsigned long align,
1952 int newidx = f->header.e_shnum++;
1953 struct obj_section *sec;
1955 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1956 f->sections[newidx] = sec = arch_new_section();
1958 memset(sec, 0, sizeof(*sec));
1959 sec->header.sh_type = SHT_PROGBITS;
1960 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1961 sec->header.sh_size = size;
1962 sec->header.sh_addralign = align;
1966 sec->contents = xmalloc(size);
1968 obj_insert_section_load_order(f, sec);
1973 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1975 unsigned long align,
1978 int newidx = f->header.e_shnum++;
1979 struct obj_section *sec;
1981 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1982 f->sections[newidx] = sec = arch_new_section();
1984 memset(sec, 0, sizeof(*sec));
1985 sec->header.sh_type = SHT_PROGBITS;
1986 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1987 sec->header.sh_size = size;
1988 sec->header.sh_addralign = align;
1992 sec->contents = xmalloc(size);
1994 sec->load_next = f->load_order;
1995 f->load_order = sec;
1996 if (f->load_order_search_start == &f->load_order)
1997 f->load_order_search_start = &sec->load_next;
2002 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2004 unsigned long oldsize = sec->header.sh_size;
2006 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2008 return sec->contents + oldsize;
2012 /* Conditionally add the symbols from the given symbol set to the
2018 int idx, struct new_module_symbol *syms, size_t nsyms)
2020 struct new_module_symbol *s;
2023 #ifdef SYMBOL_PREFIX
2025 size_t name_alloced_size = 0;
2027 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2030 gpl = obj_gpl_license(f, NULL) == 0;
2032 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2033 /* Only add symbols that are already marked external.
2034 If we override locals we may cause problems for
2035 argument initialization. We will also create a false
2036 dependency on the module. */
2037 struct obj_symbol *sym;
2040 /* GPL licensed modules can use symbols exported with
2041 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2042 * exported names. Non-GPL modules never see any GPLONLY_
2043 * symbols so they cannot fudge it by adding the prefix on
2046 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2047 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2049 ((char *)s->name) += 8;
2054 name = (char *)s->name;
2056 #ifdef SYMBOL_PREFIX
2057 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2058 kernel exports `C names', but module object files
2059 reference `linker names'). */
2060 size_t extra = sizeof SYMBOL_PREFIX;
2061 size_t name_size = strlen (name) + extra;
2062 if (name_size > name_alloced_size) {
2063 name_alloced_size = name_size * 2;
2064 name_buf = alloca (name_alloced_size);
2066 strcpy (name_buf, SYMBOL_PREFIX);
2067 strcpy (name_buf + extra - 1, name);
2069 #endif /* SYMBOL_PREFIX */
2071 sym = obj_find_symbol(f, name);
2072 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
2073 #ifdef SYMBOL_PREFIX
2074 /* Put NAME_BUF into more permanent storage. */
2075 name = xmalloc (name_size);
2076 strcpy (name, name_buf);
2078 sym = obj_add_symbol(f, name, -1,
2079 ELFW(ST_INFO) (STB_GLOBAL,
2082 /* Did our symbol just get installed? If so, mark the
2083 module as "used". */
2084 if (sym->secidx == idx)
2092 static void add_kernel_symbols(struct obj_file *f)
2094 struct external_module *m;
2097 /* Add module symbols first. */
2099 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2101 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2102 m->nsyms)) m->used = 1, ++nused;
2104 n_ext_modules_used = nused;
2106 /* And finally the symbols from the kernel proper. */
2109 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2112 static char *get_modinfo_value(struct obj_file *f, const char *key)
2114 struct obj_section *sec;
2115 char *p, *v, *n, *ep;
2116 size_t klen = strlen(key);
2118 sec = obj_find_section(f, ".modinfo");
2122 ep = p + sec->header.sh_size;
2125 n = strchr(p, '\0');
2127 if (p + klen == v && strncmp(p, key, klen) == 0)
2130 if (p + klen == n && strcmp(p, key) == 0)
2140 /*======================================================================*/
2141 /* Functions relating to module loading in pre 2.1 kernels. */
2144 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
2148 struct obj_symbol *sym;
2152 if ((q = strchr(p, '=')) == NULL) {
2158 sym = obj_find_symbol(f, p);
2160 /* Also check that the parameter was not resolved from the kernel. */
2161 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2162 bb_error_msg("symbol for parameter %s not found", p);
2166 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
2168 /* Do C quoting if we begin with a ". */
2172 str = alloca(strlen(q));
2173 for (r = str, q++; *q != '"'; ++q, ++r) {
2175 bb_error_msg("improperly terminated string argument for %s", p);
2177 } else if (*q == '\\')
2211 if (q[1] >= '0' && q[1] <= '7') {
2212 c = (c * 8) + *++q - '0';
2213 if (q[1] >= '0' && q[1] <= '7')
2214 c = (c * 8) + *++q - '0';
2227 obj_string_patch(f, sym->secidx, sym->value, str);
2228 } else if (*q >= '0' && *q <= '9') {
2230 *loc++ = strtoul(q, &q, 0);
2231 while (*q++ == ',');
2233 char *contents = f->sections[sym->secidx]->contents;
2234 char *myloc = contents + sym->value;
2235 char *r; /* To search for commas */
2237 /* Break the string with comas */
2238 while ((r = strchr(q, ',')) != (char *) NULL) {
2240 obj_string_patch(f, sym->secidx, myloc - contents, q);
2241 myloc += sizeof(char *);
2246 obj_string_patch(f, sym->secidx, myloc - contents, q);
2255 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2256 static int old_is_module_checksummed(struct obj_file *f)
2258 return obj_find_symbol(f, "Using_Versions") != NULL;
2260 /* Get the module's kernel version in the canonical integer form. */
2263 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2265 struct obj_symbol *sym;
2269 sym = obj_find_symbol(f, "kernel_version");
2273 p = f->sections[sym->secidx]->contents + sym->value;
2274 safe_strncpy(str, p, STRVERSIONLEN);
2276 a = strtoul(p, &p, 10);
2279 b = strtoul(p + 1, &p, 10);
2282 c = strtoul(p + 1, &q, 10);
2286 return a << 16 | b << 8 | c;
2289 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2291 #ifdef CONFIG_FEATURE_2_2_MODULES
2293 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
2295 static int old_get_kernel_symbols(const char *m_name)
2297 struct old_kernel_sym *ks, *k;
2298 struct new_module_symbol *s;
2299 struct external_module *mod;
2300 int nks, nms, nmod, i;
2302 nks = get_kernel_syms(NULL);
2305 bb_perror_msg("get_kernel_syms: %s", m_name);
2307 bb_error_msg("No kernel symbols");
2311 ks = k = xmalloc(nks * sizeof(*ks));
2313 if (get_kernel_syms(ks) != nks) {
2314 perror("inconsistency with get_kernel_syms -- is someone else "
2315 "playing with modules?");
2320 /* Collect the module information. */
2325 while (k->name[0] == '#' && k->name[1]) {
2326 struct old_kernel_sym *k2;
2328 /* Find out how many symbols this module has. */
2329 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
2333 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
2334 mod[nmod].name = k->name + 1;
2335 mod[nmod].addr = k->value;
2337 mod[nmod].nsyms = nms;
2338 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2340 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
2341 s->name = (unsigned long) k->name;
2342 s->value = k->value;
2349 n_ext_modules = nmod + 1;
2351 /* Now collect the symbols for the kernel proper. */
2353 if (k->name[0] == '#')
2356 nksyms = nms = nks - (k - ks);
2357 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2359 for (i = 0; i < nms; ++i, ++s, ++k) {
2360 s->name = (unsigned long) k->name;
2361 s->value = k->value;
2367 /* Return the kernel symbol checksum version, or zero if not used. */
2369 static int old_is_kernel_checksummed(void)
2371 /* Using_Versions is the first symbol. */
2373 && strcmp((char *) ksyms[0].name,
2374 "Using_Versions") == 0) return ksyms[0].value;
2380 static int old_create_mod_use_count(struct obj_file *f)
2382 struct obj_section *sec;
2384 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2387 obj_add_symbol(f, "mod_use_count_", -1,
2388 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2395 old_init_module(const char *m_name, struct obj_file *f,
2396 unsigned long m_size)
2399 struct old_mod_routines routines;
2400 struct old_symbol_table *symtab;
2403 /* Create the symbol table */
2405 int nsyms = 0, strsize = 0, total;
2407 /* Size things first... */
2410 for (i = 0; i < HASH_BUCKETS; ++i) {
2411 struct obj_symbol *sym;
2412 for (sym = f->symtab[i]; sym; sym = sym->next)
2413 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2414 && sym->secidx <= SHN_HIRESERVE)
2416 sym->ksymidx = nsyms++;
2417 strsize += strlen(sym->name) + 1;
2422 total = (sizeof(struct old_symbol_table)
2423 + nsyms * sizeof(struct old_module_symbol)
2424 + n_ext_modules_used * sizeof(struct old_module_ref)
2426 symtab = xmalloc(total);
2427 symtab->size = total;
2428 symtab->n_symbols = nsyms;
2429 symtab->n_refs = n_ext_modules_used;
2431 if (flag_export && nsyms) {
2432 struct old_module_symbol *ksym;
2436 ksym = symtab->symbol;
2437 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2438 + n_ext_modules_used * sizeof(struct old_module_ref));
2440 for (i = 0; i < HASH_BUCKETS; ++i) {
2441 struct obj_symbol *sym;
2442 for (sym = f->symtab[i]; sym; sym = sym->next)
2443 if (sym->ksymidx >= 0) {
2444 ksym->addr = obj_symbol_final_value(f, sym);
2446 (unsigned long) str - (unsigned long) symtab;
2448 strcpy(str, sym->name);
2449 str += strlen(sym->name) + 1;
2455 if (n_ext_modules_used) {
2456 struct old_module_ref *ref;
2459 ref = (struct old_module_ref *)
2460 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2462 for (i = 0; i < n_ext_modules; ++i)
2463 if (ext_modules[i].used)
2464 ref++->module = ext_modules[i].addr;
2468 /* Fill in routines. */
2471 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2473 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2475 /* Whew! All of the initialization is complete. Collect the final
2476 module image and give it to the kernel. */
2478 image = xmalloc(m_size);
2479 obj_create_image(f, image);
2481 /* image holds the complete relocated module, accounting correctly for
2482 mod_use_count. However the old module kernel support assume that
2483 it is receiving something which does not contain mod_use_count. */
2484 ret = old_sys_init_module(m_name, image + sizeof(long),
2485 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2486 : 0), &routines, symtab);
2488 bb_perror_msg("init_module: %s", m_name);
2498 #define old_create_mod_use_count(x) TRUE
2499 #define old_init_module(x, y, z) TRUE
2501 #endif /* CONFIG_FEATURE_2_2_MODULES */
2505 /*======================================================================*/
2506 /* Functions relating to module loading after 2.1.18. */
2509 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2512 char *p, *q, *key, *sym_name;
2513 struct obj_symbol *sym;
2514 char *contents, *loc;
2518 if ((q = strchr(p, '=')) == NULL) {
2523 key = alloca(q - p + 6);
2524 memcpy(key, "parm_", 5);
2525 memcpy(key + 5, p, q - p);
2528 p = get_modinfo_value(f, key);
2531 bb_error_msg("invalid parameter %s", key);
2535 #ifdef SYMBOL_PREFIX
2536 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2537 strcpy (sym_name, SYMBOL_PREFIX);
2538 strcat (sym_name, key);
2542 sym = obj_find_symbol(f, sym_name);
2544 /* Also check that the parameter was not resolved from the kernel. */
2545 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2546 bb_error_msg("symbol for parameter %s not found", key);
2551 min = strtoul(p, &p, 10);
2553 max = strtoul(p + 1, &p, 10);
2559 contents = f->sections[sym->secidx]->contents;
2560 loc = contents + sym->value;
2564 if ((*p == 's') || (*p == 'c')) {
2567 /* Do C quoting if we begin with a ", else slurp the lot. */
2571 str = alloca(strlen(q));
2572 for (r = str, q++; *q != '"'; ++q, ++r) {
2574 bb_error_msg("improperly terminated string argument for %s",
2577 } else if (*q == '\\')
2611 if (q[1] >= '0' && q[1] <= '7') {
2612 c = (c * 8) + *++q - '0';
2613 if (q[1] >= '0' && q[1] <= '7')
2614 c = (c * 8) + *++q - '0';
2631 /* In this case, the string is not quoted. We will break
2632 it using the coma (like for ints). If the user wants to
2633 include comas in a string, he just has to quote it */
2635 /* Search the next coma */
2639 if (r != (char *) NULL) {
2640 /* Recopy the current field */
2641 str = alloca(r - q + 1);
2642 memcpy(str, q, r - q);
2644 /* I don't know if it is useful, as the previous case
2645 doesn't nul terminate the string ??? */
2648 /* Keep next fields */
2659 obj_string_patch(f, sym->secidx, loc - contents, str);
2660 loc += tgt_sizeof_char_p;
2662 /* Array of chars (in fact, matrix !) */
2663 unsigned long charssize; /* size of each member */
2665 /* Get the size of each member */
2666 /* Probably we should do that outside the loop ? */
2667 if (!isdigit(*(p + 1))) {
2668 bb_error_msg("parameter type 'c' for %s must be followed by"
2669 " the maximum size", key);
2672 charssize = strtoul(p + 1, (char **) NULL, 10);
2675 if (strlen(str) >= charssize) {
2676 bb_error_msg("string too long for %s (max %ld)", key,
2681 /* Copy to location */
2682 strcpy((char *) loc, str);
2686 long v = strtoul(q, &q, 0);
2693 loc += tgt_sizeof_short;
2697 loc += tgt_sizeof_int;
2701 loc += tgt_sizeof_long;
2705 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2720 goto retry_end_of_value;
2724 bb_error_msg("too many values for %s (max %d)", key, max);
2731 bb_error_msg("invalid argument syntax for %s", key);
2738 bb_error_msg("too few values for %s (min %d)", key, min);
2748 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2749 static int new_is_module_checksummed(struct obj_file *f)
2751 const char *p = get_modinfo_value(f, "using_checksums");
2758 /* Get the module's kernel version in the canonical integer form. */
2761 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2766 p = get_modinfo_value(f, "kernel_version");
2769 safe_strncpy(str, p, STRVERSIONLEN);
2771 a = strtoul(p, &p, 10);
2774 b = strtoul(p + 1, &p, 10);
2777 c = strtoul(p + 1, &q, 10);
2781 return a << 16 | b << 8 | c;
2784 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2787 #ifdef CONFIG_FEATURE_2_4_MODULES
2789 /* Fetch the loaded modules, and all currently exported symbols. */
2791 static int new_get_kernel_symbols(void)
2793 char *module_names, *mn;
2794 struct external_module *modules, *m;
2795 struct new_module_symbol *syms, *s;
2796 size_t ret, bufsize, nmod, nsyms, i, j;
2798 /* Collect the loaded modules. */
2800 module_names = xmalloc(bufsize = 256);
2802 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2803 if (errno == ENOSPC && bufsize < ret) {
2804 module_names = xrealloc(module_names, bufsize = ret);
2805 goto retry_modules_load;
2807 bb_perror_msg("QM_MODULES");
2811 n_ext_modules = nmod = ret;
2813 /* Collect the modules' symbols. */
2816 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2817 memset(modules, 0, nmod * sizeof(*modules));
2818 for (i = 0, mn = module_names, m = modules;
2819 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2820 struct new_module_info info;
2822 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2823 if (errno == ENOENT) {
2824 /* The module was removed out from underneath us. */
2827 bb_perror_msg("query_module: QM_INFO: %s", mn);
2831 syms = xmalloc(bufsize = 1024);
2833 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2836 syms = xrealloc(syms, bufsize = ret);
2837 goto retry_mod_sym_load;
2839 /* The module was removed out from underneath us. */
2842 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2849 m->addr = info.addr;
2853 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2854 s->name += (unsigned long) syms;
2859 /* Collect the kernel's symbols. */
2861 syms = xmalloc(bufsize = 16 * 1024);
2862 retry_kern_sym_load:
2863 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2864 if (errno == ENOSPC && bufsize < ret) {
2865 syms = xrealloc(syms, bufsize = ret);
2866 goto retry_kern_sym_load;
2868 bb_perror_msg("kernel: QM_SYMBOLS");
2871 nksyms = nsyms = ret;
2874 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2875 s->name += (unsigned long) syms;
2881 /* Return the kernel symbol checksum version, or zero if not used. */
2883 static int new_is_kernel_checksummed(void)
2885 struct new_module_symbol *s;
2888 /* Using_Versions is not the first symbol, but it should be in there. */
2890 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2891 if (strcmp((char *) s->name, "Using_Versions") == 0)
2898 static int new_create_this_module(struct obj_file *f, const char *m_name)
2900 struct obj_section *sec;
2902 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2903 sizeof(struct new_module));
2904 memset(sec->contents, 0, sizeof(struct new_module));
2906 obj_add_symbol(f, SPFX "__this_module", -1,
2907 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2908 sizeof(struct new_module));
2910 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2916 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2917 /* add an entry to the __ksymtab section, creating it if necessary */
2918 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2920 struct obj_section *sec;
2923 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2924 * If __ksymtab is defined but not marked alloc, x out the first character
2925 * (no obj_delete routine) and create a new __ksymtab with the correct
2928 sec = obj_find_section(f, "__ksymtab");
2929 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2930 *((char *)(sec->name)) = 'x'; /* override const */
2934 sec = obj_create_alloced_section(f, "__ksymtab",
2935 tgt_sizeof_void_p, 0);
2938 sec->header.sh_flags |= SHF_ALLOC;
2939 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2941 ofs = sec->header.sh_size;
2942 obj_symbol_patch(f, sec->idx, ofs, sym);
2943 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2944 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2946 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2948 static int new_create_module_ksymtab(struct obj_file *f)
2950 struct obj_section *sec;
2953 /* We must always add the module references. */
2955 if (n_ext_modules_used) {
2956 struct new_module_ref *dep;
2957 struct obj_symbol *tm;
2959 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2960 (sizeof(struct new_module_ref)
2961 * n_ext_modules_used));
2965 tm = obj_find_symbol(f, SPFX "__this_module");
2966 dep = (struct new_module_ref *) sec->contents;
2967 for (i = 0; i < n_ext_modules; ++i)
2968 if (ext_modules[i].used) {
2969 dep->dep = ext_modules[i].addr;
2970 obj_symbol_patch(f, sec->idx,
2971 (char *) &dep->ref - sec->contents, tm);
2977 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2982 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2985 /* We don't want to export symbols residing in sections that
2986 aren't loaded. There are a number of these created so that
2987 we make sure certain module options don't appear twice. */
2989 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2991 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2993 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2994 struct obj_symbol *sym;
2995 for (sym = f->symtab[i]; sym; sym = sym->next)
2996 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2997 && sym->secidx <= SHN_HIRESERVE
2998 && (sym->secidx >= SHN_LORESERVE
2999 || loaded[sym->secidx])) {
3000 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
3002 obj_symbol_patch(f, sec->idx, ofs, sym);
3003 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
3010 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
3018 new_init_module(const char *m_name, struct obj_file *f,
3019 unsigned long m_size)
3021 struct new_module *module;
3022 struct obj_section *sec;
3027 sec = obj_find_section(f, ".this");
3028 if (!sec || !sec->contents) {
3029 bb_perror_msg_and_die("corrupt module %s?",m_name);
3031 module = (struct new_module *) sec->contents;
3032 m_addr = sec->header.sh_addr;
3034 module->size_of_struct = sizeof(*module);
3035 module->size = m_size;
3036 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
3038 sec = obj_find_section(f, "__ksymtab");
3039 if (sec && sec->header.sh_size) {
3040 module->syms = sec->header.sh_addr;
3041 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
3044 if (n_ext_modules_used) {
3045 sec = obj_find_section(f, ".kmodtab");
3046 module->deps = sec->header.sh_addr;
3047 module->ndeps = n_ext_modules_used;
3051 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
3053 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
3055 sec = obj_find_section(f, "__ex_table");
3057 module->ex_table_start = sec->header.sh_addr;
3058 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
3061 sec = obj_find_section(f, ".text.init");
3063 module->runsize = sec->header.sh_addr - m_addr;
3065 sec = obj_find_section(f, ".data.init");
3067 if (!module->runsize ||
3068 module->runsize > sec->header.sh_addr - m_addr)
3069 module->runsize = sec->header.sh_addr - m_addr;
3071 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
3072 if (sec && sec->header.sh_size) {
3073 module->archdata_start = (void*)sec->header.sh_addr;
3074 module->archdata_end = module->archdata_start + sec->header.sh_size;
3076 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
3077 if (sec && sec->header.sh_size) {
3078 module->kallsyms_start = (void*)sec->header.sh_addr;
3079 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
3082 if (!arch_init_module(f, module))
3085 /* Whew! All of the initialization is complete. Collect the final
3086 module image and give it to the kernel. */
3088 image = xmalloc(m_size);
3089 obj_create_image(f, image);
3091 ret = new_sys_init_module(m_name, (struct new_module *) image);
3093 bb_perror_msg("init_module: %s", m_name);
3102 #define new_init_module(x, y, z) TRUE
3103 #define new_create_this_module(x, y) 0
3104 #define new_add_ksymtab(x, y) -1
3105 #define new_create_module_ksymtab(x)
3106 #define query_module(v, w, x, y, z) -1
3108 #endif /* CONFIG_FEATURE_2_4_MODULES */
3111 /*======================================================================*/
3114 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3117 struct obj_string_patch *p;
3118 struct obj_section *strsec;
3119 size_t len = strlen(string) + 1;
3122 p = xmalloc(sizeof(*p));
3123 p->next = f->string_patches;
3124 p->reloc_secidx = secidx;
3125 p->reloc_offset = offset;
3126 f->string_patches = p;
3128 strsec = obj_find_section(f, ".kstrtab");
3129 if (strsec == NULL) {
3130 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3131 p->string_offset = 0;
3132 loc = strsec->contents;
3134 p->string_offset = strsec->header.sh_size;
3135 loc = obj_extend_section(strsec, len);
3137 memcpy(loc, string, len);
3142 #ifdef CONFIG_FEATURE_2_4_MODULES
3144 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3145 struct obj_symbol *sym)
3147 struct obj_symbol_patch *p;
3149 p = xmalloc(sizeof(*p));
3150 p->next = f->symbol_patches;
3151 p->reloc_secidx = secidx;
3152 p->reloc_offset = offset;
3154 f->symbol_patches = p;
3160 static int obj_check_undefineds(struct obj_file *f)
3165 for (i = 0; i < HASH_BUCKETS; ++i) {
3166 struct obj_symbol *sym;
3167 for (sym = f->symtab[i]; sym; sym = sym->next)
3168 if (sym->secidx == SHN_UNDEF) {
3169 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
3170 sym->secidx = SHN_ABS;
3174 bb_error_msg("unresolved symbol %s", sym->name);
3184 static void obj_allocate_commons(struct obj_file *f)
3186 struct common_entry {
3187 struct common_entry *next;
3188 struct obj_symbol *sym;
3189 } *common_head = NULL;
3193 for (i = 0; i < HASH_BUCKETS; ++i) {
3194 struct obj_symbol *sym;
3195 for (sym = f->symtab[i]; sym; sym = sym->next)
3196 if (sym->secidx == SHN_COMMON) {
3197 /* Collect all COMMON symbols and sort them by size so as to
3198 minimize space wasted by alignment requirements. */
3200 struct common_entry **p, *n;
3201 for (p = &common_head; *p; p = &(*p)->next)
3202 if (sym->size <= (*p)->sym->size)
3205 n = alloca(sizeof(*n));
3213 for (i = 1; i < f->local_symtab_size; ++i) {
3214 struct obj_symbol *sym = f->local_symtab[i];
3215 if (sym && sym->secidx == SHN_COMMON) {
3216 struct common_entry **p, *n;
3217 for (p = &common_head; *p; p = &(*p)->next)
3218 if (sym == (*p)->sym)
3220 else if (sym->size < (*p)->sym->size) {
3221 n = alloca(sizeof(*n));
3231 /* Find the bss section. */
3232 for (i = 0; i < f->header.e_shnum; ++i)
3233 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3236 /* If for some reason there hadn't been one, create one. */
3237 if (i == f->header.e_shnum) {
3238 struct obj_section *sec;
3240 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3241 f->sections[i] = sec = arch_new_section();
3242 f->header.e_shnum = i + 1;
3244 memset(sec, 0, sizeof(*sec));
3245 sec->header.sh_type = SHT_PROGBITS;
3246 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3251 /* Allocate the COMMONS. */
3253 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3254 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3255 struct common_entry *c;
3257 for (c = common_head; c; c = c->next) {
3258 ElfW(Addr) align = c->sym->value;
3260 if (align > max_align)
3262 if (bss_size & (align - 1))
3263 bss_size = (bss_size | (align - 1)) + 1;
3266 c->sym->value = bss_size;
3268 bss_size += c->sym->size;
3271 f->sections[i]->header.sh_size = bss_size;
3272 f->sections[i]->header.sh_addralign = max_align;
3276 /* For the sake of patch relocation and parameter initialization,
3277 allocate zeroed data for NOBITS sections now. Note that after
3278 this we cannot assume NOBITS are really empty. */
3279 for (i = 0; i < f->header.e_shnum; ++i) {
3280 struct obj_section *s = f->sections[i];
3281 if (s->header.sh_type == SHT_NOBITS) {
3282 if (s->header.sh_size != 0)
3283 s->contents = memset(xmalloc(s->header.sh_size),
3284 0, s->header.sh_size);
3288 s->header.sh_type = SHT_PROGBITS;
3293 static unsigned long obj_load_size(struct obj_file *f)
3295 unsigned long dot = 0;
3296 struct obj_section *sec;
3298 /* Finalize the positions of the sections relative to one another. */
3300 for (sec = f->load_order; sec; sec = sec->load_next) {
3303 align = sec->header.sh_addralign;
3304 if (align && (dot & (align - 1)))
3305 dot = (dot | (align - 1)) + 1;
3307 sec->header.sh_addr = dot;
3308 dot += sec->header.sh_size;
3314 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3316 int i, n = f->header.e_shnum;
3319 /* Finalize the addresses of the sections. */
3322 for (i = 0; i < n; ++i)
3323 f->sections[i]->header.sh_addr += base;
3325 /* And iterate over all of the relocations. */
3327 for (i = 0; i < n; ++i) {
3328 struct obj_section *relsec, *symsec, *targsec, *strsec;
3329 ElfW(RelM) * rel, *relend;
3333 relsec = f->sections[i];
3334 if (relsec->header.sh_type != SHT_RELM)
3337 symsec = f->sections[relsec->header.sh_link];
3338 targsec = f->sections[relsec->header.sh_info];
3339 strsec = f->sections[symsec->header.sh_link];
3341 rel = (ElfW(RelM) *) relsec->contents;
3342 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3343 symtab = (ElfW(Sym) *) symsec->contents;
3344 strtab = (const char *) strsec->contents;
3346 for (; rel < relend; ++rel) {
3347 ElfW(Addr) value = 0;
3348 struct obj_symbol *intsym = NULL;
3349 unsigned long symndx;
3350 ElfW(Sym) * extsym = 0;
3353 /* Attempt to find a value to use for this relocation. */
3355 symndx = ELFW(R_SYM) (rel->r_info);
3357 /* Note we've already checked for undefined symbols. */
3359 extsym = &symtab[symndx];
3360 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
3361 /* Local symbols we look up in the local table to be sure
3362 we get the one that is really intended. */
3363 intsym = f->local_symtab[symndx];
3365 /* Others we look up in the hash table. */
3367 if (extsym->st_name)
3368 name = strtab + extsym->st_name;
3370 name = f->sections[extsym->st_shndx]->name;
3371 intsym = obj_find_symbol(f, name);
3374 value = obj_symbol_final_value(f, intsym);
3375 intsym->referenced = 1;
3377 #if SHT_RELM == SHT_RELA
3378 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3379 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3380 if (!extsym || !extsym->st_name ||
3381 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
3383 value += rel->r_addend;
3387 switch (arch_apply_relocation
3388 (f, targsec, symsec, intsym, rel, value)) {
3392 case obj_reloc_overflow:
3393 errmsg = "Relocation overflow";
3395 case obj_reloc_dangerous:
3396 errmsg = "Dangerous relocation";
3398 case obj_reloc_unhandled:
3399 errmsg = "Unhandled relocation";
3402 bb_error_msg("%s of type %ld for %s", errmsg,
3403 (long) ELFW(R_TYPE) (rel->r_info),
3404 strtab + extsym->st_name);
3406 bb_error_msg("%s of type %ld", errmsg,
3407 (long) ELFW(R_TYPE) (rel->r_info));
3415 /* Finally, take care of the patches. */
3417 if (f->string_patches) {
3418 struct obj_string_patch *p;
3419 struct obj_section *strsec;
3420 ElfW(Addr) strsec_base;
3421 strsec = obj_find_section(f, ".kstrtab");
3422 strsec_base = strsec->header.sh_addr;
3424 for (p = f->string_patches; p; p = p->next) {
3425 struct obj_section *targsec = f->sections[p->reloc_secidx];
3426 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3427 = strsec_base + p->string_offset;
3431 if (f->symbol_patches) {
3432 struct obj_symbol_patch *p;
3434 for (p = f->symbol_patches; p; p = p->next) {
3435 struct obj_section *targsec = f->sections[p->reloc_secidx];
3436 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3437 = obj_symbol_final_value(f, p->sym);
3444 static int obj_create_image(struct obj_file *f, char *image)
3446 struct obj_section *sec;
3447 ElfW(Addr) base = f->baseaddr;
3449 for (sec = f->load_order; sec; sec = sec->load_next) {
3452 if (sec->contents == 0 || sec->header.sh_size == 0)
3455 secimg = image + (sec->header.sh_addr - base);
3457 /* Note that we allocated data for NOBITS sections earlier. */
3458 memcpy(secimg, sec->contents, sec->header.sh_size);
3464 /*======================================================================*/
3466 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3469 ElfW(Shdr) * section_headers;
3473 /* Read the file header. */
3475 f = arch_new_file();
3476 memset(f, 0, sizeof(*f));
3477 f->symbol_cmp = strcmp;
3478 f->symbol_hash = obj_elf_hash;
3479 f->load_order_search_start = &f->load_order;
3481 fseek(fp, 0, SEEK_SET);
3482 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3483 bb_perror_msg("error reading ELF header");
3487 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3488 || f->header.e_ident[EI_MAG1] != ELFMAG1
3489 || f->header.e_ident[EI_MAG2] != ELFMAG2
3490 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3491 bb_error_msg("not an ELF file");
3494 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3495 || f->header.e_ident[EI_DATA] != ELFDATAM
3496 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3497 || !MATCH_MACHINE(f->header.e_machine)) {
3498 bb_error_msg("ELF file not for this architecture");
3501 if (f->header.e_type != ET_REL) {
3502 bb_error_msg("ELF file not a relocatable object");
3506 /* Read the section headers. */
3508 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3509 bb_error_msg("section header size mismatch: %lu != %lu",
3510 (unsigned long) f->header.e_shentsize,
3511 (unsigned long) sizeof(ElfW(Shdr)));
3515 shnum = f->header.e_shnum;
3516 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3517 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3519 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3520 fseek(fp, f->header.e_shoff, SEEK_SET);
3521 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3522 bb_perror_msg("error reading ELF section headers");
3526 /* Read the section data. */
3528 for (i = 0; i < shnum; ++i) {
3529 struct obj_section *sec;
3531 f->sections[i] = sec = arch_new_section();
3532 memset(sec, 0, sizeof(*sec));
3534 sec->header = section_headers[i];
3537 if(sec->header.sh_size) switch (sec->header.sh_type) {
3546 if (!loadprogbits) {
3547 sec->contents = NULL;
3554 if (sec->header.sh_size > 0) {
3555 sec->contents = xmalloc(sec->header.sh_size);
3556 fseek(fp, sec->header.sh_offset, SEEK_SET);
3557 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3558 bb_perror_msg("error reading ELF section data");
3562 sec->contents = NULL;
3566 #if SHT_RELM == SHT_REL
3568 bb_error_msg("RELA relocations not supported on this architecture");
3572 bb_error_msg("REL relocations not supported on this architecture");
3577 if (sec->header.sh_type >= SHT_LOPROC) {
3578 /* Assume processor specific section types are debug
3579 info and can safely be ignored. If this is ever not
3580 the case (Hello MIPS?), don't put ifdefs here but
3581 create an arch_load_proc_section(). */
3585 bb_error_msg("can't handle sections of type %ld",
3586 (long) sec->header.sh_type);
3591 /* Do what sort of interpretation as needed by each section. */
3593 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3595 for (i = 0; i < shnum; ++i) {
3596 struct obj_section *sec = f->sections[i];
3597 sec->name = shstrtab + sec->header.sh_name;
3600 for (i = 0; i < shnum; ++i) {
3601 struct obj_section *sec = f->sections[i];
3603 /* .modinfo should be contents only but gcc has no attribute for that.
3604 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3606 if (strcmp(sec->name, ".modinfo") == 0)
3607 sec->header.sh_flags &= ~SHF_ALLOC;
3609 if (sec->header.sh_flags & SHF_ALLOC)
3610 obj_insert_section_load_order(f, sec);
3612 switch (sec->header.sh_type) {
3615 unsigned long nsym, j;
3619 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3620 bb_error_msg("symbol size mismatch: %lu != %lu",
3621 (unsigned long) sec->header.sh_entsize,
3622 (unsigned long) sizeof(ElfW(Sym)));
3626 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3627 strtab = f->sections[sec->header.sh_link]->contents;
3628 sym = (ElfW(Sym) *) sec->contents;
3630 /* Allocate space for a table of local symbols. */
3631 j = f->local_symtab_size = sec->header.sh_info;
3632 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3634 /* Insert all symbols into the hash table. */
3635 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3636 ElfW(Addr) val = sym->st_value;
3639 name = strtab + sym->st_name;
3640 else if (sym->st_shndx < shnum)
3641 name = f->sections[sym->st_shndx]->name;
3645 #if defined(__SH5__)
3647 * For sh64 it is possible that the target of a branch
3648 * requires a mode switch (32 to 16 and back again).
3650 * This is implied by the lsb being set in the target
3651 * address for SHmedia mode and clear for SHcompact.
3653 val |= sym->st_other & 4;
3656 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3663 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3664 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3665 (unsigned long) sec->header.sh_entsize,
3666 (unsigned long) sizeof(ElfW(RelM)));
3670 /* XXX Relocation code from modutils-2.3.19 is not here.
3671 * Why? That's about 20 lines of code from obj/obj_load.c,
3672 * which gets done in a second pass through the sections.
3673 * This BusyBox insmod does similar work in obj_relocate(). */
3680 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3682 * load the unloaded sections directly into the memory allocated by
3683 * kernel for the module
3686 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3688 ElfW(Addr) base = f->baseaddr;
3689 struct obj_section* sec;
3691 for (sec = f->load_order; sec; sec = sec->load_next) {
3693 /* section already loaded? */
3694 if (sec->contents != NULL)
3697 if (sec->header.sh_size == 0)
3700 sec->contents = imagebase + (sec->header.sh_addr - base);
3701 fseek(fp, sec->header.sh_offset, SEEK_SET);
3702 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3703 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3712 static void hide_special_symbols(struct obj_file *f)
3714 static const char *const specials[] = {
3715 SPFX "cleanup_module",
3717 SPFX "kernel_version",
3721 struct obj_symbol *sym;
3722 const char *const *p;
3724 for (p = specials; *p; ++p)
3725 if ((sym = obj_find_symbol(f, *p)) != NULL)
3727 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3731 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3732 static int obj_gpl_license(struct obj_file *f, const char **license)
3734 struct obj_section *sec;
3735 /* This list must match *exactly* the list of allowable licenses in
3736 * linux/include/linux/module.h. Checking for leading "GPL" will not
3737 * work, somebody will use "GPL sucks, this is proprietary".
3739 static const char *gpl_licenses[] = {
3742 "GPL and additional rights",
3747 if ((sec = obj_find_section(f, ".modinfo"))) {
3748 const char *value, *ptr, *endptr;
3749 ptr = sec->contents;
3750 endptr = ptr + sec->header.sh_size;
3751 while (ptr < endptr) {
3752 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3756 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3757 if (strcmp(value+1, gpl_licenses[i]) == 0)
3762 if (strchr(ptr, '\0'))
3763 ptr = strchr(ptr, '\0') + 1;
3771 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3772 #define TAINT_PROPRIETORY_MODULE (1<<0)
3773 #define TAINT_FORCED_MODULE (1<<1)
3774 #define TAINT_UNSAFE_SMP (1<<2)
3775 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3777 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3778 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3782 static int first = 1;
3783 if (fd < 0 && !kernel_has_tainted)
3784 return; /* New modutils on old kernel */
3785 printf("Warning: loading %s will taint the kernel: %s%s\n",
3786 m_name, text1, text2);
3788 printf(" See %s for information about tainted modules\n", TAINT_URL);
3792 read(fd, buf, sizeof(buf)-1);
3793 buf[sizeof(buf)-1] = '\0';
3794 oldval = strtoul(buf, NULL, 10);
3795 sprintf(buf, "%d\n", oldval | taint);
3796 write(fd, buf, strlen(buf));
3800 /* Check if loading this module will taint the kernel. */
3801 static void check_tainted_module(struct obj_file *f, char *m_name)
3803 static const char tainted_file[] = TAINT_FILENAME;
3804 int fd, kernel_has_tainted;
3807 kernel_has_tainted = 1;
3808 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3809 if (errno == ENOENT)
3810 kernel_has_tainted = 0;
3811 else if (errno == EACCES)
3812 kernel_has_tainted = 1;
3814 perror(tainted_file);
3815 kernel_has_tainted = 0;
3819 switch (obj_gpl_license(f, &ptr)) {
3823 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3826 /* The module has a non-GPL license so we pretend that the
3827 * kernel always has a taint flag to get a warning even on
3828 * kernels without the proc flag.
3830 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3833 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3837 if (flag_force_load)
3838 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3843 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3844 #define check_tainted_module(x, y) do { } while(0);
3845 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3847 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3848 /* add module source, timestamp, kernel version and a symbol for the
3849 * start of some sections. this info is used by ksymoops to do better
3853 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3855 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3856 if (get_modinfo_value(f, "kernel_version") == NULL)
3857 return old_get_module_version(f, str);
3859 return new_get_module_version(f, str);
3860 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3861 strncpy(str, "???", sizeof(str));
3863 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3866 /* add module source, timestamp, kernel version and a symbol for the
3867 * start of some sections. this info is used by ksymoops to do better
3871 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3874 static const char symprefix[] = "__insmod_";
3875 struct obj_section *sec;
3876 struct obj_symbol *sym;
3877 char *name, *absolute_filename;
3878 char str[STRVERSIONLEN], real[PATH_MAX];
3879 int i, l, lm_name, lfilename, use_ksymtab, version;
3880 struct stat statbuf;
3882 static const char *section_names[] = {
3890 if (realpath(filename, real)) {
3891 absolute_filename = bb_xstrdup(real);
3894 int save_errno = errno;
3895 bb_error_msg("cannot get realpath for %s", filename);
3898 absolute_filename = bb_xstrdup(filename);
3901 lm_name = strlen(m_name);
3902 lfilename = strlen(absolute_filename);
3904 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3905 * are not to be exported. otherwise leave ksymtab alone for now, the
3906 * "export all symbols" compatibility code will export these symbols later.
3908 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3910 if ((sec = obj_find_section(f, ".this"))) {
3911 /* tag the module header with the object name, last modified
3912 * timestamp and module version. worst case for module version
3913 * is 0xffffff, decimal 16777215. putting all three fields in
3914 * one symbol is less readable but saves kernel space.
3916 l = sizeof(symprefix)+ /* "__insmod_" */
3917 lm_name+ /* module name */
3919 lfilename+ /* object filename */
3921 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3923 8+ /* version in dec */
3926 if (stat(absolute_filename, &statbuf) != 0)
3927 statbuf.st_mtime = 0;
3928 version = get_module_version(f, str); /* -1 if not found */
3929 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3930 symprefix, m_name, absolute_filename,
3931 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3933 sym = obj_add_symbol(f, name, -1,
3934 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3935 sec->idx, sec->header.sh_addr, 0);
3937 new_add_ksymtab(f, sym);
3939 free(absolute_filename);
3940 #ifdef _NOT_SUPPORTED_
3941 /* record where the persistent data is going, same address as previous symbol */
3944 l = sizeof(symprefix)+ /* "__insmod_" */
3945 lm_name+ /* module name */
3947 strlen(f->persist)+ /* data store */
3950 snprintf(name, l, "%s%s_P%s",
3951 symprefix, m_name, f->persist);
3952 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3953 sec->idx, sec->header.sh_addr, 0);
3955 new_add_ksymtab(f, sym);
3957 #endif /* _NOT_SUPPORTED_ */
3958 /* tag the desired sections if size is non-zero */
3960 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3961 if ((sec = obj_find_section(f, section_names[i])) &&
3962 sec->header.sh_size) {
3963 l = sizeof(symprefix)+ /* "__insmod_" */
3964 lm_name+ /* module name */
3966 strlen(sec->name)+ /* section name */
3968 8+ /* length in dec */
3971 snprintf(name, l, "%s%s_S%s_L%ld",
3972 symprefix, m_name, sec->name,
3973 (long)sec->header.sh_size);
3974 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3975 sec->idx, sec->header.sh_addr, 0);
3977 new_add_ksymtab(f, sym);
3981 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3983 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3984 static void print_load_map(struct obj_file *f)
3986 struct obj_symbol *sym;
3987 struct obj_symbol **all, **p;
3988 struct obj_section *sec;
3989 int i, nsyms, *loaded;
3991 /* Report on the section layout. */
3993 printf("Sections: Size %-*s Align\n",
3994 (int) (2 * sizeof(void *)), "Address");
3996 for (sec = f->load_order; sec; sec = sec->load_next) {
4000 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
4005 printf("%-15s %08lx %0*lx 2**%d\n",
4007 (long)sec->header.sh_size,
4008 (int) (2 * sizeof(void *)),
4009 (long)sec->header.sh_addr,
4012 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
4013 /* Quick reference which section indicies are loaded. */
4015 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
4017 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
4019 /* Collect the symbols we'll be listing. */
4021 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
4022 for (sym = f->symtab[i]; sym; sym = sym->next)
4023 if (sym->secidx <= SHN_HIRESERVE
4024 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
4027 all = alloca(nsyms * sizeof(struct obj_symbol *));
4029 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
4030 for (sym = f->symtab[i]; sym; sym = sym->next)
4031 if (sym->secidx <= SHN_HIRESERVE
4032 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
4035 /* And list them. */
4036 printf("\nSymbols:\n");
4037 for (p = all; p < all + nsyms; ++p) {
4039 unsigned long value;
4042 if (sym->secidx == SHN_ABS) {
4045 } else if (sym->secidx == SHN_UNDEF) {
4049 sec = f->sections[sym->secidx];
4051 if (sec->header.sh_type == SHT_NOBITS)
4053 else if (sec->header.sh_flags & SHF_ALLOC) {
4054 if (sec->header.sh_flags & SHF_EXECINSTR)
4056 else if (sec->header.sh_flags & SHF_WRITE)
4061 value = sym->value + sec->header.sh_addr;
4064 if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
4065 type = tolower(type);
4067 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
4075 extern int insmod_main( int argc, char **argv)
4082 unsigned long m_size;
4087 int exit_status = EXIT_FAILURE;
4089 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4090 struct utsname uts_info;
4091 char m_strversion[STRVERSIONLEN];
4095 #ifdef CONFIG_FEATURE_CLEAN_UP
4100 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4101 int flag_print_load_map = 0;
4104 struct utsname myuname;
4106 /* Parse any options */
4107 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4108 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
4110 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
4114 case 'f': /* force loading */
4115 flag_force_load = 1;
4117 case 'k': /* module loaded by kerneld, auto-cleanable */
4120 case 's': /* log to syslog */
4121 /* log to syslog -- not supported */
4122 /* but kernel needs this for request_module(), */
4123 /* as this calls: modprobe -k -s -- <module> */
4124 /* so silently ignore this flag */
4126 case 'v': /* verbose output */
4129 case 'q': /* silent */
4132 case 'x': /* do not export externs */
4135 case 'o': /* name the output module */
4137 m_name = bb_xstrdup(optarg);
4139 case 'L': /* Stub warning */
4140 /* This is needed for compatibility with modprobe.
4141 * In theory, this does locking, but we don't do
4142 * that. So be careful and plan your life around not
4143 * loading the same module 50 times concurrently. */
4145 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4146 case 'm': /* print module load map */
4147 flag_print_load_map = 1;
4155 if (argv[optind] == NULL) {
4159 /* Grab the module name */
4160 tmp1 = bb_xstrdup(argv[optind]);
4161 tmp = basename(tmp1);
4164 if (uname(&myuname) == 0) {
4165 if (myuname.release[0] == '2') {
4166 k_version = myuname.release[2] - '0';
4170 #if defined(CONFIG_FEATURE_2_6_MODULES)
4171 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
4172 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
4178 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4184 #if defined(CONFIG_FEATURE_2_6_MODULES)
4186 bb_xasprintf(&m_fullName, "%s.ko", tmp);
4189 bb_xasprintf(&m_fullName, "%s.o", tmp);
4195 tmp1 = 0; /* flag for free(m_name) before exit() */
4198 /* Get a filedesc for the module. Check we we have a complete path */
4199 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
4200 (fp = fopen(argv[optind], "r")) == NULL) {
4201 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4202 * but do not error out yet if we fail to find it... */
4203 if (k_version) { /* uname succeedd */
4206 char real_module_dir[FILENAME_MAX];
4208 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4209 /* Jump through hoops in case /lib/modules/`uname -r`
4210 * is a symlink. We do not want recursive_action to
4211 * follow symlinks, but we do want to follow the
4212 * /lib/modules/`uname -r` dir, So resolve it ourselves
4213 * if it is a link... */
4214 if (realpath (tmdn, real_module_dir) == NULL)
4217 module_dir = real_module_dir;
4218 recursive_action(module_dir, TRUE, FALSE, FALSE,
4219 check_module_name_match, 0, m_fullName);
4223 /* Check if we have found anything yet */
4224 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4226 char module_dir[FILENAME_MAX];
4230 if (realpath (_PATH_MODULES, module_dir) == NULL)
4231 strcpy(module_dir, _PATH_MODULES);
4232 /* No module found under /lib/modules/`uname -r`, this
4233 * time cast the net a bit wider. Search /lib/modules/ */
4234 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4235 check_module_name_match, 0, m_fullName))
4238 || ((fp = fopen(m_filename, "r")) == NULL))
4240 bb_error_msg("%s: no module by that name found", m_fullName);
4244 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4247 m_filename = bb_xstrdup(argv[optind]);
4250 printf("Using %s\n", m_filename);
4252 #ifdef CONFIG_FEATURE_2_6_MODULES
4256 argv[optind + 1] = m_filename;
4257 return insmod_ng_main(argc - optind, argv + optind);
4261 if ((f = obj_load(fp, LOADBITS)) == NULL)
4262 bb_perror_msg_and_die("Could not load the module");
4264 if (get_modinfo_value(f, "kernel_version") == NULL)
4269 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4270 /* Version correspondence? */
4272 if (uname(&uts_info) < 0)
4273 uts_info.release[0] = '\0';
4274 if (m_has_modinfo) {
4275 m_version = new_get_module_version(f, m_strversion);
4277 m_version = old_get_module_version(f, m_strversion);
4278 if (m_version == -1) {
4279 bb_error_msg("couldn't find the kernel version the module was "
4285 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4286 if (flag_force_load) {
4287 bb_error_msg("Warning: kernel-module version mismatch\n"
4288 "\t%s was compiled for kernel version %s\n"
4289 "\twhile this kernel is version %s",
4290 m_filename, m_strversion, uts_info.release);
4292 bb_error_msg("kernel-module version mismatch\n"
4293 "\t%s was compiled for kernel version %s\n"
4294 "\twhile this kernel is version %s.",
4295 m_filename, m_strversion, uts_info.release);
4301 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4303 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
4305 if (k_new_syscalls) {
4306 #ifdef CONFIG_FEATURE_2_4_MODULES
4307 if (!new_get_kernel_symbols())
4309 k_crcs = new_is_kernel_checksummed();
4311 bb_error_msg("Not configured to support new kernels");
4315 #ifdef CONFIG_FEATURE_2_2_MODULES
4316 if (!old_get_kernel_symbols(m_name))
4318 k_crcs = old_is_kernel_checksummed();
4320 bb_error_msg("Not configured to support old kernels");
4325 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4327 m_crcs = new_is_module_checksummed(f);
4329 m_crcs = old_is_module_checksummed(f);
4331 if (m_crcs != k_crcs)
4332 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4333 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4335 /* Let the module know about the kernel symbols. */
4336 add_kernel_symbols(f);
4338 /* Allocate common symbols, symbol tables, and string tables. */
4341 ? !new_create_this_module(f, m_name)
4342 : !old_create_mod_use_count(f))
4347 if (!obj_check_undefineds(f)) {
4350 obj_allocate_commons(f);
4351 check_tainted_module(f, m_name);
4353 /* done with the module name, on to the optional var=value arguments */
4356 if (optind < argc) {
4358 ? !new_process_module_arguments(f, argc - optind, argv + optind)
4359 : !old_process_module_arguments(f, argc - optind, argv + optind))
4366 hide_special_symbols(f);
4368 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4369 add_ksymoops_symbols(f, m_filename, m_name);
4370 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4373 new_create_module_ksymtab(f);
4375 /* Find current size of the module */
4376 m_size = obj_load_size(f);
4379 m_addr = create_module(m_name, m_size);
4380 if (m_addr == -1) switch (errno) {
4382 bb_error_msg("A module named %s already exists", m_name);
4385 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4389 bb_perror_msg("create_module: %s", m_name);
4395 * the PROGBITS section was not loaded by the obj_load
4396 * now we can load them directly into the kernel memory
4398 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4399 delete_module(m_name);
4404 if (!obj_relocate(f, m_addr)) {
4405 delete_module(m_name);
4410 ? !new_init_module(m_name, f, m_size)
4411 : !old_init_module(m_name, f, m_size))
4413 delete_module(m_name);
4417 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4418 if(flag_print_load_map)
4422 exit_status = EXIT_SUCCESS;
4425 #ifdef CONFIG_FEATURE_CLEAN_UP
4435 return(exit_status);
4442 #ifdef CONFIG_FEATURE_2_6_MODULES
4444 #include <sys/mman.h>
4445 #include <asm/unistd.h>
4446 #include <sys/syscall.h>
4448 /* We use error numbers in a loose translation... */
4449 static const char *moderror(int err)
4453 return "Invalid module format";
4455 return "Unknown symbol in module";
4457 return "Module has wrong symbol version";
4459 return "Invalid parameters";
4461 return strerror(err);
4465 extern int insmod_ng_main( int argc, char **argv)
4473 char *filename, *options = bb_xstrdup("");
4481 /* Rest is options */
4482 for (i = 2; i < argc; i++) {
4483 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4484 /* Spaces handled by "" pairs, but no way of escaping quotes */
4485 if (strchr(argv[i], ' ')) {
4486 strcat(options, "\"");
4487 strcat(options, argv[i]);
4488 strcat(options, "\"");
4490 strcat(options, argv[i]);
4492 strcat(options, " ");
4495 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4496 bb_perror_msg_and_die("cannot open module `%s'", filename);
4501 map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
4502 if (map == MAP_FAILED) {
4503 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4506 ret = syscall(__NR_init_module, map, len, options);
4508 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4509 filename, moderror(errno), ret);