remove all Elf32 hardcodes to make sure the elf handling is 64bit clean
[oweals/busybox.git] / modutils / insmod.c
index af12f36a6ffd101bfe1c6d72b4631e3c2aad4f54..2560aee252f14dd995aa3610e276981cab518641 100644 (file)
@@ -2,13 +2,15 @@
 /*
  * Mini insmod implementation for busybox
  *
- * This version of insmod supports x86, ARM, SH3/4, powerpc, m68k, 
- * MIPS, and v850e.
+ * This version of insmod supports ARM, CRIS, H8/300, x86, ia64, x86_64,
+ * m68k, MIPS, PowerPC, S390, SH3/4/5, Sparc, v850e, and x86_64.
  *
- * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
- * Copyright (C) 1999,2000,2001,2002 by Erik Andersen <andersee@debian.org>
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  * and Ron Alder <alder@lineo.com>
  *
+ * Rodney Radford <rradford@mindspring.com> 17-Aug-2004.
+ *   Added x86_64 support.
+ *
  * Miles Bader <miles@gnu.org> added NEC V850E support.
  *
  * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
  * very minor changes required to also work with StrongArm and presumably
  * all ARM based systems.
  *
+ * Yoshinori Sato <ysato@users.sourceforge.jp> 19-May-2004.
+ *   added Renesas H8/300 support.
+ *
+ * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
+ *   Integrated support for sh64 (SH-5), from preliminary modutils
+ *   patches from Benedict Gaster <benedict.gaster@superh.com>.
+ *   Currently limited to support for 32bit ABI.
+ *
  * Magnus Damm <damm@opensource.se> 22-May-2002.
  *   The plt and got code are now using the same structs.
  *   Added generic linked list code to fully support PowerPC.
@@ -27,7 +37,7 @@
  *   These blocks should be easy maintain and sync with obj_xxx.c in modutils.
  *
  * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
- *   PowerPC specific code stolen from modutils-2.3.16, 
+ *   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 CONFIG_USE_xxx_ENTRIES...
 #include <sys/utsname.h>
 #include "busybox.h"
 
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
-# undef CONFIG_FEATURE_OLD_MODULE_INTERFACE
-# define new_sys_init_module   init_module
-#else
-# define old_sys_init_module   init_module
+#if !defined(CONFIG_FEATURE_2_4_MODULES) && \
+       !defined(CONFIG_FEATURE_2_6_MODULES)
+#define CONFIG_FEATURE_2_4_MODULES
+#endif
+
+#if !defined(CONFIG_FEATURE_2_4_MODULES)
+#define insmod_ng_main insmod_main
+#endif
+
+#if defined(CONFIG_FEATURE_2_6_MODULES)
+extern int insmod_ng_main( int argc, char **argv);
 #endif
 
+
+#if defined(CONFIG_FEATURE_2_4_MODULES)
+
+
 #ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
-#define LOADBITS 0     
+#define LOADBITS 0
 #else
 #define LOADBITS 1
 #endif
 
 
+/* Alpha */
+#if defined(__alpha__)   
+#define MATCH_MACHINE(x) (x == EM_ALPHA)
+#define SHT_RELM       SHT_RELA
+#define Elf64_RelM     Elf64_Rela
+#define ELFCLASSM      ELFCLASS64
+#endif
+
+/* ARM support */
 #if defined(__arm__)
+#define MATCH_MACHINE(x) (x == EM_ARM)
+#define SHT_RELM       SHT_REL
+#define Elf32_RelM     Elf32_Rel
+#define ELFCLASSM      ELFCLASS32
 #define CONFIG_USE_PLT_ENTRIES
 #define CONFIG_PLT_ENTRY_SIZE 8
 #define CONFIG_USE_GOT_ENTRIES
 #define CONFIG_GOT_ENTRY_SIZE 8
 #define CONFIG_USE_SINGLE
+#endif
 
-#define MATCH_MACHINE(x) (x == EM_ARM)
-#define SHT_RELM       SHT_REL
-#define Elf32_RelM     Elf32_Rel
+/* CRIS */
+#if defined(__cris__)
+#define MATCH_MACHINE(x) (x == EM_CRIS)
+#define SHT_RELM       SHT_RELA
+#define Elf32_RelM     Elf32_Rela
 #define ELFCLASSM      ELFCLASS32
+#ifndef EM_CRIS
+#define EM_CRIS 76
+#define R_CRIS_NONE 0
+#define R_CRIS_32   3
+#endif
 #endif
 
-#if defined(__i386__)
-#define CONFIG_USE_GOT_ENTRIES
-#define CONFIG_GOT_ENTRY_SIZE 4
+/* PA-RISC / HP-PA */
+#if defined(__hppa__)
+#define MATCH_MACHINE(x) (x == EM_PARISC)
+#define SHT_RELM       SHT_RELA
+#if defined(__LP64__)
+#define Elf64_RelM     Elf64_Rela
+#define ELFCLASSM      ELFCLASS64
+#else
+#define Elf32_RelM     Elf32_Rela
+#define ELFCLASSM      ELFCLASS32
+#endif
+#endif
+
+/* H8/300 */
+#if defined(__H8300H__) || defined(__H8300S__)
+#define MATCH_MACHINE(x) (x == EM_H8_300)
+#define SHT_RELM       SHT_RELA
+#define Elf32_RelM     Elf32_Rela
+#define ELFCLASSM      ELFCLASS32
 #define CONFIG_USE_SINGLE
+#define SYMBOL_PREFIX  "_"
+#endif
 
+/* x86 */
+#if defined(__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 ELFCLASSM      ELFCLASS32
-#endif
-
-#if defined(__mc68000__) 
 #define CONFIG_USE_GOT_ENTRIES
 #define CONFIG_GOT_ENTRY_SIZE 4
 #define CONFIG_USE_SINGLE
+#endif
+
+/* IA64, aka Itanium */
+#if defined(__ia64__)
+#define MATCH_MACHINE(x) (x == EM_IA_64)
+#define SHT_RELM       SHT_RELA
+#define Elf64_RelM     Elf64_Rela
+#define ELFCLASSM      ELFCLASS64
+#endif
 
+/* m68k */
+#if defined(__mc68000__)
 #define MATCH_MACHINE(x) (x == EM_68K)
 #define SHT_RELM       SHT_RELA
 #define Elf32_RelM     Elf32_Rela
+#define ELFCLASSM      ELFCLASS32
+#define CONFIG_USE_GOT_ENTRIES
+#define CONFIG_GOT_ENTRY_SIZE 4
+#define CONFIG_USE_SINGLE
 #endif
 
+/* MIPS */
 #if defined(__mips__)
+#define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
+#define SHT_RELM       SHT_REL
+#define Elf32_RelM     Elf32_Rel
+#define ELFCLASSM      ELFCLASS32
 /* Account for ELF spec changes.  */
 #ifndef EM_MIPS_RS3_LE
 #ifdef EM_MIPS_RS4_BE
 #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
-#define ELFCLASSM      ELFCLASS32
 #define ARCHDATAM       "__dbe_table"
 #endif
 
-#if defined(__powerpc__)
+/* PowerPC */
+#if defined(__powerpc64__)
+#define MATCH_MACHINE(x) (x == EM_PPC64)
+#define SHT_RELM       SHT_RELA
+#define Elf64_RelM     Elf64_Rela
+#define ELFCLASSM      ELFCLASS64
+#elif defined(__powerpc__)
+#define MATCH_MACHINE(x) (x == EM_PPC)
+#define SHT_RELM       SHT_RELA
+#define Elf32_RelM     Elf32_Rela
+#define ELFCLASSM      ELFCLASS32
 #define CONFIG_USE_PLT_ENTRIES
 #define CONFIG_PLT_ENTRY_SIZE 16
 #define CONFIG_USE_PLT_LIST
-#define CONFIG_LIST_ARCHTYPE ElfW(Addr) 
+#define CONFIG_LIST_ARCHTYPE ElfW(Addr)
 #define CONFIG_USE_LIST
+#define ARCHDATAM       "__ftr_fixup"
+#endif
 
-#define MATCH_MACHINE(x) (x == EM_PPC)
+/* S390 */
+#if defined(__s390__)
+#define MATCH_MACHINE(x) (x == EM_S390)
 #define SHT_RELM       SHT_RELA
 #define Elf32_RelM     Elf32_Rela
 #define ELFCLASSM      ELFCLASS32
-#define ARCHDATAM       "__ftr_fixup"
-#endif
-
-#if defined(__sh__)
+#define CONFIG_USE_PLT_ENTRIES
+#define CONFIG_PLT_ENTRY_SIZE 8
 #define CONFIG_USE_GOT_ENTRIES
-#define CONFIG_GOT_ENTRY_SIZE 4
+#define CONFIG_GOT_ENTRY_SIZE 8
 #define CONFIG_USE_SINGLE
+#endif
 
+/* SuperH */
+#if defined(__sh__)
 #define MATCH_MACHINE(x) (x == EM_SH)
 #define SHT_RELM       SHT_RELA
 #define Elf32_RelM     Elf32_Rela
 #define ELFCLASSM      ELFCLASS32
-
-/* the SH changes have only been tested on the SH4 in =little endian= mode */
+#define CONFIG_USE_GOT_ENTRIES
+#define CONFIG_GOT_ENTRY_SIZE 4
+#define CONFIG_USE_SINGLE
+/* the SH changes have only been tested in =little endian= mode */
 /* I'm not sure about big endian, so let's warn: */
-
-#if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
-#error insmod.c may require changes for use on big endian SH4/SH3
+#if defined(__sh__) && defined(__BIG_ENDIAN__)
+#error insmod.c may require changes for use on big endian SH
 #endif
-
-/* it may or may not work on the SH1/SH2... So let's error on those
-   also */
-#if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
-#error insmod.c may require changes for non-SH3/SH4 use
+/* it may or may not work on the SH1/SH2... Error on those also */
+#if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
+#error insmod.c may require changes for SH1 or SH2 use
 #endif
 #endif
 
+/* Sparc */
+#if defined(__sparc__)
+#define MATCH_MACHINE(x) (x == EM_SPARC)
+#define SHT_RELM       SHT_RELA
+#define Elf32_RelM     Elf32_Rela
+#define ELFCLASSM      ELFCLASS32
+#endif
+
+/* v850e */
 #if defined (__v850e__)
+#define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
+#define SHT_RELM       SHT_RELA
+#define Elf32_RelM     Elf32_Rela
+#define ELFCLASSM      ELFCLASS32
 #define CONFIG_USE_PLT_ENTRIES
 #define CONFIG_PLT_ENTRY_SIZE 8
 #define CONFIG_USE_SINGLE
-
 #ifndef EM_CYGNUS_V850 /* grumble */
 #define EM_CYGNUS_V850         0x9080
 #endif
+#define SYMBOL_PREFIX  "_"
+#endif
 
-#define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
+/* X86_64  */
+#if defined(__x86_64__)
+#define MATCH_MACHINE(x) (x == EM_X86_64)
 #define SHT_RELM       SHT_RELA
-#define Elf32_RelM     Elf32_Rela
-#define ELFCLASSM      ELFCLASS32
-
-#define SYMBOL_PREFIX  "_"
+#define CONFIG_USE_GOT_ENTRIES
+#define CONFIG_GOT_ENTRY_SIZE 8
+#define CONFIG_USE_SINGLE
+#define Elf64_RelM     Elf64_Rela
+#define ELFCLASSM      ELFCLASS64
 #endif
 
 #ifndef SHT_RELM
 #ifndef MODUTILS_MODULE_H
 static const int MODUTILS_MODULE_H = 1;
 
-#ident "$Id: insmod.c,v 1.94 2003/01/23 04:57:35 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
-   to be dependant on a particular kernel version to compile insmod.  */
-
-
-/*======================================================================*/
-/* The structures used by Linux 2.0.  */
-
-/* The symbol format used by get_kernel_syms(2).  */
-struct old_kernel_sym
-{
-  unsigned long value;
-  char name[60];
-};
-
-struct old_module_ref
-{
-  unsigned long module;                /* kernel addresses */
-  unsigned long next;
-};
-
-struct old_module_symbol
-{
-  unsigned long addr;
-  unsigned long name;
-};
-
-struct old_symbol_table
-{
-  int size;                    /* total, including string table!!! */
-  int n_symbols;
-  int n_refs;
-  struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
-  struct old_module_ref ref[0];        /* actual size defined by n_refs */
-};
-
-struct old_mod_routines
-{
-  unsigned long init;
-  unsigned long cleanup;
-};
-
-struct old_module
-{
-  unsigned long next;
-  unsigned long ref;           /* the list of modules that refer to me */
-  unsigned long symtab;
-  unsigned long name;
-  int size;                    /* size of module in pages */
-  unsigned long addr;          /* address of module */
-  int state;
-  unsigned long cleanup;       /* cleanup routine */
-};
-
-/* Sent to init_module(2) or'ed into the code size parameter.  */
-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,
-                       struct old_mod_routines *, struct old_symbol_table *);
+#ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
 
 /*======================================================================*/
 /* For sizeof() which are related to the module platform and not to the
@@ -326,54 +368,52 @@ static const int tgt_sizeof_void_p = 8;
 /* Note: new_module_symbol does not use tgt_long intentionally */
 struct new_module_symbol
 {
-  unsigned long value;
-  unsigned long name;
+       unsigned long value;
+       unsigned long name;
 };
 
 struct new_module_persist;
 
 struct new_module_ref
 {
-  unsigned tgt_long dep;               /* kernel addresses */
-  unsigned tgt_long ref;
-  unsigned tgt_long next_ref;
+       unsigned tgt_long dep;          /* kernel addresses */
+       unsigned tgt_long ref;
+       unsigned tgt_long next_ref;
 };
 
 struct new_module
 {
-  unsigned tgt_long size_of_struct;    /* == sizeof(module) */
-  unsigned tgt_long next;
-  unsigned tgt_long name;
-  unsigned tgt_long size;
-
-  tgt_long usecount;
-  unsigned tgt_long flags;             /* AUTOCLEAN et al */
-
-  unsigned nsyms;
-  unsigned ndeps;
-
-  unsigned tgt_long syms;
-  unsigned tgt_long deps;
-  unsigned tgt_long refs;
-  unsigned tgt_long init;
-  unsigned tgt_long cleanup;
-  unsigned tgt_long ex_table_start;
-  unsigned tgt_long ex_table_end;
+       unsigned tgt_long size_of_struct;       /* == sizeof(module) */
+       unsigned tgt_long next;
+       unsigned tgt_long name;
+       unsigned tgt_long size;
+
+       tgt_long usecount;
+       unsigned tgt_long flags;                /* AUTOCLEAN et al */
+
+       unsigned nsyms;
+       unsigned ndeps;
+
+       unsigned tgt_long syms;
+       unsigned tgt_long deps;
+       unsigned tgt_long refs;
+       unsigned tgt_long init;
+       unsigned tgt_long cleanup;
+       unsigned tgt_long ex_table_start;
+       unsigned tgt_long ex_table_end;
 #ifdef __alpha__
-  unsigned tgt_long gp;
-#endif
-  /* Everything after here is extension.  */
-  unsigned tgt_long persist_start;
-  unsigned tgt_long persist_end;
-  unsigned tgt_long can_unload;
-  unsigned tgt_long runsize;
-#ifdef CONFIG_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 */
+       unsigned tgt_long gp;
 #endif
+       /* Everything after here is extension.  */
+       unsigned tgt_long persist_start;
+       unsigned tgt_long persist_end;
+       unsigned tgt_long can_unload;
+       unsigned tgt_long runsize;
+       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 */
 };
 
 #ifdef ARCHDATAM
