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.24 2000/09/24 03:44:29 andersen 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.24 2000/09/24 03:44:29 andersen 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 */
326 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
327 #define SHT_RELM SHT_REL
328 #define Elf32_RelM Elf32_Rel
333 # if ELFCLASSM == ELFCLASS32
334 # define ElfW(x) Elf32_ ## x
335 # define ELFW(x) ELF32_ ## x
337 # define ElfW(x) Elf64_ ## x
338 # define ELFW(x) ELF64_ ## x
342 /* For some reason this is missing from libc5. */
343 #ifndef ELF32_ST_INFO
344 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
347 #ifndef ELF64_ST_INFO
348 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
351 struct obj_string_patch;
352 struct obj_symbol_patch;
359 struct obj_section *load_next;
365 struct obj_symbol *next; /* hash table link */
369 int secidx; /* the defining section index/module */
371 int ksymidx; /* for export to the kernel symtab */
372 int referenced; /* actually used in the link */
375 /* Hardcode the hash table size. We shouldn't be needing so many
376 symbols that we begin to degrade performance, and we get a big win
377 by giving the compiler a constant divisor. */
379 #define HASH_BUCKETS 521
385 struct obj_section **sections;
386 struct obj_section *load_order;
387 struct obj_section **load_order_search_start;
388 struct obj_string_patch *string_patches;
389 struct obj_symbol_patch *symbol_patches;
390 int (*symbol_cmp)(const char *, const char *);
391 unsigned long (*symbol_hash)(const char *);
392 unsigned long local_symtab_size;
393 struct obj_symbol **local_symtab;
394 struct obj_symbol *symtab[HASH_BUCKETS];
405 struct obj_string_patch
407 struct obj_string_patch *next;
409 ElfW(Addr) reloc_offset;
410 ElfW(Addr) string_offset;
413 struct obj_symbol_patch
415 struct obj_symbol_patch *next;
417 ElfW(Addr) reloc_offset;
418 struct obj_symbol *sym;
422 /* Generic object manipulation routines. */
424 unsigned long obj_elf_hash(const char *);
426 unsigned long obj_elf_hash_n(const char *, unsigned long len);
428 struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
429 unsigned long symidx, int info, int secidx,
430 ElfW(Addr) value, unsigned long size);
432 struct obj_symbol *obj_find_symbol (struct obj_file *f,
435 ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
436 struct obj_symbol *sym);
438 void obj_set_symbol_compare(struct obj_file *f,
439 int (*cmp)(const char *, const char *),
440 unsigned long (*hash)(const char *));
442 struct obj_section *obj_find_section (struct obj_file *f,
445 void obj_insert_section_load_order (struct obj_file *f,
446 struct obj_section *sec);
448 struct obj_section *obj_create_alloced_section (struct obj_file *f,
453 struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
458 void *obj_extend_section (struct obj_section *sec, unsigned long more);
460 int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
463 int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
464 struct obj_symbol *sym);
466 int obj_check_undefineds(struct obj_file *f);
468 void obj_allocate_commons(struct obj_file *f);
470 unsigned long obj_load_size (struct obj_file *f);
472 int obj_relocate (struct obj_file *f, ElfW(Addr) base);
474 struct obj_file *obj_load(FILE *f);
476 int obj_create_image (struct obj_file *f, char *image);
478 /* Architecture specific manipulation routines. */
480 struct obj_file *arch_new_file (void);
482 struct obj_section *arch_new_section (void);
484 struct obj_symbol *arch_new_symbol (void);
486 enum obj_reloc arch_apply_relocation (struct obj_file *f,
487 struct obj_section *targsec,
488 struct obj_section *symsec,
489 struct obj_symbol *sym,
490 ElfW(RelM) *rel, ElfW(Addr) value);
492 int arch_create_got (struct obj_file *f);
495 int arch_init_module (struct obj_file *f, struct new_module *);
498 //----------------------------------------------------------------------------
499 //--------end of modutils obj.h
500 //----------------------------------------------------------------------------
506 #define _PATH_MODULES "/lib/modules"
507 #define STRVERSIONLEN 32
509 #if !defined(BB_FEATURE_INSMOD_NEW_KERNEL) && !defined(BB_FEATURE_INSMOD_OLD_KERNEL)
510 #error "Must have ether BB_FEATURE_INSMOD_NEW_KERNEL or BB_FEATURE_INSMOD_OLD_KERNEL defined"
513 /*======================================================================*/
515 int flag_force_load = 0;
516 int flag_autoclean = 0;
517 int flag_verbose = 0;
521 /*======================================================================*/
523 /* previously, these were named i386_* but since we could be
524 compiling for the sh, I've renamed them to the more general
525 arch_* These structures are the same between the x86 and SH,
526 and we can't support anything else right now anyway. In the
527 future maybe they should be #if defined'd */
529 struct arch_got_entry {
531 unsigned offset_done:1;
532 unsigned reloc_done:1;
536 struct obj_file root;
537 struct obj_section *got;
541 struct obj_symbol root;
542 struct arch_got_entry gotent;
546 struct external_module {
551 struct new_module_symbol *syms;
554 struct new_module_symbol *ksyms;
557 struct external_module *ext_modules;
559 int n_ext_modules_used;
563 /* Some firendly syscalls to cheer everyone's day... */
564 #define __NR_new_sys_init_module __NR_init_module
565 _syscall2(int, new_sys_init_module, const char *, name,
566 const struct new_module *, info)
567 #define __NR_old_sys_init_module __NR_init_module
568 _syscall5(int, old_sys_init_module, const char *, name, char *, code,
569 unsigned, codesize, struct old_mod_routines *, routines,
570 struct old_symbol_table *, symtab)
572 _syscall1(int, delete_module, const char *, name)
574 extern int delete_module(const char *);
577 /* This is kind of troublesome. See, we don't actually support
578 the m68k or the arm the same way we support i386 and (now)
579 sh. In doing my SH patch, I just assumed that whatever works
580 for i386 also works for m68k and arm since currently insmod.c
581 does nothing special for them. If this isn't true, the below
582 line is rather misleading IMHO, and someone should either
583 change it or add more proper architecture-dependent support
586 -- Bryan Rittmeyer <bryan@ixiacom.com> */
588 #if defined(__i386__) || defined(__m68k__) || defined(__arm__)
589 /* Jump through hoops to fixup error return codes */
590 #define __NR__create_module __NR_create_module
591 static inline _syscall2(long, _create_module, const char *, name, size_t,
593 unsigned long create_module(const char *name, size_t size)
595 long ret = _create_module(name, size);
597 if (ret == -1 && errno > 125) {
604 _syscall2(unsigned long, create_module, const char *, name, size_t, size)
606 static char m_filename[BUFSIZ + 1] = "\0";
607 static char m_fullName[BUFSIZ + 1] = "\0";
609 /*======================================================================*/
612 static int findNamedModule(const char *fileName, struct stat *statbuf,
615 char *fullName = (char *) userDate;
618 if (fullName[0] == '\0')
621 char *tmp = strrchr(fileName, '/');
624 tmp = (char *) fileName;
627 if (check_wildcard_match(tmp, fullName) == TRUE) {
628 /* Stop searching if we find a match */
629 memcpy(m_filename, fileName, strlen(fileName)+1);
637 /*======================================================================*/
639 struct obj_file *arch_new_file(void)
642 f = xmalloc(sizeof(*f));
647 struct obj_section *arch_new_section(void)
649 return xmalloc(sizeof(struct obj_section));
652 struct obj_symbol *arch_new_symbol(void)
654 struct arch_symbol *sym;
655 sym = xmalloc(sizeof(*sym));
656 memset(&sym->gotent, 0, sizeof(sym->gotent));
661 arch_apply_relocation(struct obj_file *f,
662 struct obj_section *targsec,
663 struct obj_section *symsec,
664 struct obj_symbol *sym,
666 Elf32_Rela * rel, Elf32_Addr v)
668 Elf32_Rel * rel, Elf32_Addr v)
671 struct arch_file *ifile = (struct arch_file *) f;
672 struct arch_symbol *isym = (struct arch_symbol *) sym;
674 Elf32_Addr *loc = (Elf32_Addr *) (targsec->contents + rel->r_offset);
675 Elf32_Addr dot = targsec->header.sh_addr + rel->r_offset;
676 Elf32_Addr got = ifile->got ? ifile->got->header.sh_addr : 0;
678 enum obj_reloc ret = obj_reloc_ok;
680 switch (ELF32_R_TYPE(rel->r_info)) {
682 /* even though these constants seem to be the same for
683 the i386 and the sh, we "#if define" them for clarity
684 and in case that ever changes */
730 *loc += f->baseaddr + rel->r_addend;
741 *loc += got - dot + rel->r_addend;;
752 assert(isym != NULL);
753 if (!isym->gotent.reloc_done) {
754 isym->gotent.reloc_done = 1;
755 *(Elf32_Addr *) (ifile->got->contents + isym->gotent.offset) =
758 *loc += isym->gotent.offset + rel->r_addend;
762 assert(isym != NULL);
763 if (!isym->gotent.reloc_done) {
764 isym->gotent.reloc_done = 1;
765 *(Elf32_Addr *) (ifile->got->contents + isym->gotent.offset) =
768 *loc += isym->gotent.offset;
782 ret = obj_reloc_unhandled;
789 int arch_create_got(struct obj_file *f)
791 struct arch_file *ifile = (struct arch_file *) f;
792 int i, n, offset = 0, gotneeded = 0;
794 n = ifile->root.header.e_shnum;
795 for (i = 0; i < n; ++i) {
796 struct obj_section *relsec, *symsec, *strsec;
798 Elf32_Rela *rel, *relend;
800 Elf32_Rel *rel, *relend;
805 relsec = ifile->root.sections[i];
806 if (relsec->header.sh_type != SHT_REL)
809 symsec = ifile->root.sections[relsec->header.sh_link];
810 strsec = ifile->root.sections[symsec->header.sh_link];
814 rel = (Elf32_Rela *) relsec->contents;
815 relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rela));
817 rel = (Elf32_Rel *) relsec->contents;
818 relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rel));
820 symtab = (Elf32_Sym *) symsec->contents;
821 strtab = (const char *) strsec->contents;
823 for (; rel < relend; ++rel) {
825 struct arch_symbol *intsym;
828 switch (ELF32_R_TYPE(rel->r_info)) {
848 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
850 name = strtab + extsym->st_name;
852 name = f->sections[extsym->st_shndx]->name;
854 (struct arch_symbol *) obj_find_symbol(&ifile->root, name);
856 if (!intsym->gotent.offset_done) {
857 intsym->gotent.offset_done = 1;
858 intsym->gotent.offset = offset;
864 if (offset > 0 || gotneeded)
866 obj_create_alloced_section(&ifile->root, ".got", 4, offset);
871 int arch_init_module(struct obj_file *f, struct new_module *mod)
877 /*======================================================================*/
879 /* Standard ELF hash function. */
880 inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
889 if ((g = (h & 0xf0000000)) != 0) {
898 unsigned long obj_elf_hash(const char *name)
900 return obj_elf_hash_n(name, strlen(name));
903 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
904 /* Get the kernel version in the canonical integer form. */
906 static int get_kernel_version(char str[STRVERSIONLEN])
908 struct utsname uts_info;
912 if (uname(&uts_info) < 0)
914 strncpy(str, uts_info.release, STRVERSIONLEN);
915 p = uts_info.release;
917 a = strtoul(p, &p, 10);
920 b = strtoul(p + 1, &p, 10);
923 c = strtoul(p + 1, &q, 10);
927 return a << 16 | b << 8 | c;
930 /* String comparison for non-co-versioned kernel and module. */
932 static int ncv_strcmp(const char *a, const char *b)
934 size_t alen = strlen(a), blen = strlen(b);
936 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
937 return strncmp(a, b, alen);
938 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
939 return strncmp(a, b, blen);
944 /* String hashing for non-co-versioned kernel and module. Here
945 we are simply forced to drop the crc from the hash. */
947 static unsigned long ncv_symbol_hash(const char *str)
949 size_t len = strlen(str);
950 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
952 return obj_elf_hash_n(str, len);
956 obj_set_symbol_compare(struct obj_file *f,
957 int (*cmp) (const char *, const char *),
958 unsigned long (*hash) (const char *))
963 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
966 f->symbol_hash = hash;
968 memcpy(tmptab, f->symtab, sizeof(tmptab));
969 memset(f->symtab, 0, sizeof(f->symtab));
971 for (i = 0; i < HASH_BUCKETS; ++i)
972 for (sym = tmptab[i]; sym; sym = next) {
973 unsigned long h = hash(sym->name) % HASH_BUCKETS;
975 sym->next = f->symtab[h];
981 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
984 struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
985 unsigned long symidx, int info,
986 int secidx, ElfW(Addr) value,
989 struct obj_symbol *sym;
990 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
991 int n_type = ELFW(ST_TYPE) (info);
992 int n_binding = ELFW(ST_BIND) (info);
994 for (sym = f->symtab[hash]; sym; sym = sym->next)
995 if (f->symbol_cmp(sym->name, name) == 0) {
996 int o_secidx = sym->secidx;
997 int o_info = sym->info;
998 int o_type = ELFW(ST_TYPE) (o_info);
999 int o_binding = ELFW(ST_BIND) (o_info);
1001 /* A redefinition! Is it legal? */
1003 if (secidx == SHN_UNDEF)
1005 else if (o_secidx == SHN_UNDEF)
1007 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
1008 /* Cope with local and global symbols of the same name
1009 in the same object file, as might have been created
1010 by ld -r. The only reason locals are now seen at this
1011 level at all is so that we can do semi-sensible things
1014 struct obj_symbol *nsym, **p;
1016 nsym = arch_new_symbol();
1017 nsym->next = sym->next;
1020 /* Excise the old (local) symbol from the hash chain. */
1021 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
1025 } else if (n_binding == STB_LOCAL) {
1026 /* Another symbol of the same name has already been defined.
1027 Just add this to the local table. */
1028 sym = arch_new_symbol();
1031 f->local_symtab[symidx] = sym;
1033 } else if (n_binding == STB_WEAK)
1035 else if (o_binding == STB_WEAK)
1037 /* Don't unify COMMON symbols with object types the programmer
1039 else if (secidx == SHN_COMMON
1040 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
1042 else if (o_secidx == SHN_COMMON
1043 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
1046 /* Don't report an error if the symbol is coming from
1047 the kernel or some external module. */
1048 if (secidx <= SHN_HIRESERVE)
1049 errorMsg("%s multiply defined\n", name);
1054 /* Completely new symbol. */
1055 sym = arch_new_symbol();
1056 sym->next = f->symtab[hash];
1057 f->symtab[hash] = sym;
1060 if (ELFW(ST_BIND) (info) == STB_LOCAL)
1061 f->local_symtab[symidx] = sym;
1067 sym->secidx = secidx;
1073 struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
1075 struct obj_symbol *sym;
1076 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
1078 for (sym = f->symtab[hash]; sym; sym = sym->next)
1079 if (f->symbol_cmp(sym->name, name) == 0)
1086 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
1089 if (sym->secidx >= SHN_LORESERVE)
1092 return sym->value + f->sections[sym->secidx]->header.sh_addr;
1094 /* As a special case, a NULL sym has value zero. */
1099 struct obj_section *obj_find_section(struct obj_file *f, const char *name)
1101 int i, n = f->header.e_shnum;
1103 for (i = 0; i < n; ++i)
1104 if (strcmp(f->sections[i]->name, name) == 0)
1105 return f->sections[i];
1110 static int obj_load_order_prio(struct obj_section *a)
1112 unsigned long af, ac;
1114 af = a->header.sh_flags;
1117 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1118 strcmp(a->name + 5, ".init"))
1122 if (!(af & SHF_WRITE))
1124 if (af & SHF_EXECINSTR)
1126 if (a->header.sh_type != SHT_NOBITS)
1133 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1135 struct obj_section **p;
1136 int prio = obj_load_order_prio(sec);
1137 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1138 if (obj_load_order_prio(*p) < prio)
1140 sec->load_next = *p;
1144 struct obj_section *obj_create_alloced_section(struct obj_file *f,
1146 unsigned long align,
1149 int newidx = f->header.e_shnum++;
1150 struct obj_section *sec;
1152 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1153 f->sections[newidx] = sec = arch_new_section();
1155 memset(sec, 0, sizeof(*sec));
1156 sec->header.sh_type = SHT_PROGBITS;
1157 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1158 sec->header.sh_size = size;
1159 sec->header.sh_addralign = align;
1163 sec->contents = xmalloc(size);
1165 obj_insert_section_load_order(f, sec);
1170 struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1172 unsigned long align,
1175 int newidx = f->header.e_shnum++;
1176 struct obj_section *sec;
1178 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1179 f->sections[newidx] = sec = arch_new_section();
1181 memset(sec, 0, sizeof(*sec));
1182 sec->header.sh_type = SHT_PROGBITS;
1183 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1184 sec->header.sh_size = size;
1185 sec->header.sh_addralign = align;
1189 sec->contents = xmalloc(size);
1191 sec->load_next = f->load_order;
1192 f->load_order = sec;
1193 if (f->load_order_search_start == &f->load_order)
1194 f->load_order_search_start = &sec->load_next;
1199 void *obj_extend_section(struct obj_section *sec, unsigned long more)
1201 unsigned long oldsize = sec->header.sh_size;
1202 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1203 return sec->contents + oldsize;
1208 /* Conditionally add the symbols from the given symbol set to the
1214 int idx, struct new_module_symbol *syms, size_t nsyms)
1216 struct new_module_symbol *s;
1220 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1222 /* Only add symbols that are already marked external. If we
1223 override locals we may cause problems for argument initialization.
1224 We will also create a false dependency on the module. */
1225 struct obj_symbol *sym;
1227 sym = obj_find_symbol(f, (char *) s->name);
1228 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1229 sym = obj_add_symbol(f, (char *) s->name, -1,
1230 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1232 /* Did our symbol just get installed? If so, mark the
1233 module as "used". */
1234 if (sym->secidx == idx)
1242 static void add_kernel_symbols(struct obj_file *f)
1244 struct external_module *m;
1247 /* Add module symbols first. */
1249 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1251 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1252 m->nsyms)) m->used = 1, ++nused;
1254 n_ext_modules_used = nused;
1256 /* And finally the symbols from the kernel proper. */
1259 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1262 static char *get_modinfo_value(struct obj_file *f, const char *key)
1264 struct obj_section *sec;
1265 char *p, *v, *n, *ep;
1266 size_t klen = strlen(key);
1268 sec = obj_find_section(f, ".modinfo");
1272 ep = p + sec->header.sh_size;
1275 n = strchr(p, '\0');
1277 if (p + klen == v && strncmp(p, key, klen) == 0)
1280 if (p + klen == n && strcmp(p, key) == 0)
1290 /*======================================================================*/
1291 /* Functions relating to module loading in pre 2.1 kernels. */
1294 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1298 struct obj_symbol *sym;
1302 if ((q = strchr(p, '=')) == NULL) {
1308 sym = obj_find_symbol(f, p);
1310 /* Also check that the parameter was not resolved from the kernel. */
1311 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1312 errorMsg("symbol for parameter %s not found\n", p);
1316 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1318 /* Do C quoting if we begin with a ". */
1322 str = alloca(strlen(q));
1323 for (r = str, q++; *q != '"'; ++q, ++r) {
1325 errorMsg("improperly terminated string argument for %s\n", p);
1327 } else if (*q == '\\')
1361 if (q[1] >= '0' && q[1] <= '7') {
1362 c = (c * 8) + *++q - '0';
1363 if (q[1] >= '0' && q[1] <= '7')
1364 c = (c * 8) + *++q - '0';
1377 obj_string_patch(f, sym->secidx, sym->value, str);
1378 } else if (*q >= '0' && *q <= '9') {
1380 *loc++ = strtoul(q, &q, 0);
1381 while (*q++ == ',');
1383 char *contents = f->sections[sym->secidx]->contents;
1384 char *loc = contents + sym->value;
1385 char *r; /* To search for commas */
1387 /* Break the string with comas */
1388 while ((r = strchr(q, ',')) != (char *) NULL) {
1390 obj_string_patch(f, sym->secidx, loc - contents, q);
1391 loc += sizeof(char *);
1396 obj_string_patch(f, sym->secidx, loc - contents, q);
1405 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1406 static int old_is_module_checksummed(struct obj_file *f)
1408 return obj_find_symbol(f, "Using_Versions") != NULL;
1410 /* Get the module's kernel version in the canonical integer form. */
1413 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1415 struct obj_symbol *sym;
1419 sym = obj_find_symbol(f, "kernel_version");
1423 p = f->sections[sym->secidx]->contents + sym->value;
1424 strncpy(str, p, STRVERSIONLEN);
1426 a = strtoul(p, &p, 10);
1429 b = strtoul(p + 1, &p, 10);
1432 c = strtoul(p + 1, &q, 10);
1436 return a << 16 | b << 8 | c;
1439 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1441 #ifdef BB_FEATURE_INSMOD_OLD_KERNEL
1443 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1445 static int old_get_kernel_symbols(const char *m_name)
1447 struct old_kernel_sym *ks, *k;
1448 struct new_module_symbol *s;
1449 struct external_module *mod;
1450 int nks, nms, nmod, i;
1452 nks = get_kernel_syms(NULL);
1454 errorMsg("get_kernel_syms: %s: %s", m_name, strerror(errno));
1458 ks = k = xmalloc(nks * sizeof(*ks));
1460 if (get_kernel_syms(ks) != nks) {
1461 perror("inconsistency with get_kernel_syms -- is someone else "
1462 "playing with modules?");
1467 /* Collect the module information. */
1472 while (k->name[0] == '#' && k->name[1]) {
1473 struct old_kernel_sym *k2;
1474 struct new_module_symbol *s;
1476 /* Find out how many symbols this module has. */
1477 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1481 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1482 mod[nmod].name = k->name + 1;
1483 mod[nmod].addr = k->value;
1485 mod[nmod].nsyms = nms;
1486 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1488 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1489 s->name = (unsigned long) k->name;
1490 s->value = k->value;
1497 n_ext_modules = nmod + 1;
1499 /* Now collect the symbols for the kernel proper. */
1501 if (k->name[0] == '#')
1504 nksyms = nms = nks - (k - ks);
1505 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1507 for (i = 0; i < nms; ++i, ++s, ++k) {
1508 s->name = (unsigned long) k->name;
1509 s->value = k->value;
1515 /* Return the kernel symbol checksum version, or zero if not used. */
1517 static int old_is_kernel_checksummed(void)
1519 /* Using_Versions is the first symbol. */
1521 && strcmp((char *) ksyms[0].name,
1522 "Using_Versions") == 0) return ksyms[0].value;
1528 static int old_create_mod_use_count(struct obj_file *f)
1530 struct obj_section *sec;
1532 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1535 obj_add_symbol(f, "mod_use_count_", -1,
1536 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1543 old_init_module(const char *m_name, struct obj_file *f,
1544 unsigned long m_size)
1547 struct old_mod_routines routines;
1548 struct old_symbol_table *symtab;
1551 /* Create the symbol table */
1553 int nsyms = 0, strsize = 0, total;
1555 /* Size things first... */
1558 for (i = 0; i < HASH_BUCKETS; ++i) {
1559 struct obj_symbol *sym;
1560 for (sym = f->symtab[i]; sym; sym = sym->next)
1561 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1562 && sym->secidx <= SHN_HIRESERVE)
1564 sym->ksymidx = nsyms++;
1565 strsize += strlen(sym->name) + 1;
1570 total = (sizeof(struct old_symbol_table)
1571 + nsyms * sizeof(struct old_module_symbol)
1572 + n_ext_modules_used * sizeof(struct old_module_ref)
1574 symtab = xmalloc(total);
1575 symtab->size = total;
1576 symtab->n_symbols = nsyms;
1577 symtab->n_refs = n_ext_modules_used;
1579 if (flag_export && nsyms) {
1580 struct old_module_symbol *ksym;
1584 ksym = symtab->symbol;
1585 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1586 + n_ext_modules_used * sizeof(struct old_module_ref));
1588 for (i = 0; i < HASH_BUCKETS; ++i) {
1589 struct obj_symbol *sym;
1590 for (sym = f->symtab[i]; sym; sym = sym->next)
1591 if (sym->ksymidx >= 0) {
1592 ksym->addr = obj_symbol_final_value(f, sym);
1594 (unsigned long) str - (unsigned long) symtab;
1596 str = stpcpy(str, sym->name) + 1;
1602 if (n_ext_modules_used) {
1603 struct old_module_ref *ref;
1606 ref = (struct old_module_ref *)
1607 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
1609 for (i = 0; i < n_ext_modules; ++i)
1610 if (ext_modules[i].used)
1611 ref++->module = ext_modules[i].addr;
1615 /* Fill in routines. */
1618 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
1620 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
1622 /* Whew! All of the initialization is complete. Collect the final
1623 module image and give it to the kernel. */
1625 image = xmalloc(m_size);
1626 obj_create_image(f, image);
1628 /* image holds the complete relocated module, accounting correctly for
1629 mod_use_count. However the old module kernel support assume that
1630 it is receiving something which does not contain mod_use_count. */
1631 ret = old_sys_init_module(m_name, image + sizeof(long),
1632 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
1633 : 0), &routines, symtab);
1635 errorMsg("init_module: %s: %s", m_name, strerror(errno));
1645 #define old_create_mod_use_count(x) TRUE
1646 #define old_init_module(x, y, z) TRUE
1648 #endif /* BB_FEATURE_INSMOD_OLD_KERNEL */
1652 /*======================================================================*/
1653 /* Functions relating to module loading after 2.1.18. */
1656 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
1660 struct obj_symbol *sym;
1661 char *contents, *loc;
1665 if ((q = strchr(p, '=')) == NULL) {
1670 key = alloca(q - p + 6);
1671 memcpy(key, "parm_", 5);
1672 memcpy(key + 5, p, q - p);
1675 p = get_modinfo_value(f, key);
1678 errorMsg("invalid parameter %s\n", key);
1682 sym = obj_find_symbol(f, key);
1684 /* Also check that the parameter was not resolved from the kernel. */
1685 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1686 errorMsg("symbol for parameter %s not found\n", key);
1691 min = strtoul(p, &p, 10);
1693 max = strtoul(p + 1, &p, 10);
1699 contents = f->sections[sym->secidx]->contents;
1700 loc = contents + sym->value;
1704 if ((*p == 's') || (*p == 'c')) {
1707 /* Do C quoting if we begin with a ", else slurp the lot. */
1711 str = alloca(strlen(q));
1712 for (r = str, q++; *q != '"'; ++q, ++r) {
1714 errorMsg("improperly terminated string argument for %s\n",
1717 } else if (*q == '\\')
1751 if (q[1] >= '0' && q[1] <= '7') {
1752 c = (c * 8) + *++q - '0';
1753 if (q[1] >= '0' && q[1] <= '7')
1754 c = (c * 8) + *++q - '0';
1771 /* In this case, the string is not quoted. We will break
1772 it using the coma (like for ints). If the user wants to
1773 include comas in a string, he just has to quote it */
1775 /* Search the next coma */
1779 if (r != (char *) NULL) {
1780 /* Recopy the current field */
1781 str = alloca(r - q + 1);
1782 memcpy(str, q, r - q);
1784 /* I don't know if it is usefull, as the previous case
1785 doesn't null terminate the string ??? */
1788 /* Keep next fields */
1799 obj_string_patch(f, sym->secidx, loc - contents, str);
1800 loc += tgt_sizeof_char_p;
1802 /* Array of chars (in fact, matrix !) */
1803 unsigned long charssize; /* size of each member */
1805 /* Get the size of each member */
1806 /* Probably we should do that outside the loop ? */
1807 if (!isdigit(*(p + 1))) {
1808 errorMsg("parameter type 'c' for %s must be followed by"
1809 " the maximum size\n", key);
1812 charssize = strtoul(p + 1, (char **) NULL, 10);
1815 if (strlen(str) >= charssize) {
1816 errorMsg("string too long for %s (max %ld)\n", key,
1821 /* Copy to location */
1822 strcpy((char *) loc, str);
1826 long v = strtoul(q, &q, 0);
1833 loc += tgt_sizeof_short;
1837 loc += tgt_sizeof_int;
1841 loc += tgt_sizeof_long;
1845 errorMsg("unknown parameter type '%c' for %s\n", *p, key);
1860 goto retry_end_of_value;
1864 errorMsg("too many values for %s (max %d)\n", key, max);
1871 errorMsg("invalid argument syntax for %s\n", key);
1878 errorMsg("too few values for %s (min %d)\n", key, min);
1888 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1889 static int new_is_module_checksummed(struct obj_file *f)
1891 const char *p = get_modinfo_value(f, "using_checksums");
1898 /* Get the module's kernel version in the canonical integer form. */
1901 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1906 p = get_modinfo_value(f, "kernel_version");
1909 strncpy(str, p, STRVERSIONLEN);
1911 a = strtoul(p, &p, 10);
1914 b = strtoul(p + 1, &p, 10);
1917 c = strtoul(p + 1, &q, 10);
1921 return a << 16 | b << 8 | c;
1924 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1927 #ifdef BB_FEATURE_INSMOD_NEW_KERNEL
1929 /* Fetch the loaded modules, and all currently exported symbols. */
1931 static int new_get_kernel_symbols(void)
1933 char *module_names, *mn;
1934 struct external_module *modules, *m;
1935 struct new_module_symbol *syms, *s;
1936 size_t ret, bufsize, nmod, nsyms, i, j;
1938 /* Collect the loaded modules. */
1940 module_names = xmalloc(bufsize = 256);
1942 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
1943 if (errno == ENOSPC) {
1944 module_names = xrealloc(module_names, bufsize = ret);
1945 goto retry_modules_load;
1947 errorMsg("QM_MODULES: %s", strerror(errno));
1951 n_ext_modules = nmod = ret;
1952 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
1953 memset(modules, 0, nmod * sizeof(*modules));
1955 /* Collect the modules' symbols. */
1957 for (i = 0, mn = module_names, m = modules;
1958 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
1959 struct new_module_info info;
1961 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
1962 if (errno == ENOENT) {
1963 /* The module was removed out from underneath us. */
1966 errorMsg("query_module: QM_INFO: %s: %s", mn, strerror(errno));
1970 syms = xmalloc(bufsize = 1024);
1972 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
1975 syms = xrealloc(syms, bufsize = ret);
1976 goto retry_mod_sym_load;
1978 /* The module was removed out from underneath us. */
1981 errorMsg("query_module: QM_SYMBOLS: %s: %s", mn, strerror(errno));
1988 m->addr = info.addr;
1992 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
1993 s->name += (unsigned long) syms;
1997 /* Collect the kernel's symbols. */
1999 syms = xmalloc(bufsize = 16 * 1024);
2000 retry_kern_sym_load:
2001 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
2002 if (errno == ENOSPC) {
2003 syms = xrealloc(syms, bufsize = ret);
2004 goto retry_kern_sym_load;
2006 errorMsg("kernel: QM_SYMBOLS: %s", strerror(errno));
2009 nksyms = nsyms = ret;
2012 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
2013 s->name += (unsigned long) syms;
2019 /* Return the kernel symbol checksum version, or zero if not used. */
2021 static int new_is_kernel_checksummed(void)
2023 struct new_module_symbol *s;
2026 /* Using_Versions is not the first symbol, but it should be in there. */
2028 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
2029 if (strcmp((char *) s->name, "Using_Versions") == 0)
2036 static int new_create_this_module(struct obj_file *f, const char *m_name)
2038 struct obj_section *sec;
2040 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
2041 sizeof(struct new_module));
2042 memset(sec->contents, 0, sizeof(struct new_module));
2044 obj_add_symbol(f, "__this_module", -1,
2045 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
2046 sizeof(struct new_module));
2048 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
2055 static int new_create_module_ksymtab(struct obj_file *f)
2057 struct obj_section *sec;
2060 /* We must always add the module references. */
2062 if (n_ext_modules_used) {
2063 struct new_module_ref *dep;
2064 struct obj_symbol *tm;
2066 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
2067 (sizeof(struct new_module_ref)
2068 * n_ext_modules_used));
2072 tm = obj_find_symbol(f, "__this_module");
2073 dep = (struct new_module_ref *) sec->contents;
2074 for (i = 0; i < n_ext_modules; ++i)
2075 if (ext_modules[i].used) {
2076 dep->dep = ext_modules[i].addr;
2077 obj_symbol_patch(f, sec->idx,
2078 (char *) &dep->ref - sec->contents, tm);
2084 if (flag_export && !obj_find_section(f, "__ksymtab")) {
2089 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
2092 /* We don't want to export symbols residing in sections that
2093 aren't loaded. There are a number of these created so that
2094 we make sure certain module options don't appear twice. */
2096 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
2098 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2100 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2101 struct obj_symbol *sym;
2102 for (sym = f->symtab[i]; sym; sym = sym->next)
2103 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2104 && sym->secidx <= SHN_HIRESERVE
2105 && (sym->secidx >= SHN_LORESERVE
2106 || loaded[sym->secidx])) {
2107 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2109 obj_symbol_patch(f, sec->idx, ofs, sym);
2110 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2117 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2125 new_init_module(const char *m_name, struct obj_file *f,
2126 unsigned long m_size)
2128 struct new_module *module;
2129 struct obj_section *sec;
2134 sec = obj_find_section(f, ".this");
2135 module = (struct new_module *) sec->contents;
2136 m_addr = sec->header.sh_addr;
2138 module->size_of_struct = sizeof(*module);
2139 module->size = m_size;
2140 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2142 sec = obj_find_section(f, "__ksymtab");
2143 if (sec && sec->header.sh_size) {
2144 module->syms = sec->header.sh_addr;
2145 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2148 if (n_ext_modules_used) {
2149 sec = obj_find_section(f, ".kmodtab");
2150 module->deps = sec->header.sh_addr;
2151 module->ndeps = n_ext_modules_used;
2155 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2157 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2159 sec = obj_find_section(f, "__ex_table");
2161 module->ex_table_start = sec->header.sh_addr;
2162 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2165 sec = obj_find_section(f, ".text.init");
2167 module->runsize = sec->header.sh_addr - m_addr;
2169 sec = obj_find_section(f, ".data.init");
2171 if (!module->runsize ||
2172 module->runsize > sec->header.sh_addr - m_addr)
2173 module->runsize = sec->header.sh_addr - m_addr;
2176 if (!arch_init_module(f, module))
2179 /* Whew! All of the initialization is complete. Collect the final
2180 module image and give it to the kernel. */
2182 image = xmalloc(m_size);
2183 obj_create_image(f, image);
2185 ret = new_sys_init_module(m_name, (struct new_module *) image);
2187 errorMsg("init_module: %s: %s", m_name, strerror(errno));
2196 #define new_init_module(x, y, z) TRUE
2197 #define new_create_this_module(x, y) 0
2198 #define new_create_module_ksymtab(x)
2200 #endif /* BB_FEATURE_INSMOD_OLD_KERNEL */
2203 /*======================================================================*/
2206 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2209 struct obj_string_patch *p;
2210 struct obj_section *strsec;
2211 size_t len = strlen(string) + 1;
2214 p = xmalloc(sizeof(*p));
2215 p->next = f->string_patches;
2216 p->reloc_secidx = secidx;
2217 p->reloc_offset = offset;
2218 f->string_patches = p;
2220 strsec = obj_find_section(f, ".kstrtab");
2221 if (strsec == NULL) {
2222 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2223 p->string_offset = 0;
2224 loc = strsec->contents;
2226 p->string_offset = strsec->header.sh_size;
2227 loc = obj_extend_section(strsec, len);
2229 memcpy(loc, string, len);
2235 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2236 struct obj_symbol *sym)
2238 struct obj_symbol_patch *p;
2240 p = xmalloc(sizeof(*p));
2241 p->next = f->symbol_patches;
2242 p->reloc_secidx = secidx;
2243 p->reloc_offset = offset;
2245 f->symbol_patches = p;
2250 int obj_check_undefineds(struct obj_file *f)
2255 for (i = 0; i < HASH_BUCKETS; ++i) {
2256 struct obj_symbol *sym;
2257 for (sym = f->symtab[i]; sym; sym = sym->next)
2258 if (sym->secidx == SHN_UNDEF) {
2259 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2260 sym->secidx = SHN_ABS;
2263 errorMsg("unresolved symbol %s\n", sym->name);
2272 void obj_allocate_commons(struct obj_file *f)
2274 struct common_entry {
2275 struct common_entry *next;
2276 struct obj_symbol *sym;
2277 } *common_head = NULL;
2281 for (i = 0; i < HASH_BUCKETS; ++i) {
2282 struct obj_symbol *sym;
2283 for (sym = f->symtab[i]; sym; sym = sym->next)
2284 if (sym->secidx == SHN_COMMON) {
2285 /* Collect all COMMON symbols and sort them by size so as to
2286 minimize space wasted by alignment requirements. */
2288 struct common_entry **p, *n;
2289 for (p = &common_head; *p; p = &(*p)->next)
2290 if (sym->size <= (*p)->sym->size)
2293 n = alloca(sizeof(*n));
2301 for (i = 1; i < f->local_symtab_size; ++i) {
2302 struct obj_symbol *sym = f->local_symtab[i];
2303 if (sym && sym->secidx == SHN_COMMON) {
2304 struct common_entry **p, *n;
2305 for (p = &common_head; *p; p = &(*p)->next)
2306 if (sym == (*p)->sym)
2308 else if (sym->size < (*p)->sym->size) {
2309 n = alloca(sizeof(*n));
2319 /* Find the bss section. */
2320 for (i = 0; i < f->header.e_shnum; ++i)
2321 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2324 /* If for some reason there hadn't been one, create one. */
2325 if (i == f->header.e_shnum) {
2326 struct obj_section *sec;
2328 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2329 f->sections[i] = sec = arch_new_section();
2330 f->header.e_shnum = i + 1;
2332 memset(sec, 0, sizeof(*sec));
2333 sec->header.sh_type = SHT_PROGBITS;
2334 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2339 /* Allocate the COMMONS. */
2341 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2342 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2343 struct common_entry *c;
2345 for (c = common_head; c; c = c->next) {
2346 ElfW(Addr) align = c->sym->value;
2348 if (align > max_align)
2350 if (bss_size & (align - 1))
2351 bss_size = (bss_size | (align - 1)) + 1;
2354 c->sym->value = bss_size;
2356 bss_size += c->sym->size;
2359 f->sections[i]->header.sh_size = bss_size;
2360 f->sections[i]->header.sh_addralign = max_align;
2364 /* For the sake of patch relocation and parameter initialization,
2365 allocate zeroed data for NOBITS sections now. Note that after
2366 this we cannot assume NOBITS are really empty. */
2367 for (i = 0; i < f->header.e_shnum; ++i) {
2368 struct obj_section *s = f->sections[i];
2369 if (s->header.sh_type == SHT_NOBITS) {
2370 s->contents = memset(xmalloc(s->header.sh_size),
2371 0, s->header.sh_size);
2372 s->header.sh_type = SHT_PROGBITS;
2377 unsigned long obj_load_size(struct obj_file *f)
2379 unsigned long dot = 0;
2380 struct obj_section *sec;
2382 /* Finalize the positions of the sections relative to one another. */
2384 for (sec = f->load_order; sec; sec = sec->load_next) {
2387 align = sec->header.sh_addralign;
2388 if (align && (dot & (align - 1)))
2389 dot = (dot | (align - 1)) + 1;
2391 sec->header.sh_addr = dot;
2392 dot += sec->header.sh_size;
2398 int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2400 int i, n = f->header.e_shnum;
2403 /* Finalize the addresses of the sections. */
2406 for (i = 0; i < n; ++i)
2407 f->sections[i]->header.sh_addr += base;
2409 /* And iterate over all of the relocations. */
2411 for (i = 0; i < n; ++i) {
2412 struct obj_section *relsec, *symsec, *targsec, *strsec;
2413 ElfW(RelM) * rel, *relend;
2417 relsec = f->sections[i];
2418 if (relsec->header.sh_type != SHT_RELM)
2421 symsec = f->sections[relsec->header.sh_link];
2422 targsec = f->sections[relsec->header.sh_info];
2423 strsec = f->sections[symsec->header.sh_link];
2425 rel = (ElfW(RelM) *) relsec->contents;
2426 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2427 symtab = (ElfW(Sym) *) symsec->contents;
2428 strtab = (const char *) strsec->contents;
2430 for (; rel < relend; ++rel) {
2431 ElfW(Addr) value = 0;
2432 struct obj_symbol *intsym = NULL;
2433 unsigned long symndx;
2434 ElfW(Sym) * extsym = 0;
2437 /* Attempt to find a value to use for this relocation. */
2439 symndx = ELFW(R_SYM) (rel->r_info);
2441 /* Note we've already checked for undefined symbols. */
2443 extsym = &symtab[symndx];
2444 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2445 /* Local symbols we look up in the local table to be sure
2446 we get the one that is really intended. */
2447 intsym = f->local_symtab[symndx];
2449 /* Others we look up in the hash table. */
2451 if (extsym->st_name)
2452 name = strtab + extsym->st_name;
2454 name = f->sections[extsym->st_shndx]->name;
2455 intsym = obj_find_symbol(f, name);
2458 value = obj_symbol_final_value(f, intsym);
2459 intsym->referenced = 1;
2461 #if SHT_RELM == SHT_RELA
2462 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2463 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2464 if (!extsym || !extsym->st_name ||
2465 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2467 value += rel->r_addend;
2471 switch (arch_apply_relocation
2472 (f, targsec, symsec, intsym, rel, value)) {
2476 case obj_reloc_overflow:
2477 errmsg = "Relocation overflow";
2479 case obj_reloc_dangerous:
2480 errmsg = "Dangerous relocation";
2482 case obj_reloc_unhandled:
2483 errmsg = "Unhandled relocation";
2486 errorMsg("%s of type %ld for %s\n", errmsg,
2487 (long) ELFW(R_TYPE) (rel->r_info),
2488 strtab + extsym->st_name);
2490 errorMsg("%s of type %ld\n", errmsg,
2491 (long) ELFW(R_TYPE) (rel->r_info));
2499 /* Finally, take care of the patches. */
2501 if (f->string_patches) {
2502 struct obj_string_patch *p;
2503 struct obj_section *strsec;
2504 ElfW(Addr) strsec_base;
2505 strsec = obj_find_section(f, ".kstrtab");
2506 strsec_base = strsec->header.sh_addr;
2508 for (p = f->string_patches; p; p = p->next) {
2509 struct obj_section *targsec = f->sections[p->reloc_secidx];
2510 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2511 = strsec_base + p->string_offset;
2515 if (f->symbol_patches) {
2516 struct obj_symbol_patch *p;
2518 for (p = f->symbol_patches; p; p = p->next) {
2519 struct obj_section *targsec = f->sections[p->reloc_secidx];
2520 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2521 = obj_symbol_final_value(f, p->sym);
2528 int obj_create_image(struct obj_file *f, char *image)
2530 struct obj_section *sec;
2531 ElfW(Addr) base = f->baseaddr;
2533 for (sec = f->load_order; sec; sec = sec->load_next) {
2536 if (sec->header.sh_size == 0)
2539 secimg = image + (sec->header.sh_addr - base);
2541 /* Note that we allocated data for NOBITS sections earlier. */
2542 memcpy(secimg, sec->contents, sec->header.sh_size);
2548 /*======================================================================*/
2550 struct obj_file *obj_load(FILE * fp)
2553 ElfW(Shdr) * section_headers;
2557 /* Read the file header. */
2559 f = arch_new_file();
2560 memset(f, 0, sizeof(*f));
2561 f->symbol_cmp = strcmp;
2562 f->symbol_hash = obj_elf_hash;
2563 f->load_order_search_start = &f->load_order;
2565 fseek(fp, 0, SEEK_SET);
2566 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2567 errorMsg("error reading ELF header: %s", strerror(errno));
2571 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2572 || f->header.e_ident[EI_MAG1] != ELFMAG1
2573 || f->header.e_ident[EI_MAG2] != ELFMAG2
2574 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2575 errorMsg("not an ELF file\n");
2578 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2579 || f->header.e_ident[EI_DATA] != ELFDATAM
2580 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2581 || !MATCH_MACHINE(f->header.e_machine)) {
2582 errorMsg("ELF file not for this architecture\n");
2585 if (f->header.e_type != ET_REL) {
2586 errorMsg("ELF file not a relocatable object\n");
2590 /* Read the section headers. */
2592 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
2593 errorMsg("section header size mismatch: %lu != %lu\n",
2594 (unsigned long) f->header.e_shentsize,
2595 (unsigned long) sizeof(ElfW(Shdr)));
2599 shnum = f->header.e_shnum;
2600 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
2601 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
2603 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
2604 fseek(fp, f->header.e_shoff, SEEK_SET);
2605 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
2606 errorMsg("error reading ELF section headers: %s", strerror(errno));
2610 /* Read the section data. */
2612 for (i = 0; i < shnum; ++i) {
2613 struct obj_section *sec;
2615 f->sections[i] = sec = arch_new_section();
2616 memset(sec, 0, sizeof(*sec));
2618 sec->header = section_headers[i];
2621 switch (sec->header.sh_type) {
2632 if (sec->header.sh_size > 0) {
2633 sec->contents = xmalloc(sec->header.sh_size);
2634 fseek(fp, sec->header.sh_offset, SEEK_SET);
2635 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
2636 errorMsg("error reading ELF section data: %s", strerror(errno));
2640 sec->contents = NULL;
2644 #if SHT_RELM == SHT_REL
2646 errorMsg("RELA relocations not supported on this architecture\n");
2650 errorMsg("REL relocations not supported on this architecture\n");
2655 if (sec->header.sh_type >= SHT_LOPROC) {
2656 /* Assume processor specific section types are debug
2657 info and can safely be ignored. If this is ever not
2658 the case (Hello MIPS?), don't put ifdefs here but
2659 create an arch_load_proc_section(). */
2663 errorMsg("can't handle sections of type %ld\n",
2664 (long) sec->header.sh_type);
2669 /* Do what sort of interpretation as needed by each section. */
2671 shstrtab = f->sections[f->header.e_shstrndx]->contents;
2673 for (i = 0; i < shnum; ++i) {
2674 struct obj_section *sec = f->sections[i];
2675 sec->name = shstrtab + sec->header.sh_name;
2678 for (i = 0; i < shnum; ++i) {
2679 struct obj_section *sec = f->sections[i];
2681 if (sec->header.sh_flags & SHF_ALLOC)
2682 obj_insert_section_load_order(f, sec);
2684 switch (sec->header.sh_type) {
2687 unsigned long nsym, j;
2691 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
2692 errorMsg("symbol size mismatch: %lu != %lu\n",
2693 (unsigned long) sec->header.sh_entsize,
2694 (unsigned long) sizeof(ElfW(Sym)));
2698 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
2699 strtab = f->sections[sec->header.sh_link]->contents;
2700 sym = (ElfW(Sym) *) sec->contents;
2702 /* Allocate space for a table of local symbols. */
2703 j = f->local_symtab_size = sec->header.sh_info;
2704 f->local_symtab = xmalloc(j *=
2705 sizeof(struct obj_symbol *));
2706 memset(f->local_symtab, 0, j);
2708 /* Insert all symbols into the hash table. */
2709 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
2712 name = strtab + sym->st_name;
2714 name = f->sections[sym->st_shndx]->name;
2716 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
2717 sym->st_value, sym->st_size);
2723 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
2724 errorMsg("relocation entry size mismatch: %lu != %lu\n",
2725 (unsigned long) sec->header.sh_entsize,
2726 (unsigned long) sizeof(ElfW(RelM)));
2736 static void hide_special_symbols(struct obj_file *f)
2738 static const char *const specials[] = {
2745 struct obj_symbol *sym;
2746 const char *const *p;
2748 for (p = specials; *p; ++p)
2749 if ((sym = obj_find_symbol(f, *p)) != NULL)
2751 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
2756 extern int insmod_main( int argc, char **argv)
2762 unsigned long m_size;
2766 char m_name[BUFSIZ + 1] = "\0";
2767 int exit_status = FALSE;
2769 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2771 char k_strversion[STRVERSIONLEN];
2772 char m_strversion[STRVERSIONLEN];
2779 usage(insmod_usage);
2782 /* Parse any options */
2783 while (--argc > 0 && **(++argv) == '-') {
2784 while (*(++(*argv))) {
2786 case 'f': /* force loading */
2787 flag_force_load = 1;
2789 case 'k': /* module loaded by kerneld, auto-cleanable */
2792 case 'v': /* verbose output */
2795 case 'x': /* do not export externs */
2799 usage(insmod_usage);
2805 usage(insmod_usage);
2807 /* Grab the module name */
2808 if ((tmp = strrchr(*argv, '/')) != NULL) {
2815 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
2817 memcpy(m_name, tmp, len);
2818 strcpy(m_fullName, m_name);
2819 strcat(m_fullName, ".o");
2821 /* Get a filedesc for the module */
2822 if ((fp = fopen(*argv, "r")) == NULL) {
2823 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
2824 if (recursiveAction(_PATH_MODULES, TRUE, FALSE, FALSE,
2825 findNamedModule, 0, m_fullName) == FALSE)
2827 if (m_filename[0] == '\0'
2828 || ((fp = fopen(m_filename, "r")) == NULL))
2830 errorMsg("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
2834 fatalError("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
2836 memcpy(m_filename, *argv, strlen(*argv));
2839 if ((f = obj_load(fp)) == NULL) {
2840 perror("Could not load the module\n");
2844 if (get_modinfo_value(f, "kernel_version") == NULL)
2849 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2850 /* Version correspondence? */
2852 k_version = get_kernel_version(k_strversion);
2853 if (m_has_modinfo) {
2854 m_version = new_get_module_version(f, m_strversion);
2856 m_version = old_get_module_version(f, m_strversion);
2857 if (m_version == -1) {
2858 errorMsg("couldn't find the kernel version the module was "
2864 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
2865 if (flag_force_load) {
2866 errorMsg("Warning: kernel-module version mismatch\n"
2867 "\t%s was compiled for kernel version %s\n"
2868 "\twhile this kernel is version %s\n",
2869 m_filename, m_strversion, k_strversion);
2871 errorMsg("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);
2879 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2881 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
2883 if (k_new_syscalls) {
2884 #ifdef BB_FEATURE_INSMOD_NEW_KERNEL
2885 if (!new_get_kernel_symbols())
2887 k_crcs = new_is_kernel_checksummed();
2889 errorMsg("Not configured to support new kernels\n");
2893 #ifdef BB_FEATURE_INSMOD_OLD_KERNEL
2894 if (!old_get_kernel_symbols(m_name))
2896 k_crcs = old_is_kernel_checksummed();
2898 errorMsg("Not configured to support old kernels\n");
2903 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2905 m_crcs = new_is_module_checksummed(f);
2907 m_crcs = old_is_module_checksummed(f);
2909 if (m_crcs != k_crcs)
2910 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
2911 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2913 /* Let the module know about the kernel symbols. */
2914 add_kernel_symbols(f);
2916 /* Allocate common symbols, symbol tables, and string tables. */
2919 ? !new_create_this_module(f, m_name)
2920 : !old_create_mod_use_count(f))
2925 if (!obj_check_undefineds(f)) {
2928 obj_allocate_commons(f);
2930 if (optind < argc) {
2932 ? !new_process_module_arguments(f, argc - optind, argv + optind)
2933 : !old_process_module_arguments(f, argc - optind, argv + optind))
2940 hide_special_symbols(f);
2943 new_create_module_ksymtab(f);
2945 /* Find current size of the module */
2946 m_size = obj_load_size(f);
2950 m_addr = create_module(m_name, m_size);
2955 errorMsg("A module named %s already exists\n", m_name);
2958 errorMsg("Can't allocate kernel memory for module; needed %lu bytes\n",
2962 errorMsg("create_module: %s: %s", m_name, strerror(errno));
2966 if (!obj_relocate(f, m_addr)) {
2967 delete_module(m_name);
2972 ? !new_init_module(m_name, f, m_size)
2973 : !old_init_module(m_name, f, m_size))
2975 delete_module(m_name);
2983 return(exit_status);