[ARM] [COFF] Properly produce cross-section relative relocations
authorMartin Storsjö <martin@martin.st>
Tue, 30 Mar 2021 09:28:47 +0000 (12:28 +0300)
committerMartin Storsjö <martin@martin.st>
Wed, 14 Apr 2021 09:31:28 +0000 (12:31 +0300)
Differential Revision: https://reviews.llvm.org/D99574

llvm/lib/Target/ARM/MCTargetDesc/ARMWinCOFFObjectWriter.cpp
llvm/test/MC/ARM/coff-relocations.s

index def8ec6..31a8149 100644 (file)
@@ -10,6 +10,7 @@
 #include "llvm/ADT/Twine.h"
 #include "llvm/BinaryFormat/COFF.h"
 #include "llvm/MC/MCAsmBackend.h"
+#include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCFixup.h"
 #include "llvm/MC/MCFixupKindInfo.h"
@@ -49,7 +50,17 @@ unsigned ARMWinCOFFObjectWriter::getRelocType(MCContext &Ctx,
   MCSymbolRefExpr::VariantKind Modifier =
     Target.isAbsolute() ? MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
 
-  switch (static_cast<unsigned>(Fixup.getKind())) {
+  unsigned FixupKind = Fixup.getKind();
+  if (IsCrossSection) {
+    if (FixupKind != FK_Data_4) {
+      Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
+      return COFF::IMAGE_REL_ARM_ADDR32;
+    }
+    FixupKind = FK_PCRel_4;
+  }
+
+
+  switch (FixupKind) {
   default: {
     const MCFixupKindInfo &Info = MAB.getFixupKindInfo(Fixup.getKind());
     report_fatal_error(Twine("unsupported relocation type: ") + Info.Name);
@@ -63,6 +74,8 @@ unsigned ARMWinCOFFObjectWriter::getRelocType(MCContext &Ctx,
     default:
       return COFF::IMAGE_REL_ARM_ADDR32;
     }
+  case FK_PCRel_4:
+    return COFF::IMAGE_REL_ARM_REL32;
   case FK_SecRel_2:
     return COFF::IMAGE_REL_ARM_SECTION;
   case FK_SecRel_4:
index 73211b9..b2a4a92 100644 (file)
@@ -4,6 +4,9 @@
 @ RUN: llvm-mc -triple thumbv7-windows-itanium -filetype obj -o - %s \
 @ RUN:   | llvm-objdump -d - | FileCheck %s --check-prefix=CHECK-ENCODING
 
+@ RUN: llvm-mc -triple thumbv7-windows-itanium -filetype obj -o - %s \
+@ RUN:   | llvm-objdump -s - | FileCheck %s --check-prefix=CHECK-DATA
+
        .syntax unified
        .text
        .thumb
@@ -87,6 +90,11 @@ secrel:
 .Lsecrel:
        .long target(secrel32)
 
+       .section .rdata, "dr"
+.Ltable:
+       .word addr32nb - .Ltable
+       .word secrel - .Ltable
+
 @ CHECK-ENCODING-LABEL: <secrel>:
 @ CHECK-ENCODING-NEXT: ldr.w r0, [pc, #4]
 @ CHECK-ENCODING-NEXT: bx r0
@@ -105,5 +113,11 @@ secrel:
 @ CHECK-RELOCATION:     0x2C IMAGE_REL_ARM_ADDR32NB
 @ CHECK-RELOCATION:     0x38 IMAGE_REL_ARM_SECREL
 @ CHECK-RELOCATION:   }
+@ CHECK-RELOCATION:   Section (4) .rdata {
+@ CHECK-RELOCATION:     0x0 IMAGE_REL_ARM_REL32
+@ CHECK-RELOCATION:     0x4 IMAGE_REL_ARM_REL32
+@ CHECK-RELOCATION:   }
 @ CHECK-RELOCATION: ]
 
+@ CHECK-DATA: Contents of section .rdata:
+@ CHECK-DATA-NEXT: 0000 00000000 04000000