Signed-off-by: Stephen Brennan <stephen.s.brennan@xxxxxxxxxx> Reviewed-by: Alan Maguire <alan.maguire@xxxxxxxxxx> --- dutil.c | 14 +++++++++++--- dutil.h | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/dutil.c b/dutil.c index 97c4474..14f1340 100644 --- a/dutil.c +++ b/dutil.c @@ -207,13 +207,21 @@ Elf_Scn *elf_section_by_name(Elf *elf, GElf_Shdr *shp, const char *name, size_t return sec; } -Elf_Scn *elf_section_by_idx(Elf *elf, GElf_Shdr *shp, int idx) +Elf_Scn *elf_section_by_idx(Elf *elf, GElf_Shdr *shp, int idx, const char **name_out) { Elf_Scn *sec; + size_t str_idx; sec = elf_getscn(elf, idx); - if (sec) - gelf_getshdr(sec, shp); + if (!sec) + return NULL; + if (!gelf_getshdr(sec, shp)) + return NULL; + if (name_out) { + if (elf_getshdrstrndx(elf, &str_idx)) + return NULL; + *name_out = elf_strptr(elf, str_idx, shp->sh_name); + } return sec; } diff --git a/dutil.h b/dutil.h index 335a17c..ff78aa6 100644 --- a/dutil.h +++ b/dutil.h @@ -328,7 +328,7 @@ void *zalloc(const size_t size); Elf_Scn *elf_section_by_name(Elf *elf, GElf_Shdr *shp, const char *name, size_t *index); -Elf_Scn *elf_section_by_idx(Elf *elf, GElf_Shdr *shp, int idx); +Elf_Scn *elf_section_by_idx(Elf *elf, GElf_Shdr *shp, int idx, const char **name_out); #ifndef SHT_GNU_ATTRIBUTES /* Just a way to check if we're using an old elfutils version */ -- 2.43.5