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.54 2001/04/05 03:14:39 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.54 2001/04/05 03:14:39 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 /* Account for ELF spec changes. */
377 #ifndef EM_MIPS_RS3_LE
378 #ifdef EM_MIPS_RS4_BE
379 #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
381 #define EM_MIPS_RS3_LE 10
383 #endif /* !EM_MIPS_RS3_LE */
385 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
386 #define SHT_RELM SHT_REL
387 #define Elf32_RelM Elf32_Rel
389 #define ELFDATAM ELFDATA2MSB
392 #define ELFDATAM ELFDATA2LSB
395 #elif defined(__i386__)
397 /* presumably we can use these for anything but the SH and ARM*/
398 /* this is the previous behavior, but it does result in
399 insmod.c being broken on anything except i386 */
401 #define MATCH_MACHINE(x) (x == EM_386)
403 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
406 #define SHT_RELM SHT_REL
407 #define Elf32_RelM Elf32_Rel
408 #define ELFDATAM ELFDATA2LSB
411 #error Sorry, but insmod.c does not yet support this architecture...
415 # if ELFCLASSM == ELFCLASS32
416 # define ElfW(x) Elf32_ ## x
417 # define ELFW(x) ELF32_ ## x
419 # define ElfW(x) Elf64_ ## x
420 # define ELFW(x) ELF64_ ## x
424 /* For some reason this is missing from libc5. */
425 #ifndef ELF32_ST_INFO
426 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
429 #ifndef ELF64_ST_INFO
430 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
433 struct obj_string_patch;
434 struct obj_symbol_patch;
441 struct obj_section *load_next;
447 struct obj_symbol *next; /* hash table link */
451 int secidx; /* the defining section index/module */
453 int ksymidx; /* for export to the kernel symtab */
454 int referenced; /* actually used in the link */
457 /* Hardcode the hash table size. We shouldn't be needing so many
458 symbols that we begin to degrade performance, and we get a big win
459 by giving the compiler a constant divisor. */
461 #define HASH_BUCKETS 521
467 struct obj_section **sections;
468 struct obj_section *load_order;
469 struct obj_section **load_order_search_start;
470 struct obj_string_patch *string_patches;
471 struct obj_symbol_patch *symbol_patches;
472 int (*symbol_cmp)(const char *, const char *);
473 unsigned long (*symbol_hash)(const char *);
474 unsigned long local_symtab_size;
475 struct obj_symbol **local_symtab;
476 struct obj_symbol *symtab[HASH_BUCKETS];
487 struct obj_string_patch
489 struct obj_string_patch *next;
491 ElfW(Addr) reloc_offset;
492 ElfW(Addr) string_offset;
495 struct obj_symbol_patch
497 struct obj_symbol_patch *next;
499 ElfW(Addr) reloc_offset;
500 struct obj_symbol *sym;
504 /* Generic object manipulation routines. */
506 unsigned long obj_elf_hash(const char *);
508 unsigned long obj_elf_hash_n(const char *, unsigned long len);
510 struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
511 unsigned long symidx, int info, int secidx,
512 ElfW(Addr) value, unsigned long size);
514 struct obj_symbol *obj_find_symbol (struct obj_file *f,
517 ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
518 struct obj_symbol *sym);
520 void obj_set_symbol_compare(struct obj_file *f,
521 int (*cmp)(const char *, const char *),
522 unsigned long (*hash)(const char *));
524 struct obj_section *obj_find_section (struct obj_file *f,
527 void obj_insert_section_load_order (struct obj_file *f,
528 struct obj_section *sec);
530 struct obj_section *obj_create_alloced_section (struct obj_file *f,
535 struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
540 void *obj_extend_section (struct obj_section *sec, unsigned long more);
542 int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
545 int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
546 struct obj_symbol *sym);
548 int obj_check_undefineds(struct obj_file *f);
550 void obj_allocate_commons(struct obj_file *f);
552 unsigned long obj_load_size (struct obj_file *f);
554 int obj_relocate (struct obj_file *f, ElfW(Addr) base);
556 struct obj_file *obj_load(FILE *f);
558 int obj_create_image (struct obj_file *f, char *image);
560 /* Architecture specific manipulation routines. */
562 struct obj_file *arch_new_file (void);
564 struct obj_section *arch_new_section (void);
566 struct obj_symbol *arch_new_symbol (void);
568 enum obj_reloc arch_apply_relocation (struct obj_file *f,
569 struct obj_section *targsec,
570 struct obj_section *symsec,
571 struct obj_symbol *sym,
572 ElfW(RelM) *rel, ElfW(Addr) value);
574 int arch_create_got (struct obj_file *f);
577 int arch_init_module (struct obj_file *f, struct new_module *);
580 //----------------------------------------------------------------------------
581 //--------end of modutils obj.h
582 //----------------------------------------------------------------------------
588 #define _PATH_MODULES "/lib/modules"
589 static const int STRVERSIONLEN = 32;
591 /*======================================================================*/
593 int flag_force_load = 0;
594 int flag_autoclean = 0;
595 int flag_verbose = 0;
599 /*======================================================================*/
601 /* previously, these were named i386_* but since we could be
602 compiling for the sh, I've renamed them to the more general
603 arch_* These structures are the same between the x86 and SH,
604 and we can't support anything else right now anyway. In the
605 future maybe they should be #if defined'd */
611 #if defined(BB_USE_PLT_ENTRIES)
612 struct arch_plt_entry
616 int inited:1; /* has been set up */
620 #if defined(BB_USE_GOT_ENTRIES)
621 struct arch_got_entry {
623 unsigned offset_done:1;
624 unsigned reloc_done:1;
628 #if defined(__mips__)
631 struct mips_hi16 *next;
638 struct obj_file root;
639 #if defined(BB_USE_PLT_ENTRIES)
640 struct obj_section *plt;
642 #if defined(BB_USE_GOT_ENTRIES)
643 struct obj_section *got;
645 #if defined(__mips__)
646 struct mips_hi16 *mips_hi16_list;
651 struct obj_symbol root;
652 #if defined(BB_USE_PLT_ENTRIES)
653 struct arch_plt_entry pltent;
655 #if defined(BB_USE_GOT_ENTRIES)
656 struct arch_got_entry gotent;
661 struct external_module {
666 struct new_module_symbol *syms;
669 struct new_module_symbol *ksyms;
672 struct external_module *ext_modules;
674 int n_ext_modules_used;
678 /* Some firendly syscalls to cheer everyone's day... */
679 #define __NR_new_sys_init_module __NR_init_module
680 _syscall2(int, new_sys_init_module, const char *, name,
681 const struct new_module *, info)
682 #define __NR_old_sys_init_module __NR_init_module
683 _syscall5(int, old_sys_init_module, const char *, name, char *, code,
684 unsigned, codesize, struct old_mod_routines *, routines,
685 struct old_symbol_table *, symtab)
687 _syscall1(int, delete_module, const char *, name)
689 extern int delete_module(const char *);
692 /* This is kind of troublesome. See, we don't actually support
693 the m68k or the arm the same way we support i386 and (now)
694 sh. In doing my SH patch, I just assumed that whatever works
695 for i386 also works for m68k and arm since currently insmod.c
696 does nothing special for them. If this isn't true, the below
697 line is rather misleading IMHO, and someone should either
698 change it or add more proper architecture-dependent support
701 -- Bryan Rittmeyer <bryan@ixiacom.com> */
703 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
704 _syscall1(int, get_kernel_syms, struct old_kernel_sym *, ks)
707 #if defined(__i386__) || defined(__m68k__) || defined(__arm__) \
708 || defined(__powerpc__)
709 /* Jump through hoops to fixup error return codes */
710 #define __NR__create_module __NR_create_module
711 static inline _syscall2(long, _create_module, const char *, name, size_t,
713 unsigned long create_module(const char *name, size_t size)
715 long ret = _create_module(name, size);
717 if (ret == -1 && errno > 125) {
724 _syscall2(unsigned long, create_module, const char *, name, size_t, size)
726 static char m_filename[BUFSIZ + 1];
727 static char m_fullName[BUFSIZ + 1];
729 /*======================================================================*/
732 static int findNamedModule(const char *fileName, struct stat *statbuf,
735 char *fullName = (char *) userDate;
738 if (fullName[0] == '\0')
741 char *tmp = strrchr((char *) fileName, '/');
744 tmp = (char *) fileName;
747 if (check_wildcard_match(tmp, fullName) == TRUE) {
748 /* Stop searching if we find a match */
749 memcpy(m_filename, fileName, strlen(fileName)+1);
757 /*======================================================================*/
759 struct obj_file *arch_new_file(void)
762 f = xmalloc(sizeof(*f));
764 #if defined(BB_USE_PLT_ENTRIES)
767 #if defined(BB_USE_GOT_ENTRIES)
770 #if defined(__mips__)
771 f->mips_hi16_list = NULL;
777 struct obj_section *arch_new_section(void)
779 return xmalloc(sizeof(struct obj_section));
782 struct obj_symbol *arch_new_symbol(void)
784 struct arch_symbol *sym;
785 sym = xmalloc(sizeof(*sym));
787 #if defined(BB_USE_PLT_ENTRIES)
788 memset(&sym->pltent, 0, sizeof(sym->pltent));
790 #if defined(BB_USE_GOT_ENTRIES)
791 memset(&sym->gotent, 0, sizeof(sym->gotent));
798 arch_apply_relocation(struct obj_file *f,
799 struct obj_section *targsec,
800 struct obj_section *symsec,
801 struct obj_symbol *sym,
802 ElfW(RelM) *rel, ElfW(Addr) v)
804 struct arch_file *ifile = (struct arch_file *) f;
805 #if !(defined(__mips__))
806 struct arch_symbol *isym = (struct arch_symbol *) sym;
809 ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
810 ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
811 #if defined(BB_USE_GOT_ENTRIES)
812 ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
814 #if defined(BB_USE_PLT_ENTRIES)
815 ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
816 struct arch_plt_entry *pe;
819 enum obj_reloc ret = obj_reloc_ok;
821 switch (ELF32_R_TYPE(rel->r_info)) {
823 /* even though these constants seem to be the same for
824 the i386 and the sh, we "#if define" them for clarity
825 and in case that ever changes */
828 #elif defined(__arm__)
830 #elif defined(__i386__)
832 #elif defined(__powerpc__)
834 #elif defined(__mips__)
841 #elif defined(__arm__)
843 #elif defined(__i386__)
845 #elif defined(__powerpc__)
847 #elif defined(__mips__)
853 #if defined(__powerpc__)
854 case R_PPC_ADDR16_HA:
855 *(unsigned short *)loc = (v + 0x8000) >> 16;
858 case R_PPC_ADDR16_HI:
859 *(unsigned short *)loc = v >> 16;
862 case R_PPC_ADDR16_LO:
863 *(unsigned short *)loc = v;
867 #if defined(__mips__)
870 ret = obj_reloc_dangerous;
871 if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
872 ret = obj_reloc_overflow;
874 (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
882 /* We cannot relocate this one now because we don't know the value
883 of the carry we need to add. Save the information, and let LO16
884 do the actual relocation. */
885 n = (struct mips_hi16 *) xmalloc(sizeof *n);
888 n->next = ifile->mips_hi16_list;
889 ifile->mips_hi16_list = n;
895 unsigned long insnlo = *loc;
896 Elf32_Addr val, vallo;
898 /* Sign extend the addend we extract from the lo insn. */
899 vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
901 if (ifile->mips_hi16_list != NULL) {
904 l = ifile->mips_hi16_list;
906 struct mips_hi16 *next;
909 /* The value for the HI16 had best be the same. */
910 assert(v == l->value);
912 /* Do the HI16 relocation. Note that we actually don't
913 need to know anything about the LO16 itself, except where
914 to find the low 16 bits of the addend needed by the LO16. */
917 ((insn & 0xffff) << 16) +
921 /* Account for the sign extension that will happen in the
928 insn = (insn & ~0xffff) | val;
936 ifile->mips_hi16_list = NULL;
939 /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
941 insnlo = (insnlo & ~0xffff) | (val & 0xffff);
948 #elif defined(__sh__)
952 #elif defined(__i386__)
957 #elif defined(__powerpc__)
967 #elif defined(__i386__)
970 #if defined(BB_USE_PLT_ENTRIES)
976 #if defined(__powerpc__)
979 /* find the plt entry and initialize it if necessary */
980 assert(isym != NULL);
982 pe = (struct arch_plt_entry*) &isym->pltent;
985 ip = (unsigned long *) (ifile->plt->contents + pe->offset);
987 /* generate some machine code */
990 ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
991 ip[1] = v; /* sym@ */
993 #if defined(__powerpc__)
994 ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
995 ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
996 ip[2] = 0x7d6903a6; /* mtctr r11 */
997 ip[3] = 0x4e800420; /* bctr */
1002 /* relative distance to target */
1004 /* if the target is too far away.... */
1005 if ((int)v < -0x02000000 || (int)v >= 0x02000000) {
1006 /* go via the plt */
1007 v = plt + pe->offset - dot;
1010 ret = obj_reloc_dangerous;
1012 /* merge the offset into the instruction. */
1013 #if defined(__arm__)
1014 /* Convert to words. */
1017 *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
1019 #if defined(__powerpc__)
1020 *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
1023 #endif /* BB_USE_PLT_ENTRIES */
1025 #if defined(__arm__)
1026 #elif defined(__sh__)
1031 #elif defined(__i386__)
1032 case R_386_GLOB_DAT:
1033 case R_386_JMP_SLOT:
1038 #if defined(__arm__)
1039 #elif defined(__sh__)
1041 *loc += f->baseaddr + rel->r_addend;
1043 #elif defined(__i386__)
1044 case R_386_RELATIVE:
1045 *loc += f->baseaddr;
1049 #if defined(BB_USE_GOT_ENTRIES)
1053 #elif defined(__arm__)
1055 #elif defined(__i386__)
1060 *loc += got - dot + rel->r_addend;;
1061 #elif defined(__i386__) || defined(__arm__)
1068 #elif defined(__arm__)
1070 #elif defined(__i386__)
1073 assert(isym != NULL);
1074 /* needs an entry in the .got: set it, once */
1075 if (!isym->gotent.reloc_done) {
1076 isym->gotent.reloc_done = 1;
1077 *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
1079 /* make the reloc with_respect_to_.got */
1081 *loc += isym->gotent.offset + rel->r_addend;
1082 #elif defined(__i386__) || defined(__arm__)
1083 *loc += isym->gotent.offset;
1087 /* address relative to the got */
1090 #elif defined(__arm__)
1092 #elif defined(__i386__)
1099 #endif /* BB_USE_GOT_ENTRIES */
1102 printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
1103 ret = obj_reloc_unhandled;
1110 int arch_create_got(struct obj_file *f)
1112 #if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
1113 struct arch_file *ifile = (struct arch_file *) f;
1115 #if defined(BB_USE_GOT_ENTRIES)
1116 int got_offset = 0, gotneeded = 0;
1118 #if defined(BB_USE_PLT_ENTRIES)
1119 int plt_offset = 0, pltneeded = 0;
1121 struct obj_section *relsec, *symsec, *strsec;
1122 ElfW(RelM) *rel, *relend;
1123 ElfW(Sym) *symtab, *extsym;
1124 const char *strtab, *name;
1125 struct arch_symbol *intsym;
1127 for (i = 0; i < f->header.e_shnum; ++i) {
1128 relsec = f->sections[i];
1129 if (relsec->header.sh_type != SHT_RELM)
1132 symsec = f->sections[relsec->header.sh_link];
1133 strsec = f->sections[symsec->header.sh_link];
1135 rel = (ElfW(RelM) *) relsec->contents;
1136 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
1137 symtab = (ElfW(Sym) *) symsec->contents;
1138 strtab = (const char *) strsec->contents;
1140 for (; rel < relend; ++rel) {
1141 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
1143 switch (ELF32_R_TYPE(rel->r_info)) {
1144 #if defined(__arm__)
1147 #elif defined(__sh__)
1150 #elif defined(__i386__)
1155 #if defined(__powerpc__)
1161 #if defined(__arm__)
1170 if (got_offset == 0)
1172 #elif defined(__sh__)
1176 #elif defined(__i386__)
1186 if (extsym->st_name != 0) {
1187 name = strtab + extsym->st_name;
1189 name = f->sections[extsym->st_shndx]->name;
1191 intsym = (struct arch_symbol *) obj_find_symbol(f, name);
1192 #if defined(BB_USE_GOT_ENTRIES)
1193 if (!intsym->gotent.offset_done) {
1194 intsym->gotent.offset_done = 1;
1195 intsym->gotent.offset = got_offset;
1196 got_offset += BB_GOT_ENTRY_SIZE;
1199 #if defined(BB_USE_PLT_ENTRIES)
1200 if (pltneeded && intsym->pltent.allocated == 0) {
1201 intsym->pltent.allocated = 1;
1202 intsym->pltent.offset = plt_offset;
1203 plt_offset += BB_PLT_ENTRY_SIZE;
1204 intsym->pltent.inited = 0;
1211 #if defined(BB_USE_GOT_ENTRIES)
1213 struct obj_section* myrelsec = obj_find_section(f, ".got");
1216 obj_extend_section(myrelsec, got_offset);
1218 myrelsec = obj_create_alloced_section(f, ".got",
1224 ifile->got = myrelsec;
1228 #if defined(BB_USE_PLT_ENTRIES)
1230 ifile->plt = obj_create_alloced_section(f, ".plt",
1238 int arch_init_module(struct obj_file *f, struct new_module *mod)
1244 /*======================================================================*/
1246 /* Standard ELF hash function. */
1247 inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
1249 unsigned long h = 0;
1256 if ((g = (h & 0xf0000000)) != 0) {
1265 unsigned long obj_elf_hash(const char *name)
1267 return obj_elf_hash_n(name, strlen(name));
1270 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1271 /* Get the kernel version in the canonical integer form. */
1273 static int get_kernel_version(char str[STRVERSIONLEN])
1275 struct utsname uts_info;
1279 if (uname(&uts_info) < 0)
1281 strncpy(str, uts_info.release, STRVERSIONLEN);
1282 p = uts_info.release;
1284 a = strtoul(p, &p, 10);
1287 b = strtoul(p + 1, &p, 10);
1290 c = strtoul(p + 1, &q, 10);
1294 return a << 16 | b << 8 | c;
1297 /* String comparison for non-co-versioned kernel and module. */
1299 static int ncv_strcmp(const char *a, const char *b)
1301 size_t alen = strlen(a), blen = strlen(b);
1303 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
1304 return strncmp(a, b, alen);
1305 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
1306 return strncmp(a, b, blen);
1308 return strcmp(a, b);
1311 /* String hashing for non-co-versioned kernel and module. Here
1312 we are simply forced to drop the crc from the hash. */
1314 static unsigned long ncv_symbol_hash(const char *str)
1316 size_t len = strlen(str);
1317 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
1319 return obj_elf_hash_n(str, len);
1323 obj_set_symbol_compare(struct obj_file *f,
1324 int (*cmp) (const char *, const char *),
1325 unsigned long (*hash) (const char *))
1328 f->symbol_cmp = cmp;
1330 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
1333 f->symbol_hash = hash;
1335 memcpy(tmptab, f->symtab, sizeof(tmptab));
1336 memset(f->symtab, 0, sizeof(f->symtab));
1338 for (i = 0; i < HASH_BUCKETS; ++i)
1339 for (sym = tmptab[i]; sym; sym = next) {
1340 unsigned long h = hash(sym->name) % HASH_BUCKETS;
1342 sym->next = f->symtab[h];
1348 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1351 struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
1352 unsigned long symidx, int info,
1353 int secidx, ElfW(Addr) value,
1356 struct obj_symbol *sym;
1357 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1358 int n_type = ELFW(ST_TYPE) (info);
1359 int n_binding = ELFW(ST_BIND) (info);
1361 for (sym = f->symtab[hash]; sym; sym = sym->next)
1362 if (f->symbol_cmp(sym->name, name) == 0) {
1363 int o_secidx = sym->secidx;
1364 int o_info = sym->info;
1365 int o_type = ELFW(ST_TYPE) (o_info);
1366 int o_binding = ELFW(ST_BIND) (o_info);
1368 /* A redefinition! Is it legal? */
1370 if (secidx == SHN_UNDEF)
1372 else if (o_secidx == SHN_UNDEF)
1374 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1375 /* Cope with local and global symbols of the same name
1376 in the same object file, as might have been created
1377 by ld -r. The only reason locals are now seen at this
1378 level at all is so that we can do semi-sensible things
1381 struct obj_symbol *nsym, **p;
1383 nsym = arch_new_symbol();
1384 nsym->next = sym->next;
1387 /* Excise the old (local) symbol from the hash chain. */
1388 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1392 } else if (n_binding == STB_LOCAL) {
1393 /* Another symbol of the same name has already been defined.
1394 Just add this to the local table. */
1395 sym = arch_new_symbol();
1398 f->local_symtab[symidx] = sym;
1400 } else if (n_binding == STB_WEAK)
1402 else if (o_binding == STB_WEAK)
1404 /* Don't unify COMMON symbols with object types the programmer
1406 else if (secidx == SHN_COMMON
1407 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1409 else if (o_secidx == SHN_COMMON
1410 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1413 /* Don't report an error if the symbol is coming from
1414 the kernel or some external module. */
1415 if (secidx <= SHN_HIRESERVE)
1416 error_msg("%s multiply defined", name);
1421 /* Completely new symbol. */
1422 sym = arch_new_symbol();
1423 sym->next = f->symtab[hash];
1424 f->symtab[hash] = sym;
1427 if (ELFW(ST_BIND) (info) == STB_LOCAL)
1428 f->local_symtab[symidx] = sym;
1434 sym->secidx = secidx;
1440 struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
1442 struct obj_symbol *sym;
1443 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1445 for (sym = f->symtab[hash]; sym; sym = sym->next)
1446 if (f->symbol_cmp(sym->name, name) == 0)
1453 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1456 if (sym->secidx >= SHN_LORESERVE)
1459 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1461 /* As a special case, a NULL sym has value zero. */
1466 struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1468 int i, n = f->header.e_shnum;
1470 for (i = 0; i < n; ++i)
1471 if (strcmp(f->sections[i]->name, name) == 0)
1472 return f->sections[i];
1477 static int obj_load_order_prio(struct obj_section *a)
1479 unsigned long af, ac;
1481 af = a->header.sh_flags;
1484 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1485 strcmp(a->name + 5, ".init"))
1489 if (!(af & SHF_WRITE))
1491 if (af & SHF_EXECINSTR)
1493 if (a->header.sh_type != SHT_NOBITS)
1500 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1502 struct obj_section **p;
1503 int prio = obj_load_order_prio(sec);
1504 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1505 if (obj_load_order_prio(*p) < prio)
1507 sec->load_next = *p;
1511 struct obj_section *obj_create_alloced_section(struct obj_file *f,
1513 unsigned long align,
1516 int newidx = f->header.e_shnum++;
1517 struct obj_section *sec;
1519 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1520 f->sections[newidx] = sec = arch_new_section();
1522 memset(sec, 0, sizeof(*sec));
1523 sec->header.sh_type = SHT_PROGBITS;
1524 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1525 sec->header.sh_size = size;
1526 sec->header.sh_addralign = align;
1530 sec->contents = xmalloc(size);
1532 obj_insert_section_load_order(f, sec);
1537 struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1539 unsigned long align,
1542 int newidx = f->header.e_shnum++;
1543 struct obj_section *sec;
1545 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1546 f->sections[newidx] = sec = arch_new_section();
1548 memset(sec, 0, sizeof(*sec));
1549 sec->header.sh_type = SHT_PROGBITS;
1550 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1551 sec->header.sh_size = size;
1552 sec->header.sh_addralign = align;
1556 sec->contents = xmalloc(size);
1558 sec->load_next = f->load_order;
1559 f->load_order = sec;
1560 if (f->load_order_search_start == &f->load_order)
1561 f->load_order_search_start = &sec->load_next;
1566 void *obj_extend_section(struct obj_section *sec, unsigned long more)
1568 unsigned long oldsize = sec->header.sh_size;
1569 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1570 return sec->contents + oldsize;
1575 /* Conditionally add the symbols from the given symbol set to the
1581 int idx, struct new_module_symbol *syms, size_t nsyms)
1583 struct new_module_symbol *s;
1587 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1589 /* Only add symbols that are already marked external. If we
1590 override locals we may cause problems for argument initialization.
1591 We will also create a false dependency on the module. */
1592 struct obj_symbol *sym;
1594 sym = obj_find_symbol(f, (char *) s->name);
1595 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1596 sym = obj_add_symbol(f, (char *) s->name, -1,
1597 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1599 /* Did our symbol just get installed? If so, mark the
1600 module as "used". */
1601 if (sym->secidx == idx)
1609 static void add_kernel_symbols(struct obj_file *f)
1611 struct external_module *m;
1614 /* Add module symbols first. */
1616 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1618 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1619 m->nsyms)) m->used = 1, ++nused;
1621 n_ext_modules_used = nused;
1623 /* And finally the symbols from the kernel proper. */
1626 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1629 static char *get_modinfo_value(struct obj_file *f, const char *key)
1631 struct obj_section *sec;
1632 char *p, *v, *n, *ep;
1633 size_t klen = strlen(key);
1635 sec = obj_find_section(f, ".modinfo");
1639 ep = p + sec->header.sh_size;
1642 n = strchr(p, '\0');
1644 if (p + klen == v && strncmp(p, key, klen) == 0)
1647 if (p + klen == n && strcmp(p, key) == 0)
1657 /*======================================================================*/
1658 /* Functions relating to module loading in pre 2.1 kernels. */
1661 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1665 struct obj_symbol *sym;
1669 if ((q = strchr(p, '=')) == NULL) {
1675 sym = obj_find_symbol(f, p);
1677 /* Also check that the parameter was not resolved from the kernel. */
1678 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1679 error_msg("symbol for parameter %s not found", p);
1683 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1685 /* Do C quoting if we begin with a ". */
1689 str = alloca(strlen(q));
1690 for (r = str, q++; *q != '"'; ++q, ++r) {
1692 error_msg("improperly terminated string argument for %s", p);
1694 } else if (*q == '\\')
1728 if (q[1] >= '0' && q[1] <= '7') {
1729 c = (c * 8) + *++q - '0';
1730 if (q[1] >= '0' && q[1] <= '7')
1731 c = (c * 8) + *++q - '0';
1744 obj_string_patch(f, sym->secidx, sym->value, str);
1745 } else if (*q >= '0' && *q <= '9') {
1747 *loc++ = strtoul(q, &q, 0);
1748 while (*q++ == ',');
1750 char *contents = f->sections[sym->secidx]->contents;
1751 char *myloc = contents + sym->value;
1752 char *r; /* To search for commas */
1754 /* Break the string with comas */
1755 while ((r = strchr(q, ',')) != (char *) NULL) {
1757 obj_string_patch(f, sym->secidx, myloc - contents, q);
1758 myloc += sizeof(char *);
1763 obj_string_patch(f, sym->secidx, myloc - contents, q);
1772 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1773 static int old_is_module_checksummed(struct obj_file *f)
1775 return obj_find_symbol(f, "Using_Versions") != NULL;
1777 /* Get the module's kernel version in the canonical integer form. */
1780 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1782 struct obj_symbol *sym;
1786 sym = obj_find_symbol(f, "kernel_version");
1790 p = f->sections[sym->secidx]->contents + sym->value;
1791 strncpy(str, p, STRVERSIONLEN);
1793 a = strtoul(p, &p, 10);
1796 b = strtoul(p + 1, &p, 10);
1799 c = strtoul(p + 1, &q, 10);
1803 return a << 16 | b << 8 | c;
1806 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1808 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
1810 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1812 static int old_get_kernel_symbols(const char *m_name)
1814 struct old_kernel_sym *ks, *k;
1815 struct new_module_symbol *s;
1816 struct external_module *mod;
1817 int nks, nms, nmod, i;
1819 nks = get_kernel_syms(NULL);
1821 perror_msg("get_kernel_syms: %s", m_name);
1825 ks = k = xmalloc(nks * sizeof(*ks));
1827 if (get_kernel_syms(ks) != nks) {
1828 perror("inconsistency with get_kernel_syms -- is someone else "
1829 "playing with modules?");
1834 /* Collect the module information. */
1839 while (k->name[0] == '#' && k->name[1]) {
1840 struct old_kernel_sym *k2;
1841 struct new_module_symbol *s;
1843 /* Find out how many symbols this module has. */
1844 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1848 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1849 mod[nmod].name = k->name + 1;
1850 mod[nmod].addr = k->value;
1852 mod[nmod].nsyms = nms;
1853 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1855 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1856 s->name = (unsigned long) k->name;
1857 s->value = k->value;
1864 n_ext_modules = nmod + 1;
1866 /* Now collect the symbols for the kernel proper. */
1868 if (k->name[0] == '#')
1871 nksyms = nms = nks - (k - ks);
1872 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1874 for (i = 0; i < nms; ++i, ++s, ++k) {
1875 s->name = (unsigned long) k->name;
1876 s->value = k->value;
1882 /* Return the kernel symbol checksum version, or zero if not used. */
1884 static int old_is_kernel_checksummed(void)
1886 /* Using_Versions is the first symbol. */
1888 && strcmp((char *) ksyms[0].name,
1889 "Using_Versions") == 0) return ksyms[0].value;
1895 static int old_create_mod_use_count(struct obj_file *f)
1897 struct obj_section *sec;
1899 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1902 obj_add_symbol(f, "mod_use_count_", -1,
1903 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1910 old_init_module(const char *m_name, struct obj_file *f,
1911 unsigned long m_size)
1914 struct old_mod_routines routines;
1915 struct old_symbol_table *symtab;
1918 /* Create the symbol table */
1920 int nsyms = 0, strsize = 0, total;
1922 /* Size things first... */
1925 for (i = 0; i < HASH_BUCKETS; ++i) {
1926 struct obj_symbol *sym;
1927 for (sym = f->symtab[i]; sym; sym = sym->next)
1928 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1929 && sym->secidx <= SHN_HIRESERVE)
1931 sym->ksymidx = nsyms++;
1932 strsize += strlen(sym->name) + 1;
1937 total = (sizeof(struct old_symbol_table)
1938 + nsyms * sizeof(struct old_module_symbol)
1939 + n_ext_modules_used * sizeof(struct old_module_ref)
1941 symtab = xmalloc(total);
1942 symtab->size = total;
1943 symtab->n_symbols = nsyms;
1944 symtab->n_refs = n_ext_modules_used;
1946 if (flag_export && nsyms) {
1947 struct old_module_symbol *ksym;
1951 ksym = symtab->symbol;
1952 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1953 + n_ext_modules_used * sizeof(struct old_module_ref));
1955 for (i = 0; i < HASH_BUCKETS; ++i) {
1956 struct obj_symbol *sym;
1957 for (sym = f->symtab[i]; sym; sym = sym->next)
1958 if (sym->ksymidx >= 0) {
1959 ksym->addr = obj_symbol_final_value(f, sym);
1961 (unsigned long) str - (unsigned long) symtab;
1963 strcpy(str, sym->name);
1964 str += strlen(sym->name) + 1;
1970 if (n_ext_modules_used) {
1971 struct old_module_ref *ref;
1974 ref = (struct old_module_ref *)
1975 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
1977 for (i = 0; i < n_ext_modules; ++i)
1978 if (ext_modules[i].used)
1979 ref++->module = ext_modules[i].addr;
1983 /* Fill in routines. */
1986 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
1988 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
1990 /* Whew! All of the initialization is complete. Collect the final
1991 module image and give it to the kernel. */
1993 image = xmalloc(m_size);
1994 obj_create_image(f, image);
1996 /* image holds the complete relocated module, accounting correctly for
1997 mod_use_count. However the old module kernel support assume that
1998 it is receiving something which does not contain mod_use_count. */
1999 ret = old_sys_init_module(m_name, image + sizeof(long),
2000 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2001 : 0), &routines, symtab);
2003 perror_msg("init_module: %s", m_name);
2013 #define old_create_mod_use_count(x) TRUE
2014 #define old_init_module(x, y, z) TRUE
2016 #endif /* BB_FEATURE_OLD_MODULE_INTERFACE */
2020 /*======================================================================*/
2021 /* Functions relating to module loading after 2.1.18. */
2024 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2028 struct obj_symbol *sym;
2029 char *contents, *loc;
2033 if ((q = strchr(p, '=')) == NULL) {
2038 key = alloca(q - p + 6);
2039 memcpy(key, "parm_", 5);
2040 memcpy(key + 5, p, q - p);
2043 p = get_modinfo_value(f, key);
2046 error_msg("invalid parameter %s", key);
2050 sym = obj_find_symbol(f, key);
2052 /* Also check that the parameter was not resolved from the kernel. */
2053 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2054 error_msg("symbol for parameter %s not found", key);
2059 min = strtoul(p, &p, 10);
2061 max = strtoul(p + 1, &p, 10);
2067 contents = f->sections[sym->secidx]->contents;
2068 loc = contents + sym->value;
2072 if ((*p == 's') || (*p == 'c')) {
2075 /* Do C quoting if we begin with a ", else slurp the lot. */
2079 str = alloca(strlen(q));
2080 for (r = str, q++; *q != '"'; ++q, ++r) {
2082 error_msg("improperly terminated string argument for %s",
2085 } else if (*q == '\\')
2119 if (q[1] >= '0' && q[1] <= '7') {
2120 c = (c * 8) + *++q - '0';
2121 if (q[1] >= '0' && q[1] <= '7')
2122 c = (c * 8) + *++q - '0';
2139 /* In this case, the string is not quoted. We will break
2140 it using the coma (like for ints). If the user wants to
2141 include comas in a string, he just has to quote it */
2143 /* Search the next coma */
2147 if (r != (char *) NULL) {
2148 /* Recopy the current field */
2149 str = alloca(r - q + 1);
2150 memcpy(str, q, r - q);
2152 /* I don't know if it is usefull, as the previous case
2153 doesn't null terminate the string ??? */
2156 /* Keep next fields */
2167 obj_string_patch(f, sym->secidx, loc - contents, str);
2168 loc += tgt_sizeof_char_p;
2170 /* Array of chars (in fact, matrix !) */
2171 unsigned long charssize; /* size of each member */
2173 /* Get the size of each member */
2174 /* Probably we should do that outside the loop ? */
2175 if (!isdigit(*(p + 1))) {
2176 error_msg("parameter type 'c' for %s must be followed by"
2177 " the maximum size", key);
2180 charssize = strtoul(p + 1, (char **) NULL, 10);
2183 if (strlen(str) >= charssize) {
2184 error_msg("string too long for %s (max %ld)", key,
2189 /* Copy to location */
2190 strcpy((char *) loc, str);
2194 long v = strtoul(q, &q, 0);
2201 loc += tgt_sizeof_short;
2205 loc += tgt_sizeof_int;
2209 loc += tgt_sizeof_long;
2213 error_msg("unknown parameter type '%c' for %s", *p, key);
2228 goto retry_end_of_value;
2232 error_msg("too many values for %s (max %d)", key, max);
2239 error_msg("invalid argument syntax for %s", key);
2246 error_msg("too few values for %s (min %d)", key, min);
2256 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2257 static int new_is_module_checksummed(struct obj_file *f)
2259 const char *p = get_modinfo_value(f, "using_checksums");
2266 /* Get the module's kernel version in the canonical integer form. */
2269 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
2274 p = get_modinfo_value(f, "kernel_version");
2277 strncpy(str, p, STRVERSIONLEN);
2279 a = strtoul(p, &p, 10);
2282 b = strtoul(p + 1, &p, 10);
2285 c = strtoul(p + 1, &q, 10);
2289 return a << 16 | b << 8 | c;
2292 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2295 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
2297 /* Fetch the loaded modules, and all currently exported symbols. */
2299 static int new_get_kernel_symbols(void)
2301 char *module_names, *mn;
2302 struct external_module *modules, *m;
2303 struct new_module_symbol *syms, *s;
2304 size_t ret, bufsize, nmod, nsyms, i, j;
2306 /* Collect the loaded modules. */
2308 module_names = xmalloc(bufsize = 256);
2310 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
2311 if (errno == ENOSPC) {
2312 module_names = xrealloc(module_names, bufsize = ret);
2313 goto retry_modules_load;
2315 perror_msg("QM_MODULES");
2319 n_ext_modules = nmod = ret;
2321 /* Collect the modules' symbols. */
2324 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2325 memset(modules, 0, nmod * sizeof(*modules));
2326 for (i = 0, mn = module_names, m = modules;
2327 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2328 struct new_module_info info;
2330 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2331 if (errno == ENOENT) {
2332 /* The module was removed out from underneath us. */
2335 perror_msg("query_module: QM_INFO: %s", mn);
2339 syms = xmalloc(bufsize = 1024);
2341 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2344 syms = xrealloc(syms, bufsize = ret);
2345 goto retry_mod_sym_load;
2347 /* The module was removed out from underneath us. */
2350 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2357 m->addr = info.addr;
2361 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2362 s->name += (unsigned long) syms;
2367 /* Collect the kernel's symbols. */
2369 syms = xmalloc(bufsize = 16 * 1024);
2370 retry_kern_sym_load:
2371 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2372 if (errno == ENOSPC) {
2373 syms = xrealloc(syms, bufsize = ret);
2374 goto retry_kern_sym_load;
2376 perror_msg("kernel: QM_SYMBOLS");
2379 nksyms = nsyms = ret;
2382 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2383 s->name += (unsigned long) syms;
2389 /* Return the kernel symbol checksum version, or zero if not used. */
2391 static int new_is_kernel_checksummed(void)
2393 struct new_module_symbol *s;
2396 /* Using_Versions is not the first symbol, but it should be in there. */
2398 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2399 if (strcmp((char *) s->name, "Using_Versions") == 0)
2406 static int new_create_this_module(struct obj_file *f, const char *m_name)
2408 struct obj_section *sec;
2410 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2411 sizeof(struct new_module));
2412 memset(sec->contents, 0, sizeof(struct new_module));
2414 obj_add_symbol(f, "__this_module", -1,
2415 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2416 sizeof(struct new_module));
2418 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2425 static int new_create_module_ksymtab(struct obj_file *f)
2427 struct obj_section *sec;
2430 /* We must always add the module references. */
2432 if (n_ext_modules_used) {
2433 struct new_module_ref *dep;
2434 struct obj_symbol *tm;
2436 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2437 (sizeof(struct new_module_ref)
2438 * n_ext_modules_used));
2442 tm = obj_find_symbol(f, "__this_module");
2443 dep = (struct new_module_ref *) sec->contents;
2444 for (i = 0; i < n_ext_modules; ++i)
2445 if (ext_modules[i].used) {
2446 dep->dep = ext_modules[i].addr;
2447 obj_symbol_patch(f, sec->idx,
2448 (char *) &dep->ref - sec->contents, tm);
2454 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2459 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2462 /* We don't want to export symbols residing in sections that
2463 aren't loaded. There are a number of these created so that
2464 we make sure certain module options don't appear twice. */
2466 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2468 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2470 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2471 struct obj_symbol *sym;
2472 for (sym = f->symtab[i]; sym; sym = sym->next)
2473 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2474 && sym->secidx <= SHN_HIRESERVE
2475 && (sym->secidx >= SHN_LORESERVE
2476 || loaded[sym->secidx])) {
2477 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2479 obj_symbol_patch(f, sec->idx, ofs, sym);
2480 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2487 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2495 new_init_module(const char *m_name, struct obj_file *f,
2496 unsigned long m_size)
2498 struct new_module *module;
2499 struct obj_section *sec;
2504 sec = obj_find_section(f, ".this");
2505 module = (struct new_module *) sec->contents;
2506 m_addr = sec->header.sh_addr;
2508 module->size_of_struct = sizeof(*module);
2509 module->size = m_size;
2510 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2512 sec = obj_find_section(f, "__ksymtab");
2513 if (sec && sec->header.sh_size) {
2514 module->syms = sec->header.sh_addr;
2515 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2518 if (n_ext_modules_used) {
2519 sec = obj_find_section(f, ".kmodtab");
2520 module->deps = sec->header.sh_addr;
2521 module->ndeps = n_ext_modules_used;
2525 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2527 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2529 sec = obj_find_section(f, "__ex_table");
2531 module->ex_table_start = sec->header.sh_addr;
2532 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2535 sec = obj_find_section(f, ".text.init");
2537 module->runsize = sec->header.sh_addr - m_addr;
2539 sec = obj_find_section(f, ".data.init");
2541 if (!module->runsize ||
2542 module->runsize > sec->header.sh_addr - m_addr)
2543 module->runsize = sec->header.sh_addr - m_addr;
2546 if (!arch_init_module(f, module))
2549 /* Whew! All of the initialization is complete. Collect the final
2550 module image and give it to the kernel. */
2552 image = xmalloc(m_size);
2553 obj_create_image(f, image);
2555 ret = new_sys_init_module(m_name, (struct new_module *) image);
2557 perror_msg("init_module: %s", m_name);
2566 #define new_init_module(x, y, z) TRUE
2567 #define new_create_this_module(x, y) 0
2568 #define new_create_module_ksymtab(x)
2569 #define query_module(v, w, x, y, z) -1
2571 #endif /* BB_FEATURE_NEW_MODULE_INTERFACE */
2574 /*======================================================================*/
2577 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2580 struct obj_string_patch *p;
2581 struct obj_section *strsec;
2582 size_t len = strlen(string) + 1;
2585 p = xmalloc(sizeof(*p));
2586 p->next = f->string_patches;
2587 p->reloc_secidx = secidx;
2588 p->reloc_offset = offset;
2589 f->string_patches = p;
2591 strsec = obj_find_section(f, ".kstrtab");
2592 if (strsec == NULL) {
2593 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2594 p->string_offset = 0;
2595 loc = strsec->contents;
2597 p->string_offset = strsec->header.sh_size;
2598 loc = obj_extend_section(strsec, len);
2600 memcpy(loc, string, len);
2606 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2607 struct obj_symbol *sym)
2609 struct obj_symbol_patch *p;
2611 p = xmalloc(sizeof(*p));
2612 p->next = f->symbol_patches;
2613 p->reloc_secidx = secidx;
2614 p->reloc_offset = offset;
2616 f->symbol_patches = p;
2621 int obj_check_undefineds(struct obj_file *f)
2626 for (i = 0; i < HASH_BUCKETS; ++i) {
2627 struct obj_symbol *sym;
2628 for (sym = f->symtab[i]; sym; sym = sym->next)
2629 if (sym->secidx == SHN_UNDEF) {
2630 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2631 sym->secidx = SHN_ABS;
2634 error_msg("unresolved symbol %s", sym->name);
2643 void obj_allocate_commons(struct obj_file *f)
2645 struct common_entry {
2646 struct common_entry *next;
2647 struct obj_symbol *sym;
2648 } *common_head = NULL;
2652 for (i = 0; i < HASH_BUCKETS; ++i) {
2653 struct obj_symbol *sym;
2654 for (sym = f->symtab[i]; sym; sym = sym->next)
2655 if (sym->secidx == SHN_COMMON) {
2656 /* Collect all COMMON symbols and sort them by size so as to
2657 minimize space wasted by alignment requirements. */
2659 struct common_entry **p, *n;
2660 for (p = &common_head; *p; p = &(*p)->next)
2661 if (sym->size <= (*p)->sym->size)
2664 n = alloca(sizeof(*n));
2672 for (i = 1; i < f->local_symtab_size; ++i) {
2673 struct obj_symbol *sym = f->local_symtab[i];
2674 if (sym && sym->secidx == SHN_COMMON) {
2675 struct common_entry **p, *n;
2676 for (p = &common_head; *p; p = &(*p)->next)
2677 if (sym == (*p)->sym)
2679 else if (sym->size < (*p)->sym->size) {
2680 n = alloca(sizeof(*n));
2690 /* Find the bss section. */
2691 for (i = 0; i < f->header.e_shnum; ++i)
2692 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2695 /* If for some reason there hadn't been one, create one. */
2696 if (i == f->header.e_shnum) {
2697 struct obj_section *sec;
2699 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2700 f->sections[i] = sec = arch_new_section();
2701 f->header.e_shnum = i + 1;
2703 memset(sec, 0, sizeof(*sec));
2704 sec->header.sh_type = SHT_PROGBITS;
2705 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2710 /* Allocate the COMMONS. */
2712 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2713 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2714 struct common_entry *c;
2716 for (c = common_head; c; c = c->next) {
2717 ElfW(Addr) align = c->sym->value;
2719 if (align > max_align)
2721 if (bss_size & (align - 1))
2722 bss_size = (bss_size | (align - 1)) + 1;
2725 c->sym->value = bss_size;
2727 bss_size += c->sym->size;
2730 f->sections[i]->header.sh_size = bss_size;
2731 f->sections[i]->header.sh_addralign = max_align;
2735 /* For the sake of patch relocation and parameter initialization,
2736 allocate zeroed data for NOBITS sections now. Note that after
2737 this we cannot assume NOBITS are really empty. */
2738 for (i = 0; i < f->header.e_shnum; ++i) {
2739 struct obj_section *s = f->sections[i];
2740 if (s->header.sh_type == SHT_NOBITS) {
2741 if (s->header.sh_size != 0)
2742 s->contents = memset(xmalloc(s->header.sh_size),
2743 0, s->header.sh_size);
2747 s->header.sh_type = SHT_PROGBITS;
2752 unsigned long obj_load_size(struct obj_file *f)
2754 unsigned long dot = 0;
2755 struct obj_section *sec;
2757 /* Finalize the positions of the sections relative to one another. */
2759 for (sec = f->load_order; sec; sec = sec->load_next) {
2762 align = sec->header.sh_addralign;
2763 if (align && (dot & (align - 1)))
2764 dot = (dot | (align - 1)) + 1;
2766 sec->header.sh_addr = dot;
2767 dot += sec->header.sh_size;
2773 int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2775 int i, n = f->header.e_shnum;
2778 /* Finalize the addresses of the sections. */
2781 for (i = 0; i < n; ++i)
2782 f->sections[i]->header.sh_addr += base;
2784 /* And iterate over all of the relocations. */
2786 for (i = 0; i < n; ++i) {
2787 struct obj_section *relsec, *symsec, *targsec, *strsec;
2788 ElfW(RelM) * rel, *relend;
2792 relsec = f->sections[i];
2793 if (relsec->header.sh_type != SHT_RELM)
2796 symsec = f->sections[relsec->header.sh_link];
2797 targsec = f->sections[relsec->header.sh_info];
2798 strsec = f->sections[symsec->header.sh_link];
2800 rel = (ElfW(RelM) *) relsec->contents;
2801 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2802 symtab = (ElfW(Sym) *) symsec->contents;
2803 strtab = (const char *) strsec->contents;
2805 for (; rel < relend; ++rel) {
2806 ElfW(Addr) value = 0;
2807 struct obj_symbol *intsym = NULL;
2808 unsigned long symndx;
2809 ElfW(Sym) * extsym = 0;
2812 /* Attempt to find a value to use for this relocation. */
2814 symndx = ELFW(R_SYM) (rel->r_info);
2816 /* Note we've already checked for undefined symbols. */
2818 extsym = &symtab[symndx];
2819 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2820 /* Local symbols we look up in the local table to be sure
2821 we get the one that is really intended. */
2822 intsym = f->local_symtab[symndx];
2824 /* Others we look up in the hash table. */
2826 if (extsym->st_name)
2827 name = strtab + extsym->st_name;
2829 name = f->sections[extsym->st_shndx]->name;
2830 intsym = obj_find_symbol(f, name);
2833 value = obj_symbol_final_value(f, intsym);
2834 intsym->referenced = 1;
2836 #if SHT_RELM == SHT_RELA
2837 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2838 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2839 if (!extsym || !extsym->st_name ||
2840 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2842 value += rel->r_addend;
2846 switch (arch_apply_relocation
2847 (f, targsec, symsec, intsym, rel, value)) {
2851 case obj_reloc_overflow:
2852 errmsg = "Relocation overflow";
2854 case obj_reloc_dangerous:
2855 errmsg = "Dangerous relocation";
2857 case obj_reloc_unhandled:
2858 errmsg = "Unhandled relocation";
2861 error_msg("%s of type %ld for %s", errmsg,
2862 (long) ELFW(R_TYPE) (rel->r_info),
2863 strtab + extsym->st_name);
2865 error_msg("%s of type %ld", errmsg,
2866 (long) ELFW(R_TYPE) (rel->r_info));
2874 /* Finally, take care of the patches. */
2876 if (f->string_patches) {
2877 struct obj_string_patch *p;
2878 struct obj_section *strsec;
2879 ElfW(Addr) strsec_base;
2880 strsec = obj_find_section(f, ".kstrtab");
2881 strsec_base = strsec->header.sh_addr;
2883 for (p = f->string_patches; p; p = p->next) {
2884 struct obj_section *targsec = f->sections[p->reloc_secidx];
2885 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2886 = strsec_base + p->string_offset;
2890 if (f->symbol_patches) {
2891 struct obj_symbol_patch *p;
2893 for (p = f->symbol_patches; p; p = p->next) {
2894 struct obj_section *targsec = f->sections[p->reloc_secidx];
2895 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2896 = obj_symbol_final_value(f, p->sym);
2903 int obj_create_image(struct obj_file *f, char *image)
2905 struct obj_section *sec;
2906 ElfW(Addr) base = f->baseaddr;
2908 for (sec = f->load_order; sec; sec = sec->load_next) {
2911 if (sec->contents == 0 || sec->header.sh_size == 0)
2914 secimg = image + (sec->header.sh_addr - base);
2916 /* Note that we allocated data for NOBITS sections earlier. */
2917 memcpy(secimg, sec->contents, sec->header.sh_size);
2923 /*======================================================================*/
2925 struct obj_file *obj_load(FILE * fp)
2928 ElfW(Shdr) * section_headers;
2932 /* Read the file header. */
2934 f = arch_new_file();
2935 memset(f, 0, sizeof(*f));
2936 f->symbol_cmp = strcmp;
2937 f->symbol_hash = obj_elf_hash;
2938 f->load_order_search_start = &f->load_order;
2940 fseek(fp, 0, SEEK_SET);
2941 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2942 perror_msg("error reading ELF header");
2946 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2947 || f->header.e_ident[EI_MAG1] != ELFMAG1
2948 || f->header.e_ident[EI_MAG2] != ELFMAG2
2949 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2950 error_msg("not an ELF file");
2953 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2954 || f->header.e_ident[EI_DATA] != ELFDATAM
2955 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2956 || !MATCH_MACHINE(f->header.e_machine)) {
2957 error_msg("ELF file not for this architecture");
2960 if (f->header.e_type != ET_REL) {
2961 error_msg("ELF file not a relocatable object");
2965 /* Read the section headers. */
2967 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
2968 error_msg("section header size mismatch: %lu != %lu",
2969 (unsigned long) f->header.e_shentsize,
2970 (unsigned long) sizeof(ElfW(Shdr)));
2974 shnum = f->header.e_shnum;
2975 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
2976 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
2978 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
2979 fseek(fp, f->header.e_shoff, SEEK_SET);
2980 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
2981 perror_msg("error reading ELF section headers");
2985 /* Read the section data. */
2987 for (i = 0; i < shnum; ++i) {
2988 struct obj_section *sec;
2990 f->sections[i] = sec = arch_new_section();
2991 memset(sec, 0, sizeof(*sec));
2993 sec->header = section_headers[i];
2996 if(sec->header.sh_size) switch (sec->header.sh_type) {
3007 if (sec->header.sh_size > 0) {
3008 sec->contents = xmalloc(sec->header.sh_size);
3009 fseek(fp, sec->header.sh_offset, SEEK_SET);
3010 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3011 perror_msg("error reading ELF section data");
3015 sec->contents = NULL;
3019 #if SHT_RELM == SHT_REL
3021 error_msg("RELA relocations not supported on this architecture");
3025 error_msg("REL relocations not supported on this architecture");
3030 if (sec->header.sh_type >= SHT_LOPROC) {
3031 /* Assume processor specific section types are debug
3032 info and can safely be ignored. If this is ever not
3033 the case (Hello MIPS?), don't put ifdefs here but
3034 create an arch_load_proc_section(). */
3038 error_msg("can't handle sections of type %ld",
3039 (long) sec->header.sh_type);
3044 /* Do what sort of interpretation as needed by each section. */
3046 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3048 for (i = 0; i < shnum; ++i) {
3049 struct obj_section *sec = f->sections[i];
3050 sec->name = shstrtab + sec->header.sh_name;
3053 for (i = 0; i < shnum; ++i) {
3054 struct obj_section *sec = f->sections[i];
3056 if (sec->header.sh_flags & SHF_ALLOC)
3057 obj_insert_section_load_order(f, sec);
3059 switch (sec->header.sh_type) {
3062 unsigned long nsym, j;
3066 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3067 error_msg("symbol size mismatch: %lu != %lu",
3068 (unsigned long) sec->header.sh_entsize,
3069 (unsigned long) sizeof(ElfW(Sym)));
3073 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3074 strtab = f->sections[sec->header.sh_link]->contents;
3075 sym = (ElfW(Sym) *) sec->contents;
3077 /* Allocate space for a table of local symbols. */
3078 j = f->local_symtab_size = sec->header.sh_info;
3079 f->local_symtab = xmalloc(j *=
3080 sizeof(struct obj_symbol *));
3081 memset(f->local_symtab, 0, j);
3083 /* Insert all symbols into the hash table. */
3084 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3087 name = strtab + sym->st_name;
3089 name = f->sections[sym->st_shndx]->name;
3091 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3092 sym->st_value, sym->st_size);
3098 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3099 error_msg("relocation entry size mismatch: %lu != %lu",
3100 (unsigned long) sec->header.sh_entsize,
3101 (unsigned long) sizeof(ElfW(RelM)));
3111 static void hide_special_symbols(struct obj_file *f)
3113 static const char *const specials[] = {
3120 struct obj_symbol *sym;
3121 const char *const *p;
3123 for (p = specials; *p; ++p)
3124 if ((sym = obj_find_symbol(f, *p)) != NULL)
3126 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3131 extern int insmod_main( int argc, char **argv)
3138 unsigned long m_size;
3143 char m_name[BUFSIZ + 1] = "\0";
3144 int exit_status = EXIT_FAILURE;
3146 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3148 char k_strversion[STRVERSIONLEN];
3149 char m_strversion[STRVERSIONLEN];
3154 /* Parse any options */
3155 while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
3157 case 'f': /* force loading */
3158 flag_force_load = 1;
3160 case 'k': /* module loaded by kerneld, auto-cleanable */
3163 case 'v': /* verbose output */
3166 case 'x': /* do not export externs */
3169 case 'o': /* name the output module */
3170 strncpy(m_name, optarg, BUFSIZ);
3172 case 'L': /* Stub warning */
3173 /* This is needed for compatibility with modprobe.
3174 * In theory, this does locking, but we don't do
3175 * that. So be careful and plan your life around not
3176 * loading the same module 50 times concurrently. */
3183 if (argv[optind] == NULL) {
3187 /* Grab the module name */
3188 if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3195 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3197 strncpy(m_fullName, tmp, len);
3198 if (*m_name == '\0') {
3199 strcpy(m_name, m_fullName);
3201 strcat(m_fullName, ".o");
3203 /* Get a filedesc for the module */
3204 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3205 (fp = fopen(argv[optind], "r")) == NULL) {
3206 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
3207 if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE,
3208 findNamedModule, 0, m_fullName) == FALSE)
3210 if (m_filename[0] == '\0'
3211 || ((fp = fopen(m_filename, "r")) == NULL))
3213 error_msg("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
3214 return EXIT_FAILURE;
3217 error_msg_and_die("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
3219 memcpy(m_filename, argv[optind], strlen(argv[optind]));
3222 if ((f = obj_load(fp)) == NULL)
3223 perror_msg_and_die("Could not load the module");
3225 if (get_modinfo_value(f, "kernel_version") == NULL)
3230 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3231 /* Version correspondence? */
3233 k_version = get_kernel_version(k_strversion);
3234 if (m_has_modinfo) {
3235 m_version = new_get_module_version(f, m_strversion);
3237 m_version = old_get_module_version(f, m_strversion);
3238 if (m_version == -1) {
3239 error_msg("couldn't find the kernel version the module was "
3245 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
3246 if (flag_force_load) {
3247 error_msg("Warning: kernel-module version mismatch\n"
3248 "\t%s was compiled for kernel version %s\n"
3249 "\twhile this kernel is version %s",
3250 m_filename, m_strversion, k_strversion);
3252 error_msg("kernel-module version mismatch\n"
3253 "\t%s was compiled for kernel version %s\n"
3254 "\twhile this kernel is version %s.",
3255 m_filename, m_strversion, k_strversion);
3260 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3262 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3264 if (k_new_syscalls) {
3265 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
3266 if (!new_get_kernel_symbols())
3268 k_crcs = new_is_kernel_checksummed();
3270 error_msg("Not configured to support new kernels");
3274 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
3275 if (!old_get_kernel_symbols(m_name))
3277 k_crcs = old_is_kernel_checksummed();
3279 error_msg("Not configured to support old kernels");
3284 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3286 m_crcs = new_is_module_checksummed(f);
3288 m_crcs = old_is_module_checksummed(f);
3290 if (m_crcs != k_crcs)
3291 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3292 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3294 /* Let the module know about the kernel symbols. */
3295 add_kernel_symbols(f);
3297 /* Allocate common symbols, symbol tables, and string tables. */
3300 ? !new_create_this_module(f, m_name)
3301 : !old_create_mod_use_count(f))
3306 if (!obj_check_undefineds(f)) {
3309 obj_allocate_commons(f);
3311 /* done with the module name, on to the optional var=value arguments */
3314 if (optind < argc) {
3316 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3317 : !old_process_module_arguments(f, argc - optind, argv + optind))
3324 hide_special_symbols(f);
3327 new_create_module_ksymtab(f);
3329 /* Find current size of the module */
3330 m_size = obj_load_size(f);
3333 m_addr = create_module(m_name, m_size);
3334 if (m_addr==-1) switch (errno) {
3336 error_msg("A module named %s already exists", m_name);
3339 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3343 perror_msg("create_module: %s", m_name);
3347 if (!obj_relocate(f, m_addr)) {
3348 delete_module(m_name);
3353 ? !new_init_module(m_name, f, m_size)
3354 : !old_init_module(m_name, f, m_size))
3356 delete_module(m_name);
3360 exit_status = EXIT_SUCCESS;
3364 return(exit_status);