Convert a chunk of usage.h to USE_ and SKIP_ (more to do there), and fix a
[oweals/busybox.git] / modutils / insmod.c
index d1aa24f14c6b0738a25f06fad6cd54a4c94143f4..f38daa257cc24e0e8837f022d08427e9c5989d17 100644 (file)
@@ -2,12 +2,15 @@
 /*
  * Mini insmod implementation for busybox
  *
- * This version of insmod supports x86, ARM, SH3/4/5, powerpc, m68k,
- * MIPS, v850e, and H8/300.
+ * 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-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
  *   Restructured (and partly rewritten) by:
  *   Björn Ekwall <bj0rn@blox.se> February 1999
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 #include <stdlib.h>
@@ -106,60 +96,103 @@ extern int insmod_ng_main( int argc, char **argv);
 #endif
 
 
-#if defined(__arm__)
-#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
+/* 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
-#endif
-
-#if defined(__s390__)
 #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_S390)
+/* 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
+/* 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
+
+/* 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
 
+/* 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
@@ -168,92 +201,101 @@ extern int insmod_ng_main( int argc, char **argv);
 #define EM_MIPS_RS3_LE 10
 #endif
 #endif /* !EM_MIPS_RS3_LE */
+#define ARCHDATAM       "__dbe_table"
+#endif
 
-#define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
-#define SHT_RELM       SHT_REL
-#define Elf32_RelM     Elf32_Rel
+/* Nios II */
+#if defined(__nios2__)
+#define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
+#define SHT_RELM       SHT_RELA
+#define Elf32_RelM     Elf32_Rela
 #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_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
-
+#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(__sh__) && defined(__BIG_ENDIAN__)
-#error insmod.c may require changes for use on big endian SH
+#if defined(__sh__) && BB_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(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && \
-       (defined(__sh__))
+/* 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
 
-#if defined (__v850e__)
-#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
+/* 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 SYMBOL_PREFIX  "_"
+#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
-
-#if defined(__cris__)
-#ifndef EM_CRIS
-#define EM_CRIS 76
-#define R_CRIS_NONE 0
-#define R_CRIS_32   3
+#define SYMBOL_PREFIX  "_"
 #endif
 
-#define MATCH_MACHINE(x) (x == EM_CRIS)
+/* 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
-#endif
-
-#if defined(__H8300H__) || defined(__H8300S__)
+#define CONFIG_USE_GOT_ENTRIES
+#define CONFIG_GOT_ENTRY_SIZE 8
 #define CONFIG_USE_SINGLE
-
-#define MATCH_MACHINE(x) (x == EM_H8_300)
-#define SHT_RELM       SHT_RELA
-#define Elf32_RelM     Elf32_Rela
-
-#define ELFCLASSM      ELFCLASS32
-#define SYMBOL_PREFIX  "_"
+#define Elf64_RelM     Elf64_Rela
+#define ELFCLASSM      ELFCLASS64
 #endif
 
 #ifndef SHT_RELM
@@ -288,9 +330,9 @@ extern int insmod_ng_main( int argc, char **argv);
 
 
 #ifndef MODUTILS_MODULE_H
-static const int MODUTILS_MODULE_H = 1;
+/* Why? static const int MODUTILS_MODULE_H = 1;*/
 
-#ident "$Id: insmod.c,v 1.122 2004/07/20 15:43:57 sandman Exp $"
+#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
@@ -309,9 +351,11 @@ static const int MODUTILS_MODULE_H = 1;
 #undef tgt_sizeof_char_p
 #undef tgt_sizeof_void_p
 #undef tgt_long
-static const int tgt_sizeof_long = 8;
-static const int tgt_sizeof_char_p = 8;
-static const int tgt_sizeof_void_p = 8;
+enum {
+       tgt_sizeof_long = 8,
+       tgt_sizeof_char_p = 8,
+       tgt_sizeof_void_p = 8
+};
 #define tgt_long               long long
 #endif
 
@@ -386,23 +430,26 @@ struct new_module_info
 };
 
 /* Bits of module.flags.  */