@@ -386,10 +426,10 @@ struct new_module
 
 struct new_module_info
 {
-  unsigned long addr;
-  unsigned long size;
-  unsigned long flags;
-          long usecount;
+       unsigned long addr;
+       unsigned long size;
+       unsigned long flags;
+       long usecount;
 };
 
 /* Bits of module.flags.  */
@@ -399,9 +439,9 @@ 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,
-                size_t *ret);
+int init_module(const char *name, const struct new_module *);
+int query_module(const char *name, int which, void *buf,
+               size_t bufsize, size_t *ret);
 
 /* Values for query_module's which.  */
 
@@ -455,7 +495,7 @@ int delete_module(const char *);
 #ifndef MODUTILS_OBJ_H
 static const int MODUTILS_OBJ_H = 1;
 
-#ident "$Id: insmod.c,v 1.94 2003/01/23 04:57:35 andersen Exp $"
+#ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
 
 /* The relocatable object is manipulated using elfin types.  */
 
@@ -479,7 +519,7 @@ static const int MODUTILS_OBJ_H = 1;
 # endif
 #endif
 
-/* For some reason this is missing from libc5.  */
+/* For some reason this is missing from some ancient C libraries....  */
 #ifndef ELF32_ST_INFO
 # define ELF32_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
 #endif
@@ -488,28 +528,34 @@ static const int MODUTILS_OBJ_H = 1;
 # define ELF64_ST_INFO(bind, type)       (((bind) << 4) + ((type) & 0xf))
 #endif
 
+#define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
+#define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
+#define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
+#define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
+#define ELF_R_SYM(val) ELFW(R_SYM)(val)
+
 struct obj_string_patch;
 struct obj_symbol_patch;
 
 struct obj_section
 {
-  ElfW(Shdr) header;
-  const char *name;
-  char *contents;
-  struct obj_section *load_next;
-  int idx;
+       ElfW(Shdr) header;
+       const char *name;
+       char *contents;
+       struct obj_section *load_next;
+       int idx;
 };
 
 struct obj_symbol
 {
-  struct obj_symbol *next;     /* hash table link */
-  const char *name;
-  unsigned long value;
-  unsigned long size;
-  int secidx;                  /* the defining section index/module */
-  int info;
-  int ksymidx;                 /* for export to the kernel symtab */
-  int referenced;              /* actually used in the link */
+       struct obj_symbol *next;        /* hash table link */
+       const char *name;
+       unsigned long value;
+       unsigned long size;
+       int secidx;                     /* the defining section index/module */
+       int info;
+       int ksymidx;                    /* for export to the kernel symtab */
+       int referenced;         /* actually used in the link */
 };
 
 /* Hardcode the hash table size.  We shouldn't be needing so many
@@ -520,42 +566,42 @@ struct obj_symbol
 
 struct obj_file
 {
-  ElfW(Ehdr) header;
-  ElfW(Addr) baseaddr;
-  struct obj_section **sections;
-  struct obj_section *load_order;
-  struct obj_section **load_order_search_start;
-  struct obj_string_patch *string_patches;
-  struct obj_symbol_patch *symbol_patches;
-  int (*symbol_cmp)(const char *, const char *);
-  unsigned long (*symbol_hash)(const char *);
-  unsigned long local_symtab_size;
-  struct obj_symbol **local_symtab;
-  struct obj_symbol *symtab[HASH_BUCKETS];
+       ElfW(Ehdr) header;
+       ElfW(Addr) baseaddr;
+       struct obj_section **sections;
+       struct obj_section *load_order;
+       struct obj_section **load_order_search_start;
+       struct obj_string_patch *string_patches;
+       struct obj_symbol_patch *symbol_patches;
+       int (*symbol_cmp)(const char *, const char *);
+       unsigned long (*symbol_hash)(const char *);
+       unsigned long local_symtab_size;
+       struct obj_symbol **local_symtab;
+       struct obj_symbol *symtab[HASH_BUCKETS];
 };
 
 enum obj_reloc
 {
-  obj_reloc_ok,
-  obj_reloc_overflow,
-  obj_reloc_dangerous,
-  obj_reloc_unhandled
+       obj_reloc_ok,
+       obj_reloc_overflow,
+       obj_reloc_dangerous,
+       obj_reloc_unhandled
 };
 
 struct obj_string_patch
 {
-  struct obj_string_patch *next;
-  int reloc_secidx;
-  ElfW(Addr) reloc_offset;
-  ElfW(Addr) string_offset;
+       struct obj_string_patch *next;
+       int reloc_secidx;
+       ElfW(Addr) reloc_offset;
+       ElfW(Addr) string_offset;
 };
 
 struct obj_symbol_patch
 {
-  struct obj_symbol_patch *next;
-  int reloc_secidx;
-  ElfW(Addr) reloc_offset;
-  struct obj_symbol *sym;
+       struct obj_symbol_patch *next;
+       int reloc_secidx;
+       ElfW(Addr) reloc_offset;
+       struct obj_symbol *sym;
 };
 
 
@@ -598,10 +644,8 @@ static void *obj_extend_section (struct obj_section *sec, unsigned long more);
 static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
                     const char *string);
 
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
 static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
                     struct obj_symbol *sym);
-#endif
 
 static int obj_check_undefineds(struct obj_file *f);
 
@@ -631,9 +675,7 @@ static enum obj_reloc arch_apply_relocation (struct obj_file *f,
 
 static void arch_create_got (struct obj_file *f);
 
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
-static int arch_init_module (struct obj_file *f, struct new_module *);
-#endif
+static int obj_gpl_license(struct obj_file *f, const char **license);
 
 #endif /* obj.h */
 //----------------------------------------------------------------------------
@@ -641,7 +683,6 @@ static int arch_init_module (struct obj_file *f, struct new_module *);
 //----------------------------------------------------------------------------
 
 
-
 /* SPFX is always a string, so it can be concatenated to string constants.  */
 #ifdef SYMBOL_PREFIX
 #define SPFX   SYMBOL_PREFIX
@@ -690,9 +731,9 @@ struct arch_single_entry
 #if defined(__mips__)
 struct mips_hi16
 {
-  struct mips_hi16 *next;
-  Elf32_Addr *addr;
-  Elf32_Addr value;
+       struct mips_hi16 *next;
+       ElfW(Addr) *addr;
+       ElfW(Addr) value;
 };
 #endif
 
@@ -756,17 +797,17 @@ static int check_module_name_match(const char *filename, struct stat *statbuf,
        if (fullname[0] == '\0')
                return (FALSE);
        else {
-               char *tmp, *tmp1 = xstrdup(filename);
-               tmp = get_last_path_component(tmp1);
+               char *tmp, *tmp1 = bb_xstrdup(filename);
+               tmp = bb_get_last_path_component(tmp1);
                if (strcmp(tmp, fullname) == 0) {
                        free(tmp1);
                        /* Stop searching if we find a match */
-                       m_filename = xstrdup(filename);
-                       return (TRUE);
+                       m_filename = bb_xstrdup(filename);
+                       return (FALSE);
                }
                free(tmp1);
        }
-       return (FALSE);
+       return (TRUE);
 }
 
 
