From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D6F37C5321E for ; Mon, 26 Aug 2024 13:14:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=d3VzVdCX8vHAdGOmy/GRhVaeKZefcHdDx8ERim6TFk8=; b=GPxPDKNoM06ycQHJF+JDBoVcwR l3cbe9enCzQ+WziFLCPxwqyCntbw62DuOErtDCyROpl8LTtLch65h2aKlV1XLy5gP/aO/PK9ZGnuC HCWGLMlqBpij7tYhcBGbfJpiaiyJdNVBukL8XleknZK5v+3eLDC8moiCLh8wvhuvkLYYWu/PGyUqY EZkcVYvVn+1F8E1oJr7i38G60r3DiCYiZ2OMO4FmDjOHkNAbkOcyiCMQ6H5qbE4CG1Cs//WqrNH76 onavNUTVPyBIrMhthT3Xm3zTCv4NPjbDY4ZOk3SUYN66u1JYCd5x/0QwcfX13l/GlRA7MliG4VUDe Rq1iHHkQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1siZYP-00000007T0N-2AhP; Mon, 26 Aug 2024 13:14:41 +0000 Received: from out-173.mta1.migadu.com ([2001:41d0:203:375::ad]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1siZXb-00000007Sob-05yf for linux-arm-kernel@lists.infradead.org; Mon, 26 Aug 2024 13:13:52 +0000 Date: Mon, 26 Aug 2024 21:13:41 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1724678024; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=d3VzVdCX8vHAdGOmy/GRhVaeKZefcHdDx8ERim6TFk8=; b=naeHyF9Is5akLx8635moDlIkouTAkAQfBXFnFpGPdkOOYGzn9bImHB0R8acpKiHtxU88q8 KgT/ute4BXfk0Sc1FpSA8xhZw2rQOMPGgzl+7Mm5989+zhprn/vHtWoJn49JoCbSpxOyAT 5z10IH2NMqysLAABIEseG7wFca4LOFk= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Leo Yan To: Steve Clevenger Cc: james.clark@linaro.org, mike.leach@linaro.org, suzuki.poulose@arm.com, leo.yan@arm.com, ilkka@os.ampercomputing.com, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/5] Add dso__is_pie call to identify ELF PIE Message-ID: <20240826131341.GA4167@debian-dev> References: <323e931fe9f8f080eb0dfc2e29d112dd7edf1fb2.1724104248.git.scclevenger@os.amperecomputing.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <323e931fe9f8f080eb0dfc2e29d112dd7edf1fb2.1724104248.git.scclevenger@os.amperecomputing.com> X-Migadu-Flow: FLOW_OUT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240826_061351_525707_570B33DD X-CRM114-Status: GOOD ( 23.65 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Steve, On Tue, Aug 20, 2024 at 04:11:35PM -0600, Steve Clevenger wrote: > From: "steve.c.clevenger.ampere" > > Add dso__is_pie global to read the .dynamic section DT_FLAGS_1 entry for > the DF_1_PIE flag. This identifies position executable code. > > Signed-off-by: steve.c.clevenger.ampere > --- > tools/perf/util/symbol-elf.c | 55 ++++++++++++++++++++++++++++++++++++ > 1 file changed, 55 insertions(+) > > diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c > index e398abfd13a0..1d4bd222b314 100644 > --- a/tools/perf/util/symbol-elf.c > +++ b/tools/perf/util/symbol-elf.c > @@ -662,6 +662,61 @@ static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf, > return err; > } > > +/* > + * Check dynamic section DT_FLAGS_1 for a Position Independent > + * Executable (PIE). > + */ > +bool dso__is_pie(struct dso *dso) > +{ > + Elf *elf = NULL; > + Elf_Scn *scn = NULL; > + GElf_Ehdr ehdr; > + GElf_Shdr shdr; > + bool is_pie = false; > + char dso_path[PATH_MAX]; > + int fd = -1; > + > + if (!dso || (elf_version(EV_CURRENT) == EV_NONE)) > + return is_pie; // false > + > + dso__build_id_filename(dso, dso_path, sizeof(dso_path), false); > + > + fd = open(dso_path, O_RDONLY); > + > + if (fd < 0) { > + pr_debug("%s: cannot read cached %s.\n", __func__, dso_path); > + return is_pie; // false > + } > + > + elf = elf_begin(fd, ELF_C_READ, NULL); > + gelf_getehdr(elf, &ehdr); > + > + if (ehdr.e_type == ET_DYN) { The code looks good to me, just several nitpicks. To avoid indentation, we can firstly check the failure case and directly exit for it. if (ehdr.e_type != ET_DYN) goto exit_elf_end; > + scn = elf_section_by_name(elf, &ehdr, &shdr, ".dynamic", NULL); Ditto. if (!scn) goto exit_elf_end; > + if (scn) { // check DT_FLAGS_1 > + Elf_Data *data; > + GElf_Dyn *entry; > + int n_entries = shdr.sh_size / sizeof(GElf_Dyn); > + > + data = (Elf_Data *) elf_getdata(scn, NULL); For a safe code, it is good to check if pointers (data and data->d_buf) are valid before dereference them. if (!data || !data->d_buf) goto exit_elf_end; With above changes: Reviewed-by: Leo Yan > + for (int i = 0; i < n_entries; i++) { > + entry = ((GElf_Dyn *) data->d_buf) + i; > + if (entry->d_tag == DT_FLAGS_1) { > + if ((entry->d_un.d_val & DF_1_PIE) != 0) { > + is_pie = true; > + break; > + } > + } > + } // end for > + } > + } > + > + elf_end(elf); > + close(fd); > + > + return is_pie; > +} > + > /* > * We need to check if we have a .dynsym, so that we can handle the > * .plt, synthesizing its symbols, that aren't on the symtabs (be it > -- > 2.25.1 > >