/* vi: set sw=4 ts=4: */
/*
* Mini insmod implementation for busybox
+ * This version of insmod now supports x86, ARM, SH3/4, powerpc, and MIPS.
*
* Copyright (C) 1999,2000,2001 by Lineo, inc.
* Written by Erik Andersen <andersen@lineo.com>
* very minor changes required to also work with StrongArm and presumably
* all ARM based systems.
*
+ * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
+ * PowerPC specific code stolen from modutils-2.3.16,
+ * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
+ * I've only tested the code on mpc8xx platforms in big-endian mode.
+ * Did some cleanup and added BB_USE_xxx_ENTRIES...
+ *
+ * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
+ * based on modutils-2.4.2
+ * MIPS specific support for Elf loading and relocation.
+ * Copyright 1996, 1997 Linux International.
+ * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
+ *
* Based almost entirely on the Linux modutils-2.3.11 implementation.
* Copyright 1996, 1997 Linux International.
* New implementation contributed by Richard Henderson <rth@tamu.edu>
*
*/
-#include "busybox.h"
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <getopt.h>
#include <sys/utsname.h>
-#include <sys/syscall.h>
-#include <linux/unistd.h>
+#include "busybox.h"
+
+#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
+# define new_sys_init_module init_module
+#else
+# define old_sys_init_module init_module
+#endif
+
+#if defined(__powerpc__)
+#define BB_USE_PLT_ENTRIES
+#define BB_PLT_ENTRY_SIZE 16
+#endif
+
+#if defined(__arm__)
+#define BB_USE_PLT_ENTRIES
+#define BB_PLT_ENTRY_SIZE 8
+#define BB_USE_GOT_ENTRIES
+#define BB_GOT_ENTRY_SIZE 8
+#endif
+
+#if defined(__sh__)
+#define BB_USE_GOT_ENTRIES
+#define BB_GOT_ENTRY_SIZE 4
+#endif
+
+#if defined(__i386__)
+#define BB_USE_GOT_ENTRIES
+#define BB_GOT_ENTRY_SIZE 4
+#endif
+
+#if defined(__mips__)
+// neither used
+#endif
//----------------------------------------------------------------------------
//--------modutils module.h, lines 45-242
#ifndef MODUTILS_MODULE_H
static const int MODUTILS_MODULE_H = 1;
-#ident "$Id: insmod.c,v 1.46 2001/02/14 21:23:06 andersen Exp $"
+#ident "$Id: insmod.c,v 1.59 2001/04/25 17:22:32 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
#ifndef MODUTILS_OBJ_H
static const int MODUTILS_OBJ_H = 1;
-#ident "$Id: insmod.c,v 1.46 2001/02/14 21:23:06 andersen Exp $"
+#ident "$Id: insmod.c,v 1.59 2001/04/25 17:22:32 andersen Exp $"
/* The relocatable object is manipulated using elfin types. */
#endif
#define ELFCLASSM ELFCLASS32
-#define ELFDATAM ELFDATA2LSB
-
-
#if defined(__sh__)
#define MATCH_MACHINE(x) (x == EM_SH)
#define SHT_RELM SHT_RELA
#define Elf32_RelM Elf32_Rela
+#define ELFDATAM ELFDATA2LSB
#elif defined(__arm__)
#define MATCH_MACHINE(x) (x == EM_ARM)
#define SHT_RELM SHT_REL
#define Elf32_RelM Elf32_Rel
+#define ELFDATAM ELFDATA2LSB
+
+#elif defined(__powerpc__)
+
+#define MATCH_MACHINE(x) (x == EM_PPC)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFDATAM ELFDATA2MSB
+
+#elif defined(__mips__)
+
+/* Account for ELF spec changes. */
+#ifndef EM_MIPS_RS3_LE
+#ifdef EM_MIPS_RS4_BE
+#define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
+#else
+#define EM_MIPS_RS3_LE 10
+#endif
+#endif /* !EM_MIPS_RS3_LE */
+
+#define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
+#define SHT_RELM SHT_REL
+#define Elf32_RelM Elf32_Rel
+#ifdef __MIPSEB__
+#define ELFDATAM ELFDATA2MSB
+#endif
+#ifdef __MIPSEL__
+#define ELFDATAM ELFDATA2LSB
+#endif
#elif defined(__i386__)
#define SHT_RELM SHT_REL
#define Elf32_RelM Elf32_Rel
+#define ELFDATAM ELFDATA2LSB
#else
#error Sorry, but insmod.c does not yet support this architecture...
/* Done ;-) */
-#if defined(__arm__)
-struct arm_plt_entry
+
+
+#if defined(BB_USE_PLT_ENTRIES)
+struct arch_plt_entry
{
int offset;
int allocated:1;
};
#endif
+#if defined(BB_USE_GOT_ENTRIES)
struct arch_got_entry {
int offset;
unsigned offset_done:1;
unsigned reloc_done:1;
};
+#endif
+
+#if defined(__mips__)
+struct mips_hi16
+{
+ struct mips_hi16 *next;
+ Elf32_Addr *addr;
+ Elf32_Addr value;
+};
+#endif
struct arch_file {
struct obj_file root;
-#if defined(__arm__)
- struct obj_section *plt;
+#if defined(BB_USE_PLT_ENTRIES)
+ struct obj_section *plt;
#endif
+#if defined(BB_USE_GOT_ENTRIES)
struct obj_section *got;
+#endif
+#if defined(__mips__)
+ struct mips_hi16 *mips_hi16_list;
+#endif
};
struct arch_symbol {
struct obj_symbol root;
-#if defined(__arm__)
- struct arm_plt_entry pltent;
+#if defined(BB_USE_PLT_ENTRIES)
+ struct arch_plt_entry pltent;
#endif
+#if defined(BB_USE_GOT_ENTRIES)
struct arch_got_entry gotent;
+#endif
};
int n_ext_modules_used;
-
-/* Some firendly syscalls to cheer everyone's day... */
-#define __NR_new_sys_init_module __NR_init_module
-_syscall2(int, new_sys_init_module, const char *, name,
- const struct new_module *, info)
-#define __NR_old_sys_init_module __NR_init_module
-_syscall5(int, old_sys_init_module, const char *, name, char *, code,
- unsigned, codesize, struct old_mod_routines *, routines,
- struct old_symbol_table *, symtab)
-#ifndef BB_RMMOD
-_syscall1(int, delete_module, const char *, name)
-#else
extern int delete_module(const char *);
-#endif
+
/* This is kind of troublesome. See, we don't actually support
the m68k or the arm the same way we support i386 and (now)
-- Bryan Rittmeyer <bryan@ixiacom.com> */
-#ifdef BB_FEATURE_OLD_MODULE_INTERFACE
-_syscall1(int, get_kernel_syms, struct old_kernel_sym *, ks)
-#endif
-
-#if defined(__i386__) || defined(__m68k__) || defined(__arm__)
-/* Jump through hoops to fixup error return codes */
-#define __NR__create_module __NR_create_module
-static inline _syscall2(long, _create_module, const char *, name, size_t,
- size)
-unsigned long create_module(const char *name, size_t size)
-{
- long ret = _create_module(name, size);
-
- if (ret == -1 && errno > 125) {
- ret = -errno;
- errno = 0;
- }
- return ret;
-}
-#else
-_syscall2(unsigned long, create_module, const char *, name, size_t, size)
-#endif
-static char m_filename[BUFSIZ + 1] = "\0";
-static char m_fullName[BUFSIZ + 1] = "\0";
+static char m_filename[BUFSIZ + 1];
+static char m_fullName[BUFSIZ + 1];
/*======================================================================*/
tmp++;
if (check_wildcard_match(tmp, fullName) == TRUE) {
/* Stop searching if we find a match */
- memcpy(m_filename, fileName, strlen(fileName)+1);
+ safe_strncpy(m_filename, fileName, sizeof(m_filename));
return (FALSE);
}
}
{
struct arch_file *f;
f = xmalloc(sizeof(*f));
+
+#if defined(BB_USE_PLT_ENTRIES)
+ f->plt = NULL;
+#endif
+#if defined(BB_USE_GOT_ENTRIES)
f->got = NULL;
+#endif
+#if defined(__mips__)
+ f->mips_hi16_list = NULL;
+#endif
+
return &f->root;
}
{
struct arch_symbol *sym;
sym = xmalloc(sizeof(*sym));
+
+#if defined(BB_USE_PLT_ENTRIES)
+ memset(&sym->pltent, 0, sizeof(sym->pltent));
+#endif
+#if defined(BB_USE_GOT_ENTRIES)
memset(&sym->gotent, 0, sizeof(sym->gotent));
+#endif
+
return &sym->root;
}
ElfW(RelM) *rel, ElfW(Addr) v)
{
struct arch_file *ifile = (struct arch_file *) f;
+#if !(defined(__mips__))
struct arch_symbol *isym = (struct arch_symbol *) sym;
+#endif
ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
+#if defined(BB_USE_GOT_ENTRIES)
ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
-#if defined(__arm__)
+#endif
+#if defined(BB_USE_PLT_ENTRIES)
ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
-
- struct arm_plt_entry *pe;
+ struct arch_plt_entry *pe;
unsigned long *ip;
#endif
-
enum obj_reloc ret = obj_reloc_ok;
switch (ELF32_R_TYPE(rel->r_info)) {
case R_ARM_NONE:
#elif defined(__i386__)
case R_386_NONE:
+#elif defined(__powerpc__)
+ case R_PPC_NONE:
+#elif defined(__mips__)
+ case R_MIPS_NONE:
#endif
break;
#elif defined(__arm__)
case R_ARM_ABS32:
#elif defined(__i386__)
- case R_386_32:
+ case R_386_32:
+#elif defined(__powerpc__)
+ case R_PPC_ADDR32:
+#elif defined(__mips__)
+ case R_MIPS_32:
#endif
*loc += v;
break;
+#if defined(__powerpc__)
+ case R_PPC_ADDR16_HA:
+ *(unsigned short *)loc = (v + 0x8000) >> 16;
+ break;
+
+ case R_PPC_ADDR16_HI:
+ *(unsigned short *)loc = v >> 16;
+ break;
+
+ case R_PPC_ADDR16_LO:
+ *(unsigned short *)loc = v;
+ break;
+#endif
+
+#if defined(__mips__)
+ case R_MIPS_26:
+ if (v % 4)
+ ret = obj_reloc_dangerous;
+ if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
+ ret = obj_reloc_overflow;
+ *loc =
+ (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
+ 0x03ffffff);
+ break;
+
+ case R_MIPS_HI16:
+ {
+ struct mips_hi16 *n;
+
+ /* We cannot relocate this one now because we don't know the value
+ of the carry we need to add. Save the information, and let LO16
+ do the actual relocation. */
+ n = (struct mips_hi16 *) xmalloc(sizeof *n);
+ n->addr = loc;
+ n->value = v;
+ n->next = ifile->mips_hi16_list;
+ ifile->mips_hi16_list = n;
+ break;
+ }
+
+ case R_MIPS_LO16:
+ {
+ unsigned long insnlo = *loc;
+ Elf32_Addr val, vallo;
+
+ /* Sign extend the addend we extract from the lo insn. */
+ vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
+
+ if (ifile->mips_hi16_list != NULL) {
+ struct mips_hi16 *l;
+
+ l = ifile->mips_hi16_list;
+ while (l != NULL) {
+ struct mips_hi16 *next;
+ unsigned long insn;
+
+ /* The value for the HI16 had best be the same. */
+ assert(v == l->value);
+
+ /* Do the HI16 relocation. Note that we actually don't
+ need to know anything about the LO16 itself, except where
+ to find the low 16 bits of the addend needed by the LO16. */
+ insn = *l->addr;
+ val =
+ ((insn & 0xffff) << 16) +
+ vallo;
+ val += v;
+
+ /* Account for the sign extension that will happen in the
+ low bits. */
+ val =
+ ((val >> 16) +
+ ((val & 0x8000) !=
+ 0)) & 0xffff;
+
+ insn = (insn & ~0xffff) | val;
+ *l->addr = insn;
+
+ next = l->next;
+ free(l);
+ l = next;
+ }
+
+ ifile->mips_hi16_list = NULL;
+ }
+
+ /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
+ val = v + vallo;
+ insnlo = (insnlo & ~0xffff) | (val & 0xffff);
+ *loc = insnlo;
+ break;
+ }
+#endif
+
#if defined(__arm__)
#elif defined(__sh__)
case R_SH_REL32:
case R_386_PC32:
*loc += v - dot;
break;
+#elif defined(__powerpc__)
+ case R_PPC_REL32:
+ *loc = v - dot;
+ break;
#endif
#if defined(__sh__)
case R_SH_PLT32:
*loc = v - dot;
break;
-#elif defined(__arm__)
+#elif defined(__i386__)
+#endif
+
+#if defined(BB_USE_PLT_ENTRIES)
+
+#if defined(__arm__)
case R_ARM_PC24:
case R_ARM_PLT32:
+#endif
+#if defined(__powerpc__)
+ case R_PPC_REL24:
+#endif
/* find the plt entry and initialize it if necessary */
assert(isym != NULL);
- pe = (struct arm_plt_entry*) &isym->pltent;
+
+ pe = (struct arch_plt_entry*) &isym->pltent;
+
if (! pe->inited) {
ip = (unsigned long *) (ifile->plt->contents + pe->offset);
+
+ /* generate some machine code */
+
+#if defined(__arm__)
ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
ip[1] = v; /* sym@ */
+#endif
+#if defined(__powerpc__)
+ ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
+ ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
+ ip[2] = 0x7d6903a6; /* mtctr r11 */
+ ip[3] = 0x4e800420; /* bctr */
+#endif
pe->inited = 1;
}
if (v & 3)
ret = obj_reloc_dangerous;
+ /* merge the offset into the instruction. */
+#if defined(__arm__)
/* Convert to words. */
v >>= 2;
- /* merge the offset into the instruction. */
*loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
- break;
-#elif defined(__i386__)
#endif
-
+#if defined(__powerpc__)
+ *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
+#endif
+ break;
+#endif /* BB_USE_PLT_ENTRIES */
#if defined(__arm__)
#elif defined(__sh__)
break;
#endif
+#if defined(BB_USE_GOT_ENTRIES)
+
#if defined(__sh__)
case R_SH_GOTPC:
#elif defined(__arm__)
#if defined(__sh__)
case R_SH_GOT32:
#elif defined(__arm__)
- case R_ARM_GOT32:
+ case R_ARM_GOT32:
#elif defined(__i386__)
case R_386_GOT32:
#endif
#if defined(__sh__)
case R_SH_GOTOFF:
#elif defined(__arm__)
- case R_ARM_GOTOFF:
+ case R_ARM_GOTOFF:
#elif defined(__i386__)
case R_386_GOTOFF:
#endif
*loc += v - got;
break;
+#endif /* BB_USE_GOT_ENTRIES */
+
default:
printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
ret = obj_reloc_unhandled;
int arch_create_got(struct obj_file *f)
{
+#if defined(BB_USE_GOT_ENTRIES) || defined(BB_USE_PLT_ENTRIES)
struct arch_file *ifile = (struct arch_file *) f;
- int i, got_offset = 0, gotneeded = 0;
-#if defined(__arm__)
+ int i;
+#if defined(BB_USE_GOT_ENTRIES)
+ int got_offset = 0, gotneeded = 0;
+#endif
+#if defined(BB_USE_PLT_ENTRIES)
int plt_offset = 0, pltneeded = 0;
#endif
struct obj_section *relsec, *symsec, *strsec;
switch (ELF32_R_TYPE(rel->r_info)) {
#if defined(__arm__)
case R_ARM_GOT32:
+ break;
#elif defined(__sh__)
case R_SH_GOT32:
+ break;
#elif defined(__i386__)
case R_386_GOT32:
+ break;
#endif
+
+#if defined(__powerpc__)
+ case R_PPC_REL24:
+ pltneeded = 1;
break;
+#endif
#if defined(__arm__)
case R_ARM_PC24:
name = f->sections[extsym->st_shndx]->name;
}
intsym = (struct arch_symbol *) obj_find_symbol(f, name);
-
+#if defined(BB_USE_GOT_ENTRIES)
if (!intsym->gotent.offset_done) {
intsym->gotent.offset_done = 1;
intsym->gotent.offset = got_offset;
- got_offset += 4;
+ got_offset += BB_GOT_ENTRY_SIZE;
}
-#if defined(__arm__)
+#endif
+#if defined(BB_USE_PLT_ENTRIES)
if (pltneeded && intsym->pltent.allocated == 0) {
intsym->pltent.allocated = 1;
intsym->pltent.offset = plt_offset;
- plt_offset += 8;
+ plt_offset += BB_PLT_ENTRY_SIZE;
intsym->pltent.inited = 0;
pltneeded = 0;
}
}
}
-#if defined(__arm__)
+#if defined(BB_USE_GOT_ENTRIES)
if (got_offset) {
- struct obj_section* relsec = obj_find_section(f, ".got");
+ struct obj_section* myrelsec = obj_find_section(f, ".got");
- if (relsec) {
- obj_extend_section(relsec, got_offset);
+ if (myrelsec) {
+ obj_extend_section(myrelsec, got_offset);
} else {
- relsec = obj_create_alloced_section(f, ".got", 8, got_offset);
- assert(relsec);
+ myrelsec = obj_create_alloced_section(f, ".got",
+ BB_GOT_ENTRY_SIZE,
+ got_offset);
+ assert(myrelsec);
}
- ifile->got = relsec;
+ ifile->got = myrelsec;
}
+#endif
+#if defined(BB_USE_PLT_ENTRIES)
if (plt_offset)
- ifile->plt = obj_create_alloced_section(f, ".plt", 8, plt_offset);
-#else
- if (got_offset > 0 || gotneeded)
- ifile->got = obj_create_alloced_section(f, ".got", 4, got_offset);
+ ifile->plt = obj_create_alloced_section(f, ".plt",
+ BB_PLT_ENTRY_SIZE,
+ plt_offset);
+#endif
#endif
-
return 1;
}
while (*q++ == ',');
} else {
char *contents = f->sections[sym->secidx]->contents;
- char *loc = contents + sym->value;
+ char *myloc = contents + sym->value;
char *r; /* To search for commas */
/* Break the string with comas */
while ((r = strchr(q, ',')) != (char *) NULL) {
*r++ = '\0';
- obj_string_patch(f, sym->secidx, loc - contents, q);
- loc += sizeof(char *);
+ obj_string_patch(f, sym->secidx, myloc - contents, q);
+ myloc += sizeof(char *);
q = r;
}
/* last part */
- obj_string_patch(f, sym->secidx, loc - contents, q);
+ obj_string_patch(f, sym->secidx, myloc - contents, q);
}
argc--, argv++;
int nks, nms, nmod, i;
nks = get_kernel_syms(NULL);
- if (nks < 0) {
- perror_msg("get_kernel_syms: %s", m_name);
+ if (nks <= 0) {
+ if (nks)
+ perror_msg("get_kernel_syms: %s", m_name);
+ else
+ error_msg("No kernel symbols");
return 0;
}
while (k->name[0] == '#' && k->name[1]) {
struct old_kernel_sym *k2;
- struct new_module_symbol *s;
/* Find out how many symbols this module has. */
for (k2 = k + 1; k2->name[0] != '#'; ++k2)
module_names = xmalloc(bufsize = 256);
retry_modules_load:
if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
- if (errno == ENOSPC) {
+ if (errno == ENOSPC && bufsize < ret) {
module_names = xrealloc(module_names, bufsize = ret);
goto retry_modules_load;
}
}
n_ext_modules = nmod = ret;
- ext_modules = modules = xmalloc(nmod * sizeof(*modules));
- memset(modules, 0, nmod * sizeof(*modules));
/* Collect the modules' symbols. */
- for (i = 0, mn = module_names, m = modules;
- i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
- struct new_module_info info;
-
- if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
- if (errno == ENOENT) {
- /* The module was removed out from underneath us. */
- continue;
- }
- perror_msg("query_module: QM_INFO: %s", mn);
- return 0;
- }
-
- syms = xmalloc(bufsize = 1024);
- retry_mod_sym_load:
- if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
- switch (errno) {
- case ENOSPC:
- syms = xrealloc(syms, bufsize = ret);
- goto retry_mod_sym_load;
- case ENOENT:
- /* The module was removed out from underneath us. */
- continue;
- default:
- perror_msg("query_module: QM_SYMBOLS: %s", mn);
+ if (nmod){
+ ext_modules = modules = xmalloc(nmod * sizeof(*modules));
+ memset(modules, 0, nmod * sizeof(*modules));
+ for (i = 0, mn = module_names, m = modules;
+ i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
+ struct new_module_info info;
+
+ if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
+ if (errno == ENOENT) {
+ /* The module was removed out from underneath us. */
+ continue;
+ }
+ perror_msg("query_module: QM_INFO: %s", mn);
return 0;
}
- }
- nsyms = ret;
-
- m->name = mn;
- m->addr = info.addr;
- m->nsyms = nsyms;
- m->syms = syms;
-
- for (j = 0, s = syms; j < nsyms; ++j, ++s) {
- s->name += (unsigned long) syms;
+
+ syms = xmalloc(bufsize = 1024);
+ retry_mod_sym_load:
+ if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
+ switch (errno) {
+ case ENOSPC:
+ syms = xrealloc(syms, bufsize = ret);
+ goto retry_mod_sym_load;
+ case ENOENT:
+ /* The module was removed out from underneath us. */
+ continue;
+ default:
+ perror_msg("query_module: QM_SYMBOLS: %s", mn);
+ return 0;
+ }
+ }
+ nsyms = ret;
+
+ m->name = mn;
+ m->addr = info.addr;
+ m->nsyms = nsyms;
+ m->syms = syms;
+
+ for (j = 0, s = syms; j < nsyms; ++j, ++s) {
+ s->name += (unsigned long) syms;
+ }
}
}
syms = xmalloc(bufsize = 16 * 1024);
retry_kern_sym_load:
if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
- if (errno == ENOSPC) {
+ if (errno == ENOSPC && bufsize < ret) {
syms = xrealloc(syms, bufsize = ret);
goto retry_kern_sym_load;
}
for (sec = f->load_order; sec; sec = sec->load_next) {
char *secimg;
- if (sec->header.sh_size == 0)
+ if (sec->contents == 0 || sec->header.sh_size == 0)
continue;
secimg = image + (sec->header.sh_addr - base);
sec->header = section_headers[i];
sec->idx = i;
- switch (sec->header.sh_type) {
+ if(sec->header.sh_size) switch (sec->header.sh_type) {
case SHT_NULL:
case SHT_NOTE:
case SHT_NOBITS:
for (i = 0; i < shnum; ++i) {
struct obj_section *sec = f->sections[i];
+ /* .modinfo should be contents only but gcc has no attribute for that.
+ * The kernel may have marked .modinfo as ALLOC, ignore this bit.
+ */
+ if (strcmp(sec->name, ".modinfo") == 0)
+ sec->header.sh_flags &= ~SHF_ALLOC;
+
if (sec->header.sh_flags & SHF_ALLOC)
obj_insert_section_load_order(f, sec);
/* Allocate space for a table of local symbols. */
j = f->local_symtab_size = sec->header.sh_info;
- f->local_symtab = xmalloc(j *=
- sizeof(struct obj_symbol *));
- memset(f->local_symtab, 0, j);
+ f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
/* Insert all symbols into the hash table. */
for (j = 1, ++sym; j < nsym; ++j, ++sym) {
const char *name;
if (sym->st_name)
name = strtab + sym->st_name;
- else
+ else
name = f->sections[sym->st_shndx]->name;
obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
sym->st_value, sym->st_size);
- }
- }
+ }
+ }
break;
case SHT_RELM:
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(). */
}
}
ElfW(Addr) m_addr;
FILE *fp;
struct obj_file *f;
+ struct stat st;
char m_name[BUFSIZ + 1] = "\0";
int exit_status = EXIT_FAILURE;
int m_has_modinfo;
if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
len -= 2;
- strncpy(m_fullName, tmp, len);
+ memcpy(m_fullName, tmp, len);
+ m_fullName[len]='\0';
if (*m_name == '\0') {
strcpy(m_name, m_fullName);
}
strcat(m_fullName, ".o");
/* Get a filedesc for the module */
- if ((fp = fopen(argv[optind], "r")) == NULL) {
+ if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
+ (fp = fopen(argv[optind], "r")) == NULL) {
/* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE,
findNamedModule, 0, m_fullName) == FALSE)
} else
error_msg_and_die("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
} else
- memcpy(m_filename, argv[optind], strlen(argv[optind]));
+ safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
if ((f = obj_load(fp)) == NULL)