public inbox for libabigail@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/2] symtab: refactor ELF symbol value tweaks
@ 2022-04-07 16:29 Giuliano Procida
  2022-04-07 16:29 ` [PATCH 2/2] symtab: fix up 64-bit ARM address which may contain tags Giuliano Procida
  2022-05-04 10:00 ` [PATCH 1/2] symtab: refactor ELF symbol value tweaks Dodji Seketeli
  0 siblings, 2 replies; 4+ messages in thread
From: Giuliano Procida @ 2022-04-07 16:29 UTC (permalink / raw)
  To: libabigail; +Cc: dodji, kernel-team, gprocida, maennich, willdeacon

A previous changes duplicated some logic for tweaking ELF symbol
values (and possibly updating some bookkeeping information).

This change refactors the code so the logic is in one place, in
symtab::get_symbol_value.

	* src/abg-symtab-reader.cc (symtab::load_): Replace address
	tweaking logic with a call to get_symbol_value.
	(symtab::add_alternative_address_lookups): Likewise.
	(symtab::get_symbol_value): New function containing address
	tweaking logic for PPC and ARM.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 src/abg-symtab-reader.cc | 81 ++++++++++++++++++++--------------------
 src/abg-symtab-reader.h  |  5 +++
 2 files changed, 46 insertions(+), 40 deletions(-)

