[llvm] 8275dc9 - [MC] .reloc: register used symbols

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 7 18:34:41 PST 2023


Author: Fangrui Song
Date: 2023-12-07T18:34:36-08:00
New Revision: 8275dc97483347b88a2fa9067446dfb9d7d7f72e

URL: https://github.com/llvm/llvm-project/commit/8275dc97483347b88a2fa9067446dfb9d7d7f72e
DIFF: https://github.com/llvm/llvm-project/commit/8275dc97483347b88a2fa9067446dfb9d7d7f72e.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/lib/MC/MCELFStreamer.cpp
    llvm/lib/MC/MCObjectStreamer.cpp
    llvm/test/MC/ELF/reloc-directive.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCELFStreamer.cpp b/llvm/lib/MC/MCELFStreamer.cpp
index 653ff4e9435a5..e541090769e9e 100644
--- a/llvm/lib/MC/MCELFStreamer.cpp
+++ b/llvm/lib/MC/MCELFStreamer.cpp
@@ -501,7 +501,6 @@ void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE,
                                   SRE->getLoc());
   }
   const MCConstantExpr *MCOffset = MCConstantExpr::create(Offset, getContext());
-  MCObjectStreamer::visitUsedExpr(*SRE);
   if (std::optional<std::pair<bool, std::string>> Err =
           MCObjectStreamer::emitRelocDirective(
               *MCOffset, "BFD_RELOC_NONE", SRE, SRE->getLoc(),

diff  --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index 3cf7b4359caba..d11ccfb5e269f 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -797,8 +797,9 @@ MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
     return std::make_pair(true, std::string("unknown relocation name"));
 
   MCFixupKind Kind = *MaybeKind;
-
-  if (Expr == nullptr)
+  if (Expr)
+    visitUsedExpr(*Expr);
+  else
     Expr =
         MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());
 

diff  --git a/llvm/test/MC/ELF/reloc-directive.s b/llvm/test/MC/ELF/reloc-directive.s
index 59d7ace40d7e0..a4658f938d0d3 100644
--- a/llvm/test/MC/ELF/reloc-directive.s
+++ b/llvm/test/MC/ELF/reloc-directive.s
@@ -10,11 +10,14 @@
 # ASM-NEXT: .Ltmp2:
 # ASM-NEXT:  .reloc 2+.Ltmp2, R_X86_64_NONE, foo
 # 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: 0x4 R_X86_64_NONE data 0x1
+# CHECK-NEXT: 0x1 R_X86_64_NONE unused 0x0
 
 .text
 .globl foo
@@ -24,6 +27,7 @@ foo:
   .reloc .-1, R_X86_64_NONE, foo
   .reloc 2+., R_X86_64_NONE, foo
   .reloc 1+foo+3, R_X86_64_NONE, data+1
+  .reloc ., BFD_RELOC_NONE, unused
 
 .data
 .globl data


        


More information about the llvm-commits mailing list