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,
9 * Copyright (C) 1999,2000,2001 by Lineo, inc.
10 * Written by Erik Andersen <andersen@lineo.com>
11 * and Ron Alder <alder@lineo.com>
13 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
14 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
16 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
17 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
18 * very minor changes required to also work with StrongArm and presumably
19 * all ARM based systems.
21 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
22 * PowerPC specific code stolen from modutils-2.3.16,
23 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
24 * I've only tested the code on mpc8xx platforms in big-endian mode.
25 * Did some cleanup and added BB_USE_xxx_ENTRIES...
27 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
28 * based on modutils-2.4.2
29 * MIPS specific support for Elf loading and relocation.
30 * Copyright 1996, 1997 Linux International.
31 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
33 * Based almost entirely on the Linux modutils-2.3.11 implementation.
34 * Copyright 1996, 1997 Linux International.
35 * New implementation contributed by Richard Henderson <rth@tamu.edu>
36 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
37 * Restructured (and partly rewritten) by:
38 * Björn Ekwall <bj0rn@blox.se> February 1999
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version.
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
48 * General Public License for more details.
50 * You should have received a copy of the GNU General Public License
51 * along with this program; if not, write to the Free Software
52 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
66 #include <sys/utsname.h>
69 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
70 # undef BB_FEATURE_OLD_MODULE_INTERFACE
71 # define new_sys_init_module init_module
73 # define old_sys_init_module init_module
76 #ifdef BB_FEATURE_INSMOD_LOADINKMEM
82 #if defined(__powerpc__)
83 #define BB_USE_PLT_ENTRIES
84 #define BB_PLT_ENTRY_SIZE 16
88 #define BB_USE_PLT_ENTRIES
89 #define BB_PLT_ENTRY_SIZE 8
90 #define BB_USE_GOT_ENTRIES
91 #define BB_GOT_ENTRY_SIZE 8
95 #define BB_USE_GOT_ENTRIES
96 #define BB_GOT_ENTRY_SIZE 4
100 #define BB_USE_GOT_ENTRIES
101 #define BB_GOT_ENTRY_SIZE 4
104 #if defined(__mips__)
108 //----------------------------------------------------------------------------
109 //--------modutils module.h, lines 45-242
110 //----------------------------------------------------------------------------
112 /* Definitions for the Linux module syscall interface.
113 Copyright 1996, 1997 Linux International.
115 Contributed by Richard Henderson <rth@tamu.edu>
117 This file is part of the Linux modutils.
119 This program is free software; you can redistribute it and/or modify it
120 under the terms of the GNU General Public License as published by the
121 Free Software Foundation; either version 2 of the License, or (at your
122 option) any later version.
124 This program is distributed in the hope that it will be useful, but
125 WITHOUT ANY WARRANTY; without even the implied warranty of
126 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
127 General Public License for more details.
129 You should have received a copy of the GNU General Public License
130 along with this program; if not, write to the Free Software Foundation,
131 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
134 #ifndef MODUTILS_MODULE_H
135 static const int MODUTILS_MODULE_H = 1;
137 #ident "$Id: insmod.c,v 1.73 2001/08/22 05:41:57 andersen Exp $"
139 /* This file contains the structures used by the 2.0 and 2.1 kernels.
140 We do not use the kernel headers directly because we do not wish
141 to be dependant on a particular kernel version to compile insmod. */
144 /*======================================================================*/
145 /* The structures used by Linux 2.0. */
147 /* The symbol format used by get_kernel_syms(2). */
148 struct old_kernel_sym
154 struct old_module_ref
156 unsigned long module; /* kernel addresses */
160 struct old_module_symbol
166 struct old_symbol_table
168 int size; /* total, including string table!!! */
171 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
172 struct old_module_ref ref[0]; /* actual size defined by n_refs */
175 struct old_mod_routines
178 unsigned long cleanup;
184 unsigned long ref; /* the list of modules that refer to me */
185 unsigned long symtab;
187 int size; /* size of module in pages */
188 unsigned long addr; /* address of module */
190 unsigned long cleanup; /* cleanup routine */
193 /* Sent to init_module(2) or'ed into the code size parameter. */
194 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
196 int get_kernel_syms(struct old_kernel_sym *);
197 int old_sys_init_module(const char *name, char *code, unsigned codesize,
198 struct old_mod_routines *, struct old_symbol_table *);
200 /*======================================================================*/
201 /* For sizeof() which are related to the module platform and not to the
202 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
204 #define tgt_sizeof_char sizeof(char)
205 #define tgt_sizeof_short sizeof(short)
206 #define tgt_sizeof_int sizeof(int)
207 #define tgt_sizeof_long sizeof(long)
208 #define tgt_sizeof_char_p sizeof(char *)
209 #define tgt_sizeof_void_p sizeof(void *)
210 #define tgt_long long
212 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
213 #undef tgt_sizeof_long
214 #undef tgt_sizeof_char_p
215 #undef tgt_sizeof_void_p
217 static const int tgt_sizeof_long = 8;
218 static const int tgt_sizeof_char_p = 8;
219 static const int tgt_sizeof_void_p = 8;
220 #define tgt_long long long
223 /*======================================================================*/
224 /* The structures used in Linux 2.1. */
226 /* Note: new_module_symbol does not use tgt_long intentionally */
227 struct new_module_symbol
233 struct new_module_persist;
235 struct new_module_ref
237 unsigned tgt_long dep; /* kernel addresses */
238 unsigned tgt_long ref;
239 unsigned tgt_long next_ref;
244 unsigned tgt_long size_of_struct; /* == sizeof(module) */
245 unsigned tgt_long next;
246 unsigned tgt_long name;
247 unsigned tgt_long size;
250 unsigned tgt_long flags; /* AUTOCLEAN et al */
255 unsigned tgt_long syms;
256 unsigned tgt_long deps;
257 unsigned tgt_long refs;
258 unsigned tgt_long init;
259 unsigned tgt_long cleanup;
260 unsigned tgt_long ex_table_start;
261 unsigned tgt_long ex_table_end;
263 unsigned tgt_long gp;
265 /* Everything after here is extension. */
266 unsigned tgt_long persist_start;
267 unsigned tgt_long persist_end;
268 unsigned tgt_long can_unload;
269 unsigned tgt_long runsize;
270 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
271 const char *kallsyms_start; /* All symbols for kernel debugging */
272 const char *kallsyms_end;
273 const char *archdata_start; /* arch specific data for module */
274 const char *archdata_end;
275 const char *kernel_data; /* Reserved for kernel internal use */
279 #define ARCHDATA_SEC_NAME "__archdata"
280 #define KALLSYMS_SEC_NAME "__kallsyms"
283 struct new_module_info
291 /* Bits of module.flags. */
292 static const int NEW_MOD_RUNNING = 1;
293 static const int NEW_MOD_DELETED = 2;
294 static const int NEW_MOD_AUTOCLEAN = 4;
295 static const int NEW_MOD_VISITED = 8;
296 static const int NEW_MOD_USED_ONCE = 16;
298 int new_sys_init_module(const char *name, const struct new_module *);
299 int query_module(const char *name, int which, void *buf, size_t bufsize,
302 /* Values for query_module's which. */
304 static const int QM_MODULES = 1;
305 static const int QM_DEPS = 2;
306 static const int QM_REFS = 3;
307 static const int QM_SYMBOLS = 4;
308 static const int QM_INFO = 5;
310 /*======================================================================*/
311 /* The system calls unchanged between 2.0 and 2.1. */
313 unsigned long create_module(const char *, size_t);
314 int delete_module(const char *);
317 #endif /* module.h */
319 //----------------------------------------------------------------------------
320 //--------end of modutils module.h
321 //----------------------------------------------------------------------------
325 //----------------------------------------------------------------------------
326 //--------modutils obj.h, lines 253-462
327 //----------------------------------------------------------------------------
329 /* Elf object file loading and relocation routines.
330 Copyright 1996, 1997 Linux International.
332 Contributed by Richard Henderson <rth@tamu.edu>
334 This file is part of the Linux modutils.
336 This program is free software; you can redistribute it and/or modify it
337 under the terms of the GNU General Public License as published by the
338 Free Software Foundation; either version 2 of the License, or (at your
339 option) any later version.
341 This program is distributed in the hope that it will be useful, but
342 WITHOUT ANY WARRANTY; without even the implied warranty of
343 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
344 General Public License for more details.
346 You should have received a copy of the GNU General Public License
347 along with this program; if not, write to the Free Software Foundation,
348 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
351 #ifndef MODUTILS_OBJ_H
352 static const int MODUTILS_OBJ_H = 1;
354 #ident "$Id: insmod.c,v 1.73 2001/08/22 05:41:57 andersen Exp $"
356 /* The relocatable object is manipulated using elfin types. */
362 /* Machine-specific elf macros for i386 et al. */
364 /* the SH changes have only been tested on the SH4 in =little endian= mode */
365 /* I'm not sure about big endian, so let's warn: */
367 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
368 #error insmod.c may require changes for use on big endian SH4/SH3
371 /* it may or may not work on the SH1/SH2... So let's error on those
373 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
374 #error insmod.c may require changes for non-SH3/SH4 use
377 #define ELFCLASSM ELFCLASS32
379 #if (defined(__mc68000__))
380 #define ELFDATAM ELFDATA2MSB
387 #define MATCH_MACHINE(x) (x == EM_SH)
388 #define SHT_RELM SHT_RELA
389 #define Elf32_RelM Elf32_Rela
390 #define ELFDATAM ELFDATA2LSB
392 #elif defined(__arm__)
394 #define MATCH_MACHINE(x) (x == EM_ARM)
395 #define SHT_RELM SHT_REL
396 #define Elf32_RelM Elf32_Rel
397 #define ELFDATAM ELFDATA2LSB
399 #elif defined(__powerpc__)
401 #define MATCH_MACHINE(x) (x == EM_PPC)
402 #define SHT_RELM SHT_RELA
403 #define Elf32_RelM Elf32_Rela
404 #define ELFDATAM ELFDATA2MSB
406 #elif defined(__mips__)
408 /* Account for ELF spec changes. */
409 #ifndef EM_MIPS_RS3_LE
410 #ifdef EM_MIPS_RS4_BE
411 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
413 #define EM_MIPS_RS3_LE 10
415 #endif /* !EM_MIPS_RS3_LE */
417 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
418 #define SHT_RELM SHT_REL
419 #define Elf32_RelM Elf32_Rel
421 #define ELFDATAM ELFDATA2MSB
424 #define ELFDATAM ELFDATA2LSB
427 #elif defined(__i386__)
429 /* presumably we can use these for anything but the SH and ARM*/
430 /* this is the previous behavior, but it does result in
431 insmod.c being broken on anything except i386 */
433 #define MATCH_MACHINE(x) (x == EM_386)
435 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
438 #define SHT_RELM SHT_REL
439 #define Elf32_RelM Elf32_Rel
440 #define ELFDATAM ELFDATA2LSB
442 #elif defined(__mc68000__)
444 #define MATCH_MACHINE(x) (x == EM_68K)
445 #define SHT_RELM SHT_RELA
446 #define Elf32_RelM Elf32_Rela
449 #error Sorry, but insmod.c does not yet support this architecture...
453 # if ELFCLASSM == ELFCLASS32
454 # define ElfW(x) Elf32_ ## x
455 # define ELFW(x) ELF32_ ## x
457 # define ElfW(x) Elf64_ ## x
458 # define ELFW(x) ELF64_ ## x
462 /* For some reason this is missing from libc5. */
463 #ifndef ELF32_ST_INFO
464 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
467 #ifndef ELF64_ST_INFO
468 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
471 struct obj_string_patch;
472 struct obj_symbol_patch;
479 struct obj_section *load_next;
485 struct obj_symbol *next; /* hash table link */
489 int secidx; /* the defining section index/module */
491 int ksymidx; /* for export to the kernel symtab */
492 int referenced; /* actually used in the link */
495 /* Hardcode the hash table size. We shouldn't be needing so many
496 symbols that we begin to degrade performance, and we get a big win
497 by giving the compiler a constant divisor. */
499 #define HASH_BUCKETS 521
505 struct obj_section **sections;
506 struct obj_section *load_order;
507 struct obj_section **load_order_search_start;
508 struct obj_string_patch *string_patches;
509 struct obj_symbol_patch *symbol_patches;
510 int (*symbol_cmp)(const char *, const char *);
511 unsigned long (*symbol_hash)(const char *);
512 unsigned long local_symtab_size;
513 struct obj_symbol **local_symtab;
514 struct obj_symbol *symtab[HASH_BUCKETS];
525 struct obj_string_patch
527 struct obj_string_patch *next;
529 ElfW(Addr) reloc_offset;
530 ElfW(Addr) string_offset;
533 struct obj_symbol_patch
535 struct obj_symbol_patch *next;
537 ElfW(Addr) reloc_offset;
538 struct obj_symbol *sym;
542 /* Generic object manipulation routines. */
544 static unsigned long obj_elf_hash(const char *);
546 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
548 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
551 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
552 struct obj_symbol *sym);
554 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
555 static void obj_set_symbol_compare(struct obj_file *f,
556 int (*cmp)(const char *, const char *),
557 unsigned long (*hash)(const char *));
560 static struct obj_section *obj_find_section (struct obj_file *f,
563 static void obj_insert_section_load_order (struct obj_file *f,
564 struct obj_section *sec);
566 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
571 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
576 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
578 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
581 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
582 struct obj_symbol *sym);
584 static int obj_check_undefineds(struct obj_file *f);
586 static void obj_allocate_commons(struct obj_file *f);
588 static unsigned long obj_load_size (struct obj_file *f);
590 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
592 static struct obj_file *obj_load(FILE *f, int loadprogbits);
594 static int obj_create_image (struct obj_file *f, char *image);
596 /* Architecture specific manipulation routines. */
598 static struct obj_file *arch_new_file (void);
600 static struct obj_section *arch_new_section (void);
602 static struct obj_symbol *arch_new_symbol (void);
604 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
605 struct obj_section *targsec,
606 struct obj_section *symsec,
607 struct obj_symbol *sym,
608 ElfW(RelM) *rel, ElfW(Addr) value);
610 static int arch_create_got (struct obj_file *f);
612 static int arch_init_module (struct obj_file *f, struct new_module *);
615 //----------------------------------------------------------------------------
616 //--------end of modutils obj.h
617 //----------------------------------------------------------------------------
623 #define _PATH_MODULES "/lib/modules"
624 static const int STRVERSIONLEN = 32;
626 /*======================================================================*/
628 static int flag_force_load = 0;
629 static int flag_autoclean = 0;
630 static int flag_verbose = 0;
631 static int flag_export = 1;
634 /*======================================================================*/
636 /* previously, these were named i386_* but since we could be
637 compiling for the sh, I've renamed them to the more general
638 arch_* These structures are the same between the x86 and SH,
639 and we can't support anything else right now anyway. In the
640 future maybe they should be #if defined'd */
646 #if defined(BB_USE_PLT_ENTRIES)
647 struct arch_plt_entry
651 int inited:1; /* has been set up */
655 #if defined(BB_USE_GOT_ENTRIES)
656 struct arch_got_entry {
658 unsigned offset_done:1;
659 unsigned reloc_done:1;
663 #if defined(__mips__)
666 struct mips_hi16 *next;
673 struct obj_file root;
674 #if defined(BB_USE_PLT_ENTRIES)
675 struct obj_section *plt;
677 #if defined(BB_USE_GOT_ENTRIES)
678 struct obj_section *got;
680 #if defined(__mips__)
681 struct mips_hi16 *mips_hi16_list;
686 struct obj_symbol root;
687 #if defined(BB_USE_PLT_ENTRIES)
688 struct arch_plt_entry pltent;
690 #if defined(BB_USE_GOT_ENTRIES)
691 struct arch_got_entry gotent;
696 struct external_module {
701 struct new_module_symbol *syms;
704 static struct new_module_symbol *ksyms;
705 static size_t nksyms;
707 static struct external_module *ext_modules;
708 static int n_ext_modules;
709 static int n_ext_modules_used;
710 extern int delete_module(const char *);
712 static char m_filename[FILENAME_MAX + 1];
713 static char m_fullName[FILENAME_MAX + 1];
717 /*======================================================================*/
720 static int check_module_name_match(const char *filename, struct stat *statbuf,
723 char *fullname = (char *) userdata;
725 if (fullname[0] == '\0')
728 char *tmp, *tmp1 = strdup(filename);
729 tmp = get_last_path_component(tmp1);
730 if (strcmp(tmp, fullname) == 0) {
732 /* Stop searching if we find a match */
733 safe_strncpy(m_filename, filename, sizeof(m_filename));
742 /*======================================================================*/
744 static struct obj_file *arch_new_file(void)
747 f = xmalloc(sizeof(*f));
749 #if defined(BB_USE_PLT_ENTRIES)
752 #if defined(BB_USE_GOT_ENTRIES)
755 #if defined(__mips__)
756 f->mips_hi16_list = NULL;
762 static struct obj_section *arch_new_section(void)
764 return xmalloc(sizeof(struct obj_section));
767 static struct obj_symbol *arch_new_symbol(void)
769 struct arch_symbol *sym;
770 sym = xmalloc(sizeof(*sym));
772 #if defined(BB_USE_PLT_ENTRIES)
773 memset(&sym->pltent, 0, sizeof(sym->pltent));
775 #if defined(BB_USE_GOT_ENTRIES)
776 memset(&sym->gotent, 0, sizeof(sym->gotent));
782 static enum obj_reloc
783 arch_apply_relocation(struct obj_file *f,
784 struct obj_section *targsec,
785 struct obj_section *symsec,
786 struct obj_symbol *sym,
787 ElfW(RelM) *rel, ElfW(Addr) v)
789 struct arch_file *ifile = (struct arch_file *) f;
790 #if !(defined(__mips__))
791 struct arch_symbol *isym = (struct arch_symbol *) sym;
794 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
795 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
796 #if defined(BB_USE_GOT_ENTRIES)
797 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
799 #if defined(BB_USE_PLT_ENTRIES)
800 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
801 struct arch_plt_entry *pe;
804 enum obj_reloc ret = obj_reloc_ok;
806 switch (ELF32_R_TYPE(rel->r_info)) {
808 /* even though these constants seem to be the same for
809 the i386 and the sh, we "#if define" them for clarity
810 and in case that ever changes */
813 #elif defined(__arm__)
815 #elif defined(__i386__)
817 #elif defined(__mc68000__)
819 #elif defined(__powerpc__)
821 #elif defined(__mips__)
828 #elif defined(__arm__)
830 #elif defined(__i386__)
832 #elif defined(__mc68000__)
834 #elif defined(__powerpc__)
836 #elif defined(__mips__)
841 #if defined(__mc68000__)
844 ret = obj_reloc_overflow;
849 ret = obj_reloc_overflow;
852 #endif /* __mc68000__ */
854 #if defined(__powerpc__)
855 case R_PPC_ADDR16_HA:
856 *(unsigned short *)loc = (v + 0x8000) >> 16;
859 case R_PPC_ADDR16_HI:
860 *(unsigned short *)loc = v >> 16;
863 case R_PPC_ADDR16_LO:
864 *(unsigned short *)loc = v;
868 #if defined(__mips__)
871 ret = obj_reloc_dangerous;
872 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
873 ret = obj_reloc_overflow;
875 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
883 /* We cannot relocate this one now because we don't know the value
884 of the carry we need to add. Save the information, and let LO16
885 do the actual relocation. */
886 n = (struct mips_hi16 *) xmalloc(sizeof *n);
889 n->next = ifile->mips_hi16_list;
890 ifile->mips_hi16_list = n;
896 unsigned long insnlo = *loc;
897 Elf32_Addr val, vallo;
899 /* Sign extend the addend we extract from the lo insn. */
900 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
902 if (ifile->mips_hi16_list != NULL) {
905 l = ifile->mips_hi16_list;
907 struct mips_hi16 *next;
910 /* The value for the HI16 had best be the same. */
911 assert(v == l->value);
913 /* Do the HI16 relocation. Note that we actually don't
914 need to know anything about the LO16 itself, except where
915 to find the low 16 bits of the addend needed by the LO16. */
918 ((insn & 0xffff) << 16) +
922 /* Account for the sign extension that will happen in the
929 insn = (insn & ~0xffff) | val;
937 ifile->mips_hi16_list = NULL;
940 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
942 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
949 #elif defined(__sh__)
953 #elif defined(__i386__)
958 #elif defined(__mc68000__)
961 if ((Elf32_Sword)v > 0x7f || (Elf32_Sword)v < -(Elf32_Sword)0x80)
962 ret = obj_reloc_overflow;
967 if ((Elf32_Sword)v > 0x7fff || (Elf32_Sword)v < -(Elf32_Sword)0x8000)
968 ret = obj_reloc_overflow;
972 *(int *)loc = v - dot;
974 #elif defined(__powerpc__)
984 #elif defined(__i386__)
987 #if defined(BB_USE_PLT_ENTRIES)
993 #if defined(__powerpc__)
996 /* find the plt entry and initialize it if necessary */
997 assert(isym != NULL);
999 pe = (struct arch_plt_entry*) &isym->pltent;
1002 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1004 /* generate some machine code */
1006 #if defined(__arm__)
1007 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1008 ip[1] = v; /* sym@ */
1010 #if defined(__powerpc__)
1011 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1012 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1013 ip[2] = 0x7d6903a6; /* mtctr r11 */
1014 ip[3] = 0x4e800420; /* bctr */
1019 /* relative distance to target */
1021 /* if the target is too far away.... */
1022 if ((int)v < -0x02000000 || (int)v >= 0x02000000) {
1023 /* go via the plt */
1024 v = plt + pe->offset - dot;
1027 ret = obj_reloc_dangerous;
1029 /* merge the offset into the instruction. */
1030 #if defined(__arm__)
1031 /* Convert to words. */
1034 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1036 #if defined(__powerpc__)
1037 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1040 #endif /* BB_USE_PLT_ENTRIES */
1042 #if defined(__arm__)
1043 #elif defined(__sh__)
1048 #elif defined(__i386__)
1049 case R_386_GLOB_DAT:
1050 case R_386_JMP_SLOT:
1053 #elif defined(__mc68000__)
1054 case R_68K_GLOB_DAT:
1055 case R_68K_JMP_SLOT:
1060 #if defined(__arm__)
1061 #elif defined(__sh__)
1063 *loc += f->baseaddr + rel->r_addend;
1065 #elif defined(__i386__)
1066 case R_386_RELATIVE:
1067 *loc += f->baseaddr;
1069 #elif defined(__mc68000__)
1070 case R_68K_RELATIVE:
1071 *(int *)loc += f->baseaddr;
1075 #if defined(BB_USE_GOT_ENTRIES)
1077 #if !defined(__68k__)
1080 #elif defined(__arm__)
1082 #elif defined(__i386__)
1087 *loc += got - dot + rel->r_addend;;
1088 #elif defined(__i386__) || defined(__arm__) || defined(__m68k_)
1096 #elif defined(__arm__)
1098 #elif defined(__i386__)
1100 #elif defined(__mc68000__)
1103 assert(isym != NULL);
1104 /* needs an entry in the .got: set it, once */
1105 if (!isym->gotent.reloc_done) {
1106 isym->gotent.reloc_done = 1;
1107 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1109 /* make the reloc with_respect_to_.got */
1111 *loc += isym->gotent.offset + rel->r_addend;
1112 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1113 *loc += isym->gotent.offset;
1117 /* address relative to the got */
1118 #if !defined(__mc68000__)
1121 #elif defined(__arm__)
1123 #elif defined(__i386__)
1125 #elif defined(__mc68000__)
1131 #endif // __mc68000__
1133 #endif /* BB_USE_GOT_ENTRIES */
1136 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1137 ret = obj_reloc_unhandled;
1144 static int arch_create_got(struct obj_file *f)
1146 #if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
1147 struct arch_file *ifile = (struct arch_file *) f;
1149 #if defined(BB_USE_GOT_ENTRIES)
1150 int got_offset = 0, gotneeded = 0;
1152 #if defined(BB_USE_PLT_ENTRIES)
1153 int plt_offset = 0, pltneeded = 0;
1155 struct obj_section *relsec, *symsec, *strsec;
1156 ElfW(RelM) *rel, *relend;
1157 ElfW(Sym) *symtab, *extsym;
1158 const char *strtab, *name;
1159 struct arch_symbol *intsym;
1161 for (i = 0; i < f->header.e_shnum; ++i) {
1162 relsec = f->sections[i];
1163 if (relsec->header.sh_type != SHT_RELM)
1166 symsec = f->sections[relsec->header.sh_link];
1167 strsec = f->sections[symsec->header.sh_link];
1169 rel = (ElfW(RelM) *) relsec->contents;
1170 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1171 symtab = (ElfW(Sym) *) symsec->contents;
1172 strtab = (const char *) strsec->contents;
1174 for (; rel < relend; ++rel) {
1175 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1177 switch (ELF32_R_TYPE(rel->r_info)) {
1178 #if defined(__arm__)
1181 #elif defined(__sh__)
1184 #elif defined(__i386__)
1187 #elif defined(__mc68000__)
1192 #if defined(__powerpc__)
1198 #if defined(__arm__)
1207 if (got_offset == 0)
1209 #elif defined(__sh__)
1213 #elif defined(__i386__)
1223 if (extsym->st_name != 0) {
1224 name = strtab + extsym->st_name;
1226 name = f->sections[extsym->st_shndx]->name;
1228 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1229 #if defined(BB_USE_GOT_ENTRIES)
1230 if (!intsym->gotent.offset_done) {
1231 intsym->gotent.offset_done = 1;
1232 intsym->gotent.offset = got_offset;
1233 got_offset += BB_GOT_ENTRY_SIZE;
1236 #if defined(BB_USE_PLT_ENTRIES)
1237 if (pltneeded && intsym->pltent.allocated == 0) {
1238 intsym->pltent.allocated = 1;
1239 intsym->pltent.offset = plt_offset;
1240 plt_offset += BB_PLT_ENTRY_SIZE;
1241 intsym->pltent.inited = 0;
1248 #if defined(BB_USE_GOT_ENTRIES)
1250 struct obj_section* myrelsec = obj_find_section(f, ".got");
1253 obj_extend_section(myrelsec, got_offset);
1255 myrelsec = obj_create_alloced_section(f, ".got",
1261 ifile->got = myrelsec;
1265 #if defined(BB_USE_PLT_ENTRIES)
1267 ifile->plt = obj_create_alloced_section(f, ".plt",
1275 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1281 /*======================================================================*/
1283 /* Standard ELF hash function. */
1284 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1286 unsigned long h = 0;
1293 if ((g = (h & 0xf0000000)) != 0) {
1302 static unsigned long obj_elf_hash(const char *name)
1304 return obj_elf_hash_n(name, strlen(name));
1307 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1308 /* String comparison for non-co-versioned kernel and module. */
1310 static int ncv_strcmp(const char *a, const char *b)
1312 size_t alen = strlen(a), blen = strlen(b);
1314 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1315 return strncmp(a, b, alen);
1316 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1317 return strncmp(a, b, blen);
1319 return strcmp(a, b);
1322 /* String hashing for non-co-versioned kernel and module. Here
1323 we are simply forced to drop the crc from the hash. */
1325 static unsigned long ncv_symbol_hash(const char *str)
1327 size_t len = strlen(str);
1328 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1330 return obj_elf_hash_n(str, len);
1334 obj_set_symbol_compare(struct obj_file *f,
1335 int (*cmp) (const char *, const char *),
1336 unsigned long (*hash) (const char *))
1339 f->symbol_cmp = cmp;
1341 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1344 f->symbol_hash = hash;
1346 memcpy(tmptab, f->symtab, sizeof(tmptab));
1347 memset(f->symtab, 0, sizeof(f->symtab));
1349 for (i = 0; i < HASH_BUCKETS; ++i)
1350 for (sym = tmptab[i]; sym; sym = next) {
1351 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1353 sym->next = f->symtab[h];
1359 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1361 static struct obj_symbol *
1362 obj_add_symbol(struct obj_file *f, const char *name,
1363 unsigned long symidx, int info,
1364 int secidx, ElfW(Addr) value,
1367 struct obj_symbol *sym;
1368 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1369 int n_type = ELFW(ST_TYPE) (info);
1370 int n_binding = ELFW(ST_BIND) (info);
1372 for (sym = f->symtab[hash]; sym; sym = sym->next)
1373 if (f->symbol_cmp(sym->name, name) == 0) {
1374 int o_secidx = sym->secidx;
1375 int o_info = sym->info;
1376 int o_type = ELFW(ST_TYPE) (o_info);
1377 int o_binding = ELFW(ST_BIND) (o_info);
1379 /* A redefinition! Is it legal? */
1381 if (secidx == SHN_UNDEF)
1383 else if (o_secidx == SHN_UNDEF)
1385 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1386 /* Cope with local and global symbols of the same name
1387 in the same object file, as might have been created
1388 by ld -r. The only reason locals are now seen at this
1389 level at all is so that we can do semi-sensible things
1392 struct obj_symbol *nsym, **p;
1394 nsym = arch_new_symbol();
1395 nsym->next = sym->next;
1398 /* Excise the old (local) symbol from the hash chain. */
1399 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1403 } else if (n_binding == STB_LOCAL) {
1404 /* Another symbol of the same name has already been defined.
1405 Just add this to the local table. */
1406 sym = arch_new_symbol();
1409 f->local_symtab[symidx] = sym;
1411 } else if (n_binding == STB_WEAK)
1413 else if (o_binding == STB_WEAK)
1415 /* Don't unify COMMON symbols with object types the programmer
1417 else if (secidx == SHN_COMMON
1418 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1420 else if (o_secidx == SHN_COMMON
1421 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1424 /* Don't report an error if the symbol is coming from
1425 the kernel or some external module. */
1426 if (secidx <= SHN_HIRESERVE)
1427 error_msg("%s multiply defined", name);
1432 /* Completely new symbol. */
1433 sym = arch_new_symbol();
1434 sym->next = f->symtab[hash];
1435 f->symtab[hash] = sym;
1438 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1439 if (symidx >= f->local_symtab_size)
1440 error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1441 name, (long) symidx, (long) f->local_symtab_size);
1443 f->local_symtab[symidx] = sym;
1450 sym->secidx = secidx;
1456 static struct obj_symbol *
1457 obj_find_symbol(struct obj_file *f, const char *name)
1459 struct obj_symbol *sym;
1460 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1462 for (sym = f->symtab[hash]; sym; sym = sym->next)
1463 if (f->symbol_cmp(sym->name, name) == 0)
1470 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1473 if (sym->secidx >= SHN_LORESERVE)
1476 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1478 /* As a special case, a NULL sym has value zero. */
1483 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1485 int i, n = f->header.e_shnum;
1487 for (i = 0; i < n; ++i)
1488 if (strcmp(f->sections[i]->name, name) == 0)
1489 return f->sections[i];
1494 static int obj_load_order_prio(struct obj_section *a)
1496 unsigned long af, ac;
1498 af = a->header.sh_flags;
1501 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1502 strcmp(a->name + 5, ".init"))
1506 if (!(af & SHF_WRITE))
1508 if (af & SHF_EXECINSTR)
1510 if (a->header.sh_type != SHT_NOBITS)
1517 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1519 struct obj_section **p;
1520 int prio = obj_load_order_prio(sec);
1521 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1522 if (obj_load_order_prio(*p) < prio)
1524 sec->load_next = *p;
1528 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1530 unsigned long align,
1533 int newidx = f->header.e_shnum++;
1534 struct obj_section *sec;
1536 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1537 f->sections[newidx] = sec = arch_new_section();
1539 memset(sec, 0, sizeof(*sec));
1540 sec->header.sh_type = SHT_PROGBITS;
1541 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1542 sec->header.sh_size = size;
1543 sec->header.sh_addralign = align;
1547 sec->contents = xmalloc(size);
1549 obj_insert_section_load_order(f, sec);
1554 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1556 unsigned long align,
1559 int newidx = f->header.e_shnum++;
1560 struct obj_section *sec;
1562 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1563 f->sections[newidx] = sec = arch_new_section();
1565 memset(sec, 0, sizeof(*sec));
1566 sec->header.sh_type = SHT_PROGBITS;
1567 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1568 sec->header.sh_size = size;
1569 sec->header.sh_addralign = align;
1573 sec->contents = xmalloc(size);
1575 sec->load_next = f->load_order;
1576 f->load_order = sec;
1577 if (f->load_order_search_start == &f->load_order)
1578 f->load_order_search_start = &sec->load_next;
1583 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1585 unsigned long oldsize = sec->header.sh_size;
1587 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1589 return sec->contents + oldsize;
1593 /* Conditionally add the symbols from the given symbol set to the
1599 int idx, struct new_module_symbol *syms, size_t nsyms)
1601 struct new_module_symbol *s;
1605 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1607 /* Only add symbols that are already marked external. If we
1608 override locals we may cause problems for argument initialization.
1609 We will also create a false dependency on the module. */
1610 struct obj_symbol *sym;
1612 sym = obj_find_symbol(f, (char *) s->name);
1613 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1614 sym = obj_add_symbol(f, (char *) s->name, -1,
1615 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1617 /* Did our symbol just get installed? If so, mark the
1618 module as "used". */
1619 if (sym->secidx == idx)
1627 static void add_kernel_symbols(struct obj_file *f)
1629 struct external_module *m;
1632 /* Add module symbols first. */
1634 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1636 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1637 m->nsyms)) m->used = 1, ++nused;
1639 n_ext_modules_used = nused;
1641 /* And finally the symbols from the kernel proper. */
1644 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1647 static char *get_modinfo_value(struct obj_file *f, const char *key)
1649 struct obj_section *sec;
1650 char *p, *v, *n, *ep;
1651 size_t klen = strlen(key);
1653 sec = obj_find_section(f, ".modinfo");
1657 ep = p + sec->header.sh_size;
1660 n = strchr(p, '\0');
1662 if (p + klen == v && strncmp(p, key, klen) == 0)
1665 if (p + klen == n && strcmp(p, key) == 0)
1675 /*======================================================================*/
1676 /* Functions relating to module loading in pre 2.1 kernels. */
1679 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1683 struct obj_symbol *sym;
1687 if ((q = strchr(p, '=')) == NULL) {
1693 sym = obj_find_symbol(f, p);
1695 /* Also check that the parameter was not resolved from the kernel. */
1696 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1697 error_msg("symbol for parameter %s not found", p);
1701 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1703 /* Do C quoting if we begin with a ". */
1707 str = alloca(strlen(q));
1708 for (r = str, q++; *q != '"'; ++q, ++r) {
1710 error_msg("improperly terminated string argument for %s", p);
1712 } else if (*q == '\\')
1746 if (q[1] >= '0' && q[1] <= '7') {
1747 c = (c * 8) + *++q - '0';
1748 if (q[1] >= '0' && q[1] <= '7')
1749 c = (c * 8) + *++q - '0';
1762 obj_string_patch(f, sym->secidx, sym->value, str);
1763 } else if (*q >= '0' && *q <= '9') {
1765 *loc++ = strtoul(q, &q, 0);
1766 while (*q++ == ',');
1768 char *contents = f->sections[sym->secidx]->contents;
1769 char *myloc = contents + sym->value;
1770 char *r; /* To search for commas */
1772 /* Break the string with comas */
1773 while ((r = strchr(q, ',')) != (char *) NULL) {
1775 obj_string_patch(f, sym->secidx, myloc - contents, q);
1776 myloc += sizeof(char *);
1781 obj_string_patch(f, sym->secidx, myloc - contents, q);
1790 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1791 static int old_is_module_checksummed(struct obj_file *f)
1793 return obj_find_symbol(f, "Using_Versions") != NULL;
1795 /* Get the module's kernel version in the canonical integer form. */
1798 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1800 struct obj_symbol *sym;
1804 sym = obj_find_symbol(f, "kernel_version");
1808 p = f->sections[sym->secidx]->contents + sym->value;
1809 strncpy(str, p, STRVERSIONLEN);
1811 a = strtoul(p, &p, 10);
1814 b = strtoul(p + 1, &p, 10);
1817 c = strtoul(p + 1, &q, 10);
1821 return a << 16 | b << 8 | c;
1824 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1826 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
1828 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1830 static int old_get_kernel_symbols(const char *m_name)
1832 struct old_kernel_sym *ks, *k;
1833 struct new_module_symbol *s;
1834 struct external_module *mod;
1835 int nks, nms, nmod, i;
1837 nks = get_kernel_syms(NULL);
1840 perror_msg("get_kernel_syms: %s", m_name);
1842 error_msg("No kernel symbols");
1846 ks = k = xmalloc(nks * sizeof(*ks));
1848 if (get_kernel_syms(ks) != nks) {
1849 perror("inconsistency with get_kernel_syms -- is someone else "
1850 "playing with modules?");
1855 /* Collect the module information. */
1860 while (k->name[0] == '#' && k->name[1]) {
1861 struct old_kernel_sym *k2;
1863 /* Find out how many symbols this module has. */
1864 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1868 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1869 mod[nmod].name = k->name + 1;
1870 mod[nmod].addr = k->value;
1872 mod[nmod].nsyms = nms;
1873 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1875 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1876 s->name = (unsigned long) k->name;
1877 s->value = k->value;
1884 n_ext_modules = nmod + 1;
1886 /* Now collect the symbols for the kernel proper. */
1888 if (k->name[0] == '#')
1891 nksyms = nms = nks - (k - ks);
1892 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1894 for (i = 0; i < nms; ++i, ++s, ++k) {
1895 s->name = (unsigned long) k->name;
1896 s->value = k->value;
1902 /* Return the kernel symbol checksum version, or zero if not used. */
1904 static int old_is_kernel_checksummed(void)
1906 /* Using_Versions is the first symbol. */
1908 && strcmp((char *) ksyms[0].name,
1909 "Using_Versions") == 0) return ksyms[0].value;
1915 static int old_create_mod_use_count(struct obj_file *f)
1917 struct obj_section *sec;
1919 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1922 obj_add_symbol(f, "mod_use_count_", -1,
1923 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1930 old_init_module(const char *m_name, struct obj_file *f,
1931 unsigned long m_size)
1934 struct old_mod_routines routines;
1935 struct old_symbol_table *symtab;
1938 /* Create the symbol table */
1940 int nsyms = 0, strsize = 0, total;
1942 /* Size things first... */
1945 for (i = 0; i < HASH_BUCKETS; ++i) {
1946 struct obj_symbol *sym;
1947 for (sym = f->symtab[i]; sym; sym = sym->next)
1948 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1949 && sym->secidx <= SHN_HIRESERVE)
1951 sym->ksymidx = nsyms++;
1952 strsize += strlen(sym->name) + 1;
1957 total = (sizeof(struct old_symbol_table)
1958 + nsyms * sizeof(struct old_module_symbol)
1959 + n_ext_modules_used * sizeof(struct old_module_ref)
1961 symtab = xmalloc(total);
1962 symtab->size = total;
1963 symtab->n_symbols = nsyms;
1964 symtab->n_refs = n_ext_modules_used;
1966 if (flag_export && nsyms) {
1967 struct old_module_symbol *ksym;
1971 ksym = symtab->symbol;
1972 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1973 + n_ext_modules_used * sizeof(struct old_module_ref));
1975 for (i = 0; i < HASH_BUCKETS; ++i) {
1976 struct obj_symbol *sym;
1977 for (sym = f->symtab[i]; sym; sym = sym->next)
1978 if (sym->ksymidx >= 0) {
1979 ksym->addr = obj_symbol_final_value(f, sym);
1981 (unsigned long) str - (unsigned long) symtab;
1983 strcpy(str, sym->name);
1984 str += strlen(sym->name) + 1;
1990 if (n_ext_modules_used) {
1991 struct old_module_ref *ref;
1994 ref = (struct old_module_ref *)
1995 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
1997 for (i = 0; i < n_ext_modules; ++i)
1998 if (ext_modules[i].used)
1999 ref++->module = ext_modules[i].addr;
2003 /* Fill in routines. */
2006 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2008 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2010 /* Whew! All of the initialization is complete. Collect the final
2011 module image and give it to the kernel. */
2013 image = xmalloc(m_size);
2014 obj_create_image(f, image);
2016 /* image holds the complete relocated module, accounting correctly for
2017 mod_use_count. However the old module kernel support assume that
2018 it is receiving something which does not contain mod_use_count. */
2019 ret = old_sys_init_module(m_name, image + sizeof(long),
2020 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2021 : 0), &routines, symtab);
2023 perror_msg("init_module: %s", m_name);
2033 #define old_create_mod_use_count(x) TRUE
2034 #define old_init_module(x, y, z) TRUE
2036 #endif /* BB_FEATURE_OLD_MODULE_INTERFACE */
2040 /*======================================================================*/
2041 /* Functions relating to module loading after 2.1.18. */
2044 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2048 struct obj_symbol *sym;
2049 char *contents, *loc;
2053 if ((q = strchr(p, '=')) == NULL) {
2058 key = alloca(q - p + 6);
2059 memcpy(key, "parm_", 5);
2060 memcpy(key + 5, p, q - p);
2063 p = get_modinfo_value(f, key);
2066 error_msg("invalid parameter %s", key);
2070 sym = obj_find_symbol(f, key);
2072 /* Also check that the parameter was not resolved from the kernel. */
2073 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2074 error_msg("symbol for parameter %s not found", key);
2079 min = strtoul(p, &p, 10);
2081 max = strtoul(p + 1, &p, 10);
2087 contents = f->sections[sym->secidx]->contents;
2088 loc = contents + sym->value;
2092 if ((*p == 's') || (*p == 'c')) {
2095 /* Do C quoting if we begin with a ", else slurp the lot. */
2099 str = alloca(strlen(q));
2100 for (r = str, q++; *q != '"'; ++q, ++r) {
2102 error_msg("improperly terminated string argument for %s",
2105 } else if (*q == '\\')
2139 if (q[1] >= '0' && q[1] <= '7') {
2140 c = (c * 8) + *++q - '0';
2141 if (q[1] >= '0' && q[1] <= '7')
2142 c = (c * 8) + *++q - '0';
2159 /* In this case, the string is not quoted. We will break
2160 it using the coma (like for ints). If the user wants to
2161 include comas in a string, he just has to quote it */
2163 /* Search the next coma */
2167 if (r != (char *) NULL) {
2168 /* Recopy the current field */
2169 str = alloca(r - q + 1);
2170 memcpy(str, q, r - q);
2172 /* I don't know if it is usefull, as the previous case
2173 doesn't null terminate the string ??? */
2176 /* Keep next fields */
2187 obj_string_patch(f, sym->secidx, loc - contents, str);
2188 loc += tgt_sizeof_char_p;
2190 /* Array of chars (in fact, matrix !) */
2191 unsigned long charssize; /* size of each member */
2193 /* Get the size of each member */
2194 /* Probably we should do that outside the loop ? */
2195 if (!isdigit(*(p + 1))) {
2196 error_msg("parameter type 'c' for %s must be followed by"
2197 " the maximum size", key);
2200 charssize = strtoul(p + 1, (char **) NULL, 10);
2203 if (strlen(str) >= charssize) {
2204 error_msg("string too long for %s (max %ld)", key,
2209 /* Copy to location */
2210 strcpy((char *) loc, str);
2214 long v = strtoul(q, &q, 0);
2221 loc += tgt_sizeof_short;
2225 loc += tgt_sizeof_int;
2229 loc += tgt_sizeof_long;
2233 error_msg("unknown parameter type '%c' for %s", *p, key);
2248 goto retry_end_of_value;
2252 error_msg("too many values for %s (max %d)", key, max);
2259 error_msg("invalid argument syntax for %s", key);
2266 error_msg("too few values for %s (min %d)", key, min);
2276 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2277 static int new_is_module_checksummed(struct obj_file *f)
2279 const char *p = get_modinfo_value(f, "using_checksums");
2286 /* Get the module's kernel version in the canonical integer form. */
2289 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2294 p = get_modinfo_value(f, "kernel_version");
2297 strncpy(str, p, STRVERSIONLEN);
2299 a = strtoul(p, &p, 10);
2302 b = strtoul(p + 1, &p, 10);
2305 c = strtoul(p + 1, &q, 10);
2309 return a << 16 | b << 8 | c;
2312 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2315 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
2317 /* Fetch the loaded modules, and all currently exported symbols. */
2319 static int new_get_kernel_symbols(void)
2321 char *module_names, *mn;
2322 struct external_module *modules, *m;
2323 struct new_module_symbol *syms, *s;
2324 size_t ret, bufsize, nmod, nsyms, i, j;
2326 /* Collect the loaded modules. */
2328 module_names = xmalloc(bufsize = 256);
2330 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2331 if (errno == ENOSPC && bufsize < ret) {
2332 module_names = xrealloc(module_names, bufsize = ret);
2333 goto retry_modules_load;
2335 perror_msg("QM_MODULES");
2339 n_ext_modules = nmod = ret;
2341 /* Collect the modules' symbols. */
2344 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2345 memset(modules, 0, nmod * sizeof(*modules));
2346 for (i = 0, mn = module_names, m = modules;
2347 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2348 struct new_module_info info;
2350 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2351 if (errno == ENOENT) {
2352 /* The module was removed out from underneath us. */
2355 perror_msg("query_module: QM_INFO: %s", mn);
2359 syms = xmalloc(bufsize = 1024);
2361 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2364 syms = xrealloc(syms, bufsize = ret);
2365 goto retry_mod_sym_load;
2367 /* The module was removed out from underneath us. */
2370 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2377 m->addr = info.addr;
2381 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2382 s->name += (unsigned long) syms;
2387 /* Collect the kernel's symbols. */
2389 syms = xmalloc(bufsize = 16 * 1024);
2390 retry_kern_sym_load:
2391 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2392 if (errno == ENOSPC && bufsize < ret) {
2393 syms = xrealloc(syms, bufsize = ret);
2394 goto retry_kern_sym_load;
2396 perror_msg("kernel: QM_SYMBOLS");
2399 nksyms = nsyms = ret;
2402 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2403 s->name += (unsigned long) syms;
2409 /* Return the kernel symbol checksum version, or zero if not used. */
2411 static int new_is_kernel_checksummed(void)
2413 struct new_module_symbol *s;
2416 /* Using_Versions is not the first symbol, but it should be in there. */
2418 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2419 if (strcmp((char *) s->name, "Using_Versions") == 0)
2426 static int new_create_this_module(struct obj_file *f, const char *m_name)
2428 struct obj_section *sec;
2430 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2431 sizeof(struct new_module));
2432 memset(sec->contents, 0, sizeof(struct new_module));
2434 obj_add_symbol(f, "__this_module", -1,
2435 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2436 sizeof(struct new_module));
2438 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2445 static int new_create_module_ksymtab(struct obj_file *f)
2447 struct obj_section *sec;
2450 /* We must always add the module references. */
2452 if (n_ext_modules_used) {
2453 struct new_module_ref *dep;
2454 struct obj_symbol *tm;
2456 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2457 (sizeof(struct new_module_ref)
2458 * n_ext_modules_used));
2462 tm = obj_find_symbol(f, "__this_module");
2463 dep = (struct new_module_ref *) sec->contents;
2464 for (i = 0; i < n_ext_modules; ++i)
2465 if (ext_modules[i].used) {
2466 dep->dep = ext_modules[i].addr;
2467 obj_symbol_patch(f, sec->idx,
2468 (char *) &dep->ref - sec->contents, tm);
2474 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2479 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2482 /* We don't want to export symbols residing in sections that
2483 aren't loaded. There are a number of these created so that
2484 we make sure certain module options don't appear twice. */
2486 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2488 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2490 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2491 struct obj_symbol *sym;
2492 for (sym = f->symtab[i]; sym; sym = sym->next)
2493 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2494 && sym->secidx <= SHN_HIRESERVE
2495 && (sym->secidx >= SHN_LORESERVE
2496 || loaded[sym->secidx])) {
2497 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2499 obj_symbol_patch(f, sec->idx, ofs, sym);
2500 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2507 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2515 new_init_module(const char *m_name, struct obj_file *f,
2516 unsigned long m_size)
2518 struct new_module *module;
2519 struct obj_section *sec;
2524 sec = obj_find_section(f, ".this");
2525 if (!sec || !sec->contents) {
2526 perror_msg_and_die("corrupt module %s?",m_name);
2528 module = (struct new_module *) sec->contents;
2529 m_addr = sec->header.sh_addr;
2531 module->size_of_struct = sizeof(*module);
2532 module->size = m_size;
2533 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2535 sec = obj_find_section(f, "__ksymtab");
2536 if (sec && sec->header.sh_size) {
2537 module->syms = sec->header.sh_addr;
2538 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2541 if (n_ext_modules_used) {
2542 sec = obj_find_section(f, ".kmodtab");
2543 module->deps = sec->header.sh_addr;
2544 module->ndeps = n_ext_modules_used;
2548 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2550 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2552 sec = obj_find_section(f, "__ex_table");
2554 module->ex_table_start = sec->header.sh_addr;
2555 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2558 sec = obj_find_section(f, ".text.init");
2560 module->runsize = sec->header.sh_addr - m_addr;
2562 sec = obj_find_section(f, ".data.init");
2564 if (!module->runsize ||
2565 module->runsize > sec->header.sh_addr - m_addr)
2566 module->runsize = sec->header.sh_addr - m_addr;
2568 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2569 if (sec && sec->header.sh_size) {
2570 module->archdata_start = (void*)sec->header.sh_addr;
2571 module->archdata_end = module->archdata_start + sec->header.sh_size;
2573 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2574 if (sec && sec->header.sh_size) {
2575 module->kallsyms_start = (void*)sec->header.sh_addr;
2576 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2579 if (!arch_init_module(f, module))
2582 /* Whew! All of the initialization is complete. Collect the final
2583 module image and give it to the kernel. */
2585 image = xmalloc(m_size);
2586 obj_create_image(f, image);
2588 ret = new_sys_init_module(m_name, (struct new_module *) image);
2590 perror_msg("init_module: %s", m_name);
2599 #define new_init_module(x, y, z) TRUE
2600 #define new_create_this_module(x, y) 0
2601 #define new_create_module_ksymtab(x)
2602 #define query_module(v, w, x, y, z) -1
2604 #endif /* BB_FEATURE_NEW_MODULE_INTERFACE */
2607 /*======================================================================*/
2610 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2613 struct obj_string_patch *p;
2614 struct obj_section *strsec;
2615 size_t len = strlen(string) + 1;
2618 p = xmalloc(sizeof(*p));
2619 p->next = f->string_patches;
2620 p->reloc_secidx = secidx;
2621 p->reloc_offset = offset;
2622 f->string_patches = p;
2624 strsec = obj_find_section(f, ".kstrtab");
2625 if (strsec == NULL) {
2626 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2627 p->string_offset = 0;
2628 loc = strsec->contents;
2630 p->string_offset = strsec->header.sh_size;
2631 loc = obj_extend_section(strsec, len);
2633 memcpy(loc, string, len);
2639 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2640 struct obj_symbol *sym)
2642 struct obj_symbol_patch *p;
2644 p = xmalloc(sizeof(*p));
2645 p->next = f->symbol_patches;
2646 p->reloc_secidx = secidx;
2647 p->reloc_offset = offset;
2649 f->symbol_patches = p;
2654 static int obj_check_undefineds(struct obj_file *f)
2659 for (i = 0; i < HASH_BUCKETS; ++i) {
2660 struct obj_symbol *sym;
2661 for (sym = f->symtab[i]; sym; sym = sym->next)
2662 if (sym->secidx == SHN_UNDEF) {
2663 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2664 sym->secidx = SHN_ABS;
2667 error_msg("unresolved symbol %s", sym->name);
2676 static void obj_allocate_commons(struct obj_file *f)
2678 struct common_entry {
2679 struct common_entry *next;
2680 struct obj_symbol *sym;
2681 } *common_head = NULL;
2685 for (i = 0; i < HASH_BUCKETS; ++i) {
2686 struct obj_symbol *sym;
2687 for (sym = f->symtab[i]; sym; sym = sym->next)
2688 if (sym->secidx == SHN_COMMON) {
2689 /* Collect all COMMON symbols and sort them by size so as to
2690 minimize space wasted by alignment requirements. */
2692 struct common_entry **p, *n;
2693 for (p = &common_head; *p; p = &(*p)->next)
2694 if (sym->size <= (*p)->sym->size)
2697 n = alloca(sizeof(*n));
2705 for (i = 1; i < f->local_symtab_size; ++i) {
2706 struct obj_symbol *sym = f->local_symtab[i];
2707 if (sym && sym->secidx == SHN_COMMON) {
2708 struct common_entry **p, *n;
2709 for (p = &common_head; *p; p = &(*p)->next)
2710 if (sym == (*p)->sym)
2712 else if (sym->size < (*p)->sym->size) {
2713 n = alloca(sizeof(*n));
2723 /* Find the bss section. */
2724 for (i = 0; i < f->header.e_shnum; ++i)
2725 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2728 /* If for some reason there hadn't been one, create one. */
2729 if (i == f->header.e_shnum) {
2730 struct obj_section *sec;
2732 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2733 f->sections[i] = sec = arch_new_section();
2734 f->header.e_shnum = i + 1;
2736 memset(sec, 0, sizeof(*sec));
2737 sec->header.sh_type = SHT_PROGBITS;
2738 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2743 /* Allocate the COMMONS. */
2745 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2746 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2747 struct common_entry *c;
2749 for (c = common_head; c; c = c->next) {
2750 ElfW(Addr) align = c->sym->value;
2752 if (align > max_align)
2754 if (bss_size & (align - 1))
2755 bss_size = (bss_size | (align - 1)) + 1;
2758 c->sym->value = bss_size;
2760 bss_size += c->sym->size;
2763 f->sections[i]->header.sh_size = bss_size;
2764 f->sections[i]->header.sh_addralign = max_align;
2768 /* For the sake of patch relocation and parameter initialization,
2769 allocate zeroed data for NOBITS sections now. Note that after
2770 this we cannot assume NOBITS are really empty. */
2771 for (i = 0; i < f->header.e_shnum; ++i) {
2772 struct obj_section *s = f->sections[i];
2773 if (s->header.sh_type == SHT_NOBITS) {
2774 if (s->header.sh_size != 0)
2775 s->contents = memset(xmalloc(s->header.sh_size),
2776 0, s->header.sh_size);
2780 s->header.sh_type = SHT_PROGBITS;
2785 static unsigned long obj_load_size(struct obj_file *f)
2787 unsigned long dot = 0;
2788 struct obj_section *sec;
2790 /* Finalize the positions of the sections relative to one another. */
2792 for (sec = f->load_order; sec; sec = sec->load_next) {
2795 align = sec->header.sh_addralign;
2796 if (align && (dot & (align - 1)))
2797 dot = (dot | (align - 1)) + 1;
2799 sec->header.sh_addr = dot;
2800 dot += sec->header.sh_size;
2806 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2808 int i, n = f->header.e_shnum;
2811 /* Finalize the addresses of the sections. */
2814 for (i = 0; i < n; ++i)
2815 f->sections[i]->header.sh_addr += base;
2817 /* And iterate over all of the relocations. */
2819 for (i = 0; i < n; ++i) {
2820 struct obj_section *relsec, *symsec, *targsec, *strsec;
2821 ElfW(RelM) * rel, *relend;
2825 relsec = f->sections[i];
2826 if (relsec->header.sh_type != SHT_RELM)
2829 symsec = f->sections[relsec->header.sh_link];
2830 targsec = f->sections[relsec->header.sh_info];
2831 strsec = f->sections[symsec->header.sh_link];
2833 rel = (ElfW(RelM) *) relsec->contents;
2834 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2835 symtab = (ElfW(Sym) *) symsec->contents;
2836 strtab = (const char *) strsec->contents;
2838 for (; rel < relend; ++rel) {
2839 ElfW(Addr) value = 0;
2840 struct obj_symbol *intsym = NULL;
2841 unsigned long symndx;
2842 ElfW(Sym) * extsym = 0;
2845 /* Attempt to find a value to use for this relocation. */
2847 symndx = ELFW(R_SYM) (rel->r_info);
2849 /* Note we've already checked for undefined symbols. */
2851 extsym = &symtab[symndx];
2852 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2853 /* Local symbols we look up in the local table to be sure
2854 we get the one that is really intended. */
2855 intsym = f->local_symtab[symndx];
2857 /* Others we look up in the hash table. */
2859 if (extsym->st_name)
2860 name = strtab + extsym->st_name;
2862 name = f->sections[extsym->st_shndx]->name;
2863 intsym = obj_find_symbol(f, name);
2866 value = obj_symbol_final_value(f, intsym);
2867 intsym->referenced = 1;
2869 #if SHT_RELM == SHT_RELA
2870 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2871 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2872 if (!extsym || !extsym->st_name ||
2873 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2875 value += rel->r_addend;
2879 switch (arch_apply_relocation
2880 (f, targsec, symsec, intsym, rel, value)) {
2884 case obj_reloc_overflow:
2885 errmsg = "Relocation overflow";
2887 case obj_reloc_dangerous:
2888 errmsg = "Dangerous relocation";
2890 case obj_reloc_unhandled:
2891 errmsg = "Unhandled relocation";
2894 error_msg("%s of type %ld for %s", errmsg,
2895 (long) ELFW(R_TYPE) (rel->r_info),
2896 strtab + extsym->st_name);
2898 error_msg("%s of type %ld", errmsg,
2899 (long) ELFW(R_TYPE) (rel->r_info));
2907 /* Finally, take care of the patches. */
2909 if (f->string_patches) {
2910 struct obj_string_patch *p;
2911 struct obj_section *strsec;
2912 ElfW(Addr) strsec_base;
2913 strsec = obj_find_section(f, ".kstrtab");
2914 strsec_base = strsec->header.sh_addr;
2916 for (p = f->string_patches; p; p = p->next) {
2917 struct obj_section *targsec = f->sections[p->reloc_secidx];
2918 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2919 = strsec_base + p->string_offset;
2923 if (f->symbol_patches) {
2924 struct obj_symbol_patch *p;
2926 for (p = f->symbol_patches; p; p = p->next) {
2927 struct obj_section *targsec = f->sections[p->reloc_secidx];
2928 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2929 = obj_symbol_final_value(f, p->sym);
2936 static int obj_create_image(struct obj_file *f, char *image)
2938 struct obj_section *sec;
2939 ElfW(Addr) base = f->baseaddr;
2941 for (sec = f->load_order; sec; sec = sec->load_next) {
2944 if (sec->contents == 0 || sec->header.sh_size == 0)
2947 secimg = image + (sec->header.sh_addr - base);
2949 /* Note that we allocated data for NOBITS sections earlier. */
2950 memcpy(secimg, sec->contents, sec->header.sh_size);
2956 /*======================================================================*/
2958 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
2961 ElfW(Shdr) * section_headers;
2965 /* Read the file header. */
2967 f = arch_new_file();
2968 memset(f, 0, sizeof(*f));
2969 f->symbol_cmp = strcmp;
2970 f->symbol_hash = obj_elf_hash;
2971 f->load_order_search_start = &f->load_order;
2973 fseek(fp, 0, SEEK_SET);
2974 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2975 perror_msg("error reading ELF header");
2979 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2980 || f->header.e_ident[EI_MAG1] != ELFMAG1
2981 || f->header.e_ident[EI_MAG2] != ELFMAG2
2982 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2983 error_msg("not an ELF file");
2986 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2987 || f->header.e_ident[EI_DATA] != ELFDATAM
2988 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2989 || !MATCH_MACHINE(f->header.e_machine)) {
2990 error_msg("ELF file not for this architecture");
2993 if (f->header.e_type != ET_REL) {
2994 error_msg("ELF file not a relocatable object");
2998 /* Read the section headers. */
3000 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3001 error_msg("section header size mismatch: %lu != %lu",
3002 (unsigned long) f->header.e_shentsize,
3003 (unsigned long) sizeof(ElfW(Shdr)));
3007 shnum = f->header.e_shnum;
3008 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3009 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3011 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3012 fseek(fp, f->header.e_shoff, SEEK_SET);
3013 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3014 perror_msg("error reading ELF section headers");
3018 /* Read the section data. */
3020 for (i = 0; i < shnum; ++i) {
3021 struct obj_section *sec;
3023 f->sections[i] = sec = arch_new_section();
3024 memset(sec, 0, sizeof(*sec));
3026 sec->header = section_headers[i];
3029 if(sec->header.sh_size) switch (sec->header.sh_type) {
3038 if (!loadprogbits) {
3039 sec->contents = NULL;
3046 if (sec->header.sh_size > 0) {
3047 sec->contents = xmalloc(sec->header.sh_size);
3048 fseek(fp, sec->header.sh_offset, SEEK_SET);
3049 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3050 perror_msg("error reading ELF section data");
3054 sec->contents = NULL;
3058 #if SHT_RELM == SHT_REL
3060 error_msg("RELA relocations not supported on this architecture");
3064 error_msg("REL relocations not supported on this architecture");
3069 if (sec->header.sh_type >= SHT_LOPROC) {
3070 /* Assume processor specific section types are debug
3071 info and can safely be ignored. If this is ever not
3072 the case (Hello MIPS?), don't put ifdefs here but
3073 create an arch_load_proc_section(). */
3077 error_msg("can't handle sections of type %ld",
3078 (long) sec->header.sh_type);
3083 /* Do what sort of interpretation as needed by each section. */
3085 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3087 for (i = 0; i < shnum; ++i) {
3088 struct obj_section *sec = f->sections[i];
3089 sec->name = shstrtab + sec->header.sh_name;
3092 for (i = 0; i < shnum; ++i) {
3093 struct obj_section *sec = f->sections[i];
3095 /* .modinfo should be contents only but gcc has no attribute for that.
3096 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3098 if (strcmp(sec->name, ".modinfo") == 0)
3099 sec->header.sh_flags &= ~SHF_ALLOC;
3101 if (sec->header.sh_flags & SHF_ALLOC)
3102 obj_insert_section_load_order(f, sec);
3104 switch (sec->header.sh_type) {
3107 unsigned long nsym, j;
3111 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3112 error_msg("symbol size mismatch: %lu != %lu",
3113 (unsigned long) sec->header.sh_entsize,
3114 (unsigned long) sizeof(ElfW(Sym)));
3118 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3119 strtab = f->sections[sec->header.sh_link]->contents;
3120 sym = (ElfW(Sym) *) sec->contents;
3122 /* Allocate space for a table of local symbols. */
3123 j = f->local_symtab_size = sec->header.sh_info;
3124 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3126 /* Insert all symbols into the hash table. */
3127 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3130 name = strtab + sym->st_name;
3132 name = f->sections[sym->st_shndx]->name;
3134 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3135 sym->st_value, sym->st_size);
3141 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3142 error_msg("relocation entry size mismatch: %lu != %lu",
3143 (unsigned long) sec->header.sh_entsize,
3144 (unsigned long) sizeof(ElfW(RelM)));
3148 /* XXX Relocation code from modutils-2.3.19 is not here.
3149 * Why? That's about 20 lines of code from obj/obj_load.c,
3150 * which gets done in a second pass through the sections.
3151 * This BusyBox insmod does similar work in obj_relocate(). */
3158 #ifdef BB_FEATURE_INSMOD_LOADINKMEM
3160 * load the unloaded sections directly into the memory allocated by
3161 * kernel for the module
3164 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3166 ElfW(Addr) base = f->baseaddr;
3167 struct obj_section* sec;
3169 for (sec = f->load_order; sec; sec = sec->load_next) {
3171 /* section already loaded? */
3172 if (sec->contents != NULL)
3175 if (sec->header.sh_size == 0)
3178 sec->contents = imagebase + (sec->header.sh_addr - base);
3179 fseek(fp, sec->header.sh_offset, SEEK_SET);
3180 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3181 error_msg("error reading ELF section data: %s\n", strerror(errno));
3190 static void hide_special_symbols(struct obj_file *f)
3192 static const char *const specials[] = {
3199 struct obj_symbol *sym;
3200 const char *const *p;
3202 for (p = specials; *p; ++p)
3203 if ((sym = obj_find_symbol(f, *p)) != NULL)
3205 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3210 extern int insmod_main( int argc, char **argv)
3217 unsigned long m_size;
3222 char m_name[FILENAME_MAX + 1] = "\0";
3223 int exit_status = EXIT_FAILURE;
3225 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3226 struct utsname uts_info;
3227 char m_strversion[STRVERSIONLEN];
3232 /* Parse any options */
3233 while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
3235 case 'f': /* force loading */
3236 flag_force_load = 1;
3238 case 'k': /* module loaded by kerneld, auto-cleanable */
3241 case 'v': /* verbose output */
3244 case 'x': /* do not export externs */
3247 case 'o': /* name the output module */
3248 strncpy(m_name, optarg, FILENAME_MAX);
3250 case 'L': /* Stub warning */
3251 /* This is needed for compatibility with modprobe.
3252 * In theory, this does locking, but we don't do
3253 * that. So be careful and plan your life around not
3254 * loading the same module 50 times concurrently. */
3261 if (argv[optind] == NULL) {
3265 /* Grab the module name */
3266 if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3273 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3275 memcpy(m_fullName, tmp, len);
3276 m_fullName[len]='\0';
3277 if (*m_name == '\0') {
3278 strcpy(m_name, m_fullName);
3280 strcat(m_fullName, ".o");
3282 /* Get a filedesc for the module. Check we we have a complete path */
3283 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3284 (fp = fopen(argv[optind], "r")) == NULL) {
3285 struct utsname myuname;
3287 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3288 * but do not error out yet if we fail to find it... */
3289 if (uname(&myuname) == 0) {
3290 char module_dir[FILENAME_MAX];
3291 char real_module_dir[FILENAME_MAX];
3292 snprintf (module_dir, sizeof(module_dir), "%s/%s",
3293 _PATH_MODULES, myuname.release);
3294 /* Jump through hoops in case /lib/modules/`uname -r`
3295 * is a symlink. We do not want recursive_action to
3296 * follow symlinks, but we do want to follow the
3297 * /lib/modules/`uname -r` dir, So resolve it ourselves
3298 * if it is a link... */
3299 if (realpath (module_dir, real_module_dir) == NULL)
3300 strcpy(real_module_dir, module_dir);
3301 recursive_action(real_module_dir, TRUE, FALSE, FALSE,
3302 check_module_name_match, 0, m_fullName);
3305 /* Check if we have found anything yet */
3306 if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL))
3308 char module_dir[FILENAME_MAX];
3309 if (realpath (_PATH_MODULES, module_dir) == NULL)
3310 strcpy(module_dir, _PATH_MODULES);
3311 /* No module found under /lib/modules/`uname -r`, this
3312 * time cast the net a bit wider. Search /lib/modules/ */
3313 if (recursive_action(module_dir, TRUE, FALSE, FALSE,
3314 check_module_name_match, 0, m_fullName) == FALSE)
3316 if (m_filename[0] == '\0'
3317 || ((fp = fopen(m_filename, "r")) == NULL))
3319 error_msg("%s: no module by that name found", m_fullName);
3320 return EXIT_FAILURE;
3323 error_msg_and_die("%s: no module by that name found", m_fullName);
3326 safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
3328 printf("Using %s\n", m_filename);
3330 if ((f = obj_load(fp, LOADBITS)) == NULL)
3331 perror_msg_and_die("Could not load the module");
3333 if (get_modinfo_value(f, "kernel_version") == NULL)
3338 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3339 /* Version correspondence? */
3341 if (uname(&uts_info) < 0)
3342 uts_info.release[0] = '\0';
3343 if (m_has_modinfo) {
3344 m_version = new_get_module_version(f, m_strversion);
3346 m_version = old_get_module_version(f, m_strversion);
3347 if (m_version == -1) {
3348 error_msg("couldn't find the kernel version the module was "
3354 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3355 if (flag_force_load) {
3356 error_msg("Warning: kernel-module version mismatch\n"
3357 "\t%s was compiled for kernel version %s\n"
3358 "\twhile this kernel is version %s",
3359 m_filename, m_strversion, uts_info.release);
3361 error_msg("kernel-module version mismatch\n"
3362 "\t%s was compiled for kernel version %s\n"
3363 "\twhile this kernel is version %s.",
3364 m_filename, m_strversion, uts_info.release);
3369 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3371 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3373 if (k_new_syscalls) {
3374 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
3375 if (!new_get_kernel_symbols())
3377 k_crcs = new_is_kernel_checksummed();
3379 error_msg("Not configured to support new kernels");
3383 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
3384 if (!old_get_kernel_symbols(m_name))
3386 k_crcs = old_is_kernel_checksummed();
3388 error_msg("Not configured to support old kernels");
3393 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3395 m_crcs = new_is_module_checksummed(f);
3397 m_crcs = old_is_module_checksummed(f);
3399 if (m_crcs != k_crcs)
3400 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3401 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3403 /* Let the module know about the kernel symbols. */
3404 add_kernel_symbols(f);
3406 /* Allocate common symbols, symbol tables, and string tables. */
3409 ? !new_create_this_module(f, m_name)
3410 : !old_create_mod_use_count(f))
3415 if (!obj_check_undefineds(f)) {
3418 obj_allocate_commons(f);
3420 /* done with the module name, on to the optional var=value arguments */
3423 if (optind < argc) {
3425 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3426 : !old_process_module_arguments(f, argc - optind, argv + optind))
3433 hide_special_symbols(f);
3436 new_create_module_ksymtab(f);
3438 /* Find current size of the module */
3439 m_size = obj_load_size(f);
3442 m_addr = create_module(m_name, m_size);
3443 if (m_addr==-1) switch (errno) {
3445 error_msg("A module named %s already exists", m_name);
3448 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3452 perror_msg("create_module: %s", m_name);
3458 * the PROGBITS section was not loaded by the obj_load
3459 * now we can load them directly into the kernel memory
3461 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3462 delete_module(m_name);
3467 if (!obj_relocate(f, m_addr)) {
3468 delete_module(m_name);
3473 ? !new_init_module(m_name, f, m_size)
3474 : !old_init_module(m_name, f, m_size))
3476 delete_module(m_name);
3480 exit_status = EXIT_SUCCESS;
3484 return(exit_status);