[llvm] d606e23 - [MC] Support constant offset for symbol PendingFixup

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 25 21:50:54 PST 2022


Author: luxufan
Date: 2022-01-26T13:50:23+08:00
New Revision: d606e23305015f14072ed59e8e71d6b27d010b48

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

LOG: [MC] Support constant offset for symbol PendingFixup

This patch add support relocation offset of sym+constant(like `foo+4`) form for pending fixup.

In the past, llvm-mc ignored the constant in sym+constant form, for `foo+4`, `4` would be ignored. And test case
```
.text
  ret
  nop
  nop
  .reloc foo+4, R_RISCV_32, 6

.data
.globl foo
foo:
  .word 0
  .word 0
  .word 0
```
when run `llvm-mc -filetype=obj -triple=riscv64 %s | llvm-readobj -r`
The output is
```
Relocations [
  Section (3) .rela.text {
    0x0 R_RISCV_32 - 0x6
  }
]
```

After applying this patch, the output is
```
Relocations [
  Section (3) .rela.text {
    0x4 R_RISCV_32 - 0x6
  }
]
```

Differential Revision: https://reviews.llvm.org/D117316

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index ca462b3e558c..ebbbd6ad4e16 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -119,7 +119,8 @@ void MCObjectStreamer::resolvePendingFixups() {
       continue;
     }
     flushPendingLabels(PendingFixup.DF, PendingFixup.DF->getContents().size());
-    PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset());
+    PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset() +
+                                 PendingFixup.Fixup.getOffset());
 
     // If the location symbol to relocate is in MCEncodedFragmentWithFixups,
     // put the Fixup into location symbol's fragment. Otherwise
@@ -838,8 +839,9 @@ MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
     return None;
   }
 
-  PendingFixups.emplace_back(&SRE.getSymbol(), DF,
-                             MCFixup::create(-1, Expr, Kind, Loc));
+  PendingFixups.emplace_back(
+      &SRE.getSymbol(), DF,
+      MCFixup::create(OffsetVal.getConstant(), Expr, Kind, Loc));
   return None;
 }
 

diff  --git a/llvm/test/MC/RISCV/reloc-directive.s b/llvm/test/MC/RISCV/reloc-directive.s
index d4c20cde060b..0e217fa79848 100644
--- a/llvm/test/MC/RISCV/reloc-directive.s
+++ b/llvm/test/MC/RISCV/reloc-directive.s
@@ -25,6 +25,7 @@
 
 # CHECK:      Section ({{.*}}) .rela.data {
 # CHECK-NEXT:   0x0 R_RISCV_32 - 0x6
+# CHECK-NEXT:   0x4 R_RISCV_32 - 0x6
 # CHECK-NEXT: }
 
 # CHECK:      Section ({{.*}}) .rela.debug_line {
@@ -55,6 +56,7 @@
   .reloc line, R_RISCV_32, 6
   .reloc probe, R_RISCV_32, 6
 
+  .reloc foo+4, R_RISCV_32, 6
 .data
 .globl foo
 foo:


        


More information about the llvm-commits mailing list