multibuild.pl saves us again. unix2dos requires that dos2unix
[oweals/busybox.git] / insmod.c
index 136f9f06205208af3a36eb700e985e95e31a55c1..1a63ecb2ac0cb7a2bf417a65ec8cbe31eeb47ca3 100644 (file)
--- a/insmod.c
+++ b/insmod.c
@@ -2,16 +2,33 @@
 /*
  * Mini insmod implementation for busybox
  *
- * Copyright (C) 1999,2000 by Lineo, inc.
+ * This version of insmod supports x86, ARM, SH3/4, powerpc, m68k, 
+ * and MIPS.
+ *
+ *
+ * Copyright (C) 1999,2000,2001 by Lineo, inc.
  * Written by Erik Andersen <andersen@lineo.com>
  * and Ron Alder <alder@lineo.com>
  *
  * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
- * and (theoretically) SH3. Note that there is still no true
- * multiple architecture support. You just get SH3|SH4|i386, despite
- * the mention of ARM and m68k--which may or may not work (but
- * almost certainly do not, due to at least MATCH_MACHINE). I have
- * only tested SH4 in little endian mode.
+ * and (theoretically) SH3. I have only tested SH4 in little endian mode.
+ *
+ * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
+ * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI.  Only
+ * 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.
@@ -36,7 +53,6 @@
  *
  */
 
-#include "internal.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 "busybox.h"
+
+#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
+# define new_sys_init_module   init_module
+#else
+# define old_sys_init_module   init_module
+#endif
+
+#ifdef BB_FEATURE_INSMOD_LOADINKMEM
+#define LOADBITS 0     
+#else
+#define LOADBITS 1
+#endif
+
+#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
-#define MODUTILS_MODULE_H 1
+static const int MODUTILS_MODULE_H = 1;
 
-#ident "$Id: insmod.c,v 1.24 2000/09/24 03:44:29 andersen Exp $"
+#ident "$Id: insmod.c,v 1.70 2001/07/31 22:51:49 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
@@ -134,7 +190,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,
@@ -157,9 +213,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
 
@@ -210,8 +266,19 @@ struct new_module
   unsigned tgt_long persist_end;
   unsigned tgt_long can_unload;
   unsigned tgt_long runsize;
+#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
+  const char *kallsyms_start;     /* All symbols for kernel debugging */
+  const char *kallsyms_end;
+  const char *archdata_start;     /* arch specific data for module */
+  const char *archdata_end;
+  const char *kernel_data;        /* Reserved for kernel internal use */
+#endif
 };
 
+#define ARCHDATA_SEC_NAME "__archdata"
+#define KALLSYMS_SEC_NAME "__kallsyms"
+
+
 struct new_module_info
 {
   unsigned long addr;
@@ -221,11 +288,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,
@@ -233,11 +300,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.  */
@@ -281,9 +348,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.24 2000/09/24 03:44:29 andersen Exp $"
+#ident "$Id: insmod.c,v 1.70 2001/07/31 22:51:49 andersen Exp $"
 
 /* The relocatable object is manipulated using elfin types.  */
 
@@ -307,7 +374,10 @@ int delete_module(const char *);
 #endif
 
 #define ELFCLASSM      ELFCLASS32
-#define ELFDATAM       ELFDATA2LSB
+
+#if (defined(__mc68000__))                                     
+#define ELFDATAM       ELFDATA2MSB
+#endif
 
 
 
@@ -316,17 +386,66 @@ int delete_module(const char *);
 #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
 
-/* presumably we can use these for anything but the SH */
+#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
+#define ELFDATAM       ELFDATA2LSB
+
+#elif defined(__mc68000__) 
+
+#define MATCH_MACHINE(x)       (x == EM_68K)
+#define SHT_RELM                       SHT_RELA
+#define Elf32_RelM                     Elf32_Rela
 
+#else
+#error Sorry, but insmod.c does not yet support this architecture...
 #endif
 
 #ifndef ElfW
@@ -421,78 +540,75 @@ struct obj_symbol_patch
 
 /* Generic object manipulation routines.  */
 
-unsigned long obj_elf_hash(const char *);
-
-unsigned long obj_elf_hash_n(const char *, unsigned long len);
+static unsigned long obj_elf_hash(const char *);
 
-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);
+static unsigned long obj_elf_hash_n(const char *, unsigned long len);
 
-struct obj_symbol *obj_find_symbol (struct obj_file *f,
+static struct obj_symbol *obj_find_symbol (struct obj_file *f,
                                         const char *name);
 
-ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
+static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
                                  struct obj_symbol *sym);
 
