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.51 2001/03/12 23:08:34 markw 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.51 2001/03/12 23:08:34 markw 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;
2310 /* Collect the modules' symbols. */
2313 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
2314 memset(modules, 0, nmod * sizeof(*modules));
2315 for (i = 0, mn = module_names, m = modules;
2316 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
2317 struct new_module_info info;
2319 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
2320 if (errno == ENOENT) {
2321 /* The module was removed out from underneath us. */
2324 perror_msg("query_module: QM_INFO: %s", mn);
2328 syms = xmalloc(bufsize = 1024);
2330 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
2333 syms = xrealloc(syms, bufsize = ret);
2334 goto retry_mod_sym_load;
2336 /* The module was removed out from underneath us. */
2339 perror_msg("query_module: QM_SYMBOLS: %s", mn);
2346 m->addr = info.addr;
2350 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2351 s->name += (unsigned long) syms;
2356 /* Collect the kernel's symbols. */
2358 syms = xmalloc(bufsize = 16 * 1024);
2359 retry_kern_sym_load:
2360 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2361 if (errno == ENOSPC) {
2362 syms = xrealloc(syms, bufsize = ret);
2363 goto retry_kern_sym_load;
2365 perror_msg("kernel: QM_SYMBOLS");
2368 nksyms = nsyms = ret;
2371 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2372 s->name += (unsigned long) syms;
2378 /* Return the kernel symbol checksum version, or zero if not used. */
2380 static int new_is_kernel_checksummed(void)
2382 struct new_module_symbol *s;
2385 /* Using_Versions is not the first symbol, but it should be in there. */
2387 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2388 if (strcmp((char *) s->name, "Using_Versions") == 0)
2395 static int new_create_this_module(struct obj_file *f, const char *m_name)
2397 struct obj_section *sec;
2399 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2400 sizeof(struct new_module));
2401 memset(sec->contents, 0, sizeof(struct new_module));
2403 obj_add_symbol(f, "__this_module", -1,
2404 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2405 sizeof(struct new_module));
2407 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2414 static int new_create_module_ksymtab(struct obj_file *f)
2416 struct obj_section *sec;
2419 /* We must always add the module references. */
2421 if (n_ext_modules_used) {
2422 struct new_module_ref *dep;
2423 struct obj_symbol *tm;
2425 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2426 (sizeof(struct new_module_ref)
2427 * n_ext_modules_used));
2431 tm = obj_find_symbol(f, "__this_module");
2432 dep = (struct new_module_ref *) sec->contents;
2433 for (i = 0; i < n_ext_modules; ++i)
2434 if (ext_modules[i].used) {
2435 dep->dep = ext_modules[i].addr;
2436 obj_symbol_patch(f, sec->idx,
2437 (char *) &dep->ref - sec->contents, tm);
2443 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2448 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2451 /* We don't want to export symbols residing in sections that
2452 aren't loaded. There are a number of these created so that
2453 we make sure certain module options don't appear twice. */
2455 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2457 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2459 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2460 struct obj_symbol *sym;
2461 for (sym = f->symtab[i]; sym; sym = sym->next)
2462 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2463 && sym->secidx <= SHN_HIRESERVE
2464 && (sym->secidx >= SHN_LORESERVE
2465 || loaded[sym->secidx])) {
2466 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2468 obj_symbol_patch(f, sec->idx, ofs, sym);
2469 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2476 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2484 new_init_module(const char *m_name, struct obj_file *f,
2485 unsigned long m_size)
2487 struct new_module *module;
2488 struct obj_section *sec;
2493 sec = obj_find_section(f, ".this");
2494 module = (struct new_module *) sec->contents;
2495 m_addr = sec->header.sh_addr;
2497 module->size_of_struct = sizeof(*module);
2498 module->size = m_size;
2499 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2501 sec = obj_find_section(f, "__ksymtab");
2502 if (sec && sec->header.sh_size) {
2503 module->syms = sec->header.sh_addr;
2504 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2507 if (n_ext_modules_used) {
2508 sec = obj_find_section(f, ".kmodtab");
2509 module->deps = sec->header.sh_addr;
2510 module->ndeps = n_ext_modules_used;
2514 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2516 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2518 sec = obj_find_section(f, "__ex_table");
2520 module->ex_table_start = sec->header.sh_addr;
2521 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2524 sec = obj_find_section(f, ".text.init");
2526 module->runsize = sec->header.sh_addr - m_addr;
2528 sec = obj_find_section(f, ".data.init");
2530 if (!module->runsize ||
2531 module->runsize > sec->header.sh_addr - m_addr)
2532 module->runsize = sec->header.sh_addr - m_addr;
2535 if (!arch_init_module(f, module))
2538 /* Whew! All of the initialization is complete. Collect the final
2539 module image and give it to the kernel. */
2541 image = xmalloc(m_size);
2542 obj_create_image(f, image);
2544 ret = new_sys_init_module(m_name, (struct new_module *) image);
2546 perror_msg("init_module: %s", m_name);
2555 #define new_init_module(x, y, z) TRUE
2556 #define new_create_this_module(x, y) 0
2557 #define new_create_module_ksymtab(x)
2558 #define query_module(v, w, x, y, z) -1
2560 #endif /* BB_FEATURE_NEW_MODULE_INTERFACE */
2563 /*======================================================================*/
2566 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2569 struct obj_string_patch *p;
2570 struct obj_section *strsec;
2571 size_t len = strlen(string) + 1;
2574 p = xmalloc(sizeof(*p));
2575 p->next = f->string_patches;
2576 p->reloc_secidx = secidx;
2577 p->reloc_offset = offset;
2578 f->string_patches = p;
2580 strsec = obj_find_section(f, ".kstrtab");
2581 if (strsec == NULL) {
2582 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2583 p->string_offset = 0;
2584 loc = strsec->contents;
2586 p->string_offset = strsec->header.sh_size;
2587 loc = obj_extend_section(strsec, len);
2589 memcpy(loc, string, len);
2595 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2596 struct obj_symbol *sym)
2598 struct obj_symbol_patch *p;
2600 p = xmalloc(sizeof(*p));
2601 p->next = f->symbol_patches;
2602 p->reloc_secidx = secidx;
2603 p->reloc_offset = offset;
2605 f->symbol_patches = p;
2610 int obj_check_undefineds(struct obj_file *f)
2615 for (i = 0; i < HASH_BUCKETS; ++i) {
2616 struct obj_symbol *sym;
2617 for (sym = f->symtab[i]; sym; sym = sym->next)
2618 if (sym->secidx == SHN_UNDEF) {
2619 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2620 sym->secidx = SHN_ABS;
2623 error_msg("unresolved symbol %s", sym->name);
2632 void obj_allocate_commons(struct obj_file *f)
2634 struct common_entry {
2635 struct common_entry *next;
2636 struct obj_symbol *sym;
2637 } *common_head = NULL;
2641 for (i = 0; i < HASH_BUCKETS; ++i) {
2642 struct obj_symbol *sym;
2643 for (sym = f->symtab[i]; sym; sym = sym->next)
2644 if (sym->secidx == SHN_COMMON) {
2645 /* Collect all COMMON symbols and sort them by size so as to
2646 minimize space wasted by alignment requirements. */
2648 struct common_entry **p, *n;
2649 for (p = &common_head; *p; p = &(*p)->next)
2650 if (sym->size <= (*p)->sym->size)
2653 n = alloca(sizeof(*n));
2661 for (i = 1; i < f->local_symtab_size; ++i) {
2662 struct obj_symbol *sym = f->local_symtab[i];
2663 if (sym && sym->secidx == SHN_COMMON) {
2664 struct common_entry **p, *n;
2665 for (p = &common_head; *p; p = &(*p)->next)
2666 if (sym == (*p)->sym)
2668 else if (sym->size < (*p)->sym->size) {
2669 n = alloca(sizeof(*n));
2679 /* Find the bss section. */
2680 for (i = 0; i < f->header.e_shnum; ++i)
2681 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2684 /* If for some reason there hadn't been one, create one. */
2685 if (i == f->header.e_shnum) {
2686 struct obj_section *sec;
2688 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2689 f->sections[i] = sec = arch_new_section();
2690 f->header.e_shnum = i + 1;
2692 memset(sec, 0, sizeof(*sec));
2693 sec->header.sh_type = SHT_PROGBITS;
2694 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2699 /* Allocate the COMMONS. */
2701 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2702 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2703 struct common_entry *c;
2705 for (c = common_head; c; c = c->next) {
2706 ElfW(Addr) align = c->sym->value;
2708 if (align > max_align)
2710 if (bss_size & (align - 1))
2711 bss_size = (bss_size | (align - 1)) + 1;
2714 c->sym->value = bss_size;
2716 bss_size += c->sym->size;
2719 f->sections[i]->header.sh_size = bss_size;
2720 f->sections[i]->header.sh_addralign = max_align;
2724 /* For the sake of patch relocation and parameter initialization,
2725 allocate zeroed data for NOBITS sections now. Note that after
2726 this we cannot assume NOBITS are really empty. */
2727 for (i = 0; i < f->header.e_shnum; ++i) {
2728 struct obj_section *s = f->sections[i];
2729 if (s->header.sh_type == SHT_NOBITS) {
2730 if (s->header.sh_size != 0)
2731 s->contents = memset(xmalloc(s->header.sh_size),
2732 0, s->header.sh_size);
2736 s->header.sh_type = SHT_PROGBITS;
2741 unsigned long obj_load_size(struct obj_file *f)
2743 unsigned long dot = 0;
2744 struct obj_section *sec;
2746 /* Finalize the positions of the sections relative to one another. */
2748 for (sec = f->load_order; sec; sec = sec->load_next) {
2751 align = sec->header.sh_addralign;
2752 if (align && (dot & (align - 1)))
2753 dot = (dot | (align - 1)) + 1;
2755 sec->header.sh_addr = dot;
2756 dot += sec->header.sh_size;
2762 int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2764 int i, n = f->header.e_shnum;
2767 /* Finalize the addresses of the sections. */
2770 for (i = 0; i < n; ++i)
2771 f->sections[i]->header.sh_addr += base;
2773 /* And iterate over all of the relocations. */
2775 for (i = 0; i < n; ++i) {
2776 struct obj_section *relsec, *symsec, *targsec, *strsec;
2777 ElfW(RelM) * rel, *relend;
2781 relsec = f->sections[i];
2782 if (relsec->header.sh_type != SHT_RELM)
2785 symsec = f->sections[relsec->header.sh_link];
2786 targsec = f->sections[relsec->header.sh_info];
2787 strsec = f->sections[symsec->header.sh_link];
2789 rel = (ElfW(RelM) *) relsec->contents;
2790 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2791 symtab = (ElfW(Sym) *) symsec->contents;
2792 strtab = (const char *) strsec->contents;
2794 for (; rel < relend; ++rel) {
2795 ElfW(Addr) value = 0;
2796 struct obj_symbol *intsym = NULL;
2797 unsigned long symndx;
2798 ElfW(Sym) * extsym = 0;
2801 /* Attempt to find a value to use for this relocation. */
2803 symndx = ELFW(R_SYM) (rel->r_info);
2805 /* Note we've already checked for undefined symbols. */
2807 extsym = &symtab[symndx];
2808 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2809 /* Local symbols we look up in the local table to be sure
2810 we get the one that is really intended. */
2811 intsym = f->local_symtab[symndx];
2813 /* Others we look up in the hash table. */
2815 if (extsym->st_name)
2816 name = strtab + extsym->st_name;
2818 name = f->sections[extsym->st_shndx]->name;
2819 intsym = obj_find_symbol(f, name);
2822 value = obj_symbol_final_value(f, intsym);
2823 intsym->referenced = 1;
2825 #if SHT_RELM == SHT_RELA
2826 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2827 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2828 if (!extsym || !extsym->st_name ||
2829 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2831 value += rel->r_addend;
2835 switch (arch_apply_relocation
2836 (f, targsec, symsec, intsym, rel, value)) {
2840 case obj_reloc_overflow:
2841 errmsg = "Relocation overflow";
2843 case obj_reloc_dangerous:
2844 errmsg = "Dangerous relocation";
2846 case obj_reloc_unhandled:
2847 errmsg = "Unhandled relocation";
2850 error_msg("%s of type %ld for %s", errmsg,
2851 (long) ELFW(R_TYPE) (rel->r_info),
2852 strtab + extsym->st_name);
2854 error_msg("%s of type %ld", errmsg,
2855 (long) ELFW(R_TYPE) (rel->r_info));
2863 /* Finally, take care of the patches. */
2865 if (f->string_patches) {
2866 struct obj_string_patch *p;
2867 struct obj_section *strsec;
2868 ElfW(Addr) strsec_base;
2869 strsec = obj_find_section(f, ".kstrtab");
2870 strsec_base = strsec->header.sh_addr;
2872 for (p = f->string_patches; p; p = p->next) {
2873 struct obj_section *targsec = f->sections[p->reloc_secidx];
2874 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2875 = strsec_base + p->string_offset;
2879 if (f->symbol_patches) {
2880 struct obj_symbol_patch *p;
2882 for (p = f->symbol_patches; p; p = p->next) {
2883 struct obj_section *targsec = f->sections[p->reloc_secidx];
2884 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2885 = obj_symbol_final_value(f, p->sym);
2892 int obj_create_image(struct obj_file *f, char *image)
2894 struct obj_section *sec;
2895 ElfW(Addr) base = f->baseaddr;
2897 for (sec = f->load_order; sec; sec = sec->load_next) {
2900 if (sec->contents == 0 || sec->header.sh_size == 0)
2903 secimg = image + (sec->header.sh_addr - base);
2905 /* Note that we allocated data for NOBITS sections earlier. */
2906 memcpy(secimg, sec->contents, sec->header.sh_size);
2912 /*======================================================================*/
2914 struct obj_file *obj_load(FILE * fp)
2917 ElfW(Shdr) * section_headers;
2921 /* Read the file header. */
2923 f = arch_new_file();
2924 memset(f, 0, sizeof(*f));
2925 f->symbol_cmp = strcmp;
2926 f->symbol_hash = obj_elf_hash;
2927 f->load_order_search_start = &f->load_order;
2929 fseek(fp, 0, SEEK_SET);
2930 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2931 perror_msg("error reading ELF header");
2935 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2936 || f->header.e_ident[EI_MAG1] != ELFMAG1
2937 || f->header.e_ident[EI_MAG2] != ELFMAG2
2938 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2939 error_msg("not an ELF file");
2942 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2943 || f->header.e_ident[EI_DATA] != ELFDATAM
2944 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2945 || !MATCH_MACHINE(f->header.e_machine)) {
2946 error_msg("ELF file not for this architecture");
2949 if (f->header.e_type != ET_REL) {
2950 error_msg("ELF file not a relocatable object");
2954 /* Read the section headers. */
2956 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
2957 error_msg("section header size mismatch: %lu != %lu",
2958 (unsigned long) f->header.e_shentsize,
2959 (unsigned long) sizeof(ElfW(Shdr)));
2963 shnum = f->header.e_shnum;
2964 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
2965 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
2967 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
2968 fseek(fp, f->header.e_shoff, SEEK_SET);
2969 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
2970 perror_msg("error reading ELF section headers");
2974 /* Read the section data. */
2976 for (i = 0; i < shnum; ++i) {
2977 struct obj_section *sec;
2979 f->sections[i] = sec = arch_new_section();
2980 memset(sec, 0, sizeof(*sec));
2982 sec->header = section_headers[i];
2985 if(sec->header.sh_size) switch (sec->header.sh_type) {
2996 if (sec->header.sh_size > 0) {
2997 sec->contents = xmalloc(sec->header.sh_size);
2998 fseek(fp, sec->header.sh_offset, SEEK_SET);
2999 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3000 perror_msg("error reading ELF section data");
3004 sec->contents = NULL;
3008 #if SHT_RELM == SHT_REL
3010 error_msg("RELA relocations not supported on this architecture");
3014 error_msg("REL relocations not supported on this architecture");
3019 if (sec->header.sh_type >= SHT_LOPROC) {
3020 /* Assume processor specific section types are debug
3021 info and can safely be ignored. If this is ever not
3022 the case (Hello MIPS?), don't put ifdefs here but
3023 create an arch_load_proc_section(). */
3027 error_msg("can't handle sections of type %ld",
3028 (long) sec->header.sh_type);
3033 /* Do what sort of interpretation as needed by each section. */
3035 shstrtab = f->sections[f->header.e_shstrndx]->contents;
3037 for (i = 0; i < shnum; ++i) {
3038 struct obj_section *sec = f->sections[i];
3039 sec->name = shstrtab + sec->header.sh_name;
3042 for (i = 0; i < shnum; ++i) {
3043 struct obj_section *sec = f->sections[i];
3045 if (sec->header.sh_flags & SHF_ALLOC)
3046 obj_insert_section_load_order(f, sec);
3048 switch (sec->header.sh_type) {
3051 unsigned long nsym, j;
3055 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3056 error_msg("symbol size mismatch: %lu != %lu",
3057 (unsigned long) sec->header.sh_entsize,
3058 (unsigned long) sizeof(ElfW(Sym)));
3062 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
3063 strtab = f->sections[sec->header.sh_link]->contents;
3064 sym = (ElfW(Sym) *) sec->contents;
3066 /* Allocate space for a table of local symbols. */
3067 j = f->local_symtab_size = sec->header.sh_info;
3068 f->local_symtab = xmalloc(j *=
3069 sizeof(struct obj_symbol *));
3070 memset(f->local_symtab, 0, j);
3072 /* Insert all symbols into the hash table. */
3073 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
3076 name = strtab + sym->st_name;
3078 name = f->sections[sym->st_shndx]->name;
3080 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
3081 sym->st_value, sym->st_size);
3087 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3088 error_msg("relocation entry size mismatch: %lu != %lu",
3089 (unsigned long) sec->header.sh_entsize,
3090 (unsigned long) sizeof(ElfW(RelM)));
3100 static void hide_special_symbols(struct obj_file *f)
3102 static const char *const specials[] = {
3109 struct obj_symbol *sym;
3110 const char *const *p;
3112 for (p = specials; *p; ++p)
3113 if ((sym = obj_find_symbol(f, *p)) != NULL)
3115 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
3120 extern int insmod_main( int argc, char **argv)
3127 unsigned long m_size;
3132 char m_name[BUFSIZ + 1] = "\0";
3133 int exit_status = EXIT_FAILURE;
3135 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3137 char k_strversion[STRVERSIONLEN];
3138 char m_strversion[STRVERSIONLEN];
3143 /* Parse any options */
3144 while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
3146 case 'f': /* force loading */
3147 flag_force_load = 1;
3149 case 'k': /* module loaded by kerneld, auto-cleanable */
3152 case 'v': /* verbose output */
3155 case 'x': /* do not export externs */
3158 case 'o': /* name the output module */
3159 strncpy(m_name, optarg, BUFSIZ);
3161 case 'L': /* Stub warning */
3162 /* This is needed for compatibility with modprobe.
3163 * In theory, this does locking, but we don't do
3164 * that. So be careful and plan your life around not
3165 * loading the same module 50 times concurrently. */
3172 if (argv[optind] == NULL) {
3176 /* Grab the module name */
3177 if ((tmp = strrchr(argv[optind], '/')) != NULL) {
3184 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
3186 strncpy(m_fullName, tmp, len);
3187 if (*m_name == '\0') {
3188 strcpy(m_name, m_fullName);
3190 strcat(m_fullName, ".o");
3192 /* Get a filedesc for the module */
3193 if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
3194 (fp = fopen(argv[optind], "r")) == NULL) {
3195 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
3196 if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE,
3197 findNamedModule, 0, m_fullName) == FALSE)
3199 if (m_filename[0] == '\0'
3200 || ((fp = fopen(m_filename, "r")) == NULL))
3202 error_msg("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
3203 return EXIT_FAILURE;
3206 error_msg_and_die("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
3208 memcpy(m_filename, argv[optind], strlen(argv[optind]));
3211 if ((f = obj_load(fp)) == NULL)
3212 perror_msg_and_die("Could not load the module");
3214 if (get_modinfo_value(f, "kernel_version") == NULL)
3219 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3220 /* Version correspondence? */
3222 k_version = get_kernel_version(k_strversion);
3223 if (m_has_modinfo) {
3224 m_version = new_get_module_version(f, m_strversion);
3226 m_version = old_get_module_version(f, m_strversion);
3227 if (m_version == -1) {
3228 error_msg("couldn't find the kernel version the module was "
3234 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
3235 if (flag_force_load) {
3236 error_msg("Warning: kernel-module version mismatch\n"
3237 "\t%s was compiled for kernel version %s\n"
3238 "\twhile this kernel is version %s",
3239 m_filename, m_strversion, k_strversion);
3241 error_msg("kernel-module version mismatch\n"
3242 "\t%s was compiled for kernel version %s\n"
3243 "\twhile this kernel is version %s.",
3244 m_filename, m_strversion, k_strversion);
3249 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3251 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
3253 if (k_new_syscalls) {
3254 #ifdef BB_FEATURE_NEW_MODULE_INTERFACE
3255 if (!new_get_kernel_symbols())
3257 k_crcs = new_is_kernel_checksummed();
3259 error_msg("Not configured to support new kernels");
3263 #ifdef BB_FEATURE_OLD_MODULE_INTERFACE
3264 if (!old_get_kernel_symbols(m_name))
3266 k_crcs = old_is_kernel_checksummed();
3268 error_msg("Not configured to support old kernels");
3273 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
3275 m_crcs = new_is_module_checksummed(f);
3277 m_crcs = old_is_module_checksummed(f);
3279 if (m_crcs != k_crcs)
3280 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
3281 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
3283 /* Let the module know about the kernel symbols. */
3284 add_kernel_symbols(f);
3286 /* Allocate common symbols, symbol tables, and string tables. */
3289 ? !new_create_this_module(f, m_name)
3290 : !old_create_mod_use_count(f))
3295 if (!obj_check_undefineds(f)) {
3298 obj_allocate_commons(f);
3300 /* done with the module name, on to the optional var=value arguments */
3303 if (optind < argc) {
3305 ? !new_process_module_arguments(f, argc - optind, argv + optind)
3306 : !old_process_module_arguments(f, argc - optind, argv + optind))
3313 hide_special_symbols(f);
3316 new_create_module_ksymtab(f);
3318 /* Find current size of the module */
3319 m_size = obj_load_size(f);
3322 m_addr = create_module(m_name, m_size);
3323 if (m_addr==-1) switch (errno) {
3325 error_msg("A module named %s already exists", m_name);
3328 error_msg("Can't allocate kernel memory for module; needed %lu bytes",
3332 perror_msg("create_module: %s", m_name);
3336 if (!obj_relocate(f, m_addr)) {
3337 delete_module(m_name);
3342 ? !new_init_module(m_name, f, m_size)
3343 : !old_init_module(m_name, f, m_size))
3345 delete_module(m_name);
3349 exit_status = EXIT_SUCCESS;
3353 return(exit_status);