rpm2cpio: handle bz2 too; code shrink
[oweals/busybox.git] / modutils / modutils-24.c
index 4e112a06f7d166dc0622816f68a5f861827cb878..9f91c9979883e719d2bf0a04180a288c634a3fcb 100644 (file)
@@ -511,8 +511,7 @@ int delete_module(const char *module, unsigned int flags);
 struct obj_string_patch;
 struct obj_symbol_patch;
 
-struct obj_section
-{
+struct obj_section {
        ElfW(Shdr) header;
        const char *name;
        char *contents;
@@ -520,8 +519,7 @@ struct obj_section
        int idx;
 };
 
-struct obj_symbol
-{
+struct obj_symbol {
        struct obj_symbol *next;        /* hash table link */
        const char *name;
        unsigned long value;
@@ -546,8 +544,8 @@ struct obj_file {
        struct obj_section **load_order_search_start;
        struct obj_string_patch *string_patches;
        struct obj_symbol_patch *symbol_patches;
-       int (*symbol_cmp)(const char *, const char *);
-       unsigned long (*symbol_hash)(const char *);
+       int (*symbol_cmp)(const char *, const char *); /* cant be FAST_FUNC */
+       unsigned long (*symbol_hash)(const char *) FAST_FUNC;
        unsigned long local_symtab_size;
        struct obj_symbol **local_symtab;
        struct obj_symbol *symtab[HASH_BUCKETS];
@@ -577,45 +575,45 @@ struct obj_symbol_patch {
 
 /* Generic object manipulation routines.  */
 
-static unsigned long obj_elf_hash(const char *);
+static unsigned long FAST_FUNC obj_elf_hash(const char *);
 
 static unsigned long obj_elf_hash_n(const char *, unsigned long len);
 
 static struct obj_symbol *obj_find_symbol(struct obj_file *f,
-                                        const char *name);
+               const char *name);
 
 static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
-                                 struct obj_symbol *sym);
+               struct obj_symbol *sym);
 
 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
 static void obj_set_symbol_compare(struct obj_file *f,
-                           int (*cmp)(const char *, const char *),
-                           unsigned long (*hash)(const char *));
+               int (*cmp)(const char *, const char *),
+               unsigned long (*hash)(const char *) FAST_FUNC);
 #endif
 
 static struct obj_section *obj_find_section(struct obj_file *f,
-                                          const char *name);
+               const char *name);
 
 static void obj_insert_section_load_order(struct obj_file *f,
-                                   struct obj_section *sec);
+               struct obj_section *sec);
 
 static struct obj_section *obj_create_alloced_section(struct obj_file *f,
-                                               const char *name,
-                                               unsigned long align,
-                                               unsigned long size);
+               const char *name,
+               unsigned long align,
+               unsigned long size);
 
 static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
-                                                     const char *name,
-                                                     unsigned long align,
-                                                     unsigned long size);
+               const char *name,
+               unsigned long align,
+               unsigned long size);
 
 static void *obj_extend_section(struct obj_section *sec, unsigned long more);
 
 static void obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
-                    const char *string);
+               const char *string);
 
 static void obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
-                    struct obj_symbol *sym);
+               struct obj_symbol *sym);
 
 static void obj_check_undefineds(struct obj_file *f);
 
@@ -625,7 +623,11 @@ static unsigned long obj_load_size(struct obj_file *f);
 
 static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
 
-static struct obj_file *obj_load(FILE *f, int loadprogbits);
+#if !LOADBITS
+#define obj_load(image, image_size, loadprogbits) \
+       obj_load(image, image_size)
+#endif
+static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits);
 
 static int obj_create_image(struct obj_file *f, char *image);
 