@@ -824,418 +865,660 @@ arch_apply_relocation(struct obj_file *f,
 #endif
 #endif
 
-       switch (ELF32_R_TYPE(rel->r_info)) {
-
+       switch (ELF_R_TYPE(rel->r_info)) {
 
 #if defined(__arm__)
-       case R_ARM_NONE:
-               break;
-
-       case R_ARM_ABS32:
-               *loc += v;
-               break;
-               
-       case R_ARM_GOT32:
-               goto bb_use_got;
-
-       case R_ARM_GOTPC:
-               /* relative reloc, always to _GLOBAL_OFFSET_TABLE_ 
-                * (which is .got) similar to branch, 
-                * but is full 32 bits relative */
-
-               assert(got);
-               *loc += got - dot;
-               break;
-
-       case R_ARM_PC24:
-       case R_ARM_PLT32:
-               goto bb_use_plt;
-
-       case R_ARM_GOTOFF: /* address relative to the got */
-               assert(got);
-               *loc += v - got;
-               break;
+               case R_ARM_NONE:
+                       break;
 
-#elif defined(__i386__)
+               case R_ARM_ABS32:
+                       *loc += v;
+                       break;
+
+               case R_ARM_GOT32:
+                       goto bb_use_got;
 
-       case R_386_NONE:
-               break;
+               case R_ARM_GOTPC:
+                       /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
+                        * (which is .got) similar to branch,
+                        * but is full 32 bits relative */
 
-       case R_386_32:
-               *loc += v;
-               break;
+                       assert(got);
+                       *loc += got - dot;
+                       break;
 
-       case R_386_PLT32:
-       case R_386_PC32:
-               *loc += v - dot;
-               break;
+               case R_ARM_PC24:
+               case R_ARM_PLT32:
+                       goto bb_use_plt;
 
-       case R_386_GLOB_DAT:
-       case R_386_JMP_SLOT:
-               *loc = v;
-               break;
+               case R_ARM_GOTOFF: /* address relative to the got */
+                       assert(got);
+                       *loc += v - got;
+                       break;
 
-       case R_386_RELATIVE:
-               *loc += f->baseaddr;
-               break;
+#elif defined(__s390__)
+               case R_390_32:
+                       *(unsigned int *) loc += v;
+                       break;
+               case R_390_16:
+                       *(unsigned short *) loc += v;
+                       break;
+               case R_390_8:
+                       *(unsigned char *) loc += v;
+                       break;
 
-       case R_386_GOTPC:
-               assert(got != 0);
-               *loc += got - dot;
-               break;
+               case R_390_PC32:
+                       *(unsigned int *) loc += v - dot;
+                       break;
+               case R_390_PC16DBL:
+                       *(unsigned short *) loc += (v - dot) >> 1;
+                       break;
+               case R_390_PC16:
+                       *(unsigned short *) loc += v - dot;
+                       break;
 
-       case R_386_GOT32:
-               goto bb_use_got;
+               case R_390_PLT32:
+               case R_390_PLT16DBL:
+                       /* find the plt entry and initialize it.  */
+                       assert(isym != NULL);
+                       pe = (struct arch_single_entry *) &isym->pltent;
+                       assert(pe->allocated);
+                       if (pe->inited == 0) {
+                               ip = (unsigned long *)(ifile->plt->contents + pe->offset);
+                               ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
+                               ip[1] = 0x100607f1;
+                               if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
+                                       ip[2] = v - 2;
+                               else
+                                       ip[2] = v;
+                               pe->inited = 1;
+                       }
 
-       case R_386_GOTOFF:
-               assert(got != 0);
-               *loc += v - got;
-               break;
+                       /* Insert relative distance to target.  */
+                       v = plt + pe->offset - dot;
+                       if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
+                               *(unsigned int *) loc = (unsigned int) v;
+                       else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
+                               *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
+                       break;
 
-#elif defined(__mc68000__)
+               case R_390_GLOB_DAT:
+               case R_390_JMP_SLOT:
+                       *loc = v;
+                       break;
 
-       case R_68K_NONE:
-               break;
+               case R_390_RELATIVE:
+                       *loc += f->baseaddr;
+                       break;
 
-       case R_68K_32:
-               *loc += v;
-               break;
+               case R_390_GOTPC:
+                       assert(got != 0);
+                       *(unsigned long *) loc += got - dot;
+                       break;
 
-       case R_68K_8:
-               if (v > 0xff) {
-                       ret = obj_reloc_overflow;
-               }
-               *(char *)loc = v;
-               break;
+               case R_390_GOT12:
+               case R_390_GOT16:
+               case R_390_GOT32:
+                       assert(isym != NULL);
+                       assert(got != 0);
+                       if (!isym->gotent.inited)
+                       {
+                               isym->gotent.inited = 1;
+                               *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
+                       }
+                       if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
+                               *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
+                       else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
+                               *(unsigned short *) loc += isym->gotent.offset;
+                       else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
+                               *(unsigned int *) loc += isym->gotent.offset;
+                       break;
 
-       case R_68K_16:
-               if (v > 0xffff) {
-                       ret = obj_reloc_overflow;
-               }
-               *(short *)loc = v;
-               break;
-
-       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;
+#ifndef R_390_GOTOFF32
+#define R_390_GOTOFF32 R_390_GOTOFF
+#endif
+               case R_390_GOTOFF32:
+                       assert(got != 0);
+                       *loc += v - got;
+                       break;
 
-       case R_68K_PC32:
-               *(int *)loc = v - dot;
-               break;
+#elif defined(__i386__)
+
+               case R_386_NONE:
+                       break;
 
-       case R_68K_GLOB_DAT:
-       case R_68K_JMP_SLOT:
-               *loc = v;
-               break;
+               case R_386_32:
+                       *loc += v;
+                       break;
 
-       case R_68K_RELATIVE:
-               *(int *)loc += f->baseaddr;
-               break;
+               case R_386_PLT32:
+               case R_386_PC32:
+                       *loc += v - dot;
+                       break;
 
-       case R_68K_GOT32:
-               goto bb_use_got;
+               case R_386_GLOB_DAT:
+               case R_386_JMP_SLOT:
+                       *loc = v;
+                       break;
 
-       case R_68K_GOTOFF:
-               assert(got != 0);
-               *loc += v - got;
-               break;
+               case R_386_RELATIVE:
+                       *loc += f->baseaddr;
+                       break;
 
-#elif defined(__mips__)
+               case R_386_GOTPC:
+                       assert(got != 0);
+                       *loc += got - dot;
+                       break;
 
-       case R_MIPS_NONE:
-               break;
+               case R_386_GOT32:
+                       goto bb_use_got;
 
-       case R_MIPS_32:
-               *loc += v;
-               break;
+               case R_386_GOTOFF:
+                       assert(got != 0);
+                       *loc += v - got;
+                       break;
 
-       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;
+#elif defined(__x86_64__)
 
-       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_X86_64_NONE:
+                       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;
-                               }
+               case R_X86_64_64:
+                       *loc += v;
+                       break;
 
-                               ifile->mips_hi16_list = NULL;
+               case R_X86_64_32:
+                       *(unsigned int *) loc += v;
+                       if (v > 0xffffffff)
+                       {
+                               ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
+                               /* error("Possibly is module compiled without -mcmodel=kernel!"); */
                        }
+                       break;
 
-                       /* Ok, we're done with the HI16 relocs.  Now deal with the LO16.  */
-                       val = v + vallo;
-                       insnlo = (insnlo & ~0xffff) | (val & 0xffff);
-                       *loc = insnlo;
+               case R_X86_64_32S:
+                       *(signed int *) loc += v;
                        break;
-               }
 
-#elif defined(__powerpc__)
+               case R_X86_64_16:
+                       *(unsigned short *) loc += v;
+                       break;
 
-       case R_PPC_ADDR16_HA:
-               *(unsigned short *)loc = (v + 0x8000) >> 16;
-               break;
+               case R_X86_64_8:
+                       *(unsigned char *) loc += v;
+                       break;
 
-       case R_PPC_ADDR16_HI:
-               *(unsigned short *)loc = v >> 16;
-               break;
+               case R_X86_64_PC32:
+                       *(unsigned int *) loc += v - dot;
+                       break;
 
-       case R_PPC_ADDR16_LO:
-               *(unsigned short *)loc = v;
-               break;
+               case R_X86_64_PC16:
+                       *(unsigned short *) loc += v - dot;
+                       break;
 
-       case R_PPC_REL24:
-               goto bb_use_plt;
+               case R_X86_64_PC8:
+                       *(unsigned char *) loc += v - dot;
+                       break;
 
-       case R_PPC_REL32:
-               *loc = v - dot;
-               break;
+               case R_X86_64_GLOB_DAT:
+               case R_X86_64_JUMP_SLOT:
+                       *loc = v;
+                       break;
 
-       case R_PPC_ADDR32:
-               *loc = v;
-               break;
+               case R_X86_64_RELATIVE:
+                       *loc += f->baseaddr;
+                       break;
 
-#elif defined(__sh__)
+               case R_X86_64_GOT32:
+               case R_X86_64_GOTPCREL:
+                       goto bb_use_got;
+#if 0
+                       assert(isym != NULL);
+                       if (!isym->gotent.reloc_done)
+                       {
+                               isym->gotent.reloc_done = 1;
+                               *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
+                       }
+                       /* XXX are these really correct?  */
+                       if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
+                               *(unsigned int *) loc += v + isym->gotent.offset;
+                       else
+                               *loc += isym->gotent.offset;
+                       break;
+#endif
+
+#elif defined(__mc68000__)
 
-       case R_SH_NONE:
-               break;
+               case R_68K_NONE:
+                       break;
 
-       case R_SH_DIR32:
-               *loc += v;
-               break;
+               case R_68K_32:
+                       *loc += v;
+                       break;
 
-       case R_SH_REL32:
-               *loc += v - dot;
-               break;
-               
-       case R_SH_PLT32:
-               *loc = v - dot;
-               break;
+               case R_68K_8:
+                       if (v > 0xff) {
+                               ret = obj_reloc_overflow;
+                       }
+                       *(char *)loc = v;
+                       break;
 
-       case R_SH_GLOB_DAT:
-       case R_SH_JMP_SLOT:
-               *loc = v;
-               break;
+               case R_68K_16:
+                       if (v > 0xffff) {
+                               ret = obj_reloc_overflow;
+                       }
+                       *(short *)loc = v;
+                       break;
 
-       case R_SH_RELATIVE:
-               *loc = f->baseaddr + rel->r_addend;
-               break;
+               case R_68K_PC8:
+                       v -= dot;
+                       if ((ElfW(Sword))v > 0x7f ||
+                                       (ElfW(Sword))v < -(ElfW(Sword))0x80) {
+                               ret = obj_reloc_overflow;
+                       }
+                       *(char *)loc = v;
+                       break;
 
-       case R_SH_GOTPC:
-               assert(got != 0);
-               *loc = got - dot + rel->r_addend;
-               break;
+               case R_68K_PC16:
+                       v -= dot;
+                       if ((ElfW(Sword))v > 0x7fff ||
+                                       (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
+                               ret = obj_reloc_overflow;
+                       }
+                       *(short *)loc = v;
+                       break;
 
-       case R_SH_GOT32:
-               goto bb_use_got;
+               case R_68K_PC32:
+                       *(int *)loc = v - dot;
+                       break;
 
-       case R_SH_GOTOFF:
-               assert(got != 0);
-               *loc = v - got;
-               break;
+               case R_68K_GLOB_DAT:
+               case R_68K_JMP_SLOT:
+                       *loc = v;
+                       break;
 
-#endif
+               case R_68K_RELATIVE:
+                       *(int *)loc += f->baseaddr;
+                       break;
 
-       default:
-        printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
-               ret = obj_reloc_unhandled;
-               break;
+               case R_68K_GOT32:
+                       goto bb_use_got;
 
-#if defined (__v850e__)
-       case R_V850_NONE:
-               break;
-
-       case R_V850_32:
-               /* We write two shorts instead of a long because even
-                  32-bit insns only need half-word alignment, but
-                  32-bit data needs to be long-word aligned.  */
-               v += ((unsigned short *)loc)[0];
-               v += ((unsigned short *)loc)[1] << 16;
-               ((unsigned short *)loc)[0] = v & 0xffff;
-               ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
-               break;
-
-       case R_V850_22_PCREL:
-               goto bb_use_plt;
+#ifdef R_68K_GOTOFF
+               case R_68K_GOTOFF:
+                       assert(got != 0);
+                       *loc += v - got;
+                       break;
 #endif
 
-#if defined(CONFIG_USE_PLT_ENTRIES)
+#elif defined(__mips__)
 
-         bb_use_plt:
+               case R_MIPS_NONE:
+                       break;
 
-      /* find the plt entry and initialize it if necessary */
-      assert(isym != NULL);
+               case R_MIPS_32:
+                       *loc += v;
+                       break;
 
-#if defined(CONFIG_USE_PLT_LIST)
-      for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
-       pe = pe->next;
-      assert(pe != NULL);
-#else
-      pe = &isym->pltent;
-#endif
+               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;
 
-      if (! pe->inited) {
-               ip = (unsigned long *) (ifile->plt->contents + pe->offset);
+               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;
+                       }
 
-               /* generate some machine code */
+               case R_MIPS_LO16:
+                       {
+                               unsigned long insnlo = *loc;
+                               ElfW(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;
+                                       }
 
-#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
-#if defined (__v850e__)
-               /* We have to trash a register, so we assume that any control
-                  transfer more than 21-bits away must be a function call
-                  (so we can use a call-clobbered register).  */
-               ip[0] = 0x0621 + ((v & 0xffff) << 16);   /* mov sym, r1 ... */
-               ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
-#endif
-               pe->inited = 1;
-         }
+                                       ifile->mips_hi16_list = NULL;
+                               }
 
-      /* relative distance to target */
-      v -= dot;
-      /* if the target is too far away.... */
-#if defined (__arm__) || defined (__powerpc__)
-      if ((int)v < -0x02000000 || (int)v >= 0x02000000) 
-#elif defined (__v850e__)
-      if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
-#endif
-           /* go via the plt */
-           v = plt + pe->offset - dot;
+                               /* Ok, we're done with the HI16 relocs.  Now deal with the LO16.  */
+                               val = v + vallo;
+                               insnlo = (insnlo & ~0xffff) | (val & 0xffff);
+                               *loc = insnlo;
+                               break;
+                       }
 
-#if defined (__v850e__)
-      if (v & 1)
-#else
-      if (v & 3)
-#endif
-           ret = obj_reloc_dangerous;
+#elif defined(__powerpc__)
 
-      /* merge the offset into the instruction. */
-#if defined(__arm__)
-      /* Convert to words. */
-      v >>= 2;
+               case R_PPC_ADDR16_HA:
+                       *(unsigned short *)loc = (v + 0x8000) >> 16;
+                       break;
 
