[RFC][PATCH v5 19/51] objtool: mcount: Restrict using ehdr in append_func()

From: Matt Helsley
Date: Thu Jun 18 2020 - 16:43:12 EST


Use the ehdr parameter to append_func() to write the ELF file's
header but use the objtool ELF header data as the basis for
making changes. The makes it clearer when we can switch from
using the old recordmcount wrapper to write the ELF file
because ehdr will only be passed on to uwrite() calls and those
will get replaced later.

Signed-off-by: Matt Helsley <mhelsley@xxxxxxxxxx>
---
tools/objtool/recordmcount.h | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/tools/objtool/recordmcount.h b/tools/objtool/recordmcount.h
index eed592954f37..19bff5a7c8ce 100644
--- a/tools/objtool/recordmcount.h
+++ b/tools/objtool/recordmcount.h
@@ -93,7 +93,7 @@ static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_E

static int mcount_adjust = 0;

-static unsigned int get_shnum(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
+static unsigned int get_shnum(GElf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
{
if (shdr0 && !ehdr->e_shnum)
return w(shdr0->sh_size);
@@ -133,11 +133,11 @@ static int append_func(Elf_Ehdr *const ehdr,
char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
? ".rela__mcount_loc"
: ".rel__mcount_loc";
- uint_t const old_shoff = _w(ehdr->e_shoff);
+ uint_t const old_shoff = lf->ehdr.e_shoff;
uint_t const old_shstr_sh_size = _w(shstr->sh_size);
uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
Elf_Shdr *const shdr0 = (Elf_Shdr *)(old_shoff + (void *)ehdr);
- unsigned int const old_shnum = get_shnum(ehdr, shdr0);
+ unsigned int const old_shnum = get_shnum(&lf->ehdr, shdr0);
unsigned int const new_shnum = 2 + old_shnum; /* {.rel,}__mcount_loc */
uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
uint_t new_e_shoff;
@@ -149,8 +149,6 @@ static int append_func(Elf_Ehdr *const ehdr,
t += (_align & -t); /* word-byte align */
new_e_shoff = t;

- set_shnum(ehdr, shdr0, new_shnum);
-
/* body for new shstrtab */
if (ulseek(sb.st_size, SEEK_SET) < 0)
return -1;
@@ -205,6 +203,7 @@ static int append_func(Elf_Ehdr *const ehdr,
return -1;

ehdr->e_shoff = _w(new_e_shoff);
+ set_shnum(ehdr, shdr0, new_shnum);
if (ulseek(0, SEEK_SET) < 0)
return -1;
if (uwrite(ehdr, sizeof(*ehdr)) < 0)
--
2.20.1