[RFC][PATCH 08/36] objtool: recordmcount: Start using objtool's elf wrapper

From: Matt Helsley
Date: Fri Apr 10 2020 - 15:36:58 EST


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 <mhelsley@xxxxxxxxxx>
---
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