[PATCH] Revert "elf-ops.h: Map into memory the ELF to load"

Mohamed Mediouni posted 1 patch 3 days, 14 hours ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260226161146.68876-1-mohamed@unpredictable.fr
include/hw/elf_ops.h.inc | 71 +++++++++++++++-------------------------
1 file changed, 26 insertions(+), 45 deletions(-)
[PATCH] Revert "elf-ops.h: Map into memory the ELF to load"
Posted by Mohamed Mediouni 3 days, 14 hours ago
This reverts commit 816b9fe450220e19acb91a0ce4a8ade7000648d1.

Unfortunately, this optimisation breaks -kernel [x] on Windows hosts.

Reverting this commit enabled proceeding with kvm-unit-tests testing.

Signed-off-by: Mohamed Mediouni <mohamed@unpredictable.fr>
---
 include/hw/elf_ops.h.inc | 71 +++++++++++++++-------------------------
 1 file changed, 26 insertions(+), 45 deletions(-)

diff --git a/include/hw/elf_ops.h.inc b/include/hw/elf_ops.h.inc
index 9c35d1b9da..bcf484b938 100644
--- a/include/hw/elf_ops.h.inc
+++ b/include/hw/elf_ops.h.inc
@@ -325,11 +325,9 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
 {
     struct elfhdr ehdr;
     struct elf_phdr *phdr = NULL, *ph;
-    int size, i;
-    ssize_t total_size;
-    elf_word mem_size, file_size, data_offset;
+    int size, i, total_size;
+    elf_word mem_size, file_size;
     uint64_t addr, low = (uint64_t)-1, high = 0;
-    GMappedFile *mapped_file = NULL;
     uint8_t *data = NULL;
     ssize_t ret = ELF_LOAD_FAILED;
 
@@ -409,32 +407,20 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
         }
     }
 
-    /*
-     * Since we want to be able to modify the mapped buffer, we set the
-     * 'writable' parameter to 'true'. Modifications to the buffer are not
-     * written back to the file.
-     */
-    mapped_file = g_mapped_file_new_from_fd(fd, true, NULL);
-    if (!mapped_file) {
-        goto fail;
-    }
-
     total_size = 0;
     for(i = 0; i < ehdr.e_phnum; i++) {
         ph = &phdr[i];
         if (ph->p_type == PT_LOAD) {
             mem_size = ph->p_memsz; /* Size of the ROM */
             file_size = ph->p_filesz; /* Size of the allocated data */
-            data_offset = ph->p_offset; /* Offset where the data is located */
-
-            if (file_size > 0) {
-                if (g_mapped_file_get_length(mapped_file) <
-                    file_size + data_offset) {
+            data = g_malloc0(file_size);
+            if (ph->p_filesz > 0) {
+                if (lseek(fd, ph->p_offset, SEEK_SET) < 0) {
+                    goto fail;
+                }
+                if (read(fd, data, file_size) != file_size) {
                     goto fail;
                 }
-
-                data = (uint8_t *)g_mapped_file_get_contents(mapped_file);
-                data += data_offset;
             }
 
             /* The ELF spec is somewhat vague about the purpose of the
@@ -530,23 +516,22 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
                 *pentry = ehdr.e_entry - ph->p_vaddr + ph->p_paddr;
             }
 
-            /* Some ELF files really do have segments of zero size;
-             * just ignore them rather than trying to create empty
-             * ROM blobs, because the zero-length blob can falsely
-             * trigger the overlapping-ROM-blobs check.
-             */
-            if (mem_size != 0) {
+            if (mem_size == 0) {
+                /* Some ELF files really do have segments of zero size;
+                 * just ignore them rather than trying to create empty
+                 * ROM blobs, because the zero-length blob can falsely
+                 * trigger the overlapping-ROM-blobs check.
+                 */
+                g_free(data);
+            } else {
                 if (load_rom) {
                     g_autofree char *label =
                         g_strdup_printf("%s ELF program header segment %d",
                                         name, i);
 
-                    /*
-                     * rom_add_elf_program() takes its own reference to
-                     * 'mapped_file'.
-                     */
-                    rom_add_elf_program(label, mapped_file, data, file_size,
-                                        mem_size, addr, as);
+                    /* rom_add_elf_program() seize the ownership of 'data' */
+                    rom_add_elf_program(label, NULL, data, file_size, mem_size,
+                                        addr, as);
                 } else {
                     MemTxResult res;
 
@@ -584,16 +569,14 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
             struct elf_note *nhdr = NULL;
 
             file_size = ph->p_filesz; /* Size of the range of ELF notes */
-            data_offset = ph->p_offset; /* Offset where the notes are located */
-
-            if (file_size > 0) {
-                if (g_mapped_file_get_length(mapped_file) <
-                    file_size + data_offset) {
+            data = g_malloc0(file_size);
+            if (ph->p_filesz > 0) {
+                if (lseek(fd, ph->p_offset, SEEK_SET) < 0) {
+                    goto fail;
+                }
+                if (read(fd, data, file_size) != file_size) {
                     goto fail;
                 }
-
-                data = (uint8_t *)g_mapped_file_get_contents(mapped_file);
-                data += data_offset;
             }
 
             /*
@@ -607,6 +590,7 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
             if (nhdr != NULL) {
                 elf_note_fn((void *)nhdr, (void *)&ph->p_align, SZ == 64);
             }
+            g_free(data);
             data = NULL;
         }
     }
@@ -619,9 +603,6 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
     }
     ret = total_size;
  fail:
-    if (mapped_file) {
-        g_mapped_file_unref(mapped_file);
-    }
     g_free(phdr);
     return ret;
 }
-- 
2.50.1 (Apple Git-155)