Commit 971851b046 for qemu.org

commit 971851b0463806bae208ecae1ff200012d528b30
Author: Nicholas Piggin <npiggin@gmail.com>
Date:   Wed Apr 15 16:48:35 2026 +1000

    hw/riscv/boot: Warn if a ELF format file is loaded as a binary

    It is possible that an ELF file can not be loaded, in that
    case the loader falls back to loading the file as a binary
    blob. Print a warning in this case because it is likely that
    it is not intended.

    Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
    Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
    Signed-off-by: Joel Stanley <joel@jms.id.au>
    Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
    Message-ID: <20260415064838.652297-4-joel@jms.id.au>
    Signed-off-by: Alistair Francis <alistair.francis@wdc.com>

diff --git a/hw/riscv/boot.c b/hw/riscv/boot.c
index e5490beda0..9086793b7a 100644
--- a/hw/riscv/boot.c
+++ b/hw/riscv/boot.c
@@ -163,13 +163,27 @@ hwaddr riscv_load_firmware(const char *firmware_filename,

     g_assert(firmware_filename != NULL);

-    if (load_elf_ram_sym(firmware_filename, NULL, NULL, NULL,
-                         &firmware_entry, NULL, &firmware_end, NULL,
-                         0, EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
+    firmware_size = load_elf_ram_sym(firmware_filename, NULL, NULL, NULL,
+                                     &firmware_entry, NULL, &firmware_end,
+                                     NULL, 0, EM_RISCV, 1, 0, NULL, false,
+                                     sym_cb);
+    if (firmware_size > 0) {
         *firmware_load_addr = firmware_entry;
         return firmware_end;
     }

+    if (firmware_size != ELF_LOAD_NOT_ELF) {
+        /*
+         * If the user specified an ELF format firmware that could not be
+         * loaded as an ELF, it's possible that loading it as a binary is
+         * not what was intended.
+         */
+        warn_report("could not load ELF format firmware '%s' (%s). "
+                    "Attempting to load as binary.",
+                    firmware_filename,
+                    load_elf_strerror(firmware_size));
+    }
+
     firmware_size = load_image_targphys_as(firmware_filename,
                                            *firmware_load_addr,
                                            current_machine->ram_size, NULL,
@@ -179,7 +193,8 @@ hwaddr riscv_load_firmware(const char *firmware_filename,
         return *firmware_load_addr + firmware_size;
     }

-    error_report("could not load firmware '%s'", firmware_filename);
+    error_report("could not load firmware '%s': %s", firmware_filename,
+                 load_elf_strerror(firmware_size));
     exit(1);
 }