-      *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
-#endif
-#if defined(__powerpc__)
-      *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
-#endif
-#if defined (__v850e__)
-      /* We write two shorts instead of a long because even 32-bit insns
-        only need half-word alignment, but the 32-bit data write needs
-        to be long-word aligned.  */
-      ((unsigned short *)loc)[0] =
-             (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
-             | ((v >> 16) & 0x3f);             /* offs high part */
-      ((unsigned short *)loc)[1] =
-             (v & 0xffff);                    /* offs low part */
-#endif
-      break;
-#endif /* CONFIG_USE_PLT_ENTRIES */
+               case R_PPC_ADDR16_HI:
+                       *(unsigned short *)loc = v >> 16;
+                       break;
 
-#if defined(CONFIG_USE_GOT_ENTRIES)
-         bb_use_got:
+               case R_PPC_ADDR16_LO:
+                       *(unsigned short *)loc = v;
+                       break;
 
-               assert(isym != NULL);
-        /* needs an entry in the .got: set it, once */
-               if (!isym->gotent.inited) {
-                       isym->gotent.inited = 1;
-                       *(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;
-#endif
-               break;
+               case R_PPC_REL24:
+                       goto bb_use_plt;
 
-#endif /* CONFIG_USE_GOT_ENTRIES */
+               case R_PPC_REL32:
+                       *loc = v - dot;
+                       break;
+
+               case R_PPC_ADDR32:
+                       *loc = v;
+                       break;
+
+#elif defined(__sh__)
+
+               case R_SH_NONE:
+                       break;
+
+               case R_SH_DIR32:
+                       *loc += v;
+                       break;
+
+               case R_SH_REL32:
+                       *loc += v - dot;
+                       break;
+
+               case R_SH_PLT32:
+                       *loc = v - dot;
+                       break;
+
+               case R_SH_GLOB_DAT:
+               case R_SH_JMP_SLOT:
+                       *loc = v;
+                       break;
+
+               case R_SH_RELATIVE:
+                       *loc = f->baseaddr + rel->r_addend;
+                       break;
+
+               case R_SH_GOTPC:
+                       assert(got != 0);
+                       *loc = got - dot + rel->r_addend;
+                       break;
+
+               case R_SH_GOT32:
+                       goto bb_use_got;
+
+               case R_SH_GOTOFF:
+                       assert(got != 0);
+                       *loc = v - got;
+                       break;
+
+#if defined(__SH5__)
+               case R_SH_IMM_MEDLOW16:
+               case R_SH_IMM_LOW16:
+                       {
+                               ElfW(Addr) word;
+
+                               if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
+                                       v >>= 16;
+
+                               /*
+                                *  movi and shori have the format:
+                                *
+                                *  |  op  | imm  | reg | reserved |
+                                *   31..26 25..10 9.. 4 3   ..   0
+                                *
+                                * so we simply mask and or in imm.
+                                */
+                               word = *loc & ~0x3fffc00;
+                               word |= (v & 0xffff) << 10;
+
+                               *loc = word;
+
+                               break;
+                       }
+
+               case R_SH_IMM_MEDLOW16_PCREL:
+               case R_SH_IMM_LOW16_PCREL:
+                       {
+                               ElfW(Addr) word;
+
+                               word = *loc & ~0x3fffc00;
+
+                               v -= dot;
+
+                               if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
+                                       v >>= 16;
+
+                               word |= (v & 0xffff) << 10;
+
+                               *loc = word;
+
+                               break;
+                       }
+#endif /* __SH5__ */
+#endif /* __sh__ */
+
+               default:
+                       printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
+                       ret = obj_reloc_unhandled;
+                       break;
+
+#if defined (__v850e__)
+               case R_V850_NONE:
+                       break;
+
+               case R_V850_32:
+                       /* We write two shorts instead of a long because even
+                          32-bit insns only need half-word alignment, but
+                          32-bit data needs to be long-word aligned.  */
+                       v += ((unsigned short *)loc)[0];
+                       v += ((unsigned short *)loc)[1] << 16;
+                       ((unsigned short *)loc)[0] = v & 0xffff;
+                       ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
+                       break;
+
+               case R_V850_22_PCREL:
+                       goto bb_use_plt;
+#endif
+
+#if defined (__cris__)
+               case R_CRIS_NONE:
+                       break;
+
+               case R_CRIS_32:
+                       /* CRIS keeps the relocation value in the r_addend field and
+                        * should not use whats in *loc at all
+                        */
+                       *loc = v;
+                       break;
+#endif
+
+#if defined(__H8300H__) || defined(__H8300S__)
+               case R_H8_DIR24R8:
+                       loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
+                       *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
+                       break;
+               case R_H8_DIR24A8:
+                       *loc += v;
+                       break;
+               case R_H8_DIR32:
+               case R_H8_DIR32A16:
+                       *loc += v;
+                       break;
+               case R_H8_PCREL16:
+                       v -= dot + 2;
+                       if ((ElfW(Sword))v > 0x7fff || 
+                           (ElfW(Sword))v < -(ElfW(Sword))0x8000)
+                               ret = obj_reloc_overflow;
+                       else 
+                               *(unsigned short *)loc = v;
+                       break;
+               case R_H8_PCREL8:
+                       v -= dot + 1;
+                       if ((ElfW(Sword))v > 0x7f || 
+                           (ElfW(Sword))v < -(ElfW(Sword))0x80)
+                               ret = obj_reloc_overflow;
+                       else 
+                               *(unsigned char *)loc = v;
+                       break;
+#endif
+
+#if defined(CONFIG_USE_PLT_ENTRIES)
+
+bb_use_plt:
+
+                       /* find the plt entry and initialize it if necessary */
+                       assert(isym != NULL);
+
+#if defined(CONFIG_USE_PLT_LIST)
+                       for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
+                               pe = pe->next;
+                       assert(pe != NULL);
+#else
+                       pe = &isym->pltent;
+#endif
+
+                       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
+#if defined (__v850e__)
+                               /* We have to trash a register, so we assume that any control
+                                  transfer more than 21-bits away must be a function call
+                                  (so we can use a call-clobbered register).  */
+                               ip[0] = 0x0621 + ((v & 0xffff) << 16);   /* mov sym, r1 ... */
+                               ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
+#endif
+                               pe->inited = 1;
+                       }
+
+                       /* relative distance to target */
+                       v -= dot;
+                       /* if the target is too far away.... */
+#if defined (__arm__) || defined (__powerpc__)
+                       if ((int)v < -0x02000000 || (int)v >= 0x02000000)
+#elif defined (__v850e__)
+                               if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
+#endif
+                                       /* go via the plt */
+                                       v = plt + pe->offset - dot;
+
+#if defined (__v850e__)
+                       if (v & 1)
+#else
+                               if (v & 3)
+#endif
+                                       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
+#if defined (__v850e__)
+                       /* We write two shorts instead of a long because even 32-bit insns
+                          only need half-word alignment, but the 32-bit data write needs
+                          to be long-word aligned.  */
+                       ((unsigned short *)loc)[0] =
+                               (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
+                               | ((v >> 16) & 0x3f);             /* offs high part */
+                       ((unsigned short *)loc)[1] =
+                               (v & 0xffff);                    /* offs low part */
+#endif
+                       break;
+#endif /* CONFIG_USE_PLT_ENTRIES */
+
+#if defined(CONFIG_USE_GOT_ENTRIES)
+bb_use_got:
+
+                       assert(isym != NULL);
+                       /* needs an entry in the .got: set it, once */
+                       if (!isym->gotent.inited) {
+                               isym->gotent.inited = 1;
+                               *(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;
+#endif
+                       break;
+
+#endif /* CONFIG_USE_GOT_ENTRIES */
        }
 
        return ret;
 }
 
 
-#if defined(CONFIG_USE_LIST) 
+#if defined(CONFIG_USE_LIST)
 
 static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
                          int offset, int size)
@@ -1262,7 +1545,7 @@ static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
 
 #endif
 
-#if defined(CONFIG_USE_SINGLE) 
+#if defined(CONFIG_USE_SINGLE)
 
 static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
                             int offset, int size)
@@ -1280,7 +1563,7 @@ static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
 
 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
 
-static struct obj_section *arch_xsect_init(struct obj_file *f, char *name, 
+static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
                                           int offset, int size)
 {
        struct obj_section *myrelsec = obj_find_section(f, name);
@@ -1292,8 +1575,8 @@ static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
        if (myrelsec) {
                obj_extend_section(myrelsec, offset);
        } else {
-               myrelsec = obj_create_alloced_section(f, name, 
-                                                     size, offset);
+               myrelsec = obj_create_alloced_section(f, name,
+                               size, offset);
                assert(myrelsec);
        }
 
@@ -1313,7 +1596,7 @@ static void arch_create_got(struct obj_file *f)
 #if defined(CONFIG_USE_PLT_ENTRIES)
        int plt_offset = 0, plt_needed = 0, plt_allocate;
 #endif
-    struct obj_section *relsec, *symsec, *strsec;
+       struct obj_section *relsec, *symsec, *strsec;
        ElfW(RelM) *rel, *relend;
        ElfW(Sym) *symtab, *extsym;
        const char *strtab, *name;
@@ -1333,7 +1616,7 @@ static void arch_create_got(struct obj_file *f)
                strtab = (const char *) strsec->contents;
 
                for (; rel < relend; ++rel) {
-                       extsym = &symtab[ELF32_R_SYM(rel->r_info)];
+                       extsym = &symtab[ELF_R_SYM(rel->r_info)];
 
 #if defined(CONFIG_USE_GOT_ENTRIES)
                        got_allocate = 0;
@@ -1342,64 +1625,66 @@ static void arch_create_got(struct obj_file *f)
                        plt_allocate = 0;
 #endif
 
-                       switch (ELF32_R_TYPE(rel->r_info)) {
+                       switch (ELF_R_TYPE(rel->r_info)) {
 #if defined(__arm__)
-                       case R_ARM_PC24:
-                       case R_ARM_PLT32:
-                               plt_allocate = 1;
-                               break;
+                               case R_ARM_PC24:
+                               case R_ARM_PLT32:
+                                       plt_allocate = 1;
+                                       break;
 
-                       case R_ARM_GOTOFF:
-                       case R_ARM_GOTPC:
-                               got_needed = 1;
-                               continue;
+                               case R_ARM_GOTOFF:
+                               case R_ARM_GOTPC:
+                                       got_needed = 1;
+                                       continue;
 
-                       case R_ARM_GOT32:
-                               got_allocate = 1;
-                               break;
+                               case R_ARM_GOT32:
+                                       got_allocate = 1;
+                                       break;
 
 #elif defined(__i386__)
-                       case R_386_GOTPC:
-                       case R_386_GOTOFF:
-                               got_needed = 1;
-                               continue;
+                               case R_386_GOTPC:
+                               case R_386_GOTOFF:
+                                       got_needed = 1;
+                                       continue;
 
-                       case R_386_GOT32:
-                               got_allocate = 1;
-                               break;
+                               case R_386_GOT32:
+                                       got_allocate = 1;
+                                       break;
 
 #elif defined(__powerpc__)
-                       case R_PPC_REL24:
-                               plt_allocate = 1;
-                               break;
+                               case R_PPC_REL24:
+                                       plt_allocate = 1;
+                                       break;
 
 #elif defined(__mc68000__)
-                       case R_68K_GOT32:
-                               got_allocate = 1;
-                               break;
+                               case R_68K_GOT32:
+                                       got_allocate = 1;
+                                       break;
 
-                       case R_68K_GOTOFF:
-                               got_needed = 1;
-                               continue;
+#ifdef R_68K_GOTOFF
+                               case R_68K_GOTOFF:
+                                       got_needed = 1;
+                                       continue;
+#endif
 
 #elif defined(__sh__)
-                       case R_SH_GOT32:
-                               got_allocate = 1; 
-                               break;
+                               case R_SH_GOT32:
+                                       got_allocate = 1;
+                                       break;
 
-                       case R_SH_GOTPC:
-                       case R_SH_GOTOFF:
-                               got_needed = 1;
-                               continue;
+                               case R_SH_GOTPC:
+                               case R_SH_GOTOFF:
+                                       got_needed = 1;
+                                       continue;
 
 #elif defined (__v850e__)
-                       case R_V850_22_PCREL:
-                               plt_needed = 1;
-                               break;
+                               case R_V850_22_PCREL:
+                                       plt_needed = 1;
+                                       break;
 
 #endif
-                       default:
-                               continue;
+                               default:
+                                       continue;
                        }
 
                        if (extsym->st_name != 0) {
@@ -1411,22 +1696,22 @@ static void arch_create_got(struct obj_file *f)
 #if defined(CONFIG_USE_GOT_ENTRIES)
                        if (got_allocate) {
                                got_offset += arch_single_init(
-                                       rel, &intsym->gotent, 
-                                       got_offset, CONFIG_GOT_ENTRY_SIZE);
+                                               rel, &intsym->gotent,
+                                               got_offset, CONFIG_GOT_ENTRY_SIZE);
 
                                got_needed = 1;
                        }
 #endif
 #if defined(CONFIG_USE_PLT_ENTRIES)
                        if (plt_allocate) {
-#if defined(CONFIG_USE_PLT_LIST) 
+#if defined(CONFIG_USE_PLT_LIST)
                                plt_offset += arch_list_add(
-                                       rel, &intsym->pltent, 
-                                       plt_offset, CONFIG_PLT_ENTRY_SIZE);
+                                               rel, &intsym->pltent,
+                                               plt_offset, CONFIG_PLT_ENTRY_SIZE);
 #else
                                plt_offset += arch_single_init(
-                                       rel, &intsym->pltent, 
-                                       plt_offset, CONFIG_PLT_ENTRY_SIZE);
+                                               rel, &intsym->pltent,
+                                               plt_offset, CONFIG_PLT_ENTRY_SIZE);
 #endif
                                plt_needed = 1;
                        }
@@ -1437,27 +1722,20 @@ static void arch_create_got(struct obj_file *f)
 #if defined(CONFIG_USE_GOT_ENTRIES)
        if (got_needed) {
                ifile->got = arch_xsect_init(f, ".got", got_offset,
-                                           CONFIG_GOT_ENTRY_SIZE);
+                               CONFIG_GOT_ENTRY_SIZE);
        }
 #endif
 
 #if defined(CONFIG_USE_PLT_ENTRIES)
        if (plt_needed) {
                ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
-                                           CONFIG_PLT_ENTRY_SIZE);
+                               CONFIG_PLT_ENTRY_SIZE);
        }
 #endif
 
 #endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
 }
 
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
-static int arch_init_module(struct obj_file *f, struct new_module *mod)
-{
-       return 1;
-}
-#endif
-
 /*======================================================================*/
 
 /* Standard ELF hash function.  */
