Re: [PATCH 3/3] objtool: optimize add_dead_ends for split sections

From: Kees Cook
Date: Tue Apr 21 2020 - 16:14:42 EST


On Tue, Apr 21, 2020 at 11:07:24AM -0700, Sami Tolvanen wrote:
> When running objtool on vmlinux.o compiled with -ffunction-sections,
> .rela.discard.(un)reachable often contains relocations that point to
> a different section. Instead of iterating through the list of all
> instructions each time, store a pointer to the last instruction of
> each section when decoding instructions.
>
> Signed-off-by: Sami Tolvanen <samitolvanen@xxxxxxxxxx>

Reviewed-by: Kees Cook <keescook@xxxxxxxxxxxx>

-Kees

> ---
> tools/objtool/check.c | 43 ++++++++++++++++++++++++++-----------------
> tools/objtool/check.h | 4 ++++
> tools/objtool/elf.h | 1 +
> 3 files changed, 31 insertions(+), 17 deletions(-)
>
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index 4770fb07b365..7d4104de0a5e 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -237,6 +237,7 @@ static void clear_insn_state(struct insn_state *state)
> static int decode_instructions(struct objtool_file *file)
> {
> struct section *sec;
> + struct section_info *sec_info = NULL;
> struct symbol *func;
> unsigned long offset;
> struct instruction *insn;
> @@ -253,6 +254,8 @@ static int decode_instructions(struct objtool_file *file)
> strncmp(sec->name, ".discard.", 9))
> sec->text = true;
>
> + insn = NULL;
> +
> for (offset = 0; offset < sec->len; offset += insn->len) {
> insn = malloc(sizeof(*insn));
> if (!insn) {
> @@ -279,6 +282,17 @@ static int decode_instructions(struct objtool_file *file)
> nr_insns++;
> }
>
> + if (insn) {
> + sec_info = malloc(sizeof(*sec_info));
> + if (!sec_info) {
> + WARN("malloc failed");
> + return -1;
> + }
> +
> + sec_info->last_insn = insn;
> + sec->section_info = sec_info;
> + }
> +
> list_for_each_entry(func, &sec->symbol_list, list) {
> if (func->type != STT_FUNC || func->alias != func)
> continue;
> @@ -312,7 +326,6 @@ static int add_dead_ends(struct objtool_file *file)
> struct section *sec;
> struct rela *rela;
> struct instruction *insn;
> - bool found;
>
> /*
> * By default, "ud2" is a dead end unless otherwise annotated, because
> @@ -338,15 +351,13 @@ static int add_dead_ends(struct objtool_file *file)
> if (insn)
> insn = list_prev_entry(insn, list);
> else if (rela->addend == rela->sym->sec->len) {
> - found = false;
> - list_for_each_entry_reverse(insn, &file->insn_list, list) {
> - if (insn->sec == rela->sym->sec) {
> - found = true;
> - break;
> - }
> - }
> + struct section_info *sec_info = (struct section_info *)
> + rela->sym->sec->section_info;
> +
> + if (sec_info)
> + insn = sec_info->last_insn;
>
> - if (!found) {
> + if (!insn) {
> WARN("can't find unreachable insn at %s+0x%x",
> rela->sym->sec->name, rela->addend);
> return -1;
> @@ -380,15 +391,13 @@ static int add_dead_ends(struct objtool_file *file)
> if (insn)
> insn = list_prev_entry(insn, list);
> else if (rela->addend == rela->sym->sec->len) {
> - found = false;
> - list_for_each_entry_reverse(insn, &file->insn_list, list) {
> - if (insn->sec == rela->sym->sec) {
> - found = true;
> - break;
> - }
> - }
> + struct section_info *sec_info = (struct section_info *)
> + rela->sym->sec->section_info;
> +
> + if (sec_info)
> + insn = sec_info->last_insn;
>
> - if (!found) {
> + if (!insn) {
> WARN("can't find reachable insn at %s+0x%x",
> rela->sym->sec->name, rela->addend);
> return -1;
> diff --git a/tools/objtool/check.h b/tools/objtool/check.h
> index bc78eca7982e..353677ec85d4 100644
> --- a/tools/objtool/check.h
> +++ b/tools/objtool/check.h
> @@ -47,6 +47,10 @@ struct instruction {
> struct orc_entry orc;
> };
>
> +struct section_info {
> + struct instruction *last_insn;
> +};
> +
> struct objtool_file {
> struct elf *elf;
> struct list_head insn_list;
> diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h
> index ebbb10c61e24..98f2b41d18e4 100644
> --- a/tools/objtool/elf.h
> +++ b/tools/objtool/elf.h
> @@ -40,6 +40,7 @@ struct section {
> int idx;
> unsigned int len;
> bool changed, text, rodata;
> + void *section_info;
> };
>
> struct symbol {
> --
> 2.26.1.301.g55bc3eb7cb9-goog
>

--
Kees Cook