1 /* vi: set sw=4 ts=4: */
3 * Mini insmod implementation for busybox
5 * Copyright (C) 1999,2000 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. Note that there is still no true
11 * multiple architecture support. You just get SH3|SH4|i386, despite
12 * the mention of ARM and m68k--which may or may not work (but
13 * almost certainly do not, due to at least MATCH_MACHINE). I have
14 * only tested SH4 in little endian mode.
16 * Based almost entirely on the Linux modutils-2.3.11 implementation.
17 * Copyright 1996, 1997 Linux International.
18 * New implementation contributed by Richard Henderson <rth@tamu.edu>
19 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
20 * Restructured (and partly rewritten) by:
21 * Björn Ekwall <bj0rn@blox.se> February 1999
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
49 #include <sys/utsname.h>
51 //----------------------------------------------------------------------------
52 //--------modutils module.h, lines 45-242
53 //----------------------------------------------------------------------------
55 /* Definitions for the Linux module syscall interface.
56 Copyright 1996, 1997 Linux International.
58 Contributed by Richard Henderson <rth@tamu.edu>
60 This file is part of the Linux modutils.
62 This program is free software; you can redistribute it and/or modify it
63 under the terms of the GNU General Public License as published by the
64 Free Software Foundation; either version 2 of the License, or (at your
65 option) any later version.
67 This program is distributed in the hope that it will be useful, but
68 WITHOUT ANY WARRANTY; without even the implied warranty of
69 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
70 General Public License for more details.
72 You should have received a copy of the GNU General Public License
73 along with this program; if not, write to the Free Software Foundation,
74 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
77 #ifndef MODUTILS_MODULE_H
78 #define MODUTILS_MODULE_H 1
80 #ident "$Id: insmod.c,v 1.27 2000/10/23 17:55:27 kraai Exp $"
82 /* This file contains the structures used by the 2.0 and 2.1 kernels.
83 We do not use the kernel headers directly because we do not wish
84 to be dependant on a particular kernel version to compile insmod. */
87 /*======================================================================*/
88 /* The structures used by Linux 2.0. */
90 /* The symbol format used by get_kernel_syms(2). */
99 unsigned long module; /* kernel addresses */
103 struct old_module_symbol
109 struct old_symbol_table
111 int size; /* total, including string table!!! */
114 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
115 struct old_module_ref ref[0]; /* actual size defined by n_refs */
118 struct old_mod_routines
121 unsigned long cleanup;
127 unsigned long ref; /* the list of modules that refer to me */
128 unsigned long symtab;
130 int size; /* size of module in pages */
131 unsigned long addr; /* address of module */
133 unsigned long cleanup; /* cleanup routine */
136 /* Sent to init_module(2) or'ed into the code size parameter. */
137 #define OLD_MOD_AUTOCLEAN 0x40000000 /* big enough, but no sign problems... */
139 int get_kernel_syms(struct old_kernel_sym *);
140 int old_sys_init_module(const char *name, char *code, unsigned codesize,
141 struct old_mod_routines *, struct old_symbol_table *);
143 /*======================================================================*/
144 /* For sizeof() which are related to the module platform and not to the
145 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
147 #define tgt_sizeof_char sizeof(char)
148 #define tgt_sizeof_short sizeof(short)
149 #define tgt_sizeof_int sizeof(int)
150 #define tgt_sizeof_long sizeof(long)
151 #define tgt_sizeof_char_p sizeof(char *)
152 #define tgt_sizeof_void_p sizeof(void *)
153 #define tgt_long long
155 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
156 #undef tgt_sizeof_long
157 #undef tgt_sizeof_char_p
158 #undef tgt_sizeof_void_p
160 #define tgt_sizeof_long 8
161 #define tgt_sizeof_char_p 8
162 #define tgt_sizeof_void_p 8
163 #define tgt_long long long
166 /*======================================================================*/
167 /* The structures used in Linux 2.1. */
169 /* Note: new_module_symbol does not use tgt_long intentionally */
170 struct new_module_symbol
176 struct new_module_persist;
178 struct new_module_ref
180 unsigned tgt_long dep; /* kernel addresses */
181 unsigned tgt_long ref;
182 unsigned tgt_long next_ref;
187 unsigned tgt_long size_of_struct; /* == sizeof(module) */
188 unsigned tgt_long next;
189 unsigned tgt_long name;
190 unsigned tgt_long size;
193 unsigned tgt_long flags; /* AUTOCLEAN et al */
198 unsigned tgt_long syms;
199 unsigned tgt_long deps;
200 unsigned tgt_long refs;
201 unsigned tgt_long init;
202 unsigned tgt_long cleanup;
203 unsigned tgt_long ex_table_start;
204 unsigned tgt_long ex_table_end;
206 unsigned tgt_long gp;
208 /* Everything after here is extension. */
209 unsigned tgt_long persist_start;
210 unsigned tgt_long persist_end;
211 unsigned tgt_long can_unload;
212 unsigned tgt_long runsize;
215 struct new_module_info
223 /* Bits of module.flags. */
224 #define NEW_MOD_RUNNING 1
225 #define NEW_MOD_DELETED 2
226 #define NEW_MOD_AUTOCLEAN 4
227 #define NEW_MOD_VISITED 8
228 #define NEW_MOD_USED_ONCE 16
230 int new_sys_init_module(const char *name, const struct new_module *);
231 int query_module(const char *name, int which, void *buf, size_t bufsize,
234 /* Values for query_module's which. */
242 /*======================================================================*/
243 /* The system calls unchanged between 2.0 and 2.1. */
245 unsigned long create_module(const char *, size_t);
246 int delete_module(const char *);
249 #endif /* module.h */
251 //----------------------------------------------------------------------------
252 //--------end of modutils module.h
253 //----------------------------------------------------------------------------
257 //----------------------------------------------------------------------------
258 //--------modutils obj.h, lines 253-462
259 //----------------------------------------------------------------------------
261 /* Elf object file loading and relocation routines.
262 Copyright 1996, 1997 Linux International.
264 Contributed by Richard Henderson <rth@tamu.edu>
266 This file is part of the Linux modutils.
268 This program is free software; you can redistribute it and/or modify it
269 under the terms of the GNU General Public License as published by the
270 Free Software Foundation; either version 2 of the License, or (at your
271 option) any later version.
273 This program is distributed in the hope that it will be useful, but
274 WITHOUT ANY WARRANTY; without even the implied warranty of
275 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
276 General Public License for more details.
278 You should have received a copy of the GNU General Public License
279 along with this program; if not, write to the Free Software Foundation,
280 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
283 #ifndef MODUTILS_OBJ_H
284 #define MODUTILS_OBJ_H 1
286 #ident "$Id: insmod.c,v 1.27 2000/10/23 17:55:27 kraai Exp $"
288 /* The relocatable object is manipulated using elfin types. */
294 /* Machine-specific elf macros for i386 et al. */
296 /* the SH changes have only been tested on the SH4 in =little endian= mode */
297 /* I'm not sure about big endian, so let's warn: */
299 #if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
300 #error insmod.c may require changes for use on big endian SH4/SH3
303 /* it may or may not work on the SH1/SH2... So let's error on those
305 #if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
306 #error insmod.c may require changes for non-SH3/SH4 use
309 #define ELFCLASSM ELFCLASS32
310 #define ELFDATAM ELFDATA2LSB
316 #define MATCH_MACHINE(x) (x == EM_SH)
317 #define SHT_RELM SHT_RELA
318 #define Elf32_RelM Elf32_Rela
322 /* presumably we can use these for anything but the SH */
323 /* this is the previous behavior, but it does result in
324 insmod.c being broken on anything except i386 */
327 #define MATCH_MACHINE(x) (x == EM_386)
329 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
332 #define SHT_RELM SHT_REL
333 #define Elf32_RelM Elf32_Rel
338 # if ELFCLASSM == ELFCLASS32
339 # define ElfW(x) Elf32_ ## x
340 # define ELFW(x) ELF32_ ## x
342 # define ElfW(x) Elf64_ ## x
343 # define ELFW(x) ELF64_ ## x
347 /* For some reason this is missing from libc5. */
348 #ifndef ELF32_ST_INFO
349 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
352 #ifndef ELF64_ST_INFO
353 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
356 struct obj_string_patch;
357 struct obj_symbol_patch;
364 struct obj_section *load_next;
370 struct obj_symbol *next; /* hash table link */
374 int secidx; /* the defining section index/module */
376 int ksymidx; /* for export to the kernel symtab */
377 int referenced; /* actually used in the link */
380 /* Hardcode the hash table size. We shouldn't be needing so many
381 symbols that we begin to degrade performance, and we get a big win
382 by giving the compiler a constant divisor. */
384 #define HASH_BUCKETS 521
390 struct obj_section **sections;
391 struct obj_section *load_order;
392 struct obj_section **load_order_search_start;
393 struct obj_string_patch *string_patches;
394 struct obj_symbol_patch *symbol_patches;
395 int (*symbol_cmp)(const char *, const char *);
396 unsigned long (*symbol_hash)(const char *);
397 unsigned long local_symtab_size;
398 struct obj_symbol **local_symtab;
399 struct obj_symbol *symtab[HASH_BUCKETS];
410 struct obj_string_patch
412 struct obj_string_patch *next;
414 ElfW(Addr) reloc_offset;
415 ElfW(Addr) string_offset;
418 struct obj_symbol_patch
420 struct obj_symbol_patch *next;
422 ElfW(Addr) reloc_offset;
423 struct obj_symbol *sym;
427 /* Generic object manipulation routines. */
429 unsigned long obj_elf_hash(const char *);
431 unsigned long obj_elf_hash_n(const char *, unsigned long len);
433 struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
434 unsigned long symidx, int info, int secidx,
435 ElfW(Addr) value, unsigned long size);
437 struct obj_symbol *obj_find_symbol (struct obj_file *f,
440 ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
441 struct obj_symbol *sym);
443 void obj_set_symbol_compare(struct obj_file *f,
444 int (*cmp)(const char *, const char *),
445 unsigned long (*hash)(const char *));
447 struct obj_section *obj_find_section (struct obj_file *f,
450 void obj_insert_section_load_order (struct obj_file *f,
451 struct obj_section *sec);
453 struct obj_section *obj_create_alloced_section (struct obj_file *f,
458 struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
463 void *obj_extend_section (struct obj_section *sec, unsigned long more);
465 int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
468 int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
469 struct obj_symbol *sym);
471 int obj_check_undefineds(struct obj_file *f);
473 void obj_allocate_commons(struct obj_file *f);
475 unsigned long obj_load_size (struct obj_file *f);
477 int obj_relocate (struct obj_file *f, ElfW(Addr) base);
479 struct obj_file *obj_load(FILE *f);
481 int obj_create_image (struct obj_file *f, char *image);
483 /* Architecture specific manipulation routines. */
485 struct obj_file *arch_new_file (void);
487 struct obj_section *arch_new_section (void);
489 struct obj_symbol *arch_new_symbol (void);
491 enum obj_reloc arch_apply_relocation (struct obj_file *f,
492 struct obj_section *targsec,
493 struct obj_section *symsec,
494 struct obj_symbol *sym,
495 ElfW(RelM) *rel, ElfW(Addr) value);
497 int arch_create_got (struct obj_file *f);
500 int arch_init_module (struct obj_file *f, struct new_module *);
503 //----------------------------------------------------------------------------
504 //--------end of modutils obj.h
505 //----------------------------------------------------------------------------
511 #define _PATH_MODULES "/lib/modules"
512 #define STRVERSIONLEN 32
514 #if !defined(BB_FEATURE_INSMOD_NEW_KERNEL) && !defined(BB_FEATURE_INSMOD_OLD_KERNEL)
515 #error "Must have ether BB_FEATURE_INSMOD_NEW_KERNEL or BB_FEATURE_INSMOD_OLD_KERNEL defined"
518 /*======================================================================*/
520 int flag_force_load = 0;
521 int flag_autoclean = 0;
522 int flag_verbose = 0;
526 /*======================================================================*/
528 /* previously, these were named i386_* but since we could be
529 compiling for the sh, I've renamed them to the more general
530 arch_* These structures are the same between the x86 and SH,
531 and we can't support anything else right now anyway. In the
532 future maybe they should be #if defined'd */
534 struct arch_got_entry {
536 unsigned offset_done:1;
537 unsigned reloc_done:1;
541 struct obj_file root;
542 struct obj_section *got;
546 struct obj_symbol root;
547 struct arch_got_entry gotent;
551 struct external_module {
556 struct new_module_symbol *syms;
559 struct new_module_symbol *ksyms;
562 struct external_module *ext_modules;
564 int n_ext_modules_used;
568 /* Some firendly syscalls to cheer everyone's day... */
569 #define __NR_new_sys_init_module __NR_init_module
570 _syscall2(int, new_sys_init_module, const char *, name,
571 const struct new_module *, info)
572 #define __NR_old_sys_init_module __NR_init_module
573 _syscall5(int, old_sys_init_module, const char *, name, char *, code,
574 unsigned, codesize, struct old_mod_routines *, routines,
575 struct old_symbol_table *, symtab)
577 _syscall1(int, delete_module, const char *, name)
579 extern int delete_module(const char *);
582 /* This is kind of troublesome. See, we don't actually support
583 the m68k or the arm the same way we support i386 and (now)
584 sh. In doing my SH patch, I just assumed that whatever works
585 for i386 also works for m68k and arm since currently insmod.c
586 does nothing special for them. If this isn't true, the below
587 line is rather misleading IMHO, and someone should either
588 change it or add more proper architecture-dependent support
591 -- Bryan Rittmeyer <bryan@ixiacom.com> */
593 #if defined(__i386__) || defined(__m68k__) || defined(__arm__)
594 /* Jump through hoops to fixup error return codes */
595 #define __NR__create_module __NR_create_module
596 static inline _syscall2(long, _create_module, const char *, name, size_t,
598 unsigned long create_module(const char *name, size_t size)
600 long ret = _create_module(name, size);
602 if (ret == -1 && errno > 125) {
609 _syscall2(unsigned long, create_module, const char *, name, size_t, size)
611 static char m_filename[BUFSIZ + 1] = "\0";
612 static char m_fullName[BUFSIZ + 1] = "\0";
614 /*======================================================================*/
617 static int findNamedModule(const char *fileName, struct stat *statbuf,
620 char *fullName = (char *) userDate;
623 if (fullName[0] == '\0')
626 char *tmp = strrchr(fileName, '/');
629 tmp = (char *) fileName;
632 if (check_wildcard_match(tmp, fullName) == TRUE) {
633 /* Stop searching if we find a match */
634 memcpy(m_filename, fileName, strlen(fileName)+1);
642 /*======================================================================*/
644 struct obj_file *arch_new_file(void)
647 f = xmalloc(sizeof(*f));
652 struct obj_section *arch_new_section(void)
654 return xmalloc(sizeof(struct obj_section));
657 struct obj_symbol *arch_new_symbol(void)
659 struct arch_symbol *sym;
660 sym = xmalloc(sizeof(*sym));
661 memset(&sym->gotent, 0, sizeof(sym->gotent));
666 arch_apply_relocation(struct obj_file *f,
667 struct obj_section *targsec,
668 struct obj_section *symsec,
669 struct obj_symbol *sym,
671 Elf32_Rela * rel, Elf32_Addr v)
673 Elf32_Rel * rel, Elf32_Addr v)
676 struct arch_file *ifile = (struct arch_file *) f;
677 struct arch_symbol *isym = (struct arch_symbol *) sym;
679 Elf32_Addr *loc = (Elf32_Addr *) (targsec->contents + rel->r_offset);
680 Elf32_Addr dot = targsec->header.sh_addr + rel->r_offset;
681 Elf32_Addr got = ifile->got ? ifile->got->header.sh_addr : 0;
683 enum obj_reloc ret = obj_reloc_ok;
685 switch (ELF32_R_TYPE(rel->r_info)) {
687 /* even though these constants seem to be the same for
688 the i386 and the sh, we "#if define" them for clarity
689 and in case that ever changes */
735 *loc += f->baseaddr + rel->r_addend;
746 *loc += got - dot + rel->r_addend;;
757 assert(isym != NULL);
758 if (!isym->gotent.reloc_done) {
759 isym->gotent.reloc_done = 1;
760 *(Elf32_Addr *) (ifile->got->contents + isym->gotent.offset) =
763 *loc += isym->gotent.offset + rel->r_addend;
767 assert(isym != NULL);
768 if (!isym->gotent.reloc_done) {
769 isym->gotent.reloc_done = 1;
770 *(Elf32_Addr *) (ifile->got->contents + isym->gotent.offset) =
773 *loc += isym->gotent.offset;
787 ret = obj_reloc_unhandled;
794 int arch_create_got(struct obj_file *f)
796 struct arch_file *ifile = (struct arch_file *) f;
797 int i, n, offset = 0, gotneeded = 0;
799 n = ifile->root.header.e_shnum;
800 for (i = 0; i < n; ++i) {
801 struct obj_section *relsec, *symsec, *strsec;
803 Elf32_Rela *rel, *relend;
805 Elf32_Rel *rel, *relend;
810 relsec = ifile->root.sections[i];
811 if (relsec->header.sh_type != SHT_REL)
814 symsec = ifile->root.sections[relsec->header.sh_link];
815 strsec = ifile->root.sections[symsec->header.sh_link];
819 rel = (Elf32_Rela *) relsec->contents;
820 relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rela));
822 rel = (Elf32_Rel *) relsec->contents;
823 relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rel));
825 symtab = (Elf32_Sym *) symsec->contents;
826 strtab = (const char *) strsec->contents;
828 for (; rel < relend; ++rel) {
830 struct arch_symbol *intsym;
833 switch (ELF32_R_TYPE(rel->r_info)) {
853 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
855 name = strtab + extsym->st_name;
857 name = f->sections[extsym->st_shndx]->name;
859 (struct arch_symbol *) obj_find_symbol(&ifile->root, name);
861 if (!intsym->gotent.offset_done) {
862 intsym->gotent.offset_done = 1;
863 intsym->gotent.offset = offset;
869 if (offset > 0 || gotneeded)
871 obj_create_alloced_section(&ifile->root, ".got", 4, offset);
876 int arch_init_module(struct obj_file *f, struct new_module *mod)
882 /*======================================================================*/
884 /* Standard ELF hash function. */
885 inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
894 if ((g = (h & 0xf0000000)) != 0) {
903 unsigned long obj_elf_hash(const char *name)
905 return obj_elf_hash_n(name, strlen(name));
908 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
909 /* Get the kernel version in the canonical integer form. */
911 static int get_kernel_version(char str[STRVERSIONLEN])
913 struct utsname uts_info;
917 if (uname(&uts_info) < 0)
919 strncpy(str, uts_info.release, STRVERSIONLEN);
920 p = uts_info.release;
922 a = strtoul(p, &p, 10);
925 b = strtoul(p + 1, &p, 10);
928 c = strtoul(p + 1, &q, 10);
932 return a << 16 | b << 8 | c;
935 /* String comparison for non-co-versioned kernel and module. */
937 static int ncv_strcmp(const char *a, const char *b)
939 size_t alen = strlen(a), blen = strlen(b);
941 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
942 return strncmp(a, b, alen);
943 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
944 return strncmp(a, b, blen);
949 /* String hashing for non-co-versioned kernel and module. Here
950 we are simply forced to drop the crc from the hash. */
952 static unsigned long ncv_symbol_hash(const char *str)
954 size_t len = strlen(str);
955 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
957 return obj_elf_hash_n(str, len);
961 obj_set_symbol_compare(struct obj_file *f,
962 int (*cmp) (const char *, const char *),
963 unsigned long (*hash) (const char *))
968 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
971 f->symbol_hash = hash;
973 memcpy(tmptab, f->symtab, sizeof(tmptab));
974 memset(f->symtab, 0, sizeof(f->symtab));
976 for (i = 0; i < HASH_BUCKETS; ++i)
977 for (sym = tmptab[i]; sym; sym = next) {
978 unsigned long h = hash(sym->name) % HASH_BUCKETS;
980 sym->next = f->symtab[h];
986 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
989 struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
990 unsigned long symidx, int info,
991 int secidx, ElfW(Addr) value,
994 struct obj_symbol *sym;
995 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
996 int n_type = ELFW(ST_TYPE) (info);
997 int n_binding = ELFW(ST_BIND) (info);
999 for (sym = f->symtab[hash]; sym; sym = sym->next)
1000 if (f->symbol_cmp(sym->name, name) == 0) {
1001 int o_secidx = sym->secidx;
1002 int o_info = sym->info;
1003 int o_type = ELFW(ST_TYPE) (o_info);
1004 int o_binding = ELFW(ST_BIND) (o_info);
1006 /* A redefinition! Is it legal? */
1008 if (secidx == SHN_UNDEF)
1010 else if (o_secidx == SHN_UNDEF)
1012 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1013 /* Cope with local and global symbols of the same name
1014 in the same object file, as might have been created
1015 by ld -r. The only reason locals are now seen at this
1016 level at all is so that we can do semi-sensible things
1019 struct obj_symbol *nsym, **p;
1021 nsym = arch_new_symbol();
1022 nsym->next = sym->next;
1025 /* Excise the old (local) symbol from the hash chain. */
1026 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1030 } else if (n_binding == STB_LOCAL) {
1031 /* Another symbol of the same name has already been defined.
1032 Just add this to the local table. */
1033 sym = arch_new_symbol();
1036 f->local_symtab[symidx] = sym;
1038 } else if (n_binding == STB_WEAK)
1040 else if (o_binding == STB_WEAK)
1042 /* Don't unify COMMON symbols with object types the programmer
1044 else if (secidx == SHN_COMMON
1045 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1047 else if (o_secidx == SHN_COMMON
1048 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1051 /* Don't report an error if the symbol is coming from
1052 the kernel or some external module. */
1053 if (secidx <= SHN_HIRESERVE)
1054 errorMsg("%s multiply defined\n", name);
1059 /* Completely new symbol. */
1060 sym = arch_new_symbol();
1061 sym->next = f->symtab[hash];
1062 f->symtab[hash] = sym;
1065 if (ELFW(ST_BIND) (info) == STB_LOCAL)
1066 f->local_symtab[symidx] = sym;
1072 sym->secidx = secidx;
1078 struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
1080 struct obj_symbol *sym;
1081 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1083 for (sym = f->symtab[hash]; sym; sym = sym->next)
1084 if (f->symbol_cmp(sym->name, name) == 0)
1091 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1094 if (sym->secidx >= SHN_LORESERVE)
1097 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1099 /* As a special case, a NULL sym has value zero. */
1104 struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1106 int i, n = f->header.e_shnum;
1108 for (i = 0; i < n; ++i)
1109 if (strcmp(f->sections[i]->name, name) == 0)
1110 return f->sections[i];
1115 static int obj_load_order_prio(struct obj_section *a)
1117 unsigned long af, ac;
1119 af = a->header.sh_flags;
1122 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1123 strcmp(a->name + 5, ".init"))
1127 if (!(af & SHF_WRITE))
1129 if (af & SHF_EXECINSTR)
1131 if (a->header.sh_type != SHT_NOBITS)
1138 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1140 struct obj_section **p;
1141 int prio = obj_load_order_prio(sec);
1142 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1143 if (obj_load_order_prio(*p) < prio)
1145 sec->load_next = *p;
1149 struct obj_section *obj_create_alloced_section(struct obj_file *f,
1151 unsigned long align,
1154 int newidx = f->header.e_shnum++;
1155 struct obj_section *sec;
1157 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1158 f->sections[newidx] = sec = arch_new_section();
1160 memset(sec, 0, sizeof(*sec));
1161 sec->header.sh_type = SHT_PROGBITS;
1162 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1163 sec->header.sh_size = size;
1164 sec->header.sh_addralign = align;
1168 sec->contents = xmalloc(size);
1170 obj_insert_section_load_order(f, sec);
1175 struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1177 unsigned long align,
1180 int newidx = f->header.e_shnum++;
1181 struct obj_section *sec;
1183 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1184 f->sections[newidx] = sec = arch_new_section();
1186 memset(sec, 0, sizeof(*sec));
1187 sec->header.sh_type = SHT_PROGBITS;
1188 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1189 sec->header.sh_size = size;
1190 sec->header.sh_addralign = align;
1194 sec->contents = xmalloc(size);
1196 sec->load_next = f->load_order;
1197 f->load_order = sec;
1198 if (f->load_order_search_start == &f->load_order)
1199 f->load_order_search_start = &sec->load_next;
1204 void *obj_extend_section(struct obj_section *sec, unsigned long more)
1206 unsigned long oldsize = sec->header.sh_size;
1207 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1208 return sec->contents + oldsize;
1213 /* Conditionally add the symbols from the given symbol set to the
1219 int idx, struct new_module_symbol *syms, size_t nsyms)
1221 struct new_module_symbol *s;
1225 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1227 /* Only add symbols that are already marked external. If we
1228 override locals we may cause problems for argument initialization.
1229 We will also create a false dependency on the module. */
1230 struct obj_symbol *sym;
1232 sym = obj_find_symbol(f, (char *) s->name);
1233 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1234 sym = obj_add_symbol(f, (char *) s->name, -1,
1235 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1237 /* Did our symbol just get installed? If so, mark the
1238 module as "used". */
1239 if (sym->secidx == idx)
1247 static void add_kernel_symbols(struct obj_file *f)
1249 struct external_module *m;
1252 /* Add module symbols first. */
1254 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1256 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1257 m->nsyms)) m->used = 1, ++nused;
1259 n_ext_modules_used = nused;
1261 /* And finally the symbols from the kernel proper. */
1264 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1267 static char *get_modinfo_value(struct obj_file *f, const char *key)
1269 struct obj_section *sec;
1270 char *p, *v, *n, *ep;
1271 size_t klen = strlen(key);
1273 sec = obj_find_section(f, ".modinfo");
1277 ep = p + sec->header.sh_size;
1280 n = strchr(p, '\0');
1282 if (p + klen == v && strncmp(p, key, klen) == 0)
1285 if (p + klen == n && strcmp(p, key) == 0)
1295 /*======================================================================*/
1296 /* Functions relating to module loading in pre 2.1 kernels. */
1299 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1303 struct obj_symbol *sym;
1307 if ((q = strchr(p, '=')) == NULL) {
1313 sym = obj_find_symbol(f, p);
1315 /* Also check that the parameter was not resolved from the kernel. */
1316 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1317 errorMsg("symbol for parameter %s not found\n", p);
1321 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1323 /* Do C quoting if we begin with a ". */
1327 str = alloca(strlen(q));
1328 for (r = str, q++; *q != '"'; ++q, ++r) {
1330 errorMsg("improperly terminated string argument for %s\n", p);
1332 } else if (*q == '\\')
1366 if (q[1] >= '0' && q[1] <= '7') {
1367 c = (c * 8) + *++q - '0';
1368 if (q[1] >= '0' && q[1] <= '7')
1369 c = (c * 8) + *++q - '0';
1382 obj_string_patch(f, sym->secidx, sym->value, str);
1383 } else if (*q >= '0' && *q <= '9') {
1385 *loc++ = strtoul(q, &q, 0);
1386 while (*q++ == ',');
1388 char *contents = f->sections[sym->secidx]->contents;
1389 char *loc = contents + sym->value;
1390 char *r; /* To search for commas */
1392 /* Break the string with comas */
1393 while ((r = strchr(q, ',')) != (char *) NULL) {
1395 obj_string_patch(f, sym->secidx, loc - contents, q);
1396 loc += sizeof(char *);
1401 obj_string_patch(f, sym->secidx, loc - contents, q);
1410 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1411 static int old_is_module_checksummed(struct obj_file *f)
1413 return obj_find_symbol(f, "Using_Versions") != NULL;
1415 /* Get the module's kernel version in the canonical integer form. */
1418 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1420 struct obj_symbol *sym;
1424 sym = obj_find_symbol(f, "kernel_version");
1428 p = f->sections[sym->secidx]->contents + sym->value;
1429 strncpy(str, p, STRVERSIONLEN);
1431 a = strtoul(p, &p, 10);
1434 b = strtoul(p + 1, &p, 10);
1437 c = strtoul(p + 1, &q, 10);
1441 return a << 16 | b << 8 | c;
1444 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1446 #ifdef BB_FEATURE_INSMOD_OLD_KERNEL
1448 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1450 static int old_get_kernel_symbols(const char *m_name)
1452 struct old_kernel_sym *ks, *k;
1453 struct new_module_symbol *s;
1454 struct external_module *mod;
1455 int nks, nms, nmod, i;
1457 nks = get_kernel_syms(NULL);
1459 errorMsg("get_kernel_syms: %s: %s", m_name, strerror(errno));
1463 ks = k = xmalloc(nks * sizeof(*ks));
1465 if (get_kernel_syms(ks) != nks) {
1466 perror("inconsistency with get_kernel_syms -- is someone else "
1467 "playing with modules?");
1472 /* Collect the module information. */
1477 while (k->name[0] == '#' && k->name[1]) {
1478 struct old_kernel_sym *k2;
1479 struct new_module_symbol *s;
1481 /* Find out how many symbols this module has. */
1482 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1486 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1487 mod[nmod].name = k->name + 1;
1488 mod[nmod].addr = k->value;
1490 mod[nmod].nsyms = nms;
1491 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1493 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1494 s->name = (unsigned long) k->name;
1495 s->value = k->value;
1502 n_ext_modules = nmod + 1;
1504 /* Now collect the symbols for the kernel proper. */
1506 if (k->name[0] == '#')
1509 nksyms = nms = nks - (k - ks);
1510 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1512 for (i = 0; i < nms; ++i, ++s, ++k) {
1513 s->name = (unsigned long) k->name;
1514 s->value = k->value;
1520 /* Return the kernel symbol checksum version, or zero if not used. */
1522 static int old_is_kernel_checksummed(void)
1524 /* Using_Versions is the first symbol. */
1526 && strcmp((char *) ksyms[0].name,
1527 "Using_Versions") == 0) return ksyms[0].value;
1533 static int old_create_mod_use_count(struct obj_file *f)
1535 struct obj_section *sec;
1537 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1540 obj_add_symbol(f, "mod_use_count_", -1,
1541 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1548 old_init_module(const char *m_name, struct obj_file *f,
1549 unsigned long m_size)
1552 struct old_mod_routines routines;
1553 struct old_symbol_table *symtab;
1556 /* Create the symbol table */
1558 int nsyms = 0, strsize = 0, total;
1560 /* Size things first... */
1563 for (i = 0; i < HASH_BUCKETS; ++i) {
1564 struct obj_symbol *sym;
1565 for (sym = f->symtab[i]; sym; sym = sym->next)
1566 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1567 && sym->secidx <= SHN_HIRESERVE)
1569 sym->ksymidx = nsyms++;
1570 strsize += strlen(sym->name) + 1;
1575 total = (sizeof(struct old_symbol_table)
1576 + nsyms * sizeof(struct old_module_symbol)
1577 + n_ext_modules_used * sizeof(struct old_module_ref)
1579 symtab = xmalloc(total);
1580 symtab->size = total;
1581 symtab->n_symbols = nsyms;
1582 symtab->n_refs = n_ext_modules_used;
1584 if (flag_export && nsyms) {
1585 struct old_module_symbol *ksym;
1589 ksym = symtab->symbol;
1590 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1591 + n_ext_modules_used * sizeof(struct old_module_ref));
1593 for (i = 0; i < HASH_BUCKETS; ++i) {
1594 struct obj_symbol *sym;
1595 for (sym = f->symtab[i]; sym; sym = sym->next)
1596 if (sym->ksymidx >= 0) {
1597 ksym->addr = obj_symbol_final_value(f, sym);
1599 (unsigned long) str - (unsigned long) symtab;
1601 str = stpcpy(str, sym->name) + 1;
1607 if (n_ext_modules_used) {
1608 struct old_module_ref *ref;
1611 ref = (struct old_module_ref *)
1612 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
1614 for (i = 0; i < n_ext_modules; ++i)
1615 if (ext_modules[i].used)
1616 ref++->module = ext_modules[i].addr;
1620 /* Fill in routines. */
1623 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
1625 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
1627 /* Whew! All of the initialization is complete. Collect the final
1628 module image and give it to the kernel. */
1630 image = xmalloc(m_size);
1631 obj_create_image(f, image);
1633 /* image holds the complete relocated module, accounting correctly for
1634 mod_use_count. However the old module kernel support assume that
1635 it is receiving something which does not contain mod_use_count. */
1636 ret = old_sys_init_module(m_name, image + sizeof(long),
1637 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
1638 : 0), &routines, symtab);
1640 errorMsg("init_module: %s: %s", m_name, strerror(errno));
1650 #define old_create_mod_use_count(x) TRUE
1651 #define old_init_module(x, y, z) TRUE
1653 #endif /* BB_FEATURE_INSMOD_OLD_KERNEL */
1657 /*======================================================================*/
1658 /* Functions relating to module loading after 2.1.18. */
1661 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
1665 struct obj_symbol *sym;
1666 char *contents, *loc;
1670 if ((q = strchr(p, '=')) == NULL) {
1675 key = alloca(q - p + 6);
1676 memcpy(key, "parm_", 5);
1677 memcpy(key + 5, p, q - p);
1680 p = get_modinfo_value(f, key);
1683 errorMsg("invalid parameter %s\n", key);
1687 sym = obj_find_symbol(f, key);
1689 /* Also check that the parameter was not resolved from the kernel. */
1690 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1691 errorMsg("symbol for parameter %s not found\n", key);
1696 min = strtoul(p, &p, 10);
1698 max = strtoul(p + 1, &p, 10);
1704 contents = f->sections[sym->secidx]->contents;
1705 loc = contents + sym->value;
1709 if ((*p == 's') || (*p == 'c')) {
1712 /* Do C quoting if we begin with a ", else slurp the lot. */
1716 str = alloca(strlen(q));
1717 for (r = str, q++; *q != '"'; ++q, ++r) {
1719 errorMsg("improperly terminated string argument for %s\n",
1722 } else if (*q == '\\')
1756 if (q[1] >= '0' && q[1] <= '7') {
1757 c = (c * 8) + *++q - '0';
1758 if (q[1] >= '0' && q[1] <= '7')
1759 c = (c * 8) + *++q - '0';
1776 /* In this case, the string is not quoted. We will break
1777 it using the coma (like for ints). If the user wants to
1778 include comas in a string, he just has to quote it */
1780 /* Search the next coma */
1784 if (r != (char *) NULL) {
1785 /* Recopy the current field */
1786 str = alloca(r - q + 1);
1787 memcpy(str, q, r - q);
1789 /* I don't know if it is usefull, as the previous case
1790 doesn't null terminate the string ??? */
1793 /* Keep next fields */
1804 obj_string_patch(f, sym->secidx, loc - contents, str);
1805 loc += tgt_sizeof_char_p;
1807 /* Array of chars (in fact, matrix !) */
1808 unsigned long charssize; /* size of each member */
1810 /* Get the size of each member */
1811 /* Probably we should do that outside the loop ? */
1812 if (!isdigit(*(p + 1))) {
1813 errorMsg("parameter type 'c' for %s must be followed by"
1814 " the maximum size\n", key);
1817 charssize = strtoul(p + 1, (char **) NULL, 10);
1820 if (strlen(str) >= charssize) {
1821 errorMsg("string too long for %s (max %ld)\n", key,
1826 /* Copy to location */
1827 strcpy((char *) loc, str);
1831 long v = strtoul(q, &q, 0);
1838 loc += tgt_sizeof_short;
1842 loc += tgt_sizeof_int;
1846 loc += tgt_sizeof_long;
1850 errorMsg("unknown parameter type '%c' for %s\n", *p, key);
1865 goto retry_end_of_value;
1869 errorMsg("too many values for %s (max %d)\n", key, max);
1876 errorMsg("invalid argument syntax for %s\n", key);
1883 errorMsg("too few values for %s (min %d)\n", key, min);
1893 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1894 static int new_is_module_checksummed(struct obj_file *f)
1896 const char *p = get_modinfo_value(f, "using_checksums");
1903 /* Get the module's kernel version in the canonical integer form. */
1906 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1911 p = get_modinfo_value(f, "kernel_version");
1914 strncpy(str, p, STRVERSIONLEN);
1916 a = strtoul(p, &p, 10);
1919 b = strtoul(p + 1, &p, 10);
1922 c = strtoul(p + 1, &q, 10);
1926 return a << 16 | b << 8 | c;
1929 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1932 #ifdef BB_FEATURE_INSMOD_NEW_KERNEL
1934 /* Fetch the loaded modules, and all currently exported symbols. */
1936 static int new_get_kernel_symbols(void)
1938 char *module_names, *mn;
1939 struct external_module *modules, *m;
1940 struct new_module_symbol *syms, *s;
1941 size_t ret, bufsize, nmod, nsyms, i, j;
1943 /* Collect the loaded modules. */
1945 module_names = xmalloc(bufsize = 256);
1947 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
1948 if (errno == ENOSPC) {
1949 module_names = xrealloc(module_names, bufsize = ret);
1950 goto retry_modules_load;
1952 errorMsg("QM_MODULES: %s", strerror(errno));
1956 n_ext_modules = nmod = ret;
1957 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
1958 memset(modules, 0, nmod * sizeof(*modules));
1960 /* Collect the modules' symbols. */
1962 for (i = 0, mn = module_names, m = modules;
1963 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
1964 struct new_module_info info;
1966 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
1967 if (errno == ENOENT) {
1968 /* The module was removed out from underneath us. */
1971 errorMsg("query_module: QM_INFO: %s: %s", mn, strerror(errno));
1975 syms = xmalloc(bufsize = 1024);
1977 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
1980 syms = xrealloc(syms, bufsize = ret);
1981 goto retry_mod_sym_load;
1983 /* The module was removed out from underneath us. */
1986 errorMsg("query_module: QM_SYMBOLS: %s: %s", mn, strerror(errno));
1993 m->addr = info.addr;
1997 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
1998 s->name += (unsigned long) syms;
2002 /* Collect the kernel's symbols. */
2004 syms = xmalloc(bufsize = 16 * 1024);
2005 retry_kern_sym_load:
2006 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2007 if (errno == ENOSPC) {
2008 syms = xrealloc(syms, bufsize = ret);
2009 goto retry_kern_sym_load;
2011 errorMsg("kernel: QM_SYMBOLS: %s", strerror(errno));
2014 nksyms = nsyms = ret;
2017 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2018 s->name += (unsigned long) syms;
2024 /* Return the kernel symbol checksum version, or zero if not used. */
2026 static int new_is_kernel_checksummed(void)
2028 struct new_module_symbol *s;
2031 /* Using_Versions is not the first symbol, but it should be in there. */
2033 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2034 if (strcmp((char *) s->name, "Using_Versions") == 0)
2041 static int new_create_this_module(struct obj_file *f, const char *m_name)
2043 struct obj_section *sec;
2045 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2046 sizeof(struct new_module));
2047 memset(sec->contents, 0, sizeof(struct new_module));
2049 obj_add_symbol(f, "__this_module", -1,
2050 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2051 sizeof(struct new_module));
2053 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2060 static int new_create_module_ksymtab(struct obj_file *f)
2062 struct obj_section *sec;
2065 /* We must always add the module references. */
2067 if (n_ext_modules_used) {
2068 struct new_module_ref *dep;
2069 struct obj_symbol *tm;
2071 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2072 (sizeof(struct new_module_ref)
2073 * n_ext_modules_used));
2077 tm = obj_find_symbol(f, "__this_module");
2078 dep = (struct new_module_ref *) sec->contents;
2079 for (i = 0; i < n_ext_modules; ++i)
2080 if (ext_modules[i].used) {
2081 dep->dep = ext_modules[i].addr;
2082 obj_symbol_patch(f, sec->idx,
2083 (char *) &dep->ref - sec->contents, tm);
2089 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2094 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2097 /* We don't want to export symbols residing in sections that
2098 aren't loaded. There are a number of these created so that
2099 we make sure certain module options don't appear twice. */
2101 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2103 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2105 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2106 struct obj_symbol *sym;
2107 for (sym = f->symtab[i]; sym; sym = sym->next)
2108 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2109 && sym->secidx <= SHN_HIRESERVE
2110 && (sym->secidx >= SHN_LORESERVE
2111 || loaded[sym->secidx])) {
2112 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2114 obj_symbol_patch(f, sec->idx, ofs, sym);
2115 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2122 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2130 new_init_module(const char *m_name, struct obj_file *f,
2131 unsigned long m_size)
2133 struct new_module *module;
2134 struct obj_section *sec;
2139 sec = obj_find_section(f, ".this");
2140 module = (struct new_module *) sec->contents;
2141 m_addr = sec->header.sh_addr;
2143 module->size_of_struct = sizeof(*module);
2144 module->size = m_size;
2145 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2147 sec = obj_find_section(f, "__ksymtab");
2148 if (sec && sec->header.sh_size) {
2149 module->syms = sec->header.sh_addr;
2150 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2153 if (n_ext_modules_used) {
2154 sec = obj_find_section(f, ".kmodtab");
2155 module->deps = sec->header.sh_addr;
2156 module->ndeps = n_ext_modules_used;
2160 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2162 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2164 sec = obj_find_section(f, "__ex_table");
2166 module->ex_table_start = sec->header.sh_addr;
2167 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2170 sec = obj_find_section(f, ".text.init");
2172 module->runsize = sec->header.sh_addr - m_addr;
2174 sec = obj_find_section(f, ".data.init");
2176 if (!module->runsize ||
2177 module->runsize > sec->header.sh_addr - m_addr)
2178 module->runsize = sec->header.sh_addr - m_addr;
2181 if (!arch_init_module(f, module))
2184 /* Whew! All of the initialization is complete. Collect the final
2185 module image and give it to the kernel. */
2187 image = xmalloc(m_size);
2188 obj_create_image(f, image);
2190 ret = new_sys_init_module(m_name, (struct new_module *) image);
2192 errorMsg("init_module: %s: %s", m_name, strerror(errno));
2201 #define new_init_module(x, y, z) TRUE
2202 #define new_create_this_module(x, y) 0
2203 #define new_create_module_ksymtab(x)
2205 #endif /* BB_FEATURE_INSMOD_OLD_KERNEL */
2208 /*======================================================================*/
2211 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2214 struct obj_string_patch *p;
2215 struct obj_section *strsec;
2216 size_t len = strlen(string) + 1;
2219 p = xmalloc(sizeof(*p));
2220 p->next = f->string_patches;
2221 p->reloc_secidx = secidx;
2222 p->reloc_offset = offset;
2223 f->string_patches = p;
2225 strsec = obj_find_section(f, ".kstrtab");
2226 if (strsec == NULL) {
2227 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2228 p->string_offset = 0;
2229 loc = strsec->contents;
2231 p->string_offset = strsec->header.sh_size;
2232 loc = obj_extend_section(strsec, len);
2234 memcpy(loc, string, len);
2240 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2241 struct obj_symbol *sym)
2243 struct obj_symbol_patch *p;
2245 p = xmalloc(sizeof(*p));
2246 p->next = f->symbol_patches;
2247 p->reloc_secidx = secidx;
2248 p->reloc_offset = offset;
2250 f->symbol_patches = p;
2255 int obj_check_undefineds(struct obj_file *f)
2260 for (i = 0; i < HASH_BUCKETS; ++i) {
2261 struct obj_symbol *sym;
2262 for (sym = f->symtab[i]; sym; sym = sym->next)
2263 if (sym->secidx == SHN_UNDEF) {
2264 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2265 sym->secidx = SHN_ABS;
2268 errorMsg("unresolved symbol %s\n", sym->name);
2277 void obj_allocate_commons(struct obj_file *f)
2279 struct common_entry {
2280 struct common_entry *next;
2281 struct obj_symbol *sym;
2282 } *common_head = NULL;
2286 for (i = 0; i < HASH_BUCKETS; ++i) {
2287 struct obj_symbol *sym;
2288 for (sym = f->symtab[i]; sym; sym = sym->next)
2289 if (sym->secidx == SHN_COMMON) {
2290 /* Collect all COMMON symbols and sort them by size so as to
2291 minimize space wasted by alignment requirements. */
2293 struct common_entry **p, *n;
2294 for (p = &common_head; *p; p = &(*p)->next)
2295 if (sym->size <= (*p)->sym->size)
2298 n = alloca(sizeof(*n));
2306 for (i = 1; i < f->local_symtab_size; ++i) {
2307 struct obj_symbol *sym = f->local_symtab[i];
2308 if (sym && sym->secidx == SHN_COMMON) {
2309 struct common_entry **p, *n;
2310 for (p = &common_head; *p; p = &(*p)->next)
2311 if (sym == (*p)->sym)
2313 else if (sym->size < (*p)->sym->size) {
2314 n = alloca(sizeof(*n));
2324 /* Find the bss section. */
2325 for (i = 0; i < f->header.e_shnum; ++i)
2326 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2329 /* If for some reason there hadn't been one, create one. */
2330 if (i == f->header.e_shnum) {
2331 struct obj_section *sec;
2333 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2334 f->sections[i] = sec = arch_new_section();
2335 f->header.e_shnum = i + 1;
2337 memset(sec, 0, sizeof(*sec));
2338 sec->header.sh_type = SHT_PROGBITS;
2339 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2344 /* Allocate the COMMONS. */
2346 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2347 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2348 struct common_entry *c;
2350 for (c = common_head; c; c = c->next) {
2351 ElfW(Addr) align = c->sym->value;
2353 if (align > max_align)
2355 if (bss_size & (align - 1))
2356 bss_size = (bss_size | (align - 1)) + 1;
2359 c->sym->value = bss_size;
2361 bss_size += c->sym->size;
2364 f->sections[i]->header.sh_size = bss_size;
2365 f->sections[i]->header.sh_addralign = max_align;
2369 /* For the sake of patch relocation and parameter initialization,
2370 allocate zeroed data for NOBITS sections now. Note that after
2371 this we cannot assume NOBITS are really empty. */
2372 for (i = 0; i < f->header.e_shnum; ++i) {
2373 struct obj_section *s = f->sections[i];
2374 if (s->header.sh_type == SHT_NOBITS) {
2375 s->contents = memset(xmalloc(s->header.sh_size),
2376 0, s->header.sh_size);
2377 s->header.sh_type = SHT_PROGBITS;
2382 unsigned long obj_load_size(struct obj_file *f)
2384 unsigned long dot = 0;
2385 struct obj_section *sec;
2387 /* Finalize the positions of the sections relative to one another. */
2389 for (sec = f->load_order; sec; sec = sec->load_next) {
2392 align = sec->header.sh_addralign;
2393 if (align && (dot & (align - 1)))
2394 dot = (dot | (align - 1)) + 1;
2396 sec->header.sh_addr = dot;
2397 dot += sec->header.sh_size;
2403 int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2405 int i, n = f->header.e_shnum;
2408 /* Finalize the addresses of the sections. */
2411 for (i = 0; i < n; ++i)
2412 f->sections[i]->header.sh_addr += base;
2414 /* And iterate over all of the relocations. */
2416 for (i = 0; i < n; ++i) {
2417 struct obj_section *relsec, *symsec, *targsec, *strsec;
2418 ElfW(RelM) * rel, *relend;
2422 relsec = f->sections[i];
2423 if (relsec->header.sh_type != SHT_RELM)
2426 symsec = f->sections[relsec->header.sh_link];
2427 targsec = f->sections[relsec->header.sh_info];
2428 strsec = f->sections[symsec->header.sh_link];
2430 rel = (ElfW(RelM) *) relsec->contents;
2431 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2432 symtab = (ElfW(Sym) *) symsec->contents;
2433 strtab = (const char *) strsec->contents;
2435 for (; rel < relend; ++rel) {
2436 ElfW(Addr) value = 0;
2437 struct obj_symbol *intsym = NULL;
2438 unsigned long symndx;
2439 ElfW(Sym) * extsym = 0;
2442 /* Attempt to find a value to use for this relocation. */
2444 symndx = ELFW(R_SYM) (rel->r_info);
2446 /* Note we've already checked for undefined symbols. */
2448 extsym = &symtab[symndx];
2449 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2450 /* Local symbols we look up in the local table to be sure
2451 we get the one that is really intended. */
2452 intsym = f->local_symtab[symndx];
2454 /* Others we look up in the hash table. */
2456 if (extsym->st_name)
2457 name = strtab + extsym->st_name;
2459 name = f->sections[extsym->st_shndx]->name;
2460 intsym = obj_find_symbol(f, name);
2463 value = obj_symbol_final_value(f, intsym);
2464 intsym->referenced = 1;
2466 #if SHT_RELM == SHT_RELA
2467 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2468 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2469 if (!extsym || !extsym->st_name ||
2470 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2472 value += rel->r_addend;
2476 switch (arch_apply_relocation
2477 (f, targsec, symsec, intsym, rel, value)) {
2481 case obj_reloc_overflow:
2482 errmsg = "Relocation overflow";
2484 case obj_reloc_dangerous:
2485 errmsg = "Dangerous relocation";
2487 case obj_reloc_unhandled:
2488 errmsg = "Unhandled relocation";
2491 errorMsg("%s of type %ld for %s\n", errmsg,
2492 (long) ELFW(R_TYPE) (rel->r_info),
2493 strtab + extsym->st_name);
2495 errorMsg("%s of type %ld\n", errmsg,
2496 (long) ELFW(R_TYPE) (rel->r_info));
2504 /* Finally, take care of the patches. */
2506 if (f->string_patches) {
2507 struct obj_string_patch *p;
2508 struct obj_section *strsec;
2509 ElfW(Addr) strsec_base;
2510 strsec = obj_find_section(f, ".kstrtab");
2511 strsec_base = strsec->header.sh_addr;
2513 for (p = f->string_patches; p; p = p->next) {
2514 struct obj_section *targsec = f->sections[p->reloc_secidx];
2515 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2516 = strsec_base + p->string_offset;
2520 if (f->symbol_patches) {
2521 struct obj_symbol_patch *p;
2523 for (p = f->symbol_patches; p; p = p->next) {
2524 struct obj_section *targsec = f->sections[p->reloc_secidx];
2525 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2526 = obj_symbol_final_value(f, p->sym);
2533 int obj_create_image(struct obj_file *f, char *image)
2535 struct obj_section *sec;
2536 ElfW(Addr) base = f->baseaddr;
2538 for (sec = f->load_order; sec; sec = sec->load_next) {
2541 if (sec->header.sh_size == 0)
2544 secimg = image + (sec->header.sh_addr - base);
2546 /* Note that we allocated data for NOBITS sections earlier. */
2547 memcpy(secimg, sec->contents, sec->header.sh_size);
2553 /*======================================================================*/
2555 struct obj_file *obj_load(FILE * fp)
2558 ElfW(Shdr) * section_headers;
2562 /* Read the file header. */
2564 f = arch_new_file();
2565 memset(f, 0, sizeof(*f));
2566 f->symbol_cmp = strcmp;
2567 f->symbol_hash = obj_elf_hash;
2568 f->load_order_search_start = &f->load_order;
2570 fseek(fp, 0, SEEK_SET);
2571 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2572 errorMsg("error reading ELF header: %s", strerror(errno));
2576 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2577 || f->header.e_ident[EI_MAG1] != ELFMAG1
2578 || f->header.e_ident[EI_MAG2] != ELFMAG2
2579 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2580 errorMsg("not an ELF file\n");
2583 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2584 || f->header.e_ident[EI_DATA] != ELFDATAM
2585 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2586 || !MATCH_MACHINE(f->header.e_machine)) {
2587 errorMsg("ELF file not for this architecture\n");
2590 if (f->header.e_type != ET_REL) {
2591 errorMsg("ELF file not a relocatable object\n");
2595 /* Read the section headers. */
2597 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
2598 errorMsg("section header size mismatch: %lu != %lu\n",
2599 (unsigned long) f->header.e_shentsize,
2600 (unsigned long) sizeof(ElfW(Shdr)));
2604 shnum = f->header.e_shnum;
2605 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
2606 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
2608 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
2609 fseek(fp, f->header.e_shoff, SEEK_SET);
2610 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
2611 errorMsg("error reading ELF section headers: %s", strerror(errno));
2615 /* Read the section data. */
2617 for (i = 0; i < shnum; ++i) {
2618 struct obj_section *sec;
2620 f->sections[i] = sec = arch_new_section();
2621 memset(sec, 0, sizeof(*sec));
2623 sec->header = section_headers[i];
2626 switch (sec->header.sh_type) {
2637 if (sec->header.sh_size > 0) {
2638 sec->contents = xmalloc(sec->header.sh_size);
2639 fseek(fp, sec->header.sh_offset, SEEK_SET);
2640 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
2641 errorMsg("error reading ELF section data: %s", strerror(errno));
2645 sec->contents = NULL;
2649 #if SHT_RELM == SHT_REL
2651 errorMsg("RELA relocations not supported on this architecture\n");
2655 errorMsg("REL relocations not supported on this architecture\n");
2660 if (sec->header.sh_type >= SHT_LOPROC) {
2661 /* Assume processor specific section types are debug
2662 info and can safely be ignored. If this is ever not
2663 the case (Hello MIPS?), don't put ifdefs here but
2664 create an arch_load_proc_section(). */
2668 errorMsg("can't handle sections of type %ld\n",
2669 (long) sec->header.sh_type);
2674 /* Do what sort of interpretation as needed by each section. */
2676 shstrtab = f->sections[f->header.e_shstrndx]->contents;
2678 for (i = 0; i < shnum; ++i) {
2679 struct obj_section *sec = f->sections[i];
2680 sec->name = shstrtab + sec->header.sh_name;
2683 for (i = 0; i < shnum; ++i) {
2684 struct obj_section *sec = f->sections[i];
2686 if (sec->header.sh_flags & SHF_ALLOC)
2687 obj_insert_section_load_order(f, sec);
2689 switch (sec->header.sh_type) {
2692 unsigned long nsym, j;
2696 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
2697 errorMsg("symbol size mismatch: %lu != %lu\n",
2698 (unsigned long) sec->header.sh_entsize,
2699 (unsigned long) sizeof(ElfW(Sym)));
2703 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
2704 strtab = f->sections[sec->header.sh_link]->contents;
2705 sym = (ElfW(Sym) *) sec->contents;
2707 /* Allocate space for a table of local symbols. */
2708 j = f->local_symtab_size = sec->header.sh_info;
2709 f->local_symtab = xmalloc(j *=
2710 sizeof(struct obj_symbol *));
2711 memset(f->local_symtab, 0, j);
2713 /* Insert all symbols into the hash table. */
2714 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
2717 name = strtab + sym->st_name;
2719 name = f->sections[sym->st_shndx]->name;
2721 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
2722 sym->st_value, sym->st_size);
2728 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
2729 errorMsg("relocation entry size mismatch: %lu != %lu\n",
2730 (unsigned long) sec->header.sh_entsize,
2731 (unsigned long) sizeof(ElfW(RelM)));
2741 static void hide_special_symbols(struct obj_file *f)
2743 static const char *const specials[] = {
2750 struct obj_symbol *sym;
2751 const char *const *p;
2753 for (p = specials; *p; ++p)
2754 if ((sym = obj_find_symbol(f, *p)) != NULL)
2756 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
2761 extern int insmod_main( int argc, char **argv)
2767 unsigned long m_size;
2771 char m_name[BUFSIZ + 1] = "\0";
2772 int exit_status = FALSE;
2774 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2776 char k_strversion[STRVERSIONLEN];
2777 char m_strversion[STRVERSIONLEN];
2784 usage(insmod_usage);
2787 /* Parse any options */
2788 while (--argc > 0 && **(++argv) == '-') {
2789 while (*(++(*argv))) {
2791 case 'f': /* force loading */
2792 flag_force_load = 1;
2794 case 'k': /* module loaded by kerneld, auto-cleanable */
2797 case 'v': /* verbose output */
2800 case 'x': /* do not export externs */
2804 usage(insmod_usage);
2810 usage(insmod_usage);
2812 /* Grab the module name */
2813 if ((tmp = strrchr(*argv, '/')) != NULL) {
2820 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
2822 memcpy(m_name, tmp, len);
2823 strcpy(m_fullName, m_name);
2824 strcat(m_fullName, ".o");
2826 /* Get a filedesc for the module */
2827 if ((fp = fopen(*argv, "r")) == NULL) {
2828 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
2829 if (recursiveAction(_PATH_MODULES, TRUE, FALSE, FALSE,
2830 findNamedModule, 0, m_fullName) == FALSE)
2832 if (m_filename[0] == '\0'
2833 || ((fp = fopen(m_filename, "r")) == NULL))
2835 errorMsg("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
2839 fatalError("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
2841 memcpy(m_filename, *argv, strlen(*argv));
2844 if ((f = obj_load(fp)) == NULL) {
2845 perror("Could not load the module\n");
2849 if (get_modinfo_value(f, "kernel_version") == NULL)
2854 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2855 /* Version correspondence? */
2857 k_version = get_kernel_version(k_strversion);
2858 if (m_has_modinfo) {
2859 m_version = new_get_module_version(f, m_strversion);
2861 m_version = old_get_module_version(f, m_strversion);
2862 if (m_version == -1) {
2863 errorMsg("couldn't find the kernel version the module was "
2869 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
2870 if (flag_force_load) {
2871 errorMsg("Warning: kernel-module version mismatch\n"
2872 "\t%s was compiled for kernel version %s\n"
2873 "\twhile this kernel is version %s\n",
2874 m_filename, m_strversion, k_strversion);
2876 errorMsg("kernel-module version mismatch\n"
2877 "\t%s was compiled for kernel version %s\n"
2878 "\twhile this kernel is version %s.\n",
2879 m_filename, m_strversion, k_strversion);
2884 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2886 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
2888 if (k_new_syscalls) {
2889 #ifdef BB_FEATURE_INSMOD_NEW_KERNEL
2890 if (!new_get_kernel_symbols())
2892 k_crcs = new_is_kernel_checksummed();
2894 errorMsg("Not configured to support new kernels\n");
2898 #ifdef BB_FEATURE_INSMOD_OLD_KERNEL
2899 if (!old_get_kernel_symbols(m_name))
2901 k_crcs = old_is_kernel_checksummed();
2903 errorMsg("Not configured to support old kernels\n");
2908 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2910 m_crcs = new_is_module_checksummed(f);
2912 m_crcs = old_is_module_checksummed(f);
2914 if (m_crcs != k_crcs)
2915 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
2916 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2918 /* Let the module know about the kernel symbols. */
2919 add_kernel_symbols(f);
2921 /* Allocate common symbols, symbol tables, and string tables. */
2924 ? !new_create_this_module(f, m_name)
2925 : !old_create_mod_use_count(f))
2930 if (!obj_check_undefineds(f)) {
2933 obj_allocate_commons(f);
2935 if (optind < argc) {
2937 ? !new_process_module_arguments(f, argc - optind, argv + optind)
2938 : !old_process_module_arguments(f, argc - optind, argv + optind))
2945 hide_special_symbols(f);
2948 new_create_module_ksymtab(f);
2950 /* Find current size of the module */
2951 m_size = obj_load_size(f);
2955 m_addr = create_module(m_name, m_size);
2960 errorMsg("A module named %s already exists\n", m_name);
2963 errorMsg("Can't allocate kernel memory for module; needed %lu bytes\n",
2967 errorMsg("create_module: %s: %s\n", m_name, strerror(errno));
2971 if (!obj_relocate(f, m_addr)) {
2972 delete_module(m_name);
2977 ? !new_init_module(m_name, f, m_size)
2978 : !old_init_module(m_name, f, m_size))
2980 delete_module(m_name);
2988 return(exit_status);