[RFC][PATCH 11/13] objtool: recordmcount: Start using objtool's elf wrapper

Matt Helsley
2019-05-23T00:04:13Z

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 separte 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@vmware.com>
---
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 8345eb5b681b..71dae95deddc 100644
--- a/tools/objtool/recordmcount.c
+++ b/tools/objtool/recordmcount.c
@@ -33,6 +33,8 @@

#include "builtin-mcount.h"

+#include "elf.h"
+
#ifndef EM_AARCH64
#define EM_AARCH64 183
#define R_AARCH64_NONE 0
@@ -53,6 +55,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)
{
@@ -69,6 +73,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. */
@@ -166,11 +173,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(fname, O_RDONLY);
+ if (!lf) {
perror(fname);
return NULL;
}
+ fd_map = lf->fd;
if (fstat(fd_map, &sb) < 0) {
perror(fname);
goto out;
@@ -190,14 +198,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