Re: [RFC PATCH 3/3] objtool/mcount: Add powerpc specific functions

From: Peter Zijlstra
Date: Fri Mar 18 2022 - 08:27:36 EST


On Fri, Mar 18, 2022 at 04:21:40PM +0530, Sathvika Vasireddy wrote:
> This patch adds powerpc specific functions required for
> 'objtool mcount' to work, and enables mcount for ppc.

I would love to see more objtool enablement for Power :-)


> diff --git a/tools/objtool/arch/powerpc/include/arch/elf.h b/tools/objtool/arch/powerpc/include/arch/elf.h
> new file mode 100644
> index 000000000000..3c8ebb7d2a6b
> --- /dev/null
> +++ b/tools/objtool/arch/powerpc/include/arch/elf.h
> @@ -0,0 +1,8 @@
> +/* SPDX-License-Identifier: GPL-2.0-or-later */
> +
> +#ifndef _OBJTOOL_ARCH_ELF
> +#define _OBJTOOL_ARCH_ELF
> +
> +#define R_NONE R_PPC_NONE
> +
> +#endif /* _OBJTOOL_ARCH_ELF */
> diff --git a/tools/objtool/utils.c b/tools/objtool/utils.c
> index d1fc6a123a6e..c9c14fa0dfd7 100644
> --- a/tools/objtool/utils.c
> +++ b/tools/objtool/utils.c
> @@ -179,11 +179,29 @@ int create_mcount_loc_sections(struct objtool_file *file)
> loc = (unsigned long *)sec->data->d_buf + idx;
> memset(loc, 0, sizeof(unsigned long));
>
> - if (elf_add_reloc_to_insn(file->elf, sec,
> - idx * sizeof(unsigned long),
> - R_X86_64_64,
> - insn->sec, insn->offset))
> - return -1;
> + if (file->elf->ehdr.e_machine == EM_X86_64) {
> + if (elf_add_reloc_to_insn(file->elf, sec,
> + idx * sizeof(unsigned long),
> + R_X86_64_64,
> + insn->sec, insn->offset))
> + return -1;
> + }
> +
> + if (file->elf->ehdr.e_machine == EM_PPC64) {
> + if (elf_add_reloc_to_insn(file->elf, sec,
> + idx * sizeof(unsigned long),
> + R_PPC64_ADDR64,
> + insn->sec, insn->offset))
> + return -1;
> + }
> +
> + if (file->elf->ehdr.e_machine == EM_PPC) {
> + if (elf_add_reloc_to_insn(file->elf, sec,
> + idx * sizeof(unsigned long),
> + R_PPC_ADDR32,
> + insn->sec, insn->offset))
> + return -1;
> + }

It appears to me that repeating this code 3 times doesn't really scale
well, how about we introduce a helper like:


int elf_reloc_type_long(struct elf *elf)
{
switch (elf->ehdr.e_machine) {
case EM_X86_64:
return R_X86_64_64;
case EM_PPC64:
return R_PPC64_ADDR64;
case EM_PPC:
return R_PPC_ADDR32;
default:
WARN("unknown machine...")
exit(-1);
}
}

if (elf_add_reloc_to_insn(file->elf, sec,
idx * sizeof(unsigned long),
elf_reloc_type_long(file->elf),
insn->sec, insn->offset))
return -1;