15f613dfdSUlrich Weigand //===-- SystemZMCObjectWriter.cpp - SystemZ ELF writer --------------------===//
25f613dfdSUlrich Weigand //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f613dfdSUlrich Weigand //
75f613dfdSUlrich Weigand //===----------------------------------------------------------------------===//
85f613dfdSUlrich Weigand
95f613dfdSUlrich Weigand #include "MCTargetDesc/SystemZMCFixups.h"
103943d2b0SEugene Zelenko #include "MCTargetDesc/SystemZMCTargetDesc.h"
11264b5d9eSZachary Turner #include "llvm/BinaryFormat/ELF.h"
125f613dfdSUlrich Weigand #include "llvm/MC/MCELFObjectWriter.h"
135f613dfdSUlrich Weigand #include "llvm/MC/MCExpr.h"
143943d2b0SEugene Zelenko #include "llvm/MC/MCFixup.h"
1560fbc7ccSLang Hames #include "llvm/MC/MCObjectWriter.h"
165f613dfdSUlrich Weigand #include "llvm/MC/MCValue.h"
173943d2b0SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
183943d2b0SEugene Zelenko #include <cassert>
193943d2b0SEugene Zelenko #include <cstdint>
205f613dfdSUlrich Weigand
215f613dfdSUlrich Weigand using namespace llvm;
225f613dfdSUlrich Weigand
235f613dfdSUlrich Weigand namespace {
243943d2b0SEugene Zelenko
255f613dfdSUlrich Weigand class SystemZObjectWriter : public MCELFObjectTargetWriter {
265f613dfdSUlrich Weigand public:
275f613dfdSUlrich Weigand SystemZObjectWriter(uint8_t OSABI);
283943d2b0SEugene Zelenko ~SystemZObjectWriter() override = default;
295f613dfdSUlrich Weigand
305f613dfdSUlrich Weigand protected:
315f613dfdSUlrich Weigand // Override MCELFObjectTargetWriter.
328340f94dSRafael Espindola unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
338340f94dSRafael Espindola const MCFixup &Fixup, bool IsPCRel) const override;
345f613dfdSUlrich Weigand };
353943d2b0SEugene Zelenko
36c231269fSRichard Sandiford } // end anonymous namespace
375f613dfdSUlrich Weigand
SystemZObjectWriter(uint8_t OSABI)385f613dfdSUlrich Weigand SystemZObjectWriter::SystemZObjectWriter(uint8_t OSABI)
3949a3ad21SRui Ueyama : MCELFObjectTargetWriter(/*Is64Bit_=*/true, OSABI, ELF::EM_S390,
4049a3ad21SRui Ueyama /*HasRelocationAddend_=*/ true) {}
415f613dfdSUlrich Weigand
425f613dfdSUlrich Weigand // Return the relocation type for an absolute value of MCFixupKind Kind.
getAbsoluteReloc(unsigned Kind)435f613dfdSUlrich Weigand static unsigned getAbsoluteReloc(unsigned Kind) {
445f613dfdSUlrich Weigand switch (Kind) {
455f613dfdSUlrich Weigand case FK_Data_1: return ELF::R_390_8;
465f613dfdSUlrich Weigand case FK_Data_2: return ELF::R_390_16;
475f613dfdSUlrich Weigand case FK_Data_4: return ELF::R_390_32;
485f613dfdSUlrich Weigand case FK_Data_8: return ELF::R_390_64;
49*1c3ef9efSJonas Paulsson case SystemZ::FK_390_12: return ELF::R_390_12;
50*1c3ef9efSJonas Paulsson case SystemZ::FK_390_20: return ELF::R_390_20;
515f613dfdSUlrich Weigand }
525f613dfdSUlrich Weigand llvm_unreachable("Unsupported absolute address");
535f613dfdSUlrich Weigand }
545f613dfdSUlrich Weigand
555f613dfdSUlrich Weigand // Return the relocation type for a PC-relative value of MCFixupKind Kind.
getPCRelReloc(unsigned Kind)565f613dfdSUlrich Weigand static unsigned getPCRelReloc(unsigned Kind) {
575f613dfdSUlrich Weigand switch (Kind) {
585f613dfdSUlrich Weigand case FK_Data_2: return ELF::R_390_PC16;
595f613dfdSUlrich Weigand case FK_Data_4: return ELF::R_390_PC32;
605f613dfdSUlrich Weigand case FK_Data_8: return ELF::R_390_PC64;
6184404f30SUlrich Weigand case SystemZ::FK_390_PC12DBL: return ELF::R_390_PC12DBL;
625f613dfdSUlrich Weigand case SystemZ::FK_390_PC16DBL: return ELF::R_390_PC16DBL;
6384404f30SUlrich Weigand case SystemZ::FK_390_PC24DBL: return ELF::R_390_PC24DBL;
645f613dfdSUlrich Weigand case SystemZ::FK_390_PC32DBL: return ELF::R_390_PC32DBL;
655f613dfdSUlrich Weigand }
665f613dfdSUlrich Weigand llvm_unreachable("Unsupported PC-relative address");
675f613dfdSUlrich Weigand }
685f613dfdSUlrich Weigand
695f613dfdSUlrich Weigand // Return the R_390_TLS_LE* relocation type for MCFixupKind Kind.
getTLSLEReloc(unsigned Kind)705f613dfdSUlrich Weigand static unsigned getTLSLEReloc(unsigned Kind) {
715f613dfdSUlrich Weigand switch (Kind) {
725f613dfdSUlrich Weigand case FK_Data_4: return ELF::R_390_TLS_LE32;
735f613dfdSUlrich Weigand case FK_Data_8: return ELF::R_390_TLS_LE64;
745f613dfdSUlrich Weigand }
755f613dfdSUlrich Weigand llvm_unreachable("Unsupported absolute address");
765f613dfdSUlrich Weigand }
775f613dfdSUlrich Weigand
787bdd7c23SUlrich Weigand // Return the R_390_TLS_LDO* relocation type for MCFixupKind Kind.
getTLSLDOReloc(unsigned Kind)797bdd7c23SUlrich Weigand static unsigned getTLSLDOReloc(unsigned Kind) {
807bdd7c23SUlrich Weigand switch (Kind) {
817bdd7c23SUlrich Weigand case FK_Data_4: return ELF::R_390_TLS_LDO32;
827bdd7c23SUlrich Weigand case FK_Data_8: return ELF::R_390_TLS_LDO64;
837bdd7c23SUlrich Weigand }
847bdd7c23SUlrich Weigand llvm_unreachable("Unsupported absolute address");
857bdd7c23SUlrich Weigand }
867bdd7c23SUlrich Weigand
877bdd7c23SUlrich Weigand // Return the R_390_TLS_LDM* relocation type for MCFixupKind Kind.
getTLSLDMReloc(unsigned Kind)887bdd7c23SUlrich Weigand static unsigned getTLSLDMReloc(unsigned Kind) {
897bdd7c23SUlrich Weigand switch (Kind) {
907bdd7c23SUlrich Weigand case FK_Data_4: return ELF::R_390_TLS_LDM32;
917bdd7c23SUlrich Weigand case FK_Data_8: return ELF::R_390_TLS_LDM64;
927bdd7c23SUlrich Weigand case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_LDCALL;
937bdd7c23SUlrich Weigand }
947bdd7c23SUlrich Weigand llvm_unreachable("Unsupported absolute address");
957bdd7c23SUlrich Weigand }
967bdd7c23SUlrich Weigand
977bdd7c23SUlrich Weigand // Return the R_390_TLS_GD* relocation type for MCFixupKind Kind.
getTLSGDReloc(unsigned Kind)987bdd7c23SUlrich Weigand static unsigned getTLSGDReloc(unsigned Kind) {
997bdd7c23SUlrich Weigand switch (Kind) {
1007bdd7c23SUlrich Weigand case FK_Data_4: return ELF::R_390_TLS_GD32;
1017bdd7c23SUlrich Weigand case FK_Data_8: return ELF::R_390_TLS_GD64;
1027bdd7c23SUlrich Weigand case SystemZ::FK_390_TLS_CALL: return ELF::R_390_TLS_GDCALL;
1037bdd7c23SUlrich Weigand }
1047bdd7c23SUlrich Weigand llvm_unreachable("Unsupported absolute address");
1057bdd7c23SUlrich Weigand }
1067bdd7c23SUlrich Weigand
1075f613dfdSUlrich Weigand // Return the PLT relocation counterpart of MCFixupKind Kind.
getPLTReloc(unsigned Kind)1085f613dfdSUlrich Weigand static unsigned getPLTReloc(unsigned Kind) {
1095f613dfdSUlrich Weigand switch (Kind) {
11084404f30SUlrich Weigand case SystemZ::FK_390_PC12DBL: return ELF::R_390_PLT12DBL;
1115f613dfdSUlrich Weigand case SystemZ::FK_390_PC16DBL: return ELF::R_390_PLT16DBL;
11284404f30SUlrich Weigand case SystemZ::FK_390_PC24DBL: return ELF::R_390_PLT24DBL;
1135f613dfdSUlrich Weigand case SystemZ::FK_390_PC32DBL: return ELF::R_390_PLT32DBL;
1145f613dfdSUlrich Weigand }
1155f613dfdSUlrich Weigand llvm_unreachable("Unsupported absolute address");
1165f613dfdSUlrich Weigand }
1175f613dfdSUlrich Weigand
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const1188340f94dSRafael Espindola unsigned SystemZObjectWriter::getRelocType(MCContext &Ctx,
1198340f94dSRafael Espindola const MCValue &Target,
120c03f44caSRafael Espindola const MCFixup &Fixup,
121c03f44caSRafael Espindola bool IsPCRel) const {
1225f613dfdSUlrich Weigand unsigned Kind = Fixup.getKind();
123b2674670SUlrich Weigand if (Kind >= FirstLiteralRelocationKind)
124b2674670SUlrich Weigand return Kind - FirstLiteralRelocationKind;
125b2674670SUlrich Weigand MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
1265f613dfdSUlrich Weigand switch (Modifier) {
1275f613dfdSUlrich Weigand case MCSymbolRefExpr::VK_None:
1285f613dfdSUlrich Weigand if (IsPCRel)
1295f613dfdSUlrich Weigand return getPCRelReloc(Kind);
1305f613dfdSUlrich Weigand return getAbsoluteReloc(Kind);
1315f613dfdSUlrich Weigand
1325f613dfdSUlrich Weigand case MCSymbolRefExpr::VK_NTPOFF:
1335f613dfdSUlrich Weigand assert(!IsPCRel && "NTPOFF shouldn't be PC-relative");
1345f613dfdSUlrich Weigand return getTLSLEReloc(Kind);
1355f613dfdSUlrich Weigand
1367bdd7c23SUlrich Weigand case MCSymbolRefExpr::VK_INDNTPOFF:
1377bdd7c23SUlrich Weigand if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
1387bdd7c23SUlrich Weigand return ELF::R_390_TLS_IEENT;
1397bdd7c23SUlrich Weigand llvm_unreachable("Only PC-relative INDNTPOFF accesses are supported for now");
1407bdd7c23SUlrich Weigand
1417bdd7c23SUlrich Weigand case MCSymbolRefExpr::VK_DTPOFF:
1427bdd7c23SUlrich Weigand assert(!IsPCRel && "DTPOFF shouldn't be PC-relative");
1437bdd7c23SUlrich Weigand return getTLSLDOReloc(Kind);
1447bdd7c23SUlrich Weigand
1457bdd7c23SUlrich Weigand case MCSymbolRefExpr::VK_TLSLDM:
1467bdd7c23SUlrich Weigand assert(!IsPCRel && "TLSLDM shouldn't be PC-relative");
1477bdd7c23SUlrich Weigand return getTLSLDMReloc(Kind);
1487bdd7c23SUlrich Weigand
1497bdd7c23SUlrich Weigand case MCSymbolRefExpr::VK_TLSGD:
1507bdd7c23SUlrich Weigand assert(!IsPCRel && "TLSGD shouldn't be PC-relative");
1517bdd7c23SUlrich Weigand return getTLSGDReloc(Kind);
1527bdd7c23SUlrich Weigand
1535f613dfdSUlrich Weigand case MCSymbolRefExpr::VK_GOT:
1545f613dfdSUlrich Weigand if (IsPCRel && Kind == SystemZ::FK_390_PC32DBL)
1555f613dfdSUlrich Weigand return ELF::R_390_GOTENT;
1565f613dfdSUlrich Weigand llvm_unreachable("Only PC-relative GOT accesses are supported for now");
1575f613dfdSUlrich Weigand
1585f613dfdSUlrich Weigand case MCSymbolRefExpr::VK_PLT:
1595f613dfdSUlrich Weigand assert(IsPCRel && "@PLT shouldt be PC-relative");
1605f613dfdSUlrich Weigand return getPLTReloc(Kind);
1615f613dfdSUlrich Weigand
1625f613dfdSUlrich Weigand default:
1635f613dfdSUlrich Weigand llvm_unreachable("Modifier not supported");
1645f613dfdSUlrich Weigand }
1655f613dfdSUlrich Weigand }
1665f613dfdSUlrich Weigand
167dcd7d6c3SPeter Collingbourne std::unique_ptr<MCObjectTargetWriter>
createSystemZObjectWriter(uint8_t OSABI)168dcd7d6c3SPeter Collingbourne llvm::createSystemZObjectWriter(uint8_t OSABI) {
1690eaee545SJonas Devlieghere return std::make_unique<SystemZObjectWriter>(OSABI);
1705f613dfdSUlrich Weigand }
171