module loading for 2.4: use fd-based io instead of FILE based;
use xlseek and xread; simpler check for ELF signature. text data bss dec hex filename 824460 476 7616 832552 cb428 busybox_old 824333 476 7616 832425 cb3a9 busybox_unstripped
This commit is contained in:
parent
fd303b11ef
commit
e1de3af989
@ -625,7 +625,7 @@ static unsigned long obj_load_size(struct obj_file *f);
|
|||||||
|
|
||||||
static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
|
static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
|
||||||
|
|
||||||
static struct obj_file *obj_load(FILE *f, int loadprogbits);
|
static struct obj_file *obj_load(int fd, int loadprogbits);
|
||||||
|
|
||||||
static int obj_create_image(struct obj_file *f, char *image);
|
static int obj_create_image(struct obj_file *f, char *image);
|
||||||
|
|
||||||
@ -3191,8 +3191,13 @@ static int obj_create_image(struct obj_file *f, char *image)
|
|||||||
|
|
||||||
/*======================================================================*/
|
/*======================================================================*/
|
||||||
|
|
||||||
static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
|
static struct obj_file *obj_load(int fd, int loadprogbits UNUSED_PARAM)
|
||||||
{
|
{
|
||||||
|
#if BB_LITTLE_ENDIAN
|
||||||
|
# define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3)))))
|
||||||
|
#else
|
||||||
|
# define ELFMAG_U32 ((uint32_t)((((ELFMAG0 * 0x100) + ELFMAG1) * 0x100 + ELFMAG2) * 0x100 + ELFMAG3))
|
||||||
|
#endif
|
||||||
struct obj_file *f;
|
struct obj_file *f;
|
||||||
ElfW(Shdr) * section_headers;
|
ElfW(Shdr) * section_headers;
|
||||||
size_t shnum, i;
|
size_t shnum, i;
|
||||||
@ -3205,16 +3210,10 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
|
|||||||
f->symbol_hash = obj_elf_hash;
|
f->symbol_hash = obj_elf_hash;
|
||||||
f->load_order_search_start = &f->load_order;
|
f->load_order_search_start = &f->load_order;
|
||||||
|
|
||||||
fseek(fp, 0, SEEK_SET);
|
xlseek(fd, 0, SEEK_SET);
|
||||||
if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
|
xread(fd, &f->header, sizeof(f->header));
|
||||||
bb_perror_msg_and_die("error reading ELF header");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (f->header.e_ident[EI_MAG0] != ELFMAG0
|
if (*(uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
|
||||||
|| f->header.e_ident[EI_MAG1] != ELFMAG1
|
|
||||||
|| f->header.e_ident[EI_MAG2] != ELFMAG2
|
|
||||||
|| f->header.e_ident[EI_MAG3] != ELFMAG3
|
|
||||||
) {
|
|
||||||
bb_error_msg_and_die("not an ELF file");
|
bb_error_msg_and_die("not an ELF file");
|
||||||
}
|
}
|
||||||
if (f->header.e_ident[EI_CLASS] != ELFCLASSM
|
if (f->header.e_ident[EI_CLASS] != ELFCLASSM
|
||||||
@ -3243,10 +3242,8 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
|
|||||||
f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
|
f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
|
||||||
|
|
||||||
section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
|
section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
|
||||||
fseek(fp, f->header.e_shoff, SEEK_SET);
|
xlseek(fd, f->header.e_shoff, SEEK_SET);
|
||||||
if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
|
xread(fd, section_headers, sizeof(ElfW(Shdr)) * shnum);
|
||||||
bb_perror_msg_and_die("error reading ELF section headers");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Read the section data. */
|
/* Read the section data. */
|
||||||
|
|
||||||
@ -3278,10 +3275,8 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
|
|||||||
sec->contents = NULL;
|
sec->contents = NULL;
|
||||||
if (sec->header.sh_size > 0) {
|
if (sec->header.sh_size > 0) {
|
||||||
sec->contents = xmalloc(sec->header.sh_size);
|
sec->contents = xmalloc(sec->header.sh_size);
|
||||||
fseek(fp, sec->header.sh_offset, SEEK_SET);
|
xlseek(fd, sec->header.sh_offset, SEEK_SET);
|
||||||
if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
|
xread(fd, sec->contents, sec->header.sh_size);
|
||||||
bb_perror_msg_and_die("error reading ELF section data");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
#if SHT_RELM == SHT_REL
|
#if SHT_RELM == SHT_REL
|
||||||
@ -3397,27 +3392,24 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
|
|||||||
* kernel for the module
|
* kernel for the module
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int obj_load_progbits(FILE *fp, struct obj_file *f, char *imagebase)
|
static int obj_load_progbits(int fd, struct obj_file *f, char *imagebase)
|
||||||
{
|
{
|
||||||
ElfW(Addr) base = f->baseaddr;
|
ElfW(Addr) base = f->baseaddr;
|
||||||
struct obj_section* sec;
|
struct obj_section* sec;
|
||||||
|
|
||||||
for (sec = f->load_order; sec; sec = sec->load_next) {
|
for (sec = f->load_order; sec; sec = sec->load_next) {
|
||||||
|
|
||||||
/* section already loaded? */
|
/* section already loaded? */
|
||||||
if (sec->contents != NULL)
|
if (sec->contents != NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (sec->header.sh_size == 0)
|
if (sec->header.sh_size == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
sec->contents = imagebase + (sec->header.sh_addr - base);
|
sec->contents = imagebase + (sec->header.sh_addr - base);
|
||||||
fseek(fp, sec->header.sh_offset, SEEK_SET);
|
xlseek(fd, sec->header.sh_offset, SEEK_SET);
|
||||||
if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
|
errno = 0; /* read may be short without errno being set */
|
||||||
|
if (full_read(fd, sec->contents, sec->header.sh_size) != sec->header.sh_size) {
|
||||||
bb_perror_msg("error reading ELF section data");
|
bb_perror_msg("error reading ELF section data");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -3779,22 +3771,23 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
|
|||||||
struct utsname uts;
|
struct utsname uts;
|
||||||
int exit_status = EXIT_FAILURE;
|
int exit_status = EXIT_FAILURE;
|
||||||
int m_has_modinfo;
|
int m_has_modinfo;
|
||||||
char *m_name;
|
char *m_name, *p;
|
||||||
#if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
|
#if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
|
||||||
char m_strversion[STRVERSIONLEN];
|
char m_strversion[STRVERSIONLEN];
|
||||||
int m_version, m_crcs;
|
int m_version, m_crcs;
|
||||||
#endif
|
#endif
|
||||||
FILE *fp;
|
int fd;
|
||||||
|
|
||||||
uname(&uts);
|
uname(&uts);
|
||||||
fp = fopen_for_read(m_filename);
|
fd = open_or_warn(m_filename, O_RDONLY);
|
||||||
if (fp == NULL)
|
if (fd < 0)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
m_name = xstrdup(bb_basename(m_filename));
|
m_name = xstrdup(bb_basename(m_filename));
|
||||||
*strrchr(m_name, '.') = 0;
|
p = strrchr(m_name, '.');
|
||||||
|
if (p) *p = '\0';
|
||||||
|
|
||||||
f = obj_load(fp, LOADBITS);
|
f = obj_load(fd, LOADBITS);
|
||||||
|
|
||||||
m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
|
m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
|
||||||
|
|
||||||
@ -3876,7 +3869,7 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
|
|||||||
* the PROGBITS section was not loaded by the obj_load
|
* the PROGBITS section was not loaded by the obj_load
|
||||||
* now we can load them directly into the kernel memory
|
* now we can load them directly into the kernel memory
|
||||||
*/
|
*/
|
||||||
if (!obj_load_progbits(fp, f, (char*)m_addr)) {
|
if (!obj_load_progbits(fd, f, (char*)m_addr)) {
|
||||||
delete_module(m_name, 0);
|
delete_module(m_name, 0);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
@ -3898,8 +3891,7 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
|
|||||||
exit_status = EXIT_SUCCESS;
|
exit_status = EXIT_SUCCESS;
|
||||||
|
|
||||||
out:
|
out:
|
||||||
if (fp)
|
close(fd);
|
||||||
fclose(fp);
|
|
||||||
free(m_name);
|
free(m_name);
|
||||||
|
|
||||||
return exit_status;
|
return exit_status;
|
||||||
|
Loading…
Reference in New Issue
Block a user