Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp1544207ybg; Tue, 2 Jun 2020 12:56:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzuhEHMvQ+kuLOXZQwh4TJBkRVSOz6Ase9bNPsyeLRL7U5lHKw5gfKCS3EVFbOHUbBRFND6 X-Received: by 2002:a17:907:217a:: with SMTP id rl26mr24289820ejb.209.1591127761627; Tue, 02 Jun 2020 12:56:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591127761; cv=none; d=google.com; s=arc-20160816; b=ueYRL91hiv5tXtWbT+OXG2h1jb3/K8VjYKMCkUmKisFY+vlRyaJNam0LBz+TYfE0dF cXxjTM73GzYNeTSf2a/sYT/i1hcZyRjrM/A3GNrgTMDay0eWH1Zj+Vbekk2Y1zbEUh/x D72HpGLcOBkiwxT96oQFLEGZdlxfbbOqAcD6GIINl6POiCm7VqNWIWXF6+lef4UDjedV hbEfXq3qonhm0ujy+SQ1HWk5ROYLxo6Fz9IOO0LB8QJuxxzFc8OiJxcsRcoOuR8Q1yFv YM+4CVN8C/gCqFldyOcKJh68sXN//LzJI5Ycz6WUmXBOTyVAjsfViYYGBAT9q5JCSOfS IyzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=vo48KE7eyux1jly0OlWQ97C+gk8zT3bBytH8b2TFJpc=; b=IT1Yoiw+jV7E/QtBIkx3t569t+SmiTXqtzYWLMvZguIfjgkiXkWKrAaND7Zk3Gts05 hfnlbqDCNCroR1ZKzuEFk3J1eyGZLc8uKHHrGT5kQxAWe75xtK/t29f8gR2yYNlMwWY3 kaFOUR9Qz4BPPUPzz+XmUo6dWQ20X2KfLBlDB259yIfmMJnohIuik16roar09pU8ELKa 4dHV1cq5OC2yhVWmO3oZR8Ilki52wAFrFLxgAb4Zv0T/NuKrnJjUCgg3eDLPJGocFfiO jhwV0nrDqvKP7xKCwS1/Jn7yYJyWm4iGcgaF+BqNuuSHcjbB1QMuObqg/DIxXe31fqKG cbvA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=vmware.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g15si2029056ejh.346.2020.06.02.12.55.38; Tue, 02 Jun 2020 12:56:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=vmware.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728071AbgFBTu4 (ORCPT + 99 others); Tue, 2 Jun 2020 15:50:56 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:36854 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728123AbgFBTuu (ORCPT ); Tue, 2 Jun 2020 15:50:50 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Tue, 2 Jun 2020 12:50:48 -0700 Received: from sc9-mailhost3.vmware.com (unknown [10.200.192.49]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id CF3EB40BED; Tue, 2 Jun 2020 12:50:48 -0700 (PDT) From: Matt Helsley To: CC: Josh Poimboeuf , Peter Zijlstra , Steven Rostedt , Sami Tolvanen , Julien Thierry , Kamalesh Babulal , Matt Helsley Subject: [RFC][PATCH v4 03/32] objtool: recordmcount: Start using objtool's elf wrapper Date: Tue, 2 Jun 2020 12:49:56 -0700 Message-ID: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII Received-SPF: None (EX13-EDG-OU-002.vmware.com: mhelsley@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use struct elf to grab the file descriptor. We will later move these calls into other functions as we expand the lifetime of the struct elf so that it can be passed to objtool elf.[ch] functions. This creates the libelf/objtool data structures and gives us two separate ways to walk the ELF file -- the libelf/objtool way and the old recordmcount wrapper way which avoids these extra data structures by using indices, offsets, and pointers into the mmapped ELF file. Subsequent patches will convert from the old recordmcount accessors to the libelf/objtool accessors. Signed-off-by: Matt Helsley --- tools/objtool/recordmcount.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tools/objtool/recordmcount.c b/tools/objtool/recordmcount.c index 601e83840085..b2c606eb269b 100644 --- a/tools/objtool/recordmcount.c +++ b/tools/objtool/recordmcount.c @@ -33,6 +33,8 @@ #include "objtool.h" +#include "elf.h" + #ifndef EM_AARCH64 #define EM_AARCH64 183 #define R_AARCH64_NONE 0 @@ -57,6 +59,8 @@ static void *file_ptr; /* current file pointer location */ static void *file_append; /* added to the end of the file */ static size_t file_append_size; /* how much is added to end of file */ +static struct elf *lf; + /* Per-file resource cleanup when multiple files. */ static void file_append_cleanup(void) { @@ -73,6 +77,9 @@ static void mmap_cleanup(void) else free(file_map); file_map = NULL; + if (lf) + elf_close(lf); + lf = NULL; } /* ulseek, uwrite, ...: Check return value for errors. */ @@ -170,11 +177,12 @@ static void *mmap_file(char const *fname) file_updated = 0; sb.st_size = 0; - fd_map = open(fname, O_RDONLY); - if (fd_map < 0) { + lf = elf_open_read(fname, O_RDONLY); + if (!lf) { perror(fname); return NULL; } + fd_map = lf->fd; if (fstat(fd_map, &sb) < 0) { perror(fname); goto out; @@ -194,14 +202,14 @@ static void *mmap_file(char const *fname) } if (read(fd_map, file_map, sb.st_size) != sb.st_size) { perror(fname); - free(file_map); - file_map = NULL; + mmap_cleanup(); goto out; } } else mmap_failed = 0; out: - close(fd_map); + elf_close(lf); + lf = NULL; fd_map = -1; file_end = file_map + sb.st_size; -- 2.20.1