1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
4 * This version of insmod now supports x86, ARM, SH3/4, powerpc, and MIPS.
6 * Copyright (C) 1999,2000,2001 by Lineo, inc.
7 * Written by Erik Andersen <andersen@lineo.com>
8 * and Ron Alder <alder@lineo.com>
10 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
11 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
13 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
14 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
15 * very minor changes required to also work with StrongArm and presumably
16 * all ARM based systems.
18 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
19 * PowerPC specific code stolen from modutils-2.3.16,
20 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
21 * I've only tested the code on mpc8xx platforms in big-endian mode.
22 * Did some cleanup and added BB_USE_xxx_ENTRIES...
24 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
25 * based on modutils-2.4.2
26 * MIPS specific support for Elf loading and relocation.
27 * Copyright 1996, 1997 Linux International.
28 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
30 * Based almost entirely on the Linux modutils-2.3.11 implementation.
31 * Copyright 1996, 1997 Linux International.
32 * New implementation contributed by Richard Henderson <rth@tamu.edu>
33 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
34 * Restructured (and partly rewritten) by:
35 * Björn Ekwall <bj0rn@blox.se> February 1999
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
45 * General Public License for more details.
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
63 #include <sys/utsname.h>
66 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
67 # define new_sys_init_module init_module
69 # define old_sys_init_module init_module
72 #ifdef BB_FEATURE_INSMOD_LOADINKMEM
78 #if defined(__powerpc__)
79 #define BB_USE_PLT_ENTRIES
80 #define BB_PLT_ENTRY_SIZE 16
84 #define BB_USE_PLT_ENTRIES
85 #define BB_PLT_ENTRY_SIZE 8
86 #define BB_USE_GOT_ENTRIES
87 #define BB_GOT_ENTRY_SIZE 8
91 #define BB_USE_GOT_ENTRIES
92 #define BB_GOT_ENTRY_SIZE 4
96 #define BB_USE_GOT_ENTRIES
97 #define BB_GOT_ENTRY_SIZE 4
100 #if defined(__mips__)
104 //----------------------------------------------------------------------------
105 //--------modutils module.h, lines 45-242
106 //----------------------------------------------------------------------------
108 /* Definitions for the Linux module syscall interface.
109 Copyright 1996, 1997 Linux International.
111 Contributed by Richard Henderson <rth@tamu.edu>
113 This file is part of the Linux modutils.
115 This program is free software; you can redistribute it and/or modify it
116 under the terms of the GNU General Public License as published by the
117 Free Software Foundation; either version 2 of the License, or (at your
118 option) any later version.
120 This program is distributed in the hope that it will be useful, but
121 WITHOUT ANY WARRANTY; without even the implied warranty of
122 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
123 General Public License for more details.
125 You should have received a copy of the GNU General Public License
126 along with this program; if not, write to the Free Software Foundation,
127 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
130 #ifndef MODUTILS_MODULE_H
131 static const int MODUTILS_MODULE_H = 1;
133 #ident "$Id: insmod.c,v 1.68 2001/07/17 01:12:36 andersen Exp $"
135 /* This file contains the structures used by the 2.0 and 2.1 kernels.
136 We do not use the kernel headers directly because we do not wish
137 to be dependant on a particular kernel version to compile insmod. */
140 /*======================================================================*/
141 /* The structures used by Linux 2.0. */
143 /* The symbol format used by get_kernel_syms(2). */
144 struct old_kernel_sym
150 struct old_module_ref
152 unsigned long module; /* kernel addresses */
156 struct old_module_symbol
162 struct old_symbol_table
164 int size; /* total, including string table!!! */
167 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
168 struct old_module_ref ref[0]; /* actual size defined by n_refs */
171 struct old_mod_routines
174 unsigned long cleanup;
180 unsigned long ref; /* the list of modules that refer to me */
181 unsigned long symtab;
183 int size; /* size of module in pages */
184 unsigned long addr; /* address of module */
186 unsigned long cleanup; /* cleanup routine */
189 /* Sent to init_module(2) or'ed into the code size parameter. */
190 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
192 int get_kernel_syms(struct old_kernel_sym *);
193 int old_sys_init_module(const char *name, char *code, unsigned codesize,
194 struct old_mod_routines *, struct old_symbol_table *);
196 /*======================================================================*/
197 /* For sizeof() which are related to the module platform and not to the
198 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
200 #define tgt_sizeof_char sizeof(char)
201 #define tgt_sizeof_short sizeof(short)
202 #define tgt_sizeof_int sizeof(int)
203 #define tgt_sizeof_long sizeof(long)
204 #define tgt_sizeof_char_p sizeof(char *)
205 #define tgt_sizeof_void_p sizeof(void *)
206 #define tgt_long long
208 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
209 #undef tgt_sizeof_long
210 #undef tgt_sizeof_char_p
211 #undef tgt_sizeof_void_p
213 static const int tgt_sizeof_long = 8;
214 static const int tgt_sizeof_char_p = 8;
215 static const int tgt_sizeof_void_p = 8;
216 #define tgt_long long long
219 /*======================================================================*/
220 /* The structures used in Linux 2.1. */
222 /* Note: new_module_symbol does not use tgt_long intentionally */
223 struct new_module_symbol
229 struct new_module_persist;
231 struct new_module_ref
233 unsigned tgt_long dep; /* kernel addresses */
234 unsigned tgt_long ref;
235 unsigned tgt_long next_ref;
240 unsigned tgt_long size_of_struct; /* == sizeof(module) */
241 unsigned tgt_long next;
242 unsigned tgt_long name;
243 unsigned tgt_long size;
246 unsigned tgt_long flags; /* AUTOCLEAN et al */
251 unsigned tgt_long syms;
252 unsigned tgt_long deps;
253 unsigned tgt_long refs;
254 unsigned tgt_long init;
255 unsigned tgt_long cleanup;
256 unsigned tgt_long ex_table_start;
257 unsigned tgt_long ex_table_end;
259 unsigned tgt_long gp;
261 /* Everything after here is extension. */
262 unsigned tgt_long persist_start;
263 unsigned tgt_long persist_end;
264 unsigned tgt_long can_unload;
265 unsigned tgt_long runsize;
266 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
267 const char *kallsyms_start; /* All symbols for kernel debugging */
268 const char *kallsyms_end;
269 const char *archdata_start; /* arch specific data for module */
270 const char *archdata_end;
271 const char *kernel_data; /* Reserved for kernel internal use */
275 #define ARCHDATA_SEC_NAME "__archdata"
276 #define KALLSYMS_SEC_NAME "__kallsyms"
279 struct new_module_info
287 /* Bits of module.flags. */
288 static const int NEW_MOD_RUNNING = 1;
289 static const int NEW_MOD_DELETED = 2;
290 static const int NEW_MOD_AUTOCLEAN = 4;
291 static const int NEW_MOD_VISITED = 8;
292 static const int NEW_MOD_USED_ONCE = 16;
294 int new_sys_init_module(const char *name, const struct new_module *);
295 int query_module(const char *name, int which, void *buf, size_t bufsize,
298 /* Values for query_module's which. */
300 static const int QM_MODULES = 1;
301 static const int QM_DEPS = 2;
302 static const int QM_REFS = 3;
303 static const int QM_SYMBOLS = 4;
304 static const int QM_INFO = 5;
306 /*======================================================================*/
307 /* The system calls unchanged between 2.0 and 2.1. */
309 unsigned long create_module(const char *, size_t);
310 int delete_module(const char *);
313 #endif /* module.h */
315 //----------------------------------------------------------------------------
316 //--------end of modutils module.h
317 //----------------------------------------------------------------------------
321 //----------------------------------------------------------------------------
322 //--------modutils obj.h, lines 253-462
323 //----------------------------------------------------------------------------
325 /* Elf object file loading and relocation routines.
326 Copyright 1996, 1997 Linux International.
328 Contributed by Richard Henderson <rth@tamu.edu>
330 This file is part of the Linux modutils.
332 This program is free software; you can redistribute it and/or modify it
333 under the terms of the GNU General Public License as published by the
334 Free Software Foundation; either version 2 of the License, or (at your
335 option) any later version.
337 This program is distributed in the hope that it will be useful, but
338 WITHOUT ANY WARRANTY; without even the implied warranty of
339 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
340 General Public License for more details.
342 You should have received a copy of the GNU General Public License
343 along with this program; if not, write to the Free Software Foundation,
344 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
347 #ifndef MODUTILS_OBJ_H
348 static const int MODUTILS_OBJ_H = 1;
350 #ident "$Id: insmod.c,v 1.68 2001/07/17 01:12:36 andersen Exp $"
352 /* The relocatable object is manipulated using elfin types. */
358 /* Machine-specific elf macros for i386 et al. */
360 /* the SH changes have only been tested on the SH4 in =little endian= mode */
361 /* I'm not sure about big endian, so let's warn: */
363 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
364 #error insmod.c may require changes for use on big endian SH4/SH3
367 /* it may or may not work on the SH1/SH2... So let's error on those
369 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
370 #error insmod.c may require changes for non-SH3/SH4 use
373 #define ELFCLASSM ELFCLASS32
375 #if (defined(__mc68000__))
376 #define ELFDATAM ELFDATA2MSB
383 #define MATCH_MACHINE(x) (x == EM_SH)
384 #define SHT_RELM SHT_RELA
385 #define Elf32_RelM Elf32_Rela
386 #define ELFDATAM ELFDATA2LSB
388 #elif defined(__arm__)
390 #define MATCH_MACHINE(x) (x == EM_ARM)
391 #define SHT_RELM SHT_REL
392 #define Elf32_RelM Elf32_Rel
393 #define ELFDATAM ELFDATA2LSB
395 #elif defined(__powerpc__)
397 #define MATCH_MACHINE(x) (x == EM_PPC)
398 #define SHT_RELM SHT_RELA
399 #define Elf32_RelM Elf32_Rela
400 #define ELFDATAM ELFDATA2MSB
402 #elif defined(__mips__)
404 /* Account for ELF spec changes. */
405 #ifndef EM_MIPS_RS3_LE
406 #ifdef EM_MIPS_RS4_BE
407 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
409 #define EM_MIPS_RS3_LE 10
411 #endif /* !EM_MIPS_RS3_LE */
413 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
414 #define SHT_RELM SHT_REL
415 #define Elf32_RelM Elf32_Rel
417 #define ELFDATAM ELFDATA2MSB
420 #define ELFDATAM ELFDATA2LSB
423 #elif defined(__i386__)
425 /* presumably we can use these for anything but the SH and ARM*/
426 /* this is the previous behavior, but it does result in
427 insmod.c being broken on anything except i386 */
429 #define MATCH_MACHINE(x) (x == EM_386)
431 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
434 #define SHT_RELM SHT_REL
435 #define Elf32_RelM Elf32_Rel
436 #define ELFDATAM ELFDATA2LSB
438 #elif defined(__mc68000__)
440 #define MATCH_MACHINE(x) (x == EM_68K)
441 #define SHT_RELM SHT_RELA
442 #define Elf32_RelM Elf32_Rela
445 #error Sorry, but insmod.c does not yet support this architecture...
449 # if ELFCLASSM == ELFCLASS32
450 # define ElfW(x) Elf32_ ## x
451 # define ELFW(x) ELF32_ ## x
453 # define ElfW(x) Elf64_ ## x
454 # define ELFW(x) ELF64_ ## x
458 /* For some reason this is missing from libc5. */
459 #ifndef ELF32_ST_INFO
460 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
463 #ifndef ELF64_ST_INFO
464 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
467 struct obj_string_patch;
468 struct obj_symbol_patch;
475 struct obj_section *load_next;
481 struct obj_symbol *next; /* hash table link */
485 int secidx; /* the defining section index/module */
487 int ksymidx; /* for export to the kernel symtab */
488 int referenced; /* actually used in the link */
491 /* Hardcode the hash table size. We shouldn't be needing so many
492 symbols that we begin to degrade performance, and we get a big win
493 by giving the compiler a constant divisor. */
495 #define HASH_BUCKETS 521
501 struct obj_section **sections;
502 struct obj_section *load_order;
503 struct obj_section **load_order_search_start;
504 struct obj_string_patch *string_patches;
505 struct obj_symbol_patch *symbol_patches;
506 int (*symbol_cmp)(const char *, const char *);
507 unsigned long (*symbol_hash)(const char *);
508 unsigned long local_symtab_size;
509 struct obj_symbol **local_symtab;
510 struct obj_symbol *symtab[HASH_BUCKETS];
521 struct obj_string_patch
523 struct obj_string_patch *next;
525 ElfW(Addr) reloc_offset;
526 ElfW(Addr) string_offset;
529 struct obj_symbol_patch
531 struct obj_symbol_patch *next;
533 ElfW(Addr) reloc_offset;
534 struct obj_symbol *sym;
538 /* Generic object manipulation routines. */
540 static unsigned long obj_elf_hash(const char *);
542 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
544 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
547 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
548 struct obj_symbol *sym);
550 static void obj_set_symbol_compare(struct obj_file *f,
551 int (*cmp)(const char *, const char *),
552 unsigned long (*hash)(const char *));
554 static struct obj_section *obj_find_section (struct obj_file *f,
557 static void obj_insert_section_load_order (struct obj_file *f,
558 struct obj_section *sec);
560 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
565 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
570 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
572 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
575 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
576 struct obj_symbol *sym);
578 static int obj_check_undefineds(struct obj_file *f);
580 static void obj_allocate_commons(struct obj_file *f);
582 static unsigned long obj_load_size (struct obj_file *f);
584 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
586 static struct obj_file *obj_load(FILE *f, int loadprogbits);
588 static int obj_create_image (struct obj_file *f, char *image);
590 /* Architecture specific manipulation routines. */
592 static struct obj_file *arch_new_file (void);
594 static struct obj_section *arch_new_section (void);
596 static struct obj_symbol *arch_new_symbol (void);
598 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
599 struct obj_section *targsec,
600 struct obj_section *symsec,
601 struct obj_symbol *sym,
602 ElfW(RelM) *rel, ElfW(Addr) value);
604 static int arch_create_got (struct obj_file *f);
606 static int arch_init_module (struct obj_file *f, struct new_module *);
609 //----------------------------------------------------------------------------
610 //--------end of modutils obj.h
611 //----------------------------------------------------------------------------
617 #define _PATH_MODULES "/lib/modules"
618 static const int STRVERSIONLEN = 32;
620 /*======================================================================*/
622 static int flag_force_load = 0;
623 static int flag_autoclean = 0;
624 static int flag_verbose = 0;
625 static int flag_export = 1;
628 /*======================================================================*/
630 /* previously, these were named i386_* but since we could be
631 compiling for the sh, I've renamed them to the more general
632 arch_* These structures are the same between the x86 and SH,
633 and we can't support anything else right now anyway. In the
634 future maybe they should be #if defined'd */
640 #if defined(BB_USE_PLT_ENTRIES)
641 struct arch_plt_entry
645 int inited:1; /* has been set up */
649 #if defined(BB_USE_GOT_ENTRIES)
650 struct arch_got_entry {
652 unsigned offset_done:1;
653 unsigned reloc_done:1;
657 #if defined(__mips__)
660 struct mips_hi16 *next;
667 struct obj_file root;
668 #if defined(BB_USE_PLT_ENTRIES)
669 struct obj_section *plt;
671 #if defined(BB_USE_GOT_ENTRIES)
672 struct obj_section *got;
674 #if defined(__mips__)
675 struct mips_hi16 *mips_hi16_list;
680 struct obj_symbol root;
681 #if defined(BB_USE_PLT_ENTRIES)
682 struct arch_plt_entry pltent;
684 #if defined(BB_USE_GOT_ENTRIES)
685 struct arch_got_entry gotent;
690 struct external_module {
695 struct new_module_symbol *syms;
698 static struct new_module_symbol *ksyms;
699 static size_t nksyms;
701 static struct external_module *ext_modules;
702 static int n_ext_modules;
703 static int n_ext_modules_used;
704 extern int delete_module(const char *);
706 static char m_filename[FILENAME_MAX + 1];
707 static char m_fullName[FILENAME_MAX + 1];
711 /*======================================================================*/
714 static int check_module_name_match(const char *filename, struct stat *statbuf,
717 char *fullname = (char *) userdata;
719 if (fullname[0] == '\0')
722 char *tmp, *tmp1 = strdup(filename);
723 tmp = get_last_path_component(tmp1);
724 if (strcmp(tmp, fullname) == 0) {
726 /* Stop searching if we find a match */
727 safe_strncpy(m_filename, filename, sizeof(m_filename));
736 /*======================================================================*/
738 static struct obj_file *arch_new_file(void)
741 f = xmalloc(sizeof(*f));
743 #if defined(BB_USE_PLT_ENTRIES)
746 #if defined(BB_USE_GOT_ENTRIES)
749 #if defined(__mips__)
750 f->mips_hi16_list = NULL;
756 static struct obj_section *arch_new_section(void)
758 return xmalloc(sizeof(struct obj_section));
761 static struct obj_symbol *arch_new_symbol(void)
763 struct arch_symbol *sym;
764 sym = xmalloc(sizeof(*sym));
766 #if defined(BB_USE_PLT_ENTRIES)
767 memset(&sym->pltent, 0, sizeof(sym->pltent));
769 #if defined(BB_USE_GOT_ENTRIES)
770 memset(&sym->gotent, 0, sizeof(sym->gotent));
776 static enum obj_reloc
777 arch_apply_relocation(struct obj_file *f,
778 struct obj_section *targsec,
779 struct obj_section *symsec,
780 struct obj_symbol *sym,
781 ElfW(RelM) *rel, ElfW(Addr) v)
783 struct arch_file *ifile = (struct arch_file *) f;
784 #if !(defined(__mips__))
785 struct arch_symbol *isym = (struct arch_symbol *) sym;
788 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
789 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
790 #if defined(BB_USE_GOT_ENTRIES)
791 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
793 #if defined(BB_USE_PLT_ENTRIES)
794 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
795 struct arch_plt_entry *pe;
798 enum obj_reloc ret = obj_reloc_ok;
800 switch (ELF32_R_TYPE(rel->r_info)) {
802 /* even though these constants seem to be the same for
803 the i386 and the sh, we "#if define" them for clarity
804 and in case that ever changes */
807 #elif defined(__arm__)
809 #elif defined(__i386__)
811 #elif defined(__mc68000__)
813 #elif defined(__powerpc__)
815 #elif defined(__mips__)
822 #elif defined(__arm__)
824 #elif defined(__i386__)
826 #elif defined(__mc68000__)
828 #elif defined(__powerpc__)
830 #elif defined(__mips__)
835 #if defined(__mc68000__)
838 ret = obj_reloc_overflow;
843 ret = obj_reloc_overflow;
846 #endif /* __mc68000__ */
848 #if defined(__powerpc__)
849 case R_PPC_ADDR16_HA:
850 *(unsigned short *)loc = (v + 0x8000) >> 16;
853 case R_PPC_ADDR16_HI:
854 *(unsigned short *)loc = v >> 16;
857 case R_PPC_ADDR16_LO:
858 *(unsigned short *)loc = v;
862 #if defined(__mips__)
865 ret = obj_reloc_dangerous;
866 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
867 ret = obj_reloc_overflow;
869 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
877 /* We cannot relocate this one now because we don't know the value
878 of the carry we need to add. Save the information, and let LO16
879 do the actual relocation. */
880 n = (struct mips_hi16 *) xmalloc(sizeof *n);
883 n->next = ifile->mips_hi16_list;
884 ifile->mips_hi16_list = n;
890 unsigned long insnlo = *loc;
891 Elf32_Addr val, vallo;
893 /* Sign extend the addend we extract from the lo insn. */
894 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
896 if (ifile->mips_hi16_list != NULL) {
899 l = ifile->mips_hi16_list;
901 struct mips_hi16 *next;
904 /* The value for the HI16 had best be the same. */
905 assert(v == l->value);
907 /* Do the HI16 relocation. Note that we actually don't
908 need to know anything about the LO16 itself, except where
909 to find the low 16 bits of the addend needed by the LO16. */
912 ((insn & 0xffff) << 16) +
916 /* Account for the sign extension that will happen in the
923 insn = (insn & ~0xffff) | val;
931 ifile->mips_hi16_list = NULL;
934 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
936 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
943 #elif defined(__sh__)
947 #elif defined(__i386__)
952 #elif defined(__mc68000__)
955 if ((Elf32_Sword)v > 0x7f || (Elf32_Sword)v < -(Elf32_Sword)0x80)
956 ret = obj_reloc_overflow;
961 if ((Elf32_Sword)v > 0x7fff || (Elf32_Sword)v < -(Elf32_Sword)0x8000)
962 ret = obj_reloc_overflow;
966 *(int *)loc = v - dot;
968 #elif defined(__powerpc__)
978 #elif defined(__i386__)
981 #if defined(BB_USE_PLT_ENTRIES)
987 #if defined(__powerpc__)
990 /* find the plt entry and initialize it if necessary */
991 assert(isym != NULL);
993 pe = (struct arch_plt_entry*) &isym->pltent;
996 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
998 /* generate some machine code */
1000 #if defined(__arm__)
1001 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1002 ip[1] = v; /* sym@ */
1004 #if defined(__powerpc__)
1005 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1006 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1007 ip[2] = 0x7d6903a6; /* mtctr r11 */
1008 ip[3] = 0x4e800420; /* bctr */
1013 /* relative distance to target */
1015 /* if the target is too far away.... */
1016 if ((int)v < -0x02000000 || (int)v >= 0x02000000) {
1017 /* go via the plt */
1018 v = plt + pe->offset - dot;
1021 ret = obj_reloc_dangerous;
1023 /* merge the offset into the instruction. */
1024 #if defined(__arm__)
1025 /* Convert to words. */
1028 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1030 #if defined(__powerpc__)
1031 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1034 #endif /* BB_USE_PLT_ENTRIES */
1036 #if defined(__arm__)
1037 #elif defined(__sh__)
1042 #elif defined(__i386__)
1043 case R_386_GLOB_DAT:
1044 case R_386_JMP_SLOT:
1047 #elif defined(__mc68000__)
1048 case R_68K_GLOB_DAT:
1049 case R_68K_JMP_SLOT:
1054 #if defined(__arm__)
1055 #elif defined(__sh__)
1057 *loc += f->baseaddr + rel->r_addend;
1059 #elif defined(__i386__)
1060 case R_386_RELATIVE:
1061 *loc += f->baseaddr;
1063 #elif defined(__mc68000__)
1064 case R_68K_RELATIVE:
1065 *(int *)loc += f->baseaddr;
1069 #if defined(BB_USE_GOT_ENTRIES)
1071 #if !defined(__68k__)
1074 #elif defined(__arm__)
1076 #elif defined(__i386__)
1081 *loc += got - dot + rel->r_addend;;
1082 #elif defined(__i386__) || defined(__arm__) || defined(__m68k_)
1090 #elif defined(__arm__)
1092 #elif defined(__i386__)
1094 #elif defined(__mc68000__)
1097 assert(isym != NULL);
1098 /* needs an entry in the .got: set it, once */
1099 if (!isym->gotent.reloc_done) {
1100 isym->gotent.reloc_done = 1;
1101 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1103 /* make the reloc with_respect_to_.got */
1105 *loc += isym->gotent.offset + rel->r_addend;
1106 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1107 *loc += isym->gotent.offset;
1111 /* address relative to the got */
1112 #if !defined(__mc68000__)
1115 #elif defined(__arm__)
1117 #elif defined(__i386__)
1119 #elif defined(__mc68000__)
1125 #endif // __mc68000__
1127 #endif /* BB_USE_GOT_ENTRIES */
1130 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1131 ret = obj_reloc_unhandled;
1138 static int arch_create_got(struct obj_file *f)
1140 #if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
1141 struct arch_file *ifile = (struct arch_file *) f;
1143 #if defined(BB_USE_GOT_ENTRIES)
1144 int got_offset = 0, gotneeded = 0;
1146 #if defined(BB_USE_PLT_ENTRIES)
1147 int plt_offset = 0, pltneeded = 0;
1149 struct obj_section *relsec, *symsec, *strsec;
1150 ElfW(RelM) *rel, *relend;
1151 ElfW(Sym) *symtab, *extsym;
1152 const char *strtab, *name;
1153 struct arch_symbol *intsym;
1155 for (i = 0; i < f->header.e_shnum; ++i) {
1156 relsec = f->sections[i];
1157 if (relsec->header.sh_type != SHT_RELM)
1160 symsec = f->sections[relsec->header.sh_link];
1161 strsec = f->sections[symsec->header.sh_link];
1163 rel = (ElfW(RelM) *) relsec->contents;
1164 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1165 symtab = (ElfW(Sym) *) symsec->contents;
1166 strtab = (const char *) strsec->contents;
1168 for (; rel < relend; ++rel) {
1169 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1171 switch (ELF32_R_TYPE(rel->r_info)) {
1172 #if defined(__arm__)
1175 #elif defined(__sh__)
1178 #elif defined(__i386__)
1181 #elif defined(__mc68000__)
1186 #if defined(__powerpc__)
1192 #if defined(__arm__)
1201 if (got_offset == 0)
1203 #elif defined(__sh__)
1207 #elif defined(__i386__)
1217 if (extsym->st_name != 0) {
1218 name = strtab + extsym->st_name;
1220 name = f->sections[extsym->st_shndx]->name;
1222 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1223 #if defined(BB_USE_GOT_ENTRIES)
1224 if (!intsym->gotent.offset_done) {
1225 intsym->gotent.offset_done = 1;
1226 intsym->gotent.offset = got_offset;
1227 got_offset += BB_GOT_ENTRY_SIZE;
1230 #if defined(BB_USE_PLT_ENTRIES)
1231 if (pltneeded && intsym->pltent.allocated == 0) {
1232 intsym->pltent.allocated = 1;
1233 intsym->pltent.offset = plt_offset;
1234 plt_offset += BB_PLT_ENTRY_SIZE;
1235 intsym->pltent.inited = 0;
1242 #if defined(BB_USE_GOT_ENTRIES)
1244 struct obj_section* myrelsec = obj_find_section(f, ".got");
1247 obj_extend_section(myrelsec, got_offset);
1249 myrelsec = obj_create_alloced_section(f, ".got",
1255 ifile->got = myrelsec;
1259 #if defined(BB_USE_PLT_ENTRIES)
1261 ifile->plt = obj_create_alloced_section(f, ".plt",
1269 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1275 /*======================================================================*/
1277 /* Standard ELF hash function. */
1278 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1280 unsigned long h = 0;
1287 if ((g = (h & 0xf0000000)) != 0) {
1296 static unsigned long obj_elf_hash(const char *name)
1298 return obj_elf_hash_n(name, strlen(name));
1301 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1302 /* Get the kernel version in the canonical integer form. */
1304 static int get_kernel_version(char str[STRVERSIONLEN])
1306 struct utsname uts_info;
1309 if (uname(&uts_info) < 0)
1311 strncpy(str, uts_info.release, STRVERSIONLEN);
1313 kv = get_kernel_revision();
1318 /* String comparison for non-co-versioned kernel and module. */
1320 static int ncv_strcmp(const char *a, const char *b)
1322 size_t alen = strlen(a), blen = strlen(b);
1324 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1325 return strncmp(a, b, alen);
1326 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1327 return strncmp(a, b, blen);
1329 return strcmp(a, b);
1332 /* String hashing for non-co-versioned kernel and module. Here
1333 we are simply forced to drop the crc from the hash. */
1335 static unsigned long ncv_symbol_hash(const char *str)
1337 size_t len = strlen(str);
1338 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1340 return obj_elf_hash_n(str, len);
1344 obj_set_symbol_compare(struct obj_file *f,
1345 int (*cmp) (const char *, const char *),
1346 unsigned long (*hash) (const char *))
1349 f->symbol_cmp = cmp;
1351 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1354 f->symbol_hash = hash;
1356 memcpy(tmptab, f->symtab, sizeof(tmptab));
1357 memset(f->symtab, 0, sizeof(f->symtab));
1359 for (i = 0; i < HASH_BUCKETS; ++i)
1360 for (sym = tmptab[i]; sym; sym = next) {
1361 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1363 sym->next = f->symtab[h];
1369 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1371 static struct obj_symbol *
1372 obj_add_symbol(struct obj_file *f, const char *name,
1373 unsigned long symidx, int info,
1374 int secidx, ElfW(Addr) value,
1377 struct obj_symbol *sym;
1378 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1379 int n_type = ELFW(ST_TYPE) (info);
1380 int n_binding = ELFW(ST_BIND) (info);
1382 for (sym = f->symtab[hash]; sym; sym = sym->next)
1383 if (f->symbol_cmp(sym->name, name) == 0) {
1384 int o_secidx = sym->secidx;
1385 int o_info = sym->info;
1386 int o_type = ELFW(ST_TYPE) (o_info);
1387 int o_binding = ELFW(ST_BIND) (o_info);
1389 /* A redefinition! Is it legal? */
1391 if (secidx == SHN_UNDEF)
1393 else if (o_secidx == SHN_UNDEF)
1395 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1396 /* Cope with local and global symbols of the same name
1397 in the same object file, as might have been created
1398 by ld -r. The only reason locals are now seen at this
1399 level at all is so that we can do semi-sensible things
1402 struct obj_symbol *nsym, **p;
1404 nsym = arch_new_symbol();
1405 nsym->next = sym->next;
1408 /* Excise the old (local) symbol from the hash chain. */
1409 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1413 } else if (n_binding == STB_LOCAL) {
1414 /* Another symbol of the same name has already been defined.
1415 Just add this to the local table. */
1416 sym = arch_new_symbol();
1419 f->local_symtab[symidx] = sym;
1421 } else if (n_binding == STB_WEAK)
1423 else if (o_binding == STB_WEAK)
1425 /* Don't unify COMMON symbols with object types the programmer
1427 else if (secidx == SHN_COMMON
1428 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1430 else if (o_secidx == SHN_COMMON
1431 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1434 /* Don't report an error if the symbol is coming from
1435 the kernel or some external module. */
1436 if (secidx <= SHN_HIRESERVE)
1437 error_msg("%s multiply defined", name);
1442 /* Completely new symbol. */
1443 sym = arch_new_symbol();
1444 sym->next = f->symtab[hash];
1445 f->symtab[hash] = sym;
1448 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1449 if (symidx >= f->local_symtab_size)
1450 error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1451 name, (long) symidx, (long) f->local_symtab_size);
1453 f->local_symtab[symidx] = sym;
1460 sym->secidx = secidx;
1466 static struct obj_symbol *
1467 obj_find_symbol(struct obj_file *f, const char *name)
1469 struct obj_symbol *sym;
1470 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1472 for (sym = f->symtab[hash]; sym; sym = sym->next)
1473 if (f->symbol_cmp(sym->name, name) == 0)
1480 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1483 if (sym->secidx >= SHN_LORESERVE)
1486 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1488 /* As a special case, a NULL sym has value zero. */
1493 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1495 int i, n = f->header.e_shnum;
1497 for (i = 0; i < n; ++i)
1498 if (strcmp(f->sections[i]->name, name) == 0)
1499 return f->sections[i];
1504 static int obj_load_order_prio(struct obj_section *a)
1506 unsigned long af, ac;
1508 af = a->header.sh_flags;
1511 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1512 strcmp(a->name + 5, ".init"))
1516 if (!(af & SHF_WRITE))
1518 if (af & SHF_EXECINSTR)
1520 if (a->header.sh_type != SHT_NOBITS)
1527 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1529 struct obj_section **p;
1530 int prio = obj_load_order_prio(sec);
1531 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1532 if (obj_load_order_prio(*p) < prio)
1534 sec->load_next = *p;
1538 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1540 unsigned long align,
1543 int newidx = f->header.e_shnum++;
1544 struct obj_section *sec;
1546 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1547 f->sections[newidx] = sec = arch_new_section();
1549 memset(sec, 0, sizeof(*sec));
1550 sec->header.sh_type = SHT_PROGBITS;
1551 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1552 sec->header.sh_size = size;
1553 sec->header.sh_addralign = align;
1557 sec->contents = xmalloc(size);
1559 obj_insert_section_load_order(f, sec);
1564 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1566 unsigned long align,
1569 int newidx = f->header.e_shnum++;
1570 struct obj_section *sec;
1572 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1573 f->sections[newidx] = sec = arch_new_section();
1575 memset(sec, 0, sizeof(*sec));
1576 sec->header.sh_type = SHT_PROGBITS;
1577 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1578 sec->header.sh_size = size;
1579 sec->header.sh_addralign = align;
1583 sec->contents = xmalloc(size);
1585 sec->load_next = f->load_order;
1586 f->load_order = sec;
1587 if (f->load_order_search_start == &f->load_order)
1588 f->load_order_search_start = &sec->load_next;
1593 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1595 unsigned long oldsize = sec->header.sh_size;
1597 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1599 return sec->contents + oldsize;
1603 /* Conditionally add the symbols from the given symbol set to the
1609 int idx, struct new_module_symbol *syms, size_t nsyms)
1611 struct new_module_symbol *s;
1615 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1617 /* Only add symbols that are already marked external. If we
1618 override locals we may cause problems for argument initialization.
1619 We will also create a false dependency on the module. */
1620 struct obj_symbol *sym;
1622 sym = obj_find_symbol(f, (char *) s->name);
1623 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1624 sym = obj_add_symbol(f, (char *) s->name, -1,
1625 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1627 /* Did our symbol just get installed? If so, mark the
1628 module as "used". */
1629 if (sym->secidx == idx)
1637 static void add_kernel_symbols(struct obj_file *f)
1639 struct external_module *m;
1642 /* Add module symbols first. */
1644 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1646 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1647 m->nsyms)) m->used = 1, ++nused;
1649 n_ext_modules_used = nused;
1651 /* And finally the symbols from the kernel proper. */
1654 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1657 static char *get_modinfo_value(struct obj_file *f, const char *key)
1659 struct obj_section *sec;
1660 char *p, *v, *n, *ep;
1661 size_t klen = strlen(key);
1663 sec = obj_find_section(f, ".modinfo");
1667 ep = p + sec->header.sh_size;
1670 n = strchr(p, '\0');
1672 if (p + klen == v && strncmp(p, key, klen) == 0)
1675 if (p + klen == n && strcmp(p, key) == 0)
1685 /*======================================================================*/
1686 /* Functions relating to module loading in pre 2.1 kernels. */
1689 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1693 struct obj_symbol *sym;
1697 if ((q = strchr(p, '=')) == NULL) {
1703 sym = obj_find_symbol(f, p);
1705 /* Also check that the parameter was not resolved from the kernel. */
1706 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1707 error_msg("symbol for parameter %s not found", p);
1711 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1713 /* Do C quoting if we begin with a ". */
1717 str = alloca(strlen(q));
1718 for (r = str, q++; *q != '"'; ++q, ++r) {
1720 error_msg("improperly terminated string argument for %s", p);
1722 } else if (*q == '\\')
1756 if (q[1] >= '0' && q[1] <= '7') {
1757 c = (c * 8) + *++q - '0';
1758 if (q[1] >= '0' && q[1] <= '7')
1759 c = (c * 8) + *++q - '0';
1772 obj_string_patch(f, sym->secidx, sym->value, str);
1773 } else if (*q >= '0' && *q <= '9') {
1775 *loc++ = strtoul(q, &q, 0);
1776 while (*q++ == ',');
1778 char *contents = f->sections[sym->secidx]->contents;
1779 char *myloc = contents + sym->value;
1780 char *r; /* To search for commas */
1782 /* Break the string with comas */
1783 while ((r = strchr(q, ',')) != (char *) NULL) {
1785 obj_string_patch(f, sym->secidx, myloc - contents, q);
1786 myloc += sizeof(char *);
1791 obj_string_patch(f, sym->secidx, myloc - contents, q);
1800 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1801 static int old_is_module_checksummed(struct obj_file *f)
1803 return obj_find_symbol(f, "Using_Versions") != NULL;
1805 /* Get the module's kernel version in the canonical integer form. */
1808 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1810 struct obj_symbol *sym;
1814 sym = obj_find_symbol(f, "kernel_version");
1818 p = f->sections[sym->secidx]->contents + sym->value;
1819 strncpy(str, p, STRVERSIONLEN);
1821 a = strtoul(p, &p, 10);
1824 b = strtoul(p + 1, &p, 10);
1827 c = strtoul(p + 1, &q, 10);
1831 return a << 16 | b << 8 | c;
1834 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1836 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
1838 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1840 static int old_get_kernel_symbols(const char *m_name)
1842 struct old_kernel_sym *ks, *k;
1843 struct new_module_symbol *s;
1844 struct external_module *mod;
1845 int nks, nms, nmod, i;
1847 nks = get_kernel_syms(NULL);
1850 perror_msg("get_kernel_syms: %s", m_name);
1852 error_msg("No kernel symbols");
1856 ks = k = xmalloc(nks * sizeof(*ks));
1858 if (get_kernel_syms(ks) != nks) {
1859 perror("inconsistency with get_kernel_syms -- is someone else "
1860 "playing with modules?");
1865 /* Collect the module information. */
1870 while (k->name[0] == '#' && k->name[1]) {
1871 struct old_kernel_sym *k2;
1873 /* Find out how many symbols this module has. */
1874 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1878 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1879 mod[nmod].name = k->name + 1;
1880 mod[nmod].addr = k->value;
1882 mod[nmod].nsyms = nms;
1883 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1885 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1886 s->name = (unsigned long) k->name;
1887 s->value = k->value;
1894 n_ext_modules = nmod + 1;
1896 /* Now collect the symbols for the kernel proper. */
1898 if (k->name[0] == '#')
1901 nksyms = nms = nks - (k - ks);
1902 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1904 for (i = 0; i < nms; ++i, ++s, ++k) {
1905 s->name = (unsigned long) k->name;
1906 s->value = k->value;
1912 /* Return the kernel symbol checksum version, or zero if not used. */
1914 static int old_is_kernel_checksummed(void)
1916 /* Using_Versions is the first symbol. */
1918 && strcmp((char *) ksyms[0].name,
1919 "Using_Versions") == 0) return ksyms[0].value;
1925 static int old_create_mod_use_count(struct obj_file *f)
1927 struct obj_section *sec;
1929 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1932 obj_add_symbol(f, "mod_use_count_", -1,
1933 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1940 old_init_module(const char *m_name, struct obj_file *f,
1941 unsigned long m_size)
1944 struct old_mod_routines routines;
1945 struct old_symbol_table *symtab;
1948 /* Create the symbol table */
1950 int nsyms = 0, strsize = 0, total;
1952 /* Size things first... */
1955 for (i = 0; i < HASH_BUCKETS; ++i) {
1956 struct obj_symbol *sym;
1957 for (sym = f->symtab[i]; sym; sym = sym->next)
1958 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1959 && sym->secidx <= SHN_HIRESERVE)
1961 sym->ksymidx = nsyms++;
1962 strsize += strlen(sym->name) + 1;
1967 total = (sizeof(struct old_symbol_table)
1968 + nsyms * sizeof(struct old_module_symbol)
1969 + n_ext_modules_used * sizeof(struct old_module_ref)
1971 symtab = xmalloc(total);
1972 symtab->size = total;
1973 symtab->n_symbols = nsyms;
1974 symtab->n_refs = n_ext_modules_used;
1976 if (flag_export && nsyms) {
1977 struct old_module_symbol *ksym;
1981 ksym = symtab->symbol;
1982 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1983 + n_ext_modules_used * sizeof(struct old_module_ref));
1985 for (i = 0; i < HASH_BUCKETS; ++i) {
1986 struct obj_symbol *sym;
1987 for (sym = f->symtab[i]; sym; sym = sym->next)
1988 if (sym->ksymidx >= 0) {
1989 ksym->addr = obj_symbol_final_value(f, sym);
1991 (unsigned long) str - (unsigned long) symtab;
1993 strcpy(str, sym->name);
1994 str += strlen(sym->name) + 1;
2000 if (n_ext_modules_used) {
2001 struct old_module_ref *ref;
2004 ref = (struct old_module_ref *)
2005 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2007 for (i = 0; i < n_ext_modules; ++i)
2008 if (ext_modules[i].used)
2009 ref++->module = ext_modules[i].addr;
2013 /* Fill in routines. */
2016 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2018 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2020 /* Whew! All of the initialization is complete. Collect the final
2021 module image and give it to the kernel. */
2023 image = xmalloc(m_size);
2024 obj_create_image(f, image);
2026 /* image holds the complete relocated module, accounting correctly for
2027 mod_use_count. However the old module kernel support assume that
2028 it is receiving something which does not contain mod_use_count. */
2029 ret = old_sys_init_module(m_name, image + sizeof(long),
2030 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2031 : 0), &routines, symtab);
2033 perror_msg("init_module: %s", m_name);
2043 #define old_create_mod_use_count(x) TRUE
2044 #define old_init_module(x, y, z) TRUE
2046 #endif /* BB_FEATURE_OLD_MODULE_INTERFACE */
2050 /*======================================================================*/
2051 /* Functions relating to module loading after 2.1.18. */
2054 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2058 struct obj_symbol *sym;
2059 char *contents, *loc;
2063 if ((q = strchr(p, '=')) == NULL) {
2068 key = alloca(q - p + 6);
2069 memcpy(key, "parm_", 5);
2070 memcpy(key + 5, p, q - p);
2073 p = get_modinfo_value(f, key);
2076 error_msg("invalid parameter %s", key);
2080 sym = obj_find_symbol(f, key);
2082 /* Also check that the parameter was not resolved from the kernel. */
2083 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2084 error_msg("symbol for parameter %s not found", key);
2089 min = strtoul(p, &p, 10);
2091 max = strtoul(p + 1, &p, 10);
2097 contents = f->sections[sym->secidx]->contents;
2098 loc = contents + sym->value;
2102 if ((*p == 's') || (*p == 'c')) {
2105 /* Do C quoting if we begin with a ", else slurp the lot. */
2109 str = alloca(strlen(q));
2110 for (r = str, q++; *q != '"'; ++q, ++r) {
2112 error_msg("improperly terminated string argument for %s",
2115 } else if (*q == '\\')
2149 if (q[1] >= '0' && q[1] <= '7') {
2150 c = (c * 8) + *++q - '0';
2151 if (q[1] >= '0' && q[1] <= '7')
2152 c = (c * 8) + *++q - '0';
2169 /* In this case, the string is not quoted. We will break
2170 it using the coma (like for ints). If the user wants to
2171 include comas in a string, he just has to quote it */
2173 /* Search the next coma */
2177 if (r != (char *) NULL) {
2178 /* Recopy the current field */
2179 str = alloca(r - q + 1);
2180 memcpy(str, q, r - q);
2182 /* I don't know if it is usefull, as the previous case
2183 doesn't null terminate the string ??? */
2186 /* Keep next fields */
2197 obj_string_patch(f, sym->secidx, loc - contents, str);
2198 loc += tgt_sizeof_char_p;
2200 /* Array of chars (in fact, matrix !) */
2201 unsigned long charssize; /* size of each member */
2203 /* Get the size of each member */
2204 /* Probably we should do that outside the loop ? */
2205 if (!isdigit(*(p + 1))) {
2206 error_msg("parameter type 'c' for %s must be followed by"
2207 " the maximum size", key);
2210 charssize = strtoul(p + 1, (char **) NULL, 10);
2213 if (strlen(str) >= charssize) {
2214 error_msg("string too long for %s (max %ld)", key,
2219 /* Copy to location */
2220 strcpy((char *) loc, str);
2224 long v = strtoul(q, &q, 0);
2231 loc += tgt_sizeof_short;
2235 loc += tgt_sizeof_int;
2239 loc += tgt_sizeof_long;
2243 error_msg("unknown parameter type '%c' for %s", *p, key);
2258 goto retry_end_of_value;
2262 error_msg("too many values for %s (max %d)", key, max);
2269 error_msg("invalid argument syntax for %s", key);
2276 error_msg("too few values for %s (min %d)", key, min);
2286 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2287 static int new_is_module_checksummed(struct obj_file *f)
2289 const char *p = get_modinfo_value(f, "using_checksums");
2296 /* Get the module's kernel version in the canonical integer form. */
2299 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2304 p = get_modinfo_value(f, "kernel_version");
2307 strncpy(str, p, STRVERSIONLEN);
2309 a = strtoul(p, &p, 10);
2312 b = strtoul(p + 1, &p, 10);
2315 c = strtoul(p + 1, &q, 10);
2319 return a << 16 | b << 8 | c;
2322 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2325 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
2327 /* Fetch the loaded modules, and all currently exported symbols. */
2329 static int new_get_kernel_symbols(void)
2331 char *module_names, *mn;
2332 struct external_module *modules, *m;
2333 struct new_module_symbol *syms, *s;
2334 size_t ret, bufsize, nmod, nsyms, i, j;
2336 /* Collect the loaded modules. */
2338 module_names = xmalloc(bufsize = 256);
2340 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2341 if (errno == ENOSPC && bufsize < ret) {
2342 module_names = xrealloc(module_names, bufsize = ret);
2343 goto retry_modules_load;
2345 perror_msg("QM_MODULES");
2349 n_ext_modules = nmod = ret;
2351 /* Collect the modules' symbols. */
2354 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2355 memset(modules, 0, nmod * sizeof(*modules));
2356 for (i = 0, mn = module_names, m = modules;
2357 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2358 struct new_module_info info;
2360 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2361 if (errno == ENOENT) {
2362 /* The module was removed out from underneath us. */
2365 perror_msg("query_module: QM_INFO: %s", mn);
2369 syms = xmalloc(bufsize = 1024);
2371 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2374 syms = xrealloc(syms, bufsize = ret);
2375 goto retry_mod_sym_load;
2377 /* The module was removed out from underneath us. */
2380 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2387 m->addr = info.addr;
2391 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2392 s->name += (unsigned long) syms;
2397 /* Collect the kernel's symbols. */
2399 syms = xmalloc(bufsize = 16 * 1024);
2400 retry_kern_sym_load:
2401 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2402 if (errno == ENOSPC && bufsize < ret) {
2403 syms = xrealloc(syms, bufsize = ret);
2404 goto retry_kern_sym_load;
2406 perror_msg("kernel: QM_SYMBOLS");
2409 nksyms = nsyms = ret;
2412 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2413 s->name += (unsigned long) syms;
2419 /* Return the kernel symbol checksum version, or zero if not used. */
2421 static int new_is_kernel_checksummed(void)
2423 struct new_module_symbol *s;
2426 /* Using_Versions is not the first symbol, but it should be in there. */
2428 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2429 if (strcmp((char *) s->name, "Using_Versions") == 0)
2436 static int new_create_this_module(struct obj_file *f, const char *m_name)
2438 struct obj_section *sec;
2440 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2441 sizeof(struct new_module));
2442 memset(sec->contents, 0, sizeof(struct new_module));
2444 obj_add_symbol(f, "__this_module", -1,
2445 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2446 sizeof(struct new_module));
2448 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2455 static int new_create_module_ksymtab(struct obj_file *f)
2457 struct obj_section *sec;
2460 /* We must always add the module references. */
2462 if (n_ext_modules_used) {
2463 struct new_module_ref *dep;
2464 struct obj_symbol *tm;
2466 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2467 (sizeof(struct new_module_ref)
2468 * n_ext_modules_used));
2472 tm = obj_find_symbol(f, "__this_module");
2473 dep = (struct new_module_ref *) sec->contents;
2474 for (i = 0; i < n_ext_modules; ++i)
2475 if (ext_modules[i].used) {
2476 dep->dep = ext_modules[i].addr;
2477 obj_symbol_patch(f, sec->idx,
2478 (char *) &dep->ref - sec->contents, tm);
2484 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2489 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2492 /* We don't want to export symbols residing in sections that
2493 aren't loaded. There are a number of these created so that
2494 we make sure certain module options don't appear twice. */
2496 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2498 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2500 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2501 struct obj_symbol *sym;
2502 for (sym = f->symtab[i]; sym; sym = sym->next)
2503 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2504 && sym->secidx <= SHN_HIRESERVE
2505 && (sym->secidx >= SHN_LORESERVE
2506 || loaded[sym->secidx])) {
2507 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2509 obj_symbol_patch(f, sec->idx, ofs, sym);
2510 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2517 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2525 new_init_module(const char *m_name, struct obj_file *f,
2526 unsigned long m_size)
2528 struct new_module *module;
2529 struct obj_section *sec;
2534 sec = obj_find_section(f, ".this");
2535 if (!sec || !sec->contents) {
2536 perror_msg_and_die("corrupt module %s?",m_name);
2538 module = (struct new_module *) sec->contents;
2539 m_addr = sec->header.sh_addr;
2541 module->size_of_struct = sizeof(*module);
2542 module->size = m_size;
2543 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2545 sec = obj_find_section(f, "__ksymtab");
2546 if (sec && sec->header.sh_size) {
2547 module->syms = sec->header.sh_addr;
2548 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2551 if (n_ext_modules_used) {
2552 sec = obj_find_section(f, ".kmodtab");
2553 module->deps = sec->header.sh_addr;
2554 module->ndeps = n_ext_modules_used;
2558 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2560 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2562 sec = obj_find_section(f, "__ex_table");
2564 module->ex_table_start = sec->header.sh_addr;
2565 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2568 sec = obj_find_section(f, ".text.init");
2570 module->runsize = sec->header.sh_addr - m_addr;
2572 sec = obj_find_section(f, ".data.init");
2574 if (!module->runsize ||
2575 module->runsize > sec->header.sh_addr - m_addr)
2576 module->runsize = sec->header.sh_addr - m_addr;
2578 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2579 if (sec && sec->header.sh_size) {
2580 module->archdata_start = (void*)sec->header.sh_addr;
2581 module->archdata_end = module->archdata_start + sec->header.sh_size;
2583 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2584 if (sec && sec->header.sh_size) {
2585 module->kallsyms_start = (void*)sec->header.sh_addr;
2586 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2589 if (!arch_init_module(f, module))
2592 /* Whew! All of the initialization is complete. Collect the final
2593 module image and give it to the kernel. */
2595 image = xmalloc(m_size);
2596 obj_create_image(f, image);
2598 ret = new_sys_init_module(m_name, (struct new_module *) image);
2600 perror_msg("init_module: %s", m_name);
2609 #define new_init_module(x, y, z) TRUE
2610 #define new_create_this_module(x, y) 0
2611 #define new_create_module_ksymtab(x)
2612 #define query_module(v, w, x, y, z) -1
2614 #endif /* BB_FEATURE_NEW_MODULE_INTERFACE */
2617 /*======================================================================*/
2620 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2623 struct obj_string_patch *p;
2624 struct obj_section *strsec;
2625 size_t len = strlen(string) + 1;
2628 p = xmalloc(sizeof(*p));
2629 p->next = f->string_patches;
2630 p->reloc_secidx = secidx;
2631 p->reloc_offset = offset;
2632 f->string_patches = p;
2634 strsec = obj_find_section(f, ".kstrtab");
2635 if (strsec == NULL) {
2636 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2637 p->string_offset = 0;
2638 loc = strsec->contents;
2640 p->string_offset = strsec->header.sh_size;
2641 loc = obj_extend_section(strsec, len);
2643 memcpy(loc, string, len);
2649 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2650 struct obj_symbol *sym)
2652 struct obj_symbol_patch *p;
2654 p = xmalloc(sizeof(*p));
2655 p->next = f->symbol_patches;
2656 p->reloc_secidx = secidx;
2657 p->reloc_offset = offset;
2659 f->symbol_patches = p;
2664 static int obj_check_undefineds(struct obj_file *f)
2669 for (i = 0; i < HASH_BUCKETS; ++i) {
2670 struct obj_symbol *sym;
2671 for (sym = f->symtab[i]; sym; sym = sym->next)
2672 if (sym->secidx == SHN_UNDEF) {
2673 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2674 sym->secidx = SHN_ABS;
2677 error_msg("unresolved symbol %s", sym->name);
2686 static void obj_allocate_commons(struct obj_file *f)
2688 struct common_entry {
2689 struct common_entry *next;
2690 struct obj_symbol *sym;
2691 } *common_head = NULL;
2695 for (i = 0; i < HASH_BUCKETS; ++i) {
2696 struct obj_symbol *sym;
2697 for (sym = f->symtab[i]; sym; sym = sym->next)
2698 if (sym->secidx == SHN_COMMON) {
2699 /* Collect all COMMON symbols and sort them by size so as to
2700 minimize space wasted by alignment requirements. */
2702 struct common_entry **p, *n;
2703 for (p = &common_head; *p; p = &(*p)->next)
2704 if (sym->size <= (*p)->sym->size)
2707 n = alloca(sizeof(*n));
2715 for (i = 1; i < f->local_symtab_size; ++i) {
2716 struct obj_symbol *sym = f->local_symtab[i];
2717 if (sym && sym->secidx == SHN_COMMON) {
2718 struct common_entry **p, *n;
2719 for (p = &common_head; *p; p = &(*p)->next)
2720 if (sym == (*p)->sym)
2722 else if (sym->size < (*p)->sym->size) {
2723 n = alloca(sizeof(*n));
2733 /* Find the bss section. */
2734 for (i = 0; i < f->header.e_shnum; ++i)
2735 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2738 /* If for some reason there hadn't been one, create one. */
2739 if (i == f->header.e_shnum) {
2740 struct obj_section *sec;
2742 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2743 f->sections[i] = sec = arch_new_section();
2744 f->header.e_shnum = i + 1;
2746 memset(sec, 0, sizeof(*sec));
2747 sec->header.sh_type = SHT_PROGBITS;
2748 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2753 /* Allocate the COMMONS. */
2755 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2756 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2757 struct common_entry *c;
2759 for (c = common_head; c; c = c->next) {
2760 ElfW(Addr) align = c->sym->value;
2762 if (align > max_align)
2764 if (bss_size & (align - 1))
2765 bss_size = (bss_size | (align - 1)) + 1;
2768 c->sym->value = bss_size;
2770 bss_size += c->sym->size;
2773 f->sections[i]->header.sh_size = bss_size;
2774 f->sections[i]->header.sh_addralign = max_align;
2778 /* For the sake of patch relocation and parameter initialization,
2779 allocate zeroed data for NOBITS sections now. Note that after
2780 this we cannot assume NOBITS are really empty. */
2781 for (i = 0; i < f->header.e_shnum; ++i) {
2782 struct obj_section *s = f->sections[i];
2783 if (s->header.sh_type == SHT_NOBITS) {
2784 if (s->header.sh_size != 0)
2785 s->contents = memset(xmalloc(s->header.sh_size),
2786 0, s->header.sh_size);
2790 s->header.sh_type = SHT_PROGBITS;
2795 static unsigned long obj_load_size(struct obj_file *f)
2797 unsigned long dot = 0;
2798 struct obj_section *sec;
2800 /* Finalize the positions of the sections relative to one another. */
2802 for (sec = f->load_order; sec; sec = sec->load_next) {
2805 align = sec->header.sh_addralign;
2806 if (align && (dot & (align - 1)))
2807 dot = (dot | (align - 1)) + 1;
2809 sec->header.sh_addr = dot;
2810 dot += sec->header.sh_size;
2816 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2818 int i, n = f->header.e_shnum;
2821 /* Finalize the addresses of the sections. */
2824 for (i = 0; i < n; ++i)
2825 f->sections[i]->header.sh_addr += base;
2827 /* And iterate over all of the relocations. */
2829 for (i = 0; i < n; ++i) {
2830 struct obj_section *relsec, *symsec, *targsec, *strsec;
2831 ElfW(RelM) * rel, *relend;
2835 relsec = f->sections[i];
2836 if (relsec->header.sh_type != SHT_RELM)
2839 symsec = f->sections[relsec->header.sh_link];
2840 targsec = f->sections[relsec->header.sh_info];
2841 strsec = f->sections[symsec->header.sh_link];
2843 rel = (ElfW(RelM) *) relsec->contents;
2844 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2845 symtab = (ElfW(Sym) *) symsec->contents;
2846 strtab = (const char *) strsec->contents;
2848 for (; rel < relend; ++rel) {
2849 ElfW(Addr) value = 0;
2850 struct obj_symbol *intsym = NULL;
2851 unsigned long symndx;
2852 ElfW(Sym) * extsym = 0;
2855 /* Attempt to find a value to use for this relocation. */
2857 symndx = ELFW(R_SYM) (rel->r_info);
2859 /* Note we've already checked for undefined symbols. */
2861 extsym = &symtab[symndx];
2862 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2863 /* Local symbols we look up in the local table to be sure
2864 we get the one that is really intended. */
2865 intsym = f->local_symtab[symndx];
2867 /* Others we look up in the hash table. */
2869 if (extsym->st_name)
2870 name = strtab + extsym->st_name;
2872 name = f->sections[extsym->st_shndx]->name;
2873 intsym = obj_find_symbol(f, name);
2876 value = obj_symbol_final_value(f, intsym);
2877 intsym->referenced = 1;
2879 #if SHT_RELM == SHT_RELA
2880 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2881 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2882 if (!extsym || !extsym->st_name ||
2883 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2885 value += rel->r_addend;
2889 switch (arch_apply_relocation
2890 (f, targsec, symsec, intsym, rel, value)) {
2894 case obj_reloc_overflow:
2895 errmsg = "Relocation overflow";
2897 case obj_reloc_dangerous:
2898 errmsg = "Dangerous relocation";
2900 case obj_reloc_unhandled:
2901 errmsg = "Unhandled relocation";
2904 error_msg("%s of type %ld for %s", errmsg,
2905 (long) ELFW(R_TYPE) (rel->r_info),
2906 strtab + extsym->st_name);
2908 error_msg("%s of type %ld", errmsg,
2909 (long) ELFW(R_TYPE) (rel->r_info));
2917 /* Finally, take care of the patches. */
2919 if (f->string_patches) {
2920 struct obj_string_patch *p;
2921 struct obj_section *strsec;
2922 ElfW(Addr) strsec_base;
2923 strsec = obj_find_section(f, ".kstrtab");
2924 strsec_base = strsec->header.sh_addr;
2926 for (p = f->string_patches; p; p = p->next) {
2927 struct obj_section *targsec = f->sections[p->reloc_secidx];
2928 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2929 = strsec_base + p->string_offset;
2933 if (f->symbol_patches) {
2934 struct obj_symbol_patch *p;
2936 for (p = f->symbol_patches; p; p = p->next) {
2937 struct obj_section *targsec = f->sections[p->reloc_secidx];
2938 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2939 = obj_symbol_final_value(f, p->sym);
2946 static int obj_create_image(struct obj_file *f, char *image)
2948 struct obj_section *sec;
2949 ElfW(Addr) base = f->baseaddr;
2951 for (sec = f->load_order; sec; sec = sec->load_next) {
2954 if (sec->contents == 0 || sec->header.sh_size == 0)
2957 secimg = image + (sec->header.sh_addr - base);
2959 /* Note that we allocated data for NOBITS sections earlier. */
2960 memcpy(secimg, sec->contents, sec->header.sh_size);
2966 /*======================================================================*/
2968 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
2971 ElfW(Shdr) * section_headers;
2975 /* Read the file header. */
2977 f = arch_new_file();
2978 memset(f, 0, sizeof(*f));
2979 f->symbol_cmp = strcmp;
2980 f->symbol_hash = obj_elf_hash;
2981 f->load_order_search_start = &f->load_order;
2983 fseek(fp, 0, SEEK_SET);
2984 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2985 perror_msg("error reading ELF header");
2989 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2990 || f->header.e_ident[EI_MAG1] != ELFMAG1
2991 || f->header.e_ident[EI_MAG2] != ELFMAG2
2992 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2993 error_msg("not an ELF file");
2996 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2997 || f->header.e_ident[EI_DATA] != ELFDATAM
2998 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2999 || !MATCH_MACHINE(f->header.e_machine)) {
3000 error_msg("ELF file not for this architecture");
3003 if (f->header.e_type != ET_REL) {
3004 error_msg("ELF file not a relocatable object");
3008 /* Read the section headers. */
3010 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3011 error_msg("section header size mismatch: %lu != %lu",
3012 (unsigned long) f->header.e_shentsize,
3013 (unsigned long) sizeof(ElfW(Shdr)));
3017 shnum = f->header.e_shnum;
3018 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3019 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3021 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3022 fseek(fp, f->header.e_shoff, SEEK_SET);
3023 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3024 perror_msg("error reading ELF section headers");
3028 /* Read the section data. */
3030 for (i = 0; i < shnum; ++i) {
3031 struct obj_section *sec;
3033 f->sections[i] = sec = arch_new_section();
3034 memset(sec, 0, sizeof(*sec));
3036 sec->header = section_headers[i];
3039 if(sec->header.sh_size) switch (sec->header.sh_type) {
3048 if (!loadprogbits) {
3049 sec->contents = NULL;
3056 if (sec->header.sh_size > 0) {
3057 sec->contents = xmalloc(sec->header.sh_size);
3058 fseek(fp, sec->header.sh_offset, SEEK_SET);
3059 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3060 perror_msg("error reading ELF section data");
3064 sec->contents = NULL;
3068 #if SHT_RELM == SHT_REL
3070 error_msg("RELA relocations not supported on this architecture");
3074 error_msg("REL relocations not supported on this architecture");
3079 if (sec->header.sh_type >= SHT_LOPROC) {
3080 /* Assume processor specific section types are debug
3081 info and can safely be ignored. If this is ever not
3082 the case (Hello MIPS?), don't put ifdefs here but
3083 create an arch_load_proc_section(). */
3087 error_msg("can't handle sections of type %ld",
3088 (long) sec->header.sh_type);
3093 /* Do what sort of interpretation as needed by each section. */
3095 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3097 for (i = 0; i < shnum; ++i) {
3098 struct obj_section *sec = f->sections[i];
3099 sec->name = shstrtab + sec->header.sh_name;
3102 for (i = 0; i < shnum; ++i) {
3103 struct obj_section *sec = f->sections[i];
3105 /* .modinfo should be contents only but gcc has no attribute for that.
3106 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3108 if (strcmp(sec->name, ".modinfo") == 0)
3109 sec->header.sh_flags &= ~SHF_ALLOC;
3111 if (sec->header.sh_flags & SHF_ALLOC)
3112 obj_insert_section_load_order(f, sec);
3114 switch (sec->header.sh_type) {
3117 unsigned long nsym, j;
3121 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3122 error_msg("symbol size mismatch: %lu != %lu",
3123 (unsigned long) sec->header.sh_entsize,
3124 (unsigned long) sizeof(ElfW(Sym)));
3128 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3129 strtab = f->sections[sec->header.sh_link]->contents;
3130 sym = (ElfW(Sym) *) sec->contents;
3132 /* Allocate space for a table of local symbols. */
3133 j = f->local_symtab_size = sec->header.sh_info;
3134 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3136 /* Insert all symbols into the hash table. */
3137 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3140 name = strtab + sym->st_name;
3142 name = f->sections[sym->st_shndx]->name;
3144 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3145 sym->st_value, sym->st_size);
3151 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3152 error_msg("relocation entry size mismatch: %lu != %lu",
3153 (unsigned long) sec->header.sh_entsize,
3154 (unsigned long) sizeof(ElfW(RelM)));
3158 /* XXX Relocation code from modutils-2.3.19 is not here.
3159 * Why? That's about 20 lines of code from obj/obj_load.c,
3160 * which gets done in a second pass through the sections.
3161 * This BusyBox insmod does similar work in obj_relocate(). */
3168 #ifdef BB_FEATURE_INSMOD_LOADINKMEM
3170 * load the unloaded sections directly into the memory allocated by
3171 * kernel for the module
3174 static int obj_load_progbits(FILE * fp, struct obj_file* f)
3176 char* imagebase = (char*) f->imagebase;
3177 ElfW(Addr) base = f->baseaddr;
3178 struct obj_section* sec;
3180 for (sec = f->load_order; sec; sec = sec->load_next) {
3182 /* section already loaded? */
3183 if (sec->contents != NULL)
3186 if (sec->header.sh_size == 0)
3189 sec->contents = imagebase + (sec->header.sh_addr - base);
3190 fseek(fp, sec->header.sh_offset, SEEK_SET);
3191 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3192 errorMsg("error reading ELF section data: %s\n", strerror(errno));
3201 static void hide_special_symbols(struct obj_file *f)
3203 static const char *const specials[] = {
3210 struct obj_symbol *sym;
3211 const char *const *p;
3213 for (p = specials; *p; ++p)
3214 if ((sym = obj_find_symbol(f, *p)) != NULL)
3216 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3221 extern int insmod_main( int argc, char **argv)
3228 unsigned long m_size;
3233 char m_name[FILENAME_MAX + 1] = "\0";
3234 int exit_status = EXIT_FAILURE;
3236 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3238 char k_strversion[STRVERSIONLEN];
3239 char m_strversion[STRVERSIONLEN];
3244 /* Parse any options */
3245 while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
3247 case 'f': /* force loading */
3248 flag_force_load = 1;
3250 case 'k': /* module loaded by kerneld, auto-cleanable */
3253 case 'v': /* verbose output */
3256 case 'x': /* do not export externs */
3259 case 'o': /* name the output module */
3260 strncpy(m_name, optarg, FILENAME_MAX);
3262 case 'L': /* Stub warning */
3263 /* This is needed for compatibility with modprobe.
3264 * In theory, this does locking, but we don't do
3265 * that. So be careful and plan your life around not
3266 * loading the same module 50 times concurrently. */
3273 if (argv[optind] == NULL) {
3277 /* Grab the module name */
3278 if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3285 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3287 memcpy(m_fullName, tmp, len);
3288 m_fullName[len]='\0';
3289 if (*m_name == '\0') {
3290 strcpy(m_name, m_fullName);
3292 strcat(m_fullName, ".o");
3294 /* Get a filedesc for the module. Check we we have a complete path */
3295 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3296 (fp = fopen(argv[optind], "r")) == NULL) {
3297 struct utsname myuname;
3299 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3300 * but do not error out yet if we fail to find it... */
3301 if (uname(&myuname) == 0) {
3302 char module_dir[FILENAME_MAX];
3303 char real_module_dir[FILENAME_MAX];
3304 snprintf (module_dir, sizeof(module_dir), "%s/%s",
3305 _PATH_MODULES, myuname.release);
3306 /* Jump through hoops in case /lib/modules/`uname -r`
3307 * is a symlink. We do not want recursive_action to
3308 * follow symlinks, but we do want to follow the
3309 * /lib/modules/`uname -r` dir, So resolve it ourselves
3310 * if it is a link... */
3311 if (realpath (module_dir, real_module_dir) == NULL)
3312 strcpy(real_module_dir, module_dir);
3313 recursive_action(real_module_dir, TRUE, FALSE, FALSE,
3314 check_module_name_match, 0, m_fullName);
3317 /* Check if we have found anything yet */
3318 if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL))
3320 char module_dir[FILENAME_MAX];
3321 if (realpath (_PATH_MODULES, module_dir) == NULL)
3322 strcpy(module_dir, _PATH_MODULES);
3323 /* No module found under /lib/modules/`uname -r`, this
3324 * time cast the net a bit wider. Search /lib/modules/ */
3325 if (recursive_action(module_dir, TRUE, FALSE, FALSE,
3326 check_module_name_match, 0, m_fullName) == FALSE)
3328 if (m_filename[0] == '\0'
3329 || ((fp = fopen(m_filename, "r")) == NULL))
3331 error_msg("%s: no module by that name found", m_fullName);
3332 return EXIT_FAILURE;
3335 error_msg_and_die("%s: no module by that name found", m_fullName);
3338 safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
3340 printf("Using %s\n", m_filename);
3342 if ((f = obj_load(fp, LOADBITS)) == NULL)
3343 perror_msg_and_die("Could not load the module");
3345 if (get_modinfo_value(f, "kernel_version") == NULL)
3350 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3351 /* Version correspondence? */
3353 k_version = get_kernel_version(k_strversion);
3354 if (m_has_modinfo) {
3355 m_version = new_get_module_version(f, m_strversion);
3357 m_version = old_get_module_version(f, m_strversion);
3358 if (m_version == -1) {
3359 error_msg("couldn't find the kernel version the module was "
3365 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
3366 if (flag_force_load) {
3367 error_msg("Warning: kernel-module version mismatch\n"
3368 "\t%s was compiled for kernel version %s\n"
3369 "\twhile this kernel is version %s",
3370 m_filename, m_strversion, k_strversion);
3372 error_msg("kernel-module version mismatch\n"
3373 "\t%s was compiled for kernel version %s\n"
3374 "\twhile this kernel is version %s.",
3375 m_filename, m_strversion, k_strversion);
3380 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3382 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3384 if (k_new_syscalls) {
3385 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
3386 if (!new_get_kernel_symbols())
3388 k_crcs = new_is_kernel_checksummed();
3390 error_msg("Not configured to support new kernels");
3394 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
3395 if (!old_get_kernel_symbols(m_name))
3397 k_crcs = old_is_kernel_checksummed();
3399 error_msg("Not configured to support old kernels");
3404 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3406 m_crcs = new_is_module_checksummed(f);
3408 m_crcs = old_is_module_checksummed(f);
3410 if (m_crcs != k_crcs)
3411 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3412 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3414 /* Let the module know about the kernel symbols. */
3415 add_kernel_symbols(f);
3417 /* Allocate common symbols, symbol tables, and string tables. */
3420 ? !new_create_this_module(f, m_name)
3421 : !old_create_mod_use_count(f))
3426 if (!obj_check_undefineds(f)) {
3429 obj_allocate_commons(f);
3431 /* done with the module name, on to the optional var=value arguments */
3434 if (optind < argc) {
3436 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3437 : !old_process_module_arguments(f, argc - optind, argv + optind))
3444 hide_special_symbols(f);
3447 new_create_module_ksymtab(f);
3449 /* Find current size of the module */
3450 m_size = obj_load_size(f);
3453 m_addr = create_module(m_name, m_size);
3454 if (m_addr==-1) switch (errno) {
3456 error_msg("A module named %s already exists", m_name);
3459 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3463 perror_msg("create_module: %s", m_name);
3469 * the PROGBITS section was not loaded by the obj_load
3470 * now we can load them directly into the kernel memory
3472 // f->imagebase = (char*) m_addr;
3473 f->imagebase = (ElfW(Addr)) m_addr;
3474 if (!obj_load_progbits(fp, f)) {
3475 delete_module(m_name);
3480 if (!obj_relocate(f, m_addr)) {
3481 delete_module(m_name);
3486 ? !new_init_module(m_name, f, m_size)
3487 : !old_init_module(m_name, f, m_size))
3489 delete_module(m_name);
3493 exit_status = EXIT_SUCCESS;
3497 return(exit_status);