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
41 #include <sys/utsname.h>
42 #include <sys/syscall.h>
43 #include <linux/unistd.h>
45 //----------------------------------------------------------------------------
46 //--------modutils module.h, lines 45-242
47 //----------------------------------------------------------------------------
49 /* Definitions for the Linux module syscall interface.
50 Copyright 1996, 1997 Linux International.
52 Contributed by Richard Henderson <rth@tamu.edu>
54 This file is part of the Linux modutils.
56 This program is free software; you can redistribute it and/or modify it
57 under the terms of the GNU General Public License as published by the
58 Free Software Foundation; either version 2 of the License, or (at your
59 option) any later version.
61 This program is distributed in the hope that it will be useful, but
62 WITHOUT ANY WARRANTY; without even the implied warranty of
63 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
64 General Public License for more details.
66 You should have received a copy of the GNU General Public License
67 along with this program; if not, write to the Free Software Foundation,
68 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
71 #ifndef MODUTILS_MODULE_H
72 #define MODUTILS_MODULE_H 1
74 #ident "$Id: insmod.c,v 1.9 2000/06/19 19:53:30 andersen Exp $"
76 /* This file contains the structures used by the 2.0 and 2.1 kernels.
77 We do not use the kernel headers directly because we do not wish
78 to be dependant on a particular kernel version to compile insmod. */
81 /*======================================================================*/
82 /* The structures used by Linux 2.0. */
84 /* The symbol format used by get_kernel_syms(2). */
93 unsigned long module; /* kernel addresses */
97 struct old_module_symbol
103 struct old_symbol_table
105 int size; /* total, including string table!!! */
108 struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
109 struct old_module_ref ref[0]; /* actual size defined by n_refs */
112 struct old_mod_routines
115 unsigned long cleanup;
121 unsigned long ref; /* the list of modules that refer to me */
122 unsigned long symtab;
124 int size; /* size of module in pages */
125 unsigned long addr; /* address of module */
127 unsigned long cleanup; /* cleanup routine */
130 /* Sent to init_module(2) or'ed into the code size parameter. */
131 #define OLD_MOD_AUTOCLEAN 0x40000000 /* big enough, but no sign problems... */
133 int get_kernel_syms(struct old_kernel_sym *);
134 int old_sys_init_module(const char *name, char *code, unsigned codesize,
135 struct old_mod_routines *, struct old_symbol_table *);
137 /*======================================================================*/
138 /* For sizeof() which are related to the module platform and not to the
139 environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
141 #define tgt_sizeof_char sizeof(char)
142 #define tgt_sizeof_short sizeof(short)
143 #define tgt_sizeof_int sizeof(int)
144 #define tgt_sizeof_long sizeof(long)
145 #define tgt_sizeof_char_p sizeof(char *)
146 #define tgt_sizeof_void_p sizeof(void *)
147 #define tgt_long long
149 #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
150 #undef tgt_sizeof_long
151 #undef tgt_sizeof_char_p
152 #undef tgt_sizeof_void_p
154 #define tgt_sizeof_long 8
155 #define tgt_sizeof_char_p 8
156 #define tgt_sizeof_void_p 8
157 #define tgt_long long long
160 /*======================================================================*/
161 /* The structures used in Linux 2.1. */
163 /* Note: new_module_symbol does not use tgt_long intentionally */
164 struct new_module_symbol
170 struct new_module_persist;
172 struct new_module_ref
174 unsigned tgt_long dep; /* kernel addresses */
175 unsigned tgt_long ref;
176 unsigned tgt_long next_ref;
181 unsigned tgt_long size_of_struct; /* == sizeof(module) */
182 unsigned tgt_long next;
183 unsigned tgt_long name;
184 unsigned tgt_long size;
187 unsigned tgt_long flags; /* AUTOCLEAN et al */
192 unsigned tgt_long syms;
193 unsigned tgt_long deps;
194 unsigned tgt_long refs;
195 unsigned tgt_long init;
196 unsigned tgt_long cleanup;
197 unsigned tgt_long ex_table_start;
198 unsigned tgt_long ex_table_end;
200 unsigned tgt_long gp;
202 /* Everything after here is extension. */
203 unsigned tgt_long persist_start;
204 unsigned tgt_long persist_end;
205 unsigned tgt_long can_unload;
206 unsigned tgt_long runsize;
209 struct new_module_info
217 /* Bits of module.flags. */
218 #define NEW_MOD_RUNNING 1
219 #define NEW_MOD_DELETED 2
220 #define NEW_MOD_AUTOCLEAN 4
221 #define NEW_MOD_VISITED 8
222 #define NEW_MOD_USED_ONCE 16
224 int new_sys_init_module(const char *name, const struct new_module *);
225 int query_module(const char *name, int which, void *buf, size_t bufsize,
228 /* Values for query_module's which. */
236 /*======================================================================*/
237 /* The system calls unchanged between 2.0 and 2.1. */
239 unsigned long create_module(const char *, size_t);
240 int delete_module(const char *);
243 #endif /* module.h */
245 //----------------------------------------------------------------------------
246 //--------end of modutils module.h
247 //----------------------------------------------------------------------------
251 //----------------------------------------------------------------------------
252 //--------modutils obj.h, lines 253-462
253 //----------------------------------------------------------------------------
255 /* Elf object file loading and relocation routines.
256 Copyright 1996, 1997 Linux International.
258 Contributed by Richard Henderson <rth@tamu.edu>
260 This file is part of the Linux modutils.
262 This program is free software; you can redistribute it and/or modify it
263 under the terms of the GNU General Public License as published by the
264 Free Software Foundation; either version 2 of the License, or (at your
265 option) any later version.
267 This program is distributed in the hope that it will be useful, but
268 WITHOUT ANY WARRANTY; without even the implied warranty of
269 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
270 General Public License for more details.
272 You should have received a copy of the GNU General Public License
273 along with this program; if not, write to the Free Software Foundation,
274 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
277 #ifndef MODUTILS_OBJ_H
278 #define MODUTILS_OBJ_H 1
280 #ident "$Id: insmod.c,v 1.9 2000/06/19 19:53:30 andersen Exp $"
282 /* The relocatable object is manipulated using elfin types. */
288 /* Machine-specific elf macros for i386 et al. */
290 #define ELFCLASSM ELFCLASS32
291 #define ELFDATAM ELFDATA2LSB
293 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
295 #define SHT_RELM SHT_REL
296 #define Elf32_RelM Elf32_Rel
300 # if ELFCLASSM == ELFCLASS32
301 # define ElfW(x) Elf32_ ## x
302 # define ELFW(x) ELF32_ ## x
304 # define ElfW(x) Elf64_ ## x
305 # define ELFW(x) ELF64_ ## x
309 /* For some reason this is missing from libc5. */
310 #ifndef ELF32_ST_INFO
311 # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
314 #ifndef ELF64_ST_INFO
315 # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
318 struct obj_string_patch;
319 struct obj_symbol_patch;
326 struct obj_section *load_next;
332 struct obj_symbol *next; /* hash table link */
336 int secidx; /* the defining section index/module */
338 int ksymidx; /* for export to the kernel symtab */
339 int referenced; /* actually used in the link */
342 /* Hardcode the hash table size. We shouldn't be needing so many
343 symbols that we begin to degrade performance, and we get a big win
344 by giving the compiler a constant divisor. */
346 #define HASH_BUCKETS 521
352 struct obj_section **sections;
353 struct obj_section *load_order;
354 struct obj_section **load_order_search_start;
355 struct obj_string_patch *string_patches;
356 struct obj_symbol_patch *symbol_patches;
357 int (*symbol_cmp)(const char *, const char *);
358 unsigned long (*symbol_hash)(const char *);
359 unsigned long local_symtab_size;
360 struct obj_symbol **local_symtab;
361 struct obj_symbol *symtab[HASH_BUCKETS];
372 struct obj_string_patch
374 struct obj_string_patch *next;
376 ElfW(Addr) reloc_offset;
377 ElfW(Addr) string_offset;
380 struct obj_symbol_patch
382 struct obj_symbol_patch *next;
384 ElfW(Addr) reloc_offset;
385 struct obj_symbol *sym;
389 /* Generic object manipulation routines. */
391 unsigned long obj_elf_hash(const char *);
393 unsigned long obj_elf_hash_n(const char *, unsigned long len);
395 struct obj_symbol *obj_add_symbol (struct obj_file *f, const char *name,
396 unsigned long symidx, int info, int secidx,
397 ElfW(Addr) value, unsigned long size);
399 struct obj_symbol *obj_find_symbol (struct obj_file *f,
402 ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
403 struct obj_symbol *sym);
405 void obj_set_symbol_compare(struct obj_file *f,
406 int (*cmp)(const char *, const char *),
407 unsigned long (*hash)(const char *));
409 struct obj_section *obj_find_section (struct obj_file *f,
412 void obj_insert_section_load_order (struct obj_file *f,
413 struct obj_section *sec);
415 struct obj_section *obj_create_alloced_section (struct obj_file *f,
420 struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
425 void *obj_extend_section (struct obj_section *sec, unsigned long more);
427 int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
430 int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
431 struct obj_symbol *sym);
433 int obj_check_undefineds(struct obj_file *f);
435 void obj_allocate_commons(struct obj_file *f);
437 unsigned long obj_load_size (struct obj_file *f);
439 int obj_relocate (struct obj_file *f, ElfW(Addr) base);
441 struct obj_file *obj_load(FILE *f);
443 int obj_create_image (struct obj_file *f, char *image);
445 /* Architecture specific manipulation routines. */
447 struct obj_file *arch_new_file (void);
449 struct obj_section *arch_new_section (void);
451 struct obj_symbol *arch_new_symbol (void);
453 enum obj_reloc arch_apply_relocation (struct obj_file *f,
454 struct obj_section *targsec,
455 struct obj_section *symsec,
456 struct obj_symbol *sym,
457 ElfW(RelM) *rel, ElfW(Addr) value);
459 int arch_create_got (struct obj_file *f);
462 int arch_init_module (struct obj_file *f, struct new_module *);
465 //----------------------------------------------------------------------------
466 //--------end of modutils obj.h
467 //----------------------------------------------------------------------------
473 #define _PATH_MODULES "/lib/modules"
474 #define STRVERSIONLEN 32
476 #if !defined(BB_FEATURE_INSMOD_NEW_KERNEL) && !defined(BB_FEATURE_INSMOD_OLD_KERNEL)
477 #error "Must have ether BB_FEATURE_INSMOD_NEW_KERNEL or BB_FEATURE_INSMOD_OLD_KERNEL defined"
480 /*======================================================================*/
482 int flag_force_load = 0;
483 int flag_autoclean = 0;
484 int flag_verbose = 0;
488 /*======================================================================*/
490 struct i386_got_entry {
492 unsigned offset_done:1;
493 unsigned reloc_done:1;
497 struct obj_file root;
498 struct obj_section *got;
502 struct obj_symbol root;
503 struct i386_got_entry gotent;
508 struct external_module {
513 struct new_module_symbol *syms;
516 struct new_module_symbol *ksyms;
519 struct external_module *ext_modules;
521 int n_ext_modules_used;
525 /* Some firendly syscalls to cheer everyone's day... */
526 #define __NR_new_sys_init_module __NR_init_module
527 _syscall2(int, new_sys_init_module, const char *, name,
528 const struct new_module *, info)
529 #define __NR_old_sys_init_module __NR_init_module
530 _syscall5(int, old_sys_init_module, const char *, name, char *, code,
531 unsigned, codesize, struct old_mod_routines *, routines,
532 struct old_symbol_table *, symtab)
533 _syscall5(int, query_module, const char *, name, int, which,
534 void *, buf, size_t, bufsize, size_t*, ret);
536 _syscall1(int, delete_module, const char *, name)
538 extern int delete_module(const char *);
541 #if defined(__i386__) || defined(__m68k__) || defined(__arm__)
542 /* Jump through hoops to fixup error return codes */
543 #define __NR__create_module __NR_create_module
544 static inline _syscall2(long, _create_module, const char *, name, size_t,
546 unsigned long create_module(const char *name, size_t size)
548 long ret = _create_module(name, size);
550 if (ret == -1 && errno > 125) {
557 _syscall2(unsigned long, create_module, const char *, name, size_t, size)
559 static char m_filename[BUFSIZ + 1] = "\0";
560 static char m_fullName[BUFSIZ + 1] = "\0";
561 static const char insmod_usage[] =
562 "insmod [OPTION]... MODULE [symbol=value]...\n"
563 #ifndef BB_FEATURE_TRIVIAL_HELP
564 "\nLoads the specified kernel modules into the kernel.\n\n"
566 "\t-f\tForce module to load into the wrong kernel version.\n"
567 "\t-k\tMake module autoclean-able.\n"
568 "\t-v\tverbose output\n" "\t-x\tdo not export externs\n"
572 /*======================================================================*/
574 void *xrealloc(void *old, size_t size)
576 void *ptr = realloc(old, size);
578 perror("Out of memory");
585 static int findNamedModule(const char *fileName, struct stat *statbuf,
588 char *fullName = (char *) userDate;
591 if (fullName[0] == '\0')
594 char *tmp = strrchr(fileName, '/');
597 tmp = (char *) fileName;
600 if (check_wildcard_match(tmp, fullName) == TRUE) {
601 /* Stop searching if we find a match */
602 memcpy(m_filename, fileName, strlen(fileName));
610 /*======================================================================*/
612 struct obj_file *arch_new_file(void)
615 f = xmalloc(sizeof(*f));
620 struct obj_section *arch_new_section(void)
622 return xmalloc(sizeof(struct obj_section));
625 struct obj_symbol *arch_new_symbol(void)
627 struct i386_symbol *sym;
628 sym = xmalloc(sizeof(*sym));
629 memset(&sym->gotent, 0, sizeof(sym->gotent));
633 arch_apply_relocation(struct obj_file *f,
634 struct obj_section *targsec,
635 struct obj_section *symsec,
636 struct obj_symbol *sym,
637 Elf32_Rel * rel, Elf32_Addr v)
639 struct i386_file *ifile = (struct i386_file *) f;
640 struct i386_symbol *isym = (struct i386_symbol *) sym;
642 Elf32_Addr *loc = (Elf32_Addr *) (targsec->contents + rel->r_offset);
643 Elf32_Addr dot = targsec->header.sh_addr + rel->r_offset;
644 Elf32_Addr got = ifile->got ? ifile->got->header.sh_addr : 0;
646 enum obj_reloc ret = obj_reloc_ok;
648 switch (ELF32_R_TYPE(rel->r_info)) {
676 assert(isym != NULL);
677 if (!isym->gotent.reloc_done) {
678 isym->gotent.reloc_done = 1;
679 *(Elf32_Addr *) (ifile->got->contents + isym->gotent.offset) =
682 *loc += isym->gotent.offset;
691 ret = obj_reloc_unhandled;
698 int arch_create_got(struct obj_file *f)
700 struct i386_file *ifile = (struct i386_file *) f;
701 int i, n, offset = 0, gotneeded = 0;
703 n = ifile->root.header.e_shnum;
704 for (i = 0; i < n; ++i) {
705 struct obj_section *relsec, *symsec, *strsec;
706 Elf32_Rel *rel, *relend;
710 relsec = ifile->root.sections[i];
711 if (relsec->header.sh_type != SHT_REL)
714 symsec = ifile->root.sections[relsec->header.sh_link];
715 strsec = ifile->root.sections[symsec->header.sh_link];
717 rel = (Elf32_Rel *) relsec->contents;
718 relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rel));
719 symtab = (Elf32_Sym *) symsec->contents;
720 strtab = (const char *) strsec->contents;
722 for (; rel < relend; ++rel) {
724 struct i386_symbol *intsym;
727 switch (ELF32_R_TYPE(rel->r_info)) {
738 extsym = &symtab[ELF32_R_SYM(rel->r_info)];
740 name = strtab + extsym->st_name;
742 name = f->sections[extsym->st_shndx]->name;
744 (struct i386_symbol *) obj_find_symbol(&ifile->root, name);
746 if (!intsym->gotent.offset_done) {
747 intsym->gotent.offset_done = 1;
748 intsym->gotent.offset = offset;
754 if (offset > 0 || gotneeded)
756 obj_create_alloced_section(&ifile->root, ".got", 4, offset);
761 int arch_init_module(struct obj_file *f, struct new_module *mod)
767 /*======================================================================*/
769 /* Standard ELF hash function. */
770 inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
779 if ((g = (h & 0xf0000000)) != 0) {
788 unsigned long obj_elf_hash(const char *name)
790 return obj_elf_hash_n(name, strlen(name));
793 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
794 /* Get the kernel version in the canonical integer form. */
796 static int get_kernel_version(char str[STRVERSIONLEN])
798 struct utsname uts_info;
802 if (uname(&uts_info) < 0)
804 strncpy(str, uts_info.release, STRVERSIONLEN);
805 p = uts_info.release;
807 a = strtoul(p, &p, 10);
810 b = strtoul(p + 1, &p, 10);
813 c = strtoul(p + 1, &q, 10);
817 return a << 16 | b << 8 | c;
820 /* String comparison for non-co-versioned kernel and module. */
822 static int ncv_strcmp(const char *a, const char *b)
824 size_t alen = strlen(a), blen = strlen(b);
826 if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
827 return strncmp(a, b, alen);
828 else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
829 return strncmp(a, b, blen);
834 /* String hashing for non-co-versioned kernel and module. Here
835 we are simply forced to drop the crc from the hash. */
837 static unsigned long ncv_symbol_hash(const char *str)
839 size_t len = strlen(str);
840 if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
842 return obj_elf_hash_n(str, len);
846 obj_set_symbol_compare(struct obj_file *f,
847 int (*cmp) (const char *, const char *),
848 unsigned long (*hash) (const char *))
853 struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
856 f->symbol_hash = hash;
858 memcpy(tmptab, f->symtab, sizeof(tmptab));
859 memset(f->symtab, 0, sizeof(f->symtab));
861 for (i = 0; i < HASH_BUCKETS; ++i)
862 for (sym = tmptab[i]; sym; sym = next) {
863 unsigned long h = hash(sym->name) % HASH_BUCKETS;
865 sym->next = f->symtab[h];
871 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
874 struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
875 unsigned long symidx, int info,
876 int secidx, ElfW(Addr) value,
879 struct obj_symbol *sym;
880 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
881 int n_type = ELFW(ST_TYPE) (info);
882 int n_binding = ELFW(ST_BIND) (info);
884 for (sym = f->symtab[hash]; sym; sym = sym->next)
885 if (f->symbol_cmp(sym->name, name) == 0) {
886 int o_secidx = sym->secidx;
887 int o_info = sym->info;
888 int o_type = ELFW(ST_TYPE) (o_info);
889 int o_binding = ELFW(ST_BIND) (o_info);
891 /* A redefinition! Is it legal? */
893 if (secidx == SHN_UNDEF)
895 else if (o_secidx == SHN_UNDEF)
897 else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
898 /* Cope with local and global symbols of the same name
899 in the same object file, as might have been created
900 by ld -r. The only reason locals are now seen at this
901 level at all is so that we can do semi-sensible things
904 struct obj_symbol *nsym, **p;
906 nsym = arch_new_symbol();
907 nsym->next = sym->next;
910 /* Excise the old (local) symbol from the hash chain. */
911 for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
915 } else if (n_binding == STB_LOCAL) {
916 /* Another symbol of the same name has already been defined.
917 Just add this to the local table. */
918 sym = arch_new_symbol();
921 f->local_symtab[symidx] = sym;
923 } else if (n_binding == STB_WEAK)
925 else if (o_binding == STB_WEAK)
927 /* Don't unify COMMON symbols with object types the programmer
929 else if (secidx == SHN_COMMON
930 && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
932 else if (o_secidx == SHN_COMMON
933 && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
936 /* Don't report an error if the symbol is coming from
937 the kernel or some external module. */
938 if (secidx <= SHN_HIRESERVE)
939 fprintf(stderr, "%s multiply defined\n", name);
944 /* Completely new symbol. */
945 sym = arch_new_symbol();
946 sym->next = f->symtab[hash];
947 f->symtab[hash] = sym;
950 if (ELFW(ST_BIND) (info) == STB_LOCAL)
951 f->local_symtab[symidx] = sym;
957 sym->secidx = secidx;
963 struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
965 struct obj_symbol *sym;
966 unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
968 for (sym = f->symtab[hash]; sym; sym = sym->next)
969 if (f->symbol_cmp(sym->name, name) == 0)
976 obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
979 if (sym->secidx >= SHN_LORESERVE)
982 return sym->value + f->sections[sym->secidx]->header.sh_addr;
984 /* As a special case, a NULL sym has value zero. */
989 struct obj_section *obj_find_section(struct obj_file *f, const char *name)
991 int i, n = f->header.e_shnum;
993 for (i = 0; i < n; ++i)
994 if (strcmp(f->sections[i]->name, name) == 0)
995 return f->sections[i];
1000 static int obj_load_order_prio(struct obj_section *a)
1002 unsigned long af, ac;
1004 af = a->header.sh_flags;
1007 if (a->name[0] != '.' || strlen(a->name) != 10 ||
1008 strcmp(a->name + 5, ".init"))
1012 if (!(af & SHF_WRITE))
1014 if (af & SHF_EXECINSTR)
1016 if (a->header.sh_type != SHT_NOBITS)
1023 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
1025 struct obj_section **p;
1026 int prio = obj_load_order_prio(sec);
1027 for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
1028 if (obj_load_order_prio(*p) < prio)
1030 sec->load_next = *p;
1034 struct obj_section *obj_create_alloced_section(struct obj_file *f,
1036 unsigned long align,
1039 int newidx = f->header.e_shnum++;
1040 struct obj_section *sec;
1042 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1043 f->sections[newidx] = sec = arch_new_section();
1045 memset(sec, 0, sizeof(*sec));
1046 sec->header.sh_type = SHT_PROGBITS;
1047 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1048 sec->header.sh_size = size;
1049 sec->header.sh_addralign = align;
1053 sec->contents = xmalloc(size);
1055 obj_insert_section_load_order(f, sec);
1060 struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
1062 unsigned long align,
1065 int newidx = f->header.e_shnum++;
1066 struct obj_section *sec;
1068 f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
1069 f->sections[newidx] = sec = arch_new_section();
1071 memset(sec, 0, sizeof(*sec));
1072 sec->header.sh_type = SHT_PROGBITS;
1073 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
1074 sec->header.sh_size = size;
1075 sec->header.sh_addralign = align;
1079 sec->contents = xmalloc(size);
1081 sec->load_next = f->load_order;
1082 f->load_order = sec;
1083 if (f->load_order_search_start == &f->load_order)
1084 f->load_order_search_start = &sec->load_next;
1089 void *obj_extend_section(struct obj_section *sec, unsigned long more)
1091 unsigned long oldsize = sec->header.sh_size;
1092 sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
1093 return sec->contents + oldsize;
1098 /* Conditionally add the symbols from the given symbol set to the
1104 int idx, struct new_module_symbol *syms, size_t nsyms)
1106 struct new_module_symbol *s;
1110 for (i = 0, s = syms; i < nsyms; ++i, ++s) {
1112 /* Only add symbols that are already marked external. If we
1113 override locals we may cause problems for argument initialization.
1114 We will also create a false dependency on the module. */
1115 struct obj_symbol *sym;
1117 sym = obj_find_symbol(f, (char *) s->name);
1118 if (sym && !ELFW(ST_BIND) (sym->info) == STB_LOCAL) {
1119 sym = obj_add_symbol(f, (char *) s->name, -1,
1120 ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
1122 /* Did our symbol just get installed? If so, mark the
1123 module as "used". */
1124 if (sym->secidx == idx)
1132 static void add_kernel_symbols(struct obj_file *f)
1134 struct external_module *m;
1135 size_t i, nused = 0;
1137 /* Add module symbols first. */
1139 for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
1141 && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
1142 m->nsyms)) m->used = 1, ++nused;
1144 n_ext_modules_used = nused;
1146 /* And finally the symbols from the kernel proper. */
1149 add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
1152 static char *get_modinfo_value(struct obj_file *f, const char *key)
1154 struct obj_section *sec;
1155 char *p, *v, *n, *ep;
1156 size_t klen = strlen(key);
1158 sec = obj_find_section(f, ".modinfo");
1162 ep = p + sec->header.sh_size;
1165 n = strchr(p, '\0');
1167 if (v - p == klen && strncmp(p, key, klen) == 0)
1170 if (n - p == klen && strcmp(p, key) == 0)
1180 /*======================================================================*/
1181 /* Functions relating to module loading in pre 2.1 kernels. */
1184 old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1188 struct obj_symbol *sym;
1192 if ((q = strchr(p, '=')) == NULL)
1196 sym = obj_find_symbol(f, p);
1198 /* Also check that the parameter was not resolved from the kernel. */
1199 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1200 fprintf(stderr, "symbol for parameter %s not found\n", p);
1204 loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
1206 /* Do C quoting if we begin with a ". */
1210 str = alloca(strlen(q));
1211 for (r = str, q++; *q != '"'; ++q, ++r) {
1214 "improperly terminated string argument for %s\n",
1217 } else if (*q == '\\')
1251 if (q[1] >= '0' && q[1] <= '7') {
1252 c = (c * 8) + *++q - '0';
1253 if (q[1] >= '0' && q[1] <= '7')
1254 c = (c * 8) + *++q - '0';
1267 obj_string_patch(f, sym->secidx, sym->value, str);
1268 } else if (*q >= '0' && *q <= '9') {
1270 *loc++ = strtoul(q, &q, 0);
1271 while (*q++ == ',');
1273 char *contents = f->sections[sym->secidx]->contents;
1274 char *loc = contents + sym->value;
1275 char *r; /* To search for commas */
1277 /* Break the string with comas */
1278 while ((r = strchr(q, ',')) != (char *) NULL) {
1280 obj_string_patch(f, sym->secidx, loc - contents, q);
1281 loc += sizeof(char *);
1286 obj_string_patch(f, sym->secidx, loc - contents, q);
1295 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1296 static int old_is_module_checksummed(struct obj_file *f)
1298 return obj_find_symbol(f, "Using_Versions") != NULL;
1300 /* Get the module's kernel version in the canonical integer form. */
1303 old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1305 struct obj_symbol *sym;
1309 sym = obj_find_symbol(f, "kernel_version");
1313 p = f->sections[sym->secidx]->contents + sym->value;
1314 strncpy(str, p, STRVERSIONLEN);
1316 a = strtoul(p, &p, 10);
1319 b = strtoul(p + 1, &p, 10);
1322 c = strtoul(p + 1, &q, 10);
1326 return a << 16 | b << 8 | c;
1329 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1331 #ifdef BB_FEATURE_INSMOD_OLD_KERNEL
1333 /* Fetch all the symbols and divvy them up as appropriate for the modules. */
1335 static int old_get_kernel_symbols(void)
1337 struct old_kernel_sym *ks, *k;
1338 struct new_module_symbol *s;
1339 struct external_module *mod;
1340 int nks, nms, nmod, i;
1342 nks = get_kernel_syms(NULL);
1344 perror("get_kernel_syms: %m");
1348 ks = k = xmalloc(nks * sizeof(*ks));
1350 if (get_kernel_syms(ks) != nks) {
1351 perror("inconsistency with get_kernel_syms -- is someone else "
1352 "playing with modules?");
1357 /* Collect the module information. */
1362 while (k->name[0] == '#' && k->name[1]) {
1363 struct old_kernel_sym *k2;
1364 struct new_module_symbol *s;
1366 /* Find out how many symbols this module has. */
1367 for (k2 = k + 1; k2->name[0] != '#'; ++k2)
1371 mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
1372 mod[nmod].name = k->name + 1;
1373 mod[nmod].addr = k->value;
1375 mod[nmod].nsyms = nms;
1376 mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1378 for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
1379 s->name = (unsigned long) k->name;
1380 s->value = k->value;
1387 n_ext_modules = nmod + 1;
1389 /* Now collect the symbols for the kernel proper. */
1391 if (k->name[0] == '#')
1394 nksyms = nms = nks - (k - ks);
1395 ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
1397 for (i = 0; i < nms; ++i, ++s, ++k) {
1398 s->name = (unsigned long) k->name;
1399 s->value = k->value;
1405 /* Return the kernel symbol checksum version, or zero if not used. */
1407 static int old_is_kernel_checksummed(void)
1409 /* Using_Versions is the first symbol. */
1411 && strcmp((char *) ksyms[0].name,
1412 "Using_Versions") == 0) return ksyms[0].value;
1418 static int old_create_mod_use_count(struct obj_file *f)
1420 struct obj_section *sec;
1422 sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
1425 obj_add_symbol(f, "mod_use_count_", -1,
1426 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1433 old_init_module(const char *m_name, struct obj_file *f,
1434 unsigned long m_size)
1437 struct old_mod_routines routines;
1438 struct old_symbol_table *symtab;
1441 /* Create the symbol table */
1443 int nsyms = 0, strsize = 0, total;
1445 /* Size things first... */
1448 for (i = 0; i < HASH_BUCKETS; ++i) {
1449 struct obj_symbol *sym;
1450 for (sym = f->symtab[i]; sym; sym = sym->next)
1451 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1452 && sym->secidx <= SHN_HIRESERVE)
1454 sym->ksymidx = nsyms++;
1455 strsize += strlen(sym->name) + 1;
1460 total = (sizeof(struct old_symbol_table)
1461 + nsyms * sizeof(struct old_module_symbol)
1462 + n_ext_modules_used * sizeof(struct old_module_ref)
1464 symtab = xmalloc(total);
1465 symtab->size = total;
1466 symtab->n_symbols = nsyms;
1467 symtab->n_refs = n_ext_modules_used;
1469 if (flag_export && nsyms) {
1470 struct old_module_symbol *ksym;
1474 ksym = symtab->symbol;
1475 str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
1476 + n_ext_modules_used * sizeof(struct old_module_ref));
1478 for (i = 0; i < HASH_BUCKETS; ++i) {
1479 struct obj_symbol *sym;
1480 for (sym = f->symtab[i]; sym; sym = sym->next)
1481 if (sym->ksymidx >= 0) {
1482 ksym->addr = obj_symbol_final_value(f, sym);
1484 (unsigned long) str - (unsigned long) symtab;
1486 str = stpcpy(str, sym->name) + 1;
1492 if (n_ext_modules_used) {
1493 struct old_module_ref *ref;
1496 ref = (struct old_module_ref *)
1497 ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
1499 for (i = 0; i < n_ext_modules; ++i)
1500 if (ext_modules[i].used)
1501 ref++->module = ext_modules[i].addr;
1505 /* Fill in routines. */
1508 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
1510 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
1512 /* Whew! All of the initialization is complete. Collect the final
1513 module image and give it to the kernel. */
1515 image = xmalloc(m_size);
1516 obj_create_image(f, image);
1518 /* image holds the complete relocated module, accounting correctly for
1519 mod_use_count. However the old module kernel support assume that
1520 it is receiving something which does not contain mod_use_count. */
1521 ret = old_sys_init_module(m_name, image + sizeof(long),
1522 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
1523 : 0), &routines, symtab);
1525 perror("init_module: %m");
1535 #define old_create_mod_use_count(x) TRUE
1536 #define old_init_module(x, y, z) TRUE
1538 #endif /* BB_FEATURE_INSMOD_OLD_KERNEL */
1542 /*======================================================================*/
1543 /* Functions relating to module loading after 2.1.18. */
1546 new_process_module_arguments(struct obj_file *f, int argc, char **argv)
1550 struct obj_symbol *sym;
1551 char *contents, *loc;
1555 if ((q = strchr(p, '=')) == NULL)
1558 key = alloca(q - p + 6);
1559 memcpy(key, "parm_", 5);
1560 memcpy(key + 5, p, q - p);
1563 p = get_modinfo_value(f, key);
1566 fprintf(stderr, "invalid parameter %s\n", key);
1570 sym = obj_find_symbol(f, key);
1572 /* Also check that the parameter was not resolved from the kernel. */
1573 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1574 fprintf(stderr, "symbol for parameter %s not found\n", key);
1579 min = strtoul(p, &p, 10);
1581 max = strtoul(p + 1, &p, 10);
1587 contents = f->sections[sym->secidx]->contents;
1588 loc = contents + sym->value;
1592 if ((*p == 's') || (*p == 'c')) {
1595 /* Do C quoting if we begin with a ", else slurp the lot. */
1599 str = alloca(strlen(q));
1600 for (r = str, q++; *q != '"'; ++q, ++r) {
1603 "improperly terminated string argument for %s\n",
1606 } else if (*q == '\\')
1640 if (q[1] >= '0' && q[1] <= '7') {
1641 c = (c * 8) + *++q - '0';
1642 if (q[1] >= '0' && q[1] <= '7')
1643 c = (c * 8) + *++q - '0';
1660 /* In this case, the string is not quoted. We will break
1661 it using the coma (like for ints). If the user wants to
1662 include comas in a string, he just has to quote it */
1664 /* Search the next coma */
1668 if (r != (char *) NULL) {
1669 /* Recopy the current field */
1670 str = alloca(r - q + 1);
1671 memcpy(str, q, r - q);
1673 /* I don't know if it is usefull, as the previous case
1674 doesn't null terminate the string ??? */
1677 /* Keep next fields */
1688 obj_string_patch(f, sym->secidx, loc - contents, str);
1689 loc += tgt_sizeof_char_p;
1691 /* Array of chars (in fact, matrix !) */
1692 long charssize; /* size of each member */
1694 /* Get the size of each member */
1695 /* Probably we should do that outside the loop ? */
1696 if (!isdigit(*(p + 1))) {
1698 "parameter type 'c' for %s must be followed by"
1699 " the maximum size\n", key);
1702 charssize = strtoul(p + 1, (char **) NULL, 10);
1705 if (strlen(str) >= charssize) {
1707 "string too long for %s (max %ld)\n", key,
1712 /* Copy to location */
1713 strcpy((char *) loc, str);
1717 long v = strtoul(q, &q, 0);
1724 loc += tgt_sizeof_short;
1728 loc += tgt_sizeof_int;
1732 loc += tgt_sizeof_long;
1736 fprintf(stderr, "unknown parameter type '%c' for %s\n",
1752 goto retry_end_of_value;
1756 fprintf(stderr, "too many values for %s (max %d)\n",
1764 fprintf(stderr, "invalid argument syntax for %s\n", key);
1771 fprintf(stderr, "too few values for %s (min %d)\n", key, min);
1781 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
1782 static int new_is_module_checksummed(struct obj_file *f)
1784 const char *p = get_modinfo_value(f, "using_checksums");
1791 /* Get the module's kernel version in the canonical integer form. */
1794 new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
1799 p = get_modinfo_value(f, "kernel_version");
1802 strncpy(str, p, STRVERSIONLEN);
1804 a = strtoul(p, &p, 10);
1807 b = strtoul(p + 1, &p, 10);
1810 c = strtoul(p + 1, &q, 10);
1814 return a << 16 | b << 8 | c;
1817 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
1820 #ifdef BB_FEATURE_INSMOD_NEW_KERNEL
1822 /* Fetch the loaded modules, and all currently exported symbols. */
1824 static int new_get_kernel_symbols(void)
1826 char *module_names, *mn;
1827 struct external_module *modules, *m;
1828 struct new_module_symbol *syms, *s;
1829 size_t ret, bufsize, nmod, nsyms, i, j;
1831 /* Collect the loaded modules. */
1833 module_names = xmalloc(bufsize = 256);
1835 if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
1836 if (errno == ENOSPC) {
1837 module_names = xrealloc(module_names, bufsize = ret);
1838 goto retry_modules_load;
1840 perror("QM_MODULES: %m\n");
1844 n_ext_modules = nmod = ret;
1845 ext_modules = modules = xmalloc(nmod * sizeof(*modules));
1846 memset(modules, 0, nmod * sizeof(*modules));
1848 /* Collect the modules' symbols. */
1850 for (i = 0, mn = module_names, m = modules;
1851 i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
1852 struct new_module_info info;
1854 if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
1855 if (errno == ENOENT) {
1856 /* The module was removed out from underneath us. */
1859 perror("query_module: QM_INFO: %m");
1863 syms = xmalloc(bufsize = 1024);
1865 if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
1868 syms = xrealloc(syms, bufsize = ret);
1869 goto retry_mod_sym_load;
1871 /* The module was removed out from underneath us. */
1874 perror("query_module: QM_SYMBOLS: %m");
1881 m->addr = info.addr;
1885 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
1886 s->name += (unsigned long) syms;
1890 /* Collect the kernel's symbols. */
1892 syms = xmalloc(bufsize = 16 * 1024);
1893 retry_kern_sym_load:
1894 if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
1895 if (errno == ENOSPC) {
1896 syms = xrealloc(syms, bufsize = ret);
1897 goto retry_kern_sym_load;
1899 perror("kernel: QM_SYMBOLS: %m");
1902 nksyms = nsyms = ret;
1905 for (j = 0, s = syms; j < nsyms; ++j, ++s) {
1906 s->name += (unsigned long) syms;
1912 /* Return the kernel symbol checksum version, or zero if not used. */
1914 static int new_is_kernel_checksummed(void)
1916 struct new_module_symbol *s;
1919 /* Using_Versions is not the first symbol, but it should be in there. */
1921 for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
1922 if (strcmp((char *) s->name, "Using_Versions") == 0)
1929 static int new_create_this_module(struct obj_file *f, const char *m_name)
1931 struct obj_section *sec;
1933 sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
1934 sizeof(struct new_module));
1935 memset(sec->contents, 0, sizeof(struct new_module));
1937 obj_add_symbol(f, "__this_module", -1,
1938 ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
1939 sizeof(struct new_module));
1941 obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
1948 static int new_create_module_ksymtab(struct obj_file *f)
1950 struct obj_section *sec;
1953 /* We must always add the module references. */
1955 if (n_ext_modules_used) {
1956 struct new_module_ref *dep;
1957 struct obj_symbol *tm;
1959 sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
1960 (sizeof(struct new_module_ref)
1961 * n_ext_modules_used));
1965 tm = obj_find_symbol(f, "__this_module");
1966 dep = (struct new_module_ref *) sec->contents;
1967 for (i = 0; i < n_ext_modules; ++i)
1968 if (ext_modules[i].used) {
1969 dep->dep = ext_modules[i].addr;
1970 obj_symbol_patch(f, sec->idx,
1971 (char *) &dep->ref - sec->contents, tm);
1977 if (flag_export && !obj_find_section(f, "__ksymtab")) {
1982 obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
1985 /* We don't want to export symbols residing in sections that
1986 aren't loaded. There are a number of these created so that
1987 we make sure certain module options don't appear twice. */
1989 loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
1991 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
1993 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
1994 struct obj_symbol *sym;
1995 for (sym = f->symtab[i]; sym; sym = sym->next)
1996 if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
1997 && sym->secidx <= SHN_HIRESERVE
1998 && (sym->secidx >= SHN_LORESERVE
1999 || loaded[sym->secidx])) {
2000 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2002 obj_symbol_patch(f, sec->idx, ofs, sym);
2003 obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
2010 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
2018 new_init_module(const char *m_name, struct obj_file *f,
2019 unsigned long m_size)
2021 struct new_module *module;
2022 struct obj_section *sec;
2027 sec = obj_find_section(f, ".this");
2028 module = (struct new_module *) sec->contents;
2029 m_addr = sec->header.sh_addr;
2031 module->size_of_struct = sizeof(*module);
2032 module->size = m_size;
2033 module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
2035 sec = obj_find_section(f, "__ksymtab");
2036 if (sec && sec->header.sh_size) {
2037 module->syms = sec->header.sh_addr;
2038 module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
2041 if (n_ext_modules_used) {
2042 sec = obj_find_section(f, ".kmodtab");
2043 module->deps = sec->header.sh_addr;
2044 module->ndeps = n_ext_modules_used;
2048 obj_symbol_final_value(f, obj_find_symbol(f, "init_module"));
2050 obj_symbol_final_value(f, obj_find_symbol(f, "cleanup_module"));
2052 sec = obj_find_section(f, "__ex_table");
2054 module->ex_table_start = sec->header.sh_addr;
2055 module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
2058 sec = obj_find_section(f, ".text.init");
2060 module->runsize = sec->header.sh_addr - m_addr;
2062 sec = obj_find_section(f, ".data.init");
2064 if (!module->runsize ||
2065 module->runsize > sec->header.sh_addr - m_addr)
2066 module->runsize = sec->header.sh_addr - m_addr;
2069 if (!arch_init_module(f, module))
2072 /* Whew! All of the initialization is complete. Collect the final
2073 module image and give it to the kernel. */
2075 image = xmalloc(m_size);
2076 obj_create_image(f, image);
2078 ret = new_sys_init_module(m_name, (struct new_module *) image);
2080 perror("init_module: %m");
2089 #define new_init_module(x, y, z) TRUE
2090 #define new_create_this_module(x, y) 0
2091 #define new_create_module_ksymtab(x)
2093 #endif /* BB_FEATURE_INSMOD_OLD_KERNEL */
2096 /*======================================================================*/
2099 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2102 struct obj_string_patch *p;
2103 struct obj_section *strsec;
2104 size_t len = strlen(string) + 1;
2107 p = xmalloc(sizeof(*p));
2108 p->next = f->string_patches;
2109 p->reloc_secidx = secidx;
2110 p->reloc_offset = offset;
2111 f->string_patches = p;
2113 strsec = obj_find_section(f, ".kstrtab");
2114 if (strsec == NULL) {
2115 strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
2116 p->string_offset = 0;
2117 loc = strsec->contents;
2119 p->string_offset = strsec->header.sh_size;
2120 loc = obj_extend_section(strsec, len);
2122 memcpy(loc, string, len);
2128 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
2129 struct obj_symbol *sym)
2131 struct obj_symbol_patch *p;
2133 p = xmalloc(sizeof(*p));
2134 p->next = f->symbol_patches;
2135 p->reloc_secidx = secidx;
2136 p->reloc_offset = offset;
2138 f->symbol_patches = p;
2143 int obj_check_undefineds(struct obj_file *f)
2148 for (i = 0; i < HASH_BUCKETS; ++i) {
2149 struct obj_symbol *sym;
2150 for (sym = f->symtab[i]; sym; sym = sym->next)
2151 if (sym->secidx == SHN_UNDEF) {
2152 if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
2153 sym->secidx = SHN_ABS;
2156 fprintf(stderr, "unresolved symbol %s\n", sym->name);
2165 void obj_allocate_commons(struct obj_file *f)
2167 struct common_entry {
2168 struct common_entry *next;
2169 struct obj_symbol *sym;
2170 } *common_head = NULL;
2174 for (i = 0; i < HASH_BUCKETS; ++i) {
2175 struct obj_symbol *sym;
2176 for (sym = f->symtab[i]; sym; sym = sym->next)
2177 if (sym->secidx == SHN_COMMON) {
2178 /* Collect all COMMON symbols and sort them by size so as to
2179 minimize space wasted by alignment requirements. */
2181 struct common_entry **p, *n;
2182 for (p = &common_head; *p; p = &(*p)->next)
2183 if (sym->size <= (*p)->sym->size)
2186 n = alloca(sizeof(*n));
2194 for (i = 1; i < f->local_symtab_size; ++i) {
2195 struct obj_symbol *sym = f->local_symtab[i];
2196 if (sym && sym->secidx == SHN_COMMON) {
2197 struct common_entry **p, *n;
2198 for (p = &common_head; *p; p = &(*p)->next)
2199 if (sym == (*p)->sym)
2201 else if (sym->size < (*p)->sym->size) {
2202 n = alloca(sizeof(*n));
2212 /* Find the bss section. */
2213 for (i = 0; i < f->header.e_shnum; ++i)
2214 if (f->sections[i]->header.sh_type == SHT_NOBITS)
2217 /* If for some reason there hadn't been one, create one. */
2218 if (i == f->header.e_shnum) {
2219 struct obj_section *sec;
2221 f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
2222 f->sections[i] = sec = arch_new_section();
2223 f->header.e_shnum = i + 1;
2225 memset(sec, 0, sizeof(*sec));
2226 sec->header.sh_type = SHT_PROGBITS;
2227 sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
2232 /* Allocate the COMMONS. */
2234 ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
2235 ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
2236 struct common_entry *c;
2238 for (c = common_head; c; c = c->next) {
2239 ElfW(Addr) align = c->sym->value;
2241 if (align > max_align)
2243 if (bss_size & (align - 1))
2244 bss_size = (bss_size | (align - 1)) + 1;
2247 c->sym->value = bss_size;
2249 bss_size += c->sym->size;
2252 f->sections[i]->header.sh_size = bss_size;
2253 f->sections[i]->header.sh_addralign = max_align;
2257 /* For the sake of patch relocation and parameter initialization,
2258 allocate zeroed data for NOBITS sections now. Note that after
2259 this we cannot assume NOBITS are really empty. */
2260 for (i = 0; i < f->header.e_shnum; ++i) {
2261 struct obj_section *s = f->sections[i];
2262 if (s->header.sh_type == SHT_NOBITS) {
2263 s->contents = memset(xmalloc(s->header.sh_size),
2264 0, s->header.sh_size);
2265 s->header.sh_type = SHT_PROGBITS;
2270 unsigned long obj_load_size(struct obj_file *f)
2272 unsigned long dot = 0;
2273 struct obj_section *sec;
2275 /* Finalize the positions of the sections relative to one another. */
2277 for (sec = f->load_order; sec; sec = sec->load_next) {
2280 align = sec->header.sh_addralign;
2281 if (align && (dot & (align - 1)))
2282 dot = (dot | (align - 1)) + 1;
2284 sec->header.sh_addr = dot;
2285 dot += sec->header.sh_size;
2291 int obj_relocate(struct obj_file *f, ElfW(Addr) base)
2293 int i, n = f->header.e_shnum;
2296 /* Finalize the addresses of the sections. */
2299 for (i = 0; i < n; ++i)
2300 f->sections[i]->header.sh_addr += base;
2302 /* And iterate over all of the relocations. */
2304 for (i = 0; i < n; ++i) {
2305 struct obj_section *relsec, *symsec, *targsec, *strsec;
2306 ElfW(RelM) * rel, *relend;
2310 relsec = f->sections[i];
2311 if (relsec->header.sh_type != SHT_RELM)
2314 symsec = f->sections[relsec->header.sh_link];
2315 targsec = f->sections[relsec->header.sh_info];
2316 strsec = f->sections[symsec->header.sh_link];
2318 rel = (ElfW(RelM) *) relsec->contents;
2319 relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
2320 symtab = (ElfW(Sym) *) symsec->contents;
2321 strtab = (const char *) strsec->contents;
2323 for (; rel < relend; ++rel) {
2324 ElfW(Addr) value = 0;
2325 struct obj_symbol *intsym = NULL;
2326 unsigned long symndx;
2327 ElfW(Sym) * extsym = 0;
2330 /* Attempt to find a value to use for this relocation. */
2332 symndx = ELFW(R_SYM) (rel->r_info);
2334 /* Note we've already checked for undefined symbols. */
2336 extsym = &symtab[symndx];
2337 if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
2338 /* Local symbols we look up in the local table to be sure
2339 we get the one that is really intended. */
2340 intsym = f->local_symtab[symndx];
2342 /* Others we look up in the hash table. */
2344 if (extsym->st_name)
2345 name = strtab + extsym->st_name;
2347 name = f->sections[extsym->st_shndx]->name;
2348 intsym = obj_find_symbol(f, name);
2351 value = obj_symbol_final_value(f, intsym);
2352 intsym->referenced = 1;
2354 #if SHT_RELM == SHT_RELA
2355 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
2356 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
2357 if (!extsym || !extsym->st_name ||
2358 ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
2360 value += rel->r_addend;
2364 switch (arch_apply_relocation
2365 (f, targsec, symsec, intsym, rel, value)) {
2369 case obj_reloc_overflow:
2370 errmsg = "Relocation overflow";
2372 case obj_reloc_dangerous:
2373 errmsg = "Dangerous relocation";
2375 case obj_reloc_unhandled:
2376 errmsg = "Unhandled relocation";
2379 fprintf(stderr, "%s of type %ld for %s\n", errmsg,
2380 (long) ELFW(R_TYPE) (rel->r_info),
2381 strtab + extsym->st_name);
2383 fprintf(stderr, "%s of type %ld\n", errmsg,
2384 (long) ELFW(R_TYPE) (rel->r_info));
2392 /* Finally, take care of the patches. */
2394 if (f->string_patches) {
2395 struct obj_string_patch *p;
2396 struct obj_section *strsec;
2397 ElfW(Addr) strsec_base;
2398 strsec = obj_find_section(f, ".kstrtab");
2399 strsec_base = strsec->header.sh_addr;
2401 for (p = f->string_patches; p; p = p->next) {
2402 struct obj_section *targsec = f->sections[p->reloc_secidx];
2403 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2404 = strsec_base + p->string_offset;
2408 if (f->symbol_patches) {
2409 struct obj_symbol_patch *p;
2411 for (p = f->symbol_patches; p; p = p->next) {
2412 struct obj_section *targsec = f->sections[p->reloc_secidx];
2413 *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
2414 = obj_symbol_final_value(f, p->sym);
2421 int obj_create_image(struct obj_file *f, char *image)
2423 struct obj_section *sec;
2424 ElfW(Addr) base = f->baseaddr;
2426 for (sec = f->load_order; sec; sec = sec->load_next) {
2429 if (sec->header.sh_size == 0)
2432 secimg = image + (sec->header.sh_addr - base);
2434 /* Note that we allocated data for NOBITS sections earlier. */
2435 memcpy(secimg, sec->contents, sec->header.sh_size);
2441 /*======================================================================*/
2443 struct obj_file *obj_load(FILE * fp)
2446 ElfW(Shdr) * section_headers;
2450 /* Read the file header. */
2452 f = arch_new_file();
2453 memset(f, 0, sizeof(*f));
2454 f->symbol_cmp = strcmp;
2455 f->symbol_hash = obj_elf_hash;
2456 f->load_order_search_start = &f->load_order;
2458 fseek(fp, 0, SEEK_SET);
2459 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
2460 perror("error reading ELF header: %m");
2464 if (f->header.e_ident[EI_MAG0] != ELFMAG0
2465 || f->header.e_ident[EI_MAG1] != ELFMAG1
2466 || f->header.e_ident[EI_MAG2] != ELFMAG2
2467 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
2468 fprintf(stderr, "not an ELF file\n");
2471 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
2472 || f->header.e_ident[EI_DATA] != ELFDATAM
2473 || f->header.e_ident[EI_VERSION] != EV_CURRENT
2474 || !MATCH_MACHINE(f->header.e_machine)) {
2475 fprintf(stderr, "ELF file not for this architecture\n");
2478 if (f->header.e_type != ET_REL) {
2479 fprintf(stderr, "ELF file not a relocatable object\n");
2483 /* Read the section headers. */
2485 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
2486 fprintf(stderr, "section header size mismatch: %lu != %lu\n",
2487 (unsigned long) f->header.e_shentsize,
2488 (unsigned long) sizeof(ElfW(Shdr)));
2492 shnum = f->header.e_shnum;
2493 f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
2494 memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
2496 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
2497 fseek(fp, f->header.e_shoff, SEEK_SET);
2498 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
2499 perror("error reading ELF section headers: %m");
2503 /* Read the section data. */
2505 for (i = 0; i < shnum; ++i) {
2506 struct obj_section *sec;
2508 f->sections[i] = sec = arch_new_section();
2509 memset(sec, 0, sizeof(*sec));
2511 sec->header = section_headers[i];
2514 switch (sec->header.sh_type) {
2525 if (sec->header.sh_size > 0) {
2526 sec->contents = xmalloc(sec->header.sh_size);
2527 fseek(fp, sec->header.sh_offset, SEEK_SET);
2528 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
2530 "error reading ELF section data: %m\n");
2534 sec->contents = NULL;
2538 #if SHT_RELM == SHT_REL
2541 "RELA relocations not supported on this architecture\n");
2546 "REL relocations not supported on this architecture\n");
2551 if (sec->header.sh_type >= SHT_LOPROC) {
2552 /* Assume processor specific section types are debug
2553 info and can safely be ignored. If this is ever not
2554 the case (Hello MIPS?), don't put ifdefs here but
2555 create an arch_load_proc_section(). */
2559 fprintf(stderr, "can't handle sections of type %ld\n",
2560 (long) sec->header.sh_type);
2565 /* Do what sort of interpretation as needed by each section. */
2567 shstrtab = f->sections[f->header.e_shstrndx]->contents;
2569 for (i = 0; i < shnum; ++i) {
2570 struct obj_section *sec = f->sections[i];
2571 sec->name = shstrtab + sec->header.sh_name;
2574 for (i = 0; i < shnum; ++i) {
2575 struct obj_section *sec = f->sections[i];
2577 if (sec->header.sh_flags & SHF_ALLOC)
2578 obj_insert_section_load_order(f, sec);
2580 switch (sec->header.sh_type) {
2583 unsigned long nsym, j;
2587 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
2588 fprintf(stderr, "symbol size mismatch: %lu != %lu\n",
2589 (unsigned long) sec->header.sh_entsize,
2590 (unsigned long) sizeof(ElfW(Sym)));
2594 nsym = sec->header.sh_size / sizeof(ElfW(Sym));
2595 strtab = f->sections[sec->header.sh_link]->contents;
2596 sym = (ElfW(Sym) *) sec->contents;
2598 /* Allocate space for a table of local symbols. */
2599 j = f->local_symtab_size = sec->header.sh_info;
2600 f->local_symtab = xmalloc(j *=
2601 sizeof(struct obj_symbol *));
2602 memset(f->local_symtab, 0, j);
2604 /* Insert all symbols into the hash table. */
2605 for (j = 1, ++sym; j < nsym; ++j, ++sym) {
2608 name = strtab + sym->st_name;
2610 name = f->sections[sym->st_shndx]->name;
2612 obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
2613 sym->st_value, sym->st_size);
2619 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
2621 "relocation entry size mismatch: %lu != %lu\n",
2622 (unsigned long) sec->header.sh_entsize,
2623 (unsigned long) sizeof(ElfW(RelM)));
2633 static void hide_special_symbols(struct obj_file *f)
2635 static const char *const specials[] = {
2642 struct obj_symbol *sym;
2643 const char *const *p;
2645 for (p = specials; *p; ++p)
2646 if ((sym = obj_find_symbol(f, *p)) != NULL)
2648 ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
2653 extern int insmod_main( int argc, char **argv)
2659 unsigned long m_size;
2663 char m_name[BUFSIZ + 1] = "\0";
2664 int exit_status = FALSE;
2666 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2668 char k_strversion[STRVERSIONLEN];
2669 char m_strversion[STRVERSIONLEN];
2676 usage(insmod_usage);
2679 /* Parse any options */
2680 while (--argc > 0 && **(++argv) == '-') {
2681 while (*(++(*argv))) {
2683 case 'f': /* force loading */
2684 flag_force_load = 1;
2686 case 'k': /* module loaded by kerneld, auto-cleanable */
2689 case 'v': /* verbose output */
2692 case 'x': /* do not export externs */
2696 usage(insmod_usage);
2702 usage(insmod_usage);
2704 /* Grab the module name */
2705 if ((tmp = strrchr(*argv, '/')) != NULL) {
2712 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
2714 memcpy(m_name, tmp, len);
2715 strcpy(m_fullName, m_name);
2716 strcat(m_fullName, ".o");
2718 /* Get a filedesc for the module */
2719 if ((fp = fopen(*argv, "r")) == NULL) {
2720 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
2721 if (recursiveAction(_PATH_MODULES, TRUE, FALSE, FALSE,
2722 findNamedModule, 0, m_fullName) == TRUE)
2724 if (m_filename[0] == '\0'
2725 || ((fp = fopen(m_filename, "r")) == NULL))
2727 perror("No module by that name found in " _PATH_MODULES
2733 memcpy(m_filename, *argv, strlen(*argv));
2736 if ((f = obj_load(fp)) == NULL) {
2737 perror("Could not load the module\n");
2741 if (get_modinfo_value(f, "kernel_version") == NULL)
2746 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2747 /* Version correspondence? */
2749 k_version = get_kernel_version(k_strversion);
2750 if (m_has_modinfo) {
2751 m_version = new_get_module_version(f, m_strversion);
2753 m_version = old_get_module_version(f, m_strversion);
2754 if (m_version == -1) {
2756 "couldn't find the kernel version the module was compiled for\n");
2761 if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
2762 if (flag_force_load) {
2763 fprintf(stderr, "Warning: kernel-module version mismatch\n"
2764 "\t%s was compiled for kernel version %s\n"
2765 "\twhile this kernel is version %s\n",
2766 m_filename, m_strversion, k_strversion);
2768 fprintf(stderr, "kernel-module version mismatch\n"
2769 "\t%s was compiled for kernel version %s\n"
2770 "\twhile this kernel is version %s.\n",
2771 m_filename, m_strversion, k_strversion);
2776 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2778 k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
2780 if (k_new_syscalls) {
2781 #ifdef BB_FEATURE_INSMOD_NEW_KERNEL
2782 if (!new_get_kernel_symbols())
2784 k_crcs = new_is_kernel_checksummed();
2786 fprintf(stderr, "Not configured to support new kernels\n");
2790 #ifdef BB_FEATURE_INSMOD_OLD_KERNEL
2791 if (!old_get_kernel_symbols())
2793 k_crcs = old_is_kernel_checksummed();
2795 fprintf(stderr, "Not configured to support old kernels\n");
2800 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
2802 m_crcs = new_is_module_checksummed(f);
2804 m_crcs = old_is_module_checksummed(f);
2806 if (m_crcs != k_crcs)
2807 obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
2808 #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */
2810 /* Let the module know about the kernel symbols. */
2811 add_kernel_symbols(f);
2813 /* Allocate common symbols, symbol tables, and string tables. */
2816 ? !new_create_this_module(f, m_name)
2817 : !old_create_mod_use_count(f))
2822 if (!obj_check_undefineds(f)) {
2825 obj_allocate_commons(f);
2827 if (optind < argc) {
2829 ? !new_process_module_arguments(f, argc - optind, argv + optind)
2830 : !old_process_module_arguments(f, argc - optind, argv + optind))
2837 hide_special_symbols(f);
2840 new_create_module_ksymtab(f);
2842 /* Find current size of the module */
2843 m_size = obj_load_size(f);
2847 m_addr = create_module(m_name, m_size);
2852 fprintf(stderr, "A module named %s already exists\n", m_name);
2856 "Can't allocate kernel memory for module; needed %lu bytes\n",
2860 perror("create_module: %m");
2864 if (!obj_relocate(f, m_addr)) {
2865 delete_module(m_name);
2870 ? !new_init_module(m_name, f, m_size)
2871 : !old_init_module(m_name, f, m_size))
2873 delete_module(m_name);
2881 return(exit_status);