From: Maciej W. Rozycki Date: Wed, 14 Jan 2026 22:28:43 +0000 (+0000) Subject: GAS: Unify code for SET_SECTION_RELOCS call X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b45e8563ca01f874706fba05688fd683720b7003;p=thirdparty%2Fbinutils-gdb.git GAS: Unify code for SET_SECTION_RELOCS call 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. --- diff --git a/gas/config/obj-coff.c b/gas/config/obj-coff.c index 91ec08d812b..3972951c298 100644 --- a/gas/config/obj-coff.c +++ b/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"} ^ ^ diff --git a/gas/config/obj-coff.h b/gas/config/obj-coff.h index b45e7e58bff..0e64a8aed3f 100644 --- a/gas/config/obj-coff.h +++ b/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); diff --git a/gas/doc/internals.texi b/gas/doc/internals.texi index d3717c963ba..981f864fce8 100644 --- a/gas/doc/internals.texi +++ b/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 diff --git a/gas/write.c b/gas/write.c index d92bcf5c613..8869461e305 100644 --- a/gas/write.c +++ b/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, 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 {