[PATCH v3 02/10] GAS: Unify code for SET_SECTION_RELOCS call

Maciej W. Rozycki macro@orcam.me.uk
Tue Dec 9 12:40:06 GMT 2025
Fold a separate call to `bfd_set_reloc' into SET_SECTION_RELOCS itself, 
so that the GAS interface to this facility is contained in a single 
invocation.

Currently both `write_relocs' and `obj_mach_o_reorder_section_relocs' 
call `bfd_set_reloc', causing the function to be called twice by Mach-O 
targets, such as `i386-darwin', once before target-specific processing 
and again afterwards, which is at the very least fragile in terms of 
assuming that any actions made by the function on the first invocation 
won't interfere with the final intended result.

Set the macro by default to a plain call to `bfd_set_reloc', letting 
backends override the macro, with the requirement now to factor in a 
call to said function at the appropriate time.  Backends can choose 
whether to call `bfd_set_reloc' first (such as COFF), or last (such as 
Mach-O), or at any other point in relation to their own additional 
actions.

Update the COFF variant accordingly, moving it to a new function for a 
better code structure, retaining functionality.

This is in preparation for `bfd_set_reloc' to return an error status.
---
Changes from v2:

- Update the commit description with additional clarifications.

New change in v2.
---
 gas/config/obj-coff.c  |   18 ++++++++++++++++++
 gas/config/obj-coff.h  |   16 +++-------------
 gas/doc/internals.texi |    6 +++---
 gas/write.c            |    9 +++++----
 4 files changed, 29 insertions(+), 20 deletions(-)

binutils-gas-set-section-relocs-once.diff
Index: binutils-gdb/gas/config/obj-coff.c
===================================================================
--- binutils-gdb.orig/gas/config/obj-coff.c
+++ binutils-gdb/gas/config/obj-coff.c
@@ -1514,6 +1514,24 @@ coff_frob_file_after_relocs (void)
   bfd_map_over_sections (stdoutput, coff_adjust_section_syms, NULL);
 }
 
+/* Set relocations for the section and then store the number of relocations
+   in its aux entry.  */
+
+void
+obj_coff_set_section_relocs (asection *sec, arelent **relocs, unsigned int n)
+{
+  symbolS *sect_sym;
+
+  bfd_set_reloc (stdoutput, sec, n ? relocs : NULL, n);
+  sect_sym = section_symbol (sec);
+#ifdef OBJ_XCOFF
+  if (S_GET_STORAGE_CLASS (sect_sym) == C_DWARF)
+    SA_SET_SECT_NRELOC (sect_sym, n);
+  else
+#endif
+    SA_SET_SCN_NRELOC (sect_sym, n);
+}
+
 /* Implement the .section pseudo op:
   	.section name {, "flags"}
                   ^         ^
Index: binutils-gdb/gas/config/obj-coff.h
===================================================================
--- binutils-gdb.orig/gas/config/obj-coff.h
+++ binutils-gdb/gas/config/obj-coff.h
@@ -293,20 +293,10 @@ extern void coff_pop_insert (void);
    information.  */
 #define INIT_STAB_SECTION(stab, str) obj_coff_init_stab_section (stab, str)
 
-/* Store the number of relocations in the section aux entry.  */
-#ifdef OBJ_XCOFF
-#define SET_SECTION_RELOCS(sec, relocs, n)		\
-  do {							\
-    symbolS * sectSym = section_symbol (sec);		\
-    if (S_GET_STORAGE_CLASS (sectSym) == C_DWARF)	\
-      SA_SET_SECT_NRELOC (sectSym, n);			\
-    else						\
-      SA_SET_SCN_NRELOC (sectSym, n);			\
-  } while (0)
-#else
+/* We need to store the number of relocations in the section aux entry.  */
 #define SET_SECTION_RELOCS(sec, relocs, n) \
-  SA_SET_SCN_NRELOC (section_symbol (sec), n)
-#endif
+  obj_coff_set_section_relocs (sec, relocs, n)
+extern void obj_coff_set_section_relocs (asection *, arelent **, unsigned int);
 
 extern int  S_SET_DATA_TYPE              (symbolS *, int);
 extern int  S_SET_STORAGE_CLASS          (symbolS *, int);
Index: binutils-gdb/gas/doc/internals.texi
===================================================================
--- binutils-gdb.orig/gas/doc/internals.texi
+++ binutils-gdb/gas/doc/internals.texi
@@ -1699,9 +1699,9 @@ generated.
 
 @item SET_SECTION_RELOCS (@var{sec}, @var{relocs}, @var{n})
 @cindex SET_SECTION_RELOCS
-If you define this, it will be called after the relocations have been set for
-the section @var{sec}.  The list of relocations is in @var{relocs}, and the
-number of relocations is in @var{n}.
+If you define this, it will be called to set relocations for the section
+@var{sec}.  The list of relocations is in @var{relocs}, and the number of
+relocations is in @var{n}.
 @end table
 
 @node Emulations
Index: binutils-gdb/gas/write.c
===================================================================
--- binutils-gdb.orig/gas/write.c
+++ binutils-gdb/gas/write.c
@@ -28,6 +28,11 @@
 #include "compress-debug.h"
 #include "codeview.h"
 
+#ifndef SET_SECTION_RELOCS
+#define SET_SECTION_RELOCS(sec, relocs, n)	\
+  bfd_set_reloc (stdoutput, sec, n ? relocs : NULL, n)
+#endif
+
 #ifndef TC_FORCE_RELOCATION
 #define TC_FORCE_RELOCATION(FIX)		\
   (generic_force_reloc (FIX))
@@ -1414,11 +1419,7 @@ write_relocs (bfd *abfd ATTRIBUTE_UNUSED
   }
 #endif
 
-  bfd_set_reloc (stdoutput, sec, n ? relocs : NULL, n);
-
-#ifdef SET_SECTION_RELOCS
   SET_SECTION_RELOCS (sec, relocs, n);
-#endif
 
 #ifdef DEBUG3
   {


More information about the Binutils mailing list