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(__i386__)
106 #define CONFIG_USE_GOT_ENTRIES
107 #define CONFIG_GOT_ENTRY_SIZE 4
108 #define CONFIG_USE_SINGLE
111 #define MATCH_MACHINE(x) (x == EM_386)
113 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
116 #define SHT_RELM SHT_REL
117 #define Elf32_RelM Elf32_Rel
118 #define ELFCLASSM ELFCLASS32
121 #if defined(__mc68000__)
122 #define CONFIG_USE_GOT_ENTRIES
123 #define CONFIG_GOT_ENTRY_SIZE 4
124 #define CONFIG_USE_SINGLE
126 #define MATCH_MACHINE(x) (x == EM_68K)
127 #define SHT_RELM SHT_RELA
128 #define Elf32_RelM Elf32_Rela
131 #if defined(__mips__)
132 /* Account for ELF spec changes. */
133 #ifndef EM_MIPS_RS3_LE
134 #ifdef EM_MIPS_RS4_BE
135 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
137 #define EM_MIPS_RS3_LE 10
139 #endif /* !EM_MIPS_RS3_LE */
141 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
142 #define SHT_RELM SHT_REL
143 #define Elf32_RelM Elf32_Rel
144 #define ELFCLASSM ELFCLASS32
145 #define ARCHDATAM "__dbe_table"
148 #if defined(__powerpc__)
149 #define CONFIG_USE_PLT_ENTRIES
150 #define CONFIG_PLT_ENTRY_SIZE 16
151 #define CONFIG_USE_PLT_LIST
152 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
153 #define CONFIG_USE_LIST
155 #define MATCH_MACHINE(x) (x == EM_PPC)
156 #define SHT_RELM SHT_RELA
157 #define Elf32_RelM Elf32_Rela
158 #define ELFCLASSM ELFCLASS32
159 #define ARCHDATAM "__ftr_fixup"
163 #define CONFIG_USE_GOT_ENTRIES
164 #define CONFIG_GOT_ENTRY_SIZE 4
165 #define CONFIG_USE_SINGLE
167 #define MATCH_MACHINE(x) (x == EM_SH)
168 #define SHT_RELM SHT_RELA
169 #define Elf32_RelM Elf32_Rela
170 #define ELFCLASSM ELFCLASS32
172 /* the SH changes have only been tested on the SH4 in =little endian= mode */
173 /* I'm not sure about big endian, so let's warn: */
175 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
176 #error insmod.c may require changes for use on big endian SH4/SH3
179 /* it may or may not work on the SH1/SH2... So let's error on those
181 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
182 #error insmod.c may require changes for non-SH3/SH4 use
186 #if defined (__v850e__)
187 #define CONFIG_USE_PLT_ENTRIES
188 #define CONFIG_PLT_ENTRY_SIZE 8
189 #define CONFIG_USE_SINGLE
191 #ifndef EM_CYGNUS_V850 /* grumble */
192 #define EM_CYGNUS_V850 0x9080
195 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
196 #define SHT_RELM SHT_RELA
197 #define Elf32_RelM Elf32_Rela
198 #define ELFCLASSM ELFCLASS32
200 #define SYMBOL_PREFIX "_"
204 #error Sorry, but insmod.c does not yet support this architecture...
208 //----------------------------------------------------------------------------
209 //--------modutils module.h, lines 45-242
210 //----------------------------------------------------------------------------
212 /* Definitions for the Linux module syscall interface.
213 Copyright 1996, 1997 Linux International.
215 Contributed by Richard Henderson <rth@tamu.edu>
217 This file is part of the Linux modutils.
219 This program is free software; you can redistribute it and/or modify it
220 under the terms of the GNU General Public License as published by the
221 Free Software Foundation; either version 2 of the License, or (at your
222 option) any later version.
224 This program is distributed in the hope that it will be useful, but
225 WITHOUT ANY WARRANTY; without even the implied warranty of
226 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
227 General Public License for more details.
229 You should have received a copy of the GNU General Public License
230 along with this program; if not, write to the Free Software Foundation,
231 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
234 #ifndef MODUTILS_MODULE_H
235 static const int MODUTILS_MODULE_H = 1;
237 #ident "$Id: insmod.c,v 1.93 2003/01/23 04:48:34 andersen Exp $"
239 /* This file contains the structures used by the 2.0 and 2.1 kernels.
240 We do not use the kernel headers directly because we do not wish
241 to be dependant on a particular kernel version to compile insmod. */
244 /*======================================================================*/
245 /* The structures used by Linux 2.0. */
247 /* The symbol format used by get_kernel_syms(2). */
248 struct old_kernel_sym
254 struct old_module_ref
256 unsigned long module; /* kernel addresses */
260 struct old_module_symbol
266 struct old_symbol_table
268 int size; /* total, including string table!!! */
271 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
272 struct old_module_ref ref[0]; /* actual size defined by n_refs */
275 struct old_mod_routines
278 unsigned long cleanup;
284 unsigned long ref; /* the list of modules that refer to me */
285 unsigned long symtab;
287 int size; /* size of module in pages */
288 unsigned long addr; /* address of module */
290 unsigned long cleanup; /* cleanup routine */
293 /* Sent to init_module(2) or'ed into the code size parameter. */
294 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
296 int get_kernel_syms(struct old_kernel_sym *);
297 int old_sys_init_module(const char *name, char *code, unsigned codesize,
298 struct old_mod_routines *, struct old_symbol_table *);
300 /*======================================================================*/
301 /* For sizeof() which are related to the module platform and not to the
302 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
304 #define tgt_sizeof_char sizeof(char)
305 #define tgt_sizeof_short sizeof(short)
306 #define tgt_sizeof_int sizeof(int)
307 #define tgt_sizeof_long sizeof(long)
308 #define tgt_sizeof_char_p sizeof(char *)
309 #define tgt_sizeof_void_p sizeof(void *)
310 #define tgt_long long
312 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
313 #undef tgt_sizeof_long
314 #undef tgt_sizeof_char_p
315 #undef tgt_sizeof_void_p
317 static const int tgt_sizeof_long = 8;
318 static const int tgt_sizeof_char_p = 8;
319 static const int tgt_sizeof_void_p = 8;
320 #define tgt_long long long
323 /*======================================================================*/
324 /* The structures used in Linux 2.1. */
326 /* Note: new_module_symbol does not use tgt_long intentionally */
327 struct new_module_symbol
333 struct new_module_persist;
335 struct new_module_ref
337 unsigned tgt_long dep; /* kernel addresses */
338 unsigned tgt_long ref;
339 unsigned tgt_long next_ref;
344 unsigned tgt_long size_of_struct; /* == sizeof(module) */
345 unsigned tgt_long next;
346 unsigned tgt_long name;
347 unsigned tgt_long size;
350 unsigned tgt_long flags; /* AUTOCLEAN et al */
355 unsigned tgt_long syms;
356 unsigned tgt_long deps;
357 unsigned tgt_long refs;
358 unsigned tgt_long init;
359 unsigned tgt_long cleanup;
360 unsigned tgt_long ex_table_start;
361 unsigned tgt_long ex_table_end;
363 unsigned tgt_long gp;
365 /* Everything after here is extension. */
366 unsigned tgt_long persist_start;
367 unsigned tgt_long persist_end;
368 unsigned tgt_long can_unload;
369 unsigned tgt_long runsize;
370 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
371 const char *kallsyms_start; /* All symbols for kernel debugging */
372 const char *kallsyms_end;
373 const char *archdata_start; /* arch specific data for module */
374 const char *archdata_end;
375 const char *kernel_data; /* Reserved for kernel internal use */
380 #define ARCHDATA_SEC_NAME ARCHDATAM
382 #define ARCHDATA_SEC_NAME "__archdata"
384 #define KALLSYMS_SEC_NAME "__kallsyms"
387 struct new_module_info
395 /* Bits of module.flags. */
396 static const int NEW_MOD_RUNNING = 1;
397 static const int NEW_MOD_DELETED = 2;
398 static const int NEW_MOD_AUTOCLEAN = 4;
399 static const int NEW_MOD_VISITED = 8;
400 static const int NEW_MOD_USED_ONCE = 16;
402 int new_sys_init_module(const char *name, const struct new_module *);
403 int query_module(const char *name, int which, void *buf, size_t bufsize,
406 /* Values for query_module's which. */
408 static const int QM_MODULES = 1;
409 static const int QM_DEPS = 2;
410 static const int QM_REFS = 3;
411 static const int QM_SYMBOLS = 4;
412 static const int QM_INFO = 5;
414 /*======================================================================*/
415 /* The system calls unchanged between 2.0 and 2.1. */
417 unsigned long create_module(const char *, size_t);
418 int delete_module(const char *);
421 #endif /* module.h */
423 //----------------------------------------------------------------------------
424 //--------end of modutils module.h
425 //----------------------------------------------------------------------------
429 //----------------------------------------------------------------------------
430 //--------modutils obj.h, lines 253-462
431 //----------------------------------------------------------------------------
433 /* Elf object file loading and relocation routines.
434 Copyright 1996, 1997 Linux International.
436 Contributed by Richard Henderson <rth@tamu.edu>
438 This file is part of the Linux modutils.
440 This program is free software; you can redistribute it and/or modify it
441 under the terms of the GNU General Public License as published by the
442 Free Software Foundation; either version 2 of the License, or (at your
443 option) any later version.
445 This program is distributed in the hope that it will be useful, but
446 WITHOUT ANY WARRANTY; without even the implied warranty of
447 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
448 General Public License for more details.
450 You should have received a copy of the GNU General Public License
451 along with this program; if not, write to the Free Software Foundation,
452 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
455 #ifndef MODUTILS_OBJ_H
456 static const int MODUTILS_OBJ_H = 1;
458 #ident "$Id: insmod.c,v 1.93 2003/01/23 04:48:34 andersen Exp $"
460 /* The relocatable object is manipulated using elfin types. */
466 #if __BYTE_ORDER == __LITTLE_ENDIAN
467 #define ELFDATAM ELFDATA2LSB
468 #elif __BYTE_ORDER == __BIG_ENDIAN
469 #define ELFDATAM ELFDATA2MSB
473 # if ELFCLASSM == ELFCLASS32
474 # define ElfW(x) Elf32_ ## x
475 # define ELFW(x) ELF32_ ## x
477 # define ElfW(x) Elf64_ ## x
478 # define ELFW(x) ELF64_ ## x
482 /* For some reason this is missing from libc5. */
483 #ifndef ELF32_ST_INFO
484 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
487 #ifndef ELF64_ST_INFO
488 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
491 struct obj_string_patch;
492 struct obj_symbol_patch;
499 struct obj_section *load_next;
505 struct obj_symbol *next; /* hash table link */
509 int secidx; /* the defining section index/module */
511 int ksymidx; /* for export to the kernel symtab */
512 int referenced; /* actually used in the link */
515 /* Hardcode the hash table size. We shouldn't be needing so many
516 symbols that we begin to degrade performance, and we get a big win
517 by giving the compiler a constant divisor. */
519 #define HASH_BUCKETS 521
525 struct obj_section **sections;
526 struct obj_section *load_order;
527 struct obj_section **load_order_search_start;
528 struct obj_string_patch *string_patches;
529 struct obj_symbol_patch *symbol_patches;
530 int (*symbol_cmp)(const char *, const char *);
531 unsigned long (*symbol_hash)(const char *);
532 unsigned long local_symtab_size;
533 struct obj_symbol **local_symtab;
534 struct obj_symbol *symtab[HASH_BUCKETS];
545 struct obj_string_patch
547 struct obj_string_patch *next;
549 ElfW(Addr) reloc_offset;
550 ElfW(Addr) string_offset;
553 struct obj_symbol_patch
555 struct obj_symbol_patch *next;
557 ElfW(Addr) reloc_offset;
558 struct obj_symbol *sym;
562 /* Generic object manipulation routines. */
564 static unsigned long obj_elf_hash(const char *);
566 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
568 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
571 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
572 struct obj_symbol *sym);
574 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
575 static void obj_set_symbol_compare(struct obj_file *f,
576 int (*cmp)(const char *, const char *),
577 unsigned long (*hash)(const char *));
580 static struct obj_section *obj_find_section (struct obj_file *f,
583 static void obj_insert_section_load_order (struct obj_file *f,
584 struct obj_section *sec);
586 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
591 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
596 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
598 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
601 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
602 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
603 struct obj_symbol *sym);
606 static int obj_check_undefineds(struct obj_file *f);
608 static void obj_allocate_commons(struct obj_file *f);
610 static unsigned long obj_load_size (struct obj_file *f);
612 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
614 static struct obj_file *obj_load(FILE *f, int loadprogbits);
616 static int obj_create_image (struct obj_file *f, char *image);
618 /* Architecture specific manipulation routines. */
620 static struct obj_file *arch_new_file (void);
622 static struct obj_section *arch_new_section (void);
624 static struct obj_symbol *arch_new_symbol (void);
626 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
627 struct obj_section *targsec,
628 struct obj_section *symsec,
629 struct obj_symbol *sym,
630 ElfW(RelM) *rel, ElfW(Addr) value);
632 static void arch_create_got (struct obj_file *f);
634 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
635 static int arch_init_module (struct obj_file *f, struct new_module *);
639 //----------------------------------------------------------------------------
640 //--------end of modutils obj.h
641 //----------------------------------------------------------------------------
645 /* SPFX is always a string, so it can be concatenated to string constants. */
647 #define SPFX SYMBOL_PREFIX
653 #define _PATH_MODULES "/lib/modules"
654 static const int STRVERSIONLEN = 32;
656 /*======================================================================*/
658 static int flag_force_load = 0;
659 static int flag_autoclean = 0;
660 static int flag_verbose = 0;
661 static int flag_quiet = 0;
662 static int flag_export = 1;
665 /*======================================================================*/
667 #if defined(CONFIG_USE_LIST)
669 struct arch_list_entry
671 struct arch_list_entry *next;
672 CONFIG_LIST_ARCHTYPE addend;
679 #if defined(CONFIG_USE_SINGLE)
681 struct arch_single_entry
690 #if defined(__mips__)
693 struct mips_hi16 *next;
700 struct obj_file root;
701 #if defined(CONFIG_USE_PLT_ENTRIES)
702 struct obj_section *plt;
704 #if defined(CONFIG_USE_GOT_ENTRIES)
705 struct obj_section *got;
707 #if defined(__mips__)
708 struct mips_hi16 *mips_hi16_list;
713 struct obj_symbol root;
714 #if defined(CONFIG_USE_PLT_ENTRIES)
715 #if defined(CONFIG_USE_PLT_LIST)
716 struct arch_list_entry *pltent;
718 struct arch_single_entry pltent;
721 #if defined(CONFIG_USE_GOT_ENTRIES)
722 struct arch_single_entry gotent;
727 struct external_module {
732 struct new_module_symbol *syms;
735 static struct new_module_symbol *ksyms;
736 static size_t nksyms;
738 static struct external_module *ext_modules;
739 static int n_ext_modules;
740 static int n_ext_modules_used;
741 extern int delete_module(const char *);
743 static char *m_filename;
744 static char *m_fullName;
748 /*======================================================================*/
751 static int check_module_name_match(const char *filename, struct stat *statbuf,
754 char *fullname = (char *) userdata;
756 if (fullname[0] == '\0')
759 char *tmp, *tmp1 = xstrdup(filename);
760 tmp = get_last_path_component(tmp1);
761 if (strcmp(tmp, fullname) == 0) {
763 /* Stop searching if we find a match */
764 m_filename = xstrdup(filename);
773 /*======================================================================*/
775 static struct obj_file *arch_new_file(void)
778 f = xmalloc(sizeof(*f));
780 memset(f, 0, sizeof(*f));
785 static struct obj_section *arch_new_section(void)
787 return xmalloc(sizeof(struct obj_section));
790 static struct obj_symbol *arch_new_symbol(void)
792 struct arch_symbol *sym;
793 sym = xmalloc(sizeof(*sym));
795 memset(sym, 0, sizeof(*sym));
800 static enum obj_reloc
801 arch_apply_relocation(struct obj_file *f,
802 struct obj_section *targsec,
803 struct obj_section *symsec,
804 struct obj_symbol *sym,
805 ElfW(RelM) *rel, ElfW(Addr) v)
807 struct arch_file *ifile = (struct arch_file *) f;
808 enum obj_reloc ret = obj_reloc_ok;
809 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
810 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
811 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
812 struct arch_symbol *isym = (struct arch_symbol *) sym;
814 #if defined(CONFIG_USE_GOT_ENTRIES)
815 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
817 #if defined(CONFIG_USE_PLT_ENTRIES)
818 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
820 #if defined(CONFIG_USE_PLT_LIST)
821 struct arch_list_entry *pe;
823 struct arch_single_entry *pe;
827 switch (ELF32_R_TYPE(rel->r_info)) {
842 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
843 * (which is .got) similar to branch,
844 * but is full 32 bits relative */
854 case R_ARM_GOTOFF: /* address relative to the got */
859 #elif defined(__i386__)
895 #elif defined(__mc68000__)
906 ret = obj_reloc_overflow;
913 ret = obj_reloc_overflow;
920 if ((Elf32_Sword)v > 0x7f ||
921 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
922 ret = obj_reloc_overflow;
929 if ((Elf32_Sword)v > 0x7fff ||
930 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
931 ret = obj_reloc_overflow;
937 *(int *)loc = v - dot;
946 *(int *)loc += f->baseaddr;
957 #elif defined(__mips__)
968 ret = obj_reloc_dangerous;
969 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
970 ret = obj_reloc_overflow;
972 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
980 /* We cannot relocate this one now because we don't know the value
981 of the carry we need to add. Save the information, and let LO16
982 do the actual relocation. */
983 n = (struct mips_hi16 *) xmalloc(sizeof *n);
986 n->next = ifile->mips_hi16_list;
987 ifile->mips_hi16_list = n;
993 unsigned long insnlo = *loc;
994 Elf32_Addr val, vallo;
996 /* Sign extend the addend we extract from the lo insn. */
997 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
999 if (ifile->mips_hi16_list != NULL) {
1000 struct mips_hi16 *l;
1002 l = ifile->mips_hi16_list;
1004 struct mips_hi16 *next;
1007 /* The value for the HI16 had best be the same. */
1008 assert(v == l->value);
1010 /* Do the HI16 relocation. Note that we actually don't
1011 need to know anything about the LO16 itself, except where
1012 to find the low 16 bits of the addend needed by the LO16. */
1015 ((insn & 0xffff) << 16) +
1019 /* Account for the sign extension that will happen in the
1026 insn = (insn & ~0xffff) | val;
1034 ifile->mips_hi16_list = NULL;
1037 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1039 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1044 #elif defined(__powerpc__)
1046 case R_PPC_ADDR16_HA:
1047 *(unsigned short *)loc = (v + 0x8000) >> 16;
1050 case R_PPC_ADDR16_HI:
1051 *(unsigned short *)loc = v >> 16;
1054 case R_PPC_ADDR16_LO:
1055 *(unsigned short *)loc = v;
1069 #elif defined(__sh__)
1092 *loc = f->baseaddr + rel->r_addend;
1097 *loc = got - dot + rel->r_addend;
1111 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1112 ret = obj_reloc_unhandled;
1115 #if defined (__v850e__)
1120 /* We write two shorts instead of a long because even
1121 32-bit insns only need half-word alignment, but
1122 32-bit data needs to be long-word aligned. */
1123 v += ((unsigned short *)loc)[0];
1124 v += ((unsigned short *)loc)[1] << 16;
1125 ((unsigned short *)loc)[0] = v & 0xffff;
1126 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1129 case R_V850_22_PCREL:
1133 #if defined(CONFIG_USE_PLT_ENTRIES)
1137 /* find the plt entry and initialize it if necessary */
1138 assert(isym != NULL);
1140 #if defined(CONFIG_USE_PLT_LIST)
1141 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1149 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1151 /* generate some machine code */
1153 #if defined(__arm__)
1154 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1155 ip[1] = v; /* sym@ */
1157 #if defined(__powerpc__)
1158 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1159 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1160 ip[2] = 0x7d6903a6; /* mtctr r11 */
1161 ip[3] = 0x4e800420; /* bctr */
1163 #if defined (__v850e__)
1164 /* We have to trash a register, so we assume that any control
1165 transfer more than 21-bits away must be a function call
1166 (so we can use a call-clobbered register). */
1167 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1168 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1173 /* relative distance to target */
1175 /* if the target is too far away.... */
1176 #if defined (__arm__) || defined (__powerpc__)
1177 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1178 #elif defined (__v850e__)
1179 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1181 /* go via the plt */
1182 v = plt + pe->offset - dot;
1184 #if defined (__v850e__)
1189 ret = obj_reloc_dangerous;
1191 /* merge the offset into the instruction. */
1192 #if defined(__arm__)
1193 /* Convert to words. */
1196 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1198 #if defined(__powerpc__)
1199 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1201 #if defined (__v850e__)
1202 /* We write two shorts instead of a long because even 32-bit insns
1203 only need half-word alignment, but the 32-bit data write needs
1204 to be long-word aligned. */
1205 ((unsigned short *)loc)[0] =
1206 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1207 | ((v >> 16) & 0x3f); /* offs high part */
1208 ((unsigned short *)loc)[1] =
1209 (v & 0xffff); /* offs low part */
1212 #endif /* CONFIG_USE_PLT_ENTRIES */
1214 #if defined(CONFIG_USE_GOT_ENTRIES)
1217 assert(isym != NULL);
1218 /* needs an entry in the .got: set it, once */
1219 if (!isym->gotent.inited) {
1220 isym->gotent.inited = 1;
1221 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1223 /* make the reloc with_respect_to_.got */
1225 *loc += isym->gotent.offset + rel->r_addend;
1226 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1227 *loc += isym->gotent.offset;
1231 #endif /* CONFIG_USE_GOT_ENTRIES */
1238 #if defined(CONFIG_USE_LIST)
1240 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1241 int offset, int size)
1243 struct arch_list_entry *pe;
1245 for (pe = *list; pe != NULL; pe = pe->next) {
1246 if (pe->addend == rel->r_addend) {
1252 pe = xmalloc(sizeof(struct arch_list_entry));
1254 pe->addend = rel->r_addend;
1255 pe->offset = offset;
1265 #if defined(CONFIG_USE_SINGLE)
1267 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1268 int offset, int size)
1270 if (single->allocated == 0) {
1271 single->allocated = 1;
1272 single->offset = offset;
1281 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1283 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1284 int offset, int size)
1286 struct obj_section *myrelsec = obj_find_section(f, name);
1293 obj_extend_section(myrelsec, offset);
1295 myrelsec = obj_create_alloced_section(f, name,
1305 static void arch_create_got(struct obj_file *f)
1307 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1308 struct arch_file *ifile = (struct arch_file *) f;
1310 #if defined(CONFIG_USE_GOT_ENTRIES)
1311 int got_offset = 0, got_needed = 0, got_allocate;
1313 #if defined(CONFIG_USE_PLT_ENTRIES)
1314 int plt_offset = 0, plt_needed = 0, plt_allocate;
1316 struct obj_section *relsec, *symsec, *strsec;
1317 ElfW(RelM) *rel, *relend;
1318 ElfW(Sym) *symtab, *extsym;
1319 const char *strtab, *name;
1320 struct arch_symbol *intsym;
1322 for (i = 0; i < f->header.e_shnum; ++i) {
1323 relsec = f->sections[i];
1324 if (relsec->header.sh_type != SHT_RELM)
1327 symsec = f->sections[relsec->header.sh_link];
1328 strsec = f->sections[symsec->header.sh_link];
1330 rel = (ElfW(RelM) *) relsec->contents;
1331 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1332 symtab = (ElfW(Sym) *) symsec->contents;
1333 strtab = (const char *) strsec->contents;
1335 for (; rel < relend; ++rel) {
1336 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1338 #if defined(CONFIG_USE_GOT_ENTRIES)
1341 #if defined(CONFIG_USE_PLT_ENTRIES)
1345 switch (ELF32_R_TYPE(rel->r_info)) {
1346 #if defined(__arm__)
1361 #elif defined(__i386__)
1371 #elif defined(__powerpc__)
1376 #elif defined(__mc68000__)
1385 #elif defined(__sh__)
1395 #elif defined (__v850e__)
1396 case R_V850_22_PCREL:
1405 if (extsym->st_name != 0) {
1406 name = strtab + extsym->st_name;
1408 name = f->sections[extsym->st_shndx]->name;
1410 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1411 #if defined(CONFIG_USE_GOT_ENTRIES)
1413 got_offset += arch_single_init(
1414 rel, &intsym->gotent,
1415 got_offset, CONFIG_GOT_ENTRY_SIZE);
1420 #if defined(CONFIG_USE_PLT_ENTRIES)
1422 #if defined(CONFIG_USE_PLT_LIST)
1423 plt_offset += arch_list_add(
1424 rel, &intsym->pltent,
1425 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1427 plt_offset += arch_single_init(
1428 rel, &intsym->pltent,
1429 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1437 #if defined(CONFIG_USE_GOT_ENTRIES)
1439 ifile->got = arch_xsect_init(f, ".got", got_offset,
1440 CONFIG_GOT_ENTRY_SIZE);
1444 #if defined(CONFIG_USE_PLT_ENTRIES)
1446 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1447 CONFIG_PLT_ENTRY_SIZE);
1451 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1454 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
1455 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1461 /*======================================================================*/
1463 /* Standard ELF hash function. */
1464 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1466 unsigned long h = 0;
1473 if ((g = (h & 0xf0000000)) != 0) {
1482 static unsigned long obj_elf_hash(const char *name)
1484 return obj_elf_hash_n(name, strlen(name));
1487 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1488 /* String comparison for non-co-versioned kernel and module. */
1490 static int ncv_strcmp(const char *a, const char *b)
1492 size_t alen = strlen(a), blen = strlen(b);
1494 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1495 return strncmp(a, b, alen);
1496 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1497 return strncmp(a, b, blen);
1499 return strcmp(a, b);
1502 /* String hashing for non-co-versioned kernel and module. Here
1503 we are simply forced to drop the crc from the hash. */
1505 static unsigned long ncv_symbol_hash(const char *str)
1507 size_t len = strlen(str);
1508 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1510 return obj_elf_hash_n(str, len);
1514 obj_set_symbol_compare(struct obj_file *f,
1515 int (*cmp) (const char *, const char *),
1516 unsigned long (*hash) (const char *))
1519 f->symbol_cmp = cmp;
1521 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1524 f->symbol_hash = hash;
1526 memcpy(tmptab, f->symtab, sizeof(tmptab));
1527 memset(f->symtab, 0, sizeof(f->symtab));
1529 for (i = 0; i < HASH_BUCKETS; ++i)
1530 for (sym = tmptab[i]; sym; sym = next) {
1531 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1533 sym->next = f->symtab[h];
1539 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1541 static struct obj_symbol *
1542 obj_add_symbol(struct obj_file *f, const char *name,
1543 unsigned long symidx, int info,
1544 int secidx, ElfW(Addr) value,
1547 struct obj_symbol *sym;
1548 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1549 int n_type = ELFW(ST_TYPE) (info);
1550 int n_binding = ELFW(ST_BIND) (info);
1552 for (sym = f->symtab[hash]; sym; sym = sym->next)
1553 if (f->symbol_cmp(sym->name, name) == 0) {
1554 int o_secidx = sym->secidx;
1555 int o_info = sym->info;
1556 int o_type = ELFW(ST_TYPE) (o_info);
1557 int o_binding = ELFW(ST_BIND) (o_info);
1559 /* A redefinition! Is it legal? */
1561 if (secidx == SHN_UNDEF)
1563 else if (o_secidx == SHN_UNDEF)
1565 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1566 /* Cope with local and global symbols of the same name
1567 in the same object file, as might have been created
1568 by ld -r. The only reason locals are now seen at this
1569 level at all is so that we can do semi-sensible things
1572 struct obj_symbol *nsym, **p;
1574 nsym = arch_new_symbol();
1575 nsym->next = sym->next;
1578 /* Excise the old (local) symbol from the hash chain. */
1579 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1583 } else if (n_binding == STB_LOCAL) {
1584 /* Another symbol of the same name has already been defined.
1585 Just add this to the local table. */
1586 sym = arch_new_symbol();
1589 f->local_symtab[symidx] = sym;
1591 } else if (n_binding == STB_WEAK)
1593 else if (o_binding == STB_WEAK)
1595 /* Don't unify COMMON symbols with object types the programmer
1597 else if (secidx == SHN_COMMON
1598 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1600 else if (o_secidx == SHN_COMMON
1601 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1604 /* Don't report an error if the symbol is coming from
1605 the kernel or some external module. */
1606 if (secidx <= SHN_HIRESERVE)
1607 error_msg("%s multiply defined", name);
1612 /* Completely new symbol. */
1613 sym = arch_new_symbol();
1614 sym->next = f->symtab[hash];
1615 f->symtab[hash] = sym;
1618 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1619 if (symidx >= f->local_symtab_size)
1620 error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1621 name, (long) symidx, (long) f->local_symtab_size);
1623 f->local_symtab[symidx] = sym;
1630 sym->secidx = secidx;
1636 static struct obj_symbol *
1637 obj_find_symbol(struct obj_file *f, const char *name)
1639 struct obj_symbol *sym;
1640 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1642 for (sym = f->symtab[hash]; sym; sym = sym->next)
1643 if (f->symbol_cmp(sym->name, name) == 0)
1650 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1653 if (sym->secidx >= SHN_LORESERVE)
1656 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1658 /* As a special case, a NULL sym has value zero. */
1663 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1665 int i, n = f->header.e_shnum;
1667 for (i = 0; i < n; ++i)
1668 if (strcmp(f->sections[i]->name, name) == 0)
1669 return f->sections[i];
1674 static int obj_load_order_prio(struct obj_section *a)
1676 unsigned long af, ac;
1678 af = a->header.sh_flags;
1681 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1682 strcmp(a->name + 5, ".init"))
1686 if (!(af & SHF_WRITE))
1688 if (af & SHF_EXECINSTR)
1690 if (a->header.sh_type != SHT_NOBITS)
1697 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1699 struct obj_section **p;
1700 int prio = obj_load_order_prio(sec);
1701 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1702 if (obj_load_order_prio(*p) < prio)
1704 sec->load_next = *p;
1708 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1710 unsigned long align,
1713 int newidx = f->header.e_shnum++;
1714 struct obj_section *sec;
1716 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1717 f->sections[newidx] = sec = arch_new_section();
1719 memset(sec, 0, sizeof(*sec));
1720 sec->header.sh_type = SHT_PROGBITS;
1721 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1722 sec->header.sh_size = size;
1723 sec->header.sh_addralign = align;
1727 sec->contents = xmalloc(size);
1729 obj_insert_section_load_order(f, sec);
1734 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1736 unsigned long align,
1739 int newidx = f->header.e_shnum++;
1740 struct obj_section *sec;
1742 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1743 f->sections[newidx] = sec = arch_new_section();
1745 memset(sec, 0, sizeof(*sec));
1746 sec->header.sh_type = SHT_PROGBITS;
1747 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1748 sec->header.sh_size = size;
1749 sec->header.sh_addralign = align;
1753 sec->contents = xmalloc(size);
1755 sec->load_next = f->load_order;
1756 f->load_order = sec;
1757 if (f->load_order_search_start == &f->load_order)
1758 f->load_order_search_start = &sec->load_next;
1763 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1765 unsigned long oldsize = sec->header.sh_size;
1767 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1769 return sec->contents + oldsize;
1773 /* Conditionally add the symbols from the given symbol set to the
1779 int idx, struct new_module_symbol *syms, size_t nsyms)
1781 struct new_module_symbol *s;
1784 #ifdef SYMBOL_PREFIX
1786 size_t name_alloced_size = 0;
1789 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1790 /* Only add symbols that are already marked external.
1791 If we override locals we may cause problems for
1792 argument initialization. We will also create a false
1793 dependency on the module. */
1794 struct obj_symbol *sym;
1795 char *name = (char *)s->name;
1797 #ifdef SYMBOL_PREFIX
1798 /* Prepend SYMBOL_PREFIX to the symbol's name (the
1799 kernel exports `C names', but module object files
1800 reference `linker names'). */
1801 size_t extra = sizeof SYMBOL_PREFIX;
1802 size_t name_size = strlen (name) + extra;
1803 if (name_size > name_alloced_size) {
1804 name_alloced_size = name_size * 2;
1805 name_buf = alloca (name_alloced_size);
1807 strcpy (name_buf, SYMBOL_PREFIX);
1808 strcpy (name_buf + extra - 1, name);
1810 #endif /* SYMBOL_PREFIX */
1812 sym = obj_find_symbol(f, name);
1813 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
1814 #ifdef SYMBOL_PREFIX
1815 /* Put NAME_BUF into more permanent storage. */
1816 name = xmalloc (name_size);
1817 strcpy (name, name_buf);
1819 sym = obj_add_symbol(f, name, -1,
1820 ELFW(ST_INFO) (STB_GLOBAL,
1823 /* Did our symbol just get installed? If so, mark the
1824 module as "used". */
1825 if (sym->secidx == idx)
1833 static void add_kernel_symbols(struct obj_file *f)
1835 struct external_module *m;
1838 /* Add module symbols first. */
1840 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1842 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1843 m->nsyms)) m->used = 1, ++nused;
1845 n_ext_modules_used = nused;
1847 /* And finally the symbols from the kernel proper. */
1850 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1853 static char *get_modinfo_value(struct obj_file *f, const char *key)
1855 struct obj_section *sec;
1856 char *p, *v, *n, *ep;
1857 size_t klen = strlen(key);
1859 sec = obj_find_section(f, ".modinfo");
1863 ep = p + sec->header.sh_size;
1866 n = strchr(p, '\0');
1868 if (p + klen == v && strncmp(p, key, klen) == 0)
1871 if (p + klen == n && strcmp(p, key) == 0)
1881 /*======================================================================*/
1882 /* Functions relating to module loading in pre 2.1 kernels. */
1885 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1889 struct obj_symbol *sym;
1893 if ((q = strchr(p, '=')) == NULL) {
1899 sym = obj_find_symbol(f, p);
1901 /* Also check that the parameter was not resolved from the kernel. */
1902 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1903 error_msg("symbol for parameter %s not found", p);
1907 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1909 /* Do C quoting if we begin with a ". */
1913 str = alloca(strlen(q));
1914 for (r = str, q++; *q != '"'; ++q, ++r) {
1916 error_msg("improperly terminated string argument for %s", p);
1918 } else if (*q == '\\')
1952 if (q[1] >= '0' && q[1] <= '7') {
1953 c = (c * 8) + *++q - '0';
1954 if (q[1] >= '0' && q[1] <= '7')
1955 c = (c * 8) + *++q - '0';
1968 obj_string_patch(f, sym->secidx, sym->value, str);
1969 } else if (*q >= '0' && *q <= '9') {
1971 *loc++ = strtoul(q, &q, 0);
1972 while (*q++ == ',');
1974 char *contents = f->sections[sym->secidx]->contents;
1975 char *myloc = contents + sym->value;
1976 char *r; /* To search for commas */
1978 /* Break the string with comas */
1979 while ((r = strchr(q, ',')) != (char *) NULL) {
1981 obj_string_patch(f, sym->secidx, myloc - contents, q);
1982 myloc += sizeof(char *);
1987 obj_string_patch(f, sym->secidx, myloc - contents, q);
1996 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1997 static int old_is_module_checksummed(struct obj_file *f)
1999 return obj_find_symbol(f, "Using_Versions") != NULL;
2001 /* Get the module's kernel version in the canonical integer form. */
2004 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2006 struct obj_symbol *sym;
2010 sym = obj_find_symbol(f, "kernel_version");
2014 p = f->sections[sym->secidx]->contents + sym->value;
2015 safe_strncpy(str, p, STRVERSIONLEN);
2017 a = strtoul(p, &p, 10);
2020 b = strtoul(p + 1, &p, 10);
2023 c = strtoul(p + 1, &q, 10);
2027 return a << 16 | b << 8 | c;
2030 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2032 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
2034 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
2036 static int old_get_kernel_symbols(const char *m_name)
2038 struct old_kernel_sym *ks, *k;
2039 struct new_module_symbol *s;
2040 struct external_module *mod;
2041 int nks, nms, nmod, i;
2043 nks = get_kernel_syms(NULL);
2046 perror_msg("get_kernel_syms: %s", m_name);
2048 error_msg("No kernel symbols");
2052 ks = k = xmalloc(nks * sizeof(*ks));
2054 if (get_kernel_syms(ks) != nks) {
2055 perror("inconsistency with get_kernel_syms -- is someone else "
2056 "playing with modules?");
2061 /* Collect the module information. */
2066 while (k->name[0] == '#' && k->name[1]) {
2067 struct old_kernel_sym *k2;
2069 /* Find out how many symbols this module has. */
2070 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
2074 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
2075 mod[nmod].name = k->name + 1;
2076 mod[nmod].addr = k->value;
2078 mod[nmod].nsyms = nms;
2079 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2081 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
2082 s->name = (unsigned long) k->name;
2083 s->value = k->value;
2090 n_ext_modules = nmod + 1;
2092 /* Now collect the symbols for the kernel proper. */
2094 if (k->name[0] == '#')
2097 nksyms = nms = nks - (k - ks);
2098 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2100 for (i = 0; i < nms; ++i, ++s, ++k) {
2101 s->name = (unsigned long) k->name;
2102 s->value = k->value;
2108 /* Return the kernel symbol checksum version, or zero if not used. */
2110 static int old_is_kernel_checksummed(void)
2112 /* Using_Versions is the first symbol. */
2114 && strcmp((char *) ksyms[0].name,
2115 "Using_Versions") == 0) return ksyms[0].value;
2121 static int old_create_mod_use_count(struct obj_file *f)
2123 struct obj_section *sec;
2125 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2128 obj_add_symbol(f, "mod_use_count_", -1,
2129 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2136 old_init_module(const char *m_name, struct obj_file *f,
2137 unsigned long m_size)
2140 struct old_mod_routines routines;
2141 struct old_symbol_table *symtab;
2144 /* Create the symbol table */
2146 int nsyms = 0, strsize = 0, total;
2148 /* Size things first... */
2151 for (i = 0; i < HASH_BUCKETS; ++i) {
2152 struct obj_symbol *sym;
2153 for (sym = f->symtab[i]; sym; sym = sym->next)
2154 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2155 && sym->secidx <= SHN_HIRESERVE)
2157 sym->ksymidx = nsyms++;
2158 strsize += strlen(sym->name) + 1;
2163 total = (sizeof(struct old_symbol_table)
2164 + nsyms * sizeof(struct old_module_symbol)
2165 + n_ext_modules_used * sizeof(struct old_module_ref)
2167 symtab = xmalloc(total);
2168 symtab->size = total;
2169 symtab->n_symbols = nsyms;
2170 symtab->n_refs = n_ext_modules_used;
2172 if (flag_export && nsyms) {
2173 struct old_module_symbol *ksym;
2177 ksym = symtab->symbol;
2178 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2179 + n_ext_modules_used * sizeof(struct old_module_ref));
2181 for (i = 0; i < HASH_BUCKETS; ++i) {
2182 struct obj_symbol *sym;
2183 for (sym = f->symtab[i]; sym; sym = sym->next)
2184 if (sym->ksymidx >= 0) {
2185 ksym->addr = obj_symbol_final_value(f, sym);
2187 (unsigned long) str - (unsigned long) symtab;
2189 strcpy(str, sym->name);
2190 str += strlen(sym->name) + 1;
2196 if (n_ext_modules_used) {
2197 struct old_module_ref *ref;
2200 ref = (struct old_module_ref *)
2201 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2203 for (i = 0; i < n_ext_modules; ++i)
2204 if (ext_modules[i].used)
2205 ref++->module = ext_modules[i].addr;
2209 /* Fill in routines. */
2212 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2214 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2216 /* Whew! All of the initialization is complete. Collect the final
2217 module image and give it to the kernel. */
2219 image = xmalloc(m_size);
2220 obj_create_image(f, image);
2222 /* image holds the complete relocated module, accounting correctly for
2223 mod_use_count. However the old module kernel support assume that
2224 it is receiving something which does not contain mod_use_count. */
2225 ret = old_sys_init_module(m_name, image + sizeof(long),
2226 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2227 : 0), &routines, symtab);
2229 perror_msg("init_module: %s", m_name);
2239 #define old_create_mod_use_count(x) TRUE
2240 #define old_init_module(x, y, z) TRUE
2242 #endif /* CONFIG_FEATURE_OLD_MODULE_INTERFACE */
2246 /*======================================================================*/
2247 /* Functions relating to module loading after 2.1.18. */
2250 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2253 char *p, *q, *key, *sym_name;
2254 struct obj_symbol *sym;
2255 char *contents, *loc;
2259 if ((q = strchr(p, '=')) == NULL) {
2264 key = alloca(q - p + 6);
2265 memcpy(key, "parm_", 5);
2266 memcpy(key + 5, p, q - p);
2269 p = get_modinfo_value(f, key);
2272 error_msg("invalid parameter %s", key);
2276 #ifdef SYMBOL_PREFIX
2277 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2278 strcpy (sym_name, SYMBOL_PREFIX);
2279 strcat (sym_name, key);
2283 sym = obj_find_symbol(f, sym_name);
2285 /* Also check that the parameter was not resolved from the kernel. */
2286 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2287 error_msg("symbol for parameter %s not found", key);
2292 min = strtoul(p, &p, 10);
2294 max = strtoul(p + 1, &p, 10);
2300 contents = f->sections[sym->secidx]->contents;
2301 loc = contents + sym->value;
2305 if ((*p == 's') || (*p == 'c')) {
2308 /* Do C quoting if we begin with a ", else slurp the lot. */
2312 str = alloca(strlen(q));
2313 for (r = str, q++; *q != '"'; ++q, ++r) {
2315 error_msg("improperly terminated string argument for %s",
2318 } else if (*q == '\\')
2352 if (q[1] >= '0' && q[1] <= '7') {
2353 c = (c * 8) + *++q - '0';
2354 if (q[1] >= '0' && q[1] <= '7')
2355 c = (c * 8) + *++q - '0';
2372 /* In this case, the string is not quoted. We will break
2373 it using the coma (like for ints). If the user wants to
2374 include comas in a string, he just has to quote it */
2376 /* Search the next coma */
2380 if (r != (char *) NULL) {
2381 /* Recopy the current field */
2382 str = alloca(r - q + 1);
2383 memcpy(str, q, r - q);
2385 /* I don't know if it is usefull, as the previous case
2386 doesn't null terminate the string ??? */
2389 /* Keep next fields */
2400 obj_string_patch(f, sym->secidx, loc - contents, str);
2401 loc += tgt_sizeof_char_p;
2403 /* Array of chars (in fact, matrix !) */
2404 unsigned long charssize; /* size of each member */
2406 /* Get the size of each member */
2407 /* Probably we should do that outside the loop ? */
2408 if (!isdigit(*(p + 1))) {
2409 error_msg("parameter type 'c' for %s must be followed by"
2410 " the maximum size", key);
2413 charssize = strtoul(p + 1, (char **) NULL, 10);
2416 if (strlen(str) >= charssize) {
2417 error_msg("string too long for %s (max %ld)", key,
2422 /* Copy to location */
2423 strcpy((char *) loc, str);
2427 long v = strtoul(q, &q, 0);
2434 loc += tgt_sizeof_short;
2438 loc += tgt_sizeof_int;
2442 loc += tgt_sizeof_long;
2446 error_msg("unknown parameter type '%c' for %s", *p, key);
2461 goto retry_end_of_value;
2465 error_msg("too many values for %s (max %d)", key, max);
2472 error_msg("invalid argument syntax for %s", key);
2479 error_msg("too few values for %s (min %d)", key, min);
2489 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2490 static int new_is_module_checksummed(struct obj_file *f)
2492 const char *p = get_modinfo_value(f, "using_checksums");
2499 /* Get the module's kernel version in the canonical integer form. */
2502 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2507 p = get_modinfo_value(f, "kernel_version");
2510 safe_strncpy(str, p, STRVERSIONLEN);
2512 a = strtoul(p, &p, 10);
2515 b = strtoul(p + 1, &p, 10);
2518 c = strtoul(p + 1, &q, 10);
2522 return a << 16 | b << 8 | c;
2525 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2528 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2530 /* Fetch the loaded modules, and all currently exported symbols. */
2532 static int new_get_kernel_symbols(void)
2534 char *module_names, *mn;
2535 struct external_module *modules, *m;
2536 struct new_module_symbol *syms, *s;
2537 size_t ret, bufsize, nmod, nsyms, i, j;
2539 /* Collect the loaded modules. */
2541 module_names = xmalloc(bufsize = 256);
2543 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2544 if (errno == ENOSPC && bufsize < ret) {
2545 module_names = xrealloc(module_names, bufsize = ret);
2546 goto retry_modules_load;
2548 perror_msg("QM_MODULES");
2552 n_ext_modules = nmod = ret;
2554 /* Collect the modules' symbols. */
2557 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2558 memset(modules, 0, nmod * sizeof(*modules));
2559 for (i = 0, mn = module_names, m = modules;
2560 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2561 struct new_module_info info;
2563 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2564 if (errno == ENOENT) {
2565 /* The module was removed out from underneath us. */
2568 perror_msg("query_module: QM_INFO: %s", mn);
2572 syms = xmalloc(bufsize = 1024);
2574 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2577 syms = xrealloc(syms, bufsize = ret);
2578 goto retry_mod_sym_load;
2580 /* The module was removed out from underneath us. */
2583 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2590 m->addr = info.addr;
2594 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2595 s->name += (unsigned long) syms;
2600 /* Collect the kernel's symbols. */
2602 syms = xmalloc(bufsize = 16 * 1024);
2603 retry_kern_sym_load:
2604 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2605 if (errno == ENOSPC && bufsize < ret) {
2606 syms = xrealloc(syms, bufsize = ret);
2607 goto retry_kern_sym_load;
2609 perror_msg("kernel: QM_SYMBOLS");
2612 nksyms = nsyms = ret;
2615 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2616 s->name += (unsigned long) syms;
2622 /* Return the kernel symbol checksum version, or zero if not used. */
2624 static int new_is_kernel_checksummed(void)
2626 struct new_module_symbol *s;
2629 /* Using_Versions is not the first symbol, but it should be in there. */
2631 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2632 if (strcmp((char *) s->name, "Using_Versions") == 0)
2639 static int new_create_this_module(struct obj_file *f, const char *m_name)
2641 struct obj_section *sec;
2643 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2644 sizeof(struct new_module));
2645 memset(sec->contents, 0, sizeof(struct new_module));
2647 obj_add_symbol(f, SPFX "__this_module", -1,
2648 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2649 sizeof(struct new_module));
2651 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2657 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
2658 /* add an entry to the __ksymtab section, creating it if necessary */
2659 static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
2661 struct obj_section *sec;
2664 /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
2665 * If __ksymtab is defined but not marked alloc, x out the first character
2666 * (no obj_delete routine) and create a new __ksymtab with the correct
2669 sec = obj_find_section(f, "__ksymtab");
2670 if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
2671 *((char *)(sec->name)) = 'x'; /* override const */
2675 sec = obj_create_alloced_section(f, "__ksymtab",
2676 tgt_sizeof_void_p, 0);
2679 sec->header.sh_flags |= SHF_ALLOC;
2680 sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
2682 ofs = sec->header.sh_size;
2683 obj_symbol_patch(f, sec->idx, ofs, sym);
2684 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
2685 obj_extend_section(sec, 2 * tgt_sizeof_char_p);
2687 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
2689 static int new_create_module_ksymtab(struct obj_file *f)
2691 struct obj_section *sec;
2694 /* We must always add the module references. */
2696 if (n_ext_modules_used) {
2697 struct new_module_ref *dep;
2698 struct obj_symbol *tm;
2700 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2701 (sizeof(struct new_module_ref)
2702 * n_ext_modules_used));
2706 tm = obj_find_symbol(f, SPFX "__this_module");
2707 dep = (struct new_module_ref *) sec->contents;
2708 for (i = 0; i < n_ext_modules; ++i)
2709 if (ext_modules[i].used) {
2710 dep->dep = ext_modules[i].addr;
2711 obj_symbol_patch(f, sec->idx,
2712 (char *) &dep->ref - sec->contents, tm);
2718 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2723 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2726 /* We don't want to export symbols residing in sections that
2727 aren't loaded. There are a number of these created so that
2728 we make sure certain module options don't appear twice. */
2730 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2732 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2734 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2735 struct obj_symbol *sym;
2736 for (sym = f->symtab[i]; sym; sym = sym->next)
2737 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2738 && sym->secidx <= SHN_HIRESERVE
2739 && (sym->secidx >= SHN_LORESERVE
2740 || loaded[sym->secidx])) {
2741 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2743 obj_symbol_patch(f, sec->idx, ofs, sym);
2744 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2751 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2759 new_init_module(const char *m_name, struct obj_file *f,
2760 unsigned long m_size)
2762 struct new_module *module;
2763 struct obj_section *sec;
2768 sec = obj_find_section(f, ".this");
2769 if (!sec || !sec->contents) {
2770 perror_msg_and_die("corrupt module %s?",m_name);
2772 module = (struct new_module *) sec->contents;
2773 m_addr = sec->header.sh_addr;
2775 module->size_of_struct = sizeof(*module);
2776 module->size = m_size;
2777 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2779 sec = obj_find_section(f, "__ksymtab");
2780 if (sec && sec->header.sh_size) {
2781 module->syms = sec->header.sh_addr;
2782 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2785 if (n_ext_modules_used) {
2786 sec = obj_find_section(f, ".kmodtab");
2787 module->deps = sec->header.sh_addr;
2788 module->ndeps = n_ext_modules_used;
2792 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2794 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2796 sec = obj_find_section(f, "__ex_table");
2798 module->ex_table_start = sec->header.sh_addr;
2799 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2802 sec = obj_find_section(f, ".text.init");
2804 module->runsize = sec->header.sh_addr - m_addr;
2806 sec = obj_find_section(f, ".data.init");
2808 if (!module->runsize ||
2809 module->runsize > sec->header.sh_addr - m_addr)
2810 module->runsize = sec->header.sh_addr - m_addr;
2812 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2813 if (sec && sec->header.sh_size) {
2814 module->archdata_start = (void*)sec->header.sh_addr;
2815 module->archdata_end = module->archdata_start + sec->header.sh_size;
2817 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2818 if (sec && sec->header.sh_size) {
2819 module->kallsyms_start = (void*)sec->header.sh_addr;
2820 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2823 if (!arch_init_module(f, module))
2826 /* Whew! All of the initialization is complete. Collect the final
2827 module image and give it to the kernel. */
2829 image = xmalloc(m_size);
2830 obj_create_image(f, image);
2832 ret = new_sys_init_module(m_name, (struct new_module *) image);
2834 perror_msg("init_module: %s", m_name);
2843 #define new_init_module(x, y, z) TRUE
2844 #define new_create_this_module(x, y) 0
2845 #define new_add_ksymtab(x, y) -1
2846 #define new_create_module_ksymtab(x)
2847 #define query_module(v, w, x, y, z) -1
2849 #endif /* CONFIG_FEATURE_NEW_MODULE_INTERFACE */
2852 /*======================================================================*/
2855 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2858 struct obj_string_patch *p;
2859 struct obj_section *strsec;
2860 size_t len = strlen(string) + 1;
2863 p = xmalloc(sizeof(*p));
2864 p->next = f->string_patches;
2865 p->reloc_secidx = secidx;
2866 p->reloc_offset = offset;
2867 f->string_patches = p;
2869 strsec = obj_find_section(f, ".kstrtab");
2870 if (strsec == NULL) {
2871 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2872 p->string_offset = 0;
2873 loc = strsec->contents;
2875 p->string_offset = strsec->header.sh_size;
2876 loc = obj_extend_section(strsec, len);
2878 memcpy(loc, string, len);
2883 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2885 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2886 struct obj_symbol *sym)
2888 struct obj_symbol_patch *p;
2890 p = xmalloc(sizeof(*p));
2891 p->next = f->symbol_patches;
2892 p->reloc_secidx = secidx;
2893 p->reloc_offset = offset;
2895 f->symbol_patches = p;
2901 static int obj_check_undefineds(struct obj_file *f)
2906 for (i = 0; i < HASH_BUCKETS; ++i) {
2907 struct obj_symbol *sym;
2908 for (sym = f->symtab[i]; sym; sym = sym->next)
2909 if (sym->secidx == SHN_UNDEF) {
2910 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2911 sym->secidx = SHN_ABS;
2915 error_msg("unresolved symbol %s", sym->name);
2925 static void obj_allocate_commons(struct obj_file *f)
2927 struct common_entry {
2928 struct common_entry *next;
2929 struct obj_symbol *sym;
2930 } *common_head = NULL;
2934 for (i = 0; i < HASH_BUCKETS; ++i) {
2935 struct obj_symbol *sym;
2936 for (sym = f->symtab[i]; sym; sym = sym->next)
2937 if (sym->secidx == SHN_COMMON) {
2938 /* Collect all COMMON symbols and sort them by size so as to
2939 minimize space wasted by alignment requirements. */
2941 struct common_entry **p, *n;
2942 for (p = &common_head; *p; p = &(*p)->next)
2943 if (sym->size <= (*p)->sym->size)
2946 n = alloca(sizeof(*n));
2954 for (i = 1; i < f->local_symtab_size; ++i) {
2955 struct obj_symbol *sym = f->local_symtab[i];
2956 if (sym && sym->secidx == SHN_COMMON) {
2957 struct common_entry **p, *n;
2958 for (p = &common_head; *p; p = &(*p)->next)
2959 if (sym == (*p)->sym)
2961 else if (sym->size < (*p)->sym->size) {
2962 n = alloca(sizeof(*n));
2972 /* Find the bss section. */
2973 for (i = 0; i < f->header.e_shnum; ++i)
2974 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2977 /* If for some reason there hadn't been one, create one. */
2978 if (i == f->header.e_shnum) {
2979 struct obj_section *sec;
2981 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2982 f->sections[i] = sec = arch_new_section();
2983 f->header.e_shnum = i + 1;
2985 memset(sec, 0, sizeof(*sec));
2986 sec->header.sh_type = SHT_PROGBITS;
2987 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2992 /* Allocate the COMMONS. */
2994 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2995 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2996 struct common_entry *c;
2998 for (c = common_head; c; c = c->next) {
2999 ElfW(Addr) align = c->sym->value;
3001 if (align > max_align)
3003 if (bss_size & (align - 1))
3004 bss_size = (bss_size | (align - 1)) + 1;
3007 c->sym->value = bss_size;
3009 bss_size += c->sym->size;
3012 f->sections[i]->header.sh_size = bss_size;
3013 f->sections[i]->header.sh_addralign = max_align;
3017 /* For the sake of patch relocation and parameter initialization,
3018 allocate zeroed data for NOBITS sections now. Note that after
3019 this we cannot assume NOBITS are really empty. */
3020 for (i = 0; i < f->header.e_shnum; ++i) {
3021 struct obj_section *s = f->sections[i];
3022 if (s->header.sh_type == SHT_NOBITS) {
3023 if (s->header.sh_size != 0)
3024 s->contents = memset(xmalloc(s->header.sh_size),
3025 0, s->header.sh_size);
3029 s->header.sh_type = SHT_PROGBITS;
3034 static unsigned long obj_load_size(struct obj_file *f)
3036 unsigned long dot = 0;
3037 struct obj_section *sec;
3039 /* Finalize the positions of the sections relative to one another. */
3041 for (sec = f->load_order; sec; sec = sec->load_next) {
3044 align = sec->header.sh_addralign;
3045 if (align && (dot & (align - 1)))
3046 dot = (dot | (align - 1)) + 1;
3048 sec->header.sh_addr = dot;
3049 dot += sec->header.sh_size;
3055 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3057 int i, n = f->header.e_shnum;
3060 /* Finalize the addresses of the sections. */
3063 for (i = 0; i < n; ++i)
3064 f->sections[i]->header.sh_addr += base;
3066 /* And iterate over all of the relocations. */
3068 for (i = 0; i < n; ++i) {
3069 struct obj_section *relsec, *symsec, *targsec, *strsec;
3070 ElfW(RelM) * rel, *relend;
3074 relsec = f->sections[i];
3075 if (relsec->header.sh_type != SHT_RELM)
3078 symsec = f->sections[relsec->header.sh_link];
3079 targsec = f->sections[relsec->header.sh_info];
3080 strsec = f->sections[symsec->header.sh_link];
3082 rel = (ElfW(RelM) *) relsec->contents;
3083 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3084 symtab = (ElfW(Sym) *) symsec->contents;
3085 strtab = (const char *) strsec->contents;
3087 for (; rel < relend; ++rel) {
3088 ElfW(Addr) value = 0;
3089 struct obj_symbol *intsym = NULL;
3090 unsigned long symndx;
3091 ElfW(Sym) * extsym = 0;
3094 /* Attempt to find a value to use for this relocation. */
3096 symndx = ELFW(R_SYM) (rel->r_info);
3098 /* Note we've already checked for undefined symbols. */
3100 extsym = &symtab[symndx];
3101 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
3102 /* Local symbols we look up in the local table to be sure
3103 we get the one that is really intended. */
3104 intsym = f->local_symtab[symndx];
3106 /* Others we look up in the hash table. */
3108 if (extsym->st_name)
3109 name = strtab + extsym->st_name;
3111 name = f->sections[extsym->st_shndx]->name;
3112 intsym = obj_find_symbol(f, name);
3115 value = obj_symbol_final_value(f, intsym);
3116 intsym->referenced = 1;
3118 #if SHT_RELM == SHT_RELA
3119 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3120 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3121 if (!extsym || !extsym->st_name ||
3122 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
3124 value += rel->r_addend;
3128 switch (arch_apply_relocation
3129 (f, targsec, symsec, intsym, rel, value)) {
3133 case obj_reloc_overflow:
3134 errmsg = "Relocation overflow";
3136 case obj_reloc_dangerous:
3137 errmsg = "Dangerous relocation";
3139 case obj_reloc_unhandled:
3140 errmsg = "Unhandled relocation";
3143 error_msg("%s of type %ld for %s", errmsg,
3144 (long) ELFW(R_TYPE) (rel->r_info),
3145 strtab + extsym->st_name);
3147 error_msg("%s of type %ld", errmsg,
3148 (long) ELFW(R_TYPE) (rel->r_info));
3156 /* Finally, take care of the patches. */
3158 if (f->string_patches) {
3159 struct obj_string_patch *p;
3160 struct obj_section *strsec;
3161 ElfW(Addr) strsec_base;
3162 strsec = obj_find_section(f, ".kstrtab");
3163 strsec_base = strsec->header.sh_addr;
3165 for (p = f->string_patches; p; p = p->next) {
3166 struct obj_section *targsec = f->sections[p->reloc_secidx];
3167 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3168 = strsec_base + p->string_offset;
3172 if (f->symbol_patches) {
3173 struct obj_symbol_patch *p;
3175 for (p = f->symbol_patches; p; p = p->next) {
3176 struct obj_section *targsec = f->sections[p->reloc_secidx];
3177 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3178 = obj_symbol_final_value(f, p->sym);
3185 static int obj_create_image(struct obj_file *f, char *image)
3187 struct obj_section *sec;
3188 ElfW(Addr) base = f->baseaddr;
3190 for (sec = f->load_order; sec; sec = sec->load_next) {
3193 if (sec->contents == 0 || sec->header.sh_size == 0)
3196 secimg = image + (sec->header.sh_addr - base);
3198 /* Note that we allocated data for NOBITS sections earlier. */
3199 memcpy(secimg, sec->contents, sec->header.sh_size);
3205 /*======================================================================*/
3207 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3210 ElfW(Shdr) * section_headers;
3214 /* Read the file header. */
3216 f = arch_new_file();
3217 memset(f, 0, sizeof(*f));
3218 f->symbol_cmp = strcmp;
3219 f->symbol_hash = obj_elf_hash;
3220 f->load_order_search_start = &f->load_order;
3222 fseek(fp, 0, SEEK_SET);
3223 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3224 perror_msg("error reading ELF header");
3228 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3229 || f->header.e_ident[EI_MAG1] != ELFMAG1
3230 || f->header.e_ident[EI_MAG2] != ELFMAG2
3231 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3232 error_msg("not an ELF file");
3235 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3236 || f->header.e_ident[EI_DATA] != ELFDATAM
3237 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3238 || !MATCH_MACHINE(f->header.e_machine)) {
3239 error_msg("ELF file not for this architecture");
3242 if (f->header.e_type != ET_REL) {
3243 error_msg("ELF file not a relocatable object");
3247 /* Read the section headers. */
3249 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3250 error_msg("section header size mismatch: %lu != %lu",
3251 (unsigned long) f->header.e_shentsize,
3252 (unsigned long) sizeof(ElfW(Shdr)));
3256 shnum = f->header.e_shnum;
3257 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3258 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3260 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3261 fseek(fp, f->header.e_shoff, SEEK_SET);
3262 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3263 perror_msg("error reading ELF section headers");
3267 /* Read the section data. */
3269 for (i = 0; i < shnum; ++i) {
3270 struct obj_section *sec;
3272 f->sections[i] = sec = arch_new_section();
3273 memset(sec, 0, sizeof(*sec));
3275 sec->header = section_headers[i];
3278 if(sec->header.sh_size) switch (sec->header.sh_type) {
3287 if (!loadprogbits) {
3288 sec->contents = NULL;
3295 if (sec->header.sh_size > 0) {
3296 sec->contents = xmalloc(sec->header.sh_size);
3297 fseek(fp, sec->header.sh_offset, SEEK_SET);
3298 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3299 perror_msg("error reading ELF section data");
3303 sec->contents = NULL;
3307 #if SHT_RELM == SHT_REL
3309 error_msg("RELA relocations not supported on this architecture");
3313 error_msg("REL relocations not supported on this architecture");
3318 if (sec->header.sh_type >= SHT_LOPROC) {
3319 /* Assume processor specific section types are debug
3320 info and can safely be ignored. If this is ever not
3321 the case (Hello MIPS?), don't put ifdefs here but
3322 create an arch_load_proc_section(). */
3326 error_msg("can't handle sections of type %ld",
3327 (long) sec->header.sh_type);
3332 /* Do what sort of interpretation as needed by each section. */
3334 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3336 for (i = 0; i < shnum; ++i) {
3337 struct obj_section *sec = f->sections[i];
3338 sec->name = shstrtab + sec->header.sh_name;
3341 for (i = 0; i < shnum; ++i) {
3342 struct obj_section *sec = f->sections[i];
3344 /* .modinfo should be contents only but gcc has no attribute for that.
3345 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3347 if (strcmp(sec->name, ".modinfo") == 0)
3348 sec->header.sh_flags &= ~SHF_ALLOC;
3350 if (sec->header.sh_flags & SHF_ALLOC)
3351 obj_insert_section_load_order(f, sec);
3353 switch (sec->header.sh_type) {
3356 unsigned long nsym, j;
3360 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3361 error_msg("symbol size mismatch: %lu != %lu",
3362 (unsigned long) sec->header.sh_entsize,
3363 (unsigned long) sizeof(ElfW(Sym)));
3367 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3368 strtab = f->sections[sec->header.sh_link]->contents;
3369 sym = (ElfW(Sym) *) sec->contents;
3371 /* Allocate space for a table of local symbols. */
3372 j = f->local_symtab_size = sec->header.sh_info;
3373 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3375 /* Insert all symbols into the hash table. */
3376 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3379 name = strtab + sym->st_name;
3381 name = f->sections[sym->st_shndx]->name;
3383 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3384 sym->st_value, sym->st_size);
3390 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3391 error_msg("relocation entry size mismatch: %lu != %lu",
3392 (unsigned long) sec->header.sh_entsize,
3393 (unsigned long) sizeof(ElfW(RelM)));
3397 /* XXX Relocation code from modutils-2.3.19 is not here.
3398 * Why? That's about 20 lines of code from obj/obj_load.c,
3399 * which gets done in a second pass through the sections.
3400 * This BusyBox insmod does similar work in obj_relocate(). */
3407 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3409 * load the unloaded sections directly into the memory allocated by
3410 * kernel for the module
3413 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3415 ElfW(Addr) base = f->baseaddr;
3416 struct obj_section* sec;
3418 for (sec = f->load_order; sec; sec = sec->load_next) {
3420 /* section already loaded? */
3421 if (sec->contents != NULL)
3424 if (sec->header.sh_size == 0)
3427 sec->contents = imagebase + (sec->header.sh_addr - base);
3428 fseek(fp, sec->header.sh_offset, SEEK_SET);
3429 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3430 error_msg("error reading ELF section data: %s\n", strerror(errno));
3439 static void hide_special_symbols(struct obj_file *f)
3441 static const char *const specials[] = {
3442 SPFX "cleanup_module",
3444 SPFX "kernel_version",
3448 struct obj_symbol *sym;
3449 const char *const *p;
3451 for (p = specials; *p; ++p)
3452 if ((sym = obj_find_symbol(f, *p)) != NULL)
3454 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3457 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
3458 static int obj_gpl_license(struct obj_file *f, const char **license)
3460 struct obj_section *sec;
3461 /* This list must match *exactly* the list of allowable licenses in
3462 * linux/include/linux/module.h. Checking for leading "GPL" will not
3463 * work, somebody will use "GPL sucks, this is proprietary".
3465 static const char *gpl_licenses[] = {
3468 "GPL and additional rights",
3473 if ((sec = obj_find_section(f, ".modinfo"))) {
3474 const char *value, *ptr, *endptr;
3475 ptr = sec->contents;
3476 endptr = ptr + sec->header.sh_size;
3477 while (ptr < endptr) {
3478 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3482 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3483 if (strcmp(value+1, gpl_licenses[i]) == 0)
3488 if (strchr(ptr, '\0'))
3489 ptr = strchr(ptr, '\0') + 1;
3497 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3498 #define TAINT_PROPRIETORY_MODULE (1<<0)
3499 #define TAINT_FORCED_MODULE (1<<1)
3500 #define TAINT_UNSAFE_SMP (1<<2)
3501 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3503 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3504 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3508 static int first = 1;
3509 if (fd < 0 && !kernel_has_tainted)
3510 return; /* New modutils on old kernel */
3511 printf("Warning: loading %s will taint the kernel: %s%s\n",
3512 m_name, text1, text2);
3514 printf(" See %s for information about tainted modules\n", TAINT_URL);
3518 read(fd, buf, sizeof(buf)-1);
3519 buf[sizeof(buf)-1] = '\0';
3520 oldval = strtoul(buf, NULL, 10);
3521 sprintf(buf, "%d\n", oldval | taint);
3522 write(fd, buf, strlen(buf));
3526 /* Check if loading this module will taint the kernel. */
3527 static void check_tainted_module(struct obj_file *f, char *m_name)
3529 static const char tainted_file[] = TAINT_FILENAME;
3530 int fd, kernel_has_tainted;
3533 kernel_has_tainted = 1;
3534 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3535 if (errno == ENOENT)
3536 kernel_has_tainted = 0;
3537 else if (errno == EACCES)
3538 kernel_has_tainted = 1;
3540 perror(tainted_file);
3541 kernel_has_tainted = 0;
3545 switch (obj_gpl_license(f, &ptr)) {
3549 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3552 /* The module has a non-GPL license so we pretend that the
3553 * kernel always has a taint flag to get a warning even on
3554 * kernels without the proc flag.
3556 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3559 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3563 if (flag_force_load)
3564 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3569 #else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3570 #define check_tainted_module(x, y) do { } while(0);
3571 #endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
3573 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3574 /* add module source, timestamp, kernel version and a symbol for the
3575 * start of some sections. this info is used by ksymoops to do better
3579 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
3581 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3582 if (get_modinfo_value(f, "kernel_version") == NULL)
3583 return old_get_module_version(f, str);
3585 return new_get_module_version(f, str);
3586 #else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3587 strncpy(str, "???", sizeof(str));
3589 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3592 /* add module source, timestamp, kernel version and a symbol for the
3593 * start of some sections. this info is used by ksymoops to do better
3597 add_ksymoops_symbols(struct obj_file *f, const char *filename,
3600 static const char symprefix[] = "__insmod_";
3601 struct obj_section *sec;
3602 struct obj_symbol *sym;
3603 char *name, *absolute_filename;
3604 char str[STRVERSIONLEN], real[PATH_MAX];
3605 int i, l, lm_name, lfilename, use_ksymtab, version;
3606 struct stat statbuf;
3608 static const char *section_names[] = {
3616 if (realpath(filename, real)) {
3617 absolute_filename = xstrdup(real);
3620 int save_errno = errno;
3621 error_msg("cannot get realpath for %s", filename);
3624 absolute_filename = xstrdup(filename);
3627 lm_name = strlen(m_name);
3628 lfilename = strlen(absolute_filename);
3630 /* add to ksymtab if it already exists or there is no ksymtab and other symbols
3631 * are not to be exported. otherwise leave ksymtab alone for now, the
3632 * "export all symbols" compatibility code will export these symbols later.
3634 use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
3636 if ((sec = obj_find_section(f, ".this"))) {
3637 /* tag the module header with the object name, last modified
3638 * timestamp and module version. worst case for module version
3639 * is 0xffffff, decimal 16777215. putting all three fields in
3640 * one symbol is less readable but saves kernel space.
3642 l = sizeof(symprefix)+ /* "__insmod_" */
3643 lm_name+ /* module name */
3645 lfilename+ /* object filename */
3647 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
3649 8+ /* version in dec */
3652 if (stat(absolute_filename, &statbuf) != 0)
3653 statbuf.st_mtime = 0;
3654 version = get_module_version(f, str); /* -1 if not found */
3655 snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
3656 symprefix, m_name, absolute_filename,
3657 (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
3659 sym = obj_add_symbol(f, name, -1,
3660 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3661 sec->idx, sec->header.sh_addr, 0);
3663 new_add_ksymtab(f, sym);
3665 free(absolute_filename);
3666 #ifdef _NOT_SUPPORTED_
3667 /* record where the persistent data is going, same address as previous symbol */
3670 l = sizeof(symprefix)+ /* "__insmod_" */
3671 lm_name+ /* module name */
3673 strlen(f->persist)+ /* data store */
3676 snprintf(name, l, "%s%s_P%s",
3677 symprefix, m_name, f->persist);
3678 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3679 sec->idx, sec->header.sh_addr, 0);
3681 new_add_ksymtab(f, sym);
3683 #endif /* _NOT_SUPPORTED_ */
3684 /* tag the desired sections if size is non-zero */
3686 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
3687 if ((sec = obj_find_section(f, section_names[i])) &&
3688 sec->header.sh_size) {
3689 l = sizeof(symprefix)+ /* "__insmod_" */
3690 lm_name+ /* module name */
3692 strlen(sec->name)+ /* section name */
3694 8+ /* length in dec */
3697 snprintf(name, l, "%s%s_S%s_L%ld",
3698 symprefix, m_name, sec->name,
3699 (long)sec->header.sh_size);
3700 sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
3701 sec->idx, sec->header.sh_addr, 0);
3703 new_add_ksymtab(f, sym);
3707 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3709 extern int insmod_main( int argc, char **argv)
3716 unsigned long m_size;
3721 int exit_status = EXIT_FAILURE;
3723 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3724 struct utsname uts_info;
3725 char m_strversion[STRVERSIONLEN];
3729 #ifdef CONFIG_FEATURE_CLEAN_UP
3735 /* Parse any options */
3736 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0) {
3738 case 'f': /* force loading */
3739 flag_force_load = 1;
3741 case 'k': /* module loaded by kerneld, auto-cleanable */
3744 case 's': /* log to syslog */
3745 /* log to syslog -- not supported */
3746 /* but kernel needs this for request_module(), */
3747 /* as this calls: modprobe -k -s -- <module> */
3748 /* so silently ignore this flag */
3750 case 'v': /* verbose output */
3753 case 'q': /* silent */
3756 case 'x': /* do not export externs */
3759 case 'o': /* name the output module */
3761 m_name = xstrdup(optarg);
3763 case 'L': /* Stub warning */
3764 /* This is needed for compatibility with modprobe.
3765 * In theory, this does locking, but we don't do
3766 * that. So be careful and plan your life around not
3767 * loading the same module 50 times concurrently. */
3774 if (argv[optind] == NULL) {
3778 /* Grab the module name */
3779 tmp1 = xstrdup(argv[optind]);
3780 tmp = basename(tmp1);
3783 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3788 bb_asprintf(&m_fullName, "%s.o", tmp);
3794 tmp1 = 0; /* flag for free(m_name) before exit() */
3797 /* Get a filedesc for the module. Check we we have a complete path */
3798 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3799 (fp = fopen(argv[optind], "r")) == NULL) {
3800 struct utsname myuname;
3802 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3803 * but do not error out yet if we fail to find it... */
3804 if (uname(&myuname) == 0) {
3807 char real_module_dir[FILENAME_MAX];
3809 tmdn = concat_path_file(_PATH_MODULES, myuname.release);
3810 /* Jump through hoops in case /lib/modules/`uname -r`
3811 * is a symlink. We do not want recursive_action to
3812 * follow symlinks, but we do want to follow the
3813 * /lib/modules/`uname -r` dir, So resolve it ourselves
3814 * if it is a link... */
3815 if (realpath (tmdn, real_module_dir) == NULL)
3818 module_dir = real_module_dir;
3819 recursive_action(module_dir, TRUE, FALSE, FALSE,
3820 check_module_name_match, 0, m_fullName);
3824 /* Check if we have found anything yet */
3825 if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
3827 char module_dir[FILENAME_MAX];
3831 if (realpath (_PATH_MODULES, module_dir) == NULL)
3832 strcpy(module_dir, _PATH_MODULES);
3833 /* No module found under /lib/modules/`uname -r`, this
3834 * time cast the net a bit wider. Search /lib/modules/ */
3835 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3836 check_module_name_match, 0, m_fullName))
3839 || ((fp = fopen(m_filename, "r")) == NULL))
3841 error_msg("%s: no module by that name found", m_fullName);
3845 error_msg_and_die("%s: no module by that name found", m_fullName);
3848 m_filename = xstrdup(argv[optind]);
3850 printf("Using %s\n", m_filename);
3852 if ((f = obj_load(fp, LOADBITS)) == NULL)
3853 perror_msg_and_die("Could not load the module");
3855 if (get_modinfo_value(f, "kernel_version") == NULL)
3860 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3861 /* Version correspondence? */
3863 if (uname(&uts_info) < 0)
3864 uts_info.release[0] = '\0';
3865 if (m_has_modinfo) {
3866 m_version = new_get_module_version(f, m_strversion);
3868 m_version = old_get_module_version(f, m_strversion);
3869 if (m_version == -1) {
3870 error_msg("couldn't find the kernel version the module was "
3876 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3877 if (flag_force_load) {
3878 error_msg("Warning: kernel-module version mismatch\n"
3879 "\t%s was compiled for kernel version %s\n"
3880 "\twhile this kernel is version %s",
3881 m_filename, m_strversion, uts_info.release);
3883 error_msg("kernel-module version mismatch\n"
3884 "\t%s was compiled for kernel version %s\n"
3885 "\twhile this kernel is version %s.",
3886 m_filename, m_strversion, uts_info.release);
3892 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3894 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3896 if (k_new_syscalls) {
3897 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
3898 if (!new_get_kernel_symbols())
3900 k_crcs = new_is_kernel_checksummed();
3902 error_msg("Not configured to support new kernels");
3906 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
3907 if (!old_get_kernel_symbols(m_name))
3909 k_crcs = old_is_kernel_checksummed();
3911 error_msg("Not configured to support old kernels");
3916 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3918 m_crcs = new_is_module_checksummed(f);
3920 m_crcs = old_is_module_checksummed(f);
3922 if (m_crcs != k_crcs)
3923 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3924 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3926 /* Let the module know about the kernel symbols. */
3927 add_kernel_symbols(f);
3929 /* Allocate common symbols, symbol tables, and string tables. */
3932 ? !new_create_this_module(f, m_name)
3933 : !old_create_mod_use_count(f))
3938 if (!obj_check_undefineds(f)) {
3941 obj_allocate_commons(f);
3942 check_tainted_module(f, m_name);
3944 /* done with the module name, on to the optional var=value arguments */
3947 if (optind < argc) {
3949 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3950 : !old_process_module_arguments(f, argc - optind, argv + optind))
3957 hide_special_symbols(f);
3959 #ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
3960 add_ksymoops_symbols(f, m_filename, m_name);
3961 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
3964 new_create_module_ksymtab(f);
3966 /* Find current size of the module */
3967 m_size = obj_load_size(f);
3970 m_addr = create_module(m_name, m_size);
3971 if (m_addr == -1) switch (errno) {
3973 error_msg("A module named %s already exists", m_name);
3976 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3980 perror_msg("create_module: %s", m_name);
3986 * the PROGBITS section was not loaded by the obj_load
3987 * now we can load them directly into the kernel memory
3989 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3990 delete_module(m_name);
3995 if (!obj_relocate(f, m_addr)) {
3996 delete_module(m_name);
4001 ? !new_init_module(m_name, f, m_size)
4002 : !old_init_module(m_name, f, m_size))
4004 delete_module(m_name);
4008 exit_status = EXIT_SUCCESS;
4011 #ifdef CONFIG_FEATURE_CLEAN_UP
4021 return(exit_status);