gz_open and gz_close were left in, even when BB_FEATURE_TAR_GZIP was disabled.
[oweals/busybox.git] / insmod.c
index a134fea873d6f6216718e24be7cd4114921c50c5..0a7cb1e4ab3156fd43d177fbca75a3b176e9e173 100644 (file)
--- a/insmod.c
+++ b/insmod.c
@@ -2,7 +2,7 @@
 /*
  * Mini insmod implementation for busybox
  *
- * Copyright (C) 1999,2000 by Lineo, inc.
+ * Copyright (C) 1999,2000,2001 by Lineo, inc.
  * Written by Erik Andersen <andersen@lineo.com>
  * and Ron Alder <alder@lineo.com>
  *
  * very minor changes required to also work with StrongArm and presumably
  * all ARM based systems.
  *
+ * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
+ *   PowerPC specific code stolen from modutils-2.3.16, 
+ *   written by Paul Mackerras, Copyright 1996, 1997 Linux International.
+ *   I've only tested the code on mpc8xx platforms in big-endian mode.
+ *   Did some cleanup and added BB_USE_xxx_ENTRIES...
+ *
+ * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
+ *   based on modutils-2.4.2
+ *   MIPS specific support for Elf loading and relocation.
+ *   Copyright 1996, 1997 Linux International.
+ *   Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
+ *
  * Based almost entirely on the Linux modutils-2.3.11 implementation.
  *   Copyright 1996, 1997 Linux International.
  *   New implementation contributed by Richard Henderson <rth@tamu.edu>
@@ -37,7 +49,6 @@
  *
  */
 
-#include "busybox.h"
 #include <stdlib.h>
 #include <stdio.h>
 #include <stddef.h>
 #include <dirent.h>
 #include <ctype.h>
 #include <assert.h>
+#include <string.h>
 #include <getopt.h>
 #include <sys/utsname.h>
+#include <sys/syscall.h>
+#include <linux/unistd.h>
+#include "busybox.h"
+
+#if defined(__powerpc__)
+#define BB_USE_PLT_ENTRIES
+#define BB_PLT_ENTRY_SIZE 16
+#endif
+
+#if defined(__arm__)
+#define BB_USE_PLT_ENTRIES
+#define BB_PLT_ENTRY_SIZE 8
+#define BB_USE_GOT_ENTRIES
+#define BB_GOT_ENTRY_SIZE 8
+#endif
+
+#if defined(__sh__)
+#define BB_USE_GOT_ENTRIES
+#define BB_GOT_ENTRY_SIZE 4
+#endif
+
+#if defined(__i386__)
+#define BB_USE_GOT_ENTRIES
+#define BB_GOT_ENTRY_SIZE 4
+#endif
+
+#if defined(__mips__)
+// neither used
+#endif
 
 //----------------------------------------------------------------------------
 //--------modutils module.h, lines 45-242
 #ifndef MODUTILS_MODULE_H
 static const int MODUTILS_MODULE_H = 1;
 
-#ident "$Id: insmod.c,v 1.37 2001/01/24 19:07:09 andersen Exp $"
+#ident "$Id: insmod.c,v 1.53 2001/03/22 19:01:16 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
@@ -284,7 +325,7 @@ int delete_module(const char *);
 #ifndef MODUTILS_OBJ_H
 static const int MODUTILS_OBJ_H = 1;
 
-#ident "$Id: insmod.c,v 1.37 2001/01/24 19:07:09 andersen Exp $"
+#ident "$Id: insmod.c,v 1.53 2001/03/22 19:01:16 andersen Exp $"
 
 /* The relocatable object is manipulated using elfin types.  */
 
@@ -308,21 +349,48 @@ static const int MODUTILS_OBJ_H = 1;
 #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
+#define ELFDATAM       ELFDATA2LSB
 
 #elif defined(__arm__)
 
 #define MATCH_MACHINE(x) (x == EM_ARM)
 #define SHT_RELM       SHT_REL
 #define Elf32_RelM     Elf32_Rel
+#define ELFDATAM       ELFDATA2LSB
+
+#elif defined(__powerpc__)
+
+#define MATCH_MACHINE(x) (x == EM_PPC)
+#define SHT_RELM       SHT_RELA
+#define Elf32_RelM     Elf32_Rela
+#define ELFDATAM        ELFDATA2MSB
+
+#elif defined(__mips__)
+
+/* Account for ELF spec changes.  */
+#ifndef EM_MIPS_RS3_LE
+#ifdef EM_MIPS_RS4_BE
+#define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
+#else
+#define EM_MIPS_RS3_LE 10
+#endif
+#endif /* !EM_MIPS_RS3_LE */
+
+#define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
+#define SHT_RELM       SHT_REL
+#define Elf32_RelM     Elf32_Rel
+#ifdef __MIPSEB__
+#define ELFDATAM        ELFDATA2MSB
+#endif
+#ifdef __MIPSEL__
+#define ELFDATAM        ELFDATA2LSB
+#endif
 
 #elif defined(__i386__)
 
