[1/2] elf-helpers: refactor find_symbol_table_section
Commit Message
Refactor the acquisition of symtabs to explicitly provide functionality
to get the .symtab and .dynsym sections. A later patch will make use of
that to acquire .symtab while find_symbol_table_section() still provides
.dynsym as default symbol table.
This also adds a new overload to find_section to acquire the first
section by type and adjusts find_symbol_table_section() to make use of
those functions.
* src/abg-elf-helpers.cc(find_section): New overload.
(find_symtab_section): New function.
(find_dynsym_section): New function.
(find_symbol_table_section): Use new find_*_section functions.
* src/abg-elf-helpers.h(find_section): New overload declaration.
(find_symtab_section): New function declaration.
(find_dynsym_section): New function declaration.
Reviewed-by: Giuliano Procida <gprocida@google.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
src/abg-elf-helpers.cc | 59 ++++++++++++++++++++++++++++++++++--------
src/abg-elf-helpers.h | 9 +++++++
2 files changed, 57 insertions(+), 11 deletions(-)
Comments
Matthias Maennich <maennich@google.com> a écrit:
> Refactor the acquisition of symtabs to explicitly provide functionality
> to get the .symtab and .dynsym sections. A later patch will make use of
> that to acquire .symtab while find_symbol_table_section() still provides
> .dynsym as default symbol table.
>
> This also adds a new overload to find_section to acquire the first
> section by type and adjusts find_symbol_table_section() to make use of
> those functions.
>
> * src/abg-elf-helpers.cc(find_section): New overload.
> (find_symtab_section): New function.
> (find_dynsym_section): New function.
> (find_symbol_table_section): Use new find_*_section functions.
> * src/abg-elf-helpers.h(find_section): New overload declaration.
> (find_symtab_section): New function declaration.
> (find_dynsym_section): New function declaration.
>
> Reviewed-by: Giuliano Procida <gprocida@google.com>
> Signed-off-by: Matthias Maennich <maennich@google.com>
Applied to master.
Thanks!
[...]
Cheers,
@@ -331,6 +331,51 @@ find_section(Elf* elf_handle, const std::string& name, Elf64_Word section_type)
return 0;
}
+/// Find and return a section by its type.
+///
+/// @param elf_handle the elf handle to use.
+///
+/// @param section_type the type of the section. This is the
+/// Elf32_Shdr::sh_type (or Elf64_Shdr::sh_type) data member.
+/// Examples of values of this parameter are SHT_PROGBITS or SHT_NOBITS.
+///
+/// @return the section found, or nil if none was found.
+Elf_Scn*
+find_section(Elf* elf_handle, Elf64_Word section_type)
+{
+ Elf_Scn* section = nullptr;
+ while ((section = elf_nextscn(elf_handle, section)) != 0)
+ {
+ GElf_Shdr header_mem, *header;
+ header = gelf_getshdr(section, &header_mem);
+ if (header->sh_type == section_type)
+ break;
+ }
+ return section;
+}
+
+/// Find and return the .symtab section
+///
+/// @param elf_handle the elf handle to use.
+///
+/// @return the section found, or nil if none was found
+Elf_Scn*
+find_symtab_section(Elf* elf_handle)
+{
+ return find_section(elf_handle, SHT_SYMTAB);
+}
+
+/// Find and return the .symtab section
+///
+/// @param elf_handle the elf handle to use.
+///
+/// @return the section found, or nil if none was found
+Elf_Scn*
+find_dynsym_section(Elf* elf_handle)
+{
+ return find_section(elf_handle, SHT_DYNSYM);
+}
+
/// Find the symbol table.
///
/// If we are looking at a relocatable or executable file, this
@@ -346,16 +391,8 @@ find_section(Elf* elf_handle, const std::string& name, Elf64_Word section_type)
Elf_Scn*
find_symbol_table_section(Elf* elf_handle)
{
- Elf_Scn* section = 0, *dynsym = 0, *sym_tab = 0;
- while ((section = elf_nextscn(elf_handle, section)) != 0)
- {
- GElf_Shdr header_mem, *header;
- header = gelf_getshdr(section, &header_mem);
- if (header->sh_type == SHT_DYNSYM)
- dynsym = section;
- else if (header->sh_type == SHT_SYMTAB)
- sym_tab = section;
- }
+ Elf_Scn *dynsym = find_dynsym_section(elf_handle),
+ *sym_tab = find_symtab_section(elf_handle);
if (dynsym || sym_tab)
{
@@ -367,7 +404,7 @@ find_symbol_table_section(Elf* elf_handle)
else
return dynsym ? dynsym : sym_tab;
}
- return NULL;
+ return nullptr;
}
/// Find the index (in the section headers table) of the symbol table
@@ -49,6 +49,15 @@ find_section(Elf* elf_handle,
const std::string& name,
Elf64_Word section_type);
+Elf_Scn*
+find_section(Elf* elf_handle, Elf64_Word section_type);
+
+Elf_Scn*
+find_symtab_section(Elf* elf_handle);
+
+Elf_Scn*
+find_dynsym_section(Elf* elf_handle);
+
Elf_Scn*
find_symbol_table_section(Elf* elf_handle);