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 by Erik Andersen <andersee@debian.org>
10 * and Ron Alder <alder@lineo.com>
12 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
13 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
15 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
16 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
17 * very minor changes required to also work with StrongArm and presumably
18 * all ARM based systems.
20 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
21 * PowerPC specific code stolen from modutils-2.3.16,
22 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
23 * I've only tested the code on mpc8xx platforms in big-endian mode.
24 * Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
26 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
27 * based on modutils-2.4.2
28 * MIPS specific support for Elf loading and relocation.
29 * Copyright 1996, 1997 Linux International.
30 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
32 * Based almost entirely on the Linux modutils-2.3.11 implementation.
33 * Copyright 1996, 1997 Linux International.
34 * New implementation contributed by Richard Henderson <rth@tamu.edu>
35 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
36 * Restructured (and partly rewritten) by:
37 * Björn Ekwall <bj0rn@blox.se> February 1999
39 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47 * General Public License for more details.
49 * You should have received a copy of the GNU General Public License
50 * along with this program; if not, write to the Free Software
51 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
65 #include <sys/utsname.h>
68 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
69 # undef CONFIG_FEATURE_OLD_MODULE_INTERFACE
70 # define new_sys_init_module init_module
72 # define old_sys_init_module init_module
75 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
81 #if defined(__powerpc__)
82 #define CONFIG_USE_PLT_ENTRIES
83 #define CONFIG_PLT_ENTRY_SIZE 16
87 #define CONFIG_USE_PLT_ENTRIES
88 #define CONFIG_PLT_ENTRY_SIZE 8
89 #define CONFIG_USE_GOT_ENTRIES
90 #define CONFIG_GOT_ENTRY_SIZE 8
94 #define CONFIG_USE_GOT_ENTRIES
95 #define CONFIG_GOT_ENTRY_SIZE 4
99 #define CONFIG_USE_GOT_ENTRIES
100 #define CONFIG_GOT_ENTRY_SIZE 4
103 #if defined(__mips__)
107 //----------------------------------------------------------------------------
108 //--------modutils module.h, lines 45-242
109 //----------------------------------------------------------------------------
111 /* Definitions for the Linux module syscall interface.
112 Copyright 1996, 1997 Linux International.
114 Contributed by Richard Henderson <rth@tamu.edu>
116 This file is part of the Linux modutils.
118 This program is free software; you can redistribute it and/or modify it
119 under the terms of the GNU General Public License as published by the
120 Free Software Foundation; either version 2 of the License, or (at your
121 option) any later version.
123 This program is distributed in the hope that it will be useful, but
124 WITHOUT ANY WARRANTY; without even the implied warranty of
125 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
126 General Public License for more details.
128 You should have received a copy of the GNU General Public License
129 along with this program; if not, write to the Free Software Foundation,
130 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
133 #ifndef MODUTILS_MODULE_H
134 static const int MODUTILS_MODULE_H = 1;
136 #ident "$Id: insmod.c,v 1.77 2001/12/20 23:13:08 kraai Exp $"
138 /* This file contains the structures used by the 2.0 and 2.1 kernels.
139 We do not use the kernel headers directly because we do not wish
140 to be dependant on a particular kernel version to compile insmod. */
143 /*======================================================================*/
144 /* The structures used by Linux 2.0. */
146 /* The symbol format used by get_kernel_syms(2). */
147 struct old_kernel_sym
153 struct old_module_ref
155 unsigned long module; /* kernel addresses */
159 struct old_module_symbol
165 struct old_symbol_table
167 int size; /* total, including string table!!! */
170 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
171 struct old_module_ref ref[0]; /* actual size defined by n_refs */
174 struct old_mod_routines
177 unsigned long cleanup;
183 unsigned long ref; /* the list of modules that refer to me */
184 unsigned long symtab;
186 int size; /* size of module in pages */
187 unsigned long addr; /* address of module */
189 unsigned long cleanup; /* cleanup routine */
192 /* Sent to init_module(2) or'ed into the code size parameter. */
193 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
195 int get_kernel_syms(struct old_kernel_sym *);
196 int old_sys_init_module(const char *name, char *code, unsigned codesize,
197 struct old_mod_routines *, struct old_symbol_table *);
199 /*======================================================================*/
200 /* For sizeof() which are related to the module platform and not to the
201 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
203 #define tgt_sizeof_char sizeof(char)
204 #define tgt_sizeof_short sizeof(short)
205 #define tgt_sizeof_int sizeof(int)
206 #define tgt_sizeof_long sizeof(long)
207 #define tgt_sizeof_char_p sizeof(char *)
208 #define tgt_sizeof_void_p sizeof(void *)
209 #define tgt_long long
211 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
212 #undef tgt_sizeof_long
213 #undef tgt_sizeof_char_p
214 #undef tgt_sizeof_void_p
216 static const int tgt_sizeof_long = 8;
217 static const int tgt_sizeof_char_p = 8;
218 static const int tgt_sizeof_void_p = 8;
219 #define tgt_long long long
222 /*======================================================================*/
223 /* The structures used in Linux 2.1. */
225 /* Note: new_module_symbol does not use tgt_long intentionally */
226 struct new_module_symbol
232 struct new_module_persist;
234 struct new_module_ref
236 unsigned tgt_long dep; /* kernel addresses */
237 unsigned tgt_long ref;
238 unsigned tgt_long next_ref;
243 unsigned tgt_long size_of_struct; /* == sizeof(module) */
244 unsigned tgt_long next;
245 unsigned tgt_long name;
246 unsigned tgt_long size;
249 unsigned tgt_long flags; /* AUTOCLEAN et al */
254 unsigned tgt_long syms;
255 unsigned tgt_long deps;
256 unsigned tgt_long refs;
257 unsigned tgt_long init;
258 unsigned tgt_long cleanup;
259 unsigned tgt_long ex_table_start;
260 unsigned tgt_long ex_table_end;
262 unsigned tgt_long gp;
264 /* Everything after here is extension. */
265 unsigned tgt_long persist_start;
266 unsigned tgt_long persist_end;
267 unsigned tgt_long can_unload;
268 unsigned tgt_long runsize;
269 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
270 const char *kallsyms_start; /* All symbols for kernel debugging */
271 const char *kallsyms_end;
272 const char *archdata_start; /* arch specific data for module */
273 const char *archdata_end;
274 const char *kernel_data; /* Reserved for kernel internal use */
278 #define ARCHDATA_SEC_NAME "__archdata"
279 #define KALLSYMS_SEC_NAME "__kallsyms"
282 struct new_module_info
290 /* Bits of module.flags. */
291 static const int NEW_MOD_RUNNING = 1;
292 static const int NEW_MOD_DELETED = 2;
293 static const int NEW_MOD_AUTOCLEAN = 4;
294 static const int NEW_MOD_VISITED = 8;
295 static const int NEW_MOD_USED_ONCE = 16;
297 int new_sys_init_module(const char *name, const struct new_module *);
298 int query_module(const char *name, int which, void *buf, size_t bufsize,
301 /* Values for query_module's which. */
303 static const int QM_MODULES = 1;
304 static const int QM_DEPS = 2;
305 static const int QM_REFS = 3;
306 static const int QM_SYMBOLS = 4;
307 static const int QM_INFO = 5;
309 /*======================================================================*/
310 /* The system calls unchanged between 2.0 and 2.1. */
312 unsigned long create_module(const char *, size_t);
313 int delete_module(const char *);
316 #endif /* module.h */
318 //----------------------------------------------------------------------------
319 //--------end of modutils module.h
320 //----------------------------------------------------------------------------
324 //----------------------------------------------------------------------------
325 //--------modutils obj.h, lines 253-462
326 //----------------------------------------------------------------------------
328 /* Elf object file loading and relocation routines.
329 Copyright 1996, 1997 Linux International.
331 Contributed by Richard Henderson <rth@tamu.edu>
333 This file is part of the Linux modutils.
335 This program is free software; you can redistribute it and/or modify it
336 under the terms of the GNU General Public License as published by the
337 Free Software Foundation; either version 2 of the License, or (at your
338 option) any later version.
340 This program is distributed in the hope that it will be useful, but
341 WITHOUT ANY WARRANTY; without even the implied warranty of
342 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
343 General Public License for more details.
345 You should have received a copy of the GNU General Public License
346 along with this program; if not, write to the Free Software Foundation,
347 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
350 #ifndef MODUTILS_OBJ_H
351 static const int MODUTILS_OBJ_H = 1;
353 #ident "$Id: insmod.c,v 1.77 2001/12/20 23:13:08 kraai Exp $"
355 /* The relocatable object is manipulated using elfin types. */
361 /* Machine-specific elf macros for i386 et al. */
363 /* the SH changes have only been tested on the SH4 in =little endian= mode */
364 /* I'm not sure about big endian, so let's warn: */
366 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
367 #error insmod.c may require changes for use on big endian SH4/SH3
370 /* it may or may not work on the SH1/SH2... So let's error on those
372 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
373 #error insmod.c may require changes for non-SH3/SH4 use
376 #define ELFCLASSM ELFCLASS32
378 #if (defined(__mc68000__))
379 #define ELFDATAM ELFDATA2MSB
386 #define MATCH_MACHINE(x) (x == EM_SH)
387 #define SHT_RELM SHT_RELA
388 #define Elf32_RelM Elf32_Rela
389 #define ELFDATAM ELFDATA2LSB
391 #elif defined(__arm__)
393 #define MATCH_MACHINE(x) (x == EM_ARM)
394 #define SHT_RELM SHT_REL
395 #define Elf32_RelM Elf32_Rel
397 #define ELFDATAM ELFDATA2MSB
400 #define ELFDATAM ELFDATA2LSB
403 #elif defined(__powerpc__)
405 #define MATCH_MACHINE(x) (x == EM_PPC)
406 #define SHT_RELM SHT_RELA
407 #define Elf32_RelM Elf32_Rela
408 #define ELFDATAM ELFDATA2MSB
410 #elif defined(__mips__)
412 /* Account for ELF spec changes. */
413 #ifndef EM_MIPS_RS3_LE
414 #ifdef EM_MIPS_RS4_BE
415 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
417 #define EM_MIPS_RS3_LE 10
419 #endif /* !EM_MIPS_RS3_LE */
421 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
422 #define SHT_RELM SHT_REL
423 #define Elf32_RelM Elf32_Rel
425 #define ELFDATAM ELFDATA2MSB
428 #define ELFDATAM ELFDATA2LSB
431 #elif defined(__i386__)
433 /* presumably we can use these for anything but the SH and ARM*/
434 /* this is the previous behavior, but it does result in
435 insmod.c being broken on anything except i386 */
437 #define MATCH_MACHINE(x) (x == EM_386)
439 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
442 #define SHT_RELM SHT_REL
443 #define Elf32_RelM Elf32_Rel
444 #define ELFDATAM ELFDATA2LSB
446 #elif defined(__mc68000__)
448 #define MATCH_MACHINE(x) (x == EM_68K)
449 #define SHT_RELM SHT_RELA
450 #define Elf32_RelM Elf32_Rela
453 #error Sorry, but insmod.c does not yet support this architecture...
457 # if ELFCLASSM == ELFCLASS32
458 # define ElfW(x) Elf32_ ## x
459 # define ELFW(x) ELF32_ ## x
461 # define ElfW(x) Elf64_ ## x
462 # define ELFW(x) ELF64_ ## x
466 /* For some reason this is missing from libc5. */
467 #ifndef ELF32_ST_INFO
468 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
471 #ifndef ELF64_ST_INFO
472 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
475 struct obj_string_patch;
476 struct obj_symbol_patch;
483 struct obj_section *load_next;
489 struct obj_symbol *next; /* hash table link */
493 int secidx; /* the defining section index/module */
495 int ksymidx; /* for export to the kernel symtab */
496 int referenced; /* actually used in the link */
499 /* Hardcode the hash table size. We shouldn't be needing so many
500 symbols that we begin to degrade performance, and we get a big win
501 by giving the compiler a constant divisor. */
503 #define HASH_BUCKETS 521
509 struct obj_section **sections;
510 struct obj_section *load_order;
511 struct obj_section **load_order_search_start;
512 struct obj_string_patch *string_patches;
513 struct obj_symbol_patch *symbol_patches;
514 int (*symbol_cmp)(const char *, const char *);
515 unsigned long (*symbol_hash)(const char *);
516 unsigned long local_symtab_size;
517 struct obj_symbol **local_symtab;
518 struct obj_symbol *symtab[HASH_BUCKETS];
529 struct obj_string_patch
531 struct obj_string_patch *next;
533 ElfW(Addr) reloc_offset;
534 ElfW(Addr) string_offset;
537 struct obj_symbol_patch
539 struct obj_symbol_patch *next;
541 ElfW(Addr) reloc_offset;
542 struct obj_symbol *sym;
546 /* Generic object manipulation routines. */
548 static unsigned long obj_elf_hash(const char *);
550 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
552 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
555 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
556 struct obj_symbol *sym);
558 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
559 static void obj_set_symbol_compare(struct obj_file *f,
560 int (*cmp)(const char *, const char *),
561 unsigned long (*hash)(const char *));
564 static struct obj_section *obj_find_section (struct obj_file *f,
567 static void obj_insert_section_load_order (struct obj_file *f,
568 struct obj_section *sec);
570 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
575 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
580 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
582 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
585 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
586 struct obj_symbol *sym);
588 static int obj_check_undefineds(struct obj_file *f);
590 static void obj_allocate_commons(struct obj_file *f);
592 static unsigned long obj_load_size (struct obj_file *f);
594 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
596 static struct obj_file *obj_load(FILE *f, int loadprogbits);
598 static int obj_create_image (struct obj_file *f, char *image);
600 /* Architecture specific manipulation routines. */
602 static struct obj_file *arch_new_file (void);
604 static struct obj_section *arch_new_section (void);
606 static struct obj_symbol *arch_new_symbol (void);
608 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
609 struct obj_section *targsec,
610 struct obj_section *symsec,
611 struct obj_symbol *sym,
612 ElfW(RelM) *rel, ElfW(Addr) value);
614 static int arch_create_got (struct obj_file *f);
616 static int arch_init_module (struct obj_file *f, struct new_module *);
619 //----------------------------------------------------------------------------
620 //--------end of modutils obj.h
621 //----------------------------------------------------------------------------
627 #define _PATH_MODULES "/lib/modules"
628 static const int STRVERSIONLEN = 32;
630 /*======================================================================*/
632 static int flag_force_load = 0;
633 static int flag_autoclean = 0;
634 static int flag_verbose = 0;
635 static int flag_export = 1;
638 /*======================================================================*/
640 /* previously, these were named i386_* but since we could be
641 compiling for the sh, I've renamed them to the more general
642 arch_* These structures are the same between the x86 and SH,
643 and we can't support anything else right now anyway. In the
644 future maybe they should be #if defined'd */
650 #if defined(CONFIG_USE_PLT_ENTRIES)
651 struct arch_plt_entry
655 int inited:1; /* has been set up */
659 #if defined(CONFIG_USE_GOT_ENTRIES)
660 struct arch_got_entry {
662 unsigned offset_done:1;
663 unsigned reloc_done:1;
667 #if defined(__mips__)
670 struct mips_hi16 *next;
677 struct obj_file root;
678 #if defined(CONFIG_USE_PLT_ENTRIES)
679 struct obj_section *plt;
681 #if defined(CONFIG_USE_GOT_ENTRIES)
682 struct obj_section *got;
684 #if defined(__mips__)
685 struct mips_hi16 *mips_hi16_list;
690 struct obj_symbol root;
691 #if defined(CONFIG_USE_PLT_ENTRIES)
692 struct arch_plt_entry pltent;
694 #if defined(CONFIG_USE_GOT_ENTRIES)
695 struct arch_got_entry gotent;
700 struct external_module {
705 struct new_module_symbol *syms;
708 static struct new_module_symbol *ksyms;
709 static size_t nksyms;
711 static struct external_module *ext_modules;
712 static int n_ext_modules;
713 static int n_ext_modules_used;
714 extern int delete_module(const char *);
716 static char m_filename[FILENAME_MAX + 1];
717 static char m_fullName[FILENAME_MAX + 1];
721 /*======================================================================*/
724 static int check_module_name_match(const char *filename, struct stat *statbuf,
727 char *fullname = (char *) userdata;
729 if (fullname[0] == '\0')
732 char *tmp, *tmp1 = xstrdup(filename);
733 tmp = get_last_path_component(tmp1);
734 if (strcmp(tmp, fullname) == 0) {
736 /* Stop searching if we find a match */
737 safe_strncpy(m_filename, filename, sizeof(m_filename));
746 /*======================================================================*/
748 static struct obj_file *arch_new_file(void)
751 f = xmalloc(sizeof(*f));
753 #if defined(CONFIG_USE_PLT_ENTRIES)
756 #if defined(CONFIG_USE_GOT_ENTRIES)
759 #if defined(__mips__)
760 f->mips_hi16_list = NULL;
766 static struct obj_section *arch_new_section(void)
768 return xmalloc(sizeof(struct obj_section));
771 static struct obj_symbol *arch_new_symbol(void)
773 struct arch_symbol *sym;
774 sym = xmalloc(sizeof(*sym));
776 #if defined(CONFIG_USE_PLT_ENTRIES)
777 memset(&sym->pltent, 0, sizeof(sym->pltent));
779 #if defined(CONFIG_USE_GOT_ENTRIES)
780 memset(&sym->gotent, 0, sizeof(sym->gotent));
786 static enum obj_reloc
787 arch_apply_relocation(struct obj_file *f,
788 struct obj_section *targsec,
789 struct obj_section *symsec,
790 struct obj_symbol *sym,
791 ElfW(RelM) *rel, ElfW(Addr) v)
793 struct arch_file *ifile = (struct arch_file *) f;
794 #if !(defined(__mips__))
795 struct arch_symbol *isym = (struct arch_symbol *) sym;
798 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
799 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
800 #if defined(CONFIG_USE_GOT_ENTRIES)
801 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
803 #if defined(CONFIG_USE_PLT_ENTRIES)
804 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
805 struct arch_plt_entry *pe;
808 enum obj_reloc ret = obj_reloc_ok;
810 switch (ELF32_R_TYPE(rel->r_info)) {
812 /* even though these constants seem to be the same for
813 the i386 and the sh, we "#if define" them for clarity
814 and in case that ever changes */
817 #elif defined(__arm__)
819 #elif defined(__i386__)
821 #elif defined(__mc68000__)
823 #elif defined(__powerpc__)
825 #elif defined(__mips__)
832 #elif defined(__arm__)
834 #elif defined(__i386__)
836 #elif defined(__mc68000__)
838 #elif defined(__powerpc__)
840 #elif defined(__mips__)
845 #if defined(__mc68000__)
848 ret = obj_reloc_overflow;
853 ret = obj_reloc_overflow;
856 #endif /* __mc68000__ */
858 #if defined(__powerpc__)
859 case R_PPC_ADDR16_HA:
860 *(unsigned short *)loc = (v + 0x8000) >> 16;
863 case R_PPC_ADDR16_HI:
864 *(unsigned short *)loc = v >> 16;
867 case R_PPC_ADDR16_LO:
868 *(unsigned short *)loc = v;
872 #if defined(__mips__)
875 ret = obj_reloc_dangerous;
876 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
877 ret = obj_reloc_overflow;
879 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
887 /* We cannot relocate this one now because we don't know the value
888 of the carry we need to add. Save the information, and let LO16
889 do the actual relocation. */
890 n = (struct mips_hi16 *) xmalloc(sizeof *n);
893 n->next = ifile->mips_hi16_list;
894 ifile->mips_hi16_list = n;
900 unsigned long insnlo = *loc;
901 Elf32_Addr val, vallo;
903 /* Sign extend the addend we extract from the lo insn. */
904 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
906 if (ifile->mips_hi16_list != NULL) {
909 l = ifile->mips_hi16_list;
911 struct mips_hi16 *next;
914 /* The value for the HI16 had best be the same. */
915 assert(v == l->value);
917 /* Do the HI16 relocation. Note that we actually don't
918 need to know anything about the LO16 itself, except where
919 to find the low 16 bits of the addend needed by the LO16. */
922 ((insn & 0xffff) << 16) +
926 /* Account for the sign extension that will happen in the
933 insn = (insn & ~0xffff) | val;
941 ifile->mips_hi16_list = NULL;
944 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
946 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
953 #elif defined(__sh__)
957 #elif defined(__i386__)
962 #elif defined(__mc68000__)
965 if ((Elf32_Sword)v > 0x7f || (Elf32_Sword)v < -(Elf32_Sword)0x80)
966 ret = obj_reloc_overflow;
971 if ((Elf32_Sword)v > 0x7fff || (Elf32_Sword)v < -(Elf32_Sword)0x8000)
972 ret = obj_reloc_overflow;
976 *(int *)loc = v - dot;
978 #elif defined(__powerpc__)
988 #elif defined(__i386__)
991 #if defined(CONFIG_USE_PLT_ENTRIES)
997 #if defined(__powerpc__)
1000 /* find the plt entry and initialize it if necessary */
1001 assert(isym != NULL);
1003 pe = (struct arch_plt_entry*) &isym->pltent;
1006 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1008 /* generate some machine code */
1010 #if defined(__arm__)
1011 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1012 ip[1] = v; /* sym@ */
1014 #if defined(__powerpc__)
1015 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1016 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1017 ip[2] = 0x7d6903a6; /* mtctr r11 */
1018 ip[3] = 0x4e800420; /* bctr */
1023 /* relative distance to target */
1025 /* if the target is too far away.... */
1026 if ((int)v < -0x02000000 || (int)v >= 0x02000000) {
1027 /* go via the plt */
1028 v = plt + pe->offset - dot;
1031 ret = obj_reloc_dangerous;
1033 /* merge the offset into the instruction. */
1034 #if defined(__arm__)
1035 /* Convert to words. */
1038 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1040 #if defined(__powerpc__)
1041 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1044 #endif /* CONFIG_USE_PLT_ENTRIES */
1046 #if defined(__arm__)
1047 #elif defined(__sh__)
1052 #elif defined(__i386__)
1053 case R_386_GLOB_DAT:
1054 case R_386_JMP_SLOT:
1057 #elif defined(__mc68000__)
1058 case R_68K_GLOB_DAT:
1059 case R_68K_JMP_SLOT:
1064 #if defined(__arm__)
1065 #elif defined(__sh__)
1067 *loc += f->baseaddr + rel->r_addend;
1069 #elif defined(__i386__)
1070 case R_386_RELATIVE:
1071 *loc += f->baseaddr;
1073 #elif defined(__mc68000__)
1074 case R_68K_RELATIVE:
1075 *(int *)loc += f->baseaddr;
1079 #if defined(CONFIG_USE_GOT_ENTRIES)
1081 #if !defined(__68k__)
1084 #elif defined(__arm__)
1086 #elif defined(__i386__)
1091 *loc += got - dot + rel->r_addend;;
1092 #elif defined(__i386__) || defined(__arm__) || defined(__m68k_)
1100 #elif defined(__arm__)
1102 #elif defined(__i386__)
1104 #elif defined(__mc68000__)
1107 assert(isym != NULL);
1108 /* needs an entry in the .got: set it, once */
1109 if (!isym->gotent.reloc_done) {
1110 isym->gotent.reloc_done = 1;
1111 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1113 /* make the reloc with_respect_to_.got */
1115 *loc += isym->gotent.offset + rel->r_addend;
1116 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1117 *loc += isym->gotent.offset;
1121 /* address relative to the got */
1122 #if !defined(__mc68000__)
1125 #elif defined(__arm__)
1127 #elif defined(__i386__)
1129 #elif defined(__mc68000__)
1135 #endif // __mc68000__
1137 #endif /* CONFIG_USE_GOT_ENTRIES */
1140 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1141 ret = obj_reloc_unhandled;
1148 static int arch_create_got(struct obj_file *f)
1150 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
1151 struct arch_file *ifile = (struct arch_file *) f;
1153 #if defined(CONFIG_USE_GOT_ENTRIES)
1154 int got_offset = 0, gotneeded = 0;
1156 #if defined(CONFIG_USE_PLT_ENTRIES)
1157 int plt_offset = 0, pltneeded = 0;
1159 struct obj_section *relsec, *symsec, *strsec;
1160 ElfW(RelM) *rel, *relend;
1161 ElfW(Sym) *symtab, *extsym;
1162 const char *strtab, *name;
1163 struct arch_symbol *intsym;
1165 for (i = 0; i < f->header.e_shnum; ++i) {
1166 relsec = f->sections[i];
1167 if (relsec->header.sh_type != SHT_RELM)
1170 symsec = f->sections[relsec->header.sh_link];
1171 strsec = f->sections[symsec->header.sh_link];
1173 rel = (ElfW(RelM) *) relsec->contents;
1174 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1175 symtab = (ElfW(Sym) *) symsec->contents;
1176 strtab = (const char *) strsec->contents;
1178 for (; rel < relend; ++rel) {
1179 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1181 switch (ELF32_R_TYPE(rel->r_info)) {
1182 #if defined(__arm__)
1185 #elif defined(__sh__)
1188 #elif defined(__i386__)
1191 #elif defined(__mc68000__)
1196 #if defined(__powerpc__)
1202 #if defined(__arm__)
1211 if (got_offset == 0)
1213 #elif defined(__sh__)
1217 #elif defined(__i386__)
1227 if (extsym->st_name != 0) {
1228 name = strtab + extsym->st_name;
1230 name = f->sections[extsym->st_shndx]->name;
1232 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1233 #if defined(CONFIG_USE_GOT_ENTRIES)
1234 if (!intsym->gotent.offset_done) {
1235 intsym->gotent.offset_done = 1;
1236 intsym->gotent.offset = got_offset;
1237 got_offset += CONFIG_GOT_ENTRY_SIZE;
1240 #if defined(CONFIG_USE_PLT_ENTRIES)
1241 if (pltneeded && intsym->pltent.allocated == 0) {
1242 intsym->pltent.allocated = 1;
1243 intsym->pltent.offset = plt_offset;
1244 plt_offset += CONFIG_PLT_ENTRY_SIZE;
1245 intsym->pltent.inited = 0;
1252 #if defined(CONFIG_USE_GOT_ENTRIES)
1254 struct obj_section* myrelsec = obj_find_section(f, ".got");
1257 obj_extend_section(myrelsec, got_offset);
1259 myrelsec = obj_create_alloced_section(f, ".got",
1260 CONFIG_GOT_ENTRY_SIZE,
1265 ifile->got = myrelsec;
1269 #if defined(CONFIG_USE_PLT_ENTRIES)
1271 ifile->plt = obj_create_alloced_section(f, ".plt",
1272 CONFIG_PLT_ENTRY_SIZE,
1279 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1285 /*======================================================================*/
1287 /* Standard ELF hash function. */
1288 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1290 unsigned long h = 0;
1297 if ((g = (h & 0xf0000000)) != 0) {
1306 static unsigned long obj_elf_hash(const char *name)
1308 return obj_elf_hash_n(name, strlen(name));
1311 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1312 /* String comparison for non-co-versioned kernel and module. */
1314 static int ncv_strcmp(const char *a, const char *b)
1316 size_t alen = strlen(a), blen = strlen(b);
1318 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1319 return strncmp(a, b, alen);
1320 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1321 return strncmp(a, b, blen);
1323 return strcmp(a, b);
1326 /* String hashing for non-co-versioned kernel and module. Here
1327 we are simply forced to drop the crc from the hash. */
1329 static unsigned long ncv_symbol_hash(const char *str)
1331 size_t len = strlen(str);
1332 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1334 return obj_elf_hash_n(str, len);
1338 obj_set_symbol_compare(struct obj_file *f,
1339 int (*cmp) (const char *, const char *),
1340 unsigned long (*hash) (const char *))
1343 f->symbol_cmp = cmp;
1345 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1348 f->symbol_hash = hash;
1350 memcpy(tmptab, f->symtab, sizeof(tmptab));
1351 memset(f->symtab, 0, sizeof(f->symtab));
1353 for (i = 0; i < HASH_BUCKETS; ++i)
1354 for (sym = tmptab[i]; sym; sym = next) {
1355 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1357 sym->next = f->symtab[h];
1363 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1365 static struct obj_symbol *
1366 obj_add_symbol(struct obj_file *f, const char *name,
1367 unsigned long symidx, int info,
1368 int secidx, ElfW(Addr) value,
1371 struct obj_symbol *sym;
1372 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1373 int n_type = ELFW(ST_TYPE) (info);
1374 int n_binding = ELFW(ST_BIND) (info);
1376 for (sym = f->symtab[hash]; sym; sym = sym->next)
1377 if (f->symbol_cmp(sym->name, name) == 0) {
1378 int o_secidx = sym->secidx;
1379 int o_info = sym->info;
1380 int o_type = ELFW(ST_TYPE) (o_info);
1381 int o_binding = ELFW(ST_BIND) (o_info);
1383 /* A redefinition! Is it legal? */
1385 if (secidx == SHN_UNDEF)
1387 else if (o_secidx == SHN_UNDEF)
1389 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1390 /* Cope with local and global symbols of the same name
1391 in the same object file, as might have been created
1392 by ld -r. The only reason locals are now seen at this
1393 level at all is so that we can do semi-sensible things
1396 struct obj_symbol *nsym, **p;
1398 nsym = arch_new_symbol();
1399 nsym->next = sym->next;
1402 /* Excise the old (local) symbol from the hash chain. */
1403 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1407 } else if (n_binding == STB_LOCAL) {
1408 /* Another symbol of the same name has already been defined.
1409 Just add this to the local table. */
1410 sym = arch_new_symbol();
1413 f->local_symtab[symidx] = sym;
1415 } else if (n_binding == STB_WEAK)
1417 else if (o_binding == STB_WEAK)
1419 /* Don't unify COMMON symbols with object types the programmer
1421 else if (secidx == SHN_COMMON
1422 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1424 else if (o_secidx == SHN_COMMON
1425 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1428 /* Don't report an error if the symbol is coming from
1429 the kernel or some external module. */
1430 if (secidx <= SHN_HIRESERVE)
1431 error_msg("%s multiply defined", name);
1436 /* Completely new symbol. */
1437 sym = arch_new_symbol();
1438 sym->next = f->symtab[hash];
1439 f->symtab[hash] = sym;
1442 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1443 if (symidx >= f->local_symtab_size)
1444 error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1445 name, (long) symidx, (long) f->local_symtab_size);
1447 f->local_symtab[symidx] = sym;
1454 sym->secidx = secidx;
1460 static struct obj_symbol *
1461 obj_find_symbol(struct obj_file *f, const char *name)
1463 struct obj_symbol *sym;
1464 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1466 for (sym = f->symtab[hash]; sym; sym = sym->next)
1467 if (f->symbol_cmp(sym->name, name) == 0)
1474 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1477 if (sym->secidx >= SHN_LORESERVE)
1480 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1482 /* As a special case, a NULL sym has value zero. */
1487 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1489 int i, n = f->header.e_shnum;
1491 for (i = 0; i < n; ++i)
1492 if (strcmp(f->sections[i]->name, name) == 0)
1493 return f->sections[i];
1498 static int obj_load_order_prio(struct obj_section *a)
1500 unsigned long af, ac;
1502 af = a->header.sh_flags;
1505 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1506 strcmp(a->name + 5, ".init"))
1510 if (!(af & SHF_WRITE))
1512 if (af & SHF_EXECINSTR)
1514 if (a->header.sh_type != SHT_NOBITS)
1521 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1523 struct obj_section **p;
1524 int prio = obj_load_order_prio(sec);
1525 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1526 if (obj_load_order_prio(*p) < prio)
1528 sec->load_next = *p;
1532 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1534 unsigned long align,
1537 int newidx = f->header.e_shnum++;
1538 struct obj_section *sec;
1540 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1541 f->sections[newidx] = sec = arch_new_section();
1543 memset(sec, 0, sizeof(*sec));
1544 sec->header.sh_type = SHT_PROGBITS;
1545 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1546 sec->header.sh_size = size;
1547 sec->header.sh_addralign = align;
1551 sec->contents = xmalloc(size);
1553 obj_insert_section_load_order(f, sec);
1558 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1560 unsigned long align,
1563 int newidx = f->header.e_shnum++;
1564 struct obj_section *sec;
1566 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1567 f->sections[newidx] = sec = arch_new_section();
1569 memset(sec, 0, sizeof(*sec));
1570 sec->header.sh_type = SHT_PROGBITS;
1571 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1572 sec->header.sh_size = size;
1573 sec->header.sh_addralign = align;
1577 sec->contents = xmalloc(size);
1579 sec->load_next = f->load_order;
1580 f->load_order = sec;
1581 if (f->load_order_search_start == &f->load_order)
1582 f->load_order_search_start = &sec->load_next;
1587 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1589 unsigned long oldsize = sec->header.sh_size;
1591 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1593 return sec->contents + oldsize;
1597 /* Conditionally add the symbols from the given symbol set to the
1603 int idx, struct new_module_symbol *syms, size_t nsyms)
1605 struct new_module_symbol *s;
1609 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1611 /* Only add symbols that are already marked external. If we
1612 override locals we may cause problems for argument initialization.
1613 We will also create a false dependency on the module. */
1614 struct obj_symbol *sym;
1616 sym = obj_find_symbol(f, (char *) s->name);
1617 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1618 sym = obj_add_symbol(f, (char *) s->name, -1,
1619 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1621 /* Did our symbol just get installed? If so, mark the
1622 module as "used". */
1623 if (sym->secidx == idx)
1631 static void add_kernel_symbols(struct obj_file *f)
1633 struct external_module *m;
1636 /* Add module symbols first. */
1638 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1640 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1641 m->nsyms)) m->used = 1, ++nused;
1643 n_ext_modules_used = nused;
1645 /* And finally the symbols from the kernel proper. */
1648 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1651 static char *get_modinfo_value(struct obj_file *f, const char *key)
1653 struct obj_section *sec;
1654 char *p, *v, *n, *ep;
1655 size_t klen = strlen(key);
1657 sec = obj_find_section(f, ".modinfo");
1661 ep = p + sec->header.sh_size;
1664 n = strchr(p, '\0');
1666 if (p + klen == v && strncmp(p, key, klen) == 0)
1669 if (p + klen == n && strcmp(p, key) == 0)
1679 /*======================================================================*/
1680 /* Functions relating to module loading in pre 2.1 kernels. */
1683 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1687 struct obj_symbol *sym;
1691 if ((q = strchr(p, '=')) == NULL) {
1697 sym = obj_find_symbol(f, p);
1699 /* Also check that the parameter was not resolved from the kernel. */
1700 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1701 error_msg("symbol for parameter %s not found", p);
1705 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1707 /* Do C quoting if we begin with a ". */
1711 str = alloca(strlen(q));
1712 for (r = str, q++; *q != '"'; ++q, ++r) {
1714 error_msg("improperly terminated string argument for %s", p);
1716 } else if (*q == '\\')
1750 if (q[1] >= '0' && q[1] <= '7') {
1751 c = (c * 8) + *++q - '0';
1752 if (q[1] >= '0' && q[1] <= '7')
1753 c = (c * 8) + *++q - '0';
1766 obj_string_patch(f, sym->secidx, sym->value, str);
1767 } else if (*q >= '0' && *q <= '9') {
1769 *loc++ = strtoul(q, &q, 0);
1770 while (*q++ == ',');
1772 char *contents = f->sections[sym->secidx]->contents;
1773 char *myloc = contents + sym->value;
1774 char *r; /* To search for commas */
1776 /* Break the string with comas */
1777 while ((r = strchr(q, ',')) != (char *) NULL) {
1779 obj_string_patch(f, sym->secidx, myloc - contents, q);
1780 myloc += sizeof(char *);
1785 obj_string_patch(f, sym->secidx, myloc - contents, q);
1794 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
1795 static int old_is_module_checksummed(struct obj_file *f)
1797 return obj_find_symbol(f, "Using_Versions") != NULL;
1799 /* Get the module's kernel version in the canonical integer form. */
1802 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1804 struct obj_symbol *sym;
1808 sym = obj_find_symbol(f, "kernel_version");
1812 p = f->sections[sym->secidx]->contents + sym->value;
1813 strncpy(str, p, STRVERSIONLEN);
1815 a = strtoul(p, &p, 10);
1818 b = strtoul(p + 1, &p, 10);
1821 c = strtoul(p + 1, &q, 10);
1825 return a << 16 | b << 8 | c;
1828 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
1830 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
1832 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1834 static int old_get_kernel_symbols(const char *m_name)
1836 struct old_kernel_sym *ks, *k;
1837 struct new_module_symbol *s;
1838 struct external_module *mod;
1839 int nks, nms, nmod, i;
1841 nks = get_kernel_syms(NULL);
1844 perror_msg("get_kernel_syms: %s", m_name);
1846 error_msg("No kernel symbols");
1850 ks = k = xmalloc(nks * sizeof(*ks));
1852 if (get_kernel_syms(ks) != nks) {
1853 perror("inconsistency with get_kernel_syms -- is someone else "
1854 "playing with modules?");
1859 /* Collect the module information. */
1864 while (k->name[0] == '#' && k->name[1]) {
1865 struct old_kernel_sym *k2;
1867 /* Find out how many symbols this module has. */
1868 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1872 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1873 mod[nmod].name = k->name + 1;
1874 mod[nmod].addr = k->value;
1876 mod[nmod].nsyms = nms;
1877 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1879 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1880 s->name = (unsigned long) k->name;
1881 s->value = k->value;
1888 n_ext_modules = nmod + 1;
1890 /* Now collect the symbols for the kernel proper. */
1892 if (k->name[0] == '#')
1895 nksyms = nms = nks - (k - ks);
1896 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1898 for (i = 0; i < nms; ++i, ++s, ++k) {
1899 s->name = (unsigned long) k->name;
1900 s->value = k->value;
1906 /* Return the kernel symbol checksum version, or zero if not used. */
1908 static int old_is_kernel_checksummed(void)
1910 /* Using_Versions is the first symbol. */
1912 && strcmp((char *) ksyms[0].name,
1913 "Using_Versions") == 0) return ksyms[0].value;
1919 static int old_create_mod_use_count(struct obj_file *f)
1921 struct obj_section *sec;
1923 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1926 obj_add_symbol(f, "mod_use_count_", -1,
1927 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1934 old_init_module(const char *m_name, struct obj_file *f,
1935 unsigned long m_size)
1938 struct old_mod_routines routines;
1939 struct old_symbol_table *symtab;
1942 /* Create the symbol table */
1944 int nsyms = 0, strsize = 0, total;
1946 /* Size things first... */
1949 for (i = 0; i < HASH_BUCKETS; ++i) {
1950 struct obj_symbol *sym;
1951 for (sym = f->symtab[i]; sym; sym = sym->next)
1952 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1953 && sym->secidx <= SHN_HIRESERVE)
1955 sym->ksymidx = nsyms++;
1956 strsize += strlen(sym->name) + 1;
1961 total = (sizeof(struct old_symbol_table)
1962 + nsyms * sizeof(struct old_module_symbol)
1963 + n_ext_modules_used * sizeof(struct old_module_ref)
1965 symtab = xmalloc(total);
1966 symtab->size = total;
1967 symtab->n_symbols = nsyms;
1968 symtab->n_refs = n_ext_modules_used;
1970 if (flag_export && nsyms) {
1971 struct old_module_symbol *ksym;
1975 ksym = symtab->symbol;
1976 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1977 + n_ext_modules_used * sizeof(struct old_module_ref));
1979 for (i = 0; i < HASH_BUCKETS; ++i) {
1980 struct obj_symbol *sym;
1981 for (sym = f->symtab[i]; sym; sym = sym->next)
1982 if (sym->ksymidx >= 0) {
1983 ksym->addr = obj_symbol_final_value(f, sym);
1985 (unsigned long) str - (unsigned long) symtab;
1987 strcpy(str, sym->name);
1988 str += strlen(sym->name) + 1;
1994 if (n_ext_modules_used) {
1995 struct old_module_ref *ref;
1998 ref = (struct old_module_ref *)
1999 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2001 for (i = 0; i < n_ext_modules; ++i)
2002 if (ext_modules[i].used)
2003 ref++->module = ext_modules[i].addr;
2007 /* Fill in routines. */
2010 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2012 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2014 /* Whew! All of the initialization is complete. Collect the final
2015 module image and give it to the kernel. */
2017 image = xmalloc(m_size);
2018 obj_create_image(f, image);
2020 /* image holds the complete relocated module, accounting correctly for
2021 mod_use_count. However the old module kernel support assume that
2022 it is receiving something which does not contain mod_use_count. */
2023 ret = old_sys_init_module(m_name, image + sizeof(long),
2024 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2025 : 0), &routines, symtab);
2027 perror_msg("init_module: %s", m_name);
2037 #define old_create_mod_use_count(x) TRUE
2038 #define old_init_module(x, y, z) TRUE
2040 #endif /* CONFIG_FEATURE_OLD_MODULE_INTERFACE */
2044 /*======================================================================*/
2045 /* Functions relating to module loading after 2.1.18. */
2048 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2052 struct obj_symbol *sym;
2053 char *contents, *loc;
2057 if ((q = strchr(p, '=')) == NULL) {
2062 key = alloca(q - p + 6);
2063 memcpy(key, "parm_", 5);
2064 memcpy(key + 5, p, q - p);
2067 p = get_modinfo_value(f, key);
2070 error_msg("invalid parameter %s", key);
2074 sym = obj_find_symbol(f, key);
2076 /* Also check that the parameter was not resolved from the kernel. */
2077 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2078 error_msg("symbol for parameter %s not found", key);
2083 min = strtoul(p, &p, 10);
2085 max = strtoul(p + 1, &p, 10);
2091 contents = f->sections[sym->secidx]->contents;
2092 loc = contents + sym->value;
2096 if ((*p == 's') || (*p == 'c')) {
2099 /* Do C quoting if we begin with a ", else slurp the lot. */
2103 str = alloca(strlen(q));
2104 for (r = str, q++; *q != '"'; ++q, ++r) {
2106 error_msg("improperly terminated string argument for %s",
2109 } else if (*q == '\\')
2143 if (q[1] >= '0' && q[1] <= '7') {
2144 c = (c * 8) + *++q - '0';
2145 if (q[1] >= '0' && q[1] <= '7')
2146 c = (c * 8) + *++q - '0';
2163 /* In this case, the string is not quoted. We will break
2164 it using the coma (like for ints). If the user wants to
2165 include comas in a string, he just has to quote it */
2167 /* Search the next coma */
2171 if (r != (char *) NULL) {
2172 /* Recopy the current field */
2173 str = alloca(r - q + 1);
2174 memcpy(str, q, r - q);
2176 /* I don't know if it is usefull, as the previous case
2177 doesn't null terminate the string ??? */
2180 /* Keep next fields */
2191 obj_string_patch(f, sym->secidx, loc - contents, str);
2192 loc += tgt_sizeof_char_p;
2194 /* Array of chars (in fact, matrix !) */
2195 unsigned long charssize; /* size of each member */
2197 /* Get the size of each member */
2198 /* Probably we should do that outside the loop ? */
2199 if (!isdigit(*(p + 1))) {
2200 error_msg("parameter type 'c' for %s must be followed by"
2201 " the maximum size", key);
2204 charssize = strtoul(p + 1, (char **) NULL, 10);
2207 if (strlen(str) >= charssize) {
2208 error_msg("string too long for %s (max %ld)", key,
2213 /* Copy to location */
2214 strcpy((char *) loc, str);
2218 long v = strtoul(q, &q, 0);
2225 loc += tgt_sizeof_short;
2229 loc += tgt_sizeof_int;
2233 loc += tgt_sizeof_long;
2237 error_msg("unknown parameter type '%c' for %s", *p, key);
2252 goto retry_end_of_value;
2256 error_msg("too many values for %s (max %d)", key, max);
2263 error_msg("invalid argument syntax for %s", key);
2270 error_msg("too few values for %s (min %d)", key, min);
2280 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
2281 static int new_is_module_checksummed(struct obj_file *f)
2283 const char *p = get_modinfo_value(f, "using_checksums");
2290 /* Get the module's kernel version in the canonical integer form. */
2293 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2298 p = get_modinfo_value(f, "kernel_version");
2301 strncpy(str, p, STRVERSIONLEN);
2303 a = strtoul(p, &p, 10);
2306 b = strtoul(p + 1, &p, 10);
2309 c = strtoul(p + 1, &q, 10);
2313 return a << 16 | b << 8 | c;
2316 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
2319 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
2321 /* Fetch the loaded modules, and all currently exported symbols. */
2323 static int new_get_kernel_symbols(void)
2325 char *module_names, *mn;
2326 struct external_module *modules, *m;
2327 struct new_module_symbol *syms, *s;
2328 size_t ret, bufsize, nmod, nsyms, i, j;
2330 /* Collect the loaded modules. */
2332 module_names = xmalloc(bufsize = 256);
2334 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2335 if (errno == ENOSPC && bufsize < ret) {
2336 module_names = xrealloc(module_names, bufsize = ret);
2337 goto retry_modules_load;
2339 perror_msg("QM_MODULES");
2343 n_ext_modules = nmod = ret;
2345 /* Collect the modules' symbols. */
2348 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2349 memset(modules, 0, nmod * sizeof(*modules));
2350 for (i = 0, mn = module_names, m = modules;
2351 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2352 struct new_module_info info;
2354 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2355 if (errno == ENOENT) {
2356 /* The module was removed out from underneath us. */
2359 perror_msg("query_module: QM_INFO: %s", mn);
2363 syms = xmalloc(bufsize = 1024);
2365 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2368 syms = xrealloc(syms, bufsize = ret);
2369 goto retry_mod_sym_load;
2371 /* The module was removed out from underneath us. */
2374 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2381 m->addr = info.addr;
2385 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2386 s->name += (unsigned long) syms;
2391 /* Collect the kernel's symbols. */
2393 syms = xmalloc(bufsize = 16 * 1024);
2394 retry_kern_sym_load:
2395 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2396 if (errno == ENOSPC && bufsize < ret) {
2397 syms = xrealloc(syms, bufsize = ret);
2398 goto retry_kern_sym_load;
2400 perror_msg("kernel: QM_SYMBOLS");
2403 nksyms = nsyms = ret;
2406 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2407 s->name += (unsigned long) syms;
2413 /* Return the kernel symbol checksum version, or zero if not used. */
2415 static int new_is_kernel_checksummed(void)
2417 struct new_module_symbol *s;
2420 /* Using_Versions is not the first symbol, but it should be in there. */
2422 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2423 if (strcmp((char *) s->name, "Using_Versions") == 0)
2430 static int new_create_this_module(struct obj_file *f, const char *m_name)
2432 struct obj_section *sec;
2434 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2435 sizeof(struct new_module));
2436 memset(sec->contents, 0, sizeof(struct new_module));
2438 obj_add_symbol(f, "__this_module", -1,
2439 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2440 sizeof(struct new_module));
2442 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2449 static int new_create_module_ksymtab(struct obj_file *f)
2451 struct obj_section *sec;
2454 /* We must always add the module references. */
2456 if (n_ext_modules_used) {
2457 struct new_module_ref *dep;
2458 struct obj_symbol *tm;
2460 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2461 (sizeof(struct new_module_ref)
2462 * n_ext_modules_used));
2466 tm = obj_find_symbol(f, "__this_module");
2467 dep = (struct new_module_ref *) sec->contents;
2468 for (i = 0; i < n_ext_modules; ++i)
2469 if (ext_modules[i].used) {
2470 dep->dep = ext_modules[i].addr;
2471 obj_symbol_patch(f, sec->idx,
2472 (char *) &dep->ref - sec->contents, tm);
2478 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2483 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2486 /* We don't want to export symbols residing in sections that
2487 aren't loaded. There are a number of these created so that
2488 we make sure certain module options don't appear twice. */
2490 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2492 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2494 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2495 struct obj_symbol *sym;
2496 for (sym = f->symtab[i]; sym; sym = sym->next)
2497 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2498 && sym->secidx <= SHN_HIRESERVE
2499 && (sym->secidx >= SHN_LORESERVE
2500 || loaded[sym->secidx])) {
2501 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2503 obj_symbol_patch(f, sec->idx, ofs, sym);
2504 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2511 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2519 new_init_module(const char *m_name, struct obj_file *f,
2520 unsigned long m_size)
2522 struct new_module *module;
2523 struct obj_section *sec;
2528 sec = obj_find_section(f, ".this");
2529 if (!sec || !sec->contents) {
2530 perror_msg_and_die("corrupt module %s?",m_name);
2532 module = (struct new_module *) sec->contents;
2533 m_addr = sec->header.sh_addr;
2535 module->size_of_struct = sizeof(*module);
2536 module->size = m_size;
2537 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2539 sec = obj_find_section(f, "__ksymtab");
2540 if (sec && sec->header.sh_size) {
2541 module->syms = sec->header.sh_addr;
2542 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2545 if (n_ext_modules_used) {
2546 sec = obj_find_section(f, ".kmodtab");
2547 module->deps = sec->header.sh_addr;
2548 module->ndeps = n_ext_modules_used;
2552 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2554 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2556 sec = obj_find_section(f, "__ex_table");
2558 module->ex_table_start = sec->header.sh_addr;
2559 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2562 sec = obj_find_section(f, ".text.init");
2564 module->runsize = sec->header.sh_addr - m_addr;
2566 sec = obj_find_section(f, ".data.init");
2568 if (!module->runsize ||
2569 module->runsize > sec->header.sh_addr - m_addr)
2570 module->runsize = sec->header.sh_addr - m_addr;
2572 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2573 if (sec && sec->header.sh_size) {
2574 module->archdata_start = (void*)sec->header.sh_addr;
2575 module->archdata_end = module->archdata_start + sec->header.sh_size;
2577 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2578 if (sec && sec->header.sh_size) {
2579 module->kallsyms_start = (void*)sec->header.sh_addr;
2580 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2583 if (!arch_init_module(f, module))
2586 /* Whew! All of the initialization is complete. Collect the final
2587 module image and give it to the kernel. */
2589 image = xmalloc(m_size);
2590 obj_create_image(f, image);
2592 ret = new_sys_init_module(m_name, (struct new_module *) image);
2594 perror_msg("init_module: %s", m_name);
2603 #define new_init_module(x, y, z) TRUE
2604 #define new_create_this_module(x, y) 0
2605 #define new_create_module_ksymtab(x)
2606 #define query_module(v, w, x, y, z) -1
2608 #endif /* CONFIG_FEATURE_NEW_MODULE_INTERFACE */
2611 /*======================================================================*/
2614 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2617 struct obj_string_patch *p;
2618 struct obj_section *strsec;
2619 size_t len = strlen(string) + 1;
2622 p = xmalloc(sizeof(*p));
2623 p->next = f->string_patches;
2624 p->reloc_secidx = secidx;
2625 p->reloc_offset = offset;
2626 f->string_patches = p;
2628 strsec = obj_find_section(f, ".kstrtab");
2629 if (strsec == NULL) {
2630 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2631 p->string_offset = 0;
2632 loc = strsec->contents;
2634 p->string_offset = strsec->header.sh_size;
2635 loc = obj_extend_section(strsec, len);
2637 memcpy(loc, string, len);
2643 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2644 struct obj_symbol *sym)
2646 struct obj_symbol_patch *p;
2648 p = xmalloc(sizeof(*p));
2649 p->next = f->symbol_patches;
2650 p->reloc_secidx = secidx;
2651 p->reloc_offset = offset;
2653 f->symbol_patches = p;
2658 static int obj_check_undefineds(struct obj_file *f)
2663 for (i = 0; i < HASH_BUCKETS; ++i) {
2664 struct obj_symbol *sym;
2665 for (sym = f->symtab[i]; sym; sym = sym->next)
2666 if (sym->secidx == SHN_UNDEF) {
2667 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2668 sym->secidx = SHN_ABS;
2671 error_msg("unresolved symbol %s", sym->name);
2680 static void obj_allocate_commons(struct obj_file *f)
2682 struct common_entry {
2683 struct common_entry *next;
2684 struct obj_symbol *sym;
2685 } *common_head = NULL;
2689 for (i = 0; i < HASH_BUCKETS; ++i) {
2690 struct obj_symbol *sym;
2691 for (sym = f->symtab[i]; sym; sym = sym->next)
2692 if (sym->secidx == SHN_COMMON) {
2693 /* Collect all COMMON symbols and sort them by size so as to
2694 minimize space wasted by alignment requirements. */
2696 struct common_entry **p, *n;
2697 for (p = &common_head; *p; p = &(*p)->next)
2698 if (sym->size <= (*p)->sym->size)
2701 n = alloca(sizeof(*n));
2709 for (i = 1; i < f->local_symtab_size; ++i) {
2710 struct obj_symbol *sym = f->local_symtab[i];
2711 if (sym && sym->secidx == SHN_COMMON) {
2712 struct common_entry **p, *n;
2713 for (p = &common_head; *p; p = &(*p)->next)
2714 if (sym == (*p)->sym)
2716 else if (sym->size < (*p)->sym->size) {
2717 n = alloca(sizeof(*n));
2727 /* Find the bss section. */
2728 for (i = 0; i < f->header.e_shnum; ++i)
2729 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2732 /* If for some reason there hadn't been one, create one. */
2733 if (i == f->header.e_shnum) {
2734 struct obj_section *sec;
2736 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2737 f->sections[i] = sec = arch_new_section();
2738 f->header.e_shnum = i + 1;
2740 memset(sec, 0, sizeof(*sec));
2741 sec->header.sh_type = SHT_PROGBITS;
2742 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2747 /* Allocate the COMMONS. */
2749 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2750 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2751 struct common_entry *c;
2753 for (c = common_head; c; c = c->next) {
2754 ElfW(Addr) align = c->sym->value;
2756 if (align > max_align)
2758 if (bss_size & (align - 1))
2759 bss_size = (bss_size | (align - 1)) + 1;
2762 c->sym->value = bss_size;
2764 bss_size += c->sym->size;
2767 f->sections[i]->header.sh_size = bss_size;
2768 f->sections[i]->header.sh_addralign = max_align;
2772 /* For the sake of patch relocation and parameter initialization,
2773 allocate zeroed data for NOBITS sections now. Note that after
2774 this we cannot assume NOBITS are really empty. */
2775 for (i = 0; i < f->header.e_shnum; ++i) {
2776 struct obj_section *s = f->sections[i];
2777 if (s->header.sh_type == SHT_NOBITS) {
2778 if (s->header.sh_size != 0)
2779 s->contents = memset(xmalloc(s->header.sh_size),
2780 0, s->header.sh_size);
2784 s->header.sh_type = SHT_PROGBITS;
2789 static unsigned long obj_load_size(struct obj_file *f)
2791 unsigned long dot = 0;
2792 struct obj_section *sec;
2794 /* Finalize the positions of the sections relative to one another. */
2796 for (sec = f->load_order; sec; sec = sec->load_next) {
2799 align = sec->header.sh_addralign;
2800 if (align && (dot & (align - 1)))
2801 dot = (dot | (align - 1)) + 1;
2803 sec->header.sh_addr = dot;
2804 dot += sec->header.sh_size;
2810 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2812 int i, n = f->header.e_shnum;
2815 /* Finalize the addresses of the sections. */
2818 for (i = 0; i < n; ++i)
2819 f->sections[i]->header.sh_addr += base;
2821 /* And iterate over all of the relocations. */
2823 for (i = 0; i < n; ++i) {
2824 struct obj_section *relsec, *symsec, *targsec, *strsec;
2825 ElfW(RelM) * rel, *relend;
2829 relsec = f->sections[i];
2830 if (relsec->header.sh_type != SHT_RELM)
2833 symsec = f->sections[relsec->header.sh_link];
2834 targsec = f->sections[relsec->header.sh_info];
2835 strsec = f->sections[symsec->header.sh_link];
2837 rel = (ElfW(RelM) *) relsec->contents;
2838 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2839 symtab = (ElfW(Sym) *) symsec->contents;
2840 strtab = (const char *) strsec->contents;
2842 for (; rel < relend; ++rel) {
2843 ElfW(Addr) value = 0;
2844 struct obj_symbol *intsym = NULL;
2845 unsigned long symndx;
2846 ElfW(Sym) * extsym = 0;
2849 /* Attempt to find a value to use for this relocation. */
2851 symndx = ELFW(R_SYM) (rel->r_info);
2853 /* Note we've already checked for undefined symbols. */
2855 extsym = &symtab[symndx];
2856 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2857 /* Local symbols we look up in the local table to be sure
2858 we get the one that is really intended. */
2859 intsym = f->local_symtab[symndx];
2861 /* Others we look up in the hash table. */
2863 if (extsym->st_name)
2864 name = strtab + extsym->st_name;
2866 name = f->sections[extsym->st_shndx]->name;
2867 intsym = obj_find_symbol(f, name);
2870 value = obj_symbol_final_value(f, intsym);
2871 intsym->referenced = 1;
2873 #if SHT_RELM == SHT_RELA
2874 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2875 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2876 if (!extsym || !extsym->st_name ||
2877 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2879 value += rel->r_addend;
2883 switch (arch_apply_relocation
2884 (f, targsec, symsec, intsym, rel, value)) {
2888 case obj_reloc_overflow:
2889 errmsg = "Relocation overflow";
2891 case obj_reloc_dangerous:
2892 errmsg = "Dangerous relocation";
2894 case obj_reloc_unhandled:
2895 errmsg = "Unhandled relocation";
2898 error_msg("%s of type %ld for %s", errmsg,
2899 (long) ELFW(R_TYPE) (rel->r_info),
2900 strtab + extsym->st_name);
2902 error_msg("%s of type %ld", errmsg,
2903 (long) ELFW(R_TYPE) (rel->r_info));
2911 /* Finally, take care of the patches. */
2913 if (f->string_patches) {
2914 struct obj_string_patch *p;
2915 struct obj_section *strsec;
2916 ElfW(Addr) strsec_base;
2917 strsec = obj_find_section(f, ".kstrtab");
2918 strsec_base = strsec->header.sh_addr;
2920 for (p = f->string_patches; p; p = p->next) {
2921 struct obj_section *targsec = f->sections[p->reloc_secidx];
2922 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2923 = strsec_base + p->string_offset;
2927 if (f->symbol_patches) {
2928 struct obj_symbol_patch *p;
2930 for (p = f->symbol_patches; p; p = p->next) {
2931 struct obj_section *targsec = f->sections[p->reloc_secidx];
2932 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2933 = obj_symbol_final_value(f, p->sym);
2940 static int obj_create_image(struct obj_file *f, char *image)
2942 struct obj_section *sec;
2943 ElfW(Addr) base = f->baseaddr;
2945 for (sec = f->load_order; sec; sec = sec->load_next) {
2948 if (sec->contents == 0 || sec->header.sh_size == 0)
2951 secimg = image + (sec->header.sh_addr - base);
2953 /* Note that we allocated data for NOBITS sections earlier. */
2954 memcpy(secimg, sec->contents, sec->header.sh_size);
2960 /*======================================================================*/
2962 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
2965 ElfW(Shdr) * section_headers;
2969 /* Read the file header. */
2971 f = arch_new_file();
2972 memset(f, 0, sizeof(*f));
2973 f->symbol_cmp = strcmp;
2974 f->symbol_hash = obj_elf_hash;
2975 f->load_order_search_start = &f->load_order;
2977 fseek(fp, 0, SEEK_SET);
2978 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2979 perror_msg("error reading ELF header");
2983 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2984 || f->header.e_ident[EI_MAG1] != ELFMAG1
2985 || f->header.e_ident[EI_MAG2] != ELFMAG2
2986 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2987 error_msg("not an ELF file");
2990 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2991 || f->header.e_ident[EI_DATA] != ELFDATAM
2992 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2993 || !MATCH_MACHINE(f->header.e_machine)) {
2994 error_msg("ELF file not for this architecture");
2997 if (f->header.e_type != ET_REL) {
2998 error_msg("ELF file not a relocatable object");
3002 /* Read the section headers. */
3004 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3005 error_msg("section header size mismatch: %lu != %lu",
3006 (unsigned long) f->header.e_shentsize,
3007 (unsigned long) sizeof(ElfW(Shdr)));
3011 shnum = f->header.e_shnum;
3012 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3013 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3015 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3016 fseek(fp, f->header.e_shoff, SEEK_SET);
3017 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3018 perror_msg("error reading ELF section headers");
3022 /* Read the section data. */
3024 for (i = 0; i < shnum; ++i) {
3025 struct obj_section *sec;
3027 f->sections[i] = sec = arch_new_section();
3028 memset(sec, 0, sizeof(*sec));
3030 sec->header = section_headers[i];
3033 if(sec->header.sh_size) switch (sec->header.sh_type) {
3042 if (!loadprogbits) {
3043 sec->contents = NULL;
3050 if (sec->header.sh_size > 0) {
3051 sec->contents = xmalloc(sec->header.sh_size);
3052 fseek(fp, sec->header.sh_offset, SEEK_SET);
3053 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3054 perror_msg("error reading ELF section data");
3058 sec->contents = NULL;
3062 #if SHT_RELM == SHT_REL
3064 error_msg("RELA relocations not supported on this architecture");
3068 error_msg("REL relocations not supported on this architecture");
3073 if (sec->header.sh_type >= SHT_LOPROC) {
3074 /* Assume processor specific section types are debug
3075 info and can safely be ignored. If this is ever not
3076 the case (Hello MIPS?), don't put ifdefs here but
3077 create an arch_load_proc_section(). */
3081 error_msg("can't handle sections of type %ld",
3082 (long) sec->header.sh_type);
3087 /* Do what sort of interpretation as needed by each section. */
3089 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3091 for (i = 0; i < shnum; ++i) {
3092 struct obj_section *sec = f->sections[i];
3093 sec->name = shstrtab + sec->header.sh_name;
3096 for (i = 0; i < shnum; ++i) {
3097 struct obj_section *sec = f->sections[i];
3099 /* .modinfo should be contents only but gcc has no attribute for that.
3100 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3102 if (strcmp(sec->name, ".modinfo") == 0)
3103 sec->header.sh_flags &= ~SHF_ALLOC;
3105 if (sec->header.sh_flags & SHF_ALLOC)
3106 obj_insert_section_load_order(f, sec);
3108 switch (sec->header.sh_type) {
3111 unsigned long nsym, j;
3115 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3116 error_msg("symbol size mismatch: %lu != %lu",
3117 (unsigned long) sec->header.sh_entsize,
3118 (unsigned long) sizeof(ElfW(Sym)));
3122 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3123 strtab = f->sections[sec->header.sh_link]->contents;
3124 sym = (ElfW(Sym) *) sec->contents;
3126 /* Allocate space for a table of local symbols. */
3127 j = f->local_symtab_size = sec->header.sh_info;
3128 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3130 /* Insert all symbols into the hash table. */
3131 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3134 name = strtab + sym->st_name;
3136 name = f->sections[sym->st_shndx]->name;
3138 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3139 sym->st_value, sym->st_size);
3145 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3146 error_msg("relocation entry size mismatch: %lu != %lu",
3147 (unsigned long) sec->header.sh_entsize,
3148 (unsigned long) sizeof(ElfW(RelM)));
3152 /* XXX Relocation code from modutils-2.3.19 is not here.
3153 * Why? That's about 20 lines of code from obj/obj_load.c,
3154 * which gets done in a second pass through the sections.
3155 * This BusyBox insmod does similar work in obj_relocate(). */
3162 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
3164 * load the unloaded sections directly into the memory allocated by
3165 * kernel for the module
3168 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3170 ElfW(Addr) base = f->baseaddr;
3171 struct obj_section* sec;
3173 for (sec = f->load_order; sec; sec = sec->load_next) {
3175 /* section already loaded? */
3176 if (sec->contents != NULL)
3179 if (sec->header.sh_size == 0)
3182 sec->contents = imagebase + (sec->header.sh_addr - base);
3183 fseek(fp, sec->header.sh_offset, SEEK_SET);
3184 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3185 error_msg("error reading ELF section data: %s\n", strerror(errno));
3194 static void hide_special_symbols(struct obj_file *f)
3196 static const char *const specials[] = {
3203 struct obj_symbol *sym;
3204 const char *const *p;
3206 for (p = specials; *p; ++p)
3207 if ((sym = obj_find_symbol(f, *p)) != NULL)
3209 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3214 extern int insmod_main( int argc, char **argv)
3221 unsigned long m_size;
3226 char m_name[FILENAME_MAX + 1] = "\0";
3227 int exit_status = EXIT_FAILURE;
3229 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3230 struct utsname uts_info;
3231 char m_strversion[STRVERSIONLEN];
3236 /* Parse any options */
3237 while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
3239 case 'f': /* force loading */
3240 flag_force_load = 1;
3242 case 'k': /* module loaded by kerneld, auto-cleanable */
3245 case 'v': /* verbose output */
3248 case 'x': /* do not export externs */
3251 case 'o': /* name the output module */
3252 strncpy(m_name, optarg, FILENAME_MAX);
3254 case 'L': /* Stub warning */
3255 /* This is needed for compatibility with modprobe.
3256 * In theory, this does locking, but we don't do
3257 * that. So be careful and plan your life around not
3258 * loading the same module 50 times concurrently. */
3265 if (argv[optind] == NULL) {
3269 /* Grab the module name */
3270 if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3277 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3279 memcpy(m_fullName, tmp, len);
3280 m_fullName[len]='\0';
3281 if (*m_name == '\0') {
3282 strcpy(m_name, m_fullName);
3284 strcat(m_fullName, ".o");
3286 /* Get a filedesc for the module. Check we we have a complete path */
3287 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3288 (fp = fopen(argv[optind], "r")) == NULL) {
3289 struct utsname myuname;
3291 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3292 * but do not error out yet if we fail to find it... */
3293 if (uname(&myuname) == 0) {
3294 char module_dir[FILENAME_MAX];
3295 char real_module_dir[FILENAME_MAX];
3296 snprintf (module_dir, sizeof(module_dir), "%s/%s",
3297 _PATH_MODULES, myuname.release);
3298 /* Jump through hoops in case /lib/modules/`uname -r`
3299 * is a symlink. We do not want recursive_action to
3300 * follow symlinks, but we do want to follow the
3301 * /lib/modules/`uname -r` dir, So resolve it ourselves
3302 * if it is a link... */
3303 if (realpath (module_dir, real_module_dir) == NULL)
3304 strcpy(real_module_dir, module_dir);
3305 recursive_action(real_module_dir, TRUE, FALSE, FALSE,
3306 check_module_name_match, 0, m_fullName);
3309 /* Check if we have found anything yet */
3310 if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL))
3312 char module_dir[FILENAME_MAX];
3313 if (realpath (_PATH_MODULES, module_dir) == NULL)
3314 strcpy(module_dir, _PATH_MODULES);
3315 /* No module found under /lib/modules/`uname -r`, this
3316 * time cast the net a bit wider. Search /lib/modules/ */
3317 if (! recursive_action(module_dir, TRUE, FALSE, FALSE,
3318 check_module_name_match, 0, m_fullName))
3320 if (m_filename[0] == '\0'
3321 || ((fp = fopen(m_filename, "r")) == NULL))
3323 error_msg("%s: no module by that name found", m_fullName);
3324 return EXIT_FAILURE;
3327 error_msg_and_die("%s: no module by that name found", m_fullName);
3330 safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
3332 printf("Using %s\n", m_filename);
3334 if ((f = obj_load(fp, LOADBITS)) == NULL)
3335 perror_msg_and_die("Could not load the module");
3337 if (get_modinfo_value(f, "kernel_version") == NULL)
3342 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3343 /* Version correspondence? */
3345 if (uname(&uts_info) < 0)
3346 uts_info.release[0] = '\0';
3347 if (m_has_modinfo) {
3348 m_version = new_get_module_version(f, m_strversion);
3350 m_version = old_get_module_version(f, m_strversion);
3351 if (m_version == -1) {
3352 error_msg("couldn't find the kernel version the module was "
3358 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3359 if (flag_force_load) {
3360 error_msg("Warning: kernel-module version mismatch\n"
3361 "\t%s was compiled for kernel version %s\n"
3362 "\twhile this kernel is version %s",
3363 m_filename, m_strversion, uts_info.release);
3365 error_msg("kernel-module version mismatch\n"
3366 "\t%s was compiled for kernel version %s\n"
3367 "\twhile this kernel is version %s.",
3368 m_filename, m_strversion, uts_info.release);
3373 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3375 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3377 if (k_new_syscalls) {
3378 #ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
3379 if (!new_get_kernel_symbols())
3381 k_crcs = new_is_kernel_checksummed();
3383 error_msg("Not configured to support new kernels");
3387 #ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
3388 if (!old_get_kernel_symbols(m_name))
3390 k_crcs = old_is_kernel_checksummed();
3392 error_msg("Not configured to support old kernels");
3397 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
3399 m_crcs = new_is_module_checksummed(f);
3401 m_crcs = old_is_module_checksummed(f);
3403 if (m_crcs != k_crcs)
3404 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3405 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
3407 /* Let the module know about the kernel symbols. */
3408 add_kernel_symbols(f);
3410 /* Allocate common symbols, symbol tables, and string tables. */
3413 ? !new_create_this_module(f, m_name)
3414 : !old_create_mod_use_count(f))
3419 if (!obj_check_undefineds(f)) {
3422 obj_allocate_commons(f);
3424 /* done with the module name, on to the optional var=value arguments */
3427 if (optind < argc) {
3429 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3430 : !old_process_module_arguments(f, argc - optind, argv + optind))
3437 hide_special_symbols(f);
3440 new_create_module_ksymtab(f);
3442 /* Find current size of the module */
3443 m_size = obj_load_size(f);
3446 m_addr = create_module(m_name, m_size);
3447 if (m_addr==-1) switch (errno) {
3449 error_msg("A module named %s already exists", m_name);
3452 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3456 perror_msg("create_module: %s", m_name);
3462 * the PROGBITS section was not loaded by the obj_load
3463 * now we can load them directly into the kernel memory
3465 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3466 delete_module(m_name);
3471 if (!obj_relocate(f, m_addr)) {
3472 delete_module(m_name);
3477 ? !new_init_module(m_name, f, m_size)
3478 : !old_init_module(m_name, f, m_size))
3480 delete_module(m_name);
3484 exit_status = EXIT_SUCCESS;
3488 return(exit_status);