Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCObjectWriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- SystemZMCObjectWriter.cpp - SystemZ ELF 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/SystemZMCFixups.h"
10
#include "MCTargetDesc/SystemZMCTargetDesc.h"
11
#include "llvm/BinaryFormat/ELF.h"
12
#include "llvm/MC/MCELFObjectWriter.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/Support/ErrorHandling.h"
18
#include <cassert>
19
#include <cstdint>
20
21
using namespace llvm;
22
23
namespace {
24
25
class SystemZObjectWriter : public MCELFObjectTargetWriter {
26
public:
27
  SystemZObjectWriter(uint8_t OSABI);
28
1.04k
  ~SystemZObjectWriter() override = default;
29
30
protected:
31
  // Override MCELFObjectTargetWriter.
32
  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
33
                        const MCFixup &Fixup, bool IsPCRel) const override;
34
};
35
36
} // end anonymous namespace
37
38
SystemZObjectWriter::SystemZObjectWriter(uint8_t OSABI)
39
  : MCELFObjectTargetWriter(/*Is64Bit_=*/true, OSABI, ELF::EM_S390,
40
1.05k
                            /*HasRelocationAddend_=*/ true) {}
41
42
// Return the relocation type for an absolute value of MCFixupKind Kind.
43
38
static unsigned getAbsoluteReloc(unsigned Kind) {
44
38
  switch (Kind) {
45
38
  
case FK_Data_1: return ELF::R_390_80
;
46
38
  
case FK_Data_2: return ELF::R_390_160
;
47
38
  
case FK_Data_4: return ELF::R_390_3226
;
48
38
  
case FK_Data_8: return ELF::R_390_6412
;
49
0
  }
50
0
  llvm_unreachable("Unsupported absolute address");
51
0
}
52
53
// Return the relocation type for a PC-relative value of MCFixupKind Kind.
54
16
static unsigned getPCRelReloc(unsigned Kind) {
55
16
  switch (Kind) {
56
16
  
case FK_Data_2: return ELF::R_390_PC160
;
57
16
  
case FK_Data_4: return ELF::R_390_PC326
;
58
16
  
case FK_Data_8: return ELF::R_390_PC642
;
59
16
  
case SystemZ::FK_390_PC12DBL: return ELF::R_390_PC12DBL1
;
60
16
  
case SystemZ::FK_390_PC16DBL: return ELF::R_390_PC16DBL2
;
61
16
  
case SystemZ::FK_390_PC24DBL: return ELF::R_390_PC24DBL1
;
62
16
  
case SystemZ::FK_390_PC32DBL: return ELF::R_390_PC32DBL4
;
63
0
  }
64
0
  llvm_unreachable("Unsupported PC-relative address");
65
0
}
66
67
// Return the R_390_TLS_LE* relocation type for MCFixupKind Kind.
68
2
static unsigned getTLSLEReloc(unsigned Kind) {
69
2
  switch (Kind) {
70
2
  
case FK_Data_4: return ELF::R_390_TLS_LE321
;
71
2
  
case FK_Data_8: return ELF::R_390_TLS_LE641
;
72
0
  }
73
0
  llvm_unreachable("Unsupported absolute address");
74
0
}
75
76
// Return the R_390_TLS_LDO* relocation type for MCFixupKind Kind.
77
2
static unsigned getTLSLDOReloc(unsigned Kind) {
78
2
  switch (Kind) {
79
2
  
case FK_Data_4: return ELF::R_390_TLS_LDO321
;
80
2
  
case FK_Data_8: return ELF::R_390_TLS_LDO641
;
81
0
  }
82
0
  llvm_unreachable("Unsupported absolute address");
83
0
}
84
85
// Return the R_390_TLS_LDM* relocation type for MCFixupKind Kind.
86
4
static unsigned getTLSLDMReloc(unsigned Kind) {
87
4
  switch (Kind) {
88
4
  
case FK_Data_4: return ELF::R_390_TLS_LDM321
;
89
4
  
case FK_Data_8: return ELF::R_390_TLS_LDM641
;
90
4
  
case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_LDCALL2
;
91
0
  }
92
0
  llvm_unreachable("Unsupported absolute address");
93
0
}
94
95
// Return the R_390_TLS_GD* relocation type for MCFixupKind Kind.
96
4
static unsigned getTLSGDReloc(unsigned Kind) {
97
4
  switch (Kind) {
98
4
  
case FK_Data_4: return ELF::R_390_TLS_GD321
;
99
4
  
case FK_Data_8: return ELF::R_390_TLS_GD641
;
100
4
  
case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_GDCALL2
;
101
0
  }
102
0
  llvm_unreachable("Unsupported absolute address");
103
0
}
104
105
// Return the PLT relocation counterpart of MCFixupKind Kind.
106
15
static unsigned getPLTReloc(unsigned Kind) {
107
15
  switch (Kind) {
108
15
  
case SystemZ::FK_390_PC12DBL: return ELF::R_390_PLT12DBL1
;
109
15
  
case SystemZ::FK_390_PC16DBL: return ELF::R_390_PLT16DBL4
;
110
15
  
case SystemZ::FK_390_PC24DBL: return ELF::R_390_PLT24DBL1
;
111
15
  
case SystemZ::FK_390_PC32DBL: return ELF::R_390_PLT32DBL9
;
112
0
  }
113
0
  llvm_unreachable("Unsupported absolute address");
114
0
}
115
116
unsigned SystemZObjectWriter::getRelocType(MCContext &Ctx,
117
                                           const MCValue &Target,
118
                                           const MCFixup &Fixup,
119
83
                                           bool IsPCRel) const {
120
83
  MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
121
83
  unsigned Kind = Fixup.getKind();
122
83
  switch (Modifier) {
123
83
  case MCSymbolRefExpr::VK_None:
124
54
    if (IsPCRel)
125
16
      return getPCRelReloc(Kind);
126
38
    return getAbsoluteReloc(Kind);
127
38
128
38
  case MCSymbolRefExpr::VK_NTPOFF:
129
2
    assert(!IsPCRel && "NTPOFF shouldn't be PC-relative");
130
2
    return getTLSLEReloc(Kind);
131
38
132
38
  case MCSymbolRefExpr::VK_INDNTPOFF:
133
1
    if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
134
1
      return ELF::R_390_TLS_IEENT;
135
0
    llvm_unreachable("Only PC-relative INDNTPOFF accesses are supported for now");
136
0
137
2
  case MCSymbolRefExpr::VK_DTPOFF:
138
2
    assert(!IsPCRel && "DTPOFF shouldn't be PC-relative");
139
2
    return getTLSLDOReloc(Kind);
140
0
141
4
  case MCSymbolRefExpr::VK_TLSLDM:
142
4
    assert(!IsPCRel && "TLSLDM shouldn't be PC-relative");
143
4
    return getTLSLDMReloc(Kind);
144
0
145
4
  case MCSymbolRefExpr::VK_TLSGD:
146
4
    assert(!IsPCRel && "TLSGD shouldn't be PC-relative");
147
4
    return getTLSGDReloc(Kind);
148
0
149
1
  case MCSymbolRefExpr::VK_GOT:
150
1
    if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
151
1
      return ELF::R_390_GOTENT;
152
0
    llvm_unreachable("Only PC-relative GOT accesses are supported for now");
153
0
154
15
  case MCSymbolRefExpr::VK_PLT:
155
15
    assert(IsPCRel && "@PLT shouldt be PC-relative");
156
15
    return getPLTReloc(Kind);
157
0
158
0
  default:
159
0
    llvm_unreachable("Modifier not supported");
160
83
  }
161
83
}
162
163
std::unique_ptr<MCObjectTargetWriter>
164
1.05k
llvm::createSystemZObjectWriter(uint8_t OSABI) {
165
1.05k
  return llvm::make_unique<SystemZObjectWriter>(OSABI);
166
1.05k
}