From patchwork Fri Aug 11 16:50:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 712608 Delivered-To: patch@linaro.org Received: by 2002:adf:a390:0:b0:317:ecd7:513f with SMTP id l16csp1482360wrb; Fri, 11 Aug 2023 09:52:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHmUTN6hCevM5ZvJalyK+4604zjnE9Ql8qp4rt9KpYAwGrngGoQz+n0EDS71Qsi72GIL8d2 X-Received: by 2002:a0c:b356:0:b0:635:e0dd:db4b with SMTP id a22-20020a0cb356000000b00635e0dddb4bmr2224353qvf.37.1691772761511; Fri, 11 Aug 2023 09:52:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691772761; cv=none; d=google.com; s=arc-20160816; b=BH0ZpqBxoTTXj/ln/LIWeYu0z5NurWYocEkdQRUj9tv6ZOo1x6oPShx785NFypDW6J 7+ELX6VWMnUsfSJGIglu4anROyvsVm2UsSaKCMNAvDvwekWh6lBaaISxgmBNIaxZIIHV dp8hytoHck1cobg9kyBAIfiDW4+37Riuz0k1vHxPFe59b2OAeVNVL+xr3CS6o+l3kiV2 hULOrbtKZbNmQQIbjQfOOJnkzcGR4a93pVI8LV/riQ0DRQZ5bE7bELZOy/uhdtTRhncU w9pVJzAzFjiolxELEr8N3dj+XbWt+UhhD+RX8YNGnNMsvlKLE9gswuEE61Ou1COYt7/9 O3jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ePVxotW82jMDrWJ+ESk2twixLo2Priek8MjmEOc4z70=; fh=1OYPYObfZgrrVkY6LqJEmJVCCPQmahTALyAweBWp8LI=; b=VunAR70VeQtfU2yIbdm5d/ikzKI7gl2yPgBVkC1FV3wV0j4kt1/y8skza5K7VyTb2m +jB3PqviEc0vHbfiadcH1U/Cu0dJ3+eTAhx3aM1IsitZukRveZKAfKtcmCwcuGuOVHeU Cig8UQOSlgCKNMzCByODUBPQTxdUiNwQ7VxmRspBrMVcpXt9xUYN0hLfoCIgjPB4ehu1 kGu1XNJl51sMxuOlr/VxHcOK4bE4bcqTFKcyuUuHYGryWYOg8V7meY94kbLh3vnQWEom hkS7g+wkMnOQRjqL7oh4FrZNMSeO/nOhxoF92leurRHryn3aWJSuU/UqPnFszJrcDnXW EhNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="S/5bRopb"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id y19-20020a0cd993000000b0063cf4ca0934si2405852qvj.612.2023.08.11.09.52.41 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 11 Aug 2023 09:52:41 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="S/5bRopb"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qUVLt-0005lA-JB; Fri, 11 Aug 2023 12:51:05 -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 1qUVLq-0005jc-2z for qemu-devel@nongnu.org; Fri, 11 Aug 2023 12:51:02 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qUVLn-0001M7-SS for qemu-devel@nongnu.org; Fri, 11 Aug 2023 12:51:01 -0400 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-26b1adbd655so803821a91.1 for ; Fri, 11 Aug 2023 09:50:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691772658; x=1692377458; 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=ePVxotW82jMDrWJ+ESk2twixLo2Priek8MjmEOc4z70=; b=S/5bRopbca9klFqAItm/gifsMChV/8vThrgzVyOEaRiOqCHzAN0tkEkPghqOb0XKwQ wX+G/GPV3mssho23nIN3UYP4cpIddOiJGiJp3UJd1mdZECWwSzXIv87+CebmSSuIs81O wn2H10JSvqYmHdJmSbUmFY9qMr137O+JLoG/3C+uSTKt6hTYoQI63cxI9coxufO7Up34 MQWoN4INO/gRBeq/AARHxUPDyvMzo5+u9+sIF8Nhvm33Vy62VXXXcu5UMhrUsrTiivDv A42fr6rHNfJKmsVhD3RjZlWCDwCMS2krSlg6clcu3dSiQ1eZURVfaC9rITH9nZ7wq/Cc Zw0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691772658; x=1692377458; 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=ePVxotW82jMDrWJ+ESk2twixLo2Priek8MjmEOc4z70=; b=DmZYRbPpnQb8M6ltCqszV8zAHICB1edjmDhRn+IoA+uhTOezRQI/F6RQU1Nm/OAJw/ MAkAdBkkh9w6c41tl537+JY1TJXbX+4z+RL32H1LclmMwJaJxWuJxi+lHns3ANk9xJJq YTprzR5xDqxhzSj7sTjEIRMTUbxaGb9HAotsSsb4Uv5Idbgu9SE6mvo/2mdOVem5Z5YD yMMzoL2gIP27f3zL8W/HJlLV2IhoYkJRUMLMDtAOPX3i2lwE8AVJbqHYPnJlnB7N/O7A YRBFlZkIglCtDjmQyHyDFdgkYfBetpt38V5miISjX5IdPJ6B1upcLFROa0gGa957JB/6 Rm+g== X-Gm-Message-State: AOJu0YyA0pTqvVIrLlpuitj2Iq50+HvUV2bSIyhYwvVRaSHoMMpMmCiv RVkopj/mFXo2ZtQg/DGCLTdqNNSW2Gmn8DbpoZQ= X-Received: by 2002:a17:90b:3503:b0:268:105b:ca1c with SMTP id ls3-20020a17090b350300b00268105bca1cmr1750761pjb.32.1691772658441; Fri, 11 Aug 2023 09:50:58 -0700 (PDT) Received: from stoup.. ([2602:47:d483:7301:e773:351d:2db2:8a8a]) by smtp.gmail.com with ESMTPSA id 22-20020a17090a19d600b00263d3448141sm3803713pjj.8.2023.08.11.09.50.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 09:50:58 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org, pbonzini@redhat.com, philmd@linaro.org Subject: [PATCH v3 05/15] linux-user: Use ImageSource in load_symbols Date: Fri, 11 Aug 2023 09:50:42 -0700 Message-Id: <20230811165052.161080-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230811165052.161080-1-richard.henderson@linaro.org> References: <20230811165052.161080-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102e.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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Aside from the section headers, we're unlikely to hit the ImageSource cache on guest executables. But the interface for imgsrc_read_* is better. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 87 ++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index f3511ae766..19d3cac039 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2048,7 +2048,8 @@ static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { } #ifdef USE_ELF_CORE_DUMP static int elf_core_dump(int, const CPUArchState *); #endif /* USE_ELF_CORE_DUMP */ -static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias); +static void load_symbols(struct elfhdr *hdr, const ImageSource *src, + abi_ulong load_bias); /* Verify the portions of EHDR within E_IDENT for the target. This can be performed before bswapping the entire header. */ @@ -3293,7 +3294,7 @@ static void load_elf_image(const char *image_name, const ImageSource *src, } if (qemu_log_enabled()) { - load_symbols(ehdr, src->fd, load_bias); + load_symbols(ehdr, src, load_bias); } debuginfo_report_elf(image_name, src->fd, load_bias); @@ -3384,19 +3385,20 @@ static int symcmp(const void *s0, const void *s1) } /* Best attempt to load symbols from this ELF object. */ -static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias) +static void load_symbols(struct elfhdr *hdr, const ImageSource *src, + abi_ulong load_bias) { int i, shnum, nsyms, sym_idx = 0, str_idx = 0; - uint64_t segsz; - struct elf_shdr *shdr; + g_autofree struct elf_shdr *shdr = NULL; char *strings = NULL; - struct syminfo *s = NULL; - struct elf_sym *new_syms, *syms = NULL; + struct elf_sym *syms = NULL; + struct elf_sym *new_syms; + uint64_t segsz; shnum = hdr->e_shnum; - i = shnum * sizeof(struct elf_shdr); - shdr = (struct elf_shdr *)alloca(i); - if (pread(fd, shdr, i, hdr->e_shoff) != i) { + shdr = imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shdr), + src, NULL); + if (shdr == NULL) { return; } @@ -3414,31 +3416,33 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias) found: /* Now know where the strtab and symtab are. Snarf them. */ - s = g_try_new(struct syminfo, 1); - if (!s) { - goto give_up; - } segsz = shdr[str_idx].sh_size; - s->disas_strtab = strings = g_try_malloc(segsz); - if (!strings || - pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) { + strings = g_try_malloc(segsz); + if (!strings) { + goto give_up; + } + if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) { goto give_up; } segsz = shdr[sym_idx].sh_size; - syms = g_try_malloc(segsz); - if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) { - goto give_up; - } - if (segsz / sizeof(struct elf_sym) > INT_MAX) { - /* Implausibly large symbol table: give up rather than ploughing - * on with the number of symbols calculation overflowing + /* + * Implausibly large symbol table: give up rather than ploughing + * on with the number of symbols calculation overflowing. */ goto give_up; } nsyms = segsz / sizeof(struct elf_sym); + syms = g_try_malloc(segsz); + if (!syms) { + goto give_up; + } + if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) { + goto give_up; + } + for (i = 0; i < nsyms; ) { bswap_sym(syms + i); /* Throw away entries which we do not need. */ @@ -3463,10 +3467,12 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias) goto give_up; } - /* Attempt to free the storage associated with the local symbols - that we threw away. Whether or not this has any effect on the - memory allocation depends on the malloc implementation and how - many symbols we managed to discard. */ + /* + * Attempt to free the storage associated with the local symbols + * that we threw away. Whether or not this has any effect on the + * memory allocation depends on the malloc implementation and how + * many symbols we managed to discard. + */ new_syms = g_try_renew(struct elf_sym, syms, nsyms); if (new_syms == NULL) { goto give_up; @@ -3475,20 +3481,23 @@ static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias) qsort(syms, nsyms, sizeof(*syms), symcmp); - s->disas_num_syms = nsyms; -#if ELF_CLASS == ELFCLASS32 - s->disas_symtab.elf32 = syms; -#else - s->disas_symtab.elf64 = syms; -#endif - s->lookup_symbol = lookup_symbolxx; - s->next = syminfos; - syminfos = s; + { + struct syminfo *s = g_new(struct syminfo, 1); + s->disas_strtab = strings; + s->disas_num_syms = nsyms; +#if ELF_CLASS == ELFCLASS32 + s->disas_symtab.elf32 = syms; +#else + s->disas_symtab.elf64 = syms; +#endif + s->lookup_symbol = lookup_symbolxx; + s->next = syminfos; + syminfos = s; + } return; -give_up: - g_free(s); + give_up: g_free(strings); g_free(syms); }