diff --git a/src/abg-symtab-reader.cc b/src/abg-symtab-reader.cc
index b42ce87d..026988ee 100644
--- a/src/abg-symtab-reader.cc
+++ b/src/abg-symtab-reader.cc
@@ -236,9 +236,6 @@ symtab::load_(Elf*	       elf_handle,
   std::unordered_set<std::string> exported_kernel_symbols;
   std::unordered_map<std::string, uint64_t> crc_values;
 
-  const bool is_arm32 = elf_helpers::architecture_is_arm32(elf_handle);
-  const bool is_ppc64 = elf_helpers::architecture_is_ppc64(elf_handle);
-
   for (size_t i = 0; i < number_syms; ++i)
     {
       GElf_Sym *sym, sym_mem;
@@ -347,23 +344,8 @@ symtab::load_(Elf*	       elf_handle,
 	}
       else if (symbol_sptr->is_defined())
 	{
-	  GElf_Addr symbol_value =
-	      elf_helpers::maybe_adjust_et_rel_sym_addr_to_abs_addr(elf_handle,
-								    sym);
-
-	  // See also symtab::add_alternative_address_lookups.
-	  if (symbol_sptr->is_function())
-	    {
-	      if (is_arm32)
-		// Clear bit zero of ARM32 addresses as per "ELF for the Arm
-		// Architecture" section 5.5.3.
-		// https://static.docs.arm.com/ihi0044/g/aaelf32.pdf
-		symbol_value &= ~1;
-	      else if (is_ppc64)
-		update_function_entry_address_symbol_map(elf_handle, sym,
-							 symbol_sptr);
-	    }
-
+	  const GElf_Addr symbol_value =
+	    get_symbol_value(elf_handle, sym, symbol_sptr);
 	  const auto result =
 	    addr_symbol_map_.emplace(symbol_value, symbol_sptr);
 	  if (!result.second)
@@ -483,6 +465,43 @@ symtab::update_main_symbol(GElf_Addr addr, const std::string& name)
     addr_symbol_map_[addr] = new_main;
 }
 
+/// Various adjustments and bookkeeping may be needed to provide a correct
+/// interpretation (one that matches DWARF addresses) of raw symbol values.
+///
+/// @param elf_handle the ELF handle
+///
+/// @param elf_symbol the ELF symbol
+///
+/// @param symbol_sptr the libabigail symbol
+///
+/// @return a possibly-adjusted symbol value
+GElf_Addr
+symtab::get_symbol_value(Elf* elf_handle,
+			 GElf_Sym* elf_symbol,
+			 const elf_symbol_sptr& symbol_sptr)
+{
+  const bool is_arm32 = elf_helpers::architecture_is_arm32(elf_handle);
+  const bool is_ppc64 = elf_helpers::architecture_is_ppc64(elf_handle);
+
+  GElf_Addr symbol_value =
+    elf_helpers::maybe_adjust_et_rel_sym_addr_to_abs_addr(elf_handle,
+							  elf_symbol);
+
+  if (symbol_sptr->is_function())
+    {
+      if (is_arm32)
+	// Clear bit zero of ARM32 addresses as per "ELF for the Arm
+	// Architecture" section 5.5.3.
+	// https://static.docs.arm.com/ihi0044/g/aaelf32.pdf
+	symbol_value &= ~1;
+      else if (is_ppc64)
+	update_function_entry_address_symbol_map(elf_handle, elf_symbol,
+						 symbol_sptr);
+    }
+
+  return symbol_value;
+}
+
 /// Update the function entry symbol map to later allow lookups of this symbol
 /// by entry address as well. This is relevant for ppc64 ELFv1 binaries.
 ///
@@ -582,9 +601,6 @@ symtab::update_function_entry_address_symbol_map(
 void
 symtab::add_alternative_address_lookups(Elf* elf_handle)
 {
-  const bool is_arm32 = elf_helpers::architecture_is_arm32(elf_handle);
-  const bool is_ppc64 = elf_helpers::architecture_is_ppc64(elf_handle);
-
   Elf_Scn* symtab_section = elf_helpers::find_symtab_section(elf_handle);
   if (!symtab_section)
     return;
@@ -634,23 +650,8 @@ symtab::add_alternative_address_lookups(Elf* elf_handle)
 	  if (symbols.size() == 1)
 	    {
 	      const auto& symbol_sptr = symbols[0];
-	      GElf_Addr	  symbol_value =
-		  elf_helpers::maybe_adjust_et_rel_sym_addr_to_abs_addr(
-		      elf_handle, sym);
-
-	      // See also symtab::load_.
-	      if (symbol_sptr->is_function())
-		{
-		  if (is_arm32)
-		    // Clear bit zero of ARM32 addresses as per "ELF for the Arm
-		    // Architecture" section 5.5.3.
-		    // https://static.docs.arm.com/ihi0044/g/aaelf32.pdf
-		    symbol_value &= ~1;
-		  else if (is_ppc64)
-		    update_function_entry_address_symbol_map(elf_handle, sym,
-							     symbol_sptr);
-		}
-
+	      const GElf_Addr symbol_value =
+		get_symbol_value(elf_handle, sym, symbol_sptr);
 	      addr_symbol_map_.emplace(symbol_value, symbol_sptr);
 	    }
 	}
diff --git a/src/abg-symtab-reader.h b/src/abg-symtab-reader.h
index 7ac15352..bddde2f6 100644
--- a/src/abg-symtab-reader.h
+++ b/src/abg-symtab-reader.h
@@ -289,6 +289,11 @@ private:
   load_(string_elf_symbols_map_sptr function_symbol_map,
        string_elf_symbols_map_sptr variables_symbol_map);
 
+  GElf_Addr
+  get_symbol_value(Elf* elf_handle,
+		   GElf_Sym* elf_symbol,
+		   const elf_symbol_sptr& symbol_sptr);
+
   void
   update_function_entry_address_symbol_map(Elf*	     elf_handle,
 					   GElf_Sym* native_symbol,
-- 
2.35.1.1094.g7c7d902a7c-goog


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

* [PATCH 2/2] symtab: fix up 64-bit ARM address which may contain tags
  2022-04-07 16:29 [PATCH 1/2] symtab: refactor ELF symbol value tweaks Giuliano Procida
@ 2022-04-07 16:29 ` Giuliano Procida
  2022-05-04 10:05   ` Dodji Seketeli
  2022-05-04 10:00 ` [PATCH 1/2] symtab: refactor ELF symbol value tweaks Dodji Seketeli
  1 sibling, 1 reply; 4+ messages in thread
From: Giuliano Procida @ 2022-04-07 16:29 UTC (permalink / raw)
  To: libabigail; +Cc: dodji, kernel-team, gprocida, maennich, willdeacon

64-bit ARM addresses normally have bits 47 to 63 as either all 0 or
all 1.  If tagging is used, bits 56 to 63 can vary, but the
interpretation of such values is as if the bits were all the same as
bit 55.

Such tagging is used for HWASAN and this affects the ELF symbol values
seen in shared libraries.

This commit changes the interpretation of 64-bit ARM symbol values by
unconditionally extending bit 55 into bits 56 to 63.

This fixes missing types for symbols in HWASAN-compiled libraries.

	* src/abg-elf-helpers.cc: (architecture_is_arm64): Add helper.
	* src/abg-elf-helpers.h: Likewise.
	* src/abg-symtab-reader.cc: (get_symbol_value): Adjust 64-bit
	ARM symbol values by extending bit 55 into bits 56 to 63.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 src/abg-elf-helpers.cc   | 17 +++++++++++++++++
 src/abg-elf-helpers.h    |  3 +++
 src/abg-symtab-reader.cc |  6 ++++++
 3 files changed, 26 insertions(+)

diff --git a/src/abg-elf-helpers.cc b/src/abg-elf-helpers.cc
index ee631831..787a05ff 100644
--- a/src/abg-elf-helpers.cc
+++ b/src/abg-elf-helpers.cc
@@ -900,6 +900,23 @@ architecture_is_arm32(Elf* elf_handle)
   return (elf_header && elf_header->e_machine == EM_ARM);
 }
 
+/// Test if the architecture of the current binary is arm64.
+///
+/// @param elf_handle the ELF handle to consider.
+///
+/// @return true iff the architecture of the current binary is arm64.
+bool
+architecture_is_arm64(Elf* elf_handle)
+{
+#ifdef HAVE_EM_AARCH64_MACRO
+  GElf_Ehdr  eh_mem;
+  GElf_Ehdr* elf_header = gelf_getehdr(elf_handle, &eh_mem);
+  return (elf_header && elf_header->e_machine == EM_AARCH64);
+#else
+  return false;
+#endif
+}
+
 /// Test if the endianness of the current binary is Big Endian.
 ///
 /// https://en.wikipedia.org/wiki/Endianness.
diff --git a/src/abg-elf-helpers.h b/src/abg-elf-helpers.h
index 718ce9c1..afaff24a 100644
--- a/src/abg-elf-helpers.h
+++ b/src/abg-elf-helpers.h
@@ -147,6 +147,9 @@ architecture_is_ppc64(Elf* elf_handle);
 bool
 architecture_is_arm32(Elf* elf_handle);
 
+bool
+architecture_is_arm64(Elf* elf_handle);
+
 bool
 architecture_is_big_endian(Elf* elf_handle);
 
diff --git a/src/abg-symtab-reader.cc b/src/abg-symtab-reader.cc
index 026988ee..3740cb7a 100644
--- a/src/abg-symtab-reader.cc
+++ b/src/abg-symtab-reader.cc
@@ -481,6 +481,7 @@ symtab::get_symbol_value(Elf* elf_handle,
 			 const elf_symbol_sptr& symbol_sptr)
 {
   const bool is_arm32 = elf_helpers::architecture_is_arm32(elf_handle);
+  const bool is_arm64 = elf_helpers::architecture_is_arm64(elf_handle);
   const bool is_ppc64 = elf_helpers::architecture_is_ppc64(elf_handle);
 
   GElf_Addr symbol_value =
@@ -498,6 +499,11 @@ symtab::get_symbol_value(Elf* elf_handle,
 	update_function_entry_address_symbol_map(elf_handle, elf_symbol,
 						 symbol_sptr);
     }
+  if (is_arm64)
+    // Copy bit 55 over bits 56 to 63 which may be tag information.
+    symbol_value = symbol_value & (1ULL<<55)
+		   ? symbol_value | (0xffULL<<56)
+		   : symbol_value &~ (0xffULL<<56);
 
   return symbol_value;
 }
-- 
2.35.1.1094.g7c7d902a7c-goog


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

* Re: [PATCH 1/2] symtab: refactor ELF symbol value tweaks
  2022-04-07 16:29 [PATCH 1/2] symtab: refactor ELF symbol value tweaks Giuliano Procida
  2022-04-07 16:29 ` [PATCH 2/2] symtab: fix up 64-bit ARM address which may contain tags Giuliano Procida
@ 2022-05-04 10:00 ` Dodji Seketeli
  1 sibling, 0 replies; 4+ messages in thread
From: Dodji Seketeli @ 2022-05-04 10:00 UTC (permalink / raw)
  To: Giuliano Procida; +Cc: libabigail, kernel-team, maennich, willdeacon

Hello Giuliano,

Giuliano Procida <gprocida@google.com> a écrit:

> A previous changes duplicated some logic for tweaking ELF symbol
> values (and possibly updating some bookkeeping information).
>
> This change refactors the code so the logic is in one place, in
> symtab::get_symbol_value.
>
> 	* src/abg-symtab-reader.cc (symtab::load_): Replace address
> 	tweaking logic with a call to get_symbol_value.
> 	(symtab::add_alternative_address_lookups): Likewise.
> 	(symtab::get_symbol_value): New function containing address
> 	tweaking logic for PPC and ARM.
>
> Signed-off-by: Giuliano Procida <gprocida@google.com>

Applied to master, thanks!

[...]

Cheers,

-- 
		Dodji

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

* Re: [PATCH 2/2] symtab: fix up 64-bit ARM address which may contain tags
  2022-04-07 16:29 ` [PATCH 2/2] symtab: fix up 64-bit ARM address which may contain tags Giuliano Procida
@ 2022-05-04 10:05   ` Dodji Seketeli
  0 siblings, 0 replies; 4+ messages in thread
From: Dodji Seketeli @ 2022-05-04 10:05 UTC (permalink / raw)
  To: Giuliano Procida; +Cc: libabigail, kernel-team, maennich, willdeacon

Hello Giuliano,

Giuliano Procida <gprocida@google.com> a écrit:

> 64-bit ARM addresses normally have bits 47 to 63 as either all 0 or
> all 1.  If tagging is used, bits 56 to 63 can vary, but the
> interpretation of such values is as if the bits were all the same as
> bit 55.
>
> Such tagging is used for HWASAN and this affects the ELF symbol values
> seen in shared libraries.
>
> This commit changes the interpretation of 64-bit ARM symbol values by
> unconditionally extending bit 55 into bits 56 to 63.
>
> This fixes missing types for symbols in HWASAN-compiled libraries.
>
> 	* src/abg-elf-helpers.cc: (architecture_is_arm64): Add helper.
> 	* src/abg-elf-helpers.h: Likewise.
> 	* src/abg-symtab-reader.cc: (get_symbol_value): Adjust 64-bit
> 	ARM symbol values by extending bit 55 into bits 56 to 63.
>
> Signed-off-by: Giuliano Procida <gprocida@google.com>

Applied to master, thanks!

[...]

Cheers,

-- 
		Dodji

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

end of thread, other threads:[~2022-05-04 10:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-07 16:29 [PATCH 1/2] symtab: refactor ELF symbol value tweaks Giuliano Procida
2022-04-07 16:29 ` [PATCH 2/2] symtab: fix up 64-bit ARM address which may contain tags Giuliano Procida
2022-05-04 10:05   ` Dodji Seketeli
2022-05-04 10:00 ` [PATCH 1/2] symtab: refactor ELF symbol value tweaks 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).