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.64 2001/05/21 16:09:18 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.64 2001/05/21 16:09:18 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(__m68k__))
376 #define ELFDATAM ELFDATA2MSB
378 #define ELFDATAM ELFDATA2LSB
385 #define MATCH_MACHINE(x) (x == EM_SH)
386 #define SHT_RELM SHT_RELA
387 #define Elf32_RelM Elf32_Rela
388 #define ELFDATAM ELFDATA2LSB
390 #elif defined(__arm__)
392 #define MATCH_MACHINE(x) (x == EM_ARM)
393 #define SHT_RELM SHT_REL
394 #define Elf32_RelM Elf32_Rel
395 #define ELFDATAM ELFDATA2LSB
397 #elif defined(__powerpc__)
399 #define MATCH_MACHINE(x) (x == EM_PPC)
400 #define SHT_RELM SHT_RELA
401 #define Elf32_RelM Elf32_Rela
402 #define ELFDATAM ELFDATA2MSB
404 #elif defined(__mips__)
406 /* Account for ELF spec changes. */
407 #ifndef EM_MIPS_RS3_LE
408 #ifdef EM_MIPS_RS4_BE
409 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
411 #define EM_MIPS_RS3_LE 10
413 #endif /* !EM_MIPS_RS3_LE */
415 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
416 #define SHT_RELM SHT_REL
417 #define Elf32_RelM Elf32_Rel
419 #define ELFDATAM ELFDATA2MSB
422 #define ELFDATAM ELFDATA2LSB
425 #elif defined(__i386__)
427 /* presumably we can use these for anything but the SH and ARM*/
428 /* this is the previous behavior, but it does result in
429 insmod.c being broken on anything except i386 */
431 #define MATCH_MACHINE(x) (x == EM_386)
433 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
436 #define SHT_RELM SHT_REL
437 #define Elf32_RelM Elf32_Rel
438 #define ELFDATAM ELFDATA2LSB
440 #elif defined(__m68k__)
442 #define MATCH_MACHINE(x) (x == EM_68K)
443 #define SHT_RELM SHT_RELA
444 #define Elf32_RelM Elf32_Rela
447 #error Sorry, but insmod.c does not yet support this architecture...
451 # if ELFCLASSM == ELFCLASS32
452 # define ElfW(x) Elf32_ ## x
453 # define ELFW(x) ELF32_ ## x
455 # define ElfW(x) Elf64_ ## x
456 # define ELFW(x) ELF64_ ## x
460 /* For some reason this is missing from libc5. */
461 #ifndef ELF32_ST_INFO
462 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
465 #ifndef ELF64_ST_INFO
466 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
469 struct obj_string_patch;
470 struct obj_symbol_patch;
477 struct obj_section *load_next;
483 struct obj_symbol *next; /* hash table link */
487 int secidx; /* the defining section index/module */
489 int ksymidx; /* for export to the kernel symtab */
490 int referenced; /* actually used in the link */
493 /* Hardcode the hash table size. We shouldn't be needing so many
494 symbols that we begin to degrade performance, and we get a big win
495 by giving the compiler a constant divisor. */
497 #define HASH_BUCKETS 521
503 struct obj_section **sections;
504 struct obj_section *load_order;
505 struct obj_section **load_order_search_start;
506 struct obj_string_patch *string_patches;
507 struct obj_symbol_patch *symbol_patches;
508 int (*symbol_cmp)(const char *, const char *);
509 unsigned long (*symbol_hash)(const char *);
510 unsigned long local_symtab_size;
511 struct obj_symbol **local_symtab;
512 struct obj_symbol *symtab[HASH_BUCKETS];
523 struct obj_string_patch
525 struct obj_string_patch *next;
527 ElfW(Addr) reloc_offset;
528 ElfW(Addr) string_offset;
531 struct obj_symbol_patch
533 struct obj_symbol_patch *next;
535 ElfW(Addr) reloc_offset;
536 struct obj_symbol *sym;
540 /* Generic object manipulation routines. */
542 unsigned long obj_elf_hash(const char *);
544 unsigned long obj_elf_hash_n(const char *, unsigned long len);
546 struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
547 unsigned long symidx, int info, int secidx,
548 ElfW(Addr) value, unsigned long size);
550 struct obj_symbol *obj_find_symbol (struct obj_file *f,
553 ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
554 struct obj_symbol *sym);
556 void obj_set_symbol_compare(struct obj_file *f,
557 int (*cmp)(const char *, const char *),
558 unsigned long (*hash)(const char *));
560 struct obj_section *obj_find_section (struct obj_file *f,
563 void obj_insert_section_load_order (struct obj_file *f,
564 struct obj_section *sec);
566 struct obj_section *obj_create_alloced_section (struct obj_file *f,
571 struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
576 void *obj_extend_section (struct obj_section *sec, unsigned long more);
578 int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
581 int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
582 struct obj_symbol *sym);
584 int obj_check_undefineds(struct obj_file *f);
586 void obj_allocate_commons(struct obj_file *f);
588 unsigned long obj_load_size (struct obj_file *f);
590 int obj_relocate (struct obj_file *f, ElfW(Addr) base);
592 struct obj_file *obj_load(FILE *f, int loadprogbits);
594 int obj_create_image (struct obj_file *f, char *image);
596 /* Architecture specific manipulation routines. */
598 struct obj_file *arch_new_file (void);
600 struct obj_section *arch_new_section (void);
602 struct obj_symbol *arch_new_symbol (void);
604 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 int arch_create_got (struct obj_file *f);
613 int arch_init_module (struct obj_file *f, struct new_module *);
616 //----------------------------------------------------------------------------
617 //--------end of modutils obj.h
618 //----------------------------------------------------------------------------
624 #define _PATH_MODULES "/lib/modules"
625 static const int STRVERSIONLEN = 32;
627 /*======================================================================*/
629 int flag_force_load = 0;
630 int flag_autoclean = 0;
631 int flag_verbose = 0;
635 /*======================================================================*/
637 /* previously, these were named i386_* but since we could be
638 compiling for the sh, I've renamed them to the more general
639 arch_* These structures are the same between the x86 and SH,
640 and we can't support anything else right now anyway. In the
641 future maybe they should be #if defined'd */
647 #if defined(BB_USE_PLT_ENTRIES)
648 struct arch_plt_entry
652 int inited:1; /* has been set up */
656 #if defined(BB_USE_GOT_ENTRIES)
657 struct arch_got_entry {
659 unsigned offset_done:1;
660 unsigned reloc_done:1;
664 #if defined(__mips__)
667 struct mips_hi16 *next;
674 struct obj_file root;
675 #if defined(BB_USE_PLT_ENTRIES)
676 struct obj_section *plt;
678 #if defined(BB_USE_GOT_ENTRIES)
679 struct obj_section *got;
681 #if defined(__mips__)
682 struct mips_hi16 *mips_hi16_list;
687 struct obj_symbol root;
688 #if defined(BB_USE_PLT_ENTRIES)
689 struct arch_plt_entry pltent;
691 #if defined(BB_USE_GOT_ENTRIES)
692 struct arch_got_entry gotent;
697 struct external_module {
702 struct new_module_symbol *syms;
705 struct new_module_symbol *ksyms;
708 struct external_module *ext_modules;
710 int n_ext_modules_used;
711 extern int delete_module(const char *);
713 static char m_filename[FILENAME_MAX + 1];
714 static char m_fullName[FILENAME_MAX + 1];
718 /*======================================================================*/
721 static int check_module_name_match(const char *filename, struct stat *statbuf,
724 char *fullname = (char *) userdata;
726 if (fullname[0] == '\0')
729 char *tmp, *tmp1 = strdup(filename);
730 tmp = get_last_path_component(tmp1);
731 if (strcmp(tmp, fullname) == 0) {
733 /* Stop searching if we find a match */
734 safe_strncpy(m_filename, filename, sizeof(m_filename));
743 /*======================================================================*/
745 struct obj_file *arch_new_file(void)
748 f = xmalloc(sizeof(*f));
750 #if defined(BB_USE_PLT_ENTRIES)
753 #if defined(BB_USE_GOT_ENTRIES)
756 #if defined(__mips__)
757 f->mips_hi16_list = NULL;
763 struct obj_section *arch_new_section(void)
765 return xmalloc(sizeof(struct obj_section));
768 struct obj_symbol *arch_new_symbol(void)
770 struct arch_symbol *sym;
771 sym = xmalloc(sizeof(*sym));
773 #if defined(BB_USE_PLT_ENTRIES)
774 memset(&sym->pltent, 0, sizeof(sym->pltent));
776 #if defined(BB_USE_GOT_ENTRIES)
777 memset(&sym->gotent, 0, sizeof(sym->gotent));
784 arch_apply_relocation(struct obj_file *f,
785 struct obj_section *targsec,
786 struct obj_section *symsec,
787 struct obj_symbol *sym,
788 ElfW(RelM) *rel, ElfW(Addr) v)
790 struct arch_file *ifile = (struct arch_file *) f;
791 #if !(defined(__mips__))
792 struct arch_symbol *isym = (struct arch_symbol *) sym;
795 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
796 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
797 #if defined(BB_USE_GOT_ENTRIES)
798 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
800 #if defined(BB_USE_PLT_ENTRIES)
801 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
802 struct arch_plt_entry *pe;
805 enum obj_reloc ret = obj_reloc_ok;
807 switch (ELF32_R_TYPE(rel->r_info)) {
809 /* even though these constants seem to be the same for
810 the i386 and the sh, we "#if define" them for clarity
811 and in case that ever changes */
814 #elif defined(__arm__)
816 #elif defined(__i386__)
818 #elif defined(__m68k__)
820 #elif defined(__powerpc__)
822 #elif defined(__mips__)
829 #elif defined(__arm__)
831 #elif defined(__i386__)
833 #elif defined(__m68k__)
835 #elif defined(__powerpc__)
837 #elif defined(__mips__)
842 #if defined(__m68k__)
845 ret = obj_reloc_overflow;
850 ret = obj_reloc_overflow;
853 #endif /* __m68k__ */
855 #if defined(__powerpc__)
856 case R_PPC_ADDR16_HA:
857 *(unsigned short *)loc = (v + 0x8000) >> 16;
860 case R_PPC_ADDR16_HI:
861 *(unsigned short *)loc = v >> 16;
864 case R_PPC_ADDR16_LO:
865 *(unsigned short *)loc = v;
869 #if defined(__mips__)
872 ret = obj_reloc_dangerous;
873 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
874 ret = obj_reloc_overflow;
876 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
884 /* We cannot relocate this one now because we don't know the value
885 of the carry we need to add. Save the information, and let LO16
886 do the actual relocation. */
887 n = (struct mips_hi16 *) xmalloc(sizeof *n);
890 n->next = ifile->mips_hi16_list;
891 ifile->mips_hi16_list = n;
897 unsigned long insnlo = *loc;
898 Elf32_Addr val, vallo;
900 /* Sign extend the addend we extract from the lo insn. */
901 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
903 if (ifile->mips_hi16_list != NULL) {
906 l = ifile->mips_hi16_list;
908 struct mips_hi16 *next;
911 /* The value for the HI16 had best be the same. */
912 assert(v == l->value);
914 /* Do the HI16 relocation. Note that we actually don't
915 need to know anything about the LO16 itself, except where
916 to find the low 16 bits of the addend needed by the LO16. */
919 ((insn & 0xffff) << 16) +
923 /* Account for the sign extension that will happen in the
930 insn = (insn & ~0xffff) | val;
938 ifile->mips_hi16_list = NULL;
941 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
943 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
950 #elif defined(__sh__)
954 #elif defined(__i386__)
959 #elif defined(__m68k__)
962 if ((Elf32_Sword)v > 0x7f || (Elf32_Sword)v < -(Elf32_Sword)0x80)
963 ret = obj_reloc_overflow;
968 if ((Elf32_Sword)v > 0x7fff || (Elf32_Sword)v < -(Elf32_Sword)0x8000)
969 ret = obj_reloc_overflow;
973 *(int *)loc = v - dot;
975 #elif defined(__powerpc__)
985 #elif defined(__i386__)
988 #if defined(BB_USE_PLT_ENTRIES)
994 #if defined(__powerpc__)
997 /* find the plt entry and initialize it if necessary */
998 assert(isym != NULL);
1000 pe = (struct arch_plt_entry*) &isym->pltent;
1003 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1005 /* generate some machine code */
1007 #if defined(__arm__)
1008 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1009 ip[1] = v; /* sym@ */
1011 #if defined(__powerpc__)
1012 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1013 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1014 ip[2] = 0x7d6903a6; /* mtctr r11 */
1015 ip[3] = 0x4e800420; /* bctr */
1020 /* relative distance to target */
1022 /* if the target is too far away.... */
1023 if ((int)v < -0x02000000 || (int)v >= 0x02000000) {
1024 /* go via the plt */
1025 v = plt + pe->offset - dot;
1028 ret = obj_reloc_dangerous;
1030 /* merge the offset into the instruction. */
1031 #if defined(__arm__)
1032 /* Convert to words. */
1035 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1037 #if defined(__powerpc__)
1038 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1041 #endif /* BB_USE_PLT_ENTRIES */
1043 #if defined(__arm__)
1044 #elif defined(__sh__)
1049 #elif defined(__i386__)
1050 case R_386_GLOB_DAT:
1051 case R_386_JMP_SLOT:
1054 #elif defined(__m68k__)
1055 case R_68K_GLOB_DAT:
1056 case R_68K_JMP_SLOT:
1061 #if defined(__arm__)
1062 #elif defined(__sh__)
1064 *loc += f->baseaddr + rel->r_addend;
1066 #elif defined(__i386__)
1067 case R_386_RELATIVE:
1068 *loc += f->baseaddr;
1070 #elif defined(__m68k__)
1071 case R_68K_RELATIVE:
1072 *(int *)loc += f->baseaddr;
1076 #if defined(BB_USE_GOT_ENTRIES)
1078 #if !defined(__68k__)
1081 #elif defined(__arm__)
1083 #elif defined(__i386__)
1088 *loc += got - dot + rel->r_addend;;
1089 #elif defined(__i386__) || defined(__arm__) || defined(__m68k_)
1097 #elif defined(__arm__)
1099 #elif defined(__i386__)
1101 #elif defined(__m68k__)
1104 assert(isym != NULL);
1105 /* needs an entry in the .got: set it, once */
1106 if (!isym->gotent.reloc_done) {
1107 isym->gotent.reloc_done = 1;
1108 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1110 /* make the reloc with_respect_to_.got */
1112 *loc += isym->gotent.offset + rel->r_addend;
1113 #elif defined(__i386__) || defined(__arm__) || defined(__m68k__)
1114 *loc += isym->gotent.offset;
1118 /* address relative to the got */
1119 #if !defined(__m68k__)
1122 #elif defined(__arm__)
1124 #elif defined(__i386__)
1126 #elif defined(__m68k__)
1134 #endif /* BB_USE_GOT_ENTRIES */
1137 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1138 ret = obj_reloc_unhandled;
1145 int arch_create_got(struct obj_file *f)
1147 #if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
1148 struct arch_file *ifile = (struct arch_file *) f;
1150 #if defined(BB_USE_GOT_ENTRIES)
1151 int got_offset = 0, gotneeded = 0;
1153 #if defined(BB_USE_PLT_ENTRIES)
1154 int plt_offset = 0, pltneeded = 0;
1156 struct obj_section *relsec, *symsec, *strsec;
1157 ElfW(RelM) *rel, *relend;
1158 ElfW(Sym) *symtab, *extsym;
1159 const char *strtab, *name;
1160 struct arch_symbol *intsym;
1162 for (i = 0; i < f->header.e_shnum; ++i) {
1163 relsec = f->sections[i];
1164 if (relsec->header.sh_type != SHT_RELM)
1167 symsec = f->sections[relsec->header.sh_link];
1168 strsec = f->sections[symsec->header.sh_link];
1170 rel = (ElfW(RelM) *) relsec->contents;
1171 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1172 symtab = (ElfW(Sym) *) symsec->contents;
1173 strtab = (const char *) strsec->contents;
1175 for (; rel < relend; ++rel) {
1176 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1178 switch (ELF32_R_TYPE(rel->r_info)) {
1179 #if defined(__arm__)
1182 #elif defined(__sh__)
1185 #elif defined(__i386__)
1188 #elif defined(__m68k__)
1193 #if defined(__powerpc__)
1199 #if defined(__arm__)
1208 if (got_offset == 0)
1210 #elif defined(__sh__)
1214 #elif defined(__i386__)
1224 if (extsym->st_name != 0) {
1225 name = strtab + extsym->st_name;
1227 name = f->sections[extsym->st_shndx]->name;
1229 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1230 #if defined(BB_USE_GOT_ENTRIES)
1231 if (!intsym->gotent.offset_done) {
1232 intsym->gotent.offset_done = 1;
1233 intsym->gotent.offset = got_offset;
1234 got_offset += BB_GOT_ENTRY_SIZE;
1237 #if defined(BB_USE_PLT_ENTRIES)
1238 if (pltneeded && intsym->pltent.allocated == 0) {
1239 intsym->pltent.allocated = 1;
1240 intsym->pltent.offset = plt_offset;
1241 plt_offset += BB_PLT_ENTRY_SIZE;
1242 intsym->pltent.inited = 0;
1249 #if defined(BB_USE_GOT_ENTRIES)
1251 struct obj_section* myrelsec = obj_find_section(f, ".got");
1254 obj_extend_section(myrelsec, got_offset);
1256 myrelsec = obj_create_alloced_section(f, ".got",
1262 ifile->got = myrelsec;
1266 #if defined(BB_USE_PLT_ENTRIES)
1268 ifile->plt = obj_create_alloced_section(f, ".plt",
1276 int arch_init_module(struct obj_file *f, struct new_module *mod)
1282 /*======================================================================*/
1284 /* Standard ELF hash function. */
1285 inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1287 unsigned long h = 0;
1294 if ((g = (h & 0xf0000000)) != 0) {
1303 unsigned long obj_elf_hash(const char *name)
1305 return obj_elf_hash_n(name, strlen(name));
1308 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1309 /* Get the kernel version in the canonical integer form. */
1311 static int get_kernel_version(char str[STRVERSIONLEN])
1313 struct utsname uts_info;
1317 if (uname(&uts_info) < 0)
1319 strncpy(str, uts_info.release, STRVERSIONLEN);
1320 p = uts_info.release;
1322 a = strtoul(p, &p, 10);
1325 b = strtoul(p + 1, &p, 10);
1328 c = strtoul(p + 1, &q, 10);
1332 return a << 16 | b << 8 | c;
1335 /* String comparison for non-co-versioned kernel and module. */
1337 static int ncv_strcmp(const char *a, const char *b)
1339 size_t alen = strlen(a), blen = strlen(b);
1341 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1342 return strncmp(a, b, alen);
1343 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1344 return strncmp(a, b, blen);
1346 return strcmp(a, b);
1349 /* String hashing for non-co-versioned kernel and module. Here
1350 we are simply forced to drop the crc from the hash. */
1352 static unsigned long ncv_symbol_hash(const char *str)
1354 size_t len = strlen(str);
1355 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1357 return obj_elf_hash_n(str, len);
1361 obj_set_symbol_compare(struct obj_file *f,
1362 int (*cmp) (const char *, const char *),
1363 unsigned long (*hash) (const char *))
1366 f->symbol_cmp = cmp;
1368 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1371 f->symbol_hash = hash;
1373 memcpy(tmptab, f->symtab, sizeof(tmptab));
1374 memset(f->symtab, 0, sizeof(f->symtab));
1376 for (i = 0; i < HASH_BUCKETS; ++i)
1377 for (sym = tmptab[i]; sym; sym = next) {
1378 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1380 sym->next = f->symtab[h];
1386 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1389 struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
1390 unsigned long symidx, int info,
1391 int secidx, ElfW(Addr) value,
1394 struct obj_symbol *sym;
1395 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1396 int n_type = ELFW(ST_TYPE) (info);
1397 int n_binding = ELFW(ST_BIND) (info);
1399 for (sym = f->symtab[hash]; sym; sym = sym->next)
1400 if (f->symbol_cmp(sym->name, name) == 0) {
1401 int o_secidx = sym->secidx;
1402 int o_info = sym->info;
1403 int o_type = ELFW(ST_TYPE) (o_info);
1404 int o_binding = ELFW(ST_BIND) (o_info);
1406 /* A redefinition! Is it legal? */
1408 if (secidx == SHN_UNDEF)
1410 else if (o_secidx == SHN_UNDEF)
1412 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1413 /* Cope with local and global symbols of the same name
1414 in the same object file, as might have been created
1415 by ld -r. The only reason locals are now seen at this
1416 level at all is so that we can do semi-sensible things
1419 struct obj_symbol *nsym, **p;
1421 nsym = arch_new_symbol();
1422 nsym->next = sym->next;
1425 /* Excise the old (local) symbol from the hash chain. */
1426 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1430 } else if (n_binding == STB_LOCAL) {
1431 /* Another symbol of the same name has already been defined.
1432 Just add this to the local table. */
1433 sym = arch_new_symbol();
1436 f->local_symtab[symidx] = sym;
1438 } else if (n_binding == STB_WEAK)
1440 else if (o_binding == STB_WEAK)
1442 /* Don't unify COMMON symbols with object types the programmer
1444 else if (secidx == SHN_COMMON
1445 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1447 else if (o_secidx == SHN_COMMON
1448 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1451 /* Don't report an error if the symbol is coming from
1452 the kernel or some external module. */
1453 if (secidx <= SHN_HIRESERVE)
1454 error_msg("%s multiply defined", name);
1459 /* Completely new symbol. */
1460 sym = arch_new_symbol();
1461 sym->next = f->symtab[hash];
1462 f->symtab[hash] = sym;
1465 if (ELFW(ST_BIND) (info) == STB_LOCAL)
1466 f->local_symtab[symidx] = sym;
1472 sym->secidx = secidx;
1478 struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
1480 struct obj_symbol *sym;
1481 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1483 for (sym = f->symtab[hash]; sym; sym = sym->next)
1484 if (f->symbol_cmp(sym->name, name) == 0)
1491 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1494 if (sym->secidx >= SHN_LORESERVE)
1497 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1499 /* As a special case, a NULL sym has value zero. */
1504 struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1506 int i, n = f->header.e_shnum;
1508 for (i = 0; i < n; ++i)
1509 if (strcmp(f->sections[i]->name, name) == 0)
1510 return f->sections[i];
1515 static int obj_load_order_prio(struct obj_section *a)
1517 unsigned long af, ac;
1519 af = a->header.sh_flags;
1522 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1523 strcmp(a->name + 5, ".init"))
1527 if (!(af & SHF_WRITE))
1529 if (af & SHF_EXECINSTR)
1531 if (a->header.sh_type != SHT_NOBITS)
1538 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1540 struct obj_section **p;
1541 int prio = obj_load_order_prio(sec);
1542 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1543 if (obj_load_order_prio(*p) < prio)
1545 sec->load_next = *p;
1549 struct obj_section *obj_create_alloced_section(struct obj_file *f,
1551 unsigned long align,
1554 int newidx = f->header.e_shnum++;
1555 struct obj_section *sec;
1557 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1558 f->sections[newidx] = sec = arch_new_section();
1560 memset(sec, 0, sizeof(*sec));
1561 sec->header.sh_type = SHT_PROGBITS;
1562 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1563 sec->header.sh_size = size;
1564 sec->header.sh_addralign = align;
1568 sec->contents = xmalloc(size);
1570 obj_insert_section_load_order(f, sec);
1575 struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1577 unsigned long align,
1580 int newidx = f->header.e_shnum++;
1581 struct obj_section *sec;
1583 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1584 f->sections[newidx] = sec = arch_new_section();
1586 memset(sec, 0, sizeof(*sec));
1587 sec->header.sh_type = SHT_PROGBITS;
1588 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1589 sec->header.sh_size = size;
1590 sec->header.sh_addralign = align;
1594 sec->contents = xmalloc(size);
1596 sec->load_next = f->load_order;
1597 f->load_order = sec;
1598 if (f->load_order_search_start == &f->load_order)
1599 f->load_order_search_start = &sec->load_next;
1604 void *obj_extend_section(struct obj_section *sec, unsigned long more)
1606 unsigned long oldsize = sec->header.sh_size;
1608 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1610 return sec->contents + oldsize;
1615 /* Conditionally add the symbols from the given symbol set to the
1621 int idx, struct new_module_symbol *syms, size_t nsyms)
1623 struct new_module_symbol *s;
1627 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1629 /* Only add symbols that are already marked external. If we
1630 override locals we may cause problems for argument initialization.
1631 We will also create a false dependency on the module. */
1632 struct obj_symbol *sym;
1634 sym = obj_find_symbol(f, (char *) s->name);
1635 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1636 sym = obj_add_symbol(f, (char *) s->name, -1,
1637 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1639 /* Did our symbol just get installed? If so, mark the
1640 module as "used". */
1641 if (sym->secidx == idx)
1649 static void add_kernel_symbols(struct obj_file *f)
1651 struct external_module *m;
1654 /* Add module symbols first. */
1656 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1658 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1659 m->nsyms)) m->used = 1, ++nused;
1661 n_ext_modules_used = nused;
1663 /* And finally the symbols from the kernel proper. */
1666 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1669 static char *get_modinfo_value(struct obj_file *f, const char *key)
1671 struct obj_section *sec;
1672 char *p, *v, *n, *ep;
1673 size_t klen = strlen(key);
1675 sec = obj_find_section(f, ".modinfo");
1679 ep = p + sec->header.sh_size;
1682 n = strchr(p, '\0');
1684 if (p + klen == v && strncmp(p, key, klen) == 0)
1687 if (p + klen == n && strcmp(p, key) == 0)
1697 /*======================================================================*/
1698 /* Functions relating to module loading in pre 2.1 kernels. */
1701 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1705 struct obj_symbol *sym;
1709 if ((q = strchr(p, '=')) == NULL) {
1715 sym = obj_find_symbol(f, p);
1717 /* Also check that the parameter was not resolved from the kernel. */
1718 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1719 error_msg("symbol for parameter %s not found", p);
1723 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1725 /* Do C quoting if we begin with a ". */
1729 str = alloca(strlen(q));
1730 for (r = str, q++; *q != '"'; ++q, ++r) {
1732 error_msg("improperly terminated string argument for %s", p);
1734 } else if (*q == '\\')
1768 if (q[1] >= '0' && q[1] <= '7') {
1769 c = (c * 8) + *++q - '0';
1770 if (q[1] >= '0' && q[1] <= '7')
1771 c = (c * 8) + *++q - '0';
1784 obj_string_patch(f, sym->secidx, sym->value, str);
1785 } else if (*q >= '0' && *q <= '9') {
1787 *loc++ = strtoul(q, &q, 0);
1788 while (*q++ == ',');
1790 char *contents = f->sections[sym->secidx]->contents;
1791 char *myloc = contents + sym->value;
1792 char *r; /* To search for commas */
1794 /* Break the string with comas */
1795 while ((r = strchr(q, ',')) != (char *) NULL) {
1797 obj_string_patch(f, sym->secidx, myloc - contents, q);
1798 myloc += sizeof(char *);
1803 obj_string_patch(f, sym->secidx, myloc - contents, q);
1812 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1813 static int old_is_module_checksummed(struct obj_file *f)
1815 return obj_find_symbol(f, "Using_Versions") != NULL;
1817 /* Get the module's kernel version in the canonical integer form. */
1820 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1822 struct obj_symbol *sym;
1826 sym = obj_find_symbol(f, "kernel_version");
1830 p = f->sections[sym->secidx]->contents + sym->value;
1831 strncpy(str, p, STRVERSIONLEN);
1833 a = strtoul(p, &p, 10);
1836 b = strtoul(p + 1, &p, 10);
1839 c = strtoul(p + 1, &q, 10);
1843 return a << 16 | b << 8 | c;
1846 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1848 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
1850 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1852 static int old_get_kernel_symbols(const char *m_name)
1854 struct old_kernel_sym *ks, *k;
1855 struct new_module_symbol *s;
1856 struct external_module *mod;
1857 int nks, nms, nmod, i;
1859 nks = get_kernel_syms(NULL);
1862 perror_msg("get_kernel_syms: %s", m_name);
1864 error_msg("No kernel symbols");
1868 ks = k = xmalloc(nks * sizeof(*ks));
1870 if (get_kernel_syms(ks) != nks) {
1871 perror("inconsistency with get_kernel_syms -- is someone else "
1872 "playing with modules?");
1877 /* Collect the module information. */
1882 while (k->name[0] == '#' && k->name[1]) {
1883 struct old_kernel_sym *k2;
1885 /* Find out how many symbols this module has. */
1886 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1890 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1891 mod[nmod].name = k->name + 1;
1892 mod[nmod].addr = k->value;
1894 mod[nmod].nsyms = nms;
1895 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1897 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1898 s->name = (unsigned long) k->name;
1899 s->value = k->value;
1906 n_ext_modules = nmod + 1;
1908 /* Now collect the symbols for the kernel proper. */
1910 if (k->name[0] == '#')
1913 nksyms = nms = nks - (k - ks);
1914 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1916 for (i = 0; i < nms; ++i, ++s, ++k) {
1917 s->name = (unsigned long) k->name;
1918 s->value = k->value;
1924 /* Return the kernel symbol checksum version, or zero if not used. */
1926 static int old_is_kernel_checksummed(void)
1928 /* Using_Versions is the first symbol. */
1930 && strcmp((char *) ksyms[0].name,
1931 "Using_Versions") == 0) return ksyms[0].value;
1937 static int old_create_mod_use_count(struct obj_file *f)
1939 struct obj_section *sec;
1941 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1944 obj_add_symbol(f, "mod_use_count_", -1,
1945 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1952 old_init_module(const char *m_name, struct obj_file *f,
1953 unsigned long m_size)
1956 struct old_mod_routines routines;
1957 struct old_symbol_table *symtab;
1960 /* Create the symbol table */
1962 int nsyms = 0, strsize = 0, total;
1964 /* Size things first... */
1967 for (i = 0; i < HASH_BUCKETS; ++i) {
1968 struct obj_symbol *sym;
1969 for (sym = f->symtab[i]; sym; sym = sym->next)
1970 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1971 && sym->secidx <= SHN_HIRESERVE)
1973 sym->ksymidx = nsyms++;
1974 strsize += strlen(sym->name) + 1;
1979 total = (sizeof(struct old_symbol_table)
1980 + nsyms * sizeof(struct old_module_symbol)
1981 + n_ext_modules_used * sizeof(struct old_module_ref)
1983 symtab = xmalloc(total);
1984 symtab->size = total;
1985 symtab->n_symbols = nsyms;
1986 symtab->n_refs = n_ext_modules_used;
1988 if (flag_export && nsyms) {
1989 struct old_module_symbol *ksym;
1993 ksym = symtab->symbol;
1994 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1995 + n_ext_modules_used * sizeof(struct old_module_ref));
1997 for (i = 0; i < HASH_BUCKETS; ++i) {
1998 struct obj_symbol *sym;
1999 for (sym = f->symtab[i]; sym; sym = sym->next)
2000 if (sym->ksymidx >= 0) {
2001 ksym->addr = obj_symbol_final_value(f, sym);
2003 (unsigned long) str - (unsigned long) symtab;
2005 strcpy(str, sym->name);
2006 str += strlen(sym->name) + 1;
2012 if (n_ext_modules_used) {
2013 struct old_module_ref *ref;
2016 ref = (struct old_module_ref *)
2017 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2019 for (i = 0; i < n_ext_modules; ++i)
2020 if (ext_modules[i].used)
2021 ref++->module = ext_modules[i].addr;
2025 /* Fill in routines. */
2028 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2030 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2032 /* Whew! All of the initialization is complete. Collect the final
2033 module image and give it to the kernel. */
2035 image = xmalloc(m_size);
2036 obj_create_image(f, image);
2038 /* image holds the complete relocated module, accounting correctly for
2039 mod_use_count. However the old module kernel support assume that
2040 it is receiving something which does not contain mod_use_count. */
2041 ret = old_sys_init_module(m_name, image + sizeof(long),
2042 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2043 : 0), &routines, symtab);
2045 perror_msg("init_module: %s", m_name);
2055 #define old_create_mod_use_count(x) TRUE
2056 #define old_init_module(x, y, z) TRUE
2058 #endif /* BB_FEATURE_OLD_MODULE_INTERFACE */
2062 /*======================================================================*/
2063 /* Functions relating to module loading after 2.1.18. */
2066 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2070 struct obj_symbol *sym;
2071 char *contents, *loc;
2075 if ((q = strchr(p, '=')) == NULL) {
2080 key = alloca(q - p + 6);
2081 memcpy(key, "parm_", 5);
2082 memcpy(key + 5, p, q - p);
2085 p = get_modinfo_value(f, key);
2088 error_msg("invalid parameter %s", key);
2092 sym = obj_find_symbol(f, key);
2094 /* Also check that the parameter was not resolved from the kernel. */
2095 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2096 error_msg("symbol for parameter %s not found", key);
2101 min = strtoul(p, &p, 10);
2103 max = strtoul(p + 1, &p, 10);
2109 contents = f->sections[sym->secidx]->contents;
2110 loc = contents + sym->value;
2114 if ((*p == 's') || (*p == 'c')) {
2117 /* Do C quoting if we begin with a ", else slurp the lot. */
2121 str = alloca(strlen(q));
2122 for (r = str, q++; *q != '"'; ++q, ++r) {
2124 error_msg("improperly terminated string argument for %s",
2127 } else if (*q == '\\')
2161 if (q[1] >= '0' && q[1] <= '7') {
2162 c = (c * 8) + *++q - '0';
2163 if (q[1] >= '0' && q[1] <= '7')
2164 c = (c * 8) + *++q - '0';
2181 /* In this case, the string is not quoted. We will break
2182 it using the coma (like for ints). If the user wants to
2183 include comas in a string, he just has to quote it */
2185 /* Search the next coma */
2189 if (r != (char *) NULL) {
2190 /* Recopy the current field */
2191 str = alloca(r - q + 1);
2192 memcpy(str, q, r - q);
2194 /* I don't know if it is usefull, as the previous case
2195 doesn't null terminate the string ??? */
2198 /* Keep next fields */
2209 obj_string_patch(f, sym->secidx, loc - contents, str);
2210 loc += tgt_sizeof_char_p;
2212 /* Array of chars (in fact, matrix !) */
2213 unsigned long charssize; /* size of each member */
2215 /* Get the size of each member */
2216 /* Probably we should do that outside the loop ? */
2217 if (!isdigit(*(p + 1))) {
2218 error_msg("parameter type 'c' for %s must be followed by"
2219 " the maximum size", key);
2222 charssize = strtoul(p + 1, (char **) NULL, 10);
2225 if (strlen(str) >= charssize) {
2226 error_msg("string too long for %s (max %ld)", key,
2231 /* Copy to location */
2232 strcpy((char *) loc, str);
2236 long v = strtoul(q, &q, 0);
2243 loc += tgt_sizeof_short;
2247 loc += tgt_sizeof_int;
2251 loc += tgt_sizeof_long;
2255 error_msg("unknown parameter type '%c' for %s", *p, key);
2270 goto retry_end_of_value;
2274 error_msg("too many values for %s (max %d)", key, max);
2281 error_msg("invalid argument syntax for %s", key);
2288 error_msg("too few values for %s (min %d)", key, min);
2298 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2299 static int new_is_module_checksummed(struct obj_file *f)
2301 const char *p = get_modinfo_value(f, "using_checksums");
2308 /* Get the module's kernel version in the canonical integer form. */
2311 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2316 p = get_modinfo_value(f, "kernel_version");
2319 strncpy(str, p, STRVERSIONLEN);
2321 a = strtoul(p, &p, 10);
2324 b = strtoul(p + 1, &p, 10);
2327 c = strtoul(p + 1, &q, 10);
2331 return a << 16 | b << 8 | c;
2334 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2337 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
2339 /* Fetch the loaded modules, and all currently exported symbols. */
2341 static int new_get_kernel_symbols(void)
2343 char *module_names, *mn;
2344 struct external_module *modules, *m;
2345 struct new_module_symbol *syms, *s;
2346 size_t ret, bufsize, nmod, nsyms, i, j;
2348 /* Collect the loaded modules. */
2350 module_names = xmalloc(bufsize = 256);
2352 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2353 if (errno == ENOSPC && bufsize < ret) {
2354 module_names = xrealloc(module_names, bufsize = ret);
2355 goto retry_modules_load;
2357 perror_msg("QM_MODULES");
2361 n_ext_modules = nmod = ret;
2363 /* Collect the modules' symbols. */
2366 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2367 memset(modules, 0, nmod * sizeof(*modules));
2368 for (i = 0, mn = module_names, m = modules;
2369 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2370 struct new_module_info info;
2372 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2373 if (errno == ENOENT) {
2374 /* The module was removed out from underneath us. */
2377 perror_msg("query_module: QM_INFO: %s", mn);
2381 syms = xmalloc(bufsize = 1024);
2383 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2386 syms = xrealloc(syms, bufsize = ret);
2387 goto retry_mod_sym_load;
2389 /* The module was removed out from underneath us. */
2392 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2399 m->addr = info.addr;
2403 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2404 s->name += (unsigned long) syms;
2409 /* Collect the kernel's symbols. */
2411 syms = xmalloc(bufsize = 16 * 1024);
2412 retry_kern_sym_load:
2413 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2414 if (errno == ENOSPC && bufsize < ret) {
2415 syms = xrealloc(syms, bufsize = ret);
2416 goto retry_kern_sym_load;
2418 perror_msg("kernel: QM_SYMBOLS");
2421 nksyms = nsyms = ret;
2424 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2425 s->name += (unsigned long) syms;
2431 /* Return the kernel symbol checksum version, or zero if not used. */
2433 static int new_is_kernel_checksummed(void)
2435 struct new_module_symbol *s;
2438 /* Using_Versions is not the first symbol, but it should be in there. */
2440 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2441 if (strcmp((char *) s->name, "Using_Versions") == 0)
2448 static int new_create_this_module(struct obj_file *f, const char *m_name)
2450 struct obj_section *sec;
2452 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2453 sizeof(struct new_module));
2454 memset(sec->contents, 0, sizeof(struct new_module));
2456 obj_add_symbol(f, "__this_module", -1,
2457 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2458 sizeof(struct new_module));
2460 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2467 static int new_create_module_ksymtab(struct obj_file *f)
2469 struct obj_section *sec;
2472 /* We must always add the module references. */
2474 if (n_ext_modules_used) {
2475 struct new_module_ref *dep;
2476 struct obj_symbol *tm;
2478 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2479 (sizeof(struct new_module_ref)
2480 * n_ext_modules_used));
2484 tm = obj_find_symbol(f, "__this_module");
2485 dep = (struct new_module_ref *) sec->contents;
2486 for (i = 0; i < n_ext_modules; ++i)
2487 if (ext_modules[i].used) {
2488 dep->dep = ext_modules[i].addr;
2489 obj_symbol_patch(f, sec->idx,
2490 (char *) &dep->ref - sec->contents, tm);
2496 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2501 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2504 /* We don't want to export symbols residing in sections that
2505 aren't loaded. There are a number of these created so that
2506 we make sure certain module options don't appear twice. */
2508 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2510 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2512 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2513 struct obj_symbol *sym;
2514 for (sym = f->symtab[i]; sym; sym = sym->next)
2515 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2516 && sym->secidx <= SHN_HIRESERVE
2517 && (sym->secidx >= SHN_LORESERVE
2518 || loaded[sym->secidx])) {
2519 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2521 obj_symbol_patch(f, sec->idx, ofs, sym);
2522 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2529 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2537 new_init_module(const char *m_name, struct obj_file *f,
2538 unsigned long m_size)
2540 struct new_module *module;
2541 struct obj_section *sec;
2546 sec = obj_find_section(f, ".this");
2547 if (!sec || !sec->contents) {
2548 perror_msg_and_die("corrupt module %s?",m_name);
2550 module = (struct new_module *) sec->contents;
2551 m_addr = sec->header.sh_addr;
2553 module->size_of_struct = sizeof(*module);
2554 module->size = m_size;
2555 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2557 sec = obj_find_section(f, "__ksymtab");
2558 if (sec && sec->header.sh_size) {
2559 module->syms = sec->header.sh_addr;
2560 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2563 if (n_ext_modules_used) {
2564 sec = obj_find_section(f, ".kmodtab");
2565 module->deps = sec->header.sh_addr;
2566 module->ndeps = n_ext_modules_used;
2570 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2572 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2574 sec = obj_find_section(f, "__ex_table");
2576 module->ex_table_start = sec->header.sh_addr;
2577 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2580 sec = obj_find_section(f, ".text.init");
2582 module->runsize = sec->header.sh_addr - m_addr;
2584 sec = obj_find_section(f, ".data.init");
2586 if (!module->runsize ||
2587 module->runsize > sec->header.sh_addr - m_addr)
2588 module->runsize = sec->header.sh_addr - m_addr;
2590 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2591 if (sec && sec->header.sh_size) {
2592 module->archdata_start = (void*)sec->header.sh_addr;
2593 module->archdata_end = module->archdata_start + sec->header.sh_size;
2595 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2596 if (sec && sec->header.sh_size) {
2597 module->kallsyms_start = (void*)sec->header.sh_addr;
2598 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2601 if (!arch_init_module(f, module))
2604 /* Whew! All of the initialization is complete. Collect the final
2605 module image and give it to the kernel. */
2607 image = xmalloc(m_size);
2608 obj_create_image(f, image);
2610 ret = new_sys_init_module(m_name, (struct new_module *) image);
2612 perror_msg("init_module: %s", m_name);
2621 #define new_init_module(x, y, z) TRUE
2622 #define new_create_this_module(x, y) 0
2623 #define new_create_module_ksymtab(x)
2624 #define query_module(v, w, x, y, z) -1
2626 #endif /* BB_FEATURE_NEW_MODULE_INTERFACE */
2629 /*======================================================================*/
2632 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2635 struct obj_string_patch *p;
2636 struct obj_section *strsec;
2637 size_t len = strlen(string) + 1;
2640 p = xmalloc(sizeof(*p));
2641 p->next = f->string_patches;
2642 p->reloc_secidx = secidx;
2643 p->reloc_offset = offset;
2644 f->string_patches = p;
2646 strsec = obj_find_section(f, ".kstrtab");
2647 if (strsec == NULL) {
2648 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2649 p->string_offset = 0;
2650 loc = strsec->contents;
2652 p->string_offset = strsec->header.sh_size;
2653 loc = obj_extend_section(strsec, len);
2655 memcpy(loc, string, len);
2661 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2662 struct obj_symbol *sym)
2664 struct obj_symbol_patch *p;
2666 p = xmalloc(sizeof(*p));
2667 p->next = f->symbol_patches;
2668 p->reloc_secidx = secidx;
2669 p->reloc_offset = offset;
2671 f->symbol_patches = p;
2676 int obj_check_undefineds(struct obj_file *f)
2681 for (i = 0; i < HASH_BUCKETS; ++i) {
2682 struct obj_symbol *sym;
2683 for (sym = f->symtab[i]; sym; sym = sym->next)
2684 if (sym->secidx == SHN_UNDEF) {
2685 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2686 sym->secidx = SHN_ABS;
2689 error_msg("unresolved symbol %s", sym->name);
2698 void obj_allocate_commons(struct obj_file *f)
2700 struct common_entry {
2701 struct common_entry *next;
2702 struct obj_symbol *sym;
2703 } *common_head = NULL;
2707 for (i = 0; i < HASH_BUCKETS; ++i) {
2708 struct obj_symbol *sym;
2709 for (sym = f->symtab[i]; sym; sym = sym->next)
2710 if (sym->secidx == SHN_COMMON) {
2711 /* Collect all COMMON symbols and sort them by size so as to
2712 minimize space wasted by alignment requirements. */
2714 struct common_entry **p, *n;
2715 for (p = &common_head; *p; p = &(*p)->next)
2716 if (sym->size <= (*p)->sym->size)
2719 n = alloca(sizeof(*n));
2727 for (i = 1; i < f->local_symtab_size; ++i) {
2728 struct obj_symbol *sym = f->local_symtab[i];
2729 if (sym && sym->secidx == SHN_COMMON) {
2730 struct common_entry **p, *n;
2731 for (p = &common_head; *p; p = &(*p)->next)
2732 if (sym == (*p)->sym)
2734 else if (sym->size < (*p)->sym->size) {
2735 n = alloca(sizeof(*n));
2745 /* Find the bss section. */
2746 for (i = 0; i < f->header.e_shnum; ++i)
2747 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2750 /* If for some reason there hadn't been one, create one. */
2751 if (i == f->header.e_shnum) {
2752 struct obj_section *sec;
2754 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2755 f->sections[i] = sec = arch_new_section();
2756 f->header.e_shnum = i + 1;
2758 memset(sec, 0, sizeof(*sec));
2759 sec->header.sh_type = SHT_PROGBITS;
2760 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2765 /* Allocate the COMMONS. */
2767 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2768 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2769 struct common_entry *c;
2771 for (c = common_head; c; c = c->next) {
2772 ElfW(Addr) align = c->sym->value;
2774 if (align > max_align)
2776 if (bss_size & (align - 1))
2777 bss_size = (bss_size | (align - 1)) + 1;
2780 c->sym->value = bss_size;
2782 bss_size += c->sym->size;
2785 f->sections[i]->header.sh_size = bss_size;
2786 f->sections[i]->header.sh_addralign = max_align;
2790 /* For the sake of patch relocation and parameter initialization,
2791 allocate zeroed data for NOBITS sections now. Note that after
2792 this we cannot assume NOBITS are really empty. */
2793 for (i = 0; i < f->header.e_shnum; ++i) {
2794 struct obj_section *s = f->sections[i];
2795 if (s->header.sh_type == SHT_NOBITS) {
2796 if (s->header.sh_size != 0)
2797 s->contents = memset(xmalloc(s->header.sh_size),
2798 0, s->header.sh_size);
2802 s->header.sh_type = SHT_PROGBITS;
2807 unsigned long obj_load_size(struct obj_file *f)
2809 unsigned long dot = 0;
2810 struct obj_section *sec;
2812 /* Finalize the positions of the sections relative to one another. */
2814 for (sec = f->load_order; sec; sec = sec->load_next) {
2817 align = sec->header.sh_addralign;
2818 if (align && (dot & (align - 1)))
2819 dot = (dot | (align - 1)) + 1;
2821 sec->header.sh_addr = dot;
2822 dot += sec->header.sh_size;
2828 int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2830 int i, n = f->header.e_shnum;
2833 /* Finalize the addresses of the sections. */
2836 for (i = 0; i < n; ++i)
2837 f->sections[i]->header.sh_addr += base;
2839 /* And iterate over all of the relocations. */
2841 for (i = 0; i < n; ++i) {
2842 struct obj_section *relsec, *symsec, *targsec, *strsec;
2843 ElfW(RelM) * rel, *relend;
2847 relsec = f->sections[i];
2848 if (relsec->header.sh_type != SHT_RELM)
2851 symsec = f->sections[relsec->header.sh_link];
2852 targsec = f->sections[relsec->header.sh_info];
2853 strsec = f->sections[symsec->header.sh_link];
2855 rel = (ElfW(RelM) *) relsec->contents;
2856 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2857 symtab = (ElfW(Sym) *) symsec->contents;
2858 strtab = (const char *) strsec->contents;
2860 for (; rel < relend; ++rel) {
2861 ElfW(Addr) value = 0;
2862 struct obj_symbol *intsym = NULL;
2863 unsigned long symndx;
2864 ElfW(Sym) * extsym = 0;
2867 /* Attempt to find a value to use for this relocation. */
2869 symndx = ELFW(R_SYM) (rel->r_info);
2871 /* Note we've already checked for undefined symbols. */
2873 extsym = &symtab[symndx];
2874 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2875 /* Local symbols we look up in the local table to be sure
2876 we get the one that is really intended. */
2877 intsym = f->local_symtab[symndx];
2879 /* Others we look up in the hash table. */
2881 if (extsym->st_name)
2882 name = strtab + extsym->st_name;
2884 name = f->sections[extsym->st_shndx]->name;
2885 intsym = obj_find_symbol(f, name);
2888 value = obj_symbol_final_value(f, intsym);
2889 intsym->referenced = 1;
2891 #if SHT_RELM == SHT_RELA
2892 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2893 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2894 if (!extsym || !extsym->st_name ||
2895 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2897 value += rel->r_addend;
2901 switch (arch_apply_relocation
2902 (f, targsec, symsec, intsym, rel, value)) {
2906 case obj_reloc_overflow:
2907 errmsg = "Relocation overflow";
2909 case obj_reloc_dangerous:
2910 errmsg = "Dangerous relocation";
2912 case obj_reloc_unhandled:
2913 errmsg = "Unhandled relocation";
2916 error_msg("%s of type %ld for %s", errmsg,
2917 (long) ELFW(R_TYPE) (rel->r_info),
2918 strtab + extsym->st_name);
2920 error_msg("%s of type %ld", errmsg,
2921 (long) ELFW(R_TYPE) (rel->r_info));
2929 /* Finally, take care of the patches. */
2931 if (f->string_patches) {
2932 struct obj_string_patch *p;
2933 struct obj_section *strsec;
2934 ElfW(Addr) strsec_base;
2935 strsec = obj_find_section(f, ".kstrtab");
2936 strsec_base = strsec->header.sh_addr;
2938 for (p = f->string_patches; p; p = p->next) {
2939 struct obj_section *targsec = f->sections[p->reloc_secidx];
2940 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2941 = strsec_base + p->string_offset;
2945 if (f->symbol_patches) {
2946 struct obj_symbol_patch *p;
2948 for (p = f->symbol_patches; p; p = p->next) {
2949 struct obj_section *targsec = f->sections[p->reloc_secidx];
2950 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2951 = obj_symbol_final_value(f, p->sym);
2958 int obj_create_image(struct obj_file *f, char *image)
2960 struct obj_section *sec;
2961 ElfW(Addr) base = f->baseaddr;
2963 for (sec = f->load_order; sec; sec = sec->load_next) {
2966 if (sec->contents == 0 || sec->header.sh_size == 0)
2969 secimg = image + (sec->header.sh_addr - base);
2971 /* Note that we allocated data for NOBITS sections earlier. */
2972 memcpy(secimg, sec->contents, sec->header.sh_size);
2978 /*======================================================================*/
2980 struct obj_file *obj_load(FILE * fp, int loadprogbits)
2983 ElfW(Shdr) * section_headers;
2987 /* Read the file header. */
2989 f = arch_new_file();
2990 memset(f, 0, sizeof(*f));
2991 f->symbol_cmp = strcmp;
2992 f->symbol_hash = obj_elf_hash;
2993 f->load_order_search_start = &f->load_order;
2995 fseek(fp, 0, SEEK_SET);
2996 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2997 perror_msg("error reading ELF header");
3001 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3002 || f->header.e_ident[EI_MAG1] != ELFMAG1
3003 || f->header.e_ident[EI_MAG2] != ELFMAG2
3004 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3005 error_msg("not an ELF file");
3008 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3009 || f->header.e_ident[EI_DATA] != ELFDATAM
3010 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3011 || !MATCH_MACHINE(f->header.e_machine)) {
3012 error_msg("ELF file not for this architecture");
3015 if (f->header.e_type != ET_REL) {
3016 error_msg("ELF file not a relocatable object");
3020 /* Read the section headers. */
3022 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3023 error_msg("section header size mismatch: %lu != %lu",
3024 (unsigned long) f->header.e_shentsize,
3025 (unsigned long) sizeof(ElfW(Shdr)));
3029 shnum = f->header.e_shnum;
3030 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3031 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3033 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3034 fseek(fp, f->header.e_shoff, SEEK_SET);
3035 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3036 perror_msg("error reading ELF section headers");
3040 /* Read the section data. */
3042 for (i = 0; i < shnum; ++i) {
3043 struct obj_section *sec;
3045 f->sections[i] = sec = arch_new_section();
3046 memset(sec, 0, sizeof(*sec));
3048 sec->header = section_headers[i];
3051 if(sec->header.sh_size) switch (sec->header.sh_type) {
3060 if (!loadprogbits) {
3061 sec->contents = NULL;
3068 if (sec->header.sh_size > 0) {
3069 sec->contents = xmalloc(sec->header.sh_size);
3070 fseek(fp, sec->header.sh_offset, SEEK_SET);
3071 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3072 perror_msg("error reading ELF section data");
3076 sec->contents = NULL;
3080 #if SHT_RELM == SHT_REL
3082 error_msg("RELA relocations not supported on this architecture");
3086 error_msg("REL relocations not supported on this architecture");
3091 if (sec->header.sh_type >= SHT_LOPROC) {
3092 /* Assume processor specific section types are debug
3093 info and can safely be ignored. If this is ever not
3094 the case (Hello MIPS?), don't put ifdefs here but
3095 create an arch_load_proc_section(). */
3099 error_msg("can't handle sections of type %ld",
3100 (long) sec->header.sh_type);
3105 /* Do what sort of interpretation as needed by each section. */
3107 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3109 for (i = 0; i < shnum; ++i) {
3110 struct obj_section *sec = f->sections[i];
3111 sec->name = shstrtab + sec->header.sh_name;
3114 for (i = 0; i < shnum; ++i) {
3115 struct obj_section *sec = f->sections[i];
3117 /* .modinfo should be contents only but gcc has no attribute for that.
3118 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3120 if (strcmp(sec->name, ".modinfo") == 0)
3121 sec->header.sh_flags &= ~SHF_ALLOC;
3123 if (sec->header.sh_flags & SHF_ALLOC)
3124 obj_insert_section_load_order(f, sec);
3126 switch (sec->header.sh_type) {
3129 unsigned long nsym, j;
3133 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3134 error_msg("symbol size mismatch: %lu != %lu",
3135 (unsigned long) sec->header.sh_entsize,
3136 (unsigned long) sizeof(ElfW(Sym)));
3140 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3141 strtab = f->sections[sec->header.sh_link]->contents;
3142 sym = (ElfW(Sym) *) sec->contents;
3144 /* Allocate space for a table of local symbols. */
3145 j = f->local_symtab_size = sec->header.sh_info;
3146 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3148 /* Insert all symbols into the hash table. */
3149 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3152 name = strtab + sym->st_name;
3154 name = f->sections[sym->st_shndx]->name;
3156 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3157 sym->st_value, sym->st_size);
3163 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3164 error_msg("relocation entry size mismatch: %lu != %lu",
3165 (unsigned long) sec->header.sh_entsize,
3166 (unsigned long) sizeof(ElfW(RelM)));
3170 /* XXX Relocation code from modutils-2.3.19 is not here.
3171 * Why? That's about 20 lines of code from obj/obj_load.c,
3172 * which gets done in a second pass through the sections.
3173 * This BusyBox insmod does similar work in obj_relocate(). */
3180 #ifdef BB_FEATURE_INSMOD_LOADINKMEM
3182 * load the unloaded sections directly into the memory allocated by
3183 * kernel for the module
3186 int obj_load_progbits(FILE * fp, struct obj_file* f)
3188 char* imagebase = (char*) f->imagebase;
3189 ElfW(Addr) base = f->baseaddr;
3190 struct obj_section* sec;
3192 for (sec = f->load_order; sec; sec = sec->load_next) {
3194 /* section already loaded? */
3195 if (sec->contents != NULL)
3198 if (sec->header.sh_size == 0)
3201 sec->contents = imagebase + (sec->header.sh_addr - base);
3202 fseek(fp, sec->header.sh_offset, SEEK_SET);
3203 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3204 errorMsg("error reading ELF section data: %s\n", strerror(errno));
3213 static void hide_special_symbols(struct obj_file *f)
3215 static const char *const specials[] = {
3222 struct obj_symbol *sym;
3223 const char *const *p;
3225 for (p = specials; *p; ++p)
3226 if ((sym = obj_find_symbol(f, *p)) != NULL)
3228 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3233 extern int insmod_main( int argc, char **argv)
3240 unsigned long m_size;
3245 char m_name[FILENAME_MAX + 1] = "\0";
3246 int exit_status = EXIT_FAILURE;
3248 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3250 char k_strversion[STRVERSIONLEN];
3251 char m_strversion[STRVERSIONLEN];
3256 /* Parse any options */
3257 while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
3259 case 'f': /* force loading */
3260 flag_force_load = 1;
3262 case 'k': /* module loaded by kerneld, auto-cleanable */
3265 case 'v': /* verbose output */
3268 case 'x': /* do not export externs */
3271 case 'o': /* name the output module */
3272 strncpy(m_name, optarg, FILENAME_MAX);
3274 case 'L': /* Stub warning */
3275 /* This is needed for compatibility with modprobe.
3276 * In theory, this does locking, but we don't do
3277 * that. So be careful and plan your life around not
3278 * loading the same module 50 times concurrently. */
3285 if (argv[optind] == NULL) {
3289 /* Grab the module name */
3290 if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3297 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3299 memcpy(m_fullName, tmp, len);
3300 m_fullName[len]='\0';
3301 if (*m_name == '\0') {
3302 strcpy(m_name, m_fullName);
3304 strcat(m_fullName, ".o");
3306 /* Get a filedesc for the module. Check we we have a complete path */
3307 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3308 (fp = fopen(argv[optind], "r")) == NULL) {
3309 struct utsname myuname;
3311 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3312 * but do not error out yet if we fail to find it... */
3313 if (uname(&myuname) == 0) {
3314 char module_dir[FILENAME_MAX];
3315 char real_module_dir[FILENAME_MAX];
3316 snprintf (module_dir, sizeof(module_dir), "%s/%s",
3317 _PATH_MODULES, myuname.release);
3318 /* Jump through hoops in case /lib/modules/`uname -r`
3319 * is a symlink. We do not want recursive_action to
3320 * follow symlinks, but we do want to follow the
3321 * /lib/modules/`uname -r` dir, So resolve it ourselves
3322 * if it is a link... */
3323 if (realpath (module_dir, real_module_dir) == NULL)
3324 strcpy(real_module_dir, module_dir);
3325 recursive_action(real_module_dir, TRUE, FALSE, FALSE,
3326 check_module_name_match, 0, m_fullName);
3329 /* Check if we have found anything yet */
3330 if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL))
3332 char module_dir[FILENAME_MAX];
3333 if (realpath (_PATH_MODULES, module_dir) == NULL)
3334 strcpy(module_dir, _PATH_MODULES);
3335 /* No module found under /lib/modules/`uname -r`, this
3336 * time cast the net a bit wider. Search /lib/modules/ */
3337 if (recursive_action(module_dir, TRUE, FALSE, FALSE,
3338 check_module_name_match, 0, m_fullName) == FALSE)
3340 if (m_filename[0] == '\0'
3341 || ((fp = fopen(m_filename, "r")) == NULL))
3343 error_msg("%s: no module by that name found", m_fullName);
3344 return EXIT_FAILURE;
3347 error_msg_and_die("%s: no module by that name found", m_fullName);
3350 safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
3352 printf("Using %s\n", m_filename);
3354 if ((f = obj_load(fp, LOADBITS)) == NULL)
3355 perror_msg_and_die("Could not load the module");
3357 if (get_modinfo_value(f, "kernel_version") == NULL)
3362 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3363 /* Version correspondence? */
3365 k_version = get_kernel_version(k_strversion);
3366 if (m_has_modinfo) {
3367 m_version = new_get_module_version(f, m_strversion);
3369 m_version = old_get_module_version(f, m_strversion);
3370 if (m_version == -1) {
3371 error_msg("couldn't find the kernel version the module was "
3377 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
3378 if (flag_force_load) {
3379 error_msg("Warning: kernel-module version mismatch\n"
3380 "\t%s was compiled for kernel version %s\n"
3381 "\twhile this kernel is version %s",
3382 m_filename, m_strversion, k_strversion);
3384 error_msg("kernel-module version mismatch\n"
3385 "\t%s was compiled for kernel version %s\n"
3386 "\twhile this kernel is version %s.",
3387 m_filename, m_strversion, k_strversion);
3392 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3394 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3396 if (k_new_syscalls) {
3397 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
3398 if (!new_get_kernel_symbols())
3400 k_crcs = new_is_kernel_checksummed();
3402 error_msg("Not configured to support new kernels");
3406 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
3407 if (!old_get_kernel_symbols(m_name))
3409 k_crcs = old_is_kernel_checksummed();
3411 error_msg("Not configured to support old kernels");
3416 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3418 m_crcs = new_is_module_checksummed(f);
3420 m_crcs = old_is_module_checksummed(f);
3422 if (m_crcs != k_crcs)
3423 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3424 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3426 /* Let the module know about the kernel symbols. */
3427 add_kernel_symbols(f);
3429 /* Allocate common symbols, symbol tables, and string tables. */
3432 ? !new_create_this_module(f, m_name)
3433 : !old_create_mod_use_count(f))
3438 if (!obj_check_undefineds(f)) {
3441 obj_allocate_commons(f);
3443 /* done with the module name, on to the optional var=value arguments */
3446 if (optind < argc) {
3448 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3449 : !old_process_module_arguments(f, argc - optind, argv + optind))
3456 hide_special_symbols(f);
3459 new_create_module_ksymtab(f);
3461 /* Find current size of the module */
3462 m_size = obj_load_size(f);
3465 m_addr = create_module(m_name, m_size);
3466 if (m_addr==-1) switch (errno) {
3468 error_msg("A module named %s already exists", m_name);
3471 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3475 perror_msg("create_module: %s", m_name);
3481 * the PROGBITS section was not loaded by the obj_load
3482 * now we can load them directly into the kernel memory
3484 // f->imagebase = (char*) m_addr;
3485 f->imagebase = (ElfW(Addr)) m_addr;
3486 if (!obj_load_progbits(fp, f)) {
3487 delete_module(m_name);
3492 if (!obj_relocate(f, m_addr)) {
3493 delete_module(m_name);
3498 ? !new_init_module(m_name, f, m_size)
3499 : !old_init_module(m_name, f, m_size))
3501 delete_module(m_name);
3505 exit_status = EXIT_SUCCESS;
3509 return(exit_status);