-static const int NEW_MOD_RUNNING = 1;
-static const int NEW_MOD_DELETED = 2;
-static const int NEW_MOD_AUTOCLEAN = 4;
-static const int NEW_MOD_VISITED = 8;
-static const int NEW_MOD_USED_ONCE = 16;
+enum {
+       NEW_MOD_RUNNING = 1,
+       NEW_MOD_DELETED = 2,
+       NEW_MOD_AUTOCLEAN = 4,
+       NEW_MOD_VISITED = 8,
+       NEW_MOD_USED_ONCE = 16
+};
 
 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.  */
-
-static const int QM_MODULES = 1;
-static const int QM_DEPS = 2;
-static const int QM_REFS = 3;
-static const int QM_SYMBOLS = 4;
-static const int QM_INFO = 5;
+enum {
+       QM_MODULES = 1,
+       QM_DEPS = 2,
+       QM_REFS = 3,
+       QM_SYMBOLS = 4,
+       QM_INFO = 5
+};
 
 /*======================================================================*/
 /* The system calls unchanged between 2.0 and 2.1.  */
@@ -446,9 +493,9 @@ int delete_module(const char *);
 
 
 #ifndef MODUTILS_OBJ_H
-static const int MODUTILS_OBJ_H = 1;
+/* Why? static const int MODUTILS_OBJ_H = 1; */
 
-#ident "$Id: insmod.c,v 1.122 2004/07/20 15:43:57 sandman Exp $"
+#ident "$Id: insmod.c,v 1.126 2004/12/26 09:13:32 vapier Exp $"
 
 /* The relocatable object is manipulated using elfin types.  */
 
@@ -456,12 +503,6 @@ static const int MODUTILS_OBJ_H = 1;
 #include <elf.h>
 #include <endian.h>
 
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-#define ELFDATAM       ELFDATA2LSB
-#elif __BYTE_ORDER == __BIG_ENDIAN
-#define ELFDATAM       ELFDATA2MSB
-#endif
-
 #ifndef ElfW
 # if ELFCLASSM == ELFCLASS32
 #  define ElfW(x)  Elf32_ ## x
@@ -481,6 +522,12 @@ 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;
 
@@ -621,9 +668,9 @@ static enum obj_reloc arch_apply_relocation (struct obj_file *f,
                                      ElfW(RelM) *rel, ElfW(Addr) value);
 
 static void arch_create_got (struct obj_file *f);
-
+#if ENABLE_FEATURE_CHECK_TAINTED_MODULE
 static int obj_gpl_license(struct obj_file *f, const char **license);
-
+#endif /* ENABLE_FEATURE_CHECK_TAINTED_MODULE */
 #endif /* obj.h */
 //----------------------------------------------------------------------------
 //--------end of modutils obj.h
@@ -634,12 +681,12 @@ static int obj_gpl_license(struct obj_file *f, const char **license);
 #ifdef SYMBOL_PREFIX
 #define SPFX   SYMBOL_PREFIX
 #else
-#define SPFX   ""
+#define SPFX   ""
 #endif
 
 
 #define _PATH_MODULES  "/lib/modules"
-static const int STRVERSIONLEN = 32;
+enum { STRVERSIONLEN = 32 };
 
 /*======================================================================*/
 
@@ -679,8 +726,8 @@ struct arch_single_entry
 struct mips_hi16
 {
        struct mips_hi16 *next;
-       Elf32_Addr *addr;
-       Elf32_Addr value;
+       ElfW(Addr) *addr;
+       ElfW(Addr) value;
 };
 #endif
 
