diff mbox series

[v2,8/8] abg-elf-helpers: migrate maybe_adjust_et_rel_sym_addr_to_abs_addr

Message ID 20200421063551.222511-9-maennich@google.com
State Committed
Headers show
Series Refactor dwarf-reader: split out ELF helpers | expand

Commit Message

Matthias Maennich April 21, 2020, 6:35 a.m. UTC
Move maybe_adjust_et_rel_sym_addr_to_abs_addr to the elf-helpers.

This function had two overloads

  GElf_Addr
  maybe_adjust_et_rel_sym_addr_to_abs_addr(GElf_Addr addr, Elf_Scn *section);

  GElf_Addr
  maybe_adjust_et_rel_sym_addr_to_abs_addr(GElf_Sym *sym);

The former one is only ever called by the latter. Hence consolidate them
into

  GElf_Addr
  maybe_adjust_et_rel_sym_addr_to_abs_addr(Elf* elf_handle, GElf_Sym* sym);

to combine their functionality and preserve the outer interface. Just we
add the Elf* handle argument as usual as we are out of read_context now.

	* src/abg-dwarf-reader.cc(maybe_adjust_et_rel_sym_addr_to_abs_addr):
	Move out functions (drop the wrapped overload completely).
	* src/abg-elf-helpers.cc(maybe_adjust_et_rel_sym_addr_to_abs_addr):
	New function.
	* src/abg-elf-helpers.h(maybe_adjust_et_rel_sym_addr_to_abs_addr):
	New function declaration.

Reviewed-by: Giuliano Procida <gprocida@google.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
---
 src/abg-dwarf-reader.cc | 73 ++++-------------------------------------
 src/abg-elf-helpers.cc  | 40 ++++++++++++++++++++++
 src/abg-elf-helpers.h   |  3 ++
 3 files changed, 49 insertions(+), 67 deletions(-)

Comments

Dodji Seketeli April 22, 2020, 9:56 a.m. UTC | #1
Matthias Maennich <maennich@google.com> a ?crit:

[...]

> 	* src/abg-dwarf-reader.cc(maybe_adjust_et_rel_sym_addr_to_abs_addr):

Adding a space here -------------^

Likewise or the relevant lines below.

> 	Move out functions (drop the wrapped overload completely).
> 	* src/abg-elf-helpers.cc(maybe_adjust_et_rel_sym_addr_to_abs_addr):
> 	New function.
> 	* src/abg-elf-helpers.h(maybe_adjust_et_rel_sym_addr_to_abs_addr):
> 	New function declaration.

Applying to master with that change.  Thanks!

Cheers,
diff mbox series

Patch