-void obj_set_symbol_compare(struct obj_file *f,
+#ifdef BB_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 *));
+#endif
 
-struct obj_section *obj_find_section (struct obj_file *f,
+static struct obj_section *obj_find_section (struct obj_file *f,
                                           const char *name);
 
-void obj_insert_section_load_order (struct obj_file *f,
+static void obj_insert_section_load_order (struct obj_file *f,
                                    struct obj_section *sec);
 
-struct obj_section *obj_create_alloced_section (struct obj_file *f,
+static struct obj_section *obj_create_alloced_section (struct obj_file *f,
                                                const char *name,
                                                unsigned long align,
                                                unsigned long size);
 
-struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
+static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
                                                      const char *name,
                                                      unsigned long align,
                                                      unsigned long size);
 
-void *obj_extend_section (struct obj_section *sec, unsigned long more);
+static void *obj_extend_section (struct obj_section *sec, unsigned long more);
 
-int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
+static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
                     const char *string);
 
-int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
+static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
                     struct obj_symbol *sym);
 
-int obj_check_undefineds(struct obj_file *f);
+static int obj_check_undefineds(struct obj_file *f);
 
-void obj_allocate_commons(struct obj_file *f);
+static void obj_allocate_commons(struct obj_file *f);
 
-unsigned long obj_load_size (struct obj_file *f);
+static unsigned long obj_load_size (struct obj_file *f);
 
-int obj_relocate (struct obj_file *f, ElfW(Addr) base);
+static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
 
-struct obj_file *obj_load(FILE *f);
+static struct obj_file *obj_load(FILE *f, int loadprogbits);
 
-int obj_create_image (struct obj_file *f, char *image);
+static int obj_create_image (struct obj_file *f, char *image);
 
 /* Architecture specific manipulation routines.  */
 
-struct obj_file *arch_new_file (void);
+static struct obj_file *arch_new_file (void);
 
-struct obj_section *arch_new_section (void);
+static struct obj_section *arch_new_section (void);
 
-struct obj_symbol *arch_new_symbol (void);
+static struct obj_symbol *arch_new_symbol (void);
 
-enum obj_reloc arch_apply_relocation (struct obj_file *f,
+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);
 
-int arch_create_got (struct obj_file *f);
+static int arch_create_got (struct obj_file *f);
 
-struct new_module;
-int arch_init_module (struct obj_file *f, struct new_module *);
+static int arch_init_module (struct obj_file *f, struct new_module *);
 
 #endif /* obj.h */
 //----------------------------------------------------------------------------
@@ -504,18 +620,14 @@ 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;
 
 /*======================================================================*/
 
-int flag_force_load = 0;
-int flag_autoclean = 0;
-int flag_verbose = 0;
-int flag_export = 1;
+static int flag_force_load = 0;
+static int flag_autoclean = 0;
+static int flag_verbose = 0;
+static int flag_export = 1;
 
 
 /*======================================================================*/
@@ -526,20 +638,57 @@ int flag_export = 1;
    and we can't support anything else right now anyway. In the
    future maybe they should be #if defined'd */
 
+/* Done ;-) */
+
+
+
+#if defined(BB_USE_PLT_ENTRIES)
+struct arch_plt_entry
+{
+  int offset;
+  int allocated:1;
+  int inited:1;                /* has been set up */
+};
+#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(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(BB_USE_PLT_ENTRIES)
+       struct arch_plt_entry pltent;
+#endif
+#if defined(BB_USE_GOT_ENTRIES)
        struct arch_got_entry gotent;
+#endif
 };
 
 
@@ -551,130 +700,106 @@ struct external_module {
        struct new_module_symbol *syms;
 };
 
-struct new_module_symbol *ksyms;
-size_t nksyms;
-
-struct external_module *ext_modules;
-int n_ext_modules;
-int n_ext_modules_used;
-
+static struct new_module_symbol *ksyms;
+static size_t nksyms;
 
-
-/* Some firendly syscalls to cheer everyone's day...  */
-#define __NR_new_sys_init_module  __NR_init_module
-_syscall2(int, new_sys_init_module, const char *, name,
-                 const struct new_module *, info)
-#define __NR_old_sys_init_module  __NR_init_module
-_syscall5(int, old_sys_init_module, const char *, name, char *, code,
-                 unsigned, codesize, struct old_mod_routines *, routines,
-                 struct old_symbol_table *, symtab)
-#ifndef BB_RMMOD
-_syscall1(int, delete_module, const char *, name)
-#else
+static struct external_module *ext_modules;
+static int n_ext_modules;
+static int n_ext_modules_used;
 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 <bryan@ixiacom.com>                    */
+static char m_filename[FILENAME_MAX + 1];
+static char m_fullName[FILENAME_MAX + 1];
 
-#if defined(__i386__) || defined(__m68k__) || defined(__arm__)
-/* 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,
-                                               size)
-unsigned long create_module(const char *name, size_t size)
-{
-       long ret = _create_module(name, size);
 
-       if (ret == -1 && errno > 125) {
-               ret = -errno;
-               errno = 0;
-       }
-       return ret;
-}
-#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 int findNamedModule(const char *fileName, struct stat *statbuf,
-                                                  void *userDate)
+static int check_module_name_match(const char *filename, struct stat *statbuf,
+                                                  void *userdata)
 {
-       char *fullName = (char *) userDate;
+       char *fullname = (char *) userdata;
 
-
-       if (fullName[0] == '\0')
+       if (fullname[0] == '\0')
                return (FALSE);
        else {
-               char *tmp = strrchr(fileName, '/');
-
-               if (tmp == NULL)
-                       tmp = (char *) fileName;
-               else
-                       tmp++;
-               if (check_wildcard_match(tmp, fullName) == TRUE) {
+               char *tmp, *tmp1 = strdup(filename);
+               tmp = get_last_path_component(tmp1);
+               if (strcmp(tmp, fullname) == 0) {
+                       free(tmp1);
                        /* Stop searching if we find a match */
-                       memcpy(m_filename, fileName, strlen(fileName)+1);
-                       return (FALSE);
+                       safe_strncpy(m_filename, filename, sizeof(m_filename));
+                       return (TRUE);
                }
+               free(tmp1);
        }
-       return (TRUE);
+       return (FALSE);
 }
 
 
 /*======================================================================*/
 
-struct obj_file *arch_new_file(void)
+static 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;
 }
 
-struct obj_section *arch_new_section(void)
+static struct obj_section *arch_new_section(void)
 {
        return xmalloc(sizeof(struct obj_section));
 }
 
-struct obj_symbol *arch_new_symbol(void)
+static 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;
 }
 
-enum obj_reloc
+static enum obj_reloc
 arch_apply_relocation(struct obj_file *f,
                                          struct obj_section *targsec,
                                          struct obj_section *symsec,
                                          struct obj_symbol *sym,
-#if defined(__sh__)
-                                         Elf32_Rela * rel, Elf32_Addr v)
-#else
-                                         Elf32_Rel * rel, Elf32_Addr v)
-#endif
+                                     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
 
