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__) && defined(__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 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
367 static const int tgt_sizeof_long = 8;
368 static const int tgt_sizeof_char_p = 8;
369 static const int tgt_sizeof_void_p = 8;
370 #define tgt_long long long
373 /*======================================================================*/
374 /* The structures used in Linux 2.1. */
376 /* Note: new_module_symbol does not use tgt_long intentionally */
377 struct new_module_symbol
383 struct new_module_persist;
385 struct new_module_ref
387 unsigned tgt_long dep; /* kernel addresses */
388 unsigned tgt_long ref;
389 unsigned tgt_long next_ref;
394 unsigned tgt_long size_of_struct; /* == sizeof(module) */
395 unsigned tgt_long next;
396 unsigned tgt_long name;
397 unsigned tgt_long size;
400 unsigned tgt_long flags; /* AUTOCLEAN et al */
405 unsigned tgt_long syms;
406 unsigned tgt_long deps;
407 unsigned tgt_long refs;
408 unsigned tgt_long init;
409 unsigned tgt_long cleanup;
410 unsigned tgt_long ex_table_start;
411 unsigned tgt_long ex_table_end;
413 unsigned tgt_long gp;
415 /* Everything after here is extension. */
416 unsigned tgt_long persist_start;
417 unsigned tgt_long persist_end;
418 unsigned tgt_long can_unload;
419 unsigned tgt_long runsize;
420 const char *kallsyms_start; /* All symbols for kernel debugging */
421 const char *kallsyms_end;
422 const char *archdata_start; /* arch specific data for module */
423 const char *archdata_end;
424 const char *kernel_data; /* Reserved for kernel internal use */
428 #define ARCHDATA_SEC_NAME ARCHDATAM
430 #define ARCHDATA_SEC_NAME "__archdata"
432 #define KALLSYMS_SEC_NAME "__kallsyms"
435 struct new_module_info
443 /* Bits of module.flags. */
444 static const int NEW_MOD_RUNNING = 1;
445 static const int NEW_MOD_DELETED = 2;
446 static const int NEW_MOD_AUTOCLEAN = 4;
447 static const int NEW_MOD_VISITED = 8;
448 static const int NEW_MOD_USED_ONCE = 16;
450 int init_module(const char *name, const struct new_module *);
451 int query_module(const char *name, int which, void *buf,
452 size_t bufsize, size_t *ret);
454 /* Values for query_module's which. */
456 static const int QM_MODULES = 1;
457 static const int QM_DEPS = 2;
458 static const int QM_REFS = 3;
459 static const int QM_SYMBOLS = 4;
460 static const int QM_INFO = 5;
462 /*======================================================================*/
463 /* The system calls unchanged between 2.0 and 2.1. */
465 unsigned long create_module(const char *, size_t);
466 int delete_module(const char *);
469 #endif /* module.h */
471 //----------------------------------------------------------------------------
472 //--------end of modutils module.h
473 //----------------------------------------------------------------------------
477 //----------------------------------------------------------------------------
478 //--------modutils obj.h, lines 253-462
479 //----------------------------------------------------------------------------
481 /* Elf object file loading and relocation routines.
482 Copyright 1996, 1997 Linux International.
484 Contributed by Richard Henderson <rth@tamu.edu>
486 This file is part of the Linux modutils.
488 This program is free software; you can redistribute it and/or modify it
489 under the terms of the GNU General Public License as published by the
490 Free Software Foundation; either version 2 of the License, or (at your
491 option) any later version.
493 This program is distributed in the hope that it will be useful, but
494 WITHOUT ANY WARRANTY; without even the implied warranty of
495 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
496 General Public License for more details.
498 You should have received a copy of the GNU General Public License
499 along with this program; if not, write to the Free Software Foundation,
500 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
503 #ifndef MODUTILS_OBJ_H
504 static const int MODUTILS_OBJ_H = 1;
506 #ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
508 /* The relocatable object is manipulated using elfin types. */
514 #if __BYTE_ORDER == __LITTLE_ENDIAN
515 #define ELFDATAM ELFDATA2LSB
516 #elif __BYTE_ORDER == __BIG_ENDIAN
517 #define ELFDATAM ELFDATA2MSB
521 # if ELFCLASSM == ELFCLASS32
522 # define ElfW(x) Elf32_ ## x
523 # define ELFW(x) ELF32_ ## x
525 # define ElfW(x) Elf64_ ## x
526 # define ELFW(x) ELF64_ ## x
530 /* For some reason this is missing from some ancient C libraries.... */
531 #ifndef ELF32_ST_INFO
532 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
535 #ifndef ELF64_ST_INFO
536 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
539 #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
540 #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
541 #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
542 #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
543 #define ELF_R_SYM(val) ELFW(R_SYM)(val)
545 struct obj_string_patch;
546 struct obj_symbol_patch;
553 struct obj_section *load_next;
559 struct obj_symbol *next; /* hash table link */
563 int secidx; /* the defining section index/module */
565 int ksymidx; /* for export to the kernel symtab */
566 int referenced; /* actually used in the link */
569 /* Hardcode the hash table size. We shouldn't be needing so many
570 symbols that we begin to degrade performance, and we get a big win
571 by giving the compiler a constant divisor. */
573 #define HASH_BUCKETS 521
579 struct obj_section **sections;
580 struct obj_section *load_order;
581 struct obj_section **load_order_search_start;
582 struct obj_string_patch *string_patches;
583 struct obj_symbol_patch *symbol_patches;
584 int (*symbol_cmp)(const char *, const char *);
585 unsigned long (*symbol_hash)(const char *);
586 unsigned long local_symtab_size;
587 struct obj_symbol **local_symtab;
588 struct obj_symbol *symtab[HASH_BUCKETS];
599 struct obj_string_patch
601 struct obj_string_patch *next;
603 ElfW(Addr) reloc_offset;
604 ElfW(Addr) string_offset;
607 struct obj_symbol_patch
609 struct obj_symbol_patch *next;
611 ElfW(Addr) reloc_offset;
612 struct obj_symbol *sym;
616 /* Generic object manipulation routines. */
618 static unsigned long obj_elf_hash(const char *);
620 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
622 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
625 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
626 struct obj_symbol *sym);
628 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
629 static void obj_set_symbol_compare(struct obj_file *f,
630 int (*cmp)(const char *, const char *),
631 unsigned long (*hash)(const char *));
634 static struct obj_section *obj_find_section (struct obj_file *f,
637 static void obj_insert_section_load_order (struct obj_file *f,
638 struct obj_section *sec);
640 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
645 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
650 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
652 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
655 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
656 struct obj_symbol *sym);
658 static int obj_check_undefineds(struct obj_file *f);
660 static void obj_allocate_commons(struct obj_file *f);
662 static unsigned long obj_load_size (struct obj_file *f);
664 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
666 static struct obj_file *obj_load(FILE *f, int loadprogbits);
668 static int obj_create_image (struct obj_file *f, char *image);
670 /* Architecture specific manipulation routines. */
672 static struct obj_file *arch_new_file (void);
674 static struct obj_section *arch_new_section (void);
676 static struct obj_symbol *arch_new_symbol (void);
678 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
679 struct obj_section *targsec,
680 struct obj_section *symsec,
681 struct obj_symbol *sym,
682 ElfW(RelM) *rel, ElfW(Addr) value);
684 static void arch_create_got (struct obj_file *f);
686 static int obj_gpl_license(struct obj_file *f, const char **license);
689 //----------------------------------------------------------------------------
690 //--------end of modutils obj.h
691 //----------------------------------------------------------------------------
694 /* SPFX is always a string, so it can be concatenated to string constants. */
696 #define SPFX SYMBOL_PREFIX
702 #define _PATH_MODULES "/lib/modules"
703 static const int STRVERSIONLEN = 32;
705 /*======================================================================*/
707 static int flag_force_load = 0;
708 static int flag_autoclean = 0;
709 static int flag_verbose = 0;
710 static int flag_quiet = 0;
711 static int flag_export = 1;
714 /*======================================================================*/
716 #if defined(CONFIG_USE_LIST)
718 struct arch_list_entry
720 struct arch_list_entry *next;
721 CONFIG_LIST_ARCHTYPE addend;
728 #if defined(CONFIG_USE_SINGLE)
730 struct arch_single_entry
739 #if defined(__mips__)
742 struct mips_hi16 *next;
749 struct obj_file root;
750 #if defined(CONFIG_USE_PLT_ENTRIES)
751 struct obj_section *plt;
753 #if defined(CONFIG_USE_GOT_ENTRIES)
754 struct obj_section *got;
756 #if defined(__mips__)
757 struct mips_hi16 *mips_hi16_list;
762 struct obj_symbol root;
763 #if defined(CONFIG_USE_PLT_ENTRIES)
764 #if defined(CONFIG_USE_PLT_LIST)
765 struct arch_list_entry *pltent;
767 struct arch_single_entry pltent;
770 #if defined(CONFIG_USE_GOT_ENTRIES)
771 struct arch_single_entry gotent;
776 struct external_module {
781 struct new_module_symbol *syms;
784 static struct new_module_symbol *ksyms;
785 static size_t nksyms;
787 static struct external_module *ext_modules;
788 static int n_ext_modules;
789 static int n_ext_modules_used;
790 extern int delete_module(const char *);
792 static char *m_filename;
793 static char *m_fullName;
797 /*======================================================================*/
800 static int check_module_name_match(const char *filename, struct stat *statbuf,
803 char *fullname = (char *) userdata;
805 if (fullname[0] == '\0')
808 char *tmp, *tmp1 = bb_xstrdup(filename);
809 tmp = bb_get_last_path_component(tmp1);
810 if (strcmp(tmp, fullname) == 0) {
812 /* Stop searching if we find a match */
813 m_filename = bb_xstrdup(filename);
822 /*======================================================================*/
824 static struct obj_file *arch_new_file(void)
827 f = xmalloc(sizeof(*f));
829 memset(f, 0, sizeof(*f));
834 static struct obj_section *arch_new_section(void)
836 return xmalloc(sizeof(struct obj_section));
839 static struct obj_symbol *arch_new_symbol(void)
841 struct arch_symbol *sym;
842 sym = xmalloc(sizeof(*sym));
844 memset(sym, 0, sizeof(*sym));
849 static enum obj_reloc
850 arch_apply_relocation(struct obj_file *f,
851 struct obj_section *targsec,
852 struct obj_section *symsec,
853 struct obj_symbol *sym,
854 ElfW(RelM) *rel, ElfW(Addr) v)
856 struct arch_file *ifile = (struct arch_file *) f;
857 enum obj_reloc ret = obj_reloc_ok;
858 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
859 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
860 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
861 struct arch_symbol *isym = (struct arch_symbol *) sym;
863 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
864 #if defined(CONFIG_USE_GOT_ENTRIES)
865 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
868 #if defined(CONFIG_USE_PLT_ENTRIES)
869 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
871 # if defined(CONFIG_USE_PLT_LIST)
872 struct arch_list_entry *pe;
874 struct arch_single_entry *pe;
878 switch (ELF_R_TYPE(rel->r_info)) {
893 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
894 * (which is .got) similar to branch,
895 * but is full 32 bits relative */
905 case R_ARM_GOTOFF: /* address relative to the got */
910 #elif defined(__cris__)
916 /* CRIS keeps the relocation value in the r_addend field and
917 * should not use whats in *loc at all
922 #elif defined(__H8300H__) || defined(__H8300S__)
925 loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
926 *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
937 if ((ElfW(Sword))v > 0x7fff ||
938 (ElfW(Sword))v < -(ElfW(Sword))0x8000)
939 ret = obj_reloc_overflow;
941 *(unsigned short *)loc = v;
945 if ((ElfW(Sword))v > 0x7f ||
946 (ElfW(Sword))v < -(ElfW(Sword))0x80)
947 ret = obj_reloc_overflow;
949 *(unsigned char *)loc = v;
952 #elif defined(__i386__)
988 #elif defined(__mc68000__)
999 ret = obj_reloc_overflow;
1006 ret = obj_reloc_overflow;
1013 if ((ElfW(Sword))v > 0x7f ||
1014 (ElfW(Sword))v < -(ElfW(Sword))0x80) {
1015 ret = obj_reloc_overflow;
1022 if ((ElfW(Sword))v > 0x7fff ||
1023 (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
1024 ret = obj_reloc_overflow;
1030 *(int *)loc = v - dot;
1033 case R_68K_GLOB_DAT:
1034 case R_68K_JMP_SLOT:
1038 case R_68K_RELATIVE:
1039 *(int *)loc += f->baseaddr;
1045 # ifdef R_68K_GOTOFF
1052 #elif defined(__mips__)
1063 ret = obj_reloc_dangerous;
1064 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1065 ret = obj_reloc_overflow;
1067 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1073 struct mips_hi16 *n;
1075 /* We cannot relocate this one now because we don't know the value
1076 of the carry we need to add. Save the information, and let LO16
1077 do the actual relocation. */
1078 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1081 n->next = ifile->mips_hi16_list;
1082 ifile->mips_hi16_list = n;
1088 unsigned long insnlo = *loc;
1089 ElfW(Addr) val, vallo;
1091 /* Sign extend the addend we extract from the lo insn. */
1092 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1094 if (ifile->mips_hi16_list != NULL) {
1095 struct mips_hi16 *l;
1097 l = ifile->mips_hi16_list;
1099 struct mips_hi16 *next;
1102 /* The value for the HI16 had best be the same. */
1103 assert(v == l->value);
1105 /* Do the HI16 relocation. Note that we actually don't
1106 need to know anything about the LO16 itself, except where
1107 to find the low 16 bits of the addend needed by the LO16. */
1110 ((insn & 0xffff) << 16) +
1114 /* Account for the sign extension that will happen in the
1121 insn = (insn & ~0xffff) | val;
1129 ifile->mips_hi16_list = NULL;
1132 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1134 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1139 #elif defined(__nios2__)
1144 case R_NIOS2_BFD_RELOC_32:
1148 case R_NIOS2_BFD_RELOC_16:
1150 ret = obj_reloc_overflow;
1155 case R_NIOS2_BFD_RELOC_8:
1157 ret = obj_reloc_overflow;
1166 if ((Elf32_Sword)v > 0x7fff ||
1167 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1168 ret = obj_reloc_overflow;
1172 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1182 ret = obj_reloc_overflow;
1186 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1191 case R_NIOS2_PCREL16:
1196 if ((Elf32_Sword)v > 0x7fff ||
1197 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1198 ret = obj_reloc_overflow;
1202 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1208 Elf32_Addr word, gp;
1210 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1212 if ((Elf32_Sword)v > 0x7fff ||
1213 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1214 ret = obj_reloc_overflow;
1218 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
1222 case R_NIOS2_CALL26:
1224 ret = obj_reloc_dangerous;
1225 if ((v >> 28) != (dot >> 28))
1226 ret = obj_reloc_overflow;
1227 *loc = (*loc & 0x3f) | ((v >> 2) << 6);
1235 ret = obj_reloc_overflow;
1238 word = *loc & ~0x7c0;
1239 *loc = word | ((v & 0x1f) << 6);
1248 ret = obj_reloc_overflow;
1251 word = *loc & ~0xfc0;
1252 *loc = word | ((v & 0x3f) << 6);
1261 ret = obj_reloc_overflow;
1264 word = *loc & ~0x3fc0;
1265 *loc = word | ((v & 0xff) << 6);
1274 *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
1284 *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
1289 case R_NIOS2_HIADJ16:
1291 Elf32_Addr word1, word2;
1294 word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
1295 *loc = ((((word1 >> 22) << 16) | word2) << 6) |
1300 #elif defined(__powerpc64__)
1301 /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
1303 #elif defined(__powerpc__)
1305 case R_PPC_ADDR16_HA:
1306 *(unsigned short *)loc = (v + 0x8000) >> 16;
1309 case R_PPC_ADDR16_HI:
1310 *(unsigned short *)loc = v >> 16;
1313 case R_PPC_ADDR16_LO:
1314 *(unsigned short *)loc = v;
1328 #elif defined(__s390__)
1331 *(unsigned int *) loc += v;
1334 *(unsigned short *) loc += v;
1337 *(unsigned char *) loc += v;
1341 *(unsigned int *) loc += v - dot;
1344 *(unsigned short *) loc += (v - dot) >> 1;
1347 *(unsigned short *) loc += v - dot;
1351 case R_390_PLT16DBL:
1352 /* find the plt entry and initialize it. */
1353 assert(isym != NULL);
1354 pe = (struct arch_single_entry *) &isym->pltent;
1355 assert(pe->allocated);
1356 if (pe->inited == 0) {
1357 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
1358 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
1360 if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1367 /* Insert relative distance to target. */
1368 v = plt + pe->offset - dot;
1369 if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
1370 *(unsigned int *) loc = (unsigned int) v;
1371 else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
1372 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
1375 case R_390_GLOB_DAT:
1376 case R_390_JMP_SLOT:
1380 case R_390_RELATIVE:
1381 *loc += f->baseaddr;
1386 *(unsigned long *) loc += got - dot;
1392 assert(isym != NULL);
1394 if (!isym->gotent.inited)
1396 isym->gotent.inited = 1;
1397 *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
1399 if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
1400 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
1401 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
1402 *(unsigned short *) loc += isym->gotent.offset;
1403 else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
1404 *(unsigned int *) loc += isym->gotent.offset;
1407 # ifndef R_390_GOTOFF32
1408 # define R_390_GOTOFF32 R_390_GOTOFF
1410 case R_390_GOTOFF32:
1415 #elif defined(__sh__)
1438 *loc = f->baseaddr + rel->r_addend;
1443 *loc = got - dot + rel->r_addend;
1454 # if defined(__SH5__)
1455 case R_SH_IMM_MEDLOW16:
1456 case R_SH_IMM_LOW16:
1460 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
1464 * movi and shori have the format:
1466 * | op | imm | reg | reserved |
1467 * 31..26 25..10 9.. 4 3 .. 0
1469 * so we simply mask and or in imm.
1471 word = *loc & ~0x3fffc00;
1472 word |= (v & 0xffff) << 10;
1479 case R_SH_IMM_MEDLOW16_PCREL:
1480 case R_SH_IMM_LOW16_PCREL:
1484 word = *loc & ~0x3fffc00;
1488 if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
1491 word |= (v & 0xffff) << 10;
1497 # endif /* __SH5__ */
1499 #elif defined (__v850e__)
1505 /* We write two shorts instead of a long because even
1506 32-bit insns only need half-word alignment, but
1507 32-bit data needs to be long-word aligned. */
1508 v += ((unsigned short *)loc)[0];
1509 v += ((unsigned short *)loc)[1] << 16;
1510 ((unsigned short *)loc)[0] = v & 0xffff;
1511 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1514 case R_V850_22_PCREL:
1517 #elif defined(__x86_64__)
1527 *(unsigned int *) loc += v;
1530 ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
1531 /* error("Possibly is module compiled without -mcmodel=kernel!"); */
1536 *(signed int *) loc += v;
1540 *(unsigned short *) loc += v;
1544 *(unsigned char *) loc += v;
1548 *(unsigned int *) loc += v - dot;
1552 *(unsigned short *) loc += v - dot;
1556 *(unsigned char *) loc += v - dot;
1559 case R_X86_64_GLOB_DAT:
1560 case R_X86_64_JUMP_SLOT:
1564 case R_X86_64_RELATIVE:
1565 *loc += f->baseaddr;
1568 case R_X86_64_GOT32:
1569 case R_X86_64_GOTPCREL:
1572 assert(isym != NULL);
1573 if (!isym->gotent.reloc_done)
1575 isym->gotent.reloc_done = 1;
1576 *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
1578 /* XXX are these really correct? */
1579 if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
1580 *(unsigned int *) loc += v + isym->gotent.offset;
1582 *loc += isym->gotent.offset;
1587 # warning "no idea how to handle relocations on your arch"
1591 printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
1592 ret = obj_reloc_unhandled;
1595 #if defined(CONFIG_USE_PLT_ENTRIES)
1599 /* find the plt entry and initialize it if necessary */
1600 assert(isym != NULL);
1602 #if defined(CONFIG_USE_PLT_LIST)
1603 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1611 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1613 /* generate some machine code */
1615 #if defined(__arm__)
1616 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1617 ip[1] = v; /* sym@ */
1619 #if defined(__powerpc__)
1620 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1621 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1622 ip[2] = 0x7d6903a6; /* mtctr r11 */
1623 ip[3] = 0x4e800420; /* bctr */
1625 #if defined (__v850e__)
1626 /* We have to trash a register, so we assume that any control
1627 transfer more than 21-bits away must be a function call
1628 (so we can use a call-clobbered register). */
1629 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1630 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1635 /* relative distance to target */
1637 /* if the target is too far away.... */
1638 #if defined (__arm__) || defined (__powerpc__)
1639 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1640 #elif defined (__v850e__)
1641 if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
1643 /* go via the plt */
1644 v = plt + pe->offset - dot;
1646 #if defined (__v850e__)
1651 ret = obj_reloc_dangerous;
1653 /* merge the offset into the instruction. */
1654 #if defined(__arm__)
1655 /* Convert to words. */
1658 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1660 #if defined(__powerpc__)
1661 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1663 #if defined (__v850e__)
1664 /* We write two shorts instead of a long because even 32-bit insns
1665 only need half-word alignment, but the 32-bit data write needs
1666 to be long-word aligned. */
1667 ((unsigned short *)loc)[0] =
1668 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1669 | ((v >> 16) & 0x3f); /* offs high part */
1670 ((unsigned short *)loc)[1] =
1671 (v & 0xffff); /* offs low part */
1674 #endif /* CONFIG_USE_PLT_ENTRIES */
1676 #if defined(CONFIG_USE_GOT_ENTRIES)
1679 assert(isym != NULL);
1680 /* needs an entry in the .got: set it, once */
1681 if (!isym->gotent.inited) {
1682 isym->gotent.inited = 1;
1683 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1685 /* make the reloc with_respect_to_.got */
1687 *loc += isym->gotent.offset + rel->r_addend;
1688 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1689 *loc += isym->gotent.offset;
1693 #endif /* CONFIG_USE_GOT_ENTRIES */
1700 #if defined(CONFIG_USE_LIST)
1702 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1703 int offset, int size)
1705 struct arch_list_entry *pe;
1707 for (pe = *list; pe != NULL; pe = pe->next) {
1708 if (pe->addend == rel->r_addend) {
1714 pe = xmalloc(sizeof(struct arch_list_entry));
1716 pe->addend = rel->r_addend;
1717 pe->offset = offset;
1727 #if defined(CONFIG_USE_SINGLE)
1729 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1730 int offset, int size)
1732 if (single->allocated == 0) {
1733 single->allocated = 1;
1734 single->offset = offset;
1743 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1745 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1746 int offset, int size)
1748 struct obj_section *myrelsec = obj_find_section(f, name);
1755 obj_extend_section(myrelsec, offset);
1757 myrelsec = obj_create_alloced_section(f, name,
1767 static void arch_create_got(struct obj_file *f)
1769 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1770 struct arch_file *ifile = (struct arch_file *) f;
1772 #if defined(CONFIG_USE_GOT_ENTRIES)
1773 int got_offset = 0, got_needed = 0, got_allocate;
1775 #if defined(CONFIG_USE_PLT_ENTRIES)
1776 int plt_offset = 0, plt_needed = 0, plt_allocate;
1778 struct obj_section *relsec, *symsec, *strsec;
1779 ElfW(RelM) *rel, *relend;
1780 ElfW(Sym) *symtab, *extsym;
1781 const char *strtab, *name;
1782 struct arch_symbol *intsym;
1784 for (i = 0; i < f->header.e_shnum; ++i) {
1785 relsec = f->sections[i];
1786 if (relsec->header.sh_type != SHT_RELM)
1789 symsec = f->sections[relsec->header.sh_link];
1790 strsec = f->sections[symsec->header.sh_link];
1792 rel = (ElfW(RelM) *) relsec->contents;
1793 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1794 symtab = (ElfW(Sym) *) symsec->contents;
1795 strtab = (const char *) strsec->contents;
1797 for (; rel < relend; ++rel) {
1798 extsym = &symtab[ELF_R_SYM(rel->r_info)];
1800 #if defined(CONFIG_USE_GOT_ENTRIES)
1803 #if defined(CONFIG_USE_PLT_ENTRIES)
1807 switch (ELF_R_TYPE(rel->r_info)) {
1808 #if defined(__arm__)
1823 #elif defined(__i386__)
1833 #elif defined(__powerpc__)
1838 #elif defined(__mc68000__)
1849 #elif defined(__sh__)
1859 #elif defined (__v850e__)
1860 case R_V850_22_PCREL:
1869 if (extsym->st_name != 0) {
1870 name = strtab + extsym->st_name;
1872 name = f->sections[extsym->st_shndx]->name;
1874 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1875 #if defined(CONFIG_USE_GOT_ENTRIES)
1877 got_offset += arch_single_init(
1878 rel, &intsym->gotent,
1879 got_offset, CONFIG_GOT_ENTRY_SIZE);
1884 #if defined(CONFIG_USE_PLT_ENTRIES)
1886 #if defined(CONFIG_USE_PLT_LIST)
1887 plt_offset += arch_list_add(
1888 rel, &intsym->pltent,
1889 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1891 plt_offset += arch_single_init(
1892 rel, &intsym->pltent,
1893 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1901 #if defined(CONFIG_USE_GOT_ENTRIES)
1903 ifile->got = arch_xsect_init(f, ".got", got_offset,
1904 CONFIG_GOT_ENTRY_SIZE);
1908 #if defined(CONFIG_USE_PLT_ENTRIES)
1910 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1911 CONFIG_PLT_ENTRY_SIZE);
1915 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1918 /*======================================================================*/
1920 /* Standard ELF hash function. */
1921 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1923 unsigned long h = 0;
1930 if ((g = (h & 0xf0000000)) != 0) {
1939 static unsigned long obj_elf_hash(const char *name)
1941 return obj_elf_hash_n(name, strlen(name));
1944 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1945 /* String comparison for non-co-versioned kernel and module. */
1947 static int ncv_strcmp(const char *a, const char *b)
1949 size_t alen = strlen(a), blen = strlen(b);
1951 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1952 return strncmp(a, b, alen);
1953 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1954 return strncmp(a, b, blen);
1956 return strcmp(a, b);
1959 /* String hashing for non-co-versioned kernel and module. Here
1960 we are simply forced to drop the crc from the hash. */
1962 static unsigned long ncv_symbol_hash(const char *str)
1964 size_t len = strlen(str);
1965 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1967 return obj_elf_hash_n(str, len);
1971 obj_set_symbol_compare(struct obj_file *f,
1972 int (*cmp) (const char *, const char *),
1973 unsigned long (*hash) (const char *))
1976 f->symbol_cmp = cmp;
1978 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1981 f->symbol_hash = hash;
1983 memcpy(tmptab, f->symtab, sizeof(tmptab));
1984 memset(f->symtab, 0, sizeof(f->symtab));
1986 for (i = 0; i < HASH_BUCKETS; ++i)
1987 for (sym = tmptab[i]; sym; sym = next) {
1988 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1990 sym->next = f->symtab[h];
1996 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1998 static struct obj_symbol *
1999 obj_add_symbol(struct obj_file *f, const char *name,
2000 unsigned long symidx, int info,
2001 int secidx, ElfW(Addr) value,
2004 struct obj_symbol *sym;
2005 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2006 int n_type = ELF_ST_TYPE(info);
2007 int n_binding = ELF_ST_BIND(info);
2009 for (sym = f->symtab[hash]; sym; sym = sym->next)
2010 if (f->symbol_cmp(sym->name, name) == 0) {
2011 int o_secidx = sym->secidx;
2012 int o_info = sym->info;
2013 int o_type = ELF_ST_TYPE(o_info);
2014 int o_binding = ELF_ST_BIND(o_info);
2016 /* A redefinition! Is it legal? */
2018 if (secidx == SHN_UNDEF)
2020 else if (o_secidx == SHN_UNDEF)
2022 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
2023 /* Cope with local and global symbols of the same name
2024 in the same object file, as might have been created
2025 by ld -r. The only reason locals are now seen at this
2026 level at all is so that we can do semi-sensible things
2029 struct obj_symbol *nsym, **p;
2031 nsym = arch_new_symbol();
2032 nsym->next = sym->next;
2035 /* Excise the old (local) symbol from the hash chain. */
2036 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
2040 } else if (n_binding == STB_LOCAL) {
2041 /* Another symbol of the same name has already been defined.
2042 Just add this to the local table. */
2043 sym = arch_new_symbol();
2046 f->local_symtab[symidx] = sym;
2048 } else if (n_binding == STB_WEAK)
2050 else if (o_binding == STB_WEAK)
2052 /* Don't unify COMMON symbols with object types the programmer
2054 else if (secidx == SHN_COMMON
2055 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
2057 else if (o_secidx == SHN_COMMON
2058 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
2061 /* Don't report an error if the symbol is coming from
2062 the kernel or some external module. */
2063 if (secidx <= SHN_HIRESERVE)
2064 bb_error_msg("%s multiply defined", name);
2069 /* Completely new symbol. */
2070 sym = arch_new_symbol();
2071 sym->next = f->symtab[hash];
2072 f->symtab[hash] = sym;
2075 if (ELF_ST_BIND(info) == STB_LOCAL && symidx != -1) {
2076 if (symidx >= f->local_symtab_size)
2077 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
2078 name, (long) symidx, (long) f->local_symtab_size);
2080 f->local_symtab[symidx] = sym;
2087 sym->secidx = secidx;
2093 static struct obj_symbol *
2094 obj_find_symbol(struct obj_file *f, const char *name)
2096 struct obj_symbol *sym;
2097 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
2099 for (sym = f->symtab[hash]; sym; sym = sym->next)
2100 if (f->symbol_cmp(sym->name, name) == 0)
2107 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
2110 if (sym->secidx >= SHN_LORESERVE)
2113 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2115 /* As a special case, a NULL sym has value zero. */
2120 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
2122 int i, n = f->header.e_shnum;
2124 for (i = 0; i < n; ++i)
2125 if (strcmp(f->sections[i]->name, name) == 0)
2126 return f->sections[i];
2131 static int obj_load_order_prio(struct obj_section *a)
2133 unsigned long af, ac;
2135 af = a->header.sh_flags;
2138 if (a->name[0] != '.' || strlen(a->name) != 10 ||
2139 strcmp(a->name + 5, ".init"))
2143 if (!(af & SHF_WRITE))
2145 if (af & SHF_EXECINSTR)
2147 if (a->header.sh_type != SHT_NOBITS)
2154 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
2156 struct obj_section **p;
2157 int prio = obj_load_order_prio(sec);
2158 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
2159 if (obj_load_order_prio(*p) < prio)
2161 sec->load_next = *p;
2165 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
2167 unsigned long align,
2170 int newidx = f->header.e_shnum++;
2171 struct obj_section *sec;
2173 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2174 f->sections[newidx] = sec = arch_new_section();
2176 memset(sec, 0, sizeof(*sec));
2177 sec->header.sh_type = SHT_PROGBITS;
2178 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2179 sec->header.sh_size = size;
2180 sec->header.sh_addralign = align;
2184 sec->contents = xmalloc(size);
2186 obj_insert_section_load_order(f, sec);
2191 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
2193 unsigned long align,
2196 int newidx = f->header.e_shnum++;
2197 struct obj_section *sec;
2199 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
2200 f->sections[newidx] = sec = arch_new_section();
2202 memset(sec, 0, sizeof(*sec));
2203 sec->header.sh_type = SHT_PROGBITS;
2204 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2205 sec->header.sh_size = size;
2206 sec->header.sh_addralign = align;
2210 sec->contents = xmalloc(size);
2212 sec->load_next = f->load_order;
2213 f->load_order = sec;
2214 if (f->load_order_search_start == &f->load_order)
2215 f->load_order_search_start = &sec->load_next;
2220 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
2222 unsigned long oldsize = sec->header.sh_size;
2224 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
2226 return sec->contents + oldsize;
2230 /* Conditionally add the symbols from the given symbol set to the
2234 add_symbols_from( struct obj_file *f,
2235 int idx, struct new_module_symbol *syms, size_t nsyms)
2237 struct new_module_symbol *s;
2240 #ifdef SYMBOL_PREFIX
2242 size_t name_alloced_size = 0;
2244 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2247 gpl = obj_gpl_license(f, NULL) == 0;
2249 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
2250 /* Only add symbols that are already marked external.
2251 If we override locals we may cause problems for
2252 argument initialization. We will also create a false
2253 dependency on the module. */
2254 struct obj_symbol *sym;
2257 /* GPL licensed modules can use symbols exported with
2258 * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
2259 * exported names. Non-GPL modules never see any GPLONLY_
2260 * symbols so they cannot fudge it by adding the prefix on
2263 if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
2264 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
2271 name = (char *)s->name;
2273 #ifdef SYMBOL_PREFIX
2274 /* Prepend SYMBOL_PREFIX to the symbol's name (the
2275 kernel exports `C names', but module object files
2276 reference `linker names'). */
2277 size_t extra = sizeof SYMBOL_PREFIX;
2278 size_t name_size = strlen (name) + extra;
2279 if (name_size > name_alloced_size) {
2280 name_alloced_size = name_size * 2;
2281 name_buf = alloca (name_alloced_size);
2283 strcpy (name_buf, SYMBOL_PREFIX);
2284 strcpy (name_buf + extra - 1, name);
2286 #endif /* SYMBOL_PREFIX */
2288 sym = obj_find_symbol(f, name);
2289 if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
2290 #ifdef SYMBOL_PREFIX
2291 /* Put NAME_BUF into more permanent storage. */
2292 name = xmalloc (name_size);
2293 strcpy (name, name_buf);
2295 sym = obj_add_symbol(f, name, -1,
2296 ELF_ST_INFO(STB_GLOBAL,
2299 /* Did our symbol just get installed? If so, mark the
2300 module as "used". */
2301 if (sym->secidx == idx)
2309 static void add_kernel_symbols(struct obj_file *f)
2311 struct external_module *m;
2314 /* Add module symbols first. */
2316 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
2318 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
2319 m->nsyms)) m->used = 1, ++nused;
2321 n_ext_modules_used = nused;
2323 /* And finally the symbols from the kernel proper. */
2326 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
2329 static char *get_modinfo_value(struct obj_file *f, const char *key)
2331 struct obj_section *sec;
2332 char *p, *v, *n, *ep;
2333 size_t klen = strlen(key);
2335 sec = obj_find_section(f, ".modinfo");
2339 ep = p + sec->header.sh_size;
2342 n = strchr(p, '\0');
2344 if (p + klen == v && strncmp(p, key, klen) == 0)
2347 if (p + klen == n && strcmp(p, key) == 0)
2357 /*======================================================================*/
2358 /* Functions relating to module loading after 2.1.18. */
2361 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2364 char *p, *q, *key, *sym_name;
2365 struct obj_symbol *sym;
2366 char *contents, *loc;
2370 if ((q = strchr(p, '=')) == NULL) {
2375 key = alloca(q - p + 6);
2376 memcpy(key, "parm_", 5);
2377 memcpy(key + 5, p, q - p);
2380 p = get_modinfo_value(f, key);
2383 bb_error_msg("invalid parameter %s", key);
2387 #ifdef SYMBOL_PREFIX
2388 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2389 strcpy (sym_name, SYMBOL_PREFIX);
2390 strcat (sym_name, key);
2394 sym = obj_find_symbol(f, sym_name);
2396 /* Also check that the parameter was not resolved from the kernel. */
2397 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2398 bb_error_msg("symbol for parameter %s not found", key);
2403 min = strtoul(p, &p, 10);
2405 max = strtoul(p + 1, &p, 10);
2411 contents = f->sections[sym->secidx]->contents;
2412 loc = contents + sym->value;
2416 if ((*p == 's') || (*p == 'c')) {
2419 /* Do C quoting if we begin with a ", else slurp the lot. */
2423 str = alloca(strlen(q));
2424 for (r = str, q++; *q != '"'; ++q, ++r) {
2426 bb_error_msg("improperly terminated string argument for %s",
2429 } else if (*q == '\\')
2463 if (q[1] >= '0' && q[1] <= '7') {
2464 c = (c * 8) + *++q - '0';
2465 if (q[1] >= '0' && q[1] <= '7')
2466 c = (c * 8) + *++q - '0';
2483 /* In this case, the string is not quoted. We will break
2484 it using the coma (like for ints). If the user wants to
2485 include comas in a string, he just has to quote it */
2487 /* Search the next coma */
2491 if (r != (char *) NULL) {
2492 /* Recopy the current field */
2493 str = alloca(r - q + 1);
2494 memcpy(str, q, r - q);
2496 /* I don't know if it is useful, as the previous case
2497 doesn't nul terminate the string ??? */
2500 /* Keep next fields */
2511 obj_string_patch(f, sym->secidx, loc - contents, str);
2512 loc += tgt_sizeof_char_p;
2514 /* Array of chars (in fact, matrix !) */
2515 unsigned long charssize; /* size of each member */
2517 /* Get the size of each member */
2518 /* Probably we should do that outside the loop ? */
2519 if (!isdigit(*(p + 1))) {
2520 bb_error_msg("parameter type 'c' for %s must be followed by"
2521 " the maximum size", key);
2524 charssize = strtoul(p + 1, (char **) NULL, 10);
2527 if (strlen(str) >= charssize) {
2528 bb_error_msg("string too long for %s (max %ld)", key,
2533 /* Copy to location */
2534 strcpy((char *) loc, str);
2538 long v = strtoul(q, &q, 0);
2545 loc += tgt_sizeof_short;
2549 loc += tgt_sizeof_int;
2553 loc += tgt_sizeof_long;
2557 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2572 goto retry_end_of_value;
2576 bb_error_msg("too many values for %s (max %d)", key, max);
2583 bb_error_msg("invalid argument syntax for %s", key);
2590 bb_error_msg("too few values for %s (min %d)", key, min);
2600 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2601 static int new_is_module_checksummed(struct obj_file *f)
2603 const char *p = get_modinfo_value(f, "using_checksums");
2610 /* Get the module's kernel version in the canonical integer form. */
2613 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2618 p = get_modinfo_value(f, "kernel_version");
2621 safe_strncpy(str, p, STRVERSIONLEN);
2623 a = strtoul(p, &p, 10);
2626 b = strtoul(p + 1, &p, 10);
2629 c = strtoul(p + 1, &q, 10);
2633 return a << 16 | b << 8 | c;
2636 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2639 /* Fetch the loaded modules, and all currently exported symbols. */
2641 static int new_get_kernel_symbols(void)
2643 char *module_names, *mn;
2644 struct external_module *modules, *m;
2645 struct new_module_symbol *syms, *s;
2646 size_t ret, bufsize, nmod, nsyms, i, j;
2648 /* Collect the loaded modules. */
2650 module_names = xmalloc(bufsize = 256);
2652 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2653 if (errno == ENOSPC && bufsize < ret) {
2654 module_names = xrealloc(module_names, bufsize = ret);
2655 goto retry_modules_load;
2657 bb_perror_msg("QM_MODULES");
2661 n_ext_modules = nmod = ret;
2663 /* Collect the modules' symbols. */
2666 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2667 memset(modules, 0, nmod * sizeof(*modules));
2668 for (i = 0, mn = module_names, m = modules;
2669 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2670 struct new_module_info info;
2672 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2673 if (errno == ENOENT) {
2674 /* The module was removed out from underneath us. */
2677 bb_perror_msg("query_module: QM_INFO: %s", mn);
2681 syms = xmalloc(bufsize = 1024);
2683 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2686 syms = xrealloc(syms, bufsize = ret);
2687 goto retry_mod_sym_load;
2689 /* The module was removed out from underneath us. */
2692 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2699 m->addr = info.addr;
2703 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2704 s->name += (unsigned long) syms;
2709 /* Collect the kernel's symbols. */
2711 syms = xmalloc(bufsize = 16 * 1024);
2712 retry_kern_sym_load:
2713 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2714 if (errno == ENOSPC && bufsize < ret) {
2715 syms = xrealloc(syms, bufsize = ret);
2716 goto retry_kern_sym_load;
2718 bb_perror_msg("kernel: QM_SYMBOLS");
2721 nksyms = nsyms = ret;
2724 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2725 s->name += (unsigned long) syms;
2731 /* Return the kernel symbol checksum version, or zero if not used. */
2733 static int new_is_kernel_checksummed(void)
2735 struct new_module_symbol *s;
2738 /* Using_Versions is not the first symbol, but it should be in there. */
2740 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2741 if (strcmp((char *) s->name, "Using_Versions") == 0)
2748 static int new_create_this_module(struct obj_file *f, const char *m_name)
2750 struct obj_section *sec;
2752 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2753 sizeof(struct new_module));
2754 memset(sec->contents, 0, sizeof(struct new_module));
2756 obj_add_symbol(f, SPFX "__this_module", -1,
2757 ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
2758 sizeof(struct new_module));
2760 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2766 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2767 /* add an entry to the __ksymtab section, creating it if necessary */
2768 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2770 struct obj_section *sec;
2773 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2774 * If __ksymtab is defined but not marked alloc, x out the first character
2775 * (no obj_delete routine) and create a new __ksymtab with the correct
2778 sec = obj_find_section(f, "__ksymtab");
2779 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2780 *((char *)(sec->name)) = 'x'; /* override const */
2784 sec = obj_create_alloced_section(f, "__ksymtab",
2785 tgt_sizeof_void_p, 0);
2788 sec->header.sh_flags |= SHF_ALLOC;
2789 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2791 ofs = sec->header.sh_size;
2792 obj_symbol_patch(f, sec->idx, ofs, sym);
2793 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2794 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2796 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2798 static int new_create_module_ksymtab(struct obj_file *f)
2800 struct obj_section *sec;
2803 /* We must always add the module references. */
2805 if (n_ext_modules_used) {
2806 struct new_module_ref *dep;
2807 struct obj_symbol *tm;
2809 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2810 (sizeof(struct new_module_ref)
2811 * n_ext_modules_used));
2815 tm = obj_find_symbol(f, SPFX "__this_module");
2816 dep = (struct new_module_ref *) sec->contents;
2817 for (i = 0; i < n_ext_modules; ++i)
2818 if (ext_modules[i].used) {
2819 dep->dep = ext_modules[i].addr;
2820 obj_symbol_patch(f, sec->idx,
2821 (char *) &dep->ref - sec->contents, tm);
2827 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2832 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2835 /* We don't want to export symbols residing in sections that
2836 aren't loaded. There are a number of these created so that
2837 we make sure certain module options don't appear twice. */
2839 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2841 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2843 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2844 struct obj_symbol *sym;
2845 for (sym = f->symtab[i]; sym; sym = sym->next)
2846 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2847 && sym->secidx <= SHN_HIRESERVE
2848 && (sym->secidx >= SHN_LORESERVE
2849 || loaded[sym->secidx])) {
2850 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2852 obj_symbol_patch(f, sec->idx, ofs, sym);
2853 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2860 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2868 new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2870 struct new_module *module;
2871 struct obj_section *sec;
2876 sec = obj_find_section(f, ".this");
2877 if (!sec || !sec->contents) {
2878 bb_perror_msg_and_die("corrupt module %s?",m_name);
2880 module = (struct new_module *) sec->contents;
2881 m_addr = sec->header.sh_addr;
2883 module->size_of_struct = sizeof(*module);
2884 module->size = m_size;
2885 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2887 sec = obj_find_section(f, "__ksymtab");
2888 if (sec && sec->header.sh_size) {
2889 module->syms = sec->header.sh_addr;
2890 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2893 if (n_ext_modules_used) {
2894 sec = obj_find_section(f, ".kmodtab");
2895 module->deps = sec->header.sh_addr;
2896 module->ndeps = n_ext_modules_used;
2900 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2902 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2904 sec = obj_find_section(f, "__ex_table");
2906 module->ex_table_start = sec->header.sh_addr;
2907 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2910 sec = obj_find_section(f, ".text.init");
2912 module->runsize = sec->header.sh_addr - m_addr;
2914 sec = obj_find_section(f, ".data.init");
2916 if (!module->runsize ||
2917 module->runsize > sec->header.sh_addr - m_addr)
2918 module->runsize = sec->header.sh_addr - m_addr;
2920 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2921 if (sec && sec->header.sh_size) {
2922 module->archdata_start = (void*)sec->header.sh_addr;
2923 module->archdata_end = module->archdata_start + sec->header.sh_size;
2925 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2926 if (sec && sec->header.sh_size) {
2927 module->kallsyms_start = (void*)sec->header.sh_addr;
2928 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2931 /* Whew! All of the initialization is complete. Collect the final
2932 module image and give it to the kernel. */
2934 image = xmalloc(m_size);
2935 obj_create_image(f, image);
2937 ret = init_module(m_name, (struct new_module *) image);
2939 bb_perror_msg("init_module: %s", m_name);
2947 /*======================================================================*/
2950 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2953 struct obj_string_patch *p;
2954 struct obj_section *strsec;
2955 size_t len = strlen(string) + 1;
2958 p = xmalloc(sizeof(*p));
2959 p->next = f->string_patches;
2960 p->reloc_secidx = secidx;
2961 p->reloc_offset = offset;
2962 f->string_patches = p;
2964 strsec = obj_find_section(f, ".kstrtab");
2965 if (strsec == NULL) {
2966 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2967 p->string_offset = 0;
2968 loc = strsec->contents;
2970 p->string_offset = strsec->header.sh_size;
2971 loc = obj_extend_section(strsec, len);
2973 memcpy(loc, string, len);
2979 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2980 struct obj_symbol *sym)
2982 struct obj_symbol_patch *p;
2984 p = xmalloc(sizeof(*p));
2985 p->next = f->symbol_patches;
2986 p->reloc_secidx = secidx;
2987 p->reloc_offset = offset;
2989 f->symbol_patches = p;
2994 static int obj_check_undefineds(struct obj_file *f)
2999 for (i = 0; i < HASH_BUCKETS; ++i) {
3000 struct obj_symbol *sym;
3001 for (sym = f->symtab[i]; sym; sym = sym->next)
3002 if (sym->secidx == SHN_UNDEF) {
3003 if (ELF_ST_BIND(sym->info) == STB_WEAK) {
3004 sym->secidx = SHN_ABS;
3008 bb_error_msg("unresolved symbol %s", sym->name);
3018 static void obj_allocate_commons(struct obj_file *f)
3020 struct common_entry {
3021 struct common_entry *next;
3022 struct obj_symbol *sym;
3023 } *common_head = NULL;
3027 for (i = 0; i < HASH_BUCKETS; ++i) {
3028 struct obj_symbol *sym;
3029 for (sym = f->symtab[i]; sym; sym = sym->next)
3030 if (sym->secidx == SHN_COMMON) {
3031 /* Collect all COMMON symbols and sort them by size so as to
3032 minimize space wasted by alignment requirements. */
3034 struct common_entry **p, *n;
3035 for (p = &common_head; *p; p = &(*p)->next)
3036 if (sym->size <= (*p)->sym->size)
3039 n = alloca(sizeof(*n));
3047 for (i = 1; i < f->local_symtab_size; ++i) {
3048 struct obj_symbol *sym = f->local_symtab[i];
3049 if (sym && sym->secidx == SHN_COMMON) {
3050 struct common_entry **p, *n;
3051 for (p = &common_head; *p; p = &(*p)->next)
3052 if (sym == (*p)->sym)
3054 else if (sym->size < (*p)->sym->size) {
3055 n = alloca(sizeof(*n));
3065 /* Find the bss section. */
3066 for (i = 0; i < f->header.e_shnum; ++i)
3067 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3070 /* If for some reason there hadn't been one, create one. */
3071 if (i == f->header.e_shnum) {
3072 struct obj_section *sec;
3074 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3075 f->sections[i] = sec = arch_new_section();
3076 f->header.e_shnum = i + 1;
3078 memset(sec, 0, sizeof(*sec));
3079 sec->header.sh_type = SHT_PROGBITS;
3080 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3085 /* Allocate the COMMONS. */
3087 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3088 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3089 struct common_entry *c;
3091 for (c = common_head; c; c = c->next) {
3092 ElfW(Addr) align = c->sym->value;
3094 if (align > max_align)
3096 if (bss_size & (align - 1))
3097 bss_size = (bss_size | (align - 1)) + 1;
3100 c->sym->value = bss_size;
3102 bss_size += c->sym->size;
3105 f->sections[i]->header.sh_size = bss_size;
3106 f->sections[i]->header.sh_addralign = max_align;
3110 /* For the sake of patch relocation and parameter initialization,
3111 allocate zeroed data for NOBITS sections now. Note that after
3112 this we cannot assume NOBITS are really empty. */
3113 for (i = 0; i < f->header.e_shnum; ++i) {
3114 struct obj_section *s = f->sections[i];
3115 if (s->header.sh_type == SHT_NOBITS) {
3116 if (s->header.sh_size != 0)
3117 s->contents = memset(xmalloc(s->header.sh_size),
3118 0, s->header.sh_size);
3122 s->header.sh_type = SHT_PROGBITS;
3127 static unsigned long obj_load_size(struct obj_file *f)
3129 unsigned long dot = 0;
3130 struct obj_section *sec;
3132 /* Finalize the positions of the sections relative to one another. */
3134 for (sec = f->load_order; sec; sec = sec->load_next) {
3137 align = sec->header.sh_addralign;
3138 if (align && (dot & (align - 1)))
3139 dot = (dot | (align - 1)) + 1;
3141 sec->header.sh_addr = dot;
3142 dot += sec->header.sh_size;
3148 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3150 int i, n = f->header.e_shnum;
3153 /* Finalize the addresses of the sections. */
3156 for (i = 0; i < n; ++i)
3157 f->sections[i]->header.sh_addr += base;
3159 /* And iterate over all of the relocations. */
3161 for (i = 0; i < n; ++i) {
3162 struct obj_section *relsec, *symsec, *targsec, *strsec;
3163 ElfW(RelM) * rel, *relend;
3167 relsec = f->sections[i];
3168 if (relsec->header.sh_type != SHT_RELM)
3171 symsec = f->sections[relsec->header.sh_link];
3172 targsec = f->sections[relsec->header.sh_info];
3173 strsec = f->sections[symsec->header.sh_link];
3175 rel = (ElfW(RelM) *) relsec->contents;
3176 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3177 symtab = (ElfW(Sym) *) symsec->contents;
3178 strtab = (const char *) strsec->contents;
3180 for (; rel < relend; ++rel) {
3181 ElfW(Addr) value = 0;
3182 struct obj_symbol *intsym = NULL;
3183 unsigned long symndx;
3184 ElfW(Sym) * extsym = 0;
3187 /* Attempt to find a value to use for this relocation. */
3189 symndx = ELF_R_SYM(rel->r_info);
3191 /* Note we've already checked for undefined symbols. */
3193 extsym = &symtab[symndx];
3194 if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
3195 /* Local symbols we look up in the local table to be sure
3196 we get the one that is really intended. */
3197 intsym = f->local_symtab[symndx];
3199 /* Others we look up in the hash table. */
3201 if (extsym->st_name)
3202 name = strtab + extsym->st_name;
3204 name = f->sections[extsym->st_shndx]->name;
3205 intsym = obj_find_symbol(f, name);
3208 value = obj_symbol_final_value(f, intsym);
3209 intsym->referenced = 1;
3211 #if SHT_RELM == SHT_RELA
3212 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3213 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3214 if (!extsym || !extsym->st_name ||
3215 ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3217 value += rel->r_addend;
3221 switch (arch_apply_relocation
3222 (f, targsec, symsec, intsym, rel, value)) {
3226 case obj_reloc_overflow:
3227 errmsg = "Relocation overflow";
3229 case obj_reloc_dangerous:
3230 errmsg = "Dangerous relocation";
3232 case obj_reloc_unhandled:
3233 errmsg = "Unhandled relocation";
3236 bb_error_msg("%s of type %ld for %s", errmsg,
3237 (long) ELF_R_TYPE(rel->r_info),
3238 strtab + extsym->st_name);
3240 bb_error_msg("%s of type %ld", errmsg,
3241 (long) ELF_R_TYPE(rel->r_info));
3249 /* Finally, take care of the patches. */
3251 if (f->string_patches) {
3252 struct obj_string_patch *p;
3253 struct obj_section *strsec;
3254 ElfW(Addr) strsec_base;
3255 strsec = obj_find_section(f, ".kstrtab");
3256 strsec_base = strsec->header.sh_addr;
3258 for (p = f->string_patches; p; p = p->next) {
3259 struct obj_section *targsec = f->sections[p->reloc_secidx];
3260 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3261 = strsec_base + p->string_offset;
3265 if (f->symbol_patches) {
3266 struct obj_symbol_patch *p;
3268 for (p = f->symbol_patches; p; p = p->next) {
3269 struct obj_section *targsec = f->sections[p->reloc_secidx];
3270 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3271 = obj_symbol_final_value(f, p->sym);
3278 static int obj_create_image(struct obj_file *f, char *image)
3280 struct obj_section *sec;
3281 ElfW(Addr) base = f->baseaddr;
3283 for (sec = f->load_order; sec; sec = sec->load_next) {
3286 if (sec->contents == 0 || sec->header.sh_size == 0)
3289 secimg = image + (sec->header.sh_addr - base);
3291 /* Note that we allocated data for NOBITS sections earlier. */
3292 memcpy(secimg, sec->contents, sec->header.sh_size);
3298 /*======================================================================*/
3300 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3303 ElfW(Shdr) * section_headers;
3307 /* Read the file header. */
3309 f = arch_new_file();
3310 memset(f, 0, sizeof(*f));
3311 f->symbol_cmp = strcmp;
3312 f->symbol_hash = obj_elf_hash;
3313 f->load_order_search_start = &f->load_order;
3315 fseek(fp, 0, SEEK_SET);
3316 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3317 bb_perror_msg("error reading ELF header");
3321 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3322 || f->header.e_ident[EI_MAG1] != ELFMAG1
3323 || f->header.e_ident[EI_MAG2] != ELFMAG2
3324 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3325 bb_error_msg("not an ELF file");
3328 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3329 || f->header.e_ident[EI_DATA] != ELFDATAM
3330 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3331 || !MATCH_MACHINE(f->header.e_machine)) {
3332 bb_error_msg("ELF file not for this architecture");
3335 if (f->header.e_type != ET_REL) {
3336 bb_error_msg("ELF file not a relocatable object");
3340 /* Read the section headers. */
3342 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3343 bb_error_msg("section header size mismatch: %lu != %lu",
3344 (unsigned long) f->header.e_shentsize,
3345 (unsigned long) sizeof(ElfW(Shdr)));
3349 shnum = f->header.e_shnum;
3350 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3351 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3353 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3354 fseek(fp, f->header.e_shoff, SEEK_SET);
3355 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3356 bb_perror_msg("error reading ELF section headers");
3360 /* Read the section data. */
3362 for (i = 0; i < shnum; ++i) {
3363 struct obj_section *sec;
3365 f->sections[i] = sec = arch_new_section();
3366 memset(sec, 0, sizeof(*sec));
3368 sec->header = section_headers[i];
3371 if(sec->header.sh_size) switch (sec->header.sh_type) {
3380 if (!loadprogbits) {
3381 sec->contents = NULL;
3388 if (sec->header.sh_size > 0) {
3389 sec->contents = xmalloc(sec->header.sh_size);
3390 fseek(fp, sec->header.sh_offset, SEEK_SET);
3391 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3392 bb_perror_msg("error reading ELF section data");
3396 sec->contents = NULL;
3400 #if SHT_RELM == SHT_REL
3402 bb_error_msg("RELA relocations not supported on this architecture");
3406 bb_error_msg("REL relocations not supported on this architecture");
3411 if (sec->header.sh_type >= SHT_LOPROC) {
3412 /* Assume processor specific section types are debug
3413 info and can safely be ignored. If this is ever not
3414 the case (Hello MIPS?), don't put ifdefs here but
3415 create an arch_load_proc_section(). */
3419 bb_error_msg("can't handle sections of type %ld",
3420 (long) sec->header.sh_type);
3425 /* Do what sort of interpretation as needed by each section. */
3427 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3429 for (i = 0; i < shnum; ++i) {
3430 struct obj_section *sec = f->sections[i];
3431 sec->name = shstrtab + sec->header.sh_name;
3434 for (i = 0; i < shnum; ++i) {
3435 struct obj_section *sec = f->sections[i];
3437 /* .modinfo should be contents only but gcc has no attribute for that.
3438 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3440 if (strcmp(sec->name, ".modinfo") == 0)
3441 sec->header.sh_flags &= ~SHF_ALLOC;
3443 if (sec->header.sh_flags & SHF_ALLOC)
3444 obj_insert_section_load_order(f, sec);
3446 switch (sec->header.sh_type) {
3449 unsigned long nsym, j;
3453 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3454 bb_error_msg("symbol size mismatch: %lu != %lu",
3455 (unsigned long) sec->header.sh_entsize,
3456 (unsigned long) sizeof(ElfW(Sym)));
3460 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3461 strtab = f->sections[sec->header.sh_link]->contents;
3462 sym = (ElfW(Sym) *) sec->contents;
3464 /* Allocate space for a table of local symbols. */
3465 j = f->local_symtab_size = sec->header.sh_info;
3466 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3468 /* Insert all symbols into the hash table. */
3469 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3470 ElfW(Addr) val = sym->st_value;
3473 name = strtab + sym->st_name;
3474 else if (sym->st_shndx < shnum)
3475 name = f->sections[sym->st_shndx]->name;
3479 #if defined(__SH5__)
3481 * For sh64 it is possible that the target of a branch
3482 * requires a mode switch (32 to 16 and back again).
3484 * This is implied by the lsb being set in the target
3485 * address for SHmedia mode and clear for SHcompact.
3487 val |= sym->st_other & 4;
3490 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3497 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3498 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3499 (unsigned long) sec->header.sh_entsize,
3500 (unsigned long) sizeof(ElfW(RelM)));
3504 /* XXX Relocation code from modutils-2.3.19 is not here.
3505 * Why? That's about 20 lines of code from obj/obj_load.c,
3506 * which gets done in a second pass through the sections.
3507 * This BusyBox insmod does similar work in obj_relocate(). */
3514 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3516 * load the unloaded sections directly into the memory allocated by
3517 * kernel for the module
3520 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3522 ElfW(Addr) base = f->baseaddr;
3523 struct obj_section* sec;
3525 for (sec = f->load_order; sec; sec = sec->load_next) {
3527 /* section already loaded? */
3528 if (sec->contents != NULL)
3531 if (sec->header.sh_size == 0)
3534 sec->contents = imagebase + (sec->header.sh_addr - base);
3535 fseek(fp, sec->header.sh_offset, SEEK_SET);
3536 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3537 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3546 static void hide_special_symbols(struct obj_file *f)
3548 static const char *const specials[] = {
3549 SPFX "cleanup_module",
3551 SPFX "kernel_version",
3555 struct obj_symbol *sym;
3556 const char *const *p;
3558 for (p = specials; *p; ++p)
3559 if ((sym = obj_find_symbol(f, *p)) != NULL)
3561 ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
3565 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3566 static int obj_gpl_license(struct obj_file *f, const char **license)
3568 struct obj_section *sec;
3569 /* This list must match *exactly* the list of allowable licenses in
3570 * linux/include/linux/module.h. Checking for leading "GPL" will not
3571 * work, somebody will use "GPL sucks, this is proprietary".
3573 static const char * const gpl_licenses[] = {
3576 "GPL and additional rights",
3581 if ((sec = obj_find_section(f, ".modinfo"))) {
3582 const char *value, *ptr, *endptr;
3583 ptr = sec->contents;
3584 endptr = ptr + sec->header.sh_size;
3585 while (ptr < endptr) {
3586 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3590 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3591 if (strcmp(value+1, gpl_licenses[i]) == 0)
3596 if (strchr(ptr, '\0'))
3597 ptr = strchr(ptr, '\0') + 1;
3605 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3606 #define TAINT_PROPRIETORY_MODULE (1<<0)
3607 #define TAINT_FORCED_MODULE (1<<1)
3608 #define TAINT_UNSAFE_SMP (1<<2)
3609 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3611 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3612 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3616 static int first = 1;
3617 if (fd < 0 && !kernel_has_tainted)
3618 return; /* New modutils on old kernel */
3619 printf("Warning: loading %s will taint the kernel: %s%s\n",
3620 m_name, text1, text2);
3622 printf(" See %s for information about tainted modules\n", TAINT_URL);
3626 read(fd, buf, sizeof(buf)-1);
3627 buf[sizeof(buf)-1] = '\0';
3628 oldval = strtoul(buf, NULL, 10);
3629 sprintf(buf, "%d\n", oldval | taint);
3630 write(fd, buf, strlen(buf));
3634 /* Check if loading this module will taint the kernel. */
3635 static void check_tainted_module(struct obj_file *f, char *m_name)
3637 static const char tainted_file[] = TAINT_FILENAME;
3638 int fd, kernel_has_tainted;
3641 kernel_has_tainted = 1;
3642 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3643 if (errno == ENOENT)
3644 kernel_has_tainted = 0;
3645 else if (errno == EACCES)
3646 kernel_has_tainted = 1;
3648 perror(tainted_file);
3649 kernel_has_tainted = 0;
3653 switch (obj_gpl_license(f, &ptr)) {
3657 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3660 /* The module has a non-GPL license so we pretend that the
3661 * kernel always has a taint flag to get a warning even on
3662 * kernels without the proc flag.
3664 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3667 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3671 if (flag_force_load)
3672 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3677 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3678 #define check_tainted_module(x, y) do { } while(0);
3679 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3681 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3682 /* add module source, timestamp, kernel version and a symbol for the
3683 * start of some sections. this info is used by ksymoops to do better
3687 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3689 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3690 return new_get_module_version(f, str);
3691 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3692 strncpy(str, "???", sizeof(str));
3694 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3697 /* add module source, timestamp, kernel version and a symbol for the
3698 * start of some sections. this info is used by ksymoops to do better
3702 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3705 static const char symprefix[] = "__insmod_";
3706 struct obj_section *sec;
3707 struct obj_symbol *sym;
3708 char *name, *absolute_filename;
3709 char str[STRVERSIONLEN], real[PATH_MAX];
3710 int i, l, lm_name, lfilename, use_ksymtab, version;
3711 struct stat statbuf;
3713 static const char *section_names[] = {
3721 if (realpath(filename, real)) {
3722 absolute_filename = bb_xstrdup(real);
3725 int save_errno = errno;
3726 bb_error_msg("cannot get realpath for %s", filename);
3729 absolute_filename = bb_xstrdup(filename);
3732 lm_name = strlen(m_name);
3733 lfilename = strlen(absolute_filename);
3735 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3736 * are not to be exported. otherwise leave ksymtab alone for now, the
3737 * "export all symbols" compatibility code will export these symbols later.
3739 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3741 if ((sec = obj_find_section(f, ".this"))) {
3742 /* tag the module header with the object name, last modified
3743 * timestamp and module version. worst case for module version
3744 * is 0xffffff, decimal 16777215. putting all three fields in
3745 * one symbol is less readable but saves kernel space.
3747 l = sizeof(symprefix)+ /* "__insmod_" */
3748 lm_name+ /* module name */
3750 lfilename+ /* object filename */
3752 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3754 8+ /* version in dec */
3757 if (stat(absolute_filename, &statbuf) != 0)
3758 statbuf.st_mtime = 0;
3759 version = get_module_version(f, str); /* -1 if not found */
3760 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3761 symprefix, m_name, absolute_filename,
3762 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3764 sym = obj_add_symbol(f, name, -1,
3765 ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3766 sec->idx, sec->header.sh_addr, 0);
3768 new_add_ksymtab(f, sym);
3770 free(absolute_filename);
3771 #ifdef _NOT_SUPPORTED_
3772 /* record where the persistent data is going, same address as previous symbol */
3775 l = sizeof(symprefix)+ /* "__insmod_" */
3776 lm_name+ /* module name */
3778 strlen(f->persist)+ /* data store */
3781 snprintf(name, l, "%s%s_P%s",
3782 symprefix, m_name, f->persist);
3783 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3784 sec->idx, sec->header.sh_addr, 0);
3786 new_add_ksymtab(f, sym);
3788 #endif /* _NOT_SUPPORTED_ */
3789 /* tag the desired sections if size is non-zero */
3791 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3792 if ((sec = obj_find_section(f, section_names[i])) &&
3793 sec->header.sh_size) {
3794 l = sizeof(symprefix)+ /* "__insmod_" */
3795 lm_name+ /* module name */
3797 strlen(sec->name)+ /* section name */
3799 8+ /* length in dec */
3802 snprintf(name, l, "%s%s_S%s_L%ld",
3803 symprefix, m_name, sec->name,
3804 (long)sec->header.sh_size);
3805 sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
3806 sec->idx, sec->header.sh_addr, 0);
3808 new_add_ksymtab(f, sym);
3812 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3814 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3815 static void print_load_map(struct obj_file *f)
3817 struct obj_symbol *sym;
3818 struct obj_symbol **all, **p;
3819 struct obj_section *sec;
3820 int i, nsyms, *loaded;
3822 /* Report on the section layout. */
3824 printf("Sections: Size %-*s Align\n",
3825 (int) (2 * sizeof(void *)), "Address");
3827 for (sec = f->load_order; sec; sec = sec->load_next) {
3831 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3836 printf("%-15s %08lx %0*lx 2**%d\n",
3838 (long)sec->header.sh_size,
3839 (int) (2 * sizeof(void *)),
3840 (long)sec->header.sh_addr,
3843 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3844 /* Quick reference which section indicies are loaded. */
3846 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3848 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3850 /* Collect the symbols we'll be listing. */
3852 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3853 for (sym = f->symtab[i]; sym; sym = sym->next)
3854 if (sym->secidx <= SHN_HIRESERVE
3855 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3858 all = alloca(nsyms * sizeof(struct obj_symbol *));
3860 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3861 for (sym = f->symtab[i]; sym; sym = sym->next)
3862 if (sym->secidx <= SHN_HIRESERVE
3863 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3866 /* And list them. */
3867 printf("\nSymbols:\n");
3868 for (p = all; p < all + nsyms; ++p) {
3870 unsigned long value;
3873 if (sym->secidx == SHN_ABS) {
3876 } else if (sym->secidx == SHN_UNDEF) {
3880 sec = f->sections[sym->secidx];
3882 if (sec->header.sh_type == SHT_NOBITS)
3884 else if (sec->header.sh_flags & SHF_ALLOC) {
3885 if (sec->header.sh_flags & SHF_EXECINSTR)
3887 else if (sec->header.sh_flags & SHF_WRITE)
3892 value = sym->value + sec->header.sh_addr;
3895 if (ELF_ST_BIND(sym->info) == STB_LOCAL)
3896 type = tolower(type);
3898 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3906 extern int insmod_main( int argc, char **argv)
3912 unsigned long m_size;
3917 int exit_status = EXIT_FAILURE;
3919 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3920 struct utsname uts_info;
3921 char m_strversion[STRVERSIONLEN];
3922 int m_version, m_crcs;
3924 #ifdef CONFIG_FEATURE_CLEAN_UP
3929 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3930 int flag_print_load_map = 0;
3933 struct utsname myuname;
3935 /* Parse any options */
3936 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3937 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
3939 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
3943 case 'f': /* force loading */
3944 flag_force_load = 1;
3946 case 'k': /* module loaded by kerneld, auto-cleanable */
3949 case 's': /* log to syslog */
3950 /* log to syslog -- not supported */
3951 /* but kernel needs this for request_module(), */
3952 /* as this calls: modprobe -k -s -- <module> */
3953 /* so silently ignore this flag */
3955 case 'v': /* verbose output */
3958 case 'q': /* silent */
3961 case 'x': /* do not export externs */
3964 case 'o': /* name the output module */
3966 m_name = bb_xstrdup(optarg);
3968 case 'L': /* Stub warning */
3969 /* This is needed for compatibility with modprobe.
3970 * In theory, this does locking, but we don't do
3971 * that. So be careful and plan your life around not
3972 * loading the same module 50 times concurrently. */
3974 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3975 case 'm': /* print module load map */
3976 flag_print_load_map = 1;
3984 if (argv[optind] == NULL) {
3988 /* Grab the module name */
3989 tmp1 = bb_xstrdup(argv[optind]);
3990 tmp = basename(tmp1);
3993 if (uname(&myuname) == 0) {
3994 if (myuname.release[0] == '2') {
3995 k_version = myuname.release[2] - '0';
3999 #if defined(CONFIG_FEATURE_2_6_MODULES)
4000 if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
4001 tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
4007 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
4013 #if defined(CONFIG_FEATURE_2_6_MODULES)
4015 m_fullName = bb_xasprintf("%s.ko", tmp);
4018 m_fullName = bb_xasprintf("%s.o", tmp);
4024 tmp1 = 0; /* flag for free(m_name) before exit() */
4027 /* Get a filedesc for the module. Check we we have a complete path */
4028 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
4029 (fp = fopen(argv[optind], "r")) == NULL) {
4030 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4031 * but do not error out yet if we fail to find it... */
4032 if (k_version) { /* uname succeedd */
4035 char real_module_dir[FILENAME_MAX];
4037 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4038 /* Jump through hoops in case /lib/modules/`uname -r`
4039 * is a symlink. We do not want recursive_action to
4040 * follow symlinks, but we do want to follow the
4041 * /lib/modules/`uname -r` dir, So resolve it ourselves
4042 * if it is a link... */
4043 if (realpath (tmdn, real_module_dir) == NULL)
4046 module_dir = real_module_dir;
4047 recursive_action(module_dir, TRUE, FALSE, FALSE,
4048 check_module_name_match, 0, m_fullName);
4052 /* Check if we have found anything yet */
4053 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4055 char module_dir[FILENAME_MAX];
4059 if (realpath (_PATH_MODULES, module_dir) == NULL)
4060 strcpy(module_dir, _PATH_MODULES);
4061 /* No module found under /lib/modules/`uname -r`, this
4062 * time cast the net a bit wider. Search /lib/modules/ */
4063 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4064 check_module_name_match, 0, m_fullName))
4067 || ((fp = fopen(m_filename, "r")) == NULL))
4069 bb_error_msg("%s: no module by that name found", m_fullName);
4073 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4076 m_filename = bb_xstrdup(argv[optind]);
4079 printf("Using %s\n", m_filename);
4081 #ifdef CONFIG_FEATURE_2_6_MODULES
4085 argv[optind + 1] = m_filename;
4086 return insmod_ng_main(argc - optind, argv + optind);
4090 if ((f = obj_load(fp, LOADBITS)) == NULL)
4091 bb_perror_msg_and_die("Could not load the module");
4093 if (get_modinfo_value(f, "kernel_version") == NULL)
4098 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4099 /* Version correspondence? */
4101 if (uname(&uts_info) < 0)
4102 uts_info.release[0] = '\0';
4103 if (m_has_modinfo) {
4104 m_version = new_get_module_version(f, m_strversion);
4105 if (m_version == -1) {
4106 bb_error_msg("couldn't find the kernel version the module was "
4112 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4113 if (flag_force_load) {
4114 bb_error_msg("Warning: kernel-module version mismatch\n"
4115 "\t%s was compiled for kernel version %s\n"
4116 "\twhile this kernel is version %s",
4117 m_filename, m_strversion, uts_info.release);
4119 bb_error_msg("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);
4128 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4130 if (!query_module(NULL, 0, NULL, 0, NULL)) {
4131 if (!new_get_kernel_symbols())
4133 k_crcs = new_is_kernel_checksummed();
4135 bb_error_msg("Not configured to support old kernels");
4139 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4142 m_crcs = new_is_module_checksummed(f);
4144 if (m_crcs != k_crcs)
4145 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4146 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4148 /* Let the module know about the kernel symbols. */
4149 add_kernel_symbols(f);
4151 /* Allocate common symbols, symbol tables, and string tables. */
4153 if (!new_create_this_module(f, m_name))
4158 if (!obj_check_undefineds(f)) {
4161 obj_allocate_commons(f);
4162 check_tainted_module(f, m_name);
4164 /* done with the module name, on to the optional var=value arguments */
4167 if (optind < argc) {
4168 if (!new_process_module_arguments(f, argc - optind, argv + optind))
4175 hide_special_symbols(f);
4177 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4178 add_ksymoops_symbols(f, m_filename, m_name);
4179 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4181 new_create_module_ksymtab(f);
4183 /* Find current size of the module */
4184 m_size = obj_load_size(f);
4187 m_addr = create_module(m_name, m_size);
4188 if (m_addr == -1) switch (errno) {
4190 bb_error_msg("A module named %s already exists", m_name);
4193 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4197 bb_perror_msg("create_module: %s", m_name);
4203 * the PROGBITS section was not loaded by the obj_load
4204 * now we can load them directly into the kernel memory
4206 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4207 delete_module(m_name);
4212 if (!obj_relocate(f, m_addr)) {
4213 delete_module(m_name);
4217 if (!new_init_module(m_name, f, m_size))
4219 delete_module(m_name);
4223 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4224 if(flag_print_load_map)
4228 exit_status = EXIT_SUCCESS;
4231 #ifdef CONFIG_FEATURE_CLEAN_UP
4240 return(exit_status);
4247 #ifdef CONFIG_FEATURE_2_6_MODULES
4249 #include <sys/mman.h>
4250 #include <asm/unistd.h>
4251 #include <sys/syscall.h>
4253 /* We use error numbers in a loose translation... */
4254 static const char *moderror(int err)
4258 return "Invalid module format";
4260 return "Unknown symbol in module";
4262 return "Module has wrong symbol version";
4264 return "Invalid parameters";
4266 return strerror(err);
4270 extern int insmod_ng_main( int argc, char **argv)
4278 char *filename, *options = bb_xstrdup("");
4286 /* Rest is options */
4287 for (i = 2; i < argc; i++) {
4288 options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
4289 /* Spaces handled by "" pairs, but no way of escaping quotes */
4290 if (strchr(argv[i], ' ')) {
4291 strcat(options, "\"");
4292 strcat(options, argv[i]);
4293 strcat(options, "\"");
4295 strcat(options, argv[i]);
4297 strcat(options, " ");
4300 if ((fd = open(filename, O_RDONLY, 0)) < 0) {
4301 bb_perror_msg_and_die("cannot open module `%s'", filename);
4306 map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
4307 if (map == MAP_FAILED) {
4308 bb_perror_msg_and_die("cannot mmap `%s'", filename);
4311 ret = syscall(__NR_init_module, map, len, options);
4313 bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
4314 filename, moderror(errno), ret);