[PATCH 4/5] Revert "hw/elf_ops: Ignore loadable segments with zero size"

Alex Bennée posted 5 patches 9 months, 4 weeks ago
Maintainers: "Philippe Mathieu-Daudé" <philmd@linaro.org>, Chris Wulff <crwulff@gmail.com>, Marek Vasut <marex@denx.de>, Max Filippov <jcmvbkbc@gmail.com>, Paolo Bonzini <pbonzini@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Thomas Huth <thuth@redhat.com>, Laurent Vivier <laurent@vivier.eu>, Aurelien Jarno <aurelien@aurel32.net>, Jiaxun Yang <jiaxun.yang@flygoat.com>, Aleksandar Rikalo <aleksandar.rikalo@syrmia.com>, Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Bin Meng <bin.meng@windriver.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <dbarboza@ventanamicro.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Wainer dos Santos Moschetta <wainersm@redhat.com>, Beraldo Leal <bleal@redhat.com>
[PATCH 4/5] Revert "hw/elf_ops: Ignore loadable segments with zero size"
Posted by Alex Bennée 9 months, 4 weeks ago
This regressed qemu-system-xtensa:

    TEST    test_load_store on xtensa
  qemu-system-xtensa: Some ROM regions are overlapping
  These ROM regions might have been loaded by direct user request or by default.
  They could be BIOS/firmware images, a guest kernel, initrd or some other file loaded into guest memory.
  Check whether you intended to load all this guest code, and whether it has been built to load to the correct addresses.

  The following two regions overlap (in the memory address space):
    test_load_store ELF program header segment 1 (addresses 0x0000000000001000 - 0x0000000000001f26)
    test_load_store ELF program header segment 2 (addresses 0x0000000000001ab8 - 0x0000000000001ab8)
  make[1]: *** [Makefile:187: run-test_load_store] Error 1

This reverts commit 62570f1434160d356311e1c217537e24a4ac85cd.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 include/hw/elf_ops.h | 75 +++++++++++++++++++++-----------------------
 1 file changed, 36 insertions(+), 39 deletions(-)

diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
index 3e966ddd5a1..9c35d1b9da6 100644
--- a/include/hw/elf_ops.h
+++ b/include/hw/elf_ops.h
@@ -427,16 +427,6 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
             file_size = ph->p_filesz; /* Size of the allocated data */
             data_offset = ph->p_offset; /* Offset where the data is located */
 
-            /*
-             * Some ELF files really do have segments of zero size;
-             * just ignore them rather than trying to set the wrong addr,
-             * or create empty ROM blobs, because the zero-length blob can
-             * falsely trigger the overlapping-ROM-blobs check.
-             */
-            if (mem_size == 0) {
-                continue;
-            }
-
             if (file_size > 0) {
                 if (g_mapped_file_get_length(mapped_file) <
                     file_size + data_offset) {
@@ -540,38 +530,45 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
                 *pentry = ehdr.e_entry - ph->p_vaddr + ph->p_paddr;
             }
 
-            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);
-            } else {
-                MemTxResult res;
-
-                res = address_space_write(as ? as : &address_space_memory,
-                                          addr, MEMTXATTRS_UNSPECIFIED,
-                                          data, file_size);
-                if (res != MEMTX_OK) {
-                    goto fail;
-                }
-                /*
-                 * We need to zero'ify the space that is not copied
-                 * from file
-                 */
-                if (file_size < mem_size) {
-                    res = address_space_set(as ? as : &address_space_memory,
-                                            addr + file_size, 0,
-                                            mem_size - file_size,
-                                            MEMTXATTRS_UNSPECIFIED);
+            /* 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 (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);
+                } else {
+                    MemTxResult res;
+
+                    res = address_space_write(as ? as : &address_space_memory,
+                                              addr, MEMTXATTRS_UNSPECIFIED,
+                                              data, file_size);
                     if (res != MEMTX_OK) {
                         goto fail;
                     }
+                    /*
+                     * We need to zero'ify the space that is not copied
+                     * from file
+                     */
+                    if (file_size < mem_size) {
+                        res = address_space_set(as ? as : &address_space_memory,
+                                                addr + file_size, 0,
+                                                mem_size - file_size,
+                                                MEMTXATTRS_UNSPECIFIED);
+                        if (res != MEMTX_OK) {
+                            goto fail;
+                        }
+                    }
                 }
             }
 
-- 
2.39.2


Re: [PATCH 4/5] Revert "hw/elf_ops: Ignore loadable segments with zero size"
Posted by Peter Maydell 9 months, 4 weeks ago
On Thu, 1 Feb 2024 at 12:30, Alex Bennée <alex.bennee@linaro.org> wrote:
>
> This regressed qemu-system-xtensa:
>
>     TEST    test_load_store on xtensa
>   qemu-system-xtensa: Some ROM regions are overlapping
>   These ROM regions might have been loaded by direct user request or by default.
>   They could be BIOS/firmware images, a guest kernel, initrd or some other file loaded into guest memory.
>   Check whether you intended to load all this guest code, and whether it has been built to load to the correct addresses.
>
>   The following two regions overlap (in the memory address space):
>     test_load_store ELF program header segment 1 (addresses 0x0000000000001000 - 0x0000000000001f26)
>     test_load_store ELF program header segment 2 (addresses 0x0000000000001ab8 - 0x0000000000001ab8)

Hmm -- this second segment is zero length, so why did we create
a ROM blob for it? The commit being reverted here looks like it
ought to be expanding the set of things for which we say
"zero size, ignore entirely"...

Anyway, revert given we have a regression is the first thing
to do if there's not an immediately obvious fix.

-- PMM
Re: [PATCH 4/5] Revert "hw/elf_ops: Ignore loadable segments with zero size"
Posted by Thomas Huth 9 months, 4 weeks ago
On 01/02/2024 13.28, Alex Bennée wrote:
> This regressed qemu-system-xtensa:
> 
>      TEST    test_load_store on xtensa
>    qemu-system-xtensa: Some ROM regions are overlapping
>    These ROM regions might have been loaded by direct user request or by default.
>    They could be BIOS/firmware images, a guest kernel, initrd or some other file loaded into guest memory.
>    Check whether you intended to load all this guest code, and whether it has been built to load to the correct addresses.
> 
>    The following two regions overlap (in the memory address space):
>      test_load_store ELF program header segment 1 (addresses 0x0000000000001000 - 0x0000000000001f26)
>      test_load_store ELF program header segment 2 (addresses 0x0000000000001ab8 - 0x0000000000001ab8)
>    make[1]: *** [Makefile:187: run-test_load_store] Error 1
> 
> This reverts commit 62570f1434160d356311e1c217537e24a4ac85cd.
> 
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>   include/hw/elf_ops.h | 75 +++++++++++++++++++++-----------------------
>   1 file changed, 36 insertions(+), 39 deletions(-)

Reviewed-by: Thomas Huth <thuth@redhat.com>