1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * This version of insmod supports x86, ARM, SH3/4, powerpc, m68k,
8 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
9 * Copyright (C) 1999,2000,2001,2002 by Erik Andersen <andersee@debian.org>
10 * and Ron Alder <alder@lineo.com>
12 * Miles Bader <miles@gnu.org> added NEC V850E support.
14 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
15 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
17 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
18 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
19 * very minor changes required to also work with StrongArm and presumably
20 * all ARM based systems.
22 * Magnus Damm <damm@opensource.se> 22-May-2002.
23 * The plt and got code are now using the same structs.
24 * Added generic linked list code to fully support PowerPC.
25 * Replaced the mess in arch_apply_relocation() with architecture blocks.
26 * The arch_create_got() function got cleaned up with architecture blocks.
27 * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
29 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
30 * PowerPC specific code stolen from modutils-2.3.16,
31 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
32 * I've only tested the code on mpc8xx platforms in big-endian mode.
33 * Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
35 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
36 * based on modutils-2.4.2
37 * MIPS specific support for Elf loading and relocation.
38 * Copyright 1996, 1997 Linux International.
39 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
41 * Based almost entirely on the Linux modutils-2.3.11 implementation.
42 * Copyright 1996, 1997 Linux International.
43 * New implementation contributed by Richard Henderson <rth@tamu.edu>
44 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
45 * Restructured (and partly rewritten) by:
46 * Björn Ekwall <bj0rn@blox.se> February 1999
48 * This program is free software; you can redistribute it and/or modify
49 * it under the terms of the GNU General Public License as published by
50 * the Free Software Foundation; either version 2 of the License, or
51 * (at your option) any later version.
53 * This program is distributed in the hope that it will be useful,
54 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
56 * General Public License for more details.
58 * You should have received a copy of the GNU General Public License
59 * along with this program; if not, write to the Free Software
60 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
75 #include <sys/utsname.h>
78 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
79 # undef CONFIG_FEATURE_OLD_MODULE_INTERFACE
80 # define new_sys_init_module init_module
82 # define old_sys_init_module init_module
85 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
93 #define CONFIG_USE_PLT_ENTRIES
94 #define CONFIG_PLT_ENTRY_SIZE 8
95 #define CONFIG_USE_GOT_ENTRIES
96 #define CONFIG_GOT_ENTRY_SIZE 8
97 #define CONFIG_USE_SINGLE
99 #define MATCH_MACHINE(x) (x == EM_ARM)
100 #define SHT_RELM SHT_REL
101 #define Elf32_RelM Elf32_Rel
102 #define ELFCLASSM ELFCLASS32
105 #if defined(__s390__)
106 #define BB_USE_PLT_ENTRIES
107 #define BB_PLT_ENTRY_SIZE 8
108 #define BB_USE_GOT_ENTRIES
109 #define BB_GOT_ENTRY_SIZE 8
110 #define BB_USE_SINGLE
112 #define MATCH_MACHINE(x) (x == EM_S390)
113 #define SHT_RELM SHT_RELA
114 #define Elf32_RelM Elf32_Rela
115 #define ELFCLASSM ELFCLASS32
118 #if defined(__i386__)
119 #define CONFIG_USE_GOT_ENTRIES
120 #define CONFIG_GOT_ENTRY_SIZE 4
121 #define CONFIG_USE_SINGLE
124 #define MATCH_MACHINE(x) (x == EM_386)
126 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
129 #define SHT_RELM SHT_REL
130 #define Elf32_RelM Elf32_Rel
131 #define ELFCLASSM ELFCLASS32
134 #if defined(__mc68000__)
135 #define CONFIG_USE_GOT_ENTRIES
136 #define CONFIG_GOT_ENTRY_SIZE 4
137 #define CONFIG_USE_SINGLE
139 #define MATCH_MACHINE(x) (x == EM_68K)
140 #define SHT_RELM SHT_RELA
141 #define Elf32_RelM Elf32_Rela
144 #if defined(__mips__)
145 /* Account for ELF spec changes. */
146 #ifndef EM_MIPS_RS3_LE
147 #ifdef EM_MIPS_RS4_BE
148 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
150 #define EM_MIPS_RS3_LE 10
152 #endif /* !EM_MIPS_RS3_LE */
154 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
155 #define SHT_RELM SHT_REL
156 #define Elf32_RelM Elf32_Rel
157 #define ELFCLASSM ELFCLASS32
158 #define ARCHDATAM "__dbe_table"
161 #if defined(__powerpc__)
162 #define CONFIG_USE_PLT_ENTRIES
163 #define CONFIG_PLT_ENTRY_SIZE 16
164 #define CONFIG_USE_PLT_LIST
165 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
166 #define CONFIG_USE_LIST
168 #define MATCH_MACHINE(x) (x == EM_PPC)
169 #define SHT_RELM SHT_RELA
170 #define Elf32_RelM Elf32_Rela
171 #define ELFCLASSM ELFCLASS32
172 #define ARCHDATAM "__ftr_fixup"
176 #define CONFIG_USE_GOT_ENTRIES
177 #define CONFIG_GOT_ENTRY_SIZE 4
178 #define CONFIG_USE_SINGLE
180 #define MATCH_MACHINE(x) (x == EM_SH)
181 #define SHT_RELM SHT_RELA
182 #define Elf32_RelM Elf32_Rela
183 #define ELFCLASSM ELFCLASS32
185 /* the SH changes have only been tested on the SH4 in =little endian= mode */
186 /* I'm not sure about big endian, so let's warn: */
188 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
189 #error insmod.c may require changes for use on big endian SH4/SH3
192 /* it may or may not work on the SH1/SH2... So let's error on those
194 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
195 #error insmod.c may require changes for non-SH3/SH4 use
199 #if defined (__v850e__)
200 #define CONFIG_USE_PLT_ENTRIES
201 #define CONFIG_PLT_ENTRY_SIZE 8
202 #define CONFIG_USE_SINGLE
204 #ifndef EM_CYGNUS_V850 /* grumble */
205 #define EM_CYGNUS_V850 0x9080
208 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
209 #define SHT_RELM SHT_RELA
210 #define Elf32_RelM Elf32_Rela
211 #define ELFCLASSM ELFCLASS32
213 #define SYMBOL_PREFIX "_"
217 #error Sorry, but insmod.c does not yet support this architecture...
221 //----------------------------------------------------------------------------
222 //--------modutils module.h, lines 45-242
223 //----------------------------------------------------------------------------
225 /* Definitions for the Linux module syscall interface.
226 Copyright 1996, 1997 Linux International.
228 Contributed by Richard Henderson <rth@tamu.edu>
230 This file is part of the Linux modutils.
232 This program is free software; you can redistribute it and/or modify it
233 under the terms of the GNU General Public License as published by the
234 Free Software Foundation; either version 2 of the License, or (at your
235 option) any later version.
237 This program is distributed in the hope that it will be useful, but
238 WITHOUT ANY WARRANTY; without even the implied warranty of
239 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
240 General Public License for more details.
242 You should have received a copy of the GNU General Public License
243 along with this program; if not, write to the Free Software Foundation,
244 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
247 #ifndef MODUTILS_MODULE_H
248 static const int MODUTILS_MODULE_H = 1;
250 #ident "$Id: insmod.c,v 1.95 2003/01/23 06:02:39 andersen Exp $"
252 /* This file contains the structures used by the 2.0 and 2.1 kernels.
253 We do not use the kernel headers directly because we do not wish
254 to be dependant on a particular kernel version to compile insmod. */
257 /*======================================================================*/
258 /* The structures used by Linux 2.0. */
260 /* The symbol format used by get_kernel_syms(2). */
261 struct old_kernel_sym
267 struct old_module_ref
269 unsigned long module; /* kernel addresses */
273 struct old_module_symbol
279 struct old_symbol_table
281 int size; /* total, including string table!!! */
284 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
285 struct old_module_ref ref[0]; /* actual size defined by n_refs */
288 struct old_mod_routines
291 unsigned long cleanup;
297 unsigned long ref; /* the list of modules that refer to me */
298 unsigned long symtab;
300 int size; /* size of module in pages */
301 unsigned long addr; /* address of module */
303 unsigned long cleanup; /* cleanup routine */
306 /* Sent to init_module(2) or'ed into the code size parameter. */
307 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
309 int get_kernel_syms(struct old_kernel_sym *);
310 int old_sys_init_module(const char *name, char *code, unsigned codesize,
311 struct old_mod_routines *, struct old_symbol_table *);
313 /*======================================================================*/
314 /* For sizeof() which are related to the module platform and not to the
315 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
317 #define tgt_sizeof_char sizeof(char)
318 #define tgt_sizeof_short sizeof(short)
319 #define tgt_sizeof_int sizeof(int)
320 #define tgt_sizeof_long sizeof(long)
321 #define tgt_sizeof_char_p sizeof(char *)
322 #define tgt_sizeof_void_p sizeof(void *)
323 #define tgt_long long
325 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
326 #undef tgt_sizeof_long
327 #undef tgt_sizeof_char_p
328 #undef tgt_sizeof_void_p
330 static const int tgt_sizeof_long = 8;
331 static const int tgt_sizeof_char_p = 8;
332 static const int tgt_sizeof_void_p = 8;
333 #define tgt_long long long
336 /*======================================================================*/
337 /* The structures used in Linux 2.1. */
339 /* Note: new_module_symbol does not use tgt_long intentionally */
340 struct new_module_symbol
346 struct new_module_persist;
348 struct new_module_ref
350 unsigned tgt_long dep; /* kernel addresses */
351 unsigned tgt_long ref;
352 unsigned tgt_long next_ref;
357 unsigned tgt_long size_of_struct; /* == sizeof(module) */
358 unsigned tgt_long next;
359 unsigned tgt_long name;
360 unsigned tgt_long size;
363 unsigned tgt_long flags; /* AUTOCLEAN et al */
368 unsigned tgt_long syms;
369 unsigned tgt_long deps;
370 unsigned tgt_long refs;
371 unsigned tgt_long init;
372 unsigned tgt_long cleanup;
373 unsigned tgt_long ex_table_start;
374 unsigned tgt_long ex_table_end;
376 unsigned tgt_long gp;
378 /* Everything after here is extension. */
379 unsigned tgt_long persist_start;
380 unsigned tgt_long persist_end;
381 unsigned tgt_long can_unload;
382 unsigned tgt_long runsize;
383 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
384 const char *kallsyms_start; /* All symbols for kernel debugging */
385 const char *kallsyms_end;
386 const char *archdata_start; /* arch specific data for module */
387 const char *archdata_end;
388 const char *kernel_data; /* Reserved for kernel internal use */
393 #define ARCHDATA_SEC_NAME ARCHDATAM
395 #define ARCHDATA_SEC_NAME "__archdata"
397 #define KALLSYMS_SEC_NAME "__kallsyms"
400 struct new_module_info
408 /* Bits of module.flags. */
409 static const int NEW_MOD_RUNNING = 1;
410 static const int NEW_MOD_DELETED = 2;
411 static const int NEW_MOD_AUTOCLEAN = 4;
412 static const int NEW_MOD_VISITED = 8;
413 static const int NEW_MOD_USED_ONCE = 16;
415 int new_sys_init_module(const char *name, const struct new_module *);
416 int query_module(const char *name, int which, void *buf, size_t bufsize,
419 /* Values for query_module's which. */
421 static const int QM_MODULES = 1;
422 static const int QM_DEPS = 2;
423 static const int QM_REFS = 3;
424 static const int QM_SYMBOLS = 4;
425 static const int QM_INFO = 5;
427 /*======================================================================*/
428 /* The system calls unchanged between 2.0 and 2.1. */
430 unsigned long create_module(const char *, size_t);
431 int delete_module(const char *);
434 #endif /* module.h */
436 //----------------------------------------------------------------------------
437 //--------end of modutils module.h
438 //----------------------------------------------------------------------------
442 //----------------------------------------------------------------------------
443 //--------modutils obj.h, lines 253-462
444 //----------------------------------------------------------------------------
446 /* Elf object file loading and relocation routines.
447 Copyright 1996, 1997 Linux International.
449 Contributed by Richard Henderson <rth@tamu.edu>
451 This file is part of the Linux modutils.
453 This program is free software; you can redistribute it and/or modify it
454 under the terms of the GNU General Public License as published by the
455 Free Software Foundation; either version 2 of the License, or (at your
456 option) any later version.
458 This program is distributed in the hope that it will be useful, but
459 WITHOUT ANY WARRANTY; without even the implied warranty of
460 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
461 General Public License for more details.
463 You should have received a copy of the GNU General Public License
464 along with this program; if not, write to the Free Software Foundation,
465 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
468 #ifndef MODUTILS_OBJ_H
469 static const int MODUTILS_OBJ_H = 1;
471 #ident "$Id: insmod.c,v 1.95 2003/01/23 06:02:39 andersen Exp $"
473 /* The relocatable object is manipulated using elfin types. */
479 #if __BYTE_ORDER == __LITTLE_ENDIAN
480 #define ELFDATAM ELFDATA2LSB
481 #elif __BYTE_ORDER == __BIG_ENDIAN
482 #define ELFDATAM ELFDATA2MSB
486 # if ELFCLASSM == ELFCLASS32
487 # define ElfW(x) Elf32_ ## x
488 # define ELFW(x) ELF32_ ## x
490 # define ElfW(x) Elf64_ ## x
491 # define ELFW(x) ELF64_ ## x
495 /* For some reason this is missing from libc5. */
496 #ifndef ELF32_ST_INFO
497 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
500 #ifndef ELF64_ST_INFO
501 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
504 struct obj_string_patch;
505 struct obj_symbol_patch;
512 struct obj_section *load_next;
518 struct obj_symbol *next; /* hash table link */
522 int secidx; /* the defining section index/module */
524 int ksymidx; /* for export to the kernel symtab */
525 int referenced; /* actually used in the link */
528 /* Hardcode the hash table size. We shouldn't be needing so many
529 symbols that we begin to degrade performance, and we get a big win
530 by giving the compiler a constant divisor. */
532 #define HASH_BUCKETS 521
538 struct obj_section **sections;
539 struct obj_section *load_order;
540 struct obj_section **load_order_search_start;
541 struct obj_string_patch *string_patches;
542 struct obj_symbol_patch *symbol_patches;
543 int (*symbol_cmp)(const char *, const char *);
544 unsigned long (*symbol_hash)(const char *);
545 unsigned long local_symtab_size;
546 struct obj_symbol **local_symtab;
547 struct obj_symbol *symtab[HASH_BUCKETS];
558 struct obj_string_patch
560 struct obj_string_patch *next;
562 ElfW(Addr) reloc_offset;
563 ElfW(Addr) string_offset;
566 struct obj_symbol_patch
568 struct obj_symbol_patch *next;
570 ElfW(Addr) reloc_offset;
571 struct obj_symbol *sym;
575 /* Generic object manipulation routines. */
577 static unsigned long obj_elf_hash(const char *);
579 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
581 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
584 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
585 struct obj_symbol *sym);
587 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
588 static void obj_set_symbol_compare(struct obj_file *f,
589 int (*cmp)(const char *, const char *),
590 unsigned long (*hash)(const char *));
593 static struct obj_section *obj_find_section (struct obj_file *f,
596 static void obj_insert_section_load_order (struct obj_file *f,
597 struct obj_section *sec);
599 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
604 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
609 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
611 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
614 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
615 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
616 struct obj_symbol *sym);
619 static int obj_check_undefineds(struct obj_file *f);
621 static void obj_allocate_commons(struct obj_file *f);
623 static unsigned long obj_load_size (struct obj_file *f);
625 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
627 static struct obj_file *obj_load(FILE *f, int loadprogbits);
629 static int obj_create_image (struct obj_file *f, char *image);
631 /* Architecture specific manipulation routines. */
633 static struct obj_file *arch_new_file (void);
635 static struct obj_section *arch_new_section (void);
637 static struct obj_symbol *arch_new_symbol (void);
639 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
640 struct obj_section *targsec,
641 struct obj_section *symsec,
642 struct obj_symbol *sym,
643 ElfW(RelM) *rel, ElfW(Addr) value);
645 static void arch_create_got (struct obj_file *f);
647 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
648 static int arch_init_module (struct obj_file *f, struct new_module *);
652 //----------------------------------------------------------------------------
653 //--------end of modutils obj.h
654 //----------------------------------------------------------------------------
658 /* SPFX is always a string, so it can be concatenated to string constants. */
660 #define SPFX SYMBOL_PREFIX
666 #define _PATH_MODULES "/lib/modules"
667 static const int STRVERSIONLEN = 32;
669 /*======================================================================*/
671 static int flag_force_load = 0;
672 static int flag_autoclean = 0;
673 static int flag_verbose = 0;
674 static int flag_quiet = 0;
675 static int flag_export = 1;
678 /*======================================================================*/
680 #if defined(CONFIG_USE_LIST)
682 struct arch_list_entry
684 struct arch_list_entry *next;
685 CONFIG_LIST_ARCHTYPE addend;
692 #if defined(CONFIG_USE_SINGLE)
694 struct arch_single_entry
703 #if defined(__mips__)
706 struct mips_hi16 *next;
713 struct obj_file root;
714 #if defined(CONFIG_USE_PLT_ENTRIES)
715 struct obj_section *plt;
717 #if defined(CONFIG_USE_GOT_ENTRIES)
718 struct obj_section *got;
720 #if defined(__mips__)
721 struct mips_hi16 *mips_hi16_list;
726 struct obj_symbol root;
727 #if defined(CONFIG_USE_PLT_ENTRIES)
728 #if defined(CONFIG_USE_PLT_LIST)
729 struct arch_list_entry *pltent;
731 struct arch_single_entry pltent;
734 #if defined(CONFIG_USE_GOT_ENTRIES)
735 struct arch_single_entry gotent;
740 struct external_module {
745 struct new_module_symbol *syms;
748 static struct new_module_symbol *ksyms;
749 static size_t nksyms;
751 static struct external_module *ext_modules;
752 static int n_ext_modules;
753 static int n_ext_modules_used;
754 extern int delete_module(const char *);
756 static char *m_filename;
757 static char *m_fullName;
761 /*======================================================================*/
764 static int check_module_name_match(const char *filename, struct stat *statbuf,
767 char *fullname = (char *) userdata;
769 if (fullname[0] == '\0')
772 char *tmp, *tmp1 = xstrdup(filename);
773 tmp = get_last_path_component(tmp1);
774 if (strcmp(tmp, fullname) == 0) {
776 /* Stop searching if we find a match */
777 m_filename = xstrdup(filename);
786 /*======================================================================*/
788 static struct obj_file *arch_new_file(void)
791 f = xmalloc(sizeof(*f));
793 memset(f, 0, sizeof(*f));
798 static struct obj_section *arch_new_section(void)
800 return xmalloc(sizeof(struct obj_section));
803 static struct obj_symbol *arch_new_symbol(void)
805 struct arch_symbol *sym;
806 sym = xmalloc(sizeof(*sym));
808 memset(sym, 0, sizeof(*sym));
813 static enum obj_reloc
814 arch_apply_relocation(struct obj_file *f,
815 struct obj_section *targsec,
816 struct obj_section *symsec,
817 struct obj_symbol *sym,
818 ElfW(RelM) *rel, ElfW(Addr) v)
820 struct arch_file *ifile = (struct arch_file *) f;
821 enum obj_reloc ret = obj_reloc_ok;
822 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
823 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
824 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
825 struct arch_symbol *isym = (struct arch_symbol *) sym;
827 #if defined(CONFIG_USE_GOT_ENTRIES)
828 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
830 #if defined(CONFIG_USE_PLT_ENTRIES)
831 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
833 #if defined(CONFIG_USE_PLT_LIST)
834 struct arch_list_entry *pe;
836 struct arch_single_entry *pe;
840 switch (ELF32_R_TYPE(rel->r_info)) {
855 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
856 * (which is .got) similar to branch,
857 * but is full 32 bits relative */
867 case R_ARM_GOTOFF: /* address relative to the got */
872 #elif defined(__s390__)
874 *(unsigned int *) loc += v;
877 *(unsigned short *) loc += v;
880 *(unsigned char *) loc += v;
884 *(unsigned int *) loc += v - dot;
887 *(unsigned short *) loc += (v - dot) >> 1;
890 *(unsigned short *) loc += v - dot;
895 /* find the plt entry and initialize it. */
896 assert(isym != NULL);
897 pe = (struct arch_single_entry *) &isym->pltent;
898 assert(pe->allocated);
899 if (pe->inited == 0) {
900 ip = (unsigned long *)(ifile->plt->contents + pe->offset);
901 ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
903 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
910 /* Insert relative distance to target. */
911 v = plt + pe->offset - dot;
912 if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
913 *(unsigned int *) loc = (unsigned int) v;
914 else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
915 *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
929 *(unsigned long *) loc += got - dot;
935 assert(isym != NULL);
937 if (!isym->gotent.inited)
939 isym->gotent.inited = 1;
940 *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
942 if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
943 *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
944 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
945 *(unsigned short *) loc += isym->gotent.offset;
946 else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
947 *(unsigned int *) loc += isym->gotent.offset;
955 #elif defined(__i386__)
991 #elif defined(__mc68000__)
1002 ret = obj_reloc_overflow;
1009 ret = obj_reloc_overflow;
1016 if ((Elf32_Sword)v > 0x7f ||
1017 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
1018 ret = obj_reloc_overflow;
1025 if ((Elf32_Sword)v > 0x7fff ||
1026 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
1027 ret = obj_reloc_overflow;
1033 *(int *)loc = v - dot;
1036 case R_68K_GLOB_DAT:
1037 case R_68K_JMP_SLOT:
1041 case R_68K_RELATIVE:
1042 *(int *)loc += f->baseaddr;
1053 #elif defined(__mips__)
1064 ret = obj_reloc_dangerous;
1065 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
1066 ret = obj_reloc_overflow;
1068 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
1074 struct mips_hi16 *n;
1076 /* We cannot relocate this one now because we don't know the value
1077 of the carry we need to add. Save the information, and let LO16
1078 do the actual relocation. */
1079 n = (struct mips_hi16 *) xmalloc(sizeof *n);
1082 n->next = ifile->mips_hi16_list;
1083 ifile->mips_hi16_list = n;
1089 unsigned long insnlo = *loc;
1090 Elf32_Addr val, vallo;
1092 /* Sign extend the addend we extract from the lo insn. */
1093 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1095 if (ifile->mips_hi16_list != NULL) {
1096 struct mips_hi16 *l;
1098 l = ifile->mips_hi16_list;
1100 struct mips_hi16 *next;
1103 /* The value for the HI16 had best be the same. */
1104 assert(v == l->value);
1106 /* Do the HI16 relocation. Note that we actually don't
1107 need to know anything about the LO16 itself, except where
1108 to find the low 16 bits of the addend needed by the LO16. */
1111 ((insn & 0xffff) << 16) +
1115 /* Account for the sign extension that will happen in the
1122 insn = (insn & ~0xffff) | val;
1130 ifile->mips_hi16_list = NULL;
1133 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1135 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1140 #elif defined(__powerpc__)
1142 case R_PPC_ADDR16_HA:
1143 *(unsigned short *)loc = (v + 0x8000) >> 16;
1146 case R_PPC_ADDR16_HI:
1147 *(unsigned short *)loc = v >> 16;
1150 case R_PPC_ADDR16_LO:
1151 *(unsigned short *)loc = v;
1165 #elif defined(__sh__)
1188 *loc = f->baseaddr + rel->r_addend;
1193 *loc = got - dot + rel->r_addend;
1207 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1208 ret = obj_reloc_unhandled;
1211 #if defined (__v850e__)
1216 /* We write two shorts instead of a long because even
1217 32-bit insns only need half-word alignment, but
1218 32-bit data needs to be long-word aligned. */
1219 v += ((unsigned short *)loc)[0];
1220 v += ((unsigned short *)loc)[1] << 16;
1221 ((unsigned short *)loc)[0] = v & 0xffff;
1222 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1225 case R_V850_22_PCREL:
1229 #if defined(CONFIG_USE_PLT_ENTRIES)
1233 /* find the plt entry and initialize it if necessary */
1234 assert(isym != NULL);
1236 #if defined(CONFIG_USE_PLT_LIST)
1237 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1245 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1247 /* generate some machine code */
1249 #if defined(__arm__)
1250 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1251 ip[1] = v; /* sym@ */
1253 #if defined(__powerpc__)
1254 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1255 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1256 ip[2] = 0x7d6903a6; /* mtctr r11 */
1257 ip[3] = 0x4e800420; /* bctr */
1259 #if defined (__v850e__)
1260 /* We have to trash a register, so we assume that any control
1261 transfer more than 21-bits away must be a function call
1262 (so we can use a call-clobbered register). */
1263 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1264 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1269 /* relative distance to target */
1271 /* if the target is too far away.... */
1272 #if defined (__arm__) || defined (__powerpc__)
1273 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1274 #elif defined (__v850e__)
1275 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1277 /* go via the plt */
1278 v = plt + pe->offset - dot;
1280 #if defined (__v850e__)
1285 ret = obj_reloc_dangerous;
1287 /* merge the offset into the instruction. */
1288 #if defined(__arm__)
1289 /* Convert to words. */
1292 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1294 #if defined(__powerpc__)
1295 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1297 #if defined (__v850e__)
1298 /* We write two shorts instead of a long because even 32-bit insns
1299 only need half-word alignment, but the 32-bit data write needs
1300 to be long-word aligned. */
1301 ((unsigned short *)loc)[0] =
1302 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1303 | ((v >> 16) & 0x3f); /* offs high part */
1304 ((unsigned short *)loc)[1] =
1305 (v & 0xffff); /* offs low part */
1308 #endif /* CONFIG_USE_PLT_ENTRIES */
1310 #if defined(CONFIG_USE_GOT_ENTRIES)
1313 assert(isym != NULL);
1314 /* needs an entry in the .got: set it, once */
1315 if (!isym->gotent.inited) {
1316 isym->gotent.inited = 1;
1317 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1319 /* make the reloc with_respect_to_.got */
1321 *loc += isym->gotent.offset + rel->r_addend;
1322 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1323 *loc += isym->gotent.offset;
1327 #endif /* CONFIG_USE_GOT_ENTRIES */
1334 #if defined(CONFIG_USE_LIST)
1336 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1337 int offset, int size)
1339 struct arch_list_entry *pe;
1341 for (pe = *list; pe != NULL; pe = pe->next) {
1342 if (pe->addend == rel->r_addend) {
1348 pe = xmalloc(sizeof(struct arch_list_entry));
1350 pe->addend = rel->r_addend;
1351 pe->offset = offset;
1361 #if defined(CONFIG_USE_SINGLE)
1363 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1364 int offset, int size)
1366 if (single->allocated == 0) {
1367 single->allocated = 1;
1368 single->offset = offset;
1377 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1379 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1380 int offset, int size)
1382 struct obj_section *myrelsec = obj_find_section(f, name);
1389 obj_extend_section(myrelsec, offset);
1391 myrelsec = obj_create_alloced_section(f, name,
1401 static void arch_create_got(struct obj_file *f)
1403 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1404 struct arch_file *ifile = (struct arch_file *) f;
1406 #if defined(CONFIG_USE_GOT_ENTRIES)
1407 int got_offset = 0, got_needed = 0, got_allocate;
1409 #if defined(CONFIG_USE_PLT_ENTRIES)
1410 int plt_offset = 0, plt_needed = 0, plt_allocate;
1412 struct obj_section *relsec, *symsec, *strsec;
1413 ElfW(RelM) *rel, *relend;
1414 ElfW(Sym) *symtab, *extsym;
1415 const char *strtab, *name;
1416 struct arch_symbol *intsym;
1418 for (i = 0; i < f->header.e_shnum; ++i) {
1419 relsec = f->sections[i];
1420 if (relsec->header.sh_type != SHT_RELM)
1423 symsec = f->sections[relsec->header.sh_link];
1424 strsec = f->sections[symsec->header.sh_link];
1426 rel = (ElfW(RelM) *) relsec->contents;
1427 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1428 symtab = (ElfW(Sym) *) symsec->contents;
1429 strtab = (const char *) strsec->contents;
1431 for (; rel < relend; ++rel) {
1432 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1434 #if defined(CONFIG_USE_GOT_ENTRIES)
1437 #if defined(CONFIG_USE_PLT_ENTRIES)
1441 switch (ELF32_R_TYPE(rel->r_info)) {
1442 #if defined(__arm__)
1457 #elif defined(__i386__)
1467 #elif defined(__powerpc__)
1472 #elif defined(__mc68000__)
1481 #elif defined(__sh__)
1491 #elif defined (__v850e__)
1492 case R_V850_22_PCREL:
1501 if (extsym->st_name != 0) {
1502 name = strtab + extsym->st_name;
1504 name = f->sections[extsym->st_shndx]->name;
1506 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1507 #if defined(CONFIG_USE_GOT_ENTRIES)
1509 got_offset += arch_single_init(
1510 rel, &intsym->gotent,
1511 got_offset, CONFIG_GOT_ENTRY_SIZE);
1516 #if defined(CONFIG_USE_PLT_ENTRIES)
1518 #if defined(CONFIG_USE_PLT_LIST)
1519 plt_offset += arch_list_add(
1520 rel, &intsym->pltent,
1521 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1523 plt_offset += arch_single_init(
1524 rel, &intsym->pltent,
1525 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1533 #if defined(CONFIG_USE_GOT_ENTRIES)
1535 ifile->got = arch_xsect_init(f, ".got", got_offset,
1536 CONFIG_GOT_ENTRY_SIZE);
1540 #if defined(CONFIG_USE_PLT_ENTRIES)
1542 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1543 CONFIG_PLT_ENTRY_SIZE);
1547 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1550 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
1551 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1557 /*======================================================================*/
1559 /* Standard ELF hash function. */
1560 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1562 unsigned long h = 0;
1569 if ((g = (h & 0xf0000000)) != 0) {
1578 static unsigned long obj_elf_hash(const char *name)
1580 return obj_elf_hash_n(name, strlen(name));
1583 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1584 /* String comparison for non-co-versioned kernel and module. */
1586 static int ncv_strcmp(const char *a, const char *b)
1588 size_t alen = strlen(a), blen = strlen(b);
1590 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1591 return strncmp(a, b, alen);
1592 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1593 return strncmp(a, b, blen);
1595 return strcmp(a, b);
1598 /* String hashing for non-co-versioned kernel and module. Here
1599 we are simply forced to drop the crc from the hash. */
1601 static unsigned long ncv_symbol_hash(const char *str)
1603 size_t len = strlen(str);
1604 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1606 return obj_elf_hash_n(str, len);
1610 obj_set_symbol_compare(struct obj_file *f,
1611 int (*cmp) (const char *, const char *),
1612 unsigned long (*hash) (const char *))
1615 f->symbol_cmp = cmp;
1617 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1620 f->symbol_hash = hash;
1622 memcpy(tmptab, f->symtab, sizeof(tmptab));
1623 memset(f->symtab, 0, sizeof(f->symtab));
1625 for (i = 0; i < HASH_BUCKETS; ++i)
1626 for (sym = tmptab[i]; sym; sym = next) {
1627 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1629 sym->next = f->symtab[h];
1635 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1637 static struct obj_symbol *
1638 obj_add_symbol(struct obj_file *f, const char *name,
1639 unsigned long symidx, int info,
1640 int secidx, ElfW(Addr) value,
1643 struct obj_symbol *sym;
1644 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1645 int n_type = ELFW(ST_TYPE) (info);
1646 int n_binding = ELFW(ST_BIND) (info);
1648 for (sym = f->symtab[hash]; sym; sym = sym->next)
1649 if (f->symbol_cmp(sym->name, name) == 0) {
1650 int o_secidx = sym->secidx;
1651 int o_info = sym->info;
1652 int o_type = ELFW(ST_TYPE) (o_info);
1653 int o_binding = ELFW(ST_BIND) (o_info);
1655 /* A redefinition! Is it legal? */
1657 if (secidx == SHN_UNDEF)
1659 else if (o_secidx == SHN_UNDEF)
1661 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1662 /* Cope with local and global symbols of the same name
1663 in the same object file, as might have been created
1664 by ld -r. The only reason locals are now seen at this
1665 level at all is so that we can do semi-sensible things
1668 struct obj_symbol *nsym, **p;
1670 nsym = arch_new_symbol();
1671 nsym->next = sym->next;
1674 /* Excise the old (local) symbol from the hash chain. */
1675 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1679 } else if (n_binding == STB_LOCAL) {
1680 /* Another symbol of the same name has already been defined.
1681 Just add this to the local table. */
1682 sym = arch_new_symbol();
1685 f->local_symtab[symidx] = sym;
1687 } else if (n_binding == STB_WEAK)
1689 else if (o_binding == STB_WEAK)
1691 /* Don't unify COMMON symbols with object types the programmer
1693 else if (secidx == SHN_COMMON
1694 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1696 else if (o_secidx == SHN_COMMON
1697 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1700 /* Don't report an error if the symbol is coming from
1701 the kernel or some external module. */
1702 if (secidx <= SHN_HIRESERVE)
1703 error_msg("%s multiply defined", name);
1708 /* Completely new symbol. */
1709 sym = arch_new_symbol();
1710 sym->next = f->symtab[hash];
1711 f->symtab[hash] = sym;
1714 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1715 if (symidx >= f->local_symtab_size)
1716 error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1717 name, (long) symidx, (long) f->local_symtab_size);
1719 f->local_symtab[symidx] = sym;
1726 sym->secidx = secidx;
1732 static struct obj_symbol *
1733 obj_find_symbol(struct obj_file *f, const char *name)
1735 struct obj_symbol *sym;
1736 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1738 for (sym = f->symtab[hash]; sym; sym = sym->next)
1739 if (f->symbol_cmp(sym->name, name) == 0)
1746 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1749 if (sym->secidx >= SHN_LORESERVE)
1752 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1754 /* As a special case, a NULL sym has value zero. */
1759 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1761 int i, n = f->header.e_shnum;
1763 for (i = 0; i < n; ++i)
1764 if (strcmp(f->sections[i]->name, name) == 0)
1765 return f->sections[i];
1770 static int obj_load_order_prio(struct obj_section *a)
1772 unsigned long af, ac;
1774 af = a->header.sh_flags;
1777 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1778 strcmp(a->name + 5, ".init"))
1782 if (!(af & SHF_WRITE))
1784 if (af & SHF_EXECINSTR)
1786 if (a->header.sh_type != SHT_NOBITS)
1793 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1795 struct obj_section **p;
1796 int prio = obj_load_order_prio(sec);
1797 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1798 if (obj_load_order_prio(*p) < prio)
1800 sec->load_next = *p;
1804 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1806 unsigned long align,
1809 int newidx = f->header.e_shnum++;
1810 struct obj_section *sec;
1812 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1813 f->sections[newidx] = sec = arch_new_section();
1815 memset(sec, 0, sizeof(*sec));
1816 sec->header.sh_type = SHT_PROGBITS;
1817 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1818 sec->header.sh_size = size;
1819 sec->header.sh_addralign = align;
1823 sec->contents = xmalloc(size);
1825 obj_insert_section_load_order(f, sec);
1830 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1832 unsigned long align,
1835 int newidx = f->header.e_shnum++;
1836 struct obj_section *sec;
1838 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1839 f->sections[newidx] = sec = arch_new_section();
1841 memset(sec, 0, sizeof(*sec));
1842 sec->header.sh_type = SHT_PROGBITS;
1843 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1844 sec->header.sh_size = size;
1845 sec->header.sh_addralign = align;
1849 sec->contents = xmalloc(size);
1851 sec->load_next = f->load_order;
1852 f->load_order = sec;
1853 if (f->load_order_search_start == &f->load_order)
1854 f->load_order_search_start = &sec->load_next;
1859 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1861 unsigned long oldsize = sec->header.sh_size;
1863 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1865 return sec->contents + oldsize;
1869 /* Conditionally add the symbols from the given symbol set to the
1875 int idx, struct new_module_symbol *syms, size_t nsyms)
1877 struct new_module_symbol *s;
1880 #ifdef SYMBOL_PREFIX
1882 size_t name_alloced_size = 0;
1885 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1886 /* Only add symbols that are already marked external.
1887 If we override locals we may cause problems for
1888 argument initialization. We will also create a false
1889 dependency on the module. */
1890 struct obj_symbol *sym;
1891 char *name = (char *)s->name;
1893 #ifdef SYMBOL_PREFIX
1894 /* Prepend SYMBOL_PREFIX to the symbol's name (the
1895 kernel exports `C names', but module object files
1896 reference `linker names'). */
1897 size_t extra = sizeof SYMBOL_PREFIX;
1898 size_t name_size = strlen (name) + extra;
1899 if (name_size > name_alloced_size) {
1900 name_alloced_size = name_size * 2;
1901 name_buf = alloca (name_alloced_size);
1903 strcpy (name_buf, SYMBOL_PREFIX);
1904 strcpy (name_buf + extra - 1, name);
1906 #endif /* SYMBOL_PREFIX */
1908 sym = obj_find_symbol(f, name);
1909 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
1910 #ifdef SYMBOL_PREFIX
1911 /* Put NAME_BUF into more permanent storage. */
1912 name = xmalloc (name_size);
1913 strcpy (name, name_buf);
1915 sym = obj_add_symbol(f, name, -1,
1916 ELFW(ST_INFO) (STB_GLOBAL,
1919 /* Did our symbol just get installed? If so, mark the
1920 module as "used". */
1921 if (sym->secidx == idx)
1929 static void add_kernel_symbols(struct obj_file *f)
1931 struct external_module *m;
1934 /* Add module symbols first. */
1936 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1938 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1939 m->nsyms)) m->used = 1, ++nused;
1941 n_ext_modules_used = nused;
1943 /* And finally the symbols from the kernel proper. */
1946 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1949 static char *get_modinfo_value(struct obj_file *f, const char *key)
1951 struct obj_section *sec;
1952 char *p, *v, *n, *ep;
1953 size_t klen = strlen(key);
1955 sec = obj_find_section(f, ".modinfo");
1959 ep = p + sec->header.sh_size;
1962 n = strchr(p, '\0');
1964 if (p + klen == v && strncmp(p, key, klen) == 0)
1967 if (p + klen == n && strcmp(p, key) == 0)
1977 /*======================================================================*/
1978 /* Functions relating to module loading in pre 2.1 kernels. */
1981 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1985 struct obj_symbol *sym;
1989 if ((q = strchr(p, '=')) == NULL) {
1995 sym = obj_find_symbol(f, p);
1997 /* Also check that the parameter was not resolved from the kernel. */
1998 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1999 error_msg("symbol for parameter %s not found", p);
2003 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
2005 /* Do C quoting if we begin with a ". */
2009 str = alloca(strlen(q));
2010 for (r = str, q++; *q != '"'; ++q, ++r) {
2012 error_msg("improperly terminated string argument for %s", p);
2014 } else if (*q == '\\')
2048 if (q[1] >= '0' && q[1] <= '7') {
2049 c = (c * 8) + *++q - '0';
2050 if (q[1] >= '0' && q[1] <= '7')
2051 c = (c * 8) + *++q - '0';
2064 obj_string_patch(f, sym->secidx, sym->value, str);
2065 } else if (*q >= '0' && *q <= '9') {
2067 *loc++ = strtoul(q, &q, 0);
2068 while (*q++ == ',');
2070 char *contents = f->sections[sym->secidx]->contents;
2071 char *myloc = contents + sym->value;
2072 char *r; /* To search for commas */
2074 /* Break the string with comas */
2075 while ((r = strchr(q, ',')) != (char *) NULL) {
2077 obj_string_patch(f, sym->secidx, myloc - contents, q);
2078 myloc += sizeof(char *);
2083 obj_string_patch(f, sym->secidx, myloc - contents, q);
2092 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2093 static int old_is_module_checksummed(struct obj_file *f)
2095 return obj_find_symbol(f, "Using_Versions") != NULL;
2097 /* Get the module's kernel version in the canonical integer form. */
2100 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2102 struct obj_symbol *sym;
2106 sym = obj_find_symbol(f, "kernel_version");
2110 p = f->sections[sym->secidx]->contents + sym->value;
2111 safe_strncpy(str, p, STRVERSIONLEN);
2113 a = strtoul(p, &p, 10);
2116 b = strtoul(p + 1, &p, 10);
2119 c = strtoul(p + 1, &q, 10);
2123 return a << 16 | b << 8 | c;
2126 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2128 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
2130 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
2132 static int old_get_kernel_symbols(const char *m_name)
2134 struct old_kernel_sym *ks, *k;
2135 struct new_module_symbol *s;
2136 struct external_module *mod;
2137 int nks, nms, nmod, i;
2139 nks = get_kernel_syms(NULL);
2142 perror_msg("get_kernel_syms: %s", m_name);
2144 error_msg("No kernel symbols");
2148 ks = k = xmalloc(nks * sizeof(*ks));
2150 if (get_kernel_syms(ks) != nks) {
2151 perror("inconsistency with get_kernel_syms -- is someone else "
2152 "playing with modules?");
2157 /* Collect the module information. */
2162 while (k->name[0] == '#' && k->name[1]) {
2163 struct old_kernel_sym *k2;
2165 /* Find out how many symbols this module has. */
2166 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
2170 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
2171 mod[nmod].name = k->name + 1;
2172 mod[nmod].addr = k->value;
2174 mod[nmod].nsyms = nms;
2175 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2177 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
2178 s->name = (unsigned long) k->name;
2179 s->value = k->value;
2186 n_ext_modules = nmod + 1;
2188 /* Now collect the symbols for the kernel proper. */
2190 if (k->name[0] == '#')
2193 nksyms = nms = nks - (k - ks);
2194 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2196 for (i = 0; i < nms; ++i, ++s, ++k) {
2197 s->name = (unsigned long) k->name;
2198 s->value = k->value;
2204 /* Return the kernel symbol checksum version, or zero if not used. */
2206 static int old_is_kernel_checksummed(void)
2208 /* Using_Versions is the first symbol. */
2210 && strcmp((char *) ksyms[0].name,
2211 "Using_Versions") == 0) return ksyms[0].value;
2217 static int old_create_mod_use_count(struct obj_file *f)
2219 struct obj_section *sec;
2221 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2224 obj_add_symbol(f, "mod_use_count_", -1,
2225 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2232 old_init_module(const char *m_name, struct obj_file *f,
2233 unsigned long m_size)
2236 struct old_mod_routines routines;
2237 struct old_symbol_table *symtab;
2240 /* Create the symbol table */
2242 int nsyms = 0, strsize = 0, total;
2244 /* Size things first... */
2247 for (i = 0; i < HASH_BUCKETS; ++i) {
2248 struct obj_symbol *sym;
2249 for (sym = f->symtab[i]; sym; sym = sym->next)
2250 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2251 && sym->secidx <= SHN_HIRESERVE)
2253 sym->ksymidx = nsyms++;
2254 strsize += strlen(sym->name) + 1;
2259 total = (sizeof(struct old_symbol_table)
2260 + nsyms * sizeof(struct old_module_symbol)
2261 + n_ext_modules_used * sizeof(struct old_module_ref)
2263 symtab = xmalloc(total);
2264 symtab->size = total;
2265 symtab->n_symbols = nsyms;
2266 symtab->n_refs = n_ext_modules_used;
2268 if (flag_export && nsyms) {
2269 struct old_module_symbol *ksym;
2273 ksym = symtab->symbol;
2274 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2275 + n_ext_modules_used * sizeof(struct old_module_ref));
2277 for (i = 0; i < HASH_BUCKETS; ++i) {
2278 struct obj_symbol *sym;
2279 for (sym = f->symtab[i]; sym; sym = sym->next)
2280 if (sym->ksymidx >= 0) {
2281 ksym->addr = obj_symbol_final_value(f, sym);
2283 (unsigned long) str - (unsigned long) symtab;
2285 strcpy(str, sym->name);
2286 str += strlen(sym->name) + 1;
2292 if (n_ext_modules_used) {
2293 struct old_module_ref *ref;
2296 ref = (struct old_module_ref *)
2297 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2299 for (i = 0; i < n_ext_modules; ++i)
2300 if (ext_modules[i].used)
2301 ref++->module = ext_modules[i].addr;
2305 /* Fill in routines. */
2308 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2310 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2312 /* Whew! All of the initialization is complete. Collect the final
2313 module image and give it to the kernel. */
2315 image = xmalloc(m_size);
2316 obj_create_image(f, image);
2318 /* image holds the complete relocated module, accounting correctly for
2319 mod_use_count. However the old module kernel support assume that
2320 it is receiving something which does not contain mod_use_count. */
2321 ret = old_sys_init_module(m_name, image + sizeof(long),
2322 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2323 : 0), &routines, symtab);
2325 perror_msg("init_module: %s", m_name);
2335 #define old_create_mod_use_count(x) TRUE
2336 #define old_init_module(x, y, z) TRUE
2338 #endif /* CONFIG_FEATURE_OLD_MODULE_INTERFACE */
2342 /*======================================================================*/
2343 /* Functions relating to module loading after 2.1.18. */
2346 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2349 char *p, *q, *key, *sym_name;
2350 struct obj_symbol *sym;
2351 char *contents, *loc;
2355 if ((q = strchr(p, '=')) == NULL) {
2360 key = alloca(q - p + 6);
2361 memcpy(key, "parm_", 5);
2362 memcpy(key + 5, p, q - p);
2365 p = get_modinfo_value(f, key);
2368 error_msg("invalid parameter %s", key);
2372 #ifdef SYMBOL_PREFIX
2373 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2374 strcpy (sym_name, SYMBOL_PREFIX);
2375 strcat (sym_name, key);
2379 sym = obj_find_symbol(f, sym_name);
2381 /* Also check that the parameter was not resolved from the kernel. */
2382 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2383 error_msg("symbol for parameter %s not found", key);
2388 min = strtoul(p, &p, 10);
2390 max = strtoul(p + 1, &p, 10);
2396 contents = f->sections[sym->secidx]->contents;
2397 loc = contents + sym->value;
2401 if ((*p == 's') || (*p == 'c')) {
2404 /* Do C quoting if we begin with a ", else slurp the lot. */
2408 str = alloca(strlen(q));
2409 for (r = str, q++; *q != '"'; ++q, ++r) {
2411 error_msg("improperly terminated string argument for %s",
2414 } else if (*q == '\\')
2448 if (q[1] >= '0' && q[1] <= '7') {
2449 c = (c * 8) + *++q - '0';
2450 if (q[1] >= '0' && q[1] <= '7')
2451 c = (c * 8) + *++q - '0';
2468 /* In this case, the string is not quoted. We will break
2469 it using the coma (like for ints). If the user wants to
2470 include comas in a string, he just has to quote it */
2472 /* Search the next coma */
2476 if (r != (char *) NULL) {
2477 /* Recopy the current field */
2478 str = alloca(r - q + 1);
2479 memcpy(str, q, r - q);
2481 /* I don't know if it is usefull, as the previous case
2482 doesn't null terminate the string ??? */
2485 /* Keep next fields */
2496 obj_string_patch(f, sym->secidx, loc - contents, str);
2497 loc += tgt_sizeof_char_p;
2499 /* Array of chars (in fact, matrix !) */
2500 unsigned long charssize; /* size of each member */
2502 /* Get the size of each member */
2503 /* Probably we should do that outside the loop ? */
2504 if (!isdigit(*(p + 1))) {
2505 error_msg("parameter type 'c' for %s must be followed by"
2506 " the maximum size", key);
2509 charssize = strtoul(p + 1, (char **) NULL, 10);
2512 if (strlen(str) >= charssize) {
2513 error_msg("string too long for %s (max %ld)", key,
2518 /* Copy to location */
2519 strcpy((char *) loc, str);
2523 long v = strtoul(q, &q, 0);
2530 loc += tgt_sizeof_short;
2534 loc += tgt_sizeof_int;
2538 loc += tgt_sizeof_long;
2542 error_msg("unknown parameter type '%c' for %s", *p, key);
2557 goto retry_end_of_value;
2561 error_msg("too many values for %s (max %d)", key, max);
2568 error_msg("invalid argument syntax for %s", key);
2575 error_msg("too few values for %s (min %d)", key, min);
2585 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2586 static int new_is_module_checksummed(struct obj_file *f)
2588 const char *p = get_modinfo_value(f, "using_checksums");
2595 /* Get the module's kernel version in the canonical integer form. */
2598 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2603 p = get_modinfo_value(f, "kernel_version");
2606 safe_strncpy(str, p, STRVERSIONLEN);
2608 a = strtoul(p, &p, 10);
2611 b = strtoul(p + 1, &p, 10);
2614 c = strtoul(p + 1, &q, 10);
2618 return a << 16 | b << 8 | c;
2621 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2624 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2626 /* Fetch the loaded modules, and all currently exported symbols. */
2628 static int new_get_kernel_symbols(void)
2630 char *module_names, *mn;
2631 struct external_module *modules, *m;
2632 struct new_module_symbol *syms, *s;
2633 size_t ret, bufsize, nmod, nsyms, i, j;
2635 /* Collect the loaded modules. */
2637 module_names = xmalloc(bufsize = 256);
2639 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2640 if (errno == ENOSPC && bufsize < ret) {
2641 module_names = xrealloc(module_names, bufsize = ret);
2642 goto retry_modules_load;
2644 perror_msg("QM_MODULES");
2648 n_ext_modules = nmod = ret;
2650 /* Collect the modules' symbols. */
2653 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2654 memset(modules, 0, nmod * sizeof(*modules));
2655 for (i = 0, mn = module_names, m = modules;
2656 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2657 struct new_module_info info;
2659 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2660 if (errno == ENOENT) {
2661 /* The module was removed out from underneath us. */
2664 perror_msg("query_module: QM_INFO: %s", mn);
2668 syms = xmalloc(bufsize = 1024);
2670 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2673 syms = xrealloc(syms, bufsize = ret);
2674 goto retry_mod_sym_load;
2676 /* The module was removed out from underneath us. */
2679 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2686 m->addr = info.addr;
2690 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2691 s->name += (unsigned long) syms;
2696 /* Collect the kernel's symbols. */
2698 syms = xmalloc(bufsize = 16 * 1024);
2699 retry_kern_sym_load:
2700 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2701 if (errno == ENOSPC && bufsize < ret) {
2702 syms = xrealloc(syms, bufsize = ret);
2703 goto retry_kern_sym_load;
2705 perror_msg("kernel: QM_SYMBOLS");
2708 nksyms = nsyms = ret;
2711 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2712 s->name += (unsigned long) syms;
2718 /* Return the kernel symbol checksum version, or zero if not used. */
2720 static int new_is_kernel_checksummed(void)
2722 struct new_module_symbol *s;
2725 /* Using_Versions is not the first symbol, but it should be in there. */
2727 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2728 if (strcmp((char *) s->name, "Using_Versions") == 0)
2735 static int new_create_this_module(struct obj_file *f, const char *m_name)
2737 struct obj_section *sec;
2739 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2740 sizeof(struct new_module));
2741 memset(sec->contents, 0, sizeof(struct new_module));
2743 obj_add_symbol(f, SPFX "__this_module", -1,
2744 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2745 sizeof(struct new_module));
2747 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2753 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2754 /* add an entry to the __ksymtab section, creating it if necessary */
2755 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2757 struct obj_section *sec;
2760 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2761 * If __ksymtab is defined but not marked alloc, x out the first character
2762 * (no obj_delete routine) and create a new __ksymtab with the correct
2765 sec = obj_find_section(f, "__ksymtab");
2766 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2767 *((char *)(sec->name)) = 'x'; /* override const */
2771 sec = obj_create_alloced_section(f, "__ksymtab",
2772 tgt_sizeof_void_p, 0);
2775 sec->header.sh_flags |= SHF_ALLOC;
2776 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2778 ofs = sec->header.sh_size;
2779 obj_symbol_patch(f, sec->idx, ofs, sym);
2780 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2781 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2783 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2785 static int new_create_module_ksymtab(struct obj_file *f)
2787 struct obj_section *sec;
2790 /* We must always add the module references. */
2792 if (n_ext_modules_used) {
2793 struct new_module_ref *dep;
2794 struct obj_symbol *tm;
2796 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2797 (sizeof(struct new_module_ref)
2798 * n_ext_modules_used));
2802 tm = obj_find_symbol(f, SPFX "__this_module");
2803 dep = (struct new_module_ref *) sec->contents;
2804 for (i = 0; i < n_ext_modules; ++i)
2805 if (ext_modules[i].used) {
2806 dep->dep = ext_modules[i].addr;
2807 obj_symbol_patch(f, sec->idx,
2808 (char *) &dep->ref - sec->contents, tm);
2814 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2819 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2822 /* We don't want to export symbols residing in sections that
2823 aren't loaded. There are a number of these created so that
2824 we make sure certain module options don't appear twice. */
2826 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2828 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2830 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2831 struct obj_symbol *sym;
2832 for (sym = f->symtab[i]; sym; sym = sym->next)
2833 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2834 && sym->secidx <= SHN_HIRESERVE
2835 && (sym->secidx >= SHN_LORESERVE
2836 || loaded[sym->secidx])) {
2837 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2839 obj_symbol_patch(f, sec->idx, ofs, sym);
2840 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2847 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2855 new_init_module(const char *m_name, struct obj_file *f,
2856 unsigned long m_size)
2858 struct new_module *module;
2859 struct obj_section *sec;
2864 sec = obj_find_section(f, ".this");
2865 if (!sec || !sec->contents) {
2866 perror_msg_and_die("corrupt module %s?",m_name);
2868 module = (struct new_module *) sec->contents;
2869 m_addr = sec->header.sh_addr;
2871 module->size_of_struct = sizeof(*module);
2872 module->size = m_size;
2873 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2875 sec = obj_find_section(f, "__ksymtab");
2876 if (sec && sec->header.sh_size) {
2877 module->syms = sec->header.sh_addr;
2878 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2881 if (n_ext_modules_used) {
2882 sec = obj_find_section(f, ".kmodtab");
2883 module->deps = sec->header.sh_addr;
2884 module->ndeps = n_ext_modules_used;
2888 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2890 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2892 sec = obj_find_section(f, "__ex_table");
2894 module->ex_table_start = sec->header.sh_addr;
2895 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2898 sec = obj_find_section(f, ".text.init");
2900 module->runsize = sec->header.sh_addr - m_addr;
2902 sec = obj_find_section(f, ".data.init");
2904 if (!module->runsize ||
2905 module->runsize > sec->header.sh_addr - m_addr)
2906 module->runsize = sec->header.sh_addr - m_addr;
2908 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2909 if (sec && sec->header.sh_size) {
2910 module->archdata_start = (void*)sec->header.sh_addr;
2911 module->archdata_end = module->archdata_start + sec->header.sh_size;
2913 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2914 if (sec && sec->header.sh_size) {
2915 module->kallsyms_start = (void*)sec->header.sh_addr;
2916 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2919 if (!arch_init_module(f, module))
2922 /* Whew! All of the initialization is complete. Collect the final
2923 module image and give it to the kernel. */
2925 image = xmalloc(m_size);
2926 obj_create_image(f, image);
2928 ret = new_sys_init_module(m_name, (struct new_module *) image);
2930 perror_msg("init_module: %s", m_name);
2939 #define new_init_module(x, y, z) TRUE
2940 #define new_create_this_module(x, y) 0
2941 #define new_add_ksymtab(x, y) -1
2942 #define new_create_module_ksymtab(x)
2943 #define query_module(v, w, x, y, z) -1
2945 #endif /* CONFIG_FEATURE_NEW_MODULE_INTERFACE */
2948 /*======================================================================*/
2951 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2954 struct obj_string_patch *p;
2955 struct obj_section *strsec;
2956 size_t len = strlen(string) + 1;
2959 p = xmalloc(sizeof(*p));
2960 p->next = f->string_patches;
2961 p->reloc_secidx = secidx;
2962 p->reloc_offset = offset;
2963 f->string_patches = p;
2965 strsec = obj_find_section(f, ".kstrtab");
2966 if (strsec == NULL) {
2967 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2968 p->string_offset = 0;
2969 loc = strsec->contents;
2971 p->string_offset = strsec->header.sh_size;
2972 loc = obj_extend_section(strsec, len);
2974 memcpy(loc, string, len);
2979 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2981 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2982 struct obj_symbol *sym)
2984 struct obj_symbol_patch *p;
2986 p = xmalloc(sizeof(*p));
2987 p->next = f->symbol_patches;
2988 p->reloc_secidx = secidx;
2989 p->reloc_offset = offset;
2991 f->symbol_patches = p;
2997 static int obj_check_undefineds(struct obj_file *f)
3002 for (i = 0; i < HASH_BUCKETS; ++i) {
3003 struct obj_symbol *sym;
3004 for (sym = f->symtab[i]; sym; sym = sym->next)
3005 if (sym->secidx == SHN_UNDEF) {
3006 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
3007 sym->secidx = SHN_ABS;
3011 error_msg("unresolved symbol %s", sym->name);
3021 static void obj_allocate_commons(struct obj_file *f)
3023 struct common_entry {
3024 struct common_entry *next;
3025 struct obj_symbol *sym;
3026 } *common_head = NULL;
3030 for (i = 0; i < HASH_BUCKETS; ++i) {
3031 struct obj_symbol *sym;
3032 for (sym = f->symtab[i]; sym; sym = sym->next)
3033 if (sym->secidx == SHN_COMMON) {
3034 /* Collect all COMMON symbols and sort them by size so as to
3035 minimize space wasted by alignment requirements. */
3037 struct common_entry **p, *n;
3038 for (p = &common_head; *p; p = &(*p)->next)
3039 if (sym->size <= (*p)->sym->size)
3042 n = alloca(sizeof(*n));
3050 for (i = 1; i < f->local_symtab_size; ++i) {
3051 struct obj_symbol *sym = f->local_symtab[i];
3052 if (sym && sym->secidx == SHN_COMMON) {
3053 struct common_entry **p, *n;
3054 for (p = &common_head; *p; p = &(*p)->next)
3055 if (sym == (*p)->sym)
3057 else if (sym->size < (*p)->sym->size) {
3058 n = alloca(sizeof(*n));
3068 /* Find the bss section. */
3069 for (i = 0; i < f->header.e_shnum; ++i)
3070 if (f->sections[i]->header.sh_type == SHT_NOBITS)
3073 /* If for some reason there hadn't been one, create one. */
3074 if (i == f->header.e_shnum) {
3075 struct obj_section *sec;
3077 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
3078 f->sections[i] = sec = arch_new_section();
3079 f->header.e_shnum = i + 1;
3081 memset(sec, 0, sizeof(*sec));
3082 sec->header.sh_type = SHT_PROGBITS;
3083 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
3088 /* Allocate the COMMONS. */
3090 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
3091 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
3092 struct common_entry *c;
3094 for (c = common_head; c; c = c->next) {
3095 ElfW(Addr) align = c->sym->value;
3097 if (align > max_align)
3099 if (bss_size & (align - 1))
3100 bss_size = (bss_size | (align - 1)) + 1;
3103 c->sym->value = bss_size;
3105 bss_size += c->sym->size;
3108 f->sections[i]->header.sh_size = bss_size;
3109 f->sections[i]->header.sh_addralign = max_align;
3113 /* For the sake of patch relocation and parameter initialization,
3114 allocate zeroed data for NOBITS sections now. Note that after
3115 this we cannot assume NOBITS are really empty. */
3116 for (i = 0; i < f->header.e_shnum; ++i) {
3117 struct obj_section *s = f->sections[i];
3118 if (s->header.sh_type == SHT_NOBITS) {
3119 if (s->header.sh_size != 0)
3120 s->contents = memset(xmalloc(s->header.sh_size),
3121 0, s->header.sh_size);
3125 s->header.sh_type = SHT_PROGBITS;
3130 static unsigned long obj_load_size(struct obj_file *f)
3132 unsigned long dot = 0;
3133 struct obj_section *sec;
3135 /* Finalize the positions of the sections relative to one another. */
3137 for (sec = f->load_order; sec; sec = sec->load_next) {
3140 align = sec->header.sh_addralign;
3141 if (align && (dot & (align - 1)))
3142 dot = (dot | (align - 1)) + 1;
3144 sec->header.sh_addr = dot;
3145 dot += sec->header.sh_size;
3151 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3153 int i, n = f->header.e_shnum;
3156 /* Finalize the addresses of the sections. */
3159 for (i = 0; i < n; ++i)
3160 f->sections[i]->header.sh_addr += base;
3162 /* And iterate over all of the relocations. */
3164 for (i = 0; i < n; ++i) {
3165 struct obj_section *relsec, *symsec, *targsec, *strsec;
3166 ElfW(RelM) * rel, *relend;
3170 relsec = f->sections[i];
3171 if (relsec->header.sh_type != SHT_RELM)
3174 symsec = f->sections[relsec->header.sh_link];
3175 targsec = f->sections[relsec->header.sh_info];
3176 strsec = f->sections[symsec->header.sh_link];
3178 rel = (ElfW(RelM) *) relsec->contents;
3179 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3180 symtab = (ElfW(Sym) *) symsec->contents;
3181 strtab = (const char *) strsec->contents;
3183 for (; rel < relend; ++rel) {
3184 ElfW(Addr) value = 0;
3185 struct obj_symbol *intsym = NULL;
3186 unsigned long symndx;
3187 ElfW(Sym) * extsym = 0;
3190 /* Attempt to find a value to use for this relocation. */
3192 symndx = ELFW(R_SYM) (rel->r_info);
3194 /* Note we've already checked for undefined symbols. */
3196 extsym = &symtab[symndx];
3197 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
3198 /* Local symbols we look up in the local table to be sure
3199 we get the one that is really intended. */
3200 intsym = f->local_symtab[symndx];
3202 /* Others we look up in the hash table. */
3204 if (extsym->st_name)
3205 name = strtab + extsym->st_name;
3207 name = f->sections[extsym->st_shndx]->name;
3208 intsym = obj_find_symbol(f, name);
3211 value = obj_symbol_final_value(f, intsym);
3212 intsym->referenced = 1;
3214 #if SHT_RELM == SHT_RELA
3215 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3216 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3217 if (!extsym || !extsym->st_name ||
3218 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
3220 value += rel->r_addend;
3224 switch (arch_apply_relocation
3225 (f, targsec, symsec, intsym, rel, value)) {
3229 case obj_reloc_overflow:
3230 errmsg = "Relocation overflow";
3232 case obj_reloc_dangerous:
3233 errmsg = "Dangerous relocation";
3235 case obj_reloc_unhandled:
3236 errmsg = "Unhandled relocation";
3239 error_msg("%s of type %ld for %s", errmsg,
3240 (long) ELFW(R_TYPE) (rel->r_info),
3241 strtab + extsym->st_name);
3243 error_msg("%s of type %ld", errmsg,
3244 (long) ELFW(R_TYPE) (rel->r_info));
3252 /* Finally, take care of the patches. */
3254 if (f->string_patches) {
3255 struct obj_string_patch *p;
3256 struct obj_section *strsec;
3257 ElfW(Addr) strsec_base;
3258 strsec = obj_find_section(f, ".kstrtab");
3259 strsec_base = strsec->header.sh_addr;
3261 for (p = f->string_patches; p; p = p->next) {
3262 struct obj_section *targsec = f->sections[p->reloc_secidx];
3263 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3264 = strsec_base + p->string_offset;
3268 if (f->symbol_patches) {
3269 struct obj_symbol_patch *p;
3271 for (p = f->symbol_patches; p; p = p->next) {
3272 struct obj_section *targsec = f->sections[p->reloc_secidx];
3273 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3274 = obj_symbol_final_value(f, p->sym);
3281 static int obj_create_image(struct obj_file *f, char *image)
3283 struct obj_section *sec;
3284 ElfW(Addr) base = f->baseaddr;
3286 for (sec = f->load_order; sec; sec = sec->load_next) {
3289 if (sec->contents == 0 || sec->header.sh_size == 0)
3292 secimg = image + (sec->header.sh_addr - base);
3294 /* Note that we allocated data for NOBITS sections earlier. */
3295 memcpy(secimg, sec->contents, sec->header.sh_size);
3301 /*======================================================================*/
3303 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3306 ElfW(Shdr) * section_headers;
3310 /* Read the file header. */
3312 f = arch_new_file();
3313 memset(f, 0, sizeof(*f));
3314 f->symbol_cmp = strcmp;
3315 f->symbol_hash = obj_elf_hash;
3316 f->load_order_search_start = &f->load_order;
3318 fseek(fp, 0, SEEK_SET);
3319 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3320 perror_msg("error reading ELF header");
3324 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3325 || f->header.e_ident[EI_MAG1] != ELFMAG1
3326 || f->header.e_ident[EI_MAG2] != ELFMAG2
3327 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3328 error_msg("not an ELF file");
3331 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3332 || f->header.e_ident[EI_DATA] != ELFDATAM
3333 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3334 || !MATCH_MACHINE(f->header.e_machine)) {
3335 error_msg("ELF file not for this architecture");
3338 if (f->header.e_type != ET_REL) {
3339 error_msg("ELF file not a relocatable object");
3343 /* Read the section headers. */
3345 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3346 error_msg("section header size mismatch: %lu != %lu",
3347 (unsigned long) f->header.e_shentsize,
3348 (unsigned long) sizeof(ElfW(Shdr)));
3352 shnum = f->header.e_shnum;
3353 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3354 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3356 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3357 fseek(fp, f->header.e_shoff, SEEK_SET);
3358 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3359 perror_msg("error reading ELF section headers");
3363 /* Read the section data. */
3365 for (i = 0; i < shnum; ++i) {
3366 struct obj_section *sec;
3368 f->sections[i] = sec = arch_new_section();
3369 memset(sec, 0, sizeof(*sec));
3371 sec->header = section_headers[i];
3374 if(sec->header.sh_size) switch (sec->header.sh_type) {
3383 if (!loadprogbits) {
3384 sec->contents = NULL;
3391 if (sec->header.sh_size > 0) {
3392 sec->contents = xmalloc(sec->header.sh_size);
3393 fseek(fp, sec->header.sh_offset, SEEK_SET);
3394 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3395 perror_msg("error reading ELF section data");
3399 sec->contents = NULL;
3403 #if SHT_RELM == SHT_REL
3405 error_msg("RELA relocations not supported on this architecture");
3409 error_msg("REL relocations not supported on this architecture");
3414 if (sec->header.sh_type >= SHT_LOPROC) {
3415 /* Assume processor specific section types are debug
3416 info and can safely be ignored. If this is ever not
3417 the case (Hello MIPS?), don't put ifdefs here but
3418 create an arch_load_proc_section(). */
3422 error_msg("can't handle sections of type %ld",
3423 (long) sec->header.sh_type);
3428 /* Do what sort of interpretation as needed by each section. */
3430 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3432 for (i = 0; i < shnum; ++i) {
3433 struct obj_section *sec = f->sections[i];
3434 sec->name = shstrtab + sec->header.sh_name;
3437 for (i = 0; i < shnum; ++i) {
3438 struct obj_section *sec = f->sections[i];
3440 /* .modinfo should be contents only but gcc has no attribute for that.
3441 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3443 if (strcmp(sec->name, ".modinfo") == 0)
3444 sec->header.sh_flags &= ~SHF_ALLOC;
3446 if (sec->header.sh_flags & SHF_ALLOC)
3447 obj_insert_section_load_order(f, sec);
3449 switch (sec->header.sh_type) {
3452 unsigned long nsym, j;
3456 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3457 error_msg("symbol size mismatch: %lu != %lu",
3458 (unsigned long) sec->header.sh_entsize,
3459 (unsigned long) sizeof(ElfW(Sym)));
3463 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3464 strtab = f->sections[sec->header.sh_link]->contents;
3465 sym = (ElfW(Sym) *) sec->contents;
3467 /* Allocate space for a table of local symbols. */
3468 j = f->local_symtab_size = sec->header.sh_info;
3469 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3471 /* Insert all symbols into the hash table. */
3472 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3475 name = strtab + sym->st_name;
3477 name = f->sections[sym->st_shndx]->name;
3479 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3480 sym->st_value, sym->st_size);
3486 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3487 error_msg("relocation entry size mismatch: %lu != %lu",
3488 (unsigned long) sec->header.sh_entsize,
3489 (unsigned long) sizeof(ElfW(RelM)));
3493 /* XXX Relocation code from modutils-2.3.19 is not here.
3494 * Why? That's about 20 lines of code from obj/obj_load.c,
3495 * which gets done in a second pass through the sections.
3496 * This BusyBox insmod does similar work in obj_relocate(). */
3503 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3505 * load the unloaded sections directly into the memory allocated by
3506 * kernel for the module
3509 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3511 ElfW(Addr) base = f->baseaddr;
3512 struct obj_section* sec;
3514 for (sec = f->load_order; sec; sec = sec->load_next) {
3516 /* section already loaded? */
3517 if (sec->contents != NULL)
3520 if (sec->header.sh_size == 0)
3523 sec->contents = imagebase + (sec->header.sh_addr - base);
3524 fseek(fp, sec->header.sh_offset, SEEK_SET);
3525 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3526 error_msg("error reading ELF section data: %s\n", strerror(errno));
3535 static void hide_special_symbols(struct obj_file *f)
3537 static const char *const specials[] = {
3538 SPFX "cleanup_module",
3540 SPFX "kernel_version",
3544 struct obj_symbol *sym;
3545 const char *const *p;
3547 for (p = specials; *p; ++p)
3548 if ((sym = obj_find_symbol(f, *p)) != NULL)
3550 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3553 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3554 static int obj_gpl_license(struct obj_file *f, const char **license)
3556 struct obj_section *sec;
3557 /* This list must match *exactly* the list of allowable licenses in
3558 * linux/include/linux/module.h. Checking for leading "GPL" will not
3559 * work, somebody will use "GPL sucks, this is proprietary".
3561 static const char *gpl_licenses[] = {
3564 "GPL and additional rights",
3569 if ((sec = obj_find_section(f, ".modinfo"))) {
3570 const char *value, *ptr, *endptr;
3571 ptr = sec->contents;
3572 endptr = ptr + sec->header.sh_size;
3573 while (ptr < endptr) {
3574 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3578 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3579 if (strcmp(value+1, gpl_licenses[i]) == 0)
3584 if (strchr(ptr, '\0'))
3585 ptr = strchr(ptr, '\0') + 1;
3593 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3594 #define TAINT_PROPRIETORY_MODULE (1<<0)
3595 #define TAINT_FORCED_MODULE (1<<1)
3596 #define TAINT_UNSAFE_SMP (1<<2)
3597 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3599 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3600 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3604 static int first = 1;
3605 if (fd < 0 && !kernel_has_tainted)
3606 return; /* New modutils on old kernel */
3607 printf("Warning: loading %s will taint the kernel: %s%s\n",
3608 m_name, text1, text2);
3610 printf(" See %s for information about tainted modules\n", TAINT_URL);
3614 read(fd, buf, sizeof(buf)-1);
3615 buf[sizeof(buf)-1] = '\0';
3616 oldval = strtoul(buf, NULL, 10);
3617 sprintf(buf, "%d\n", oldval | taint);
3618 write(fd, buf, strlen(buf));
3622 /* Check if loading this module will taint the kernel. */
3623 static void check_tainted_module(struct obj_file *f, char *m_name)
3625 static const char tainted_file[] = TAINT_FILENAME;
3626 int fd, kernel_has_tainted;
3629 kernel_has_tainted = 1;
3630 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3631 if (errno == ENOENT)
3632 kernel_has_tainted = 0;
3633 else if (errno == EACCES)
3634 kernel_has_tainted = 1;
3636 perror(tainted_file);
3637 kernel_has_tainted = 0;
3641 switch (obj_gpl_license(f, &ptr)) {
3645 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3648 /* The module has a non-GPL license so we pretend that the
3649 * kernel always has a taint flag to get a warning even on
3650 * kernels without the proc flag.
3652 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3655 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3659 if (flag_force_load)
3660 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3665 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3666 #define check_tainted_module(x, y) do { } while(0);
3667 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3669 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3670 /* add module source, timestamp, kernel version and a symbol for the
3671 * start of some sections. this info is used by ksymoops to do better
3675 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3677 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3678 if (get_modinfo_value(f, "kernel_version") == NULL)
3679 return old_get_module_version(f, str);
3681 return new_get_module_version(f, str);
3682 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3683 strncpy(str, "???", sizeof(str));
3685 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3688 /* add module source, timestamp, kernel version and a symbol for the
3689 * start of some sections. this info is used by ksymoops to do better
3693 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3696 static const char symprefix[] = "__insmod_";
3697 struct obj_section *sec;
3698 struct obj_symbol *sym;
3699 char *name, *absolute_filename;
3700 char str[STRVERSIONLEN], real[PATH_MAX];
3701 int i, l, lm_name, lfilename, use_ksymtab, version;
3702 struct stat statbuf;
3704 static const char *section_names[] = {
3712 if (realpath(filename, real)) {
3713 absolute_filename = xstrdup(real);
3716 int save_errno = errno;
3717 error_msg("cannot get realpath for %s", filename);
3720 absolute_filename = xstrdup(filename);
3723 lm_name = strlen(m_name);
3724 lfilename = strlen(absolute_filename);
3726 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3727 * are not to be exported. otherwise leave ksymtab alone for now, the
3728 * "export all symbols" compatibility code will export these symbols later.
3730 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3732 if ((sec = obj_find_section(f, ".this"))) {
3733 /* tag the module header with the object name, last modified
3734 * timestamp and module version. worst case for module version
3735 * is 0xffffff, decimal 16777215. putting all three fields in
3736 * one symbol is less readable but saves kernel space.
3738 l = sizeof(symprefix)+ /* "__insmod_" */
3739 lm_name+ /* module name */
3741 lfilename+ /* object filename */
3743 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3745 8+ /* version in dec */
3748 if (stat(absolute_filename, &statbuf) != 0)
3749 statbuf.st_mtime = 0;
3750 version = get_module_version(f, str); /* -1 if not found */
3751 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3752 symprefix, m_name, absolute_filename,
3753 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3755 sym = obj_add_symbol(f, name, -1,
3756 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3757 sec->idx, sec->header.sh_addr, 0);
3759 new_add_ksymtab(f, sym);
3761 free(absolute_filename);
3762 #ifdef _NOT_SUPPORTED_
3763 /* record where the persistent data is going, same address as previous symbol */
3766 l = sizeof(symprefix)+ /* "__insmod_" */
3767 lm_name+ /* module name */
3769 strlen(f->persist)+ /* data store */
3772 snprintf(name, l, "%s%s_P%s",
3773 symprefix, m_name, f->persist);
3774 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3775 sec->idx, sec->header.sh_addr, 0);
3777 new_add_ksymtab(f, sym);
3779 #endif /* _NOT_SUPPORTED_ */
3780 /* tag the desired sections if size is non-zero */
3782 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3783 if ((sec = obj_find_section(f, section_names[i])) &&
3784 sec->header.sh_size) {
3785 l = sizeof(symprefix)+ /* "__insmod_" */
3786 lm_name+ /* module name */
3788 strlen(sec->name)+ /* section name */
3790 8+ /* length in dec */
3793 snprintf(name, l, "%s%s_S%s_L%ld",
3794 symprefix, m_name, sec->name,
3795 (long)sec->header.sh_size);
3796 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3797 sec->idx, sec->header.sh_addr, 0);
3799 new_add_ksymtab(f, sym);
3803 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3805 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3806 static void print_load_map(struct obj_file *f)
3808 struct obj_symbol *sym;
3809 struct obj_symbol **all, **p;
3810 struct obj_section *sec;
3811 int i, nsyms, *loaded;
3813 /* Report on the section layout. */
3815 printf("Sections: Size %-*s Align\n",
3816 (int) (2 * sizeof(void *)), "Address");
3818 for (sec = f->load_order; sec; sec = sec->load_next) {
3822 for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
3827 printf("%-15s %08lx %0*lx 2**%d\n",
3829 (long)sec->header.sh_size,
3830 (int) (2 * sizeof(void *)),
3831 (long)sec->header.sh_addr,
3834 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
3835 /* Quick reference which section indicies are loaded. */
3837 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
3839 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
3841 /* Collect the symbols we'll be listing. */
3843 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3844 for (sym = f->symtab[i]; sym; sym = sym->next)
3845 if (sym->secidx <= SHN_HIRESERVE
3846 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3849 all = alloca(nsyms * sizeof(struct obj_symbol *));
3851 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3852 for (sym = f->symtab[i]; sym; sym = sym->next)
3853 if (sym->secidx <= SHN_HIRESERVE
3854 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
3857 /* And list them. */
3858 printf("\nSymbols:\n");
3859 for (p = all; p < all + nsyms; ++p) {
3861 unsigned long value;
3864 if (sym->secidx == SHN_ABS) {
3867 } else if (sym->secidx == SHN_UNDEF) {
3871 sec = f->sections[sym->secidx];
3873 if (sec->header.sh_type == SHT_NOBITS)
3875 else if (sec->header.sh_flags & SHF_ALLOC) {
3876 if (sec->header.sh_flags & SHF_EXECINSTR)
3878 else if (sec->header.sh_flags & SHF_WRITE)
3883 value = sym->value + sec->header.sh_addr;
3886 if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
3887 type = tolower(type);
3889 printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
3897 extern int insmod_main( int argc, char **argv)
3904 unsigned long m_size;
3909 int exit_status = EXIT_FAILURE;
3911 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3912 struct utsname uts_info;
3913 char m_strversion[STRVERSIONLEN];
3917 #ifdef CONFIG_FEATURE_CLEAN_UP
3922 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3923 int flag_print_load_map = 0;
3926 /* Parse any options */
3927 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3928 while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0) {
3930 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0) {
3933 case 'f': /* force loading */
3934 flag_force_load = 1;
3936 case 'k': /* module loaded by kerneld, auto-cleanable */
3939 case 's': /* log to syslog */
3940 /* log to syslog -- not supported */
3941 /* but kernel needs this for request_module(), */
3942 /* as this calls: modprobe -k -s -- <module> */
3943 /* so silently ignore this flag */
3945 case 'v': /* verbose output */
3948 case 'q': /* silent */
3951 case 'x': /* do not export externs */
3954 case 'o': /* name the output module */
3956 m_name = xstrdup(optarg);
3958 case 'L': /* Stub warning */
3959 /* This is needed for compatibility with modprobe.
3960 * In theory, this does locking, but we don't do
3961 * that. So be careful and plan your life around not
3962 * loading the same module 50 times concurrently. */
3964 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
3965 case 'm': /* print module load map */
3966 flag_print_load_map = 1;
3974 if (argv[optind] == NULL) {
3978 /* Grab the module name */
3979 tmp1 = xstrdup(argv[optind]);
3980 tmp = basename(tmp1);
3983 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3988 bb_asprintf(&m_fullName, "%s.o", tmp);
3994 tmp1 = 0; /* flag for free(m_name) before exit() */
3997 /* Get a filedesc for the module. Check we we have a complete path */
3998 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3999 (fp = fopen(argv[optind], "r")) == NULL) {
4000 struct utsname myuname;
4002 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
4003 * but do not error out yet if we fail to find it... */
4004 if (uname(&myuname) == 0) {
4007 char real_module_dir[FILENAME_MAX];
4009 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
4010 /* Jump through hoops in case /lib/modules/`uname -r`
4011 * is a symlink. We do not want recursive_action to
4012 * follow symlinks, but we do want to follow the
4013 * /lib/modules/`uname -r` dir, So resolve it ourselves
4014 * if it is a link... */
4015 if (realpath (tmdn, real_module_dir) == NULL)
4018 module_dir = real_module_dir;
4019 recursive_action(module_dir, TRUE, FALSE, FALSE,
4020 check_module_name_match, 0, m_fullName);
4024 /* Check if we have found anything yet */
4025 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
4027 char module_dir[FILENAME_MAX];
4031 if (realpath (_PATH_MODULES, module_dir) == NULL)
4032 strcpy(module_dir, _PATH_MODULES);
4033 /* No module found under /lib/modules/`uname -r`, this
4034 * time cast the net a bit wider. Search /lib/modules/ */
4035 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
4036 check_module_name_match, 0, m_fullName))
4039 || ((fp = fopen(m_filename, "r")) == NULL))
4041 error_msg("%s: no module by that name found", m_fullName);
4045 error_msg_and_die("%s: no module by that name found", m_fullName);
4048 m_filename = xstrdup(argv[optind]);
4050 printf("Using %s\n", m_filename);
4052 if ((f = obj_load(fp, LOADBITS)) == NULL)
4053 perror_msg_and_die("Could not load the module");
4055 if (get_modinfo_value(f, "kernel_version") == NULL)
4060 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4061 /* Version correspondence? */
4063 if (uname(&uts_info) < 0)
4064 uts_info.release[0] = '\0';
4065 if (m_has_modinfo) {
4066 m_version = new_get_module_version(f, m_strversion);
4068 m_version = old_get_module_version(f, m_strversion);
4069 if (m_version == -1) {
4070 error_msg("couldn't find the kernel version the module was "
4076 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4077 if (flag_force_load) {
4078 error_msg("Warning: kernel-module version mismatch\n"
4079 "\t%s was compiled for kernel version %s\n"
4080 "\twhile this kernel is version %s",
4081 m_filename, m_strversion, uts_info.release);
4083 error_msg("kernel-module version mismatch\n"
4084 "\t%s was compiled for kernel version %s\n"
4085 "\twhile this kernel is version %s.",
4086 m_filename, m_strversion, uts_info.release);
4092 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4094 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
4096 if (k_new_syscalls) {
4097 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
4098 if (!new_get_kernel_symbols())
4100 k_crcs = new_is_kernel_checksummed();
4102 error_msg("Not configured to support new kernels");
4106 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
4107 if (!old_get_kernel_symbols(m_name))
4109 k_crcs = old_is_kernel_checksummed();
4111 error_msg("Not configured to support old kernels");
4116 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
4118 m_crcs = new_is_module_checksummed(f);
4120 m_crcs = old_is_module_checksummed(f);
4122 if (m_crcs != k_crcs)
4123 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
4124 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
4126 /* Let the module know about the kernel symbols. */
4127 add_kernel_symbols(f);
4129 /* Allocate common symbols, symbol tables, and string tables. */
4132 ? !new_create_this_module(f, m_name)
4133 : !old_create_mod_use_count(f))
4138 if (!obj_check_undefineds(f)) {
4141 obj_allocate_commons(f);
4142 check_tainted_module(f, m_name);
4144 /* done with the module name, on to the optional var=value arguments */
4147 if (optind < argc) {
4149 ? !new_process_module_arguments(f, argc - optind, argv + optind)
4150 : !old_process_module_arguments(f, argc - optind, argv + optind))
4157 hide_special_symbols(f);
4159 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
4160 add_ksymoops_symbols(f, m_filename, m_name);
4161 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
4164 new_create_module_ksymtab(f);
4166 /* Find current size of the module */
4167 m_size = obj_load_size(f);
4170 m_addr = create_module(m_name, m_size);
4171 if (m_addr == -1) switch (errno) {
4173 error_msg("A module named %s already exists", m_name);
4176 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4180 perror_msg("create_module: %s", m_name);
4186 * the PROGBITS section was not loaded by the obj_load
4187 * now we can load them directly into the kernel memory
4189 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
4190 delete_module(m_name);
4195 if (!obj_relocate(f, m_addr)) {
4196 delete_module(m_name);
4201 ? !new_init_module(m_name, f, m_size)
4202 : !old_init_module(m_name, f, m_size))
4204 delete_module(m_name);
4208 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
4209 if(flag_print_load_map)
4213 exit_status = EXIT_SUCCESS;
4216 #ifdef CONFIG_FEATURE_CLEAN_UP
4226 return(exit_status);