diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-03-29 16:38:59 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-03-29 16:38:59 +0000 |
commit | e1de3af9892a89156c109befa13403359ee8568f (patch) | |
tree | c95c223c13eaa470f6749b064e2c6379c1cae138 /modutils/modutils-24.c | |
parent | fd303b11efb2c87ed239b36c432566a36f859c8e (diff) | |
download | busybox-w32-e1de3af9892a89156c109befa13403359ee8568f.tar.gz busybox-w32-e1de3af9892a89156c109befa13403359ee8568f.tar.bz2 busybox-w32-e1de3af9892a89156c109befa13403359ee8568f.zip |
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
Diffstat (limited to 'modutils/modutils-24.c')
-rw-r--r-- | modutils/modutils-24.c | 62 |
1 files changed, 27 insertions, 35 deletions
diff --git a/modutils/modutils-24.c b/modutils/modutils-24.c index 169fe54ae..89bc30fe5 100644 --- a/modutils/modutils-24.c +++ b/modutils/modutils-24.c | |||
@@ -625,7 +625,7 @@ static unsigned long obj_load_size(struct obj_file *f); | |||
625 | 625 | ||
626 | static int obj_relocate(struct obj_file *f, ElfW(Addr) base); | 626 | static int obj_relocate(struct obj_file *f, ElfW(Addr) base); |
627 | 627 | ||
628 | static struct obj_file *obj_load(FILE *f, int loadprogbits); | 628 | static struct obj_file *obj_load(int fd, int loadprogbits); |
629 | 629 | ||
630 | static int obj_create_image(struct obj_file *f, char *image); | 630 | static int obj_create_image(struct obj_file *f, char *image); |
631 | 631 | ||
@@ -3191,8 +3191,13 @@ static int obj_create_image(struct obj_file *f, char *image) | |||
3191 | 3191 | ||
3192 | /*======================================================================*/ | 3192 | /*======================================================================*/ |
3193 | 3193 | ||
3194 | static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM) | 3194 | static struct obj_file *obj_load(int fd, int loadprogbits UNUSED_PARAM) |
3195 | { | 3195 | { |
3196 | #if BB_LITTLE_ENDIAN | ||
3197 | # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3))))) | ||
3198 | #else | ||
3199 | # define ELFMAG_U32 ((uint32_t)((((ELFMAG0 * 0x100) + ELFMAG1) * 0x100 + ELFMAG2) * 0x100 + ELFMAG3)) | ||
3200 | #endif | ||
3196 | struct obj_file *f; | 3201 | struct obj_file *f; |
3197 | ElfW(Shdr) * section_headers; | 3202 | ElfW(Shdr) * section_headers; |
3198 | size_t shnum, i; | 3203 | size_t shnum, i; |
@@ -3205,16 +3210,10 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM) | |||
3205 | f->symbol_hash = obj_elf_hash; | 3210 | f->symbol_hash = obj_elf_hash; |
3206 | f->load_order_search_start = &f->load_order; | 3211 | f->load_order_search_start = &f->load_order; |
3207 | 3212 | ||
3208 | fseek(fp, 0, SEEK_SET); | 3213 | xlseek(fd, 0, SEEK_SET); |
3209 | if (fread(&f->header, sizeof(f->header), 1, fp) != 1) { | 3214 | xread(fd, &f->header, sizeof(f->header)); |
3210 | bb_perror_msg_and_die("error reading ELF header"); | ||
3211 | } | ||
3212 | 3215 | ||
3213 | if (f->header.e_ident[EI_MAG0] != ELFMAG0 | 3216 | if (*(uint32_t*)(&f->header.e_ident) != ELFMAG_U32) { |
3214 | || f->header.e_ident[EI_MAG1] != ELFMAG1 | ||
3215 | || f->header.e_ident[EI_MAG2] != ELFMAG2 | ||
3216 | || f->header.e_ident[EI_MAG3] != ELFMAG3 | ||
3217 | ) { | ||
3218 | bb_error_msg_and_die("not an ELF file"); | 3217 | bb_error_msg_and_die("not an ELF file"); |
3219 | } | 3218 | } |
3220 | if (f->header.e_ident[EI_CLASS] != ELFCLASSM | 3219 | if (f->header.e_ident[EI_CLASS] != ELFCLASSM |
@@ -3243,10 +3242,8 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM) | |||
3243 | f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4)); | 3242 | f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4)); |
3244 | 3243 | ||
3245 | section_headers = alloca(sizeof(ElfW(Shdr)) * shnum); | 3244 | section_headers = alloca(sizeof(ElfW(Shdr)) * shnum); |
3246 | fseek(fp, f->header.e_shoff, SEEK_SET); | 3245 | xlseek(fd, f->header.e_shoff, SEEK_SET); |
3247 | if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) { | 3246 | xread(fd, section_headers, sizeof(ElfW(Shdr)) * shnum); |
3248 | bb_perror_msg_and_die("error reading ELF section headers"); | ||
3249 | } | ||
3250 | 3247 | ||
3251 | /* Read the section data. */ | 3248 | /* Read the section data. */ |
3252 | 3249 | ||
@@ -3278,10 +3275,8 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM) | |||
3278 | sec->contents = NULL; | 3275 | sec->contents = NULL; |
3279 | if (sec->header.sh_size > 0) { | 3276 | if (sec->header.sh_size > 0) { |
3280 | sec->contents = xmalloc(sec->header.sh_size); | 3277 | sec->contents = xmalloc(sec->header.sh_size); |
3281 | fseek(fp, sec->header.sh_offset, SEEK_SET); | 3278 | xlseek(fd, sec->header.sh_offset, SEEK_SET); |
3282 | if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) { | 3279 | xread(fd, sec->contents, sec->header.sh_size); |
3283 | bb_perror_msg_and_die("error reading ELF section data"); | ||
3284 | } | ||
3285 | } | 3280 | } |
3286 | break; | 3281 | break; |
3287 | #if SHT_RELM == SHT_REL | 3282 | #if SHT_RELM == SHT_REL |
@@ -3397,27 +3392,24 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM) | |||
3397 | * kernel for the module | 3392 | * kernel for the module |
3398 | */ | 3393 | */ |
3399 | 3394 | ||
3400 | static int obj_load_progbits(FILE *fp, struct obj_file *f, char *imagebase) | 3395 | static int obj_load_progbits(int fd, struct obj_file *f, char *imagebase) |
3401 | { | 3396 | { |
3402 | ElfW(Addr) base = f->baseaddr; | 3397 | ElfW(Addr) base = f->baseaddr; |
3403 | struct obj_section* sec; | 3398 | struct obj_section* sec; |
3404 | 3399 | ||
3405 | for (sec = f->load_order; sec; sec = sec->load_next) { | 3400 | for (sec = f->load_order; sec; sec = sec->load_next) { |
3406 | |||
3407 | /* section already loaded? */ | 3401 | /* section already loaded? */ |
3408 | if (sec->contents != NULL) | 3402 | if (sec->contents != NULL) |
3409 | continue; | 3403 | continue; |
3410 | |||
3411 | if (sec->header.sh_size == 0) | 3404 | if (sec->header.sh_size == 0) |
3412 | continue; | 3405 | continue; |
3413 | |||
3414 | sec->contents = imagebase + (sec->header.sh_addr - base); | 3406 | sec->contents = imagebase + (sec->header.sh_addr - base); |
3415 | fseek(fp, sec->header.sh_offset, SEEK_SET); | 3407 | xlseek(fd, sec->header.sh_offset, SEEK_SET); |
3416 | if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) { | 3408 | errno = 0; /* read may be short without errno being set */ |
3409 | if (full_read(fd, sec->contents, sec->header.sh_size) != sec->header.sh_size) { | ||
3417 | bb_perror_msg("error reading ELF section data"); | 3410 | bb_perror_msg("error reading ELF section data"); |
3418 | return 0; | 3411 | return 0; |
3419 | } | 3412 | } |
3420 | |||
3421 | } | 3413 | } |
3422 | return 1; | 3414 | return 1; |
3423 | } | 3415 | } |
@@ -3779,22 +3771,23 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options) | |||
3779 | struct utsname uts; | 3771 | struct utsname uts; |
3780 | int exit_status = EXIT_FAILURE; | 3772 | int exit_status = EXIT_FAILURE; |
3781 | int m_has_modinfo; | 3773 | int m_has_modinfo; |
3782 | char *m_name; | 3774 | char *m_name, *p; |
3783 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING | 3775 | #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING |
3784 | char m_strversion[STRVERSIONLEN]; | 3776 | char m_strversion[STRVERSIONLEN]; |
3785 | int m_version, m_crcs; | 3777 | int m_version, m_crcs; |
3786 | #endif | 3778 | #endif |
3787 | FILE *fp; | 3779 | int fd; |
3788 | 3780 | ||
3789 | uname(&uts); | 3781 | uname(&uts); |
3790 | fp = fopen_for_read(m_filename); | 3782 | fd = open_or_warn(m_filename, O_RDONLY); |
3791 | if (fp == NULL) | 3783 | if (fd < 0) |
3792 | return EXIT_FAILURE; | 3784 | return EXIT_FAILURE; |
3793 | 3785 | ||
3794 | m_name = xstrdup(bb_basename(m_filename)); | 3786 | m_name = xstrdup(bb_basename(m_filename)); |
3795 | *strrchr(m_name, '.') = 0; | 3787 | p = strrchr(m_name, '.'); |
3788 | if (p) *p = '\0'; | ||
3796 | 3789 | ||
3797 | f = obj_load(fp, LOADBITS); | 3790 | f = obj_load(fd, LOADBITS); |
3798 | 3791 | ||
3799 | m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL); | 3792 | m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL); |
3800 | 3793 | ||
@@ -3876,7 +3869,7 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options) | |||
3876 | * the PROGBITS section was not loaded by the obj_load | 3869 | * the PROGBITS section was not loaded by the obj_load |
3877 | * now we can load them directly into the kernel memory | 3870 | * now we can load them directly into the kernel memory |
3878 | */ | 3871 | */ |
3879 | if (!obj_load_progbits(fp, f, (char*)m_addr)) { | 3872 | if (!obj_load_progbits(fd, f, (char*)m_addr)) { |
3880 | delete_module(m_name, 0); | 3873 | delete_module(m_name, 0); |
3881 | goto out; | 3874 | goto out; |
3882 | } | 3875 | } |
@@ -3898,8 +3891,7 @@ int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options) | |||
3898 | exit_status = EXIT_SUCCESS; | 3891 | exit_status = EXIT_SUCCESS; |
3899 | 3892 | ||
3900 | out: | 3893 | out: |
3901 | if (fp) | 3894 | close(fd); |
3902 | fclose(fp); | ||
3903 | free(m_name); | 3895 | free(m_name); |
3904 | 3896 | ||
3905 | return exit_status; | 3897 | return exit_status; |