diff mbox series

[1/2] elf-helpers: refactor find_symbol_table_section

Message ID 20210614100736.1665072-1-maennich@google.com
State New
Headers show
Series [1/2] elf-helpers: refactor find_symbol_table_section | expand

Commit Message

Matthias Männich June 14, 2021, 10:07 a.m. UTC
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

Dodji Seketeli July 15, 2021, 2:18 p.m. UTC | #1
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,
diff mbox series

Patch

diff --git a/src/abg-elf-helpers.cc b/src/abg-elf-helpers.cc
index 480381dec77e..998675a6059a 100644
--- a/src/abg-elf-helpers.cc
+++ b/src/abg-elf-helpers.cc
@@ -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
diff --git a/src/abg-elf-helpers.h b/src/abg-elf-helpers.h
index 96e03d26ccad..59ea0a74e45c 100644
--- a/src/abg-elf-helpers.h
+++ b/src/abg-elf-helpers.h
@@ -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);