Hi Denys,
> Denys Vlasenko schrieb:
>> But it does not look to be _too_ hard. bb_init_module_24()
>> is an entry point to 2.4 module loading. It opens a file.
>> Then file descriptor gets passed _only_ to obj_load()
>> and obj_load_progbits(). Those only ever seek and read from it.
>>
>> By replacing "int fd" param with "void *image, size_t image_size"
>> pair and passing it instead, we can work on in-memory images.
>> Unpacking and reading of .gz modules is _extremely_ easy too:
>>
>> size_t len = MAXINT(ssize_t);
>> void *image = xmalloc_open_zipped_read_close(filename, &len);
>>
>> That's it.
>>
>> Care to make it work, Guenter?
please take a look at attached patch; this seems to work almost for me =
means that insmod as well as modprobe load gzip'd as well as
non-compressed modules fine, but currently I see another prob regarding
dependencies, but I guess this is a separate prob with moddep stuff...

attached patch is a diff -u against modutils-24.c from current svn
trunk; it does not yet patch obj_load_progbits() since there another
function for loading is used, and was unsure if I can replace there in
same way as I did in obj_load(); also I want to hear your comments first
if my patch is what you expect to see...

===================================================================
--- modutils/modutils-24.c.orig 2009-03-30 06:00:52.000000000 +0200
+++ modutils/modutils-24.c      2009-04-01 22:08:50.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;
@@ -3776,18 +3779,23 @@
        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)
+
+       if (strstr(m_filename, ".gz"))
+               m_image = xmalloc_open_zipped_read_close(m_filename, &len);
+       else
+               m_image = xmalloc_open_read_close(m_filename, &len);
+       if (!m_image)
                return EXIT_FAILURE;

        m_name = xstrdup(bb_basename(m_filename));
-       p = strrchr(m_name, '.');
+       p = strchr(m_name, '.');
        if (p) *p = '\0';

-       f = obj_load(fd, LOADBITS);
+       f = obj_load(m_image, len, LOADBITS);

        m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);

@@ -3891,7 +3899,6 @@
        exit_status = EXIT_SUCCESS;

  out:
-       close(fd);
        free(m_name);

        return exit_status;

--- modutils/modutils-24.c.orig	2009-03-30 06:00:52.000000000 +0200
+++ modutils/modutils-24.c	2009-04-01 22:08:50.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;
@@ -3776,18 +3779,23 @@
 	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)
+
+	if (strstr(m_filename, ".gz"))
+		m_image = xmalloc_open_zipped_read_close(m_filename, &len);
+	else
+		m_image = xmalloc_open_read_close(m_filename, &len);
+	if (!m_image)
 		return EXIT_FAILURE;
 
 	m_name = xstrdup(bb_basename(m_filename));
-	p = strrchr(m_name, '.');
+	p = strchr(m_name, '.');
 	if (p) *p = '\0';
 
-	f = obj_load(fd, LOADBITS);
+	f = obj_load(m_image, len, LOADBITS);
 
 	m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
 
@@ -3891,7 +3899,6 @@
 	exit_status = EXIT_SUCCESS;
 
  out:
-	close(fd);
 	free(m_name);
 
 	return exit_status;
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to