From nobody Fri Nov 7 14:34:58 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.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 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1548094856724516.4295976799689; Mon, 21 Jan 2019 10:20:56 -0800 (PST) Received: from localhost ([127.0.0.1]:57533 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gleBm-0007D0-K8 for importer@patchew.org; Mon, 21 Jan 2019 13:20:50 -0500 Received: from eggs.gnu.org ([209.51.188.92]:51799) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gldyC-0004Rv-Eg for qemu-devel@nongnu.org; Mon, 21 Jan 2019 13:06:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gldyB-0004GA-Kl for qemu-devel@nongnu.org; Mon, 21 Jan 2019 13:06:48 -0500 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:33178) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gldyB-0004FJ-Do for qemu-devel@nongnu.org; Mon, 21 Jan 2019 13:06:47 -0500 Received: by mail-wr1-x444.google.com with SMTP id c14so24530438wrr.0 for ; Mon, 21 Jan 2019 10:06:47 -0800 (PST) Received: from 640k.lan ([93.56.166.5]) by smtp.gmail.com with ESMTPSA id 133sm56197732wme.9.2019.01.21.10.06.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Jan 2019 10:06:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=mGgA8mhxRDL22XvQhvbdFvsJ+SEiVcBnIl4weZyYzLs=; b=sO+qKTQI6fzmkEiHgBXSl4fgdRmJy1WIjSTzrBNDsN3ztn+3laO0VSE8bleTUH9hqE Nwi+hYb3hXuE0ToVLy0cty5fljKsG8UsUbHBa7ho1u6HTG9L63gutov+BBbQykEPMNDN 28783NObVFNKsN3tieIw7hMC1u3htusLIz02McRPT+9ihXNhJVWZLerkatb3PruKgOpk jNIRHduGaNg5bV03Wj7olZPbLWQFJo4YQdIm6palFWIEycs25gZI0xmagruGVw7pl6T5 xapJi/EhAQIGg+1naWMDxuBDcfdtsH0pob5o+og80DBBEhFStEGbx+4Z8Q4v4u7XTslo d4Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=mGgA8mhxRDL22XvQhvbdFvsJ+SEiVcBnIl4weZyYzLs=; b=CDFRXoG5G2arhnm8xLccYCgh1OLr5//7oEgVZW2ga7YE9ypA3/dol20xGt3gOhghF9 HV4GHhq8W9ib41DatVwXmUApMJqqYtDYM34tUfDitgI/LRe842G90BpRXkMmP6715piY GN+nLn5eop9EJqEnwn089BhlGoITvIh+Li1BwDdCRev0f9e8hVbqqf5H0O7Oithkb8We UOxUMnmRMRKnoCIq2+IVZHCOBLriiRSoYJ/rDo+1eW3h3AGTVj3ZhCvoMtY7ao5UxTvP RU+OTbJ3Se5ehU2SNgPu6Lsv7T98OHI9Nteo8Db3xFHEYNScSrn61q+a32LpK8NdZM6r l6Cw== X-Gm-Message-State: AJcUukdyQGs9MffRziOM9jP7SwXEpaYflooh8WWPx7qdKu0aSoylCNt3 wRI1Ui0+fZC6SWVAB785w5lIGi7F X-Google-Smtp-Source: ALg8bN6Hl3H6wmmJzH21qTq4F02aPdTiAGuyLPyahtB7wH2ad3mDV1pWU1tVtmq61zV5C6EcSE6fgg== X-Received: by 2002:adf:bc02:: with SMTP id s2mr28312436wrg.255.1548094006178; Mon, 21 Jan 2019 10:06:46 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 21 Jan 2019 19:05:55 +0100 Message-Id: <1548093980-43088-25-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1548093980-43088-1-git-send-email-pbonzini@redhat.com> References: <1548093980-43088-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::444 Subject: [Qemu-devel] [PULL 24/49] elf-ops.h: Add get_elf_note_type() 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Liam Merwick Introduce a routine which, given a pointer to a range of ELF Notes, searches through them looking for a note matching the type specified and returns a pointer to the matching ELF note. get_elf_note_type() is used by elf_load[32|64]() to find the specified note type required by the 'elf_note_fn' parameter added in the previous commit. Signed-off-by: Liam Merwick Signed-off-by: Paolo Bonzini Signed-off-by: Liam Merwick --- include/hw/elf_ops.h | 75 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 75 insertions(+) diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h index 3438d6f..690f923 100644 --- a/include/hw/elf_ops.h +++ b/include/hw/elf_ops.h @@ -265,6 +265,51 @@ fail: return ret; } =20 +/* + * Given 'nhdr', a pointer to a range of ELF Notes, search through them + * for a note matching type 'elf_note_type' and return a pointer to + * the matching ELF note. + */ +static struct elf_note *glue(get_elf_note_type, SZ)(struct elf_note *nhdr, + elf_word note_size, + elf_word phdr_align, + elf_word elf_note_type) +{ + elf_word nhdr_size =3D sizeof(struct elf_note); + elf_word elf_note_entry_offset =3D 0; + elf_word note_type; + elf_word nhdr_namesz; + elf_word nhdr_descsz; + + if (nhdr =3D=3D NULL) { + return NULL; + } + + note_type =3D nhdr->n_type; + while (note_type !=3D elf_note_type) { + nhdr_namesz =3D nhdr->n_namesz; + nhdr_descsz =3D nhdr->n_descsz; + + elf_note_entry_offset =3D nhdr_size + + QEMU_ALIGN_UP(nhdr_namesz, phdr_align) + + QEMU_ALIGN_UP(nhdr_descsz, phdr_align); + + /* + * If the offset calculated in this iteration exceeds the + * supplied size, we are done and no matching note was found. + */ + if (elf_note_entry_offset > note_size) { + return NULL; + } + + /* skip to the next ELF Note entry */ + nhdr =3D (void *)nhdr + elf_note_entry_offset; + note_type =3D nhdr->n_type; + } + + return nhdr; +} + static int glue(load_elf, SZ)(const char *name, int fd, uint64_t (*elf_note_fn)(void *, void *, bool= ), uint64_t (*translate_fn)(void *, uint64_t), @@ -497,6 +542,36 @@ static int glue(load_elf, SZ)(const char *name, int fd, high =3D addr + mem_size; =20 data =3D NULL; + + } else if (ph->p_type =3D=3D PT_NOTE && elf_note_fn) { + struct elf_note *nhdr =3D NULL; + + file_size =3D ph->p_filesz; /* Size of the range of ELF notes = */ + data =3D g_malloc0(file_size); + if (ph->p_filesz > 0) { + if (lseek(fd, ph->p_offset, SEEK_SET) < 0) { + goto fail; + } + if (read(fd, data, file_size) !=3D file_size) { + goto fail; + } + } + + /* + * Search the ELF notes to find one with a type matching the + * value passed in via 'translate_opaque' + */ + nhdr =3D (struct elf_note *)data; + assert(translate_opaque !=3D NULL); + nhdr =3D glue(get_elf_note_type, SZ)(nhdr, file_size, ph->p_al= ign, + *(uint64_t *)translate_opaq= ue); + if (nhdr !=3D NULL) { + bool is64 =3D + sizeof(struct elf_note) =3D=3D sizeof(struct elf64_not= e); + elf_note_fn((void *)nhdr, (void *)&ph->p_align, is64); + } + g_free(data); + data =3D NULL; } } =20 --=20 1.8.3.1