/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCObjectWriter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- SystemZMCObjectWriter.cpp - SystemZ ELF writer --------------------===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | |
10 | | #include "MCTargetDesc/SystemZMCFixups.h" |
11 | | #include "MCTargetDesc/SystemZMCTargetDesc.h" |
12 | | #include "llvm/BinaryFormat/ELF.h" |
13 | | #include "llvm/MC/MCELFObjectWriter.h" |
14 | | #include "llvm/MC/MCExpr.h" |
15 | | #include "llvm/MC/MCFixup.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 | 13 | ~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 | 13 | /*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 | 0 | case FK_Data_1: return ELF::R_390_8; |
46 | 0 | case FK_Data_2: return ELF::R_390_16; |
47 | 26 | case FK_Data_4: return ELF::R_390_32; |
48 | 12 | case FK_Data_8: return ELF::R_390_64; |
49 | 0 | } |
50 | 0 | llvm_unreachable0 ("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 | 0 | case FK_Data_2: return ELF::R_390_PC16; |
57 | 6 | case FK_Data_4: return ELF::R_390_PC32; |
58 | 2 | case FK_Data_8: return ELF::R_390_PC64; |
59 | 1 | case SystemZ::FK_390_PC12DBL: return ELF::R_390_PC12DBL; |
60 | 2 | case SystemZ::FK_390_PC16DBL: return ELF::R_390_PC16DBL; |
61 | 1 | case SystemZ::FK_390_PC24DBL: return ELF::R_390_PC24DBL; |
62 | 4 | case SystemZ::FK_390_PC32DBL: return ELF::R_390_PC32DBL; |
63 | 0 | } |
64 | 0 | llvm_unreachable0 ("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 | 1 | case FK_Data_4: return ELF::R_390_TLS_LE32; |
71 | 1 | case FK_Data_8: return ELF::R_390_TLS_LE64; |
72 | 0 | } |
73 | 0 | llvm_unreachable0 ("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 | 1 | case FK_Data_4: return ELF::R_390_TLS_LDO32; |
80 | 1 | case FK_Data_8: return ELF::R_390_TLS_LDO64; |
81 | 0 | } |
82 | 0 | llvm_unreachable0 ("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 | 1 | case FK_Data_4: return ELF::R_390_TLS_LDM32; |
89 | 1 | case FK_Data_8: return ELF::R_390_TLS_LDM64; |
90 | 2 | case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_LDCALL; |
91 | 0 | } |
92 | 0 | llvm_unreachable0 ("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 | 1 | case FK_Data_4: return ELF::R_390_TLS_GD32; |
99 | 1 | case FK_Data_8: return ELF::R_390_TLS_GD64; |
100 | 2 | case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_GDCALL; |
101 | 0 | } |
102 | 0 | llvm_unreachable0 ("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 | 1 | case SystemZ::FK_390_PC12DBL: return ELF::R_390_PLT12DBL; |
109 | 4 | case SystemZ::FK_390_PC16DBL: return ELF::R_390_PLT16DBL; |
110 | 1 | case SystemZ::FK_390_PC24DBL: return ELF::R_390_PLT24DBL; |
111 | 9 | case SystemZ::FK_390_PC32DBL: return ELF::R_390_PLT32DBL; |
112 | 0 | } |
113 | 0 | llvm_unreachable0 ("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 | 54 | case MCSymbolRefExpr::VK_None: |
124 | 54 | if (IsPCRel) |
125 | 16 | return getPCRelReloc(Kind); |
126 | 38 | return getAbsoluteReloc(Kind); |
127 | 38 | |
128 | 2 | case MCSymbolRefExpr::VK_NTPOFF: |
129 | 2 | assert(!IsPCRel && "NTPOFF shouldn't be PC-relative"); |
130 | 2 | return getTLSLEReloc(Kind); |
131 | 38 | |
132 | 1 | case MCSymbolRefExpr::VK_INDNTPOFF: |
133 | 1 | if (IsPCRel && 1 Kind == SystemZ::FK_390_PC32DBL1 ) |
134 | 1 | return ELF::R_390_TLS_IEENT; |
135 | 0 | llvm_unreachable0 ("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 && 1 Kind == SystemZ::FK_390_PC32DBL1 ) |
151 | 1 | return ELF::R_390_GOTENT; |
152 | 0 | llvm_unreachable0 ("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 | 0 | } |
161 | 0 | } |
162 | | |
163 | | MCObjectWriter *llvm::createSystemZObjectWriter(raw_pwrite_stream &OS, |
164 | 13 | uint8_t OSABI) { |
165 | 13 | MCELFObjectTargetWriter *MOTW = new SystemZObjectWriter(OSABI); |
166 | 13 | return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false); |
167 | 13 | } |