Bug 33885 - gas: Add an option to disable section symbol adjustment
Summary: gas: Add an option to disable section symbol adjustment
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: gas (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: 2.47
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-02-08 08:09 UTC by Fangrui Song
Modified: 2026-07-05 20:01 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2026-02-08 08:09:51 UTC
When the assembler generates an unresolved fixup for a local symbol, it can convert the relocation to reference the section symbol (`STT_SECTION`) instead, folding the original symbol's offset within the section into the addend.
This allows the original local symbol to be omitted from `.symtab`.
The tradeoff is that the `STT_SECTION` symbol itself must be present, so the conversion saves `.symtab` entries only when a section has more than one local symbol referenced by relocations.
This is common in practice:

* Text sections often contain labels for jump targets or C++ exception handling.
* DWARF `.debug_*` sections contain labels referenced by other `.debug_*` sections.
* `SHF_STRINGS` sections (`.rodata.str1.1`, `.debug_str`, `.debug_line_str`) have a label for each string literal.

(
Not all relocations are eligible for this conversion.
PLT-generating and GOT-generating relocations, for example, may require dynamic relocations where the symbol identity is significant, so they must reference the original symbol.
In GNU Assembler, the backend hook `tc_fix_adjustable` controls which relocation types are excluded from the conversion.)


In certain scenarios, disabling the STT_SECTION adjustment is preferable.
For instance, when range extension thunks for x86-64 are available, retaining the original foo0 and foo1 symbols in R_X86_64_PLT32 relocations allows the linker to generate descriptive thunk names like __X86_64CallThunk_foo0
With the unnamed section symbol, the linker cannot easily generate a descriptive name (if clang is used with -ffunction-sections and -fno-unique-section-names, the section will be .text instead of .text.foo0).

call foo0
call foo1

.section .text.foo0,"ax"
foo0:

.section .text.foo1,"ax"
foo1:

(In lld/ELF's --branch-to-branch optimization https://github.com/llvm/llvm-project/pull/145579/ , it would be simpler if no assembler generated R_X86_64_PLT32 referencing STT_SECTION symbol.)
Comment 1 Fangrui Song 2026-02-08 08:13:33 UTC
Added the write-up to https://maskray.me/blog/2025-03-16-relocation-generation-in-assemblers#section-symbol-adjustment

Candidate option name: --adjust-reloc-syms={none,nonlocal,all}

* all: adjust all eligible relocations to reference STT_SECTION
* nonlocal: adjust eligible relocations that reference a non-local symbol
* none: don't adjust relocations
Comment 2 Sourceware Commits 2026-07-05 15:19:36 UTC
The master branch has been updated by Fangrui Song <maskray@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=eec25cabd2d004cfc2a9898b1ec93cbdef2fcd77

commit eec25cabd2d004cfc2a9898b1ec93cbdef2fcd77
Author: Fangrui Song <maskray@sourceware.org>
Date:   Sun Feb 8 11:56:49 2026 -0800

    gas: add --reloc-section-sym={all,internal,none} option for ELF
    
    When generating relocations for non-ifunc local symbols that satisfies
    several conditions, GAS converts them to reference the section symbol
    (STT_SECTION) instead, folding the original symbol's offset into the
    addend.  This allows the original local symbol to be omitted from
    .symtab, but the STT_SECTION symbol itself must be present, so the
    conversion saves .symtab entries only when a section has more than one
    local symbol referenced by relocations.
    
    Add --reloc-section-sym to control this conversion:
    
    - all (default): convert all eligible local symbols
    - internal: only convert compiler-generated locals (.L prefix)
    - none: never convert; keep all symbols as-is in relocations
    
    This is useful for debugging and for tools that benefit from preserved
    symbol names.
    
    PR gas/33885
Comment 3 Sam James 2026-07-05 20:01:18 UTC
Fixed for 2.47.