-       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;
-
+       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;
+#endif
+#if defined(BB_USE_PLT_ENTRIES)
+       ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
+       struct arch_plt_entry *pe;
+       unsigned long *ip;
+#endif
        enum obj_reloc ret = obj_reloc_ok;
 
        switch (ELF32_R_TYPE(rel->r_info)) {
@@ -684,101 +809,330 @@ arch_apply_relocation(struct obj_file *f,
    and in case that ever changes */
 #if defined(__sh__)
        case R_SH_NONE:
-#else
+#elif defined(__arm__)
+       case R_ARM_NONE:
+#elif defined(__i386__)
        case R_386_NONE:
+#elif defined(__mc68000__) 
+       case R_68K_NONE:
+#elif defined(__powerpc__)
+       case R_PPC_NONE:
+#elif defined(__mips__)
+       case R_MIPS_NONE:
 #endif
                break;
 
 #if defined(__sh__)
        case R_SH_DIR32:
-#else
-       case R_386_32:
+#elif defined(__arm__)
+       case R_ARM_ABS32:
+#elif defined(__i386__)
+       case R_386_32:  
+#elif defined(__mc68000__) 
+       case R_68K_32:
+#elif defined(__powerpc__)
+       case R_PPC_ADDR32:
+#elif defined(__mips__)
+       case R_MIPS_32:
 #endif
                *loc += v;
                break;
+#if defined(__mc68000__)
+    case R_68K_8:
+               if (v > 0xff)
+               ret = obj_reloc_overflow;
+               *(char *)loc = v;
+               break;
+    case R_68K_16:
+               if (v > 0xffff)
+               ret = obj_reloc_overflow;
+               *(short *)loc = v;
+               break;
+#endif /* __mc68000__   */
 
-#if defined(__sh__)
+#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:
-#else
+               *loc += v - dot;
+               break;
+#elif defined(__i386__)
        case R_386_PLT32:
        case R_386_PC32:
-#endif
                *loc += v - dot;
                break;
+#elif defined(__mc68000__)
+    case R_68K_PC8:
+               v -= dot;
+               if ((Elf32_Sword)v > 0x7f || (Elf32_Sword)v < -(Elf32_Sword)0x80)
+               ret = obj_reloc_overflow;
+               *(char *)loc = v;
+    break;
+               case R_68K_PC16:
+               v -= dot;
+               if ((Elf32_Sword)v > 0x7fff || (Elf32_Sword)v < -(Elf32_Sword)0x8000)
+               ret = obj_reloc_overflow;
+               *(short *)loc = v;
+               break;
+    case R_68K_PC32:
+               *(int *)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(__i386__)
 #endif
 
+#if defined(BB_USE_PLT_ENTRIES)
 
-#if defined(__sh__)
+#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 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;
+         }
+
+      /* 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;
+
+      /* merge the offset into the instruction. */
+#if defined(__arm__)
+      /* Convert to words. */
+      v >>= 2;
+
+      *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
+#endif
+#if defined(__powerpc__)
+      *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
+#endif
+      break;
+#endif /* BB_USE_PLT_ENTRIES */
+
+#if defined(__arm__)
+#elif defined(__sh__)
         case R_SH_GLOB_DAT:
         case R_SH_JMP_SLOT:
                        *loc = v;
                 break;
-#else
+#elif defined(__i386__)
        case R_386_GLOB_DAT:
        case R_386_JMP_SLOT:
                *loc = v;
                break;
+#elif defined(__mc68000__)
+       case R_68K_GLOB_DAT:
+       case R_68K_JMP_SLOT:
+               *loc = v;
+               break;
 #endif
 
-#if defined(__sh__)
+#if defined(__arm__)
+#elif defined(__sh__)
         case R_SH_RELATIVE:
                *loc += f->baseaddr + rel->r_addend;
                 break;
-#else
+#elif defined(__i386__)
         case R_386_RELATIVE:
                *loc += f->baseaddr;
                break;
+#elif defined(__mc68000__)
+    case R_68K_RELATIVE:
+       *(int *)loc += f->baseaddr;
+       break;
 #endif
 
+#if defined(BB_USE_GOT_ENTRIES)
+
+#if !defined(__68k__)
 #if defined(__sh__)
         case R_SH_GOTPC:
-               assert(got != 0);
-               *loc += got - dot + rel->r_addend;;
-               break;
-#else
+#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__) || defined(__m68k_)
                *loc += got - dot;
-               break;
 #endif
+               break;
+#endif // __68k__
 
 #if defined(__sh__)
        case R_SH_GOT32:
-               assert(isym != NULL);
-               if (!isym->gotent.reloc_done) {
-                       isym->gotent.reloc_done = 1;
-                       *(Elf32_Addr *) (ifile->got->contents + isym->gotent.offset) =
-                               v;
-               }
-               *loc += isym->gotent.offset + rel->r_addend;
-               break;
-#else
+#elif defined(__arm__)
+       case R_ARM_GOT32:
+#elif defined(__i386__)
        case R_386_GOT32:
+#elif defined(__mc68000__)
+       case R_68K_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__) || defined(__mc68000__)
                *loc += isym->gotent.offset;
-               break;
 #endif
+               break;
 
+    /* address relative to the got */
+#if !defined(__mc68000__)
 #if defined(__sh__)
        case R_SH_GOTOFF:
-#else
+#elif defined(__arm__)
+       case R_ARM_GOTOFF:
+#elif defined(__i386__)
        case R_386_GOTOFF:
+#elif defined(__mc68000__)
+       case R_68K_GOTOFF:
 #endif
                assert(got != 0);
                *loc += v - got;
                break;
+#endif // __mc68000__
+
+#endif /* BB_USE_GOT_ENTRIES */
 
        default:
+        printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
                ret = obj_reloc_unhandled;
                break;
        }
@@ -786,89 +1140,138 @@ arch_apply_relocation(struct obj_file *f,
        return ret;
 }
 
