From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: alex.bennee@linaro.org, laurent@vivier.eu,
"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: [PATCH v5 06/20] linux-user: Use ImageSource in load_symbols
Date: Tue, 29 Aug 2023 15:02:14 -0700 [thread overview]
Message-ID: <20230829220228.928506-7-richard.henderson@linaro.org> (raw)
In-Reply-To: <20230829220228.928506-1-richard.henderson@linaro.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é <philmd@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
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 833c22da25..f34fb64c0c 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2049,7 +2049,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. */
@@ -3294,7 +3295,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);
@@ -3385,19 +3386,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;
}
@@ -3415,31 +3417,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. */
@@ -3464,10 +3468,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;
@@ -3476,20 +3482,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);
}
--
2.34.1
next prev parent reply other threads:[~2023-08-29 23:26 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-29 22:02 [PATCH v5 00/20] linux-user: Implement VDSOs Richard Henderson
2023-08-29 22:02 ` [PATCH v5 01/20] tests/docker: Update docker-loongarch-cross toolchain Richard Henderson
2023-08-30 9:43 ` gaosong
2023-09-04 14:02 ` Alex Bennée
2023-09-04 23:08 ` Richard Henderson
2023-09-05 9:23 ` Alex Bennée
2023-08-29 22:02 ` [PATCH v5 02/20] linux-user: Introduce imgsrc_read, imgsrc_read_alloc Richard Henderson
2023-09-04 14:27 ` Alex Bennée
2023-08-29 22:02 ` [PATCH v5 03/20] linux-user: Tidy loader_exec Richard Henderson
2023-09-04 14:54 ` Alex Bennée
2023-08-29 22:02 ` [PATCH v5 04/20] linux-user: Do not clobber bprm_buf swapping ehdr Richard Henderson
2023-09-04 14:58 ` Alex Bennée
2023-08-29 22:02 ` [PATCH v5 05/20] linux-user: Use ImageSource in load_elf_image Richard Henderson
2023-08-29 22:02 ` Richard Henderson [this message]
2023-08-29 22:02 ` [PATCH v5 07/20] linux-user: Replace bprm->fd with bprm->src.fd Richard Henderson
2023-08-29 22:02 ` [PATCH v5 08/20] linux-user: Load vdso image if available Richard Henderson
2023-08-30 14:22 ` Philippe Mathieu-Daudé
2023-08-30 16:17 ` Richard Henderson
2023-08-30 20:56 ` Philippe Mathieu-Daudé
2023-08-30 22:08 ` Richard Henderson
2023-08-29 22:02 ` [PATCH v5 09/20] linux-user: Add gen-vdso tool Richard Henderson
2023-08-29 22:02 ` [PATCH v5 10/20] linux-user: Add build-vdso.sh tool Richard Henderson
2023-08-29 22:02 ` [PATCH v5 11/20] linux-user/i386: Add vdso Richard Henderson
2023-08-29 22:02 ` [PATCH v5 12/20] linux-user/x86_64: " Richard Henderson
2023-08-29 22:02 ` [PATCH v5 13/20] linux-user/aarch64: " Richard Henderson
2023-08-29 22:02 ` [PATCH v5 14/20] linux-user/arm: " Richard Henderson
2023-08-29 22:02 ` [PATCH v5 15/20] linux-user/hppa: " Richard Henderson
2023-08-29 22:02 ` [PATCH v5 16/20] linux-user/riscv: " Richard Henderson
2023-09-19 4:47 ` Alistair Francis
2023-08-29 22:02 ` [PATCH v5 17/20] linux-user/loongarch64: " Richard Henderson
2023-08-29 22:02 ` [PATCH v5 18/20] linux-user/ppc: " Richard Henderson
2023-08-29 22:02 ` [PATCH v5 19/20] linux-user/s390x: Rename __SIGNAL_FRAMESIZE to STACK_FRAME_OVERHEAD Richard Henderson
2023-08-30 14:14 ` Philippe Mathieu-Daudé
2023-08-29 22:02 ` [PATCH v5 20/20] linux-user/s390x: Add vdso Richard Henderson
2023-09-04 15:00 ` Alex Bennée
2023-09-06 17:56 ` Richard Henderson
2023-09-07 6:17 ` Philippe Mathieu-Daudé
2023-09-07 9:20 ` Alex Bennée
2023-08-30 14:52 ` [PATCH v5 00/20] linux-user: Implement VDSOs Helge Deller
2023-08-30 16:20 ` Richard Henderson
2023-08-31 7:57 ` Alex Bennée
2023-09-01 14:21 ` Helge Deller
2023-09-04 12:51 ` Alex Bennée
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230829220228.928506-7-richard.henderson@linaro.org \
--to=richard.henderson@linaro.org \
--cc=alex.bennee@linaro.org \
--cc=laurent@vivier.eu \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).