X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=modutils%2Finsmod.c;h=fc49dcd3444e11ad4a370ab75306f187efc965b1;hb=39b727f498aad830780169228995e6e8f34953de;hp=f6575c97fe77f36c04c71a4e405d400d17401c27;hpb=8a24a6783af7a0d54b1f2ebcda5b07757bd19c99;p=oweals%2Fbusybox.git diff --git a/modutils/insmod.c b/modutils/insmod.c index f6575c97f..fc49dcd34 100644 --- a/modutils/insmod.c +++ b/modutils/insmod.c @@ -6,6 +6,14 @@ * Written by Erik Andersen * and Ron Alder * + * Modified by Bryan Rittmeyer to support SH4 + * and (theoretically) SH3. I have only tested SH4 in little endian mode. + * + * Modified by Alcove, Julien Gaulmin and + * Nicolas Ferre to support ARM7TDMI. Only + * very minor changes required to also work with StrongArm and presumably + * all ARM based systems. + * * Based almost entirely on the Linux modutils-2.3.11 implementation. * Copyright 1996, 1997 Linux International. * New implementation contributed by Richard Henderson @@ -29,7 +37,7 @@ * */ -#include "internal.h" +#include "busybox.h" #include #include #include @@ -38,9 +46,8 @@ #include #include #include +#include #include -#include -#include //---------------------------------------------------------------------------- //--------modutils module.h, lines 45-242 @@ -69,9 +76,9 @@ #ifndef MODUTILS_MODULE_H -#define MODUTILS_MODULE_H 1 +static const int MODUTILS_MODULE_H = 1; -#ident "$Id: insmod.c,v 1.10 2000/06/22 18:19:31 andersen Exp $" +#ident "$Id: insmod.c,v 1.39 2001/01/24 23:59:50 andersen Exp $" /* This file contains the structures used by the 2.0 and 2.1 kernels. We do not use the kernel headers directly because we do not wish @@ -128,7 +135,7 @@ struct old_module }; /* Sent to init_module(2) or'ed into the code size parameter. */ -#define OLD_MOD_AUTOCLEAN 0x40000000 /* big enough, but no sign problems... */ +static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */ int get_kernel_syms(struct old_kernel_sym *); int old_sys_init_module(const char *name, char *code, unsigned codesize, @@ -151,9 +158,9 @@ int old_sys_init_module(const char *name, char *code, unsigned codesize, #undef tgt_sizeof_char_p #undef tgt_sizeof_void_p #undef tgt_long -#define tgt_sizeof_long 8 -#define tgt_sizeof_char_p 8 -#define tgt_sizeof_void_p 8 +static const int tgt_sizeof_long = 8; +static const int tgt_sizeof_char_p = 8; +static const int tgt_sizeof_void_p = 8; #define tgt_long long long #endif @@ -215,11 +222,11 @@ struct new_module_info }; /* Bits of module.flags. */ -#define NEW_MOD_RUNNING 1 -#define NEW_MOD_DELETED 2 -#define NEW_MOD_AUTOCLEAN 4 -#define NEW_MOD_VISITED 8 -#define NEW_MOD_USED_ONCE 16 +static const int NEW_MOD_RUNNING = 1; +static const int NEW_MOD_DELETED = 2; +static const int NEW_MOD_AUTOCLEAN = 4; +static const int NEW_MOD_VISITED = 8; +static const int NEW_MOD_USED_ONCE = 16; int new_sys_init_module(const char *name, const struct new_module *); int query_module(const char *name, int which, void *buf, size_t bufsize, @@ -227,11 +234,11 @@ int query_module(const char *name, int which, void *buf, size_t bufsize, /* Values for query_module's which. */ -#define QM_MODULES 1 -#define QM_DEPS 2 -#define QM_REFS 3 -#define QM_SYMBOLS 4 -#define QM_INFO 5 +static const int QM_MODULES = 1; +static const int QM_DEPS = 2; +static const int QM_REFS = 3; +static const int QM_SYMBOLS = 4; +static const int QM_INFO = 5; /*======================================================================*/ /* The system calls unchanged between 2.0 and 2.1. */ @@ -275,9 +282,9 @@ int delete_module(const char *); #ifndef MODUTILS_OBJ_H -#define MODUTILS_OBJ_H 1 +static const int MODUTILS_OBJ_H = 1; -#ident "$Id: insmod.c,v 1.10 2000/06/22 18:19:31 andersen Exp $" +#ident "$Id: insmod.c,v 1.39 2001/01/24 23:59:50 andersen Exp $" /* The relocatable object is manipulated using elfin types. */ @@ -287,14 +294,53 @@ int delete_module(const char *); /* Machine-specific elf macros for i386 et al. */ +/* the SH changes have only been tested on the SH4 in =little endian= mode */ +/* I'm not sure about big endian, so let's warn: */ + +#if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__) +#error insmod.c may require changes for use on big endian SH4/SH3 +#endif + +/* it may or may not work on the SH1/SH2... So let's error on those + also */ +#if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__)))) +#error insmod.c may require changes for non-SH3/SH4 use +#endif + #define ELFCLASSM ELFCLASS32 #define ELFDATAM ELFDATA2LSB + + +#if defined(__sh__) + +#define MATCH_MACHINE(x) (x == EM_SH) +#define SHT_RELM SHT_RELA +#define Elf32_RelM Elf32_Rela + +#elif defined(__arm__) + +#define MATCH_MACHINE(x) (x == EM_ARM) +#define SHT_RELM SHT_REL +#define Elf32_RelM Elf32_Rel + +#elif defined(__i386__) + +/* presumably we can use these for anything but the SH and ARM*/ +/* this is the previous behavior, but it does result in + insmod.c being broken on anything except i386 */ +#ifndef EM_486 +#define MATCH_MACHINE(x) (x == EM_386) +#else #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486) +#endif #define SHT_RELM SHT_REL #define Elf32_RelM Elf32_Rel +#else +#error insmod.c no platform specified +#endif #ifndef ElfW # if ELFCLASSM == ELFCLASS32 @@ -471,11 +517,7 @@ int arch_init_module (struct obj_file *f, struct new_module *); #define _PATH_MODULES "/lib/modules" -#define STRVERSIONLEN 32 - -#if !defined(BB_FEATURE_INSMOD_NEW_KERNEL) && !defined(BB_FEATURE_INSMOD_OLD_KERNEL) -#error "Must have ether BB_FEATURE_INSMOD_NEW_KERNEL or BB_FEATURE_INSMOD_OLD_KERNEL defined" -#endif +static const int STRVERSIONLEN = 32; /*======================================================================*/ @@ -487,24 +529,46 @@ int flag_export = 1; /*======================================================================*/ -struct i386_got_entry { +/* previously, these were named i386_* but since we could be + compiling for the sh, I've renamed them to the more general + arch_* These structures are the same between the x86 and SH, + and we can't support anything else right now anyway. In the + future maybe they should be #if defined'd */ + +/* Done ;-) */ + +#if defined(__arm__) +struct arm_plt_entry +{ + int offset; + int allocated:1; + int inited:1; /* has been set up */ +}; +#endif + +struct arch_got_entry { int offset; unsigned offset_done:1; unsigned reloc_done:1; }; -struct i386_file { +struct arch_file { struct obj_file root; +#if defined(__arm__) + struct obj_section *plt; +#endif struct obj_section *got; }; -struct i386_symbol { +struct arch_symbol { struct obj_symbol root; - struct i386_got_entry gotent; +#if defined(__arm__) + struct arm_plt_entry pltent; +#endif + struct arch_got_entry gotent; }; - struct external_module { const char *name; ElfW(Addr) addr; @@ -530,14 +594,27 @@ _syscall2(int, new_sys_init_module, const char *, name, _syscall5(int, old_sys_init_module, const char *, name, char *, code, unsigned, codesize, struct old_mod_routines *, routines, struct old_symbol_table *, symtab) -_syscall5(int, query_module, const char *, name, int, which, - void *, buf, size_t, bufsize, size_t*, ret); #ifndef BB_RMMOD _syscall1(int, delete_module, const char *, name) #else extern int delete_module(const char *); #endif +/* This is kind of troublesome. See, we don't actually support + the m68k or the arm the same way we support i386 and (now) + sh. In doing my SH patch, I just assumed that whatever works + for i386 also works for m68k and arm since currently insmod.c + does nothing special for them. If this isn't true, the below + line is rather misleading IMHO, and someone should either + change it or add more proper architecture-dependent support + for these boys. + + -- Bryan Rittmeyer */ + +#ifdef BB_FEATURE_OLD_MODULE_INTERFACE +_syscall1(int, get_kernel_syms, struct old_kernel_sym *, ks) +#endif + #if defined(__i386__) || defined(__m68k__) || defined(__arm__) /* Jump through hoops to fixup error return codes */ #define __NR__create_module __NR_create_module @@ -558,29 +635,9 @@ _syscall2(unsigned long, create_module, const char *, name, size_t, size) #endif static char m_filename[BUFSIZ + 1] = "\0"; static char m_fullName[BUFSIZ + 1] = "\0"; -static const char insmod_usage[] = - "insmod [OPTION]... MODULE [symbol=value]...\n" -#ifndef BB_FEATURE_TRIVIAL_HELP - "\nLoads the specified kernel modules into the kernel.\n\n" - "Options:\n" - "\t-f\tForce module to load into the wrong kernel version.\n" - "\t-k\tMake module autoclean-able.\n" - "\t-v\tverbose output\n" "\t-x\tdo not export externs\n" -#endif -; /*======================================================================*/ -void *xrealloc(void *old, size_t size) -{ - void *ptr = realloc(old, size); - if (!ptr) { - perror("Out of memory"); - exit(1); - } - return ptr; -} - static int findNamedModule(const char *fileName, struct stat *statbuf, void *userDate) @@ -591,7 +648,7 @@ static int findNamedModule(const char *fileName, struct stat *statbuf, if (fullName[0] == '\0') return (FALSE); else { - char *tmp = strrchr(fileName, '/'); + char *tmp = strrchr((char *) fileName, '/'); if (tmp == NULL) tmp = (char *) fileName; @@ -599,7 +656,7 @@ static int findNamedModule(const char *fileName, struct stat *statbuf, tmp++; if (check_wildcard_match(tmp, fullName) == TRUE) { /* Stop searching if we find a match */ - memcpy(m_filename, fileName, strlen(fileName)); + memcpy(m_filename, fileName, strlen(fileName)+1); return (FALSE); } } @@ -611,7 +668,7 @@ static int findNamedModule(const char *fileName, struct stat *statbuf, struct obj_file *arch_new_file(void) { - struct i386_file *f; + struct arch_file *f; f = xmalloc(sizeof(*f)); f->got = NULL; return &f->root; @@ -624,70 +681,181 @@ struct obj_section *arch_new_section(void) struct obj_symbol *arch_new_symbol(void) { - struct i386_symbol *sym; + struct arch_symbol *sym; sym = xmalloc(sizeof(*sym)); memset(&sym->gotent, 0, sizeof(sym->gotent)); return &sym->root; } + enum obj_reloc arch_apply_relocation(struct obj_file *f, struct obj_section *targsec, struct obj_section *symsec, struct obj_symbol *sym, - Elf32_Rel * rel, Elf32_Addr v) + ElfW(RelM) *rel, ElfW(Addr) v) { - struct i386_file *ifile = (struct i386_file *) f; - struct i386_symbol *isym = (struct i386_symbol *) sym; + struct arch_file *ifile = (struct arch_file *) f; + struct arch_symbol *isym = (struct arch_symbol *) sym; + + ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset); + ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset; + ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0; +#if defined(__arm__) + ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0; - Elf32_Addr *loc = (Elf32_Addr *) (targsec->contents + rel->r_offset); - Elf32_Addr dot = targsec->header.sh_addr + rel->r_offset; - Elf32_Addr got = ifile->got ? ifile->got->header.sh_addr : 0; + struct arm_plt_entry *pe; + unsigned long *ip; +#endif enum obj_reloc ret = obj_reloc_ok; switch (ELF32_R_TYPE(rel->r_info)) { + +/* even though these constants seem to be the same for + the i386 and the sh, we "#if define" them for clarity + and in case that ever changes */ +#if defined(__sh__) + case R_SH_NONE: +#elif defined(__arm__) + case R_ARM_NONE: +#elif defined(__i386__) case R_386_NONE: +#endif break; +#if defined(__sh__) + case R_SH_DIR32: +#elif defined(__arm__) + case R_ARM_ABS32: +#elif defined(__i386__) case R_386_32: +#endif *loc += v; break; +#if defined(__arm__) +#elif defined(__sh__) + case R_SH_REL32: + *loc += v - dot; + break; +#elif defined(__i386__) case R_386_PLT32: case R_386_PC32: *loc += v - dot; break; +#endif + +#if defined(__sh__) + case R_SH_PLT32: + *loc = v - dot; + break; +#elif defined(__arm__) + case R_ARM_PC24: + case R_ARM_PLT32: + /* find the plt entry and initialize it if necessary */ + assert(isym != NULL); + pe = (struct arm_plt_entry*) &isym->pltent; + if (! pe->inited) { + ip = (unsigned long *) (ifile->plt->contents + pe->offset); + ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */ + ip[1] = v; /* sym@ */ + pe->inited = 1; + } + + /* relative distance to target */ + v -= dot; + /* if the target is too far away.... */ + if ((int)v < -0x02000000 || (int)v >= 0x02000000) { + /* go via the plt */ + v = plt + pe->offset - dot; + } + if (v & 3) + ret = obj_reloc_dangerous; + + /* Convert to words. */ + v >>= 2; + + /* merge the offset into the instruction. */ + *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff); + break; +#elif defined(__i386__) +#endif + +#if defined(__arm__) +#elif defined(__sh__) + case R_SH_GLOB_DAT: + case R_SH_JMP_SLOT: + *loc = v; + break; +#elif defined(__i386__) case R_386_GLOB_DAT: case R_386_JMP_SLOT: *loc = v; break; +#endif - case R_386_RELATIVE: +#if defined(__arm__) +#elif defined(__sh__) + case R_SH_RELATIVE: + *loc += f->baseaddr + rel->r_addend; + break; +#elif defined(__i386__) + case R_386_RELATIVE: *loc += f->baseaddr; break; +#endif +#if defined(__sh__) + case R_SH_GOTPC: +#elif defined(__arm__) + case R_ARM_GOTPC: +#elif defined(__i386__) case R_386_GOTPC: +#endif assert(got != 0); +#if defined(__sh__) + *loc += got - dot + rel->r_addend;; +#elif defined(__i386__) || defined(__arm__) *loc += got - dot; +#endif break; +#if defined(__sh__) + case R_SH_GOT32: +#elif defined(__arm__) + case R_ARM_GOT32: +#elif defined(__i386__) case R_386_GOT32: +#endif assert(isym != NULL); + /* needs an entry in the .got: set it, once */ if (!isym->gotent.reloc_done) { isym->gotent.reloc_done = 1; - *(Elf32_Addr *) (ifile->got->contents + isym->gotent.offset) = - v; + *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v; } + /* make the reloc with_respect_to_.got */ +#if defined(__sh__) + *loc += isym->gotent.offset + rel->r_addend; +#elif defined(__i386__) || defined(__arm__) *loc += isym->gotent.offset; +#endif break; + /* address relative to the got */ +#if defined(__sh__) + case R_SH_GOTOFF: +#elif defined(__arm__) + case R_ARM_GOTOFF: +#elif defined(__i386__) case R_386_GOTOFF: +#endif assert(got != 0); *loc += v - got; break; default: + printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info)); ret = obj_reloc_unhandled; break; } @@ -697,63 +865,112 @@ arch_apply_relocation(struct obj_file *f, int arch_create_got(struct obj_file *f) { - struct i386_file *ifile = (struct i386_file *) f; - int i, n, offset = 0, gotneeded = 0; - - n = ifile->root.header.e_shnum; - for (i = 0; i < n; ++i) { - struct obj_section *relsec, *symsec, *strsec; - Elf32_Rel *rel, *relend; - Elf32_Sym *symtab; - const char *strtab; + struct arch_file *ifile = (struct arch_file *) f; + int i, got_offset = 0, gotneeded = 0; +#if defined(__arm__) + int plt_offset = 0, pltneeded = 0; +#endif + struct obj_section *relsec, *symsec, *strsec; + ElfW(RelM) *rel, *relend; + ElfW(Sym) *symtab, *extsym; + const char *strtab, *name; + struct arch_symbol *intsym; - relsec = ifile->root.sections[i]; - if (relsec->header.sh_type != SHT_REL) + for (i = 0; i < f->header.e_shnum; ++i) { + relsec = f->sections[i]; + if (relsec->header.sh_type != SHT_RELM) continue; - symsec = ifile->root.sections[relsec->header.sh_link]; - strsec = ifile->root.sections[symsec->header.sh_link]; + symsec = f->sections[relsec->header.sh_link]; + strsec = f->sections[symsec->header.sh_link]; - rel = (Elf32_Rel *) relsec->contents; - relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rel)); - symtab = (Elf32_Sym *) symsec->contents; + rel = (ElfW(RelM) *) relsec->contents; + relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM))); + symtab = (ElfW(Sym) *) symsec->contents; strtab = (const char *) strsec->contents; for (; rel < relend; ++rel) { - Elf32_Sym *extsym; - struct i386_symbol *intsym; - const char *name; + extsym = &symtab[ELF32_R_SYM(rel->r_info)]; switch (ELF32_R_TYPE(rel->r_info)) { +#if defined(__arm__) + case R_ARM_GOT32: +#elif defined(__sh__) + case R_SH_GOT32: +#elif defined(__i386__) + case R_386_GOT32: +#endif + break; + +#if defined(__arm__) + case R_ARM_PC24: + case R_ARM_PLT32: + pltneeded = 1; + break; + + case R_ARM_GOTPC: + case R_ARM_GOTOFF: + gotneeded = 1; + if (got_offset == 0) + got_offset = 4; +#elif defined(__sh__) + case R_SH_GOTPC: + case R_SH_GOTOFF: + gotneeded = 1; +#elif defined(__i386__) case R_386_GOTPC: case R_386_GOTOFF: gotneeded = 1; +#endif + default: continue; - - case R_386_GOT32: - break; } - extsym = &symtab[ELF32_R_SYM(rel->r_info)]; - if (extsym->st_name) + if (extsym->st_name != 0) { name = strtab + extsym->st_name; - else + } else { name = f->sections[extsym->st_shndx]->name; - intsym = - (struct i386_symbol *) obj_find_symbol(&ifile->root, name); + } + intsym = (struct arch_symbol *) obj_find_symbol(f, name); if (!intsym->gotent.offset_done) { intsym->gotent.offset_done = 1; - intsym->gotent.offset = offset; - offset += 4; + intsym->gotent.offset = got_offset; + got_offset += 4; + } +#if defined(__arm__) + if (pltneeded && intsym->pltent.allocated == 0) { + intsym->pltent.allocated = 1; + intsym->pltent.offset = plt_offset; + plt_offset += 8; + intsym->pltent.inited = 0; + pltneeded = 0; } +#endif + } + } + +#if defined(__arm__) + if (got_offset) { + struct obj_section* relsec = obj_find_section(f, ".got"); + + if (relsec) { + obj_extend_section(relsec, got_offset); + } else { + relsec = obj_create_alloced_section(f, ".got", 8, got_offset); + assert(relsec); } + + ifile->got = relsec; } - if (offset > 0 || gotneeded) - ifile->got = - obj_create_alloced_section(&ifile->root, ".got", 4, offset); + if (plt_offset) + ifile->plt = obj_create_alloced_section(f, ".plt", 8, plt_offset); +#else + if (got_offset > 0 || gotneeded) + ifile->got = obj_create_alloced_section(f, ".got", 4, got_offset); +#endif return 1; } @@ -936,7 +1153,7 @@ struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name, /* Don't report an error if the symbol is coming from the kernel or some external module. */ if (secidx <= SHN_HIRESERVE) - fprintf(stderr, "%s multiply defined\n", name); + error_msg("%s multiply defined\n", name); return sym; } } @@ -1132,7 +1349,7 @@ add_symbols_from( static void add_kernel_symbols(struct obj_file *f) { struct external_module *m; - size_t i, nused = 0; + int i, nused = 0; /* Add module symbols first. */ @@ -1164,10 +1381,10 @@ static char *get_modinfo_value(struct obj_file *f, const char *key) v = strchr(p, '='); n = strchr(p, '\0'); if (v) { - if (v - p == klen && strncmp(p, key, klen) == 0) + if (p + klen == v && strncmp(p, key, klen) == 0) return v + 1; } else { - if (n - p == klen && strcmp(p, key) == 0) + if (p + klen == n && strcmp(p, key) == 0) return n; } p = n + 1; @@ -1189,15 +1406,17 @@ old_process_module_arguments(struct obj_file *f, int argc, char **argv) int *loc; p = *argv; - if ((q = strchr(p, '=')) == NULL) + if ((q = strchr(p, '=')) == NULL) { + argc--; continue; + } *q++ = '\0'; sym = obj_find_symbol(f, p); /* Also check that the parameter was not resolved from the kernel. */ if (sym == NULL || sym->secidx > SHN_HIRESERVE) { - fprintf(stderr, "symbol for parameter %s not found\n", p); + error_msg("symbol for parameter %s not found\n", p); return 0; } @@ -1210,7 +1429,7 @@ old_process_module_arguments(struct obj_file *f, int argc, char **argv) str = alloca(strlen(q)); for (r = str, q++; *q != '"'; ++q, ++r) { if (*q == '\0') { - fprintf(stderr, "improperly terminated string argument for %s\n", p); + error_msg("improperly terminated string argument for %s\n", p); return 0; } else if (*q == '\\') switch (*++q) { @@ -1326,11 +1545,11 @@ old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN]) #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */ -#ifdef BB_FEATURE_INSMOD_OLD_KERNEL +#ifdef BB_FEATURE_OLD_MODULE_INTERFACE /* Fetch all the symbols and divvy them up as appropriate for the modules. */ -static int old_get_kernel_symbols(void) +static int old_get_kernel_symbols(const char *m_name) { struct old_kernel_sym *ks, *k; struct new_module_symbol *s; @@ -1339,7 +1558,7 @@ static int old_get_kernel_symbols(void) nks = get_kernel_syms(NULL); if (nks < 0) { - errorMsg("get_kernel_syms: %s: %s", m_name, strerror(errno)); + perror_msg("get_kernel_syms: %s", m_name); return 0; } @@ -1481,7 +1700,8 @@ old_init_module(const char *m_name, struct obj_file *f, ksym->name = (unsigned long) str - (unsigned long) symtab; - str = stpcpy(str, sym->name) + 1; + strcpy(str, sym->name); + str += strlen(sym->name) + 1; ksym++; } } @@ -1520,7 +1740,7 @@ old_init_module(const char *m_name, struct obj_file *f, m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN : 0), &routines, symtab); if (ret) - errorMsg("init_module: %s: %s", m_name, strerror(errno)); + perror_msg("init_module: %s", m_name); free(image); free(symtab); @@ -1533,7 +1753,7 @@ old_init_module(const char *m_name, struct obj_file *f, #define old_create_mod_use_count(x) TRUE #define old_init_module(x, y, z) TRUE -#endif /* BB_FEATURE_INSMOD_OLD_KERNEL */ +#endif /* BB_FEATURE_OLD_MODULE_INTERFACE */ @@ -1550,8 +1770,10 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv) int min, max, n; p = *argv; - if ((q = strchr(p, '=')) == NULL) + if ((q = strchr(p, '=')) == NULL) { + argc--; continue; + } key = alloca(q - p + 6); memcpy(key, "parm_", 5); @@ -1561,7 +1783,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv) p = get_modinfo_value(f, key); key += 5; if (p == NULL) { - fprintf(stderr, "invalid parameter %s\n", key); + error_msg("invalid parameter %s\n", key); return 0; } @@ -1569,7 +1791,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv) /* Also check that the parameter was not resolved from the kernel. */ if (sym == NULL || sym->secidx > SHN_HIRESERVE) { - fprintf(stderr, "symbol for parameter %s not found\n", key); + error_msg("symbol for parameter %s not found\n", key); return 0; } @@ -1597,8 +1819,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv) str = alloca(strlen(q)); for (r = str, q++; *q != '"'; ++q, ++r) { if (*q == '\0') { - fprintf(stderr, - "improperly terminated string argument for %s\n", + error_msg("improperly terminated string argument for %s\n", key); return 0; } else if (*q == '\\') @@ -1687,13 +1908,12 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv) loc += tgt_sizeof_char_p; } else { /* Array of chars (in fact, matrix !) */ - long charssize; /* size of each member */ + unsigned long charssize; /* size of each member */ /* Get the size of each member */ /* Probably we should do that outside the loop ? */ if (!isdigit(*(p + 1))) { - fprintf(stderr, - "parameter type 'c' for %s must be followed by" + error_msg("parameter type 'c' for %s must be followed by" " the maximum size\n", key); return 0; } @@ -1701,8 +1921,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv) /* Check length */ if (strlen(str) >= charssize) { - fprintf(stderr, - "string too long for %s (max %ld)\n", key, + error_msg("string too long for %s (max %ld)\n", key, charssize - 1); return 0; } @@ -1731,8 +1950,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv) break; default: - fprintf(stderr, "unknown parameter type '%c' for %s\n", - *p, key); + error_msg("unknown parameter type '%c' for %s\n", *p, key); return 0; } } @@ -1751,22 +1969,21 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv) case ',': if (++n > max) { - fprintf(stderr, "too many values for %s (max %d)\n", - key, max); + error_msg("too many values for %s (max %d)\n", key, max); return 0; } ++q; break; default: - fprintf(stderr, "invalid argument syntax for %s\n", key); + error_msg("invalid argument syntax for %s\n", key); return 0; } } end_of_arg: if (n < min) { - fprintf(stderr, "too few values for %s (min %d)\n", key, min); + error_msg("too few values for %s (min %d)\n", key, min); return 0; } @@ -1815,7 +2032,7 @@ new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN]) #endif /* BB_FEATURE_INSMOD_VERSION_CHECKING */ -#ifdef BB_FEATURE_INSMOD_NEW_KERNEL +#ifdef BB_FEATURE_NEW_MODULE_INTERFACE /* Fetch the loaded modules, and all currently exported symbols. */ @@ -1835,7 +2052,7 @@ static int new_get_kernel_symbols(void) module_names = xrealloc(module_names, bufsize = ret); goto retry_modules_load; } - errorMsg("QM_MODULES: %s", strerror(errno)); + perror_msg("QM_MODULES"); return 0; } @@ -1854,7 +2071,7 @@ static int new_get_kernel_symbols(void) /* The module was removed out from underneath us. */ continue; } - errorMsg("query_module: QM_INFO: %s: %s", mn, strerror(errno)); + perror_msg("query_module: QM_INFO: %s", mn); return 0; } @@ -1869,7 +2086,7 @@ static int new_get_kernel_symbols(void) /* The module was removed out from underneath us. */ continue; default: - errorMsg("query_module: QM_SYMBOLS: %s: %s", mn, strerror(errno)); + perror_msg("query_module: QM_SYMBOLS: %s", mn); return 0; } } @@ -1894,7 +2111,7 @@ static int new_get_kernel_symbols(void) syms = xrealloc(syms, bufsize = ret); goto retry_kern_sym_load; } - errorMsg("kernel: QM_SYMBOLS: %s", strerror(errno)); + perror_msg("kernel: QM_SYMBOLS"); return 0; } nksyms = nsyms = ret; @@ -2075,7 +2292,7 @@ new_init_module(const char *m_name, struct obj_file *f, ret = new_sys_init_module(m_name, (struct new_module *) image); if (ret) - errorMsg("init_module: %s: %s", m_name, strerror(errno)); + perror_msg("init_module: %s", m_name); free(image); @@ -2087,8 +2304,9 @@ new_init_module(const char *m_name, struct obj_file *f, #define new_init_module(x, y, z) TRUE #define new_create_this_module(x, y) 0 #define new_create_module_ksymtab(x) +#define query_module(v, w, x, y, z) -1 -#endif /* BB_FEATURE_INSMOD_OLD_KERNEL */ +#endif /* BB_FEATURE_NEW_MODULE_INTERFACE */ /*======================================================================*/ @@ -2151,7 +2369,7 @@ int obj_check_undefineds(struct obj_file *f) sym->secidx = SHN_ABS; sym->value = 0; } else { - fprintf(stderr, "unresolved symbol %s\n", sym->name); + error_msg("unresolved symbol %s\n", sym->name); ret = 0; } } @@ -2258,8 +2476,12 @@ void obj_allocate_commons(struct obj_file *f) for (i = 0; i < f->header.e_shnum; ++i) { struct obj_section *s = f->sections[i]; if (s->header.sh_type == SHT_NOBITS) { + if (s->header.sh_size != 0) s->contents = memset(xmalloc(s->header.sh_size), 0, s->header.sh_size); + else + s->contents = NULL; + s->header.sh_type = SHT_PROGBITS; } } @@ -2374,11 +2596,11 @@ int obj_relocate(struct obj_file *f, ElfW(Addr) base) errmsg = "Unhandled relocation"; bad_reloc: if (extsym) { - fprintf(stderr, "%s of type %ld for %s\n", errmsg, + error_msg("%s of type %ld for %s\n", errmsg, (long) ELFW(R_TYPE) (rel->r_info), strtab + extsym->st_name); } else { - fprintf(stderr, "%s of type %ld\n", errmsg, + error_msg("%s of type %ld\n", errmsg, (long) ELFW(R_TYPE) (rel->r_info)); } ret = 0; @@ -2455,7 +2677,7 @@ struct obj_file *obj_load(FILE * fp) fseek(fp, 0, SEEK_SET); if (fread(&f->header, sizeof(f->header), 1, fp) != 1) { - errorMsg("error reading ELF header: %s", strerror(errno)); + perror_msg("error reading ELF header"); return NULL; } @@ -2463,25 +2685,25 @@ struct obj_file *obj_load(FILE * fp) || f->header.e_ident[EI_MAG1] != ELFMAG1 || f->header.e_ident[EI_MAG2] != ELFMAG2 || f->header.e_ident[EI_MAG3] != ELFMAG3) { - fprintf(stderr, "not an ELF file\n"); + error_msg("not an ELF file\n"); return NULL; } if (f->header.e_ident[EI_CLASS] != ELFCLASSM || f->header.e_ident[EI_DATA] != ELFDATAM || f->header.e_ident[EI_VERSION] != EV_CURRENT || !MATCH_MACHINE(f->header.e_machine)) { - fprintf(stderr, "ELF file not for this architecture\n"); + error_msg("ELF file not for this architecture\n"); return NULL; } if (f->header.e_type != ET_REL) { - fprintf(stderr, "ELF file not a relocatable object\n"); + error_msg("ELF file not a relocatable object\n"); return NULL; } /* Read the section headers. */ if (f->header.e_shentsize != sizeof(ElfW(Shdr))) { - fprintf(stderr, "section header size mismatch: %lu != %lu\n", + error_msg("section header size mismatch: %lu != %lu\n", (unsigned long) f->header.e_shentsize, (unsigned long) sizeof(ElfW(Shdr))); return NULL; @@ -2494,7 +2716,7 @@ struct obj_file *obj_load(FILE * fp) section_headers = alloca(sizeof(ElfW(Shdr)) * shnum); fseek(fp, f->header.e_shoff, SEEK_SET); if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) { - errorMsg("error reading ELF section headers: %s", strerror(errno)); + perror_msg("error reading ELF section headers"); return NULL; } @@ -2524,7 +2746,7 @@ struct obj_file *obj_load(FILE * fp) sec->contents = xmalloc(sec->header.sh_size); fseek(fp, sec->header.sh_offset, SEEK_SET); if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) { - errorMsg("error reading ELF section data: %s", strerror(errno)); + perror_msg("error reading ELF section data"); return NULL; } } else { @@ -2534,13 +2756,11 @@ struct obj_file *obj_load(FILE * fp) #if SHT_RELM == SHT_REL case SHT_RELA: - fprintf(stderr, - "RELA relocations not supported on this architecture\n"); + error_msg("RELA relocations not supported on this architecture\n"); return NULL; #else case SHT_REL: - fprintf(stderr, - "REL relocations not supported on this architecture\n"); + error_msg("REL relocations not supported on this architecture\n"); return NULL; #endif @@ -2553,7 +2773,7 @@ struct obj_file *obj_load(FILE * fp) break; } - fprintf(stderr, "can't handle sections of type %ld\n", + error_msg("can't handle sections of type %ld\n", (long) sec->header.sh_type); return NULL; } @@ -2582,7 +2802,7 @@ struct obj_file *obj_load(FILE * fp) ElfW(Sym) * sym; if (sec->header.sh_entsize != sizeof(ElfW(Sym))) { - fprintf(stderr, "symbol size mismatch: %lu != %lu\n", + error_msg("symbol size mismatch: %lu != %lu\n", (unsigned long) sec->header.sh_entsize, (unsigned long) sizeof(ElfW(Sym))); return NULL; @@ -2614,8 +2834,7 @@ struct obj_file *obj_load(FILE * fp) case SHT_RELM: if (sec->header.sh_entsize != sizeof(ElfW(RelM))) { - fprintf(stderr, - "relocation entry size mismatch: %lu != %lu\n", + error_msg("relocation entry size mismatch: %lu != %lu\n", (unsigned long) sec->header.sh_entsize, (unsigned long) sizeof(ElfW(RelM))); return NULL; @@ -2649,6 +2868,7 @@ static void hide_special_symbols(struct obj_file *f) extern int insmod_main( int argc, char **argv) { + int opt; int k_crcs; int k_new_syscalls; int len; @@ -2658,7 +2878,7 @@ extern int insmod_main( int argc, char **argv) FILE *fp; struct obj_file *f; char m_name[BUFSIZ + 1] = "\0"; - int exit_status = FALSE; + int exit_status = EXIT_FAILURE; int m_has_modinfo; #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING int k_version; @@ -2668,15 +2888,9 @@ extern int insmod_main( int argc, char **argv) int m_crcs; #endif - - if (argc <= 1) { - usage(insmod_usage); - } - /* Parse any options */ - while (--argc > 0 && **(++argv) == '-') { - while (*(++(*argv))) { - switch (**argv) { + while ((opt = getopt(argc, argv, "fkvxL")) > 0) { + switch (opt) { case 'f': /* force loading */ flag_force_load = 1; break; @@ -2689,20 +2903,26 @@ extern int insmod_main( int argc, char **argv) case 'x': /* do not export externs */ flag_export = 0; break; + case 'L': /* Stub warning */ + /* This is needed for compatibility with modprobe. + * In theory, this does locking, but we don't do + * that. So be careful and plan your life around not + * loading the same module 50 times concurrently. */ + break; default: usage(insmod_usage); - } } } - - if (argc <= 0) { + + if (argv[optind] == NULL) { usage(insmod_usage); } + /* Grab the module name */ - if ((tmp = strrchr(*argv, '/')) != NULL) { + if ((tmp = strrchr(argv[optind], '/')) != NULL) { tmp++; } else { - tmp = *argv; + tmp = argv[optind]; } len = strlen(tmp); @@ -2713,26 +2933,25 @@ extern int insmod_main( int argc, char **argv) strcat(m_fullName, ".o"); /* Get a filedesc for the module */ - if ((fp = fopen(*argv, "r")) == NULL) { + if ((fp = fopen(argv[optind], "r")) == NULL) { /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */ - if (recursiveAction(_PATH_MODULES, TRUE, FALSE, FALSE, - findNamedModule, 0, m_fullName) == TRUE) + if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE, + findNamedModule, 0, m_fullName) == FALSE) { if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL)) { - errorMsg("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES); - exit(FALSE); + error_msg("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES); + return EXIT_FAILURE; } - } + } else + error_msg_and_die("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES); } else - memcpy(m_filename, *argv, strlen(*argv)); + memcpy(m_filename, argv[optind], strlen(argv[optind])); - if ((f = obj_load(fp)) == NULL) { - perror("Could not load the module\n"); - exit(FALSE); - } + if ((f = obj_load(fp)) == NULL) + perror_msg_and_die("Could not load the module"); if (get_modinfo_value(f, "kernel_version") == NULL) m_has_modinfo = 0; @@ -2748,20 +2967,20 @@ extern int insmod_main( int argc, char **argv) } else { m_version = old_get_module_version(f, m_strversion); if (m_version == -1) { - fprintf(stderr, - "couldn't find the kernel version the module was compiled for\n"); + error_msg("couldn't find the kernel version the module was " + "compiled for\n"); goto out; } } if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) { if (flag_force_load) { - fprintf(stderr, "Warning: kernel-module version mismatch\n" + error_msg("Warning: kernel-module version mismatch\n" "\t%s was compiled for kernel version %s\n" "\twhile this kernel is version %s\n", m_filename, m_strversion, k_strversion); } else { - fprintf(stderr, "kernel-module version mismatch\n" + error_msg("kernel-module version mismatch\n" "\t%s was compiled for kernel version %s\n" "\twhile this kernel is version %s.\n", m_filename, m_strversion, k_strversion); @@ -2774,21 +2993,21 @@ extern int insmod_main( int argc, char **argv) k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL); if (k_new_syscalls) { -#ifdef BB_FEATURE_INSMOD_NEW_KERNEL +#ifdef BB_FEATURE_NEW_MODULE_INTERFACE if (!new_get_kernel_symbols()) goto out; k_crcs = new_is_kernel_checksummed(); #else - fprintf(stderr, "Not configured to support new kernels\n"); + error_msg("Not configured to support new kernels\n"); goto out; #endif } else { -#ifdef BB_FEATURE_INSMOD_OLD_KERNEL - if (!old_get_kernel_symbols()) +#ifdef BB_FEATURE_OLD_MODULE_INTERFACE + if (!old_get_kernel_symbols(m_name)) goto out; k_crcs = old_is_kernel_checksummed(); #else - fprintf(stderr, "Not configured to support old kernels\n"); + error_msg("Not configured to support old kernels\n"); goto out; #endif } @@ -2820,6 +3039,9 @@ extern int insmod_main( int argc, char **argv) } obj_allocate_commons(f); + /* done with the module name, on to the optional var=value arguments */ + ++optind; + if (optind < argc) { if (m_has_modinfo ? !new_process_module_arguments(f, argc - optind, argv + optind) @@ -2839,21 +3061,17 @@ extern int insmod_main( int argc, char **argv) m_size = obj_load_size(f); - errno = 0; m_addr = create_module(m_name, m_size); - switch (errno) { - case 0: - break; + if (m_addr==-1) switch (errno) { case EEXIST: - fprintf(stderr, "A module named %s already exists\n", m_name); + error_msg("A module named %s already exists\n", m_name); goto out; case ENOMEM: - fprintf(stderr, - "Can't allocate kernel memory for module; needed %lu bytes\n", + error_msg("Can't allocate kernel memory for module; needed %lu bytes\n", m_size); goto out; default: - errorMsg("create_module: %s: %s", m_name, strerror(errno)); + perror_msg("create_module: %s", m_name); goto out; } @@ -2870,7 +3088,7 @@ extern int insmod_main( int argc, char **argv) goto out; } - exit_status = TRUE; + exit_status = EXIT_SUCCESS; out: fclose(fp);