-int arch_create_got(struct obj_file *f)
+static 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, n, offset = 0, gotneeded = 0;
-
-       n = ifile->root.header.e_shnum;
-       for (i = 0; i < n; ++i) {
-               struct obj_section *relsec, *symsec, *strsec;
-#if defined(__sh__)
-               Elf32_Rela *rel, *relend;
-#else
-               Elf32_Rel *rel, *relend;
+       int i;
+#if defined(BB_USE_GOT_ENTRIES)
+       int got_offset = 0, gotneeded = 0;
 #endif
-               Elf32_Sym *symtab;
-               const char *strtab;
+#if defined(BB_USE_PLT_ENTRIES)
+       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];
 
-#if defined(__sh__)
-               rel = (Elf32_Rela *) relsec->contents;
-               relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rela));
-#else
-               rel = (Elf32_Rel *) relsec->contents;
-               relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rel));
-#endif
-               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 arch_symbol *intsym;
-                       const char *name;
+                       extsym = &symtab[ELF32_R_SYM(rel->r_info)];
 
                        switch (ELF32_R_TYPE(rel->r_info)) {
-#if defined(__sh__)
+#if defined(__arm__)
+                       case R_ARM_GOT32:
+                               break;
+#elif defined(__sh__)
+                       case R_SH_GOT32:
+                               break;
+#elif defined(__i386__)
+                       case R_386_GOT32:
+                               break;
+#elif defined(__mc68000__)
+                       case R_68K_GOT32:
+                               break;
+#endif
+
+#if defined(__powerpc__)
+                       case R_PPC_REL24:
+                               pltneeded = 1;
+                               break;
+#endif
+
+#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:
-#else
+                               gotneeded = 1;
+#elif defined(__i386__)
                        case R_386_GOTPC:
                        case R_386_GOTOFF:
-#endif
                                gotneeded = 1;
+#endif
+
                        default:
                                continue;
-
-#if defined(__sh__)
-                       case R_SH_GOT32:
-#else
-                       case R_386_GOT32:
-#endif
-                               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 arch_symbol *) obj_find_symbol(&ifile->root, 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 = offset;
-                               offset += 4;
+                               intsym->gotent.offset = got_offset;
+                               got_offset += BB_GOT_ENTRY_SIZE;
+                       }
+#endif
+#if defined(BB_USE_PLT_ENTRIES)
+                       if (pltneeded && intsym->pltent.allocated == 0) {
+                               intsym->pltent.allocated = 1;
+                               intsym->pltent.offset = plt_offset;
+                               plt_offset += BB_PLT_ENTRY_SIZE;
+                               intsym->pltent.inited = 0;
+                               pltneeded = 0;
+                       }
+#endif
                        }
                }
-       }
 
-       if (offset > 0 || gotneeded)
-               ifile->got =
-                       obj_create_alloced_section(&ifile->root, ".got", 4, offset);
+#if defined(BB_USE_GOT_ENTRIES)
+       if (got_offset) {
+               struct obj_section* myrelsec = obj_find_section(f, ".got");
 
+               if (myrelsec) {
+                       obj_extend_section(myrelsec, got_offset);
+               } else {
+                       myrelsec = obj_create_alloced_section(f, ".got", 
+                                                           BB_GOT_ENTRY_SIZE,
+                                                           got_offset);
+                       assert(myrelsec);
+               }
+
+               ifile->got = myrelsec;
+       }
+#endif
+
+#if defined(BB_USE_PLT_ENTRIES)
+       if (plt_offset)
+               ifile->plt = obj_create_alloced_section(f, ".plt", 
+                                                       BB_PLT_ENTRY_SIZE, 
+                                                       plt_offset);
+#endif
+#endif
        return 1;
 }
 
-int arch_init_module(struct obj_file *f, struct new_module *mod)
+static int arch_init_module(struct obj_file *f, struct new_module *mod)
 {
        return 1;
 }
@@ -877,7 +1280,7 @@ int arch_init_module(struct obj_file *f, struct new_module *mod)
 /*======================================================================*/
 
 /* Standard ELF hash function.  */
-inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
+static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
 {
        unsigned long h = 0;
        unsigned long g;
@@ -895,7 +1298,7 @@ inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
        return h;
 }
 
-unsigned long obj_elf_hash(const char *name)
+static unsigned long obj_elf_hash(const char *name)
 {
        return obj_elf_hash_n(name, strlen(name));
 }
@@ -906,25 +1309,15 @@ unsigned long obj_elf_hash(const char *name)
 static int get_kernel_version(char str[STRVERSIONLEN])
 {
        struct utsname uts_info;
-       char *p, *q;
-       int a, b, c;
+       int kv;
 
        if (uname(&uts_info) < 0)
                return -1;
        strncpy(str, uts_info.release, STRVERSIONLEN);
-       p = uts_info.release;
 
-       a = strtoul(p, &p, 10);
-       if (*p != '.')
+       kv = get_kernel_revision();
+       if(kv==0)
                return -1;
-       b = strtoul(p + 1, &p, 10);
-       if (*p != '.')
-               return -1;
-       c = strtoul(p + 1, &q, 10);
-       if (p + 1 == q)
-               return -1;
-
-       return a << 16 | b << 8 | c;
 }
 
 /* String comparison for non-co-versioned kernel and module.  */
@@ -952,7 +1345,7 @@ static unsigned long ncv_symbol_hash(const char *str)
        return obj_elf_hash_n(str, len);
 }
 
-void
+static void
 obj_set_symbol_compare(struct obj_file *f,
                                           int (*cmp) (const char *, const char *),
                                           unsigned long (*hash) (const char *))
@@ -980,8 +1373,8 @@ obj_set_symbol_compare(struct obj_file *f,
 
 #endif                                                 /* BB_FEATURE_INSMOD_VERSION_CHECKING */
 
-
-struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
+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)
@@ -1046,7 +1439,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)
-                                       errorMsg("%s multiply defined\n", name);
+                                       error_msg("%s multiply defined", name);
                                return sym;
                        }
                }
