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 * Based almost entirely on the Linux modutils-2.3.11 implementation.
10 * Copyright 1996, 1997 Linux International.
11 * New implementation contributed by Richard Henderson <rth@tamu.edu>
12 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
13 * Restructured (and partly rewritten) by:
14 * Björn Ekwall <bj0rn@blox.se> February 1999
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 #include <sys/utsname.h>
43 #include <sys/syscall.h>
44 #include <linux/unistd.h>
46 //----------------------------------------------------------------------------
47 //--------modutils module.h, lines 45-242
48 //----------------------------------------------------------------------------
50 /* Definitions for the Linux module syscall interface.
51 Copyright 1996, 1997 Linux International.
53 Contributed by Richard Henderson <rth@tamu.edu>
55 This file is part of the Linux modutils.
57 This program is free software; you can redistribute it and/or modify it
58 under the terms of the GNU General Public License as published by the
59 Free Software Foundation; either version 2 of the License, or (at your
60 option) any later version.
62 This program is distributed in the hope that it will be useful, but
63 WITHOUT ANY WARRANTY; without even the implied warranty of
64 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
65 General Public License for more details.
67 You should have received a copy of the GNU General Public License
68 along with this program; if not, write to the Free Software Foundation,
69 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
72 #ifndef MODUTILS_MODULE_H
73 #define MODUTILS_MODULE_H 1
75 #ident "$Id: insmod.c,v 1.13 2000/07/09 06:59:58 andersen Exp $"
77 /* This file contains the structures used by the 2.0 and 2.1 kernels.
78 We do not use the kernel headers directly because we do not wish
79 to be dependant on a particular kernel version to compile insmod. */
82 /*======================================================================*/
83 /* The structures used by Linux 2.0. */
85 /* The symbol format used by get_kernel_syms(2). */
94 unsigned long module; /* kernel addresses */
98 struct old_module_symbol
104 struct old_symbol_table
106 int size; /* total, including string table!!! */
109 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
110 struct old_module_ref ref[0]; /* actual size defined by n_refs */
113 struct old_mod_routines
116 unsigned long cleanup;
122 unsigned long ref; /* the list of modules that refer to me */
123 unsigned long symtab;
125 int size; /* size of module in pages */
126 unsigned long addr; /* address of module */
128 unsigned long cleanup; /* cleanup routine */
131 /* Sent to init_module(2) or'ed into the code size parameter. */
132 #define OLD_MOD_AUTOCLEAN 0x40000000 /* big enough, but no sign problems... */
134 int get_kernel_syms(struct old_kernel_sym *);
135 int old_sys_init_module(const char *name, char *code, unsigned codesize,
136 struct old_mod_routines *, struct old_symbol_table *);
138 /*======================================================================*/
139 /* For sizeof() which are related to the module platform and not to the
140 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
142 #define tgt_sizeof_char sizeof(char)
143 #define tgt_sizeof_short sizeof(short)
144 #define tgt_sizeof_int sizeof(int)
145 #define tgt_sizeof_long sizeof(long)
146 #define tgt_sizeof_char_p sizeof(char *)
147 #define tgt_sizeof_void_p sizeof(void *)
148 #define tgt_long long
150 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
151 #undef tgt_sizeof_long
152 #undef tgt_sizeof_char_p
153 #undef tgt_sizeof_void_p
155 #define tgt_sizeof_long 8
156 #define tgt_sizeof_char_p 8
157 #define tgt_sizeof_void_p 8
158 #define tgt_long long long
161 /*======================================================================*/
162 /* The structures used in Linux 2.1. */
164 /* Note: new_module_symbol does not use tgt_long intentionally */
165 struct new_module_symbol
171 struct new_module_persist;
173 struct new_module_ref
175 unsigned tgt_long dep; /* kernel addresses */
176 unsigned tgt_long ref;
177 unsigned tgt_long next_ref;
182 unsigned tgt_long size_of_struct; /* == sizeof(module) */
183 unsigned tgt_long next;
184 unsigned tgt_long name;
185 unsigned tgt_long size;
188 unsigned tgt_long flags; /* AUTOCLEAN et al */
193 unsigned tgt_long syms;
194 unsigned tgt_long deps;
195 unsigned tgt_long refs;
196 unsigned tgt_long init;
197 unsigned tgt_long cleanup;
198 unsigned tgt_long ex_table_start;
199 unsigned tgt_long ex_table_end;
201 unsigned tgt_long gp;
203 /* Everything after here is extension. */
204 unsigned tgt_long persist_start;
205 unsigned tgt_long persist_end;
206 unsigned tgt_long can_unload;
207 unsigned tgt_long runsize;
210 struct new_module_info
218 /* Bits of module.flags. */
219 #define NEW_MOD_RUNNING 1
220 #define NEW_MOD_DELETED 2
221 #define NEW_MOD_AUTOCLEAN 4
222 #define NEW_MOD_VISITED 8
223 #define NEW_MOD_USED_ONCE 16
225 int new_sys_init_module(const char *name, const struct new_module *);
226 int query_module(const char *name, int which, void *buf, size_t bufsize,
229 /* Values for query_module's which. */
237 /*======================================================================*/
238 /* The system calls unchanged between 2.0 and 2.1. */
240 unsigned long create_module(const char *, size_t);
241 int delete_module(const char *);
244 #endif /* module.h */
246 //----------------------------------------------------------------------------
247 //--------end of modutils module.h
248 //----------------------------------------------------------------------------
252 //----------------------------------------------------------------------------
253 //--------modutils obj.h, lines 253-462
254 //----------------------------------------------------------------------------
256 /* Elf object file loading and relocation routines.
257 Copyright 1996, 1997 Linux International.
259 Contributed by Richard Henderson <rth@tamu.edu>
261 This file is part of the Linux modutils.
263 This program is free software; you can redistribute it and/or modify it
264 under the terms of the GNU General Public License as published by the
265 Free Software Foundation; either version 2 of the License, or (at your
266 option) any later version.
268 This program is distributed in the hope that it will be useful, but
269 WITHOUT ANY WARRANTY; without even the implied warranty of
270 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
271 General Public License for more details.
273 You should have received a copy of the GNU General Public License
274 along with this program; if not, write to the Free Software Foundation,
275 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
278 #ifndef MODUTILS_OBJ_H
279 #define MODUTILS_OBJ_H 1
281 #ident "$Id: insmod.c,v 1.13 2000/07/09 06:59:58 andersen Exp $"
283 /* The relocatable object is manipulated using elfin types. */
289 /* Machine-specific elf macros for i386 et al. */
291 #define ELFCLASSM ELFCLASS32
292 #define ELFDATAM ELFDATA2LSB
294 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
296 #define SHT_RELM SHT_REL
297 #define Elf32_RelM Elf32_Rel
301 # if ELFCLASSM == ELFCLASS32
302 # define ElfW(x) Elf32_ ## x
303 # define ELFW(x) ELF32_ ## x
305 # define ElfW(x) Elf64_ ## x
306 # define ELFW(x) ELF64_ ## x
310 /* For some reason this is missing from libc5. */
311 #ifndef ELF32_ST_INFO
312 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
315 #ifndef ELF64_ST_INFO
316 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
319 struct obj_string_patch;
320 struct obj_symbol_patch;
327 struct obj_section *load_next;
333 struct obj_symbol *next; /* hash table link */
337 int secidx; /* the defining section index/module */
339 int ksymidx; /* for export to the kernel symtab */
340 int referenced; /* actually used in the link */
343 /* Hardcode the hash table size. We shouldn't be needing so many
344 symbols that we begin to degrade performance, and we get a big win
345 by giving the compiler a constant divisor. */
347 #define HASH_BUCKETS 521
353 struct obj_section **sections;
354 struct obj_section *load_order;
355 struct obj_section **load_order_search_start;
356 struct obj_string_patch *string_patches;
357 struct obj_symbol_patch *symbol_patches;
358 int (*symbol_cmp)(const char *, const char *);
359 unsigned long (*symbol_hash)(const char *);
360 unsigned long local_symtab_size;
361 struct obj_symbol **local_symtab;
362 struct obj_symbol *symtab[HASH_BUCKETS];
373 struct obj_string_patch
375 struct obj_string_patch *next;
377 ElfW(Addr) reloc_offset;
378 ElfW(Addr) string_offset;
381 struct obj_symbol_patch
383 struct obj_symbol_patch *next;
385 ElfW(Addr) reloc_offset;
386 struct obj_symbol *sym;
390 /* Generic object manipulation routines. */
392 unsigned long obj_elf_hash(const char *);
394 unsigned long obj_elf_hash_n(const char *, unsigned long len);
396 struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
397 unsigned long symidx, int info, int secidx,
398 ElfW(Addr) value, unsigned long size);
400 struct obj_symbol *obj_find_symbol (struct obj_file *f,
403 ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
404 struct obj_symbol *sym);
406 void obj_set_symbol_compare(struct obj_file *f,
407 int (*cmp)(const char *, const char *),
408 unsigned long (*hash)(const char *));
410 struct obj_section *obj_find_section (struct obj_file *f,
413 void obj_insert_section_load_order (struct obj_file *f,
414 struct obj_section *sec);
416 struct obj_section *obj_create_alloced_section (struct obj_file *f,
421 struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
426 void *obj_extend_section (struct obj_section *sec, unsigned long more);
428 int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
431 int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
432 struct obj_symbol *sym);
434 int obj_check_undefineds(struct obj_file *f);
436 void obj_allocate_commons(struct obj_file *f);
438 unsigned long obj_load_size (struct obj_file *f);
440 int obj_relocate (struct obj_file *f, ElfW(Addr) base);
442 struct obj_file *obj_load(FILE *f);
444 int obj_create_image (struct obj_file *f, char *image);
446 /* Architecture specific manipulation routines. */
448 struct obj_file *arch_new_file (void);
450 struct obj_section *arch_new_section (void);
452 struct obj_symbol *arch_new_symbol (void);
454 enum obj_reloc arch_apply_relocation (struct obj_file *f,
455 struct obj_section *targsec,
456 struct obj_section *symsec,
457 struct obj_symbol *sym,
458 ElfW(RelM) *rel, ElfW(Addr) value);
460 int arch_create_got (struct obj_file *f);
463 int arch_init_module (struct obj_file *f, struct new_module *);
466 //----------------------------------------------------------------------------
467 //--------end of modutils obj.h
468 //----------------------------------------------------------------------------
474 #define _PATH_MODULES "/lib/modules"
475 #define STRVERSIONLEN 32
477 #if !defined(BB_FEATURE_INSMOD_NEW_KERNEL) && !defined(BB_FEATURE_INSMOD_OLD_KERNEL)
478 #error "Must have ether BB_FEATURE_INSMOD_NEW_KERNEL or BB_FEATURE_INSMOD_OLD_KERNEL defined"
481 /*======================================================================*/
483 int flag_force_load = 0;
484 int flag_autoclean = 0;
485 int flag_verbose = 0;
489 /*======================================================================*/
491 struct i386_got_entry {
493 unsigned offset_done:1;
494 unsigned reloc_done:1;
498 struct obj_file root;
499 struct obj_section *got;
503 struct obj_symbol root;
504 struct i386_got_entry gotent;
509 struct external_module {
514 struct new_module_symbol *syms;
517 struct new_module_symbol *ksyms;
520 struct external_module *ext_modules;
522 int n_ext_modules_used;
526 /* Some firendly syscalls to cheer everyone's day... */
527 #define __NR_new_sys_init_module __NR_init_module
528 _syscall2(int, new_sys_init_module, const char *, name,
529 const struct new_module *, info)
530 #define __NR_old_sys_init_module __NR_init_module
531 _syscall5(int, old_sys_init_module, const char *, name, char *, code,
532 unsigned, codesize, struct old_mod_routines *, routines,
533 struct old_symbol_table *, symtab)
534 #ifndef __NR_query_module
535 #define __NR_query_module 167
537 _syscall5(int, query_module, const char *, name, int, which,
538 void *, buf, size_t, bufsize, size_t*, ret);
540 _syscall1(int, delete_module, const char *, name)
542 extern int delete_module(const char *);
545 #if defined(__i386__) || defined(__m68k__) || defined(__arm__)
546 /* Jump through hoops to fixup error return codes */
547 #define __NR__create_module __NR_create_module
548 static inline _syscall2(long, _create_module, const char *, name, size_t,
550 unsigned long create_module(const char *name, size_t size)
552 long ret = _create_module(name, size);
554 if (ret == -1 && errno > 125) {
561 _syscall2(unsigned long, create_module, const char *, name, size_t, size)
563 static char m_filename[BUFSIZ + 1] = "\0";
564 static char m_fullName[BUFSIZ + 1] = "\0";
565 static const char insmod_usage[] =
566 "insmod [OPTION]... MODULE [symbol=value]...\n"
567 #ifndef BB_FEATURE_TRIVIAL_HELP
568 "\nLoads the specified kernel modules into the kernel.\n\n"
570 "\t-f\tForce module to load into the wrong kernel version.\n"
571 "\t-k\tMake module autoclean-able.\n"
572 "\t-v\tverbose output\n" "\t-x\tdo not export externs\n"
576 /*======================================================================*/
578 void *xrealloc(void *old, size_t size)
580 void *ptr = realloc(old, size);
582 perror("Out of memory");
589 static int findNamedModule(const char *fileName, struct stat *statbuf,
592 char *fullName = (char *) userDate;
595 if (fullName[0] == '\0')
598 char *tmp = strrchr(fileName, '/');
601 tmp = (char *) fileName;
604 if (check_wildcard_match(tmp, fullName) == TRUE) {
605 /* Stop searching if we find a match */
606 memcpy(m_filename, fileName, strlen(fileName));
614 /*======================================================================*/
616 struct obj_file *arch_new_file(void)
619 f = xmalloc(sizeof(*f));
624 struct obj_section *arch_new_section(void)
626 return xmalloc(sizeof(struct obj_section));
629 struct obj_symbol *arch_new_symbol(void)
631 struct i386_symbol *sym;
632 sym = xmalloc(sizeof(*sym));
633 memset(&sym->gotent, 0, sizeof(sym->gotent));
637 arch_apply_relocation(struct obj_file *f,
638 struct obj_section *targsec,
639 struct obj_section *symsec,
640 struct obj_symbol *sym,
641 Elf32_Rel * rel, Elf32_Addr v)
643 struct i386_file *ifile = (struct i386_file *) f;
644 struct i386_symbol *isym = (struct i386_symbol *) sym;
646 Elf32_Addr *loc = (Elf32_Addr *) (targsec->contents + rel->r_offset);
647 Elf32_Addr dot = targsec->header.sh_addr + rel->r_offset;
648 Elf32_Addr got = ifile->got ? ifile->got->header.sh_addr : 0;
650 enum obj_reloc ret = obj_reloc_ok;
652 switch (ELF32_R_TYPE(rel->r_info)) {
680 assert(isym != NULL);
681 if (!isym->gotent.reloc_done) {
682 isym->gotent.reloc_done = 1;
683 *(Elf32_Addr *) (ifile->got->contents + isym->gotent.offset) =
686 *loc += isym->gotent.offset;
695 ret = obj_reloc_unhandled;
702 int arch_create_got(struct obj_file *f)
704 struct i386_file *ifile = (struct i386_file *) f;
705 int i, n, offset = 0, gotneeded = 0;
707 n = ifile->root.header.e_shnum;
708 for (i = 0; i < n; ++i) {
709 struct obj_section *relsec, *symsec, *strsec;
710 Elf32_Rel *rel, *relend;
714 relsec = ifile->root.sections[i];
715 if (relsec->header.sh_type != SHT_REL)
718 symsec = ifile->root.sections[relsec->header.sh_link];
719 strsec = ifile->root.sections[symsec->header.sh_link];
721 rel = (Elf32_Rel *) relsec->contents;
722 relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rel));
723 symtab = (Elf32_Sym *) symsec->contents;
724 strtab = (const char *) strsec->contents;
726 for (; rel < relend; ++rel) {
728 struct i386_symbol *intsym;
731 switch (ELF32_R_TYPE(rel->r_info)) {
742 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
744 name = strtab + extsym->st_name;
746 name = f->sections[extsym->st_shndx]->name;
748 (struct i386_symbol *) obj_find_symbol(&ifile->root, name);
750 if (!intsym->gotent.offset_done) {
751 intsym->gotent.offset_done = 1;
752 intsym->gotent.offset = offset;
758 if (offset > 0 || gotneeded)
760 obj_create_alloced_section(&ifile->root, ".got", 4, offset);
765 int arch_init_module(struct obj_file *f, struct new_module *mod)
771 /*======================================================================*/
773 /* Standard ELF hash function. */
774 inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
783 if ((g = (h & 0xf0000000)) != 0) {
792 unsigned long obj_elf_hash(const char *name)
794 return obj_elf_hash_n(name, strlen(name));
797 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
798 /* Get the kernel version in the canonical integer form. */
800 static int get_kernel_version(char str[STRVERSIONLEN])
802 struct utsname uts_info;
806 if (uname(&uts_info) < 0)
808 strncpy(str, uts_info.release, STRVERSIONLEN);
809 p = uts_info.release;
811 a = strtoul(p, &p, 10);
814 b = strtoul(p + 1, &p, 10);
817 c = strtoul(p + 1, &q, 10);
821 return a << 16 | b << 8 | c;
824 /* String comparison for non-co-versioned kernel and module. */
826 static int ncv_strcmp(const char *a, const char *b)
828 size_t alen = strlen(a), blen = strlen(b);
830 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
831 return strncmp(a, b, alen);
832 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
833 return strncmp(a, b, blen);
838 /* String hashing for non-co-versioned kernel and module. Here
839 we are simply forced to drop the crc from the hash. */
841 static unsigned long ncv_symbol_hash(const char *str)
843 size_t len = strlen(str);
844 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
846 return obj_elf_hash_n(str, len);
850 obj_set_symbol_compare(struct obj_file *f,
851 int (*cmp) (const char *, const char *),
852 unsigned long (*hash) (const char *))
857 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
860 f->symbol_hash = hash;
862 memcpy(tmptab, f->symtab, sizeof(tmptab));
863 memset(f->symtab, 0, sizeof(f->symtab));
865 for (i = 0; i < HASH_BUCKETS; ++i)
866 for (sym = tmptab[i]; sym; sym = next) {
867 unsigned long h = hash(sym->name) % HASH_BUCKETS;
869 sym->next = f->symtab[h];
875 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
878 struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
879 unsigned long symidx, int info,
880 int secidx, ElfW(Addr) value,
883 struct obj_symbol *sym;
884 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
885 int n_type = ELFW(ST_TYPE) (info);
886 int n_binding = ELFW(ST_BIND) (info);
888 for (sym = f->symtab[hash]; sym; sym = sym->next)
889 if (f->symbol_cmp(sym->name, name) == 0) {
890 int o_secidx = sym->secidx;
891 int o_info = sym->info;
892 int o_type = ELFW(ST_TYPE) (o_info);
893 int o_binding = ELFW(ST_BIND) (o_info);
895 /* A redefinition! Is it legal? */
897 if (secidx == SHN_UNDEF)
899 else if (o_secidx == SHN_UNDEF)
901 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
902 /* Cope with local and global symbols of the same name
903 in the same object file, as might have been created
904 by ld -r. The only reason locals are now seen at this
905 level at all is so that we can do semi-sensible things
908 struct obj_symbol *nsym, **p;
910 nsym = arch_new_symbol();
911 nsym->next = sym->next;
914 /* Excise the old (local) symbol from the hash chain. */
915 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
919 } else if (n_binding == STB_LOCAL) {
920 /* Another symbol of the same name has already been defined.
921 Just add this to the local table. */
922 sym = arch_new_symbol();
925 f->local_symtab[symidx] = sym;
927 } else if (n_binding == STB_WEAK)
929 else if (o_binding == STB_WEAK)
931 /* Don't unify COMMON symbols with object types the programmer
933 else if (secidx == SHN_COMMON
934 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
936 else if (o_secidx == SHN_COMMON
937 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
940 /* Don't report an error if the symbol is coming from
941 the kernel or some external module. */
942 if (secidx <= SHN_HIRESERVE)
943 fprintf(stderr, "%s multiply defined\n", name);
948 /* Completely new symbol. */
949 sym = arch_new_symbol();
950 sym->next = f->symtab[hash];
951 f->symtab[hash] = sym;
954 if (ELFW(ST_BIND) (info) == STB_LOCAL)
955 f->local_symtab[symidx] = sym;
961 sym->secidx = secidx;
967 struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
969 struct obj_symbol *sym;
970 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
972 for (sym = f->symtab[hash]; sym; sym = sym->next)
973 if (f->symbol_cmp(sym->name, name) == 0)
980 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
983 if (sym->secidx >= SHN_LORESERVE)
986 return sym->value + f->sections[sym->secidx]->header.sh_addr;
988 /* As a special case, a NULL sym has value zero. */
993 struct obj_section *obj_find_section(struct obj_file *f, const char *name)
995 int i, n = f->header.e_shnum;
997 for (i = 0; i < n; ++i)
998 if (strcmp(f->sections[i]->name, name) == 0)
999 return f->sections[i];
1004 static int obj_load_order_prio(struct obj_section *a)
1006 unsigned long af, ac;
1008 af = a->header.sh_flags;
1011 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1012 strcmp(a->name + 5, ".init"))
1016 if (!(af & SHF_WRITE))
1018 if (af & SHF_EXECINSTR)
1020 if (a->header.sh_type != SHT_NOBITS)
1027 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1029 struct obj_section **p;
1030 int prio = obj_load_order_prio(sec);
1031 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1032 if (obj_load_order_prio(*p) < prio)
1034 sec->load_next = *p;
1038 struct obj_section *obj_create_alloced_section(struct obj_file *f,
1040 unsigned long align,
1043 int newidx = f->header.e_shnum++;
1044 struct obj_section *sec;
1046 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1047 f->sections[newidx] = sec = arch_new_section();
1049 memset(sec, 0, sizeof(*sec));
1050 sec->header.sh_type = SHT_PROGBITS;
1051 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1052 sec->header.sh_size = size;
1053 sec->header.sh_addralign = align;
1057 sec->contents = xmalloc(size);
1059 obj_insert_section_load_order(f, sec);
1064 struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1066 unsigned long align,
1069 int newidx = f->header.e_shnum++;
1070 struct obj_section *sec;
1072 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1073 f->sections[newidx] = sec = arch_new_section();
1075 memset(sec, 0, sizeof(*sec));
1076 sec->header.sh_type = SHT_PROGBITS;
1077 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1078 sec->header.sh_size = size;
1079 sec->header.sh_addralign = align;
1083 sec->contents = xmalloc(size);
1085 sec->load_next = f->load_order;
1086 f->load_order = sec;
1087 if (f->load_order_search_start == &f->load_order)
1088 f->load_order_search_start = &sec->load_next;
1093 void *obj_extend_section(struct obj_section *sec, unsigned long more)
1095 unsigned long oldsize = sec->header.sh_size;
1096 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1097 return sec->contents + oldsize;
1102 /* Conditionally add the symbols from the given symbol set to the
1108 int idx, struct new_module_symbol *syms, size_t nsyms)
1110 struct new_module_symbol *s;
1114 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1116 /* Only add symbols that are already marked external. If we
1117 override locals we may cause problems for argument initialization.
1118 We will also create a false dependency on the module. */
1119 struct obj_symbol *sym;
1121 sym = obj_find_symbol(f, (char *) s->name);
1122 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1123 sym = obj_add_symbol(f, (char *) s->name, -1,
1124 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1126 /* Did our symbol just get installed? If so, mark the
1127 module as "used". */
1128 if (sym->secidx == idx)
1136 static void add_kernel_symbols(struct obj_file *f)
1138 struct external_module *m;
1139 size_t i, nused = 0;
1141 /* Add module symbols first. */
1143 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1145 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1146 m->nsyms)) m->used = 1, ++nused;
1148 n_ext_modules_used = nused;
1150 /* And finally the symbols from the kernel proper. */
1153 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1156 static char *get_modinfo_value(struct obj_file *f, const char *key)
1158 struct obj_section *sec;
1159 char *p, *v, *n, *ep;
1160 size_t klen = strlen(key);
1162 sec = obj_find_section(f, ".modinfo");
1166 ep = p + sec->header.sh_size;
1169 n = strchr(p, '\0');
1171 if (v - p == klen && strncmp(p, key, klen) == 0)
1174 if (n - p == klen && strcmp(p, key) == 0)
1184 /*======================================================================*/
1185 /* Functions relating to module loading in pre 2.1 kernels. */
1188 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1192 struct obj_symbol *sym;
1196 if ((q = strchr(p, '=')) == NULL) {
1202 sym = obj_find_symbol(f, p);
1204 /* Also check that the parameter was not resolved from the kernel. */
1205 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1206 fprintf(stderr, "symbol for parameter %s not found\n", p);
1210 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1212 /* Do C quoting if we begin with a ". */
1216 str = alloca(strlen(q));
1217 for (r = str, q++; *q != '"'; ++q, ++r) {
1219 fprintf(stderr, "improperly terminated string argument for %s\n", p);
1221 } else if (*q == '\\')
1255 if (q[1] >= '0' && q[1] <= '7') {
1256 c = (c * 8) + *++q - '0';
1257 if (q[1] >= '0' && q[1] <= '7')
1258 c = (c * 8) + *++q - '0';
1271 obj_string_patch(f, sym->secidx, sym->value, str);
1272 } else if (*q >= '0' && *q <= '9') {
1274 *loc++ = strtoul(q, &q, 0);
1275 while (*q++ == ',');
1277 char *contents = f->sections[sym->secidx]->contents;
1278 char *loc = contents + sym->value;
1279 char *r; /* To search for commas */
1281 /* Break the string with comas */
1282 while ((r = strchr(q, ',')) != (char *) NULL) {
1284 obj_string_patch(f, sym->secidx, loc - contents, q);
1285 loc += sizeof(char *);
1290 obj_string_patch(f, sym->secidx, loc - contents, q);
1299 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1300 static int old_is_module_checksummed(struct obj_file *f)
1302 return obj_find_symbol(f, "Using_Versions") != NULL;
1304 /* Get the module's kernel version in the canonical integer form. */
1307 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1309 struct obj_symbol *sym;
1313 sym = obj_find_symbol(f, "kernel_version");
1317 p = f->sections[sym->secidx]->contents + sym->value;
1318 strncpy(str, p, STRVERSIONLEN);
1320 a = strtoul(p, &p, 10);
1323 b = strtoul(p + 1, &p, 10);
1326 c = strtoul(p + 1, &q, 10);
1330 return a << 16 | b << 8 | c;
1333 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1335 #ifdef BB_FEATURE_INSMOD_OLD_KERNEL
1337 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1339 static int old_get_kernel_symbols(void)
1341 struct old_kernel_sym *ks, *k;
1342 struct new_module_symbol *s;
1343 struct external_module *mod;
1344 int nks, nms, nmod, i;
1346 nks = get_kernel_syms(NULL);
1348 errorMsg("get_kernel_syms: %s: %s", m_name, strerror(errno));
1352 ks = k = xmalloc(nks * sizeof(*ks));
1354 if (get_kernel_syms(ks) != nks) {
1355 perror("inconsistency with get_kernel_syms -- is someone else "
1356 "playing with modules?");
1361 /* Collect the module information. */
1366 while (k->name[0] == '#' && k->name[1]) {
1367 struct old_kernel_sym *k2;
1368 struct new_module_symbol *s;
1370 /* Find out how many symbols this module has. */
1371 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1375 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1376 mod[nmod].name = k->name + 1;
1377 mod[nmod].addr = k->value;
1379 mod[nmod].nsyms = nms;
1380 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1382 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1383 s->name = (unsigned long) k->name;
1384 s->value = k->value;
1391 n_ext_modules = nmod + 1;
1393 /* Now collect the symbols for the kernel proper. */
1395 if (k->name[0] == '#')
1398 nksyms = nms = nks - (k - ks);
1399 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1401 for (i = 0; i < nms; ++i, ++s, ++k) {
1402 s->name = (unsigned long) k->name;
1403 s->value = k->value;
1409 /* Return the kernel symbol checksum version, or zero if not used. */
1411 static int old_is_kernel_checksummed(void)
1413 /* Using_Versions is the first symbol. */
1415 && strcmp((char *) ksyms[0].name,
1416 "Using_Versions") == 0) return ksyms[0].value;
1422 static int old_create_mod_use_count(struct obj_file *f)
1424 struct obj_section *sec;
1426 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1429 obj_add_symbol(f, "mod_use_count_", -1,
1430 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1437 old_init_module(const char *m_name, struct obj_file *f,
1438 unsigned long m_size)
1441 struct old_mod_routines routines;
1442 struct old_symbol_table *symtab;
1445 /* Create the symbol table */
1447 int nsyms = 0, strsize = 0, total;
1449 /* Size things first... */
1452 for (i = 0; i < HASH_BUCKETS; ++i) {
1453 struct obj_symbol *sym;
1454 for (sym = f->symtab[i]; sym; sym = sym->next)
1455 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1456 && sym->secidx <= SHN_HIRESERVE)
1458 sym->ksymidx = nsyms++;
1459 strsize += strlen(sym->name) + 1;
1464 total = (sizeof(struct old_symbol_table)
1465 + nsyms * sizeof(struct old_module_symbol)
1466 + n_ext_modules_used * sizeof(struct old_module_ref)
1468 symtab = xmalloc(total);
1469 symtab->size = total;
1470 symtab->n_symbols = nsyms;
1471 symtab->n_refs = n_ext_modules_used;
1473 if (flag_export && nsyms) {
1474 struct old_module_symbol *ksym;
1478 ksym = symtab->symbol;
1479 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1480 + n_ext_modules_used * sizeof(struct old_module_ref));
1482 for (i = 0; i < HASH_BUCKETS; ++i) {
1483 struct obj_symbol *sym;
1484 for (sym = f->symtab[i]; sym; sym = sym->next)
1485 if (sym->ksymidx >= 0) {
1486 ksym->addr = obj_symbol_final_value(f, sym);
1488 (unsigned long) str - (unsigned long) symtab;
1490 str = stpcpy(str, sym->name) + 1;
1496 if (n_ext_modules_used) {
1497 struct old_module_ref *ref;
1500 ref = (struct old_module_ref *)
1501 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
1503 for (i = 0; i < n_ext_modules; ++i)
1504 if (ext_modules[i].used)
1505 ref++->module = ext_modules[i].addr;
1509 /* Fill in routines. */
1512 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
1514 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
1516 /* Whew! All of the initialization is complete. Collect the final
1517 module image and give it to the kernel. */
1519 image = xmalloc(m_size);
1520 obj_create_image(f, image);
1522 /* image holds the complete relocated module, accounting correctly for
1523 mod_use_count. However the old module kernel support assume that
1524 it is receiving something which does not contain mod_use_count. */
1525 ret = old_sys_init_module(m_name, image + sizeof(long),
1526 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
1527 : 0), &routines, symtab);
1529 errorMsg("init_module: %s: %s", m_name, strerror(errno));
1539 #define old_create_mod_use_count(x) TRUE
1540 #define old_init_module(x, y, z) TRUE
1542 #endif /* BB_FEATURE_INSMOD_OLD_KERNEL */
1546 /*======================================================================*/
1547 /* Functions relating to module loading after 2.1.18. */
1550 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
1554 struct obj_symbol *sym;
1555 char *contents, *loc;
1559 if ((q = strchr(p, '=')) == NULL) {
1564 key = alloca(q - p + 6);
1565 memcpy(key, "parm_", 5);
1566 memcpy(key + 5, p, q - p);
1569 p = get_modinfo_value(f, key);
1572 fprintf(stderr, "invalid parameter %s\n", key);
1576 sym = obj_find_symbol(f, key);
1578 /* Also check that the parameter was not resolved from the kernel. */
1579 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1580 fprintf(stderr, "symbol for parameter %s not found\n", key);
1585 min = strtoul(p, &p, 10);
1587 max = strtoul(p + 1, &p, 10);
1593 contents = f->sections[sym->secidx]->contents;
1594 loc = contents + sym->value;
1598 if ((*p == 's') || (*p == 'c')) {
1601 /* Do C quoting if we begin with a ", else slurp the lot. */
1605 str = alloca(strlen(q));
1606 for (r = str, q++; *q != '"'; ++q, ++r) {
1609 "improperly terminated string argument for %s\n",
1612 } else if (*q == '\\')
1646 if (q[1] >= '0' && q[1] <= '7') {
1647 c = (c * 8) + *++q - '0';
1648 if (q[1] >= '0' && q[1] <= '7')
1649 c = (c * 8) + *++q - '0';
1666 /* In this case, the string is not quoted. We will break
1667 it using the coma (like for ints). If the user wants to
1668 include comas in a string, he just has to quote it */
1670 /* Search the next coma */
1674 if (r != (char *) NULL) {
1675 /* Recopy the current field */
1676 str = alloca(r - q + 1);
1677 memcpy(str, q, r - q);
1679 /* I don't know if it is usefull, as the previous case
1680 doesn't null terminate the string ??? */
1683 /* Keep next fields */
1694 obj_string_patch(f, sym->secidx, loc - contents, str);
1695 loc += tgt_sizeof_char_p;
1697 /* Array of chars (in fact, matrix !) */
1698 long charssize; /* size of each member */
1700 /* Get the size of each member */
1701 /* Probably we should do that outside the loop ? */
1702 if (!isdigit(*(p + 1))) {
1704 "parameter type 'c' for %s must be followed by"
1705 " the maximum size\n", key);
1708 charssize = strtoul(p + 1, (char **) NULL, 10);
1711 if (strlen(str) >= charssize) {
1713 "string too long for %s (max %ld)\n", key,
1718 /* Copy to location */
1719 strcpy((char *) loc, str);
1723 long v = strtoul(q, &q, 0);
1730 loc += tgt_sizeof_short;
1734 loc += tgt_sizeof_int;
1738 loc += tgt_sizeof_long;
1742 fprintf(stderr, "unknown parameter type '%c' for %s\n",
1758 goto retry_end_of_value;
1762 fprintf(stderr, "too many values for %s (max %d)\n",
1770 fprintf(stderr, "invalid argument syntax for %s\n", key);
1777 fprintf(stderr, "too few values for %s (min %d)\n", key, min);
1787 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1788 static int new_is_module_checksummed(struct obj_file *f)
1790 const char *p = get_modinfo_value(f, "using_checksums");
1797 /* Get the module's kernel version in the canonical integer form. */
1800 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1805 p = get_modinfo_value(f, "kernel_version");
1808 strncpy(str, p, STRVERSIONLEN);
1810 a = strtoul(p, &p, 10);
1813 b = strtoul(p + 1, &p, 10);
1816 c = strtoul(p + 1, &q, 10);
1820 return a << 16 | b << 8 | c;
1823 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1826 #ifdef BB_FEATURE_INSMOD_NEW_KERNEL
1828 /* Fetch the loaded modules, and all currently exported symbols. */
1830 static int new_get_kernel_symbols(void)
1832 char *module_names, *mn;
1833 struct external_module *modules, *m;
1834 struct new_module_symbol *syms, *s;
1835 size_t ret, bufsize, nmod, nsyms, i, j;
1837 /* Collect the loaded modules. */
1839 module_names = xmalloc(bufsize = 256);
1841 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
1842 if (errno == ENOSPC) {
1843 module_names = xrealloc(module_names, bufsize = ret);
1844 goto retry_modules_load;
1846 errorMsg("QM_MODULES: %s", strerror(errno));
1850 n_ext_modules = nmod = ret;
1851 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
1852 memset(modules, 0, nmod * sizeof(*modules));
1854 /* Collect the modules' symbols. */
1856 for (i = 0, mn = module_names, m = modules;
1857 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
1858 struct new_module_info info;
1860 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
1861 if (errno == ENOENT) {
1862 /* The module was removed out from underneath us. */
1865 errorMsg("query_module: QM_INFO: %s: %s", mn, strerror(errno));
1869 syms = xmalloc(bufsize = 1024);
1871 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
1874 syms = xrealloc(syms, bufsize = ret);
1875 goto retry_mod_sym_load;
1877 /* The module was removed out from underneath us. */
1880 errorMsg("query_module: QM_SYMBOLS: %s: %s", mn, strerror(errno));
1887 m->addr = info.addr;
1891 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
1892 s->name += (unsigned long) syms;
1896 /* Collect the kernel's symbols. */
1898 syms = xmalloc(bufsize = 16 * 1024);
1899 retry_kern_sym_load:
1900 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
1901 if (errno == ENOSPC) {
1902 syms = xrealloc(syms, bufsize = ret);
1903 goto retry_kern_sym_load;
1905 errorMsg("kernel: QM_SYMBOLS: %s", strerror(errno));
1908 nksyms = nsyms = ret;
1911 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
1912 s->name += (unsigned long) syms;
1918 /* Return the kernel symbol checksum version, or zero if not used. */
1920 static int new_is_kernel_checksummed(void)
1922 struct new_module_symbol *s;
1925 /* Using_Versions is not the first symbol, but it should be in there. */
1927 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
1928 if (strcmp((char *) s->name, "Using_Versions") == 0)
1935 static int new_create_this_module(struct obj_file *f, const char *m_name)
1937 struct obj_section *sec;
1939 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
1940 sizeof(struct new_module));
1941 memset(sec->contents, 0, sizeof(struct new_module));
1943 obj_add_symbol(f, "__this_module", -1,
1944 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1945 sizeof(struct new_module));
1947 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
1954 static int new_create_module_ksymtab(struct obj_file *f)
1956 struct obj_section *sec;
1959 /* We must always add the module references. */
1961 if (n_ext_modules_used) {
1962 struct new_module_ref *dep;
1963 struct obj_symbol *tm;
1965 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
1966 (sizeof(struct new_module_ref)
1967 * n_ext_modules_used));
1971 tm = obj_find_symbol(f, "__this_module");
1972 dep = (struct new_module_ref *) sec->contents;
1973 for (i = 0; i < n_ext_modules; ++i)
1974 if (ext_modules[i].used) {
1975 dep->dep = ext_modules[i].addr;
1976 obj_symbol_patch(f, sec->idx,
1977 (char *) &dep->ref - sec->contents, tm);
1983 if (flag_export && !obj_find_section(f, "__ksymtab")) {
1988 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
1991 /* We don't want to export symbols residing in sections that
1992 aren't loaded. There are a number of these created so that
1993 we make sure certain module options don't appear twice. */
1995 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
1997 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
1999 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2000 struct obj_symbol *sym;
2001 for (sym = f->symtab[i]; sym; sym = sym->next)
2002 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
2003 && sym->secidx <= SHN_HIRESERVE
2004 && (sym->secidx >= SHN_LORESERVE
2005 || loaded[sym->secidx])) {
2006 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2008 obj_symbol_patch(f, sec->idx, ofs, sym);
2009 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2016 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2024 new_init_module(const char *m_name, struct obj_file *f,
2025 unsigned long m_size)
2027 struct new_module *module;
2028 struct obj_section *sec;
2033 sec = obj_find_section(f, ".this");
2034 module = (struct new_module *) sec->contents;
2035 m_addr = sec->header.sh_addr;
2037 module->size_of_struct = sizeof(*module);
2038 module->size = m_size;
2039 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2041 sec = obj_find_section(f, "__ksymtab");
2042 if (sec && sec->header.sh_size) {
2043 module->syms = sec->header.sh_addr;
2044 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2047 if (n_ext_modules_used) {
2048 sec = obj_find_section(f, ".kmodtab");
2049 module->deps = sec->header.sh_addr;
2050 module->ndeps = n_ext_modules_used;
2054 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2056 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2058 sec = obj_find_section(f, "__ex_table");
2060 module->ex_table_start = sec->header.sh_addr;
2061 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2064 sec = obj_find_section(f, ".text.init");
2066 module->runsize = sec->header.sh_addr - m_addr;
2068 sec = obj_find_section(f, ".data.init");
2070 if (!module->runsize ||
2071 module->runsize > sec->header.sh_addr - m_addr)
2072 module->runsize = sec->header.sh_addr - m_addr;
2075 if (!arch_init_module(f, module))
2078 /* Whew! All of the initialization is complete. Collect the final
2079 module image and give it to the kernel. */
2081 image = xmalloc(m_size);
2082 obj_create_image(f, image);
2084 ret = new_sys_init_module(m_name, (struct new_module *) image);
2086 errorMsg("init_module: %s: %s", m_name, strerror(errno));
2095 #define new_init_module(x, y, z) TRUE
2096 #define new_create_this_module(x, y) 0
2097 #define new_create_module_ksymtab(x)
2099 #endif /* BB_FEATURE_INSMOD_OLD_KERNEL */
2102 /*======================================================================*/
2105 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2108 struct obj_string_patch *p;
2109 struct obj_section *strsec;
2110 size_t len = strlen(string) + 1;
2113 p = xmalloc(sizeof(*p));
2114 p->next = f->string_patches;
2115 p->reloc_secidx = secidx;
2116 p->reloc_offset = offset;
2117 f->string_patches = p;
2119 strsec = obj_find_section(f, ".kstrtab");
2120 if (strsec == NULL) {
2121 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2122 p->string_offset = 0;
2123 loc = strsec->contents;
2125 p->string_offset = strsec->header.sh_size;
2126 loc = obj_extend_section(strsec, len);
2128 memcpy(loc, string, len);
2134 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2135 struct obj_symbol *sym)
2137 struct obj_symbol_patch *p;
2139 p = xmalloc(sizeof(*p));
2140 p->next = f->symbol_patches;
2141 p->reloc_secidx = secidx;
2142 p->reloc_offset = offset;
2144 f->symbol_patches = p;
2149 int obj_check_undefineds(struct obj_file *f)
2154 for (i = 0; i < HASH_BUCKETS; ++i) {
2155 struct obj_symbol *sym;
2156 for (sym = f->symtab[i]; sym; sym = sym->next)
2157 if (sym->secidx == SHN_UNDEF) {
2158 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2159 sym->secidx = SHN_ABS;
2162 fprintf(stderr, "unresolved symbol %s\n", sym->name);
2171 void obj_allocate_commons(struct obj_file *f)
2173 struct common_entry {
2174 struct common_entry *next;
2175 struct obj_symbol *sym;
2176 } *common_head = NULL;
2180 for (i = 0; i < HASH_BUCKETS; ++i) {
2181 struct obj_symbol *sym;
2182 for (sym = f->symtab[i]; sym; sym = sym->next)
2183 if (sym->secidx == SHN_COMMON) {
2184 /* Collect all COMMON symbols and sort them by size so as to
2185 minimize space wasted by alignment requirements. */
2187 struct common_entry **p, *n;
2188 for (p = &common_head; *p; p = &(*p)->next)
2189 if (sym->size <= (*p)->sym->size)
2192 n = alloca(sizeof(*n));
2200 for (i = 1; i < f->local_symtab_size; ++i) {
2201 struct obj_symbol *sym = f->local_symtab[i];
2202 if (sym && sym->secidx == SHN_COMMON) {
2203 struct common_entry **p, *n;
2204 for (p = &common_head; *p; p = &(*p)->next)
2205 if (sym == (*p)->sym)
2207 else if (sym->size < (*p)->sym->size) {
2208 n = alloca(sizeof(*n));
2218 /* Find the bss section. */
2219 for (i = 0; i < f->header.e_shnum; ++i)
2220 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2223 /* If for some reason there hadn't been one, create one. */
2224 if (i == f->header.e_shnum) {
2225 struct obj_section *sec;
2227 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2228 f->sections[i] = sec = arch_new_section();
2229 f->header.e_shnum = i + 1;
2231 memset(sec, 0, sizeof(*sec));
2232 sec->header.sh_type = SHT_PROGBITS;
2233 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2238 /* Allocate the COMMONS. */
2240 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2241 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2242 struct common_entry *c;
2244 for (c = common_head; c; c = c->next) {
2245 ElfW(Addr) align = c->sym->value;
2247 if (align > max_align)
2249 if (bss_size & (align - 1))
2250 bss_size = (bss_size | (align - 1)) + 1;
2253 c->sym->value = bss_size;
2255 bss_size += c->sym->size;
2258 f->sections[i]->header.sh_size = bss_size;
2259 f->sections[i]->header.sh_addralign = max_align;
2263 /* For the sake of patch relocation and parameter initialization,
2264 allocate zeroed data for NOBITS sections now. Note that after
2265 this we cannot assume NOBITS are really empty. */
2266 for (i = 0; i < f->header.e_shnum; ++i) {
2267 struct obj_section *s = f->sections[i];
2268 if (s->header.sh_type == SHT_NOBITS) {
2269 s->contents = memset(xmalloc(s->header.sh_size),
2270 0, s->header.sh_size);
2271 s->header.sh_type = SHT_PROGBITS;
2276 unsigned long obj_load_size(struct obj_file *f)
2278 unsigned long dot = 0;
2279 struct obj_section *sec;
2281 /* Finalize the positions of the sections relative to one another. */
2283 for (sec = f->load_order; sec; sec = sec->load_next) {
2286 align = sec->header.sh_addralign;
2287 if (align && (dot & (align - 1)))
2288 dot = (dot | (align - 1)) + 1;
2290 sec->header.sh_addr = dot;
2291 dot += sec->header.sh_size;
2297 int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2299 int i, n = f->header.e_shnum;
2302 /* Finalize the addresses of the sections. */
2305 for (i = 0; i < n; ++i)
2306 f->sections[i]->header.sh_addr += base;
2308 /* And iterate over all of the relocations. */
2310 for (i = 0; i < n; ++i) {
2311 struct obj_section *relsec, *symsec, *targsec, *strsec;
2312 ElfW(RelM) * rel, *relend;
2316 relsec = f->sections[i];
2317 if (relsec->header.sh_type != SHT_RELM)
2320 symsec = f->sections[relsec->header.sh_link];
2321 targsec = f->sections[relsec->header.sh_info];
2322 strsec = f->sections[symsec->header.sh_link];
2324 rel = (ElfW(RelM) *) relsec->contents;
2325 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2326 symtab = (ElfW(Sym) *) symsec->contents;
2327 strtab = (const char *) strsec->contents;
2329 for (; rel < relend; ++rel) {
2330 ElfW(Addr) value = 0;
2331 struct obj_symbol *intsym = NULL;
2332 unsigned long symndx;
2333 ElfW(Sym) * extsym = 0;
2336 /* Attempt to find a value to use for this relocation. */
2338 symndx = ELFW(R_SYM) (rel->r_info);
2340 /* Note we've already checked for undefined symbols. */
2342 extsym = &symtab[symndx];
2343 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2344 /* Local symbols we look up in the local table to be sure
2345 we get the one that is really intended. */
2346 intsym = f->local_symtab[symndx];
2348 /* Others we look up in the hash table. */
2350 if (extsym->st_name)
2351 name = strtab + extsym->st_name;
2353 name = f->sections[extsym->st_shndx]->name;
2354 intsym = obj_find_symbol(f, name);
2357 value = obj_symbol_final_value(f, intsym);
2358 intsym->referenced = 1;
2360 #if SHT_RELM == SHT_RELA
2361 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2362 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2363 if (!extsym || !extsym->st_name ||
2364 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2366 value += rel->r_addend;
2370 switch (arch_apply_relocation
2371 (f, targsec, symsec, intsym, rel, value)) {
2375 case obj_reloc_overflow:
2376 errmsg = "Relocation overflow";
2378 case obj_reloc_dangerous:
2379 errmsg = "Dangerous relocation";
2381 case obj_reloc_unhandled:
2382 errmsg = "Unhandled relocation";
2385 fprintf(stderr, "%s of type %ld for %s\n", errmsg,
2386 (long) ELFW(R_TYPE) (rel->r_info),
2387 strtab + extsym->st_name);
2389 fprintf(stderr, "%s of type %ld\n", errmsg,
2390 (long) ELFW(R_TYPE) (rel->r_info));
2398 /* Finally, take care of the patches. */
2400 if (f->string_patches) {
2401 struct obj_string_patch *p;
2402 struct obj_section *strsec;
2403 ElfW(Addr) strsec_base;
2404 strsec = obj_find_section(f, ".kstrtab");
2405 strsec_base = strsec->header.sh_addr;
2407 for (p = f->string_patches; p; p = p->next) {
2408 struct obj_section *targsec = f->sections[p->reloc_secidx];
2409 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2410 = strsec_base + p->string_offset;
2414 if (f->symbol_patches) {
2415 struct obj_symbol_patch *p;
2417 for (p = f->symbol_patches; p; p = p->next) {
2418 struct obj_section *targsec = f->sections[p->reloc_secidx];
2419 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2420 = obj_symbol_final_value(f, p->sym);
2427 int obj_create_image(struct obj_file *f, char *image)
2429 struct obj_section *sec;
2430 ElfW(Addr) base = f->baseaddr;
2432 for (sec = f->load_order; sec; sec = sec->load_next) {
2435 if (sec->header.sh_size == 0)
2438 secimg = image + (sec->header.sh_addr - base);
2440 /* Note that we allocated data for NOBITS sections earlier. */
2441 memcpy(secimg, sec->contents, sec->header.sh_size);
2447 /*======================================================================*/
2449 struct obj_file *obj_load(FILE * fp)
2452 ElfW(Shdr) * section_headers;
2456 /* Read the file header. */
2458 f = arch_new_file();
2459 memset(f, 0, sizeof(*f));
2460 f->symbol_cmp = strcmp;
2461 f->symbol_hash = obj_elf_hash;
2462 f->load_order_search_start = &f->load_order;
2464 fseek(fp, 0, SEEK_SET);
2465 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2466 errorMsg("error reading ELF header: %s", strerror(errno));
2470 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2471 || f->header.e_ident[EI_MAG1] != ELFMAG1
2472 || f->header.e_ident[EI_MAG2] != ELFMAG2
2473 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2474 fprintf(stderr, "not an ELF file\n");
2477 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2478 || f->header.e_ident[EI_DATA] != ELFDATAM
2479 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2480 || !MATCH_MACHINE(f->header.e_machine)) {
2481 fprintf(stderr, "ELF file not for this architecture\n");
2484 if (f->header.e_type != ET_REL) {
2485 fprintf(stderr, "ELF file not a relocatable object\n");
2489 /* Read the section headers. */
2491 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
2492 fprintf(stderr, "section header size mismatch: %lu != %lu\n",
2493 (unsigned long) f->header.e_shentsize,
2494 (unsigned long) sizeof(ElfW(Shdr)));
2498 shnum = f->header.e_shnum;
2499 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
2500 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
2502 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
2503 fseek(fp, f->header.e_shoff, SEEK_SET);
2504 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
2505 errorMsg("error reading ELF section headers: %s", strerror(errno));
2509 /* Read the section data. */
2511 for (i = 0; i < shnum; ++i) {
2512 struct obj_section *sec;
2514 f->sections[i] = sec = arch_new_section();
2515 memset(sec, 0, sizeof(*sec));
2517 sec->header = section_headers[i];
2520 switch (sec->header.sh_type) {
2531 if (sec->header.sh_size > 0) {
2532 sec->contents = xmalloc(sec->header.sh_size);
2533 fseek(fp, sec->header.sh_offset, SEEK_SET);
2534 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
2535 errorMsg("error reading ELF section data: %s", strerror(errno));
2539 sec->contents = NULL;
2543 #if SHT_RELM == SHT_REL
2546 "RELA relocations not supported on this architecture\n");
2551 "REL relocations not supported on this architecture\n");
2556 if (sec->header.sh_type >= SHT_LOPROC) {
2557 /* Assume processor specific section types are debug
2558 info and can safely be ignored. If this is ever not
2559 the case (Hello MIPS?), don't put ifdefs here but
2560 create an arch_load_proc_section(). */
2564 fprintf(stderr, "can't handle sections of type %ld\n",
2565 (long) sec->header.sh_type);
2570 /* Do what sort of interpretation as needed by each section. */
2572 shstrtab = f->sections[f->header.e_shstrndx]->contents;
2574 for (i = 0; i < shnum; ++i) {
2575 struct obj_section *sec = f->sections[i];
2576 sec->name = shstrtab + sec->header.sh_name;
2579 for (i = 0; i < shnum; ++i) {
2580 struct obj_section *sec = f->sections[i];
2582 if (sec->header.sh_flags & SHF_ALLOC)
2583 obj_insert_section_load_order(f, sec);
2585 switch (sec->header.sh_type) {
2588 unsigned long nsym, j;
2592 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
2593 fprintf(stderr, "symbol size mismatch: %lu != %lu\n",
2594 (unsigned long) sec->header.sh_entsize,
2595 (unsigned long) sizeof(ElfW(Sym)));
2599 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
2600 strtab = f->sections[sec->header.sh_link]->contents;
2601 sym = (ElfW(Sym) *) sec->contents;
2603 /* Allocate space for a table of local symbols. */
2604 j = f->local_symtab_size = sec->header.sh_info;
2605 f->local_symtab = xmalloc(j *=
2606 sizeof(struct obj_symbol *));
2607 memset(f->local_symtab, 0, j);
2609 /* Insert all symbols into the hash table. */
2610 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
2613 name = strtab + sym->st_name;
2615 name = f->sections[sym->st_shndx]->name;
2617 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
2618 sym->st_value, sym->st_size);
2624 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
2626 "relocation entry size mismatch: %lu != %lu\n",
2627 (unsigned long) sec->header.sh_entsize,
2628 (unsigned long) sizeof(ElfW(RelM)));
2638 static void hide_special_symbols(struct obj_file *f)
2640 static const char *const specials[] = {
2647 struct obj_symbol *sym;
2648 const char *const *p;
2650 for (p = specials; *p; ++p)
2651 if ((sym = obj_find_symbol(f, *p)) != NULL)
2653 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
2658 extern int insmod_main( int argc, char **argv)
2664 unsigned long m_size;
2668 char m_name[BUFSIZ + 1] = "\0";
2669 int exit_status = FALSE;
2671 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2673 char k_strversion[STRVERSIONLEN];
2674 char m_strversion[STRVERSIONLEN];
2681 usage(insmod_usage);
2684 /* Parse any options */
2685 while (--argc > 0 && **(++argv) == '-') {
2686 while (*(++(*argv))) {
2688 case 'f': /* force loading */
2689 flag_force_load = 1;
2691 case 'k': /* module loaded by kerneld, auto-cleanable */
2694 case 'v': /* verbose output */
2697 case 'x': /* do not export externs */
2701 usage(insmod_usage);
2707 usage(insmod_usage);
2709 /* Grab the module name */
2710 if ((tmp = strrchr(*argv, '/')) != NULL) {
2717 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
2719 memcpy(m_name, tmp, len);
2720 strcpy(m_fullName, m_name);
2721 strcat(m_fullName, ".o");
2723 /* Get a filedesc for the module */
2724 if ((fp = fopen(*argv, "r")) == NULL) {
2725 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
2726 if (recursiveAction(_PATH_MODULES, TRUE, FALSE, FALSE,
2727 findNamedModule, 0, m_fullName) == TRUE)
2729 if (m_filename[0] == '\0'
2730 || ((fp = fopen(m_filename, "r")) == NULL))
2732 errorMsg("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
2737 memcpy(m_filename, *argv, strlen(*argv));
2740 if ((f = obj_load(fp)) == NULL) {
2741 perror("Could not load the module\n");
2745 if (get_modinfo_value(f, "kernel_version") == NULL)
2750 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2751 /* Version correspondence? */
2753 k_version = get_kernel_version(k_strversion);
2754 if (m_has_modinfo) {
2755 m_version = new_get_module_version(f, m_strversion);
2757 m_version = old_get_module_version(f, m_strversion);
2758 if (m_version == -1) {
2760 "couldn't find the kernel version the module was compiled for\n");
2765 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
2766 if (flag_force_load) {
2767 fprintf(stderr, "Warning: kernel-module version mismatch\n"
2768 "\t%s was compiled for kernel version %s\n"
2769 "\twhile this kernel is version %s\n",
2770 m_filename, m_strversion, k_strversion);
2772 fprintf(stderr, "kernel-module version mismatch\n"
2773 "\t%s was compiled for kernel version %s\n"
2774 "\twhile this kernel is version %s.\n",
2775 m_filename, m_strversion, k_strversion);
2780 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2782 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
2784 if (k_new_syscalls) {
2785 #ifdef BB_FEATURE_INSMOD_NEW_KERNEL
2786 if (!new_get_kernel_symbols())
2788 k_crcs = new_is_kernel_checksummed();
2790 fprintf(stderr, "Not configured to support new kernels\n");
2794 #ifdef BB_FEATURE_INSMOD_OLD_KERNEL
2795 if (!old_get_kernel_symbols())
2797 k_crcs = old_is_kernel_checksummed();
2799 fprintf(stderr, "Not configured to support old kernels\n");
2804 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2806 m_crcs = new_is_module_checksummed(f);
2808 m_crcs = old_is_module_checksummed(f);
2810 if (m_crcs != k_crcs)
2811 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
2812 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2814 /* Let the module know about the kernel symbols. */
2815 add_kernel_symbols(f);
2817 /* Allocate common symbols, symbol tables, and string tables. */
2820 ? !new_create_this_module(f, m_name)
2821 : !old_create_mod_use_count(f))
2826 if (!obj_check_undefineds(f)) {
2829 obj_allocate_commons(f);
2831 if (optind < argc) {
2833 ? !new_process_module_arguments(f, argc - optind, argv + optind)
2834 : !old_process_module_arguments(f, argc - optind, argv + optind))
2841 hide_special_symbols(f);
2844 new_create_module_ksymtab(f);
2846 /* Find current size of the module */
2847 m_size = obj_load_size(f);
2851 m_addr = create_module(m_name, m_size);
2856 fprintf(stderr, "A module named %s already exists\n", m_name);
2860 "Can't allocate kernel memory for module; needed %lu bytes\n",
2864 errorMsg("create_module: %s: %s", m_name, strerror(errno));
2868 if (!obj_relocate(f, m_addr)) {
2869 delete_module(m_name);
2874 ? !new_init_module(m_name, f, m_size)
2875 : !old_init_module(m_name, f, m_size))
2877 delete_module(m_name);
2885 return(exit_status);