1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * Copyright (C) 1999,2000,2001 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>
7 * and Ron Alder <alder@lineo.com>
9 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
10 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
12 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
13 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
14 * very minor changes required to also work with StrongArm and presumably
15 * all ARM based systems.
17 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
18 * PowerPC specific code stolen from modutils-2.3.16,
19 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
20 * I've only tested the code on mpc8xx platforms in big-endian mode.
21 * Did some cleanup and added BB_USE_xxx_ENTRIES...
23 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
24 * based on modutils-2.4.2
25 * MIPS specific support for Elf loading and relocation.
26 * Copyright 1996, 1997 Linux International.
27 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
29 * Based almost entirely on the Linux modutils-2.3.11 implementation.
30 * Copyright 1996, 1997 Linux International.
31 * New implementation contributed by Richard Henderson <rth@tamu.edu>
32 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
33 * Restructured (and partly rewritten) by:
34 * Björn Ekwall <bj0rn@blox.se> February 1999
36 * This program is free software; you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation; either version 2 of the License, or
39 * (at your option) any later version.
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44 * General Public License for more details.
46 * You should have received a copy of the GNU General Public License
47 * along with this program; if not, write to the Free Software
48 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
62 #include <sys/utsname.h>
65 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
66 # define new_sys_init_module init_module
68 # define old_sys_init_module init_module
71 #if defined(__powerpc__)
72 #define BB_USE_PLT_ENTRIES
73 #define BB_PLT_ENTRY_SIZE 16
77 #define BB_USE_PLT_ENTRIES
78 #define BB_PLT_ENTRY_SIZE 8
79 #define BB_USE_GOT_ENTRIES
80 #define BB_GOT_ENTRY_SIZE 8
84 #define BB_USE_GOT_ENTRIES
85 #define BB_GOT_ENTRY_SIZE 4
89 #define BB_USE_GOT_ENTRIES
90 #define BB_GOT_ENTRY_SIZE 4
97 //----------------------------------------------------------------------------
98 //--------modutils module.h, lines 45-242
99 //----------------------------------------------------------------------------
101 /* Definitions for the Linux module syscall interface.
102 Copyright 1996, 1997 Linux International.
104 Contributed by Richard Henderson <rth@tamu.edu>
106 This file is part of the Linux modutils.
108 This program is free software; you can redistribute it and/or modify it
109 under the terms of the GNU General Public License as published by the
110 Free Software Foundation; either version 2 of the License, or (at your
111 option) any later version.
113 This program is distributed in the hope that it will be useful, but
114 WITHOUT ANY WARRANTY; without even the implied warranty of
115 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
116 General Public License for more details.
118 You should have received a copy of the GNU General Public License
119 along with this program; if not, write to the Free Software Foundation,
120 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
123 #ifndef MODUTILS_MODULE_H
124 static const int MODUTILS_MODULE_H = 1;
126 #ident "$Id: insmod.c,v 1.57 2001/04/05 07:33:10 andersen Exp $"
128 /* This file contains the structures used by the 2.0 and 2.1 kernels.
129 We do not use the kernel headers directly because we do not wish
130 to be dependant on a particular kernel version to compile insmod. */
133 /*======================================================================*/
134 /* The structures used by Linux 2.0. */
136 /* The symbol format used by get_kernel_syms(2). */
137 struct old_kernel_sym
143 struct old_module_ref
145 unsigned long module; /* kernel addresses */
149 struct old_module_symbol
155 struct old_symbol_table
157 int size; /* total, including string table!!! */
160 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
161 struct old_module_ref ref[0]; /* actual size defined by n_refs */
164 struct old_mod_routines
167 unsigned long cleanup;
173 unsigned long ref; /* the list of modules that refer to me */
174 unsigned long symtab;
176 int size; /* size of module in pages */
177 unsigned long addr; /* address of module */
179 unsigned long cleanup; /* cleanup routine */
182 /* Sent to init_module(2) or'ed into the code size parameter. */
183 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
185 int get_kernel_syms(struct old_kernel_sym *);
186 int old_sys_init_module(const char *name, char *code, unsigned codesize,
187 struct old_mod_routines *, struct old_symbol_table *);
189 /*======================================================================*/
190 /* For sizeof() which are related to the module platform and not to the
191 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
193 #define tgt_sizeof_char sizeof(char)
194 #define tgt_sizeof_short sizeof(short)
195 #define tgt_sizeof_int sizeof(int)
196 #define tgt_sizeof_long sizeof(long)
197 #define tgt_sizeof_char_p sizeof(char *)
198 #define tgt_sizeof_void_p sizeof(void *)
199 #define tgt_long long
201 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
202 #undef tgt_sizeof_long
203 #undef tgt_sizeof_char_p
204 #undef tgt_sizeof_void_p
206 static const int tgt_sizeof_long = 8;
207 static const int tgt_sizeof_char_p = 8;
208 static const int tgt_sizeof_void_p = 8;
209 #define tgt_long long long
212 /*======================================================================*/
213 /* The structures used in Linux 2.1. */
215 /* Note: new_module_symbol does not use tgt_long intentionally */
216 struct new_module_symbol
222 struct new_module_persist;
224 struct new_module_ref
226 unsigned tgt_long dep; /* kernel addresses */
227 unsigned tgt_long ref;
228 unsigned tgt_long next_ref;
233 unsigned tgt_long size_of_struct; /* == sizeof(module) */
234 unsigned tgt_long next;
235 unsigned tgt_long name;
236 unsigned tgt_long size;
239 unsigned tgt_long flags; /* AUTOCLEAN et al */
244 unsigned tgt_long syms;
245 unsigned tgt_long deps;
246 unsigned tgt_long refs;
247 unsigned tgt_long init;
248 unsigned tgt_long cleanup;
249 unsigned tgt_long ex_table_start;
250 unsigned tgt_long ex_table_end;
252 unsigned tgt_long gp;
254 /* Everything after here is extension. */
255 unsigned tgt_long persist_start;
256 unsigned tgt_long persist_end;
257 unsigned tgt_long can_unload;
258 unsigned tgt_long runsize;
261 struct new_module_info
269 /* Bits of module.flags. */
270 static const int NEW_MOD_RUNNING = 1;
271 static const int NEW_MOD_DELETED = 2;
272 static const int NEW_MOD_AUTOCLEAN = 4;
273 static const int NEW_MOD_VISITED = 8;
274 static const int NEW_MOD_USED_ONCE = 16;
276 int new_sys_init_module(const char *name, const struct new_module *);
277 int query_module(const char *name, int which, void *buf, size_t bufsize,
280 /* Values for query_module's which. */
282 static const int QM_MODULES = 1;
283 static const int QM_DEPS = 2;
284 static const int QM_REFS = 3;
285 static const int QM_SYMBOLS = 4;
286 static const int QM_INFO = 5;
288 /*======================================================================*/
289 /* The system calls unchanged between 2.0 and 2.1. */
291 unsigned long create_module(const char *, size_t);
292 int delete_module(const char *);
295 #endif /* module.h */
297 //----------------------------------------------------------------------------
298 //--------end of modutils module.h
299 //----------------------------------------------------------------------------
303 //----------------------------------------------------------------------------
304 //--------modutils obj.h, lines 253-462
305 //----------------------------------------------------------------------------
307 /* Elf object file loading and relocation routines.
308 Copyright 1996, 1997 Linux International.
310 Contributed by Richard Henderson <rth@tamu.edu>
312 This file is part of the Linux modutils.
314 This program is free software; you can redistribute it and/or modify it
315 under the terms of the GNU General Public License as published by the
316 Free Software Foundation; either version 2 of the License, or (at your
317 option) any later version.
319 This program is distributed in the hope that it will be useful, but
320 WITHOUT ANY WARRANTY; without even the implied warranty of
321 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
322 General Public License for more details.
324 You should have received a copy of the GNU General Public License
325 along with this program; if not, write to the Free Software Foundation,
326 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
329 #ifndef MODUTILS_OBJ_H
330 static const int MODUTILS_OBJ_H = 1;
332 #ident "$Id: insmod.c,v 1.57 2001/04/05 07:33:10 andersen Exp $"
334 /* The relocatable object is manipulated using elfin types. */
340 /* Machine-specific elf macros for i386 et al. */
342 /* the SH changes have only been tested on the SH4 in =little endian= mode */
343 /* I'm not sure about big endian, so let's warn: */
345 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
346 #error insmod.c may require changes for use on big endian SH4/SH3
349 /* it may or may not work on the SH1/SH2... So let's error on those
351 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
352 #error insmod.c may require changes for non-SH3/SH4 use
355 #define ELFCLASSM ELFCLASS32
359 #define MATCH_MACHINE(x) (x == EM_SH)
360 #define SHT_RELM SHT_RELA
361 #define Elf32_RelM Elf32_Rela
362 #define ELFDATAM ELFDATA2LSB
364 #elif defined(__arm__)
366 #define MATCH_MACHINE(x) (x == EM_ARM)
367 #define SHT_RELM SHT_REL
368 #define Elf32_RelM Elf32_Rel
369 #define ELFDATAM ELFDATA2LSB
371 #elif defined(__powerpc__)
373 #define MATCH_MACHINE(x) (x == EM_PPC)
374 #define SHT_RELM SHT_RELA
375 #define Elf32_RelM Elf32_Rela
376 #define ELFDATAM ELFDATA2MSB
378 #elif defined(__mips__)
380 /* Account for ELF spec changes. */
381 #ifndef EM_MIPS_RS3_LE
382 #ifdef EM_MIPS_RS4_BE
383 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
385 #define EM_MIPS_RS3_LE 10
387 #endif /* !EM_MIPS_RS3_LE */
389 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
390 #define SHT_RELM SHT_REL
391 #define Elf32_RelM Elf32_Rel
393 #define ELFDATAM ELFDATA2MSB
396 #define ELFDATAM ELFDATA2LSB
399 #elif defined(__i386__)
401 /* presumably we can use these for anything but the SH and ARM*/
402 /* this is the previous behavior, but it does result in
403 insmod.c being broken on anything except i386 */
405 #define MATCH_MACHINE(x) (x == EM_386)
407 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
410 #define SHT_RELM SHT_REL
411 #define Elf32_RelM Elf32_Rel
412 #define ELFDATAM ELFDATA2LSB
415 #error Sorry, but insmod.c does not yet support this architecture...
419 # if ELFCLASSM == ELFCLASS32
420 # define ElfW(x) Elf32_ ## x
421 # define ELFW(x) ELF32_ ## x
423 # define ElfW(x) Elf64_ ## x
424 # define ELFW(x) ELF64_ ## x
428 /* For some reason this is missing from libc5. */
429 #ifndef ELF32_ST_INFO
430 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
433 #ifndef ELF64_ST_INFO
434 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
437 struct obj_string_patch;
438 struct obj_symbol_patch;
445 struct obj_section *load_next;
451 struct obj_symbol *next; /* hash table link */
455 int secidx; /* the defining section index/module */
457 int ksymidx; /* for export to the kernel symtab */
458 int referenced; /* actually used in the link */
461 /* Hardcode the hash table size. We shouldn't be needing so many
462 symbols that we begin to degrade performance, and we get a big win
463 by giving the compiler a constant divisor. */
465 #define HASH_BUCKETS 521
471 struct obj_section **sections;
472 struct obj_section *load_order;
473 struct obj_section **load_order_search_start;
474 struct obj_string_patch *string_patches;
475 struct obj_symbol_patch *symbol_patches;
476 int (*symbol_cmp)(const char *, const char *);
477 unsigned long (*symbol_hash)(const char *);
478 unsigned long local_symtab_size;
479 struct obj_symbol **local_symtab;
480 struct obj_symbol *symtab[HASH_BUCKETS];
491 struct obj_string_patch
493 struct obj_string_patch *next;
495 ElfW(Addr) reloc_offset;
496 ElfW(Addr) string_offset;
499 struct obj_symbol_patch
501 struct obj_symbol_patch *next;
503 ElfW(Addr) reloc_offset;
504 struct obj_symbol *sym;
508 /* Generic object manipulation routines. */
510 unsigned long obj_elf_hash(const char *);
512 unsigned long obj_elf_hash_n(const char *, unsigned long len);
514 struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
515 unsigned long symidx, int info, int secidx,
516 ElfW(Addr) value, unsigned long size);
518 struct obj_symbol *obj_find_symbol (struct obj_file *f,
521 ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
522 struct obj_symbol *sym);
524 void obj_set_symbol_compare(struct obj_file *f,
525 int (*cmp)(const char *, const char *),
526 unsigned long (*hash)(const char *));
528 struct obj_section *obj_find_section (struct obj_file *f,
531 void obj_insert_section_load_order (struct obj_file *f,
532 struct obj_section *sec);
534 struct obj_section *obj_create_alloced_section (struct obj_file *f,
539 struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
544 void *obj_extend_section (struct obj_section *sec, unsigned long more);
546 int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
549 int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
550 struct obj_symbol *sym);
552 int obj_check_undefineds(struct obj_file *f);
554 void obj_allocate_commons(struct obj_file *f);
556 unsigned long obj_load_size (struct obj_file *f);
558 int obj_relocate (struct obj_file *f, ElfW(Addr) base);
560 struct obj_file *obj_load(FILE *f);
562 int obj_create_image (struct obj_file *f, char *image);
564 /* Architecture specific manipulation routines. */
566 struct obj_file *arch_new_file (void);
568 struct obj_section *arch_new_section (void);
570 struct obj_symbol *arch_new_symbol (void);
572 enum obj_reloc arch_apply_relocation (struct obj_file *f,
573 struct obj_section *targsec,
574 struct obj_section *symsec,
575 struct obj_symbol *sym,
576 ElfW(RelM) *rel, ElfW(Addr) value);
578 int arch_create_got (struct obj_file *f);
581 int arch_init_module (struct obj_file *f, struct new_module *);
584 //----------------------------------------------------------------------------
585 //--------end of modutils obj.h
586 //----------------------------------------------------------------------------
592 #define _PATH_MODULES "/lib/modules"
593 static const int STRVERSIONLEN = 32;
595 /*======================================================================*/
597 int flag_force_load = 0;
598 int flag_autoclean = 0;
599 int flag_verbose = 0;
603 /*======================================================================*/
605 /* previously, these were named i386_* but since we could be
606 compiling for the sh, I've renamed them to the more general
607 arch_* These structures are the same between the x86 and SH,
608 and we can't support anything else right now anyway. In the
609 future maybe they should be #if defined'd */
615 #if defined(BB_USE_PLT_ENTRIES)
616 struct arch_plt_entry
620 int inited:1; /* has been set up */
624 #if defined(BB_USE_GOT_ENTRIES)
625 struct arch_got_entry {
627 unsigned offset_done:1;
628 unsigned reloc_done:1;
632 #if defined(__mips__)
635 struct mips_hi16 *next;
642 struct obj_file root;
643 #if defined(BB_USE_PLT_ENTRIES)
644 struct obj_section *plt;
646 #if defined(BB_USE_GOT_ENTRIES)
647 struct obj_section *got;
649 #if defined(__mips__)
650 struct mips_hi16 *mips_hi16_list;
655 struct obj_symbol root;
656 #if defined(BB_USE_PLT_ENTRIES)
657 struct arch_plt_entry pltent;
659 #if defined(BB_USE_GOT_ENTRIES)
660 struct arch_got_entry gotent;
665 struct external_module {
670 struct new_module_symbol *syms;
673 struct new_module_symbol *ksyms;
676 struct external_module *ext_modules;
678 int n_ext_modules_used;
681 extern int delete_module(const char *);
684 /* This is kind of troublesome. See, we don't actually support
685 the m68k or the arm the same way we support i386 and (now)
686 sh. In doing my SH patch, I just assumed that whatever works
687 for i386 also works for m68k and arm since currently insmod.c
688 does nothing special for them. If this isn't true, the below
689 line is rather misleading IMHO, and someone should either
690 change it or add more proper architecture-dependent support
693 -- Bryan Rittmeyer <bryan@ixiacom.com> */
695 static char m_filename[BUFSIZ + 1];
696 static char m_fullName[BUFSIZ + 1];
698 /*======================================================================*/
701 static int findNamedModule(const char *fileName, struct stat *statbuf,
704 char *fullName = (char *) userDate;
707 if (fullName[0] == '\0')
710 char *tmp = strrchr((char *) fileName, '/');
713 tmp = (char *) fileName;
716 if (check_wildcard_match(tmp, fullName) == TRUE) {
717 /* Stop searching if we find a match */
718 memcpy(m_filename, fileName, strlen(fileName)+1);
726 /*======================================================================*/
728 struct obj_file *arch_new_file(void)
731 f = xmalloc(sizeof(*f));
733 #if defined(BB_USE_PLT_ENTRIES)
736 #if defined(BB_USE_GOT_ENTRIES)
739 #if defined(__mips__)
740 f->mips_hi16_list = NULL;
746 struct obj_section *arch_new_section(void)
748 return xmalloc(sizeof(struct obj_section));
751 struct obj_symbol *arch_new_symbol(void)
753 struct arch_symbol *sym;
754 sym = xmalloc(sizeof(*sym));
756 #if defined(BB_USE_PLT_ENTRIES)
757 memset(&sym->pltent, 0, sizeof(sym->pltent));
759 #if defined(BB_USE_GOT_ENTRIES)
760 memset(&sym->gotent, 0, sizeof(sym->gotent));
767 arch_apply_relocation(struct obj_file *f,
768 struct obj_section *targsec,
769 struct obj_section *symsec,
770 struct obj_symbol *sym,
771 ElfW(RelM) *rel, ElfW(Addr) v)
773 struct arch_file *ifile = (struct arch_file *) f;
774 #if !(defined(__mips__))
775 struct arch_symbol *isym = (struct arch_symbol *) sym;
778 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
779 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
780 #if defined(BB_USE_GOT_ENTRIES)
781 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
783 #if defined(BB_USE_PLT_ENTRIES)
784 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
785 struct arch_plt_entry *pe;
788 enum obj_reloc ret = obj_reloc_ok;
790 switch (ELF32_R_TYPE(rel->r_info)) {
792 /* even though these constants seem to be the same for
793 the i386 and the sh, we "#if define" them for clarity
794 and in case that ever changes */
797 #elif defined(__arm__)
799 #elif defined(__i386__)
801 #elif defined(__powerpc__)
803 #elif defined(__mips__)
810 #elif defined(__arm__)
812 #elif defined(__i386__)
814 #elif defined(__powerpc__)
816 #elif defined(__mips__)
822 #if defined(__powerpc__)
823 case R_PPC_ADDR16_HA:
824 *(unsigned short *)loc = (v + 0x8000) >> 16;
827 case R_PPC_ADDR16_HI:
828 *(unsigned short *)loc = v >> 16;
831 case R_PPC_ADDR16_LO:
832 *(unsigned short *)loc = v;
836 #if defined(__mips__)
839 ret = obj_reloc_dangerous;
840 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
841 ret = obj_reloc_overflow;
843 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
851 /* We cannot relocate this one now because we don't know the value
852 of the carry we need to add. Save the information, and let LO16
853 do the actual relocation. */
854 n = (struct mips_hi16 *) xmalloc(sizeof *n);
857 n->next = ifile->mips_hi16_list;
858 ifile->mips_hi16_list = n;
864 unsigned long insnlo = *loc;
865 Elf32_Addr val, vallo;
867 /* Sign extend the addend we extract from the lo insn. */
868 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
870 if (ifile->mips_hi16_list != NULL) {
873 l = ifile->mips_hi16_list;
875 struct mips_hi16 *next;
878 /* The value for the HI16 had best be the same. */
879 assert(v == l->value);
881 /* Do the HI16 relocation. Note that we actually don't
882 need to know anything about the LO16 itself, except where
883 to find the low 16 bits of the addend needed by the LO16. */
886 ((insn & 0xffff) << 16) +
890 /* Account for the sign extension that will happen in the
897 insn = (insn & ~0xffff) | val;
905 ifile->mips_hi16_list = NULL;
908 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
910 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
917 #elif defined(__sh__)
921 #elif defined(__i386__)
926 #elif defined(__powerpc__)
936 #elif defined(__i386__)
939 #if defined(BB_USE_PLT_ENTRIES)
945 #if defined(__powerpc__)
948 /* find the plt entry and initialize it if necessary */
949 assert(isym != NULL);
951 pe = (struct arch_plt_entry*) &isym->pltent;
954 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
956 /* generate some machine code */
959 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
960 ip[1] = v; /* sym@ */
962 #if defined(__powerpc__)
963 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
964 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
965 ip[2] = 0x7d6903a6; /* mtctr r11 */
966 ip[3] = 0x4e800420; /* bctr */
971 /* relative distance to target */
973 /* if the target is too far away.... */
974 if ((int)v < -0x02000000 || (int)v >= 0x02000000) {
976 v = plt + pe->offset - dot;
979 ret = obj_reloc_dangerous;
981 /* merge the offset into the instruction. */
983 /* Convert to words. */
986 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
988 #if defined(__powerpc__)
989 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
992 #endif /* BB_USE_PLT_ENTRIES */
995 #elif defined(__sh__)
1000 #elif defined(__i386__)
1001 case R_386_GLOB_DAT:
1002 case R_386_JMP_SLOT:
1007 #if defined(__arm__)
1008 #elif defined(__sh__)
1010 *loc += f->baseaddr + rel->r_addend;
1012 #elif defined(__i386__)
1013 case R_386_RELATIVE:
1014 *loc += f->baseaddr;
1018 #if defined(BB_USE_GOT_ENTRIES)
1022 #elif defined(__arm__)
1024 #elif defined(__i386__)
1029 *loc += got - dot + rel->r_addend;;
1030 #elif defined(__i386__) || defined(__arm__)
1037 #elif defined(__arm__)
1039 #elif defined(__i386__)
1042 assert(isym != NULL);
1043 /* needs an entry in the .got: set it, once */
1044 if (!isym->gotent.reloc_done) {
1045 isym->gotent.reloc_done = 1;
1046 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1048 /* make the reloc with_respect_to_.got */
1050 *loc += isym->gotent.offset + rel->r_addend;
1051 #elif defined(__i386__) || defined(__arm__)
1052 *loc += isym->gotent.offset;
1056 /* address relative to the got */
1059 #elif defined(__arm__)
1061 #elif defined(__i386__)
1068 #endif /* BB_USE_GOT_ENTRIES */
1071 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1072 ret = obj_reloc_unhandled;
1079 int arch_create_got(struct obj_file *f)
1081 #if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
1082 struct arch_file *ifile = (struct arch_file *) f;
1084 #if defined(BB_USE_GOT_ENTRIES)
1085 int got_offset = 0, gotneeded = 0;
1087 #if defined(BB_USE_PLT_ENTRIES)
1088 int plt_offset = 0, pltneeded = 0;
1090 struct obj_section *relsec, *symsec, *strsec;
1091 ElfW(RelM) *rel, *relend;
1092 ElfW(Sym) *symtab, *extsym;
1093 const char *strtab, *name;
1094 struct arch_symbol *intsym;
1096 for (i = 0; i < f->header.e_shnum; ++i) {
1097 relsec = f->sections[i];
1098 if (relsec->header.sh_type != SHT_RELM)
1101 symsec = f->sections[relsec->header.sh_link];
1102 strsec = f->sections[symsec->header.sh_link];
1104 rel = (ElfW(RelM) *) relsec->contents;
1105 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1106 symtab = (ElfW(Sym) *) symsec->contents;
1107 strtab = (const char *) strsec->contents;
1109 for (; rel < relend; ++rel) {
1110 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1112 switch (ELF32_R_TYPE(rel->r_info)) {
1113 #if defined(__arm__)
1116 #elif defined(__sh__)
1119 #elif defined(__i386__)
1124 #if defined(__powerpc__)
1130 #if defined(__arm__)
1139 if (got_offset == 0)
1141 #elif defined(__sh__)
1145 #elif defined(__i386__)
1155 if (extsym->st_name != 0) {
1156 name = strtab + extsym->st_name;
1158 name = f->sections[extsym->st_shndx]->name;
1160 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1161 #if defined(BB_USE_GOT_ENTRIES)
1162 if (!intsym->gotent.offset_done) {
1163 intsym->gotent.offset_done = 1;
1164 intsym->gotent.offset = got_offset;
1165 got_offset += BB_GOT_ENTRY_SIZE;
1168 #if defined(BB_USE_PLT_ENTRIES)
1169 if (pltneeded && intsym->pltent.allocated == 0) {
1170 intsym->pltent.allocated = 1;
1171 intsym->pltent.offset = plt_offset;
1172 plt_offset += BB_PLT_ENTRY_SIZE;
1173 intsym->pltent.inited = 0;
1180 #if defined(BB_USE_GOT_ENTRIES)
1182 struct obj_section* myrelsec = obj_find_section(f, ".got");
1185 obj_extend_section(myrelsec, got_offset);
1187 myrelsec = obj_create_alloced_section(f, ".got",
1193 ifile->got = myrelsec;
1197 #if defined(BB_USE_PLT_ENTRIES)
1199 ifile->plt = obj_create_alloced_section(f, ".plt",
1207 int arch_init_module(struct obj_file *f, struct new_module *mod)
1213 /*======================================================================*/
1215 /* Standard ELF hash function. */
1216 inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1218 unsigned long h = 0;
1225 if ((g = (h & 0xf0000000)) != 0) {
1234 unsigned long obj_elf_hash(const char *name)
1236 return obj_elf_hash_n(name, strlen(name));
1239 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1240 /* Get the kernel version in the canonical integer form. */
1242 static int get_kernel_version(char str[STRVERSIONLEN])
1244 struct utsname uts_info;
1248 if (uname(&uts_info) < 0)
1250 strncpy(str, uts_info.release, STRVERSIONLEN);
1251 p = uts_info.release;
1253 a = strtoul(p, &p, 10);
1256 b = strtoul(p + 1, &p, 10);
1259 c = strtoul(p + 1, &q, 10);
1263 return a << 16 | b << 8 | c;
1266 /* String comparison for non-co-versioned kernel and module. */
1268 static int ncv_strcmp(const char *a, const char *b)
1270 size_t alen = strlen(a), blen = strlen(b);
1272 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1273 return strncmp(a, b, alen);
1274 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1275 return strncmp(a, b, blen);
1277 return strcmp(a, b);
1280 /* String hashing for non-co-versioned kernel and module. Here
1281 we are simply forced to drop the crc from the hash. */
1283 static unsigned long ncv_symbol_hash(const char *str)
1285 size_t len = strlen(str);
1286 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1288 return obj_elf_hash_n(str, len);
1292 obj_set_symbol_compare(struct obj_file *f,
1293 int (*cmp) (const char *, const char *),
1294 unsigned long (*hash) (const char *))
1297 f->symbol_cmp = cmp;
1299 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1302 f->symbol_hash = hash;
1304 memcpy(tmptab, f->symtab, sizeof(tmptab));
1305 memset(f->symtab, 0, sizeof(f->symtab));
1307 for (i = 0; i < HASH_BUCKETS; ++i)
1308 for (sym = tmptab[i]; sym; sym = next) {
1309 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1311 sym->next = f->symtab[h];
1317 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1320 struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
1321 unsigned long symidx, int info,
1322 int secidx, ElfW(Addr) value,
1325 struct obj_symbol *sym;
1326 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1327 int n_type = ELFW(ST_TYPE) (info);
1328 int n_binding = ELFW(ST_BIND) (info);
1330 for (sym = f->symtab[hash]; sym; sym = sym->next)
1331 if (f->symbol_cmp(sym->name, name) == 0) {
1332 int o_secidx = sym->secidx;
1333 int o_info = sym->info;
1334 int o_type = ELFW(ST_TYPE) (o_info);
1335 int o_binding = ELFW(ST_BIND) (o_info);
1337 /* A redefinition! Is it legal? */
1339 if (secidx == SHN_UNDEF)
1341 else if (o_secidx == SHN_UNDEF)
1343 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1344 /* Cope with local and global symbols of the same name
1345 in the same object file, as might have been created
1346 by ld -r. The only reason locals are now seen at this
1347 level at all is so that we can do semi-sensible things
1350 struct obj_symbol *nsym, **p;
1352 nsym = arch_new_symbol();
1353 nsym->next = sym->next;
1356 /* Excise the old (local) symbol from the hash chain. */
1357 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1361 } else if (n_binding == STB_LOCAL) {
1362 /* Another symbol of the same name has already been defined.
1363 Just add this to the local table. */
1364 sym = arch_new_symbol();
1367 f->local_symtab[symidx] = sym;
1369 } else if (n_binding == STB_WEAK)
1371 else if (o_binding == STB_WEAK)
1373 /* Don't unify COMMON symbols with object types the programmer
1375 else if (secidx == SHN_COMMON
1376 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1378 else if (o_secidx == SHN_COMMON
1379 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1382 /* Don't report an error if the symbol is coming from
1383 the kernel or some external module. */
1384 if (secidx <= SHN_HIRESERVE)
1385 error_msg("%s multiply defined", name);
1390 /* Completely new symbol. */
1391 sym = arch_new_symbol();
1392 sym->next = f->symtab[hash];
1393 f->symtab[hash] = sym;
1396 if (ELFW(ST_BIND) (info) == STB_LOCAL)
1397 f->local_symtab[symidx] = sym;
1403 sym->secidx = secidx;
1409 struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
1411 struct obj_symbol *sym;
1412 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1414 for (sym = f->symtab[hash]; sym; sym = sym->next)
1415 if (f->symbol_cmp(sym->name, name) == 0)
1422 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1425 if (sym->secidx >= SHN_LORESERVE)
1428 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1430 /* As a special case, a NULL sym has value zero. */
1435 struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1437 int i, n = f->header.e_shnum;
1439 for (i = 0; i < n; ++i)
1440 if (strcmp(f->sections[i]->name, name) == 0)
1441 return f->sections[i];
1446 static int obj_load_order_prio(struct obj_section *a)
1448 unsigned long af, ac;
1450 af = a->header.sh_flags;
1453 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1454 strcmp(a->name + 5, ".init"))
1458 if (!(af & SHF_WRITE))
1460 if (af & SHF_EXECINSTR)
1462 if (a->header.sh_type != SHT_NOBITS)
1469 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1471 struct obj_section **p;
1472 int prio = obj_load_order_prio(sec);
1473 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1474 if (obj_load_order_prio(*p) < prio)
1476 sec->load_next = *p;
1480 struct obj_section *obj_create_alloced_section(struct obj_file *f,
1482 unsigned long align,
1485 int newidx = f->header.e_shnum++;
1486 struct obj_section *sec;
1488 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1489 f->sections[newidx] = sec = arch_new_section();
1491 memset(sec, 0, sizeof(*sec));
1492 sec->header.sh_type = SHT_PROGBITS;
1493 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1494 sec->header.sh_size = size;
1495 sec->header.sh_addralign = align;
1499 sec->contents = xmalloc(size);
1501 obj_insert_section_load_order(f, sec);
1506 struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1508 unsigned long align,
1511 int newidx = f->header.e_shnum++;
1512 struct obj_section *sec;
1514 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1515 f->sections[newidx] = sec = arch_new_section();
1517 memset(sec, 0, sizeof(*sec));
1518 sec->header.sh_type = SHT_PROGBITS;
1519 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1520 sec->header.sh_size = size;
1521 sec->header.sh_addralign = align;
1525 sec->contents = xmalloc(size);
1527 sec->load_next = f->load_order;
1528 f->load_order = sec;
1529 if (f->load_order_search_start == &f->load_order)
1530 f->load_order_search_start = &sec->load_next;
1535 void *obj_extend_section(struct obj_section *sec, unsigned long more)
1537 unsigned long oldsize = sec->header.sh_size;
1538 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1539 return sec->contents + oldsize;
1544 /* Conditionally add the symbols from the given symbol set to the
1550 int idx, struct new_module_symbol *syms, size_t nsyms)
1552 struct new_module_symbol *s;
1556 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1558 /* Only add symbols that are already marked external. If we
1559 override locals we may cause problems for argument initialization.
1560 We will also create a false dependency on the module. */
1561 struct obj_symbol *sym;
1563 sym = obj_find_symbol(f, (char *) s->name);
1564 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1565 sym = obj_add_symbol(f, (char *) s->name, -1,
1566 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1568 /* Did our symbol just get installed? If so, mark the
1569 module as "used". */
1570 if (sym->secidx == idx)
1578 static void add_kernel_symbols(struct obj_file *f)
1580 struct external_module *m;
1583 /* Add module symbols first. */
1585 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1587 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1588 m->nsyms)) m->used = 1, ++nused;
1590 n_ext_modules_used = nused;
1592 /* And finally the symbols from the kernel proper. */
1595 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1598 static char *get_modinfo_value(struct obj_file *f, const char *key)
1600 struct obj_section *sec;
1601 char *p, *v, *n, *ep;
1602 size_t klen = strlen(key);
1604 sec = obj_find_section(f, ".modinfo");
1608 ep = p + sec->header.sh_size;
1611 n = strchr(p, '\0');
1613 if (p + klen == v && strncmp(p, key, klen) == 0)
1616 if (p + klen == n && strcmp(p, key) == 0)
1626 /*======================================================================*/
1627 /* Functions relating to module loading in pre 2.1 kernels. */
1630 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1634 struct obj_symbol *sym;
1638 if ((q = strchr(p, '=')) == NULL) {
1644 sym = obj_find_symbol(f, p);
1646 /* Also check that the parameter was not resolved from the kernel. */
1647 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1648 error_msg("symbol for parameter %s not found", p);
1652 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1654 /* Do C quoting if we begin with a ". */
1658 str = alloca(strlen(q));
1659 for (r = str, q++; *q != '"'; ++q, ++r) {
1661 error_msg("improperly terminated string argument for %s", p);
1663 } else if (*q == '\\')
1697 if (q[1] >= '0' && q[1] <= '7') {
1698 c = (c * 8) + *++q - '0';
1699 if (q[1] >= '0' && q[1] <= '7')
1700 c = (c * 8) + *++q - '0';
1713 obj_string_patch(f, sym->secidx, sym->value, str);
1714 } else if (*q >= '0' && *q <= '9') {
1716 *loc++ = strtoul(q, &q, 0);
1717 while (*q++ == ',');
1719 char *contents = f->sections[sym->secidx]->contents;
1720 char *myloc = contents + sym->value;
1721 char *r; /* To search for commas */
1723 /* Break the string with comas */
1724 while ((r = strchr(q, ',')) != (char *) NULL) {
1726 obj_string_patch(f, sym->secidx, myloc - contents, q);
1727 myloc += sizeof(char *);
1732 obj_string_patch(f, sym->secidx, myloc - contents, q);
1741 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1742 static int old_is_module_checksummed(struct obj_file *f)
1744 return obj_find_symbol(f, "Using_Versions") != NULL;
1746 /* Get the module's kernel version in the canonical integer form. */
1749 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1751 struct obj_symbol *sym;
1755 sym = obj_find_symbol(f, "kernel_version");
1759 p = f->sections[sym->secidx]->contents + sym->value;
1760 strncpy(str, p, STRVERSIONLEN);
1762 a = strtoul(p, &p, 10);
1765 b = strtoul(p + 1, &p, 10);
1768 c = strtoul(p + 1, &q, 10);
1772 return a << 16 | b << 8 | c;
1775 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1777 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
1779 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1781 static int old_get_kernel_symbols(const char *m_name)
1783 struct old_kernel_sym *ks, *k;
1784 struct new_module_symbol *s;
1785 struct external_module *mod;
1786 int nks, nms, nmod, i;
1788 nks = get_kernel_syms(NULL);
1790 perror_msg("get_kernel_syms: %s", m_name);
1794 ks = k = xmalloc(nks * sizeof(*ks));
1796 if (get_kernel_syms(ks) != nks) {
1797 perror("inconsistency with get_kernel_syms -- is someone else "
1798 "playing with modules?");
1803 /* Collect the module information. */
1808 while (k->name[0] == '#' && k->name[1]) {
1809 struct old_kernel_sym *k2;
1810 struct new_module_symbol *s;
1812 /* Find out how many symbols this module has. */
1813 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1817 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1818 mod[nmod].name = k->name + 1;
1819 mod[nmod].addr = k->value;
1821 mod[nmod].nsyms = nms;
1822 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1824 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1825 s->name = (unsigned long) k->name;
1826 s->value = k->value;
1833 n_ext_modules = nmod + 1;
1835 /* Now collect the symbols for the kernel proper. */
1837 if (k->name[0] == '#')
1840 nksyms = nms = nks - (k - ks);
1841 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1843 for (i = 0; i < nms; ++i, ++s, ++k) {
1844 s->name = (unsigned long) k->name;
1845 s->value = k->value;
1851 /* Return the kernel symbol checksum version, or zero if not used. */
1853 static int old_is_kernel_checksummed(void)
1855 /* Using_Versions is the first symbol. */
1857 && strcmp((char *) ksyms[0].name,
1858 "Using_Versions") == 0) return ksyms[0].value;
1864 static int old_create_mod_use_count(struct obj_file *f)
1866 struct obj_section *sec;
1868 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1871 obj_add_symbol(f, "mod_use_count_", -1,
1872 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1879 old_init_module(const char *m_name, struct obj_file *f,
1880 unsigned long m_size)
1883 struct old_mod_routines routines;
1884 struct old_symbol_table *symtab;
1887 /* Create the symbol table */
1889 int nsyms = 0, strsize = 0, total;
1891 /* Size things first... */
1894 for (i = 0; i < HASH_BUCKETS; ++i) {
1895 struct obj_symbol *sym;
1896 for (sym = f->symtab[i]; sym; sym = sym->next)
1897 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1898 && sym->secidx <= SHN_HIRESERVE)
1900 sym->ksymidx = nsyms++;
1901 strsize += strlen(sym->name) + 1;
1906 total = (sizeof(struct old_symbol_table)
1907 + nsyms * sizeof(struct old_module_symbol)
1908 + n_ext_modules_used * sizeof(struct old_module_ref)
1910 symtab = xmalloc(total);
1911 symtab->size = total;
1912 symtab->n_symbols = nsyms;
1913 symtab->n_refs = n_ext_modules_used;
1915 if (flag_export && nsyms) {
1916 struct old_module_symbol *ksym;
1920 ksym = symtab->symbol;
1921 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1922 + n_ext_modules_used * sizeof(struct old_module_ref));
1924 for (i = 0; i < HASH_BUCKETS; ++i) {
1925 struct obj_symbol *sym;
1926 for (sym = f->symtab[i]; sym; sym = sym->next)
1927 if (sym->ksymidx >= 0) {
1928 ksym->addr = obj_symbol_final_value(f, sym);
1930 (unsigned long) str - (unsigned long) symtab;
1932 strcpy(str, sym->name);
1933 str += strlen(sym->name) + 1;
1939 if (n_ext_modules_used) {
1940 struct old_module_ref *ref;
1943 ref = (struct old_module_ref *)
1944 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
1946 for (i = 0; i < n_ext_modules; ++i)
1947 if (ext_modules[i].used)
1948 ref++->module = ext_modules[i].addr;
1952 /* Fill in routines. */
1955 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
1957 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
1959 /* Whew! All of the initialization is complete. Collect the final
1960 module image and give it to the kernel. */
1962 image = xmalloc(m_size);
1963 obj_create_image(f, image);
1965 /* image holds the complete relocated module, accounting correctly for
1966 mod_use_count. However the old module kernel support assume that
1967 it is receiving something which does not contain mod_use_count. */
1968 ret = old_sys_init_module(m_name, image + sizeof(long),
1969 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
1970 : 0), &routines, symtab);
1972 perror_msg("init_module: %s", m_name);
1982 #define old_create_mod_use_count(x) TRUE
1983 #define old_init_module(x, y, z) TRUE
1985 #endif /* BB_FEATURE_OLD_MODULE_INTERFACE */
1989 /*======================================================================*/
1990 /* Functions relating to module loading after 2.1.18. */
1993 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
1997 struct obj_symbol *sym;
1998 char *contents, *loc;
2002 if ((q = strchr(p, '=')) == NULL) {
2007 key = alloca(q - p + 6);
2008 memcpy(key, "parm_", 5);
2009 memcpy(key + 5, p, q - p);
2012 p = get_modinfo_value(f, key);
2015 error_msg("invalid parameter %s", key);
2019 sym = obj_find_symbol(f, key);
2021 /* Also check that the parameter was not resolved from the kernel. */
2022 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2023 error_msg("symbol for parameter %s not found", key);
2028 min = strtoul(p, &p, 10);
2030 max = strtoul(p + 1, &p, 10);
2036 contents = f->sections[sym->secidx]->contents;
2037 loc = contents + sym->value;
2041 if ((*p == 's') || (*p == 'c')) {
2044 /* Do C quoting if we begin with a ", else slurp the lot. */
2048 str = alloca(strlen(q));
2049 for (r = str, q++; *q != '"'; ++q, ++r) {
2051 error_msg("improperly terminated string argument for %s",
2054 } else if (*q == '\\')
2088 if (q[1] >= '0' && q[1] <= '7') {
2089 c = (c * 8) + *++q - '0';
2090 if (q[1] >= '0' && q[1] <= '7')
2091 c = (c * 8) + *++q - '0';
2108 /* In this case, the string is not quoted. We will break
2109 it using the coma (like for ints). If the user wants to
2110 include comas in a string, he just has to quote it */
2112 /* Search the next coma */
2116 if (r != (char *) NULL) {
2117 /* Recopy the current field */
2118 str = alloca(r - q + 1);
2119 memcpy(str, q, r - q);
2121 /* I don't know if it is usefull, as the previous case
2122 doesn't null terminate the string ??? */
2125 /* Keep next fields */
2136 obj_string_patch(f, sym->secidx, loc - contents, str);
2137 loc += tgt_sizeof_char_p;
2139 /* Array of chars (in fact, matrix !) */
2140 unsigned long charssize; /* size of each member */
2142 /* Get the size of each member */
2143 /* Probably we should do that outside the loop ? */
2144 if (!isdigit(*(p + 1))) {
2145 error_msg("parameter type 'c' for %s must be followed by"
2146 " the maximum size", key);
2149 charssize = strtoul(p + 1, (char **) NULL, 10);
2152 if (strlen(str) >= charssize) {
2153 error_msg("string too long for %s (max %ld)", key,
2158 /* Copy to location */
2159 strcpy((char *) loc, str);
2163 long v = strtoul(q, &q, 0);
2170 loc += tgt_sizeof_short;
2174 loc += tgt_sizeof_int;
2178 loc += tgt_sizeof_long;
2182 error_msg("unknown parameter type '%c' for %s", *p, key);
2197 goto retry_end_of_value;
2201 error_msg("too many values for %s (max %d)", key, max);
2208 error_msg("invalid argument syntax for %s", key);
2215 error_msg("too few values for %s (min %d)", key, min);
2225 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2226 static int new_is_module_checksummed(struct obj_file *f)
2228 const char *p = get_modinfo_value(f, "using_checksums");
2235 /* Get the module's kernel version in the canonical integer form. */
2238 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2243 p = get_modinfo_value(f, "kernel_version");
2246 strncpy(str, p, STRVERSIONLEN);
2248 a = strtoul(p, &p, 10);
2251 b = strtoul(p + 1, &p, 10);
2254 c = strtoul(p + 1, &q, 10);
2258 return a << 16 | b << 8 | c;
2261 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2264 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
2266 /* Fetch the loaded modules, and all currently exported symbols. */
2268 static int new_get_kernel_symbols(void)
2270 char *module_names, *mn;
2271 struct external_module *modules, *m;
2272 struct new_module_symbol *syms, *s;
2273 size_t ret, bufsize, nmod, nsyms, i, j;
2275 /* Collect the loaded modules. */
2277 module_names = xmalloc(bufsize = 256);
2279 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2280 if (errno == ENOSPC) {
2281 module_names = xrealloc(module_names, bufsize = ret);
2282 goto retry_modules_load;
2284 perror_msg("QM_MODULES");
2288 n_ext_modules = nmod = ret;
2290 /* Collect the modules' symbols. */
2293 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2294 memset(modules, 0, nmod * sizeof(*modules));
2295 for (i = 0, mn = module_names, m = modules;
2296 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2297 struct new_module_info info;
2299 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2300 if (errno == ENOENT) {
2301 /* The module was removed out from underneath us. */
2304 perror_msg("query_module: QM_INFO: %s", mn);
2308 syms = xmalloc(bufsize = 1024);
2310 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2313 syms = xrealloc(syms, bufsize = ret);
2314 goto retry_mod_sym_load;
2316 /* The module was removed out from underneath us. */
2319 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2326 m->addr = info.addr;
2330 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2331 s->name += (unsigned long) syms;
2336 /* Collect the kernel's symbols. */
2338 syms = xmalloc(bufsize = 16 * 1024);
2339 retry_kern_sym_load:
2340 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2341 if (errno == ENOSPC) {
2342 syms = xrealloc(syms, bufsize = ret);
2343 goto retry_kern_sym_load;
2345 perror_msg("kernel: QM_SYMBOLS");
2348 nksyms = nsyms = ret;
2351 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2352 s->name += (unsigned long) syms;
2358 /* Return the kernel symbol checksum version, or zero if not used. */
2360 static int new_is_kernel_checksummed(void)
2362 struct new_module_symbol *s;
2365 /* Using_Versions is not the first symbol, but it should be in there. */
2367 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2368 if (strcmp((char *) s->name, "Using_Versions") == 0)
2375 static int new_create_this_module(struct obj_file *f, const char *m_name)
2377 struct obj_section *sec;
2379 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2380 sizeof(struct new_module));
2381 memset(sec->contents, 0, sizeof(struct new_module));
2383 obj_add_symbol(f, "__this_module", -1,
2384 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2385 sizeof(struct new_module));
2387 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2394 static int new_create_module_ksymtab(struct obj_file *f)
2396 struct obj_section *sec;
2399 /* We must always add the module references. */
2401 if (n_ext_modules_used) {
2402 struct new_module_ref *dep;
2403 struct obj_symbol *tm;
2405 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2406 (sizeof(struct new_module_ref)
2407 * n_ext_modules_used));
2411 tm = obj_find_symbol(f, "__this_module");
2412 dep = (struct new_module_ref *) sec->contents;
2413 for (i = 0; i < n_ext_modules; ++i)
2414 if (ext_modules[i].used) {
2415 dep->dep = ext_modules[i].addr;
2416 obj_symbol_patch(f, sec->idx,
2417 (char *) &dep->ref - sec->contents, tm);
2423 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2428 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2431 /* We don't want to export symbols residing in sections that
2432 aren't loaded. There are a number of these created so that
2433 we make sure certain module options don't appear twice. */
2435 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2437 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2439 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2440 struct obj_symbol *sym;
2441 for (sym = f->symtab[i]; sym; sym = sym->next)
2442 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2443 && sym->secidx <= SHN_HIRESERVE
2444 && (sym->secidx >= SHN_LORESERVE
2445 || loaded[sym->secidx])) {
2446 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2448 obj_symbol_patch(f, sec->idx, ofs, sym);
2449 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2456 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2464 new_init_module(const char *m_name, struct obj_file *f,
2465 unsigned long m_size)
2467 struct new_module *module;
2468 struct obj_section *sec;
2473 sec = obj_find_section(f, ".this");
2474 module = (struct new_module *) sec->contents;
2475 m_addr = sec->header.sh_addr;
2477 module->size_of_struct = sizeof(*module);
2478 module->size = m_size;
2479 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2481 sec = obj_find_section(f, "__ksymtab");
2482 if (sec && sec->header.sh_size) {
2483 module->syms = sec->header.sh_addr;
2484 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2487 if (n_ext_modules_used) {
2488 sec = obj_find_section(f, ".kmodtab");
2489 module->deps = sec->header.sh_addr;
2490 module->ndeps = n_ext_modules_used;
2494 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2496 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2498 sec = obj_find_section(f, "__ex_table");
2500 module->ex_table_start = sec->header.sh_addr;
2501 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2504 sec = obj_find_section(f, ".text.init");
2506 module->runsize = sec->header.sh_addr - m_addr;
2508 sec = obj_find_section(f, ".data.init");
2510 if (!module->runsize ||
2511 module->runsize > sec->header.sh_addr - m_addr)
2512 module->runsize = sec->header.sh_addr - m_addr;
2515 if (!arch_init_module(f, module))
2518 /* Whew! All of the initialization is complete. Collect the final
2519 module image and give it to the kernel. */
2521 image = xmalloc(m_size);
2522 obj_create_image(f, image);
2524 ret = new_sys_init_module(m_name, (struct new_module *) image);
2526 perror_msg("init_module: %s", m_name);
2535 #define new_init_module(x, y, z) TRUE
2536 #define new_create_this_module(x, y) 0
2537 #define new_create_module_ksymtab(x)
2538 #define query_module(v, w, x, y, z) -1
2540 #endif /* BB_FEATURE_NEW_MODULE_INTERFACE */
2543 /*======================================================================*/
2546 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2549 struct obj_string_patch *p;
2550 struct obj_section *strsec;
2551 size_t len = strlen(string) + 1;
2554 p = xmalloc(sizeof(*p));
2555 p->next = f->string_patches;
2556 p->reloc_secidx = secidx;
2557 p->reloc_offset = offset;
2558 f->string_patches = p;
2560 strsec = obj_find_section(f, ".kstrtab");
2561 if (strsec == NULL) {
2562 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2563 p->string_offset = 0;
2564 loc = strsec->contents;
2566 p->string_offset = strsec->header.sh_size;
2567 loc = obj_extend_section(strsec, len);
2569 memcpy(loc, string, len);
2575 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2576 struct obj_symbol *sym)
2578 struct obj_symbol_patch *p;
2580 p = xmalloc(sizeof(*p));
2581 p->next = f->symbol_patches;
2582 p->reloc_secidx = secidx;
2583 p->reloc_offset = offset;
2585 f->symbol_patches = p;
2590 int obj_check_undefineds(struct obj_file *f)
2595 for (i = 0; i < HASH_BUCKETS; ++i) {
2596 struct obj_symbol *sym;
2597 for (sym = f->symtab[i]; sym; sym = sym->next)
2598 if (sym->secidx == SHN_UNDEF) {
2599 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2600 sym->secidx = SHN_ABS;
2603 error_msg("unresolved symbol %s", sym->name);
2612 void obj_allocate_commons(struct obj_file *f)
2614 struct common_entry {
2615 struct common_entry *next;
2616 struct obj_symbol *sym;
2617 } *common_head = NULL;
2621 for (i = 0; i < HASH_BUCKETS; ++i) {
2622 struct obj_symbol *sym;
2623 for (sym = f->symtab[i]; sym; sym = sym->next)
2624 if (sym->secidx == SHN_COMMON) {
2625 /* Collect all COMMON symbols and sort them by size so as to
2626 minimize space wasted by alignment requirements. */
2628 struct common_entry **p, *n;
2629 for (p = &common_head; *p; p = &(*p)->next)
2630 if (sym->size <= (*p)->sym->size)
2633 n = alloca(sizeof(*n));
2641 for (i = 1; i < f->local_symtab_size; ++i) {
2642 struct obj_symbol *sym = f->local_symtab[i];
2643 if (sym && sym->secidx == SHN_COMMON) {
2644 struct common_entry **p, *n;
2645 for (p = &common_head; *p; p = &(*p)->next)
2646 if (sym == (*p)->sym)
2648 else if (sym->size < (*p)->sym->size) {
2649 n = alloca(sizeof(*n));
2659 /* Find the bss section. */
2660 for (i = 0; i < f->header.e_shnum; ++i)
2661 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2664 /* If for some reason there hadn't been one, create one. */
2665 if (i == f->header.e_shnum) {
2666 struct obj_section *sec;
2668 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2669 f->sections[i] = sec = arch_new_section();
2670 f->header.e_shnum = i + 1;
2672 memset(sec, 0, sizeof(*sec));
2673 sec->header.sh_type = SHT_PROGBITS;
2674 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2679 /* Allocate the COMMONS. */
2681 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2682 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2683 struct common_entry *c;
2685 for (c = common_head; c; c = c->next) {
2686 ElfW(Addr) align = c->sym->value;
2688 if (align > max_align)
2690 if (bss_size & (align - 1))
2691 bss_size = (bss_size | (align - 1)) + 1;
2694 c->sym->value = bss_size;
2696 bss_size += c->sym->size;
2699 f->sections[i]->header.sh_size = bss_size;
2700 f->sections[i]->header.sh_addralign = max_align;
2704 /* For the sake of patch relocation and parameter initialization,
2705 allocate zeroed data for NOBITS sections now. Note that after
2706 this we cannot assume NOBITS are really empty. */
2707 for (i = 0; i < f->header.e_shnum; ++i) {
2708 struct obj_section *s = f->sections[i];
2709 if (s->header.sh_type == SHT_NOBITS) {
2710 if (s->header.sh_size != 0)
2711 s->contents = memset(xmalloc(s->header.sh_size),
2712 0, s->header.sh_size);
2716 s->header.sh_type = SHT_PROGBITS;
2721 unsigned long obj_load_size(struct obj_file *f)
2723 unsigned long dot = 0;
2724 struct obj_section *sec;
2726 /* Finalize the positions of the sections relative to one another. */
2728 for (sec = f->load_order; sec; sec = sec->load_next) {
2731 align = sec->header.sh_addralign;
2732 if (align && (dot & (align - 1)))
2733 dot = (dot | (align - 1)) + 1;
2735 sec->header.sh_addr = dot;
2736 dot += sec->header.sh_size;
2742 int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2744 int i, n = f->header.e_shnum;
2747 /* Finalize the addresses of the sections. */
2750 for (i = 0; i < n; ++i)
2751 f->sections[i]->header.sh_addr += base;
2753 /* And iterate over all of the relocations. */
2755 for (i = 0; i < n; ++i) {
2756 struct obj_section *relsec, *symsec, *targsec, *strsec;
2757 ElfW(RelM) * rel, *relend;
2761 relsec = f->sections[i];
2762 if (relsec->header.sh_type != SHT_RELM)
2765 symsec = f->sections[relsec->header.sh_link];
2766 targsec = f->sections[relsec->header.sh_info];
2767 strsec = f->sections[symsec->header.sh_link];
2769 rel = (ElfW(RelM) *) relsec->contents;
2770 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2771 symtab = (ElfW(Sym) *) symsec->contents;
2772 strtab = (const char *) strsec->contents;
2774 for (; rel < relend; ++rel) {
2775 ElfW(Addr) value = 0;
2776 struct obj_symbol *intsym = NULL;
2777 unsigned long symndx;
2778 ElfW(Sym) * extsym = 0;
2781 /* Attempt to find a value to use for this relocation. */
2783 symndx = ELFW(R_SYM) (rel->r_info);
2785 /* Note we've already checked for undefined symbols. */
2787 extsym = &symtab[symndx];
2788 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2789 /* Local symbols we look up in the local table to be sure
2790 we get the one that is really intended. */
2791 intsym = f->local_symtab[symndx];
2793 /* Others we look up in the hash table. */
2795 if (extsym->st_name)
2796 name = strtab + extsym->st_name;
2798 name = f->sections[extsym->st_shndx]->name;
2799 intsym = obj_find_symbol(f, name);
2802 value = obj_symbol_final_value(f, intsym);
2803 intsym->referenced = 1;
2805 #if SHT_RELM == SHT_RELA
2806 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2807 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2808 if (!extsym || !extsym->st_name ||
2809 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2811 value += rel->r_addend;
2815 switch (arch_apply_relocation
2816 (f, targsec, symsec, intsym, rel, value)) {
2820 case obj_reloc_overflow:
2821 errmsg = "Relocation overflow";
2823 case obj_reloc_dangerous:
2824 errmsg = "Dangerous relocation";
2826 case obj_reloc_unhandled:
2827 errmsg = "Unhandled relocation";
2830 error_msg("%s of type %ld for %s", errmsg,
2831 (long) ELFW(R_TYPE) (rel->r_info),
2832 strtab + extsym->st_name);
2834 error_msg("%s of type %ld", errmsg,
2835 (long) ELFW(R_TYPE) (rel->r_info));
2843 /* Finally, take care of the patches. */
2845 if (f->string_patches) {
2846 struct obj_string_patch *p;
2847 struct obj_section *strsec;
2848 ElfW(Addr) strsec_base;
2849 strsec = obj_find_section(f, ".kstrtab");
2850 strsec_base = strsec->header.sh_addr;
2852 for (p = f->string_patches; p; p = p->next) {
2853 struct obj_section *targsec = f->sections[p->reloc_secidx];
2854 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2855 = strsec_base + p->string_offset;
2859 if (f->symbol_patches) {
2860 struct obj_symbol_patch *p;
2862 for (p = f->symbol_patches; p; p = p->next) {
2863 struct obj_section *targsec = f->sections[p->reloc_secidx];
2864 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2865 = obj_symbol_final_value(f, p->sym);
2872 int obj_create_image(struct obj_file *f, char *image)
2874 struct obj_section *sec;
2875 ElfW(Addr) base = f->baseaddr;
2877 for (sec = f->load_order; sec; sec = sec->load_next) {
2880 if (sec->contents == 0 || sec->header.sh_size == 0)
2883 secimg = image + (sec->header.sh_addr - base);
2885 /* Note that we allocated data for NOBITS sections earlier. */
2886 memcpy(secimg, sec->contents, sec->header.sh_size);
2892 /*======================================================================*/
2894 struct obj_file *obj_load(FILE * fp)
2897 ElfW(Shdr) * section_headers;
2901 /* Read the file header. */
2903 f = arch_new_file();
2904 memset(f, 0, sizeof(*f));
2905 f->symbol_cmp = strcmp;
2906 f->symbol_hash = obj_elf_hash;
2907 f->load_order_search_start = &f->load_order;
2909 fseek(fp, 0, SEEK_SET);
2910 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2911 perror_msg("error reading ELF header");
2915 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2916 || f->header.e_ident[EI_MAG1] != ELFMAG1
2917 || f->header.e_ident[EI_MAG2] != ELFMAG2
2918 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2919 error_msg("not an ELF file");
2922 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2923 || f->header.e_ident[EI_DATA] != ELFDATAM
2924 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2925 || !MATCH_MACHINE(f->header.e_machine)) {
2926 error_msg("ELF file not for this architecture");
2929 if (f->header.e_type != ET_REL) {
2930 error_msg("ELF file not a relocatable object");
2934 /* Read the section headers. */
2936 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
2937 error_msg("section header size mismatch: %lu != %lu",
2938 (unsigned long) f->header.e_shentsize,
2939 (unsigned long) sizeof(ElfW(Shdr)));
2943 shnum = f->header.e_shnum;
2944 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
2945 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
2947 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
2948 fseek(fp, f->header.e_shoff, SEEK_SET);
2949 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
2950 perror_msg("error reading ELF section headers");
2954 /* Read the section data. */
2956 for (i = 0; i < shnum; ++i) {
2957 struct obj_section *sec;
2959 f->sections[i] = sec = arch_new_section();
2960 memset(sec, 0, sizeof(*sec));
2962 sec->header = section_headers[i];
2965 if(sec->header.sh_size) switch (sec->header.sh_type) {
2976 if (sec->header.sh_size > 0) {
2977 sec->contents = xmalloc(sec->header.sh_size);
2978 fseek(fp, sec->header.sh_offset, SEEK_SET);
2979 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
2980 perror_msg("error reading ELF section data");
2984 sec->contents = NULL;
2988 #if SHT_RELM == SHT_REL
2990 error_msg("RELA relocations not supported on this architecture");
2994 error_msg("REL relocations not supported on this architecture");
2999 if (sec->header.sh_type >= SHT_LOPROC) {
3000 /* Assume processor specific section types are debug
3001 info and can safely be ignored. If this is ever not
3002 the case (Hello MIPS?), don't put ifdefs here but
3003 create an arch_load_proc_section(). */
3007 error_msg("can't handle sections of type %ld",
3008 (long) sec->header.sh_type);
3013 /* Do what sort of interpretation as needed by each section. */
3015 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3017 for (i = 0; i < shnum; ++i) {
3018 struct obj_section *sec = f->sections[i];
3019 sec->name = shstrtab + sec->header.sh_name;
3022 for (i = 0; i < shnum; ++i) {
3023 struct obj_section *sec = f->sections[i];
3025 if (sec->header.sh_flags & SHF_ALLOC)
3026 obj_insert_section_load_order(f, sec);
3028 switch (sec->header.sh_type) {
3031 unsigned long nsym, j;
3035 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3036 error_msg("symbol size mismatch: %lu != %lu",
3037 (unsigned long) sec->header.sh_entsize,
3038 (unsigned long) sizeof(ElfW(Sym)));
3042 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3043 strtab = f->sections[sec->header.sh_link]->contents;
3044 sym = (ElfW(Sym) *) sec->contents;
3046 /* Allocate space for a table of local symbols. */
3047 j = f->local_symtab_size = sec->header.sh_info;
3048 f->local_symtab = xmalloc(j *=
3049 sizeof(struct obj_symbol *));
3050 memset(f->local_symtab, 0, j);
3052 /* Insert all symbols into the hash table. */
3053 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3056 name = strtab + sym->st_name;
3058 name = f->sections[sym->st_shndx]->name;
3060 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3061 sym->st_value, sym->st_size);
3067 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3068 error_msg("relocation entry size mismatch: %lu != %lu",
3069 (unsigned long) sec->header.sh_entsize,
3070 (unsigned long) sizeof(ElfW(RelM)));
3080 static void hide_special_symbols(struct obj_file *f)
3082 static const char *const specials[] = {
3089 struct obj_symbol *sym;
3090 const char *const *p;
3092 for (p = specials; *p; ++p)
3093 if ((sym = obj_find_symbol(f, *p)) != NULL)
3095 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3100 extern int insmod_main( int argc, char **argv)
3107 unsigned long m_size;
3112 char m_name[BUFSIZ + 1] = "\0";
3113 int exit_status = EXIT_FAILURE;
3115 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3117 char k_strversion[STRVERSIONLEN];
3118 char m_strversion[STRVERSIONLEN];
3123 /* Parse any options */
3124 while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
3126 case 'f': /* force loading */
3127 flag_force_load = 1;
3129 case 'k': /* module loaded by kerneld, auto-cleanable */
3132 case 'v': /* verbose output */
3135 case 'x': /* do not export externs */
3138 case 'o': /* name the output module */
3139 strncpy(m_name, optarg, BUFSIZ);
3141 case 'L': /* Stub warning */
3142 /* This is needed for compatibility with modprobe.
3143 * In theory, this does locking, but we don't do
3144 * that. So be careful and plan your life around not
3145 * loading the same module 50 times concurrently. */
3152 if (argv[optind] == NULL) {
3156 /* Grab the module name */
3157 if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3164 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3166 strncpy(m_fullName, tmp, len);
3167 if (*m_name == '\0') {
3168 strcpy(m_name, m_fullName);
3170 strcat(m_fullName, ".o");
3172 /* Get a filedesc for the module */
3173 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3174 (fp = fopen(argv[optind], "r")) == NULL) {
3175 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
3176 if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE,
3177 findNamedModule, 0, m_fullName) == FALSE)
3179 if (m_filename[0] == '\0'
3180 || ((fp = fopen(m_filename, "r")) == NULL))
3182 error_msg("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
3183 return EXIT_FAILURE;
3186 error_msg_and_die("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
3188 memcpy(m_filename, argv[optind], strlen(argv[optind]));
3191 if ((f = obj_load(fp)) == NULL)
3192 perror_msg_and_die("Could not load the module");
3194 if (get_modinfo_value(f, "kernel_version") == NULL)
3199 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3200 /* Version correspondence? */
3202 k_version = get_kernel_version(k_strversion);
3203 if (m_has_modinfo) {
3204 m_version = new_get_module_version(f, m_strversion);
3206 m_version = old_get_module_version(f, m_strversion);
3207 if (m_version == -1) {
3208 error_msg("couldn't find the kernel version the module was "
3214 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
3215 if (flag_force_load) {
3216 error_msg("Warning: kernel-module version mismatch\n"
3217 "\t%s was compiled for kernel version %s\n"
3218 "\twhile this kernel is version %s",
3219 m_filename, m_strversion, k_strversion);
3221 error_msg("kernel-module version mismatch\n"
3222 "\t%s was compiled for kernel version %s\n"
3223 "\twhile this kernel is version %s.",
3224 m_filename, m_strversion, k_strversion);
3229 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3231 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3233 if (k_new_syscalls) {
3234 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
3235 if (!new_get_kernel_symbols())
3237 k_crcs = new_is_kernel_checksummed();
3239 error_msg("Not configured to support new kernels");
3243 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
3244 if (!old_get_kernel_symbols(m_name))
3246 k_crcs = old_is_kernel_checksummed();
3248 error_msg("Not configured to support old kernels");
3253 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3255 m_crcs = new_is_module_checksummed(f);
3257 m_crcs = old_is_module_checksummed(f);
3259 if (m_crcs != k_crcs)
3260 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3261 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3263 /* Let the module know about the kernel symbols. */
3264 add_kernel_symbols(f);
3266 /* Allocate common symbols, symbol tables, and string tables. */
3269 ? !new_create_this_module(f, m_name)
3270 : !old_create_mod_use_count(f))
3275 if (!obj_check_undefineds(f)) {
3278 obj_allocate_commons(f);
3280 /* done with the module name, on to the optional var=value arguments */
3283 if (optind < argc) {
3285 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3286 : !old_process_module_arguments(f, argc - optind, argv + optind))
3293 hide_special_symbols(f);
3296 new_create_module_ksymtab(f);
3298 /* Find current size of the module */
3299 m_size = obj_load_size(f);
3302 m_addr = create_module(m_name, m_size);
3303 if (m_addr==-1) switch (errno) {
3305 error_msg("A module named %s already exists", m_name);
3308 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3312 perror_msg("create_module: %s", m_name);
3316 if (!obj_relocate(f, m_addr)) {
3317 delete_module(m_name);
3322 ? !new_init_module(m_name, f, m_size)
3323 : !old_init_module(m_name, f, m_size))
3325 delete_module(m_name);
3329 exit_status = EXIT_SUCCESS;
3333 return(exit_status);