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
74 #include <sys/utsname.h>
77 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
78 # undef CONFIG_FEATURE_OLD_MODULE_INTERFACE
79 # define new_sys_init_module init_module
81 # define old_sys_init_module init_module
84 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
92 #define CONFIG_USE_PLT_ENTRIES
93 #define CONFIG_PLT_ENTRY_SIZE 8
94 #define CONFIG_USE_GOT_ENTRIES
95 #define CONFIG_GOT_ENTRY_SIZE 8
96 #define CONFIG_USE_SINGLE
98 #define MATCH_MACHINE(x) (x == EM_ARM)
99 #define SHT_RELM SHT_REL
100 #define Elf32_RelM Elf32_Rel
101 #define ELFCLASSM ELFCLASS32
104 #if defined(__i386__)
105 #define CONFIG_USE_GOT_ENTRIES
106 #define CONFIG_GOT_ENTRY_SIZE 4
107 #define CONFIG_USE_SINGLE
110 #define MATCH_MACHINE(x) (x == EM_386)
112 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
115 #define SHT_RELM SHT_REL
116 #define Elf32_RelM Elf32_Rel
117 #define ELFCLASSM ELFCLASS32
120 #if defined(__mc68000__)
121 #define CONFIG_USE_GOT_ENTRIES
122 #define CONFIG_GOT_ENTRY_SIZE 4
123 #define CONFIG_USE_SINGLE
125 #define MATCH_MACHINE(x) (x == EM_68K)
126 #define SHT_RELM SHT_RELA
127 #define Elf32_RelM Elf32_Rela
130 #if defined(__mips__)
131 /* Account for ELF spec changes. */
132 #ifndef EM_MIPS_RS3_LE
133 #ifdef EM_MIPS_RS4_BE
134 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
136 #define EM_MIPS_RS3_LE 10
138 #endif /* !EM_MIPS_RS3_LE */
140 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
141 #define SHT_RELM SHT_REL
142 #define Elf32_RelM Elf32_Rel
143 #define ELFCLASSM ELFCLASS32
144 #define ARCHDATAM "__dbe_table"
147 #if defined(__powerpc__)
148 #define CONFIG_USE_PLT_ENTRIES
149 #define CONFIG_PLT_ENTRY_SIZE 16
150 #define CONFIG_USE_PLT_LIST
151 #define CONFIG_LIST_ARCHTYPE ElfW(Addr)
152 #define CONFIG_USE_LIST
154 #define MATCH_MACHINE(x) (x == EM_PPC)
155 #define SHT_RELM SHT_RELA
156 #define Elf32_RelM Elf32_Rela
157 #define ELFCLASSM ELFCLASS32
158 #define ARCHDATAM "__ftr_fixup"
162 #define CONFIG_USE_GOT_ENTRIES
163 #define CONFIG_GOT_ENTRY_SIZE 4
164 #define CONFIG_USE_SINGLE
166 #define MATCH_MACHINE(x) (x == EM_SH)
167 #define SHT_RELM SHT_RELA
168 #define Elf32_RelM Elf32_Rela
169 #define ELFCLASSM ELFCLASS32
171 /* the SH changes have only been tested on the SH4 in =little endian= mode */
172 /* I'm not sure about big endian, so let's warn: */
174 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
175 #error insmod.c may require changes for use on big endian SH4/SH3
178 /* it may or may not work on the SH1/SH2... So let's error on those
180 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
181 #error insmod.c may require changes for non-SH3/SH4 use
185 #if defined (__v850e__)
186 #define CONFIG_USE_PLT_ENTRIES
187 #define CONFIG_PLT_ENTRY_SIZE 8
188 #define CONFIG_USE_SINGLE
190 #ifndef EM_CYGNUS_V850 /* grumble */
191 #define EM_CYGNUS_V850 0x9080
194 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
195 #define SHT_RELM SHT_RELA
196 #define Elf32_RelM Elf32_Rela
197 #define ELFCLASSM ELFCLASS32
199 #define SYMBOL_PREFIX "_"
203 #error Sorry, but insmod.c does not yet support this architecture...
207 //----------------------------------------------------------------------------
208 //--------modutils module.h, lines 45-242
209 //----------------------------------------------------------------------------
211 /* Definitions for the Linux module syscall interface.
212 Copyright 1996, 1997 Linux International.
214 Contributed by Richard Henderson <rth@tamu.edu>
216 This file is part of the Linux modutils.
218 This program is free software; you can redistribute it and/or modify it
219 under the terms of the GNU General Public License as published by the
220 Free Software Foundation; either version 2 of the License, or (at your
221 option) any later version.
223 This program is distributed in the hope that it will be useful, but
224 WITHOUT ANY WARRANTY; without even the implied warranty of
225 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
226 General Public License for more details.
228 You should have received a copy of the GNU General Public License
229 along with this program; if not, write to the Free Software Foundation,
230 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
233 #ifndef MODUTILS_MODULE_H
234 static const int MODUTILS_MODULE_H = 1;
236 #ident "$Id: insmod.c,v 1.84 2002/06/06 14:24:57 andersen Exp $"
238 /* This file contains the structures used by the 2.0 and 2.1 kernels.
239 We do not use the kernel headers directly because we do not wish
240 to be dependant on a particular kernel version to compile insmod. */
243 /*======================================================================*/
244 /* The structures used by Linux 2.0. */
246 /* The symbol format used by get_kernel_syms(2). */
247 struct old_kernel_sym
253 struct old_module_ref
255 unsigned long module; /* kernel addresses */
259 struct old_module_symbol
265 struct old_symbol_table
267 int size; /* total, including string table!!! */
270 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
271 struct old_module_ref ref[0]; /* actual size defined by n_refs */
274 struct old_mod_routines
277 unsigned long cleanup;
283 unsigned long ref; /* the list of modules that refer to me */
284 unsigned long symtab;
286 int size; /* size of module in pages */
287 unsigned long addr; /* address of module */
289 unsigned long cleanup; /* cleanup routine */
292 /* Sent to init_module(2) or'ed into the code size parameter. */
293 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
295 int get_kernel_syms(struct old_kernel_sym *);
296 int old_sys_init_module(const char *name, char *code, unsigned codesize,
297 struct old_mod_routines *, struct old_symbol_table *);
299 /*======================================================================*/
300 /* For sizeof() which are related to the module platform and not to the
301 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
303 #define tgt_sizeof_char sizeof(char)
304 #define tgt_sizeof_short sizeof(short)
305 #define tgt_sizeof_int sizeof(int)
306 #define tgt_sizeof_long sizeof(long)
307 #define tgt_sizeof_char_p sizeof(char *)
308 #define tgt_sizeof_void_p sizeof(void *)
309 #define tgt_long long
311 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
312 #undef tgt_sizeof_long
313 #undef tgt_sizeof_char_p
314 #undef tgt_sizeof_void_p
316 static const int tgt_sizeof_long = 8;
317 static const int tgt_sizeof_char_p = 8;
318 static const int tgt_sizeof_void_p = 8;
319 #define tgt_long long long
322 /*======================================================================*/
323 /* The structures used in Linux 2.1. */
325 /* Note: new_module_symbol does not use tgt_long intentionally */
326 struct new_module_symbol
332 struct new_module_persist;
334 struct new_module_ref
336 unsigned tgt_long dep; /* kernel addresses */
337 unsigned tgt_long ref;
338 unsigned tgt_long next_ref;
343 unsigned tgt_long size_of_struct; /* == sizeof(module) */
344 unsigned tgt_long next;
345 unsigned tgt_long name;
346 unsigned tgt_long size;
349 unsigned tgt_long flags; /* AUTOCLEAN et al */
354 unsigned tgt_long syms;
355 unsigned tgt_long deps;
356 unsigned tgt_long refs;
357 unsigned tgt_long init;
358 unsigned tgt_long cleanup;
359 unsigned tgt_long ex_table_start;
360 unsigned tgt_long ex_table_end;
362 unsigned tgt_long gp;
364 /* Everything after here is extension. */
365 unsigned tgt_long persist_start;
366 unsigned tgt_long persist_end;
367 unsigned tgt_long can_unload;
368 unsigned tgt_long runsize;
369 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
370 const char *kallsyms_start; /* All symbols for kernel debugging */
371 const char *kallsyms_end;
372 const char *archdata_start; /* arch specific data for module */
373 const char *archdata_end;
374 const char *kernel_data; /* Reserved for kernel internal use */
379 #define ARCHDATA_SEC_NAME ARCHDATAM
381 #define ARCHDATA_SEC_NAME "__archdata"
383 #define KALLSYMS_SEC_NAME "__kallsyms"
386 struct new_module_info
394 /* Bits of module.flags. */
395 static const int NEW_MOD_RUNNING = 1;
396 static const int NEW_MOD_DELETED = 2;
397 static const int NEW_MOD_AUTOCLEAN = 4;
398 static const int NEW_MOD_VISITED = 8;
399 static const int NEW_MOD_USED_ONCE = 16;
401 int new_sys_init_module(const char *name, const struct new_module *);
402 int query_module(const char *name, int which, void *buf, size_t bufsize,
405 /* Values for query_module's which. */
407 static const int QM_MODULES = 1;
408 static const int QM_DEPS = 2;
409 static const int QM_REFS = 3;
410 static const int QM_SYMBOLS = 4;
411 static const int QM_INFO = 5;
413 /*======================================================================*/
414 /* The system calls unchanged between 2.0 and 2.1. */
416 unsigned long create_module(const char *, size_t);
417 int delete_module(const char *);
420 #endif /* module.h */
422 //----------------------------------------------------------------------------
423 //--------end of modutils module.h
424 //----------------------------------------------------------------------------
428 //----------------------------------------------------------------------------
429 //--------modutils obj.h, lines 253-462
430 //----------------------------------------------------------------------------
432 /* Elf object file loading and relocation routines.
433 Copyright 1996, 1997 Linux International.
435 Contributed by Richard Henderson <rth@tamu.edu>
437 This file is part of the Linux modutils.
439 This program is free software; you can redistribute it and/or modify it
440 under the terms of the GNU General Public License as published by the
441 Free Software Foundation; either version 2 of the License, or (at your
442 option) any later version.
444 This program is distributed in the hope that it will be useful, but
445 WITHOUT ANY WARRANTY; without even the implied warranty of
446 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
447 General Public License for more details.
449 You should have received a copy of the GNU General Public License
450 along with this program; if not, write to the Free Software Foundation,
451 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
454 #ifndef MODUTILS_OBJ_H
455 static const int MODUTILS_OBJ_H = 1;
457 #ident "$Id: insmod.c,v 1.84 2002/06/06 14:24:57 andersen Exp $"
459 /* The relocatable object is manipulated using elfin types. */
465 #if __BYTE_ORDER == __LITTLE_ENDIAN
466 #define ELFDATAM ELFDATA2LSB
467 #elif __BYTE_ORDER == __BIG_ENDIAN
468 #define ELFDATAM ELFDATA2MSB
472 # if ELFCLASSM == ELFCLASS32
473 # define ElfW(x) Elf32_ ## x
474 # define ELFW(x) ELF32_ ## x
476 # define ElfW(x) Elf64_ ## x
477 # define ELFW(x) ELF64_ ## x
481 /* For some reason this is missing from libc5. */
482 #ifndef ELF32_ST_INFO
483 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
486 #ifndef ELF64_ST_INFO
487 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
490 struct obj_string_patch;
491 struct obj_symbol_patch;
498 struct obj_section *load_next;
504 struct obj_symbol *next; /* hash table link */
508 int secidx; /* the defining section index/module */
510 int ksymidx; /* for export to the kernel symtab */
511 int referenced; /* actually used in the link */
514 /* Hardcode the hash table size. We shouldn't be needing so many
515 symbols that we begin to degrade performance, and we get a big win
516 by giving the compiler a constant divisor. */
518 #define HASH_BUCKETS 521
524 struct obj_section **sections;
525 struct obj_section *load_order;
526 struct obj_section **load_order_search_start;
527 struct obj_string_patch *string_patches;
528 struct obj_symbol_patch *symbol_patches;
529 int (*symbol_cmp)(const char *, const char *);
530 unsigned long (*symbol_hash)(const char *);
531 unsigned long local_symtab_size;
532 struct obj_symbol **local_symtab;
533 struct obj_symbol *symtab[HASH_BUCKETS];
544 struct obj_string_patch
546 struct obj_string_patch *next;
548 ElfW(Addr) reloc_offset;
549 ElfW(Addr) string_offset;
552 struct obj_symbol_patch
554 struct obj_symbol_patch *next;
556 ElfW(Addr) reloc_offset;
557 struct obj_symbol *sym;
561 /* Generic object manipulation routines. */
563 static unsigned long obj_elf_hash(const char *);
565 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
567 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
570 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
571 struct obj_symbol *sym);
573 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
574 static void obj_set_symbol_compare(struct obj_file *f,
575 int (*cmp)(const char *, const char *),
576 unsigned long (*hash)(const char *));
579 static struct obj_section *obj_find_section (struct obj_file *f,
582 static void obj_insert_section_load_order (struct obj_file *f,
583 struct obj_section *sec);
585 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
590 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
595 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
597 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
600 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
601 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
602 struct obj_symbol *sym);
605 static int obj_check_undefineds(struct obj_file *f);
607 static void obj_allocate_commons(struct obj_file *f);
609 static unsigned long obj_load_size (struct obj_file *f);
611 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
613 static struct obj_file *obj_load(FILE *f, int loadprogbits);
615 static int obj_create_image (struct obj_file *f, char *image);
617 /* Architecture specific manipulation routines. */
619 static struct obj_file *arch_new_file (void);
621 static struct obj_section *arch_new_section (void);
623 static struct obj_symbol *arch_new_symbol (void);
625 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
626 struct obj_section *targsec,
627 struct obj_section *symsec,
628 struct obj_symbol *sym,
629 ElfW(RelM) *rel, ElfW(Addr) value);
631 static void arch_create_got (struct obj_file *f);
633 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
634 static int arch_init_module (struct obj_file *f, struct new_module *);
638 //----------------------------------------------------------------------------
639 //--------end of modutils obj.h
640 //----------------------------------------------------------------------------
644 /* SPFX is always a string, so it can be concatenated to string constants. */
646 #define SPFX SYMBOL_PREFIX
652 #define _PATH_MODULES "/lib/modules"
653 static const int STRVERSIONLEN = 32;
655 /*======================================================================*/
657 static int flag_force_load = 0;
658 static int flag_autoclean = 0;
659 static int flag_verbose = 0;
660 static int flag_export = 1;
663 /*======================================================================*/
665 #if defined(CONFIG_USE_LIST)
667 struct arch_list_entry
669 struct arch_list_entry *next;
670 CONFIG_LIST_ARCHTYPE addend;
677 #if defined(CONFIG_USE_SINGLE)
679 struct arch_single_entry
688 #if defined(__mips__)
691 struct mips_hi16 *next;
698 struct obj_file root;
699 #if defined(CONFIG_USE_PLT_ENTRIES)
700 struct obj_section *plt;
702 #if defined(CONFIG_USE_GOT_ENTRIES)
703 struct obj_section *got;
705 #if defined(__mips__)
706 struct mips_hi16 *mips_hi16_list;
711 struct obj_symbol root;
712 #if defined(CONFIG_USE_PLT_ENTRIES)
713 #if defined(CONFIG_USE_PLT_LIST)
714 struct arch_list_entry *pltent;
716 struct arch_single_entry pltent;
719 #if defined(CONFIG_USE_GOT_ENTRIES)
720 struct arch_single_entry gotent;
725 struct external_module {
730 struct new_module_symbol *syms;
733 static struct new_module_symbol *ksyms;
734 static size_t nksyms;
736 static struct external_module *ext_modules;
737 static int n_ext_modules;
738 static int n_ext_modules_used;
739 extern int delete_module(const char *);
741 static char m_filename[FILENAME_MAX + 1];
742 static char m_fullName[FILENAME_MAX + 1];
746 /*======================================================================*/
749 static int check_module_name_match(const char *filename, struct stat *statbuf,
752 char *fullname = (char *) userdata;
754 if (fullname[0] == '\0')
757 char *tmp, *tmp1 = xstrdup(filename);
758 tmp = get_last_path_component(tmp1);
759 if (strcmp(tmp, fullname) == 0) {
761 /* Stop searching if we find a match */
762 safe_strncpy(m_filename, filename, sizeof(m_filename));
771 /*======================================================================*/
773 static struct obj_file *arch_new_file(void)
776 f = xmalloc(sizeof(*f));
778 memset(f, 0, sizeof(*f));
783 static struct obj_section *arch_new_section(void)
785 return xmalloc(sizeof(struct obj_section));
788 static struct obj_symbol *arch_new_symbol(void)
790 struct arch_symbol *sym;
791 sym = xmalloc(sizeof(*sym));
793 memset(sym, 0, sizeof(*sym));
798 static enum obj_reloc
799 arch_apply_relocation(struct obj_file *f,
800 struct obj_section *targsec,
801 struct obj_section *symsec,
802 struct obj_symbol *sym,
803 ElfW(RelM) *rel, ElfW(Addr) v)
805 struct arch_file *ifile = (struct arch_file *) f;
806 enum obj_reloc ret = obj_reloc_ok;
807 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
808 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
809 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
810 struct arch_symbol *isym = (struct arch_symbol *) sym;
812 #if defined(CONFIG_USE_GOT_ENTRIES)
813 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
815 #if defined(CONFIG_USE_PLT_ENTRIES)
816 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
818 #if defined(CONFIG_USE_PLT_LIST)
819 struct arch_list_entry *pe;
821 struct arch_single_entry *pe;
825 switch (ELF32_R_TYPE(rel->r_info)) {
840 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
841 * (which is .got) similar to branch,
842 * but is full 32 bits relative */
852 case R_ARM_GOTOFF: /* address relative to the got */
857 #elif defined(__i386__)
893 #elif defined(__mc68000__)
904 ret = obj_reloc_overflow;
911 ret = obj_reloc_overflow;
918 if ((Elf32_Sword)v > 0x7f ||
919 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
920 ret = obj_reloc_overflow;
927 if ((Elf32_Sword)v > 0x7fff ||
928 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
929 ret = obj_reloc_overflow;
935 *(int *)loc = v - dot;
944 *(int *)loc += f->baseaddr;
955 #elif defined(__mips__)
966 ret = obj_reloc_dangerous;
967 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
968 ret = obj_reloc_overflow;
970 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
978 /* We cannot relocate this one now because we don't know the value
979 of the carry we need to add. Save the information, and let LO16
980 do the actual relocation. */
981 n = (struct mips_hi16 *) xmalloc(sizeof *n);
984 n->next = ifile->mips_hi16_list;
985 ifile->mips_hi16_list = n;
991 unsigned long insnlo = *loc;
992 Elf32_Addr val, vallo;
994 /* Sign extend the addend we extract from the lo insn. */
995 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
997 if (ifile->mips_hi16_list != NULL) {
1000 l = ifile->mips_hi16_list;
1002 struct mips_hi16 *next;
1005 /* The value for the HI16 had best be the same. */
1006 assert(v == l->value);
1008 /* Do the HI16 relocation. Note that we actually don't
1009 need to know anything about the LO16 itself, except where
1010 to find the low 16 bits of the addend needed by the LO16. */
1013 ((insn & 0xffff) << 16) +
1017 /* Account for the sign extension that will happen in the
1024 insn = (insn & ~0xffff) | val;
1032 ifile->mips_hi16_list = NULL;
1035 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1037 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1042 #elif defined(__powerpc__)
1044 case R_PPC_ADDR16_HA:
1045 *(unsigned short *)loc = (v + 0x8000) >> 16;
1048 case R_PPC_ADDR16_HI:
1049 *(unsigned short *)loc = v >> 16;
1052 case R_PPC_ADDR16_LO:
1053 *(unsigned short *)loc = v;
1067 #elif defined(__sh__)
1090 *loc = f->baseaddr + rel->r_addend;
1095 *loc = got - dot + rel->r_addend;
1109 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1110 ret = obj_reloc_unhandled;
1113 #if defined (__v850e__)
1118 /* We write two shorts instead of a long because even
1119 32-bit insns only need half-word alignment, but
1120 32-bit data needs to be long-word aligned. */
1121 v += ((unsigned short *)loc)[0];
1122 v += ((unsigned short *)loc)[1] << 16;
1123 ((unsigned short *)loc)[0] = v & 0xffff;
1124 ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
1127 case R_V850_22_PCREL:
1131 #if defined(CONFIG_USE_PLT_ENTRIES)
1135 /* find the plt entry and initialize it if necessary */
1136 assert(isym != NULL);
1138 #if defined(CONFIG_USE_PLT_LIST)
1139 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1147 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1149 /* generate some machine code */
1151 #if defined(__arm__)
1152 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1153 ip[1] = v; /* sym@ */
1155 #if defined(__powerpc__)
1156 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1157 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1158 ip[2] = 0x7d6903a6; /* mtctr r11 */
1159 ip[3] = 0x4e800420; /* bctr */
1161 #if defined (__v850e__)
1162 /* We have to trash a register, so we assume that any control
1163 transfer more than 21-bits away must be a function call
1164 (so we can use a call-clobbered register). */
1165 ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
1166 ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
1171 /* relative distance to target */
1173 /* if the target is too far away.... */
1174 #if defined (__arm__) || defined (__powerpc__)
1175 if ((int)v < -0x02000000 || (int)v >= 0x02000000)
1176 #elif defined (__v850e__)
1177 if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
1179 /* go via the plt */
1180 v = plt + pe->offset - dot;
1182 #if defined (__v850e__)
1187 ret = obj_reloc_dangerous;
1189 /* merge the offset into the instruction. */
1190 #if defined(__arm__)
1191 /* Convert to words. */
1194 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1196 #if defined(__powerpc__)
1197 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1199 #if defined (__v850e__)
1200 /* We write two shorts instead of a long because even 32-bit insns
1201 only need half-word alignment, but the 32-bit data write needs
1202 to be long-word aligned. */
1203 ((unsigned short *)loc)[0] =
1204 (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
1205 | ((v >> 16) & 0x3f); /* offs high part */
1206 ((unsigned short *)loc)[1] =
1207 (v & 0xffff); /* offs low part */
1210 #endif /* CONFIG_USE_PLT_ENTRIES */
1212 #if defined(CONFIG_USE_GOT_ENTRIES)
1215 assert(isym != NULL);
1216 /* needs an entry in the .got: set it, once */
1217 if (!isym->gotent.inited) {
1218 isym->gotent.inited = 1;
1219 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1221 /* make the reloc with_respect_to_.got */
1223 *loc += isym->gotent.offset + rel->r_addend;
1224 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1225 *loc += isym->gotent.offset;
1229 #endif /* CONFIG_USE_GOT_ENTRIES */
1236 #if defined(CONFIG_USE_LIST)
1238 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1239 int offset, int size)
1241 struct arch_list_entry *pe;
1243 for (pe = *list; pe != NULL; pe = pe->next) {
1244 if (pe->addend == rel->r_addend) {
1250 pe = xmalloc(sizeof(struct arch_list_entry));
1252 pe->addend = rel->r_addend;
1253 pe->offset = offset;
1263 #if defined(CONFIG_USE_SINGLE)
1265 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1266 int offset, int size)
1268 if (single->allocated == 0) {
1269 single->allocated = 1;
1270 single->offset = offset;
1279 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1281 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1282 int offset, int size)
1284 struct obj_section *myrelsec = obj_find_section(f, name);
1291 obj_extend_section(myrelsec, offset);
1293 myrelsec = obj_create_alloced_section(f, name,
1303 static void arch_create_got(struct obj_file *f)
1305 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1306 struct arch_file *ifile = (struct arch_file *) f;
1308 #if defined(CONFIG_USE_GOT_ENTRIES)
1309 int got_offset = 0, got_needed = 0, got_allocate;
1311 #if defined(CONFIG_USE_PLT_ENTRIES)
1312 int plt_offset = 0, plt_needed = 0, plt_allocate;
1314 struct obj_section *relsec, *symsec, *strsec;
1315 ElfW(RelM) *rel, *relend;
1316 ElfW(Sym) *symtab, *extsym;
1317 const char *strtab, *name;
1318 struct arch_symbol *intsym;
1320 for (i = 0; i < f->header.e_shnum; ++i) {
1321 relsec = f->sections[i];
1322 if (relsec->header.sh_type != SHT_RELM)
1325 symsec = f->sections[relsec->header.sh_link];
1326 strsec = f->sections[symsec->header.sh_link];
1328 rel = (ElfW(RelM) *) relsec->contents;
1329 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1330 symtab = (ElfW(Sym) *) symsec->contents;
1331 strtab = (const char *) strsec->contents;
1333 for (; rel < relend; ++rel) {
1334 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1336 #if defined(CONFIG_USE_GOT_ENTRIES)
1339 #if defined(CONFIG_USE_PLT_ENTRIES)
1343 switch (ELF32_R_TYPE(rel->r_info)) {
1344 #if defined(__arm__)
1359 #elif defined(__i386__)
1369 #elif defined(__powerpc__)
1374 #elif defined(__mc68000__)
1383 #elif defined(__sh__)
1393 #elif defined (__v850e__)
1394 case R_V850_22_PCREL:
1403 if (extsym->st_name != 0) {
1404 name = strtab + extsym->st_name;
1406 name = f->sections[extsym->st_shndx]->name;
1408 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1409 #if defined(CONFIG_USE_GOT_ENTRIES)
1411 got_offset += arch_single_init(
1412 rel, &intsym->gotent,
1413 got_offset, CONFIG_GOT_ENTRY_SIZE);
1418 #if defined(CONFIG_USE_PLT_ENTRIES)
1420 #if defined(CONFIG_USE_PLT_LIST)
1421 plt_offset += arch_list_add(
1422 rel, &intsym->pltent,
1423 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1425 plt_offset += arch_single_init(
1426 rel, &intsym->pltent,
1427 plt_offset, CONFIG_PLT_ENTRY_SIZE);
1435 #if defined(CONFIG_USE_GOT_ENTRIES)
1437 ifile->got = arch_xsect_init(f, ".got", got_offset,
1438 CONFIG_GOT_ENTRY_SIZE);
1442 #if defined(CONFIG_USE_PLT_ENTRIES)
1444 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1445 CONFIG_PLT_ENTRY_SIZE);
1449 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
1452 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
1453 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1459 /*======================================================================*/
1461 /* Standard ELF hash function. */
1462 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1464 unsigned long h = 0;
1471 if ((g = (h & 0xf0000000)) != 0) {
1480 static unsigned long obj_elf_hash(const char *name)
1482 return obj_elf_hash_n(name, strlen(name));
1485 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1486 /* String comparison for non-co-versioned kernel and module. */
1488 static int ncv_strcmp(const char *a, const char *b)
1490 size_t alen = strlen(a), blen = strlen(b);
1492 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1493 return strncmp(a, b, alen);
1494 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1495 return strncmp(a, b, blen);
1497 return strcmp(a, b);
1500 /* String hashing for non-co-versioned kernel and module. Here
1501 we are simply forced to drop the crc from the hash. */
1503 static unsigned long ncv_symbol_hash(const char *str)
1505 size_t len = strlen(str);
1506 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1508 return obj_elf_hash_n(str, len);
1512 obj_set_symbol_compare(struct obj_file *f,
1513 int (*cmp) (const char *, const char *),
1514 unsigned long (*hash) (const char *))
1517 f->symbol_cmp = cmp;
1519 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1522 f->symbol_hash = hash;
1524 memcpy(tmptab, f->symtab, sizeof(tmptab));
1525 memset(f->symtab, 0, sizeof(f->symtab));
1527 for (i = 0; i < HASH_BUCKETS; ++i)
1528 for (sym = tmptab[i]; sym; sym = next) {
1529 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1531 sym->next = f->symtab[h];
1537 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1539 static struct obj_symbol *
1540 obj_add_symbol(struct obj_file *f, const char *name,
1541 unsigned long symidx, int info,
1542 int secidx, ElfW(Addr) value,
1545 struct obj_symbol *sym;
1546 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1547 int n_type = ELFW(ST_TYPE) (info);
1548 int n_binding = ELFW(ST_BIND) (info);
1550 for (sym = f->symtab[hash]; sym; sym = sym->next)
1551 if (f->symbol_cmp(sym->name, name) == 0) {
1552 int o_secidx = sym->secidx;
1553 int o_info = sym->info;
1554 int o_type = ELFW(ST_TYPE) (o_info);
1555 int o_binding = ELFW(ST_BIND) (o_info);
1557 /* A redefinition! Is it legal? */
1559 if (secidx == SHN_UNDEF)
1561 else if (o_secidx == SHN_UNDEF)
1563 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1564 /* Cope with local and global symbols of the same name
1565 in the same object file, as might have been created
1566 by ld -r. The only reason locals are now seen at this
1567 level at all is so that we can do semi-sensible things
1570 struct obj_symbol *nsym, **p;
1572 nsym = arch_new_symbol();
1573 nsym->next = sym->next;
1576 /* Excise the old (local) symbol from the hash chain. */
1577 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1581 } else if (n_binding == STB_LOCAL) {
1582 /* Another symbol of the same name has already been defined.
1583 Just add this to the local table. */
1584 sym = arch_new_symbol();
1587 f->local_symtab[symidx] = sym;
1589 } else if (n_binding == STB_WEAK)
1591 else if (o_binding == STB_WEAK)
1593 /* Don't unify COMMON symbols with object types the programmer
1595 else if (secidx == SHN_COMMON
1596 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1598 else if (o_secidx == SHN_COMMON
1599 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1602 /* Don't report an error if the symbol is coming from
1603 the kernel or some external module. */
1604 if (secidx <= SHN_HIRESERVE)
1605 error_msg("%s multiply defined", name);
1610 /* Completely new symbol. */
1611 sym = arch_new_symbol();
1612 sym->next = f->symtab[hash];
1613 f->symtab[hash] = sym;
1616 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1617 if (symidx >= f->local_symtab_size)
1618 error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1619 name, (long) symidx, (long) f->local_symtab_size);
1621 f->local_symtab[symidx] = sym;
1628 sym->secidx = secidx;
1634 static struct obj_symbol *
1635 obj_find_symbol(struct obj_file *f, const char *name)
1637 struct obj_symbol *sym;
1638 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1640 for (sym = f->symtab[hash]; sym; sym = sym->next)
1641 if (f->symbol_cmp(sym->name, name) == 0)
1648 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1651 if (sym->secidx >= SHN_LORESERVE)
1654 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1656 /* As a special case, a NULL sym has value zero. */
1661 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1663 int i, n = f->header.e_shnum;
1665 for (i = 0; i < n; ++i)
1666 if (strcmp(f->sections[i]->name, name) == 0)
1667 return f->sections[i];
1672 static int obj_load_order_prio(struct obj_section *a)
1674 unsigned long af, ac;
1676 af = a->header.sh_flags;
1679 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1680 strcmp(a->name + 5, ".init"))
1684 if (!(af & SHF_WRITE))
1686 if (af & SHF_EXECINSTR)
1688 if (a->header.sh_type != SHT_NOBITS)
1695 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1697 struct obj_section **p;
1698 int prio = obj_load_order_prio(sec);
1699 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1700 if (obj_load_order_prio(*p) < prio)
1702 sec->load_next = *p;
1706 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1708 unsigned long align,
1711 int newidx = f->header.e_shnum++;
1712 struct obj_section *sec;
1714 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1715 f->sections[newidx] = sec = arch_new_section();
1717 memset(sec, 0, sizeof(*sec));
1718 sec->header.sh_type = SHT_PROGBITS;
1719 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1720 sec->header.sh_size = size;
1721 sec->header.sh_addralign = align;
1725 sec->contents = xmalloc(size);
1727 obj_insert_section_load_order(f, sec);
1732 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1734 unsigned long align,
1737 int newidx = f->header.e_shnum++;
1738 struct obj_section *sec;
1740 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1741 f->sections[newidx] = sec = arch_new_section();
1743 memset(sec, 0, sizeof(*sec));
1744 sec->header.sh_type = SHT_PROGBITS;
1745 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1746 sec->header.sh_size = size;
1747 sec->header.sh_addralign = align;
1751 sec->contents = xmalloc(size);
1753 sec->load_next = f->load_order;
1754 f->load_order = sec;
1755 if (f->load_order_search_start == &f->load_order)
1756 f->load_order_search_start = &sec->load_next;
1761 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1763 unsigned long oldsize = sec->header.sh_size;
1765 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1767 return sec->contents + oldsize;
1771 /* Conditionally add the symbols from the given symbol set to the
1777 int idx, struct new_module_symbol *syms, size_t nsyms)
1779 struct new_module_symbol *s;
1782 #ifdef SYMBOL_PREFIX
1784 size_t name_alloced_size = 0;
1787 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1788 /* Only add symbols that are already marked external.
1789 If we override locals we may cause problems for
1790 argument initialization. We will also create a false
1791 dependency on the module. */
1792 struct obj_symbol *sym;
1793 char *name = (char *)s->name;
1795 #ifdef SYMBOL_PREFIX
1796 /* Prepend SYMBOL_PREFIX to the symbol's name (the
1797 kernel exports `C names', but module object files
1798 reference `linker names'). */
1799 size_t extra = sizeof SYMBOL_PREFIX;
1800 size_t name_size = strlen (name) + extra;
1801 if (name_size > name_alloced_size) {
1802 name_alloced_size = name_size * 2;
1803 name_buf = alloca (name_alloced_size);
1805 strcpy (name_buf, SYMBOL_PREFIX);
1806 strcpy (name_buf + extra - 1, name);
1808 #endif /* SYMBOL_PREFIX */
1810 sym = obj_find_symbol(f, name);
1811 if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
1812 #ifdef SYMBOL_PREFIX
1813 /* Put NAME_BUF into more permanent storage. */
1814 name = xmalloc (name_size);
1815 strcpy (name, name_buf);
1817 sym = obj_add_symbol(f, name, -1,
1818 ELFW(ST_INFO) (STB_GLOBAL,
1821 /* Did our symbol just get installed? If so, mark the
1822 module as "used". */
1823 if (sym->secidx == idx)
1831 static void add_kernel_symbols(struct obj_file *f)
1833 struct external_module *m;
1836 /* Add module symbols first. */
1838 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1840 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1841 m->nsyms)) m->used = 1, ++nused;
1843 n_ext_modules_used = nused;
1845 /* And finally the symbols from the kernel proper. */
1848 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1851 static char *get_modinfo_value(struct obj_file *f, const char *key)
1853 struct obj_section *sec;
1854 char *p, *v, *n, *ep;
1855 size_t klen = strlen(key);
1857 sec = obj_find_section(f, ".modinfo");
1861 ep = p + sec->header.sh_size;
1864 n = strchr(p, '\0');
1866 if (p + klen == v && strncmp(p, key, klen) == 0)
1869 if (p + klen == n && strcmp(p, key) == 0)
1879 /*======================================================================*/
1880 /* Functions relating to module loading in pre 2.1 kernels. */
1883 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1887 struct obj_symbol *sym;
1891 if ((q = strchr(p, '=')) == NULL) {
1897 sym = obj_find_symbol(f, p);
1899 /* Also check that the parameter was not resolved from the kernel. */
1900 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1901 error_msg("symbol for parameter %s not found", p);
1905 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1907 /* Do C quoting if we begin with a ". */
1911 str = alloca(strlen(q));
1912 for (r = str, q++; *q != '"'; ++q, ++r) {
1914 error_msg("improperly terminated string argument for %s", p);
1916 } else if (*q == '\\')
1950 if (q[1] >= '0' && q[1] <= '7') {
1951 c = (c * 8) + *++q - '0';
1952 if (q[1] >= '0' && q[1] <= '7')
1953 c = (c * 8) + *++q - '0';
1966 obj_string_patch(f, sym->secidx, sym->value, str);
1967 } else if (*q >= '0' && *q <= '9') {
1969 *loc++ = strtoul(q, &q, 0);
1970 while (*q++ == ',');
1972 char *contents = f->sections[sym->secidx]->contents;
1973 char *myloc = contents + sym->value;
1974 char *r; /* To search for commas */
1976 /* Break the string with comas */
1977 while ((r = strchr(q, ',')) != (char *) NULL) {
1979 obj_string_patch(f, sym->secidx, myloc - contents, q);
1980 myloc += sizeof(char *);
1985 obj_string_patch(f, sym->secidx, myloc - contents, q);
1994 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1995 static int old_is_module_checksummed(struct obj_file *f)
1997 return obj_find_symbol(f, "Using_Versions") != NULL;
1999 /* Get the module's kernel version in the canonical integer form. */
2002 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2004 struct obj_symbol *sym;
2008 sym = obj_find_symbol(f, "kernel_version");
2012 p = f->sections[sym->secidx]->contents + sym->value;
2013 strncpy(str, p, STRVERSIONLEN);
2015 a = strtoul(p, &p, 10);
2018 b = strtoul(p + 1, &p, 10);
2021 c = strtoul(p + 1, &q, 10);
2025 return a << 16 | b << 8 | c;
2028 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2030 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
2032 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
2034 static int old_get_kernel_symbols(const char *m_name)
2036 struct old_kernel_sym *ks, *k;
2037 struct new_module_symbol *s;
2038 struct external_module *mod;
2039 int nks, nms, nmod, i;
2041 nks = get_kernel_syms(NULL);
2044 perror_msg("get_kernel_syms: %s", m_name);
2046 error_msg("No kernel symbols");
2050 ks = k = xmalloc(nks * sizeof(*ks));
2052 if (get_kernel_syms(ks) != nks) {
2053 perror("inconsistency with get_kernel_syms -- is someone else "
2054 "playing with modules?");
2059 /* Collect the module information. */
2064 while (k->name[0] == '#' && k->name[1]) {
2065 struct old_kernel_sym *k2;
2067 /* Find out how many symbols this module has. */
2068 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
2072 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
2073 mod[nmod].name = k->name + 1;
2074 mod[nmod].addr = k->value;
2076 mod[nmod].nsyms = nms;
2077 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2079 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
2080 s->name = (unsigned long) k->name;
2081 s->value = k->value;
2088 n_ext_modules = nmod + 1;
2090 /* Now collect the symbols for the kernel proper. */
2092 if (k->name[0] == '#')
2095 nksyms = nms = nks - (k - ks);
2096 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2098 for (i = 0; i < nms; ++i, ++s, ++k) {
2099 s->name = (unsigned long) k->name;
2100 s->value = k->value;
2106 /* Return the kernel symbol checksum version, or zero if not used. */
2108 static int old_is_kernel_checksummed(void)
2110 /* Using_Versions is the first symbol. */
2112 && strcmp((char *) ksyms[0].name,
2113 "Using_Versions") == 0) return ksyms[0].value;
2119 static int old_create_mod_use_count(struct obj_file *f)
2121 struct obj_section *sec;
2123 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2126 obj_add_symbol(f, "mod_use_count_", -1,
2127 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2134 old_init_module(const char *m_name, struct obj_file *f,
2135 unsigned long m_size)
2138 struct old_mod_routines routines;
2139 struct old_symbol_table *symtab;
2142 /* Create the symbol table */
2144 int nsyms = 0, strsize = 0, total;
2146 /* Size things first... */
2149 for (i = 0; i < HASH_BUCKETS; ++i) {
2150 struct obj_symbol *sym;
2151 for (sym = f->symtab[i]; sym; sym = sym->next)
2152 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2153 && sym->secidx <= SHN_HIRESERVE)
2155 sym->ksymidx = nsyms++;
2156 strsize += strlen(sym->name) + 1;
2161 total = (sizeof(struct old_symbol_table)
2162 + nsyms * sizeof(struct old_module_symbol)
2163 + n_ext_modules_used * sizeof(struct old_module_ref)
2165 symtab = xmalloc(total);
2166 symtab->size = total;
2167 symtab->n_symbols = nsyms;
2168 symtab->n_refs = n_ext_modules_used;
2170 if (flag_export && nsyms) {
2171 struct old_module_symbol *ksym;
2175 ksym = symtab->symbol;
2176 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2177 + n_ext_modules_used * sizeof(struct old_module_ref));
2179 for (i = 0; i < HASH_BUCKETS; ++i) {
2180 struct obj_symbol *sym;
2181 for (sym = f->symtab[i]; sym; sym = sym->next)
2182 if (sym->ksymidx >= 0) {
2183 ksym->addr = obj_symbol_final_value(f, sym);
2185 (unsigned long) str - (unsigned long) symtab;
2187 strcpy(str, sym->name);
2188 str += strlen(sym->name) + 1;
2194 if (n_ext_modules_used) {
2195 struct old_module_ref *ref;
2198 ref = (struct old_module_ref *)
2199 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2201 for (i = 0; i < n_ext_modules; ++i)
2202 if (ext_modules[i].used)
2203 ref++->module = ext_modules[i].addr;
2207 /* Fill in routines. */
2210 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2212 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2214 /* Whew! All of the initialization is complete. Collect the final
2215 module image and give it to the kernel. */
2217 image = xmalloc(m_size);
2218 obj_create_image(f, image);
2220 /* image holds the complete relocated module, accounting correctly for
2221 mod_use_count. However the old module kernel support assume that
2222 it is receiving something which does not contain mod_use_count. */
2223 ret = old_sys_init_module(m_name, image + sizeof(long),
2224 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2225 : 0), &routines, symtab);
2227 perror_msg("init_module: %s", m_name);
2237 #define old_create_mod_use_count(x) TRUE
2238 #define old_init_module(x, y, z) TRUE
2240 #endif /* CONFIG_FEATURE_OLD_MODULE_INTERFACE */
2244 /*======================================================================*/
2245 /* Functions relating to module loading after 2.1.18. */
2248 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2251 char *p, *q, *key, *sym_name;
2252 struct obj_symbol *sym;
2253 char *contents, *loc;
2257 if ((q = strchr(p, '=')) == NULL) {
2262 key = alloca(q - p + 6);
2263 memcpy(key, "parm_", 5);
2264 memcpy(key + 5, p, q - p);
2267 p = get_modinfo_value(f, key);
2270 error_msg("invalid parameter %s", key);
2274 #ifdef SYMBOL_PREFIX
2275 sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
2276 strcpy (sym_name, SYMBOL_PREFIX);
2277 strcat (sym_name, key);
2281 sym = obj_find_symbol(f, sym_name);
2283 /* Also check that the parameter was not resolved from the kernel. */
2284 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2285 error_msg("symbol for parameter %s not found", key);
2290 min = strtoul(p, &p, 10);
2292 max = strtoul(p + 1, &p, 10);
2298 contents = f->sections[sym->secidx]->contents;
2299 loc = contents + sym->value;
2303 if ((*p == 's') || (*p == 'c')) {
2306 /* Do C quoting if we begin with a ", else slurp the lot. */
2310 str = alloca(strlen(q));
2311 for (r = str, q++; *q != '"'; ++q, ++r) {
2313 error_msg("improperly terminated string argument for %s",
2316 } else if (*q == '\\')
2350 if (q[1] >= '0' && q[1] <= '7') {
2351 c = (c * 8) + *++q - '0';
2352 if (q[1] >= '0' && q[1] <= '7')
2353 c = (c * 8) + *++q - '0';
2370 /* In this case, the string is not quoted. We will break
2371 it using the coma (like for ints). If the user wants to
2372 include comas in a string, he just has to quote it */
2374 /* Search the next coma */
2378 if (r != (char *) NULL) {
2379 /* Recopy the current field */
2380 str = alloca(r - q + 1);
2381 memcpy(str, q, r - q);
2383 /* I don't know if it is usefull, as the previous case
2384 doesn't null terminate the string ??? */
2387 /* Keep next fields */
2398 obj_string_patch(f, sym->secidx, loc - contents, str);
2399 loc += tgt_sizeof_char_p;
2401 /* Array of chars (in fact, matrix !) */
2402 unsigned long charssize; /* size of each member */
2404 /* Get the size of each member */
2405 /* Probably we should do that outside the loop ? */
2406 if (!isdigit(*(p + 1))) {
2407 error_msg("parameter type 'c' for %s must be followed by"
2408 " the maximum size", key);
2411 charssize = strtoul(p + 1, (char **) NULL, 10);
2414 if (strlen(str) >= charssize) {
2415 error_msg("string too long for %s (max %ld)", key,
2420 /* Copy to location */
2421 strcpy((char *) loc, str);
2425 long v = strtoul(q, &q, 0);
2432 loc += tgt_sizeof_short;
2436 loc += tgt_sizeof_int;
2440 loc += tgt_sizeof_long;
2444 error_msg("unknown parameter type '%c' for %s", *p, key);
2459 goto retry_end_of_value;
2463 error_msg("too many values for %s (max %d)", key, max);
2470 error_msg("invalid argument syntax for %s", key);
2477 error_msg("too few values for %s (min %d)", key, min);
2487 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2488 static int new_is_module_checksummed(struct obj_file *f)
2490 const char *p = get_modinfo_value(f, "using_checksums");
2497 /* Get the module's kernel version in the canonical integer form. */
2500 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2505 p = get_modinfo_value(f, "kernel_version");
2508 strncpy(str, p, STRVERSIONLEN);
2510 a = strtoul(p, &p, 10);
2513 b = strtoul(p + 1, &p, 10);
2516 c = strtoul(p + 1, &q, 10);
2520 return a << 16 | b << 8 | c;
2523 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2526 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2528 /* Fetch the loaded modules, and all currently exported symbols. */
2530 static int new_get_kernel_symbols(void)
2532 char *module_names, *mn;
2533 struct external_module *modules, *m;
2534 struct new_module_symbol *syms, *s;
2535 size_t ret, bufsize, nmod, nsyms, i, j;
2537 /* Collect the loaded modules. */
2539 module_names = xmalloc(bufsize = 256);
2541 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2542 if (errno == ENOSPC && bufsize < ret) {
2543 module_names = xrealloc(module_names, bufsize = ret);
2544 goto retry_modules_load;
2546 perror_msg("QM_MODULES");
2550 n_ext_modules = nmod = ret;
2552 /* Collect the modules' symbols. */
2555 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2556 memset(modules, 0, nmod * sizeof(*modules));
2557 for (i = 0, mn = module_names, m = modules;
2558 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2559 struct new_module_info info;
2561 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2562 if (errno == ENOENT) {
2563 /* The module was removed out from underneath us. */
2566 perror_msg("query_module: QM_INFO: %s", mn);
2570 syms = xmalloc(bufsize = 1024);
2572 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2575 syms = xrealloc(syms, bufsize = ret);
2576 goto retry_mod_sym_load;
2578 /* The module was removed out from underneath us. */
2581 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2588 m->addr = info.addr;
2592 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2593 s->name += (unsigned long) syms;
2598 /* Collect the kernel's symbols. */
2600 syms = xmalloc(bufsize = 16 * 1024);
2601 retry_kern_sym_load:
2602 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2603 if (errno == ENOSPC && bufsize < ret) {
2604 syms = xrealloc(syms, bufsize = ret);
2605 goto retry_kern_sym_load;
2607 perror_msg("kernel: QM_SYMBOLS");
2610 nksyms = nsyms = ret;
2613 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2614 s->name += (unsigned long) syms;
2620 /* Return the kernel symbol checksum version, or zero if not used. */
2622 static int new_is_kernel_checksummed(void)
2624 struct new_module_symbol *s;
2627 /* Using_Versions is not the first symbol, but it should be in there. */
2629 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2630 if (strcmp((char *) s->name, "Using_Versions") == 0)
2637 static int new_create_this_module(struct obj_file *f, const char *m_name)
2639 struct obj_section *sec;
2641 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2642 sizeof(struct new_module));
2643 memset(sec->contents, 0, sizeof(struct new_module));
2645 obj_add_symbol(f, SPFX "__this_module", -1,
2646 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2647 sizeof(struct new_module));
2649 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2656 static int new_create_module_ksymtab(struct obj_file *f)
2658 struct obj_section *sec;
2661 /* We must always add the module references. */
2663 if (n_ext_modules_used) {
2664 struct new_module_ref *dep;
2665 struct obj_symbol *tm;
2667 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2668 (sizeof(struct new_module_ref)
2669 * n_ext_modules_used));
2673 tm = obj_find_symbol(f, SPFX "__this_module");
2674 dep = (struct new_module_ref *) sec->contents;
2675 for (i = 0; i < n_ext_modules; ++i)
2676 if (ext_modules[i].used) {
2677 dep->dep = ext_modules[i].addr;
2678 obj_symbol_patch(f, sec->idx,
2679 (char *) &dep->ref - sec->contents, tm);
2685 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2690 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2693 /* We don't want to export symbols residing in sections that
2694 aren't loaded. There are a number of these created so that
2695 we make sure certain module options don't appear twice. */
2697 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2699 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2701 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2702 struct obj_symbol *sym;
2703 for (sym = f->symtab[i]; sym; sym = sym->next)
2704 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2705 && sym->secidx <= SHN_HIRESERVE
2706 && (sym->secidx >= SHN_LORESERVE
2707 || loaded[sym->secidx])) {
2708 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2710 obj_symbol_patch(f, sec->idx, ofs, sym);
2711 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2718 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2726 new_init_module(const char *m_name, struct obj_file *f,
2727 unsigned long m_size)
2729 struct new_module *module;
2730 struct obj_section *sec;
2735 sec = obj_find_section(f, ".this");
2736 if (!sec || !sec->contents) {
2737 perror_msg_and_die("corrupt module %s?",m_name);
2739 module = (struct new_module *) sec->contents;
2740 m_addr = sec->header.sh_addr;
2742 module->size_of_struct = sizeof(*module);
2743 module->size = m_size;
2744 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2746 sec = obj_find_section(f, "__ksymtab");
2747 if (sec && sec->header.sh_size) {
2748 module->syms = sec->header.sh_addr;
2749 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2752 if (n_ext_modules_used) {
2753 sec = obj_find_section(f, ".kmodtab");
2754 module->deps = sec->header.sh_addr;
2755 module->ndeps = n_ext_modules_used;
2759 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
2761 obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
2763 sec = obj_find_section(f, "__ex_table");
2765 module->ex_table_start = sec->header.sh_addr;
2766 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2769 sec = obj_find_section(f, ".text.init");
2771 module->runsize = sec->header.sh_addr - m_addr;
2773 sec = obj_find_section(f, ".data.init");
2775 if (!module->runsize ||
2776 module->runsize > sec->header.sh_addr - m_addr)
2777 module->runsize = sec->header.sh_addr - m_addr;
2779 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2780 if (sec && sec->header.sh_size) {
2781 module->archdata_start = (void*)sec->header.sh_addr;
2782 module->archdata_end = module->archdata_start + sec->header.sh_size;
2784 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2785 if (sec && sec->header.sh_size) {
2786 module->kallsyms_start = (void*)sec->header.sh_addr;
2787 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2790 if (!arch_init_module(f, module))
2793 /* Whew! All of the initialization is complete. Collect the final
2794 module image and give it to the kernel. */
2796 image = xmalloc(m_size);
2797 obj_create_image(f, image);
2799 ret = new_sys_init_module(m_name, (struct new_module *) image);
2801 perror_msg("init_module: %s", m_name);
2810 #define new_init_module(x, y, z) TRUE
2811 #define new_create_this_module(x, y) 0
2812 #define new_create_module_ksymtab(x)
2813 #define query_module(v, w, x, y, z) -1
2815 #endif /* CONFIG_FEATURE_NEW_MODULE_INTERFACE */
2818 /*======================================================================*/
2821 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2824 struct obj_string_patch *p;
2825 struct obj_section *strsec;
2826 size_t len = strlen(string) + 1;
2829 p = xmalloc(sizeof(*p));
2830 p->next = f->string_patches;
2831 p->reloc_secidx = secidx;
2832 p->reloc_offset = offset;
2833 f->string_patches = p;
2835 strsec = obj_find_section(f, ".kstrtab");
2836 if (strsec == NULL) {
2837 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2838 p->string_offset = 0;
2839 loc = strsec->contents;
2841 p->string_offset = strsec->header.sh_size;
2842 loc = obj_extend_section(strsec, len);
2844 memcpy(loc, string, len);
2849 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2851 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2852 struct obj_symbol *sym)
2854 struct obj_symbol_patch *p;
2856 p = xmalloc(sizeof(*p));
2857 p->next = f->symbol_patches;
2858 p->reloc_secidx = secidx;
2859 p->reloc_offset = offset;
2861 f->symbol_patches = p;
2867 static int obj_check_undefineds(struct obj_file *f)
2872 for (i = 0; i < HASH_BUCKETS; ++i) {
2873 struct obj_symbol *sym;
2874 for (sym = f->symtab[i]; sym; sym = sym->next)
2875 if (sym->secidx == SHN_UNDEF) {
2876 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2877 sym->secidx = SHN_ABS;
2880 error_msg("unresolved symbol %s", sym->name);
2889 static void obj_allocate_commons(struct obj_file *f)
2891 struct common_entry {
2892 struct common_entry *next;
2893 struct obj_symbol *sym;
2894 } *common_head = NULL;
2898 for (i = 0; i < HASH_BUCKETS; ++i) {
2899 struct obj_symbol *sym;
2900 for (sym = f->symtab[i]; sym; sym = sym->next)
2901 if (sym->secidx == SHN_COMMON) {
2902 /* Collect all COMMON symbols and sort them by size so as to
2903 minimize space wasted by alignment requirements. */
2905 struct common_entry **p, *n;
2906 for (p = &common_head; *p; p = &(*p)->next)
2907 if (sym->size <= (*p)->sym->size)
2910 n = alloca(sizeof(*n));
2918 for (i = 1; i < f->local_symtab_size; ++i) {
2919 struct obj_symbol *sym = f->local_symtab[i];
2920 if (sym && sym->secidx == SHN_COMMON) {
2921 struct common_entry **p, *n;
2922 for (p = &common_head; *p; p = &(*p)->next)
2923 if (sym == (*p)->sym)
2925 else if (sym->size < (*p)->sym->size) {
2926 n = alloca(sizeof(*n));
2936 /* Find the bss section. */
2937 for (i = 0; i < f->header.e_shnum; ++i)
2938 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2941 /* If for some reason there hadn't been one, create one. */
2942 if (i == f->header.e_shnum) {
2943 struct obj_section *sec;
2945 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2946 f->sections[i] = sec = arch_new_section();
2947 f->header.e_shnum = i + 1;
2949 memset(sec, 0, sizeof(*sec));
2950 sec->header.sh_type = SHT_PROGBITS;
2951 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2956 /* Allocate the COMMONS. */
2958 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2959 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2960 struct common_entry *c;
2962 for (c = common_head; c; c = c->next) {
2963 ElfW(Addr) align = c->sym->value;
2965 if (align > max_align)
2967 if (bss_size & (align - 1))
2968 bss_size = (bss_size | (align - 1)) + 1;
2971 c->sym->value = bss_size;
2973 bss_size += c->sym->size;
2976 f->sections[i]->header.sh_size = bss_size;
2977 f->sections[i]->header.sh_addralign = max_align;
2981 /* For the sake of patch relocation and parameter initialization,
2982 allocate zeroed data for NOBITS sections now. Note that after
2983 this we cannot assume NOBITS are really empty. */
2984 for (i = 0; i < f->header.e_shnum; ++i) {
2985 struct obj_section *s = f->sections[i];
2986 if (s->header.sh_type == SHT_NOBITS) {
2987 if (s->header.sh_size != 0)
2988 s->contents = memset(xmalloc(s->header.sh_size),
2989 0, s->header.sh_size);
2993 s->header.sh_type = SHT_PROGBITS;
2998 static unsigned long obj_load_size(struct obj_file *f)
3000 unsigned long dot = 0;
3001 struct obj_section *sec;
3003 /* Finalize the positions of the sections relative to one another. */
3005 for (sec = f->load_order; sec; sec = sec->load_next) {
3008 align = sec->header.sh_addralign;
3009 if (align && (dot & (align - 1)))
3010 dot = (dot | (align - 1)) + 1;
3012 sec->header.sh_addr = dot;
3013 dot += sec->header.sh_size;
3019 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3021 int i, n = f->header.e_shnum;
3024 /* Finalize the addresses of the sections. */
3027 for (i = 0; i < n; ++i)
3028 f->sections[i]->header.sh_addr += base;
3030 /* And iterate over all of the relocations. */
3032 for (i = 0; i < n; ++i) {
3033 struct obj_section *relsec, *symsec, *targsec, *strsec;
3034 ElfW(RelM) * rel, *relend;
3038 relsec = f->sections[i];
3039 if (relsec->header.sh_type != SHT_RELM)
3042 symsec = f->sections[relsec->header.sh_link];
3043 targsec = f->sections[relsec->header.sh_info];
3044 strsec = f->sections[symsec->header.sh_link];
3046 rel = (ElfW(RelM) *) relsec->contents;
3047 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
3048 symtab = (ElfW(Sym) *) symsec->contents;
3049 strtab = (const char *) strsec->contents;
3051 for (; rel < relend; ++rel) {
3052 ElfW(Addr) value = 0;
3053 struct obj_symbol *intsym = NULL;
3054 unsigned long symndx;
3055 ElfW(Sym) * extsym = 0;
3058 /* Attempt to find a value to use for this relocation. */
3060 symndx = ELFW(R_SYM) (rel->r_info);
3062 /* Note we've already checked for undefined symbols. */
3064 extsym = &symtab[symndx];
3065 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
3066 /* Local symbols we look up in the local table to be sure
3067 we get the one that is really intended. */
3068 intsym = f->local_symtab[symndx];
3070 /* Others we look up in the hash table. */
3072 if (extsym->st_name)
3073 name = strtab + extsym->st_name;
3075 name = f->sections[extsym->st_shndx]->name;
3076 intsym = obj_find_symbol(f, name);
3079 value = obj_symbol_final_value(f, intsym);
3080 intsym->referenced = 1;
3082 #if SHT_RELM == SHT_RELA
3083 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3084 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3085 if (!extsym || !extsym->st_name ||
3086 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
3088 value += rel->r_addend;
3092 switch (arch_apply_relocation
3093 (f, targsec, symsec, intsym, rel, value)) {
3097 case obj_reloc_overflow:
3098 errmsg = "Relocation overflow";
3100 case obj_reloc_dangerous:
3101 errmsg = "Dangerous relocation";
3103 case obj_reloc_unhandled:
3104 errmsg = "Unhandled relocation";
3107 error_msg("%s of type %ld for %s", errmsg,
3108 (long) ELFW(R_TYPE) (rel->r_info),
3109 strtab + extsym->st_name);
3111 error_msg("%s of type %ld", errmsg,
3112 (long) ELFW(R_TYPE) (rel->r_info));
3120 /* Finally, take care of the patches. */
3122 if (f->string_patches) {
3123 struct obj_string_patch *p;
3124 struct obj_section *strsec;
3125 ElfW(Addr) strsec_base;
3126 strsec = obj_find_section(f, ".kstrtab");
3127 strsec_base = strsec->header.sh_addr;
3129 for (p = f->string_patches; p; p = p->next) {
3130 struct obj_section *targsec = f->sections[p->reloc_secidx];
3131 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3132 = strsec_base + p->string_offset;
3136 if (f->symbol_patches) {
3137 struct obj_symbol_patch *p;
3139 for (p = f->symbol_patches; p; p = p->next) {
3140 struct obj_section *targsec = f->sections[p->reloc_secidx];
3141 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3142 = obj_symbol_final_value(f, p->sym);
3149 static int obj_create_image(struct obj_file *f, char *image)
3151 struct obj_section *sec;
3152 ElfW(Addr) base = f->baseaddr;
3154 for (sec = f->load_order; sec; sec = sec->load_next) {
3157 if (sec->contents == 0 || sec->header.sh_size == 0)
3160 secimg = image + (sec->header.sh_addr - base);
3162 /* Note that we allocated data for NOBITS sections earlier. */
3163 memcpy(secimg, sec->contents, sec->header.sh_size);
3169 /*======================================================================*/
3171 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3174 ElfW(Shdr) * section_headers;
3178 /* Read the file header. */
3180 f = arch_new_file();
3181 memset(f, 0, sizeof(*f));
3182 f->symbol_cmp = strcmp;
3183 f->symbol_hash = obj_elf_hash;
3184 f->load_order_search_start = &f->load_order;
3186 fseek(fp, 0, SEEK_SET);
3187 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3188 perror_msg("error reading ELF header");
3192 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3193 || f->header.e_ident[EI_MAG1] != ELFMAG1
3194 || f->header.e_ident[EI_MAG2] != ELFMAG2
3195 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3196 error_msg("not an ELF file");
3199 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3200 || f->header.e_ident[EI_DATA] != ELFDATAM
3201 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3202 || !MATCH_MACHINE(f->header.e_machine)) {
3203 error_msg("ELF file not for this architecture");
3206 if (f->header.e_type != ET_REL) {
3207 error_msg("ELF file not a relocatable object");
3211 /* Read the section headers. */
3213 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3214 error_msg("section header size mismatch: %lu != %lu",
3215 (unsigned long) f->header.e_shentsize,
3216 (unsigned long) sizeof(ElfW(Shdr)));
3220 shnum = f->header.e_shnum;
3221 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3222 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3224 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3225 fseek(fp, f->header.e_shoff, SEEK_SET);
3226 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3227 perror_msg("error reading ELF section headers");
3231 /* Read the section data. */
3233 for (i = 0; i < shnum; ++i) {
3234 struct obj_section *sec;
3236 f->sections[i] = sec = arch_new_section();
3237 memset(sec, 0, sizeof(*sec));
3239 sec->header = section_headers[i];
3242 if(sec->header.sh_size) switch (sec->header.sh_type) {
3251 if (!loadprogbits) {
3252 sec->contents = NULL;
3259 if (sec->header.sh_size > 0) {
3260 sec->contents = xmalloc(sec->header.sh_size);
3261 fseek(fp, sec->header.sh_offset, SEEK_SET);
3262 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3263 perror_msg("error reading ELF section data");
3267 sec->contents = NULL;
3271 #if SHT_RELM == SHT_REL
3273 error_msg("RELA relocations not supported on this architecture");
3277 error_msg("REL relocations not supported on this architecture");
3282 if (sec->header.sh_type >= SHT_LOPROC) {
3283 /* Assume processor specific section types are debug
3284 info and can safely be ignored. If this is ever not
3285 the case (Hello MIPS?), don't put ifdefs here but
3286 create an arch_load_proc_section(). */
3290 error_msg("can't handle sections of type %ld",
3291 (long) sec->header.sh_type);
3296 /* Do what sort of interpretation as needed by each section. */
3298 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3300 for (i = 0; i < shnum; ++i) {
3301 struct obj_section *sec = f->sections[i];
3302 sec->name = shstrtab + sec->header.sh_name;
3305 for (i = 0; i < shnum; ++i) {
3306 struct obj_section *sec = f->sections[i];
3308 /* .modinfo should be contents only but gcc has no attribute for that.
3309 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3311 if (strcmp(sec->name, ".modinfo") == 0)
3312 sec->header.sh_flags &= ~SHF_ALLOC;
3314 if (sec->header.sh_flags & SHF_ALLOC)
3315 obj_insert_section_load_order(f, sec);
3317 switch (sec->header.sh_type) {
3320 unsigned long nsym, j;
3324 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3325 error_msg("symbol size mismatch: %lu != %lu",
3326 (unsigned long) sec->header.sh_entsize,
3327 (unsigned long) sizeof(ElfW(Sym)));
3331 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3332 strtab = f->sections[sec->header.sh_link]->contents;
3333 sym = (ElfW(Sym) *) sec->contents;
3335 /* Allocate space for a table of local symbols. */
3336 j = f->local_symtab_size = sec->header.sh_info;
3337 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3339 /* Insert all symbols into the hash table. */
3340 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3343 name = strtab + sym->st_name;
3345 name = f->sections[sym->st_shndx]->name;
3347 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3348 sym->st_value, sym->st_size);
3354 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3355 error_msg("relocation entry size mismatch: %lu != %lu",
3356 (unsigned long) sec->header.sh_entsize,
3357 (unsigned long) sizeof(ElfW(RelM)));
3361 /* XXX Relocation code from modutils-2.3.19 is not here.
3362 * Why? That's about 20 lines of code from obj/obj_load.c,
3363 * which gets done in a second pass through the sections.
3364 * This BusyBox insmod does similar work in obj_relocate(). */
3371 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3373 * load the unloaded sections directly into the memory allocated by
3374 * kernel for the module
3377 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3379 ElfW(Addr) base = f->baseaddr;
3380 struct obj_section* sec;
3382 for (sec = f->load_order; sec; sec = sec->load_next) {
3384 /* section already loaded? */
3385 if (sec->contents != NULL)
3388 if (sec->header.sh_size == 0)
3391 sec->contents = imagebase + (sec->header.sh_addr - base);
3392 fseek(fp, sec->header.sh_offset, SEEK_SET);
3393 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3394 error_msg("error reading ELF section data: %s\n", strerror(errno));
3403 static void hide_special_symbols(struct obj_file *f)
3405 static const char *const specials[] = {
3406 SPFX "cleanup_module",
3408 SPFX "kernel_version",
3412 struct obj_symbol *sym;
3413 const char *const *p;
3415 for (p = specials; *p; ++p)
3416 if ((sym = obj_find_symbol(f, *p)) != NULL)
3418 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3423 extern int insmod_main( int argc, char **argv)
3430 unsigned long m_size;
3435 char m_name[FILENAME_MAX + 1] = "\0";
3436 int exit_status = EXIT_FAILURE;
3438 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3439 struct utsname uts_info;
3440 char m_strversion[STRVERSIONLEN];
3445 /* Parse any options */
3446 while ((opt = getopt(argc, argv, "fksvxLo:")) > 0) {
3448 case 'f': /* force loading */
3449 flag_force_load = 1;
3451 case 'k': /* module loaded by kerneld, auto-cleanable */
3454 case 's': /* log to syslog */
3455 /* log to syslog -- not supported */
3456 /* but kernel needs this for request_module(), */
3457 /* as this calls: modprobe -k -s -- <module> */
3458 /* so silently ignore this flag */
3460 case 'v': /* verbose output */
3463 case 'x': /* do not export externs */
3466 case 'o': /* name the output module */
3467 safe_strncpy(m_name, optarg, sizeof(m_name));
3469 case 'L': /* Stub warning */
3470 /* This is needed for compatibility with modprobe.
3471 * In theory, this does locking, but we don't do
3472 * that. So be careful and plan your life around not
3473 * loading the same module 50 times concurrently. */
3480 if (argv[optind] == NULL) {
3484 /* Grab the module name */
3485 tmp1 = xstrdup(argv[optind]);
3486 tmp = basename(tmp1);
3489 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3493 if (len >= sizeof(m_fullName)) {
3494 len = sizeof(m_fullName);
3496 safe_strncpy(m_fullName, tmp, len);
3499 if (*m_name == '\0') {
3500 safe_strncpy(m_name, m_fullName, sizeof(m_name));
3502 len = strlen(m_fullName);
3503 if (len > (sizeof(m_fullName)-3))
3504 error_msg_and_die("%s: no module by that name found", m_fullName);
3505 strcat(m_fullName, ".o");
3507 /* Get a filedesc for the module. Check we we have a complete path */
3508 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3509 (fp = fopen(argv[optind], "r")) == NULL) {
3510 struct utsname myuname;
3512 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3513 * but do not error out yet if we fail to find it... */
3514 if (uname(&myuname) == 0) {
3515 char module_dir[FILENAME_MAX];
3516 char real_module_dir[FILENAME_MAX];
3517 snprintf (module_dir, sizeof(module_dir), "%s/%s",
3518 _PATH_MODULES, myuname.release);
3519 /* Jump through hoops in case /lib/modules/`uname -r`
3520 * is a symlink. We do not want recursive_action to
3521 * follow symlinks, but we do want to follow the
3522 * /lib/modules/`uname -r` dir, So resolve it ourselves
3523 * if it is a link... */
3524 if (realpath (module_dir, real_module_dir) == NULL)
3525 strcpy(real_module_dir, module_dir);
3526 recursive_action(real_module_dir, TRUE, FALSE, FALSE,
3527 check_module_name_match, 0, m_fullName);
3530 /* Check if we have found anything yet */
3531 if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL))
3533 char module_dir[FILENAME_MAX];
3534 if (realpath (_PATH_MODULES, module_dir) == NULL)
3535 strcpy(module_dir, _PATH_MODULES);
3536 /* No module found under /lib/modules/`uname -r`, this
3537 * time cast the net a bit wider. Search /lib/modules/ */
3538 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3539 check_module_name_match, 0, m_fullName))
3541 if (m_filename[0] == '\0'
3542 || ((fp = fopen(m_filename, "r")) == NULL))
3544 error_msg("%s: no module by that name found", m_fullName);
3545 return EXIT_FAILURE;
3548 error_msg_and_die("%s: no module by that name found", m_fullName);
3551 safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
3553 printf("Using %s\n", m_filename);
3555 if ((f = obj_load(fp, LOADBITS)) == NULL)
3556 perror_msg_and_die("Could not load the module");
3558 if (get_modinfo_value(f, "kernel_version") == NULL)
3563 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3564 /* Version correspondence? */
3566 if (uname(&uts_info) < 0)
3567 uts_info.release[0] = '\0';
3568 if (m_has_modinfo) {
3569 m_version = new_get_module_version(f, m_strversion);
3571 m_version = old_get_module_version(f, m_strversion);
3572 if (m_version == -1) {
3573 error_msg("couldn't find the kernel version the module was "
3579 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3580 if (flag_force_load) {
3581 error_msg("Warning: kernel-module version mismatch\n"
3582 "\t%s was compiled for kernel version %s\n"
3583 "\twhile this kernel is version %s",
3584 m_filename, m_strversion, uts_info.release);
3586 error_msg("kernel-module version mismatch\n"
3587 "\t%s was compiled for kernel version %s\n"
3588 "\twhile this kernel is version %s.",
3589 m_filename, m_strversion, uts_info.release);
3594 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3596 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3598 if (k_new_syscalls) {
3599 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
3600 if (!new_get_kernel_symbols())
3602 k_crcs = new_is_kernel_checksummed();
3604 error_msg("Not configured to support new kernels");
3608 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
3609 if (!old_get_kernel_symbols(m_name))
3611 k_crcs = old_is_kernel_checksummed();
3613 error_msg("Not configured to support old kernels");
3618 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3620 m_crcs = new_is_module_checksummed(f);
3622 m_crcs = old_is_module_checksummed(f);
3624 if (m_crcs != k_crcs)
3625 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3626 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3628 /* Let the module know about the kernel symbols. */
3629 add_kernel_symbols(f);
3631 /* Allocate common symbols, symbol tables, and string tables. */
3634 ? !new_create_this_module(f, m_name)
3635 : !old_create_mod_use_count(f))
3640 if (!obj_check_undefineds(f)) {
3643 obj_allocate_commons(f);
3645 /* done with the module name, on to the optional var=value arguments */
3648 if (optind < argc) {
3650 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3651 : !old_process_module_arguments(f, argc - optind, argv + optind))
3658 hide_special_symbols(f);
3661 new_create_module_ksymtab(f);
3663 /* Find current size of the module */
3664 m_size = obj_load_size(f);
3667 m_addr = create_module(m_name, m_size);
3668 if (m_addr==-1) switch (errno) {
3670 error_msg("A module named %s already exists", m_name);
3673 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3677 perror_msg("create_module: %s", m_name);
3683 * the PROGBITS section was not loaded by the obj_load
3684 * now we can load them directly into the kernel memory
3686 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3687 delete_module(m_name);
3692 if (!obj_relocate(f, m_addr)) {
3693 delete_module(m_name);
3698 ? !new_init_module(m_name, f, m_size)
3699 : !old_init_module(m_name, f, m_size))
3701 delete_module(m_name);
3705 exit_status = EXIT_SUCCESS;
3709 return(exit_status);