@@ -337,9 +405,10 @@ static const int MODUTILS_OBJ_H = 1;
 
 #define SHT_RELM       SHT_REL
 #define Elf32_RelM     Elf32_Rel
+#define ELFDATAM       ELFDATA2LSB
 
 #else
-#error insmod.c no platform specified
+#error Sorry, but insmod.c does not yet support this architecture...
 #endif
 
 #ifndef ElfW
@@ -519,10 +588,6 @@ int arch_init_module (struct obj_file *f, struct new_module *);
 #define _PATH_MODULES  "/lib/modules"
 static const int 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
-
 /*======================================================================*/
 
 int flag_force_load = 0;
@@ -541,8 +606,10 @@ int flag_export = 1;
 
 /* Done ;-) */
 
-#if defined(__arm__)
-struct arm_plt_entry
+
+
+#if defined(BB_USE_PLT_ENTRIES)
+struct arch_plt_entry
 {
   int offset;
   int allocated:1;
@@ -550,26 +617,44 @@ struct arm_plt_entry
 };
 #endif
 
+#if defined(BB_USE_GOT_ENTRIES)
 struct arch_got_entry {
        int offset;
        unsigned offset_done:1;
        unsigned reloc_done:1;
 };
+#endif
+
+#if defined(__mips__)
+struct mips_hi16
+{
+  struct mips_hi16 *next;
+  Elf32_Addr *addr;
+  Elf32_Addr value;
+};
+#endif
 
 struct arch_file {
        struct obj_file root;
-#if defined(__arm__)
-    struct obj_section *plt;
+#if defined(BB_USE_PLT_ENTRIES)
+       struct obj_section *plt;
 #endif
+#if defined(BB_USE_GOT_ENTRIES)
        struct obj_section *got;
+#endif
+#if defined(__mips__)
+       struct mips_hi16 *mips_hi16_list;
+#endif
 };
 
 struct arch_symbol {
        struct obj_symbol root;
-#if defined(__arm__)
-    struct arm_plt_entry pltent;
+#if defined(BB_USE_PLT_ENTRIES)
+       struct arch_plt_entry pltent;
 #endif
+#if defined(BB_USE_GOT_ENTRIES)
        struct arch_got_entry gotent;
+#endif
 };
 
 
@@ -615,11 +700,12 @@ extern int delete_module(const char *);
 
    -- Bryan Rittmeyer <bryan@ixiacom.com>                    */
 
-#ifdef BB_FEATURE_INSMOD_OLD_KERNEL
+#ifdef BB_FEATURE_OLD_MODULE_INTERFACE
 _syscall1(int, get_kernel_syms, struct old_kernel_sym *, ks)
 #endif
 
