blob: 3de570bf30cc47711c04a56d9a350ce241f6d311 [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
Ulrich Weigand5f613df2013-05-06 16:15:19 +000010#include "MCTargetDesc/SystemZMCFixups.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000011#include "MCTargetDesc/SystemZMCTargetDesc.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000012#include "llvm/MC/MCELFObjectWriter.h"
13#include "llvm/MC/MCExpr.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000014#include "llvm/MC/MCFixup.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000015#include "llvm/MC/MCValue.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000016#include "llvm/Support/ELF.h"
17#include "llvm/Support/ErrorHandling.h"
18#include <cassert>
19#include <cstdint>
Ulrich Weigand5f613df2013-05-06 16:15:19 +000020
21using namespace llvm;
22
23namespace {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000024
Ulrich Weigand5f613df2013-05-06 16:15:19 +000025class SystemZObjectWriter : public MCELFObjectTargetWriter {
26public:
27 SystemZObjectWriter(uint8_t OSABI);
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000028 ~SystemZObjectWriter() override = default;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000029
30protected:
31 // Override MCELFObjectTargetWriter.
Rafael Espindola8340f942016-01-13 22:56:57 +000032 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
33 const MCFixup &Fixup, bool IsPCRel) const override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000034};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000035
Richard Sandifordc2312692014-03-06 10:38:30 +000036} // end anonymous namespace
Ulrich Weigand5f613df2013-05-06 16:15:19 +000037
38SystemZObjectWriter::SystemZObjectWriter(uint8_t OSABI)
39 : MCELFObjectTargetWriter(/*Is64Bit=*/true, OSABI, ELF::EM_S390,
40 /*HasRelocationAddend=*/ true) {}
41
Ulrich Weigand5f613df2013-05-06 16:15:19 +000042// Return the relocation type for an absolute value of MCFixupKind Kind.
43static unsigned getAbsoluteReloc(unsigned Kind) {
44 switch (Kind) {
45 case FK_Data_1: return ELF::R_390_8;
46 case FK_Data_2: return ELF::R_390_16;
47 case FK_Data_4: return ELF::R_390_32;
48 case FK_Data_8: return ELF::R_390_64;
49 }
50 llvm_unreachable("Unsupported absolute address");
51}
52
53// Return the relocation type for a PC-relative value of MCFixupKind Kind.
54static unsigned getPCRelReloc(unsigned Kind) {
55 switch (Kind) {
56 case FK_Data_2: return ELF::R_390_PC16;
57 case FK_Data_4: return ELF::R_390_PC32;
58 case FK_Data_8: return ELF::R_390_PC64;
Ulrich Weigand84404f32016-11-28 14:01:51 +000059 case SystemZ::FK_390_PC12DBL: return ELF::R_390_PC12DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000060 case SystemZ::FK_390_PC16DBL: return ELF::R_390_PC16DBL;
Ulrich Weigand84404f32016-11-28 14:01:51 +000061 case SystemZ::FK_390_PC24DBL: return ELF::R_390_PC24DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000062 case SystemZ::FK_390_PC32DBL: return ELF::R_390_PC32DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000063 }
64 llvm_unreachable("Unsupported PC-relative address");
65}
66
67// Return the R_390_TLS_LE* relocation type for MCFixupKind Kind.
68static unsigned getTLSLEReloc(unsigned Kind) {
69 switch (Kind) {
70 case FK_Data_4: return ELF::R_390_TLS_LE32;
71 case FK_Data_8: return ELF::R_390_TLS_LE64;
72 }
73 llvm_unreachable("Unsupported absolute address");
74}
75
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +000076// Return the R_390_TLS_LDO* relocation type for MCFixupKind Kind.
77static unsigned getTLSLDOReloc(unsigned Kind) {
78 switch (Kind) {
79 case FK_Data_4: return ELF::R_390_TLS_LDO32;
80 case FK_Data_8: return ELF::R_390_TLS_LDO64;
81 }
82 llvm_unreachable("Unsupported absolute address");
83}
84
85// Return the R_390_TLS_LDM* relocation type for MCFixupKind Kind.
86static unsigned getTLSLDMReloc(unsigned Kind) {
87 switch (Kind) {
88 case FK_Data_4: return ELF::R_390_TLS_LDM32;
89 case FK_Data_8: return ELF::R_390_TLS_LDM64;
90 case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_LDCALL;
91 }
92 llvm_unreachable("Unsupported absolute address");
93}
94
95// Return the R_390_TLS_GD* relocation type for MCFixupKind Kind.
96static unsigned getTLSGDReloc(unsigned Kind) {
97 switch (Kind) {
98 case FK_Data_4: return ELF::R_390_TLS_GD32;
99 case FK_Data_8: return ELF::R_390_TLS_GD64;
100 case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_GDCALL;
101 }
102 llvm_unreachable("Unsupported absolute address");
103}
104
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000105// Return the PLT relocation counterpart of MCFixupKind Kind.
106static unsigned getPLTReloc(unsigned Kind) {
107 switch (Kind) {
Ulrich Weigand84404f32016-11-28 14:01:51 +0000108 case SystemZ::FK_390_PC12DBL: return ELF::R_390_PLT12DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000109 case SystemZ::FK_390_PC16DBL: return ELF::R_390_PLT16DBL;
Ulrich Weigand84404f32016-11-28 14:01:51 +0000110 case SystemZ::FK_390_PC24DBL: return ELF::R_390_PLT24DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000111 case SystemZ::FK_390_PC32DBL: return ELF::R_390_PLT32DBL;
112 }
113 llvm_unreachable("Unsupported absolute address");
114}
115
Rafael Espindola8340f942016-01-13 22:56:57 +0000116unsigned SystemZObjectWriter::getRelocType(MCContext &Ctx,
117 const MCValue &Target,
Rafael Espindolac03f44c2014-03-27 20:49:35 +0000118 const MCFixup &Fixup,
119 bool IsPCRel) const {
Rafael Espindola3d082fa2014-05-03 19:57:04 +0000120 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000121 unsigned Kind = Fixup.getKind();
122 switch (Modifier) {
123 case MCSymbolRefExpr::VK_None:
124 if (IsPCRel)
125 return getPCRelReloc(Kind);
126 return getAbsoluteReloc(Kind);
127
128 case MCSymbolRefExpr::VK_NTPOFF:
129 assert(!IsPCRel && "NTPOFF shouldn't be PC-relative");
130 return getTLSLEReloc(Kind);
131
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000132 case MCSymbolRefExpr::VK_INDNTPOFF:
133 if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
134 return ELF::R_390_TLS_IEENT;
135 llvm_unreachable("Only PC-relative INDNTPOFF accesses are supported for now");
136
137 case MCSymbolRefExpr::VK_DTPOFF:
138 assert(!IsPCRel && "DTPOFF shouldn't be PC-relative");
139 return getTLSLDOReloc(Kind);
140
141 case MCSymbolRefExpr::VK_TLSLDM:
142 assert(!IsPCRel && "TLSLDM shouldn't be PC-relative");
143 return getTLSLDMReloc(Kind);
144
145 case MCSymbolRefExpr::VK_TLSGD:
146 assert(!IsPCRel && "TLSGD shouldn't be PC-relative");
147 return getTLSGDReloc(Kind);
148
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000149 case MCSymbolRefExpr::VK_GOT:
150 if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
151 return ELF::R_390_GOTENT;
152 llvm_unreachable("Only PC-relative GOT accesses are supported for now");
153
154 case MCSymbolRefExpr::VK_PLT:
155 assert(IsPCRel && "@PLT shouldt be PC-relative");
156 return getPLTReloc(Kind);
157
158 default:
159 llvm_unreachable("Modifier not supported");
160 }
161}
162
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000163MCObjectWriter *llvm::createSystemZObjectWriter(raw_pwrite_stream &OS,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000164 uint8_t OSABI) {
165 MCELFObjectTargetWriter *MOTW = new SystemZObjectWriter(OSABI);
166 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false);
167}