Skip to content

Commit 8275dc9

Browse files
committed
[MC] .reloc: register used symbols
When `sym` in `.reloc ., BFD_RELOC_NONE, sym` is not referenced elsewhere, `sym` is not in the symbol table and the relocation references the null symbol. Visit the expression to fix the issue.
1 parent 4162a9b commit 8275dc9

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

llvm/lib/MC/MCELFStreamer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,6 @@ void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE,
501501
SRE->getLoc());
502502
}
503503
const MCConstantExpr *MCOffset = MCConstantExpr::create(Offset, getContext());
504-
MCObjectStreamer::visitUsedExpr(*SRE);
505504
if (std::optional<std::pair<bool, std::string>> Err =
506505
MCObjectStreamer::emitRelocDirective(
507506
*MCOffset, "BFD_RELOC_NONE", SRE, SRE->getLoc(),

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,9 @@ MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
797797
return std::make_pair(true, std::string("unknown relocation name"));
798798

799799
MCFixupKind Kind = *MaybeKind;
800-
801-
if (Expr == nullptr)
800+
if (Expr)
801+
visitUsedExpr(*Expr);
802+
else
802803
Expr =
803804
MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());
804805

llvm/test/MC/ELF/reloc-directive.s

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
# ASM-NEXT: .Ltmp2:
1111
# ASM-NEXT: .reloc 2+.Ltmp2, R_X86_64_NONE, foo
1212
# ASM-NEXT: .reloc (1+foo)+3, R_X86_64_NONE, data+1
13+
# ASM-NEXT: .Ltmp3:
14+
# ASM-NEXT: .reloc .Ltmp3, BFD_RELOC_NONE, unused
1315

1416
# CHECK: 0x2 R_X86_64_NONE foo 0x0
1517
# CHECK-NEXT: 0x0 R_X86_64_NONE foo 0x0
1618
# CHECK-NEXT: 0x3 R_X86_64_NONE foo 0x0
1719
# CHECK-NEXT: 0x4 R_X86_64_NONE data 0x1
20+
# CHECK-NEXT: 0x1 R_X86_64_NONE unused 0x0
1821

1922
.text
2023
.globl foo
@@ -24,6 +27,7 @@ foo:
2427
.reloc .-1, R_X86_64_NONE, foo
2528
.reloc 2+., R_X86_64_NONE, foo
2629
.reloc 1+foo+3, R_X86_64_NONE, data+1
30+
.reloc ., BFD_RELOC_NONE, unused
2731

2832
.data
2933
.globl data

0 commit comments

Comments
 (0)