@@ -1546,15 +1824,15 @@ obj_add_symbol(struct obj_file *f, const char *name,
 {
        struct obj_symbol *sym;
        unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
-       int n_type = ELFW(ST_TYPE) (info);
-       int n_binding = ELFW(ST_BIND) (info);
+       int n_type = ELF_ST_TYPE(info);
+       int n_binding = ELF_ST_BIND(info);
 
        for (sym = f->symtab[hash]; sym; sym = sym->next)
                if (f->symbol_cmp(sym->name, name) == 0) {
                        int o_secidx = sym->secidx;
                        int o_info = sym->info;
-                       int o_type = ELFW(ST_TYPE) (o_info);
-                       int o_binding = ELFW(ST_BIND) (o_info);
+                       int o_type = ELF_ST_TYPE(o_info);
+                       int o_binding = ELF_ST_BIND(o_info);
 
                        /* A redefinition!  Is it legal?  */
 
@@ -1595,16 +1873,16 @@ obj_add_symbol(struct obj_file *f, const char *name,
                        /* Don't unify COMMON symbols with object types the programmer
                           doesn't expect.  */
                        else if (secidx == SHN_COMMON
-                                        && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
+                                       && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
                                return sym;
                        else if (o_secidx == SHN_COMMON
-                                        && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
+                                       && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
                                goto found;
                        else {
                                /* 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", name);
+                                       bb_error_msg("%s multiply defined", name);
                                return sym;
                        }
                }
@@ -1615,15 +1893,15 @@ obj_add_symbol(struct obj_file *f, const char *name,
        f->symtab[hash] = sym;
        sym->ksymidx = -1;
 
-       if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
+       if (ELF_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",
+                       bb_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:
+found:
        sym->name = name;
        sym->value = value;
        sym->size = size;
@@ -1679,7 +1957,7 @@ static int obj_load_order_prio(struct obj_section *a)
 
        ac = 0;
        if (a->name[0] != '.' || strlen(a->name) != 10 ||
-               strcmp(a->name + 5, ".init"))
+                       strcmp(a->name + 5, ".init"))
                ac |= 32;
        if (af & SHF_ALLOC)
                ac |= 16;
@@ -1763,7 +2041,7 @@ static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
 static void *obj_extend_section(struct obj_section *sec, unsigned long more)
 {
        unsigned long oldsize = sec->header.sh_size;
-       if (more) { 
+       if (more) {
                sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
        }
        return sec->contents + oldsize;
@@ -1774,8 +2052,7 @@ static void *obj_extend_section(struct obj_section *sec, unsigned long more)
    new module.  */
 
 static int
-add_symbols_from(
-                                struct obj_file *f,
+add_symbols_from( struct obj_file *f,
                                 int idx, struct new_module_symbol *syms, size_t nsyms)
 {
        struct new_module_symbol *s;
@@ -1785,14 +2062,34 @@ add_symbols_from(
        char *name_buf = 0;
        size_t name_alloced_size = 0;
 #endif
+#ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
+       int gpl;
 
+       gpl = obj_gpl_license(f, NULL) == 0;
+#endif
        for (i = 0, s = syms; i < nsyms; ++i, ++s) {
                /* Only add symbols that are already marked external.
                   If we override locals we may cause problems for
                   argument initialization.  We will also create a false
                   dependency on the module.  */
                struct obj_symbol *sym;
-               char *name = (char *)s->name;
+               char *name;
+
+               /* GPL licensed modules can use symbols exported with
+                * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
+                * exported names.  Non-GPL modules never see any GPLONLY_
+                * symbols so they cannot fudge it by adding the prefix on
+                * their references.
+                */
+               if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
+#ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
+                       if (gpl)
+                               s->name += 8;
+                       else
+#endif
+                               continue;
+               }
+               name = (char *)s->name;
 
 #ifdef SYMBOL_PREFIX
                /* Prepend SYMBOL_PREFIX to the symbol's name (the
@@ -1807,440 +2104,75 @@ add_symbols_from(
                strcpy (name_buf, SYMBOL_PREFIX);
                strcpy (name_buf + extra - 1, name);
                name = name_buf;
-#endif /* SYMBOL_PREFIX */
-
-               sym = obj_find_symbol(f, name);
-               if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
-#ifdef SYMBOL_PREFIX
-                       /* Put NAME_BUF into more permanent storage.  */
-                       name = xmalloc (name_size);
-                       strcpy (name, name_buf);
-#endif
-                       sym = obj_add_symbol(f, name, -1,
-                                            ELFW(ST_INFO) (STB_GLOBAL,
-                                                           STT_NOTYPE),
-                                            idx, s->value, 0);
-                       /* Did our symbol just get installed?  If so, mark the
-                          module as "used".  */
-                       if (sym->secidx == idx)
-                               used = 1;
-               }
-       }
-
-       return used;
-}
-
-static void add_kernel_symbols(struct obj_file *f)
-{
-       struct external_module *m;
-       int i, nused = 0;
-
-       /* Add module symbols first.  */
-
-       for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
-               if (m->nsyms
-                       && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
-                                                               m->nsyms)) m->used = 1, ++nused;
-
-       n_ext_modules_used = nused;
-
-       /* And finally the symbols from the kernel proper.  */
-
-       if (nksyms)
-               add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
-}
-
-static char *get_modinfo_value(struct obj_file *f, const char *key)
-{
-       struct obj_section *sec;
-       char *p, *v, *n, *ep;
-       size_t klen = strlen(key);
-
-       sec = obj_find_section(f, ".modinfo");
-       if (sec == NULL)
-               return NULL;
-       p = sec->contents;
-       ep = p + sec->header.sh_size;
-       while (p < ep) {
-               v = strchr(p, '=');
-               n = strchr(p, '\0');
-               if (v) {
-                       if (p + klen == v && strncmp(p, key, klen) == 0)
-                               return v + 1;
-               } else {
-                       if (p + klen == n && strcmp(p, key) == 0)
-                               return n;
-               }
-               p = n + 1;
-       }
-
-       return NULL;
-}
-
-
-/*======================================================================*/
-/* Functions relating to module loading in pre 2.1 kernels.  */
-
-static int
-old_process_module_arguments(struct obj_file *f, int argc, char **argv)
-{
-       while (argc > 0) {
-               char *p, *q;
-               struct obj_symbol *sym;
-               int *loc;
-
-               p = *argv;
-               if ((q = strchr(p, '=')) == NULL) {
-                       argc--;
-                       continue;
-                }
-               *q++ = '\0';
-
-               sym = obj_find_symbol(f, p);
-
-               /* Also check that the parameter was not resolved from the kernel.  */
-               if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
-                       error_msg("symbol for parameter %s not found", p);
-                       return 0;
-               }
-
-               loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
-
-               /* Do C quoting if we begin with a ".  */
-               if (*q == '"') {
-                       char *r, *str;
-
-                       str = alloca(strlen(q));
-                       for (r = str, q++; *q != '"'; ++q, ++r) {
-                               if (*q == '\0') {
-                                       error_msg("improperly terminated string argument for %s", p);
-                                       return 0;
-                               } else if (*q == '\\')
-                                       switch (*++q) {
-                                       case 'a':
-                                               *r = '\a';
-                                               break;
-                                       case 'b':
-                                               *r = '\b';
-                                               break;
-                                       case 'e':
-                                               *r = '\033';
-                                               break;
-                                       case 'f':
-                                               *r = '\f';
-                                               break;
-                                       case 'n':
-                                               *r = '\n';
-                                               break;
-                                       case 'r':
-                                               *r = '\r';
-                                               break;
-                                       case 't':
-                                               *r = '\t';
-                                               break;
-
-                                       case '0':
-                                       case '1':
-                                       case '2':
-                                       case '3':
-                                       case '4':
-                                       case '5':
-                                       case '6':
-                                       case '7':
-                                               {
-                                                       int c = *q - '0';
-                                                       if (q[1] >= '0' && q[1] <= '7') {
-                                                               c = (c * 8) + *++q - '0';
-                                                               if (q[1] >= '0' && q[1] <= '7')
-                                                                       c = (c * 8) + *++q - '0';
-                                                       }
-                                                       *r = c;
-                                               }
-                                               break;
-
-                                       default:
-                                               *r = *q;
-                                               break;
-                               } else
-                                       *r = *q;
-                       }
-                       *r = '\0';
-                       obj_string_patch(f, sym->secidx, sym->value, str);
-               } else if (*q >= '0' && *q <= '9') {
-                       do
-                               *loc++ = strtoul(q, &q, 0);
-                       while (*q++ == ',');
-               } else {
-                       char *contents = f->sections[sym->secidx]->contents;
-                       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, myloc - contents, q);
-                               myloc += sizeof(char *);
-                               q = r;
-                       }
-
-                       /* last part */
-                       obj_string_patch(f, sym->secidx, myloc - contents, q);
-               }
-
-               argc--, argv++;
-       }
-
-       return 1;
-}
-
-#ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
-static int old_is_module_checksummed(struct obj_file *f)
-{
-       return obj_find_symbol(f, "Using_Versions") != NULL;
-}
-/* Get the module's kernel version in the canonical integer form.  */
-
-static int
-old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
-{
-       struct obj_symbol *sym;
-       char *p, *q;
-       int a, b, c;
-
-       sym = obj_find_symbol(f, "kernel_version");
-       if (sym == NULL)
-               return -1;
-
-       p = f->sections[sym->secidx]->contents + sym->value;
-       safe_strncpy(str, p, STRVERSIONLEN);
-
-       a = strtoul(p, &p, 10);
-       if (*p != '.')
-               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;
-}
-
-#endif   /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
-
-#ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
-
-/* Fetch all the symbols and divvy them up as appropriate for the modules.  */
-
-static int old_get_kernel_symbols(const char *m_name)
-{
-       struct old_kernel_sym *ks, *k;
-       struct new_module_symbol *s;
-       struct external_module *mod;
-       int nks, nms, nmod, i;
-
-       nks = get_kernel_syms(NULL);
-       if (nks <= 0) {
-               if (nks)
-                       perror_msg("get_kernel_syms: %s", m_name);
-               else
-                       error_msg("No kernel symbols");
-               return 0;
-       }
-
-       ks = k = xmalloc(nks * sizeof(*ks));
-
-       if (get_kernel_syms(ks) != nks) {
-               perror("inconsistency with get_kernel_syms -- is someone else "
-                          "playing with modules?");
-               free(ks);
-               return 0;
-       }
-
-       /* Collect the module information.  */
-
-       mod = NULL;
-       nmod = -1;
-
-       while (k->name[0] == '#' && k->name[1]) {
-               struct old_kernel_sym *k2;
-
-               /* Find out how many symbols this module has.  */
-               for (k2 = k + 1; k2->name[0] != '#'; ++k2)
-                       continue;
-               nms = k2 - k - 1;
-
-               mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
-               mod[nmod].name = k->name + 1;
-               mod[nmod].addr = k->value;
-               mod[nmod].used = 0;
-               mod[nmod].nsyms = nms;
-               mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
-
-               for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
-                       s->name = (unsigned long) k->name;
-                       s->value = k->value;
-               }
-
-               k = k2;
-       }
-
-       ext_modules = mod;
-       n_ext_modules = nmod + 1;
-
-       /* Now collect the symbols for the kernel proper.  */
-
-       if (k->name[0] == '#')
-               ++k;
-
-       nksyms = nms = nks - (k - ks);
-       ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
-
-       for (i = 0; i < nms; ++i, ++s, ++k) {
-               s->name = (unsigned long) k->name;
-               s->value = k->value;
-       }
-
-       return 1;
-}
-
-/* Return the kernel symbol checksum version, or zero if not used.  */
-
-static int old_is_kernel_checksummed(void)
-{
-       /* Using_Versions is the first symbol.  */
-       if (nksyms > 0
-               && strcmp((char *) ksyms[0].name,
-                                 "Using_Versions") == 0) return ksyms[0].value;
-       else
-               return 0;
-}
-
-
-static int old_create_mod_use_count(struct obj_file *f)
-{
-       struct obj_section *sec;
-
-       sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
-                                                                                  sizeof(long));
-
-       obj_add_symbol(f, "mod_use_count_", -1,
-                                  ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
-                                  sizeof(long));
-
-       return 1;
-}
-
-static int
-old_init_module(const char *m_name, struct obj_file *f,
-                               unsigned long m_size)
-{
-       char *image;
-       struct old_mod_routines routines;
-       struct old_symbol_table *symtab;
-       int ret;
-
-       /* Create the symbol table */
-       {
-               int nsyms = 0, strsize = 0, total;
-
-               /* Size things first... */
-               if (flag_export) {
-                       int i;
-                       for (i = 0; i < HASH_BUCKETS; ++i) {
-                               struct obj_symbol *sym;
-                               for (sym = f->symtab[i]; sym; sym = sym->next)
-                                       if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
-                                               && sym->secidx <= SHN_HIRESERVE) 
-                                       {
-                                               sym->ksymidx = nsyms++;
-                                               strsize += strlen(sym->name) + 1;
-                                       }
-                       }
-               }
-
-               total = (sizeof(struct old_symbol_table)
-                                + nsyms * sizeof(struct old_module_symbol)
-                                + n_ext_modules_used * sizeof(struct old_module_ref)
-                                + strsize);
-               symtab = xmalloc(total);
-               symtab->size = total;
-               symtab->n_symbols = nsyms;
-               symtab->n_refs = n_ext_modules_used;
-
-               if (flag_export && nsyms) {
-                       struct old_module_symbol *ksym;
-                       char *str;
-                       int i;
-
-                       ksym = symtab->symbol;
-                       str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
-                                  + n_ext_modules_used * sizeof(struct old_module_ref));
-
-                       for (i = 0; i < HASH_BUCKETS; ++i) {
-                               struct obj_symbol *sym;
-                               for (sym = f->symtab[i]; sym; sym = sym->next)
-                                       if (sym->ksymidx >= 0) {
-                                               ksym->addr = obj_symbol_final_value(f, sym);
-                                               ksym->name =
-                                                       (unsigned long) str - (unsigned long) symtab;
-
-                                               strcpy(str, sym->name);
-                                               str += strlen(sym->name) + 1;
-                                               ksym++;
-                                       }
-                       }
-               }
-
-               if (n_ext_modules_used) {
-                       struct old_module_ref *ref;
-                       int i;
-
-                       ref = (struct old_module_ref *)
-                               ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
+#endif /* SYMBOL_PREFIX */
 
-                       for (i = 0; i < n_ext_modules; ++i)
-                               if (ext_modules[i].used)
-                                       ref++->module = ext_modules[i].addr;
+               sym = obj_find_symbol(f, name);
+               if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
+#ifdef SYMBOL_PREFIX
+                       /* Put NAME_BUF into more permanent storage.  */
+                       name = xmalloc (name_size);
+                       strcpy (name, name_buf);
+#endif
+                       sym = obj_add_symbol(f, name, -1,
+                                       ELF_ST_INFO(STB_GLOBAL,
+                                               STT_NOTYPE),
+                                       idx, s->value, 0);
+                       /* Did our symbol just get installed?  If so, mark the
+                          module as "used".  */
+                       if (sym->secidx == idx)
+                               used = 1;
                }
        }
 
-       /* Fill in routines.  */
+       return used;
+}
 
-       routines.init =
-               obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
-       routines.cleanup =
-               obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
+static void add_kernel_symbols(struct obj_file *f)
+{
+       struct external_module *m;
+       int i, nused = 0;
 
-       /* Whew!  All of the initialization is complete.  Collect the final
-          module image and give it to the kernel.  */
+       /* Add module symbols first.  */
 
-       image = xmalloc(m_size);
-       obj_create_image(f, image);
+       for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
+               if (m->nsyms
+                               && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
+                                       m->nsyms)) m->used = 1, ++nused;
 
-       /* image holds the complete relocated module, accounting correctly for
-          mod_use_count.  However the old module kernel support assume that
-          it is receiving something which does not contain mod_use_count.  */
-       ret = old_sys_init_module(m_name, image + sizeof(long),
-                                                         m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
-                                                                               : 0), &routines, symtab);
-       if (ret)
-               perror_msg("init_module: %s", m_name);
+       n_ext_modules_used = nused;
 
-       free(image);
-       free(symtab);
+       /* And finally the symbols from the kernel proper.  */
 
-       return ret == 0;
+       if (nksyms)
+               add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
 }
 
-#else
-
-#define old_create_mod_use_count(x) TRUE
-#define old_init_module(x, y, z) TRUE
+static char *get_modinfo_value(struct obj_file *f, const char *key)
+{
+       struct obj_section *sec;
+       char *p, *v, *n, *ep;
+       size_t klen = strlen(key);
 
-#endif                                                 /* CONFIG_FEATURE_OLD_MODULE_INTERFACE */
+       sec = obj_find_section(f, ".modinfo");
+       if (sec == NULL)
+               return NULL;
+       p = sec->contents;
+       ep = p + sec->header.sh_size;
+       while (p < ep) {
+               v = strchr(p, '=');
+               n = strchr(p, '\0');
+               if (v) {
+                       if (p + klen == v && strncmp(p, key, klen) == 0)
+                               return v + 1;
+               } else {
+                       if (p + klen == n && strcmp(p, key) == 0)
+                               return n;
+               }
+               p = n + 1;
+       }
 
+       return NULL;
+}
 
 
 /*======================================================================*/
@@ -2259,7 +2191,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
                if ((q = strchr(p, '=')) == NULL) {
                        argc--;
                        continue;
-                }
+               }
 
                key = alloca(q - p + 6);
                memcpy(key, "parm_", 5);
@@ -2269,7 +2201,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", key);
+                       bb_error_msg("invalid parameter %s", key);
                        return 0;
                }
 
@@ -2284,7 +2216,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", key);
+                       bb_error_msg("symbol for parameter %s not found", key);
                        return 0;
                }
 
@@ -2312,57 +2244,57 @@ 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",
+                                                       bb_error_msg("improperly terminated string argument for %s",
                                                                        key);
                                                        return 0;
                                                } else if (*q == '\\')
                                                        switch (*++q) {
-                                                       case 'a':
-                                                               *r = '\a';
-                                                               break;
-                                                       case 'b':
-                                                               *r = '\b';
-                                                               break;
-                                                       case 'e':
-                                                               *r = '\033';
-                                                               break;
-                                                       case 'f':
-                                                               *r = '\f';
-                                                               break;
-                                                       case 'n':
-                                                               *r = '\n';
-                                                               break;
-                                                       case 'r':
-                                                               *r = '\r';
-                                                               break;
-                                                       case 't':
-                                                               *r = '\t';
-                                                               break;
-
-                                                       case '0':
-                                                       case '1':
-                                                       case '2':
-                                                       case '3':
-                                                       case '4':
-                                                       case '5':
-                                                       case '6':
-                                                       case '7':
-                                                               {
-                                                                       int c = *q - '0';
-                                                                       if (q[1] >= '0' && q[1] <= '7') {
-                                                                               c = (c * 8) + *++q - '0';
-                                                                               if (q[1] >= '0' && q[1] <= '7')
+                                                               case 'a':
+                                                                       *r = '\a';
+                                                                       break;
+                                                               case 'b':
+                                                                       *r = '\b';
+                                                                       break;
+                                                               case 'e':
+                                                                       *r = '\033';
+                                                                       break;
+                                                               case 'f':
+                                                                       *r = '\f';
+                                                                       break;
+                                                               case 'n':
+                                                                       *r = '\n';
+                                                                       break;
+                                                               case 'r':
+                                                                       *r = '\r';
+                                                                       break;
+                                                               case 't':
+                                                                       *r = '\t';
+                                                                       break;
+
+                                                               case '0':
+                                                               case '1':
+                                                               case '2':
+                                                               case '3':
+                                                               case '4':
+                                                               case '5':
+                                                               case '6':
+                                                               case '7':
+                                                                       {
+                                                                               int c = *q - '0';
+                                                                               if (q[1] >= '0' && q[1] <= '7') {
                                                                                        c = (c * 8) + *++q - '0';
+                                                                                       if (q[1] >= '0' && q[1] <= '7')
+                                                                                               c = (c * 8) + *++q - '0';
+                                                                               }
+                                                                               *r = c;
                                                                        }
-                                                                       *r = c;
-                                                               }
-                                                               break;
+                                                                       break;
 
-                                                       default:
+                                                               default:
+                                                                       *r = *q;
+                                                                       break;
+                                                       } else
                                                                *r = *q;
-                                                               break;
-                                               } else
-                                                       *r = *q;
                                        }
                                        *r = '\0';
                                        ++q;
