blob: 43a96e84289ccf79e79d519ba594b9efb9385a1f [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- 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/SystemZMCTargetDesc.h"
11#include "MCTargetDesc/SystemZMCFixups.h"
12#include "llvm/MC/MCELFObjectWriter.h"
13#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCValue.h"
15
16using namespace llvm;
17
18namespace {
19class SystemZObjectWriter : public MCELFObjectTargetWriter {
20public:
21 SystemZObjectWriter(uint8_t OSABI);
22
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000023 ~SystemZObjectWriter() override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000024
25protected:
26 // Override MCELFObjectTargetWriter.
Rafael Espindola8340f942016-01-13 22:56:57 +000027 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
28 const MCFixup &Fixup, bool IsPCRel) const override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000029};
Richard Sandifordc2312692014-03-06 10:38:30 +000030} // end anonymous namespace
Ulrich Weigand5f613df2013-05-06 16:15:19 +000031
32SystemZObjectWriter::SystemZObjectWriter(uint8_t OSABI)
33 : MCELFObjectTargetWriter(/*Is64Bit=*/true, OSABI, ELF::EM_S390,
34 /*HasRelocationAddend=*/ true) {}
35
36SystemZObjectWriter::~SystemZObjectWriter() {
37}
38
39// Return the relocation type for an absolute value of MCFixupKind Kind.
40static unsigned getAbsoluteReloc(unsigned Kind) {
41 switch (Kind) {
42 case FK_Data_1: return ELF::R_390_8;
43 case FK_Data_2: return ELF::R_390_16;
44 case FK_Data_4: return ELF::R_390_32;
45 case FK_Data_8: return ELF::R_390_64;
46 }
47 llvm_unreachable("Unsupported absolute address");
48}
49
50// Return the relocation type for a PC-relative value of MCFixupKind Kind.
51static unsigned getPCRelReloc(unsigned Kind) {
52 switch (Kind) {
53 case FK_Data_2: return ELF::R_390_PC16;
54 case FK_Data_4: return ELF::R_390_PC32;
55 case FK_Data_8: return ELF::R_390_PC64;
Ulrich Weigand84404f32016-11-28 14:01:51 +000056 case SystemZ::FK_390_PC12DBL: return ELF::R_390_PC12DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000057 case SystemZ::FK_390_PC16DBL: return ELF::R_390_PC16DBL;
Ulrich Weigand84404f32016-11-28 14:01:51 +000058 case SystemZ::FK_390_PC24DBL: return ELF::R_390_PC24DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000059 case SystemZ::FK_390_PC32DBL: return ELF::R_390_PC32DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000060 }
61 llvm_unreachable("Unsupported PC-relative address");
62}
63
64// Return the R_390_TLS_LE* relocation type for MCFixupKind Kind.
65static unsigned getTLSLEReloc(unsigned Kind) {
66 switch (Kind) {
67 case FK_Data_4: return ELF::R_390_TLS_LE32;
68 case FK_Data_8: return ELF::R_390_TLS_LE64;
69 }
70 llvm_unreachable("Unsupported absolute address");
71}
72
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +000073// Return the R_390_TLS_LDO* relocation type for MCFixupKind Kind.
74static unsigned getTLSLDOReloc(unsigned Kind) {
75 switch (Kind) {
76 case FK_Data_4: return ELF::R_390_TLS_LDO32;
77 case FK_Data_8: return ELF::R_390_TLS_LDO64;
78 }
79 llvm_unreachable("Unsupported absolute address");
80}
81
82// Return the R_390_TLS_LDM* relocation type for MCFixupKind Kind.
83static unsigned getTLSLDMReloc(unsigned Kind) {
84 switch (Kind) {
85 case FK_Data_4: return ELF::R_390_TLS_LDM32;
86 case FK_Data_8: return ELF::R_390_TLS_LDM64;
87 case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_LDCALL;
88 }
89 llvm_unreachable("Unsupported absolute address");
90}
91
92// Return the R_390_TLS_GD* relocation type for MCFixupKind Kind.
93static unsigned getTLSGDReloc(unsigned Kind) {
94 switch (Kind) {
95 case FK_Data_4: return ELF::R_390_TLS_GD32;
96 case FK_Data_8: return ELF::R_390_TLS_GD64;
97 case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_GDCALL;
98 }
99 llvm_unreachable("Unsupported absolute address");
100}
101
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000102// Return the PLT relocation counterpart of MCFixupKind Kind.
103static unsigned getPLTReloc(unsigned Kind) {
104 switch (Kind) {
Ulrich Weigand84404f32016-11-28 14:01:51 +0000105 case SystemZ::FK_390_PC12DBL: return ELF::R_390_PLT12DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000106 case SystemZ::FK_390_PC16DBL: return ELF::R_390_PLT16DBL;
Ulrich Weigand84404f32016-11-28 14:01:51 +0000107 case SystemZ::FK_390_PC24DBL: return ELF::R_390_PLT24DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000108 case SystemZ::FK_390_PC32DBL: return ELF::R_390_PLT32DBL;
109 }
110 llvm_unreachable("Unsupported absolute address");
111}
112
Rafael Espindola8340f942016-01-13 22:56:57 +0000113unsigned SystemZObjectWriter::getRelocType(MCContext &Ctx,
114 const MCValue &Target,
Rafael Espindolac03f44c2014-03-27 20:49:35 +0000115 const MCFixup &Fixup,
116 bool IsPCRel) const {
Rafael Espindola3d082fa2014-05-03 19:57:04 +0000117 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000118 unsigned Kind = Fixup.getKind();
119 switch (Modifier) {
120 case MCSymbolRefExpr::VK_None:
121 if (IsPCRel)
122 return getPCRelReloc(Kind);
123 return getAbsoluteReloc(Kind);
124
125 case MCSymbolRefExpr::VK_NTPOFF:
126 assert(!IsPCRel && "NTPOFF shouldn't be PC-relative");
127 return getTLSLEReloc(Kind);
128
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000129 case MCSymbolRefExpr::VK_INDNTPOFF:
130 if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
131 return ELF::R_390_TLS_IEENT;
132 llvm_unreachable("Only PC-relative INDNTPOFF accesses are supported for now");
133
134 case MCSymbolRefExpr::VK_DTPOFF:
135 assert(!IsPCRel && "DTPOFF shouldn't be PC-relative");
136 return getTLSLDOReloc(Kind);
137
138 case MCSymbolRefExpr::VK_TLSLDM:
139 assert(!IsPCRel && "TLSLDM shouldn't be PC-relative");
140 return getTLSLDMReloc(Kind);
141
142 case MCSymbolRefExpr::VK_TLSGD:
143 assert(!IsPCRel && "TLSGD shouldn't be PC-relative");
144 return getTLSGDReloc(Kind);
145
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000146 case MCSymbolRefExpr::VK_GOT:
147 if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
148 return ELF::R_390_GOTENT;
149 llvm_unreachable("Only PC-relative GOT accesses are supported for now");
150
151 case MCSymbolRefExpr::VK_PLT:
152 assert(IsPCRel && "@PLT shouldt be PC-relative");
153 return getPLTReloc(Kind);
154
155 default:
156 llvm_unreachable("Modifier not supported");
157 }
158}
159
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000160MCObjectWriter *llvm::createSystemZObjectWriter(raw_pwrite_stream &OS,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000161 uint8_t OSABI) {
162 MCELFObjectTargetWriter *MOTW = new SystemZObjectWriter(OSABI);
163 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false);
164}