1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * Copyright (C) 1999,2000,2001 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>
7 * and Ron Alder <alder@lineo.com>
9 * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
10 * and (theoretically) SH3. I have only tested SH4 in little endian mode.
12 * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
13 * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
14 * very minor changes required to also work with StrongArm and presumably
15 * all ARM based systems.
17 * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
18 * PowerPC specific code stolen from modutils-2.3.16,
19 * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
20 * I've only tested the code on mpc8xx platforms in big-endian mode.
21 * Did some cleanup and added BB_USE_xxx_ENTRIES...
23 * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
24 * based on modutils-2.4.2
25 * MIPS specific support for Elf loading and relocation.
26 * Copyright 1996, 1997 Linux International.
27 * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
29 * Based almost entirely on the Linux modutils-2.3.11 implementation.
30 * Copyright 1996, 1997 Linux International.
31 * New implementation contributed by Richard Henderson <rth@tamu.edu>
32 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
33 * Restructured (and partly rewritten) by:
34 * Björn Ekwall <bj0rn@blox.se> February 1999
36 * This program is free software; you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation; either version 2 of the License, or
39 * (at your option) any later version.
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44 * General Public License for more details.
46 * You should have received a copy of the GNU General Public License
47 * along with this program; if not, write to the Free Software
48 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
62 #include <sys/utsname.h>
63 #include <sys/syscall.h>
64 #include <linux/unistd.h>
67 #if defined(__powerpc__)
68 #define BB_USE_PLT_ENTRIES
69 #define BB_PLT_ENTRY_SIZE 16
73 #define BB_USE_PLT_ENTRIES
74 #define BB_PLT_ENTRY_SIZE 8
75 #define BB_USE_GOT_ENTRIES
76 #define BB_GOT_ENTRY_SIZE 8
80 #define BB_USE_GOT_ENTRIES
81 #define BB_GOT_ENTRY_SIZE 4
85 #define BB_USE_GOT_ENTRIES
86 #define BB_GOT_ENTRY_SIZE 4
93 //----------------------------------------------------------------------------
94 //--------modutils module.h, lines 45-242
95 //----------------------------------------------------------------------------
97 /* Definitions for the Linux module syscall interface.
98 Copyright 1996, 1997 Linux International.
100 Contributed by Richard Henderson <rth@tamu.edu>
102 This file is part of the Linux modutils.
104 This program is free software; you can redistribute it and/or modify it
105 under the terms of the GNU General Public License as published by the
106 Free Software Foundation; either version 2 of the License, or (at your
107 option) any later version.
109 This program is distributed in the hope that it will be useful, but
110 WITHOUT ANY WARRANTY; without even the implied warranty of
111 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
112 General Public License for more details.
114 You should have received a copy of the GNU General Public License
115 along with this program; if not, write to the Free Software Foundation,
116 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
119 #ifndef MODUTILS_MODULE_H
120 static const int MODUTILS_MODULE_H = 1;
122 #ident "$Id: insmod.c,v 1.50 2001/02/24 20:01:53 andersen Exp $"
124 /* This file contains the structures used by the 2.0 and 2.1 kernels.
125 We do not use the kernel headers directly because we do not wish
126 to be dependant on a particular kernel version to compile insmod. */
129 /*======================================================================*/
130 /* The structures used by Linux 2.0. */
132 /* The symbol format used by get_kernel_syms(2). */
133 struct old_kernel_sym
139 struct old_module_ref
141 unsigned long module; /* kernel addresses */
145 struct old_module_symbol
151 struct old_symbol_table
153 int size; /* total, including string table!!! */
156 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
157 struct old_module_ref ref[0]; /* actual size defined by n_refs */
160 struct old_mod_routines
163 unsigned long cleanup;
169 unsigned long ref; /* the list of modules that refer to me */
170 unsigned long symtab;
172 int size; /* size of module in pages */
173 unsigned long addr; /* address of module */
175 unsigned long cleanup; /* cleanup routine */
178 /* Sent to init_module(2) or'ed into the code size parameter. */
179 static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
181 int get_kernel_syms(struct old_kernel_sym *);
182 int old_sys_init_module(const char *name, char *code, unsigned codesize,
183 struct old_mod_routines *, struct old_symbol_table *);
185 /*======================================================================*/
186 /* For sizeof() which are related to the module platform and not to the
187 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
189 #define tgt_sizeof_char sizeof(char)
190 #define tgt_sizeof_short sizeof(short)
191 #define tgt_sizeof_int sizeof(int)
192 #define tgt_sizeof_long sizeof(long)
193 #define tgt_sizeof_char_p sizeof(char *)
194 #define tgt_sizeof_void_p sizeof(void *)
195 #define tgt_long long
197 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
198 #undef tgt_sizeof_long
199 #undef tgt_sizeof_char_p
200 #undef tgt_sizeof_void_p
202 static const int tgt_sizeof_long = 8;
203 static const int tgt_sizeof_char_p = 8;
204 static const int tgt_sizeof_void_p = 8;
205 #define tgt_long long long
208 /*======================================================================*/
209 /* The structures used in Linux 2.1. */
211 /* Note: new_module_symbol does not use tgt_long intentionally */
212 struct new_module_symbol
218 struct new_module_persist;
220 struct new_module_ref
222 unsigned tgt_long dep; /* kernel addresses */
223 unsigned tgt_long ref;
224 unsigned tgt_long next_ref;
229 unsigned tgt_long size_of_struct; /* == sizeof(module) */
230 unsigned tgt_long next;
231 unsigned tgt_long name;
232 unsigned tgt_long size;
235 unsigned tgt_long flags; /* AUTOCLEAN et al */
240 unsigned tgt_long syms;
241 unsigned tgt_long deps;
242 unsigned tgt_long refs;
243 unsigned tgt_long init;
244 unsigned tgt_long cleanup;
245 unsigned tgt_long ex_table_start;
246 unsigned tgt_long ex_table_end;
248 unsigned tgt_long gp;
250 /* Everything after here is extension. */
251 unsigned tgt_long persist_start;
252 unsigned tgt_long persist_end;
253 unsigned tgt_long can_unload;
254 unsigned tgt_long runsize;
257 struct new_module_info
265 /* Bits of module.flags. */
266 static const int NEW_MOD_RUNNING = 1;
267 static const int NEW_MOD_DELETED = 2;
268 static const int NEW_MOD_AUTOCLEAN = 4;
269 static const int NEW_MOD_VISITED = 8;
270 static const int NEW_MOD_USED_ONCE = 16;
272 int new_sys_init_module(const char *name, const struct new_module *);
273 int query_module(const char *name, int which, void *buf, size_t bufsize,
276 /* Values for query_module's which. */
278 static const int QM_MODULES = 1;
279 static const int QM_DEPS = 2;
280 static const int QM_REFS = 3;
281 static const int QM_SYMBOLS = 4;
282 static const int QM_INFO = 5;
284 /*======================================================================*/
285 /* The system calls unchanged between 2.0 and 2.1. */
287 unsigned long create_module(const char *, size_t);
288 int delete_module(const char *);
291 #endif /* module.h */
293 //----------------------------------------------------------------------------
294 //--------end of modutils module.h
295 //----------------------------------------------------------------------------
299 //----------------------------------------------------------------------------
300 //--------modutils obj.h, lines 253-462
301 //----------------------------------------------------------------------------
303 /* Elf object file loading and relocation routines.
304 Copyright 1996, 1997 Linux International.
306 Contributed by Richard Henderson <rth@tamu.edu>
308 This file is part of the Linux modutils.
310 This program is free software; you can redistribute it and/or modify it
311 under the terms of the GNU General Public License as published by the
312 Free Software Foundation; either version 2 of the License, or (at your
313 option) any later version.
315 This program is distributed in the hope that it will be useful, but
316 WITHOUT ANY WARRANTY; without even the implied warranty of
317 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
318 General Public License for more details.
320 You should have received a copy of the GNU General Public License
321 along with this program; if not, write to the Free Software Foundation,
322 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
325 #ifndef MODUTILS_OBJ_H
326 static const int MODUTILS_OBJ_H = 1;
328 #ident "$Id: insmod.c,v 1.50 2001/02/24 20:01:53 andersen Exp $"
330 /* The relocatable object is manipulated using elfin types. */
336 /* Machine-specific elf macros for i386 et al. */
338 /* the SH changes have only been tested on the SH4 in =little endian= mode */
339 /* I'm not sure about big endian, so let's warn: */
341 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
342 #error insmod.c may require changes for use on big endian SH4/SH3
345 /* it may or may not work on the SH1/SH2... So let's error on those
347 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
348 #error insmod.c may require changes for non-SH3/SH4 use
351 #define ELFCLASSM ELFCLASS32
355 #define MATCH_MACHINE(x) (x == EM_SH)
356 #define SHT_RELM SHT_RELA
357 #define Elf32_RelM Elf32_Rela
358 #define ELFDATAM ELFDATA2LSB
360 #elif defined(__arm__)
362 #define MATCH_MACHINE(x) (x == EM_ARM)
363 #define SHT_RELM SHT_REL
364 #define Elf32_RelM Elf32_Rel
365 #define ELFDATAM ELFDATA2LSB
367 #elif defined(__powerpc__)
369 #define MATCH_MACHINE(x) (x == EM_PPC)
370 #define SHT_RELM SHT_RELA
371 #define Elf32_RelM Elf32_Rela
372 #define ELFDATAM ELFDATA2MSB
374 #elif defined(__mips__)
376 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
377 #define SHT_RELM SHT_REL
378 #define Elf32_RelM Elf32_Rel
380 #define ELFDATAM ELFDATA2MSB
383 #define ELFDATAM ELFDATA2LSB
386 #elif defined(__i386__)
388 /* presumably we can use these for anything but the SH and ARM*/
389 /* this is the previous behavior, but it does result in
390 insmod.c being broken on anything except i386 */
392 #define MATCH_MACHINE(x) (x == EM_386)
394 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
397 #define SHT_RELM SHT_REL
398 #define Elf32_RelM Elf32_Rel
399 #define ELFDATAM ELFDATA2LSB
402 #error Sorry, but insmod.c does not yet support this architecture...
406 # if ELFCLASSM == ELFCLASS32
407 # define ElfW(x) Elf32_ ## x
408 # define ELFW(x) ELF32_ ## x
410 # define ElfW(x) Elf64_ ## x
411 # define ELFW(x) ELF64_ ## x
415 /* For some reason this is missing from libc5. */
416 #ifndef ELF32_ST_INFO
417 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
420 #ifndef ELF64_ST_INFO
421 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
424 struct obj_string_patch;
425 struct obj_symbol_patch;
432 struct obj_section *load_next;
438 struct obj_symbol *next; /* hash table link */
442 int secidx; /* the defining section index/module */
444 int ksymidx; /* for export to the kernel symtab */
445 int referenced; /* actually used in the link */
448 /* Hardcode the hash table size. We shouldn't be needing so many
449 symbols that we begin to degrade performance, and we get a big win
450 by giving the compiler a constant divisor. */
452 #define HASH_BUCKETS 521
458 struct obj_section **sections;
459 struct obj_section *load_order;
460 struct obj_section **load_order_search_start;
461 struct obj_string_patch *string_patches;
462 struct obj_symbol_patch *symbol_patches;
463 int (*symbol_cmp)(const char *, const char *);
464 unsigned long (*symbol_hash)(const char *);
465 unsigned long local_symtab_size;
466 struct obj_symbol **local_symtab;
467 struct obj_symbol *symtab[HASH_BUCKETS];
478 struct obj_string_patch
480 struct obj_string_patch *next;
482 ElfW(Addr) reloc_offset;
483 ElfW(Addr) string_offset;
486 struct obj_symbol_patch
488 struct obj_symbol_patch *next;
490 ElfW(Addr) reloc_offset;
491 struct obj_symbol *sym;
495 /* Generic object manipulation routines. */
497 unsigned long obj_elf_hash(const char *);
499 unsigned long obj_elf_hash_n(const char *, unsigned long len);
501 struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
502 unsigned long symidx, int info, int secidx,
503 ElfW(Addr) value, unsigned long size);
505 struct obj_symbol *obj_find_symbol (struct obj_file *f,
508 ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
509 struct obj_symbol *sym);
511 void obj_set_symbol_compare(struct obj_file *f,
512 int (*cmp)(const char *, const char *),
513 unsigned long (*hash)(const char *));
515 struct obj_section *obj_find_section (struct obj_file *f,
518 void obj_insert_section_load_order (struct obj_file *f,
519 struct obj_section *sec);
521 struct obj_section *obj_create_alloced_section (struct obj_file *f,
526 struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
531 void *obj_extend_section (struct obj_section *sec, unsigned long more);
533 int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
536 int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
537 struct obj_symbol *sym);
539 int obj_check_undefineds(struct obj_file *f);
541 void obj_allocate_commons(struct obj_file *f);
543 unsigned long obj_load_size (struct obj_file *f);
545 int obj_relocate (struct obj_file *f, ElfW(Addr) base);
547 struct obj_file *obj_load(FILE *f);
549 int obj_create_image (struct obj_file *f, char *image);
551 /* Architecture specific manipulation routines. */
553 struct obj_file *arch_new_file (void);
555 struct obj_section *arch_new_section (void);
557 struct obj_symbol *arch_new_symbol (void);
559 enum obj_reloc arch_apply_relocation (struct obj_file *f,
560 struct obj_section *targsec,
561 struct obj_section *symsec,
562 struct obj_symbol *sym,
563 ElfW(RelM) *rel, ElfW(Addr) value);
565 int arch_create_got (struct obj_file *f);
568 int arch_init_module (struct obj_file *f, struct new_module *);
571 //----------------------------------------------------------------------------
572 //--------end of modutils obj.h
573 //----------------------------------------------------------------------------
579 #define _PATH_MODULES "/lib/modules"
580 static const int STRVERSIONLEN = 32;
582 /*======================================================================*/
584 int flag_force_load = 0;
585 int flag_autoclean = 0;
586 int flag_verbose = 0;
590 /*======================================================================*/
592 /* previously, these were named i386_* but since we could be
593 compiling for the sh, I've renamed them to the more general
594 arch_* These structures are the same between the x86 and SH,
595 and we can't support anything else right now anyway. In the
596 future maybe they should be #if defined'd */
602 #if defined(BB_USE_PLT_ENTRIES)
603 struct arch_plt_entry
607 int inited:1; /* has been set up */
611 #if defined(BB_USE_GOT_ENTRIES)
612 struct arch_got_entry {
614 unsigned offset_done:1;
615 unsigned reloc_done:1;
619 #if defined(__mips__)
622 struct mips_hi16 *next;
629 struct obj_file root;
630 #if defined(BB_USE_PLT_ENTRIES)
631 struct obj_section *plt;
633 #if defined(BB_USE_GOT_ENTRIES)
634 struct obj_section *got;
636 #if defined(__mips__)
637 struct mips_hi16 *mips_hi16_list;
642 struct obj_symbol root;
643 #if defined(BB_USE_PLT_ENTRIES)
644 struct arch_plt_entry pltent;
646 #if defined(BB_USE_GOT_ENTRIES)
647 struct arch_got_entry gotent;
652 struct external_module {
657 struct new_module_symbol *syms;
660 struct new_module_symbol *ksyms;
663 struct external_module *ext_modules;
665 int n_ext_modules_used;
669 /* Some firendly syscalls to cheer everyone's day... */
670 #define __NR_new_sys_init_module __NR_init_module
671 _syscall2(int, new_sys_init_module, const char *, name,
672 const struct new_module *, info)
673 #define __NR_old_sys_init_module __NR_init_module
674 _syscall5(int, old_sys_init_module, const char *, name, char *, code,
675 unsigned, codesize, struct old_mod_routines *, routines,
676 struct old_symbol_table *, symtab)
678 _syscall1(int, delete_module, const char *, name)
680 extern int delete_module(const char *);
683 /* This is kind of troublesome. See, we don't actually support
684 the m68k or the arm the same way we support i386 and (now)
685 sh. In doing my SH patch, I just assumed that whatever works
686 for i386 also works for m68k and arm since currently insmod.c
687 does nothing special for them. If this isn't true, the below
688 line is rather misleading IMHO, and someone should either
689 change it or add more proper architecture-dependent support
692 -- Bryan Rittmeyer <bryan@ixiacom.com> */
694 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
695 _syscall1(int, get_kernel_syms, struct old_kernel_sym *, ks)
698 #if defined(__i386__) || defined(__m68k__) || defined(__arm__) \
699 || defined(__powerpc__)
700 /* Jump through hoops to fixup error return codes */
701 #define __NR__create_module __NR_create_module
702 static inline _syscall2(long, _create_module, const char *, name, size_t,
704 unsigned long create_module(const char *name, size_t size)
706 long ret = _create_module(name, size);
708 if (ret == -1 && errno > 125) {
715 _syscall2(unsigned long, create_module, const char *, name, size_t, size)
717 static char m_filename[BUFSIZ + 1] = "\0";
718 static char m_fullName[BUFSIZ + 1] = "\0";
720 /*======================================================================*/
723 static int findNamedModule(const char *fileName, struct stat *statbuf,
726 char *fullName = (char *) userDate;
729 if (fullName[0] == '\0')
732 char *tmp = strrchr((char *) fileName, '/');
735 tmp = (char *) fileName;
738 if (check_wildcard_match(tmp, fullName) == TRUE) {
739 /* Stop searching if we find a match */
740 memcpy(m_filename, fileName, strlen(fileName)+1);
748 /*======================================================================*/
750 struct obj_file *arch_new_file(void)
753 f = xmalloc(sizeof(*f));
755 #if defined(BB_USE_PLT_ENTRIES)
758 #if defined(BB_USE_GOT_ENTRIES)
761 #if defined(__mips__)
762 f->mips_hi16_list = NULL;
768 struct obj_section *arch_new_section(void)
770 return xmalloc(sizeof(struct obj_section));
773 struct obj_symbol *arch_new_symbol(void)
775 struct arch_symbol *sym;
776 sym = xmalloc(sizeof(*sym));
778 #if defined(BB_USE_PLT_ENTRIES)
779 memset(&sym->pltent, 0, sizeof(sym->pltent));
781 #if defined(BB_USE_GOT_ENTRIES)
782 memset(&sym->gotent, 0, sizeof(sym->gotent));
789 arch_apply_relocation(struct obj_file *f,
790 struct obj_section *targsec,
791 struct obj_section *symsec,
792 struct obj_symbol *sym,
793 ElfW(RelM) *rel, ElfW(Addr) v)
795 struct arch_file *ifile = (struct arch_file *) f;
796 struct arch_symbol *isym = (struct arch_symbol *) sym;
798 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
799 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
800 #if defined(BB_USE_GOT_ENTRIES)
801 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
803 #if defined(BB_USE_PLT_ENTRIES)
804 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
805 struct arch_plt_entry *pe;
808 enum obj_reloc ret = obj_reloc_ok;
810 switch (ELF32_R_TYPE(rel->r_info)) {
812 /* even though these constants seem to be the same for
813 the i386 and the sh, we "#if define" them for clarity
814 and in case that ever changes */
817 #elif defined(__arm__)
819 #elif defined(__i386__)
821 #elif defined(__powerpc__)
823 #elif defined(__mips__)
830 #elif defined(__arm__)
832 #elif defined(__i386__)
834 #elif defined(__powerpc__)
836 #elif defined(__mips__)
842 #if defined(__powerpc__)
843 case R_PPC_ADDR16_HA:
844 *(unsigned short *)loc = (v + 0x8000) >> 16;
847 case R_PPC_ADDR16_HI:
848 *(unsigned short *)loc = v >> 16;
851 case R_PPC_ADDR16_LO:
852 *(unsigned short *)loc = v;
856 #if defined(__mips__)
859 ret = obj_reloc_dangerous;
860 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
861 ret = obj_reloc_overflow;
863 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
871 /* We cannot relocate this one now because we don't know the value
872 of the carry we need to add. Save the information, and let LO16
873 do the actual relocation. */
874 n = (struct mips_hi16 *) xmalloc(sizeof *n);
877 n->next = ifile->mips_hi16_list;
878 ifile->mips_hi16_list = n;
884 unsigned long insnlo = *loc;
885 Elf32_Addr val, vallo;
887 /* Sign extend the addend we extract from the lo insn. */
888 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
890 if (ifile->mips_hi16_list != NULL) {
893 l = ifile->mips_hi16_list;
895 struct mips_hi16 *next;
898 /* The value for the HI16 had best be the same. */
899 assert(v == l->value);
901 /* Do the HI16 relocation. Note that we actually don't
902 need to know anything about the LO16 itself, except where
903 to find the low 16 bits of the addend needed by the LO16. */
906 ((insn & 0xffff) << 16) +
910 /* Account for the sign extension that will happen in the
917 insn = (insn & ~0xffff) | val;
925 ifile->mips_hi16_list = NULL;
928 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
930 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
937 #elif defined(__sh__)
941 #elif defined(__i386__)
946 #elif defined(__powerpc__)
956 #elif defined(__i386__)
959 #if defined(BB_USE_PLT_ENTRIES)
965 #if defined(__powerpc__)
968 /* find the plt entry and initialize it if necessary */
969 assert(isym != NULL);
971 pe = (struct arch_plt_entry*) &isym->pltent;
974 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
976 /* generate some machine code */
979 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
980 ip[1] = v; /* sym@ */
982 #if defined(__powerpc__)
983 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
984 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
985 ip[2] = 0x7d6903a6; /* mtctr r11 */
986 ip[3] = 0x4e800420; /* bctr */
991 /* relative distance to target */
993 /* if the target is too far away.... */
994 if ((int)v < -0x02000000 || (int)v >= 0x02000000) {
996 v = plt + pe->offset - dot;
999 ret = obj_reloc_dangerous;
1001 /* merge the offset into the instruction. */
1002 #if defined(__arm__)
1003 /* Convert to words. */
1006 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1008 #if defined(__powerpc__)
1009 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1012 #endif /* BB_USE_PLT_ENTRIES */
1014 #if defined(__arm__)
1015 #elif defined(__sh__)
1020 #elif defined(__i386__)
1021 case R_386_GLOB_DAT:
1022 case R_386_JMP_SLOT:
1027 #if defined(__arm__)
1028 #elif defined(__sh__)
1030 *loc += f->baseaddr + rel->r_addend;
1032 #elif defined(__i386__)
1033 case R_386_RELATIVE:
1034 *loc += f->baseaddr;
1038 #if defined(BB_USE_GOT_ENTRIES)
1042 #elif defined(__arm__)
1044 #elif defined(__i386__)
1049 *loc += got - dot + rel->r_addend;;
1050 #elif defined(__i386__) || defined(__arm__)
1057 #elif defined(__arm__)
1059 #elif defined(__i386__)
1062 assert(isym != NULL);
1063 /* needs an entry in the .got: set it, once */
1064 if (!isym->gotent.reloc_done) {
1065 isym->gotent.reloc_done = 1;
1066 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1068 /* make the reloc with_respect_to_.got */
1070 *loc += isym->gotent.offset + rel->r_addend;
1071 #elif defined(__i386__) || defined(__arm__)
1072 *loc += isym->gotent.offset;
1076 /* address relative to the got */
1079 #elif defined(__arm__)
1081 #elif defined(__i386__)
1088 #endif /* BB_USE_GOT_ENTRIES */
1091 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1092 ret = obj_reloc_unhandled;
1099 int arch_create_got(struct obj_file *f)
1101 #if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
1102 struct arch_file *ifile = (struct arch_file *) f;
1104 #if defined(BB_USE_GOT_ENTRIES)
1105 int got_offset = 0, gotneeded = 0;
1107 #if defined(BB_USE_PLT_ENTRIES)
1108 int plt_offset = 0, pltneeded = 0;
1110 struct obj_section *relsec, *symsec, *strsec;
1111 ElfW(RelM) *rel, *relend;
1112 ElfW(Sym) *symtab, *extsym;
1113 const char *strtab, *name;
1114 struct arch_symbol *intsym;
1116 for (i = 0; i < f->header.e_shnum; ++i) {
1117 relsec = f->sections[i];
1118 if (relsec->header.sh_type != SHT_RELM)
1121 symsec = f->sections[relsec->header.sh_link];
1122 strsec = f->sections[symsec->header.sh_link];
1124 rel = (ElfW(RelM) *) relsec->contents;
1125 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1126 symtab = (ElfW(Sym) *) symsec->contents;
1127 strtab = (const char *) strsec->contents;
1129 for (; rel < relend; ++rel) {
1130 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1132 switch (ELF32_R_TYPE(rel->r_info)) {
1133 #if defined(__arm__)
1136 #elif defined(__sh__)
1139 #elif defined(__i386__)
1144 #if defined(__powerpc__)
1150 #if defined(__arm__)
1159 if (got_offset == 0)
1161 #elif defined(__sh__)
1165 #elif defined(__i386__)
1175 if (extsym->st_name != 0) {
1176 name = strtab + extsym->st_name;
1178 name = f->sections[extsym->st_shndx]->name;
1180 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1181 #if defined(BB_USE_GOT_ENTRIES)
1182 if (!intsym->gotent.offset_done) {
1183 intsym->gotent.offset_done = 1;
1184 intsym->gotent.offset = got_offset;
1185 got_offset += BB_GOT_ENTRY_SIZE;
1188 #if defined(BB_USE_PLT_ENTRIES)
1189 if (pltneeded && intsym->pltent.allocated == 0) {
1190 intsym->pltent.allocated = 1;
1191 intsym->pltent.offset = plt_offset;
1192 plt_offset += BB_PLT_ENTRY_SIZE;
1193 intsym->pltent.inited = 0;
1200 #if defined(BB_USE_GOT_ENTRIES)
1202 struct obj_section* relsec = obj_find_section(f, ".got");
1205 obj_extend_section(relsec, got_offset);
1207 relsec = obj_create_alloced_section(f, ".got",
1213 ifile->got = relsec;
1217 #if defined(BB_USE_PLT_ENTRIES)
1219 ifile->plt = obj_create_alloced_section(f, ".plt",
1227 int arch_init_module(struct obj_file *f, struct new_module *mod)
1233 /*======================================================================*/
1235 /* Standard ELF hash function. */
1236 inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1238 unsigned long h = 0;
1245 if ((g = (h & 0xf0000000)) != 0) {
1254 unsigned long obj_elf_hash(const char *name)
1256 return obj_elf_hash_n(name, strlen(name));
1259 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1260 /* Get the kernel version in the canonical integer form. */
1262 static int get_kernel_version(char str[STRVERSIONLEN])
1264 struct utsname uts_info;
1268 if (uname(&uts_info) < 0)
1270 strncpy(str, uts_info.release, STRVERSIONLEN);
1271 p = uts_info.release;
1273 a = strtoul(p, &p, 10);
1276 b = strtoul(p + 1, &p, 10);
1279 c = strtoul(p + 1, &q, 10);
1283 return a << 16 | b << 8 | c;
1286 /* String comparison for non-co-versioned kernel and module. */
1288 static int ncv_strcmp(const char *a, const char *b)
1290 size_t alen = strlen(a), blen = strlen(b);
1292 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1293 return strncmp(a, b, alen);
1294 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1295 return strncmp(a, b, blen);
1297 return strcmp(a, b);
1300 /* String hashing for non-co-versioned kernel and module. Here
1301 we are simply forced to drop the crc from the hash. */
1303 static unsigned long ncv_symbol_hash(const char *str)
1305 size_t len = strlen(str);
1306 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1308 return obj_elf_hash_n(str, len);
1312 obj_set_symbol_compare(struct obj_file *f,
1313 int (*cmp) (const char *, const char *),
1314 unsigned long (*hash) (const char *))
1317 f->symbol_cmp = cmp;
1319 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1322 f->symbol_hash = hash;
1324 memcpy(tmptab, f->symtab, sizeof(tmptab));
1325 memset(f->symtab, 0, sizeof(f->symtab));
1327 for (i = 0; i < HASH_BUCKETS; ++i)
1328 for (sym = tmptab[i]; sym; sym = next) {
1329 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1331 sym->next = f->symtab[h];
1337 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1340 struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
1341 unsigned long symidx, int info,
1342 int secidx, ElfW(Addr) value,
1345 struct obj_symbol *sym;
1346 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1347 int n_type = ELFW(ST_TYPE) (info);
1348 int n_binding = ELFW(ST_BIND) (info);
1350 for (sym = f->symtab[hash]; sym; sym = sym->next)
1351 if (f->symbol_cmp(sym->name, name) == 0) {
1352 int o_secidx = sym->secidx;
1353 int o_info = sym->info;
1354 int o_type = ELFW(ST_TYPE) (o_info);
1355 int o_binding = ELFW(ST_BIND) (o_info);
1357 /* A redefinition! Is it legal? */
1359 if (secidx == SHN_UNDEF)
1361 else if (o_secidx == SHN_UNDEF)
1363 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1364 /* Cope with local and global symbols of the same name
1365 in the same object file, as might have been created
1366 by ld -r. The only reason locals are now seen at this
1367 level at all is so that we can do semi-sensible things
1370 struct obj_symbol *nsym, **p;
1372 nsym = arch_new_symbol();
1373 nsym->next = sym->next;
1376 /* Excise the old (local) symbol from the hash chain. */
1377 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1381 } else if (n_binding == STB_LOCAL) {
1382 /* Another symbol of the same name has already been defined.
1383 Just add this to the local table. */
1384 sym = arch_new_symbol();
1387 f->local_symtab[symidx] = sym;
1389 } else if (n_binding == STB_WEAK)
1391 else if (o_binding == STB_WEAK)
1393 /* Don't unify COMMON symbols with object types the programmer
1395 else if (secidx == SHN_COMMON
1396 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1398 else if (o_secidx == SHN_COMMON
1399 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1402 /* Don't report an error if the symbol is coming from
1403 the kernel or some external module. */
1404 if (secidx <= SHN_HIRESERVE)
1405 error_msg("%s multiply defined", name);
1410 /* Completely new symbol. */
1411 sym = arch_new_symbol();
1412 sym->next = f->symtab[hash];
1413 f->symtab[hash] = sym;
1416 if (ELFW(ST_BIND) (info) == STB_LOCAL)
1417 f->local_symtab[symidx] = sym;
1423 sym->secidx = secidx;
1429 struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
1431 struct obj_symbol *sym;
1432 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1434 for (sym = f->symtab[hash]; sym; sym = sym->next)
1435 if (f->symbol_cmp(sym->name, name) == 0)
1442 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1445 if (sym->secidx >= SHN_LORESERVE)
1448 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1450 /* As a special case, a NULL sym has value zero. */
1455 struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1457 int i, n = f->header.e_shnum;
1459 for (i = 0; i < n; ++i)
1460 if (strcmp(f->sections[i]->name, name) == 0)
1461 return f->sections[i];
1466 static int obj_load_order_prio(struct obj_section *a)
1468 unsigned long af, ac;
1470 af = a->header.sh_flags;
1473 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1474 strcmp(a->name + 5, ".init"))
1478 if (!(af & SHF_WRITE))
1480 if (af & SHF_EXECINSTR)
1482 if (a->header.sh_type != SHT_NOBITS)
1489 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1491 struct obj_section **p;
1492 int prio = obj_load_order_prio(sec);
1493 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1494 if (obj_load_order_prio(*p) < prio)
1496 sec->load_next = *p;
1500 struct obj_section *obj_create_alloced_section(struct obj_file *f,
1502 unsigned long align,
1505 int newidx = f->header.e_shnum++;
1506 struct obj_section *sec;
1508 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1509 f->sections[newidx] = sec = arch_new_section();
1511 memset(sec, 0, sizeof(*sec));
1512 sec->header.sh_type = SHT_PROGBITS;
1513 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1514 sec->header.sh_size = size;
1515 sec->header.sh_addralign = align;
1519 sec->contents = xmalloc(size);
1521 obj_insert_section_load_order(f, sec);
1526 struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1528 unsigned long align,
1531 int newidx = f->header.e_shnum++;
1532 struct obj_section *sec;
1534 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1535 f->sections[newidx] = sec = arch_new_section();
1537 memset(sec, 0, sizeof(*sec));
1538 sec->header.sh_type = SHT_PROGBITS;
1539 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1540 sec->header.sh_size = size;
1541 sec->header.sh_addralign = align;
1545 sec->contents = xmalloc(size);
1547 sec->load_next = f->load_order;
1548 f->load_order = sec;
1549 if (f->load_order_search_start == &f->load_order)
1550 f->load_order_search_start = &sec->load_next;
1555 void *obj_extend_section(struct obj_section *sec, unsigned long more)
1557 unsigned long oldsize = sec->header.sh_size;
1558 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1559 return sec->contents + oldsize;
1564 /* Conditionally add the symbols from the given symbol set to the
1570 int idx, struct new_module_symbol *syms, size_t nsyms)
1572 struct new_module_symbol *s;
1576 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1578 /* Only add symbols that are already marked external. If we
1579 override locals we may cause problems for argument initialization.
1580 We will also create a false dependency on the module. */
1581 struct obj_symbol *sym;
1583 sym = obj_find_symbol(f, (char *) s->name);
1584 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1585 sym = obj_add_symbol(f, (char *) s->name, -1,
1586 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1588 /* Did our symbol just get installed? If so, mark the
1589 module as "used". */
1590 if (sym->secidx == idx)
1598 static void add_kernel_symbols(struct obj_file *f)
1600 struct external_module *m;
1603 /* Add module symbols first. */
1605 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1607 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1608 m->nsyms)) m->used = 1, ++nused;
1610 n_ext_modules_used = nused;
1612 /* And finally the symbols from the kernel proper. */
1615 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1618 static char *get_modinfo_value(struct obj_file *f, const char *key)
1620 struct obj_section *sec;
1621 char *p, *v, *n, *ep;
1622 size_t klen = strlen(key);
1624 sec = obj_find_section(f, ".modinfo");
1628 ep = p + sec->header.sh_size;
1631 n = strchr(p, '\0');
1633 if (p + klen == v && strncmp(p, key, klen) == 0)
1636 if (p + klen == n && strcmp(p, key) == 0)
1646 /*======================================================================*/
1647 /* Functions relating to module loading in pre 2.1 kernels. */
1650 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1654 struct obj_symbol *sym;
1658 if ((q = strchr(p, '=')) == NULL) {
1664 sym = obj_find_symbol(f, p);
1666 /* Also check that the parameter was not resolved from the kernel. */
1667 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1668 error_msg("symbol for parameter %s not found", p);
1672 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1674 /* Do C quoting if we begin with a ". */
1678 str = alloca(strlen(q));
1679 for (r = str, q++; *q != '"'; ++q, ++r) {
1681 error_msg("improperly terminated string argument for %s", p);
1683 } else if (*q == '\\')
1717 if (q[1] >= '0' && q[1] <= '7') {
1718 c = (c * 8) + *++q - '0';
1719 if (q[1] >= '0' && q[1] <= '7')
1720 c = (c * 8) + *++q - '0';
1733 obj_string_patch(f, sym->secidx, sym->value, str);
1734 } else if (*q >= '0' && *q <= '9') {
1736 *loc++ = strtoul(q, &q, 0);
1737 while (*q++ == ',');
1739 char *contents = f->sections[sym->secidx]->contents;
1740 char *loc = contents + sym->value;
1741 char *r; /* To search for commas */
1743 /* Break the string with comas */
1744 while ((r = strchr(q, ',')) != (char *) NULL) {
1746 obj_string_patch(f, sym->secidx, loc - contents, q);
1747 loc += sizeof(char *);
1752 obj_string_patch(f, sym->secidx, loc - contents, q);
1761 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1762 static int old_is_module_checksummed(struct obj_file *f)
1764 return obj_find_symbol(f, "Using_Versions") != NULL;
1766 /* Get the module's kernel version in the canonical integer form. */
1769 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1771 struct obj_symbol *sym;
1775 sym = obj_find_symbol(f, "kernel_version");
1779 p = f->sections[sym->secidx]->contents + sym->value;
1780 strncpy(str, p, STRVERSIONLEN);
1782 a = strtoul(p, &p, 10);
1785 b = strtoul(p + 1, &p, 10);
1788 c = strtoul(p + 1, &q, 10);
1792 return a << 16 | b << 8 | c;
1795 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1797 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
1799 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1801 static int old_get_kernel_symbols(const char *m_name)
1803 struct old_kernel_sym *ks, *k;
1804 struct new_module_symbol *s;
1805 struct external_module *mod;
1806 int nks, nms, nmod, i;
1808 nks = get_kernel_syms(NULL);
1810 perror_msg("get_kernel_syms: %s", m_name);
1814 ks = k = xmalloc(nks * sizeof(*ks));
1816 if (get_kernel_syms(ks) != nks) {
1817 perror("inconsistency with get_kernel_syms -- is someone else "
1818 "playing with modules?");
1823 /* Collect the module information. */
1828 while (k->name[0] == '#' && k->name[1]) {
1829 struct old_kernel_sym *k2;
1830 struct new_module_symbol *s;
1832 /* Find out how many symbols this module has. */
1833 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1837 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1838 mod[nmod].name = k->name + 1;
1839 mod[nmod].addr = k->value;
1841 mod[nmod].nsyms = nms;
1842 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1844 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1845 s->name = (unsigned long) k->name;
1846 s->value = k->value;
1853 n_ext_modules = nmod + 1;
1855 /* Now collect the symbols for the kernel proper. */
1857 if (k->name[0] == '#')
1860 nksyms = nms = nks - (k - ks);
1861 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1863 for (i = 0; i < nms; ++i, ++s, ++k) {
1864 s->name = (unsigned long) k->name;
1865 s->value = k->value;
1871 /* Return the kernel symbol checksum version, or zero if not used. */
1873 static int old_is_kernel_checksummed(void)
1875 /* Using_Versions is the first symbol. */
1877 && strcmp((char *) ksyms[0].name,
1878 "Using_Versions") == 0) return ksyms[0].value;
1884 static int old_create_mod_use_count(struct obj_file *f)
1886 struct obj_section *sec;
1888 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1891 obj_add_symbol(f, "mod_use_count_", -1,
1892 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1899 old_init_module(const char *m_name, struct obj_file *f,
1900 unsigned long m_size)
1903 struct old_mod_routines routines;
1904 struct old_symbol_table *symtab;
1907 /* Create the symbol table */
1909 int nsyms = 0, strsize = 0, total;
1911 /* Size things first... */
1914 for (i = 0; i < HASH_BUCKETS; ++i) {
1915 struct obj_symbol *sym;
1916 for (sym = f->symtab[i]; sym; sym = sym->next)
1917 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1918 && sym->secidx <= SHN_HIRESERVE)
1920 sym->ksymidx = nsyms++;
1921 strsize += strlen(sym->name) + 1;
1926 total = (sizeof(struct old_symbol_table)
1927 + nsyms * sizeof(struct old_module_symbol)
1928 + n_ext_modules_used * sizeof(struct old_module_ref)
1930 symtab = xmalloc(total);
1931 symtab->size = total;
1932 symtab->n_symbols = nsyms;
1933 symtab->n_refs = n_ext_modules_used;
1935 if (flag_export && nsyms) {
1936 struct old_module_symbol *ksym;
1940 ksym = symtab->symbol;
1941 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1942 + n_ext_modules_used * sizeof(struct old_module_ref));
1944 for (i = 0; i < HASH_BUCKETS; ++i) {
1945 struct obj_symbol *sym;
1946 for (sym = f->symtab[i]; sym; sym = sym->next)
1947 if (sym->ksymidx >= 0) {
1948 ksym->addr = obj_symbol_final_value(f, sym);
1950 (unsigned long) str - (unsigned long) symtab;
1952 strcpy(str, sym->name);
1953 str += strlen(sym->name) + 1;
1959 if (n_ext_modules_used) {
1960 struct old_module_ref *ref;
1963 ref = (struct old_module_ref *)
1964 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
1966 for (i = 0; i < n_ext_modules; ++i)
1967 if (ext_modules[i].used)
1968 ref++->module = ext_modules[i].addr;
1972 /* Fill in routines. */
1975 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
1977 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
1979 /* Whew! All of the initialization is complete. Collect the final
1980 module image and give it to the kernel. */
1982 image = xmalloc(m_size);
1983 obj_create_image(f, image);
1985 /* image holds the complete relocated module, accounting correctly for
1986 mod_use_count. However the old module kernel support assume that
1987 it is receiving something which does not contain mod_use_count. */
1988 ret = old_sys_init_module(m_name, image + sizeof(long),
1989 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
1990 : 0), &routines, symtab);
1992 perror_msg("init_module: %s", m_name);
2002 #define old_create_mod_use_count(x) TRUE
2003 #define old_init_module(x, y, z) TRUE
2005 #endif /* BB_FEATURE_OLD_MODULE_INTERFACE */
2009 /*======================================================================*/
2010 /* Functions relating to module loading after 2.1.18. */
2013 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2017 struct obj_symbol *sym;
2018 char *contents, *loc;
2022 if ((q = strchr(p, '=')) == NULL) {
2027 key = alloca(q - p + 6);
2028 memcpy(key, "parm_", 5);
2029 memcpy(key + 5, p, q - p);
2032 p = get_modinfo_value(f, key);
2035 error_msg("invalid parameter %s", key);
2039 sym = obj_find_symbol(f, key);
2041 /* Also check that the parameter was not resolved from the kernel. */
2042 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2043 error_msg("symbol for parameter %s not found", key);
2048 min = strtoul(p, &p, 10);
2050 max = strtoul(p + 1, &p, 10);
2056 contents = f->sections[sym->secidx]->contents;
2057 loc = contents + sym->value;
2061 if ((*p == 's') || (*p == 'c')) {
2064 /* Do C quoting if we begin with a ", else slurp the lot. */
2068 str = alloca(strlen(q));
2069 for (r = str, q++; *q != '"'; ++q, ++r) {
2071 error_msg("improperly terminated string argument for %s",
2074 } else if (*q == '\\')
2108 if (q[1] >= '0' && q[1] <= '7') {
2109 c = (c * 8) + *++q - '0';
2110 if (q[1] >= '0' && q[1] <= '7')
2111 c = (c * 8) + *++q - '0';
2128 /* In this case, the string is not quoted. We will break
2129 it using the coma (like for ints). If the user wants to
2130 include comas in a string, he just has to quote it */
2132 /* Search the next coma */
2136 if (r != (char *) NULL) {
2137 /* Recopy the current field */
2138 str = alloca(r - q + 1);
2139 memcpy(str, q, r - q);
2141 /* I don't know if it is usefull, as the previous case
2142 doesn't null terminate the string ??? */
2145 /* Keep next fields */
2156 obj_string_patch(f, sym->secidx, loc - contents, str);
2157 loc += tgt_sizeof_char_p;
2159 /* Array of chars (in fact, matrix !) */
2160 unsigned long charssize; /* size of each member */
2162 /* Get the size of each member */
2163 /* Probably we should do that outside the loop ? */
2164 if (!isdigit(*(p + 1))) {
2165 error_msg("parameter type 'c' for %s must be followed by"
2166 " the maximum size", key);
2169 charssize = strtoul(p + 1, (char **) NULL, 10);
2172 if (strlen(str) >= charssize) {
2173 error_msg("string too long for %s (max %ld)", key,
2178 /* Copy to location */
2179 strcpy((char *) loc, str);
2183 long v = strtoul(q, &q, 0);
2190 loc += tgt_sizeof_short;
2194 loc += tgt_sizeof_int;
2198 loc += tgt_sizeof_long;
2202 error_msg("unknown parameter type '%c' for %s", *p, key);
2217 goto retry_end_of_value;
2221 error_msg("too many values for %s (max %d)", key, max);
2228 error_msg("invalid argument syntax for %s", key);
2235 error_msg("too few values for %s (min %d)", key, min);
2245 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2246 static int new_is_module_checksummed(struct obj_file *f)
2248 const char *p = get_modinfo_value(f, "using_checksums");
2255 /* Get the module's kernel version in the canonical integer form. */
2258 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2263 p = get_modinfo_value(f, "kernel_version");
2266 strncpy(str, p, STRVERSIONLEN);
2268 a = strtoul(p, &p, 10);
2271 b = strtoul(p + 1, &p, 10);
2274 c = strtoul(p + 1, &q, 10);
2278 return a << 16 | b << 8 | c;
2281 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2284 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
2286 /* Fetch the loaded modules, and all currently exported symbols. */
2288 static int new_get_kernel_symbols(void)
2290 char *module_names, *mn;
2291 struct external_module *modules, *m;
2292 struct new_module_symbol *syms, *s;
2293 size_t ret, bufsize, nmod, nsyms, i, j;
2295 /* Collect the loaded modules. */
2297 module_names = xmalloc(bufsize = 256);
2299 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2300 if (errno == ENOSPC) {
2301 module_names = xrealloc(module_names, bufsize = ret);
2302 goto retry_modules_load;
2304 perror_msg("QM_MODULES");
2308 n_ext_modules = nmod = ret;
2309 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2310 memset(modules, 0, nmod * sizeof(*modules));
2312 /* Collect the modules' symbols. */
2314 for (i = 0, mn = module_names, m = modules;
2315 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2316 struct new_module_info info;
2318 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2319 if (errno == ENOENT) {
2320 /* The module was removed out from underneath us. */
2323 perror_msg("query_module: QM_INFO: %s", mn);
2327 syms = xmalloc(bufsize = 1024);
2329 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2332 syms = xrealloc(syms, bufsize = ret);
2333 goto retry_mod_sym_load;
2335 /* The module was removed out from underneath us. */
2338 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2345 m->addr = info.addr;
2349 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2350 s->name += (unsigned long) syms;
2354 /* Collect the kernel's symbols. */
2356 syms = xmalloc(bufsize = 16 * 1024);
2357 retry_kern_sym_load:
2358 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2359 if (errno == ENOSPC) {
2360 syms = xrealloc(syms, bufsize = ret);
2361 goto retry_kern_sym_load;
2363 perror_msg("kernel: QM_SYMBOLS");
2366 nksyms = nsyms = ret;
2369 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2370 s->name += (unsigned long) syms;
2376 /* Return the kernel symbol checksum version, or zero if not used. */
2378 static int new_is_kernel_checksummed(void)
2380 struct new_module_symbol *s;
2383 /* Using_Versions is not the first symbol, but it should be in there. */
2385 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2386 if (strcmp((char *) s->name, "Using_Versions") == 0)
2393 static int new_create_this_module(struct obj_file *f, const char *m_name)
2395 struct obj_section *sec;
2397 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2398 sizeof(struct new_module));
2399 memset(sec->contents, 0, sizeof(struct new_module));
2401 obj_add_symbol(f, "__this_module", -1,
2402 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2403 sizeof(struct new_module));
2405 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2412 static int new_create_module_ksymtab(struct obj_file *f)
2414 struct obj_section *sec;
2417 /* We must always add the module references. */
2419 if (n_ext_modules_used) {
2420 struct new_module_ref *dep;
2421 struct obj_symbol *tm;
2423 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2424 (sizeof(struct new_module_ref)
2425 * n_ext_modules_used));
2429 tm = obj_find_symbol(f, "__this_module");
2430 dep = (struct new_module_ref *) sec->contents;
2431 for (i = 0; i < n_ext_modules; ++i)
2432 if (ext_modules[i].used) {
2433 dep->dep = ext_modules[i].addr;
2434 obj_symbol_patch(f, sec->idx,
2435 (char *) &dep->ref - sec->contents, tm);
2441 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2446 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2449 /* We don't want to export symbols residing in sections that
2450 aren't loaded. There are a number of these created so that
2451 we make sure certain module options don't appear twice. */
2453 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2455 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2457 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2458 struct obj_symbol *sym;
2459 for (sym = f->symtab[i]; sym; sym = sym->next)
2460 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2461 && sym->secidx <= SHN_HIRESERVE
2462 && (sym->secidx >= SHN_LORESERVE
2463 || loaded[sym->secidx])) {
2464 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2466 obj_symbol_patch(f, sec->idx, ofs, sym);
2467 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2474 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2482 new_init_module(const char *m_name, struct obj_file *f,
2483 unsigned long m_size)
2485 struct new_module *module;
2486 struct obj_section *sec;
2491 sec = obj_find_section(f, ".this");
2492 module = (struct new_module *) sec->contents;
2493 m_addr = sec->header.sh_addr;
2495 module->size_of_struct = sizeof(*module);
2496 module->size = m_size;
2497 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2499 sec = obj_find_section(f, "__ksymtab");
2500 if (sec && sec->header.sh_size) {
2501 module->syms = sec->header.sh_addr;
2502 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2505 if (n_ext_modules_used) {
2506 sec = obj_find_section(f, ".kmodtab");
2507 module->deps = sec->header.sh_addr;
2508 module->ndeps = n_ext_modules_used;
2512 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2514 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2516 sec = obj_find_section(f, "__ex_table");
2518 module->ex_table_start = sec->header.sh_addr;
2519 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2522 sec = obj_find_section(f, ".text.init");
2524 module->runsize = sec->header.sh_addr - m_addr;
2526 sec = obj_find_section(f, ".data.init");
2528 if (!module->runsize ||
2529 module->runsize > sec->header.sh_addr - m_addr)
2530 module->runsize = sec->header.sh_addr - m_addr;
2533 if (!arch_init_module(f, module))
2536 /* Whew! All of the initialization is complete. Collect the final
2537 module image and give it to the kernel. */
2539 image = xmalloc(m_size);
2540 obj_create_image(f, image);
2542 ret = new_sys_init_module(m_name, (struct new_module *) image);
2544 perror_msg("init_module: %s", m_name);
2553 #define new_init_module(x, y, z) TRUE
2554 #define new_create_this_module(x, y) 0
2555 #define new_create_module_ksymtab(x)
2556 #define query_module(v, w, x, y, z) -1
2558 #endif /* BB_FEATURE_NEW_MODULE_INTERFACE */
2561 /*======================================================================*/
2564 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2567 struct obj_string_patch *p;
2568 struct obj_section *strsec;
2569 size_t len = strlen(string) + 1;
2572 p = xmalloc(sizeof(*p));
2573 p->next = f->string_patches;
2574 p->reloc_secidx = secidx;
2575 p->reloc_offset = offset;
2576 f->string_patches = p;
2578 strsec = obj_find_section(f, ".kstrtab");
2579 if (strsec == NULL) {
2580 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2581 p->string_offset = 0;
2582 loc = strsec->contents;
2584 p->string_offset = strsec->header.sh_size;
2585 loc = obj_extend_section(strsec, len);
2587 memcpy(loc, string, len);
2593 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2594 struct obj_symbol *sym)
2596 struct obj_symbol_patch *p;
2598 p = xmalloc(sizeof(*p));
2599 p->next = f->symbol_patches;
2600 p->reloc_secidx = secidx;
2601 p->reloc_offset = offset;
2603 f->symbol_patches = p;
2608 int obj_check_undefineds(struct obj_file *f)
2613 for (i = 0; i < HASH_BUCKETS; ++i) {
2614 struct obj_symbol *sym;
2615 for (sym = f->symtab[i]; sym; sym = sym->next)
2616 if (sym->secidx == SHN_UNDEF) {
2617 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2618 sym->secidx = SHN_ABS;
2621 error_msg("unresolved symbol %s", sym->name);
2630 void obj_allocate_commons(struct obj_file *f)
2632 struct common_entry {
2633 struct common_entry *next;
2634 struct obj_symbol *sym;
2635 } *common_head = NULL;
2639 for (i = 0; i < HASH_BUCKETS; ++i) {
2640 struct obj_symbol *sym;
2641 for (sym = f->symtab[i]; sym; sym = sym->next)
2642 if (sym->secidx == SHN_COMMON) {
2643 /* Collect all COMMON symbols and sort them by size so as to
2644 minimize space wasted by alignment requirements. */
2646 struct common_entry **p, *n;
2647 for (p = &common_head; *p; p = &(*p)->next)
2648 if (sym->size <= (*p)->sym->size)
2651 n = alloca(sizeof(*n));
2659 for (i = 1; i < f->local_symtab_size; ++i) {
2660 struct obj_symbol *sym = f->local_symtab[i];
2661 if (sym && sym->secidx == SHN_COMMON) {
2662 struct common_entry **p, *n;
2663 for (p = &common_head; *p; p = &(*p)->next)
2664 if (sym == (*p)->sym)
2666 else if (sym->size < (*p)->sym->size) {
2667 n = alloca(sizeof(*n));
2677 /* Find the bss section. */
2678 for (i = 0; i < f->header.e_shnum; ++i)
2679 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2682 /* If for some reason there hadn't been one, create one. */
2683 if (i == f->header.e_shnum) {
2684 struct obj_section *sec;
2686 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2687 f->sections[i] = sec = arch_new_section();
2688 f->header.e_shnum = i + 1;
2690 memset(sec, 0, sizeof(*sec));
2691 sec->header.sh_type = SHT_PROGBITS;
2692 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2697 /* Allocate the COMMONS. */
2699 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2700 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2701 struct common_entry *c;
2703 for (c = common_head; c; c = c->next) {
2704 ElfW(Addr) align = c->sym->value;
2706 if (align > max_align)
2708 if (bss_size & (align - 1))
2709 bss_size = (bss_size | (align - 1)) + 1;
2712 c->sym->value = bss_size;
2714 bss_size += c->sym->size;
2717 f->sections[i]->header.sh_size = bss_size;
2718 f->sections[i]->header.sh_addralign = max_align;
2722 /* For the sake of patch relocation and parameter initialization,
2723 allocate zeroed data for NOBITS sections now. Note that after
2724 this we cannot assume NOBITS are really empty. */
2725 for (i = 0; i < f->header.e_shnum; ++i) {
2726 struct obj_section *s = f->sections[i];
2727 if (s->header.sh_type == SHT_NOBITS) {
2728 if (s->header.sh_size != 0)
2729 s->contents = memset(xmalloc(s->header.sh_size),
2730 0, s->header.sh_size);
2734 s->header.sh_type = SHT_PROGBITS;
2739 unsigned long obj_load_size(struct obj_file *f)
2741 unsigned long dot = 0;
2742 struct obj_section *sec;
2744 /* Finalize the positions of the sections relative to one another. */
2746 for (sec = f->load_order; sec; sec = sec->load_next) {
2749 align = sec->header.sh_addralign;
2750 if (align && (dot & (align - 1)))
2751 dot = (dot | (align - 1)) + 1;
2753 sec->header.sh_addr = dot;
2754 dot += sec->header.sh_size;
2760 int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2762 int i, n = f->header.e_shnum;
2765 /* Finalize the addresses of the sections. */
2768 for (i = 0; i < n; ++i)
2769 f->sections[i]->header.sh_addr += base;
2771 /* And iterate over all of the relocations. */
2773 for (i = 0; i < n; ++i) {
2774 struct obj_section *relsec, *symsec, *targsec, *strsec;
2775 ElfW(RelM) * rel, *relend;
2779 relsec = f->sections[i];
2780 if (relsec->header.sh_type != SHT_RELM)
2783 symsec = f->sections[relsec->header.sh_link];
2784 targsec = f->sections[relsec->header.sh_info];
2785 strsec = f->sections[symsec->header.sh_link];
2787 rel = (ElfW(RelM) *) relsec->contents;
2788 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2789 symtab = (ElfW(Sym) *) symsec->contents;
2790 strtab = (const char *) strsec->contents;
2792 for (; rel < relend; ++rel) {
2793 ElfW(Addr) value = 0;
2794 struct obj_symbol *intsym = NULL;
2795 unsigned long symndx;
2796 ElfW(Sym) * extsym = 0;
2799 /* Attempt to find a value to use for this relocation. */
2801 symndx = ELFW(R_SYM) (rel->r_info);
2803 /* Note we've already checked for undefined symbols. */
2805 extsym = &symtab[symndx];
2806 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2807 /* Local symbols we look up in the local table to be sure
2808 we get the one that is really intended. */
2809 intsym = f->local_symtab[symndx];
2811 /* Others we look up in the hash table. */
2813 if (extsym->st_name)
2814 name = strtab + extsym->st_name;
2816 name = f->sections[extsym->st_shndx]->name;
2817 intsym = obj_find_symbol(f, name);
2820 value = obj_symbol_final_value(f, intsym);
2821 intsym->referenced = 1;
2823 #if SHT_RELM == SHT_RELA
2824 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2825 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2826 if (!extsym || !extsym->st_name ||
2827 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2829 value += rel->r_addend;
2833 switch (arch_apply_relocation
2834 (f, targsec, symsec, intsym, rel, value)) {
2838 case obj_reloc_overflow:
2839 errmsg = "Relocation overflow";
2841 case obj_reloc_dangerous:
2842 errmsg = "Dangerous relocation";
2844 case obj_reloc_unhandled:
2845 errmsg = "Unhandled relocation";
2848 error_msg("%s of type %ld for %s", errmsg,
2849 (long) ELFW(R_TYPE) (rel->r_info),
2850 strtab + extsym->st_name);
2852 error_msg("%s of type %ld", errmsg,
2853 (long) ELFW(R_TYPE) (rel->r_info));
2861 /* Finally, take care of the patches. */
2863 if (f->string_patches) {
2864 struct obj_string_patch *p;
2865 struct obj_section *strsec;
2866 ElfW(Addr) strsec_base;
2867 strsec = obj_find_section(f, ".kstrtab");
2868 strsec_base = strsec->header.sh_addr;
2870 for (p = f->string_patches; p; p = p->next) {
2871 struct obj_section *targsec = f->sections[p->reloc_secidx];
2872 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2873 = strsec_base + p->string_offset;
2877 if (f->symbol_patches) {
2878 struct obj_symbol_patch *p;
2880 for (p = f->symbol_patches; p; p = p->next) {
2881 struct obj_section *targsec = f->sections[p->reloc_secidx];
2882 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2883 = obj_symbol_final_value(f, p->sym);
2890 int obj_create_image(struct obj_file *f, char *image)
2892 struct obj_section *sec;
2893 ElfW(Addr) base = f->baseaddr;
2895 for (sec = f->load_order; sec; sec = sec->load_next) {
2898 if (sec->contents == 0 || sec->header.sh_size == 0)
2901 secimg = image + (sec->header.sh_addr - base);
2903 /* Note that we allocated data for NOBITS sections earlier. */
2904 memcpy(secimg, sec->contents, sec->header.sh_size);
2910 /*======================================================================*/
2912 struct obj_file *obj_load(FILE * fp)
2915 ElfW(Shdr) * section_headers;
2919 /* Read the file header. */
2921 f = arch_new_file();
2922 memset(f, 0, sizeof(*f));
2923 f->symbol_cmp = strcmp;
2924 f->symbol_hash = obj_elf_hash;
2925 f->load_order_search_start = &f->load_order;
2927 fseek(fp, 0, SEEK_SET);
2928 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2929 perror_msg("error reading ELF header");
2933 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2934 || f->header.e_ident[EI_MAG1] != ELFMAG1
2935 || f->header.e_ident[EI_MAG2] != ELFMAG2
2936 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2937 error_msg("not an ELF file");
2940 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2941 || f->header.e_ident[EI_DATA] != ELFDATAM
2942 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2943 || !MATCH_MACHINE(f->header.e_machine)) {
2944 error_msg("ELF file not for this architecture");
2947 if (f->header.e_type != ET_REL) {
2948 error_msg("ELF file not a relocatable object");
2952 /* Read the section headers. */
2954 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
2955 error_msg("section header size mismatch: %lu != %lu",
2956 (unsigned long) f->header.e_shentsize,
2957 (unsigned long) sizeof(ElfW(Shdr)));
2961 shnum = f->header.e_shnum;
2962 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
2963 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
2965 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
2966 fseek(fp, f->header.e_shoff, SEEK_SET);
2967 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
2968 perror_msg("error reading ELF section headers");
2972 /* Read the section data. */
2974 for (i = 0; i < shnum; ++i) {
2975 struct obj_section *sec;
2977 f->sections[i] = sec = arch_new_section();
2978 memset(sec, 0, sizeof(*sec));
2980 sec->header = section_headers[i];
2983 if(sec->header.sh_size) switch (sec->header.sh_type) {
2994 if (sec->header.sh_size > 0) {
2995 sec->contents = xmalloc(sec->header.sh_size);
2996 fseek(fp, sec->header.sh_offset, SEEK_SET);
2997 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
2998 perror_msg("error reading ELF section data");
3002 sec->contents = NULL;
3006 #if SHT_RELM == SHT_REL
3008 error_msg("RELA relocations not supported on this architecture");
3012 error_msg("REL relocations not supported on this architecture");
3017 if (sec->header.sh_type >= SHT_LOPROC) {
3018 /* Assume processor specific section types are debug
3019 info and can safely be ignored. If this is ever not
3020 the case (Hello MIPS?), don't put ifdefs here but
3021 create an arch_load_proc_section(). */
3025 error_msg("can't handle sections of type %ld",
3026 (long) sec->header.sh_type);
3031 /* Do what sort of interpretation as needed by each section. */
3033 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3035 for (i = 0; i < shnum; ++i) {
3036 struct obj_section *sec = f->sections[i];
3037 sec->name = shstrtab + sec->header.sh_name;
3040 for (i = 0; i < shnum; ++i) {
3041 struct obj_section *sec = f->sections[i];
3043 if (sec->header.sh_flags & SHF_ALLOC)
3044 obj_insert_section_load_order(f, sec);
3046 switch (sec->header.sh_type) {
3049 unsigned long nsym, j;
3053 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3054 error_msg("symbol size mismatch: %lu != %lu",
3055 (unsigned long) sec->header.sh_entsize,
3056 (unsigned long) sizeof(ElfW(Sym)));
3060 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3061 strtab = f->sections[sec->header.sh_link]->contents;
3062 sym = (ElfW(Sym) *) sec->contents;
3064 /* Allocate space for a table of local symbols. */
3065 j = f->local_symtab_size = sec->header.sh_info;
3066 f->local_symtab = xmalloc(j *=
3067 sizeof(struct obj_symbol *));
3068 memset(f->local_symtab, 0, j);
3070 /* Insert all symbols into the hash table. */
3071 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3074 name = strtab + sym->st_name;
3076 name = f->sections[sym->st_shndx]->name;
3078 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3079 sym->st_value, sym->st_size);
3085 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3086 error_msg("relocation entry size mismatch: %lu != %lu",
3087 (unsigned long) sec->header.sh_entsize,
3088 (unsigned long) sizeof(ElfW(RelM)));
3098 static void hide_special_symbols(struct obj_file *f)
3100 static const char *const specials[] = {
3107 struct obj_symbol *sym;
3108 const char *const *p;
3110 for (p = specials; *p; ++p)
3111 if ((sym = obj_find_symbol(f, *p)) != NULL)
3113 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3118 extern int insmod_main( int argc, char **argv)
3125 unsigned long m_size;
3130 char m_name[BUFSIZ + 1] = "\0";
3131 int exit_status = EXIT_FAILURE;
3133 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3135 char k_strversion[STRVERSIONLEN];
3136 char m_strversion[STRVERSIONLEN];
3141 /* Parse any options */
3142 while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
3144 case 'f': /* force loading */
3145 flag_force_load = 1;
3147 case 'k': /* module loaded by kerneld, auto-cleanable */
3150 case 'v': /* verbose output */
3153 case 'x': /* do not export externs */
3156 case 'o': /* name the output module */
3157 strncpy(m_name, optarg, BUFSIZ);
3159 case 'L': /* Stub warning */
3160 /* This is needed for compatibility with modprobe.
3161 * In theory, this does locking, but we don't do
3162 * that. So be careful and plan your life around not
3163 * loading the same module 50 times concurrently. */
3170 if (argv[optind] == NULL) {
3174 /* Grab the module name */
3175 if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3182 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3184 strncpy(m_fullName, tmp, len);
3185 if (*m_name == '\0') {
3186 strcpy(m_name, m_fullName);
3188 strcat(m_fullName, ".o");
3190 /* Get a filedesc for the module */
3191 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3192 (fp = fopen(argv[optind], "r")) == NULL) {
3193 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
3194 if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE,
3195 findNamedModule, 0, m_fullName) == FALSE)
3197 if (m_filename[0] == '\0'
3198 || ((fp = fopen(m_filename, "r")) == NULL))
3200 error_msg("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
3201 return EXIT_FAILURE;
3204 error_msg_and_die("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
3206 memcpy(m_filename, argv[optind], strlen(argv[optind]));
3209 if ((f = obj_load(fp)) == NULL)
3210 perror_msg_and_die("Could not load the module");
3212 if (get_modinfo_value(f, "kernel_version") == NULL)
3217 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3218 /* Version correspondence? */
3220 k_version = get_kernel_version(k_strversion);
3221 if (m_has_modinfo) {
3222 m_version = new_get_module_version(f, m_strversion);
3224 m_version = old_get_module_version(f, m_strversion);
3225 if (m_version == -1) {
3226 error_msg("couldn't find the kernel version the module was "
3232 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
3233 if (flag_force_load) {
3234 error_msg("Warning: kernel-module version mismatch\n"
3235 "\t%s was compiled for kernel version %s\n"
3236 "\twhile this kernel is version %s",
3237 m_filename, m_strversion, k_strversion);
3239 error_msg("kernel-module version mismatch\n"
3240 "\t%s was compiled for kernel version %s\n"
3241 "\twhile this kernel is version %s.",
3242 m_filename, m_strversion, k_strversion);
3247 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3249 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3251 if (k_new_syscalls) {
3252 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
3253 if (!new_get_kernel_symbols())
3255 k_crcs = new_is_kernel_checksummed();
3257 error_msg("Not configured to support new kernels");
3261 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
3262 if (!old_get_kernel_symbols(m_name))
3264 k_crcs = old_is_kernel_checksummed();
3266 error_msg("Not configured to support old kernels");
3271 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3273 m_crcs = new_is_module_checksummed(f);
3275 m_crcs = old_is_module_checksummed(f);
3277 if (m_crcs != k_crcs)
3278 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3279 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3281 /* Let the module know about the kernel symbols. */
3282 add_kernel_symbols(f);
3284 /* Allocate common symbols, symbol tables, and string tables. */
3287 ? !new_create_this_module(f, m_name)
3288 : !old_create_mod_use_count(f))
3293 if (!obj_check_undefineds(f)) {
3296 obj_allocate_commons(f);
3298 /* done with the module name, on to the optional var=value arguments */
3301 if (optind < argc) {
3303 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3304 : !old_process_module_arguments(f, argc - optind, argv + optind))
3311 hide_special_symbols(f);
3314 new_create_module_ksymtab(f);
3316 /* Find current size of the module */
3317 m_size = obj_load_size(f);
3320 m_addr = create_module(m_name, m_size);
3321 if (m_addr==-1) switch (errno) {
3323 error_msg("A module named %s already exists", m_name);
3326 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3330 perror_msg("create_module: %s", m_name);
3334 if (!obj_relocate(f, m_addr)) {
3335 delete_module(m_name);
3340 ? !new_init_module(m_name, f, m_size)
3341 : !old_init_module(m_name, f, m_size))
3343 delete_module(m_name);
3347 exit_status = EXIT_SUCCESS;
3351 return(exit_status);