| Eugene Zelenko | e894b4d | 2017-02-03 23:38:40 +0000 | [diff] [blame] | 1 | //===- AMDGPUELFObjectWriter.cpp - AMDGPU ELF Writer ----------------------===// |
| Tom Stellard | 9991659 | 2013-04-15 17:51:21 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Tom Stellard | 9991659 | 2013-04-15 17:51:21 +0000 | [diff] [blame] | 6 | // |
| Tom Stellard | 9991659 | 2013-04-15 17:51:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "AMDGPUMCTargetDesc.h" |
| Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 10 | #include "llvm/BinaryFormat/ELF.h" |
| Tom Stellard | 9991659 | 2013-04-15 17:51:21 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCELFObjectWriter.h" |
| Eugene Zelenko | e894b4d | 2017-02-03 23:38:40 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCExpr.h" |
| Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCFixup.h" |
| Lang Hames | 60fbc7c | 2017-10-10 16:28:07 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCObjectWriter.h" |
| Eugene Zelenko | e894b4d | 2017-02-03 23:38:40 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSymbol.h" |
| 16 | #include "llvm/MC/MCValue.h" |
| Eugene Zelenko | e894b4d | 2017-02-03 23:38:40 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" |
| Tom Stellard | 9991659 | 2013-04-15 17:51:21 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class AMDGPUELFObjectWriter : public MCELFObjectTargetWriter { |
| 24 | public: |
| Konstantin Zhuravlyov | 1e126c5 | 2019-02-14 23:56:04 +0000 | [diff] [blame] | 25 | AMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI, bool HasRelocationAddend, |
| 26 | uint8_t ABIVersion); |
| Eugene Zelenko | e894b4d | 2017-02-03 23:38:40 +0000 | [diff] [blame] | 27 | |
| Tom Stellard | 9991659 | 2013-04-15 17:51:21 +0000 | [diff] [blame] | 28 | protected: |
| Rafael Espindola | 8340f94 | 2016-01-13 22:56:57 +0000 | [diff] [blame] | 29 | unsigned getRelocType(MCContext &Ctx, const MCValue &Target, |
| Tom Stellard | 1c89eb7 | 2016-06-20 16:59:44 +0000 | [diff] [blame] | 30 | const MCFixup &Fixup, bool IsPCRel) const override; |
| Tom Stellard | 9991659 | 2013-04-15 17:51:21 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | |
| Eugene Zelenko | e894b4d | 2017-02-03 23:38:40 +0000 | [diff] [blame] | 34 | } // end anonymous namespace |
| Tom Stellard | 9991659 | 2013-04-15 17:51:21 +0000 | [diff] [blame] | 35 | |
| Tom Stellard | bf3e6e5 | 2016-06-14 20:29:59 +0000 | [diff] [blame] | 36 | AMDGPUELFObjectWriter::AMDGPUELFObjectWriter(bool Is64Bit, |
| Konstantin Zhuravlyov | 8684f7b | 2017-10-04 22:44:13 +0000 | [diff] [blame] | 37 | uint8_t OSABI, |
| Konstantin Zhuravlyov | 1e126c5 | 2019-02-14 23:56:04 +0000 | [diff] [blame] | 38 | bool HasRelocationAddend, |
| 39 | uint8_t ABIVersion) |
| Konstantin Zhuravlyov | 8684f7b | 2017-10-04 22:44:13 +0000 | [diff] [blame] | 40 | : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_AMDGPU, |
| Konstantin Zhuravlyov | 1e126c5 | 2019-02-14 23:56:04 +0000 | [diff] [blame] | 41 | HasRelocationAddend, ABIVersion) {} |
| Tom Stellard | 9991659 | 2013-04-15 17:51:21 +0000 | [diff] [blame] | 42 | |
| Tom Stellard | 1c89eb7 | 2016-06-20 16:59:44 +0000 | [diff] [blame] | 43 | unsigned AMDGPUELFObjectWriter::getRelocType(MCContext &Ctx, |
| 44 | const MCValue &Target, |
| 45 | const MCFixup &Fixup, |
| 46 | bool IsPCRel) const { |
| Matt Arsenault | 6bc43d8 | 2016-10-06 16:20:41 +0000 | [diff] [blame] | 47 | if (const auto *SymA = Target.getSymA()) { |
| 48 | // SCRATCH_RSRC_DWORD[01] is a special global variable that represents |
| 49 | // the scratch buffer. |
| Nicolai Haehnle | 8d5e974 | 2018-12-19 11:55:03 +0000 | [diff] [blame] | 50 | if (SymA->getSymbol().getName() == "SCRATCH_RSRC_DWORD0" || |
| 51 | SymA->getSymbol().getName() == "SCRATCH_RSRC_DWORD1") |
| Matt Arsenault | 6bc43d8 | 2016-10-06 16:20:41 +0000 | [diff] [blame] | 52 | return ELF::R_AMDGPU_ABS32_LO; |
| Matt Arsenault | 6bc43d8 | 2016-10-06 16:20:41 +0000 | [diff] [blame] | 53 | } |
| Tom Stellard | 1c89eb7 | 2016-06-20 16:59:44 +0000 | [diff] [blame] | 54 | |
| Tom Stellard | 14416ae | 2016-06-23 23:11:29 +0000 | [diff] [blame] | 55 | switch (Target.getAccessVariant()) { |
| 56 | default: |
| 57 | break; |
| 58 | case MCSymbolRefExpr::VK_GOTPCREL: |
| 59 | return ELF::R_AMDGPU_GOTPCREL; |
| Konstantin Zhuravlyov | 2a2ac37 | 2016-10-14 04:21:32 +0000 | [diff] [blame] | 60 | case MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_LO: |
| 61 | return ELF::R_AMDGPU_GOTPCREL32_LO; |
| 62 | case MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_HI: |
| 63 | return ELF::R_AMDGPU_GOTPCREL32_HI; |
| 64 | case MCSymbolRefExpr::VK_AMDGPU_REL32_LO: |
| 65 | return ELF::R_AMDGPU_REL32_LO; |
| 66 | case MCSymbolRefExpr::VK_AMDGPU_REL32_HI: |
| 67 | return ELF::R_AMDGPU_REL32_HI; |
| Konstantin Zhuravlyov | 3e5d66a | 2018-06-11 21:37:57 +0000 | [diff] [blame] | 68 | case MCSymbolRefExpr::VK_AMDGPU_REL64: |
| 69 | return ELF::R_AMDGPU_REL64; |
| Tom Stellard | 14416ae | 2016-06-23 23:11:29 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| Tom Stellard | 5350894 | 2016-06-20 17:33:43 +0000 | [diff] [blame] | 72 | switch (Fixup.getKind()) { |
| 73 | default: break; |
| 74 | case FK_PCRel_4: |
| 75 | return ELF::R_AMDGPU_REL32; |
| Konstantin Zhuravlyov | 1556262 | 2016-07-21 15:29:19 +0000 | [diff] [blame] | 76 | case FK_Data_4: |
| Jan Vesely | 3bc1af2 | 2016-06-25 18:24:16 +0000 | [diff] [blame] | 77 | case FK_SecRel_4: |
| 78 | return ELF::R_AMDGPU_ABS32; |
| Tom Stellard | 6982bb8 | 2016-10-07 20:36:58 +0000 | [diff] [blame] | 79 | case FK_Data_8: |
| 80 | return ELF::R_AMDGPU_ABS64; |
| Tom Stellard | 5350894 | 2016-06-20 17:33:43 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| Tom Stellard | 1c89eb7 | 2016-06-20 16:59:44 +0000 | [diff] [blame] | 83 | llvm_unreachable("unhandled relocation type"); |
| 84 | } |
| 85 | |
| Peter Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 86 | std::unique_ptr<MCObjectTargetWriter> |
| Lang Hames | 60fbc7c | 2017-10-10 16:28:07 +0000 | [diff] [blame] | 87 | llvm::createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI, |
| Konstantin Zhuravlyov | 1e126c5 | 2019-02-14 23:56:04 +0000 | [diff] [blame] | 88 | bool HasRelocationAddend, |
| 89 | uint8_t ABIVersion) { |
| Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 90 | return std::make_unique<AMDGPUELFObjectWriter>(Is64Bit, OSABI, |
| Konstantin Zhuravlyov | 1e126c5 | 2019-02-14 23:56:04 +0000 | [diff] [blame] | 91 | HasRelocationAddend, |
| 92 | ABIVersion); |
| Tom Stellard | 9991659 | 2013-04-15 17:51:21 +0000 | [diff] [blame] | 93 | } |