[RFC][PATCH v5 39/51] objtool: mcount: Verify x86 instruction with memcmp()

From: Matt Helsley
Date: Thu Jun 18 2020 - 16:41:07 EST


Instead of hard-coding what amounts to a memcmp() use memcmp to
determine if the instruction we wish to replace matches what we
expect. This makes the x86 code more like that of, for instance,
ARM.

Signed-off-by: Matt Helsley <mhelsley@xxxxxxxxxx>
---
tools/objtool/mcount.c | 19 ++++++-------------
1 file changed, 6 insertions(+), 13 deletions(-)

diff --git a/tools/objtool/mcount.c b/tools/objtool/mcount.c
index 4d6596a031bf..5c59df0df97b 100644
--- a/tools/objtool/mcount.c
+++ b/tools/objtool/mcount.c
@@ -47,9 +47,10 @@ extern int warn_on_notrace_sect; /* warn when section has mcount not being recor

static struct elf *lf;

-static unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
-static unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
-static unsigned char *ideal_nop;
+static const unsigned char ip_relative_call_x86[5] = { 0xe8, 0x00, 0x00, 0x00, 0x00 };
+static const unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
+static const unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
+static const unsigned char *ideal_nop;

static char rel_type_nop;

@@ -57,20 +58,12 @@ static int (*make_nop)(struct section *, size_t const offset);

static int make_nop_x86(struct section *txts, size_t const offset)
{
- uint32_t *ptr;
- unsigned char *op;
- void *map = txts->data->d_buf;
+ unsigned char *op = txts->data->d_buf + offset - 1;

if (offset < 1)
return -1;

- /* Confirm we have 0xe8 0x0 0x0 0x0 0x0 */
- ptr = map + offset;
- if (*ptr != 0)
- return -1;
-
- op = map + offset - 1;
- if (*op != 0xe8)
+ if (memcmp(op, ip_relative_call_x86, 5) != 0)
return -1;

/* convert to nop */
--
2.20.1