public inbox for libabigail@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/2] elf-helpers: refactor find_symbol_table_section
@ 2021-06-14 10:07 Matthias Maennich
  2021-06-14 10:07 ` [PATCH 2/2] symtab-reader: add support for binaries compiled with CFI Matthias Maennich
  2021-07-15 14:18 ` [PATCH 1/2] elf-helpers: refactor find_symbol_table_section Dodji Seketeli
  0 siblings, 2 replies; 4+ messages in thread
From: Matthias Maennich @ 2021-06-14 10:07 UTC (permalink / raw)
  To: libabigail; +Cc: dodji, gprocida, kernel-team, maennich

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(-)

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);
 
-- 
2.32.0.272.g935e593368-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-07-15 16:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-14 10:07 [PATCH 1/2] elf-helpers: refactor find_symbol_table_section Matthias Maennich
2021-06-14 10:07 ` [PATCH 2/2] symtab-reader: add support for binaries compiled with CFI Matthias Maennich
2021-07-15 16:11   ` Dodji Seketeli
2021-07-15 14:18 ` [PATCH 1/2] elf-helpers: refactor find_symbol_table_section Dodji Seketeli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).