@@ -1057,8 +1450,13 @@ struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
        f->symtab[hash] = sym;
        sym->ksymidx = -1;
 
-       if (ELFW(ST_BIND) (info) == STB_LOCAL)
-               f->local_symtab[symidx] = sym;
+       if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
+               if (symidx >= f->local_symtab_size)
+                       error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
+                                       name, (long) symidx, (long) f->local_symtab_size);
+               else
+                       f->local_symtab[symidx] = sym;
+       }
 
   found:
        sym->name = name;
@@ -1070,7 +1468,8 @@ struct obj_symbol *obj_add_symbol(struct obj_file *f, const char *name,
        return sym;
 }
 
-struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
+static struct obj_symbol *
+obj_find_symbol(struct obj_file *f, const char *name)
 {
        struct obj_symbol *sym;
        unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
@@ -1082,7 +1481,7 @@ struct obj_symbol *obj_find_symbol(struct obj_file *f, const char *name)
        return NULL;
 }
 
-ElfW(Addr)
+static ElfW(Addr)
        obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
 {
        if (sym) {
@@ -1096,7 +1495,7 @@ ElfW(Addr)
        }
 }
 
-struct obj_section *obj_find_section(struct obj_file *f, const char *name)
+static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
 {
        int i, n = f->header.e_shnum;
 
@@ -1129,7 +1528,7 @@ static int obj_load_order_prio(struct obj_section *a)
        return ac;
 }
 
-void
+static void
 obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
 {
        struct obj_section **p;
@@ -1141,7 +1540,7 @@ obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
        *p = sec;
 }
 
-struct obj_section *obj_create_alloced_section(struct obj_file *f,
+static struct obj_section *obj_create_alloced_section(struct obj_file *f,
                                                                                           const char *name,
                                                                                           unsigned long align,
                                                                                           unsigned long size)
@@ -1167,7 +1566,7 @@ struct obj_section *obj_create_alloced_section(struct obj_file *f,
        return sec;
 }
 
-struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
+static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
                                                                                                         const char *name,
                                                                                                         unsigned long align,
                                                                                                         unsigned long size)
@@ -1196,15 +1595,16 @@ struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
        return sec;
 }
 
-void *obj_extend_section(struct obj_section *sec, unsigned long more)
+static void *obj_extend_section(struct obj_section *sec, unsigned long more)
 {
        unsigned long oldsize = sec->header.sh_size;
-       sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
+       if (more) { 
+               sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
+       }
        return sec->contents + oldsize;
 }
 
 
-
 /* Conditionally add the symbols from the given symbol set to the
    new module.  */
 
@@ -1309,7 +1709,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) {
-                       errorMsg("symbol for parameter %s not found\n", p);
+                       error_msg("symbol for parameter %s not found", p);
                        return 0;
                }
 
@@ -1322,7 +1722,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') {
-                                       errorMsg("improperly terminated string argument for %s\n", p);
+                                       error_msg("improperly terminated string argument for %s", p);
                                        return 0;
                                } else if (*q == '\\')
                                        switch (*++q) {
@@ -1381,19 +1781,19 @@ old_process_module_arguments(struct obj_file *f, int argc, char **argv)
                        while (*q++ == ',');
                } else {
                        char *contents = f->sections[sym->secidx]->contents;
-                       char *loc = contents + sym->value;
+                       char *myloc = contents + sym->value;
                        char *r;                        /* To search for commas */
 
                        /* Break the string with comas */
                        while ((r = strchr(q, ',')) != (char *) NULL) {
                                *r++ = '\0';
-                               obj_string_patch(f, sym->secidx, loc - contents, q);
-                               loc += sizeof(char *);
+                               obj_string_patch(f, sym->secidx, myloc - contents, q);
+                               myloc += sizeof(char *);
                                q = r;
                        }
 
                        /* last part */
-                       obj_string_patch(f, sym->secidx, loc - contents, q);
+                       obj_string_patch(f, sym->secidx, myloc - contents, q);
                }
 
                argc--, argv++;
@@ -1438,7 +1838,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.  */
 
@@ -1450,8 +1850,11 @@ static int old_get_kernel_symbols(const char *m_name)
        int nks, nms, nmod, i;
 
        nks = get_kernel_syms(NULL);
-       if (nks < 0) {
-               errorMsg("get_kernel_syms: %s: %s", m_name, strerror(errno));
+       if (nks <= 0) {
+               if (nks)
+                       perror_msg("get_kernel_syms: %s", m_name);
+               else
+                       error_msg("No kernel symbols");
                return 0;
        }
 
@@ -1471,7 +1874,6 @@ static int old_get_kernel_symbols(const char *m_name)
 
        while (k->name[0] == '#' && k->name[1]) {
                struct old_kernel_sym *k2;
-               struct new_module_symbol *s;
 
                /* Find out how many symbols this module has.  */
                for (k2 = k + 1; k2->name[0] != '#'; ++k2)
@@ -1593,7 +1995,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++;
                                        }
                        }
@@ -1632,7 +2035,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);
@@ -1645,7 +2048,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 */
 
 
 
@@ -1675,7 +2078,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
                p = get_modinfo_value(f, key);
                key += 5;
                if (p == NULL) {
-                       errorMsg("invalid parameter %s\n", key);
+                       error_msg("invalid parameter %s", key);
                        return 0;
                }
 
@@ -1683,7 +2086,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) {
-                       errorMsg("symbol for parameter %s not found\n", key);
+                       error_msg("symbol for parameter %s not found", key);
                        return 0;
                }
 
@@ -1711,7 +2114,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') {
-                                                       errorMsg("improperly terminated string argument for %s\n",
+                                                       error_msg("improperly terminated string argument for %s",
                                                                        key);
                                                        return 0;
                                                } else if (*q == '\\')
