1 //===- AMDGPUELFObjectWriter.cpp - AMDGPU ELF Writer ----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "AMDGPUMCTargetDesc.h"
10 #include "llvm/BinaryFormat/ELF.h"
11 #include "llvm/MC/MCELFObjectWriter.h"
12 #include "llvm/MC/MCExpr.h"
13 #include "llvm/MC/MCFixup.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/Support/ErrorHandling.h"
23 class AMDGPUELFObjectWriter
: public MCELFObjectTargetWriter
{
25 AMDGPUELFObjectWriter(bool Is64Bit
, uint8_t OSABI
, bool HasRelocationAddend
,
29 unsigned getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
30 const MCFixup
&Fixup
, bool IsPCRel
) const override
;
34 } // end anonymous namespace
36 AMDGPUELFObjectWriter::AMDGPUELFObjectWriter(bool Is64Bit
,
38 bool HasRelocationAddend
,
40 : MCELFObjectTargetWriter(Is64Bit
, OSABI
, ELF::EM_AMDGPU
,
41 HasRelocationAddend
, ABIVersion
) {}
43 unsigned AMDGPUELFObjectWriter::getRelocType(MCContext
&Ctx
,
44 const MCValue
&Target
,
47 if (const auto *SymA
= Target
.getSymA()) {
48 // SCRATCH_RSRC_DWORD[01] is a special global variable that represents
49 // the scratch buffer.
50 if (SymA
->getSymbol().getName() == "SCRATCH_RSRC_DWORD0" ||
51 SymA
->getSymbol().getName() == "SCRATCH_RSRC_DWORD1")
52 return ELF::R_AMDGPU_ABS32_LO
;
55 switch (Target
.getAccessVariant()) {
58 case MCSymbolRefExpr::VK_GOTPCREL
:
59 return ELF::R_AMDGPU_GOTPCREL
;
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
;
68 case MCSymbolRefExpr::VK_AMDGPU_REL64
:
69 return ELF::R_AMDGPU_REL64
;
72 switch (Fixup
.getKind()) {
75 return ELF::R_AMDGPU_REL32
;
78 return ELF::R_AMDGPU_ABS32
;
80 return ELF::R_AMDGPU_ABS64
;
83 llvm_unreachable("unhandled relocation type");
86 std::unique_ptr
<MCObjectTargetWriter
>
87 llvm::createAMDGPUELFObjectWriter(bool Is64Bit
, uint8_t OSABI
,
88 bool HasRelocationAddend
,
90 return std::make_unique
<AMDGPUELFObjectWriter
>(Is64Bit
, OSABI
,