From patchwork Sat Sep 30 02:15:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 728070 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp740382wrt; Fri, 29 Sep 2023 19:16:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFiQTnhPTzUjhLjh92NVCgyrTDNYKXsop68GRebCXknbotVpnGC2hV0HC1oc4shKcL8R9QV X-Received: by 2002:a05:622a:40a:b0:403:a9d3:91e3 with SMTP id n10-20020a05622a040a00b00403a9d391e3mr6161561qtx.32.1696040184190; Fri, 29 Sep 2023 19:16:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696040184; cv=none; d=google.com; s=arc-20160816; b=nkB8J+QEvmqaD1jWRHNH40J6tOIV+S/hZtQ3pjxjZIwbRmamw6U63Q6zOcgDq3UNoh JKwKcilgWU5zjVmKofY4AoORteMujkO22CZZlsuRzSBUSLre9Osxq+rKiAJfkiu2jp/J LeTbg65maYhcKY6PKA3DKTD8zVOl/3DhP4BMFhP0yI5ACAsDq/SSOO13Hrbco90P1aDm 7RU9qsWy7lt+T7US84i1Blf0gnnYBlj5l0WMfin2nMiHQFA55COIhsBWAvj3zTVnPhOU C055z/Y1H0bwZ4MCdSrco6fMKTwKuZP6cDEb6Jleho+s+a7XEDHvFFcl2ehsGbCMXPOb D5Mg== 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=kZLkhD4Ob1/Y3qD3OMY7JWNNP8AXZZHIk5RVf2/dQ8w=; fh=cP0QZFg6MuyBR24J0KjrrDlt4joDhDmvpCU8OseFE7U=; b=nWdW/xcc5W0bAIe8oqFaQx6bhJfs3bcmaM7s1j3p2lKztNOST8odXtXEgi6RTKf1BR SYLsn4t6CPZzcmLW9tREVwQUV3L0kHWeT81rZiety4pHRrZd4XFzK6jjtXxqMu9Vi8AK MMxhkndd9JUuLzLTxOtr6acyEHpxmQ0jXfSFYQzysGoGl9K/rFXtbR6/p9YOrjg6W82C uElav4xMW4tvI6Xv63S3DkUhY3upV55gJFexzKYUioG6kuO2bNz0Ob10InJUn8gb6vGr BULj4NJfIvWYHDWIBoaN1RvFBxopNBsD+3iDSrlCoqyJ/mbwF1YE7nyBgtglChD75Mzw CNPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LizXQ2pO; 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 p15-20020a05622a00cf00b0041983479612si631369qtw.628.2023.09.29.19.16.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 29 Sep 2023 19:16:24 -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=LizXQ2pO; 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 1qmPWJ-0000FF-9r; Fri, 29 Sep 2023 22:15:52 -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 1qmPWC-0000CL-Cr for qemu-devel@nongnu.org; Fri, 29 Sep 2023 22:15:44 -0400 Received: from mail-oo1-xc2a.google.com ([2607:f8b0:4864:20::c2a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qmPW5-0001z5-B0 for qemu-devel@nongnu.org; Fri, 29 Sep 2023 22:15:43 -0400 Received: by mail-oo1-xc2a.google.com with SMTP id 006d021491bc7-57bab4e9e1aso6009778eaf.3 for ; Fri, 29 Sep 2023 19:15:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696040136; x=1696644936; 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=kZLkhD4Ob1/Y3qD3OMY7JWNNP8AXZZHIk5RVf2/dQ8w=; b=LizXQ2pOyXyTELlSIabQu/67fUknh/s9UWuGi/kVg0+swFpH1UFQ0/4YhKeenMZ8jC AaGoKrUTQFO9vFc+oRkIxbA3vyu9d6JAhh1xIEPEmjr/pwaK9h3AZwdt0l+DoMTJiFI2 BTJaSvXxpd834ajIDgyhCd/bQJ/XZpAT0DdWIqlFy/gCM7yVNot/zczQUsHKARzcw0lo i56WtUaB6WWukTrRVsL18nYVWZeIEFX3GG00hnw6Rz+VV7DG+MAL9Wvw7kRn/WEmSE3I 1va0uYNyn15WXNZ4XJW7OQj8u5E7hnjRTxTpIZziCkwOzA6Nyr+FieU5xhQ9YRHFHBKw waSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696040136; x=1696644936; 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=kZLkhD4Ob1/Y3qD3OMY7JWNNP8AXZZHIk5RVf2/dQ8w=; b=E3+bHyfCT0rmTA2ZYGTdowlsDu2eCZBNchtgwlJ/rVfSIoKHTIkS0mJcaWP2EkO/ay a6n8QqAixP7t30i7L8MP5xxrz+6n4RNhgFI1dGElio/j5toidzZuJUo1BUmTEkIiEPV9 rzvgTeXgeqCZC5vyF8j6h1aRkHcLOULwzB5JTJGGWQt45Vd2cX/qfeTq4BmCTtAKjfw1 QdVHOqAdXIusCGO/eRg10QGONoqClkz2gAAEFztEXH8lMEiWjV3zOM4Cfmgi/F8Q+RLO Fjv9MUaZr8Yi6wR0V39yVTywfeY5E+52a2ysDiqcGbhmHwXtkTwxSGECWXuuREA5Xf2R 8umQ== X-Gm-Message-State: AOJu0YwnQEpYmOA0YlO2d9HsKl8WFOPR36Z+uAAzMn56e/wjQkbuF/Pj 7EfXviIdKCJ77cKsQF4KsT3qcRxjd9bBDGR9s3w= X-Received: by 2002:a05:6358:7e07:b0:132:d333:4a5c with SMTP id o7-20020a0563587e0700b00132d3334a5cmr6982929rwm.10.1696040136016; Fri, 29 Sep 2023 19:15:36 -0700 (PDT) Received: from stoup.. ([71.212.149.95]) by smtp.gmail.com with ESMTPSA id fv21-20020a17090b0e9500b002775281b9easm2130692pjb.50.2023.09.29.19.15.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 19:15:35 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org, laurent@vivier.eu, =?utf-8?q?Philippe_Mathieu-Da?= =?utf-8?q?ud=C3=A9?= Subject: [PATCH v6 05/19] linux-user: Use ImageSource in load_symbols Date: Fri, 29 Sep 2023 19:15:15 -0700 Message-Id: <20230930021529.987950-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230930021529.987950-1-richard.henderson@linaro.org> References: <20230930021529.987950-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::c2a; envelope-from=richard.henderson@linaro.org; helo=mail-oo1-xc2a.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 d6f1b8d2b1..ed276edb3c 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2193,7 +2193,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. */ @@ -3438,7 +3439,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); @@ -3529,19 +3530,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; } @@ -3559,31 +3561,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. */ @@ -3608,10 +3612,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; @@ -3620,20 +3626,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); }