From patchwork Tue Jul 6 23:49:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 470574 Delivered-To: patch@linaro.org Received: by 2002:a17:906:971a:0:0:0:0 with SMTP id k26csp5546291ejx; Tue, 6 Jul 2021 16:53:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyxjUSC2jmjVJifJsakuKZbkX2ubFQ56DqU+tBPE9GU7FA4P/Ce/mkysdSJJ3Ex0MqU0+9D X-Received: by 2002:a92:a005:: with SMTP id e5mr15854792ili.22.1625615602834; Tue, 06 Jul 2021 16:53:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625615602; cv=none; d=google.com; s=arc-20160816; b=rDWXyiQnMg3Bkk97TWyeCyb7AMRQP9UFVhVZ02cus6dyYKBxxv8L+Dzvxlqq1yW40p c0mkSgNRexRmkXbnfn0+C/DL/ZmORFyiOaLMioxS9x/Rm7r9iCvegp4fHre1h5rd85Vm TQVx2wf4XcefLjwxX6oPo9r+1jGdUfZl6lWFiP+BgzqW6rxI5SzK/bbMUClJHiVrwZTd 0B3OUUAKWT4qbirshKo9kJVqPdFxU2ZI9G/UODR+jcsGMYSp/q4GGB6tErleKhZZOi7a Bb5/FwQlEHjy6xhoDjShlGVfzRl0wOIN1fvaxum8ctx7M/BytqfGmkMCEt1KLxQ7HCWD ytuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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:to:from :dkim-signature; bh=9RcoFmxGQXgPRY+jmXRZEPO1LtZWOQX+pDz9H/4119E=; b=rWDY6sB4fMQcRqk3T8bC1t0Duh3SwnGgtFo6v7EmmdT8CizYM3WUBoWEH/FiR42Hlq Zr0EuZUBNcYiUEBbF9jc0YBwYRShM1OV/FsJnWRiaTi5R/v0H/g2sOWcQl+h/A6cQeyA x3GZV6z1gpUR/dXyy6m+IQjW0yuIW8zaamtnWPmhW/HkFIj94Ld6PbEBsZessZVhriFf EOe8u65XaB8Y4xy2vPINS4lOnsTGyCjRv5XYS6B3b0do8kwR67NEDXmgwAi3Gr055W8X OHKh9lcGjFTE1n1+n2qULsdEH7tIWsRP1aojn8yxlYOqk+kTLzN4CPGrDpKKMK6ExART 1/vQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=R7sZRA70; 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=fail (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 f3si18340678ilq.115.2021.07.06.16.53.22 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 06 Jul 2021 16:53:22 -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=fail header.i=@linaro.org header.s=google header.b=R7sZRA70; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:44472 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m0usU-0006Vi-4I for patch@linaro.org; Tue, 06 Jul 2021 19:53:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52926) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m0uov-0000or-TW for qemu-devel@nongnu.org; Tue, 06 Jul 2021 19:49:41 -0400 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]:38782) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m0uot-0006bf-AN for qemu-devel@nongnu.org; Tue, 06 Jul 2021 19:49:41 -0400 Received: by mail-pg1-x52c.google.com with SMTP id h4so407826pgp.5 for ; Tue, 06 Jul 2021 16:49:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9RcoFmxGQXgPRY+jmXRZEPO1LtZWOQX+pDz9H/4119E=; b=R7sZRA70v7H1x5y9EUN8f/2nDivfeG6gN+hH8Jj0y7JHd0hgMKyTUX7SIHS96aIqmI 5umG1S84pOBlYh4ofSX6lGZy9VSh/JiY/K5b81qpSfw73o7nwlM8hhYwdEEFjQuerOYK Zp45NEqds390dVkQ04Q6gvrHcz9HbSjE581Wivi1EJFTf1RckAO7FECgRkpUdu+kZJVh gpFCKZQkPUU5J9Zjb0aQc4EnapRTp4KO6Mp+Rg5+PEvIfNJ1EXNRMFN8Sa6au0Gxpj4T N7Fxb/xhgn8oyj/7dkGshdZgGZ6nhWCGBTfvGP/jzPwdxGKTc28sG9448ogXJJGYBHXn Yzfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9RcoFmxGQXgPRY+jmXRZEPO1LtZWOQX+pDz9H/4119E=; b=V3aoI2c+qXOMf5B9plLN8limPfmPGDmn5hc+O0MoZOKNmoM+Gk6XFS5mgEd/B/DA/U kTktWfeaTTNnjkaXEiDhKq4IG/y6QBhF+FIbePQfq6IkVMfc7Gxcekq6nFTHquTfIZG8 l94bnYl14b4An+hHYfwVXDfFUSUOIcSULtQvfMeKHNelbfdFLVFSPdjJ4nLAUu8sAWFo 5zF/WQ8DQqN2FwbZ6UOtXgQh0r05UC555gEwfdmzOZvE/RmJbcYWnyOatI8bmQz4aqm1 5UuUZWOX8dzjgHiSLTSVJthZ1FOs+xLFwH9goAOgldNkMjNo/JKlmuJPEorQACW8/Ehk QOkg== X-Gm-Message-State: AOAM533hGYLbXgigENiBTuf6QqGuE9U4BUkqiYHzLBcI7PN1w5cn7CY9 3isNT1NMZReJv3g7iJhGqP5NO3dgBefHWQ== X-Received: by 2002:a63:303:: with SMTP id 3mr22609238pgd.111.1625615377979; Tue, 06 Jul 2021 16:49:37 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id b3sm18058114pfi.179.2021.07.06.16.49.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jul 2021 16:49:37 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 07/36] linux-user: Use ImageSource in load_symbols Date: Tue, 6 Jul 2021 16:49:03 -0700 Message-Id: <20210706234932.356913-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210706234932.356913-1-richard.henderson@linaro.org> References: <20210706234932.356913-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52c; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52c.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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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(-) -- 2.25.1 diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 9113bf82f1..490b374d13 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1734,7 +1734,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. */ @@ -2901,7 +2902,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); } mmap_unlock(); @@ -2989,19 +2990,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; } @@ -3019,31 +3021,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. */ @@ -3068,10 +3072,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; @@ -3080,20 +3086,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); }