@@ -1805,15 +2208,15 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
                                        /* Get the size of each member */
                                        /* Probably we should do that outside the loop ? */
                                        if (!isdigit(*(p + 1))) {
-                                               errorMsg("parameter type 'c' for %s must be followed by"
-                                                               " the maximum size\n", key);
+                                               error_msg("parameter type 'c' for %s must be followed by"
+                                                               " the maximum size", key);
                                                return 0;
                                        }
                                        charssize = strtoul(p + 1, (char **) NULL, 10);
 
                                        /* Check length */
                                        if (strlen(str) >= charssize) {
-                                               errorMsg("string too long for %s (max %ld)\n", key,
+                                               error_msg("string too long for %s (max %ld)", key,
                                                                charssize - 1);
                                                return 0;
                                        }
@@ -1842,7 +2245,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
                                        break;
 
                                default:
-                                       errorMsg("unknown parameter type '%c' for %s\n", *p, key);
+                                       error_msg("unknown parameter type '%c' for %s", *p, key);
                                        return 0;
                                }
                        }
@@ -1861,21 +2264,21 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
 
                        case ',':
                                if (++n > max) {
-                                       errorMsg("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:
-                               errorMsg("invalid argument syntax for %s\n", key);
+                               error_msg("invalid argument syntax for %s", key);
                                return 0;
                        }
                }
 
          end_of_arg:
                if (n < min) {
-                       errorMsg("too few values for %s (min %d)\n", key, min);
+                       error_msg("too few values for %s (min %d)", key, min);
                        return 0;
                }
 
@@ -1924,7 +2327,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.  */
 
@@ -1940,57 +2343,59 @@ static int new_get_kernel_symbols(void)
        module_names = xmalloc(bufsize = 256);
   retry_modules_load:
        if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
-               if (errno == ENOSPC) {
+               if (errno == ENOSPC && bufsize < ret) {
                        module_names = xrealloc(module_names, bufsize = ret);
                        goto retry_modules_load;
                }
-               errorMsg("QM_MODULES: %s", strerror(errno));
+               perror_msg("QM_MODULES");
                return 0;
        }
 
        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;
-                       }
-                       errorMsg("query_module: QM_INFO: %s: %s", mn, strerror(errno));
-                       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:
-                               errorMsg("query_module: QM_SYMBOLS: %s: %s", mn, strerror(errno));
+       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;
+                       }
                }
        }
 
@@ -1999,11 +2404,11 @@ static int new_get_kernel_symbols(void)
        syms = xmalloc(bufsize = 16 * 1024);
   retry_kern_sym_load:
        if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
-               if (errno == ENOSPC) {
+               if (errno == ENOSPC && bufsize < ret) {
                        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;
@@ -2132,6 +2537,9 @@ new_init_module(const char *m_name, struct obj_file *f,
        tgt_long m_addr;
 
        sec = obj_find_section(f, ".this");
+       if (!sec || !sec->contents) { 
+               perror_msg_and_die("corrupt module %s?",m_name);
+       }
        module = (struct new_module *) sec->contents;
        m_addr = sec->header.sh_addr;
 
@@ -2172,6 +2580,16 @@ new_init_module(const char *m_name, struct obj_file *f,
                        module->runsize > sec->header.sh_addr - m_addr)
                                module->runsize = sec->header.sh_addr - m_addr;
        }
+       sec = obj_find_section(f, ARCHDATA_SEC_NAME);
+       if (sec && sec->header.sh_size) {
+               module->archdata_start = (void*)sec->header.sh_addr;
+               module->archdata_end = module->archdata_start + sec->header.sh_size;
+       }
+       sec = obj_find_section(f, KALLSYMS_SEC_NAME);
+       if (sec && sec->header.sh_size) {
+               module->kallsyms_start = (void*)sec->header.sh_addr;
+               module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
+       }
 
        if (!arch_init_module(f, module))
                return 0;
@@ -2184,7 +2602,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);
 
@@ -2196,13 +2614,14 @@ 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 */
 
 
 /*======================================================================*/
 
-int
+static int
 obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
                                 const char *string)
 {
@@ -2231,7 +2650,7 @@ obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
        return 1;
 }
 
-int
+static int
 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
                                 struct obj_symbol *sym)
 {
@@ -2247,7 +2666,7 @@ obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
        return 1;
 }
 
-int obj_check_undefineds(struct obj_file *f)
+static int obj_check_undefineds(struct obj_file *f)
 {
        unsigned long i;
        int ret = 1;
@@ -2260,7 +2679,7 @@ int obj_check_undefineds(struct obj_file *f)
                                        sym->secidx = SHN_ABS;
                                        sym->value = 0;
                                } else {
-                                       errorMsg("unresolved symbol %s\n", sym->name);
+                                       error_msg("unresolved symbol %s", sym->name);
                                        ret = 0;
                                }
                        }
@@ -2269,7 +2688,7 @@ int obj_check_undefineds(struct obj_file *f)
        return ret;
 }
 
-void obj_allocate_commons(struct obj_file *f)
+static void obj_allocate_commons(struct obj_file *f)
 {
        struct common_entry {
                struct common_entry *next;
@@ -2367,14 +2786,18 @@ 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;
                }
        }
 }
 
-unsigned long obj_load_size(struct obj_file *f)
+static unsigned long obj_load_size(struct obj_file *f)
 {
        unsigned long dot = 0;
        struct obj_section *sec;
@@ -2395,7 +2818,7 @@ unsigned long obj_load_size(struct obj_file *f)
        return dot;
 }
 
