1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * This version of insmod supports ARM, CRIS, H8/300, x86, ia64, x86_64,
6 * m68k, MIPS, PowerPC, S390, SH3/4/5, Sparc, v850e, and x86_64.
8 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
9 * and Ron Alder <alder@lineo.com>
11 * Rodney Radford <rradford@mindspring.com> 17-Aug-2004.
12 * Added x86_64 support.
14 * Miles Bader <miles@gnu.org> added NEC V850E support.
16 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
17 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
19 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
20 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
21 * very minor changes required to also work with StrongArm and presumably
22 * all ARM based systems.
24 * Yoshinori Sato <ysato@users.sourceforge.jp> 19-May-2004.
25 * added Renesas H8/300 support.
27 * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
28 * Integrated support for sh64 (SH-5), from preliminary modutils
29 * patches from Benedict Gaster <benedict.gaster@superh.com>.
30 * Currently limited to support for 32bit ABI.
32 * Magnus Damm <damm@opensource.se> 22-May-2002.
33 * The plt and got code are now using the same structs.
34 * Added generic linked list code to fully support PowerPC.
35 * Replaced the mess in arch_apply_relocation() with architecture blocks.
36 * The arch_create_got() function got cleaned up with architecture blocks.
37 * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
39 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
40 * PowerPC specific code stolen from modutils-2.3.16,
41 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
42 * I've only tested the code on mpc8xx platforms in big-endian mode.
43 * Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
45 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
46 * based on modutils-2.4.2
47 * MIPS specific support for Elf loading and relocation.
48 * Copyright 1996, 1997 Linux International.
49 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
51 * Based almost entirely on the Linux modutils-2.3.11 implementation.
52 * Copyright 1996, 1997 Linux International.
53 * New implementation contributed by Richard Henderson <rth@tamu.edu>
54 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
55 * Restructured (and partly rewritten) by:
56 * Björn Ekwall <bj0rn@blox.se> February 1999
58 * This program is free software; you can redistribute it and/or modify
59 * it under the terms of the GNU General Public License as published by
60 * the Free Software Foundation; either version 2 of the License, or
61 * (at your option) any later version.
63 * This program is distributed in the hope that it will be useful,
64 * but WITHOUT ANY WARRANTY; without even the implied warranty of
65 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
66 * General Public License for more details.
68 * You should have received a copy of the GNU General Public License
69 * along with this program; if not, write to the Free Software
70 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
85 #include <sys/utsname.h>
88 #if !defined(CONFIG_FEATURE_2_4_MODULES) && \
89 !defined(CONFIG_FEATURE_2_6_MODULES)
90 #define CONFIG_FEATURE_2_4_MODULES
93 #if !defined(CONFIG_FEATURE_2_4_MODULES)
94 #define insmod_ng_main insmod_main
97 #if defined(CONFIG_FEATURE_2_6_MODULES)
98 extern int insmod_ng_main( int argc, char **argv);
102 #if defined(CONFIG_FEATURE_2_4_MODULES)
105 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
113 #if defined(__alpha__)
114 #define MATCH_MACHINE(x) (x == EM_ALPHA)
115 #define SHT_RELM SHT_RELA
116 #define Elf64_RelM Elf64_Rela
117 #define ELFCLASSM ELFCLASS64
122 #define MATCH_MACHINE(x) (x == EM_ARM)
123 #define SHT_RELM SHT_REL
124 #define Elf32_RelM Elf32_Rel
125 #define ELFCLASSM ELFCLASS32
126 #define CONFIG_USE_PLT_ENTRIES
127 #define CONFIG_PLT_ENTRY_SIZE 8
128 #define CONFIG_USE_GOT_ENTRIES
129 #define CONFIG_GOT_ENTRY_SIZE 8
130 #define CONFIG_USE_SINGLE
134 #if defined(__cris__)
135 #define MATCH_MACHINE(x) (x == EM_CRIS)
136 #define SHT_RELM SHT_RELA
137 #define Elf32_RelM Elf32_Rela
138 #define ELFCLASSM ELFCLASS32
141 #define R_CRIS_NONE 0
147 #if defined(__H8300H__) || defined(__H8300S__)
148 #define MATCH_MACHINE(x) (x == EM_H8_300)
149 #define SHT_RELM SHT_RELA
150 #define Elf32_RelM Elf32_Rela
151 #define ELFCLASSM ELFCLASS32
152 #define CONFIG_USE_SINGLE
153 #define SYMBOL_PREFIX "_"
156 /* PA-RISC / HP-PA */
157 #if defined(__hppa__)
158 #define MATCH_MACHINE(x) (x == EM_PARISC)
159 #define SHT_RELM SHT_RELA
160 #if defined(__LP64__)
161 #define Elf64_RelM Elf64_Rela
162 #define ELFCLASSM ELFCLASS64
164 #define Elf32_RelM Elf32_Rela
165 #define ELFCLASSM ELFCLASS32
170 #if defined(__i386__)
172 #define MATCH_MACHINE(x) (x == EM_386)
174 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
176 #define SHT_RELM SHT_REL
177 #define Elf32_RelM Elf32_Rel
178 #define ELFCLASSM ELFCLASS32
179 #define CONFIG_USE_GOT_ENTRIES
180 #define CONFIG_GOT_ENTRY_SIZE 4
181 #define CONFIG_USE_SINGLE
184 /* IA64, aka Itanium */
185 #if defined(__ia64__)
186 #define MATCH_MACHINE(x) (x == EM_IA_64)
187 #define SHT_RELM SHT_RELA
188 #define Elf64_RelM Elf64_Rela
189 #define ELFCLASSM ELFCLASS64
193 #if defined(__mc68000__)
194 #define MATCH_MACHINE(x) (x == EM_68K)
195 #define SHT_RELM SHT_RELA
196 #define Elf32_RelM Elf32_Rela
197 #define ELFCLASSM ELFCLASS32
198 #define CONFIG_USE_GOT_ENTRIES
199 #define CONFIG_GOT_ENTRY_SIZE 4
200 #define CONFIG_USE_SINGLE
204 #if defined(__mips__)
205 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
206 #define SHT_RELM SHT_REL
207 #define Elf32_RelM Elf32_Rel
208 #define ELFCLASSM ELFCLASS32
209 /* Account for ELF spec changes. */
210 #ifndef EM_MIPS_RS3_LE
211 #ifdef EM_MIPS_RS4_BE
212 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
214 #define EM_MIPS_RS3_LE 10
216 #endif /* !EM_MIPS_RS3_LE */
217 #define ARCHDATAM "__dbe_table"
221 #if defined(__nios2__)
222 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
223 #define SHT_RELM SHT_RELA
224 #define Elf32_RelM Elf32_Rela
225 #define ELFCLASSM ELFCLASS32
229 #if defined(__powerpc64__)
230 #define MATCH_MACHINE(x) (x == EM_PPC64)
231 #define SHT_RELM SHT_RELA
232 #define Elf64_RelM Elf64_Rela
233 #define ELFCLASSM ELFCLASS64
234 #elif defined(__powerpc__)
235 #define MATCH_MACHINE(x) (x == EM_PPC)
236 #define SHT_RELM SHT_RELA
237 #define Elf32_RelM Elf32_Rela
238 #define ELFCLASSM ELFCLASS32
239 #define CONFIG_USE_PLT_ENTRIES
240 #define CONFIG_PLT_ENTRY_SIZE 16
241 #define CONFIG_USE_PLT_LIST
242 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
243 #define CONFIG_USE_LIST
244 #define ARCHDATAM "__ftr_fixup"
248 #if defined(__s390__)
249 #define MATCH_MACHINE(x) (x == EM_S390)
250 #define SHT_RELM SHT_RELA
251 #define Elf32_RelM Elf32_Rela
252 #define ELFCLASSM ELFCLASS32
253 #define CONFIG_USE_PLT_ENTRIES
254 #define CONFIG_PLT_ENTRY_SIZE 8
255 #define CONFIG_USE_GOT_ENTRIES
256 #define CONFIG_GOT_ENTRY_SIZE 8
257 #define CONFIG_USE_SINGLE
262 #define MATCH_MACHINE(x) (x == EM_SH)
263 #define SHT_RELM SHT_RELA
264 #define Elf32_RelM Elf32_Rela
265 #define ELFCLASSM ELFCLASS32
266 #define CONFIG_USE_GOT_ENTRIES
267 #define CONFIG_GOT_ENTRY_SIZE 4
268 #define CONFIG_USE_SINGLE
269 /* the SH changes have only been tested in =little endian= mode */
270 /* I'm not sure about big endian, so let's warn: */
271 #if defined(__sh__) && BB_BIG_ENDIAN
272 # error insmod.c may require changes for use on big endian SH
274 /* it may or may not work on the SH1/SH2... Error on those also */
275 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
276 #error insmod.c may require changes for SH1 or SH2 use
281 #if defined(__sparc__)
282 #define MATCH_MACHINE(x) (x == EM_SPARC)
283 #define SHT_RELM SHT_RELA
284 #define Elf32_RelM Elf32_Rela
285 #define ELFCLASSM ELFCLASS32
289 #if defined (__v850e__)
290 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
291 #define SHT_RELM SHT_RELA
292 #define Elf32_RelM Elf32_Rela
293 #define ELFCLASSM ELFCLASS32
294 #define CONFIG_USE_PLT_ENTRIES
295 #define CONFIG_PLT_ENTRY_SIZE 8
296 #define CONFIG_USE_SINGLE
297 #ifndef EM_CYGNUS_V850 /* grumble */
298 #define EM_CYGNUS_V850 0x9080
300 #define SYMBOL_PREFIX "_"
304 #if defined(__x86_64__)
305 #define MATCH_MACHINE(x) (x == EM_X86_64)
306 #define SHT_RELM SHT_RELA
307 #define CONFIG_USE_GOT_ENTRIES
308 #define CONFIG_GOT_ENTRY_SIZE 8
309 #define CONFIG_USE_SINGLE
310 #define Elf64_RelM Elf64_Rela
311 #define ELFCLASSM ELFCLASS64
315 #error Sorry, but insmod.c does not yet support this architecture...
319 //----------------------------------------------------------------------------
320 //--------modutils module.h, lines 45-242
321 //----------------------------------------------------------------------------
323 /* Definitions for the Linux module syscall interface.
324 Copyright 1996, 1997 Linux International.
326 Contributed by Richard Henderson <rth@tamu.edu>
328 This file is part of the Linux modutils.
330 This program is free software; you can redistribute it and/or modify it
331 under the terms of the GNU General Public License as published by the
332 Free Software Foundation; either version 2 of the License, or (at your
333 option) any later version.
335 This program is distributed in the hope that it will be useful, but
336 WITHOUT ANY WARRANTY; without even the implied warranty of
337 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
338 General Public License for more details.
340 You should have received a copy of the GNU General Public License
341 along with this program; if not, write to the Free Software Foundation,
342 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
345 #ifndef MODUTILS_MODULE_H
346 /* Why? static const int MODUTILS_MODULE_H = 1;*/
348 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
350 /*======================================================================*/
351 /* For sizeof() which are related to the module platform and not to the
352 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
354 #define tgt_sizeof_char sizeof(char)
355 #define tgt_sizeof_short sizeof(short)
356 #define tgt_sizeof_int sizeof(int)
357 #define tgt_sizeof_long sizeof(long)
358 #define tgt_sizeof_char_p sizeof(char *)
359 #define tgt_sizeof_void_p sizeof(void *)
360 #define tgt_long long
362 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
363 #undef tgt_sizeof_long
364 #undef tgt_sizeof_char_p
365 #undef tgt_sizeof_void_p
369 tgt_sizeof_char_p = 8,
370 tgt_sizeof_void_p = 8
372 #define tgt_long long long
375 /*======================================================================*/
376 /* The structures used in Linux 2.1. */
378 /* Note: new_module_symbol does not use tgt_long intentionally */
379 struct new_module_symbol
385 struct new_module_persist;
387 struct new_module_ref
389 unsigned tgt_long dep; /* kernel addresses */
390 unsigned tgt_long ref;
391 unsigned tgt_long next_ref;
396 unsigned tgt_long size_of_struct; /* == sizeof(module) */
397 unsigned tgt_long next;
398 unsigned tgt_long name;
399 unsigned tgt_long size;
402 unsigned tgt_long flags; /* AUTOCLEAN et al */
407 unsigned tgt_long syms;
408 unsigned tgt_long deps;
409 unsigned tgt_long refs;
410 unsigned tgt_long init;
411 unsigned tgt_long cleanup;
412 unsigned tgt_long ex_table_start;
413 unsigned tgt_long ex_table_end;
415 unsigned tgt_long gp;
417 /* Everything after here is extension. */
418 unsigned tgt_long persist_start;
419 unsigned tgt_long persist_end;
420 unsigned tgt_long can_unload;
421 unsigned tgt_long runsize;
422 const char *kallsyms_start; /* All symbols for kernel debugging */
423 const char *kallsyms_end;
424 const char *archdata_start; /* arch specific data for module */
425 const char *archdata_end;
426 const char *kernel_data; /* Reserved for kernel internal use */
430 #define ARCHDATA_SEC_NAME ARCHDATAM
432 #define ARCHDATA_SEC_NAME "__archdata"
434 #define KALLSYMS_SEC_NAME "__kallsyms"
437 struct new_module_info
445 /* Bits of module.flags. */
449 NEW_MOD_AUTOCLEAN = 4,
451 NEW_MOD_USED_ONCE = 16
454 int init_module(const char *name, const struct new_module *);
455 int query_module(const char *name, int which, void *buf,
456 size_t bufsize, size_t *ret);
458 /* Values for query_module's which. */
467 /*======================================================================*/
468 /* The system calls unchanged between 2.0 and 2.1. */
470 unsigned long create_module(const char *, size_t);
471 int delete_module(const char *);
474 #endif /* module.h */
476 //----------------------------------------------------------------------------
477 //--------end of modutils module.h
478 //----------------------------------------------------------------------------
482 //----------------------------------------------------------------------------
483 //--------modutils obj.h, lines 253-462
484 //----------------------------------------------------------------------------
486 /* Elf object file loading and relocation routines.
487 Copyright 1996, 1997 Linux International.
489 Contributed by Richard Henderson <rth@tamu.edu>
491 This file is part of the Linux modutils.
493 This program is free software; you can redistribute it and/or modify it
494 under the terms of the GNU General Public License as published by the
495 Free Software Foundation; either version 2 of the License, or (at your
496 option) any later version.
498 This program is distributed in the hope that it will be useful, but
499 WITHOUT ANY WARRANTY; without even the implied warranty of
500 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
501 General Public License for more details.
503 You should have received a copy of the GNU General Public License
504 along with this program; if not, write to the Free Software Foundation,
505 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
508 #ifndef MODUTILS_OBJ_H
509 /* Why? static const int MODUTILS_OBJ_H = 1; */
511 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
513 /* The relocatable object is manipulated using elfin types. */
520 # define ELFDATAM ELFDATA2LSB
522 # define ELFDATAM ELFDATA2MSB
526 # if ELFCLASSM == ELFCLASS32
527 # define ElfW(x) Elf32_ ## x
528 # define ELFW(x) ELF32_ ## x
530 # define ElfW(x) Elf64_ ## x
531 # define ELFW(x) ELF64_ ## x
535 /* For some reason this is missing from some ancient C libraries.... */
536 #ifndef ELF32_ST_INFO
537 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
540 #ifndef ELF64_ST_INFO
541 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
544 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
545 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
546 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
547 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
548 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
550 struct obj_string_patch;
551 struct obj_symbol_patch;
558 struct obj_section *load_next;
564 struct obj_symbol *next; /* hash table link */
568 int secidx; /* the defining section index/module */
570 int ksymidx; /* for export to the kernel symtab */
571 int referenced; /* actually used in the link */
574 /* Hardcode the hash table size. We shouldn't be needing so many
575 symbols that we begin to degrade performance, and we get a big win
576 by giving the compiler a constant divisor. */
578 #define HASH_BUCKETS 521
584 struct obj_section **sections;
585 struct obj_section *load_order;
586 struct obj_section **load_order_search_start;
587 struct obj_string_patch *string_patches;
588 struct obj_symbol_patch *symbol_patches;
589 int (*symbol_cmp)(const char *, const char *);
590 unsigned long (*symbol_hash)(const char *);
591 unsigned long local_symtab_size;
592 struct obj_symbol **local_symtab;
593 struct obj_symbol *symtab[HASH_BUCKETS];
604 struct obj_string_patch
606 struct obj_string_patch *next;
608 ElfW(Addr) reloc_offset;
609 ElfW(Addr) string_offset;
612 struct obj_symbol_patch
614 struct obj_symbol_patch *next;
616 ElfW(Addr) reloc_offset;
617 struct obj_symbol *sym;
621 /* Generic object manipulation routines. */
623 static unsigned long obj_elf_hash(const char *);
625 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
627 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
630 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
631 struct obj_symbol *sym);
633 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
634 static void obj_set_symbol_compare(struct obj_file *f,
635 int (*cmp)(const char *, const char *),
636 unsigned long (*hash)(const char *));
639 static struct obj_section *obj_find_section (struct obj_file *f,
642 static void obj_insert_section_load_order (struct obj_file *f,
643 struct obj_section *sec);
645 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
650 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
655 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
657 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
660 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
661 struct obj_symbol *sym);
663 static int obj_check_undefineds(struct obj_file *f);
665 static void obj_allocate_commons(struct obj_file *f);
667 static unsigned long obj_load_size (struct obj_file *f);
669 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
671 static struct obj_file *obj_load(FILE *f, int loadprogbits);
673 static int obj_create_image (struct obj_file *f, char *image);
675 /* Architecture specific manipulation routines. */
677 static struct obj_file *arch_new_file (void);
679 static struct obj_section *arch_new_section (void);
681 static struct obj_symbol *arch_new_symbol (void);
683 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
684 struct obj_section *targsec,
685 struct obj_section *symsec,
686 struct obj_symbol *sym,
687 ElfW(RelM) *rel, ElfW(Addr) value);
689 static void arch_create_got (struct obj_file *f);
691 static int obj_gpl_license(struct obj_file *f, const char **license);
694 //----------------------------------------------------------------------------
695 //--------end of modutils obj.h
696 //----------------------------------------------------------------------------
699 /* SPFX is always a string, so it can be concatenated to string constants. */
701 #define SPFX SYMBOL_PREFIX
707 #define _PATH_MODULES "/lib/modules"
708 enum { STRVERSIONLEN = 32 };
710 /*======================================================================*/
712 static int flag_force_load = 0;
713 static int flag_autoclean = 0;
714 static int flag_verbose = 0;
715 static int flag_quiet = 0;
716 static int flag_export = 1;
719 /*======================================================================*/
721 #if defined(CONFIG_USE_LIST)
723 struct arch_list_entry
725 struct arch_list_entry *next;
726 CONFIG_LIST_ARCHTYPE addend;
733 #if defined(CONFIG_USE_SINGLE)
735 struct arch_single_entry
744 #if defined(__mips__)
747 struct mips_hi16 *next;
754 struct obj_file root;
755 #if defined(CONFIG_USE_PLT_ENTRIES)
756 struct obj_section *plt;
758 #if defined(CONFIG_USE_GOT_ENTRIES)
759 struct obj_section *got;
761 #if defined(__mips__)
762 struct mips_hi16 *mips_hi16_list;
767 struct obj_symbol root;
768 #if defined(CONFIG_USE_PLT_ENTRIES)
769 #if defined(CONFIG_USE_PLT_LIST)
770 struct arch_list_entry *pltent;
772 struct arch_single_entry pltent;
775 #if defined(CONFIG_USE_GOT_ENTRIES)
776 struct arch_single_entry gotent;
781 struct external_module {
786 struct new_module_symbol *syms;
789 static struct new_module_symbol *ksyms;
790 static size_t nksyms;
792 static struct external_module *ext_modules;
793 static int n_ext_modules;
794 static int n_ext_modules_used;
795 extern int delete_module(const char *);
797 static char *m_filename;
798 static char *m_fullName;
802 /*======================================================================*/
805 static int check_module_name_match(const char *filename, struct stat *statbuf,
808 char *fullname = (char *) userdata;
810 if (fullname[0] == '\0')
813 char *tmp, *tmp1 = bb_xstrdup(filename);
814 tmp = bb_get_last_path_component(tmp1);
815 if (strcmp(tmp, fullname) == 0) {
817 /* Stop searching if we find a match */
818 m_filename = bb_xstrdup(filename);
827 /*======================================================================*/
829 static struct obj_file *arch_new_file(void)
832 f = xmalloc(sizeof(*f));
834 memset(f, 0, sizeof(*f));
839 static struct obj_section *arch_new_section(void)
841 return xmalloc(sizeof(struct obj_section));
844 static struct obj_symbol *arch_new_symbol(void)
846 struct arch_symbol *sym;
847 sym = xmalloc(sizeof(*sym));
849 memset(sym, 0, sizeof(*sym));
854 static enum obj_reloc
855 arch_apply_relocation(struct obj_file *f,
856 struct obj_section *targsec,
857 struct obj_section *symsec,
858 struct obj_symbol *sym,
859 ElfW(RelM) *rel, ElfW(Addr) v)
861 struct arch_file *ifile = (struct arch_file *) f;
862 enum obj_reloc ret = obj_reloc_ok;
863 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
864 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
865 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
866 struct arch_symbol *isym = (struct arch_symbol *) sym;
868 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
869 #if defined(CONFIG_USE_GOT_ENTRIES)
870 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
873 #if defined(CONFIG_USE_PLT_ENTRIES)
874 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
876 # if defined(CONFIG_USE_PLT_LIST)
877 struct arch_list_entry *pe;
879 struct arch_single_entry *pe;
883 switch (ELF_R_TYPE(rel->r_info)) {
898 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
899 * (which is .got) similar to branch,
900 * but is full 32 bits relative */
910 case R_ARM_GOTOFF: /* address relative to the got */
915 #elif defined(__cris__)
921 /* CRIS keeps the relocation value in the r_addend field and
922 * should not use whats in *loc at all
927 #elif defined(__H8300H__) || defined(__H8300S__)
930 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
931 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
942 if ((ElfW(Sword))v > 0x7fff ||
943 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
944 ret = obj_reloc_overflow;
946 *(unsigned short *)loc = v;
950 if ((ElfW(Sword))v > 0x7f ||
951 (ElfW(Sword))v < -(ElfW(Sword))0x80)
952 ret = obj_reloc_overflow;
954 *(unsigned char *)loc = v;
957 #elif defined(__i386__)
993 #elif defined(__mc68000__)
1004 ret = obj_reloc_overflow;
1011 ret = obj_reloc_overflow;
1018 if ((ElfW(Sword))v > 0x7f ||
1019 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1020 ret = obj_reloc_overflow;
1027 if ((ElfW(Sword))v > 0x7fff ||
1028 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1029 ret = obj_reloc_overflow;
1035 *(int *)loc = v - dot;
1038 case R_68K_GLOB_DAT:
1039 case R_68K_JMP_SLOT:
1043 case R_68K_RELATIVE:
1044 *(int *)loc += f->baseaddr;
1050 # ifdef R_68K_GOTOFF
1057 #elif defined(__mips__)
1068 ret = obj_reloc_dangerous;
1069 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1070 ret = obj_reloc_overflow;
1072 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1078 struct mips_hi16 *n;
1080 /* We cannot relocate this one now because we don't know the value
1081 of the carry we need to add. Save the information, and let LO16
1082 do the actual relocation. */
1083 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1086 n->next = ifile->mips_hi16_list;
1087 ifile->mips_hi16_list = n;
1093 unsigned long insnlo = *loc;
1094 ElfW(Addr) val, vallo;
1096 /* Sign extend the addend we extract from the lo insn. */
1097 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1099 if (ifile->mips_hi16_list != NULL) {
1100 struct mips_hi16 *l;
1102 l = ifile->mips_hi16_list;
1104 struct mips_hi16 *next;
1107 /* The value for the HI16 had best be the same. */
1108 assert(v == l->value);
1110 /* Do the HI16 relocation. Note that we actually don't
1111 need to know anything about the LO16 itself, except where
1112 to find the low 16 bits of the addend needed by the LO16. */
1115 ((insn & 0xffff) << 16) +
1119 /* Account for the sign extension that will happen in the
1126 insn = (insn & ~0xffff) | val;
1134 ifile->mips_hi16_list = NULL;
1137 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1139 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1144 #elif defined(__nios2__)
1149 case R_NIOS2_BFD_RELOC_32:
1153 case R_NIOS2_BFD_RELOC_16:
1155 ret = obj_reloc_overflow;
1160 case R_NIOS2_BFD_RELOC_8:
1162 ret = obj_reloc_overflow;
1171 if ((Elf32_Sword)v > 0x7fff ||
1172 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1173 ret = obj_reloc_overflow;
1177 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1187 ret = obj_reloc_overflow;
1191 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1196 case R_NIOS2_PCREL16:
1201 if ((Elf32_Sword)v > 0x7fff ||
1202 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1203 ret = obj_reloc_overflow;
1207 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1213 Elf32_Addr word, gp;
1215 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1217 if ((Elf32_Sword)v > 0x7fff ||
1218 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1219 ret = obj_reloc_overflow;
1223 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1227 case R_NIOS2_CALL26:
1229 ret = obj_reloc_dangerous;
1230 if ((v >> 28) != (dot >> 28))
1231 ret = obj_reloc_overflow;
1232 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1240 ret = obj_reloc_overflow;
1243 word = *loc & ~0x7c0;
1244 *loc = word | ((v & 0x1f) << 6);
1253 ret = obj_reloc_overflow;
1256 word = *loc & ~0xfc0;
1257 *loc = word | ((v & 0x3f) << 6);
1266 ret = obj_reloc_overflow;
1269 word = *loc & ~0x3fc0;
1270 *loc = word | ((v & 0xff) << 6);
1279 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1289 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1294 case R_NIOS2_HIADJ16:
1296 Elf32_Addr word1, word2;
1299 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1300 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1305 #elif defined(__powerpc64__)
1306 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1308 #elif defined(__powerpc__)
1310 case R_PPC_ADDR16_HA:
1311 *(unsigned short *)loc = (v + 0x8000) >> 16;
1314 case R_PPC_ADDR16_HI:
1315 *(unsigned short *)loc = v >> 16;
1318 case R_PPC_ADDR16_LO:
1319 *(unsigned short *)loc = v;
1333 #elif defined(__s390__)
1336 *(unsigned int *) loc += v;
1339 *(unsigned short *) loc += v;
1342 *(unsigned char *) loc += v;
1346 *(unsigned int *) loc += v - dot;
1349 *(unsigned short *) loc += (v - dot) >> 1;
1352 *(unsigned short *) loc += v - dot;
1356 case R_390_PLT16DBL:
1357 /* find the plt entry and initialize it. */
1358 assert(isym != NULL);
1359 pe = (struct arch_single_entry *) &isym->pltent;
1360 assert(pe->allocated);
1361 if (pe->inited == 0) {
1362 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1363 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1365 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1372 /* Insert relative distance to target. */
1373 v = plt + pe->offset - dot;
1374 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1375 *(unsigned int *) loc = (unsigned int) v;
1376 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1377 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1380 case R_390_GLOB_DAT:
1381 case R_390_JMP_SLOT:
1385 case R_390_RELATIVE:
1386 *loc += f->baseaddr;
1391 *(unsigned long *) loc += got - dot;
1397 assert(isym != NULL);
1399 if (!isym->gotent.inited)
1401 isym->gotent.inited = 1;
1402 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1404 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1405 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1406 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1407 *(unsigned short *) loc += isym->gotent.offset;
1408 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1409 *(unsigned int *) loc += isym->gotent.offset;
1412 # ifndef R_390_GOTOFF32
1413 # define R_390_GOTOFF32 R_390_GOTOFF
1415 case R_390_GOTOFF32:
1420 #elif defined(__sh__)
1443 *loc = f->baseaddr + rel->r_addend;
1448 *loc = got - dot + rel->r_addend;
1459 # if defined(__SH5__)
1460 case R_SH_IMM_MEDLOW16:
1461 case R_SH_IMM_LOW16:
1465 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1469 * movi and shori have the format:
1471 * | op | imm | reg | reserved |
1472 * 31..26 25..10 9.. 4 3 .. 0
1474 * so we simply mask and or in imm.
1476 word = *loc & ~0x3fffc00;
1477 word |= (v & 0xffff) << 10;
1484 case R_SH_IMM_MEDLOW16_PCREL:
1485 case R_SH_IMM_LOW16_PCREL:
1489 word = *loc & ~0x3fffc00;
1493 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1496 word |= (v & 0xffff) << 10;
1502 # endif /* __SH5__ */
1504 #elif defined (__v850e__)
1510 /* We write two shorts instead of a long because even
1511 32-bit insns only need half-word alignment, but
1512 32-bit data needs to be long-word aligned. */
1513 v += ((unsigned short *)loc)[0];
1514 v += ((unsigned short *)loc)[1] << 16;
1515 ((unsigned short *)loc)[0] = v & 0xffff;
1516 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1519 case R_V850_22_PCREL:
1522 #elif defined(__x86_64__)
1532 *(unsigned int *) loc += v;
1535 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1536 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1541 *(signed int *) loc += v;
1545 *(unsigned short *) loc += v;
1549 *(unsigned char *) loc += v;
1553 *(unsigned int *) loc += v - dot;
1557 *(unsigned short *) loc += v - dot;
1561 *(unsigned char *) loc += v - dot;
1564 case R_X86_64_GLOB_DAT:
1565 case R_X86_64_JUMP_SLOT:
1569 case R_X86_64_RELATIVE:
1570 *loc += f->baseaddr;
1573 case R_X86_64_GOT32:
1574 case R_X86_64_GOTPCREL:
1577 assert(isym != NULL);
1578 if (!isym->gotent.reloc_done)
1580 isym->gotent.reloc_done = 1;
1581 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1583 /* XXX are these really correct? */
1584 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1585 *(unsigned int *) loc += v + isym->gotent.offset;
1587 *loc += isym->gotent.offset;
1592 # warning "no idea how to handle relocations on your arch"
1596 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1597 ret = obj_reloc_unhandled;
1600 #if defined(CONFIG_USE_PLT_ENTRIES)
1604 /* find the plt entry and initialize it if necessary */
1605 assert(isym != NULL);
1607 #if defined(CONFIG_USE_PLT_LIST)
1608 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1616 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1618 /* generate some machine code */
1620 #if defined(__arm__)
1621 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1622 ip[1] = v; /* sym@ */
1624 #if defined(__powerpc__)
1625 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1626 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1627 ip[2] = 0x7d6903a6; /* mtctr r11 */
1628 ip[3] = 0x4e800420; /* bctr */
1630 #if defined (__v850e__)
1631 /* We have to trash a register, so we assume that any control
1632 transfer more than 21-bits away must be a function call
1633 (so we can use a call-clobbered register). */
1634 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1635 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1640 /* relative distance to target */
1642 /* if the target is too far away.... */
1643 #if defined (__arm__) || defined (__powerpc__)
1644 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1645 #elif defined (__v850e__)
1646 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1648 /* go via the plt */
1649 v = plt + pe->offset - dot;
1651 #if defined (__v850e__)
1656 ret = obj_reloc_dangerous;
1658 /* merge the offset into the instruction. */
1659 #if defined(__arm__)
1660 /* Convert to words. */
1663 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1665 #if defined(__powerpc__)
1666 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1668 #if defined (__v850e__)
1669 /* We write two shorts instead of a long because even 32-bit insns
1670 only need half-word alignment, but the 32-bit data write needs
1671 to be long-word aligned. */
1672 ((unsigned short *)loc)[0] =
1673 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1674 | ((v >> 16) & 0x3f); /* offs high part */
1675 ((unsigned short *)loc)[1] =
1676 (v & 0xffff); /* offs low part */
1679 #endif /* CONFIG_USE_PLT_ENTRIES */
1681 #if defined(CONFIG_USE_GOT_ENTRIES)
1684 assert(isym != NULL);
1685 /* needs an entry in the .got: set it, once */
1686 if (!isym->gotent.inited) {
1687 isym->gotent.inited = 1;
1688 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1690 /* make the reloc with_respect_to_.got */
1692 *loc += isym->gotent.offset + rel->r_addend;
1693 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1694 *loc += isym->gotent.offset;
1698 #endif /* CONFIG_USE_GOT_ENTRIES */
1705 #if defined(CONFIG_USE_LIST)
1707 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1708 int offset, int size)
1710 struct arch_list_entry *pe;
1712 for (pe = *list; pe != NULL; pe = pe->next) {
1713 if (pe->addend == rel->r_addend) {
1719 pe = xmalloc(sizeof(struct arch_list_entry));
1721 pe->addend = rel->r_addend;
1722 pe->offset = offset;
1732 #if defined(CONFIG_USE_SINGLE)
1734 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1735 int offset, int size)
1737 if (single->allocated == 0) {
1738 single->allocated = 1;
1739 single->offset = offset;
1748 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1750 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1751 int offset, int size)
1753 struct obj_section *myrelsec = obj_find_section(f, name);
1760 obj_extend_section(myrelsec, offset);
1762 myrelsec = obj_create_alloced_section(f, name,
1772 static void arch_create_got(struct obj_file *f)
1774 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1775 struct arch_file *ifile = (struct arch_file *) f;
1777 #if defined(CONFIG_USE_GOT_ENTRIES)
1778 int got_offset = 0, got_needed = 0, got_allocate;
1780 #if defined(CONFIG_USE_PLT_ENTRIES)
1781 int plt_offset = 0, plt_needed = 0, plt_allocate;
1783 struct obj_section *relsec, *symsec, *strsec;
1784 ElfW(RelM) *rel, *relend;
1785 ElfW(Sym) *symtab, *extsym;
1786 const char *strtab, *name;
1787 struct arch_symbol *intsym;
1789 for (i = 0; i < f->header.e_shnum; ++i) {
1790 relsec = f->sections[i];
1791 if (relsec->header.sh_type != SHT_RELM)
1794 symsec = f->sections[relsec->header.sh_link];
1795 strsec = f->sections[symsec->header.sh_link];
1797 rel = (ElfW(RelM) *) relsec->contents;
1798 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1799 symtab = (ElfW(Sym) *) symsec->contents;
1800 strtab = (const char *) strsec->contents;
1802 for (; rel < relend; ++rel) {
1803 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1805 #if defined(CONFIG_USE_GOT_ENTRIES)
1808 #if defined(CONFIG_USE_PLT_ENTRIES)
1812 switch (ELF_R_TYPE(rel->r_info)) {
1813 #if defined(__arm__)
1828 #elif defined(__i386__)
1838 #elif defined(__powerpc__)
1843 #elif defined(__mc68000__)
1854 #elif defined(__sh__)
1864 #elif defined (__v850e__)
1865 case R_V850_22_PCREL:
1874 if (extsym->st_name != 0) {
1875 name = strtab + extsym->st_name;
1877 name = f->sections[extsym->st_shndx]->name;
1879 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1880 #if defined(CONFIG_USE_GOT_ENTRIES)
1882 got_offset += arch_single_init(
1883 rel, &intsym->gotent,
1884 got_offset, CONFIG_GOT_ENTRY_SIZE);
1889 #if defined(CONFIG_USE_PLT_ENTRIES)
1891 #if defined(CONFIG_USE_PLT_LIST)
1892 plt_offset += arch_list_add(
1893 rel, &intsym->pltent,
1894 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1896 plt_offset += arch_single_init(
1897 rel, &intsym->pltent,
1898 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1906 #if defined(CONFIG_USE_GOT_ENTRIES)
1908 ifile->got = arch_xsect_init(f, ".got", got_offset,
1909 CONFIG_GOT_ENTRY_SIZE);
1913 #if defined(CONFIG_USE_PLT_ENTRIES)
1915 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1916 CONFIG_PLT_ENTRY_SIZE);
1920 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1923 /*======================================================================*/
1925 /* Standard ELF hash function. */
1926 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1928 unsigned long h = 0;
1935 if ((g = (h & 0xf0000000)) != 0) {
1944 static unsigned long obj_elf_hash(const char *name)
1946 return obj_elf_hash_n(name, strlen(name));
1949 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1950 /* String comparison for non-co-versioned kernel and module. */
1952 static int ncv_strcmp(const char *a, const char *b)
1954 size_t alen = strlen(a), blen = strlen(b);
1956 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1957 return strncmp(a, b, alen);
1958 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1959 return strncmp(a, b, blen);
1961 return strcmp(a, b);
1964 /* String hashing for non-co-versioned kernel and module. Here
1965 we are simply forced to drop the crc from the hash. */
1967 static unsigned long ncv_symbol_hash(const char *str)
1969 size_t len = strlen(str);
1970 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1972 return obj_elf_hash_n(str, len);
1976 obj_set_symbol_compare(struct obj_file *f,
1977 int (*cmp) (const char *, const char *),
1978 unsigned long (*hash) (const char *))
1981 f->symbol_cmp = cmp;
1983 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1986 f->symbol_hash = hash;
1988 memcpy(tmptab, f->symtab, sizeof(tmptab));
1989 memset(f->symtab, 0, sizeof(f->symtab));
1991 for (i = 0; i < HASH_BUCKETS; ++i)
1992 for (sym = tmptab[i]; sym; sym = next) {
1993 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1995 sym->next = f->symtab[h];
2001 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2003 static struct obj_symbol *
2004 obj_add_symbol(struct obj_file *f, const char *name,
2005 unsigned long symidx, int info,
2006 int secidx, ElfW(Addr) value,
2009 struct obj_symbol *sym;
2010 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2011 int n_type = ELF_ST_TYPE(info);
2012 int n_binding = ELF_ST_BIND(info);
2014 for (sym = f->symtab[hash]; sym; sym = sym->next)
2015 if (f->symbol_cmp(sym->name, name) == 0) {
2016 int o_secidx = sym->secidx;
2017 int o_info = sym->info;
2018 int o_type = ELF_ST_TYPE(o_info);
2019 int o_binding = ELF_ST_BIND(o_info);
2021 /* A redefinition! Is it legal? */
2023 if (secidx == SHN_UNDEF)
2025 else if (o_secidx == SHN_UNDEF)
2027 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2028 /* Cope with local and global symbols of the same name
2029 in the same object file, as might have been created
2030 by ld -r. The only reason locals are now seen at this
2031 level at all is so that we can do semi-sensible things
2034 struct obj_symbol *nsym, **p;
2036 nsym = arch_new_symbol();
2037 nsym->next = sym->next;
2040 /* Excise the old (local) symbol from the hash chain. */
2041 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2045 } else if (n_binding == STB_LOCAL) {
2046 /* Another symbol of the same name has already been defined.
2047 Just add this to the local table. */
2048 sym = arch_new_symbol();
2051 f->local_symtab[symidx] = sym;
2053 } else if (n_binding == STB_WEAK)
2055 else if (o_binding == STB_WEAK)
2057 /* Don't unify COMMON symbols with object types the programmer
2059 else if (secidx == SHN_COMMON
2060 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2062 else if (o_secidx == SHN_COMMON
2063 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2066 /* Don't report an error if the symbol is coming from
2067 the kernel or some external module. */
2068 if (secidx <= SHN_HIRESERVE)
2069 bb_error_msg("%s multiply defined", name);
2074 /* Completely new symbol. */
2075 sym = arch_new_symbol();
2076 sym->next = f->symtab[hash];
2077 f->symtab[hash] = sym;
2080 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2081 if (symidx >= f->local_symtab_size)
2082 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2083 name, (long) symidx, (long) f->local_symtab_size);
2085 f->local_symtab[symidx] = sym;
2092 sym->secidx = secidx;
2098 static struct obj_symbol *
2099 obj_find_symbol(struct obj_file *f, const char *name)
2101 struct obj_symbol *sym;
2102 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2104 for (sym = f->symtab[hash]; sym; sym = sym->next)
2105 if (f->symbol_cmp(sym->name, name) == 0)
2112 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2115 if (sym->secidx >= SHN_LORESERVE)
2118 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2120 /* As a special case, a NULL sym has value zero. */
2125 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2127 int i, n = f->header.e_shnum;
2129 for (i = 0; i < n; ++i)
2130 if (strcmp(f->sections[i]->name, name) == 0)
2131 return f->sections[i];
2136 static int obj_load_order_prio(struct obj_section *a)
2138 unsigned long af, ac;
2140 af = a->header.sh_flags;
2143 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2144 strcmp(a->name + 5, ".init"))
2148 if (!(af & SHF_WRITE))
2150 if (af & SHF_EXECINSTR)
2152 if (a->header.sh_type != SHT_NOBITS)
2159 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2161 struct obj_section **p;
2162 int prio = obj_load_order_prio(sec);
2163 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2164 if (obj_load_order_prio(*p) < prio)
2166 sec->load_next = *p;
2170 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2172 unsigned long align,
2175 int newidx = f->header.e_shnum++;
2176 struct obj_section *sec;
2178 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2179 f->sections[newidx] = sec = arch_new_section();
2181 memset(sec, 0, sizeof(*sec));
2182 sec->header.sh_type = SHT_PROGBITS;
2183 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2184 sec->header.sh_size = size;
2185 sec->header.sh_addralign = align;
2189 sec->contents = xmalloc(size);
2191 obj_insert_section_load_order(f, sec);
2196 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2198 unsigned long align,
2201 int newidx = f->header.e_shnum++;
2202 struct obj_section *sec;
2204 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2205 f->sections[newidx] = sec = arch_new_section();
2207 memset(sec, 0, sizeof(*sec));
2208 sec->header.sh_type = SHT_PROGBITS;
2209 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2210 sec->header.sh_size = size;
2211 sec->header.sh_addralign = align;
2215 sec->contents = xmalloc(size);
2217 sec->load_next = f->load_order;
2218 f->load_order = sec;
2219 if (f->load_order_search_start == &f->load_order)
2220 f->load_order_search_start = &sec->load_next;
2225 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2227 unsigned long oldsize = sec->header.sh_size;
2229 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2231 return sec->contents + oldsize;
2235 /* Conditionally add the symbols from the given symbol set to the
2239 add_symbols_from( struct obj_file *f,
2240 int idx, struct new_module_symbol *syms, size_t nsyms)
2242 struct new_module_symbol *s;
2245 #ifdef SYMBOL_PREFIX
2247 size_t name_alloced_size = 0;
2249 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2252 gpl = obj_gpl_license(f, NULL) == 0;
2254 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2255 /* Only add symbols that are already marked external.
2256 If we override locals we may cause problems for
2257 argument initialization. We will also create a false
2258 dependency on the module. */
2259 struct obj_symbol *sym;
2262 /* GPL licensed modules can use symbols exported with
2263 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2264 * exported names. Non-GPL modules never see any GPLONLY_
2265 * symbols so they cannot fudge it by adding the prefix on
2268 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2269 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2276 name = (char *)s->name;
2278 #ifdef SYMBOL_PREFIX
2279 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2280 kernel exports `C names', but module object files
2281 reference `linker names'). */
2282 size_t extra = sizeof SYMBOL_PREFIX;
2283 size_t name_size = strlen (name) + extra;
2284 if (name_size > name_alloced_size) {
2285 name_alloced_size = name_size * 2;
2286 name_buf = alloca (name_alloced_size);
2288 strcpy (name_buf, SYMBOL_PREFIX);
2289 strcpy (name_buf + extra - 1, name);
2291 #endif /* SYMBOL_PREFIX */
2293 sym = obj_find_symbol(f, name);
2294 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2295 #ifdef SYMBOL_PREFIX
2296 /* Put NAME_BUF into more permanent storage. */
2297 name = xmalloc (name_size);
2298 strcpy (name, name_buf);
2300 sym = obj_add_symbol(f, name, -1,
2301 ELF_ST_INFO(STB_GLOBAL,
2304 /* Did our symbol just get installed? If so, mark the
2305 module as "used". */
2306 if (sym->secidx == idx)
2314 static void add_kernel_symbols(struct obj_file *f)
2316 struct external_module *m;
2319 /* Add module symbols first. */
2321 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2323 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2324 m->nsyms)) m->used = 1, ++nused;
2326 n_ext_modules_used = nused;
2328 /* And finally the symbols from the kernel proper. */
2331 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2334 static char *get_modinfo_value(struct obj_file *f, const char *key)
2336 struct obj_section *sec;
2337 char *p, *v, *n, *ep;
2338 size_t klen = strlen(key);
2340 sec = obj_find_section(f, ".modinfo");
2344 ep = p + sec->header.sh_size;
2347 n = strchr(p, '\0');
2349 if (p + klen == v && strncmp(p, key, klen) == 0)
2352 if (p + klen == n && strcmp(p, key) == 0)
2362 /*======================================================================*/
2363 /* Functions relating to module loading after 2.1.18. */
2366 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2369 char *p, *q, *key, *sym_name;
2370 struct obj_symbol *sym;
2371 char *contents, *loc;
2375 if ((q = strchr(p, '=')) == NULL) {
2380 key = alloca(q - p + 6);
2381 memcpy(key, "parm_", 5);
2382 memcpy(key + 5, p, q - p);
2385 p = get_modinfo_value(f, key);
2388 bb_error_msg("invalid parameter %s", key);
2392 #ifdef SYMBOL_PREFIX
2393 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2394 strcpy (sym_name, SYMBOL_PREFIX);
2395 strcat (sym_name, key);
2399 sym = obj_find_symbol(f, sym_name);
2401 /* Also check that the parameter was not resolved from the kernel. */
2402 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2403 bb_error_msg("symbol for parameter %s not found", key);
2408 min = strtoul(p, &p, 10);
2410 max = strtoul(p + 1, &p, 10);
2416 contents = f->sections[sym->secidx]->contents;
2417 loc = contents + sym->value;
2421 if ((*p == 's') || (*p == 'c')) {
2424 /* Do C quoting if we begin with a ", else slurp the lot. */
2428 str = alloca(strlen(q));
2429 for (r = str, q++; *q != '"'; ++q, ++r) {
2431 bb_error_msg("improperly terminated string argument for %s",
2434 } else if (*q == '\\')
2468 if (q[1] >= '0' && q[1] <= '7') {
2469 c = (c * 8) + *++q - '0';
2470 if (q[1] >= '0' && q[1] <= '7')
2471 c = (c * 8) + *++q - '0';
2488 /* In this case, the string is not quoted. We will break
2489 it using the coma (like for ints). If the user wants to
2490 include comas in a string, he just has to quote it */
2492 /* Search the next coma */
2496 if (r != (char *) NULL) {
2497 /* Recopy the current field */
2498 str = alloca(r - q + 1);
2499 memcpy(str, q, r - q);
2501 /* I don't know if it is useful, as the previous case
2502 doesn't nul terminate the string ??? */
2505 /* Keep next fields */
2516 obj_string_patch(f, sym->secidx, loc - contents, str);
2517 loc += tgt_sizeof_char_p;
2519 /* Array of chars (in fact, matrix !) */
2520 unsigned long charssize; /* size of each member */
2522 /* Get the size of each member */
2523 /* Probably we should do that outside the loop ? */
2524 if (!isdigit(*(p + 1))) {
2525 bb_error_msg("parameter type 'c' for %s must be followed by"
2526 " the maximum size", key);
2529 charssize = strtoul(p + 1, (char **) NULL, 10);
2532 if (strlen(str) >= charssize) {
2533 bb_error_msg("string too long for %s (max %ld)", key,
2538 /* Copy to location */
2539 strcpy((char *) loc, str);
2543 long v = strtoul(q, &q, 0);
2550 loc += tgt_sizeof_short;
2554 loc += tgt_sizeof_int;
2558 loc += tgt_sizeof_long;
2562 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2577 goto retry_end_of_value;
2581 bb_error_msg("too many values for %s (max %d)", key, max);
2588 bb_error_msg("invalid argument syntax for %s", key);
2595 bb_error_msg("too few values for %s (min %d)", key, min);
2605 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2606 static int new_is_module_checksummed(struct obj_file *f)
2608 const char *p = get_modinfo_value(f, "using_checksums");
2615 /* Get the module's kernel version in the canonical integer form. */
2618 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2623 p = get_modinfo_value(f, "kernel_version");
2626 safe_strncpy(str, p, STRVERSIONLEN);
2628 a = strtoul(p, &p, 10);
2631 b = strtoul(p + 1, &p, 10);
2634 c = strtoul(p + 1, &q, 10);
2638 return a << 16 | b << 8 | c;
2641 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2644 /* Fetch the loaded modules, and all currently exported symbols. */
2646 static int new_get_kernel_symbols(void)
2648 char *module_names, *mn;
2649 struct external_module *modules, *m;
2650 struct new_module_symbol *syms, *s;
2651 size_t ret, bufsize, nmod, nsyms, i, j;
2653 /* Collect the loaded modules. */
2655 module_names = xmalloc(bufsize = 256);
2657 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2658 if (errno == ENOSPC && bufsize < ret) {
2659 module_names = xrealloc(module_names, bufsize = ret);
2660 goto retry_modules_load;
2662 bb_perror_msg("QM_MODULES");
2666 n_ext_modules = nmod = ret;
2668 /* Collect the modules' symbols. */
2671 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2672 memset(modules, 0, nmod * sizeof(*modules));
2673 for (i = 0, mn = module_names, m = modules;
2674 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2675 struct new_module_info info;
2677 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2678 if (errno == ENOENT) {
2679 /* The module was removed out from underneath us. */
2682 bb_perror_msg("query_module: QM_INFO: %s", mn);
2686 syms = xmalloc(bufsize = 1024);
2688 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2691 syms = xrealloc(syms, bufsize = ret);
2692 goto retry_mod_sym_load;
2694 /* The module was removed out from underneath us. */
2697 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2704 m->addr = info.addr;
2708 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2709 s->name += (unsigned long) syms;
2714 /* Collect the kernel's symbols. */
2716 syms = xmalloc(bufsize = 16 * 1024);
2717 retry_kern_sym_load:
2718 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2719 if (errno == ENOSPC && bufsize < ret) {
2720 syms = xrealloc(syms, bufsize = ret);
2721 goto retry_kern_sym_load;
2723 bb_perror_msg("kernel: QM_SYMBOLS");
2726 nksyms = nsyms = ret;
2729 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2730 s->name += (unsigned long) syms;
2736 /* Return the kernel symbol checksum version, or zero if not used. */
2738 static int new_is_kernel_checksummed(void)
2740 struct new_module_symbol *s;
2743 /* Using_Versions is not the first symbol, but it should be in there. */
2745 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2746 if (strcmp((char *) s->name, "Using_Versions") == 0)
2753 static int new_create_this_module(struct obj_file *f, const char *m_name)
2755 struct obj_section *sec;
2757 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2758 sizeof(struct new_module));
2759 memset(sec->contents, 0, sizeof(struct new_module));
2761 obj_add_symbol(f, SPFX "__this_module", -1,
2762 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2763 sizeof(struct new_module));
2765 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2771 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2772 /* add an entry to the __ksymtab section, creating it if necessary */
2773 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2775 struct obj_section *sec;
2778 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2779 * If __ksymtab is defined but not marked alloc, x out the first character
2780 * (no obj_delete routine) and create a new __ksymtab with the correct
2783 sec = obj_find_section(f, "__ksymtab");
2784 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2785 *((char *)(sec->name)) = 'x'; /* override const */
2789 sec = obj_create_alloced_section(f, "__ksymtab",
2790 tgt_sizeof_void_p, 0);
2793 sec->header.sh_flags |= SHF_ALLOC;
2794 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2796 ofs = sec->header.sh_size;
2797 obj_symbol_patch(f, sec->idx, ofs, sym);
2798 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2799 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2801 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2803 static int new_create_module_ksymtab(struct obj_file *f)
2805 struct obj_section *sec;
2808 /* We must always add the module references. */
2810 if (n_ext_modules_used) {
2811 struct new_module_ref *dep;
2812 struct obj_symbol *tm;
2814 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2815 (sizeof(struct new_module_ref)
2816 * n_ext_modules_used));
2820 tm = obj_find_symbol(f, SPFX "__this_module");
2821 dep = (struct new_module_ref *) sec->contents;
2822 for (i = 0; i < n_ext_modules; ++i)
2823 if (ext_modules[i].used) {
2824 dep->dep = ext_modules[i].addr;
2825 obj_symbol_patch(f, sec->idx,
2826 (char *) &dep->ref - sec->contents, tm);
2832 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2837 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2840 /* We don't want to export symbols residing in sections that
2841 aren't loaded. There are a number of these created so that
2842 we make sure certain module options don't appear twice. */
2844 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2846 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2848 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2849 struct obj_symbol *sym;
2850 for (sym = f->symtab[i]; sym; sym = sym->next)
2851 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2852 && sym->secidx <= SHN_HIRESERVE
2853 && (sym->secidx >= SHN_LORESERVE
2854 || loaded[sym->secidx])) {
2855 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2857 obj_symbol_patch(f, sec->idx, ofs, sym);
2858 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2865 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2873 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2875 struct new_module *module;
2876 struct obj_section *sec;
2881 sec = obj_find_section(f, ".this");
2882 if (!sec || !sec->contents) {
2883 bb_perror_msg_and_die("corrupt module %s?",m_name);
2885 module = (struct new_module *) sec->contents;
2886 m_addr = sec->header.sh_addr;
2888 module->size_of_struct = sizeof(*module);
2889 module->size = m_size;
2890 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2892 sec = obj_find_section(f, "__ksymtab");
2893 if (sec && sec->header.sh_size) {
2894 module->syms = sec->header.sh_addr;
2895 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2898 if (n_ext_modules_used) {
2899 sec = obj_find_section(f, ".kmodtab");
2900 module->deps = sec->header.sh_addr;
2901 module->ndeps = n_ext_modules_used;
2905 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2907 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2909 sec = obj_find_section(f, "__ex_table");
2911 module->ex_table_start = sec->header.sh_addr;
2912 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2915 sec = obj_find_section(f, ".text.init");
2917 module->runsize = sec->header.sh_addr - m_addr;
2919 sec = obj_find_section(f, ".data.init");
2921 if (!module->runsize ||
2922 module->runsize > sec->header.sh_addr - m_addr)
2923 module->runsize = sec->header.sh_addr - m_addr;
2925 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2926 if (sec && sec->header.sh_size) {
2927 module->archdata_start = (void*)sec->header.sh_addr;
2928 module->archdata_end = module->archdata_start + sec->header.sh_size;
2930 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2931 if (sec && sec->header.sh_size) {
2932 module->kallsyms_start = (void*)sec->header.sh_addr;
2933 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2936 /* Whew! All of the initialization is complete. Collect the final
2937 module image and give it to the kernel. */
2939 image = xmalloc(m_size);
2940 obj_create_image(f, image);
2942 ret = init_module(m_name, (struct new_module *) image);
2944 bb_perror_msg("init_module: %s", m_name);
2952 /*======================================================================*/
2955 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2958 struct obj_string_patch *p;
2959 struct obj_section *strsec;
2960 size_t len = strlen(string) + 1;
2963 p = xmalloc(sizeof(*p));
2964 p->next = f->string_patches;
2965 p->reloc_secidx = secidx;
2966 p->reloc_offset = offset;
2967 f->string_patches = p;
2969 strsec = obj_find_section(f, ".kstrtab");
2970 if (strsec == NULL) {
2971 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2972 p->string_offset = 0;
2973 loc = strsec->contents;
2975 p->string_offset = strsec->header.sh_size;
2976 loc = obj_extend_section(strsec, len);
2978 memcpy(loc, string, len);
2984 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2985 struct obj_symbol *sym)
2987 struct obj_symbol_patch *p;
2989 p = xmalloc(sizeof(*p));
2990 p->next = f->symbol_patches;
2991 p->reloc_secidx = secidx;
2992 p->reloc_offset = offset;
2994 f->symbol_patches = p;
2999 static int obj_check_undefineds(struct obj_file *f)
3004 for (i = 0; i < HASH_BUCKETS; ++i) {
3005 struct obj_symbol *sym;
3006 for (sym = f->symtab[i]; sym; sym = sym->next)
3007 if (sym->secidx == SHN_UNDEF) {
3008 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3009 sym->secidx = SHN_ABS;
3013 bb_error_msg("unresolved symbol %s", sym->name);
3023 static void obj_allocate_commons(struct obj_file *f)
3025 struct common_entry {
3026 struct common_entry *next;
3027 struct obj_symbol *sym;
3028 } *common_head = NULL;
3032 for (i = 0; i < HASH_BUCKETS; ++i) {
3033 struct obj_symbol *sym;
3034 for (sym = f->symtab[i]; sym; sym = sym->next)
3035 if (sym->secidx == SHN_COMMON) {
3036 /* Collect all COMMON symbols and sort them by size so as to
3037 minimize space wasted by alignment requirements. */
3039 struct common_entry **p, *n;
3040 for (p = &common_head; *p; p = &(*p)->next)
3041 if (sym->size <= (*p)->sym->size)
3044 n = alloca(sizeof(*n));
3052 for (i = 1; i < f->local_symtab_size; ++i) {
3053 struct obj_symbol *sym = f->local_symtab[i];
3054 if (sym && sym->secidx == SHN_COMMON) {
3055 struct common_entry **p, *n;
3056 for (p = &common_head; *p; p = &(*p)->next)
3057 if (sym == (*p)->sym)
3059 else if (sym->size < (*p)->sym->size) {
3060 n = alloca(sizeof(*n));
3070 /* Find the bss section. */
3071 for (i = 0; i < f->header.e_shnum; ++i)
3072 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3075 /* If for some reason there hadn't been one, create one. */
3076 if (i == f->header.e_shnum) {
3077 struct obj_section *sec;
3079 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3080 f->sections[i] = sec = arch_new_section();
3081 f->header.e_shnum = i + 1;
3083 memset(sec, 0, sizeof(*sec));
3084 sec->header.sh_type = SHT_PROGBITS;
3085 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3090 /* Allocate the COMMONS. */
3092 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3093 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3094 struct common_entry *c;
3096 for (c = common_head; c; c = c->next) {
3097 ElfW(Addr) align = c->sym->value;
3099 if (align > max_align)
3101 if (bss_size & (align - 1))
3102 bss_size = (bss_size | (align - 1)) + 1;
3105 c->sym->value = bss_size;
3107 bss_size += c->sym->size;
3110 f->sections[i]->header.sh_size = bss_size;
3111 f->sections[i]->header.sh_addralign = max_align;
3115 /* For the sake of patch relocation and parameter initialization,
3116 allocate zeroed data for NOBITS sections now. Note that after
3117 this we cannot assume NOBITS are really empty. */
3118 for (i = 0; i < f->header.e_shnum; ++i) {
3119 struct obj_section *s = f->sections[i];
3120 if (s->header.sh_type == SHT_NOBITS) {
3121 if (s->header.sh_size != 0)
3122 s->contents = memset(xmalloc(s->header.sh_size),
3123 0, s->header.sh_size);
3127 s->header.sh_type = SHT_PROGBITS;
3132 static unsigned long obj_load_size(struct obj_file *f)
3134 unsigned long dot = 0;
3135 struct obj_section *sec;
3137 /* Finalize the positions of the sections relative to one another. */
3139 for (sec = f->load_order; sec; sec = sec->load_next) {
3142 align = sec->header.sh_addralign;
3143 if (align && (dot & (align - 1)))
3144 dot = (dot | (align - 1)) + 1;
3146 sec->header.sh_addr = dot;
3147 dot += sec->header.sh_size;
3153 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3155 int i, n = f->header.e_shnum;
3158 /* Finalize the addresses of the sections. */
3161 for (i = 0; i < n; ++i)
3162 f->sections[i]->header.sh_addr += base;
3164 /* And iterate over all of the relocations. */
3166 for (i = 0; i < n; ++i) {
3167 struct obj_section *relsec, *symsec, *targsec, *strsec;
3168 ElfW(RelM) * rel, *relend;
3172 relsec = f->sections[i];
3173 if (relsec->header.sh_type != SHT_RELM)
3176 symsec = f->sections[relsec->header.sh_link];
3177 targsec = f->sections[relsec->header.sh_info];
3178 strsec = f->sections[symsec->header.sh_link];
3180 rel = (ElfW(RelM) *) relsec->contents;
3181 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3182 symtab = (ElfW(Sym) *) symsec->contents;
3183 strtab = (const char *) strsec->contents;
3185 for (; rel < relend; ++rel) {
3186 ElfW(Addr) value = 0;
3187 struct obj_symbol *intsym = NULL;
3188 unsigned long symndx;
3189 ElfW(Sym) * extsym = 0;
3192 /* Attempt to find a value to use for this relocation. */
3194 symndx = ELF_R_SYM(rel->r_info);
3196 /* Note we've already checked for undefined symbols. */
3198 extsym = &symtab[symndx];
3199 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3200 /* Local symbols we look up in the local table to be sure
3201 we get the one that is really intended. */
3202 intsym = f->local_symtab[symndx];
3204 /* Others we look up in the hash table. */
3206 if (extsym->st_name)
3207 name = strtab + extsym->st_name;
3209 name = f->sections[extsym->st_shndx]->name;
3210 intsym = obj_find_symbol(f, name);
3213 value = obj_symbol_final_value(f, intsym);
3214 intsym->referenced = 1;
3216 #if SHT_RELM == SHT_RELA
3217 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3218 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3219 if (!extsym || !extsym->st_name ||
3220 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3222 value += rel->r_addend;
3226 switch (arch_apply_relocation
3227 (f, targsec, symsec, intsym, rel, value)) {
3231 case obj_reloc_overflow:
3232 errmsg = "Relocation overflow";
3234 case obj_reloc_dangerous:
3235 errmsg = "Dangerous relocation";
3237 case obj_reloc_unhandled:
3238 errmsg = "Unhandled relocation";
3241 bb_error_msg("%s of type %ld for %s", errmsg,
3242 (long) ELF_R_TYPE(rel->r_info),
3243 strtab + extsym->st_name);
3245 bb_error_msg("%s of type %ld", errmsg,
3246 (long) ELF_R_TYPE(rel->r_info));
3254 /* Finally, take care of the patches. */
3256 if (f->string_patches) {
3257 struct obj_string_patch *p;
3258 struct obj_section *strsec;
3259 ElfW(Addr) strsec_base;
3260 strsec = obj_find_section(f, ".kstrtab");
3261 strsec_base = strsec->header.sh_addr;
3263 for (p = f->string_patches; p; p = p->next) {
3264 struct obj_section *targsec = f->sections[p->reloc_secidx];
3265 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3266 = strsec_base + p->string_offset;
3270 if (f->symbol_patches) {
3271 struct obj_symbol_patch *p;
3273 for (p = f->symbol_patches; p; p = p->next) {
3274 struct obj_section *targsec = f->sections[p->reloc_secidx];
3275 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3276 = obj_symbol_final_value(f, p->sym);
3283 static int obj_create_image(struct obj_file *f, char *image)
3285 struct obj_section *sec;
3286 ElfW(Addr) base = f->baseaddr;
3288 for (sec = f->load_order; sec; sec = sec->load_next) {
3291 if (sec->contents == 0 || sec->header.sh_size == 0)
3294 secimg = image + (sec->header.sh_addr - base);
3296 /* Note that we allocated data for NOBITS sections earlier. */
3297 memcpy(secimg, sec->contents, sec->header.sh_size);
3303 /*======================================================================*/
3305 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3308 ElfW(Shdr) * section_headers;
3312 /* Read the file header. */
3314 f = arch_new_file();
3315 memset(f, 0, sizeof(*f));
3316 f->symbol_cmp = strcmp;
3317 f->symbol_hash = obj_elf_hash;
3318 f->load_order_search_start = &f->load_order;
3320 fseek(fp, 0, SEEK_SET);
3321 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3322 bb_perror_msg("error reading ELF header");
3326 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3327 || f->header.e_ident[EI_MAG1] != ELFMAG1
3328 || f->header.e_ident[EI_MAG2] != ELFMAG2
3329 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3330 bb_error_msg("not an ELF file");
3333 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3334 || f->header.e_ident[EI_DATA] != ELFDATAM
3335 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3336 || !MATCH_MACHINE(f->header.e_machine)) {
3337 bb_error_msg("ELF file not for this architecture");
3340 if (f->header.e_type != ET_REL) {
3341 bb_error_msg("ELF file not a relocatable object");
3345 /* Read the section headers. */
3347 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3348 bb_error_msg("section header size mismatch: %lu != %lu",
3349 (unsigned long) f->header.e_shentsize,
3350 (unsigned long) sizeof(ElfW(Shdr)));
3354 shnum = f->header.e_shnum;
3355 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3356 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3358 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3359 fseek(fp, f->header.e_shoff, SEEK_SET);
3360 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3361 bb_perror_msg("error reading ELF section headers");
3365 /* Read the section data. */
3367 for (i = 0; i < shnum; ++i) {
3368 struct obj_section *sec;
3370 f->sections[i] = sec = arch_new_section();
3371 memset(sec, 0, sizeof(*sec));
3373 sec->header = section_headers[i];
3376 if(sec->header.sh_size) switch (sec->header.sh_type) {
3385 if (!loadprogbits) {
3386 sec->contents = NULL;
3393 if (sec->header.sh_size > 0) {
3394 sec->contents = xmalloc(sec->header.sh_size);
3395 fseek(fp, sec->header.sh_offset, SEEK_SET);
3396 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3397 bb_perror_msg("error reading ELF section data");
3401 sec->contents = NULL;
3405 #if SHT_RELM == SHT_REL
3407 bb_error_msg("RELA relocations not supported on this architecture");
3411 bb_error_msg("REL relocations not supported on this architecture");
3416 if (sec->header.sh_type >= SHT_LOPROC) {
3417 /* Assume processor specific section types are debug
3418 info and can safely be ignored. If this is ever not
3419 the case (Hello MIPS?), don't put ifdefs here but
3420 create an arch_load_proc_section(). */
3424 bb_error_msg("can't handle sections of type %ld",
3425 (long) sec->header.sh_type);
3430 /* Do what sort of interpretation as needed by each section. */
3432 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3434 for (i = 0; i < shnum; ++i) {
3435 struct obj_section *sec = f->sections[i];
3436 sec->name = shstrtab + sec->header.sh_name;
3439 for (i = 0; i < shnum; ++i) {
3440 struct obj_section *sec = f->sections[i];
3442 /* .modinfo should be contents only but gcc has no attribute for that.
3443 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3445 if (strcmp(sec->name, ".modinfo") == 0)
3446 sec->header.sh_flags &= ~SHF_ALLOC;
3448 if (sec->header.sh_flags & SHF_ALLOC)
3449 obj_insert_section_load_order(f, sec);
3451 switch (sec->header.sh_type) {
3454 unsigned long nsym, j;
3458 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3459 bb_error_msg("symbol size mismatch: %lu != %lu",
3460 (unsigned long) sec->header.sh_entsize,
3461 (unsigned long) sizeof(ElfW(Sym)));
3465 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3466 strtab = f->sections[sec->header.sh_link]->contents;
3467 sym = (ElfW(Sym) *) sec->contents;
3469 /* Allocate space for a table of local symbols. */
3470 j = f->local_symtab_size = sec->header.sh_info;
3471 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3473 /* Insert all symbols into the hash table. */
3474 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3475 ElfW(Addr) val = sym->st_value;
3478 name = strtab + sym->st_name;
3479 else if (sym->st_shndx < shnum)
3480 name = f->sections[sym->st_shndx]->name;
3484 #if defined(__SH5__)
3486 * For sh64 it is possible that the target of a branch
3487 * requires a mode switch (32 to 16 and back again).
3489 * This is implied by the lsb being set in the target
3490 * address for SHmedia mode and clear for SHcompact.
3492 val |= sym->st_other & 4;
3495 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3502 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3503 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3504 (unsigned long) sec->header.sh_entsize,
3505 (unsigned long) sizeof(ElfW(RelM)));
3509 /* XXX Relocation code from modutils-2.3.19 is not here.
3510 * Why? That's about 20 lines of code from obj/obj_load.c,
3511 * which gets done in a second pass through the sections.
3512 * This BusyBox insmod does similar work in obj_relocate(). */
3519 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3521 * load the unloaded sections directly into the memory allocated by
3522 * kernel for the module
3525 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3527 ElfW(Addr) base = f->baseaddr;
3528 struct obj_section* sec;
3530 for (sec = f->load_order; sec; sec = sec->load_next) {
3532 /* section already loaded? */
3533 if (sec->contents != NULL)
3536 if (sec->header.sh_size == 0)
3539 sec->contents = imagebase + (sec->header.sh_addr - base);
3540 fseek(fp, sec->header.sh_offset, SEEK_SET);
3541 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3542 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3551 static void hide_special_symbols(struct obj_file *f)
3553 static const char *const specials[] = {
3554 SPFX "cleanup_module",
3556 SPFX "kernel_version",
3560 struct obj_symbol *sym;
3561 const char *const *p;
3563 for (p = specials; *p; ++p)
3564 if ((sym = obj_find_symbol(f, *p)) != NULL)
3566 ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3570 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3571 static int obj_gpl_license(struct obj_file *f, const char **license)
3573 struct obj_section *sec;
3574 /* This list must match *exactly* the list of allowable licenses in
3575 * linux/include/linux/module.h. Checking for leading "GPL" will not
3576 * work, somebody will use "GPL sucks, this is proprietary".
3578 static const char * const gpl_licenses[] = {
3581 "GPL and additional rights",
3586 if ((sec = obj_find_section(f, ".modinfo"))) {
3587 const char *value, *ptr, *endptr;
3588 ptr = sec->contents;
3589 endptr = ptr + sec->header.sh_size;
3590 while (ptr < endptr) {
3591 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3595 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3596 if (strcmp(value+1, gpl_licenses[i]) == 0)
3601 if (strchr(ptr, '\0'))
3602 ptr = strchr(ptr, '\0') + 1;
3610 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3611 #define TAINT_PROPRIETORY_MODULE (1<<0)
3612 #define TAINT_FORCED_MODULE (1<<1)
3613 #define TAINT_UNSAFE_SMP (1<<2)
3614 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3616 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3617 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3621 static int first = 1;
3622 if (fd < 0 && !kernel_has_tainted)
3623 return; /* New modutils on old kernel */
3624 printf("Warning: loading %s will taint the kernel: %s%s\n",
3625 m_name, text1, text2);
3627 printf(" See %s for information about tainted modules\n", TAINT_URL);
3631 read(fd, buf, sizeof(buf)-1);
3632 buf[sizeof(buf)-1] = '\0';
3633 oldval = strtoul(buf, NULL, 10);
3634 sprintf(buf, "%d\n", oldval | taint);
3635 write(fd, buf, strlen(buf));
3639 /* Check if loading this module will taint the kernel. */
3640 static void check_tainted_module(struct obj_file *f, char *m_name)
3642 static const char tainted_file[] = TAINT_FILENAME;
3643 int fd, kernel_has_tainted;
3646 kernel_has_tainted = 1;
3647 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3648 if (errno == ENOENT)
3649 kernel_has_tainted = 0;
3650 else if (errno == EACCES)
3651 kernel_has_tainted = 1;
3653 perror(tainted_file);
3654 kernel_has_tainted = 0;
3658 switch (obj_gpl_license(f, &ptr)) {
3662 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3665 /* The module has a non-GPL license so we pretend that the
3666 * kernel always has a taint flag to get a warning even on
3667 * kernels without the proc flag.
3669 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3672 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3676 if (flag_force_load)
3677 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3682 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3683 #define check_tainted_module(x, y) do { } while(0);
3684 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3686 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3687 /* add module source, timestamp, kernel version and a symbol for the
3688 * start of some sections. this info is used by ksymoops to do better
3692 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3694 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3695 return new_get_module_version(f, str);
3696 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3697 strncpy(str, "???", sizeof(str));
3699 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3702 /* add module source, timestamp, kernel version and a symbol for the
3703 * start of some sections. this info is used by ksymoops to do better
3707 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3710 static const char symprefix[] = "__insmod_";
3711 struct obj_section *sec;
3712 struct obj_symbol *sym;
3713 char *name, *absolute_filename;
3714 char str[STRVERSIONLEN], real[PATH_MAX];
3715 int i, l, lm_name, lfilename, use_ksymtab, version;
3716 struct stat statbuf;
3718 static const char *section_names[] = {
3726 if (realpath(filename, real)) {
3727 absolute_filename = bb_xstrdup(real);
3730 int save_errno = errno;
3731 bb_error_msg("cannot get realpath for %s", filename);
3734 absolute_filename = bb_xstrdup(filename);
3737 lm_name = strlen(m_name);
3738 lfilename = strlen(absolute_filename);
3740 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3741 * are not to be exported. otherwise leave ksymtab alone for now, the
3742 * "export all symbols" compatibility code will export these symbols later.
3744 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3746 if ((sec = obj_find_section(f, ".this"))) {
3747 /* tag the module header with the object name, last modified
3748 * timestamp and module version. worst case for module version
3749 * is 0xffffff, decimal 16777215. putting all three fields in
3750 * one symbol is less readable but saves kernel space.
3752 l = sizeof(symprefix)+ /* "__insmod_" */
3753 lm_name+ /* module name */
3755 lfilename+ /* object filename */
3757 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3759 8+ /* version in dec */
3762 if (stat(absolute_filename, &statbuf) != 0)
3763 statbuf.st_mtime = 0;
3764 version = get_module_version(f, str); /* -1 if not found */
3765 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3766 symprefix, m_name, absolute_filename,
3767 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3769 sym = obj_add_symbol(f, name, -1,
3770 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3771 sec->idx, sec->header.sh_addr, 0);
3773 new_add_ksymtab(f, sym);
3775 free(absolute_filename);
3776 #ifdef _NOT_SUPPORTED_
3777 /* record where the persistent data is going, same address as previous symbol */
3780 l = sizeof(symprefix)+ /* "__insmod_" */
3781 lm_name+ /* module name */
3783 strlen(f->persist)+ /* data store */
3786 snprintf(name, l, "%s%s_P%s",
3787 symprefix, m_name, f->persist);
3788 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3789 sec->idx, sec->header.sh_addr, 0);
3791 new_add_ksymtab(f, sym);
3793 #endif /* _NOT_SUPPORTED_ */
3794 /* tag the desired sections if size is non-zero */
3796 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3797 if ((sec = obj_find_section(f, section_names[i])) &&
3798 sec->header.sh_size) {
3799 l = sizeof(symprefix)+ /* "__insmod_" */
3800 lm_name+ /* module name */
3802 strlen(sec->name)+ /* section name */
3804 8+ /* length in dec */
3807 snprintf(name, l, "%s%s_S%s_L%ld",
3808 symprefix, m_name, sec->name,
3809 (long)sec->header.sh_size);
3810 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3811 sec->idx, sec->header.sh_addr, 0);
3813 new_add_ksymtab(f, sym);
3817 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3819 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3820 static void print_load_map(struct obj_file *f)
3822 struct obj_symbol *sym;
3823 struct obj_symbol **all, **p;
3824 struct obj_section *sec;
3825 int i, nsyms, *loaded;
3827 /* Report on the section layout. */
3829 printf("Sections: Size %-*s Align\n",
3830 (int) (2 * sizeof(void *)), "Address");
3832 for (sec = f->load_order; sec; sec = sec->load_next) {
3836 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3841 printf("%-15s %08lx %0*lx 2**%d\n",
3843 (long)sec->header.sh_size,
3844 (int) (2 * sizeof(void *)),
3845 (long)sec->header.sh_addr,
3848 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3849 /* Quick reference which section indicies are loaded. */
3851 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3853 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3855 /* Collect the symbols we'll be listing. */
3857 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3858 for (sym = f->symtab[i]; sym; sym = sym->next)
3859 if (sym->secidx <= SHN_HIRESERVE
3860 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3863 all = alloca(nsyms * sizeof(struct obj_symbol *));
3865 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3866 for (sym = f->symtab[i]; sym; sym = sym->next)
3867 if (sym->secidx <= SHN_HIRESERVE
3868 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3871 /* And list them. */
3872 printf("\nSymbols:\n");
3873 for (p = all; p < all + nsyms; ++p) {
3875 unsigned long value;
3878 if (sym->secidx == SHN_ABS) {
3881 } else if (sym->secidx == SHN_UNDEF) {
3885 sec = f->sections[sym->secidx];
3887 if (sec->header.sh_type == SHT_NOBITS)
3889 else if (sec->header.sh_flags & SHF_ALLOC) {
3890 if (sec->header.sh_flags & SHF_EXECINSTR)
3892 else if (sec->header.sh_flags & SHF_WRITE)
3897 value = sym->value + sec->header.sh_addr;
3900 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3901 type = tolower(type);
3903 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3911 int insmod_main( int argc, char **argv)
3917 unsigned long m_size;
3922 int exit_status = EXIT_FAILURE;
3924 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3925 struct utsname uts_info;
3926 char m_strversion[STRVERSIONLEN];
3927 int m_version, m_crcs;
3929 #ifdef CONFIG_FEATURE_CLEAN_UP
3934 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3935 int flag_print_load_map = 0;
3938 struct utsname myuname;
3940 /* Parse any options */
3941 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3942 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
3944 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
3948 case 'f': /* force loading */
3949 flag_force_load = 1;
3951 case 'k': /* module loaded by kerneld, auto-cleanable */
3954 case 's': /* log to syslog */
3955 /* log to syslog -- not supported */
3956 /* but kernel needs this for request_module(), */
3957 /* as this calls: modprobe -k -s -- <module> */
3958 /* so silently ignore this flag */
3960 case 'v': /* verbose output */
3963 case 'q': /* silent */
3966 case 'x': /* do not export externs */
3969 case 'o': /* name the output module */
3971 m_name = bb_xstrdup(optarg);
3973 case 'L': /* Stub warning */
3974 /* This is needed for compatibility with modprobe.
3975 * In theory, this does locking, but we don't do
3976 * that. So be careful and plan your life around not
3977 * loading the same module 50 times concurrently. */
3979 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3980 case 'm': /* print module load map */
3981 flag_print_load_map = 1;
3989 if (argv[optind] == NULL) {
3993 /* Grab the module name */
3994 tmp1 = bb_xstrdup(argv[optind]);
3995 tmp = basename(tmp1);
3998 if (uname(&myuname) == 0) {
3999 if (myuname.release[0] == '2') {
4000 k_version = myuname.release[2] - '0';
4004 #if defined(CONFIG_FEATURE_2_6_MODULES)
4005 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
4006 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
4012 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4018 #if defined(CONFIG_FEATURE_2_6_MODULES)
4020 m_fullName = bb_xasprintf("%s.ko", tmp);
4023 m_fullName = bb_xasprintf("%s.o", tmp);
4029 tmp1 = 0; /* flag for free(m_name) before exit() */
4032 /* Get a filedesc for the module. Check we we have a complete path */
4033 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
4034 (fp = fopen(argv[optind], "r")) == NULL) {
4035 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4036 * but do not error out yet if we fail to find it... */
4037 if (k_version) { /* uname succeedd */
4040 char real_module_dir[FILENAME_MAX];
4042 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4043 /* Jump through hoops in case /lib/modules/`uname -r`
4044 * is a symlink. We do not want recursive_action to
4045 * follow symlinks, but we do want to follow the
4046 * /lib/modules/`uname -r` dir, So resolve it ourselves
4047 * if it is a link... */
4048 if (realpath (tmdn, real_module_dir) == NULL)
4051 module_dir = real_module_dir;
4052 recursive_action(module_dir, TRUE, FALSE, FALSE,
4053 check_module_name_match, 0, m_fullName);
4057 /* Check if we have found anything yet */
4058 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4060 char module_dir[FILENAME_MAX];
4064 if (realpath (_PATH_MODULES, module_dir) == NULL)
4065 strcpy(module_dir, _PATH_MODULES);
4066 /* No module found under /lib/modules/`uname -r`, this
4067 * time cast the net a bit wider. Search /lib/modules/ */
4068 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4069 check_module_name_match, 0, m_fullName))
4072 || ((fp = fopen(m_filename, "r")) == NULL))
4074 bb_error_msg("%s: no module by that name found", m_fullName);
4078 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4081 m_filename = bb_xstrdup(argv[optind]);
4084 printf("Using %s\n", m_filename);
4086 #ifdef CONFIG_FEATURE_2_6_MODULES
4090 argv[optind + 1] = m_filename;
4091 return insmod_ng_main(argc - optind, argv + optind);
4095 if ((f = obj_load(fp, LOADBITS)) == NULL)
4096 bb_perror_msg_and_die("Could not load the module");
4098 if (get_modinfo_value(f, "kernel_version") == NULL)
4103 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4104 /* Version correspondence? */
4106 if (uname(&uts_info) < 0)
4107 uts_info.release[0] = '\0';
4108 if (m_has_modinfo) {
4109 m_version = new_get_module_version(f, m_strversion);
4110 if (m_version == -1) {
4111 bb_error_msg("couldn't find the kernel version the module was "
4117 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4118 if (flag_force_load) {
4119 bb_error_msg("Warning: kernel-module version mismatch\n"
4120 "\t%s was compiled for kernel version %s\n"
4121 "\twhile this kernel is version %s",
4122 m_filename, m_strversion, uts_info.release);
4124 bb_error_msg("kernel-module version mismatch\n"
4125 "\t%s was compiled for kernel version %s\n"
4126 "\twhile this kernel is version %s.",
4127 m_filename, m_strversion, uts_info.release);
4133 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4135 if (!query_module(NULL, 0, NULL, 0, NULL)) {
4136 if (!new_get_kernel_symbols())
4138 k_crcs = new_is_kernel_checksummed();
4140 bb_error_msg("Not configured to support old kernels");
4144 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4147 m_crcs = new_is_module_checksummed(f);
4149 if (m_crcs != k_crcs)
4150 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4151 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4153 /* Let the module know about the kernel symbols. */
4154 add_kernel_symbols(f);
4156 /* Allocate common symbols, symbol tables, and string tables. */
4158 if (!new_create_this_module(f, m_name))
4163 if (!obj_check_undefineds(f)) {
4166 obj_allocate_commons(f);
4167 check_tainted_module(f, m_name);
4169 /* done with the module name, on to the optional var=value arguments */
4172 if (optind < argc) {
4173 if (!new_process_module_arguments(f, argc - optind, argv + optind))
4180 hide_special_symbols(f);
4182 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4183 add_ksymoops_symbols(f, m_filename, m_name);
4184 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4186 new_create_module_ksymtab(f);
4188 /* Find current size of the module */
4189 m_size = obj_load_size(f);
4192 m_addr = create_module(m_name, m_size);
4193 if (m_addr == -1) switch (errno) {
4195 bb_error_msg("A module named %s already exists", m_name);
4198 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4202 bb_perror_msg("create_module: %s", m_name);
4208 * the PROGBITS section was not loaded by the obj_load
4209 * now we can load them directly into the kernel memory
4211 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4212 delete_module(m_name);
4217 if (!obj_relocate(f, m_addr)) {
4218 delete_module(m_name);
4222 if (!new_init_module(m_name, f, m_size))
4224 delete_module(m_name);
4228 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4229 if(flag_print_load_map)
4233 exit_status = EXIT_SUCCESS;
4236 #ifdef CONFIG_FEATURE_CLEAN_UP
4245 return(exit_status);
4252 #ifdef CONFIG_FEATURE_2_6_MODULES
4254 #include <sys/mman.h>
4255 #include <asm/unistd.h>
4256 #include <sys/syscall.h>
4258 /* We use error numbers in a loose translation... */
4259 static const char *moderror(int err)
4263 return "Invalid module format";
4265 return "Unknown symbol in module";
4267 return "Module has wrong symbol version";
4269 return "Invalid parameters";
4271 return strerror(err);
4275 int insmod_ng_main( int argc, char **argv)
4283 char *filename, *options = bb_xstrdup("");
4291 /* Rest is options */
4292 for (i = 2; i < argc; i++) {
4293 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4294 /* Spaces handled by "" pairs, but no way of escaping quotes */
4295 if (strchr(argv[i], ' ')) {
4296 strcat(options, "\"");
4297 strcat(options, argv[i]);
4298 strcat(options, "\"");
4300 strcat(options, argv[i]);
4302 strcat(options, " ");
4305 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4306 bb_perror_msg_and_die("cannot open module `%s'", filename);
4311 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4312 if (map == MAP_FAILED) {
4313 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4316 ret = syscall(__NR_init_module, map, len, options);
4318 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4319 filename, moderror(errno), ret);