diff --git a/src/abg-dwarf-reader.cc b/src/abg-dwarf-reader.cc
index dc3332410b1e..c65c01776398 100644
--- a/src/abg-dwarf-reader.cc
+++ b/src/abg-dwarf-reader.cc
@@ -6425,7 +6425,8 @@  public:
 
 		{
 		  GElf_Addr symbol_value =
-		    maybe_adjust_et_rel_sym_addr_to_abs_addr(sym);
+		      maybe_adjust_et_rel_sym_addr_to_abs_addr(elf_handle(),
+							       sym);
 
 		  addr_elf_symbol_sptr_map_type::const_iterator it =
 		    fun_addr_sym_map_->find(symbol_value);
@@ -6563,7 +6564,8 @@  public:
 		else
 		  {
 		    GElf_Addr symbol_value =
-		      maybe_adjust_et_rel_sym_addr_to_abs_addr(sym);
+			maybe_adjust_et_rel_sym_addr_to_abs_addr(elf_handle(),
+								 sym);
 		    addr_elf_symbol_sptr_map_type::const_iterator it =
 		      var_addr_sym_map_->find(symbol_value);
 		    if (it == var_addr_sym_map_->end())
@@ -7147,8 +7149,8 @@  public:
 	// the symbol value of native_symbol is relative to the
 	// section that symbol is defined in.  We need to translate it
 	// into an absolute (okay, binary-relative, rather) address.
-	GElf_Addr symbol_address =
-	  maybe_adjust_et_rel_sym_addr_to_abs_addr (&native_symbol);
+	GElf_Addr symbol_address = maybe_adjust_et_rel_sym_addr_to_abs_addr(
+	    elf_handle(), &native_symbol);
 
 	address_set_sptr set;
 	if (symbol->is_function())
@@ -7523,69 +7525,6 @@  public:
     return addr;
   }
 
-  /// Translate a section-relative symbol address (i.e, symbol value)
-  /// into an absolute symbol address by adding the address of the
-  /// section the symbol belongs to, to the address value.
-  ///
-  /// This is useful when looking at symbol values coming from
-  /// relocatable files (of ET_REL kind).  If the binary is not
-  /// ET_REL, then the function does nothing and returns the input
-  /// address unchanged.
-  ///
-  /// @param addr the symbol address to possibly translate.
-  ///
-  /// @param section the section the symbol which value is @p addr
-  /// belongs to.
-  ///
-  /// @return the section-relative address, translated into an
-  /// absolute address, if @p section is an ET_REL binary.  Otherwise,
-  /// return @p addr, unchanged.
-  GElf_Addr
-  maybe_adjust_et_rel_sym_addr_to_abs_addr(GElf_Addr addr, Elf_Scn *section)
-  {
-    if (!section)
-      return addr;
-
-    Elf* elf = elf_handle();
-    GElf_Ehdr elf_header;
-
-    if (!gelf_getehdr(elf, &elf_header))
-      return addr;
-
-    if (elf_header.e_type != ET_REL)
-      return addr;
-
-    GElf_Shdr section_header;
-    if (!gelf_getshdr(section, &section_header))
-      return addr;
-
-    return addr + section_header.sh_addr;
-  }
-
-  /// Translate a section-relative symbol address (i.e, symbol value)
-  /// into an absolute symbol address by adding the address of the
-  /// section the symbol belongs to, to the address value.
-  ///
-  /// This is useful when looking at symbol values coming from
-  /// relocatable files (of ET_REL kind).  If the binary is not
-  /// ET_REL, then the function does nothing and returns the input
-  /// address unchanged.
-  ///
-  /// @param sym the symbol whose address to possibly needs to be
-  /// translated.
-  ///
-  /// @return the section-relative address, translated into an
-  /// absolute address, if @p sym is from an ET_REL binary.
-  /// Otherwise, return the address of @p sym, unchanged.
-  GElf_Addr
-  maybe_adjust_et_rel_sym_addr_to_abs_addr(GElf_Sym *sym)
-  {
-    Elf_Scn *symbol_section = elf_getscn(elf_handle(), sym->st_shndx);
-    GElf_Addr result = sym->st_value;
-    result = maybe_adjust_et_rel_sym_addr_to_abs_addr(result, symbol_section);
-    return result;
-  }
-
   /// Test if a given address is in a given section.
   ///
   /// @param addr the address to consider.
diff --git a/src/abg-elf-helpers.cc b/src/abg-elf-helpers.cc
index 895feb6f7768..02ecbcf250ed 100644
--- a/src/abg-elf-helpers.cc
+++ b/src/abg-elf-helpers.cc
@@ -987,5 +987,45 @@  is_dso(Elf* elf_handle)
   return elf_header.e_type == ET_DYN;
 }
 
+/// Translate a section-relative symbol address (i.e, symbol value)
+/// into an absolute symbol address by adding the address of the
+/// section the symbol belongs to, to the address value.
+///
+/// This is useful when looking at symbol values coming from
+/// relocatable files (of ET_REL kind).  If the binary is not
+/// ET_REL, then the function does nothing and returns the input
+/// address unchanged.
+///
+/// @param elf_handle the elf handle for the binary to consider.
+///
+/// @param sym the symbol whose address to possibly needs to be
+/// translated.
+///
+/// @return the section-relative address, translated into an
+/// absolute address, if @p sym is from an ET_REL binary.
+/// Otherwise, return the address of @p sym, unchanged.
+GElf_Addr
+maybe_adjust_et_rel_sym_addr_to_abs_addr(Elf* elf_handle, GElf_Sym* sym)
+{
+  Elf_Scn*  symbol_section = elf_getscn(elf_handle, sym->st_shndx);
+  GElf_Addr addr = sym->st_value;
+
+  if (!symbol_section)
+    return addr;
+
+  GElf_Ehdr elf_header;
+  if (!gelf_getehdr(elf_handle, &elf_header))
+    return addr;
+
+  if (elf_header.e_type != ET_REL)
+    return addr;
+
+  GElf_Shdr section_header;
+  if (!gelf_getshdr(symbol_section, &section_header))
+    return addr;
+
+  return addr + section_header.sh_addr;
+}
+
 } // end namespace elf_helpers
 } // end namespace abigail
diff --git a/src/abg-elf-helpers.h b/src/abg-elf-helpers.h
index 6eff245a6a30..647c92703dfa 100644
--- a/src/abg-elf-helpers.h
+++ b/src/abg-elf-helpers.h
@@ -174,6 +174,9 @@  is_executable(Elf* elf_handle);
 bool
 is_dso(Elf* elf_handle);
 
+GElf_Addr
+maybe_adjust_et_rel_sym_addr_to_abs_addr(Elf* elf_handle, GElf_Sym* sym);
+
 } // end namespace elf_helpers
 } // end namespace abigail