]> sourceware.org Git - libabigail.git/commitdiff
elf-helpers: refactor find_symbol_table_section
authorMatthias Maennich <maennich@google.com>
Mon, 14 Jun 2021 10:07:35 +0000 (11:07 +0100)
committerDodji Seketeli <dodji@redhat.com>
Thu, 15 Jul 2021 14:14:10 +0000 (16:14 +0200)
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
src/abg-elf-helpers.h

index 480381dec77e4d33034dab80af54eedc27f715d5..998675a6059a0fff69f7366313a98ccb5aa1d5e3 100644 (file)
@@ -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
index 96e03d26ccad169ab6e29eb3020b8cab8109c3b5..59ea0a74e45ccc9d0021a934ad4a53a3885dfd3f 100644 (file)
@@ -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);
 
This page took 0.038052 seconds and 5 git commands to generate.