Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/X86/MCTargetDesc/X86WinCOFFObjectWriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- X86WinCOFFObjectWriter.cpp - X86 Win COFF Writer ------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "MCTargetDesc/X86FixupKinds.h"
10
#include "MCTargetDesc/X86MCTargetDesc.h"
11
#include "llvm/BinaryFormat/COFF.h"
12
#include "llvm/MC/MCContext.h"
13
#include "llvm/MC/MCExpr.h"
14
#include "llvm/MC/MCFixup.h"
15
#include "llvm/MC/MCObjectWriter.h"
16
#include "llvm/MC/MCValue.h"
17
#include "llvm/MC/MCWinCOFFObjectWriter.h"
18
#include "llvm/Support/ErrorHandling.h"
19
20
using namespace llvm;
21
22
namespace {
23
24
class X86WinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
25
public:
26
  X86WinCOFFObjectWriter(bool Is64Bit);
27
1.07k
  ~X86WinCOFFObjectWriter() override = default;
28
29
  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
30
                        const MCFixup &Fixup, bool IsCrossSection,
31
                        const MCAsmBackend &MAB) const override;
32
};
33
34
} // end anonymous namespace
35
36
X86WinCOFFObjectWriter::X86WinCOFFObjectWriter(bool Is64Bit)
37
    : MCWinCOFFObjectTargetWriter(Is64Bit ? COFF::IMAGE_FILE_MACHINE_AMD64
38
1.08k
                                          : COFF::IMAGE_FILE_MACHINE_I386) {}
39
40
unsigned X86WinCOFFObjectWriter::getRelocType(MCContext &Ctx,
41
                                              const MCValue &Target,
42
                                              const MCFixup &Fixup,
43
                                              bool IsCrossSection,
44
4.06k
                                              const MCAsmBackend &MAB) const {
45
4.06k
  unsigned FixupKind = Fixup.getKind();
46
4.06k
  if (IsCrossSection) {
47
15
    if (FixupKind != FK_Data_4 && 
FixupKind != llvm::X86::reloc_signed_4byte4
) {
48
3
      Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
49
3
      return COFF::IMAGE_REL_AMD64_ADDR32;
50
3
    }
51
12
    FixupKind = FK_PCRel_4;
52
12
  }
53
4.06k
54
4.06k
  
MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() 4.06k
?
55
4.06k
    
MCSymbolRefExpr::VK_None0
: Target.getSymA()->getKind();
56
4.06k
57
4.06k
  if (getMachine() == COFF::IMAGE_FILE_MACHINE_AMD64) {
58
2.96k
    switch (FixupKind) {
59
2.96k
    case FK_PCRel_4:
60
382
    case X86::reloc_riprel_4byte:
61
382
    case X86::reloc_riprel_4byte_movq_load:
62
382
    case X86::reloc_riprel_4byte_relax:
63
382
    case X86::reloc_riprel_4byte_relax_rex:
64
382
    case X86::reloc_branch_4byte_pcrel:
65
382
      return COFF::IMAGE_REL_AMD64_REL32;
66
794
    case FK_Data_4:
67
794
    case X86::reloc_signed_4byte:
68
794
    case X86::reloc_signed_4byte_relax:
69
794
      if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
70
770
        return COFF::IMAGE_REL_AMD64_ADDR32NB;
71
24
      if (Modifier == MCSymbolRefExpr::VK_SECREL)
72
1
        return COFF::IMAGE_REL_AMD64_SECREL;
73
23
      return COFF::IMAGE_REL_AMD64_ADDR32;
74
184
    case FK_Data_8:
75
184
      return COFF::IMAGE_REL_AMD64_ADDR64;
76
727
    case FK_SecRel_2:
77
727
      return COFF::IMAGE_REL_AMD64_SECTION;
78
877
    case FK_SecRel_4:
79
877
      return COFF::IMAGE_REL_AMD64_SECREL;
80
23
    default:
81
1
      Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
82
1
      return COFF::IMAGE_REL_AMD64_ADDR32;
83
1.09k
    }
84
1.09k
  } else if (getMachine() == COFF::IMAGE_FILE_MACHINE_I386) {
85
1.09k
    switch (FixupKind) {
86
1.09k
    case FK_PCRel_4:
87
135
    case X86::reloc_riprel_4byte:
88
135
    case X86::reloc_riprel_4byte_movq_load:
89
135
      return COFF::IMAGE_REL_I386_REL32;
90
293
    case FK_Data_4:
91
293
    case X86::reloc_signed_4byte:
92
293
    case X86::reloc_signed_4byte_relax:
93
293
      if (Modifier == MCSymbolRefExpr::VK_COFF_IMGREL32)
94
93
        return COFF::IMAGE_REL_I386_DIR32NB;
95
200
      if (Modifier == MCSymbolRefExpr::VK_SECREL)
96
3
        return COFF::IMAGE_REL_AMD64_SECREL;
97
197
      return COFF::IMAGE_REL_I386_DIR32;
98
301
    case FK_SecRel_2:
99
301
      return COFF::IMAGE_REL_I386_SECTION;
100
366
    case FK_SecRel_4:
101
366
      return COFF::IMAGE_REL_I386_SECREL;
102
197
    default:
103
1
      Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
104
1
      return COFF::IMAGE_REL_I386_DIR32;
105
1.09k
    }
106
1.09k
  } else
107
1.09k
    llvm_unreachable("Unsupported COFF machine type.");
108
4.06k
}
109
110
std::unique_ptr<MCObjectTargetWriter>
111
1.08k
llvm::createX86WinCOFFObjectWriter(bool Is64Bit) {
112
1.08k
  return llvm::make_unique<X86WinCOFFObjectWriter>(Is64Bit);
113
1.08k
}