[PATCH] D117316: [MC] Support constant offset for symbol PendingFixup

luxufan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 14 07:16:49 PST 2022


StephenFan created this revision.
StephenFan added reviewers: MaskRay, jrtc27, jhenderson, asb, luismarques.
Herald added subscribers: luke957, frasercrmck, apazos, sameer.abuasal, s.egerton, Jim, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya.
StephenFan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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
    }
  ]


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117316

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


Index: llvm/test/MC/RISCV/reloc-directive.s
===================================================================
--- llvm/test/MC/RISCV/reloc-directive.s
+++ llvm/test/MC/RISCV/reloc-directive.s
@@ -22,6 +22,7 @@
 # CHECK-NEXT: 0x0 R_RISCV_NONE - 0x9
 # CHECK-NEXT: 0x0 R_RISCV_32 - 0x9
 # CHECK-NEXT: 0x0 R_RISCV_64 - 0x9
+# CHECK-NEXT: 0x4 R_RISCV_32 - 0x6
 .text
   ret
   nop
@@ -38,6 +39,8 @@
   .reloc 0, BFD_RELOC_32, 9
   .reloc 0, BFD_RELOC_64, 9
 
+  .reloc foo+4, R_RISCV_32, 6
+
 .data
 .globl foo
 foo:
Index: llvm/lib/MC/MCObjectStreamer.cpp
===================================================================
--- llvm/lib/MC/MCObjectStreamer.cpp
+++ llvm/lib/MC/MCObjectStreamer.cpp
@@ -119,7 +119,8 @@
       continue;
     }
     flushPendingLabels(PendingFixup.DF, PendingFixup.DF->getContents().size());
-    PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset());
+    PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset() +
+                                 PendingFixup.Fixup.getOffset());
     PendingFixup.DF->getFixups().push_back(PendingFixup.Fixup);
   }
   PendingFixups.clear();
@@ -816,8 +817,9 @@
     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;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117316.399992.patch
Type: text/x-patch
Size: 1431 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220114/c9b65338/attachment.bin>


More information about the llvm-commits mailing list