]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
GAS: Unify code for SET_SECTION_RELOCS call
authorMaciej W. Rozycki <macro@orcam.me.uk>
Wed, 14 Jan 2026 22:28:43 +0000 (22:28 +0000)
committerMaciej W. Rozycki <macro@orcam.me.uk>
Wed, 14 Jan 2026 22:28:43 +0000 (22:28 +0000)
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.

gas/config/obj-coff.c
gas/config/obj-coff.h
gas/doc/internals.texi
gas/write.c

index 91ec08d812b6022ef7a8a626a5330be71b3794eb..3972951c2984fffa9adad68ebb36a0384048e658 100644 (file)
@@ -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 b45e7e58bff728a2b02820199391e6ac8cc42d61..0e64a8aed3f372fe8ede023f986781ff860d7abf 100644 (file)
@@ -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 d3717c963badf223fc7b916d128c149e1958fc08..981f864fce8f2734b2f3273b7a82dec46ddeced8 100644 (file)
@@ -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 d92bcf5c613eaa772e6d24010e8641d43647317d..8869461e3059c1e2dcb925e4e7a37a1306c8ee71 100644 (file)
 #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, asection *sec,
   }
 #endif
 
-  bfd_set_reloc (stdoutput, sec, n ? relocs : NULL, n);
-
-#ifdef SET_SECTION_RELOCS
   SET_SECTION_RELOCS (sec, relocs, n);
-#endif
 
 #ifdef DEBUG3
   {