This logic will also be needed for the CONFIG_CFI_CLANG support. Signed-off-by: Josh Poimboeuf <jpoim...@redhat.com> --- tools/objtool/elf.c | 59 ++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 22 deletions(-)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index be89c741ba9a..292f015f7ec6 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -262,6 +262,38 @@ struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, uns return find_reloc_by_dest_range(elf, sec, offset, 1); } +static int find_unsuffixed_func(const struct elf *elf, struct symbol *sym, + const char *suffix, struct symbol **func) +{ + char name[MAX_NAME_LEN + 1]; + const char *loc; + size_t len; + + *func = NULL; + + loc = strstr(sym->name, suffix); + if (!loc) + return 0; + + len = loc - sym->name; + if (len > MAX_NAME_LEN) { + WARN("%s(): unsuffixed function name exceeds maximum length of %d characters", + sym->name, MAX_NAME_LEN); + return -1; + } + + strncpy(name, sym->name, len); + name[len] = '\0'; + + *func = find_symbol_by_name(elf, name); + if (!*func || (*func)->type != STT_FUNC) { + WARN("%s(): can't find unsuffixed function", sym->name); + return -1; + } + + return 0; +} + void insn_to_reloc_sym_addend(struct section *sec, unsigned long offset, struct reloc *reloc) { @@ -456,37 +488,20 @@ static int read_symbols(struct elf *elf) /* Create parent/child links for any cold subfunctions */ list_for_each_entry(sec, &elf->sections, list) { list_for_each_entry(sym, &sec->symbol_list, list) { - char pname[MAX_NAME_LEN + 1]; - size_t pnamelen; if (sym->type != STT_FUNC) continue; - if (sym->pfunc == NULL) + if (!sym->pfunc) sym->pfunc = sym; - if (sym->cfunc == NULL) + if (!sym->cfunc) sym->cfunc = sym; - coldstr = strstr(sym->name, ".cold"); - if (!coldstr) - continue; - - pnamelen = coldstr - sym->name; - if (pnamelen > MAX_NAME_LEN) { - WARN("%s(): parent function name exceeds maximum length of %d characters", - sym->name, MAX_NAME_LEN); + if (find_unsuffixed_func(elf, sym, ".cold", &pfunc)) return -1; - } - strncpy(pname, sym->name, pnamelen); - pname[pnamelen] = '\0'; - pfunc = find_symbol_by_name(elf, pname); - - if (!pfunc) { - WARN("%s(): can't find parent function", - sym->name); - return -1; - } + if (!pfunc) + continue; sym->pfunc = pfunc; pfunc->cfunc = sym; -- 2.29.2