@@ -2382,8 +2314,8 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
                                                str = alloca(r - q + 1);
                                                memcpy(str, q, r - q);
 
-                                               /* I don't know if it is usefull, as the previous case
-                                                  doesn't null terminate the string ??? */
+                                               /* I don't know if it is useful, as the previous case
+                                                  doesn't nul terminate the string ??? */
                                                str[r - q] = '\0';
 
                                                /* Keep next fields */
@@ -2406,7 +2338,7 @@ 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))) {
-                                               error_msg("parameter type 'c' for %s must be followed by"
+                                               bb_error_msg("parameter type 'c' for %s must be followed by"
                                                                " the maximum size", key);
                                                return 0;
                                        }
@@ -2414,7 +2346,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
 
                                        /* Check length */
                                        if (strlen(str) >= charssize) {
-                                               error_msg("string too long for %s (max %ld)", key,
+                                               bb_error_msg("string too long for %s (max %ld)", key,
                                                                charssize - 1);
                                                return 0;
                                        }
@@ -2426,57 +2358,57 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
                        } else {
                                long v = strtoul(q, &q, 0);
                                switch (*p) {
-                               case 'b':
-                                       *loc++ = v;
-                                       break;
-                               case 'h':
-                                       *(short *) loc = v;
-                                       loc += tgt_sizeof_short;
-                                       break;
-                               case 'i':
-                                       *(int *) loc = v;
-                                       loc += tgt_sizeof_int;
-                                       break;
-                               case 'l':
-                                       *(long *) loc = v;
-                                       loc += tgt_sizeof_long;
-                                       break;
+                                       case 'b':
+                                               *loc++ = v;
+                                               break;
+                                       case 'h':
+                                               *(short *) loc = v;
+                                               loc += tgt_sizeof_short;
+                                               break;
+                                       case 'i':
+                                               *(int *) loc = v;
+                                               loc += tgt_sizeof_int;
+                                               break;
+                                       case 'l':
+                                               *(long *) loc = v;
+                                               loc += tgt_sizeof_long;
+                                               break;
 
-                               default:
-                                       error_msg("unknown parameter type '%c' for %s", *p, key);
-                                       return 0;
+                                       default:
+                                               bb_error_msg("unknown parameter type '%c' for %s", *p, key);
+                                               return 0;
                                }
                        }
 
-                 retry_end_of_value:
+retry_end_of_value:
                        switch (*q) {
-                       case '\0':
-                               goto end_of_arg;
-
-                       case ' ':
-                       case '\t':
-                       case '\n':
-                       case '\r':
-                               ++q;
-                               goto retry_end_of_value;
-
-                       case ',':
-                               if (++n > max) {
-                                       error_msg("too many values for %s (max %d)", key, max);
-                                       return 0;
-                               }
-                               ++q;
-                               break;
+                               case '\0':
+                                       goto end_of_arg;
 
-                       default:
-                               error_msg("invalid argument syntax for %s", key);
-                               return 0;
+                               case ' ':
+                               case '\t':
+                               case '\n':
+                               case '\r':
+                                       ++q;
+                                       goto retry_end_of_value;
+
+                               case ',':
+                                       if (++n > max) {
+                                               bb_error_msg("too many values for %s (max %d)", key, max);
+                                               return 0;
+                                       }
+                                       ++q;
+                                       break;
+
+                               default:
+                                       bb_error_msg("invalid argument syntax for %s", key);
+                                       return 0;
                        }
                }
 
-         end_of_arg:
+end_of_arg:
                if (n < min) {
-                       error_msg("too few values for %s (min %d)", key, min);
+                       bb_error_msg("too few values for %s (min %d)", key, min);
                        return 0;
                }
 
@@ -2525,8 +2457,6 @@ new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
 #endif   /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
 
 
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
-
 /* Fetch the loaded modules, and all currently exported symbols.  */
 
 static int new_get_kernel_symbols(void)
@@ -2539,13 +2469,13 @@ static int new_get_kernel_symbols(void)
        /* Collect the loaded modules.  */
 
        module_names = xmalloc(bufsize = 256);
-  retry_modules_load:
+retry_modules_load:
        if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
                if (errno == ENOSPC && bufsize < ret) {
                        module_names = xrealloc(module_names, bufsize = ret);
                        goto retry_modules_load;
                }
-               perror_msg("QM_MODULES");
+               bb_perror_msg("QM_MODULES");
                return 0;
        }
 
@@ -2557,40 +2487,40 @@ static int new_get_kernel_symbols(void)
                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) {
+                               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);
+                               bb_perror_msg("query_module: QM_INFO: %s", mn);
                                return 0;
                        }
-       
+
                        syms = xmalloc(bufsize = 1024);
-                 retry_mod_sym_load:
+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;
+                                       case ENOSPC:
+                                               syms = xrealloc(syms, bufsize = ret);
+                                               goto retry_mod_sym_load;
+                                       case ENOENT:
+                                               /* The module was removed out from underneath us.  */
+                                               continue;
+                                       default:
+                                               bb_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;
                        }
@@ -2600,13 +2530,13 @@ static int new_get_kernel_symbols(void)
        /* Collect the kernel's symbols.  */
 
        syms = xmalloc(bufsize = 16 * 1024);
-  retry_kern_sym_load:
+retry_kern_sym_load:
        if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
                if (errno == ENOSPC && bufsize < ret) {
                        syms = xrealloc(syms, bufsize = ret);
                        goto retry_kern_sym_load;
                }
-               perror_msg("kernel: QM_SYMBOLS");
+               bb_perror_msg("kernel: QM_SYMBOLS");
                return 0;
        }
        nksyms = nsyms = ret;
@@ -2641,15 +2571,15 @@ static int new_create_this_module(struct obj_file *f, const char *m_name)
        struct obj_section *sec;
 
        sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
-                                                                                  sizeof(struct new_module));
+                       sizeof(struct new_module));
        memset(sec->contents, 0, sizeof(struct new_module));
 
        obj_add_symbol(f, SPFX "__this_module", -1,
-                      ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
-                      sizeof(struct new_module));
+                       ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
+                       sizeof(struct new_module));
 
        obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
-                                        m_name);
+                       m_name);
 
        return 1;
 }
@@ -2673,12 +2603,12 @@ static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
        }
        if (!sec)
                sec = obj_create_alloced_section(f, "__ksymtab",
-                                                tgt_sizeof_void_p, 0);
+                               tgt_sizeof_void_p, 0);
        if (!sec)
                return;
        sec->header.sh_flags |= SHF_ALLOC;
        sec->header.sh_addralign = tgt_sizeof_void_p;   /* Empty section might
-                                                          be byte-aligned */
+                                                                                                          be byte-aligned */
        ofs = sec->header.sh_size;
        obj_symbol_patch(f, sec->idx, ofs, sym);
        obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
@@ -2698,8 +2628,8 @@ static int new_create_module_ksymtab(struct obj_file *f)
                struct obj_symbol *tm;
 
                sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
-                                                                                (sizeof(struct new_module_ref)
-                                                                                 * n_ext_modules_used));
+                               (sizeof(struct new_module_ref)
+                                * n_ext_modules_used));
                if (!sec)
                        return 0;
 
@@ -2709,7 +2639,7 @@ static int new_create_module_ksymtab(struct obj_file *f)
                        if (ext_modules[i].used) {
                                dep->dep = ext_modules[i].addr;
                                obj_symbol_patch(f, sec->idx,
-                                                                (char *) &dep->ref - sec->contents, tm);
+                                               (char *) &dep->ref - sec->contents, tm);
                                dep->next_ref = 0;
                                ++dep;
                        }
@@ -2721,7 +2651,7 @@ static int new_create_module_ksymtab(struct obj_file *f)
 
                sec =
                        obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
-                                                                          0);
+                                       0);
 
                /* We don't want to export symbols residing in sections that
                   aren't loaded.  There are a number of these created so that
@@ -2734,15 +2664,15 @@ static int new_create_module_ksymtab(struct obj_file *f)
                for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
                        struct obj_symbol *sym;
                        for (sym = f->symtab[i]; sym; sym = sym->next)
-                               if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
-                                       && sym->secidx <= SHN_HIRESERVE
-                                       && (sym->secidx >= SHN_LORESERVE
-                                               || loaded[sym->secidx])) {
+                               if (ELF_ST_BIND(sym->info) != STB_LOCAL
+                                               && sym->secidx <= SHN_HIRESERVE
+                                               && (sym->secidx >= SHN_LORESERVE
+                                                       || loaded[sym->secidx])) {
                                        ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
 
                                        obj_symbol_patch(f, sec->idx, ofs, sym);
                                        obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
-                                                                        sym->name);
+                                                       sym->name);
 
                                        nsyms++;
                                }
@@ -2756,8 +2686,7 @@ static int new_create_module_ksymtab(struct obj_file *f)
 
 
 static int
-new_init_module(const char *m_name, struct obj_file *f,
-                               unsigned long m_size)
+new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
 {
        struct new_module *module;
        struct obj_section *sec;
@@ -2766,8 +2695,8 @@ 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);
+       if (!sec || !sec->contents) {
+               bb_perror_msg_and_die("corrupt module %s?",m_name);
        }
        module = (struct new_module *) sec->contents;
        m_addr = sec->header.sh_addr;
@@ -2806,8 +2735,8 @@ new_init_module(const char *m_name, struct obj_file *f,
        sec = obj_find_section(f, ".data.init");
        if (sec) {
                if (!module->runsize ||
-                       module->runsize > sec->header.sh_addr - m_addr)
-                               module->runsize = sec->header.sh_addr - m_addr;
+                               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) {
@@ -2820,34 +2749,21 @@ new_init_module(const char *m_name, struct obj_file *f,
                module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
        }
 
-       if (!arch_init_module(f, module))
-               return 0;
-
        /* Whew!  All of the initialization is complete.  Collect the final
           module image and give it to the kernel.  */
 
        image = xmalloc(m_size);
        obj_create_image(f, image);
 
-       ret = new_sys_init_module(m_name, (struct new_module *) image);
+       ret = init_module(m_name, (struct new_module *) image);
        if (ret)
-               perror_msg("init_module: %s", m_name);
+               bb_perror_msg("init_module: %s", m_name);
 
        free(image);
 
        return ret == 0;
 }
 
-#else
-
-#define new_init_module(x, y, z) TRUE
-#define new_create_this_module(x, y) 0
-#define new_add_ksymtab(x, y) -1
-#define new_create_module_ksymtab(x)
-#define query_module(v, w, x, y, z) -1
-
-#endif                                                 /* CONFIG_FEATURE_NEW_MODULE_INTERFACE */
-
 
 /*======================================================================*/
 
@@ -2880,7 +2796,6 @@ obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
        return 1;
 }
 
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
 static int
 obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
                                 struct obj_symbol *sym)
@@ -2896,7 +2811,6 @@ obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
 
        return 1;
 }
-#endif
 
 static int obj_check_undefineds(struct obj_file *f)
 {
@@ -2907,12 +2821,12 @@ static int obj_check_undefineds(struct obj_file *f)
                struct obj_symbol *sym;
                for (sym = f->symtab[i]; sym; sym = sym->next)
                        if (sym->secidx == SHN_UNDEF) {
-                               if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
+                               if (ELF_ST_BIND(sym->info) == STB_WEAK) {
                                        sym->secidx = SHN_ABS;
                                        sym->value = 0;
                                } else {
                                        if (!flag_quiet) {
-                                               error_msg("unresolved symbol %s", sym->name);
+                                               bb_error_msg("unresolved symbol %s", sym->name);
                                        }
                                        ret = 0;
                                }
@@ -3021,8 +2935,8 @@ static void obj_allocate_commons(struct obj_file *f)
                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);
+                               s->contents = memset(xmalloc(s->header.sh_size),
+                                               0, s->header.sh_size);
                        else
                                s->contents = NULL;
 
@@ -3093,12 +3007,12 @@ static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
 
                        /* Attempt to find a value to use for this relocation.  */
 
-                       symndx = ELFW(R_SYM) (rel->r_info);
+                       symndx = ELF_R_SYM(rel->r_info);
                        if (symndx) {
                                /* Note we've already checked for undefined symbols.  */
 
                                extsym = &symtab[symndx];
-                               if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
+                               if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
                                        /* Local symbols we look up in the local table to be sure
                                           we get the one that is really intended.  */
                                        intsym = f->local_symtab[symndx];
@@ -3119,7 +3033,7 @@ static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
 #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
                        /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9.  */
                        if (!extsym || !extsym->st_name ||
-                               ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
+                                       ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
 #endif
                                value += rel->r_addend;
 #endif
@@ -3127,28 +3041,28 @@ static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
                        /* Do it! */
                        switch (arch_apply_relocation
                                        (f, targsec, symsec, intsym, rel, value)) {
-                       case obj_reloc_ok:
-                               break;
+                               case obj_reloc_ok:
+                                       break;
 
-                       case obj_reloc_overflow:
-                               errmsg = "Relocation overflow";
-                               goto bad_reloc;
-                       case obj_reloc_dangerous:
-                               errmsg = "Dangerous relocation";
-                               goto bad_reloc;
-                       case obj_reloc_unhandled:
-                               errmsg = "Unhandled relocation";
-                         bad_reloc:
-                               if (extsym) {
-                                       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", errmsg,
-                                                       (long) ELFW(R_TYPE) (rel->r_info));
-                               }
-                               ret = 0;
-                               break;
+                               case obj_reloc_overflow:
+                                       errmsg = "Relocation overflow";
+                                       goto bad_reloc;
+                               case obj_reloc_dangerous:
+                                       errmsg = "Dangerous relocation";
+                                       goto bad_reloc;
+                               case obj_reloc_unhandled:
+                                       errmsg = "Unhandled relocation";
+bad_reloc:
+                                       if (extsym) {
+                                               bb_error_msg("%s of type %ld for %s", errmsg,
+                                                               (long) ELF_R_TYPE(rel->r_info),
+                                                               strtab + extsym->st_name);
+                                       } else {
+                                               bb_error_msg("%s of type %ld", errmsg,
+                                                               (long) ELF_R_TYPE(rel->r_info));
+                                       }
+                                       ret = 0;
+                                       break;
                        }
                }
        }
