From nobody Tue Feb 10 02:44:07 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519999147605288.1508204516101; Fri, 2 Mar 2018 05:59:07 -0800 (PST) Received: from localhost ([::1]:35144 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erlD7-0003LB-MM for importer@patchew.org; Fri, 02 Mar 2018 08:58:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58516) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1erl8A-0007UY-QL for qemu-devel@nongnu.org; Fri, 02 Mar 2018 08:53:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1erl87-0002Ag-Mh for qemu-devel@nongnu.org; Fri, 02 Mar 2018 08:53:50 -0500 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:43971) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1erl87-0002AX-EA for qemu-devel@nongnu.org; Fri, 02 Mar 2018 08:53:47 -0500 Received: by mail-pg0-x244.google.com with SMTP id e9so3797925pgs.10 for ; Fri, 02 Mar 2018 05:53:47 -0800 (PST) Received: from localhost.localdomain (125-237-39-90.jetstream.xtra.co.nz. [125.237.39.90]) by smtp.gmail.com with ESMTPSA id g16sm14046966pfd.23.2018.03.02.05.53.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 02 Mar 2018 05:53:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=MeWDUVFWiHoHBwOutv1ggFssELclfAXvv41jMs6bhps=; b=fh4bkxN4h7B042JP38nL/YIr6VR4bxxcejZE0txS+chULi8LMyzFsZRXtcwJMzRVJG 2vJIpPb9R4voZO2fYiRncHUYDltW8GTOHGuI51aKG7jj5GzJ8pVm4jGdUd2w3vJ5JhOQ YfSlFaQGcpEbzHYHUFxbECG8KoMCpiJTANZh4WKZKk1FMTvYJnJ9MAROz88HpWRUMs87 3EkoM0WglXEPp1K2ZZURuVtpZhZ6ft08yFBEwLzYBdwaaltELlsggTPb+AhV4T2Hve9Z lVmwPhAfXChSWyUhgsEmJPokxT1JXOSD0lJns91mS+YOnHw57+Apbl+MAocJ8xWrwzkw WOgw== 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; bh=MeWDUVFWiHoHBwOutv1ggFssELclfAXvv41jMs6bhps=; b=C6R7uGvomDhk0p4wuUvzp5dNWATF3Y8KiXIOekGBE8sDrf5MR9lHS97qH7zsIl599x 36uXHwxBGG78bRKgyndaVCWfCJJwMABZ2atcnQJKXLGjHisglRKzeQQaLZQ1Wqdbcmvf QHhY7mH/yEYyGulM49BwZxJoLmKWsKfpIHZiHsoRIxdfcA+NC3aQZL4yYuD5yTQl4Y6y QLBgEas8ALc5f5Tx4MuegCHnA/7fINxrm40FEKHsp7IFZhxguQVPEX0Qw6siFxdg+8FE TK4194L5AuJouGf7w+ypAzixPX8NID6HiXuCKslwFEZrOa+52feNcKSph1x0hXDeDCOf dRaA== X-Gm-Message-State: APf1xPBJXV3Rpok+4yz+FXaSI0ws9UekoxlmYgCNIgdSoFSa1WQSD9v+ JDo3LdagsOdPuua7XAn6jGv2/AFKrtI= X-Google-Smtp-Source: AG47ELtak5FKAx6uIPMg6RTCjk2GfnlPTI/Xe4l5oSV9sY6kfIjX/DL+pWu+K2HN4qYWy3iV5A9pKg== X-Received: by 10.101.64.10 with SMTP id f10mr4468013pgp.171.1519998826173; Fri, 02 Mar 2018 05:53:46 -0800 (PST) From: Michael Clark To: qemu-devel@nongnu.org Date: Sat, 3 Mar 2018 02:51:39 +1300 Message-Id: <1519998711-73430-12-git-send-email-mjc@sifive.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1519998711-73430-1-git-send-email-mjc@sifive.com> References: <1519998711-73430-1-git-send-email-mjc@sifive.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::244 Subject: [Qemu-devel] [PATCH v8 11/23] Add symbol table callback interface to load_elf X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Bastian Koppelmann , Michael Clark , Palmer Dabbelt , Sagar Karandikar , RISC-V Patches Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The RISC-V HTIF (Host Target Interface) console device requires access to the symbol table to locate the 'tohost' and 'fromhost' symbols. Reviewed-by: Richard Henderson Signed-off-by: Michael Clark Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/core/loader.c | 18 ++++++++++++++++-- include/hw/elf_ops.h | 34 +++++++++++++++++++++------------- include/hw/loader.h | 17 ++++++++++++++++- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/hw/core/loader.c b/hw/core/loader.c index c08f130..1d26c6e 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -450,6 +450,20 @@ int load_elf_ram(const char *filename, int clear_lsb, int data_swab, AddressSpace *as, bool load_rom) { + return load_elf_ram_sym(filename, translate_fn, translate_opaque, + pentry, lowaddr, highaddr, big_endian, + elf_machine, clear_lsb, data_swab, as, + load_rom, NULL); +} + +/* return < 0 if error, otherwise the number of bytes loaded in memory */ +int load_elf_ram_sym(const char *filename, + uint64_t (*translate_fn)(void *, uint64_t), + void *translate_opaque, uint64_t *pentry, + uint64_t *lowaddr, uint64_t *highaddr, int big_endian, + int elf_machine, int clear_lsb, int data_swab, + AddressSpace *as, bool load_rom, symbol_fn_t sym_cb) +{ int fd, data_order, target_data_order, must_swab, ret =3D ELF_LOAD_FAI= LED; uint8_t e_ident[EI_NIDENT]; =20 @@ -488,11 +502,11 @@ int load_elf_ram(const char *filename, if (e_ident[EI_CLASS] =3D=3D ELFCLASS64) { ret =3D load_elf64(filename, fd, translate_fn, translate_opaque, m= ust_swab, pentry, lowaddr, highaddr, elf_machine, clear_lsb, - data_swab, as, load_rom); + data_swab, as, load_rom, sym_cb); } else { ret =3D load_elf32(filename, fd, translate_fn, translate_opaque, m= ust_swab, pentry, lowaddr, highaddr, elf_machine, clear_lsb, - data_swab, as, load_rom); + data_swab, as, load_rom, sym_cb); } =20 fail: diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h index d192e7e..b6e19e3 100644 --- a/include/hw/elf_ops.h +++ b/include/hw/elf_ops.h @@ -105,7 +105,7 @@ static int glue(symcmp, SZ)(const void *s0, const void = *s1) } =20 static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_sw= ab, - int clear_lsb) + int clear_lsb, symbol_fn_t sym_cb) { struct elf_shdr *symtab, *strtab, *shdr_table =3D NULL; struct elf_sym *syms =3D NULL; @@ -133,10 +133,26 @@ static int glue(load_symbols, SZ)(struct elfhdr *ehdr= , int fd, int must_swab, =20 nsyms =3D symtab->sh_size / sizeof(struct elf_sym); =20 + /* String table */ + if (symtab->sh_link >=3D ehdr->e_shnum) { + goto fail; + } + strtab =3D &shdr_table[symtab->sh_link]; + + str =3D load_at(fd, strtab->sh_offset, strtab->sh_size); + if (!str) { + goto fail; + } + i =3D 0; while (i < nsyms) { - if (must_swab) + if (must_swab) { glue(bswap_sym, SZ)(&syms[i]); + } + if (sym_cb) { + sym_cb(str + syms[i].st_name, syms[i].st_info, + syms[i].st_value, syms[i].st_size); + } /* We are only interested in function symbols. Throw everything else away. */ if (syms[i].st_shndx =3D=3D SHN_UNDEF || @@ -163,15 +179,6 @@ static int glue(load_symbols, SZ)(struct elfhdr *ehdr,= int fd, int must_swab, } } =20 - /* String table */ - if (symtab->sh_link >=3D ehdr->e_shnum) - goto fail; - strtab =3D &shdr_table[symtab->sh_link]; - - str =3D load_at(fd, strtab->sh_offset, strtab->sh_size); - if (!str) - goto fail; - /* Commit */ s =3D g_malloc0(sizeof(*s)); s->lookup_symbol =3D glue(lookup_symbol, SZ); @@ -264,7 +271,8 @@ static int glue(load_elf, SZ)(const char *name, int fd, int must_swab, uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr, int elf_machine, int clear_lsb, int data_swa= b, - AddressSpace *as, bool load_rom) + AddressSpace *as, bool load_rom, + symbol_fn_t sym_cb) { struct elfhdr ehdr; struct elf_phdr *phdr =3D NULL, *ph; @@ -329,7 +337,7 @@ static int glue(load_elf, SZ)(const char *name, int fd, if (pentry) *pentry =3D (uint64_t)(elf_sword)ehdr.e_entry; =20 - glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb); + glue(load_symbols, SZ)(&ehdr, fd, must_swab, clear_lsb, sym_cb); =20 size =3D ehdr.e_phnum * sizeof(phdr[0]); if (lseek(fd, ehdr.e_phoff, SEEK_SET) !=3D ehdr.e_phoff) { diff --git a/include/hw/loader.h b/include/hw/loader.h index 5edbe02..7b05e8b 100644 --- a/include/hw/loader.h +++ b/include/hw/loader.h @@ -64,7 +64,7 @@ int load_image_gzipped(const char *filename, hwaddr addr,= uint64_t max_sz); #define ELF_LOAD_WRONG_ENDIAN -4 const char *load_elf_strerror(int error); =20 -/** load_elf_ram: +/** load_elf_ram_sym: * @filename: Path of ELF file * @translate_fn: optional function to translate load addresses * @translate_opaque: opaque data passed to @translate_fn @@ -81,6 +81,7 @@ const char *load_elf_strerror(int error); * @as: The AddressSpace to load the ELF to. The value of address_space_me= mory * is used if nothing is supplied here. * @load_rom : Load ELF binary as ROM + * @sym_cb: Callback function for symbol table entries * * Load an ELF file's contents to the emulated system's address space. * Clients may optionally specify a callback to perform address @@ -93,6 +94,20 @@ const char *load_elf_strerror(int error); * If @elf_machine is EM_NONE then the machine type will be read from the * ELF header and no checks will be carried out against the machine type. */ +typedef void (*symbol_fn_t)(const char *st_name, int st_info, + uint64_t st_value, uint64_t st_size); + +int load_elf_ram_sym(const char *filename, + uint64_t (*translate_fn)(void *, uint64_t), + void *translate_opaque, uint64_t *pentry, + uint64_t *lowaddr, uint64_t *highaddr, int big_endian, + int elf_machine, int clear_lsb, int data_swab, + AddressSpace *as, bool load_rom, symbol_fn_t sym_cb); + +/** load_elf_ram: + * Same as load_elf_ram_sym(), but doesn't allow the caller to specify a + * symbol callback function + */ int load_elf_ram(const char *filename, uint64_t (*translate_fn)(void *, uint64_t), void *translate_opaque, uint64_t *pentry, uint64_t *lowad= dr, --=20 2.7.0