-#if defined(__i386__) || defined(__m68k__) || defined(__arm__)
+#if defined(__i386__) || defined(__m68k__) || defined(__arm__) \
+ || defined(__powerpc__)
 /* Jump through hoops to fixup error return codes */
 #define __NR__create_module  __NR_create_module
 static inline _syscall2(long, _create_module, const char *, name, size_t,
@@ -637,8 +723,8 @@ unsigned long create_module(const char *name, size_t size)
 #else
 _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 char m_filename[BUFSIZ + 1];
+static char m_fullName[BUFSIZ + 1];
 
 /*======================================================================*/
 
@@ -674,7 +760,17 @@ struct obj_file *arch_new_file(void)
 {
        struct arch_file *f;
        f = xmalloc(sizeof(*f));
+
+#if defined(BB_USE_PLT_ENTRIES)
+       f->plt = NULL;
+#endif
+#if defined(BB_USE_GOT_ENTRIES)
        f->got = NULL;
+#endif
+#if defined(__mips__)
+       f->mips_hi16_list = NULL;
+#endif
+
        return &f->root;
 }
 
@@ -687,7 +783,14 @@ struct obj_symbol *arch_new_symbol(void)
 {
        struct arch_symbol *sym;
        sym = xmalloc(sizeof(*sym));
+
+#if defined(BB_USE_PLT_ENTRIES)
+       memset(&sym->pltent, 0, sizeof(sym->pltent));
+#endif
+#if defined(BB_USE_GOT_ENTRIES)
        memset(&sym->gotent, 0, sizeof(sym->gotent));
+#endif
+
        return &sym->root;
 }
 
@@ -699,18 +802,20 @@ arch_apply_relocation(struct obj_file *f,
                                      ElfW(RelM) *rel, ElfW(Addr) v)
 {
        struct arch_file *ifile = (struct arch_file *) f;
+#if !(defined(__mips__))
        struct arch_symbol *isym = (struct arch_symbol *) sym;
+#endif
 
        ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
        ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
+#if defined(BB_USE_GOT_ENTRIES)
        ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
-#if defined(__arm__)
+#endif
+#if defined(BB_USE_PLT_ENTRIES)
        ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
-
-       struct arm_plt_entry *pe;
+       struct arch_plt_entry *pe;
        unsigned long *ip;
 #endif
-
        enum obj_reloc ret = obj_reloc_ok;
 
        switch (ELF32_R_TYPE(rel->r_info)) {
@@ -724,6 +829,10 @@ arch_apply_relocation(struct obj_file *f,
        case R_ARM_NONE:
 #elif defined(__i386__)
        case R_386_NONE:
+#elif defined(__powerpc__)
+       case R_PPC_NONE:
+#elif defined(__mips__)
+       case R_MIPS_NONE:
 #endif
                break;
 
@@ -732,11 +841,109 @@ arch_apply_relocation(struct obj_file *f,
 #elif defined(__arm__)
        case R_ARM_ABS32:
 #elif defined(__i386__)
-       case R_386_32:
+       case R_386_32:  
+#elif defined(__powerpc__)
+       case R_PPC_ADDR32:
+#elif defined(__mips__)
+       case R_MIPS_32:
 #endif
                *loc += v;
                break;
 
+#if defined(__powerpc__)
+       case R_PPC_ADDR16_HA:
+               *(unsigned short *)loc = (v + 0x8000) >> 16;
+               break;
+
+       case R_PPC_ADDR16_HI:
+               *(unsigned short *)loc = v >> 16;
+               break;
+
+       case R_PPC_ADDR16_LO:
+               *(unsigned short *)loc = v;
+               break;
+#endif
+
+#if defined(__mips__)
+       case R_MIPS_26:
+               if (v % 4)
+                       ret = obj_reloc_dangerous;
+               if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
+                       ret = obj_reloc_overflow;
+               *loc =
+                   (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
+                                           0x03ffffff);
+               break;
+
+       case R_MIPS_HI16:
+               {
+                       struct mips_hi16 *n;
+
+                       /* We cannot relocate this one now because we don't know the value
+                          of the carry we need to add.  Save the information, and let LO16
+                          do the actual relocation.  */
+                       n = (struct mips_hi16 *) xmalloc(sizeof *n);
+                       n->addr = loc;
+                       n->value = v;
+                       n->next = ifile->mips_hi16_list;
+                       ifile->mips_hi16_list = n;
+                       break;
+               }
+
+       case R_MIPS_LO16:
+               {
+                       unsigned long insnlo = *loc;
+                       Elf32_Addr val, vallo;
+
+                       /* Sign extend the addend we extract from the lo insn.  */
+                       vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
+
+                       if (ifile->mips_hi16_list != NULL) {
+                               struct mips_hi16 *l;
+
+                               l = ifile->mips_hi16_list;
+                               while (l != NULL) {
+                                       struct mips_hi16 *next;
+                                       unsigned long insn;
+
+                                       /* The value for the HI16 had best be the same. */
+                                       assert(v == l->value);
+
+                                       /* Do the HI16 relocation.  Note that we actually don't
+                                          need to know anything about the LO16 itself, except where
+                                          to find the low 16 bits of the addend needed by the LO16.  */
+                                       insn = *l->addr;
+                                       val =
+                                           ((insn & 0xffff) << 16) +
+                                           vallo;
+                                       val += v;
+
+                                       /* Account for the sign extension that will happen in the
+                                          low bits.  */
+                                       val =
+                                           ((val >> 16) +
+                                            ((val & 0x8000) !=
+                                             0)) & 0xffff;
+
+                                       insn = (insn & ~0xffff) | val;
+                                       *l->addr = insn;
+
+                                       next = l->next;
+                                       free(l);
+                                       l = next;
+                               }
+
+                               ifile->mips_hi16_list = NULL;
+                       }
+
+                       /* Ok, we're done with the HI16 relocs.  Now deal with the LO16.  */
+                       val = v + vallo;
+                       insnlo = (insnlo & ~0xffff) | (val & 0xffff);
+                       *loc = insnlo;
+                       break;
+               }
+#endif
+
 #if defined(__arm__)
 #elif defined(__sh__)
         case R_SH_REL32:
@@ -747,22 +954,48 @@ arch_apply_relocation(struct obj_file *f,
        case R_386_PC32:
                *loc += v - dot;
                break;
+#elif defined(__powerpc__)
+       case R_PPC_REL32:
+               *loc = v - dot;
+               break;
 #endif
 
 #if defined(__sh__)
         case R_SH_PLT32:
                 *loc = v - dot;
                 break;
-#elif defined(__arm__)
+#elif defined(__i386__)
+#endif
+
+#if defined(BB_USE_PLT_ENTRIES)
+
+#if defined(__arm__)
     case R_ARM_PC24:
     case R_ARM_PLT32:
+#endif
+#if defined(__powerpc__)
+       case R_PPC_REL24:
+#endif
       /* find the plt entry and initialize it if necessary */
       assert(isym != NULL);
-      pe = (struct arm_plt_entry*) &isym->pltent;
+
+      pe = (struct arch_plt_entry*) &isym->pltent;
+
       if (! pe->inited) {
                ip = (unsigned long *) (ifile->plt->contents + pe->offset);
+
+               /* generate some machine code */
+
+#if defined(__arm__)
                ip[0] = 0xe51ff004;                     /* ldr pc,[pc,#-4] */
                ip[1] = v;                              /* sym@ */
+#endif
+#if defined(__powerpc__)
+         ip[0] = 0x3d600000 + ((v + 0x8000) >> 16);  /* lis r11,sym@ha */
+         ip[1] = 0x396b0000 + (v & 0xffff);          /* addi r11,r11,sym@l */
+         ip[2] = 0x7d6903a6;                         /* mtctr r11 */
+         ip[3] = 0x4e800420;                         /* bctr */
+#endif
                pe->inited = 1;
          }
 
@@ -776,15 +1009,18 @@ arch_apply_relocation(struct obj_file *f,
       if (v & 3)
            ret = obj_reloc_dangerous;
 
+      /* merge the offset into the instruction. */
+#if defined(__arm__)
       /* Convert to words. */
       v >>= 2;
 
-      /* merge the offset into the instruction. */
       *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
-      break;
-#elif defined(__i386__)
 #endif
-
+#if defined(__powerpc__)
+      *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
+#endif
+      break;
+#endif /* BB_USE_PLT_ENTRIES */
 
 #if defined(__arm__)
 #elif defined(__sh__)
@@ -810,6 +1046,8 @@ arch_apply_relocation(struct obj_file *f,
                break;
 #endif
 
+#if defined(BB_USE_GOT_ENTRIES)
+
 #if defined(__sh__)
         case R_SH_GOTPC:
 #elif defined(__arm__)
@@ -828,7 +1066,7 @@ arch_apply_relocation(struct obj_file *f,
 #if defined(__sh__)
        case R_SH_GOT32:
 #elif defined(__arm__)
-    case R_ARM_GOT32:
+       case R_ARM_GOT32:
 #elif defined(__i386__)
        case R_386_GOT32:
 #endif
@@ -850,7 +1088,7 @@ arch_apply_relocation(struct obj_file *f,
 #if defined(__sh__)
        case R_SH_GOTOFF:
 #elif defined(__arm__)
-    case R_ARM_GOTOFF:
+       case R_ARM_GOTOFF:
 #elif defined(__i386__)
        case R_386_GOTOFF:
 #endif
@@ -858,6 +1096,8 @@ arch_apply_relocation(struct obj_file *f,
                *loc += v - got;
                break;
 
+#endif /* BB_USE_GOT_ENTRIES */
+
        default:
         printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
                ret = obj_reloc_unhandled;
@@ -869,9 +1109,13 @@ arch_apply_relocation(struct obj_file *f,
 
 int arch_create_got(struct obj_file *f)
 {
+#if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
        struct arch_file *ifile = (struct arch_file *) f;
-       int i, got_offset = 0, gotneeded = 0;
-#if defined(__arm__)
+       int i;
+#if defined(BB_USE_GOT_ENTRIES)
+       int got_offset = 0, gotneeded = 0;
+#endif
+#if defined(BB_USE_PLT_ENTRIES)
        int plt_offset = 0, pltneeded = 0;
 #endif
     struct obj_section *relsec, *symsec, *strsec;
@@ -899,12 +1143,20 @@ int arch_create_got(struct obj_file *f)
                        switch (ELF32_R_TYPE(rel->r_info)) {
 #if defined(__arm__)
                        case R_ARM_GOT32:
+                               break;
 #elif defined(__sh__)
                        case R_SH_GOT32:
+                               break;
 #elif defined(__i386__)
                        case R_386_GOT32:
+                               break;
 #endif
+
+#if defined(__powerpc__)
+                       case R_PPC_REL24:
+                               pltneeded = 1;
                                break;
+#endif
 
 #if defined(__arm__)
                        case R_ARM_PC24:
@@ -937,17 +1189,18 @@ int arch_create_got(struct obj_file *f)
                                name = f->sections[extsym->st_shndx]->name;
                        }
                        intsym = (struct arch_symbol *) obj_find_symbol(f, name);
-
+#if defined(BB_USE_GOT_ENTRIES)
                        if (!intsym->gotent.offset_done) {
                                intsym->gotent.offset_done = 1;
                                intsym->gotent.offset = got_offset;
-                               got_offset += 4;
+                               got_offset += BB_GOT_ENTRY_SIZE;
                        }
-#if defined(__arm__)
+#endif
+#if defined(BB_USE_PLT_ENTRIES)
                        if (pltneeded && intsym->pltent.allocated == 0) {
                                intsym->pltent.allocated = 1;
                                intsym->pltent.offset = plt_offset;
-                               plt_offset += 8;
+                               plt_offset += BB_PLT_ENTRY_SIZE;
                                intsym->pltent.inited = 0;
                                pltneeded = 0;
                        }
@@ -955,27 +1208,30 @@ int arch_create_got(struct obj_file *f)
                        }
                }
 
-#if defined(__arm__)
+#if defined(BB_USE_GOT_ENTRIES)
        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);
+                       relsec = obj_create_alloced_section(f, ".got", 
+                                                           BB_GOT_ENTRY_SIZE,
+                                                           got_offset);
                        assert(relsec);
                }
 
                ifile->got = relsec;
        }
+#endif
 
+#if defined(BB_USE_PLT_ENTRIES)
        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);
+               ifile->plt = obj_create_alloced_section(f, ".plt", 
+                                                       BB_PLT_ENTRY_SIZE, 
+                                                       plt_offset);
+#endif
 #endif
-
        return 1;
 }
 
@@ -1157,7 +1413,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)
-                                       error_msg("%s multiply defined\n", name);
+                                       error_msg("%s multiply defined", name);
                                return sym;
                        }
                }
@@ -1420,7 +1676,7 @@ old_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) {
-                       error_msg("symbol for parameter %s not found\n", p);
+                       error_msg("symbol for parameter %s not found", p);
                        return 0;
                }
 
@@ -1433,7 +1689,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') {
-                                       error_msg("improperly terminated string argument for %s\n", p);
+                                       error_msg("improperly terminated string argument for %s", p);
                                        return 0;
                                } else if (*q == '\\')
                                        switch (*++q) {
@@ -1549,7 +1805,7 @@ 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.  */
 
@@ -1757,7 +2013,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 */
 
 
 
@@ -1787,7 +2043,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
                p = get_modinfo_value(f, key);
                key += 5;
                if (p == NULL) {
-                       error_msg("invalid parameter %s\n", key);
+                       error_msg("invalid parameter %s", key);
                        return 0;
                }
 
@@ -1795,7 +2051,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) {
-                       error_msg("symbol for parameter %s not found\n", key);
+                       error_msg("symbol for parameter %s not found", key);
                        return 0;
                }
 
@@ -1823,7 +2079,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') {
-                                                       error_msg("improperly terminated string argument for %s\n",
+                                                       error_msg("improperly terminated string argument for %s",
                                                                        key);
                                                        return 0;
                                                } else if (*q == '\\')
@@ -1918,14 +2174,14 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
                                        /* Probably we should do that outside the loop ? */
                                        if (!isdigit(*(p + 1))) {
                                                error_msg("parameter type 'c' for %s must be followed by"
-                                                               " the maximum size\n", key);
+                                                               " the maximum size", key);
                                                return 0;
                                        }
                                        charssize = strtoul(p + 1, (char **) NULL, 10);
 
                                        /* Check length */
                                        if (strlen(str) >= charssize) {
-                                               error_msg("string too long for %s (max %ld)\n", key,
+                                               error_msg("string too long for %s (max %ld)", key,
                                                                charssize - 1);
                                                return 0;
                                        }
@@ -1954,7 +2210,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
                                        break;
 
                                default:
-                                       error_msg("unknown parameter type '%c' for %s\n", *p, key);
+                                       error_msg("unknown parameter type '%c' for %s", *p, key);
                                        return 0;
                                }
                        }
