[llvm] [MC] Add --reloc-use-section-sym option to control section symbol conversion (PR #182725)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 21 21:20:49 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-mc
Author: Fangrui Song (MaskRay)
<details>
<summary>Changes</summary>
When generating relocations for non-ifunc local symbols that satisfies
several conditions, the integrated assembler 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.
Similar to recent GAS addition, add --reloc-section-sym to control this
conversion:
- all (default): convert all eligible local symbols.
(Note: There are many ineligible conditions, see ELFObjectWriter::useSectionSymbol)
- internal: only convert .L prefix local symbols (usually compiler-generated)
- none: never convert; keep all symbols as-is in relocations
This is useful for debugging and for tools that benefit from preserved
symbol names.
Original feature request:
<https://discourse.llvm.org/t/mc-how-why-does-converting-relocation-against-symbol-to-relocation-against-section-work/88786/7>
Note: We have to use name check instead of `isTemporary()`, because
--save-temp-labels makes all symbols non-temporary.
---
Full diff: https://github.com/llvm/llvm-project/pull/182725.diff
5 Files Affected:
- (modified) llvm/include/llvm/MC/MCTargetOptions.h (+11)
- (modified) llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h (+3)
- (modified) llvm/lib/MC/ELFObjectWriter.cpp (+8-1)
- (modified) llvm/lib/MC/MCTargetOptionsCommandFlags.cpp (+15)
- (added) llvm/test/MC/ELF/reloc-section-sym.s (+60)
``````````diff
diff --git a/llvm/include/llvm/MC/MCTargetOptions.h b/llvm/include/llvm/MC/MCTargetOptions.h
index 235d58d585b40..2f24608b5f6ba 100644
--- a/llvm/include/llvm/MC/MCTargetOptions.h
+++ b/llvm/include/llvm/MC/MCTargetOptions.h
@@ -24,6 +24,14 @@ enum class EmitDwarfUnwindType {
Default, // Default behavior is based on the target
};
+// For ELF targets, whether to adjust relocations referencing eligible local
+// symbols to use section symbols.
+enum class RelocSectionSymType {
+ All, // For all eligible local symbols (default)
+ Internal, // For .L symbols
+ None, // Never use section symbols
+};
+
class StringRef;
class MCTargetOptions {
@@ -62,6 +70,9 @@ class MCTargetOptions {
bool X86Sse2Avx = false;
+ // For ELF relocations, controls section symbol conversion.
+ RelocSectionSymType RelocSectionSym = RelocSectionSymType::All;
+
std::optional<unsigned> OutputAsmVariant;
EmitDwarfUnwindType EmitDwarfUnwind;
diff --git a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
index 168131b43cca8..ae80887900349 100644
--- a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
+++ b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
@@ -21,6 +21,7 @@
namespace llvm {
class MCTargetOptions;
+enum class RelocSectionSymType;
enum class EmitDwarfUnwindType;
class StringRef;
@@ -63,6 +64,8 @@ LLVM_ABI bool getX86RelaxRelocations();
LLVM_ABI bool getX86Sse2Avx();
+LLVM_ABI RelocSectionSymType getRelocSectionSym();
+
LLVM_ABI StringRef getABIName();
LLVM_ABI StringRef getAsSecureLogFile();
diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index 3d7f503bf08b0..d3e8acf9ecaaf 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -1358,9 +1358,16 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F,
// Convert SymA to an STT_SECTION symbol if it's defined, local, and meets
// specific conditions, unless it's a .reloc directive, which disables
// STT_SECTION adjustment.
+ const MCTargetOptions *TO = Ctx.getTargetOptions();
bool UseSectionSym = SymA && SymA->getBinding() == ELF::STB_LOCAL &&
!SymA->isUndefined() &&
!mc::isRelocRelocation(Fixup.getKind());
+ if (UseSectionSym) {
+ auto RSS = TO ? TO->RelocSectionSym : RelocSectionSymType::All;
+ UseSectionSym = RSS == RelocSectionSymType::All ||
+ (RSS == RelocSectionSymType::Internal &&
+ SymA->getName().starts_with(".L"));
+ }
if (UseSectionSym && useSectionSymbol(Target, SymA, Addend, Type)) {
Addend += Asm->getSymbolOffset(*SymA);
SymA = static_cast<const MCSymbolELF *>(SecA->getBeginSymbol());
@@ -1370,7 +1377,7 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F,
if (SymA)
SymA->setUsedInReloc();
- FixedValue = usesRela(Ctx.getTargetOptions(), Section) ? 0 : Addend;
+ FixedValue = usesRela(TO, Section) ? 0 : Addend;
Relocations[&Section].emplace_back(FixupOffset, SymA, Type, Addend);
}
diff --git a/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
index 22494fa11eb2a..359c1bceb25b1 100644
--- a/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
+++ b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
@@ -59,6 +59,7 @@ MCOPT(bool, Crel)
MCOPT(bool, ImplicitMapSyms)
MCOPT(bool, X86RelaxRelocations)
MCOPT(bool, X86Sse2Avx)
+MCOPT(RelocSectionSymType, RelocSectionSym)
MCSTROPT(ABIName)
MCSTROPT(AsSecureLogFile)
@@ -168,6 +169,19 @@ llvm::mc::RegisterMCTargetOptionsFlags::RegisterMCTargetOptionsFlags() {
"instructions with VEX prefix"));
MCBINDOPT(X86Sse2Avx);
+ static cl::opt<RelocSectionSymType> RelocSectionSym(
+ "reloc-section-sym",
+ cl::desc("Control section symbol conversion for relocations"),
+ cl::init(RelocSectionSymType::All),
+ cl::values(
+ clEnumValN(RelocSectionSymType::All, "all",
+ "Use section symbols for all eligible local symbols"),
+ clEnumValN(RelocSectionSymType::Internal, "internal",
+ "Only use section symbols for internal local symbols"),
+ clEnumValN(RelocSectionSymType::None, "none",
+ "Never use section symbols")));
+ MCBINDOPT(RelocSectionSym);
+
static cl::opt<std::string> ABIName(
"target-abi",
cl::desc("The name of the ABI to be targeted from the backend."),
@@ -199,6 +213,7 @@ MCTargetOptions llvm::mc::InitMCTargetOptionsFromFlags() {
Options.ImplicitMapSyms = getImplicitMapSyms();
Options.X86RelaxRelocations = getX86RelaxRelocations();
Options.X86Sse2Avx = getX86Sse2Avx();
+ Options.RelocSectionSym = getRelocSectionSym();
Options.EmitDwarfUnwind = getEmitDwarfUnwind();
Options.EmitCompactUnwindNonCanonical = getEmitCompactUnwindNonCanonical();
Options.EmitSFrameUnwind = getEmitSFrameUnwind();
diff --git a/llvm/test/MC/ELF/reloc-section-sym.s b/llvm/test/MC/ELF/reloc-section-sym.s
new file mode 100644
index 0000000000000..f59e541c3c849
--- /dev/null
+++ b/llvm/test/MC/ELF/reloc-section-sym.s
@@ -0,0 +1,60 @@
+# RUN: llvm-mc -filetype=obj -triple x86_64 %s -o %t.all
+# RUN: llvm-mc -filetype=obj -triple x86_64 --reloc-section-sym=all %s -o %t.all2
+# RUN: llvm-mc -filetype=obj -triple x86_64 --reloc-section-sym=internal %s -o %t.internal
+# RUN: llvm-mc -filetype=obj -triple x86_64 --reloc-section-sym=none %s -o %t.none
+# RUN: llvm-mc -filetype=obj -triple x86_64 --reloc-section-sym=internal --save-temp-labels %s -o %t.internal-stl
+# RUN: llvm-readelf -rs %t.all | FileCheck %s --check-prefix=ALL
+# RUN: cmp %t.all %t.all2
+# RUN: llvm-readelf -rs %t.internal | FileCheck %s --check-prefixes=INT,INTERNAL
+# RUN: llvm-readelf -rs %t.none | FileCheck %s --check-prefix=NONE
+# RUN: llvm-readelf -rs %t.internal-stl | FileCheck %s --check-prefixes=INT,INTERNAL2
+
+.text
+ nop
+local:
+ nop
+.Ltemp:
+ nop
+
+.section .text1,"ax"
+ call local
+ call .Ltemp
+
+.section .rodata,"a"
+.long local + 16
+.long .Ltemp + 16
+
+## =all (default): all eligible local binding symbols are converted to section symbols.
+# ALL: .rela.text1
+# ALL: R_X86_64_PLT32 {{.*}} .text - 3
+# ALL-NEXT: R_X86_64_PLT32 {{.*}} .text - 2
+# ALL: .rela.rodata
+# ALL: R_X86_64_32 {{.*}} .text + 11
+# ALL-NEXT: R_X86_64_32 {{.*}} .text + 12
+# ALL: SECTION LOCAL DEFAULT {{.*}} .text
+# ALL: NOTYPE LOCAL DEFAULT {{.*}} local
+
+## =internal: internal symbols (.L) are converted to section symbols.
+## With --save-temp-labels, .Ltemp is still converted but remains in the symbol table.
+# INT: .rela.text1
+# INT: R_X86_64_PLT32 {{.*}} local - 4
+# INT-NEXT: R_X86_64_PLT32 {{.*}} .text - 2
+# INT: .rela.rodata
+# INT: R_X86_64_32 {{.*}} local + 10
+# INT-NEXT: R_X86_64_32 {{.*}} .text + 12
+# INT: SECTION LOCAL DEFAULT {{.*}} .text
+# INTERNAL: NOTYPE LOCAL DEFAULT {{.*}} local
+# INTERNAL-NOT: {{.}}
+# INTERNAL2: NOTYPE LOCAL DEFAULT {{.*}} local
+# INTERNAL2-NEXT: NOTYPE LOCAL DEFAULT {{.*}} .Ltemp
+
+## =none: no symbol-to-section conversion.
+# NONE: .rela.text1
+# NONE: R_X86_64_PLT32 {{.*}} local - 4
+# NONE-NEXT: R_X86_64_PLT32 {{.*}} .Ltemp - 4
+# NONE: .rela.rodata
+# NONE: R_X86_64_32 {{.*}} local + 10
+# NONE-NEXT: R_X86_64_32 {{.*}} .Ltemp + 10
+# NONE-NOT: SECTION
+# NONE: NOTYPE LOCAL DEFAULT {{.*}} local
+# NONE-NEXT: NOTYPE LOCAL DEFAULT {{.*}} .Ltemp
``````````
</details>
https://github.com/llvm/llvm-project/pull/182725
More information about the llvm-commits
mailing list