@@ -790,7 +837,7 @@ arch_apply_relocation(struct obj_file *f,
                                          struct obj_section *targsec,
                                          struct obj_section *symsec,
                                          struct obj_symbol *sym,
-                                     ElfW(RelM) *rel, ElfW(Addr) v)
+                                         ElfW(RelM) *rel, ElfW(Addr) v)
 {
        struct arch_file *ifile = (struct arch_file *) f;
        enum obj_reloc ret = obj_reloc_ok;
@@ -799,23 +846,25 @@ arch_apply_relocation(struct obj_file *f,
 #if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
        struct arch_symbol *isym = (struct arch_symbol *) sym;
 #endif
+#if defined(__arm__) || defined(__i386__) || defined(__mc68000__) || defined(__sh__) || defined(__s390__)
 #if defined(CONFIG_USE_GOT_ENTRIES)
        ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
 #endif
+#endif
 #if defined(CONFIG_USE_PLT_ENTRIES)
        ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
        unsigned long *ip;
-#if defined(CONFIG_USE_PLT_LIST)
+# if defined(CONFIG_USE_PLT_LIST)
        struct arch_list_entry *pe;
-#else
+# else
        struct arch_single_entry *pe;
+# endif
 #endif
-#endif
-
-       switch (ELF32_R_TYPE(rel->r_info)) {
 
+       switch (ELF_R_TYPE(rel->r_info)) {
 
 #if defined(__arm__)
+
                case R_ARM_NONE:
                        break;
 
@@ -831,7 +880,7 @@ arch_apply_relocation(struct obj_file *f,
                         * (which is .got) similar to branch,
                         * but is full 32 bits relative */
 
-                       assert(got);
+                       assert(got != 0);
                        *loc += got - dot;
                        break;
 
@@ -840,94 +889,50 @@ arch_apply_relocation(struct obj_file *f,
                        goto bb_use_plt;
 
                case R_ARM_GOTOFF: /* address relative to the got */
-                       assert(got);
+                       assert(got != 0);
                        *loc += v - got;
                        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_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_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 (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
-                                       ip[2] = v - 2;
-                               else
-                                       ip[2] = v;
-                               pe->inited = 1;
-                       }
+#elif defined(__cris__)
 
-                       /* Insert relative distance to target.  */
-                       v = plt + pe->offset - dot;
-                       if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
-                               *(unsigned int *) loc = (unsigned int) v;
-                       else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
-                               *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
+               case R_CRIS_NONE:
                        break;
 
-               case R_390_GLOB_DAT:
-               case R_390_JMP_SLOT:
+               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;
 
-               case R_390_RELATIVE:
-                       *loc += f->baseaddr;
-                       break;
+#elif defined(__H8300H__) || defined(__H8300S__)
 
-               case R_390_GOTPC:
-                       assert(got != 0);
-                       *(unsigned long *) loc += got - dot;
+               case R_H8_DIR24R8:
+                       loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
+                       *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + 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;
-                               *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
-                       }
-                       if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
-                               *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
-                       else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
-                               *(unsigned short *) loc += isym->gotent.offset;
-                       else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
-                               *(unsigned int *) loc += isym->gotent.offset;
+               case R_H8_DIR24A8:
+                       *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;
+               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;
 
 #elif defined(__i386__)
@@ -991,8 +996,8 @@ arch_apply_relocation(struct obj_file *f,
 
                case R_68K_PC8:
                        v -= dot;
-                       if ((Elf32_Sword)v > 0x7f ||
-                                       (Elf32_Sword)v < -(Elf32_Sword)0x80) {
+                       if ((ElfW(Sword))v > 0x7f ||
+                                       (ElfW(Sword))v < -(ElfW(Sword))0x80) {
                                ret = obj_reloc_overflow;
                        }
                        *(char *)loc = v;
@@ -1000,8 +1005,8 @@ arch_apply_relocation(struct obj_file *f,
 
                case R_68K_PC16:
                        v -= dot;
-                       if ((Elf32_Sword)v > 0x7fff ||
-                                       (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
+                       if ((ElfW(Sword))v > 0x7fff ||
+                                       (ElfW(Sword))v < -(ElfW(Sword))0x8000) {
                                ret = obj_reloc_overflow;
                        }
                        *(short *)loc = v;
@@ -1023,12 +1028,12 @@ arch_apply_relocation(struct obj_file *f,
                case R_68K_GOT32:
                        goto bb_use_got;
 
-#ifdef R_68K_GOTOFF
+# ifdef R_68K_GOTOFF
                case R_68K_GOTOFF:
                        assert(got != 0);
                        *loc += v - got;
                        break;
-#endif
+# endif
 
 #elif defined(__mips__)
 
@@ -1067,7 +1072,7 @@ arch_apply_relocation(struct obj_file *f,
                case R_MIPS_LO16:
                        {
                                unsigned long insnlo = *loc;
-                               Elf32_Addr val, vallo;
+                               ElfW(Addr) val, vallo;
 
                                /* Sign extend the addend we extract from the lo insn.  */
                                vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
@@ -1117,6 +1122,170 @@ arch_apply_relocation(struct obj_file *f,
                                break;
                        }
 
+#elif defined(__nios2__)
+
+               case R_NIOS2_NONE:
+                       break;
+
+               case R_NIOS2_BFD_RELOC_32:
+                       *loc += v;
+                       break;
+
+               case R_NIOS2_BFD_RELOC_16:
+                       if (v > 0xffff) {
+                               ret = obj_reloc_overflow;
+                       }
+                       *(short *)loc = v;
+                       break;
+
+               case R_NIOS2_BFD_RELOC_8:
+                       if (v > 0xff) {
+                               ret = obj_reloc_overflow;
+                       }
+                       *(char *)loc = v;
+                       break;
+
+               case R_NIOS2_S16:
+                       {
+                               Elf32_Addr word;
+
+                               if ((Elf32_Sword)v > 0x7fff ||
+                                   (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
+                                       ret = obj_reloc_overflow;
+                               }
+
+                               word = *loc;
+                               *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
+                                      (word & 0x3f);
+                       }
+                       break;
+
+               case R_NIOS2_U16:
+                       {
+                               Elf32_Addr word;
+
+                               if (v > 0xffff) {
+                                       ret = obj_reloc_overflow;
+                               }
+
+                               word = *loc;
+                               *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
+                                      (word & 0x3f);
+                       }
+                       break;
+
+               case R_NIOS2_PCREL16:
+                       {
+                               Elf32_Addr word;
+
+                               v -= dot + 4;
+                               if ((Elf32_Sword)v > 0x7fff ||
+                                   (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
+                                       ret = obj_reloc_overflow;
+                               }
+
+                               word = *loc;
+                               *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
+                       }
+                       break;
+
+               case R_NIOS2_GPREL:
+                       {
+                               Elf32_Addr word, gp;
+                               /* get _gp */
+                               gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
+                               v-=gp;
+                               if ((Elf32_Sword)v > 0x7fff ||
+                                               (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
+                                       ret = obj_reloc_overflow;
+                               }
+
+                               word = *loc;
+                               *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
+                       }
+                       break;
+
+               case R_NIOS2_CALL26:
+                       if (v & 3)
+                               ret = obj_reloc_dangerous;
+                       if ((v >> 28) != (dot >> 28))
+                               ret = obj_reloc_overflow;
+                       *loc = (*loc & 0x3f) | ((v >> 2) << 6);
+                       break;
+
+               case R_NIOS2_IMM5:
+                       {
+                               Elf32_Addr word;
+
+                               if (v > 0x1f) {
+                                       ret = obj_reloc_overflow;
+                               }
+
+                               word = *loc & ~0x7c0;
+                               *loc = word | ((v & 0x1f) << 6);
+                       }
+                       break;
+
+               case R_NIOS2_IMM6:
+                       {
+                               Elf32_Addr word;
+
+                               if (v > 0x3f) {
+                                       ret = obj_reloc_overflow;
+                               }
+
+                               word = *loc & ~0xfc0;
+                               *loc = word | ((v & 0x3f) << 6);
+                       }
+                       break;
+
+               case R_NIOS2_IMM8:
+                       {
+                               Elf32_Addr word;
+
+                               if (v > 0xff) {
+                                       ret = obj_reloc_overflow;
+                               }
+
+                               word = *loc & ~0x3fc0;
+                               *loc = word | ((v & 0xff) << 6);
+                       }
+                       break;
+
+               case R_NIOS2_HI16:
+                       {
+                               Elf32_Addr word;
+
+                               word = *loc;
+                               *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
+                                      (word & 0x3f);
+                       }
+                       break;
+
+               case R_NIOS2_LO16:
+                       {
+                               Elf32_Addr word;
+
+                               word = *loc;
+                               *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
+                                      (word & 0x3f);
+                       }
+                       break;
+
+               case R_NIOS2_HIADJ16:
+                       {
+                               Elf32_Addr word1, word2;
+
+                               word1 = *loc;
+                               word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
+                               *loc = ((((word1 >> 22) << 16) | word2) << 6) |
+                                      (word1 & 0x3f);
+                       }
+                       break;
+
+#elif defined(__powerpc64__)
+               /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
+
 #elif defined(__powerpc__)
 
                case R_PPC_ADDR16_HA:
@@ -1142,6 +1311,93 @@ arch_apply_relocation(struct obj_file *f,
                        *loc = v;
                        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_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_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;
+                       }
+
+                       /* 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;
+
+               case R_390_GLOB_DAT:
+               case R_390_JMP_SLOT:
+                       *loc = v;
+                       break;
+
+               case R_390_RELATIVE:
+                       *loc += f->baseaddr;
+                       break;
+
+               case R_390_GOTPC:
+                       assert(got != 0);
+                       *(unsigned long *) loc += got - dot;
+                       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;
+
+# ifndef R_390_GOTOFF32
+#  define R_390_GOTOFF32 R_390_GOTOFF
+# endif
+               case R_390_GOTOFF32:
+                       assert(got != 0);
+                       *loc += v - got;
+                       break;
+
 #elif defined(__sh__)
 
                case R_SH_NONE:
@@ -1181,13 +1437,13 @@ arch_apply_relocation(struct obj_file *f,
                        *loc = v - got;
                        break;
 
-#if defined(__SH5__)
+# if defined(__SH5__)
                case R_SH_IMM_MEDLOW16:
                case R_SH_IMM_LOW16:
                        {
-                               Elf32_Addr word;
+                               ElfW(Addr) word;
 
-                               if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
+                               if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
                                        v >>= 16;
 
                                /*
@@ -1209,13 +1465,13 @@ arch_apply_relocation(struct obj_file *f,
                case R_SH_IMM_MEDLOW16_PCREL:
                case R_SH_IMM_LOW16_PCREL:
                        {
-                               Elf32_Addr word;
+                               ElfW(Addr) word;
 
                                word = *loc & ~0x3fffc00;
 
                                v -= dot;
 
-                               if (ELF32_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
+                               if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
                                        v >>= 16;
 
                                word |= (v & 0xffff) << 10;
@@ -1224,15 +1480,10 @@ arch_apply_relocation(struct obj_file *f,
 
                                break;
                        }
-#endif /* __SH5__ */
-#endif /* __sh__ */
+# endif /* __SH5__ */
 
-               default:
-                       printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
-                       ret = obj_reloc_unhandled;
-                       break;
+#elif defined (__v850e__)
 
-#if defined (__v850e__)
                case R_V850_NONE:
                        break;
 
@@ -1248,50 +1499,85 @@ arch_apply_relocation(struct obj_file *f,
 
                case R_V850_22_PCREL:
                        goto bb_use_plt;
-#endif
 
-#if defined (__cris__)
-               case R_CRIS_NONE:
+#elif defined(__x86_64__)
+
+               case R_X86_64_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;
+               case R_X86_64_64:
+                       *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);
+               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;
-               case R_H8_DIR24A8:
-                       *loc += v;
+
+               case R_X86_64_32S:
+                       *(signed int *) loc += v;
                        break;
-               case R_H8_DIR32:
-               case R_H8_DIR32A16:
-                       *loc += v;
+
+               case R_X86_64_16:
+                       *(unsigned short *) loc += v;
                        break;
-               case R_H8_PCREL16:
-                       v -= dot + 2;
-                       if ((Elf32_Sword)v > 0x7fff || 
-                           (Elf32_Sword)v < -(Elf32_Sword)0x8000)
-                               ret = obj_reloc_overflow;
-                       else 
-                               *(unsigned short *)loc = v;
+
+               case R_X86_64_8:
+                       *(unsigned char *) loc += v;
                        break;
-               case R_H8_PCREL8:
-                       v -= dot + 1;
-                       if ((Elf32_Sword)v > 0x7f || 
-                           (Elf32_Sword)v < -(Elf32_Sword)0x80)
-                               ret = obj_reloc_overflow;
-                       else 
-                               *(unsigned char *)loc = v;
+
+               case R_X86_64_PC32:
+                       *(unsigned int *) loc += v - dot;
+                       break;
+
+               case R_X86_64_PC16:
+                       *(unsigned short *) loc += v - dot;
                        break;
+
+               case R_X86_64_PC8:
+                       *(unsigned char *) loc += v - dot;
+                       break;
+
+               case R_X86_64_GLOB_DAT:
+               case R_X86_64_JUMP_SLOT:
+                       *loc = v;
+                       break;
+
+               case R_X86_64_RELATIVE:
+                       *loc += f->baseaddr;
+                       break;
+
+               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
+
+#else
+# warning "no idea how to handle relocations on your arch"
 #endif
 
+               default:
+                       printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
+                       ret = obj_reloc_unhandled;
+                       break;
+
 #if defined(CONFIG_USE_PLT_ENTRIES)
 
 bb_use_plt:
@@ -1318,7 +1604,7 @@ bb_use_plt:
 #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[1] = 0x396b0000 + (v & 0xffff);          /* addi r11,r11,sym@l */
                                ip[2] = 0x7d6903a6;                           /* mtctr r11 */
                                ip[3] = 0x4e800420;                           /* bctr */
 #endif
@@ -1338,7 +1624,7 @@ bb_use_plt:
 #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)
+                               if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
 #endif
                                        /* go via the plt */
                                        v = plt + pe->offset - dot;
@@ -1495,7 +1781,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;
@@ -1504,7 +1790,7 @@ 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:
@@ -1703,15 +1989,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?  */
 
@@ -1772,7 +2058,7 @@ 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)
                        bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
                                        name, (long) symidx, (long) f->local_symtab_size);
@@ -1963,7 +2249,7 @@ add_symbols_from( struct obj_file *f,
                if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
 #ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
                        if (gpl)
-                               ((char *)s->name) += 8;
+                               s->name += 8;
                        else
 #endif
                                continue;
@@ -1986,14 +2272,14 @@ add_symbols_from( struct obj_file *f,
 #endif /* SYMBOL_PREFIX */
 
                sym = obj_find_symbol(f, name);
-               if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
+               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,
-                                       ELFW(ST_INFO) (STB_GLOBAL,
+                                       ELF_ST_INFO(STB_GLOBAL,
                                                STT_NOTYPE),
                                        idx, s->value, 0);
                        /* Did our symbol just get installed?  If so, mark the
@@ -2454,7 +2740,7 @@ static int new_create_this_module(struct obj_file *f, const char *m_name)
        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,
+                       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),
@@ -2543,7 +2829,7 @@ 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
+                               if (ELF_ST_BIND(sym->info) != STB_LOCAL
                                                && sym->secidx <= SHN_HIRESERVE
                                                && (sym->secidx >= SHN_LORESERVE
                                                        || loaded[sym->secidx])) {
@@ -2700,7 +2986,7 @@ 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 {
@@ -2886,12 +3172,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];
@@ -2912,7 +3198,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
@@ -2934,11 +3220,11 @@ static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
 bad_reloc:
                                        if (extsym) {
                                                bb_error_msg("%s of type %ld for %s", errmsg,
-                                                               (long) ELFW(R_TYPE) (rel->r_info),
+                                                               (long) ELF_R_TYPE(rel->r_info),
                                                                strtab + extsym->st_name);
                                        } else {
                                                bb_error_msg("%s of type %ld", errmsg,
-                                                               (long) ELFW(R_TYPE) (rel->r_info));
+                                                               (long) ELF_R_TYPE(rel->r_info));
                                        }
                                        ret = 0;
                                        break;
@@ -3026,7 +3312,8 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
                return NULL;
        }
        if (f->header.e_ident[EI_CLASS] != ELFCLASSM
-                       || f->header.e_ident[EI_DATA] != ELFDATAM
+                       || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN
+                               ? ELFDATA2MSB : ELFDATA2LSB)
                        || f->header.e_ident[EI_VERSION] != EV_CURRENT
                        || !MATCH_MACHINE(f->header.e_machine)) {
                bb_error_msg("ELF file not for this architecture");
@@ -3258,7 +3545,7 @@ 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));
 }
 
 
@@ -3270,7 +3557,7 @@ static int obj_gpl_license(struct obj_file *f, const char **license)
         * linux/include/linux/module.h.  Checking for leading "GPL" will not
         * work, somebody will use "GPL sucks, this is proprietary".
         */
-       static const char *gpl_licenses[] = {
+       static const char * const gpl_licenses[] = {
                "GPL",
                "GPL v2",
                "GPL and additional rights",
@@ -3306,7 +3593,7 @@ static int obj_gpl_license(struct obj_file *f, const char **license)
 #define TAINT_PROPRIETORY_MODULE        (1<<0)
 #define TAINT_FORCED_MODULE             (1<<1)
 #define TAINT_UNSAFE_SMP                (1<<2)
-#define TAINT_URL                                              "http://www.tux.org/lkml/#export-tainted"
+#define TAINT_URL                       "http://www.tux.org/lkml/#export-tainted"
 
 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)
@@ -3414,8 +3701,8 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
                ".text",
                ".rodata",
                ".data",
-               ".bss"
-                       ".sbss"
+               ".bss",
+               ".sbss"
        };
 
        if (realpath(filename, real)) {
@@ -3462,7 +3749,7 @@ add_ksymoops_symbols(struct obj_file *f, const char *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),
+                               ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
                                sec->idx, sec->header.sh_addr, 0);
                if (use_ksymtab)
                        new_add_ksymtab(f, sym);
@@ -3480,7 +3767,7 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
                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),
+               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);
@@ -3502,7 +3789,7 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
                        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),
+                       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);
@@ -3592,7 +3879,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,
@@ -3603,7 +3890,7 @@ static void print_load_map(struct obj_file *f)
 
 #endif
 
-extern int insmod_main( int argc, char **argv)
+int insmod_main( int argc, char **argv)
 {
        int opt;
        int len;
@@ -3712,10 +3999,10 @@ extern int insmod_main( int argc, char **argv)
 
 #if defined(CONFIG_FEATURE_2_6_MODULES)
        if (k_version > 4)
-               bb_xasprintf(&m_fullName, "%s.ko", tmp);
+               m_fullName = bb_xasprintf("%s.ko", tmp);
        else
 #endif
-               bb_xasprintf(&m_fullName, "%s.o", tmp);
+               m_fullName = bb_xasprintf("%s.o", tmp);
 
        if (!m_name) {
                m_name = tmp;
@@ -3775,7 +4062,7 @@ extern int insmod_main( int argc, char **argv)
        } else
                m_filename = bb_xstrdup(argv[optind]);
 
-       if (!flag_quiet)
+       if (flag_verbose)
                printf("Using %s\n", m_filename);
 
 #ifdef CONFIG_FEATURE_2_6_MODULES
@@ -3931,9 +4218,8 @@ out:
 #ifdef CONFIG_FEATURE_CLEAN_UP
        if(fp)
                fclose(fp);
-       if(tmp1) {
-               free(tmp1);
-       } else {
+       free(tmp1);
+       if(!tmp1) {
                free(m_name);
        }
        free(m_filename);
@@ -3968,7 +4254,7 @@ static const char *moderror(int err)
        }
 }
 
-extern int insmod_ng_main( int argc, char **argv)
+int insmod_ng_main( int argc, char **argv)
 {
        int i;
        int fd;
@@ -3998,13 +4284,11 @@ extern int insmod_ng_main( int argc, char **argv)
                strcat(options, " ");
        }
 
-       if ((fd = open(filename, O_RDONLY, 0)) < 0) {
-               bb_perror_msg_and_die("cannot open module `%s'", filename);
-       }
+       fd = bb_xopen3(filename, O_RDONLY, 0);
 
        fstat(fd, &st);
        len = st.st_size;
-       map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
+       map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
        if (map == MAP_FAILED) {
                bb_perror_msg_and_die("cannot mmap `%s'", filename);
        }