| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 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 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/SystemZMCFixups.h" |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/SystemZMCTargetDesc.h" |
| Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 12 | #include "llvm/BinaryFormat/ELF.h" |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCELFObjectWriter.h" |
| 14 | #include "llvm/MC/MCExpr.h" |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCFixup.h" |
| Lang Hames | 60fbc7c | 2017-10-10 16:28:07 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCObjectWriter.h" |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCValue.h" |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | #include <cassert> |
| 20 | #include <cstdint> |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 25 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 26 | class SystemZObjectWriter : public MCELFObjectTargetWriter { |
| 27 | public: |
| 28 | SystemZObjectWriter(uint8_t OSABI); |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 29 | ~SystemZObjectWriter() override = default; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 30 | |
| 31 | protected: |
| 32 | // Override MCELFObjectTargetWriter. |
| Rafael Espindola | 8340f94 | 2016-01-13 22:56:57 +0000 | [diff] [blame] | 33 | unsigned getRelocType(MCContext &Ctx, const MCValue &Target, |
| 34 | const MCFixup &Fixup, bool IsPCRel) const override; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 35 | }; |
| Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 36 | |
| Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 37 | } // end anonymous namespace |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 38 | |
| 39 | SystemZObjectWriter::SystemZObjectWriter(uint8_t OSABI) |
| 40 | : MCELFObjectTargetWriter(/*Is64Bit=*/true, OSABI, ELF::EM_S390, |
| 41 | /*HasRelocationAddend=*/ true) {} |
| 42 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 43 | // Return the relocation type for an absolute value of MCFixupKind Kind. |
| 44 | static 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. |
| 55 | static 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 Weigand | 84404f3 | 2016-11-28 14:01:51 +0000 | [diff] [blame] | 60 | case SystemZ::FK_390_PC12DBL: return ELF::R_390_PC12DBL; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 61 | case SystemZ::FK_390_PC16DBL: return ELF::R_390_PC16DBL; |
| Ulrich Weigand | 84404f3 | 2016-11-28 14:01:51 +0000 | [diff] [blame] | 62 | case SystemZ::FK_390_PC24DBL: return ELF::R_390_PC24DBL; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 63 | case SystemZ::FK_390_PC32DBL: return ELF::R_390_PC32DBL; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 64 | } |
| 65 | llvm_unreachable("Unsupported PC-relative address"); |
| 66 | } |
| 67 | |
| 68 | // Return the R_390_TLS_LE* relocation type for MCFixupKind Kind. |
| 69 | static 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 Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 77 | // Return the R_390_TLS_LDO* relocation type for MCFixupKind Kind. |
| 78 | static 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. |
| 87 | static 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. |
| 97 | static 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 Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 106 | // Return the PLT relocation counterpart of MCFixupKind Kind. |
| 107 | static unsigned getPLTReloc(unsigned Kind) { |
| 108 | switch (Kind) { |
| Ulrich Weigand | 84404f3 | 2016-11-28 14:01:51 +0000 | [diff] [blame] | 109 | case SystemZ::FK_390_PC12DBL: return ELF::R_390_PLT12DBL; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 110 | case SystemZ::FK_390_PC16DBL: return ELF::R_390_PLT16DBL; |
| Ulrich Weigand | 84404f3 | 2016-11-28 14:01:51 +0000 | [diff] [blame] | 111 | case SystemZ::FK_390_PC24DBL: return ELF::R_390_PLT24DBL; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 112 | case SystemZ::FK_390_PC32DBL: return ELF::R_390_PLT32DBL; |
| 113 | } |
| 114 | llvm_unreachable("Unsupported absolute address"); |
| 115 | } |
| 116 | |
| Rafael Espindola | 8340f94 | 2016-01-13 22:56:57 +0000 | [diff] [blame] | 117 | unsigned SystemZObjectWriter::getRelocType(MCContext &Ctx, |
| 118 | const MCValue &Target, |
| Rafael Espindola | c03f44c | 2014-03-27 20:49:35 +0000 | [diff] [blame] | 119 | const MCFixup &Fixup, |
| 120 | bool IsPCRel) const { |
| Rafael Espindola | 3d082fa | 2014-05-03 19:57:04 +0000 | [diff] [blame] | 121 | MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant(); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 122 | 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 Weigand | 7bdd7c2 | 2015-02-18 09:11:36 +0000 | [diff] [blame] | 133 | 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 Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 150 | 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 Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 164 | std::unique_ptr<MCObjectTargetWriter> |
| 165 | llvm::createSystemZObjectWriter(uint8_t OSABI) { |
| 166 | return llvm::make_unique<SystemZObjectWriter>(OSABI); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 167 | } |