--- elfutils/src/findtextrel.c +++ elfutils/src/findtextrel.c @@ -29,7 +29,6 @@ #endif #include -#include #include #include #include @@ -489,6 +488,22 @@ ptrcompare (const void *p1, const void *p2) } +static const char * +get_idx_str (Elf *elf, Elf_Data *symdata, GElf_Shdr *shdr, GElf_Sym *sym, + int idx) +{ + if (idx == -1) + return NULL; + + sym = gelf_getsym (symdata, idx, sym); + if (!sym) + return NULL; + + const char *str = elf_strptr (elf, shdr->sh_link, sym->st_name); + return (str && *str) ? str : NULL; +} + + static void check_rel (size_t nsegments, struct segments segments[nsegments], GElf_Addr addr, Elf *elf, Elf_Scn *symscn, Dwarf *dw, @@ -554,15 +569,11 @@ check_rel (size_t nsegments, struct segments segments[nsegments], } } - if (lowidx != -1) + const char *lowstr = get_idx_str (elf, symdata, shdr, &sym_mem, + lowidx); + if (lowstr) { - sym = gelf_getsym (symdata, lowidx, &sym_mem); - assert (sym != NULL); - - const char *lowstr = elf_strptr (elf, shdr->sh_link, - sym->st_name); - - if (sym->st_value + sym->st_size > addr) + if (sym_mem.st_value + sym_mem.st_size > addr) { /* It is this function. */ if (tfind (lowstr, knownsrcs, ptrcompare) == NULL) @@ -572,31 +583,29 @@ the file containing the function '%s' is not compiled with -fpic/-fPIC\n"), lowstr); tsearch (lowstr, knownsrcs, ptrcompare); } + return; } - else if (highidx == -1) + const char *highstr = get_idx_str (elf, symdata, shdr, + &sym_mem, highidx); + if (!highstr) printf (gettext ("\ the file containing the function '%s' might not be compiled with -fpic/-fPIC\n"), lowstr); else { - sym = gelf_getsym (symdata, highidx, &sym_mem); - assert (sym != NULL); - printf (gettext ("\ either the file containing the function '%s' or the file containing the function '%s' is not compiled with -fpic/-fPIC\n"), - lowstr, elf_strptr (elf, shdr->sh_link, - sym->st_name)); + lowstr, highstr); } return; } - else if (highidx != -1) + const char *highstr = get_idx_str (elf, symdata, shdr, + &sym_mem, highidx); + if (highstr) { - sym = gelf_getsym (symdata, highidx, &sym_mem); - assert (sym != NULL); - printf (gettext ("\ the file containing the function '%s' might not be compiled with -fpic/-fPIC\n"), - elf_strptr (elf, shdr->sh_link, sym->st_name)); + highstr); return; } }