From 1c5961c0481b9c7421d38e3141d3c5a1e6084234 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 12 Apr 2025 21:00:25 -0700 Subject: [PATCH] ELFObjectWriter: Disable STT_SECTION adjustment for .reloc to match GNU Assembler. This generalizes the SHT_LLVM_CALL_GRAPH_PROFILE (which uses BFD_RELOC_NONE https://reviews.llvm.org/D104080) special case. --- llvm/lib/MC/ELFObjectWriter.cpp | 5 ++--- llvm/test/MC/ELF/reloc-directive.s | 7 ++++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp index 75450aa7de9d..45fcc6a1de4c 100644 --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -1390,9 +1390,8 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm, if (UseSectionSym) { UseSectionSym = useSectionSymbol(Asm, Target, SymA, C, Type); - // Disable STT_SECTION adjustment for CG Profile to help with --cg-profile. - const auto *Parent = cast(Fragment->getParent()); - UseSectionSym &= Parent->getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE; + // Disable STT_SECTION adjustment for .reloc directives. + UseSectionSym &= Fixup.getKind() >= FirstLiteralRelocationKind; } uint64_t Addend = UseSectionSym ? C + Asm.getSymbolOffset(*SymA) : C; diff --git a/llvm/test/MC/ELF/reloc-directive.s b/llvm/test/MC/ELF/reloc-directive.s index f4121ef07181..42995aa9e7d8 100644 --- a/llvm/test/MC/ELF/reloc-directive.s +++ b/llvm/test/MC/ELF/reloc-directive.s @@ -8,24 +8,25 @@ # ASM-NEXT: .Ltmp1: # ASM-NEXT: .reloc .Ltmp1-1, R_X86_64_NONE, foo # ASM-NEXT: .Ltmp2: -# ASM-NEXT: .reloc 2+.Ltmp2, R_X86_64_NONE, foo +# ASM-NEXT: .reloc 2+.Ltmp2, R_X86_64_NONE, local # ASM-NEXT: .reloc 1+foo+3, R_X86_64_NONE, data+1 # ASM-NEXT: .Ltmp3: # ASM-NEXT: .reloc .Ltmp3, BFD_RELOC_NONE, unused # CHECK: 0x2 R_X86_64_NONE foo 0x0 # CHECK-NEXT: 0x0 R_X86_64_NONE foo 0x0 -# CHECK-NEXT: 0x3 R_X86_64_NONE foo 0x0 +# CHECK-NEXT: 0x3 R_X86_64_NONE local 0x0 # CHECK-NEXT: 0x4 R_X86_64_NONE data 0x1 # CHECK-NEXT: 0x1 R_X86_64_NONE unused 0x0 .text .globl foo foo: +local: ret .reloc .+3-2, R_X86_64_NONE, foo .reloc .-1, R_X86_64_NONE, foo - .reloc 2+., R_X86_64_NONE, foo + .reloc 2+., R_X86_64_NONE, local .reloc 1+foo+3, R_X86_64_NONE, data+1 .reloc ., BFD_RELOC_NONE, unused