Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp910114ybb; Fri, 10 Apr 2020 12:38:15 -0700 (PDT) X-Google-Smtp-Source: APiQypKrYSGMIlq0EJZ8BSqL2OxbxVhaOvV2tYcifxgn/6zuJ1zq2bwa8HpOlMf2j4cCu3CTP4bM X-Received: by 2002:ac8:7246:: with SMTP id l6mr785181qtp.298.1586547494942; Fri, 10 Apr 2020 12:38:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586547494; cv=none; d=google.com; s=arc-20160816; b=yLPvpYxg19j6tHZ1IfL8ZlqqVIZZez5R6pcSYNyKeaePWIIIPTMtcSbOmGh2wr1FnF PnzXk8fUQk94udDH6W7ySQo4toDKi90hXCbF369reOx03Ay7dlHf1jAyvZZ37M6D7QMF swb/MfqqQZD2vC4Pi1Vl6juTC74ljq5UExcV39c8JRPCo1M1DNG/UMpVMkl7MrdvjUtF Brd5MysiJJUX3u6D68ZtmaWBxn2G7mHHJ8AiKMhTkFSb/3g6eJ2TARFkVv7Mcbl5pP/s Zw75EKd9yMvnI5TyxHR7XpnentAig3hG8pUvZUHdAPKhmagEP7+RKTHfxJDX4wWJejC/ BwDg== 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=3NFA1gvIQmqJ8oXli8Im5U/S3gA1MtyL6G1nKVzprns=; b=0y8487q5GFTDwO6c75jq0Frcb/axta+zCHAiaLVRyPY85LCNhvgAw6lqa694ibqIT6 5iLTMf01VucnMYisFPL7LD8lkTE4F4KtNNWCHbm4XVk74oYtKn+lJJK1OQi31LwLjziE urnpVK1cvB2kGdH3TYsZjuG2jiUmokk+pFe5onCAJ2/2FHSwXS+vqSyd9nb5KoIhUIPn 8Wed7dkSNs5HZSq//Ydoq99WPho692FcugdXgAxCbQfbAO8omYw0s2wRDTc+qMIanpgL 7vSr5TYQRfUq5DgPKJFbgYUTnfCUYgPGOHkv0u0zHPPVGJ2om3GKcZMdEu0WsRjU1m/f w73g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=vmware.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 6si1444649qth.171.2020.04.10.12.38.00; Fri, 10 Apr 2020 12:38:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=vmware.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726837AbgDJTg4 (ORCPT + 99 others); Fri, 10 Apr 2020 15:36:56 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:55144 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726594AbgDJTg4 (ORCPT ); Fri, 10 Apr 2020 15:36:56 -0400 Received: from sc9-mailhost2.vmware.com (10.113.161.72) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Fri, 10 Apr 2020 12:36:51 -0700 Received: from sc9-mailhost3.vmware.com (unknown [10.166.69.226]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 76B76B2C6E; Fri, 10 Apr 2020 15:36:55 -0400 (EDT) From: Matt Helsley To: CC: Josh Poimboeuf , Peter Zijlstra , Julien Thierry , Ingo Molnar , Steven Rostedt , Miroslav Benes , Matt Helsley Subject: [RFC][PATCH 08/36] objtool: recordmcount: Start using objtool's elf wrapper Date: Fri, 10 Apr 2020 12:35:31 -0700 Message-ID: <8c03636a45b927add190a5c229a1bd2fc84971e4.1586468801.git.mhelsley@vmware.com> 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-001.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/objcount 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 f9844166fdaf..69da5339575f 100644 --- a/tools/objtool/recordmcount.c +++ b/tools/objtool/recordmcount.c @@ -33,6 +33,8 @@ #include "mcount.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_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