-int obj_relocate(struct obj_file *f, ElfW(Addr) base)
+static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
 {
        int i, n = f->header.e_shnum;
        int ret = 1;
@@ -2483,11 +2906,11 @@ int obj_relocate(struct obj_file *f, ElfW(Addr) base)
                                errmsg = "Unhandled relocation";
                          bad_reloc:
                                if (extsym) {
-                                       errorMsg("%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 {
-                                       errorMsg("%s of type %ld\n", errmsg,
+                                       error_msg("%s of type %ld", errmsg,
                                                        (long) ELFW(R_TYPE) (rel->r_info));
                                }
                                ret = 0;
@@ -2525,7 +2948,7 @@ int obj_relocate(struct obj_file *f, ElfW(Addr) base)
        return ret;
 }
 
-int obj_create_image(struct obj_file *f, char *image)
+static int obj_create_image(struct obj_file *f, char *image)
 {
        struct obj_section *sec;
        ElfW(Addr) base = f->baseaddr;
@@ -2533,7 +2956,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);
@@ -2547,7 +2970,7 @@ int obj_create_image(struct obj_file *f, char *image)
 
 /*======================================================================*/
 
-struct obj_file *obj_load(FILE * fp)
+static struct obj_file *obj_load(FILE * fp, int loadprogbits)
 {
        struct obj_file *f;
        ElfW(Shdr) * section_headers;
@@ -2564,7 +2987,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;
        }
 
@@ -2572,25 +2995,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) {
-               errorMsg("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)) {
-               errorMsg("ELF file not for this architecture\n");
+               error_msg("ELF file not for this architecture");
                return NULL;
        }
        if (f->header.e_type != ET_REL) {
-               errorMsg("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))) {
-               errorMsg("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;
@@ -2603,7 +3026,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;
        }
 
@@ -2618,7 +3041,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:
@@ -2626,6 +3049,12 @@ struct obj_file *obj_load(FILE * fp)
                        break;
 
                case SHT_PROGBITS:
+#if LOADBITS
+                       if (!loadprogbits) {
+                               sec->contents = NULL;
+                               break;
+                       }
+#endif                 
                case SHT_SYMTAB:
                case SHT_STRTAB:
                case SHT_RELM:
@@ -2633,7 +3062,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 {
@@ -2643,11 +3072,11 @@ struct obj_file *obj_load(FILE * fp)
 
 #if SHT_RELM == SHT_REL
                case SHT_RELA:
-                       errorMsg("RELA relocations not supported on this architecture\n");
+                       error_msg("RELA relocations not supported on this architecture");
                        return NULL;
 #else
                case SHT_REL:
-                       errorMsg("REL relocations not supported on this architecture\n");
+                       error_msg("REL relocations not supported on this architecture");
                        return NULL;
 #endif
 
@@ -2660,7 +3089,7 @@ struct obj_file *obj_load(FILE * fp)
                                break;
                        }
 
-                       errorMsg("can't handle sections of type %ld\n",
+                       error_msg("can't handle sections of type %ld",
                                        (long) sec->header.sh_type);
                        return NULL;
                }
@@ -2678,6 +3107,12 @@ struct obj_file *obj_load(FILE * fp)
        for (i = 0; i < shnum; ++i) {
                struct obj_section *sec = f->sections[i];
 
+               /* .modinfo should be contents only but gcc has no attribute for that.
+                * The kernel may have marked .modinfo as ALLOC, ignore this bit.
+                */
+               if (strcmp(sec->name, ".modinfo") == 0)
+                       sec->header.sh_flags &= ~SHF_ALLOC;
+
                if (sec->header.sh_flags & SHF_ALLOC)
                        obj_insert_section_load_order(f, sec);
 
@@ -2689,7 +3124,7 @@ struct obj_file *obj_load(FILE * fp)
                                ElfW(Sym) * sym;
 
                                if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
-                                       errorMsg("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;
@@ -2701,38 +3136,73 @@ struct obj_file *obj_load(FILE * fp)
 
                                /* Allocate space for a table of local symbols.  */
                                j = f->local_symtab_size = sec->header.sh_info;
-                               f->local_symtab = xmalloc(j *=
-                                                                                 sizeof(struct obj_symbol *));
-                               memset(f->local_symtab, 0, j);
+                               f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
 
                                /* Insert all symbols into the hash table.  */
                                for (j = 1, ++sym; j < nsym; ++j, ++sym) {
                                        const char *name;
                                        if (sym->st_name)
                                                name = strtab + sym->st_name;
-               else
+                                       else
                                                name = f->sections[sym->st_shndx]->name;
 
                                        obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
                                                                   sym->st_value, sym->st_size);
-               }
-       }
+                               }
+                       }
                        break;
 
                case SHT_RELM:
                        if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
-                               errorMsg("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;
                        }
                        break;
+                       /* XXX  Relocation code from modutils-2.3.19 is not here.
+                        * Why?  That's about 20 lines of code from obj/obj_load.c,
+                        * which gets done in a second pass through the sections.
+                        * This BusyBox insmod does similar work in obj_relocate(). */
                }
        }
 
        return f;
 }
 
+#ifdef BB_FEATURE_INSMOD_LOADINKMEM
+/*
+ * load the unloaded sections directly into the memory allocated by
+ * kernel for the module
+ */
+
+static int obj_load_progbits(FILE * fp, struct obj_file* f)
+{
+       char* imagebase = (char*) f->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) {
+                       errorMsg("error reading ELF section data: %s\n", strerror(errno));
+                       return 0;
+               }
+
+       }
+       return 1;
+}
+#endif
+
 static void hide_special_symbols(struct obj_file *f)
 {
        static const char *const specials[] = {
@@ -2755,6 +3225,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;
@@ -2763,8 +3234,9 @@ extern int insmod_main( int argc, char **argv)
        ElfW(Addr) m_addr;
        FILE *fp;
        struct obj_file *f;
-       char m_name[BUFSIZ + 1] = "\0";
-       int exit_status = FALSE;
+       struct stat st;
+       char m_name[FILENAME_MAX + 1] = "\0";
+       int exit_status = EXIT_FAILURE;
        int m_has_modinfo;
 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
        int k_version;
@@ -2774,15 +3246,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, "fkvxLo:")) > 0) {
+               switch (opt) {
                        case 'f':                       /* force loading */
                                flag_force_load = 1;
                                break;
@@ -2795,51 +3261,91 @@ 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, FILENAME_MAX);
+                               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);
-                       }
+                               show_usage();
                }
        }