@@ -3221,33 +3135,33 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
 
        fseek(fp, 0, SEEK_SET);
        if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
-               perror_msg("error reading ELF header");
+               bb_perror_msg("error reading ELF header");
                return NULL;
        }
 
        if (f->header.e_ident[EI_MAG0] != ELFMAG0
-               || f->header.e_ident[EI_MAG1] != ELFMAG1
-               || f->header.e_ident[EI_MAG2] != ELFMAG2
-               || f->header.e_ident[EI_MAG3] != ELFMAG3) {
-               error_msg("not an ELF file");
+                       || f->header.e_ident[EI_MAG1] != ELFMAG1
+                       || f->header.e_ident[EI_MAG2] != ELFMAG2
+                       || f->header.e_ident[EI_MAG3] != ELFMAG3) {
+               bb_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");
+                       || f->header.e_ident[EI_DATA] != ELFDATAM
+                       || f->header.e_ident[EI_VERSION] != EV_CURRENT
+                       || !MATCH_MACHINE(f->header.e_machine)) {
+               bb_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");
+               bb_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",
+               bb_error_msg("section header size mismatch: %lu != %lu",
                                (unsigned long) f->header.e_shentsize,
                                (unsigned long) sizeof(ElfW(Shdr)));
                return NULL;
@@ -3260,7 +3174,7 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
        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) {
-               perror_msg("error reading ELF section headers");
+               bb_perror_msg("error reading ELF section headers");
                return NULL;
        }
 
@@ -3276,56 +3190,56 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
                sec->idx = i;
 
                if(sec->header.sh_size) switch (sec->header.sh_type) {
-               case SHT_NULL:
-               case SHT_NOTE:
-               case SHT_NOBITS:
-                       /* ignore */
-                       break;
+                       case SHT_NULL:
+                       case SHT_NOTE:
+                       case SHT_NOBITS:
+                               /* ignore */
+                               break;
 
-               case SHT_PROGBITS:
+                       case SHT_PROGBITS:
 #if LOADBITS
-                       if (!loadprogbits) {
-                               sec->contents = NULL;
-                               break;
-                       }
-#endif                 
-               case SHT_SYMTAB:
-               case SHT_STRTAB:
-               case SHT_RELM:
-                       if (sec->header.sh_size > 0) {
-                               sec->contents = xmalloc(sec->header.sh_size);
-                               fseek(fp, sec->header.sh_offset, SEEK_SET);
-                               if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
-                                       perror_msg("error reading ELF section data");
-                                       return NULL;
+                               if (!loadprogbits) {
+                                       sec->contents = NULL;
+                                       break;
                                }
-                       } else {
-                               sec->contents = NULL;
-                       }
-                       break;
+#endif
+                       case SHT_SYMTAB:
+                       case SHT_STRTAB:
+                       case SHT_RELM:
+                               if (sec->header.sh_size > 0) {
+                                       sec->contents = xmalloc(sec->header.sh_size);
+                                       fseek(fp, sec->header.sh_offset, SEEK_SET);
+                                       if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
+                                               bb_perror_msg("error reading ELF section data");
+                                               return NULL;
+                                       }
+                               } else {
+                                       sec->contents = NULL;
+                               }
+                               break;
 
 #if SHT_RELM == SHT_REL
-               case SHT_RELA:
-                       error_msg("RELA relocations not supported on this architecture");
-                       return NULL;
+                       case SHT_RELA:
+                               bb_error_msg("RELA relocations not supported on this architecture");
+                               return NULL;
 #else
-               case SHT_REL:
-                       error_msg("REL relocations not supported on this architecture");
-                       return NULL;
+                       case SHT_REL:
+                               bb_error_msg("REL relocations not supported on this architecture");
+                               return NULL;
 #endif
 
-               default:
-                       if (sec->header.sh_type >= SHT_LOPROC) {
-                               /* Assume processor specific section types are debug
-                                  info and can safely be ignored.  If this is ever not
-                                  the case (Hello MIPS?), don't put ifdefs here but
-                                  create an arch_load_proc_section().  */
-                               break;
-                       }
+                       default:
+                               if (sec->header.sh_type >= SHT_LOPROC) {
+                                       /* Assume processor specific section types are debug
+                                          info and can safely be ignored.  If this is ever not
+                                          the case (Hello MIPS?), don't put ifdefs here but
+                                          create an arch_load_proc_section().  */
+                                       break;
+                               }
 
-                       error_msg("can't handle sections of type %ld",
-                                       (long) sec->header.sh_type);
-                       return NULL;
+                               bb_error_msg("can't handle sections of type %ld",
+                                               (long) sec->header.sh_type);
+                               return NULL;
                }
        }
 
@@ -3351,53 +3265,67 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
                        obj_insert_section_load_order(f, sec);
 
                switch (sec->header.sh_type) {
-               case SHT_SYMTAB:
-                       {
-                               unsigned long nsym, j;
-                               char *strtab;
-                               ElfW(Sym) * sym;
-
-                               if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
-                                       error_msg("symbol size mismatch: %lu != %lu",
-                                                       (unsigned long) sec->header.sh_entsize,
-                                                       (unsigned long) sizeof(ElfW(Sym)));
-                                       return NULL;
-                               }
-
-                               nsym = sec->header.sh_size / sizeof(ElfW(Sym));
-                               strtab = f->sections[sec->header.sh_link]->contents;
-                               sym = (ElfW(Sym) *) sec->contents;
-
-                               /* Allocate space for a table of local symbols.  */
-                               j = f->local_symtab_size = sec->header.sh_info;
-                               f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
+                       case SHT_SYMTAB:
+                               {
+                                       unsigned long nsym, j;
+                                       char *strtab;
+                                       ElfW(Sym) * sym;
+
+                                       if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
+                                               bb_error_msg("symbol size mismatch: %lu != %lu",
+                                                               (unsigned long) sec->header.sh_entsize,
+                                                               (unsigned long) sizeof(ElfW(Sym)));
+                                               return NULL;
+                                       }
 
-                               /* 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
-                                               name = f->sections[sym->st_shndx]->name;
+                                       nsym = sec->header.sh_size / sizeof(ElfW(Sym));
+                                       strtab = f->sections[sec->header.sh_link]->contents;
+                                       sym = (ElfW(Sym) *) sec->contents;
+
+                                       /* Allocate space for a table of local symbols.  */
+                                       j = f->local_symtab_size = sec->header.sh_info;
+                                       f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
+
+                                       /* Insert all symbols into the hash table.  */
+                                       for (j = 1, ++sym; j < nsym; ++j, ++sym) {
+                                               ElfW(Addr) val = sym->st_value;
+                                               const char *name;
+                                               if (sym->st_name)
+                                                       name = strtab + sym->st_name;
+                                               else if (sym->st_shndx < shnum)
+                                                       name = f->sections[sym->st_shndx]->name;
+                                               else
+                                                       continue;
+
+#if defined(__SH5__)
+                                               /*
+                                                * For sh64 it is possible that the target of a branch
+                                                * requires a mode switch (32 to 16 and back again).
+                                                *
+                                                * This is implied by the lsb being set in the target
+                                                * address for SHmedia mode and clear for SHcompact.
+                                                */
+                                               val |= sym->st_other & 4;
+#endif
 
-                                       obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
-                                                                  sym->st_value, sym->st_size);
+                                               obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
+                                                               val, sym->st_size);
+                                       }
                                }
-                       }
-                       break;
+                               break;
 
-               case SHT_RELM:
-                       if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
-                               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(). */
+                       case SHT_RELM:
+                               if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
+                                       bb_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(). */
                }
        }
 
@@ -3414,20 +3342,20 @@ static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
 {
        ElfW(Addr) base = f->baseaddr;
        struct obj_section* sec;
-       
+
        for (sec = f->load_order; sec; sec = sec->load_next) {
 
                /* section already loaded? */
                if (sec->contents != NULL)
                        continue;
-               
+
                if (sec->header.sh_size == 0)
                        continue;
 
                sec->contents = imagebase + (sec->header.sh_addr - base);
                fseek(fp, sec->header.sh_offset, SEEK_SET);
                if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
-                       error_msg("error reading ELF section data: %s\n", strerror(errno));
+                       bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
                        return 0;
                }
 
@@ -3451,9 +3379,10 @@ static void hide_special_symbols(struct obj_file *f)
        for (p = specials; *p; ++p)
                if ((sym = obj_find_symbol(f, *p)) != NULL)
                        sym->info =
-                               ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
+                               ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
 }
 
+
 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
 static int obj_gpl_license(struct obj_file *f, const char **license)
 {
@@ -3500,7 +3429,7 @@ static int obj_gpl_license(struct obj_file *f, const char **license)
 #define TAINT_UNSAFE_SMP                (1<<2)
 #define TAINT_URL                                              "http://www.tux.org/lkml/#export-tainted"
 
-static void set_tainted(struct obj_file *f, int fd, char *m_name, 
+static void set_tainted(struct obj_file *f, int fd, char *m_name,
                int kernel_has_tainted, int taint, const char *text1, const char *text2)
 {
        char buf[80];
@@ -3579,13 +3508,10 @@ static int
 get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
 {
 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
-  if (get_modinfo_value(f, "kernel_version") == NULL)
-    return old_get_module_version(f, str);
-  else
-    return new_get_module_version(f, str);
+       return new_get_module_version(f, str);
 #else  /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
-    strncpy(str, "???", sizeof(str));
-    return -1;
+       strncpy(str, "???", sizeof(str));
+       return -1;
 #endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
 }
 
@@ -3593,7 +3519,7 @@ get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
  * start of some sections.  this info is used by ksymoops to do better
  * debugging.
  */
-static void 
+static void
 add_ksymoops_symbols(struct obj_file *f, const char *filename,
                                 const char *m_name)
 {
@@ -3609,19 +3535,19 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
                ".text",
                ".rodata",
                ".data",
-               ".bss"
+               ".bss",
                ".sbss"
        };
 
        if (realpath(filename, real)) {
-               absolute_filename = xstrdup(real);
+               absolute_filename = bb_xstrdup(real);
        }
        else {
                int save_errno = errno;
-               error_msg("cannot get realpath for %s", filename);
+               bb_error_msg("cannot get realpath for %s", filename);
                errno = save_errno;
                perror("");
-               absolute_filename = xstrdup(filename);
+               absolute_filename = bb_xstrdup(filename);
        }
 
        lm_name = strlen(m_name);
@@ -3640,27 +3566,27 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
                 * one symbol is less readable but saves kernel space.
                 */
                l = sizeof(symprefix)+                  /* "__insmod_" */
-                   lm_name+                            /* module name */
-                   2+                                  /* "_O" */
-                   lfilename+                          /* object filename */
-                   2+                                  /* "_M" */
-                   2*sizeof(statbuf.st_mtime)+         /* mtime in hex */
-                   2+                                  /* "_V" */
-                   8+                                  /* version in dec */
-                   1;                                  /* nul */
+                       lm_name+                                /* module name */
+                       2+                                      /* "_O" */
+                       lfilename+                              /* object filename */
+                       2+                                      /* "_M" */
+                       2*sizeof(statbuf.st_mtime)+             /* mtime in hex */
+                       2+                                      /* "_V" */
+                       8+                                      /* version in dec */
+                       1;                                      /* nul */
                name = xmalloc(l);
                if (stat(absolute_filename, &statbuf) != 0)
                        statbuf.st_mtime = 0;
                version = get_module_version(f, str);   /* -1 if not found */
                snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
-                        symprefix, m_name, absolute_filename,
-                        (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
-                        version);
+                               symprefix, m_name, absolute_filename,
+                               (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
+                               version);
                sym = obj_add_symbol(f, name, -1,
-                                    ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
-                                    sec->idx, sec->header.sh_addr, 0);
+                               ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
+                               sec->idx, sec->header.sh_addr, 0);
                if (use_ksymtab)
-                   new_add_ksymtab(f, sym);
+                       new_add_ksymtab(f, sym);
        }
        free(absolute_filename);
 #ifdef _NOT_SUPPORTED_
@@ -3674,18 +3600,18 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
                        1;                      /* nul */
                name = xmalloc(l);
                snprintf(name, l, "%s%s_P%s",
-                        symprefix, m_name, f->persist);
-               sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
-                                    sec->idx, sec->header.sh_addr, 0);
+                               symprefix, m_name, f->persist);
+               sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
+                               sec->idx, sec->header.sh_addr, 0);
                if (use_ksymtab)
-                   new_add_ksymtab(f, sym);
+                       new_add_ksymtab(f, sym);
        }
 #endif /* _NOT_SUPPORTED_ */
        /* tag the desired sections if size is non-zero */
 
        for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
                if ((sec = obj_find_section(f, section_names[i])) &&
-                   sec->header.sh_size) {
+                               sec->header.sh_size) {
                        l = sizeof(symprefix)+          /* "__insmod_" */
                                lm_name+                /* module name */
                                2+                      /* "_S" */
@@ -3695,12 +3621,12 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
                                1;                      /* nul */
                        name = xmalloc(l);
                        snprintf(name, l, "%s%s_S%s_L%ld",
-                                symprefix, m_name, sec->name,
-                                (long)sec->header.sh_size);
-                       sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
-                                            sec->idx, sec->header.sh_addr, 0);
+                                       symprefix, m_name, sec->name,
+                                       (long)sec->header.sh_size);
+                       sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
+                                       sec->idx, sec->header.sh_addr, 0);
                        if (use_ksymtab)
-                           new_add_ksymtab(f, sym);
+                               new_add_ksymtab(f, sym);
                }
        }
 }
