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 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
73 #include <sys/utsname.h>
75 #if !defined(CONFIG_FEATURE_2_4_MODULES) && \
76 !defined(CONFIG_FEATURE_2_6_MODULES)
77 #define CONFIG_FEATURE_2_4_MODULES
80 #if !defined(CONFIG_FEATURE_2_4_MODULES)
81 #define insmod_ng_main insmod_main
84 #if defined(CONFIG_FEATURE_2_6_MODULES)
85 extern int insmod_ng_main( int argc, char **argv);
89 #if defined(CONFIG_FEATURE_2_4_MODULES)
92 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
100 #if defined(__alpha__)
101 #define MATCH_MACHINE(x) (x == EM_ALPHA)
102 #define SHT_RELM SHT_RELA
103 #define Elf64_RelM Elf64_Rela
104 #define ELFCLASSM ELFCLASS64
109 #define MATCH_MACHINE(x) (x == EM_ARM)
110 #define SHT_RELM SHT_REL
111 #define Elf32_RelM Elf32_Rel
112 #define ELFCLASSM ELFCLASS32
113 #define CONFIG_USE_PLT_ENTRIES
114 #define CONFIG_PLT_ENTRY_SIZE 8
115 #define CONFIG_USE_GOT_ENTRIES
116 #define CONFIG_GOT_ENTRY_SIZE 8
117 #define CONFIG_USE_SINGLE
122 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
123 #define SHT_RELM SHT_RELA
124 #define Elf32_RelM Elf32_Rela
125 #define ELFCLASSM ELFCLASS32
129 #if defined(__cris__)
130 #define MATCH_MACHINE(x) (x == EM_CRIS)
131 #define SHT_RELM SHT_RELA
132 #define Elf32_RelM Elf32_Rela
133 #define ELFCLASSM ELFCLASS32
136 #define R_CRIS_NONE 0
142 #if defined(__H8300H__) || defined(__H8300S__)
143 #define MATCH_MACHINE(x) (x == EM_H8_300)
144 #define SHT_RELM SHT_RELA
145 #define Elf32_RelM Elf32_Rela
146 #define ELFCLASSM ELFCLASS32
147 #define CONFIG_USE_SINGLE
148 #define SYMBOL_PREFIX "_"
151 /* PA-RISC / HP-PA */
152 #if defined(__hppa__)
153 #define MATCH_MACHINE(x) (x == EM_PARISC)
154 #define SHT_RELM SHT_RELA
155 #if defined(__LP64__)
156 #define Elf64_RelM Elf64_Rela
157 #define ELFCLASSM ELFCLASS64
159 #define Elf32_RelM Elf32_Rela
160 #define ELFCLASSM ELFCLASS32
165 #if defined(__i386__)
167 #define MATCH_MACHINE(x) (x == EM_386)
169 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
171 #define SHT_RELM SHT_REL
172 #define Elf32_RelM Elf32_Rel
173 #define ELFCLASSM ELFCLASS32
174 #define CONFIG_USE_GOT_ENTRIES
175 #define CONFIG_GOT_ENTRY_SIZE 4
176 #define CONFIG_USE_SINGLE
179 /* IA64, aka Itanium */
180 #if defined(__ia64__)
181 #define MATCH_MACHINE(x) (x == EM_IA_64)
182 #define SHT_RELM SHT_RELA
183 #define Elf64_RelM Elf64_Rela
184 #define ELFCLASSM ELFCLASS64
188 #if defined(__mc68000__)
189 #define MATCH_MACHINE(x) (x == EM_68K)
190 #define SHT_RELM SHT_RELA
191 #define Elf32_RelM Elf32_Rela
192 #define ELFCLASSM ELFCLASS32
193 #define CONFIG_USE_GOT_ENTRIES
194 #define CONFIG_GOT_ENTRY_SIZE 4
195 #define CONFIG_USE_SINGLE
199 #if defined(__microblaze__)
200 #define CONFIG_USE_SINGLE
201 #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
202 #define SHT_RELM SHT_RELA
203 #define Elf32_RelM Elf32_Rela
204 #define ELFCLASSM ELFCLASS32
208 #if defined(__mips__)
209 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
210 #define SHT_RELM SHT_REL
211 #define Elf32_RelM Elf32_Rel
212 #define ELFCLASSM ELFCLASS32
213 /* Account for ELF spec changes. */
214 #ifndef EM_MIPS_RS3_LE
215 #ifdef EM_MIPS_RS4_BE
216 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
218 #define EM_MIPS_RS3_LE 10
220 #endif /* !EM_MIPS_RS3_LE */
221 #define ARCHDATAM "__dbe_table"
225 #if defined(__nios2__)
226 #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
227 #define SHT_RELM SHT_RELA
228 #define Elf32_RelM Elf32_Rela
229 #define ELFCLASSM ELFCLASS32
233 #if defined(__powerpc64__)
234 #define MATCH_MACHINE(x) (x == EM_PPC64)
235 #define SHT_RELM SHT_RELA
236 #define Elf64_RelM Elf64_Rela
237 #define ELFCLASSM ELFCLASS64
238 #elif defined(__powerpc__)
239 #define MATCH_MACHINE(x) (x == EM_PPC)
240 #define SHT_RELM SHT_RELA
241 #define Elf32_RelM Elf32_Rela
242 #define ELFCLASSM ELFCLASS32
243 #define CONFIG_USE_PLT_ENTRIES
244 #define CONFIG_PLT_ENTRY_SIZE 16
245 #define CONFIG_USE_PLT_LIST
246 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
247 #define CONFIG_USE_LIST
248 #define ARCHDATAM "__ftr_fixup"
252 #if defined(__s390__)
253 #define MATCH_MACHINE(x) (x == EM_S390)
254 #define SHT_RELM SHT_RELA
255 #define Elf32_RelM Elf32_Rela
256 #define ELFCLASSM ELFCLASS32
257 #define CONFIG_USE_PLT_ENTRIES
258 #define CONFIG_PLT_ENTRY_SIZE 8
259 #define CONFIG_USE_GOT_ENTRIES
260 #define CONFIG_GOT_ENTRY_SIZE 8
261 #define CONFIG_USE_SINGLE
266 #define MATCH_MACHINE(x) (x == EM_SH)
267 #define SHT_RELM SHT_RELA
268 #define Elf32_RelM Elf32_Rela
269 #define ELFCLASSM ELFCLASS32
270 #define CONFIG_USE_GOT_ENTRIES
271 #define CONFIG_GOT_ENTRY_SIZE 4
272 #define CONFIG_USE_SINGLE
273 /* the SH changes have only been tested in =little endian= mode */
274 /* I'm not sure about big endian, so let's warn: */
275 #if defined(__sh__) && BB_BIG_ENDIAN
276 # error insmod.c may require changes for use on big endian SH
278 /* it may or may not work on the SH1/SH2... Error on those also */
279 #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
280 #error insmod.c may require changes for SH1 or SH2 use
285 #if defined(__sparc__)
286 #define MATCH_MACHINE(x) (x == EM_SPARC)
287 #define SHT_RELM SHT_RELA
288 #define Elf32_RelM Elf32_Rela
289 #define ELFCLASSM ELFCLASS32
293 #if defined (__v850e__)
294 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
295 #define SHT_RELM SHT_RELA
296 #define Elf32_RelM Elf32_Rela
297 #define ELFCLASSM ELFCLASS32
298 #define CONFIG_USE_PLT_ENTRIES
299 #define CONFIG_PLT_ENTRY_SIZE 8
300 #define CONFIG_USE_SINGLE
301 #ifndef EM_CYGNUS_V850 /* grumble */
302 #define EM_CYGNUS_V850 0x9080
304 #define SYMBOL_PREFIX "_"
308 #if defined(__x86_64__)
309 #define MATCH_MACHINE(x) (x == EM_X86_64)
310 #define SHT_RELM SHT_RELA
311 #define CONFIG_USE_GOT_ENTRIES
312 #define CONFIG_GOT_ENTRY_SIZE 8
313 #define CONFIG_USE_SINGLE
314 #define Elf64_RelM Elf64_Rela
315 #define ELFCLASSM ELFCLASS64
319 #error Sorry, but insmod.c does not yet support this architecture...
323 //----------------------------------------------------------------------------
324 //--------modutils module.h, lines 45-242
325 //----------------------------------------------------------------------------
327 /* Definitions for the Linux module syscall interface.
328 Copyright 1996, 1997 Linux International.
330 Contributed by Richard Henderson <rth@tamu.edu>
332 This file is part of the Linux modutils.
334 This program is free software; you can redistribute it and/or modify it
335 under the terms of the GNU General Public License as published by the
336 Free Software Foundation; either version 2 of the License, or (at your
337 option) any later version.
339 This program is distributed in the hope that it will be useful, but
340 WITHOUT ANY WARRANTY; without even the implied warranty of
341 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
342 General Public License for more details.
344 You should have received a copy of the GNU General Public License
345 along with this program; if not, write to the Free Software Foundation,
346 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
349 #ifndef MODUTILS_MODULE_H
350 /* Why? static const int MODUTILS_MODULE_H = 1;*/
352 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
354 /*======================================================================*/
355 /* For sizeof() which are related to the module platform and not to the
356 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
358 #define tgt_sizeof_char sizeof(char)
359 #define tgt_sizeof_short sizeof(short)
360 #define tgt_sizeof_int sizeof(int)
361 #define tgt_sizeof_long sizeof(long)
362 #define tgt_sizeof_char_p sizeof(char *)
363 #define tgt_sizeof_void_p sizeof(void *)
364 #define tgt_long long
366 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
367 #undef tgt_sizeof_long
368 #undef tgt_sizeof_char_p
369 #undef tgt_sizeof_void_p
373 tgt_sizeof_char_p = 8,
374 tgt_sizeof_void_p = 8
376 #define tgt_long long long
379 /*======================================================================*/
380 /* The structures used in Linux 2.1. */
382 /* Note: new_module_symbol does not use tgt_long intentionally */
383 struct new_module_symbol
389 struct new_module_persist;
391 struct new_module_ref
393 unsigned tgt_long dep; /* kernel addresses */
394 unsigned tgt_long ref;
395 unsigned tgt_long next_ref;
400 unsigned tgt_long size_of_struct; /* == sizeof(module) */
401 unsigned tgt_long next;
402 unsigned tgt_long name;
403 unsigned tgt_long size;
406 unsigned tgt_long flags; /* AUTOCLEAN et al */
411 unsigned tgt_long syms;
412 unsigned tgt_long deps;
413 unsigned tgt_long refs;
414 unsigned tgt_long init;
415 unsigned tgt_long cleanup;
416 unsigned tgt_long ex_table_start;
417 unsigned tgt_long ex_table_end;
419 unsigned tgt_long gp;
421 /* Everything after here is extension. */
422 unsigned tgt_long persist_start;
423 unsigned tgt_long persist_end;
424 unsigned tgt_long can_unload;
425 unsigned tgt_long runsize;
426 const char *kallsyms_start; /* All symbols for kernel debugging */
427 const char *kallsyms_end;
428 const char *archdata_start; /* arch specific data for module */
429 const char *archdata_end;
430 const char *kernel_data; /* Reserved for kernel internal use */
434 #define ARCHDATA_SEC_NAME ARCHDATAM
436 #define ARCHDATA_SEC_NAME "__archdata"
438 #define KALLSYMS_SEC_NAME "__kallsyms"
441 struct new_module_info
449 /* Bits of module.flags. */
453 NEW_MOD_AUTOCLEAN = 4,
455 NEW_MOD_USED_ONCE = 16
458 int init_module(const char *name, const struct new_module *);
459 int query_module(const char *name, int which, void *buf,
460 size_t bufsize, size_t *ret);
462 /* Values for query_module's which. */
471 /*======================================================================*/
472 /* The system calls unchanged between 2.0 and 2.1. */
474 unsigned long create_module(const char *, size_t);
475 int delete_module(const char *);
478 #endif /* module.h */
480 //----------------------------------------------------------------------------
481 //--------end of modutils module.h
482 //----------------------------------------------------------------------------
486 //----------------------------------------------------------------------------
487 //--------modutils obj.h, lines 253-462
488 //----------------------------------------------------------------------------
490 /* Elf object file loading and relocation routines.
491 Copyright 1996, 1997 Linux International.
493 Contributed by Richard Henderson <rth@tamu.edu>
495 This file is part of the Linux modutils.
497 This program is free software; you can redistribute it and/or modify it
498 under the terms of the GNU General Public License as published by the
499 Free Software Foundation; either version 2 of the License, or (at your
500 option) any later version.
502 This program is distributed in the hope that it will be useful, but
503 WITHOUT ANY WARRANTY; without even the implied warranty of
504 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
505 General Public License for more details.
507 You should have received a copy of the GNU General Public License
508 along with this program; if not, write to the Free Software Foundation,
509 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
512 #ifndef MODUTILS_OBJ_H
513 /* Why? static const int MODUTILS_OBJ_H = 1; */
515 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
517 /* The relocatable object is manipulated using elfin types. */
524 # if ELFCLASSM == ELFCLASS32
525 # define ElfW(x) Elf32_ ## x
526 # define ELFW(x) ELF32_ ## x
528 # define ElfW(x) Elf64_ ## x
529 # define ELFW(x) ELF64_ ## x
533 /* For some reason this is missing from some ancient C libraries.... */
534 #ifndef ELF32_ST_INFO
535 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
538 #ifndef ELF64_ST_INFO
539 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
542 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
543 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
544 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
545 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
546 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
548 struct obj_string_patch;
549 struct obj_symbol_patch;
556 struct obj_section *load_next;
562 struct obj_symbol *next; /* hash table link */
566 int secidx; /* the defining section index/module */
568 int ksymidx; /* for export to the kernel symtab */
569 int referenced; /* actually used in the link */
572 /* Hardcode the hash table size. We shouldn't be needing so many
573 symbols that we begin to degrade performance, and we get a big win
574 by giving the compiler a constant divisor. */
576 #define HASH_BUCKETS 521
582 struct obj_section **sections;
583 struct obj_section *load_order;
584 struct obj_section **load_order_search_start;
585 struct obj_string_patch *string_patches;
586 struct obj_symbol_patch *symbol_patches;
587 int (*symbol_cmp)(const char *, const char *);
588 unsigned long (*symbol_hash)(const char *);
589 unsigned long local_symtab_size;
590 struct obj_symbol **local_symtab;
591 struct obj_symbol *symtab[HASH_BUCKETS];
602 struct obj_string_patch
604 struct obj_string_patch *next;
606 ElfW(Addr) reloc_offset;
607 ElfW(Addr) string_offset;
610 struct obj_symbol_patch
612 struct obj_symbol_patch *next;
614 ElfW(Addr) reloc_offset;
615 struct obj_symbol *sym;
619 /* Generic object manipulation routines. */
621 static unsigned long obj_elf_hash(const char *);
623 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
625 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
628 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
629 struct obj_symbol *sym);
631 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
632 static void obj_set_symbol_compare(struct obj_file *f,
633 int (*cmp)(const char *, const char *),
634 unsigned long (*hash)(const char *));
637 static struct obj_section *obj_find_section (struct obj_file *f,
640 static void obj_insert_section_load_order (struct obj_file *f,
641 struct obj_section *sec);
643 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
648 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
653 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
655 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
658 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
659 struct obj_symbol *sym);
661 static int obj_check_undefineds(struct obj_file *f);
663 static void obj_allocate_commons(struct obj_file *f);
665 static unsigned long obj_load_size (struct obj_file *f);
667 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
669 static struct obj_file *obj_load(FILE *f, int loadprogbits);
671 static int obj_create_image (struct obj_file *f, char *image);
673 /* Architecture specific manipulation routines. */
675 static struct obj_file *arch_new_file (void);
677 static struct obj_section *arch_new_section (void);
679 static struct obj_symbol *arch_new_symbol (void);
681 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
682 struct obj_section *targsec,
683 struct obj_section *symsec,
684 struct obj_symbol *sym,
685 ElfW(RelM) *rel, ElfW(Addr) value);
687 static void arch_create_got (struct obj_file *f);
688 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
689 static int obj_gpl_license(struct obj_file *f, const char **license);
690 #endif /* ENABLE_FEATURE_CHECK_TAINTED_MODULE */
692 //----------------------------------------------------------------------------
693 //--------end of modutils obj.h
694 //----------------------------------------------------------------------------
697 /* SPFX is always a string, so it can be concatenated to string constants. */
699 #define SPFX SYMBOL_PREFIX
705 #define _PATH_MODULES "/lib/modules"
706 enum { STRVERSIONLEN = 32 };
708 /*======================================================================*/
710 static int flag_force_load = 0;
711 static int flag_autoclean = 0;
712 static int flag_verbose = 0;
713 static int flag_quiet = 0;
714 static int flag_export = 1;
717 /*======================================================================*/
719 #if defined(CONFIG_USE_LIST)
721 struct arch_list_entry
723 struct arch_list_entry *next;
724 CONFIG_LIST_ARCHTYPE addend;
731 #if defined(CONFIG_USE_SINGLE)
733 struct arch_single_entry
742 #if defined(__mips__)
745 struct mips_hi16 *next;
752 struct obj_file root;
753 #if defined(CONFIG_USE_PLT_ENTRIES)
754 struct obj_section *plt;
756 #if defined(CONFIG_USE_GOT_ENTRIES)
757 struct obj_section *got;
759 #if defined(__mips__)
760 struct mips_hi16 *mips_hi16_list;
765 struct obj_symbol root;
766 #if defined(CONFIG_USE_PLT_ENTRIES)
767 #if defined(CONFIG_USE_PLT_LIST)
768 struct arch_list_entry *pltent;
770 struct arch_single_entry pltent;
773 #if defined(CONFIG_USE_GOT_ENTRIES)
774 struct arch_single_entry gotent;
779 struct external_module {
784 struct new_module_symbol *syms;
787 static struct new_module_symbol *ksyms;
788 static size_t nksyms;
790 static struct external_module *ext_modules;
791 static int n_ext_modules;
792 static int n_ext_modules_used;
793 extern int delete_module(const char *);
795 static char *m_filename;
796 static char *m_fullName;
800 /*======================================================================*/
803 static int check_module_name_match(const char *filename, struct stat *statbuf,
806 char *fullname = (char *) userdata;
808 if (fullname[0] == '\0')
811 char *tmp, *tmp1 = bb_xstrdup(filename);
812 tmp = bb_get_last_path_component(tmp1);
813 if (strcmp(tmp, fullname) == 0) {
815 /* Stop searching if we find a match */
816 m_filename = bb_xstrdup(filename);
825 /*======================================================================*/
827 static struct obj_file *arch_new_file(void)
830 f = xmalloc(sizeof(*f));
832 memset(f, 0, sizeof(*f));
837 static struct obj_section *arch_new_section(void)
839 return xmalloc(sizeof(struct obj_section));
842 static struct obj_symbol *arch_new_symbol(void)
844 struct arch_symbol *sym;
845 sym = xmalloc(sizeof(*sym));
847 memset(sym, 0, sizeof(*sym));
852 static enum obj_reloc
853 arch_apply_relocation(struct obj_file *f,
854 struct obj_section *targsec,
855 struct obj_section *symsec,
856 struct obj_symbol *sym,
857 ElfW(RelM) *rel, ElfW(Addr) v)
859 struct arch_file *ifile = (struct arch_file *) f;
860 enum obj_reloc ret = obj_reloc_ok;
861 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
862 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
863 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
864 struct arch_symbol *isym = (struct arch_symbol *) sym;
866 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
867 #if defined(CONFIG_USE_GOT_ENTRIES)
868 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
871 #if defined(CONFIG_USE_PLT_ENTRIES)
872 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
874 # if defined(CONFIG_USE_PLT_LIST)
875 struct arch_list_entry *pe;
877 struct arch_single_entry *pe;
881 switch (ELF_R_TYPE(rel->r_info)) {
896 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
897 * (which is .got) similar to branch,
898 * but is full 32 bits relative */
908 case R_ARM_GOTOFF: /* address relative to the got */
913 #elif defined(__cris__)
919 /* CRIS keeps the relocation value in the r_addend field and
920 * should not use whats in *loc at all
925 #elif defined(__H8300H__) || defined(__H8300S__)
928 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
929 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
940 if ((ElfW(Sword))v > 0x7fff ||
941 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
942 ret = obj_reloc_overflow;
944 *(unsigned short *)loc = v;
948 if ((ElfW(Sword))v > 0x7f ||
949 (ElfW(Sword))v < -(ElfW(Sword))0x80)
950 ret = obj_reloc_overflow;
952 *(unsigned char *)loc = v;
955 #elif defined(__i386__)
991 #elif defined (__microblaze__)
992 case R_MICROBLAZE_NONE:
993 case R_MICROBLAZE_64_NONE:
994 case R_MICROBLAZE_32_SYM_OP_SYM:
995 case R_MICROBLAZE_32_PCREL:
998 case R_MICROBLAZE_64_PCREL: {
999 /* dot is the address of the current instruction.
1000 * v is the target symbol address.
1001 * So we need to extract the offset in the code,
1002 * adding v, then subtrating the current address
1003 * of this instruction.
1004 * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
1007 /* Get split offset stored in code */
1008 unsigned int temp = (loc[0] & 0xFFFF) << 16 |
1011 /* Adjust relative offset. -4 adjustment required
1012 * because dot points to the IMM insn, but branch
1013 * is computed relative to the branch instruction itself.
1015 temp += v - dot - 4;
1017 /* Store back into code */
1018 loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
1019 loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
1024 case R_MICROBLAZE_32:
1028 case R_MICROBLAZE_64: {
1029 /* Get split pointer stored in code */
1030 unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
1033 /* Add reloc offset */
1036 /* Store back into code */
1037 loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
1038 loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
1043 case R_MICROBLAZE_32_PCREL_LO:
1044 case R_MICROBLAZE_32_LO:
1045 case R_MICROBLAZE_SRO32:
1046 case R_MICROBLAZE_SRW32:
1047 ret = obj_reloc_unhandled;
1050 #elif defined(__mc68000__)
1061 ret = obj_reloc_overflow;
1068 ret = obj_reloc_overflow;
1075 if ((ElfW(Sword))v > 0x7f ||
1076 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1077 ret = obj_reloc_overflow;
1084 if ((ElfW(Sword))v > 0x7fff ||
1085 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1086 ret = obj_reloc_overflow;
1092 *(int *)loc = v - dot;
1095 case R_68K_GLOB_DAT:
1096 case R_68K_JMP_SLOT:
1100 case R_68K_RELATIVE:
1101 *(int *)loc += f->baseaddr;
1107 # ifdef R_68K_GOTOFF
1114 #elif defined(__mips__)
1125 ret = obj_reloc_dangerous;
1126 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1127 ret = obj_reloc_overflow;
1129 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1135 struct mips_hi16 *n;
1137 /* We cannot relocate this one now because we don't know the value
1138 of the carry we need to add. Save the information, and let LO16
1139 do the actual relocation. */
1140 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1143 n->next = ifile->mips_hi16_list;
1144 ifile->mips_hi16_list = n;
1150 unsigned long insnlo = *loc;
1151 ElfW(Addr) val, vallo;
1153 /* Sign extend the addend we extract from the lo insn. */
1154 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1156 if (ifile->mips_hi16_list != NULL) {
1157 struct mips_hi16 *l;
1159 l = ifile->mips_hi16_list;
1161 struct mips_hi16 *next;
1164 /* The value for the HI16 had best be the same. */
1165 assert(v == l->value);
1167 /* Do the HI16 relocation. Note that we actually don't
1168 need to know anything about the LO16 itself, except where
1169 to find the low 16 bits of the addend needed by the LO16. */
1172 ((insn & 0xffff) << 16) +
1176 /* Account for the sign extension that will happen in the
1183 insn = (insn & ~0xffff) | val;
1191 ifile->mips_hi16_list = NULL;
1194 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1196 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1201 #elif defined(__nios2__)
1206 case R_NIOS2_BFD_RELOC_32:
1210 case R_NIOS2_BFD_RELOC_16:
1212 ret = obj_reloc_overflow;
1217 case R_NIOS2_BFD_RELOC_8:
1219 ret = obj_reloc_overflow;
1228 if ((Elf32_Sword)v > 0x7fff ||
1229 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1230 ret = obj_reloc_overflow;
1234 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1244 ret = obj_reloc_overflow;
1248 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1253 case R_NIOS2_PCREL16:
1258 if ((Elf32_Sword)v > 0x7fff ||
1259 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1260 ret = obj_reloc_overflow;
1264 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1270 Elf32_Addr word, gp;
1272 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1274 if ((Elf32_Sword)v > 0x7fff ||
1275 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1276 ret = obj_reloc_overflow;
1280 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1284 case R_NIOS2_CALL26:
1286 ret = obj_reloc_dangerous;
1287 if ((v >> 28) != (dot >> 28))
1288 ret = obj_reloc_overflow;
1289 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1297 ret = obj_reloc_overflow;
1300 word = *loc & ~0x7c0;
1301 *loc = word | ((v & 0x1f) << 6);
1310 ret = obj_reloc_overflow;
1313 word = *loc & ~0xfc0;
1314 *loc = word | ((v & 0x3f) << 6);
1323 ret = obj_reloc_overflow;
1326 word = *loc & ~0x3fc0;
1327 *loc = word | ((v & 0xff) << 6);
1336 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1346 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1351 case R_NIOS2_HIADJ16:
1353 Elf32_Addr word1, word2;
1356 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1357 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1362 #elif defined(__powerpc64__)
1363 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1365 #elif defined(__powerpc__)
1367 case R_PPC_ADDR16_HA:
1368 *(unsigned short *)loc = (v + 0x8000) >> 16;
1371 case R_PPC_ADDR16_HI:
1372 *(unsigned short *)loc = v >> 16;
1375 case R_PPC_ADDR16_LO:
1376 *(unsigned short *)loc = v;
1390 #elif defined(__s390__)
1393 *(unsigned int *) loc += v;
1396 *(unsigned short *) loc += v;
1399 *(unsigned char *) loc += v;
1403 *(unsigned int *) loc += v - dot;
1406 *(unsigned short *) loc += (v - dot) >> 1;
1409 *(unsigned short *) loc += v - dot;
1413 case R_390_PLT16DBL:
1414 /* find the plt entry and initialize it. */
1415 assert(isym != NULL);
1416 pe = (struct arch_single_entry *) &isym->pltent;
1417 assert(pe->allocated);
1418 if (pe->inited == 0) {
1419 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1420 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1422 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1429 /* Insert relative distance to target. */
1430 v = plt + pe->offset - dot;
1431 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1432 *(unsigned int *) loc = (unsigned int) v;
1433 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1434 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1437 case R_390_GLOB_DAT:
1438 case R_390_JMP_SLOT:
1442 case R_390_RELATIVE:
1443 *loc += f->baseaddr;
1448 *(unsigned long *) loc += got - dot;
1454 assert(isym != NULL);
1456 if (!isym->gotent.inited)
1458 isym->gotent.inited = 1;
1459 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1461 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1462 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1463 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1464 *(unsigned short *) loc += isym->gotent.offset;
1465 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1466 *(unsigned int *) loc += isym->gotent.offset;
1469 # ifndef R_390_GOTOFF32
1470 # define R_390_GOTOFF32 R_390_GOTOFF
1472 case R_390_GOTOFF32:
1477 #elif defined(__sh__)
1500 *loc = f->baseaddr + rel->r_addend;
1505 *loc = got - dot + rel->r_addend;
1516 # if defined(__SH5__)
1517 case R_SH_IMM_MEDLOW16:
1518 case R_SH_IMM_LOW16:
1522 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1526 * movi and shori have the format:
1528 * | op | imm | reg | reserved |
1529 * 31..26 25..10 9.. 4 3 .. 0
1531 * so we simply mask and or in imm.
1533 word = *loc & ~0x3fffc00;
1534 word |= (v & 0xffff) << 10;
1541 case R_SH_IMM_MEDLOW16_PCREL:
1542 case R_SH_IMM_LOW16_PCREL:
1546 word = *loc & ~0x3fffc00;
1550 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1553 word |= (v & 0xffff) << 10;
1559 # endif /* __SH5__ */
1561 #elif defined (__v850e__)
1567 /* We write two shorts instead of a long because even
1568 32-bit insns only need half-word alignment, but
1569 32-bit data needs to be long-word aligned. */
1570 v += ((unsigned short *)loc)[0];
1571 v += ((unsigned short *)loc)[1] << 16;
1572 ((unsigned short *)loc)[0] = v & 0xffff;
1573 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1576 case R_V850_22_PCREL:
1579 #elif defined(__x86_64__)
1589 *(unsigned int *) loc += v;
1592 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1593 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1598 *(signed int *) loc += v;
1602 *(unsigned short *) loc += v;
1606 *(unsigned char *) loc += v;
1610 *(unsigned int *) loc += v - dot;
1614 *(unsigned short *) loc += v - dot;
1618 *(unsigned char *) loc += v - dot;
1621 case R_X86_64_GLOB_DAT:
1622 case R_X86_64_JUMP_SLOT:
1626 case R_X86_64_RELATIVE:
1627 *loc += f->baseaddr;
1630 case R_X86_64_GOT32:
1631 case R_X86_64_GOTPCREL:
1634 assert(isym != NULL);
1635 if (!isym->gotent.reloc_done)
1637 isym->gotent.reloc_done = 1;
1638 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1640 /* XXX are these really correct? */
1641 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1642 *(unsigned int *) loc += v + isym->gotent.offset;
1644 *loc += isym->gotent.offset;
1649 # warning "no idea how to handle relocations on your arch"
1653 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1654 ret = obj_reloc_unhandled;
1657 #if defined(CONFIG_USE_PLT_ENTRIES)
1661 /* find the plt entry and initialize it if necessary */
1662 assert(isym != NULL);
1664 #if defined(CONFIG_USE_PLT_LIST)
1665 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1673 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1675 /* generate some machine code */
1677 #if defined(__arm__)
1678 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1679 ip[1] = v; /* sym@ */
1681 #if defined(__powerpc__)
1682 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1683 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1684 ip[2] = 0x7d6903a6; /* mtctr r11 */
1685 ip[3] = 0x4e800420; /* bctr */
1687 #if defined (__v850e__)
1688 /* We have to trash a register, so we assume that any control
1689 transfer more than 21-bits away must be a function call
1690 (so we can use a call-clobbered register). */
1691 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1692 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1697 /* relative distance to target */
1699 /* if the target is too far away.... */
1700 #if defined (__arm__) || defined (__powerpc__)
1701 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1702 #elif defined (__v850e__)
1703 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1705 /* go via the plt */
1706 v = plt + pe->offset - dot;
1708 #if defined (__v850e__)
1713 ret = obj_reloc_dangerous;
1715 /* merge the offset into the instruction. */
1716 #if defined(__arm__)
1717 /* Convert to words. */
1720 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1722 #if defined(__powerpc__)
1723 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1725 #if defined (__v850e__)
1726 /* We write two shorts instead of a long because even 32-bit insns
1727 only need half-word alignment, but the 32-bit data write needs
1728 to be long-word aligned. */
1729 ((unsigned short *)loc)[0] =
1730 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1731 | ((v >> 16) & 0x3f); /* offs high part */
1732 ((unsigned short *)loc)[1] =
1733 (v & 0xffff); /* offs low part */
1736 #endif /* CONFIG_USE_PLT_ENTRIES */
1738 #if defined(CONFIG_USE_GOT_ENTRIES)
1741 assert(isym != NULL);
1742 /* needs an entry in the .got: set it, once */
1743 if (!isym->gotent.inited) {
1744 isym->gotent.inited = 1;
1745 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1747 /* make the reloc with_respect_to_.got */
1749 *loc += isym->gotent.offset + rel->r_addend;
1750 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1751 *loc += isym->gotent.offset;
1755 #endif /* CONFIG_USE_GOT_ENTRIES */
1762 #if defined(CONFIG_USE_LIST)
1764 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1765 int offset, int size)
1767 struct arch_list_entry *pe;
1769 for (pe = *list; pe != NULL; pe = pe->next) {
1770 if (pe->addend == rel->r_addend) {
1776 pe = xmalloc(sizeof(struct arch_list_entry));
1778 pe->addend = rel->r_addend;
1779 pe->offset = offset;
1789 #if defined(CONFIG_USE_SINGLE)
1791 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1792 int offset, int size)
1794 if (single->allocated == 0) {
1795 single->allocated = 1;
1796 single->offset = offset;
1805 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1807 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1808 int offset, int size)
1810 struct obj_section *myrelsec = obj_find_section(f, name);
1817 obj_extend_section(myrelsec, offset);
1819 myrelsec = obj_create_alloced_section(f, name,
1829 static void arch_create_got(struct obj_file *f)
1831 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1832 struct arch_file *ifile = (struct arch_file *) f;
1834 #if defined(CONFIG_USE_GOT_ENTRIES)
1835 int got_offset = 0, got_needed = 0, got_allocate;
1837 #if defined(CONFIG_USE_PLT_ENTRIES)
1838 int plt_offset = 0, plt_needed = 0, plt_allocate;
1840 struct obj_section *relsec, *symsec, *strsec;
1841 ElfW(RelM) *rel, *relend;
1842 ElfW(Sym) *symtab, *extsym;
1843 const char *strtab, *name;
1844 struct arch_symbol *intsym;
1846 for (i = 0; i < f->header.e_shnum; ++i) {
1847 relsec = f->sections[i];
1848 if (relsec->header.sh_type != SHT_RELM)
1851 symsec = f->sections[relsec->header.sh_link];
1852 strsec = f->sections[symsec->header.sh_link];
1854 rel = (ElfW(RelM) *) relsec->contents;
1855 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1856 symtab = (ElfW(Sym) *) symsec->contents;
1857 strtab = (const char *) strsec->contents;
1859 for (; rel < relend; ++rel) {
1860 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1862 #if defined(CONFIG_USE_GOT_ENTRIES)
1865 #if defined(CONFIG_USE_PLT_ENTRIES)
1869 switch (ELF_R_TYPE(rel->r_info)) {
1870 #if defined(__arm__)
1885 #elif defined(__i386__)
1895 #elif defined(__powerpc__)
1900 #elif defined(__mc68000__)
1911 #elif defined(__sh__)
1921 #elif defined (__v850e__)
1922 case R_V850_22_PCREL:
1931 if (extsym->st_name != 0) {
1932 name = strtab + extsym->st_name;
1934 name = f->sections[extsym->st_shndx]->name;
1936 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1937 #if defined(CONFIG_USE_GOT_ENTRIES)
1939 got_offset += arch_single_init(
1940 rel, &intsym->gotent,
1941 got_offset, CONFIG_GOT_ENTRY_SIZE);
1946 #if defined(CONFIG_USE_PLT_ENTRIES)
1948 #if defined(CONFIG_USE_PLT_LIST)
1949 plt_offset += arch_list_add(
1950 rel, &intsym->pltent,
1951 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1953 plt_offset += arch_single_init(
1954 rel, &intsym->pltent,
1955 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1963 #if defined(CONFIG_USE_GOT_ENTRIES)
1965 ifile->got = arch_xsect_init(f, ".got", got_offset,
1966 CONFIG_GOT_ENTRY_SIZE);
1970 #if defined(CONFIG_USE_PLT_ENTRIES)
1972 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1973 CONFIG_PLT_ENTRY_SIZE);
1977 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1980 /*======================================================================*/
1982 /* Standard ELF hash function. */
1983 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1985 unsigned long h = 0;
1992 if ((g = (h & 0xf0000000)) != 0) {
2001 static unsigned long obj_elf_hash(const char *name)
2003 return obj_elf_hash_n(name, strlen(name));
2006 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2007 /* String comparison for non-co-versioned kernel and module. */
2009 static int ncv_strcmp(const char *a, const char *b)
2011 size_t alen = strlen(a), blen = strlen(b);
2013 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
2014 return strncmp(a, b, alen);
2015 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
2016 return strncmp(a, b, blen);
2018 return strcmp(a, b);
2021 /* String hashing for non-co-versioned kernel and module. Here
2022 we are simply forced to drop the crc from the hash. */
2024 static unsigned long ncv_symbol_hash(const char *str)
2026 size_t len = strlen(str);
2027 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
2029 return obj_elf_hash_n(str, len);
2033 obj_set_symbol_compare(struct obj_file *f,
2034 int (*cmp) (const char *, const char *),
2035 unsigned long (*hash) (const char *))
2038 f->symbol_cmp = cmp;
2040 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
2043 f->symbol_hash = hash;
2045 memcpy(tmptab, f->symtab, sizeof(tmptab));
2046 memset(f->symtab, 0, sizeof(f->symtab));
2048 for (i = 0; i < HASH_BUCKETS; ++i)
2049 for (sym = tmptab[i]; sym; sym = next) {
2050 unsigned long h = hash(sym->name) % HASH_BUCKETS;
2052 sym->next = f->symtab[h];
2058 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2060 static struct obj_symbol *
2061 obj_add_symbol(struct obj_file *f, const char *name,
2062 unsigned long symidx, int info,
2063 int secidx, ElfW(Addr) value,
2066 struct obj_symbol *sym;
2067 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2068 int n_type = ELF_ST_TYPE(info);
2069 int n_binding = ELF_ST_BIND(info);
2071 for (sym = f->symtab[hash]; sym; sym = sym->next)
2072 if (f->symbol_cmp(sym->name, name) == 0) {
2073 int o_secidx = sym->secidx;
2074 int o_info = sym->info;
2075 int o_type = ELF_ST_TYPE(o_info);
2076 int o_binding = ELF_ST_BIND(o_info);
2078 /* A redefinition! Is it legal? */
2080 if (secidx == SHN_UNDEF)
2082 else if (o_secidx == SHN_UNDEF)
2084 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2085 /* Cope with local and global symbols of the same name
2086 in the same object file, as might have been created
2087 by ld -r. The only reason locals are now seen at this
2088 level at all is so that we can do semi-sensible things
2091 struct obj_symbol *nsym, **p;
2093 nsym = arch_new_symbol();
2094 nsym->next = sym->next;
2097 /* Excise the old (local) symbol from the hash chain. */
2098 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2102 } else if (n_binding == STB_LOCAL) {
2103 /* Another symbol of the same name has already been defined.
2104 Just add this to the local table. */
2105 sym = arch_new_symbol();
2108 f->local_symtab[symidx] = sym;
2110 } else if (n_binding == STB_WEAK)
2112 else if (o_binding == STB_WEAK)
2114 /* Don't unify COMMON symbols with object types the programmer
2116 else if (secidx == SHN_COMMON
2117 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2119 else if (o_secidx == SHN_COMMON
2120 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2123 /* Don't report an error if the symbol is coming from
2124 the kernel or some external module. */
2125 if (secidx <= SHN_HIRESERVE)
2126 bb_error_msg("%s multiply defined", name);
2131 /* Completely new symbol. */
2132 sym = arch_new_symbol();
2133 sym->next = f->symtab[hash];
2134 f->symtab[hash] = sym;
2137 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2138 if (symidx >= f->local_symtab_size)
2139 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2140 name, (long) symidx, (long) f->local_symtab_size);
2142 f->local_symtab[symidx] = sym;
2149 sym->secidx = secidx;
2155 static struct obj_symbol *
2156 obj_find_symbol(struct obj_file *f, const char *name)
2158 struct obj_symbol *sym;
2159 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2161 for (sym = f->symtab[hash]; sym; sym = sym->next)
2162 if (f->symbol_cmp(sym->name, name) == 0)
2169 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2172 if (sym->secidx >= SHN_LORESERVE)
2175 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2177 /* As a special case, a NULL sym has value zero. */
2182 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2184 int i, n = f->header.e_shnum;
2186 for (i = 0; i < n; ++i)
2187 if (strcmp(f->sections[i]->name, name) == 0)
2188 return f->sections[i];
2193 static int obj_load_order_prio(struct obj_section *a)
2195 unsigned long af, ac;
2197 af = a->header.sh_flags;
2200 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2201 strcmp(a->name + 5, ".init"))
2205 if (!(af & SHF_WRITE))
2207 if (af & SHF_EXECINSTR)
2209 if (a->header.sh_type != SHT_NOBITS)
2216 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2218 struct obj_section **p;
2219 int prio = obj_load_order_prio(sec);
2220 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2221 if (obj_load_order_prio(*p) < prio)
2223 sec->load_next = *p;
2227 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2229 unsigned long align,
2232 int newidx = f->header.e_shnum++;
2233 struct obj_section *sec;
2235 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2236 f->sections[newidx] = sec = arch_new_section();
2238 memset(sec, 0, sizeof(*sec));
2239 sec->header.sh_type = SHT_PROGBITS;
2240 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2241 sec->header.sh_size = size;
2242 sec->header.sh_addralign = align;
2246 sec->contents = xmalloc(size);
2248 obj_insert_section_load_order(f, sec);
2253 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2255 unsigned long align,
2258 int newidx = f->header.e_shnum++;
2259 struct obj_section *sec;
2261 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2262 f->sections[newidx] = sec = arch_new_section();
2264 memset(sec, 0, sizeof(*sec));
2265 sec->header.sh_type = SHT_PROGBITS;
2266 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2267 sec->header.sh_size = size;
2268 sec->header.sh_addralign = align;
2272 sec->contents = xmalloc(size);
2274 sec->load_next = f->load_order;
2275 f->load_order = sec;
2276 if (f->load_order_search_start == &f->load_order)
2277 f->load_order_search_start = &sec->load_next;
2282 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2284 unsigned long oldsize = sec->header.sh_size;
2286 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2288 return sec->contents + oldsize;
2292 /* Conditionally add the symbols from the given symbol set to the
2296 add_symbols_from( struct obj_file *f,
2297 int idx, struct new_module_symbol *syms, size_t nsyms)
2299 struct new_module_symbol *s;
2302 #ifdef SYMBOL_PREFIX
2304 size_t name_alloced_size = 0;
2306 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2309 gpl = obj_gpl_license(f, NULL) == 0;
2311 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2312 /* Only add symbols that are already marked external.
2313 If we override locals we may cause problems for
2314 argument initialization. We will also create a false
2315 dependency on the module. */
2316 struct obj_symbol *sym;
2319 /* GPL licensed modules can use symbols exported with
2320 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2321 * exported names. Non-GPL modules never see any GPLONLY_
2322 * symbols so they cannot fudge it by adding the prefix on
2325 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2326 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2333 name = (char *)s->name;
2335 #ifdef SYMBOL_PREFIX
2336 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2337 kernel exports `C names', but module object files
2338 reference `linker names'). */
2339 size_t extra = sizeof SYMBOL_PREFIX;
2340 size_t name_size = strlen (name) + extra;
2341 if (name_size > name_alloced_size) {
2342 name_alloced_size = name_size * 2;
2343 name_buf = alloca (name_alloced_size);
2345 strcpy (name_buf, SYMBOL_PREFIX);
2346 strcpy (name_buf + extra - 1, name);
2348 #endif /* SYMBOL_PREFIX */
2350 sym = obj_find_symbol(f, name);
2351 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2352 #ifdef SYMBOL_PREFIX
2353 /* Put NAME_BUF into more permanent storage. */
2354 name = xmalloc (name_size);
2355 strcpy (name, name_buf);
2357 sym = obj_add_symbol(f, name, -1,
2358 ELF_ST_INFO(STB_GLOBAL,
2361 /* Did our symbol just get installed? If so, mark the
2362 module as "used". */
2363 if (sym->secidx == idx)
2371 static void add_kernel_symbols(struct obj_file *f)
2373 struct external_module *m;
2376 /* Add module symbols first. */
2378 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2380 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2381 m->nsyms)) m->used = 1, ++nused;
2383 n_ext_modules_used = nused;
2385 /* And finally the symbols from the kernel proper. */
2388 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2391 static char *get_modinfo_value(struct obj_file *f, const char *key)
2393 struct obj_section *sec;
2394 char *p, *v, *n, *ep;
2395 size_t klen = strlen(key);
2397 sec = obj_find_section(f, ".modinfo");
2401 ep = p + sec->header.sh_size;
2404 n = strchr(p, '\0');
2406 if (p + klen == v && strncmp(p, key, klen) == 0)
2409 if (p + klen == n && strcmp(p, key) == 0)
2419 /*======================================================================*/
2420 /* Functions relating to module loading after 2.1.18. */
2423 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2426 char *p, *q, *key, *sym_name;
2427 struct obj_symbol *sym;
2428 char *contents, *loc;
2432 if ((q = strchr(p, '=')) == NULL) {
2437 key = alloca(q - p + 6);
2438 memcpy(key, "parm_", 5);
2439 memcpy(key + 5, p, q - p);
2442 p = get_modinfo_value(f, key);
2445 bb_error_msg("invalid parameter %s", key);
2449 #ifdef SYMBOL_PREFIX
2450 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2451 strcpy (sym_name, SYMBOL_PREFIX);
2452 strcat (sym_name, key);
2456 sym = obj_find_symbol(f, sym_name);
2458 /* Also check that the parameter was not resolved from the kernel. */
2459 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2460 bb_error_msg("symbol for parameter %s not found", key);
2465 min = strtoul(p, &p, 10);
2467 max = strtoul(p + 1, &p, 10);
2473 contents = f->sections[sym->secidx]->contents;
2474 loc = contents + sym->value;
2478 if ((*p == 's') || (*p == 'c')) {
2481 /* Do C quoting if we begin with a ", else slurp the lot. */
2485 str = alloca(strlen(q));
2486 for (r = str, q++; *q != '"'; ++q, ++r) {
2488 bb_error_msg("improperly terminated string argument for %s",
2491 } else if (*q == '\\')
2525 if (q[1] >= '0' && q[1] <= '7') {
2526 c = (c * 8) + *++q - '0';
2527 if (q[1] >= '0' && q[1] <= '7')
2528 c = (c * 8) + *++q - '0';
2545 /* In this case, the string is not quoted. We will break
2546 it using the coma (like for ints). If the user wants to
2547 include comas in a string, he just has to quote it */
2549 /* Search the next coma */
2553 if (r != (char *) NULL) {
2554 /* Recopy the current field */
2555 str = alloca(r - q + 1);
2556 memcpy(str, q, r - q);
2558 /* I don't know if it is useful, as the previous case
2559 doesn't nul terminate the string ??? */
2562 /* Keep next fields */
2573 obj_string_patch(f, sym->secidx, loc - contents, str);
2574 loc += tgt_sizeof_char_p;
2576 /* Array of chars (in fact, matrix !) */
2577 unsigned long charssize; /* size of each member */
2579 /* Get the size of each member */
2580 /* Probably we should do that outside the loop ? */
2581 if (!isdigit(*(p + 1))) {
2582 bb_error_msg("parameter type 'c' for %s must be followed by"
2583 " the maximum size", key);
2586 charssize = strtoul(p + 1, (char **) NULL, 10);
2589 if (strlen(str) >= charssize) {
2590 bb_error_msg("string too long for %s (max %ld)", key,
2595 /* Copy to location */
2596 strcpy((char *) loc, str);
2600 long v = strtoul(q, &q, 0);
2607 loc += tgt_sizeof_short;
2611 loc += tgt_sizeof_int;
2615 loc += tgt_sizeof_long;
2619 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2634 goto retry_end_of_value;
2638 bb_error_msg("too many values for %s (max %d)", key, max);
2645 bb_error_msg("invalid argument syntax for %s", key);
2652 bb_error_msg("too few values for %s (min %d)", key, min);
2662 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2663 static int new_is_module_checksummed(struct obj_file *f)
2665 const char *p = get_modinfo_value(f, "using_checksums");
2672 /* Get the module's kernel version in the canonical integer form. */
2675 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2680 p = get_modinfo_value(f, "kernel_version");
2683 safe_strncpy(str, p, STRVERSIONLEN);
2685 a = strtoul(p, &p, 10);
2688 b = strtoul(p + 1, &p, 10);
2691 c = strtoul(p + 1, &q, 10);
2695 return a << 16 | b << 8 | c;
2698 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2701 /* Fetch the loaded modules, and all currently exported symbols. */
2703 static int new_get_kernel_symbols(void)
2705 char *module_names, *mn;
2706 struct external_module *modules, *m;
2707 struct new_module_symbol *syms, *s;
2708 size_t ret, bufsize, nmod, nsyms, i, j;
2710 /* Collect the loaded modules. */
2712 module_names = xmalloc(bufsize = 256);
2714 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2715 if (errno == ENOSPC && bufsize < ret) {
2716 module_names = xrealloc(module_names, bufsize = ret);
2717 goto retry_modules_load;
2719 bb_perror_msg("QM_MODULES");
2723 n_ext_modules = nmod = ret;
2725 /* Collect the modules' symbols. */
2728 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2729 memset(modules, 0, nmod * sizeof(*modules));
2730 for (i = 0, mn = module_names, m = modules;
2731 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2732 struct new_module_info info;
2734 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2735 if (errno == ENOENT) {
2736 /* The module was removed out from underneath us. */
2739 bb_perror_msg("query_module: QM_INFO: %s", mn);
2743 syms = xmalloc(bufsize = 1024);
2745 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2748 syms = xrealloc(syms, bufsize = ret);
2749 goto retry_mod_sym_load;
2751 /* The module was removed out from underneath us. */
2754 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2761 m->addr = info.addr;
2765 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2766 s->name += (unsigned long) syms;
2771 /* Collect the kernel's symbols. */
2773 syms = xmalloc(bufsize = 16 * 1024);
2774 retry_kern_sym_load:
2775 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2776 if (errno == ENOSPC && bufsize < ret) {
2777 syms = xrealloc(syms, bufsize = ret);
2778 goto retry_kern_sym_load;
2780 bb_perror_msg("kernel: QM_SYMBOLS");
2783 nksyms = nsyms = ret;
2786 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2787 s->name += (unsigned long) syms;
2793 /* Return the kernel symbol checksum version, or zero if not used. */
2795 static int new_is_kernel_checksummed(void)
2797 struct new_module_symbol *s;
2800 /* Using_Versions is not the first symbol, but it should be in there. */
2802 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2803 if (strcmp((char *) s->name, "Using_Versions") == 0)
2810 static int new_create_this_module(struct obj_file *f, const char *m_name)
2812 struct obj_section *sec;
2814 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2815 sizeof(struct new_module));
2816 memset(sec->contents, 0, sizeof(struct new_module));
2818 obj_add_symbol(f, SPFX "__this_module", -1,
2819 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2820 sizeof(struct new_module));
2822 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2828 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2829 /* add an entry to the __ksymtab section, creating it if necessary */
2830 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2832 struct obj_section *sec;
2835 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2836 * If __ksymtab is defined but not marked alloc, x out the first character
2837 * (no obj_delete routine) and create a new __ksymtab with the correct
2840 sec = obj_find_section(f, "__ksymtab");
2841 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2842 *((char *)(sec->name)) = 'x'; /* override const */
2846 sec = obj_create_alloced_section(f, "__ksymtab",
2847 tgt_sizeof_void_p, 0);
2850 sec->header.sh_flags |= SHF_ALLOC;
2851 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2853 ofs = sec->header.sh_size;
2854 obj_symbol_patch(f, sec->idx, ofs, sym);
2855 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2856 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2858 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2860 static int new_create_module_ksymtab(struct obj_file *f)
2862 struct obj_section *sec;
2865 /* We must always add the module references. */
2867 if (n_ext_modules_used) {
2868 struct new_module_ref *dep;
2869 struct obj_symbol *tm;
2871 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2872 (sizeof(struct new_module_ref)
2873 * n_ext_modules_used));
2877 tm = obj_find_symbol(f, SPFX "__this_module");
2878 dep = (struct new_module_ref *) sec->contents;
2879 for (i = 0; i < n_ext_modules; ++i)
2880 if (ext_modules[i].used) {
2881 dep->dep = ext_modules[i].addr;
2882 obj_symbol_patch(f, sec->idx,
2883 (char *) &dep->ref - sec->contents, tm);
2889 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2894 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2897 /* We don't want to export symbols residing in sections that
2898 aren't loaded. There are a number of these created so that
2899 we make sure certain module options don't appear twice. */
2901 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2903 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2905 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2906 struct obj_symbol *sym;
2907 for (sym = f->symtab[i]; sym; sym = sym->next)
2908 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2909 && sym->secidx <= SHN_HIRESERVE
2910 && (sym->secidx >= SHN_LORESERVE
2911 || loaded[sym->secidx])) {
2912 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2914 obj_symbol_patch(f, sec->idx, ofs, sym);
2915 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2922 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2930 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2932 struct new_module *module;
2933 struct obj_section *sec;
2938 sec = obj_find_section(f, ".this");
2939 if (!sec || !sec->contents) {
2940 bb_perror_msg_and_die("corrupt module %s?",m_name);
2942 module = (struct new_module *) sec->contents;
2943 m_addr = sec->header.sh_addr;
2945 module->size_of_struct = sizeof(*module);
2946 module->size = m_size;
2947 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2949 sec = obj_find_section(f, "__ksymtab");
2950 if (sec && sec->header.sh_size) {
2951 module->syms = sec->header.sh_addr;
2952 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2955 if (n_ext_modules_used) {
2956 sec = obj_find_section(f, ".kmodtab");
2957 module->deps = sec->header.sh_addr;
2958 module->ndeps = n_ext_modules_used;
2962 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2964 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2966 sec = obj_find_section(f, "__ex_table");
2968 module->ex_table_start = sec->header.sh_addr;
2969 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2972 sec = obj_find_section(f, ".text.init");
2974 module->runsize = sec->header.sh_addr - m_addr;
2976 sec = obj_find_section(f, ".data.init");
2978 if (!module->runsize ||
2979 module->runsize > sec->header.sh_addr - m_addr)
2980 module->runsize = sec->header.sh_addr - m_addr;
2982 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2983 if (sec && sec->header.sh_size) {
2984 module->archdata_start = (void*)sec->header.sh_addr;
2985 module->archdata_end = module->archdata_start + sec->header.sh_size;
2987 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2988 if (sec && sec->header.sh_size) {
2989 module->kallsyms_start = (void*)sec->header.sh_addr;
2990 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2993 /* Whew! All of the initialization is complete. Collect the final
2994 module image and give it to the kernel. */
2996 image = xmalloc(m_size);
2997 obj_create_image(f, image);
2999 ret = init_module(m_name, (struct new_module *) image);
3001 bb_perror_msg("init_module: %s", m_name);
3009 /*======================================================================*/
3012 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3015 struct obj_string_patch *p;
3016 struct obj_section *strsec;
3017 size_t len = strlen(string) + 1;
3020 p = xmalloc(sizeof(*p));
3021 p->next = f->string_patches;
3022 p->reloc_secidx = secidx;
3023 p->reloc_offset = offset;
3024 f->string_patches = p;
3026 strsec = obj_find_section(f, ".kstrtab");
3027 if (strsec == NULL) {
3028 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
3029 p->string_offset = 0;
3030 loc = strsec->contents;
3032 p->string_offset = strsec->header.sh_size;
3033 loc = obj_extend_section(strsec, len);
3035 memcpy(loc, string, len);
3041 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
3042 struct obj_symbol *sym)
3044 struct obj_symbol_patch *p;
3046 p = xmalloc(sizeof(*p));
3047 p->next = f->symbol_patches;
3048 p->reloc_secidx = secidx;
3049 p->reloc_offset = offset;
3051 f->symbol_patches = p;
3056 static int obj_check_undefineds(struct obj_file *f)
3061 for (i = 0; i < HASH_BUCKETS; ++i) {
3062 struct obj_symbol *sym;
3063 for (sym = f->symtab[i]; sym; sym = sym->next)
3064 if (sym->secidx == SHN_UNDEF) {
3065 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3066 sym->secidx = SHN_ABS;
3070 bb_error_msg("unresolved symbol %s", sym->name);
3080 static void obj_allocate_commons(struct obj_file *f)
3082 struct common_entry {
3083 struct common_entry *next;
3084 struct obj_symbol *sym;
3085 } *common_head = NULL;
3089 for (i = 0; i < HASH_BUCKETS; ++i) {
3090 struct obj_symbol *sym;
3091 for (sym = f->symtab[i]; sym; sym = sym->next)
3092 if (sym->secidx == SHN_COMMON) {
3093 /* Collect all COMMON symbols and sort them by size so as to
3094 minimize space wasted by alignment requirements. */
3096 struct common_entry **p, *n;
3097 for (p = &common_head; *p; p = &(*p)->next)
3098 if (sym->size <= (*p)->sym->size)
3101 n = alloca(sizeof(*n));
3109 for (i = 1; i < f->local_symtab_size; ++i) {
3110 struct obj_symbol *sym = f->local_symtab[i];
3111 if (sym && sym->secidx == SHN_COMMON) {
3112 struct common_entry **p, *n;
3113 for (p = &common_head; *p; p = &(*p)->next)
3114 if (sym == (*p)->sym)
3116 else if (sym->size < (*p)->sym->size) {
3117 n = alloca(sizeof(*n));
3127 /* Find the bss section. */
3128 for (i = 0; i < f->header.e_shnum; ++i)
3129 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3132 /* If for some reason there hadn't been one, create one. */
3133 if (i == f->header.e_shnum) {
3134 struct obj_section *sec;
3136 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3137 f->sections[i] = sec = arch_new_section();
3138 f->header.e_shnum = i + 1;
3140 memset(sec, 0, sizeof(*sec));
3141 sec->header.sh_type = SHT_PROGBITS;
3142 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3147 /* Allocate the COMMONS. */
3149 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3150 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3151 struct common_entry *c;
3153 for (c = common_head; c; c = c->next) {
3154 ElfW(Addr) align = c->sym->value;
3156 if (align > max_align)
3158 if (bss_size & (align - 1))
3159 bss_size = (bss_size | (align - 1)) + 1;
3162 c->sym->value = bss_size;
3164 bss_size += c->sym->size;
3167 f->sections[i]->header.sh_size = bss_size;
3168 f->sections[i]->header.sh_addralign = max_align;
3172 /* For the sake of patch relocation and parameter initialization,
3173 allocate zeroed data for NOBITS sections now. Note that after
3174 this we cannot assume NOBITS are really empty. */
3175 for (i = 0; i < f->header.e_shnum; ++i) {
3176 struct obj_section *s = f->sections[i];
3177 if (s->header.sh_type == SHT_NOBITS) {
3178 if (s->header.sh_size != 0)
3179 s->contents = memset(xmalloc(s->header.sh_size),
3180 0, s->header.sh_size);
3184 s->header.sh_type = SHT_PROGBITS;
3189 static unsigned long obj_load_size(struct obj_file *f)
3191 unsigned long dot = 0;
3192 struct obj_section *sec;
3194 /* Finalize the positions of the sections relative to one another. */
3196 for (sec = f->load_order; sec; sec = sec->load_next) {
3199 align = sec->header.sh_addralign;
3200 if (align && (dot & (align - 1)))
3201 dot = (dot | (align - 1)) + 1;
3203 sec->header.sh_addr = dot;
3204 dot += sec->header.sh_size;
3210 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3212 int i, n = f->header.e_shnum;
3215 /* Finalize the addresses of the sections. */
3218 for (i = 0; i < n; ++i)
3219 f->sections[i]->header.sh_addr += base;
3221 /* And iterate over all of the relocations. */
3223 for (i = 0; i < n; ++i) {
3224 struct obj_section *relsec, *symsec, *targsec, *strsec;
3225 ElfW(RelM) * rel, *relend;
3229 relsec = f->sections[i];
3230 if (relsec->header.sh_type != SHT_RELM)
3233 symsec = f->sections[relsec->header.sh_link];
3234 targsec = f->sections[relsec->header.sh_info];
3235 strsec = f->sections[symsec->header.sh_link];
3237 rel = (ElfW(RelM) *) relsec->contents;
3238 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3239 symtab = (ElfW(Sym) *) symsec->contents;
3240 strtab = (const char *) strsec->contents;
3242 for (; rel < relend; ++rel) {
3243 ElfW(Addr) value = 0;
3244 struct obj_symbol *intsym = NULL;
3245 unsigned long symndx;
3246 ElfW(Sym) * extsym = 0;
3249 /* Attempt to find a value to use for this relocation. */
3251 symndx = ELF_R_SYM(rel->r_info);
3253 /* Note we've already checked for undefined symbols. */
3255 extsym = &symtab[symndx];
3256 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3257 /* Local symbols we look up in the local table to be sure
3258 we get the one that is really intended. */
3259 intsym = f->local_symtab[symndx];
3261 /* Others we look up in the hash table. */
3263 if (extsym->st_name)
3264 name = strtab + extsym->st_name;
3266 name = f->sections[extsym->st_shndx]->name;
3267 intsym = obj_find_symbol(f, name);
3270 value = obj_symbol_final_value(f, intsym);
3271 intsym->referenced = 1;
3273 #if SHT_RELM == SHT_RELA
3274 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3275 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3276 if (!extsym || !extsym->st_name ||
3277 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3279 value += rel->r_addend;
3283 switch (arch_apply_relocation
3284 (f, targsec, symsec, intsym, rel, value)) {
3288 case obj_reloc_overflow:
3289 errmsg = "Relocation overflow";
3291 case obj_reloc_dangerous:
3292 errmsg = "Dangerous relocation";
3294 case obj_reloc_unhandled:
3295 errmsg = "Unhandled relocation";
3298 bb_error_msg("%s of type %ld for %s", errmsg,
3299 (long) ELF_R_TYPE(rel->r_info),
3300 strtab + extsym->st_name);
3302 bb_error_msg("%s of type %ld", errmsg,
3303 (long) ELF_R_TYPE(rel->r_info));
3311 /* Finally, take care of the patches. */
3313 if (f->string_patches) {
3314 struct obj_string_patch *p;
3315 struct obj_section *strsec;
3316 ElfW(Addr) strsec_base;
3317 strsec = obj_find_section(f, ".kstrtab");
3318 strsec_base = strsec->header.sh_addr;
3320 for (p = f->string_patches; p; p = p->next) {
3321 struct obj_section *targsec = f->sections[p->reloc_secidx];
3322 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3323 = strsec_base + p->string_offset;
3327 if (f->symbol_patches) {
3328 struct obj_symbol_patch *p;
3330 for (p = f->symbol_patches; p; p = p->next) {
3331 struct obj_section *targsec = f->sections[p->reloc_secidx];
3332 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3333 = obj_symbol_final_value(f, p->sym);
3340 static int obj_create_image(struct obj_file *f, char *image)
3342 struct obj_section *sec;
3343 ElfW(Addr) base = f->baseaddr;
3345 for (sec = f->load_order; sec; sec = sec->load_next) {
3348 if (sec->contents == 0 || sec->header.sh_size == 0)
3351 secimg = image + (sec->header.sh_addr - base);
3353 /* Note that we allocated data for NOBITS sections earlier. */
3354 memcpy(secimg, sec->contents, sec->header.sh_size);
3360 /*======================================================================*/
3362 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3365 ElfW(Shdr) * section_headers;
3369 /* Read the file header. */
3371 f = arch_new_file();
3372 memset(f, 0, sizeof(*f));
3373 f->symbol_cmp = strcmp;
3374 f->symbol_hash = obj_elf_hash;
3375 f->load_order_search_start = &f->load_order;
3377 fseek(fp, 0, SEEK_SET);
3378 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3379 bb_perror_msg("error reading ELF header");
3383 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3384 || f->header.e_ident[EI_MAG1] != ELFMAG1
3385 || f->header.e_ident[EI_MAG2] != ELFMAG2
3386 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3387 bb_error_msg("not an ELF file");
3390 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3391 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
3392 ? ELFDATA2MSB : ELFDATA2LSB)
3393 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3394 || !MATCH_MACHINE(f->header.e_machine)) {
3395 bb_error_msg("ELF file not for this architecture");
3398 if (f->header.e_type != ET_REL) {
3399 bb_error_msg("ELF file not a relocatable object");
3403 /* Read the section headers. */
3405 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3406 bb_error_msg("section header size mismatch: %lu != %lu",
3407 (unsigned long) f->header.e_shentsize,
3408 (unsigned long) sizeof(ElfW(Shdr)));
3412 shnum = f->header.e_shnum;
3413 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3414 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3416 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3417 fseek(fp, f->header.e_shoff, SEEK_SET);
3418 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3419 bb_perror_msg("error reading ELF section headers");
3423 /* Read the section data. */
3425 for (i = 0; i < shnum; ++i) {
3426 struct obj_section *sec;
3428 f->sections[i] = sec = arch_new_section();
3429 memset(sec, 0, sizeof(*sec));
3431 sec->header = section_headers[i];
3434 if(sec->header.sh_size) switch (sec->header.sh_type) {
3443 if (!loadprogbits) {
3444 sec->contents = NULL;
3451 if (sec->header.sh_size > 0) {
3452 sec->contents = xmalloc(sec->header.sh_size);
3453 fseek(fp, sec->header.sh_offset, SEEK_SET);
3454 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3455 bb_perror_msg("error reading ELF section data");
3459 sec->contents = NULL;
3463 #if SHT_RELM == SHT_REL
3465 bb_error_msg("RELA relocations not supported on this architecture");
3469 bb_error_msg("REL relocations not supported on this architecture");
3474 if (sec->header.sh_type >= SHT_LOPROC) {
3475 /* Assume processor specific section types are debug
3476 info and can safely be ignored. If this is ever not
3477 the case (Hello MIPS?), don't put ifdefs here but
3478 create an arch_load_proc_section(). */
3482 bb_error_msg("can't handle sections of type %ld",
3483 (long) sec->header.sh_type);
3488 /* Do what sort of interpretation as needed by each section. */
3490 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3492 for (i = 0; i < shnum; ++i) {
3493 struct obj_section *sec = f->sections[i];
3494 sec->name = shstrtab + sec->header.sh_name;
3497 for (i = 0; i < shnum; ++i) {
3498 struct obj_section *sec = f->sections[i];
3500 /* .modinfo should be contents only but gcc has no attribute for that.
3501 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3503 if (strcmp(sec->name, ".modinfo") == 0)
3504 sec->header.sh_flags &= ~SHF_ALLOC;
3506 if (sec->header.sh_flags & SHF_ALLOC)
3507 obj_insert_section_load_order(f, sec);
3509 switch (sec->header.sh_type) {
3512 unsigned long nsym, j;
3516 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3517 bb_error_msg("symbol size mismatch: %lu != %lu",
3518 (unsigned long) sec->header.sh_entsize,
3519 (unsigned long) sizeof(ElfW(Sym)));
3523 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3524 strtab = f->sections[sec->header.sh_link]->contents;
3525 sym = (ElfW(Sym) *) sec->contents;
3527 /* Allocate space for a table of local symbols. */
3528 j = f->local_symtab_size = sec->header.sh_info;
3529 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3531 /* Insert all symbols into the hash table. */
3532 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3533 ElfW(Addr) val = sym->st_value;
3536 name = strtab + sym->st_name;
3537 else if (sym->st_shndx < shnum)
3538 name = f->sections[sym->st_shndx]->name;
3542 #if defined(__SH5__)
3544 * For sh64 it is possible that the target of a branch
3545 * requires a mode switch (32 to 16 and back again).
3547 * This is implied by the lsb being set in the target
3548 * address for SHmedia mode and clear for SHcompact.
3550 val |= sym->st_other & 4;
3553 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3560 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3561 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3562 (unsigned long) sec->header.sh_entsize,
3563 (unsigned long) sizeof(ElfW(RelM)));
3567 /* XXX Relocation code from modutils-2.3.19 is not here.
3568 * Why? That's about 20 lines of code from obj/obj_load.c,
3569 * which gets done in a second pass through the sections.
3570 * This BusyBox insmod does similar work in obj_relocate(). */
3577 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3579 * load the unloaded sections directly into the memory allocated by
3580 * kernel for the module
3583 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3585 ElfW(Addr) base = f->baseaddr;
3586 struct obj_section* sec;
3588 for (sec = f->load_order; sec; sec = sec->load_next) {
3590 /* section already loaded? */
3591 if (sec->contents != NULL)
3594 if (sec->header.sh_size == 0)
3597 sec->contents = imagebase + (sec->header.sh_addr - base);
3598 fseek(fp, sec->header.sh_offset, SEEK_SET);
3599 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3600 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3609 static void hide_special_symbols(struct obj_file *f)
3611 static const char *const specials[] = {
3612 SPFX "cleanup_module",
3614 SPFX "kernel_version",
3618 struct obj_symbol *sym;
3619 const char *const *p;
3621 for (p = specials; *p; ++p)
3622 if ((sym = obj_find_symbol(f, *p)) != NULL)
3624 ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3628 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3629 static int obj_gpl_license(struct obj_file *f, const char **license)
3631 struct obj_section *sec;
3632 /* This list must match *exactly* the list of allowable licenses in
3633 * linux/include/linux/module.h. Checking for leading "GPL" will not
3634 * work, somebody will use "GPL sucks, this is proprietary".
3636 static const char * const gpl_licenses[] = {
3639 "GPL and additional rights",
3644 if ((sec = obj_find_section(f, ".modinfo"))) {
3645 const char *value, *ptr, *endptr;
3646 ptr = sec->contents;
3647 endptr = ptr + sec->header.sh_size;
3648 while (ptr < endptr) {
3649 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3653 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3654 if (strcmp(value+1, gpl_licenses[i]) == 0)
3659 if (strchr(ptr, '\0'))
3660 ptr = strchr(ptr, '\0') + 1;
3668 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3669 #define TAINT_PROPRIETORY_MODULE (1<<0)
3670 #define TAINT_FORCED_MODULE (1<<1)
3671 #define TAINT_UNSAFE_SMP (1<<2)
3672 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3674 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3675 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3679 static int first = 1;
3680 if (fd < 0 && !kernel_has_tainted)
3681 return; /* New modutils on old kernel */
3682 printf("Warning: loading %s will taint the kernel: %s%s\n",
3683 m_name, text1, text2);
3685 printf(" See %s for information about tainted modules\n", TAINT_URL);
3689 read(fd, buf, sizeof(buf)-1);
3690 buf[sizeof(buf)-1] = '\0';
3691 oldval = strtoul(buf, NULL, 10);
3692 sprintf(buf, "%d\n", oldval | taint);
3693 write(fd, buf, strlen(buf));
3697 /* Check if loading this module will taint the kernel. */
3698 static void check_tainted_module(struct obj_file *f, char *m_name)
3700 static const char tainted_file[] = TAINT_FILENAME;
3701 int fd, kernel_has_tainted;
3704 kernel_has_tainted = 1;
3705 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3706 if (errno == ENOENT)
3707 kernel_has_tainted = 0;
3708 else if (errno == EACCES)
3709 kernel_has_tainted = 1;
3711 perror(tainted_file);
3712 kernel_has_tainted = 0;
3716 switch (obj_gpl_license(f, &ptr)) {
3720 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3723 /* The module has a non-GPL license so we pretend that the
3724 * kernel always has a taint flag to get a warning even on
3725 * kernels without the proc flag.
3727 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3730 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3734 if (flag_force_load)
3735 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3740 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3741 #define check_tainted_module(x, y) do { } while(0);
3742 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3744 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3745 /* add module source, timestamp, kernel version and a symbol for the
3746 * start of some sections. this info is used by ksymoops to do better
3750 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3752 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3753 return new_get_module_version(f, str);
3754 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3755 strncpy(str, "???", sizeof(str));
3757 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3760 /* add module source, timestamp, kernel version and a symbol for the
3761 * start of some sections. this info is used by ksymoops to do better
3765 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3768 static const char symprefix[] = "__insmod_";
3769 struct obj_section *sec;
3770 struct obj_symbol *sym;
3771 char *name, *absolute_filename;
3772 char str[STRVERSIONLEN], real[PATH_MAX];
3773 int i, l, lm_name, lfilename, use_ksymtab, version;
3774 struct stat statbuf;
3776 static const char *section_names[] = {
3784 if (realpath(filename, real)) {
3785 absolute_filename = bb_xstrdup(real);
3788 int save_errno = errno;
3789 bb_error_msg("cannot get realpath for %s", filename);
3792 absolute_filename = bb_xstrdup(filename);
3795 lm_name = strlen(m_name);
3796 lfilename = strlen(absolute_filename);
3798 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3799 * are not to be exported. otherwise leave ksymtab alone for now, the
3800 * "export all symbols" compatibility code will export these symbols later.
3802 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3804 if ((sec = obj_find_section(f, ".this"))) {
3805 /* tag the module header with the object name, last modified
3806 * timestamp and module version. worst case for module version
3807 * is 0xffffff, decimal 16777215. putting all three fields in
3808 * one symbol is less readable but saves kernel space.
3810 l = sizeof(symprefix)+ /* "__insmod_" */
3811 lm_name+ /* module name */
3813 lfilename+ /* object filename */
3815 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3817 8+ /* version in dec */
3820 if (stat(absolute_filename, &statbuf) != 0)
3821 statbuf.st_mtime = 0;
3822 version = get_module_version(f, str); /* -1 if not found */
3823 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3824 symprefix, m_name, absolute_filename,
3825 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3827 sym = obj_add_symbol(f, name, -1,
3828 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3829 sec->idx, sec->header.sh_addr, 0);
3831 new_add_ksymtab(f, sym);
3833 free(absolute_filename);
3834 #ifdef _NOT_SUPPORTED_
3835 /* record where the persistent data is going, same address as previous symbol */
3838 l = sizeof(symprefix)+ /* "__insmod_" */
3839 lm_name+ /* module name */
3841 strlen(f->persist)+ /* data store */
3844 snprintf(name, l, "%s%s_P%s",
3845 symprefix, m_name, f->persist);
3846 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3847 sec->idx, sec->header.sh_addr, 0);
3849 new_add_ksymtab(f, sym);
3851 #endif /* _NOT_SUPPORTED_ */
3852 /* tag the desired sections if size is non-zero */
3854 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3855 if ((sec = obj_find_section(f, section_names[i])) &&
3856 sec->header.sh_size) {
3857 l = sizeof(symprefix)+ /* "__insmod_" */
3858 lm_name+ /* module name */
3860 strlen(sec->name)+ /* section name */
3862 8+ /* length in dec */
3865 snprintf(name, l, "%s%s_S%s_L%ld",
3866 symprefix, m_name, sec->name,
3867 (long)sec->header.sh_size);
3868 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3869 sec->idx, sec->header.sh_addr, 0);
3871 new_add_ksymtab(f, sym);
3875 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3877 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3878 static void print_load_map(struct obj_file *f)
3880 struct obj_symbol *sym;
3881 struct obj_symbol **all, **p;
3882 struct obj_section *sec;
3883 int i, nsyms, *loaded;
3885 /* Report on the section layout. */
3887 printf("Sections: Size %-*s Align\n",
3888 (int) (2 * sizeof(void *)), "Address");
3890 for (sec = f->load_order; sec; sec = sec->load_next) {
3894 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3899 printf("%-15s %08lx %0*lx 2**%d\n",
3901 (long)sec->header.sh_size,
3902 (int) (2 * sizeof(void *)),
3903 (long)sec->header.sh_addr,
3906 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3907 /* Quick reference which section indicies are loaded. */
3909 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3911 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3913 /* Collect the symbols we'll be listing. */
3915 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3916 for (sym = f->symtab[i]; sym; sym = sym->next)
3917 if (sym->secidx <= SHN_HIRESERVE
3918 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3921 all = alloca(nsyms * sizeof(struct obj_symbol *));
3923 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3924 for (sym = f->symtab[i]; sym; sym = sym->next)
3925 if (sym->secidx <= SHN_HIRESERVE
3926 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3929 /* And list them. */
3930 printf("\nSymbols:\n");
3931 for (p = all; p < all + nsyms; ++p) {
3933 unsigned long value;
3936 if (sym->secidx == SHN_ABS) {
3939 } else if (sym->secidx == SHN_UNDEF) {
3943 sec = f->sections[sym->secidx];
3945 if (sec->header.sh_type == SHT_NOBITS)
3947 else if (sec->header.sh_flags & SHF_ALLOC) {
3948 if (sec->header.sh_flags & SHF_EXECINSTR)
3950 else if (sec->header.sh_flags & SHF_WRITE)
3955 value = sym->value + sec->header.sh_addr;
3958 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3959 type = tolower(type);
3961 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3969 int insmod_main( int argc, char **argv)
3975 unsigned long m_size;
3980 int exit_status = EXIT_FAILURE;
3982 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3983 struct utsname uts_info;
3984 char m_strversion[STRVERSIONLEN];
3985 int m_version, m_crcs;
3987 #ifdef CONFIG_FEATURE_CLEAN_UP
3992 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3993 int flag_print_load_map = 0;
3996 struct utsname myuname;
3998 /* Parse any options */
3999 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4000 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
4002 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
4006 case 'f': /* force loading */
4007 flag_force_load = 1;
4009 case 'k': /* module loaded by kerneld, auto-cleanable */
4012 case 's': /* log to syslog */
4013 /* log to syslog -- not supported */
4014 /* but kernel needs this for request_module(), */
4015 /* as this calls: modprobe -k -s -- <module> */
4016 /* so silently ignore this flag */
4018 case 'v': /* verbose output */
4021 case 'q': /* silent */
4024 case 'x': /* do not export externs */
4027 case 'o': /* name the output module */
4029 m_name = bb_xstrdup(optarg);
4031 case 'L': /* Stub warning */
4032 /* This is needed for compatibility with modprobe.
4033 * In theory, this does locking, but we don't do
4034 * that. So be careful and plan your life around not
4035 * loading the same module 50 times concurrently. */
4037 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4038 case 'm': /* print module load map */
4039 flag_print_load_map = 1;
4047 if (argv[optind] == NULL) {
4051 /* Grab the module name */
4052 tmp1 = bb_xstrdup(argv[optind]);
4053 tmp = basename(tmp1);
4056 if (uname(&myuname) == 0) {
4057 if (myuname.release[0] == '2') {
4058 k_version = myuname.release[2] - '0';
4062 #if defined(CONFIG_FEATURE_2_6_MODULES)
4063 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
4064 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
4070 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4076 #if defined(CONFIG_FEATURE_2_6_MODULES)
4078 m_fullName = bb_xasprintf("%s.ko", tmp);
4081 m_fullName = bb_xasprintf("%s.o", tmp);
4087 tmp1 = 0; /* flag for free(m_name) before exit() */
4090 /* Get a filedesc for the module. Check we we have a complete path */
4091 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
4092 (fp = fopen(argv[optind], "r")) == NULL) {
4093 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4094 * but do not error out yet if we fail to find it... */
4095 if (k_version) { /* uname succeedd */
4098 char real_module_dir[FILENAME_MAX];
4100 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4101 /* Jump through hoops in case /lib/modules/`uname -r`
4102 * is a symlink. We do not want recursive_action to
4103 * follow symlinks, but we do want to follow the
4104 * /lib/modules/`uname -r` dir, So resolve it ourselves
4105 * if it is a link... */
4106 if (realpath (tmdn, real_module_dir) == NULL)
4109 module_dir = real_module_dir;
4110 recursive_action(module_dir, TRUE, FALSE, FALSE,
4111 check_module_name_match, 0, m_fullName);
4115 /* Check if we have found anything yet */
4116 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4118 char module_dir[FILENAME_MAX];
4122 if (realpath (_PATH_MODULES, module_dir) == NULL)
4123 strcpy(module_dir, _PATH_MODULES);
4124 /* No module found under /lib/modules/`uname -r`, this
4125 * time cast the net a bit wider. Search /lib/modules/ */
4126 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4127 check_module_name_match, 0, m_fullName))
4130 || ((fp = fopen(m_filename, "r")) == NULL))
4132 bb_error_msg("%s: no module by that name found", m_fullName);
4136 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4139 m_filename = bb_xstrdup(argv[optind]);
4142 printf("Using %s\n", m_filename);
4144 #ifdef CONFIG_FEATURE_2_6_MODULES
4148 argv[optind + 1] = m_filename;
4149 return insmod_ng_main(argc - optind, argv + optind);
4153 if ((f = obj_load(fp, LOADBITS)) == NULL)
4154 bb_perror_msg_and_die("Could not load the module");
4156 if (get_modinfo_value(f, "kernel_version") == NULL)
4161 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4162 /* Version correspondence? */
4164 if (uname(&uts_info) < 0)
4165 uts_info.release[0] = '\0';
4166 if (m_has_modinfo) {
4167 m_version = new_get_module_version(f, m_strversion);
4168 if (m_version == -1) {
4169 bb_error_msg("couldn't find the kernel version the module was "
4175 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4176 if (flag_force_load) {
4177 bb_error_msg("Warning: kernel-module version mismatch\n"
4178 "\t%s was compiled for kernel version %s\n"
4179 "\twhile this kernel is version %s",
4180 m_filename, m_strversion, uts_info.release);
4182 bb_error_msg("kernel-module version mismatch\n"
4183 "\t%s was compiled for kernel version %s\n"
4184 "\twhile this kernel is version %s.",
4185 m_filename, m_strversion, uts_info.release);
4191 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4193 if (!query_module(NULL, 0, NULL, 0, NULL)) {
4194 if (!new_get_kernel_symbols())
4196 k_crcs = new_is_kernel_checksummed();
4198 bb_error_msg("Not configured to support old kernels");
4202 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4205 m_crcs = new_is_module_checksummed(f);
4207 if (m_crcs != k_crcs)
4208 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4209 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4211 /* Let the module know about the kernel symbols. */
4212 add_kernel_symbols(f);
4214 /* Allocate common symbols, symbol tables, and string tables. */
4216 if (!new_create_this_module(f, m_name))
4221 if (!obj_check_undefineds(f)) {
4224 obj_allocate_commons(f);
4225 check_tainted_module(f, m_name);
4227 /* done with the module name, on to the optional var=value arguments */
4230 if (optind < argc) {
4231 if (!new_process_module_arguments(f, argc - optind, argv + optind))
4238 hide_special_symbols(f);
4240 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4241 add_ksymoops_symbols(f, m_filename, m_name);
4242 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4244 new_create_module_ksymtab(f);
4246 /* Find current size of the module */
4247 m_size = obj_load_size(f);
4250 m_addr = create_module(m_name, m_size);
4251 if (m_addr == -1) switch (errno) {
4253 bb_error_msg("A module named %s already exists", m_name);
4256 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4260 bb_perror_msg("create_module: %s", m_name);
4266 * the PROGBITS section was not loaded by the obj_load
4267 * now we can load them directly into the kernel memory
4269 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4270 delete_module(m_name);
4275 if (!obj_relocate(f, m_addr)) {
4276 delete_module(m_name);
4280 if (!new_init_module(m_name, f, m_size))
4282 delete_module(m_name);
4286 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4287 if(flag_print_load_map)
4291 exit_status = EXIT_SUCCESS;
4294 #ifdef CONFIG_FEATURE_CLEAN_UP
4303 return(exit_status);
4310 #ifdef CONFIG_FEATURE_2_6_MODULES
4312 #include <sys/mman.h>
4313 #include <asm/unistd.h>
4314 #include <sys/syscall.h>
4316 /* We use error numbers in a loose translation... */
4317 static const char *moderror(int err)
4321 return "Invalid module format";
4323 return "Unknown symbol in module";
4325 return "Module has wrong symbol version";
4327 return "Invalid parameters";
4329 return strerror(err);
4333 int insmod_ng_main( int argc, char **argv)
4341 char *filename, *options = bb_xstrdup("");
4349 /* Rest is options */
4350 for (i = 2; i < argc; i++) {
4351 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4352 /* Spaces handled by "" pairs, but no way of escaping quotes */
4353 if (strchr(argv[i], ' ')) {
4354 strcat(options, "\"");
4355 strcat(options, argv[i]);
4356 strcat(options, "\"");
4358 strcat(options, argv[i]);
4360 strcat(options, " ");
4363 fd = bb_xopen3(filename, O_RDONLY, 0);
4367 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4368 if (map == MAP_FAILED) {
4369 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4372 ret = syscall(__NR_init_module, map, len, options);
4374 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4375 filename, moderror(errno), ret);