From patchwork Mon Oct 30 21:17:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 739291 Delivered-To: patch@linaro.org Received: by 2002:a5d:4c47:0:b0:32d:baff:b0ca with SMTP id n7csp1337835wrt; Mon, 30 Oct 2023 14:22:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGnCg7P1ysW47El+/Xv7NwSjLH6nnnCAHxabgY+Gg3uCM9pN3eeIArPGw4fmyJ1+fZm2az8 X-Received: by 2002:a05:620a:458a:b0:775:9f94:16f1 with SMTP id bp10-20020a05620a458a00b007759f9416f1mr1451195qkb.22.1698700948571; Mon, 30 Oct 2023 14:22:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698700948; cv=none; d=google.com; s=arc-20160816; b=uSULmxK1eI2F16JuUp2CS3eraaJcjCRQyvz45jEZUmrJcYv58co444gy5zXS94/a+M zrx//3FpedrfRG/zAEUF7T/bLe8ihsoihNH3w4UhZ72G1l2Nj5/jR2pF1xImnbYobA4a TwLMFOaJY59I2Ya2B7rdjy+Zz8pR2nHHPqFb+gMk/kt6eSLPG+o3hJKUmmD6Kfven+pv zwF+ZM42DsuJjHk7nayzc78rUZVR67BZbih4R9Rxf+G3bDQ0KizpLG/Wd07YEa+arWiV vGo3v/bL+jmHLYwvKbPuAqxEPeWpkDFkT3z+0bE58XdY33/Z/U873xM3r4pnGk49Vmsc MfVA== 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=7kHG9dpDbo2QunqRCqU6AhR+zRVLjJJWJlLUD5rS4Pk=; fh=kQadAhxV42GJ3cd2VUw1RyURv/zJQGvmUdF7iLEVTtc=; b=Xy/TbbvnxP0Bj7ozmXb02Nuu0VTJRipv17hWNDwGyUvf1hLiM+o8uYh2UquvZV8NXE 4KDOWgwHk96BQiF0x03+17zNI0fBjq7gGObJbpajD09t6OEHGfBwA7MmLqVoYVHuf9DF LO9X4nXW9rKYzI+matjTzQ6eoYtwwRO2UOB3n4SPvmvbWa6kEOWWfQUbFZKOJlFfbLsZ 76kKuXtwWtJqA/1D7LH5eCWIUbWgrmYqESdwsk5N10XnT7YlZ2+R/edBSyP1tMlrt/Ii 0A706GEpLlGU6avpjwnnG0jLBKxeLY/i01TJrg5pM2RTUqZDEFcVt8CtG1KQYpD+FNOS OX0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zeRhZnKV; 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 s10-20020a05620a29ca00b007758f5499b0si6617388qkp.426.2023.10.30.14.22.28 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 30 Oct 2023 14:22:28 -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=zeRhZnKV; 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 1qxZeL-0001My-1W; Mon, 30 Oct 2023 17:18:17 -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 1qxZdj-0000z3-Mf for qemu-devel@nongnu.org; Mon, 30 Oct 2023 17:17:43 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qxZdf-0006U8-Hu for qemu-devel@nongnu.org; Mon, 30 Oct 2023 17:17:39 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1cc3388621cso15939795ad.1 for ; Mon, 30 Oct 2023 14:17:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1698700654; x=1699305454; 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=7kHG9dpDbo2QunqRCqU6AhR+zRVLjJJWJlLUD5rS4Pk=; b=zeRhZnKV4nCNE2MbksCCyh/aRl7RSeCZEKzZK0ON0O3ILEqiIhIDF/mRIDfmIBpJHk NdNP1h85RFA67IcYxGQOEYO7Oe2CfhnFstnnPJKCTlq8Oz3vvhijN06Ou9G2Hbv5Va6c MM04RM0M/3vbJaNV+y4BlqURsAUQAd2nHzAeNHXD1FfFJ9r9ZZE9lK3q/jPYKnbN9ywB eMEpQ54V1tRY4UVI/JLC7MIrWCrTi+W4LUgNL5E66K6jkiWKLWT8khyldwAOiY87l04n xcBRdc3lV4R8NRnO0GxSu4N30BQu8t+N37TRCvoF/vJHPsYRvXL3l/7BvYJWKlrvg/j4 jXyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698700654; x=1699305454; 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=7kHG9dpDbo2QunqRCqU6AhR+zRVLjJJWJlLUD5rS4Pk=; b=tFzyAa0VBzmSWIPhJFSiERHxh2/9HDBc4K+TqBc2DGKP53WEZHIYGGdqOXF/ElMGYk RXARROlBDByzICX3uIak7iQ1uSy/ssB4N4cJLiY0D0LFqLmZ1p4oFioonLNM/2KLkQLg hFmjJ3jorxbrly6kqREGX3n3DBOlqkjv6vldNTk7lNhrOXdpfUcHEoY5olJCIPR8+3qS RtzYXyy4mV0BcIPuGkCSRVLT7IxW8k7LhbEbFqLBzPu83ctAcpyOUfml3+y+Sq9aWbxU Z3MIFtQ68VQAT5f2fUggu9RounNbKJQ2M8rAX6+XRqeiYG0elamrVHOyQfL9SN2PNUt7 fqEw== X-Gm-Message-State: AOJu0Yx0M/Jwx23eFuyVlPmLyAM58eehx0b9rNJcDWchh90hdrOi4si/ /npV50U4D20/QgxFzBTK6F8E9iOggX+arUd5axY= X-Received: by 2002:a17:902:e74a:b0:1bc:1e17:6d70 with SMTP id p10-20020a170902e74a00b001bc1e176d70mr931741plf.24.1698700654172; Mon, 30 Oct 2023 14:17:34 -0700 (PDT) Received: from stoup.. ([71.212.149.95]) by smtp.gmail.com with ESMTPSA id jf2-20020a170903268200b001cc32261bdfsm4670350plb.38.2023.10.30.14.17.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 14:17:33 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PULL 06/21] linux-user: Use ImageSource in load_symbols Date: Mon, 30 Oct 2023 14:17:12 -0700 Message-Id: <20231030211727.165090-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231030211727.165090-1-richard.henderson@linaro.org> References: <20231030211727.165090-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::633; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x633.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. Reviewed-by: Philippe Mathieu-Daudé 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 d763cb855a..88c2b01402 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2201,7 +2201,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. */ @@ -3473,7 +3474,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); @@ -3564,19 +3565,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; } @@ -3594,31 +3596,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. */ @@ -3643,10 +3647,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; @@ -3655,20 +3661,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); }