@@ -3787,7 +3713,7 @@ static void print_load_map(struct obj_file *f)
                        value = sym->value + sec->header.sh_addr;
                }
 
-               if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
+               if (ELF_ST_BIND(sym->info) == STB_LOCAL)
                        type = tolower(type);
 
                printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
@@ -3801,9 +3727,8 @@ static void print_load_map(struct obj_file *f)
 extern int insmod_main( int argc, char **argv)
 {
        int opt;
-       int k_crcs;
-       int k_new_syscalls;
        int len;
+       int k_crcs;
        char *tmp, *tmp1;
        unsigned long m_size;
        ElfW(Addr) m_addr;
@@ -3815,8 +3740,7 @@ extern int insmod_main( int argc, char **argv)
 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
        struct utsname uts_info;
        char m_strversion[STRVERSIONLEN];
-       int m_version;
-       int m_crcs;
+       int m_version, m_crcs;
 #endif
 #ifdef CONFIG_FEATURE_CLEAN_UP
        FILE *fp = 0;
@@ -3826,70 +3750,93 @@ extern int insmod_main( int argc, char **argv)
 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
        int flag_print_load_map = 0;
 #endif
+       int k_version = 0;
+       struct utsname myuname;
 
        /* Parse any options */
 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
-       while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0) {
+       while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0)
 #else
-       while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0) {
+       while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0)
 #endif
-               switch (opt) {
-                       case 'f':                       /* force loading */
-                               flag_force_load = 1;
-                               break;
-                       case 'k':                       /* module loaded by kerneld, auto-cleanable */
-                               flag_autoclean = 1;
-                               break;
-                       case 's':                       /* log to syslog */
-                               /* log to syslog -- not supported              */
-                               /* but kernel needs this for request_module(), */
-                               /* as this calls: modprobe -k -s -- <module>   */
-                               /* so silently ignore this flag                */
-                               break;
-                       case 'v':                       /* verbose output */
-                               flag_verbose = 1;
-                               break;
-                       case 'q':                       /* silent */
-                               flag_quiet = 1;
-                               break;
-                       case 'x':                       /* do not export externs */
-                               flag_export = 0;
-                               break;
-                       case 'o':                       /* name the output module */
-                               free(m_name);
-                               m_name = xstrdup(optarg);
-                               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;
+               {
+                       switch (opt) {
+                               case 'f':                       /* force loading */
+                                       flag_force_load = 1;
+                                       break;
+                               case 'k':                       /* module loaded by kerneld, auto-cleanable */
+                                       flag_autoclean = 1;
+                                       break;
+                               case 's':                       /* log to syslog */
+                                       /* log to syslog -- not supported              */
+                                       /* but kernel needs this for request_module(), */
+                                       /* as this calls: modprobe -k -s -- <module>   */
+                                       /* so silently ignore this flag                */
+                                       break;
+                               case 'v':                       /* verbose output */
+                                       flag_verbose = 1;
+                                       break;
+                               case 'q':                       /* silent */
+                                       flag_quiet = 1;
+                                       break;
+                               case 'x':                       /* do not export externs */
+                                       flag_export = 0;
+                                       break;
+                               case 'o':                       /* name the output module */
+                                       free(m_name);
+                                       m_name = bb_xstrdup(optarg);
+                                       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;
 #ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
-                       case 'm':                       /* print module load map */
-                               flag_print_load_map = 1;
-                               break;
+                               case 'm':                       /* print module load map */
+                                       flag_print_load_map = 1;
+                                       break;
 #endif
-                       default:
-                               show_usage();
+                               default:
+                                       bb_show_usage();
+                       }
                }
-       }
-       
+
        if (argv[optind] == NULL) {
-               show_usage();
+               bb_show_usage();
        }
 
        /* Grab the module name */
-       tmp1 = xstrdup(argv[optind]);
+       tmp1 = bb_xstrdup(argv[optind]);
        tmp = basename(tmp1);
        len = strlen(tmp);
 
-       if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
-               len-=2;
+       if (uname(&myuname) == 0) {
+               if (myuname.release[0] == '2') {
+                       k_version = myuname.release[2] - '0';
+               }
+       }
+
+#if defined(CONFIG_FEATURE_2_6_MODULES)
+       if (k_version > 4 && len > 3 && tmp[len - 3] == '.' &&
+                       tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
+               len-=3;
                tmp[len] = '\0';
        }
+       else
+#endif
+               if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
+                       len-=2;
+                       tmp[len] = '\0';
+               }
+
 
-       bb_asprintf(&m_fullName, "%s.o", tmp);
+#if defined(CONFIG_FEATURE_2_6_MODULES)
+       if (k_version > 4)
+               m_fullName = bb_xasprintf("%s.ko", tmp);
+       else
+#endif
+               m_fullName = bb_xasprintf("%s.o", tmp);
 
        if (!m_name) {
                m_name = tmp;
@@ -3901,28 +3848,26 @@ extern int insmod_main( int argc, char **argv)
        /* 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;
-                       char *tmdn;
+               if (k_version) {        /* uname succeedd */
+                       char *module_dir;
+                       char *tmdn;
                        char real_module_dir[FILENAME_MAX];
 
-                       tmdn = concat_path_file(_PATH_MODULES, myuname.release);
+                       tmdn = concat_path_file(_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 (tmdn, real_module_dir) == NULL)
-                               module_dir = tmdn;
-                       else
-                               module_dir = real_module_dir;
-                       recursive_action(module_dir, TRUE, FALSE, FALSE,
+                       if (realpath (tmdn, real_module_dir) == NULL)
+                               module_dir = tmdn;
+                       else
+                               module_dir = real_module_dir;
+                       recursive_action(module_dir, TRUE, FALSE, FALSE,
                                        check_module_name_match, 0, m_fullName);
-                       free(tmdn);
+                       free(tmdn);
                }
 
                /* Check if we have found anything yet */
@@ -3930,31 +3875,41 @@ extern int insmod_main( int argc, char **argv)
                {
                        char module_dir[FILENAME_MAX];
 
-                       free(m_filename);
-                       m_filename = 0;
+                       free(m_filename);
+                       m_filename = 0;
                        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)) 
+                                               check_module_name_match, 0, m_fullName))
                        {
                                if (m_filename == 0
-                                               || ((fp = fopen(m_filename, "r")) == NULL)) 
+                                               || ((fp = fopen(m_filename, "r")) == NULL))
                                {
-                                       error_msg("%s: no module by that name found", m_fullName);
+                                       bb_error_msg("%s: no module by that name found", m_fullName);
                                        goto out;
                                }
                        } else
-                               error_msg_and_die("%s: no module by that name found", m_fullName);
+                               bb_error_msg_and_die("%s: no module by that name found", m_fullName);
                }
-       } else 
-               m_filename = xstrdup(argv[optind]);
+       } else
+               m_filename = bb_xstrdup(argv[optind]);
 
-       printf("Using %s\n", m_filename);
+       if (!flag_quiet)
+               printf("Using %s\n", m_filename);
+
+#ifdef CONFIG_FEATURE_2_6_MODULES
+       if (k_version > 4)
+       {
+               optind--;
+               argv[optind + 1] = m_filename;
+               return insmod_ng_main(argc - optind, argv + optind);
+       }
+#endif
 
        if ((f = obj_load(fp, LOADBITS)) == NULL)
-               perror_msg_and_die("Could not load the module");
+               bb_perror_msg_and_die("Could not load the module");
 
        if (get_modinfo_value(f, "kernel_version") == NULL)
                m_has_modinfo = 0;
@@ -3968,10 +3923,8 @@ extern int insmod_main( int argc, char **argv)
                        uts_info.release[0] = '\0';
                if (m_has_modinfo) {
                        m_version = new_get_module_version(f, m_strversion);
-               } else {
-                       m_version = old_get_module_version(f, m_strversion);
                        if (m_version == -1) {
-                               error_msg("couldn't find the kernel version the module was "
+                               bb_error_msg("couldn't find the kernel version the module was "
                                                "compiled for");
                                goto out;
                        }
@@ -3979,12 +3932,12 @@ extern int insmod_main( int argc, char **argv)
 
                if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
                        if (flag_force_load) {
-                               error_msg("Warning: kernel-module version mismatch\n"
+                               bb_error_msg("Warning: kernel-module version mismatch\n"
                                                "\t%s was compiled for kernel version %s\n"
                                                "\twhile this kernel is version %s",
                                                m_filename, m_strversion, uts_info.release);
                        } else {
-                               error_msg("kernel-module version mismatch\n"
+                               bb_error_msg("kernel-module version mismatch\n"
                                                "\t%s was compiled for kernel version %s\n"
                                                "\twhile this kernel is version %s.",
                                                m_filename, m_strversion, uts_info.release);
@@ -3995,33 +3948,19 @@ extern int insmod_main( int argc, char **argv)
        k_crcs = 0;
 #endif                                                 /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
 
-       k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
-
-       if (k_new_syscalls) {
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
+       if (!query_module(NULL, 0, NULL, 0, NULL)) {
                if (!new_get_kernel_symbols())
                        goto out;
                k_crcs = new_is_kernel_checksummed();
-#else
-               error_msg("Not configured to support new kernels");
-               goto out;
-#endif
        } else {
-#ifdef CONFIG_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");
+               bb_error_msg("Not configured to support old kernels");
                goto out;
-#endif
        }
 
 #ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
+       m_crcs = 0;
        if (m_has_modinfo)
                m_crcs = new_is_module_checksummed(f);
-       else
-               m_crcs = old_is_module_checksummed(f);
 
        if (m_crcs != k_crcs)
                obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
@@ -4032,9 +3971,7 @@ extern int insmod_main( int argc, char **argv)
 
        /* Allocate common symbols, symbol tables, and string tables.  */
 
-       if (k_new_syscalls 
-               ? !new_create_this_module(f, m_name)
-               : !old_create_mod_use_count(f)) 
+       if (!new_create_this_module(f, m_name))
        {
                goto out;
        }
@@ -4049,9 +3986,7 @@ extern int insmod_main( int argc, char **argv)
        ++optind;
 
        if (optind < argc) {
-               if (m_has_modinfo
-                       ? !new_process_module_arguments(f, argc - optind, argv + optind) 
-                       : !old_process_module_arguments(f, argc - optind, argv + optind)) 
+               if (!new_process_module_arguments(f, argc - optind, argv + optind))
                {
                        goto out;
                }
@@ -4064,8 +3999,7 @@ extern int insmod_main( int argc, char **argv)
        add_ksymoops_symbols(f, m_filename, m_name);
 #endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
 
-       if (k_new_syscalls)
-               new_create_module_ksymtab(f);
+       new_create_module_ksymtab(f);
 
        /* Find current size of the module */
        m_size = obj_load_size(f);
@@ -4073,16 +4007,16 @@ extern int insmod_main( int argc, char **argv)
 
        m_addr = create_module(m_name, m_size);
        if (m_addr == -1) switch (errno) {
-       case EEXIST:
-               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",
-                               m_size);
-               goto out;
-       default:
-               perror_msg("create_module: %s", m_name);
-               goto out;
+               case EEXIST:
+                       bb_error_msg("A module named %s already exists", m_name);
+                       goto out;
+               case ENOMEM:
+                       bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
+                                       m_size);
+                       goto out;
+               default:
+                       bb_perror_msg("create_module: %s", m_name);
+                       goto out;
        }
 
 #if  !LOADBITS
@@ -4094,16 +4028,14 @@ extern int insmod_main( int argc, char **argv)
                delete_module(m_name);
                goto out;
        }
-#endif 
+#endif
 
        if (!obj_relocate(f, m_addr)) {
                delete_module(m_name);
                goto out;
        }
 
-       if (k_new_syscalls 
-               ? !new_init_module(m_name, f, m_size)
-               : !old_init_module(m_name, f, m_size)) 
+       if (!new_init_module(m_name, f, m_size))
        {
                delete_module(m_name);
                goto out;
@@ -4119,7 +4051,7 @@ extern int insmod_main( int argc, char **argv)
 out:
 #ifdef CONFIG_FEATURE_CLEAN_UP
        if(fp)
-       fclose(fp);
+               fclose(fp);
        if(tmp1) {
                free(tmp1);
        } else {
@@ -4129,3 +4061,82 @@ out:
 #endif
        return(exit_status);
 }
+
+
+#endif
+
+
+#ifdef CONFIG_FEATURE_2_6_MODULES
+
+#include <sys/mman.h>
+#include <asm/unistd.h>
+#include <sys/syscall.h>
+
+/* We use error numbers in a loose translation... */
+static const char *moderror(int err)
+{
+       switch (err) {
+               case ENOEXEC:
+                       return "Invalid module format";
+               case ENOENT:
+                       return "Unknown symbol in module";
+               case ESRCH:
+                       return "Module has wrong symbol version";
+               case EINVAL:
+                       return "Invalid parameters";
+               default:
+                       return strerror(err);
+       }
+}
+
+extern int insmod_ng_main( int argc, char **argv)
+{
+       int i;
+       int fd;
+       long int ret;
+       struct stat st;
+       unsigned long len;
+       void *map;
+       char *filename, *options = bb_xstrdup("");
+
+       filename = argv[1];
+       if (!filename) {
+               bb_show_usage();
+               return -1;
+       }
+
+       /* Rest is options */
+       for (i = 2; i < argc; i++) {
+               options = xrealloc(options, strlen(options) + 2 + strlen(argv[i]) + 2);
+               /* Spaces handled by "" pairs, but no way of escaping quotes */
+               if (strchr(argv[i], ' ')) {
+                       strcat(options, "\"");
+                       strcat(options, argv[i]);
+                       strcat(options, "\"");
+               } else {
+                       strcat(options, argv[i]);
+               }
+               strcat(options, " ");
+       }
+
+       if ((fd = open(filename, O_RDONLY, 0)) < 0) {
+               bb_perror_msg_and_die("cannot open module `%s'", filename);
+       }
+
+       fstat(fd, &st);
+       len = st.st_size;
+       map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
+       if (map == MAP_FAILED) {
+               bb_perror_msg_and_die("cannot mmap `%s'", filename);
+       }
+
+       ret = syscall(__NR_init_module, map, len, options);
+       if (ret != 0) {
+               bb_perror_msg_and_die("cannot insert `%s': %s (%li)",
+                               filename, moderror(errno), ret);
+       }
+
+       return 0;
+}
+
+#endif