@@ -638,10 +640,10 @@ static struct obj_section *arch_new_section(void);
 static struct obj_symbol *arch_new_symbol(void);
 
 static enum obj_reloc arch_apply_relocation(struct obj_file *f,
-                                     struct obj_section *targsec,
-                                     /*struct obj_section *symsec,*/
-                                     struct obj_symbol *sym,
-                                     ElfW(RelM) *rel, ElfW(Addr) value);
+               struct obj_section *targsec,
+               /*struct obj_section *symsec,*/
+               struct obj_symbol *sym,
+               ElfW(RelM) *rel, ElfW(Addr) value);
 
 static void arch_create_got(struct obj_file *f);
 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
@@ -675,8 +677,7 @@ enum { STRVERSIONLEN = 64 };
 
 #if defined(USE_LIST)
 
-struct arch_list_entry
-{
+struct arch_list_entry {
        struct arch_list_entry *next;
        LIST_ARCHTYPE addend;
        int offset;
@@ -687,8 +688,7 @@ struct arch_list_entry
 
 #if defined(USE_SINGLE)
 
-struct arch_single_entry
-{
+struct arch_single_entry {
        int offset;
        int inited : 1;
        int allocated : 1;
@@ -697,8 +697,7 @@ struct arch_single_entry
 #endif
 
 #if defined(__mips__)
-struct mips_hi16
-{
+struct mips_hi16 {
        struct mips_hi16 *next;
        ElfW(Addr) *addr;
        ElfW(Addr) value;
@@ -772,10 +771,10 @@ static struct obj_symbol *arch_new_symbol(void)
 
 static enum obj_reloc
 arch_apply_relocation(struct obj_file *f,
-                               struct obj_section *targsec,
-                               /*struct obj_section *symsec,*/
-                               struct obj_symbol *sym,
-                               ElfW(RelM) *rel, ElfW(Addr) v)
+               struct obj_section *targsec,
+               /*struct obj_section *symsec,*/
+               struct obj_symbol *sym,
+               ElfW(RelM) *rel, ElfW(Addr) v)
 {
 #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
  || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \
@@ -1703,7 +1702,7 @@ static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
 #if defined(USE_SINGLE)
 
 static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
-                            int offset, int size)
+               int offset, int size)
 {
        if (single->allocated == 0) {
                single->allocated = 1;
@@ -1719,7 +1718,7 @@ static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *singl
 #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
 
 static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
-                                          int offset, int size)
+               int offset, int size)
 {
        struct obj_section *myrelsec = obj_find_section(f, name);
 
@@ -1912,7 +1911,7 @@ static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
        return h;
 }
 
-static unsigned long obj_elf_hash(const char *name)
+static unsigned long FAST_FUNC obj_elf_hash(const char *name)
 {
        return obj_elf_hash_n(name, strlen(name));
 }
@@ -1935,7 +1934,7 @@ static int ncv_strcmp(const char *a, const char *b)
 /* String hashing for non-co-versioned kernel and module.  Here
    we are simply forced to drop the crc from the hash.  */
 
-static unsigned long ncv_symbol_hash(const char *str)
+static unsigned long FAST_FUNC ncv_symbol_hash(const char *str)
 {
        size_t len = strlen(str);
        if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
@@ -1945,8 +1944,8 @@ static unsigned long ncv_symbol_hash(const char *str)
 
 static void
 obj_set_symbol_compare(struct obj_file *f,
-                                          int (*cmp) (const char *, const char *),
-                                          unsigned long (*hash) (const char *))
+               int (*cmp) (const char *, const char *),
+               unsigned long (*hash) (const char *) FAST_FUNC)
 {
        if (cmp)
                f->symbol_cmp = cmp;
@@ -1959,13 +1958,14 @@ obj_set_symbol_compare(struct obj_file *f,
                memcpy(tmptab, f->symtab, sizeof(tmptab));
                memset(f->symtab, 0, sizeof(f->symtab));
 
-               for (i = 0; i < HASH_BUCKETS; ++i)
+               for (i = 0; i < HASH_BUCKETS; ++i) {
                        for (sym = tmptab[i]; sym; sym = next) {
                                unsigned long h = hash(sym->name) % HASH_BUCKETS;
                                next = sym->next;
                                sym->next = f->symtab[h];
                                f->symtab[h] = sym;
                        }
+               }
        }
 }
 
@@ -1973,9 +1973,9 @@ obj_set_symbol_compare(struct obj_file *f,
 
 static struct obj_symbol *
 obj_add_symbol(struct obj_file *f, const char *name,
-                               unsigned long symidx, int info,
-                               int secidx, ElfW(Addr) value,
-                               unsigned long size)
+               unsigned long symidx, int info,
+               int secidx, ElfW(Addr) value,
+               unsigned long size)
 {
        struct obj_symbol *sym;
        unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
@@ -2135,10 +2135,10 @@ obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
        *p = sec;
 }
 
-static struct obj_section *obj_create_alloced_section(struct obj_file *f,
-                               const char *name,
-                               unsigned long align,
-                               unsigned long size)
+static struct obj_section *helper_create_alloced_section(struct obj_file *f,
+               const char *name,
+               unsigned long align,
+               unsigned long size)
 {
        int newidx = f->header.e_shnum++;
        struct obj_section *sec;
@@ -2155,31 +2155,29 @@ static struct obj_section *obj_create_alloced_section(struct obj_file *f,
        if (size)
                sec->contents = xzalloc(size);
 
-       obj_insert_section_load_order(f, sec);
-
        return sec;
 }
 
-static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
-                               const char *name,
-                               unsigned long align,
-                               unsigned long size)
+static struct obj_section *obj_create_alloced_section(struct obj_file *f,
+               const char *name,
+               unsigned long align,
+               unsigned long size)
 {
-       int newidx = f->header.e_shnum++;
        struct obj_section *sec;
 
-       f->sections = xrealloc_vector(f->sections, 2, newidx);
-       f->sections[newidx] = sec = arch_new_section();
+       sec = helper_create_alloced_section(f, name, align, size);
+       obj_insert_section_load_order(f, sec);
+       return sec;
+}
 
-       sec->header.sh_type = SHT_PROGBITS;
-       sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
-       sec->header.sh_size = size;
-       sec->header.sh_addralign = align;
-       sec->name = name;
-       sec->idx = newidx;
-       if (size)
-               sec->contents = xzalloc(size);
+static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
+               const char *name,
+               unsigned long align,
+               unsigned long size)
+{
+       struct obj_section *sec;
 
+       sec = helper_create_alloced_section(f, name, align, size);
        sec->load_next = f->load_order;
        f->load_order = sec;
        if (f->load_order_search_start == &f->load_order)
@@ -2202,15 +2200,16 @@ static void *obj_extend_section(struct obj_section *sec, unsigned long more)
 /* Conditionally add the symbols from the given symbol set to the
    new module.  */
 
-static int
-add_symbols_from( struct obj_file *f,
-                                int idx, struct new_module_symbol *syms, size_t nsyms)
+static int add_symbols_from(struct obj_file *f,
+               int idx,
+               struct new_module_symbol *syms,
+               size_t nsyms)
 {
        struct new_module_symbol *s;
        size_t i;
        int used = 0;
 #ifdef SYMBOL_PREFIX
-       char *name_buf = 0;
+       char *name_buf = NULL;
        size_t name_alloced_size = 0;
 #endif
 #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
@@ -2882,7 +2881,7 @@ obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
 
 static void
 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
-                                struct obj_symbol *sym)
+               struct obj_symbol *sym)
 {
        struct obj_symbol_patch *p;
 
@@ -2900,7 +2899,7 @@ static void obj_check_undefineds(struct obj_file *f)
 
        for (i = 0; i < HASH_BUCKETS; ++i) {
                struct obj_symbol *sym;
-               for (sym = f->symtab[i]; sym; sym = sym->next)
+               for (sym = f->symtab[i]; sym; sym = sym->next) {
                        if (sym->secidx == SHN_UNDEF) {
                                if (ELF_ST_BIND(sym->info) == STB_WEAK) {
                                        sym->secidx = SHN_ABS;
@@ -2910,6 +2909,7 @@ static void obj_check_undefineds(struct obj_file *f)
                                                bb_error_msg_and_die("unresolved symbol %s", sym->name);
                                }
                        }
+               }
        }
 }
 
@@ -3074,7 +3074,7 @@ static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
                        ElfW(Addr) value = 0;
                        struct obj_symbol *intsym = NULL;
                        unsigned long symndx;
-                       ElfW(Sym) * extsym = 0;
+                       ElfW(Sym) *extsym = NULL;
                        const char *errmsg;
 
                        /* Attempt to find a value to use for this relocation.  */
@@ -3191,8 +3191,13 @@ static int obj_create_image(struct obj_file *f, char *image)
 
 /*======================================================================*/
 
-static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
+static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits)
 {
+#if BB_LITTLE_ENDIAN
+# define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3)))))
+#else
+# define ELFMAG_U32 ((uint32_t)((((ELFMAG0 * 0x100) + ELFMAG1) * 0x100 + ELFMAG2) * 0x100 + ELFMAG3))
+#endif
        struct obj_file *f;
        ElfW(Shdr) * section_headers;
        size_t shnum, i;
@@ -3205,16 +3210,11 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
        f->symbol_hash = obj_elf_hash;
        f->load_order_search_start = &f->load_order;
 
-       fseek(fp, 0, SEEK_SET);
-       if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
-               bb_perror_msg_and_die("error reading ELF header");
-       }
+       if (image_size < sizeof(f->header))
+               bb_error_msg_and_die("error while loading ELF header");
+       memcpy(&f->header, image, sizeof(f->header));
 
-       if (f->header.e_ident[EI_MAG0] != ELFMAG0
-        || f->header.e_ident[EI_MAG1] != ELFMAG1
-        || f->header.e_ident[EI_MAG2] != ELFMAG2
-        || f->header.e_ident[EI_MAG3] != ELFMAG3
-       ) {
+       if (*(uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
                bb_error_msg_and_die("not an ELF file");
        }
        if (f->header.e_ident[EI_CLASS] != ELFCLASSM
@@ -3243,10 +3243,9 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
        f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
 
        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) {
-               bb_perror_msg_and_die("error reading ELF section headers");
-       }
+       if (image_size < f->header.e_shoff + sizeof(ElfW(Shdr)) * shnum)
+               bb_error_msg_and_die("error while loading section headers");
+       memcpy(section_headers, image + f->header.e_shoff, sizeof(ElfW(Shdr)) * shnum);
 
        /* Read the section data.  */
 
@@ -3278,10 +3277,9 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
                                sec->contents = NULL;
                                if (sec->header.sh_size > 0) {
                                        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) {
-                                               bb_perror_msg_and_die("error reading ELF section data");
-                                       }
+                                       if (image_size < (sec->header.sh_offset + sec->header.sh_size))
+                                               bb_error_msg_and_die("error while loading section data");
+                                       memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
                                }
                                break;
 #if SHT_RELM == SHT_REL
@@ -3397,27 +3395,23 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
  * kernel for the module
  */
 
-static int obj_load_progbits(FILE *fp, struct obj_file *f, char *imagebase)
+static int obj_load_progbits(char *image, size_t image_size, struct obj_file *f, char *imagebase)
 {
        ElfW(Addr) base = f->baseaddr;
        struct obj_section* sec;
 
        for (sec = f->load_order; sec; sec = sec->load_next) {
-
                /* section already loaded? */
                if (sec->contents != NULL)
                        continue;
-
                if (sec->header.sh_size == 0)
                        continue;
-
                sec->contents = imagebase + (sec->header.sh_addr - base);
-               fseek(fp, sec->header.sh_offset, SEEK_SET);
-               if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
-                       bb_perror_msg("error reading ELF section data");
-                       return 0;
+               if (image_size < (sec->header.sh_offset + sec->header.sh_size)) {
+                       bb_error_msg("error reading ELF section data");
+                       return 0; /* need to delete half-loaded module! */
                }
-
+               memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
        }
        return 1;
 }
@@ -3493,7 +3487,8 @@ static int obj_gpl_license(struct obj_file *f, const char **license)
 #define TAINT_URL                       "http://www.tux.org/lkml/#export-tainted"
 
 static void set_tainted(int fd, const char *m_name,
-               int kernel_has_tainted, int taint, const char *text1, const char *text2)
+               int kernel_has_tainted, int taint,
+               const char *text1, const char *text2)
 {
        static smallint printed_info;
 
@@ -3513,7 +3508,7 @@ static void set_tainted(int fd, const char *m_name,
                buf[sizeof(buf)-1] = '\0';
                oldval = strtoul(buf, NULL, 10);
                sprintf(buf, "%d\n", oldval | taint);
-               write(fd, buf, strlen(buf));
+               xwrite_str(fd, buf);
        }
 }
 
@@ -3588,7 +3583,7 @@ get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
  */
 static void
 add_ksymoops_symbols(struct obj_file *f, const char *filename,
-                                const char *m_name)
+               const char *m_name)
 {
        static const char symprefix[] ALIGN1 = "__insmod_";
        static const char section_names[][8] = {
@@ -3634,7 +3629,8 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
                version = get_module_version(f, str);   /* -1 if not found */
                name = xasprintf("%s%s_O%s_M%0*lX_V%d",
                                symprefix, m_name, absolute_filename,
-                               (int)(2 * sizeof(statbuf.st_mtime)), statbuf.st_mtime,
+                               (int)(2 * sizeof(statbuf.st_mtime)),
+                               (long)statbuf.st_mtime,
                                version);
                sym = obj_add_symbol(f, name, -1,
                                ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
@@ -3645,7 +3641,6 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
        free(absolute_filename);
 #ifdef _NOT_SUPPORTED_
        /* record where the persistent data is going, same address as previous symbol */
-
        if (f->persist) {
                name = xasprintf("%s%s_P%s",
                                symprefix, m_name, f->persist);
@@ -3656,7 +3651,6 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
        }
 #endif
        /* tag the desired sections if size is non-zero */
-
        for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
                sec = obj_find_section(f, section_names[i]);
                if (sec && sec->header.sh_size) {
@@ -3678,11 +3672,11 @@ static void print_load_map(struct obj_file *f)
        struct obj_section *sec;
 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
        struct obj_symbol **all, **p;
-       int i, nsyms, *loaded;
+       int i, nsyms;
+       char *loaded; /* array of booleans */
        struct obj_symbol *sym;
 #endif
        /* Report on the section layout.  */
-
        printf("Sections:       Size      %-*s  Align\n",
                        (int) (2 * sizeof(void *)), "Address");
 
@@ -3704,14 +3698,12 @@ static void print_load_map(struct obj_file *f)
        }
 #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
        /* Quick reference which section indices are loaded.  */
-
        i = f->header.e_shnum;
-       loaded = alloca(sizeof(int) * i);
+       loaded = alloca(i * sizeof(loaded[0]));
        while (--i >= 0)
                loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
 
        /* Collect the symbols we'll be listing.  */
-
        for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
                for (sym = f->symtab[i]; sym; sym = sym->next)
                        if (sym->secidx <= SHN_HIRESERVE
@@ -3720,7 +3712,7 @@ static void print_load_map(struct obj_file *f)
                                ++nsyms;
                        }
 
-       all = alloca(nsyms * sizeof(struct obj_symbol *));
+       all = alloca(nsyms * sizeof(all[0]));
 
        for (i = 0, p = all; i < HASH_BUCKETS; ++i)
                for (sym = f->symtab[i]; sym; sym = sym->next)
@@ -3760,7 +3752,7 @@ static void print_load_map(struct obj_file *f)
                }
 
                if (ELF_ST_BIND(sym->info) == STB_LOCAL)
-                       type = tolower(type);
+                       type |= 0x20; /* tolower. safe for '?' too */
 
                printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
                                type, sym->name);
@@ -3779,42 +3771,41 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
        unsigned long m_size;
        ElfW(Addr) m_addr;
        struct obj_file *f;
-       struct utsname uts;
        int exit_status = EXIT_FAILURE;
-       int m_has_modinfo;
        char *m_name;
 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
-       char m_strversion[STRVERSIONLEN];
-       int m_version, m_crcs;
+       int m_has_modinfo;
 #endif
-       FILE *fp;
+       char *image;
+       size_t image_size = 64 * 1024 * 1024;
 
-       uname(&uts);
-       fp = fopen_for_read(m_filename);
-       if (fp == NULL)
+       /* Load module into memory and unzip if compressed */
+       image = xmalloc_open_zipped_read_close(m_filename, &image_size);
+       if (!image)
                return EXIT_FAILURE;
 
        m_name = xstrdup(bb_basename(m_filename));
-       *strrchr(m_name, '.') = 0;
+       /* "module.o[.gz]" -> "module" */
+       *strchrnul(m_name, '.') = '\0';
 
-       f = obj_load(fp, LOADBITS);
-
-       if (get_modinfo_value(f, "kernel_version") == NULL)
-               m_has_modinfo = 0;
-       else
-               m_has_modinfo = 1;
+       f = obj_load(image, image_size, LOADBITS);
 
 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
        /* Version correspondence?  */
+       m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
        if (!flag_quiet) {
+               char m_strversion[STRVERSIONLEN];
+               struct utsname uts;
+
                if (m_has_modinfo) {
-                       m_version = new_get_module_version(f, m_strversion);
+                       int m_version = new_get_module_version(f, m_strversion);
                        if (m_version == -1) {
-                               bb_error_msg_and_die("cannot find the kernel version the module was "
-                                               "compiled for");
+                               bb_error_msg_and_die("can't find the kernel version "
+                                       "the module was compiled for");
                        }
                }
 
+               uname(&uts);
                if (strncmp(uts.release, m_strversion, STRVERSIONLEN) != 0) {
                        bb_error_msg("%skernel-module version mismatch\n"
                                "\t%s was compiled for kernel version %s\n"
@@ -3825,34 +3816,33 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
                                goto out;
                }
        }
-       k_crcs = 0;
 #endif
 
        if (query_module(NULL, 0, NULL, 0, NULL))
-               bb_error_msg_and_die("not configured to support old kernels");
+               bb_error_msg_and_die("old (unsupported) kernel");
        new_get_kernel_symbols();
        k_crcs = new_is_kernel_checksummed();
 
 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
-       m_crcs = 0;
-       if (m_has_modinfo)
-               m_crcs = new_is_module_checksummed(f);
-
-       if (m_crcs != k_crcs)
-               obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
+       {
+               int m_crcs = 0;
+               if (m_has_modinfo)
+                       m_crcs = new_is_module_checksummed(f);
+               if (m_crcs != k_crcs)
+                       obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
+       }
 #endif
 
        /* Let the module know about the kernel symbols.  */
        add_kernel_symbols(f);
 
        /* Allocate common symbols, symbol tables, and string tables.  */
-
        new_create_this_module(f, m_name);
        obj_check_undefineds(f);
        obj_allocate_commons(f);
        check_tainted_module(f, m_name);
 
-       /* done with the module name, on to the optional var=value arguments */
+       /* Done with the module name, on to the optional var=value arguments */
        new_process_module_arguments(f, options);
 
        arch_create_got(f);
@@ -3883,7 +3873,7 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
         * the PROGBITS section was not loaded by the obj_load
         * now we can load them directly into the kernel memory
         */
-       if (!obj_load_progbits(fp, f, (char*)m_addr)) {
+       if (!obj_load_progbits(image, image_size, f, (char*)m_addr)) {
                delete_module(m_name, 0);
                goto out;
        }
@@ -3905,8 +3895,7 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
        exit_status = EXIT_SUCCESS;
 
  out:
-       if (fp)
-               fclose(fp);
+       free(image);
        free(m_name);
 
        return exit_status;