1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * This version of insmod supports x86, ARM, SH3/4, powerpc, m68k,
9 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
10 * Copyright (C) 1999-2002 Erik Andersen <andersee@debian.org>
11 * Written by Erik Andersen and Ron Alder <alder@lineo.com>
13 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
14 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
16 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
17 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
18 * very minor changes required to also work with StrongArm and presumably
19 * all ARM based systems.
21 * Magnus Damm <damm@opensource.se> 22-May-2002.
22 * The plt and got code are now using the same structs.
23 * Added generic linked list code to fully support PowerPC.
24 * Replaced the mess in arch_apply_relocation() with architecture blocks.
25 * The arch_create_got() function got cleaned up with architecture blocks.
26 * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
28 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
29 * PowerPC specific code stolen from modutils-2.3.16,
30 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
31 * I've only tested the code on mpc8xx platforms in big-endian mode.
32 * Did some cleanup and added BB_USE_xxx_ENTRIES...
34 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
35 * based on modutils-2.4.2
36 * MIPS specific support for Elf loading and relocation.
37 * Copyright 1996, 1997 Linux International.
38 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
40 * Based almost entirely on the Linux modutils-2.3.11 implementation.
41 * Copyright 1996, 1997 Linux International.
42 * New implementation contributed by Richard Henderson <rth@tamu.edu>
43 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
44 * Restructured (and partly rewritten) by:
45 * Björn Ekwall <bj0rn@blox.se> February 1999
47 * This program is free software; you can redistribute it and/or modify
48 * it under the terms of the GNU General Public License as published by
49 * the Free Software Foundation; either version 2 of the License, or
50 * (at your option) any later version.
52 * This program is distributed in the hope that it will be useful,
53 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55 * General Public License for more details.
57 * You should have received a copy of the GNU General Public License
58 * along with this program; if not, write to the Free Software
59 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
74 #include <sys/utsname.h>
77 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
78 # undef BB_FEATURE_OLD_MODULE_INTERFACE
79 # define new_sys_init_module init_module
81 # define old_sys_init_module init_module
84 #ifdef BB_FEATURE_INSMOD_LOADINKMEM
91 #define BB_USE_PLT_ENTRIES
92 #define BB_PLT_ENTRY_SIZE 8
93 #define BB_USE_GOT_ENTRIES
94 #define BB_GOT_ENTRY_SIZE 8
97 #define MATCH_MACHINE(x) (x == EM_ARM)
98 #define SHT_RELM SHT_REL
99 #define Elf32_RelM Elf32_Rel
100 #define ELFCLASSM ELFCLASS32
103 #if defined(__i386__)
104 #define BB_USE_GOT_ENTRIES
105 #define BB_GOT_ENTRY_SIZE 4
106 #define BB_USE_SINGLE
109 #define MATCH_MACHINE(x) (x == EM_386)
111 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
114 #define SHT_RELM SHT_REL
115 #define Elf32_RelM Elf32_Rel
116 #define ELFCLASSM ELFCLASS32
119 #if defined(__mc68000__)
120 #define BB_USE_GOT_ENTRIES
121 #define BB_GOT_ENTRY_SIZE 4
122 #define BB_USE_SINGLE
124 #define MATCH_MACHINE(x) (x == EM_68K)
125 #define SHT_RELM SHT_RELA
126 #define Elf32_RelM Elf32_Rela
129 #if defined(__mips__)
130 /* Account for ELF spec changes. */
131 #ifndef EM_MIPS_RS3_LE
132 #ifdef EM_MIPS_RS4_BE
133 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
135 #define EM_MIPS_RS3_LE 10
137 #endif /* !EM_MIPS_RS3_LE */
139 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
140 #define SHT_RELM SHT_REL
141 #define Elf32_RelM Elf32_Rel
142 #define ELFCLASSM ELFCLASS32
143 #define ARCHDATAM "__dbe_table"
146 #if defined(__powerpc__)
147 #define BB_USE_PLT_ENTRIES
148 #define BB_PLT_ENTRY_SIZE 16
149 #define BB_USE_PLT_LIST
150 #define BB_LIST_ARCHTYPE ElfW(Addr)
153 #define MATCH_MACHINE(x) (x == EM_PPC)
154 #define SHT_RELM SHT_RELA
155 #define Elf32_RelM Elf32_Rela
156 #define ELFCLASSM ELFCLASS32
157 #define ARCHDATAM "__ftr_fixup"
161 #define BB_USE_GOT_ENTRIES
162 #define BB_GOT_ENTRY_SIZE 4
163 #define BB_USE_SINGLE
165 #define MATCH_MACHINE(x) (x == EM_SH)
166 #define SHT_RELM SHT_RELA
167 #define Elf32_RelM Elf32_Rela
168 #define ELFCLASSM ELFCLASS32
170 /* the SH changes have only been tested on the SH4 in =little endian= mode */
171 /* I'm not sure about big endian, so let's warn: */
173 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
174 #error insmod.c may require changes for use on big endian SH4/SH3
177 /* it may or may not work on the SH1/SH2... So let's error on those
179 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
180 #error insmod.c may require changes for non-SH3/SH4 use
185 #error Sorry, but insmod.c does not yet support this architecture...
188 //----------------------------------------------------------------------------
189 //--------modutils module.h, lines 45-242
190 //----------------------------------------------------------------------------
192 /* Definitions for the Linux module syscall interface.
193 Copyright 1996, 1997 Linux International.
195 Contributed by Richard Henderson <rth@tamu.edu>
197 This file is part of the Linux modutils.
199 This program is free software; you can redistribute it and/or modify it
200 under the terms of the GNU General Public License as published by the
201 Free Software Foundation; either version 2 of the License, or (at your
202 option) any later version.
204 This program is distributed in the hope that it will be useful, but
205 WITHOUT ANY WARRANTY; without even the implied warranty of
206 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
207 General Public License for more details.
209 You should have received a copy of the GNU General Public License
210 along with this program; if not, write to the Free Software Foundation,
211 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
214 #ifndef MODUTILS_MODULE_H
215 #define MODUTILS_MODULE_H
217 /* This file contains the structures used by the 2.0 and 2.1 kernels.
218 We do not use the kernel headers directly because we do not wish
219 to be dependant on a particular kernel version to compile insmod. */
222 /*======================================================================*/
223 /* The structures used by Linux 2.0. */
225 /* The symbol format used by get_kernel_syms(2). */
226 struct old_kernel_sym
232 struct old_module_ref
234 unsigned long module; /* kernel addresses */
238 struct old_module_symbol
244 struct old_symbol_table
246 int size; /* total, including string table!!! */
249 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
250 struct old_module_ref ref[0]; /* actual size defined by n_refs */
253 struct old_mod_routines
256 unsigned long cleanup;
262 unsigned long ref; /* the list of modules that refer to me */
263 unsigned long symtab;
265 int size; /* size of module in pages */
266 unsigned long addr; /* address of module */
268 unsigned long cleanup; /* cleanup routine */
271 /* Sent to init_module(2) or'ed into the code size parameter. */
272 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
274 int get_kernel_syms(struct old_kernel_sym *);
275 int old_sys_init_module(const char *name, char *code, unsigned codesize,
276 struct old_mod_routines *, struct old_symbol_table *);
278 /*======================================================================*/
279 /* For sizeof() which are related to the module platform and not to the
280 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
282 #define tgt_sizeof_char sizeof(char)
283 #define tgt_sizeof_short sizeof(short)
284 #define tgt_sizeof_int sizeof(int)
285 #define tgt_sizeof_long sizeof(long)
286 #define tgt_sizeof_char_p sizeof(char *)
287 #define tgt_sizeof_void_p sizeof(void *)
288 #define tgt_long long
290 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
291 #undef tgt_sizeof_long
292 #undef tgt_sizeof_char_p
293 #undef tgt_sizeof_void_p
295 static const int tgt_sizeof_long = 8;
296 static const int tgt_sizeof_char_p = 8;
297 static const int tgt_sizeof_void_p = 8;
298 #define tgt_long long long
301 /*======================================================================*/
302 /* The structures used in Linux 2.1. */
304 /* Note: new_module_symbol does not use tgt_long intentionally */
305 struct new_module_symbol
311 struct new_module_persist;
313 struct new_module_ref
315 unsigned tgt_long dep; /* kernel addresses */
316 unsigned tgt_long ref;
317 unsigned tgt_long next_ref;
322 unsigned tgt_long size_of_struct; /* == sizeof(module) */
323 unsigned tgt_long next;
324 unsigned tgt_long name;
325 unsigned tgt_long size;
328 unsigned tgt_long flags; /* AUTOCLEAN et al */
333 unsigned tgt_long syms;
334 unsigned tgt_long deps;
335 unsigned tgt_long refs;
336 unsigned tgt_long init;
337 unsigned tgt_long cleanup;
338 unsigned tgt_long ex_table_start;
339 unsigned tgt_long ex_table_end;
341 unsigned tgt_long gp;
343 /* Everything after here is extension. */
344 unsigned tgt_long persist_start;
345 unsigned tgt_long persist_end;
346 unsigned tgt_long can_unload;
347 unsigned tgt_long runsize;
348 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
349 const char *kallsyms_start; /* All symbols for kernel debugging */
350 const char *kallsyms_end;
351 const char *archdata_start; /* arch specific data for module */
352 const char *archdata_end;
353 const char *kernel_data; /* Reserved for kernel internal use */
358 #define ARCHDATA_SEC_NAME ARCHDATAM
360 #define ARCHDATA_SEC_NAME "__archdata"
362 #define KALLSYMS_SEC_NAME "__kallsyms"
365 struct new_module_info
373 /* Bits of module.flags. */
374 static const int NEW_MOD_RUNNING = 1;
375 static const int NEW_MOD_DELETED = 2;
376 static const int NEW_MOD_AUTOCLEAN = 4;
377 static const int NEW_MOD_VISITED = 8;
378 static const int NEW_MOD_USED_ONCE = 16;
380 int new_sys_init_module(const char *name, const struct new_module *);
381 int query_module(const char *name, int which, void *buf, size_t bufsize,
384 /* Values for query_module's which. */
386 static const int QM_MODULES = 1;
387 static const int QM_DEPS = 2;
388 static const int QM_REFS = 3;
389 static const int QM_SYMBOLS = 4;
390 static const int QM_INFO = 5;
392 /*======================================================================*/
393 /* The system calls unchanged between 2.0 and 2.1. */
395 unsigned long create_module(const char *, size_t);
396 int delete_module(const char *);
399 #endif /* module.h */
401 //----------------------------------------------------------------------------
402 //--------end of modutils module.h
403 //----------------------------------------------------------------------------
407 //----------------------------------------------------------------------------
408 //--------modutils obj.h, lines 253-462
409 //----------------------------------------------------------------------------
411 /* Elf object file loading and relocation routines.
412 Copyright 1996, 1997 Linux International.
414 Contributed by Richard Henderson <rth@tamu.edu>
416 This file is part of the Linux modutils.
418 This program is free software; you can redistribute it and/or modify it
419 under the terms of the GNU General Public License as published by the
420 Free Software Foundation; either version 2 of the License, or (at your
421 option) any later version.
423 This program is distributed in the hope that it will be useful, but
424 WITHOUT ANY WARRANTY; without even the implied warranty of
425 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
426 General Public License for more details.
428 You should have received a copy of the GNU General Public License
429 along with this program; if not, write to the Free Software Foundation,
430 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
433 #ifndef MODUTILS_OBJ_H
434 static const int MODUTILS_OBJ_H = 1;
436 /* The relocatable object is manipulated using elfin types. */
442 #if __BYTE_ORDER == __LITTLE_ENDIAN
443 #define ELFDATAM ELFDATA2LSB
444 #elif __BYTE_ORDER == __BIG_ENDIAN
445 #define ELFDATAM ELFDATA2MSB
449 # if ELFCLASSM == ELFCLASS32
450 # define ElfW(x) Elf32_ ## x
451 # define ELFW(x) ELF32_ ## x
453 # define ElfW(x) Elf64_ ## x
454 # define ELFW(x) ELF64_ ## x
458 /* For some reason this is missing from libc5. */
459 #ifndef ELF32_ST_INFO
460 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
463 #ifndef ELF64_ST_INFO
464 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
467 struct obj_string_patch;
468 struct obj_symbol_patch;
475 struct obj_section *load_next;
481 struct obj_symbol *next; /* hash table link */
485 int secidx; /* the defining section index/module */
487 int ksymidx; /* for export to the kernel symtab */
488 int referenced; /* actually used in the link */
491 /* Hardcode the hash table size. We shouldn't be needing so many
492 symbols that we begin to degrade performance, and we get a big win
493 by giving the compiler a constant divisor. */
495 #define HASH_BUCKETS 521
501 struct obj_section **sections;
502 struct obj_section *load_order;
503 struct obj_section **load_order_search_start;
504 struct obj_string_patch *string_patches;
505 struct obj_symbol_patch *symbol_patches;
506 int (*symbol_cmp)(const char *, const char *);
507 unsigned long (*symbol_hash)(const char *);
508 unsigned long local_symtab_size;
509 struct obj_symbol **local_symtab;
510 struct obj_symbol *symtab[HASH_BUCKETS];
521 struct obj_string_patch
523 struct obj_string_patch *next;
525 ElfW(Addr) reloc_offset;
526 ElfW(Addr) string_offset;
529 struct obj_symbol_patch
531 struct obj_symbol_patch *next;
533 ElfW(Addr) reloc_offset;
534 struct obj_symbol *sym;
538 /* Generic object manipulation routines. */
540 static unsigned long obj_elf_hash(const char *);
542 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
544 static struct obj_symbol *obj_find_symbol (struct obj_file *f,
547 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
548 struct obj_symbol *sym);
550 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
551 static void obj_set_symbol_compare(struct obj_file *f,
552 int (*cmp)(const char *, const char *),
553 unsigned long (*hash)(const char *));
556 static struct obj_section *obj_find_section (struct obj_file *f,
559 static void obj_insert_section_load_order (struct obj_file *f,
560 struct obj_section *sec);
562 static struct obj_section *obj_create_alloced_section (struct obj_file *f,
567 static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
572 static void *obj_extend_section (struct obj_section *sec, unsigned long more);
574 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
577 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
578 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
579 struct obj_symbol *sym);
582 static int obj_check_undefineds(struct obj_file *f);
584 static void obj_allocate_commons(struct obj_file *f);
586 static unsigned long obj_load_size (struct obj_file *f);
588 static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
590 static struct obj_file *obj_load(FILE *f, int loadprogbits);
592 static int obj_create_image (struct obj_file *f, char *image);
594 /* Architecture specific manipulation routines. */
596 static struct obj_file *arch_new_file (void);
598 static struct obj_section *arch_new_section (void);
600 static struct obj_symbol *arch_new_symbol (void);
602 static enum obj_reloc arch_apply_relocation (struct obj_file *f,
603 struct obj_section *targsec,
604 struct obj_section *symsec,
605 struct obj_symbol *sym,
606 ElfW(RelM) *rel, ElfW(Addr) value);
608 static void arch_create_got (struct obj_file *f);
610 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
611 static int arch_init_module (struct obj_file *f, struct new_module *);
615 //----------------------------------------------------------------------------
616 //--------end of modutils obj.h
617 //----------------------------------------------------------------------------
623 #define _PATH_MODULES "/lib/modules"
624 static const int STRVERSIONLEN = 32;
626 /*======================================================================*/
628 static int flag_force_load = 0;
629 static int flag_autoclean = 0;
630 static int flag_verbose = 0;
631 static int flag_quiet = 0;
632 static int flag_export = 1;
635 /*======================================================================*/
637 #if defined(BB_USE_LIST)
639 struct arch_list_entry
641 struct arch_list_entry *next;
642 BB_LIST_ARCHTYPE addend;
649 #if defined(BB_USE_SINGLE)
651 struct arch_single_entry
660 #if defined(__mips__)
663 struct mips_hi16 *next;
670 struct obj_file root;
671 #if defined(BB_USE_PLT_ENTRIES)
672 struct obj_section *plt;
674 #if defined(BB_USE_GOT_ENTRIES)
675 struct obj_section *got;
677 #if defined(__mips__)
678 struct mips_hi16 *mips_hi16_list;
683 struct obj_symbol root;
684 #if defined(BB_USE_PLT_ENTRIES)
685 #if defined(BB_USE_PLT_LIST)
686 struct arch_list_entry *pltent;
688 struct arch_single_entry pltent;
691 #if defined(BB_USE_GOT_ENTRIES)
692 struct arch_single_entry gotent;
697 struct external_module {
702 struct new_module_symbol *syms;
705 static struct new_module_symbol *ksyms;
706 static size_t nksyms;
708 static struct external_module *ext_modules;
709 static int n_ext_modules;
710 static int n_ext_modules_used;
711 extern int delete_module(const char *);
713 static char m_filename[FILENAME_MAX];
714 static char m_fullName[FILENAME_MAX];
718 /*======================================================================*/
721 static int check_module_name_match(const char *filename, struct stat *statbuf,
724 char *fullname = (char *) userdata;
726 if (fullname[0] == '\0')
729 char *tmp, *tmp1 = strdup(filename);
730 tmp = get_last_path_component(tmp1);
731 if (strcmp(tmp, fullname) == 0) {
733 /* Stop searching if we find a match */
734 safe_strncpy(m_filename, filename, sizeof(m_filename));
743 /*======================================================================*/
745 static struct obj_file *arch_new_file(void)
748 f = xmalloc(sizeof(*f));
750 memset(f, 0, sizeof(*f));
755 static struct obj_section *arch_new_section(void)
757 return xmalloc(sizeof(struct obj_section));
760 static struct obj_symbol *arch_new_symbol(void)
762 struct arch_symbol *sym;
763 sym = xmalloc(sizeof(*sym));
765 memset(sym, 0, sizeof(*sym));
770 static enum obj_reloc
771 arch_apply_relocation(struct obj_file *f,
772 struct obj_section *targsec,
773 struct obj_section *symsec,
774 struct obj_symbol *sym,
775 ElfW(RelM) *rel, ElfW(Addr) v)
777 struct arch_file *ifile = (struct arch_file *) f;
778 enum obj_reloc ret = obj_reloc_ok;
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) || defined(BB_USE_PLT_ENTRIES)
782 struct arch_symbol *isym = (struct arch_symbol *) sym;
784 #if defined(BB_USE_GOT_ENTRIES)
785 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
787 #if defined(BB_USE_PLT_ENTRIES)
788 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
790 #if defined(BB_USE_PLT_LIST)
791 struct arch_list_entry *pe;
793 struct arch_single_entry *pe;
797 switch (ELF32_R_TYPE(rel->r_info)) {
811 /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
812 * (which is .got) similar to branch,
813 * but is full 32 bits relative */
823 case R_ARM_GOTOFF: /* address relative to the got */
828 #elif defined(__i386__)
864 #elif defined(__mc68000__)
875 ret = obj_reloc_overflow;
882 ret = obj_reloc_overflow;
889 if ((Elf32_Sword)v > 0x7f ||
890 (Elf32_Sword)v < -(Elf32_Sword)0x80) {
891 ret = obj_reloc_overflow;
898 if ((Elf32_Sword)v > 0x7fff ||
899 (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
900 ret = obj_reloc_overflow;
906 *(int *)loc = v - dot;
915 *(int *)loc += f->baseaddr;
926 #elif defined(__mips__)
937 ret = obj_reloc_dangerous;
938 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
939 ret = obj_reloc_overflow;
941 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
949 /* We cannot relocate this one now because we don't know the value
950 of the carry we need to add. Save the information, and let LO16
951 do the actual relocation. */
952 n = (struct mips_hi16 *) xmalloc(sizeof *n);
955 n->next = ifile->mips_hi16_list;
956 ifile->mips_hi16_list = n;
962 unsigned long insnlo = *loc;
963 Elf32_Addr val, vallo;
965 /* Sign extend the addend we extract from the lo insn. */
966 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
968 if (ifile->mips_hi16_list != NULL) {
971 l = ifile->mips_hi16_list;
973 struct mips_hi16 *next;
976 /* The value for the HI16 had best be the same. */
977 assert(v == l->value);
979 /* Do the HI16 relocation. Note that we actually don't
980 need to know anything about the LO16 itself, except where
981 to find the low 16 bits of the addend needed by the LO16. */
984 ((insn & 0xffff) << 16) +
988 /* Account for the sign extension that will happen in the
995 insn = (insn & ~0xffff) | val;
1003 ifile->mips_hi16_list = NULL;
1006 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
1008 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1013 #elif defined(__powerpc__)
1015 case R_PPC_ADDR16_HA:
1016 *(unsigned short *)loc = (v + 0x8000) >> 16;
1019 case R_PPC_ADDR16_HI:
1020 *(unsigned short *)loc = v >> 16;
1023 case R_PPC_ADDR16_LO:
1024 *(unsigned short *)loc = v;
1038 #elif defined(__sh__)
1061 *loc = f->baseaddr + rel->r_addend;
1066 *loc = got - dot + rel->r_addend;
1080 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1081 ret = obj_reloc_unhandled;
1084 #if defined(BB_USE_PLT_ENTRIES)
1088 /* find the plt entry and initialize it if necessary */
1089 assert(isym != NULL);
1091 #if defined(BB_USE_PLT_LIST)
1092 for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
1100 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
1102 /* generate some machine code */
1104 #if defined(__arm__)
1105 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
1106 ip[1] = v; /* sym@ */
1108 #if defined(__powerpc__)
1109 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
1110 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
1111 ip[2] = 0x7d6903a6; /* mtctr r11 */
1112 ip[3] = 0x4e800420; /* bctr */
1117 /* relative distance to target */
1119 /* if the target is too far away.... */
1120 if ((int)v < -0x02000000 || (int)v >= 0x02000000) {
1121 /* go via the plt */
1122 v = plt + pe->offset - dot;
1125 ret = obj_reloc_dangerous;
1127 /* merge the offset into the instruction. */
1128 #if defined(__arm__)
1129 /* Convert to words. */
1132 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1134 #if defined(__powerpc__)
1135 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1138 #endif /* BB_USE_PLT_ENTRIES */
1140 #if defined(BB_USE_GOT_ENTRIES)
1143 assert(isym != NULL);
1144 /* needs an entry in the .got: set it, once */
1145 if (!isym->gotent.inited) {
1146 isym->gotent.inited = 1;
1147 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1149 /* make the reloc with_respect_to_.got */
1151 *loc += isym->gotent.offset + rel->r_addend;
1152 #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
1153 *loc += isym->gotent.offset;
1157 #endif /* BB_USE_GOT_ENTRIES */
1163 #if defined(BB_USE_LIST)
1165 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
1166 int offset, int size)
1168 struct arch_list_entry *pe;
1170 for (pe = *list; pe != NULL; pe = pe->next) {
1171 if (pe->addend == rel->r_addend) {
1177 pe = xmalloc(sizeof(struct arch_list_entry));
1179 pe->addend = rel->r_addend;
1180 pe->offset = offset;
1190 #if defined(BB_USE_SINGLE)
1192 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
1193 int offset, int size)
1195 if (single->allocated == 0) {
1196 single->allocated = 1;
1197 single->offset = offset;
1206 #if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
1208 static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
1209 int offset, int size)
1211 struct obj_section *myrelsec = obj_find_section(f, name);
1218 obj_extend_section(myrelsec, offset);
1220 myrelsec = obj_create_alloced_section(f, name,
1230 static void arch_create_got(struct obj_file *f)
1232 #if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
1233 struct arch_file *ifile = (struct arch_file *) f;
1235 #if defined(BB_USE_GOT_ENTRIES)
1236 int got_offset = 0, got_needed = 0, got_allocate;
1238 #if defined(BB_USE_PLT_ENTRIES)
1239 int plt_offset = 0, plt_needed = 0, plt_allocate;
1241 struct obj_section *relsec, *symsec, *strsec;
1242 ElfW(RelM) *rel, *relend;
1243 ElfW(Sym) *symtab, *extsym;
1244 const char *strtab, *name;
1245 struct arch_symbol *intsym;
1247 for (i = 0; i < f->header.e_shnum; ++i) {
1248 relsec = f->sections[i];
1250 if (relsec->header.sh_type != SHT_RELM)
1253 symsec = f->sections[relsec->header.sh_link];
1254 strsec = f->sections[symsec->header.sh_link];
1256 rel = (ElfW(RelM) *) relsec->contents;
1257 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1258 symtab = (ElfW(Sym) *) symsec->contents;
1259 strtab = (const char *) strsec->contents;
1261 for (; rel < relend; ++rel) {
1262 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1264 #if defined(BB_USE_GOT_ENTRIES)
1267 #if defined(BB_USE_PLT_ENTRIES)
1271 switch (ELF32_R_TYPE(rel->r_info)) {
1273 #if defined(__arm__)
1289 #elif defined(__i386__)
1300 #elif defined(__powerpc__)
1306 #elif defined(__mc68000__)
1316 #elif defined(__sh__)
1332 if (extsym->st_name != 0) {
1333 name = strtab + extsym->st_name;
1335 name = f->sections[extsym->st_shndx]->name;
1337 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1338 #if defined(BB_USE_GOT_ENTRIES)
1340 got_offset += arch_single_init(
1341 rel, &intsym->gotent,
1342 got_offset, BB_GOT_ENTRY_SIZE);
1347 #if defined(BB_USE_PLT_ENTRIES)
1349 #if defined(BB_USE_PLT_LIST)
1350 plt_offset += arch_list_add(
1351 rel, &intsym->pltent,
1352 plt_offset, BB_PLT_ENTRY_SIZE);
1354 plt_offset += arch_single_init(
1355 rel, &intsym->pltent,
1356 plt_offset, BB_PLT_ENTRY_SIZE);
1364 #if defined(BB_USE_GOT_ENTRIES)
1366 ifile->got = arch_xsect_init(f, ".got", got_offset,
1371 #if defined(BB_USE_PLT_ENTRIES)
1373 ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
1378 #endif /* defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES) */
1381 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
1382 static int arch_init_module(struct obj_file *f, struct new_module *mod)
1389 /*======================================================================*/
1391 /* Standard ELF hash function. */
1392 static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1394 unsigned long h = 0;
1401 if ((g = (h & 0xf0000000)) != 0) {
1410 static unsigned long obj_elf_hash(const char *name)
1412 return obj_elf_hash_n(name, strlen(name));
1415 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1416 /* String comparison for non-co-versioned kernel and module. */
1418 static int ncv_strcmp(const char *a, const char *b)
1420 size_t alen = strlen(a), blen = strlen(b);
1422 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1423 return strncmp(a, b, alen);
1424 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1425 return strncmp(a, b, blen);
1427 return strcmp(a, b);
1430 /* String hashing for non-co-versioned kernel and module. Here
1431 we are simply forced to drop the crc from the hash. */
1433 static unsigned long ncv_symbol_hash(const char *str)
1435 size_t len = strlen(str);
1436 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1438 return obj_elf_hash_n(str, len);
1442 obj_set_symbol_compare(struct obj_file *f,
1443 int (*cmp) (const char *, const char *),
1444 unsigned long (*hash) (const char *))
1447 f->symbol_cmp = cmp;
1449 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1452 f->symbol_hash = hash;
1454 memcpy(tmptab, f->symtab, sizeof(tmptab));
1455 memset(f->symtab, 0, sizeof(f->symtab));
1457 for (i = 0; i < HASH_BUCKETS; ++i)
1458 for (sym = tmptab[i]; sym; sym = next) {
1459 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1461 sym->next = f->symtab[h];
1467 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1469 static struct obj_symbol *
1470 obj_add_symbol(struct obj_file *f, const char *name,
1471 unsigned long symidx, int info,
1472 int secidx, ElfW(Addr) value,
1475 struct obj_symbol *sym;
1476 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1477 int n_type = ELFW(ST_TYPE) (info);
1478 int n_binding = ELFW(ST_BIND) (info);
1480 for (sym = f->symtab[hash]; sym; sym = sym->next)
1481 if (f->symbol_cmp(sym->name, name) == 0) {
1482 int o_secidx = sym->secidx;
1483 int o_info = sym->info;
1484 int o_type = ELFW(ST_TYPE) (o_info);
1485 int o_binding = ELFW(ST_BIND) (o_info);
1487 /* A redefinition! Is it legal? */
1489 if (secidx == SHN_UNDEF)
1491 else if (o_secidx == SHN_UNDEF)
1493 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1494 /* Cope with local and global symbols of the same name
1495 in the same object file, as might have been created
1496 by ld -r. The only reason locals are now seen at this
1497 level at all is so that we can do semi-sensible things
1500 struct obj_symbol *nsym, **p;
1502 nsym = arch_new_symbol();
1503 nsym->next = sym->next;
1506 /* Excise the old (local) symbol from the hash chain. */
1507 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1511 } else if (n_binding == STB_LOCAL) {
1512 /* Another symbol of the same name has already been defined.
1513 Just add this to the local table. */
1514 sym = arch_new_symbol();
1517 f->local_symtab[symidx] = sym;
1519 } else if (n_binding == STB_WEAK)
1521 else if (o_binding == STB_WEAK)
1523 /* Don't unify COMMON symbols with object types the programmer
1525 else if (secidx == SHN_COMMON
1526 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1528 else if (o_secidx == SHN_COMMON
1529 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1532 /* Don't report an error if the symbol is coming from
1533 the kernel or some external module. */
1534 if (secidx <= SHN_HIRESERVE)
1535 error_msg("%s multiply defined", name);
1540 /* Completely new symbol. */
1541 sym = arch_new_symbol();
1542 sym->next = f->symtab[hash];
1543 f->symtab[hash] = sym;
1546 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1547 if (symidx >= f->local_symtab_size)
1548 error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1549 name, (long) symidx, (long) f->local_symtab_size);
1551 f->local_symtab[symidx] = sym;
1558 sym->secidx = secidx;
1564 static struct obj_symbol *
1565 obj_find_symbol(struct obj_file *f, const char *name)
1567 struct obj_symbol *sym;
1568 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1570 for (sym = f->symtab[hash]; sym; sym = sym->next)
1571 if (f->symbol_cmp(sym->name, name) == 0)
1578 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1581 if (sym->secidx >= SHN_LORESERVE)
1584 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1586 /* As a special case, a NULL sym has value zero. */
1591 static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1593 int i, n = f->header.e_shnum;
1595 for (i = 0; i < n; ++i)
1596 if (strcmp(f->sections[i]->name, name) == 0)
1597 return f->sections[i];
1602 static int obj_load_order_prio(struct obj_section *a)
1604 unsigned long af, ac;
1606 af = a->header.sh_flags;
1609 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1610 strcmp(a->name + 5, ".init"))
1614 if (!(af & SHF_WRITE))
1616 if (af & SHF_EXECINSTR)
1618 if (a->header.sh_type != SHT_NOBITS)
1625 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1627 struct obj_section **p;
1628 int prio = obj_load_order_prio(sec);
1629 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1630 if (obj_load_order_prio(*p) < prio)
1632 sec->load_next = *p;
1636 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
1638 unsigned long align,
1641 int newidx = f->header.e_shnum++;
1642 struct obj_section *sec;
1644 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1645 f->sections[newidx] = sec = arch_new_section();
1647 memset(sec, 0, sizeof(*sec));
1648 sec->header.sh_type = SHT_PROGBITS;
1649 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1650 sec->header.sh_size = size;
1651 sec->header.sh_addralign = align;
1655 sec->contents = xmalloc(size);
1657 obj_insert_section_load_order(f, sec);
1662 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1664 unsigned long align,
1667 int newidx = f->header.e_shnum++;
1668 struct obj_section *sec;
1670 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1671 f->sections[newidx] = sec = arch_new_section();
1673 memset(sec, 0, sizeof(*sec));
1674 sec->header.sh_type = SHT_PROGBITS;
1675 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1676 sec->header.sh_size = size;
1677 sec->header.sh_addralign = align;
1681 sec->contents = xmalloc(size);
1683 sec->load_next = f->load_order;
1684 f->load_order = sec;
1685 if (f->load_order_search_start == &f->load_order)
1686 f->load_order_search_start = &sec->load_next;
1691 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
1693 unsigned long oldsize = sec->header.sh_size;
1695 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1697 return sec->contents + oldsize;
1701 /* Conditionally add the symbols from the given symbol set to the
1707 int idx, struct new_module_symbol *syms, size_t nsyms)
1709 struct new_module_symbol *s;
1713 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1715 /* Only add symbols that are already marked external. If we
1716 override locals we may cause problems for argument initialization.
1717 We will also create a false dependency on the module. */
1718 struct obj_symbol *sym;
1720 sym = obj_find_symbol(f, (char *) s->name);
1721 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1722 sym = obj_add_symbol(f, (char *) s->name, -1,
1723 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1725 /* Did our symbol just get installed? If so, mark the
1726 module as "used". */
1727 if (sym->secidx == idx)
1735 static void add_kernel_symbols(struct obj_file *f)
1737 struct external_module *m;
1740 /* Add module symbols first. */
1742 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1744 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1745 m->nsyms)) m->used = 1, ++nused;
1747 n_ext_modules_used = nused;
1749 /* And finally the symbols from the kernel proper. */
1752 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1755 static char *get_modinfo_value(struct obj_file *f, const char *key)
1757 struct obj_section *sec;
1758 char *p, *v, *n, *ep;
1759 size_t klen = strlen(key);
1761 sec = obj_find_section(f, ".modinfo");
1765 ep = p + sec->header.sh_size;
1768 n = strchr(p, '\0');
1770 if (p + klen == v && strncmp(p, key, klen) == 0)
1773 if (p + klen == n && strcmp(p, key) == 0)
1783 /*======================================================================*/
1784 /* Functions relating to module loading in pre 2.1 kernels. */
1787 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1791 struct obj_symbol *sym;
1795 if ((q = strchr(p, '=')) == NULL) {
1801 sym = obj_find_symbol(f, p);
1803 /* Also check that the parameter was not resolved from the kernel. */
1804 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1805 error_msg("symbol for parameter %s not found", p);
1809 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1811 /* Do C quoting if we begin with a ". */
1815 str = alloca(strlen(q));
1816 for (r = str, q++; *q != '"'; ++q, ++r) {
1818 error_msg("improperly terminated string argument for %s", p);
1820 } else if (*q == '\\')
1854 if (q[1] >= '0' && q[1] <= '7') {
1855 c = (c * 8) + *++q - '0';
1856 if (q[1] >= '0' && q[1] <= '7')
1857 c = (c * 8) + *++q - '0';
1870 obj_string_patch(f, sym->secidx, sym->value, str);
1871 } else if (*q >= '0' && *q <= '9') {
1873 *loc++ = strtoul(q, &q, 0);
1874 while (*q++ == ',');
1876 char *contents = f->sections[sym->secidx]->contents;
1877 char *myloc = contents + sym->value;
1878 char *r; /* To search for commas */
1880 /* Break the string with comas */
1881 while ((r = strchr(q, ',')) != (char *) NULL) {
1883 obj_string_patch(f, sym->secidx, myloc - contents, q);
1884 myloc += sizeof(char *);
1889 obj_string_patch(f, sym->secidx, myloc - contents, q);
1898 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1899 static int old_is_module_checksummed(struct obj_file *f)
1901 return obj_find_symbol(f, "Using_Versions") != NULL;
1903 /* Get the module's kernel version in the canonical integer form. */
1906 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1908 struct obj_symbol *sym;
1912 sym = obj_find_symbol(f, "kernel_version");
1916 p = f->sections[sym->secidx]->contents + sym->value;
1917 safe_strncpy(str, p, STRVERSIONLEN);
1919 a = strtoul(p, &p, 10);
1922 b = strtoul(p + 1, &p, 10);
1925 c = strtoul(p + 1, &q, 10);
1929 return a << 16 | b << 8 | c;
1932 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1934 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
1936 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1938 static int old_get_kernel_symbols(const char *m_name)
1940 struct old_kernel_sym *ks, *k;
1941 struct new_module_symbol *s;
1942 struct external_module *mod;
1943 int nks, nms, nmod, i;
1945 nks = get_kernel_syms(NULL);
1948 perror_msg("get_kernel_syms: %s", m_name);
1950 error_msg("No kernel symbols");
1954 ks = k = xmalloc(nks * sizeof(*ks));
1956 if (get_kernel_syms(ks) != nks) {
1957 perror("inconsistency with get_kernel_syms -- is someone else "
1958 "playing with modules?");
1963 /* Collect the module information. */
1968 while (k->name[0] == '#' && k->name[1]) {
1969 struct old_kernel_sym *k2;
1971 /* Find out how many symbols this module has. */
1972 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1976 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1977 mod[nmod].name = k->name + 1;
1978 mod[nmod].addr = k->value;
1980 mod[nmod].nsyms = nms;
1981 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1983 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1984 s->name = (unsigned long) k->name;
1985 s->value = k->value;
1992 n_ext_modules = nmod + 1;
1994 /* Now collect the symbols for the kernel proper. */
1996 if (k->name[0] == '#')
1999 nksyms = nms = nks - (k - ks);
2000 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
2002 for (i = 0; i < nms; ++i, ++s, ++k) {
2003 s->name = (unsigned long) k->name;
2004 s->value = k->value;
2010 /* Return the kernel symbol checksum version, or zero if not used. */
2012 static int old_is_kernel_checksummed(void)
2014 /* Using_Versions is the first symbol. */
2016 && strcmp((char *) ksyms[0].name,
2017 "Using_Versions") == 0) return ksyms[0].value;
2023 static int old_create_mod_use_count(struct obj_file *f)
2025 struct obj_section *sec;
2027 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
2030 obj_add_symbol(f, "mod_use_count_", -1,
2031 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2038 old_init_module(const char *m_name, struct obj_file *f,
2039 unsigned long m_size)
2042 struct old_mod_routines routines;
2043 struct old_symbol_table *symtab;
2046 /* Create the symbol table */
2048 int nsyms = 0, strsize = 0, total;
2050 /* Size things first... */
2053 for (i = 0; i < HASH_BUCKETS; ++i) {
2054 struct obj_symbol *sym;
2055 for (sym = f->symtab[i]; sym; sym = sym->next)
2056 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2057 && sym->secidx <= SHN_HIRESERVE)
2059 sym->ksymidx = nsyms++;
2060 strsize += strlen(sym->name) + 1;
2065 total = (sizeof(struct old_symbol_table)
2066 + nsyms * sizeof(struct old_module_symbol)
2067 + n_ext_modules_used * sizeof(struct old_module_ref)
2069 symtab = xmalloc(total);
2070 symtab->size = total;
2071 symtab->n_symbols = nsyms;
2072 symtab->n_refs = n_ext_modules_used;
2074 if (flag_export && nsyms) {
2075 struct old_module_symbol *ksym;
2079 ksym = symtab->symbol;
2080 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
2081 + n_ext_modules_used * sizeof(struct old_module_ref));
2083 for (i = 0; i < HASH_BUCKETS; ++i) {
2084 struct obj_symbol *sym;
2085 for (sym = f->symtab[i]; sym; sym = sym->next)
2086 if (sym->ksymidx >= 0) {
2087 ksym->addr = obj_symbol_final_value(f, sym);
2089 (unsigned long) str - (unsigned long) symtab;
2091 strcpy(str, sym->name);
2092 str += strlen(sym->name) + 1;
2098 if (n_ext_modules_used) {
2099 struct old_module_ref *ref;
2102 ref = (struct old_module_ref *)
2103 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
2105 for (i = 0; i < n_ext_modules; ++i)
2106 if (ext_modules[i].used)
2107 ref++->module = ext_modules[i].addr;
2111 /* Fill in routines. */
2114 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2116 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2118 /* Whew! All of the initialization is complete. Collect the final
2119 module image and give it to the kernel. */
2121 image = xmalloc(m_size);
2122 obj_create_image(f, image);
2124 /* image holds the complete relocated module, accounting correctly for
2125 mod_use_count. However the old module kernel support assume that
2126 it is receiving something which does not contain mod_use_count. */
2127 ret = old_sys_init_module(m_name, image + sizeof(long),
2128 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2129 : 0), &routines, symtab);
2131 perror_msg("init_module: %s", m_name);
2141 #define old_create_mod_use_count(x) TRUE
2142 #define old_init_module(x, y, z) TRUE
2144 #endif /* BB_FEATURE_OLD_MODULE_INTERFACE */
2148 /*======================================================================*/
2149 /* Functions relating to module loading after 2.1.18. */
2152 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2156 struct obj_symbol *sym;
2157 char *contents, *loc;
2161 if ((q = strchr(p, '=')) == NULL) {
2166 key = alloca(q - p + 6);
2167 memcpy(key, "parm_", 5);
2168 memcpy(key + 5, p, q - p);
2171 p = get_modinfo_value(f, key);
2174 error_msg("invalid parameter %s", key);
2178 sym = obj_find_symbol(f, key);
2180 /* Also check that the parameter was not resolved from the kernel. */
2181 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2182 error_msg("symbol for parameter %s not found", key);
2187 min = strtoul(p, &p, 10);
2189 max = strtoul(p + 1, &p, 10);
2195 contents = f->sections[sym->secidx]->contents;
2196 loc = contents + sym->value;
2200 if ((*p == 's') || (*p == 'c')) {
2203 /* Do C quoting if we begin with a ", else slurp the lot. */
2207 str = alloca(strlen(q));
2208 for (r = str, q++; *q != '"'; ++q, ++r) {
2210 error_msg("improperly terminated string argument for %s",
2213 } else if (*q == '\\')
2247 if (q[1] >= '0' && q[1] <= '7') {
2248 c = (c * 8) + *++q - '0';
2249 if (q[1] >= '0' && q[1] <= '7')
2250 c = (c * 8) + *++q - '0';
2267 /* In this case, the string is not quoted. We will break
2268 it using the coma (like for ints). If the user wants to
2269 include comas in a string, he just has to quote it */
2271 /* Search the next coma */
2275 if (r != (char *) NULL) {
2276 /* Recopy the current field */
2277 str = alloca(r - q + 1);
2278 memcpy(str, q, r - q);
2280 /* I don't know if it is usefull, as the previous case
2281 doesn't null terminate the string ??? */
2284 /* Keep next fields */
2295 obj_string_patch(f, sym->secidx, loc - contents, str);
2296 loc += tgt_sizeof_char_p;
2298 /* Array of chars (in fact, matrix !) */
2299 unsigned long charssize; /* size of each member */
2301 /* Get the size of each member */
2302 /* Probably we should do that outside the loop ? */
2303 if (!isdigit(*(p + 1))) {
2304 error_msg("parameter type 'c' for %s must be followed by"
2305 " the maximum size", key);
2308 charssize = strtoul(p + 1, (char **) NULL, 10);
2311 if (strlen(str) >= charssize) {
2312 error_msg("string too long for %s (max %ld)", key,
2317 /* Copy to location */
2318 strcpy((char *) loc, str);
2322 long v = strtoul(q, &q, 0);
2329 loc += tgt_sizeof_short;
2333 loc += tgt_sizeof_int;
2337 loc += tgt_sizeof_long;
2341 error_msg("unknown parameter type '%c' for %s", *p, key);
2356 goto retry_end_of_value;
2360 error_msg("too many values for %s (max %d)", key, max);
2367 error_msg("invalid argument syntax for %s", key);
2374 error_msg("too few values for %s (min %d)", key, min);
2384 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2385 static int new_is_module_checksummed(struct obj_file *f)
2387 const char *p = get_modinfo_value(f, "using_checksums");
2394 /* Get the module's kernel version in the canonical integer form. */
2397 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2402 p = get_modinfo_value(f, "kernel_version");
2405 safe_strncpy(str, p, STRVERSIONLEN);
2407 a = strtoul(p, &p, 10);
2410 b = strtoul(p + 1, &p, 10);
2413 c = strtoul(p + 1, &q, 10);
2417 return a << 16 | b << 8 | c;
2420 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2423 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
2425 /* Fetch the loaded modules, and all currently exported symbols. */
2427 static int new_get_kernel_symbols(void)
2429 char *module_names, *mn;
2430 struct external_module *modules, *m;
2431 struct new_module_symbol *syms, *s;
2432 size_t ret, bufsize, nmod, nsyms, i, j;
2434 /* Collect the loaded modules. */
2436 module_names = xmalloc(bufsize = 256);
2438 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2439 if (errno == ENOSPC && bufsize < ret) {
2440 module_names = xrealloc(module_names, bufsize = ret);
2441 goto retry_modules_load;
2443 perror_msg("QM_MODULES");
2447 n_ext_modules = nmod = ret;
2449 /* Collect the modules' symbols. */
2452 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2453 memset(modules, 0, nmod * sizeof(*modules));
2454 for (i = 0, mn = module_names, m = modules;
2455 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2456 struct new_module_info info;
2458 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2459 if (errno == ENOENT) {
2460 /* The module was removed out from underneath us. */
2463 perror_msg("query_module: QM_INFO: %s", mn);
2467 syms = xmalloc(bufsize = 1024);
2469 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2472 syms = xrealloc(syms, bufsize = ret);
2473 goto retry_mod_sym_load;
2475 /* The module was removed out from underneath us. */
2478 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2485 m->addr = info.addr;
2489 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2490 s->name += (unsigned long) syms;
2495 /* Collect the kernel's symbols. */
2497 syms = xmalloc(bufsize = 16 * 1024);
2498 retry_kern_sym_load:
2499 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2500 if (errno == ENOSPC && bufsize < ret) {
2501 syms = xrealloc(syms, bufsize = ret);
2502 goto retry_kern_sym_load;
2504 perror_msg("kernel: QM_SYMBOLS");
2507 nksyms = nsyms = ret;
2510 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2511 s->name += (unsigned long) syms;
2517 /* Return the kernel symbol checksum version, or zero if not used. */
2519 static int new_is_kernel_checksummed(void)
2521 struct new_module_symbol *s;
2524 /* Using_Versions is not the first symbol, but it should be in there. */
2526 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2527 if (strcmp((char *) s->name, "Using_Versions") == 0)
2534 static int new_create_this_module(struct obj_file *f, const char *m_name)
2536 struct obj_section *sec;
2538 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2539 sizeof(struct new_module));
2540 memset(sec->contents, 0, sizeof(struct new_module));
2542 obj_add_symbol(f, "__this_module", -1,
2543 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2544 sizeof(struct new_module));
2546 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2553 static int new_create_module_ksymtab(struct obj_file *f)
2555 struct obj_section *sec;
2558 /* We must always add the module references. */
2560 if (n_ext_modules_used) {
2561 struct new_module_ref *dep;
2562 struct obj_symbol *tm;
2564 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2565 (sizeof(struct new_module_ref)
2566 * n_ext_modules_used));
2570 tm = obj_find_symbol(f, "__this_module");
2571 dep = (struct new_module_ref *) sec->contents;
2572 for (i = 0; i < n_ext_modules; ++i)
2573 if (ext_modules[i].used) {
2574 dep->dep = ext_modules[i].addr;
2575 obj_symbol_patch(f, sec->idx,
2576 (char *) &dep->ref - sec->contents, tm);
2582 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2587 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2590 /* We don't want to export symbols residing in sections that
2591 aren't loaded. There are a number of these created so that
2592 we make sure certain module options don't appear twice. */
2594 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2596 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2598 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2599 struct obj_symbol *sym;
2600 for (sym = f->symtab[i]; sym; sym = sym->next)
2601 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2602 && sym->secidx <= SHN_HIRESERVE
2603 && (sym->secidx >= SHN_LORESERVE
2604 || loaded[sym->secidx])) {
2605 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2607 obj_symbol_patch(f, sec->idx, ofs, sym);
2608 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2615 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2623 new_init_module(const char *m_name, struct obj_file *f,
2624 unsigned long m_size)
2626 struct new_module *module;
2627 struct obj_section *sec;
2632 sec = obj_find_section(f, ".this");
2633 if (!sec || !sec->contents) {
2634 perror_msg_and_die("corrupt module %s?",m_name);
2636 module = (struct new_module *) sec->contents;
2637 m_addr = sec->header.sh_addr;
2639 module->size_of_struct = sizeof(*module);
2640 module->size = m_size;
2641 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2643 sec = obj_find_section(f, "__ksymtab");
2644 if (sec && sec->header.sh_size) {
2645 module->syms = sec->header.sh_addr;
2646 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2649 if (n_ext_modules_used) {
2650 sec = obj_find_section(f, ".kmodtab");
2651 module->deps = sec->header.sh_addr;
2652 module->ndeps = n_ext_modules_used;
2656 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2658 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2660 sec = obj_find_section(f, "__ex_table");
2662 module->ex_table_start = sec->header.sh_addr;
2663 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2666 sec = obj_find_section(f, ".text.init");
2668 module->runsize = sec->header.sh_addr - m_addr;
2670 sec = obj_find_section(f, ".data.init");
2672 if (!module->runsize ||
2673 module->runsize > sec->header.sh_addr - m_addr)
2674 module->runsize = sec->header.sh_addr - m_addr;
2676 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2677 if (sec && sec->header.sh_size) {
2678 module->archdata_start = (void*)sec->header.sh_addr;
2679 module->archdata_end = module->archdata_start + sec->header.sh_size;
2681 sec = obj_find_section(f, KALLSYMS_SEC_NAME);
2682 if (sec && sec->header.sh_size) {
2683 module->kallsyms_start = (void*)sec->header.sh_addr;
2684 module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
2687 if (!arch_init_module(f, module))
2690 /* Whew! All of the initialization is complete. Collect the final
2691 module image and give it to the kernel. */
2693 image = xmalloc(m_size);
2694 obj_create_image(f, image);
2696 ret = new_sys_init_module(m_name, (struct new_module *) image);
2698 perror_msg("init_module: %s", m_name);
2707 #define new_init_module(x, y, z) TRUE
2708 #define new_create_this_module(x, y) 0
2709 #define new_create_module_ksymtab(x)
2710 #define query_module(v, w, x, y, z) -1
2712 #endif /* BB_FEATURE_NEW_MODULE_INTERFACE */
2715 /*======================================================================*/
2718 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2721 struct obj_string_patch *p;
2722 struct obj_section *strsec;
2723 size_t len = strlen(string) + 1;
2726 p = xmalloc(sizeof(*p));
2727 p->next = f->string_patches;
2728 p->reloc_secidx = secidx;
2729 p->reloc_offset = offset;
2730 f->string_patches = p;
2732 strsec = obj_find_section(f, ".kstrtab");
2733 if (strsec == NULL) {
2734 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2735 p->string_offset = 0;
2736 loc = strsec->contents;
2738 p->string_offset = strsec->header.sh_size;
2739 loc = obj_extend_section(strsec, len);
2741 memcpy(loc, string, len);
2746 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
2748 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2749 struct obj_symbol *sym)
2751 struct obj_symbol_patch *p;
2753 p = xmalloc(sizeof(*p));
2754 p->next = f->symbol_patches;
2755 p->reloc_secidx = secidx;
2756 p->reloc_offset = offset;
2758 f->symbol_patches = p;
2764 static int obj_check_undefineds(struct obj_file *f)
2769 for (i = 0; i < HASH_BUCKETS; ++i) {
2770 struct obj_symbol *sym;
2771 for (sym = f->symtab[i]; sym; sym = sym->next)
2772 if (sym->secidx == SHN_UNDEF) {
2773 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2774 sym->secidx = SHN_ABS;
2778 error_msg("unresolved symbol %s", sym->name);
2788 static void obj_allocate_commons(struct obj_file *f)
2790 struct common_entry {
2791 struct common_entry *next;
2792 struct obj_symbol *sym;
2793 } *common_head = NULL;
2797 for (i = 0; i < HASH_BUCKETS; ++i) {
2798 struct obj_symbol *sym;
2799 for (sym = f->symtab[i]; sym; sym = sym->next)
2800 if (sym->secidx == SHN_COMMON) {
2801 /* Collect all COMMON symbols and sort them by size so as to
2802 minimize space wasted by alignment requirements. */
2804 struct common_entry **p, *n;
2805 for (p = &common_head; *p; p = &(*p)->next)
2806 if (sym->size <= (*p)->sym->size)
2809 n = alloca(sizeof(*n));
2817 for (i = 1; i < f->local_symtab_size; ++i) {
2818 struct obj_symbol *sym = f->local_symtab[i];
2819 if (sym && sym->secidx == SHN_COMMON) {
2820 struct common_entry **p, *n;
2821 for (p = &common_head; *p; p = &(*p)->next)
2822 if (sym == (*p)->sym)
2824 else if (sym->size < (*p)->sym->size) {
2825 n = alloca(sizeof(*n));
2835 /* Find the bss section. */
2836 for (i = 0; i < f->header.e_shnum; ++i)
2837 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2840 /* If for some reason there hadn't been one, create one. */
2841 if (i == f->header.e_shnum) {
2842 struct obj_section *sec;
2844 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2845 f->sections[i] = sec = arch_new_section();
2846 f->header.e_shnum = i + 1;
2848 memset(sec, 0, sizeof(*sec));
2849 sec->header.sh_type = SHT_PROGBITS;
2850 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2855 /* Allocate the COMMONS. */
2857 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2858 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2859 struct common_entry *c;
2861 for (c = common_head; c; c = c->next) {
2862 ElfW(Addr) align = c->sym->value;
2864 if (align > max_align)
2866 if (bss_size & (align - 1))
2867 bss_size = (bss_size | (align - 1)) + 1;
2870 c->sym->value = bss_size;
2872 bss_size += c->sym->size;
2875 f->sections[i]->header.sh_size = bss_size;
2876 f->sections[i]->header.sh_addralign = max_align;
2880 /* For the sake of patch relocation and parameter initialization,
2881 allocate zeroed data for NOBITS sections now. Note that after
2882 this we cannot assume NOBITS are really empty. */
2883 for (i = 0; i < f->header.e_shnum; ++i) {
2884 struct obj_section *s = f->sections[i];
2885 if (s->header.sh_type == SHT_NOBITS) {
2886 if (s->header.sh_size != 0)
2887 s->contents = memset(xmalloc(s->header.sh_size),
2888 0, s->header.sh_size);
2892 s->header.sh_type = SHT_PROGBITS;
2897 static unsigned long obj_load_size(struct obj_file *f)
2899 unsigned long dot = 0;
2900 struct obj_section *sec;
2902 /* Finalize the positions of the sections relative to one another. */
2904 for (sec = f->load_order; sec; sec = sec->load_next) {
2907 align = sec->header.sh_addralign;
2908 if (align && (dot & (align - 1)))
2909 dot = (dot | (align - 1)) + 1;
2911 sec->header.sh_addr = dot;
2912 dot += sec->header.sh_size;
2918 static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2920 int i, n = f->header.e_shnum;
2923 /* Finalize the addresses of the sections. */
2926 for (i = 0; i < n; ++i)
2927 f->sections[i]->header.sh_addr += base;
2929 /* And iterate over all of the relocations. */
2931 for (i = 0; i < n; ++i) {
2932 struct obj_section *relsec, *symsec, *targsec, *strsec;
2933 ElfW(RelM) * rel, *relend;
2937 relsec = f->sections[i];
2938 if (relsec->header.sh_type != SHT_RELM)
2941 symsec = f->sections[relsec->header.sh_link];
2942 targsec = f->sections[relsec->header.sh_info];
2943 strsec = f->sections[symsec->header.sh_link];
2945 rel = (ElfW(RelM) *) relsec->contents;
2946 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2947 symtab = (ElfW(Sym) *) symsec->contents;
2948 strtab = (const char *) strsec->contents;
2950 for (; rel < relend; ++rel) {
2951 ElfW(Addr) value = 0;
2952 struct obj_symbol *intsym = NULL;
2953 unsigned long symndx;
2954 ElfW(Sym) * extsym = 0;
2957 /* Attempt to find a value to use for this relocation. */
2959 symndx = ELFW(R_SYM) (rel->r_info);
2961 /* Note we've already checked for undefined symbols. */
2963 extsym = &symtab[symndx];
2964 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2965 /* Local symbols we look up in the local table to be sure
2966 we get the one that is really intended. */
2967 intsym = f->local_symtab[symndx];
2969 /* Others we look up in the hash table. */
2971 if (extsym->st_name)
2972 name = strtab + extsym->st_name;
2974 name = f->sections[extsym->st_shndx]->name;
2975 intsym = obj_find_symbol(f, name);
2978 value = obj_symbol_final_value(f, intsym);
2979 intsym->referenced = 1;
2981 #if SHT_RELM == SHT_RELA
2982 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2983 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2984 if (!extsym || !extsym->st_name ||
2985 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2987 value += rel->r_addend;
2991 switch (arch_apply_relocation
2992 (f, targsec, symsec, intsym, rel, value)) {
2996 case obj_reloc_overflow:
2997 errmsg = "Relocation overflow";
2999 case obj_reloc_dangerous:
3000 errmsg = "Dangerous relocation";
3002 case obj_reloc_unhandled:
3003 errmsg = "Unhandled relocation";
3006 error_msg("%s of type %ld for %s", errmsg,
3007 (long) ELFW(R_TYPE) (rel->r_info),
3008 strtab + extsym->st_name);
3010 error_msg("%s of type %ld", errmsg,
3011 (long) ELFW(R_TYPE) (rel->r_info));
3019 /* Finally, take care of the patches. */
3021 if (f->string_patches) {
3022 struct obj_string_patch *p;
3023 struct obj_section *strsec;
3024 ElfW(Addr) strsec_base;
3025 strsec = obj_find_section(f, ".kstrtab");
3026 strsec_base = strsec->header.sh_addr;
3028 for (p = f->string_patches; p; p = p->next) {
3029 struct obj_section *targsec = f->sections[p->reloc_secidx];
3030 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3031 = strsec_base + p->string_offset;
3035 if (f->symbol_patches) {
3036 struct obj_symbol_patch *p;
3038 for (p = f->symbol_patches; p; p = p->next) {
3039 struct obj_section *targsec = f->sections[p->reloc_secidx];
3040 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
3041 = obj_symbol_final_value(f, p->sym);
3048 static int obj_create_image(struct obj_file *f, char *image)
3050 struct obj_section *sec;
3051 ElfW(Addr) base = f->baseaddr;
3053 for (sec = f->load_order; sec; sec = sec->load_next) {
3056 if (sec->contents == 0 || sec->header.sh_size == 0)
3059 secimg = image + (sec->header.sh_addr - base);
3061 /* Note that we allocated data for NOBITS sections earlier. */
3062 memcpy(secimg, sec->contents, sec->header.sh_size);
3068 /*======================================================================*/
3070 static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3073 ElfW(Shdr) * section_headers;
3077 /* Read the file header. */
3079 f = arch_new_file();
3080 memset(f, 0, sizeof(*f));
3081 f->symbol_cmp = strcmp;
3082 f->symbol_hash = obj_elf_hash;
3083 f->load_order_search_start = &f->load_order;
3085 fseek(fp, 0, SEEK_SET);
3086 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3087 perror_msg("error reading ELF header");
3091 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3092 || f->header.e_ident[EI_MAG1] != ELFMAG1
3093 || f->header.e_ident[EI_MAG2] != ELFMAG2
3094 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3095 error_msg("not an ELF file");
3098 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3099 || f->header.e_ident[EI_DATA] != ELFDATAM
3100 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3101 || !MATCH_MACHINE(f->header.e_machine)) {
3102 error_msg("ELF file not for this architecture");
3105 if (f->header.e_type != ET_REL) {
3106 error_msg("ELF file not a relocatable object");
3110 /* Read the section headers. */
3112 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3113 error_msg("section header size mismatch: %lu != %lu",
3114 (unsigned long) f->header.e_shentsize,
3115 (unsigned long) sizeof(ElfW(Shdr)));
3119 shnum = f->header.e_shnum;
3120 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
3121 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
3123 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3124 fseek(fp, f->header.e_shoff, SEEK_SET);
3125 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3126 perror_msg("error reading ELF section headers");
3130 /* Read the section data. */
3132 for (i = 0; i < shnum; ++i) {
3133 struct obj_section *sec;
3135 f->sections[i] = sec = arch_new_section();
3136 memset(sec, 0, sizeof(*sec));
3138 sec->header = section_headers[i];
3141 if(sec->header.sh_size) switch (sec->header.sh_type) {
3150 if (!loadprogbits) {
3151 sec->contents = NULL;
3158 if (sec->header.sh_size > 0) {
3159 sec->contents = xmalloc(sec->header.sh_size);
3160 fseek(fp, sec->header.sh_offset, SEEK_SET);
3161 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3162 perror_msg("error reading ELF section data");
3166 sec->contents = NULL;
3170 #if SHT_RELM == SHT_REL
3172 error_msg("RELA relocations not supported on this architecture");
3176 error_msg("REL relocations not supported on this architecture");
3181 if (sec->header.sh_type >= SHT_LOPROC) {
3182 /* Assume processor specific section types are debug
3183 info and can safely be ignored. If this is ever not
3184 the case (Hello MIPS?), don't put ifdefs here but
3185 create an arch_load_proc_section(). */
3189 error_msg("can't handle sections of type %ld",
3190 (long) sec->header.sh_type);
3195 /* Do what sort of interpretation as needed by each section. */
3197 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3199 for (i = 0; i < shnum; ++i) {
3200 struct obj_section *sec = f->sections[i];
3201 sec->name = shstrtab + sec->header.sh_name;
3204 for (i = 0; i < shnum; ++i) {
3205 struct obj_section *sec = f->sections[i];
3207 /* .modinfo should be contents only but gcc has no attribute for that.
3208 * The kernel may have marked .modinfo as ALLOC, ignore this bit.
3210 if (strcmp(sec->name, ".modinfo") == 0)
3211 sec->header.sh_flags &= ~SHF_ALLOC;
3213 if (sec->header.sh_flags & SHF_ALLOC)
3214 obj_insert_section_load_order(f, sec);
3216 switch (sec->header.sh_type) {
3219 unsigned long nsym, j;
3223 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3224 error_msg("symbol size mismatch: %lu != %lu",
3225 (unsigned long) sec->header.sh_entsize,
3226 (unsigned long) sizeof(ElfW(Sym)));
3230 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3231 strtab = f->sections[sec->header.sh_link]->contents;
3232 sym = (ElfW(Sym) *) sec->contents;
3234 /* Allocate space for a table of local symbols. */
3235 j = f->local_symtab_size = sec->header.sh_info;
3236 f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
3238 /* Insert all symbols into the hash table. */
3239 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3242 name = strtab + sym->st_name;
3244 name = f->sections[sym->st_shndx]->name;
3246 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3247 sym->st_value, sym->st_size);
3253 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3254 error_msg("relocation entry size mismatch: %lu != %lu",
3255 (unsigned long) sec->header.sh_entsize,
3256 (unsigned long) sizeof(ElfW(RelM)));
3260 /* XXX Relocation code from modutils-2.3.19 is not here.
3261 * Why? That's about 20 lines of code from obj/obj_load.c,
3262 * which gets done in a second pass through the sections.
3263 * This BusyBox insmod does similar work in obj_relocate(). */
3270 #ifdef BB_FEATURE_INSMOD_LOADINKMEM
3272 * load the unloaded sections directly into the memory allocated by
3273 * kernel for the module
3276 static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3278 ElfW(Addr) base = f->baseaddr;
3279 struct obj_section* sec;
3281 for (sec = f->load_order; sec; sec = sec->load_next) {
3283 /* section already loaded? */
3284 if (sec->contents != NULL)
3287 if (sec->header.sh_size == 0)
3290 sec->contents = imagebase + (sec->header.sh_addr - base);
3291 fseek(fp, sec->header.sh_offset, SEEK_SET);
3292 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3293 error_msg("error reading ELF section data: %s\n", strerror(errno));
3302 static void hide_special_symbols(struct obj_file *f)
3304 static const char *const specials[] = {
3311 struct obj_symbol *sym;
3312 const char *const *p;
3314 for (p = specials; *p; ++p)
3315 if ((sym = obj_find_symbol(f, *p)) != NULL)
3317 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3320 static int obj_gpl_license(struct obj_file *f, const char **license)
3322 struct obj_section *sec;
3323 /* This list must match *exactly* the list of allowable licenses in
3324 * linux/include/linux/module.h. Checking for leading "GPL" will not
3325 * work, somebody will use "GPL sucks, this is proprietary".
3327 static const char *gpl_licenses[] = {
3330 "GPL and additional rights",
3335 if ((sec = obj_find_section(f, ".modinfo"))) {
3336 const char *value, *ptr, *endptr;
3337 ptr = sec->contents;
3338 endptr = ptr + sec->header.sh_size;
3339 while (ptr < endptr) {
3340 if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
3344 for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
3345 if (strcmp(value+1, gpl_licenses[i]) == 0)
3350 if (strchr(ptr, '\0'))
3351 ptr = strchr(ptr, '\0') + 1;
3359 #define TAINT_FILENAME "/proc/sys/kernel/tainted"
3360 #define TAINT_PROPRIETORY_MODULE (1<<0)
3361 #define TAINT_FORCED_MODULE (1<<1)
3362 #define TAINT_UNSAFE_SMP (1<<2)
3363 #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
3365 static void set_tainted(struct obj_file *f, int fd, char *m_name,
3366 int kernel_has_tainted, int taint, const char *text1, const char *text2)
3370 static int first = 1;
3371 if (fd < 0 && !kernel_has_tainted)
3372 return; /* New modutils on old kernel */
3373 printf("Warning: loading %s will taint the kernel: %s%s\n",
3374 m_name, text1, text2);
3376 printf(" See %s for information about tainted modules\n", TAINT_URL);
3380 read(fd, buf, sizeof(buf)-1);
3381 buf[sizeof(buf)-1] = '\0';
3382 oldval = strtoul(buf, NULL, 10);
3383 sprintf(buf, "%d\n", oldval | taint);
3384 write(fd, buf, strlen(buf));
3388 /* Check if loading this module will taint the kernel. */
3389 static void check_tainted_module(struct obj_file *f, char *m_name)
3391 static const char tainted_file[] = TAINT_FILENAME;
3392 int fd, kernel_has_tainted;
3395 kernel_has_tainted = 1;
3396 if ((fd = open(tainted_file, O_RDWR)) < 0) {
3397 if (errno == ENOENT)
3398 kernel_has_tainted = 0;
3399 else if (errno == EACCES)
3400 kernel_has_tainted = 1;
3402 perror(tainted_file);
3403 kernel_has_tainted = 0;
3407 switch (obj_gpl_license(f, &ptr)) {
3411 set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
3414 /* The module has a non-GPL license so we pretend that the
3415 * kernel always has a taint flag to get a warning even on
3416 * kernels without the proc flag.
3418 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
3421 set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
3425 if (flag_force_load)
3426 set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
3432 extern int insmod_main( int argc, char **argv)
3439 unsigned long m_size;
3444 char m_name[FILENAME_MAX] = "\0";
3445 int exit_status = EXIT_FAILURE;
3447 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3448 struct utsname uts_info;
3449 char m_strversion[STRVERSIONLEN];
3454 /* Parse any options */
3455 while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0) {
3457 case 'f': /* force loading */
3458 flag_force_load = 1;
3460 case 'k': /* module loaded by kerneld, auto-cleanable */
3463 case 's': /* log to syslog */
3464 /* log to syslog -- not supported */
3465 /* but kernel needs this for request_module(), */
3466 /* as this calls: modprobe -k -s -- <module> */
3467 /* so silently ignore this flag */
3469 case 'v': /* verbose output */
3472 case 'q': /* silent */
3475 case 'x': /* do not export externs */
3478 case 'o': /* name the output module */
3479 safe_strncpy(m_name, optarg, sizeof(m_name));
3481 case 'L': /* Stub warning */
3482 /* This is needed for compatibility with modprobe.
3483 * In theory, this does locking, but we don't do
3484 * that. So be careful and plan your life around not
3485 * loading the same module 50 times concurrently. */
3492 if (argv[optind] == NULL) {
3496 /* Grab the module name */
3497 tmp1 = xstrdup(argv[optind]);
3498 tmp = basename(tmp1);
3501 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
3505 /* Make sure there is space for the terminal NULL */
3508 if (len >= sizeof(m_fullName)) {
3509 len = sizeof(m_fullName);
3511 safe_strncpy(m_fullName, tmp, len);
3514 if (*m_name == '\0') {
3515 safe_strncpy(m_name, m_fullName, sizeof(m_name));
3517 len = strlen(m_fullName);
3518 if (len > (sizeof(m_fullName)-3))
3519 error_msg_and_die("%s: no module by that name found", m_fullName);
3520 strcat(m_fullName, ".o");
3522 /* Get a filedesc for the module. Check we we have a complete path */
3523 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3524 (fp = fopen(argv[optind], "r")) == NULL) {
3525 struct utsname myuname;
3527 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3528 * but do not error out yet if we fail to find it... */
3529 if (uname(&myuname) == 0) {
3530 char module_dir[FILENAME_MAX];
3531 char real_module_dir[FILENAME_MAX];
3532 snprintf (module_dir, sizeof(module_dir), "%s/%s",
3533 _PATH_MODULES, myuname.release);
3534 /* Jump through hoops in case /lib/modules/`uname -r`
3535 * is a symlink. We do not want recursive_action to
3536 * follow symlinks, but we do want to follow the
3537 * /lib/modules/`uname -r` dir, So resolve it ourselves
3538 * if it is a link... */
3539 if (realpath (module_dir, real_module_dir) == NULL)
3540 strcpy(real_module_dir, module_dir);
3541 recursive_action(real_module_dir, TRUE, FALSE, FALSE,
3542 check_module_name_match, 0, m_fullName);
3545 /* Check if we have found anything yet */
3546 if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL))
3548 char module_dir[FILENAME_MAX];
3549 if (realpath (_PATH_MODULES, module_dir) == NULL)
3550 strcpy(module_dir, _PATH_MODULES);
3551 /* No module found under /lib/modules/`uname -r`, this
3552 * time cast the net a bit wider. Search /lib/modules/ */
3553 if (recursive_action(module_dir, TRUE, FALSE, FALSE,
3554 check_module_name_match, 0, m_fullName) == FALSE)
3556 if (m_filename[0] == '\0'
3557 || ((fp = fopen(m_filename, "r")) == NULL))
3559 error_msg("%s: no module by that name found", m_fullName);
3560 return EXIT_FAILURE;
3563 error_msg_and_die("%s: no module by that name found", m_fullName);
3566 safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
3570 printf("Using %s\n", m_filename);
3574 if ((f = obj_load(fp, LOADBITS)) == NULL)
3575 perror_msg_and_die("Could not load the module");
3577 if (get_modinfo_value(f, "kernel_version") == NULL)
3582 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3583 /* Version correspondence? */
3585 if (uname(&uts_info) < 0)
3586 uts_info.release[0] = '\0';
3587 if (m_has_modinfo) {
3588 m_version = new_get_module_version(f, m_strversion);
3590 m_version = old_get_module_version(f, m_strversion);
3591 if (m_version == -1) {
3592 error_msg("couldn't find the kernel version the module was "
3598 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
3599 if (flag_force_load) {
3600 error_msg("Warning: kernel-module version mismatch\n"
3601 "\t%s was compiled for kernel version %s\n"
3602 "\twhile this kernel is version %s",
3603 m_filename, m_strversion, uts_info.release);
3605 error_msg("kernel-module version mismatch\n"
3606 "\t%s was compiled for kernel version %s\n"
3607 "\twhile this kernel is version %s.",
3608 m_filename, m_strversion, uts_info.release);
3614 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3616 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3618 if (k_new_syscalls) {
3619 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
3620 if (!new_get_kernel_symbols())
3622 k_crcs = new_is_kernel_checksummed();
3624 error_msg("Not configured to support new kernels");
3628 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
3629 if (!old_get_kernel_symbols(m_name))
3631 k_crcs = old_is_kernel_checksummed();
3633 error_msg("Not configured to support old kernels");
3638 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3640 m_crcs = new_is_module_checksummed(f);
3642 m_crcs = old_is_module_checksummed(f);
3644 if (m_crcs != k_crcs)
3645 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3646 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3648 /* Let the module know about the kernel symbols. */
3649 add_kernel_symbols(f);
3651 /* Allocate common symbols, symbol tables, and string tables. */
3654 ? !new_create_this_module(f, m_name)
3655 : !old_create_mod_use_count(f))
3660 if (!obj_check_undefineds(f)) {
3663 obj_allocate_commons(f);
3664 /* BRCM begin: sorry, we don't check */
3665 //check_tainted_module(f, m_name);
3668 /* done with the module name, on to the optional var=value arguments */
3671 if (optind < argc) {
3673 ? !old_process_module_arguments(f, argc - optind, argv + optind)
3674 : !old_process_module_arguments(f, argc - optind, argv + optind))
3681 hide_special_symbols(f);
3684 new_create_module_ksymtab(f);
3686 /* Find current size of the module */
3687 m_size = obj_load_size(f);
3690 m_addr = create_module(m_name, m_size);
3691 if (m_addr==-1) switch (errno) {
3693 error_msg("A module named %s already exists", m_name);
3696 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3700 perror_msg("create_module: %s", m_name);
3706 * the PROGBITS section was not loaded by the obj_load
3707 * now we can load them directly into the kernel memory
3709 if (!obj_load_progbits(fp, f, (char*)m_addr)) {
3710 delete_module(m_name);
3715 if (!obj_relocate(f, m_addr)) {
3716 delete_module(m_name);
3721 ? !new_init_module(m_name, f, m_size)
3722 : !old_init_module(m_name, f, m_size))
3724 delete_module(m_name);
3728 exit_status = EXIT_SUCCESS;
3732 return(exit_status);