blob: 888be519fb1637f8b0ea2d5114ba272d315abf34 [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"
Zachary Turner264b5d92017-06-07 03:48:56 +000012#include "llvm/BinaryFormat/ELF.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000013#include "llvm/MC/MCELFObjectWriter.h"
14#include "llvm/MC/MCExpr.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000015#include "llvm/MC/MCFixup.h"
Lang Hames60fbc7c2017-10-10 16:28:07 +000016#include "llvm/MC/MCObjectWriter.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000017#include "llvm/MC/MCValue.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000018#include "llvm/Support/ErrorHandling.h"
19#include <cassert>
20#include <cstdint>
Ulrich Weigand5f613df2013-05-06 16:15:19 +000021
22using namespace llvm;
23
24namespace {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000025
Ulrich Weigand5f613df2013-05-06 16:15:19 +000026class SystemZObjectWriter : public MCELFObjectTargetWriter {
27public:
28 SystemZObjectWriter(uint8_t OSABI);
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000029 ~SystemZObjectWriter() override = default;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000030
31protected:
32 // Override MCELFObjectTargetWriter.
Rafael Espindola8340f942016-01-13 22:56:57 +000033 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
34 const MCFixup &Fixup, bool IsPCRel) const override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000035};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000036
Richard Sandifordc2312692014-03-06 10:38:30 +000037} // end anonymous namespace
Ulrich Weigand5f613df2013-05-06 16:15:19 +000038
39SystemZObjectWriter::SystemZObjectWriter(uint8_t OSABI)
40 : MCELFObjectTargetWriter(/*Is64Bit=*/true, OSABI, ELF::EM_S390,
41 /*HasRelocationAddend=*/ true) {}
42
Ulrich Weigand5f613df2013-05-06 16:15:19 +000043// Return the relocation type for an absolute value of MCFixupKind Kind.
44static unsigned getAbsoluteReloc(unsigned Kind) {
45 switch (Kind) {
46 case FK_Data_1: return ELF::R_390_8;
47 case FK_Data_2: return ELF::R_390_16;
48 case FK_Data_4: return ELF::R_390_32;
49 case FK_Data_8: return ELF::R_390_64;
50 }
51 llvm_unreachable("Unsupported absolute address");
52}
53
54// Return the relocation type for a PC-relative value of MCFixupKind Kind.
55static unsigned getPCRelReloc(unsigned Kind) {
56 switch (Kind) {
57 case FK_Data_2: return ELF::R_390_PC16;
58 case FK_Data_4: return ELF::R_390_PC32;
59 case FK_Data_8: return ELF::R_390_PC64;
Ulrich Weigand84404f32016-11-28 14:01:51 +000060 case SystemZ::FK_390_PC12DBL: return ELF::R_390_PC12DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000061 case SystemZ::FK_390_PC16DBL: return ELF::R_390_PC16DBL;
Ulrich Weigand84404f32016-11-28 14:01:51 +000062 case SystemZ::FK_390_PC24DBL: return ELF::R_390_PC24DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000063 case SystemZ::FK_390_PC32DBL: return ELF::R_390_PC32DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +000064 }
65 llvm_unreachable("Unsupported PC-relative address");
66}
67
68// Return the R_390_TLS_LE* relocation type for MCFixupKind Kind.
69static unsigned getTLSLEReloc(unsigned Kind) {
70 switch (Kind) {
71 case FK_Data_4: return ELF::R_390_TLS_LE32;
72 case FK_Data_8: return ELF::R_390_TLS_LE64;
73 }
74 llvm_unreachable("Unsupported absolute address");
75}
76
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +000077// Return the R_390_TLS_LDO* relocation type for MCFixupKind Kind.
78static unsigned getTLSLDOReloc(unsigned Kind) {
79 switch (Kind) {
80 case FK_Data_4: return ELF::R_390_TLS_LDO32;
81 case FK_Data_8: return ELF::R_390_TLS_LDO64;
82 }
83 llvm_unreachable("Unsupported absolute address");
84}
85
86// Return the R_390_TLS_LDM* relocation type for MCFixupKind Kind.
87static unsigned getTLSLDMReloc(unsigned Kind) {
88 switch (Kind) {
89 case FK_Data_4: return ELF::R_390_TLS_LDM32;
90 case FK_Data_8: return ELF::R_390_TLS_LDM64;
91 case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_LDCALL;
92 }
93 llvm_unreachable("Unsupported absolute address");
94}
95
96// Return the R_390_TLS_GD* relocation type for MCFixupKind Kind.
97static unsigned getTLSGDReloc(unsigned Kind) {
98 switch (Kind) {
99 case FK_Data_4: return ELF::R_390_TLS_GD32;
100 case FK_Data_8: return ELF::R_390_TLS_GD64;
101 case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_GDCALL;
102 }
103 llvm_unreachable("Unsupported absolute address");
104}
105
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000106// Return the PLT relocation counterpart of MCFixupKind Kind.
107static unsigned getPLTReloc(unsigned Kind) {
108 switch (Kind) {
Ulrich Weigand84404f32016-11-28 14:01:51 +0000109 case SystemZ::FK_390_PC12DBL: return ELF::R_390_PLT12DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000110 case SystemZ::FK_390_PC16DBL: return ELF::R_390_PLT16DBL;
Ulrich Weigand84404f32016-11-28 14:01:51 +0000111 case SystemZ::FK_390_PC24DBL: return ELF::R_390_PLT24DBL;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000112 case SystemZ::FK_390_PC32DBL: return ELF::R_390_PLT32DBL;
113 }
114 llvm_unreachable("Unsupported absolute address");
115}
116
Rafael Espindola8340f942016-01-13 22:56:57 +0000117unsigned SystemZObjectWriter::getRelocType(MCContext &Ctx,
118 const MCValue &Target,
Rafael Espindolac03f44c2014-03-27 20:49:35 +0000119 const MCFixup &Fixup,
120 bool IsPCRel) const {
Rafael Espindola3d082fa2014-05-03 19:57:04 +0000121 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000122 unsigned Kind = Fixup.getKind();
123 switch (Modifier) {
124 case MCSymbolRefExpr::VK_None:
125 if (IsPCRel)
126 return getPCRelReloc(Kind);
127 return getAbsoluteReloc(Kind);
128
129 case MCSymbolRefExpr::VK_NTPOFF:
130 assert(!IsPCRel && "NTPOFF shouldn't be PC-relative");
131 return getTLSLEReloc(Kind);
132
Ulrich Weigand7bdd7c22015-02-18 09:11:36 +0000133 case MCSymbolRefExpr::VK_INDNTPOFF:
134 if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
135 return ELF::R_390_TLS_IEENT;
136 llvm_unreachable("Only PC-relative INDNTPOFF accesses are supported for now");
137
138 case MCSymbolRefExpr::VK_DTPOFF:
139 assert(!IsPCRel && "DTPOFF shouldn't be PC-relative");
140 return getTLSLDOReloc(Kind);
141
142 case MCSymbolRefExpr::VK_TLSLDM:
143 assert(!IsPCRel && "TLSLDM shouldn't be PC-relative");
144 return getTLSLDMReloc(Kind);
145
146 case MCSymbolRefExpr::VK_TLSGD:
147 assert(!IsPCRel && "TLSGD shouldn't be PC-relative");
148 return getTLSGDReloc(Kind);
149
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000150 case MCSymbolRefExpr::VK_GOT:
151 if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
152 return ELF::R_390_GOTENT;
153 llvm_unreachable("Only PC-relative GOT accesses are supported for now");
154
155 case MCSymbolRefExpr::VK_PLT:
156 assert(IsPCRel && "@PLT shouldt be PC-relative");
157 return getPLTReloc(Kind);
158
159 default:
160 llvm_unreachable("Modifier not supported");
161 }
162}
163
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000164std::unique_ptr<MCObjectTargetWriter>
165llvm::createSystemZObjectWriter(uint8_t OSABI) {
166 return llvm::make_unique<SystemZObjectWriter>(OSABI);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000167}