[v7,1/3] Adjust pdata function table entries sorting for AArch64

Message ID 20260319111638.93074-2-evgeny.karpov@arm.com
State New
Headers
Series Implement Structured Exception Handling (SEH) on AArch64 |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Evgeny Karpov March 19, 2026, 11:16 a.m. UTC
  The .pdata section contains an array of function table entries that
are used for exception handling. The entries should be sorted by
begin address, which is usually the first 4 bytes RVA in the entry.
Entry sizes are different for x64 and AArch64.
This difference is addressed in this patch.

This is the first patch in the patch series implementing
Structured Exception Handling (SEH) for aarch64-w64-mingw32.

Co-authored-by: Zac Walker <zacwalker@microsoft.com>
Co-authored-by: Ron Riddle <ron.riddle@microsoft.com>
Signed-off-by: Evgeny Karpov <evgeny@kmaps.co>

bfd/ChangeLog:

	* peXXigen.c (sort_x64_pdata): Rename from this ...
	(sort_pdata): ... to this.
	(defined): Use function_table_entry_size.
---
 bfd/peXXigen.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
  

Patch

diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c
index 2ab0dd50bf4..bdb23dcbcab 100644
--- a/bfd/peXXigen.c
+++ b/bfd/peXXigen.c
@@ -3169,7 +3169,7 @@  _bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
 
 #if !defined(COFF_WITH_pep) && (defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64) || defined(COFF_WITH_peLoongArch64) || defined (COFF_WITH_peRiscV64))
 static int
-sort_x64_pdata (const void *l, const void *r)
+sort_pdata (const void *l, const void *r)
 {
   const char *lp = (const char *) l;
   const char *rp = (const char *) r;
@@ -4724,9 +4724,21 @@  _bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
 
 	if (bfd_malloc_and_get_section (abfd, sec, &tmp_data))
 	  {
+	    /* The size of a .pdata entry that describes a function that is used
+	       for exception handling.  */
+	    unsigned function_table_entry_size;
+#if defined (COFF_WITH_peAArch64)
+	    /* https://learn.microsoft.com/en-us/cpp/build/arm64-exception-handling#pdata-records.  */
+	    function_table_entry_size = 8;
+#else
+	    /* https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#the-pdata-section.  */
+	    function_table_entry_size = 12;
+#endif
+	    /* .pdata entries should be sorted by the function start
+	       address.  */
 	    qsort (tmp_data,
-		   (size_t) (x / 12),
-		   12, sort_x64_pdata);
+		   (size_t) (x / function_table_entry_size),
+		   function_table_entry_size, sort_pdata);
 	    bfd_set_section_contents (pfinfo->output_bfd, sec,
 				      tmp_data, 0, x);
 	    free (tmp_data);