@@ -1973,21 +2229,21 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
 
                        case ',':
                                if (++n > max) {
-                                       error_msg("too many values for %s (max %d)\n", key, max);
+                                       error_msg("too many values for %s (max %d)", key, max);
                                        return 0;
                                }
                                ++q;
                                break;
 
                        default:
-                               error_msg("invalid argument syntax for %s\n", key);
+                               error_msg("invalid argument syntax for %s", key);
                                return 0;
                        }
                }
 
          end_of_arg:
                if (n < min) {
-                       error_msg("too few values for %s (min %d)\n", key, min);
+                       error_msg("too few values for %s (min %d)", key, min);
                        return 0;
                }
 
@@ -2036,7 +2292,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.  */
 
@@ -2061,48 +2317,50 @@ static int new_get_kernel_symbols(void)
        }
 
        n_ext_modules = nmod = ret;
-       ext_modules = modules = xmalloc(nmod * sizeof(*modules));
-       memset(modules, 0, nmod * sizeof(*modules));
 
        /* Collect the modules' symbols.  */
 
-       for (i = 0, mn = module_names, m = modules;
-                i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
-               struct new_module_info info;
-
-               if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
-                       if (errno == ENOENT) {
-                               /* The module was removed out from underneath us.  */
-                               continue;
-                       }
-                       perror_msg("query_module: QM_INFO: %s", mn);
-                       return 0;
-               }
-
-               syms = xmalloc(bufsize = 1024);
-         retry_mod_sym_load:
-               if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
-                       switch (errno) {
-                       case ENOSPC:
-                               syms = xrealloc(syms, bufsize = ret);
-                               goto retry_mod_sym_load;
-                       case ENOENT:
-                               /* The module was removed out from underneath us.  */
-                               continue;
-                       default:
-                               perror_msg("query_module: QM_SYMBOLS: %s", mn);
+       if (nmod){
+               ext_modules = modules = xmalloc(nmod * sizeof(*modules));
+               memset(modules, 0, nmod * sizeof(*modules));
+               for (i = 0, mn = module_names, m = modules;
+                        i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
+                       struct new_module_info info;
+       
+                       if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
+                               if (errno == ENOENT) {
+                                       /* The module was removed out from underneath us.  */
+                                       continue;
+                               }
+                               perror_msg("query_module: QM_INFO: %s", mn);
                                return 0;
                        }
-               }
-               nsyms = ret;
-
-               m->name = mn;
-               m->addr = info.addr;
-               m->nsyms = nsyms;
-               m->syms = syms;
-
-               for (j = 0, s = syms; j < nsyms; ++j, ++s) {
-                       s->name += (unsigned long) syms;
+       
+                       syms = xmalloc(bufsize = 1024);
+                 retry_mod_sym_load:
+                       if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
+                               switch (errno) {
+                               case ENOSPC:
+                                       syms = xrealloc(syms, bufsize = ret);
+                                       goto retry_mod_sym_load;
+                               case ENOENT:
+                                       /* The module was removed out from underneath us.  */
+                                       continue;
+                               default:
+                                       perror_msg("query_module: QM_SYMBOLS: %s", mn);
+                                       return 0;
+                               }
+                       }
+                       nsyms = ret;
+       
+                       m->name = mn;
+                       m->addr = info.addr;
+                       m->nsyms = nsyms;
+                       m->syms = syms;
+       
+                       for (j = 0, s = syms; j < nsyms; ++j, ++s) {
+                               s->name += (unsigned long) syms;
+                       }
                }
        }
 
