From nobody Mon Nov 25 09:20:09 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=1716349511; cv=none; d=zohomail.com; s=zohoarc; b=HqkZ+zmnKrsmIp3F0kt5ZfJ8eYywqAroXCN7QCov39+PA0Uyj9KNcilMMNZBN21L/swghLoBwFBHylSXJBSSFMN7XIm2qQqv+1q/9oBCnScWy4EtybJlf7xT3uAEYLGU0iq2v0OH/nIVPj7STMyk0wfwtexzYD4JmVD79Qi8WEI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716349511; 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=DxnVevkjDDR7Ml3cZIbgFH0f/FylZWZsDaOsDtl+L1x050zySlcl50cX6rB838QwV2z412HAcOBVibUCIPOxWNKQ4RW5rnKotjy1FT0vWBuleVgQOjtX4uLYFH3v2HPy1jWn/xIWL4efCxBNcszjuNN6ZXf+8bOW4IKkV4zl7B4= 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 1716349511240925.8410049250568; Tue, 21 May 2024 20:45:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s9cuI-0003RE-BZ; Tue, 21 May 2024 23:44:50 -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 1s9cuH-0003R1-Da for qemu-devel@nongnu.org; Tue, 21 May 2024 23:44:49 -0400 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s9cuE-00049z-Or for qemu-devel@nongnu.org; Tue, 21 May 2024 23:44:49 -0400 Received: by mail-ot1-x336.google.com with SMTP id 46e09a7af769-6f1242f1dcdso2651313a34.3 for ; Tue, 21 May 2024 20:44:45 -0700 (PDT) Received: from stoup.. (174-21-72-5.tukw.qwest.net. [174.21.72.5]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2af2bafsm21416617b3a.162.2024.05.21.20.44.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 20:44:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1716349484; x=1716954284; 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=EaseiIvlNAgFLx9w/lZSWwTK7QD1NDoEm43To1yrYMKdOf4Pm0heydnuY8FABHz3XZ PnR+o7PlSX1F/XFdPhMnAHoSeQpu21xeStYmVWrR4XOHvJ0+wrqE963S3S3k7bnLUTny ppd5GAGw613gQ/AvG9g5x4fzKH3OqffIvovgKGmRWhUU1UekBBG9UtlUZYVON+zqQNRl rUfR+NsDVmOpEBd8c0uu66+HzwJa1ulrV4fllxvoPxb0NHlnkL813fKvKm59XSd4v53n fvNXfy2k9qqiJ3gvjgkz+UTgbcwhALF/8BzgDZvvBVSZBziagO1YeJpYA500mo+IcjF2 1hjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716349484; x=1716954284; 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=uYR+VFMCGFAWmxCV0ZMHC6ZzMp/lpkutKxATGc3oxZK27wwp684HaRbs6qv0MQYvjY Cb6mZLgkTdgBhnAgY3gJSdL3ot2R3lHkVDmbURz74i/HkCe/8Ks48/Kdt2dHMZaaUf3d G25Af8yhc+HrJS/QzYq6A55FDNzcaUIQMQ0GCvtCJ4l5iILh0oJnXG5oyO6ugUzGKmiR +zxBfDq4HoMFQU8KWm2ipQBQ5WyDnp1UEGTHbD4Rqb+g8LMwS0dPw+NwwY5hI9Nyystu Vj8EdnSvDDQXTPrxOKdOHCVT+wkSI26FWvPw9kbiuwmTIOAerL5tlsplyvoFJrmv0kA5 XOIg== X-Gm-Message-State: AOJu0YxHDYAinTDS27Q0DZft1V2Fh90u5J0LxTBibMFaFdS/DtXxiOX+ P1fShcOIq4e+aEERSfZW3cvRhjdBFPB7CYpHBQUEQoXAGwRlj/kHFp9l4Q0p8AnfSALU/D86l8U t X-Google-Smtp-Source: AGHT+IEvfh3LfDGQx1RVE09ovmRHUf4g5MePJitgJP2E6RyGEPEY80LLbo4/XosJdxTRzicwiTWgFA== X-Received: by 2002:a05:6870:59d:b0:221:1c2f:23ee with SMTP id 586e51a60fabf-24c68b5de72mr976548fac.22.1716349484414; Tue, 21 May 2024 20:44:44 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, mark.cave-ayland@ilande.co.uk Subject: [PATCH RISU v2 1/8] risu: Allow use of ELF test files Date: Tue, 21 May 2024 20:44:35 -0700 Message-Id: <20240522034442.140293-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240522034442.140293-1-richard.henderson@linaro.org> References: <20240522034442.140293-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::336; envelope-from=richard.henderson@linaro.org; helo=mail-ot1-x336.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 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: 1716349512059100001 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 --- 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