-
-       if (argc <= 0) {
-               usage(insmod_usage);
+       
+       if (argv[optind] == NULL) {
+               show_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);
 
        if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
                len -= 2;
-       memcpy(m_name, tmp, len);
-       strcpy(m_fullName, m_name);
+       memcpy(m_fullName, tmp, len);
+       m_fullName[len]='\0';
+       if (*m_name == '\0') {
+               strcpy(m_name, m_fullName);
+       }
        strcat(m_fullName, ".o");
 
-       /* Get a filedesc for the module */
-       if ((fp = fopen(*argv, "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) == FALSE) 
+       /* Get a filedesc for the module.  Check we we have a complete path */
+       if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
+                       (fp = fopen(argv[optind], "r")) == NULL) {
+               struct utsname myuname;
+
+               /* Hmm.  Could not open it.  First search under /lib/modules/`uname -r`,
+                * but do not error out yet if we fail to find it... */
+               if (uname(&myuname) == 0) {
+                       char module_dir[FILENAME_MAX];
+                       char real_module_dir[FILENAME_MAX];
+                       snprintf (module_dir, sizeof(module_dir), "%s/%s", 
+                                       _PATH_MODULES, myuname.release);
+                       /* Jump through hoops in case /lib/modules/`uname -r`
+                        * is a symlink.  We do not want recursive_action to
+                        * follow symlinks, but we do want to follow the
+                        * /lib/modules/`uname -r` dir, So resolve it ourselves
+                        * if it is a link... */
+                       if (realpath (module_dir, real_module_dir) == NULL)
+                               strcpy(real_module_dir, module_dir);
+                       recursive_action(real_module_dir, TRUE, FALSE, FALSE,
+                                       check_module_name_match, 0, m_fullName);
+               }
+
+               /* Check if we have found anything yet */
+               if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL)) 
                {
-                       if (m_filename[0] == '\0'
-                               || ((fp = fopen(m_filename, "r")) == NULL)) 
+                       char module_dir[FILENAME_MAX];
+                       if (realpath (_PATH_MODULES, module_dir) == NULL)
+                               strcpy(module_dir, _PATH_MODULES);
+                       /* No module found under /lib/modules/`uname -r`, this
+                        * time cast the net a bit wider.  Search /lib/modules/ */
+                       if (recursive_action(module_dir, TRUE, FALSE, FALSE,
+                                               check_module_name_match, 0, m_fullName) == FALSE) 
                        {
-                               errorMsg("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
-                               exit(FALSE);
-                       }
-               } else
-                       fatalError("No module named '%s' found in '%s'\n", m_fullName, _PATH_MODULES);
-       } else
-               memcpy(m_filename, *argv, strlen(*argv));
+                               if (m_filename[0] == '\0'
+                                               || ((fp = fopen(m_filename, "r")) == NULL)) 
+                               {
+                                       error_msg("%s: no module by that name found", m_fullName);
+                                       return EXIT_FAILURE;
+                               }
+                       } else
+                               error_msg_and_die("%s: no module by that name found", m_fullName);
+               }
+       } else 
+               safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
 
+       printf("Using %s\n", m_filename);
 
-       if ((f = obj_load(fp)) == NULL) {
-               perror("Could not load the module\n");
-               exit(FALSE);
-       }
+       if ((f = obj_load(fp, LOADBITS)) == NULL)
+               perror_msg_and_die("Could not load the module");
 
        if (get_modinfo_value(f, "kernel_version") == NULL)
                m_has_modinfo = 0;
@@ -2855,22 +3361,22 @@ extern int insmod_main( int argc, char **argv)
        } else {
                m_version = old_get_module_version(f, m_strversion);
                if (m_version == -1) {
-                       errorMsg("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");
                        goto out;
                }
        }
 
        if (strncmp(k_strversion, m_strversion, STRVERSIONLEN) != 0) {
                if (flag_force_load) {
-                       errorMsg("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",
+                                       "\twhile this kernel is version %s",
                                        m_filename, m_strversion, k_strversion);
                } else {
-                       errorMsg("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",
+                                       "\twhile this kernel is version %s.",
                                        m_filename, m_strversion, k_strversion);
                        goto out;
                }
@@ -2881,21 +3387,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
-               errorMsg("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
-               errorMsg("Not configured to support old kernels\n");
+               error_msg("Not configured to support old kernels");
                goto out;
 #endif
        }
@@ -2927,6 +3433,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) 
@@ -2946,22 +3455,32 @@ 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:
-               errorMsg("A module named %s already exists\n", m_name);
+               error_msg("A module named %s already exists", m_name);
                goto out;
        case ENOMEM:
-               errorMsg("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:
-               errorMsg("create_module: %s: %s", m_name, strerror(errno));
+               perror_msg("create_module: %s", m_name);
+               goto out;
+       }
+
+#if  !LOADBITS
+       /*
+        * the PROGBITS section was not loaded by the obj_load
+        * now we can load them directly into the kernel memory
+        */
+       //      f->imagebase = (char*) m_addr;
+       f->imagebase = (ElfW(Addr)) m_addr;
+       if (!obj_load_progbits(fp, f)) {
+               delete_module(m_name);
                goto out;
        }
+#endif 
 
        if (!obj_relocate(f, m_addr)) {
                delete_module(m_name);
@@ -2976,7 +3495,7 @@ extern int insmod_main( int argc, char **argv)
                goto out;
        }
 
-       exit_status = TRUE;
+       exit_status = EXIT_SUCCESS;
 
 out:
        fclose(fp);