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 #if defined(__powerpc__)
73 #define BB_USE_PLT_ENTRIES
74 #define BB_PLT_ENTRY_SIZE 16
78 #define BB_USE_PLT_ENTRIES
79 #define BB_PLT_ENTRY_SIZE 8
80 #define BB_USE_GOT_ENTRIES
81 #define BB_GOT_ENTRY_SIZE 8
85 #define BB_USE_GOT_ENTRIES
86 #define BB_GOT_ENTRY_SIZE 4
90 #define BB_USE_GOT_ENTRIES
91 #define BB_GOT_ENTRY_SIZE 4
98 //----------------------------------------------------------------------------
99 //--------modutils module.h, lines 45-242
100 //----------------------------------------------------------------------------
102 /* Definitions for the Linux module syscall interface.
103 Copyright 1996, 1997 Linux International.
105 Contributed by Richard Henderson <rth@tamu.edu>
107 This file is part of the Linux modutils.
109 This program is free software; you can redistribute it and/or modify it
110 under the terms of the GNU General Public License as published by the
111 Free Software Foundation; either version 2 of the License, or (at your
112 option) any later version.
114 This program is distributed in the hope that it will be useful, but
115 WITHOUT ANY WARRANTY; without even the implied warranty of
116 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
117 General Public License for more details.
119 You should have received a copy of the GNU General Public License
120 along with this program; if not, write to the Free Software Foundation,
121 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
124 #ifndef MODUTILS_MODULE_H
125 static const int MODUTILS_MODULE_H = 1;
127 #ident "$Id: insmod.c,v 1.59 2001/04/25 17:22:32 andersen Exp $"
129 /* This file contains the structures used by the 2.0 and 2.1 kernels.
130 We do not use the kernel headers directly because we do not wish
131 to be dependant on a particular kernel version to compile insmod. */
134 /*======================================================================*/
135 /* The structures used by Linux 2.0. */
137 /* The symbol format used by get_kernel_syms(2). */
138 struct old_kernel_sym
144 struct old_module_ref
146 unsigned long module; /* kernel addresses */
150 struct old_module_symbol
156 struct old_symbol_table
158 int size; /* total, including string table!!! */
161 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
162 struct old_module_ref ref[0]; /* actual size defined by n_refs */
165 struct old_mod_routines
168 unsigned long cleanup;
174 unsigned long ref; /* the list of modules that refer to me */
175 unsigned long symtab;
177 int size; /* size of module in pages */
178 unsigned long addr; /* address of module */
180 unsigned long cleanup; /* cleanup routine */
183 /* Sent to init_module(2) or'ed into the code size parameter. */
184 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
186 int get_kernel_syms(struct old_kernel_sym *);
187 int old_sys_init_module(const char *name, char *code, unsigned codesize,
188 struct old_mod_routines *, struct old_symbol_table *);
190 /*======================================================================*/
191 /* For sizeof() which are related to the module platform and not to the
192 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
194 #define tgt_sizeof_char sizeof(char)
195 #define tgt_sizeof_short sizeof(short)
196 #define tgt_sizeof_int sizeof(int)
197 #define tgt_sizeof_long sizeof(long)
198 #define tgt_sizeof_char_p sizeof(char *)
199 #define tgt_sizeof_void_p sizeof(void *)
200 #define tgt_long long
202 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
203 #undef tgt_sizeof_long
204 #undef tgt_sizeof_char_p
205 #undef tgt_sizeof_void_p
207 static const int tgt_sizeof_long = 8;
208 static const int tgt_sizeof_char_p = 8;
209 static const int tgt_sizeof_void_p = 8;
210 #define tgt_long long long
213 /*======================================================================*/
214 /* The structures used in Linux 2.1. */
216 /* Note: new_module_symbol does not use tgt_long intentionally */
217 struct new_module_symbol
223 struct new_module_persist;
225 struct new_module_ref
227 unsigned tgt_long dep; /* kernel addresses */
228 unsigned tgt_long ref;
229 unsigned tgt_long next_ref;
234 unsigned tgt_long size_of_struct; /* == sizeof(module) */
235 unsigned tgt_long next;
236 unsigned tgt_long name;
237 unsigned tgt_long size;
240 unsigned tgt_long flags; /* AUTOCLEAN et al */
245 unsigned tgt_long syms;
246 unsigned tgt_long deps;
247 unsigned tgt_long refs;
248 unsigned tgt_long init;
249 unsigned tgt_long cleanup;
250 unsigned tgt_long ex_table_start;
251 unsigned tgt_long ex_table_end;
253 unsigned tgt_long gp;
255 /* Everything after here is extension. */
256 unsigned tgt_long persist_start;
257 unsigned tgt_long persist_end;
258 unsigned tgt_long can_unload;
259 unsigned tgt_long runsize;
262 struct new_module_info
270 /* Bits of module.flags. */
271 static const int NEW_MOD_RUNNING = 1;
272 static const int NEW_MOD_DELETED = 2;
273 static const int NEW_MOD_AUTOCLEAN = 4;
274 static const int NEW_MOD_VISITED = 8;
275 static const int NEW_MOD_USED_ONCE = 16;
277 int new_sys_init_module(const char *name, const struct new_module *);
278 int query_module(const char *name, int which, void *buf, size_t bufsize,
281 /* Values for query_module's which. */
283 static const int QM_MODULES = 1;
284 static const int QM_DEPS = 2;
285 static const int QM_REFS = 3;
286 static const int QM_SYMBOLS = 4;
287 static const int QM_INFO = 5;
289 /*======================================================================*/
290 /* The system calls unchanged between 2.0 and 2.1. */
292 unsigned long create_module(const char *, size_t);
293 int delete_module(const char *);
296 #endif /* module.h */
298 //----------------------------------------------------------------------------
299 //--------end of modutils module.h
300 //----------------------------------------------------------------------------
304 //----------------------------------------------------------------------------
305 //--------modutils obj.h, lines 253-462
306 //----------------------------------------------------------------------------
308 /* Elf object file loading and relocation routines.
309 Copyright 1996, 1997 Linux International.
311 Contributed by Richard Henderson <rth@tamu.edu>
313 This file is part of the Linux modutils.
315 This program is free software; you can redistribute it and/or modify it
316 under the terms of the GNU General Public License as published by the
317 Free Software Foundation; either version 2 of the License, or (at your
318 option) any later version.
320 This program is distributed in the hope that it will be useful, but
321 WITHOUT ANY WARRANTY; without even the implied warranty of
322 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
323 General Public License for more details.
325 You should have received a copy of the GNU General Public License
326 along with this program; if not, write to the Free Software Foundation,
327 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
330 #ifndef MODUTILS_OBJ_H
331 static const int MODUTILS_OBJ_H = 1;
333 #ident "$Id: insmod.c,v 1.59 2001/04/25 17:22:32 andersen Exp $"
335 /* The relocatable object is manipulated using elfin types. */
341 /* Machine-specific elf macros for i386 et al. */
343 /* the SH changes have only been tested on the SH4 in =little endian= mode */
344 /* I'm not sure about big endian, so let's warn: */
346 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
347 #error insmod.c may require changes for use on big endian SH4/SH3
350 /* it may or may not work on the SH1/SH2... So let's error on those
352 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
353 #error insmod.c may require changes for non-SH3/SH4 use
356 #define ELFCLASSM ELFCLASS32
360 #define MATCH_MACHINE(x) (x == EM_SH)
361 #define SHT_RELM SHT_RELA
362 #define Elf32_RelM Elf32_Rela
363 #define ELFDATAM ELFDATA2LSB
365 #elif defined(__arm__)
367 #define MATCH_MACHINE(x) (x == EM_ARM)
368 #define SHT_RELM SHT_REL
369 #define Elf32_RelM Elf32_Rel
370 #define ELFDATAM ELFDATA2LSB
372 #elif defined(__powerpc__)
374 #define MATCH_MACHINE(x) (x == EM_PPC)
375 #define SHT_RELM SHT_RELA
376 #define Elf32_RelM Elf32_Rela
377 #define ELFDATAM ELFDATA2MSB
379 #elif defined(__mips__)
381 /* Account for ELF spec changes. */
382 #ifndef EM_MIPS_RS3_LE
383 #ifdef EM_MIPS_RS4_BE
384 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
386 #define EM_MIPS_RS3_LE 10
388 #endif /* !EM_MIPS_RS3_LE */
390 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
391 #define SHT_RELM SHT_REL
392 #define Elf32_RelM Elf32_Rel
394 #define ELFDATAM ELFDATA2MSB
397 #define ELFDATAM ELFDATA2LSB
400 #elif defined(__i386__)
402 /* presumably we can use these for anything but the SH and ARM*/
403 /* this is the previous behavior, but it does result in
404 insmod.c being broken on anything except i386 */
406 #define MATCH_MACHINE(x) (x == EM_386)
408 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
411 #define SHT_RELM SHT_REL
412 #define Elf32_RelM Elf32_Rel
413 #define ELFDATAM ELFDATA2LSB
416 #error Sorry, but insmod.c does not yet support this architecture...
420 # if ELFCLASSM == ELFCLASS32
421 # define ElfW(x) Elf32_ ## x
422 # define ELFW(x) ELF32_ ## x
424 # define ElfW(x) Elf64_ ## x
425 # define ELFW(x) ELF64_ ## x
429 /* For some reason this is missing from libc5. */
430 #ifndef ELF32_ST_INFO
431 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
434 #ifndef ELF64_ST_INFO
435 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
438 struct obj_string_patch;
439 struct obj_symbol_patch;
446 struct obj_section *load_next;
452 struct obj_symbol *next; /* hash table link */
456 int secidx; /* the defining section index/module */
458 int ksymidx; /* for export to the kernel symtab */
459 int referenced; /* actually used in the link */
462 /* Hardcode the hash table size. We shouldn't be needing so many
463 symbols that we begin to degrade performance, and we get a big win
464 by giving the compiler a constant divisor. */
466 #define HASH_BUCKETS 521
472 struct obj_section **sections;
473 struct obj_section *load_order;
474 struct obj_section **load_order_search_start;
475 struct obj_string_patch *string_patches;
476 struct obj_symbol_patch *symbol_patches;
477 int (*symbol_cmp)(const char *, const char *);
478 unsigned long (*symbol_hash)(const char *);
479 unsigned long local_symtab_size;
480 struct obj_symbol **local_symtab;
481 struct obj_symbol *symtab[HASH_BUCKETS];
492 struct obj_string_patch
494 struct obj_string_patch *next;
496 ElfW(Addr) reloc_offset;
497 ElfW(Addr) string_offset;
500 struct obj_symbol_patch
502 struct obj_symbol_patch *next;
504 ElfW(Addr) reloc_offset;
505 struct obj_symbol *sym;
509 /* Generic object manipulation routines. */
511 unsigned long obj_elf_hash(const char *);
513 unsigned long obj_elf_hash_n(const char *, unsigned long len);
515 struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
516 unsigned long symidx, int info, int secidx,
517 ElfW(Addr) value, unsigned long size);
519 struct obj_symbol *obj_find_symbol (struct obj_file *f,
522 ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
523 struct obj_symbol *sym);
525 void obj_set_symbol_compare(struct obj_file *f,
526 int (*cmp)(const char *, const char *),
527 unsigned long (*hash)(const char *));
529 struct obj_section *obj_find_section (struct obj_file *f,
532 void obj_insert_section_load_order (struct obj_file *f,
533 struct obj_section *sec);
535 struct obj_section *obj_create_alloced_section (struct obj_file *f,
540 struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
545 void *obj_extend_section (struct obj_section *sec, unsigned long more);
547 int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
550 int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
551 struct obj_symbol *sym);
553 int obj_check_undefineds(struct obj_file *f);
555 void obj_allocate_commons(struct obj_file *f);
557 unsigned long obj_load_size (struct obj_file *f);
559 int obj_relocate (struct obj_file *f, ElfW(Addr) base);
561 struct obj_file *obj_load(FILE *f);
563 int obj_create_image (struct obj_file *f, char *image);
565 /* Architecture specific manipulation routines. */
567 struct obj_file *arch_new_file (void);
569 struct obj_section *arch_new_section (void);
571 struct obj_symbol *arch_new_symbol (void);
573 enum obj_reloc arch_apply_relocation (struct obj_file *f,
574 struct obj_section *targsec,
575 struct obj_section *symsec,
576 struct obj_symbol *sym,
577 ElfW(RelM) *rel, ElfW(Addr) value);
579 int arch_create_got (struct obj_file *f);
582 int arch_init_module (struct obj_file *f, struct new_module *);
585 //----------------------------------------------------------------------------
586 //--------end of modutils obj.h
587 //----------------------------------------------------------------------------
593 #define _PATH_MODULES "/lib/modules"
594 static const int STRVERSIONLEN = 32;
596 /*======================================================================*/
598 int flag_force_load = 0;
599 int flag_autoclean = 0;
600 int flag_verbose = 0;
604 /*======================================================================*/
606 /* previously, these were named i386_* but since we could be
607 compiling for the sh, I've renamed them to the more general
608 arch_* These structures are the same between the x86 and SH,
609 and we can't support anything else right now anyway. In the
610 future maybe they should be #if defined'd */
616 #if defined(BB_USE_PLT_ENTRIES)
617 struct arch_plt_entry
621 int inited:1; /* has been set up */
625 #if defined(BB_USE_GOT_ENTRIES)
626 struct arch_got_entry {
628 unsigned offset_done:1;
629 unsigned reloc_done:1;
633 #if defined(__mips__)
636 struct mips_hi16 *next;
643 struct obj_file root;
644 #if defined(BB_USE_PLT_ENTRIES)
645 struct obj_section *plt;
647 #if defined(BB_USE_GOT_ENTRIES)
648 struct obj_section *got;
650 #if defined(__mips__)
651 struct mips_hi16 *mips_hi16_list;
656 struct obj_symbol root;
657 #if defined(BB_USE_PLT_ENTRIES)
658 struct arch_plt_entry pltent;
660 #if defined(BB_USE_GOT_ENTRIES)
661 struct arch_got_entry gotent;
666 struct external_module {
671 struct new_module_symbol *syms;
674 struct new_module_symbol *ksyms;
677 struct external_module *ext_modules;
679 int n_ext_modules_used;
682 extern int delete_module(const char *);
685 /* This is kind of troublesome. See, we don't actually support
686 the m68k or the arm the same way we support i386 and (now)
687 sh. In doing my SH patch, I just assumed that whatever works
688 for i386 also works for m68k and arm since currently insmod.c
689 does nothing special for them. If this isn't true, the below
690 line is rather misleading IMHO, and someone should either
691 change it or add more proper architecture-dependent support
694 -- Bryan Rittmeyer <bryan@ixiacom.com> */
696 static char m_filename[BUFSIZ + 1];
697 static char m_fullName[BUFSIZ + 1];
699 /*======================================================================*/
702 static int findNamedModule(const char *fileName, struct stat *statbuf,
705 char *fullName = (char *) userDate;
708 if (fullName[0] == '\0')
711 char *tmp = strrchr((char *) fileName, '/');
714 tmp = (char *) fileName;
717 if (check_wildcard_match(tmp, fullName) == TRUE) {
718 /* Stop searching if we find a match */
719 safe_strncpy(m_filename, fileName, sizeof(m_filename));
727 /*======================================================================*/
729 struct obj_file *arch_new_file(void)
732 f = xmalloc(sizeof(*f));
734 #if defined(BB_USE_PLT_ENTRIES)
737 #if defined(BB_USE_GOT_ENTRIES)
740 #if defined(__mips__)
741 f->mips_hi16_list = NULL;
747 struct obj_section *arch_new_section(void)
749 return xmalloc(sizeof(struct obj_section));
752 struct obj_symbol *arch_new_symbol(void)
754 struct arch_symbol *sym;
755 sym = xmalloc(sizeof(*sym));
757 #if defined(BB_USE_PLT_ENTRIES)
758 memset(&sym->pltent, 0, sizeof(sym->pltent));
760 #if defined(BB_USE_GOT_ENTRIES)
761 memset(&sym->gotent, 0, sizeof(sym->gotent));
768 arch_apply_relocation(struct obj_file *f,
769 struct obj_section *targsec,
770 struct obj_section *symsec,
771 struct obj_symbol *sym,
772 ElfW(RelM) *rel, ElfW(Addr) v)
774 struct arch_file *ifile = (struct arch_file *) f;
775 #if !(defined(__mips__))
776 struct arch_symbol *isym = (struct arch_symbol *) sym;
779 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
780 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
781 #if defined(BB_USE_GOT_ENTRIES)
782 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
784 #if defined(BB_USE_PLT_ENTRIES)
785 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
786 struct arch_plt_entry *pe;
789 enum obj_reloc ret = obj_reloc_ok;
791 switch (ELF32_R_TYPE(rel->r_info)) {
793 /* even though these constants seem to be the same for
794 the i386 and the sh, we "#if define" them for clarity
795 and in case that ever changes */
798 #elif defined(__arm__)
800 #elif defined(__i386__)
802 #elif defined(__powerpc__)
804 #elif defined(__mips__)
811 #elif defined(__arm__)
813 #elif defined(__i386__)
815 #elif defined(__powerpc__)
817 #elif defined(__mips__)
823 #if defined(__powerpc__)
824 case R_PPC_ADDR16_HA:
825 *(unsigned short *)loc = (v + 0x8000) >> 16;
828 case R_PPC_ADDR16_HI:
829 *(unsigned short *)loc = v >> 16;
832 case R_PPC_ADDR16_LO:
833 *(unsigned short *)loc = v;
837 #if defined(__mips__)
840 ret = obj_reloc_dangerous;
841 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
842 ret = obj_reloc_overflow;
844 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
852 /* We cannot relocate this one now because we don't know the value
853 of the carry we need to add. Save the information, and let LO16
854 do the actual relocation. */
855 n = (struct mips_hi16 *) xmalloc(sizeof *n);
858 n->next = ifile->mips_hi16_list;
859 ifile->mips_hi16_list = n;
865 unsigned long insnlo = *loc;
866 Elf32_Addr val, vallo;
868 /* Sign extend the addend we extract from the lo insn. */
869 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
871 if (ifile->mips_hi16_list != NULL) {
874 l = ifile->mips_hi16_list;
876 struct mips_hi16 *next;
879 /* The value for the HI16 had best be the same. */
880 assert(v == l->value);
882 /* Do the HI16 relocation. Note that we actually don't
883 need to know anything about the LO16 itself, except where
884 to find the low 16 bits of the addend needed by the LO16. */
887 ((insn & 0xffff) << 16) +
891 /* Account for the sign extension that will happen in the
898 insn = (insn & ~0xffff) | val;
906 ifile->mips_hi16_list = NULL;
909 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
911 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
918 #elif defined(__sh__)
922 #elif defined(__i386__)
927 #elif defined(__powerpc__)
937 #elif defined(__i386__)
940 #if defined(BB_USE_PLT_ENTRIES)
946 #if defined(__powerpc__)
949 /* find the plt entry and initialize it if necessary */
950 assert(isym != NULL);
952 pe = (struct arch_plt_entry*) &isym->pltent;
955 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
957 /* generate some machine code */
960 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
961 ip[1] = v; /* sym@ */
963 #if defined(__powerpc__)
964 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
965 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
966 ip[2] = 0x7d6903a6; /* mtctr r11 */
967 ip[3] = 0x4e800420; /* bctr */
972 /* relative distance to target */
974 /* if the target is too far away.... */
975 if ((int)v < -0x02000000 || (int)v >= 0x02000000) {
977 v = plt + pe->offset - dot;
980 ret = obj_reloc_dangerous;
982 /* merge the offset into the instruction. */
984 /* Convert to words. */
987 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
989 #if defined(__powerpc__)
990 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
993 #endif /* BB_USE_PLT_ENTRIES */
996 #elif defined(__sh__)
1001 #elif defined(__i386__)
1002 case R_386_GLOB_DAT:
1003 case R_386_JMP_SLOT:
1008 #if defined(__arm__)
1009 #elif defined(__sh__)
1011 *loc += f->baseaddr + rel->r_addend;
1013 #elif defined(__i386__)
1014 case R_386_RELATIVE:
1015 *loc += f->baseaddr;
1019 #if defined(BB_USE_GOT_ENTRIES)
1023 #elif defined(__arm__)
1025 #elif defined(__i386__)
1030 *loc += got - dot + rel->r_addend;;
1031 #elif defined(__i386__) || defined(__arm__)
1038 #elif defined(__arm__)
1040 #elif defined(__i386__)
1043 assert(isym != NULL);
1044 /* needs an entry in the .got: set it, once */
1045 if (!isym->gotent.reloc_done) {
1046 isym->gotent.reloc_done = 1;
1047 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1049 /* make the reloc with_respect_to_.got */
1051 *loc += isym->gotent.offset + rel->r_addend;
1052 #elif defined(__i386__) || defined(__arm__)
1053 *loc += isym->gotent.offset;
1057 /* address relative to the got */
1060 #elif defined(__arm__)
1062 #elif defined(__i386__)
1069 #endif /* BB_USE_GOT_ENTRIES */
1072 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1073 ret = obj_reloc_unhandled;
1080 int arch_create_got(struct obj_file *f)
1082 #if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
1083 struct arch_file *ifile = (struct arch_file *) f;
1085 #if defined(BB_USE_GOT_ENTRIES)
1086 int got_offset = 0, gotneeded = 0;
1088 #if defined(BB_USE_PLT_ENTRIES)
1089 int plt_offset = 0, pltneeded = 0;
1091 struct obj_section *relsec, *symsec, *strsec;
1092 ElfW(RelM) *rel, *relend;
1093 ElfW(Sym) *symtab, *extsym;
1094 const char *strtab, *name;
1095 struct arch_symbol *intsym;
1097 for (i = 0; i < f->header.e_shnum; ++i) {
1098 relsec = f->sections[i];
1099 if (relsec->header.sh_type != SHT_RELM)
1102 symsec = f->sections[relsec->header.sh_link];
1103 strsec = f->sections[symsec->header.sh_link];
1105 rel = (ElfW(RelM) *) relsec->contents;
1106 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1107 symtab = (ElfW(Sym) *) symsec->contents;
1108 strtab = (const char *) strsec->contents;
1110 for (; rel < relend; ++rel) {
1111 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1113 switch (ELF32_R_TYPE(rel->r_info)) {
1114 #if defined(__arm__)
1117 #elif defined(__sh__)
1120 #elif defined(__i386__)
1125 #if defined(__powerpc__)
1131 #if defined(__arm__)
1140 if (got_offset == 0)
1142 #elif defined(__sh__)
1146 #elif defined(__i386__)
1156 if (extsym->st_name != 0) {
1157 name = strtab + extsym->st_name;
1159 name = f->sections[extsym->st_shndx]->name;
1161 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1162 #if defined(BB_USE_GOT_ENTRIES)
1163 if (!intsym->gotent.offset_done) {
1164 intsym->gotent.offset_done = 1;
1165 intsym->gotent.offset = got_offset;
1166 got_offset += BB_GOT_ENTRY_SIZE;
1169 #if defined(BB_USE_PLT_ENTRIES)
1170 if (pltneeded && intsym->pltent.allocated == 0) {
1171 intsym->pltent.allocated = 1;
1172 intsym->pltent.offset = plt_offset;
1173 plt_offset += BB_PLT_ENTRY_SIZE;
1174 intsym->pltent.inited = 0;
1181 #if defined(BB_USE_GOT_ENTRIES)
1183 struct obj_section* myrelsec = obj_find_section(f, ".got");
1186 obj_extend_section(myrelsec, got_offset);
1188 myrelsec = obj_create_alloced_section(f, ".got",
1194 ifile->got = myrelsec;
1198 #if defined(BB_USE_PLT_ENTRIES)
1200 ifile->plt = obj_create_alloced_section(f, ".plt",
1208 int arch_init_module(struct obj_file *f, struct new_module *mod)
1214 /*======================================================================*/
1216 /* Standard ELF hash function. */
1217 inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1219 unsigned long h = 0;
1226 if ((g = (h & 0xf0000000)) != 0) {
1235 unsigned long obj_elf_hash(const char *name)
1237 return obj_elf_hash_n(name, strlen(name));
1240 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1241 /* Get the kernel version in the canonical integer form. */
1243 static int get_kernel_version(char str[STRVERSIONLEN])
1245 struct utsname uts_info;
1249 if (uname(&uts_info) < 0)
1251 strncpy(str, uts_info.release, STRVERSIONLEN);
1252 p = uts_info.release;
1254 a = strtoul(p, &p, 10);
1257 b = strtoul(p + 1, &p, 10);
1260 c = strtoul(p + 1, &q, 10);
1264 return a << 16 | b << 8 | c;
1267 /* String comparison for non-co-versioned kernel and module. */
1269 static int ncv_strcmp(const char *a, const char *b)
1271 size_t alen = strlen(a), blen = strlen(b);
1273 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1274 return strncmp(a, b, alen);
1275 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1276 return strncmp(a, b, blen);
1278 return strcmp(a, b);
1281 /* String hashing for non-co-versioned kernel and module. Here
1282 we are simply forced to drop the crc from the hash. */
1284 static unsigned long ncv_symbol_hash(const char *str)
1286 size_t len = strlen(str);
1287 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1289 return obj_elf_hash_n(str, len);
1293 obj_set_symbol_compare(struct obj_file *f,
1294 int (*cmp) (const char *, const char *),
1295 unsigned long (*hash) (const char *))
1298 f->symbol_cmp = cmp;
1300 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1303 f->symbol_hash = hash;
1305 memcpy(tmptab, f->symtab, sizeof(tmptab));
1306 memset(f->symtab, 0, sizeof(f->symtab));
1308 for (i = 0; i < HASH_BUCKETS; ++i)
1309 for (sym = tmptab[i]; sym; sym = next) {
1310 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1312 sym->next = f->symtab[h];
1318 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1321 struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
1322 unsigned long symidx, int info,
1323 int secidx, ElfW(Addr) value,
1326 struct obj_symbol *sym;
1327 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1328 int n_type = ELFW(ST_TYPE) (info);
1329 int n_binding = ELFW(ST_BIND) (info);
1331 for (sym = f->symtab[hash]; sym; sym = sym->next)
1332 if (f->symbol_cmp(sym->name, name) == 0) {
1333 int o_secidx = sym->secidx;
1334 int o_info = sym->info;
1335 int o_type = ELFW(ST_TYPE) (o_info);
1336 int o_binding = ELFW(ST_BIND) (o_info);
1338 /* A redefinition! Is it legal? */
1340 if (secidx == SHN_UNDEF)
1342 else if (o_secidx == SHN_UNDEF)
1344 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1345 /* Cope with local and global symbols of the same name
1346 in the same object file, as might have been created
1347 by ld -r. The only reason locals are now seen at this
1348 level at all is so that we can do semi-sensible things
1351 struct obj_symbol *nsym, **p;
1353 nsym = arch_new_symbol();
1354 nsym->next = sym->next;
1357 /* Excise the old (local) symbol from the hash chain. */
1358 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1362 } else if (n_binding == STB_LOCAL) {
1363 /* Another symbol of the same name has already been defined.
1364 Just add this to the local table. */
1365 sym = arch_new_symbol();
1368 f->local_symtab[symidx] = sym;
1370 } else if (n_binding == STB_WEAK)
1372 else if (o_binding == STB_WEAK)
1374 /* Don't unify COMMON symbols with object types the programmer
1376 else if (secidx == SHN_COMMON
1377 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1379 else if (o_secidx == SHN_COMMON
1380 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1383 /* Don't report an error if the symbol is coming from
1384 the kernel or some external module. */
1385 if (secidx <= SHN_HIRESERVE)
1386 error_msg("%s multiply defined", name);
1391 /* Completely new symbol. */
1392 sym = arch_new_symbol();
1393 sym->next = f->symtab[hash];
1394 f->symtab[hash] = sym;
1397 if (ELFW(ST_BIND) (info) == STB_LOCAL)
1398 f->local_symtab[symidx] = sym;
1404 sym->secidx = secidx;
1410 struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
1412 struct obj_symbol *sym;
1413 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1415 for (sym = f->symtab[hash]; sym; sym = sym->next)
1416 if (f->symbol_cmp(sym->name, name) == 0)
1423 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1426 if (sym->secidx >= SHN_LORESERVE)
1429 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1431 /* As a special case, a NULL sym has value zero. */
1436 struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1438 int i, n = f->header.e_shnum;
1440 for (i = 0; i < n; ++i)
1441 if (strcmp(f->sections[i]->name, name) == 0)
1442 return f->sections[i];
1447 static int obj_load_order_prio(struct obj_section *a)
1449 unsigned long af, ac;
1451 af = a->header.sh_flags;
1454 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1455 strcmp(a->name + 5, ".init"))
1459 if (!(af & SHF_WRITE))
1461 if (af & SHF_EXECINSTR)
1463 if (a->header.sh_type != SHT_NOBITS)
1470 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1472 struct obj_section **p;
1473 int prio = obj_load_order_prio(sec);
1474 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1475 if (obj_load_order_prio(*p) < prio)
1477 sec->load_next = *p;
1481 struct obj_section *obj_create_alloced_section(struct obj_file *f,
1483 unsigned long align,
1486 int newidx = f->header.e_shnum++;
1487 struct obj_section *sec;
1489 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1490 f->sections[newidx] = sec = arch_new_section();
1492 memset(sec, 0, sizeof(*sec));
1493 sec->header.sh_type = SHT_PROGBITS;
1494 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1495 sec->header.sh_size = size;
1496 sec->header.sh_addralign = align;
1500 sec->contents = xmalloc(size);
1502 obj_insert_section_load_order(f, sec);
1507 struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1509 unsigned long align,
1512 int newidx = f->header.e_shnum++;
1513 struct obj_section *sec;
1515 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1516 f->sections[newidx] = sec = arch_new_section();
1518 memset(sec, 0, sizeof(*sec));
1519 sec->header.sh_type = SHT_PROGBITS;
1520 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1521 sec->header.sh_size = size;
1522 sec->header.sh_addralign = align;
1526 sec->contents = xmalloc(size);
1528 sec->load_next = f->load_order;
1529 f->load_order = sec;
1530 if (f->load_order_search_start == &f->load_order)
1531 f->load_order_search_start = &sec->load_next;
1536 void *obj_extend_section(struct obj_section *sec, unsigned long more)
1538 unsigned long oldsize = sec->header.sh_size;
1539 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1540 return sec->contents + oldsize;
1545 /* Conditionally add the symbols from the given symbol set to the
1551 int idx, struct new_module_symbol *syms, size_t nsyms)
1553 struct new_module_symbol *s;
1557 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1559 /* Only add symbols that are already marked external. If we
1560 override locals we may cause problems for argument initialization.
1561 We will also create a false dependency on the module. */
1562 struct obj_symbol *sym;
1564 sym = obj_find_symbol(f, (char *) s->name);
1565 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1566 sym = obj_add_symbol(f, (char *) s->name, -1,
1567 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1569 /* Did our symbol just get installed? If so, mark the
1570 module as "used". */
1571 if (sym->secidx == idx)
1579 static void add_kernel_symbols(struct obj_file *f)
1581 struct external_module *m;
1584 /* Add module symbols first. */
1586 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1588 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1589 m->nsyms)) m->used = 1, ++nused;
1591 n_ext_modules_used = nused;
1593 /* And finally the symbols from the kernel proper. */
1596 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1599 static char *get_modinfo_value(struct obj_file *f, const char *key)
1601 struct obj_section *sec;
1602 char *p, *v, *n, *ep;
1603 size_t klen = strlen(key);
1605 sec = obj_find_section(f, ".modinfo");
1609 ep = p + sec->header.sh_size;
1612 n = strchr(p, '\0');
1614 if (p + klen == v && strncmp(p, key, klen) == 0)
1617 if (p + klen == n && strcmp(p, key) == 0)
1627 /*======================================================================*/
1628 /* Functions relating to module loading in pre 2.1 kernels. */
1631 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1635 struct obj_symbol *sym;
1639 if ((q = strchr(p, '=')) == NULL) {
1645 sym = obj_find_symbol(f, p);
1647 /* Also check that the parameter was not resolved from the kernel. */
1648 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1649 error_msg("symbol for parameter %s not found", p);
1653 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1655 /* Do C quoting if we begin with a ". */
1659 str = alloca(strlen(q));
1660 for (r = str, q++; *q != '"'; ++q, ++r) {
1662 error_msg("improperly terminated string argument for %s", p);
1664 } else if (*q == '\\')
1698 if (q[1] >= '0' && q[1] <= '7') {
1699 c = (c * 8) + *++q - '0';
1700 if (q[1] >= '0' && q[1] <= '7')
1701 c = (c * 8) + *++q - '0';
1714 obj_string_patch(f, sym->secidx, sym->value, str);
1715 } else if (*q >= '0' && *q <= '9') {
1717 *loc++ = strtoul(q, &q, 0);
1718 while (*q++ == ',');
1720 char *contents = f->sections[sym->secidx]->contents;
1721 char *myloc = contents + sym->value;
1722 char *r; /* To search for commas */
1724 /* Break the string with comas */
1725 while ((r = strchr(q, ',')) != (char *) NULL) {
1727 obj_string_patch(f, sym->secidx, myloc - contents, q);
1728 myloc += sizeof(char *);
1733 obj_string_patch(f, sym->secidx, myloc - contents, q);
1742 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1743 static int old_is_module_checksummed(struct obj_file *f)
1745 return obj_find_symbol(f, "Using_Versions") != NULL;
1747 /* Get the module's kernel version in the canonical integer form. */
1750 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1752 struct obj_symbol *sym;
1756 sym = obj_find_symbol(f, "kernel_version");
1760 p = f->sections[sym->secidx]->contents + sym->value;
1761 strncpy(str, p, STRVERSIONLEN);
1763 a = strtoul(p, &p, 10);
1766 b = strtoul(p + 1, &p, 10);
1769 c = strtoul(p + 1, &q, 10);
1773 return a << 16 | b << 8 | c;
1776 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1778 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
1780 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1782 static int old_get_kernel_symbols(const char *m_name)
1784 struct old_kernel_sym *ks, *k;
1785 struct new_module_symbol *s;
1786 struct external_module *mod;
1787 int nks, nms, nmod, i;
1789 nks = get_kernel_syms(NULL);
1792 perror_msg("get_kernel_syms: %s", m_name);
1794 error_msg("No kernel symbols");
1798 ks = k = xmalloc(nks * sizeof(*ks));
1800 if (get_kernel_syms(ks) != nks) {
1801 perror("inconsistency with get_kernel_syms -- is someone else "
1802 "playing with modules?");
1807 /* Collect the module information. */
1812 while (k->name[0] == '#' && k->name[1]) {
1813 struct old_kernel_sym *k2;
1815 /* Find out how many symbols this module has. */
1816 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1820 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1821 mod[nmod].name = k->name + 1;
1822 mod[nmod].addr = k->value;
1824 mod[nmod].nsyms = nms;
1825 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1827 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1828 s->name = (unsigned long) k->name;
1829 s->value = k->value;
1836 n_ext_modules = nmod + 1;
1838 /* Now collect the symbols for the kernel proper. */
1840 if (k->name[0] == '#')
1843 nksyms = nms = nks - (k - ks);
1844 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1846 for (i = 0; i < nms; ++i, ++s, ++k) {
1847 s->name = (unsigned long) k->name;
1848 s->value = k->value;
1854 /* Return the kernel symbol checksum version, or zero if not used. */
1856 static int old_is_kernel_checksummed(void)
1858 /* Using_Versions is the first symbol. */
1860 && strcmp((char *) ksyms[0].name,
1861 "Using_Versions") == 0) return ksyms[0].value;
1867 static int old_create_mod_use_count(struct obj_file *f)
1869 struct obj_section *sec;
1871 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1874 obj_add_symbol(f, "mod_use_count_", -1,
1875 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1882 old_init_module(const char *m_name, struct obj_file *f,
1883 unsigned long m_size)
1886 struct old_mod_routines routines;
1887 struct old_symbol_table *symtab;
1890 /* Create the symbol table */
1892 int nsyms = 0, strsize = 0, total;
1894 /* Size things first... */
1897 for (i = 0; i < HASH_BUCKETS; ++i) {
1898 struct obj_symbol *sym;
1899 for (sym = f->symtab[i]; sym; sym = sym->next)
1900 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1901 && sym->secidx <= SHN_HIRESERVE)
1903 sym->ksymidx = nsyms++;
1904 strsize += strlen(sym->name) + 1;
1909 total = (sizeof(struct old_symbol_table)
1910 + nsyms * sizeof(struct old_module_symbol)
1911 + n_ext_modules_used * sizeof(struct old_module_ref)
1913 symtab = xmalloc(total);
1914 symtab->size = total;
1915 symtab->n_symbols = nsyms;
1916 symtab->n_refs = n_ext_modules_used;
1918 if (flag_export && nsyms) {
1919 struct old_module_symbol *ksym;
1923 ksym = symtab->symbol;
1924 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1925 + n_ext_modules_used * sizeof(struct old_module_ref));
1927 for (i = 0; i < HASH_BUCKETS; ++i) {
1928 struct obj_symbol *sym;
1929 for (sym = f->symtab[i]; sym; sym = sym->next)
1930 if (sym->ksymidx >= 0) {
1931 ksym->addr = obj_symbol_final_value(f, sym);
1933 (unsigned long) str - (unsigned long) symtab;
1935 strcpy(str, sym->name);
1936 str += strlen(sym->name) + 1;
1942 if (n_ext_modules_used) {
1943 struct old_module_ref *ref;
1946 ref = (struct old_module_ref *)
1947 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
1949 for (i = 0; i < n_ext_modules; ++i)
1950 if (ext_modules[i].used)
1951 ref++->module = ext_modules[i].addr;
1955 /* Fill in routines. */
1958 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
1960 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
1962 /* Whew! All of the initialization is complete. Collect the final
1963 module image and give it to the kernel. */
1965 image = xmalloc(m_size);
1966 obj_create_image(f, image);
1968 /* image holds the complete relocated module, accounting correctly for
1969 mod_use_count. However the old module kernel support assume that
1970 it is receiving something which does not contain mod_use_count. */
1971 ret = old_sys_init_module(m_name, image + sizeof(long),
1972 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
1973 : 0), &routines, symtab);
1975 perror_msg("init_module: %s", m_name);
1985 #define old_create_mod_use_count(x) TRUE
1986 #define old_init_module(x, y, z) TRUE
1988 #endif /* BB_FEATURE_OLD_MODULE_INTERFACE */
1992 /*======================================================================*/
1993 /* Functions relating to module loading after 2.1.18. */
1996 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2000 struct obj_symbol *sym;
2001 char *contents, *loc;
2005 if ((q = strchr(p, '=')) == NULL) {
2010 key = alloca(q - p + 6);
2011 memcpy(key, "parm_", 5);
2012 memcpy(key + 5, p, q - p);
2015 p = get_modinfo_value(f, key);
2018 error_msg("invalid parameter %s", key);
2022 sym = obj_find_symbol(f, key);
2024 /* Also check that the parameter was not resolved from the kernel. */
2025 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2026 error_msg("symbol for parameter %s not found", key);
2031 min = strtoul(p, &p, 10);
2033 max = strtoul(p + 1, &p, 10);
2039 contents = f->sections[sym->secidx]->contents;
2040 loc = contents + sym->value;
2044 if ((*p == 's') || (*p == 'c')) {
2047 /* Do C quoting if we begin with a ", else slurp the lot. */
2051 str = alloca(strlen(q));
2052 for (r = str, q++; *q != '"'; ++q, ++r) {
2054 error_msg("improperly terminated string argument for %s",
2057 } else if (*q == '\\')
2091 if (q[1] >= '0' && q[1] <= '7') {
2092 c = (c * 8) + *++q - '0';
2093 if (q[1] >= '0' && q[1] <= '7')
2094 c = (c * 8) + *++q - '0';
2111 /* In this case, the string is not quoted. We will break
2112 it using the coma (like for ints). If the user wants to
2113 include comas in a string, he just has to quote it */
2115 /* Search the next coma */
2119 if (r != (char *) NULL) {
2120 /* Recopy the current field */
2121 str = alloca(r - q + 1);
2122 memcpy(str, q, r - q);
2124 /* I don't know if it is usefull, as the previous case
2125 doesn't null terminate the string ??? */
2128 /* Keep next fields */
2139 obj_string_patch(f, sym->secidx, loc - contents, str);
2140 loc += tgt_sizeof_char_p;
2142 /* Array of chars (in fact, matrix !) */
2143 unsigned long charssize; /* size of each member */
2145 /* Get the size of each member */
2146 /* Probably we should do that outside the loop ? */
2147 if (!isdigit(*(p + 1))) {
2148 error_msg("parameter type 'c' for %s must be followed by"
2149 " the maximum size", key);
2152 charssize = strtoul(p + 1, (char **) NULL, 10);
2155 if (strlen(str) >= charssize) {
2156 error_msg("string too long for %s (max %ld)", key,
2161 /* Copy to location */
2162 strcpy((char *) loc, str);
2166 long v = strtoul(q, &q, 0);
2173 loc += tgt_sizeof_short;
2177 loc += tgt_sizeof_int;
2181 loc += tgt_sizeof_long;
2185 error_msg("unknown parameter type '%c' for %s", *p, key);
2200 goto retry_end_of_value;
2204 error_msg("too many values for %s (max %d)", key, max);
2211 error_msg("invalid argument syntax for %s", key);
2218 error_msg("too few values for %s (min %d)", key, min);
2228 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2229 static int new_is_module_checksummed(struct obj_file *f)
2231 const char *p = get_modinfo_value(f, "using_checksums");
2238 /* Get the module's kernel version in the canonical integer form. */
2241 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2246 p = get_modinfo_value(f, "kernel_version");
2249 strncpy(str, p, STRVERSIONLEN);
2251 a = strtoul(p, &p, 10);
2254 b = strtoul(p + 1, &p, 10);
2257 c = strtoul(p + 1, &q, 10);
2261 return a << 16 | b << 8 | c;
2264 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2267 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
2269 /* Fetch the loaded modules, and all currently exported symbols. */
2271 static int new_get_kernel_symbols(void)
2273 char *module_names, *mn;
2274 struct external_module *modules, *m;
2275 struct new_module_symbol *syms, *s;
2276 size_t ret, bufsize, nmod, nsyms, i, j;
2278 /* Collect the loaded modules. */
2280 module_names = xmalloc(bufsize = 256);
2282 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2283 if (errno == ENOSPC && bufsize < ret) {
2284 module_names = xrealloc(module_names, bufsize = ret);
2285 goto retry_modules_load;
2287 perror_msg("QM_MODULES");
2291 n_ext_modules = nmod = ret;
2293 /* Collect the modules' symbols. */
2296 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2297 memset(modules, 0, nmod * sizeof(*modules));
2298 for (i = 0, mn = module_names, m = modules;
2299 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2300 struct new_module_info info;
2302 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2303 if (errno == ENOENT) {
2304 /* The module was removed out from underneath us. */
2307 perror_msg("query_module: QM_INFO: %s", mn);
2311 syms = xmalloc(bufsize = 1024);
2313 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2316 syms = xrealloc(syms, bufsize = ret);
2317 goto retry_mod_sym_load;
2319 /* The module was removed out from underneath us. */
2322 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2329 m->addr = info.addr;
2333 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2334 s->name += (unsigned long) syms;
2339 /* Collect the kernel's symbols. */
2341 syms = xmalloc(bufsize = 16 * 1024);
2342 retry_kern_sym_load:
2343 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2344 if (errno == ENOSPC && bufsize < ret) {
2345 syms = xrealloc(syms, bufsize = ret);
2346 goto retry_kern_sym_load;
2348 perror_msg("kernel: QM_SYMBOLS");
2351 nksyms = nsyms = ret;
2354 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2355 s->name += (unsigned long) syms;
2361 /* Return the kernel symbol checksum version, or zero if not used. */
2363 static int new_is_kernel_checksummed(void)
2365 struct new_module_symbol *s;
2368 /* Using_Versions is not the first symbol, but it should be in there. */
2370 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2371 if (strcmp((char *) s->name, "Using_Versions") == 0)
2378 static int new_create_this_module(struct obj_file *f, const char *m_name)
2380 struct obj_section *sec;
2382 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2383 sizeof(struct new_module));
2384 memset(sec->contents, 0, sizeof(struct new_module));
2386 obj_add_symbol(f, "__this_module", -1,
2387 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2388 sizeof(struct new_module));
2390 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2397 static int new_create_module_ksymtab(struct obj_file *f)
2399 struct obj_section *sec;
2402 /* We must always add the module references. */
2404 if (n_ext_modules_used) {
2405 struct new_module_ref *dep;
2406 struct obj_symbol *tm;
2408 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2409 (sizeof(struct new_module_ref)
2410 * n_ext_modules_used));
2414 tm = obj_find_symbol(f, "__this_module");
2415 dep = (struct new_module_ref *) sec->contents;
2416 for (i = 0; i < n_ext_modules; ++i)
2417 if (ext_modules[i].used) {
2418 dep->dep = ext_modules[i].addr;
2419 obj_symbol_patch(f, sec->idx,
2420 (char *) &dep->ref - sec->contents, tm);
2426 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2431 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2434 /* We don't want to export symbols residing in sections that
2435 aren't loaded. There are a number of these created so that
2436 we make sure certain module options don't appear twice. */
2438 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2440 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2442 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2443 struct obj_symbol *sym;
2444 for (sym = f->symtab[i]; sym; sym = sym->next)
2445 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2446 && sym->secidx <= SHN_HIRESERVE
2447 && (sym->secidx >= SHN_LORESERVE
2448 || loaded[sym->secidx])) {
2449 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2451 obj_symbol_patch(f, sec->idx, ofs, sym);
2452 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2459 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2467 new_init_module(const char *m_name, struct obj_file *f,
2468 unsigned long m_size)
2470 struct new_module *module;
2471 struct obj_section *sec;
2476 sec = obj_find_section(f, ".this");
2477 module = (struct new_module *) sec->contents;
2478 m_addr = sec->header.sh_addr;
2480 module->size_of_struct = sizeof(*module);
2481 module->size = m_size;
2482 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2484 sec = obj_find_section(f, "__ksymtab");
2485 if (sec && sec->header.sh_size) {
2486 module->syms = sec->header.sh_addr;
2487 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2490 if (n_ext_modules_used) {
2491 sec = obj_find_section(f, ".kmodtab");
2492 module->deps = sec->header.sh_addr;
2493 module->ndeps = n_ext_modules_used;
2497 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2499 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2501 sec = obj_find_section(f, "__ex_table");
2503 module->ex_table_start = sec->header.sh_addr;
2504 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2507 sec = obj_find_section(f, ".text.init");
2509 module->runsize = sec->header.sh_addr - m_addr;
2511 sec = obj_find_section(f, ".data.init");
2513 if (!module->runsize ||
2514 module->runsize > sec->header.sh_addr - m_addr)
2515 module->runsize = sec->header.sh_addr - m_addr;
2518 if (!arch_init_module(f, module))
2521 /* Whew! All of the initialization is complete. Collect the final
2522 module image and give it to the kernel. */
2524 image = xmalloc(m_size);
2525 obj_create_image(f, image);
2527 ret = new_sys_init_module(m_name, (struct new_module *) image);
2529 perror_msg("init_module: %s", m_name);
2538 #define new_init_module(x, y, z) TRUE
2539 #define new_create_this_module(x, y) 0
2540 #define new_create_module_ksymtab(x)
2541 #define query_module(v, w, x, y, z) -1
2543 #endif /* BB_FEATURE_NEW_MODULE_INTERFACE */
2546 /*======================================================================*/
2549 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2552 struct obj_string_patch *p;
2553 struct obj_section *strsec;
2554 size_t len = strlen(string) + 1;
2557 p = xmalloc(sizeof(*p));
2558 p->next = f->string_patches;
2559 p->reloc_secidx = secidx;
2560 p->reloc_offset = offset;
2561 f->string_patches = p;
2563 strsec = obj_find_section(f, ".kstrtab");
2564 if (strsec == NULL) {
2565 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2566 p->string_offset = 0;
2567 loc = strsec->contents;
2569 p->string_offset = strsec->header.sh_size;
2570 loc = obj_extend_section(strsec, len);
2572 memcpy(loc, string, len);
2578 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2579 struct obj_symbol *sym)
2581 struct obj_symbol_patch *p;
2583 p = xmalloc(sizeof(*p));
2584 p->next = f->symbol_patches;
2585 p->reloc_secidx = secidx;
2586 p->reloc_offset = offset;
2588 f->symbol_patches = p;
2593 int obj_check_undefineds(struct obj_file *f)
2598 for (i = 0; i < HASH_BUCKETS; ++i) {
2599 struct obj_symbol *sym;
2600 for (sym = f->symtab[i]; sym; sym = sym->next)
2601 if (sym->secidx == SHN_UNDEF) {
2602 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2603 sym->secidx = SHN_ABS;
2606 error_msg("unresolved symbol %s", sym->name);
2615 void obj_allocate_commons(struct obj_file *f)
2617 struct common_entry {
2618 struct common_entry *next;
2619 struct obj_symbol *sym;
2620 } *common_head = NULL;
2624 for (i = 0; i < HASH_BUCKETS; ++i) {
2625 struct obj_symbol *sym;
2626 for (sym = f->symtab[i]; sym; sym = sym->next)
2627 if (sym->secidx == SHN_COMMON) {
2628 /* Collect all COMMON symbols and sort them by size so as to
2629 minimize space wasted by alignment requirements. */
2631 struct common_entry **p, *n;
2632 for (p = &common_head; *p; p = &(*p)->next)
2633 if (sym->size <= (*p)->sym->size)
2636 n = alloca(sizeof(*n));
2644 for (i = 1; i < f->local_symtab_size; ++i) {
2645 struct obj_symbol *sym = f->local_symtab[i];
2646 if (sym && sym->secidx == SHN_COMMON) {
2647 struct common_entry **p, *n;
2648 for (p = &common_head; *p; p = &(*p)->next)
2649 if (sym == (*p)->sym)
2651 else if (sym->size < (*p)->sym->size) {
2652 n = alloca(sizeof(*n));
2662 /* Find the bss section. */
2663 for (i = 0; i < f->header.e_shnum; ++i)
2664 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2667 /* If for some reason there hadn't been one, create one. */
2668 if (i == f->header.e_shnum) {
2669 struct obj_section *sec;
2671 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2672 f->sections[i] = sec = arch_new_section();
2673 f->header.e_shnum = i + 1;
2675 memset(sec, 0, sizeof(*sec));
2676 sec->header.sh_type = SHT_PROGBITS;
2677 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2682 /* Allocate the COMMONS. */
2684 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2685 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2686 struct common_entry *c;
2688 for (c = common_head; c; c = c->next) {
2689 ElfW(Addr) align = c->sym->value;
2691 if (align > max_align)
2693 if (bss_size & (align - 1))
2694 bss_size = (bss_size | (align - 1)) + 1;
2697 c->sym->value = bss_size;
2699 bss_size += c->sym->size;
2702 f->sections[i]->header.sh_size = bss_size;
2703 f->sections[i]->header.sh_addralign = max_align;
2707 /* For the sake of patch relocation and parameter initialization,
2708 allocate zeroed data for NOBITS sections now. Note that after
2709 this we cannot assume NOBITS are really empty. */
2710 for (i = 0; i < f->header.e_shnum; ++i) {
2711 struct obj_section *s = f->sections[i];
2712 if (s->header.sh_type == SHT_NOBITS) {
2713 if (s->header.sh_size != 0)
2714 s->contents = memset(xmalloc(s->header.sh_size),
2715 0, s->header.sh_size);
2719 s->header.sh_type = SHT_PROGBITS;
2724 unsigned long obj_load_size(struct obj_file *f)
2726 unsigned long dot = 0;
2727 struct obj_section *sec;
2729 /* Finalize the positions of the sections relative to one another. */
2731 for (sec = f->load_order; sec; sec = sec->load_next) {
2734 align = sec->header.sh_addralign;
2735 if (align && (dot & (align - 1)))
2736 dot = (dot | (align - 1)) + 1;
2738 sec->header.sh_addr = dot;
2739 dot += sec->header.sh_size;
2745 int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2747 int i, n = f->header.e_shnum;
2750 /* Finalize the addresses of the sections. */
2753 for (i = 0; i < n; ++i)
2754 f->sections[i]->header.sh_addr += base;
2756 /* And iterate over all of the relocations. */
2758 for (i = 0; i < n; ++i) {
2759 struct obj_section *relsec, *symsec, *targsec, *strsec;
2760 ElfW(RelM) * rel, *relend;
2764 relsec = f->sections[i];
2765 if (relsec->header.sh_type != SHT_RELM)
2768 symsec = f->sections[relsec->header.sh_link];
2769 targsec = f->sections[relsec->header.sh_info];
2770 strsec = f->sections[symsec->header.sh_link];
2772 rel = (ElfW(RelM) *) relsec->contents;
2773 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2774 symtab = (ElfW(Sym) *) symsec->contents;
2775 strtab = (const char *) strsec->contents;
2777 for (; rel < relend; ++rel) {
2778 ElfW(Addr) value = 0;
2779 struct obj_symbol *intsym = NULL;
2780 unsigned long symndx;
2781 ElfW(Sym) * extsym = 0;
2784 /* Attempt to find a value to use for this relocation. */
2786 symndx = ELFW(R_SYM) (rel->r_info);
2788 /* Note we've already checked for undefined symbols. */
2790 extsym = &symtab[symndx];
2791 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2792 /* Local symbols we look up in the local table to be sure
2793 we get the one that is really intended. */
2794 intsym = f->local_symtab[symndx];
2796 /* Others we look up in the hash table. */
2798 if (extsym->st_name)
2799 name = strtab + extsym->st_name;
2801 name = f->sections[extsym->st_shndx]->name;
2802 intsym = obj_find_symbol(f, name);
2805 value = obj_symbol_final_value(f, intsym);
2806 intsym->referenced = 1;
2808 #if SHT_RELM == SHT_RELA
2809 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2810 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2811 if (!extsym || !extsym->st_name ||
2812 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2814 value += rel->r_addend;
2818 switch (arch_apply_relocation
2819 (f, targsec, symsec, intsym, rel, value)) {
2823 case obj_reloc_overflow:
2824 errmsg = "Relocation overflow";
2826 case obj_reloc_dangerous:
2827 errmsg = "Dangerous relocation";
2829 case obj_reloc_unhandled:
2830 errmsg = "Unhandled relocation";
2833 error_msg("%s of type %ld for %s", errmsg,
2834 (long) ELFW(R_TYPE) (rel->r_info),
2835 strtab + extsym->st_name);
2837 error_msg("%s of type %ld", errmsg,
2838 (long) ELFW(R_TYPE) (rel->r_info));
2846 /* Finally, take care of the patches. */
2848 if (f->string_patches) {
2849 struct obj_string_patch *p;
2850 struct obj_section *strsec;
2851 ElfW(Addr) strsec_base;
2852 strsec = obj_find_section(f, ".kstrtab");
2853 strsec_base = strsec->header.sh_addr;
2855 for (p = f->string_patches; p; p = p->next) {
2856 struct obj_section *targsec = f->sections[p->reloc_secidx];
2857 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2858 = strsec_base + p->string_offset;
2862 if (f->symbol_patches) {
2863 struct obj_symbol_patch *p;
2865 for (p = f->symbol_patches; p; p = p->next) {
2866 struct obj_section *targsec = f->sections[p->reloc_secidx];
2867 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2868 = obj_symbol_final_value(f, p->sym);
2875 int obj_create_image(struct obj_file *f, char *image)
2877 struct obj_section *sec;
2878 ElfW(Addr) base = f->baseaddr;
2880 for (sec = f->load_order; sec; sec = sec->load_next) {
2883 if (sec->contents == 0 || sec->header.sh_size == 0)
2886 secimg = image + (sec->header.sh_addr - base);
2888 /* Note that we allocated data for NOBITS sections earlier. */
2889 memcpy(secimg, sec->contents, sec->header.sh_size);
2895 /*======================================================================*/
2897 struct obj_file *obj_load(FILE * fp)
2900 ElfW(Shdr) * section_headers;
2904 /* Read the file header. */
2906 f = arch_new_file();
2907 memset(f, 0, sizeof(*f));
2908 f->symbol_cmp = strcmp;
2909 f->symbol_hash = obj_elf_hash;
2910 f->load_order_search_start = &f->load_order;
2912 fseek(fp, 0, SEEK_SET);
2913 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2914 perror_msg("error reading ELF header");
2918 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2919 || f->header.e_ident[EI_MAG1] != ELFMAG1
2920 || f->header.e_ident[EI_MAG2] != ELFMAG2
2921 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2922 error_msg("not an ELF file");
2925 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2926 || f->header.e_ident[EI_DATA] != ELFDATAM
2927 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2928 || !MATCH_MACHINE(f->header.e_machine)) {
2929 error_msg("ELF file not for this architecture");
2932 if (f->header.e_type != ET_REL) {
2933 error_msg("ELF file not a relocatable object");
2937 /* Read the section headers. */
2939 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
2940 error_msg("section header size mismatch: %lu != %lu",
2941 (unsigned long) f->header.e_shentsize,
2942 (unsigned long) sizeof(ElfW(Shdr)));
2946 shnum = f->header.e_shnum;
2947 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
2948 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
2950 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
2951 fseek(fp, f->header.e_shoff, SEEK_SET);
2952 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
2953 perror_msg("error reading ELF section headers");
2957 /* Read the section data. */
2959 for (i = 0; i < shnum; ++i) {
2960 struct obj_section *sec;
2962 f->sections[i] = sec = arch_new_section();
2963 memset(sec, 0, sizeof(*sec));
2965 sec->header = section_headers[i];
2968 if(sec->header.sh_size) switch (sec->header.sh_type) {
2979 if (sec->header.sh_size > 0) {
2980 sec->contents = xmalloc(sec->header.sh_size);
2981 fseek(fp, sec->header.sh_offset, SEEK_SET);
2982 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
2983 perror_msg("error reading ELF section data");
2987 sec->contents = NULL;
2991 #if SHT_RELM == SHT_REL
2993 error_msg("RELA relocations not supported on this architecture");
2997 error_msg("REL relocations not supported on this architecture");
3002 if (sec->header.sh_type >= SHT_LOPROC) {
3003 /* Assume processor specific section types are debug
3004 info and can safely be ignored. If this is ever not
3005 the case (Hello MIPS?), don't put ifdefs here but
3006 create an arch_load_proc_section(). */
3010 error_msg("can't handle sections of type %ld",
3011 (long) sec->header.sh_type);
3016 /* Do what sort of interpretation as needed by each section. */
3018 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3020 for (i = 0; i < shnum; ++i) {
3021 struct obj_section *sec = f->sections[i];
3022 sec->name = shstrtab + sec->header.sh_name;
3025 for (i = 0; i < shnum; ++i) {
3026 struct obj_section *sec = f->sections[i];
3028 /* .modinfo should be contents only but gcc has no attribute for that.
3029 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3031 if (strcmp(sec->name, ".modinfo") == 0)
3032 sec->header.sh_flags &= ~SHF_ALLOC;
3034 if (sec->header.sh_flags & SHF_ALLOC)
3035 obj_insert_section_load_order(f, sec);
3037 switch (sec->header.sh_type) {
3040 unsigned long nsym, j;
3044 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3045 error_msg("symbol size mismatch: %lu != %lu",
3046 (unsigned long) sec->header.sh_entsize,
3047 (unsigned long) sizeof(ElfW(Sym)));
3051 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3052 strtab = f->sections[sec->header.sh_link]->contents;
3053 sym = (ElfW(Sym) *) sec->contents;
3055 /* Allocate space for a table of local symbols. */
3056 j = f->local_symtab_size = sec->header.sh_info;
3057 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3059 /* Insert all symbols into the hash table. */
3060 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3063 name = strtab + sym->st_name;
3065 name = f->sections[sym->st_shndx]->name;
3067 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3068 sym->st_value, sym->st_size);
3074 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3075 error_msg("relocation entry size mismatch: %lu != %lu",
3076 (unsigned long) sec->header.sh_entsize,
3077 (unsigned long) sizeof(ElfW(RelM)));
3081 /* XXX Relocation code from modutils-2.3.19 is not here.
3082 * Why? That's about 20 lines of code from obj/obj_load.c,
3083 * which gets done in a second pass through the sections.
3084 * This BusyBox insmod does similar work in obj_relocate(). */
3091 static void hide_special_symbols(struct obj_file *f)
3093 static const char *const specials[] = {
3100 struct obj_symbol *sym;
3101 const char *const *p;
3103 for (p = specials; *p; ++p)
3104 if ((sym = obj_find_symbol(f, *p)) != NULL)
3106 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3111 extern int insmod_main( int argc, char **argv)
3118 unsigned long m_size;
3123 char m_name[BUFSIZ + 1] = "\0";
3124 int exit_status = EXIT_FAILURE;
3126 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3128 char k_strversion[STRVERSIONLEN];
3129 char m_strversion[STRVERSIONLEN];
3134 /* Parse any options */
3135 while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
3137 case 'f': /* force loading */
3138 flag_force_load = 1;
3140 case 'k': /* module loaded by kerneld, auto-cleanable */
3143 case 'v': /* verbose output */
3146 case 'x': /* do not export externs */
3149 case 'o': /* name the output module */
3150 strncpy(m_name, optarg, BUFSIZ);
3152 case 'L': /* Stub warning */
3153 /* This is needed for compatibility with modprobe.
3154 * In theory, this does locking, but we don't do
3155 * that. So be careful and plan your life around not
3156 * loading the same module 50 times concurrently. */
3163 if (argv[optind] == NULL) {
3167 /* Grab the module name */
3168 if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3175 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3177 memcpy(m_fullName, tmp, len);
3178 m_fullName[len]='\0';
3179 if (*m_name == '\0') {
3180 strcpy(m_name, m_fullName);
3182 strcat(m_fullName, ".o");
3184 /* Get a filedesc for the module */
3185 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3186 (fp = fopen(argv[optind], "r")) == NULL) {
3187 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
3188 if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE,
3189 findNamedModule, 0, m_fullName) == FALSE)
3191 if (m_filename[0] == '\0'
3192 || ((fp = fopen(m_filename, "r")) == NULL))
3194 error_msg("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
3195 return EXIT_FAILURE;
3198 error_msg_and_die("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
3200 safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
3203 if ((f = obj_load(fp)) == NULL)
3204 perror_msg_and_die("Could not load the module");
3206 if (get_modinfo_value(f, "kernel_version") == NULL)
3211 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3212 /* Version correspondence? */
3214 k_version = get_kernel_version(k_strversion);
3215 if (m_has_modinfo) {
3216 m_version = new_get_module_version(f, m_strversion);
3218 m_version = old_get_module_version(f, m_strversion);
3219 if (m_version == -1) {
3220 error_msg("couldn't find the kernel version the module was "
3226 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
3227 if (flag_force_load) {
3228 error_msg("Warning: kernel-module version mismatch\n"
3229 "\t%s was compiled for kernel version %s\n"
3230 "\twhile this kernel is version %s",
3231 m_filename, m_strversion, k_strversion);
3233 error_msg("kernel-module version mismatch\n"
3234 "\t%s was compiled for kernel version %s\n"
3235 "\twhile this kernel is version %s.",
3236 m_filename, m_strversion, k_strversion);
3241 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3243 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3245 if (k_new_syscalls) {
3246 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
3247 if (!new_get_kernel_symbols())
3249 k_crcs = new_is_kernel_checksummed();
3251 error_msg("Not configured to support new kernels");
3255 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
3256 if (!old_get_kernel_symbols(m_name))
3258 k_crcs = old_is_kernel_checksummed();
3260 error_msg("Not configured to support old kernels");
3265 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3267 m_crcs = new_is_module_checksummed(f);
3269 m_crcs = old_is_module_checksummed(f);
3271 if (m_crcs != k_crcs)
3272 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3273 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3275 /* Let the module know about the kernel symbols. */
3276 add_kernel_symbols(f);
3278 /* Allocate common symbols, symbol tables, and string tables. */
3281 ? !new_create_this_module(f, m_name)
3282 : !old_create_mod_use_count(f))
3287 if (!obj_check_undefineds(f)) {
3290 obj_allocate_commons(f);
3292 /* done with the module name, on to the optional var=value arguments */
3295 if (optind < argc) {
3297 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3298 : !old_process_module_arguments(f, argc - optind, argv + optind))
3305 hide_special_symbols(f);
3308 new_create_module_ksymtab(f);
3310 /* Find current size of the module */
3311 m_size = obj_load_size(f);
3314 m_addr = create_module(m_name, m_size);
3315 if (m_addr==-1) switch (errno) {
3317 error_msg("A module named %s already exists", m_name);
3320 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3324 perror_msg("create_module: %s", m_name);
3328 if (!obj_relocate(f, m_addr)) {
3329 delete_module(m_name);
3334 ? !new_init_module(m_name, f, m_size)
3335 : !old_init_module(m_name, f, m_size))
3337 delete_module(m_name);
3341 exit_status = EXIT_SUCCESS;
3345 return(exit_status);