From nobody Mon Nov 25 09:30:49 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1716752249; cv=none; d=zohomail.com; s=zohoarc; b=EVO7DSlYd5vHwgNw2bnntZRNvCfuL9sLe+ZN8MbK4gMgCwlORFZ47qYGXB9YfWk5OU3RulX30hLk03xJh4tVcza2YAreqXcDb6NMdntYpZJK9mpz51ovlXkPW0VPVzdQs7Hlyu+BrAf3WCmbTX8Psx2meSzpx8bLk6dltDmxVdA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716752249; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ClmjZOy3QQNiKciKtrEnKHv+DzIwPzBDEJYBBNDBr+E=; b=TTdi3By93/BTUVowixmawhtKuA9M+g98CXZATeV2CkGVweu5Jj4zK0mcCsA0wyIPjh0ZYr058dWnOpbANmo2HKJUPTIQlOLSZ0b0XUPnlhmxoLpnZ2AtPpxmUtGhN65pmjaPWO5hXugmI5z4c7kXda5KE/3aVz76SeT3pbU06Mo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716752249932850.2722011970249; Sun, 26 May 2024 12:37:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sBJfs-00080i-Sy; Sun, 26 May 2024 15:36:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBJfo-0007ys-5u for qemu-devel@nongnu.org; Sun, 26 May 2024 15:36:52 -0400 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sBJfd-0006TW-P8 for qemu-devel@nongnu.org; Sun, 26 May 2024 15:36:44 -0400 Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-2bfdae0b0e0so39388a91.0 for ; Sun, 26 May 2024 12:36:41 -0700 (PDT) Received: from stoup.. (174-21-72-5.tukw.qwest.net. [174.21.72.5]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2bf5f6155b6sm4943761a91.29.2024.05.26.12.36.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 12:36:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1716752200; x=1717357000; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ClmjZOy3QQNiKciKtrEnKHv+DzIwPzBDEJYBBNDBr+E=; b=IJbOoxe/czAzduaMEWaFs0lGyKWq0RAo4+sJ0O8gSd3d/KRSdZibjSYWxeIMnIis5D +S28EyL7mIjEZy8Rt2/uNcn95ixyvcN0g8argrxXQrL7OBUNBCmvVi4RrOMFzb4gVnS6 3q2Gsx5PKW9xdufMx4OwAyXlSUE2WHBCa8UaWCx2E01ZeMLLRu9LHQJkrwYYSWkUT7TJ gF9nq2GGH5feqkkzHjLNt8i4MuHlRZi7Jcfiik7V2GjeUedk7GG9oJMVXptiQLfnzau8 MkPgy6erLjSihADwZvXP4qFYHocg05pr7p9i8PgZ0rGE7t9IMD+tgLeh2LnuDFWJS/jx dh2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716752200; x=1717357000; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ClmjZOy3QQNiKciKtrEnKHv+DzIwPzBDEJYBBNDBr+E=; b=SI7vfR+1APst4SLldCw7ybYhBvGsaU4CNVOxvO6BIWa9yw4RnCQZCmhu3siedXUuZA 79ppURjr8gjLEkNf0KAs8via3U1nMawabmkyYtkLVIcDRoxl9Cz8G+eE8mZyKgglQNSm d3iDplLfsAWB9mm0Sb6Ms3jfCdZW0Rcj3h0l52bLYVAI2m43t5435FUn8Ee0Lg3TEww0 /6ftzUFEM7Y3f4vUdZdirJ6LPR8sTA0lDPPa8Esw9EmHKj+6JaloidOSLTzsc2UHt87K 1Wp7FJW1a0s6FRxw3fQDfBTSHoyZXZn4VeEC+hekdjQdTTC9KIkUx2mw98ipxhHFLxTn uNug== X-Gm-Message-State: AOJu0Yxlb8O1YqTomLNW2xwqeHWQLKRDEY1a/EM1fdFyXRfNIxWpnoKS W37AKK5rOKHKmDlLKlmIFVLe89+tesF/IVYSz4DI8VF1syXQiGXGzRTBvRSejTPnW6trNexQL7E D X-Google-Smtp-Source: AGHT+IFieMRRiC/FrMuy+Am3GtBHjQKaPDgDDUiALMlnBJ+hysStfFhvRBv5cxES7iGCTxPwYC6jWw== X-Received: by 2002:a17:90a:296:b0:2bd:d2f9:fa18 with SMTP id 98e67ed59e1d1-2bf5f85ddcdmr6502183a91.48.1716752199933; Sun, 26 May 2024 12:36:39 -0700 (PDT) From: Richard Henderson 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 Message-Id: <20240526193637.459064-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240526193637.459064-1-richard.henderson@linaro.org> References: <20240526193637.459064-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102a; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1716752251774100002 Content-Type: text/plain; charset="utf-8" By using elf files, we make it easier to disassemble the test file, to match comparison failures to code. Signed-off-by: Richard Henderson Reviewed-by: Peter Maydell --- 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 #include #include +#include + +/* TODO: Improve configure. */ +#ifdef __linux__ +#include +#define HOST_BIG_ENDIAN (__BYTE_ORDER =3D=3D __BIG_ENDIAN) +#else +#error Need HOST_BIG_ENDIAN +#endif =20 #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 =3D open(imgfile, O_RDONLY); + fd =3D 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 =3D st.st_size; - void *addr; =20 /* Map writable because we include the memory area for store * testing in the image. */ - addr =3D mmap(0, len, PROT_READ | PROT_WRITE | PROT_EXEC, + addr =3D mmap(0, st.st_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0); if (addr =3D=3D MAP_FAILED) { perror("mmap"); exit(EXIT_FAILURE); } close(fd); - image_start =3D addr; + + if (memcmp(addr, ELFMAG, SELFMAG) =3D=3D 0) { + union { + unsigned char ident[EI_NIDENT]; + Elf32_Ehdr h32; + Elf64_Ehdr h64; + } *e =3D addr; + size_t entry; + + /* + * TODO: More complete parsing of ELF file, verify assumtion, name= ly: + * 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] !=3D + (HOST_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB)) { + fprintf(stderr, "%s: Endian mismatch: EI_DATA =3D %x\n", + imgfile, e->ident[EI_DATA]); + exit(1); + } + + switch (e->ident[EI_CLASS]) { + case ELFCLASS32: + entry =3D e->h32.e_entry; + break; + case ELFCLASS64: + entry =3D e->h64.e_entry; + break; + default: + fprintf(stderr, "%s: Bad elf header: EI_CLASS =3D %x\n", + imgfile, e->ident[EI_CLASS]); + exit(1); + } + image_start =3D addr + entry; + } else { + /* Raw binary image. */ + image_start =3D addr; + } image_start_address =3D (uintptr_t) addr; } =20 --=20 2.34.1