qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: mark.cave-ayland@ilande.co.uk
Subject: [PATCH RISU v2 01/13] risu: Allow use of ELF test files
Date: Sun, 26 May 2024 12:36:25 -0700	[thread overview]
Message-ID: <20240526193637.459064-2-richard.henderson@linaro.org> (raw)
In-Reply-To: <20240526193637.459064-1-richard.henderson@linaro.org>

By using elf files, we make it easier to disassemble
the test file, to match comparison failures to code.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 risu.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 52 insertions(+), 5 deletions(-)

diff --git a/risu.c b/risu.c
index c28b4a5..c9c3a38 100644
--- a/risu.c
+++ b/risu.c
@@ -24,6 +24,15 @@
 #include <sys/mman.h>
 #include <fcntl.h>
 #include <string.h>
+#include <elf.h>
+
+/* TODO: Improve configure. */
+#ifdef __linux__
+#include <endian.h>
+#define HOST_BIG_ENDIAN  (__BYTE_ORDER == __BIG_ENDIAN)
+#else
+#error Need HOST_BIG_ENDIAN
+#endif
 
 #include "config.h"
 #include "risu.h"
@@ -346,8 +355,11 @@ static void load_image(const char *imgfile)
 {
     /* Load image file into memory as executable */
     struct stat st;
+    void *addr;
+    int fd;
+
     fprintf(stderr, "loading test image %s...\n", imgfile);
-    int fd = open(imgfile, O_RDONLY);
+    fd = open(imgfile, O_RDONLY);
     if (fd < 0) {
         fprintf(stderr, "failed to open image file %s\n", imgfile);
         exit(EXIT_FAILURE);
@@ -356,20 +368,55 @@ static void load_image(const char *imgfile)
         perror("fstat");
         exit(EXIT_FAILURE);
     }
-    size_t len = st.st_size;
-    void *addr;
 
     /* Map writable because we include the memory area for store
      * testing in the image.
      */
-    addr = mmap(0, len, PROT_READ | PROT_WRITE | PROT_EXEC,
+    addr = mmap(0, st.st_size, PROT_READ | PROT_WRITE | PROT_EXEC,
                 MAP_PRIVATE, fd, 0);
     if (addr == MAP_FAILED) {
         perror("mmap");
         exit(EXIT_FAILURE);
     }
     close(fd);
-    image_start = addr;
+
+    if (memcmp(addr, ELFMAG, SELFMAG) == 0) {
+        union {
+            unsigned char ident[EI_NIDENT];
+            Elf32_Ehdr h32;
+            Elf64_Ehdr h64;
+        } *e = addr;
+        size_t entry;
+
+        /*
+         * TODO: More complete parsing of ELF file, verify assumtion, namely:
+         * Single PT_LOAD covering the ELF header, code and data.
+         * For now, simply update image_start from the elf header.
+         */
+        if (e->ident[EI_DATA] !=
+            (HOST_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB)) {
+            fprintf(stderr, "%s: Endian mismatch: EI_DATA = %x\n",
+                    imgfile, e->ident[EI_DATA]);
+            exit(1);
+        }
+
+        switch (e->ident[EI_CLASS]) {
+        case ELFCLASS32:
+            entry = e->h32.e_entry;
+            break;
+        case ELFCLASS64:
+            entry = e->h64.e_entry;
+            break;
+        default:
+            fprintf(stderr, "%s: Bad elf header: EI_CLASS = %x\n",
+                    imgfile, e->ident[EI_CLASS]);
+            exit(1);
+        }
+        image_start = addr + entry;
+    } else {
+        /* Raw binary image. */
+        image_start = addr;
+    }
     image_start_address = (uintptr_t) addr;
 }
 
-- 
2.34.1



  reply	other threads:[~2024-05-26 19:37 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-26 19:36 [PATCH RISU v2 00/13] ELF and Sparc64 support Richard Henderson
2024-05-26 19:36 ` Richard Henderson [this message]
2024-05-30 12:45   ` [PATCH RISU v2 01/13] risu: Allow use of ELF test files Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 02/13] Build elf test cases instead of raw binaries Richard Henderson
2024-05-30 12:45   ` Peter Maydell
2024-05-30 13:40   ` Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 03/13] Introduce host_context_t Richard Henderson
2024-05-30 12:46   ` Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 04/13] risu: Add initial sparc64 support Richard Henderson
2024-05-30 13:02   ` Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 05/13] risugen: Be explicit about print destinations Richard Henderson
2024-05-30 12:51   ` Peter Maydell
2024-05-30 17:37     ` Richard Henderson
2024-05-31  9:53       ` Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 06/13] risugen: Add sparc64 support Richard Henderson
2024-05-30 13:23   ` Peter Maydell
2024-05-30 17:44     ` Richard Henderson
2024-05-26 19:36 ` [PATCH RISU v2 07/13] contrib/generate_all: Do not rely on ag Richard Henderson
2024-05-30 13:13   ` Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 08/13] sparc64: Add a few logical insns Richard Henderson
2024-05-30 13:24   ` Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 09/13] sparc64: Add VIS1 instructions Richard Henderson
2024-05-30 13:25   ` Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 10/13] sparc64: Add VIS2 and FMAF insns Richard Henderson
2024-05-30 13:25   ` Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 11/13] sparc64: Add VIS3 instructions Richard Henderson
2024-05-30 13:26   ` Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 12/13] sparc64: Add IMA instructions Richard Henderson
2024-05-30 13:25   ` Peter Maydell
2024-05-26 19:36 ` [PATCH RISU v2 13/13] sparc64: Add VIS4 instructions Richard Henderson
2024-05-30 13:25   ` Peter Maydell
2024-05-28 20:43 ` [PATCH RISU v2 00/13] ELF and Sparc64 support Mark Cave-Ayland

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240526193637.459064-2-richard.henderson@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=mark.cave-ayland@ilande.co.uk \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).