@@ -2310,7 +2568,7 @@ new_init_module(const char *m_name, struct obj_file *f,
 #define new_create_module_ksymtab(x)
 #define query_module(v, w, x, y, z) -1
 
-#endif                                                 /* BB_FEATURE_INSMOD_NEW_KERNEL */
+#endif                                                 /* BB_FEATURE_NEW_MODULE_INTERFACE */
 
 
 /*======================================================================*/
@@ -2373,7 +2631,7 @@ int obj_check_undefineds(struct obj_file *f)
                                        sym->secidx = SHN_ABS;
                                        sym->value = 0;
                                } else {
-                                       error_msg("unresolved symbol %s\n", sym->name);
+                                       error_msg("unresolved symbol %s", sym->name);
                                        ret = 0;
                                }
                        }
@@ -2600,11 +2858,11 @@ int obj_relocate(struct obj_file *f, ElfW(Addr) base)
                                errmsg = "Unhandled relocation";
                          bad_reloc:
                                if (extsym) {
-                                       error_msg("%s of type %ld for %s\n", errmsg,
+                                       error_msg("%s of type %ld for %s", errmsg,
                                                        (long) ELFW(R_TYPE) (rel->r_info),
                                                        strtab + extsym->st_name);
                                } else {
-                                       error_msg("%s of type %ld\n", errmsg,
+                                       error_msg("%s of type %ld", errmsg,
                                                        (long) ELFW(R_TYPE) (rel->r_info));
                                }
                                ret = 0;
@@ -2650,7 +2908,7 @@ int obj_create_image(struct obj_file *f, char *image)
        for (sec = f->load_order; sec; sec = sec->load_next) {
                char *secimg;
 
-               if (sec->header.sh_size == 0)
+               if (sec->contents == 0 || sec->header.sh_size == 0)
                        continue;
 
                secimg = image + (sec->header.sh_addr - base);
@@ -2689,25 +2947,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) {
-               error_msg("not an ELF file\n");
+               error_msg("not an ELF file");
                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)) {
-               error_msg("ELF file not for this architecture\n");
+               error_msg("ELF file not for this architecture");
                return NULL;
        }
        if (f->header.e_type != ET_REL) {
-               error_msg("ELF file not a relocatable object\n");
+               error_msg("ELF file not a relocatable object");
                return NULL;
        }
 
        /* Read the section headers.  */
 
        if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
-               error_msg("section header size mismatch: %lu != %lu\n",
+               error_msg("section header size mismatch: %lu != %lu",
                                (unsigned long) f->header.e_shentsize,
                                (unsigned long) sizeof(ElfW(Shdr)));
                return NULL;
@@ -2735,7 +2993,7 @@ struct obj_file *obj_load(FILE * fp)
                sec->header = section_headers[i];
                sec->idx = i;
 
-               switch (sec->header.sh_type) {
+               if(sec->header.sh_size) switch (sec->header.sh_type) {
                case SHT_NULL:
                case SHT_NOTE:
                case SHT_NOBITS:
@@ -2760,11 +3018,11 @@ struct obj_file *obj_load(FILE * fp)
 
 #if SHT_RELM == SHT_REL
                case SHT_RELA:
-                       error_msg("RELA relocations not supported on this architecture\n");
+                       error_msg("RELA relocations not supported on this architecture");
                        return NULL;
 #else
                case SHT_REL:
-                       error_msg("REL relocations not supported on this architecture\n");
+                       error_msg("REL relocations not supported on this architecture");
                        return NULL;
 #endif
 
@@ -2777,7 +3035,7 @@ struct obj_file *obj_load(FILE * fp)
                                break;
                        }
 
-                       error_msg("can't handle sections of type %ld\n",
+                       error_msg("can't handle sections of type %ld",
                                        (long) sec->header.sh_type);
                        return NULL;
                }
@@ -2806,7 +3064,7 @@ struct obj_file *obj_load(FILE * fp)
                                ElfW(Sym) * sym;
 
                                if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
-                                       error_msg("symbol size mismatch: %lu != %lu\n",
+                                       error_msg("symbol size mismatch: %lu != %lu",
                                                        (unsigned long) sec->header.sh_entsize,
                                                        (unsigned long) sizeof(ElfW(Sym)));
                                        return NULL;
@@ -2838,7 +3096,7 @@ struct obj_file *obj_load(FILE * fp)
 
                case SHT_RELM:
                        if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
-                               error_msg("relocation entry size mismatch: %lu != %lu\n",
+                               error_msg("relocation entry size mismatch: %lu != %lu",
                                                (unsigned long) sec->header.sh_entsize,
                                                (unsigned long) sizeof(ElfW(RelM)));
                                return NULL;
@@ -2881,6 +3139,7 @@ extern int insmod_main( int argc, char **argv)
        ElfW(Addr) m_addr;
        FILE *fp;
        struct obj_file *f;
+       struct stat st;
        char m_name[BUFSIZ + 1] = "\0";
        int exit_status = EXIT_FAILURE;
        int m_has_modinfo;
@@ -2893,7 +3152,7 @@ extern int insmod_main( int argc, char **argv)
 #endif
 
        /* Parse any options */
-       while ((opt = getopt(argc, argv, "fkvxL")) > 0) {
+       while ((opt = getopt(argc, argv, "fkvxLo:")) > 0) {
                switch (opt) {
                        case 'f':                       /* force loading */
                                flag_force_load = 1;
@@ -2907,6 +3166,9 @@ extern int insmod_main( int argc, char **argv)
                        case 'x':                       /* do not export externs */
                                flag_export = 0;
                                break;
+                       case 'o':                       /* name the output module */
+                               strncpy(m_name, optarg, BUFSIZ);
+                               break;
                        case 'L':                       /* Stub warning */
                                /* This is needed for compatibility with modprobe.
                                 * In theory, this does locking, but we don't do
@@ -2914,12 +3176,12 @@ extern int insmod_main( int argc, char **argv)
                                 * loading the same module 50 times concurrently. */
                                break;
                        default:
-                               usage(insmod_usage);
+                               show_usage();
                }
        }
        
        if (argv[optind] == NULL) {
-               usage(insmod_usage);
+               show_usage();
        }
 
        /* Grab the module name */
@@ -2932,12 +3194,15 @@ extern int insmod_main( int argc, char **argv)
 
        if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
                len -= 2;
-       memcpy(m_name, tmp, len);
-       strcpy(m_fullName, m_name);
+       strncpy(m_fullName, tmp, len);
+       if (*m_name == '\0') {
+               strcpy(m_name, m_fullName);
+       }
        strcat(m_fullName, ".o");
 
        /* Get a filedesc for the module */
-       if ((fp = fopen(argv[optind], "r")) == NULL) {
+       if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
+                       (fp = fopen(argv[optind], "r")) == NULL) {
                /* Hmpf.  Could not open it. Search through _PATH_MODULES to find a module named m_name */
                if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE,
                                                        findNamedModule, 0, m_fullName) == FALSE) 
@@ -2945,11 +3210,11 @@ extern int insmod_main( int argc, char **argv)
                        if (m_filename[0] == '\0'
                                || ((fp = fopen(m_filename, "r")) == NULL)) 
                        {
-                               error_msg("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
+                               error_msg("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
                                return EXIT_FAILURE;
                        }
                } else
-                       error_msg_and_die("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
+                       error_msg_and_die("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
        } else
                memcpy(m_filename, argv[optind], strlen(argv[optind]));
 
@@ -2972,7 +3237,7 @@ extern int insmod_main( int argc, char **argv)
                m_version = old_get_module_version(f, m_strversion);
                if (m_version == -1) {
                        error_msg("couldn't find the kernel version the module was "
-                                       "compiled for\n");
+                                       "compiled for");
                        goto out;
                }
        }
@@ -2981,12 +3246,12 @@ extern int insmod_main( int argc, char **argv)
                if (flag_force_load) {
                        error_msg("Warning: kernel-module version mismatch\n"
                                        "\t%s was compiled for kernel version %s\n"
-                                       "\twhile this kernel is version %s\n",
+                                       "\twhile this kernel is version %s",
                                        m_filename, m_strversion, k_strversion);
                } else {
                        error_msg("kernel-module version mismatch\n"
                                        "\t%s was compiled for kernel version %s\n"
-                                       "\twhile this kernel is version %s.\n",
+                                       "\twhile this kernel is version %s.",
                                        m_filename, m_strversion, k_strversion);
                        goto out;
                }
@@ -2997,21 +3262,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
-               error_msg("Not configured to support new kernels\n");
+               error_msg("Not configured to support new kernels");
                goto out;
 #endif
        } else {
-#ifdef BB_FEATURE_INSMOD_OLD_KERNEL
+#ifdef BB_FEATURE_OLD_MODULE_INTERFACE
                if (!old_get_kernel_symbols(m_name))
                        goto out;
                k_crcs = old_is_kernel_checksummed();
 #else
-               error_msg("Not configured to support old kernels\n");
+               error_msg("Not configured to support old kernels");
                goto out;
 #endif
        }
@@ -3043,6 +3308,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) 
@@ -3062,16 +3330,13 @@ 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:
-               error_msg("A module named %s already exists\n", m_name);
+               error_msg("A module named %s already exists", m_name);
                goto out;
        case ENOMEM:
-               error_msg("Can't allocate kernel memory for module; needed %lu bytes\n",
+               error_msg("Can't allocate kernel memory for module; needed %lu bytes",
                                m_size);
                goto out;
        default: