Hi Denys,
here's next attempt attached based on your previous comemnts...

in obj_load_progbits() I've added a //FIXME! comment;
this is because I think the previous used bb_perror_msg() function is
now wrong here because we have no longer an errno set; can you please
point me to a proper bb_* function for error output?

thanks, Guen.


--- modutils/modutils-24.c.orig 2009-03-30 06:00:52.000000000 +0200
+++ modutils/modutils-24.c      2009-04-02 18:28:06.000000000 +0200
@@ -625,7 +625,7 @@
 
 static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
 
-static struct obj_file *obj_load(int fd, int loadprogbits);
+static struct obj_file *obj_load(void *memimg, size_t memimg_size, int 
loadprogbits);
 
 static int obj_create_image(struct obj_file *f, char *image);
 
@@ -3191,7 +3191,7 @@
 
 /*======================================================================*/
 
-static struct obj_file *obj_load(int fd, int loadprogbits UNUSED_PARAM)
+static struct obj_file *obj_load(void *memimg, size_t memimg_size, int 
loadprogbits UNUSED_PARAM)
 {
 #if BB_LITTLE_ENDIAN
 # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 
+ 0x100 * ELFMAG3)))))
@@ -3210,8 +3210,9 @@
        f->symbol_hash = obj_elf_hash;
        f->load_order_search_start = &f->load_order;
 
-       xlseek(fd, 0, SEEK_SET);
-       xread(fd, &f->header, sizeof(f->header));
+       if (memimg_size < sizeof(f->header))
+               bb_error_msg_and_die("loaded memory image seems too small");
+       memcpy(&f->header, memimg, sizeof(f->header));
 
        if (*(uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
                bb_error_msg_and_die("not an ELF file");
@@ -3242,8 +3243,9 @@
        f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
 
        section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
-       xlseek(fd, f->header.e_shoff, SEEK_SET);
-       xread(fd, section_headers, sizeof(ElfW(Shdr)) * shnum);
+       if (memimg_size < (f->header.e_shoff + sizeof(ElfW(Shdr)) * shnum))
+               bb_error_msg_and_die("loaded memory image seems too small");
+       memcpy(section_headers, memimg + f->header.e_shoff, sizeof(ElfW(Shdr)) 
* shnum);
 
        /* Read the section data.  */
 
@@ -3275,8 +3277,9 @@
                                sec->contents = NULL;
                                if (sec->header.sh_size > 0) {
                                        sec->contents = 
xmalloc(sec->header.sh_size);
-                                       xlseek(fd, sec->header.sh_offset, 
SEEK_SET);
-                                       xread(fd, sec->contents, 
sec->header.sh_size);
+                                       if (memimg_size < 
(sec->header.sh_offset + sec->header.sh_size))
+                                               bb_error_msg_and_die("loaded 
memory image seems too small");
+                                       memcpy(sec->contents, memimg + 
sec->header.sh_offset, sec->header.sh_size);
                                }
                                break;
 #if SHT_RELM == SHT_REL
@@ -3392,7 +3395,7 @@
  * kernel for the module
  */
 
-static int obj_load_progbits(int fd, struct obj_file *f, char *imagebase)
+static int obj_load_progbits(void *memimg, size_t memimg_size, struct obj_file 
*f, char *imagebase)
 {
        ElfW(Addr) base = f->baseaddr;
        struct obj_section* sec;
@@ -3404,12 +3407,11 @@
                if (sec->header.sh_size == 0)
                        continue;
                sec->contents = imagebase + (sec->header.sh_addr - base);
-               xlseek(fd, sec->header.sh_offset, SEEK_SET);
-               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");
+               if (memimg_size < (sec->header.sh_offset + 
sec->header.sh_size)) {
+                       bb_perror_msg("error reading ELF section data"); 
//FIXME!
                        return 0;
                }
+               memcpy(sec->contents, memimg + sec->header.sh_offset, 
sec->header.sh_size);
        }
        return 1;
 }
@@ -3771,23 +3773,26 @@
        struct utsname uts;
        int exit_status = EXIT_FAILURE;
        int m_has_modinfo;
-       char *m_name, *p;
+       char *m_name;
 #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
        char m_strversion[STRVERSIONLEN];
        int m_version, m_crcs;
 #endif
-       int fd;
+       void *m_image = NULL;
+       size_t len = MAXINT(ssize_t);
 
        uname(&uts);
-       fd = open_or_warn(m_filename, O_RDONLY);
-       if (fd < 0)
+
+       /* load module into memory and unzip if compressed */
+       m_image = xmalloc_open_zipped_read_close(m_filename, &len);
+       if (!m_image)
                return EXIT_FAILURE;
 
        m_name = xstrdup(bb_basename(m_filename));
-       p = strrchr(m_name, '.');
-       if (p) *p = '\0';
+       /* "module.o[.gz]" -> "module" */
+       *strchrnul(m_name, '.') = '\0';
 
-       f = obj_load(fd, LOADBITS);
+       f = obj_load(m_image, len, LOADBITS);
 
        m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
 
@@ -3869,7 +3874,7 @@
         * the PROGBITS section was not loaded by the obj_load
         * now we can load them directly into the kernel memory
         */
-       if (!obj_load_progbits(fd, f, (char*)m_addr)) {
+       if (!obj_load_progbits(m_image, len, f, (char*)m_addr)) {
                delete_module(m_name, 0);
                goto out;
        }
@@ -3891,7 +3896,7 @@
        exit_status = EXIT_SUCCESS;
 
  out:
-       close(fd);
+       free(m_image);
        free(m_name);
 
        return exit_status;
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to