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 "AMDGPUFixupKinds.h"
10 #include "AMDGPUMCTargetDesc.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCELFObjectWriter.h"
13 #include "llvm/MC/MCValue.h"
19 class AMDGPUELFObjectWriter
: public MCELFObjectTargetWriter
{
21 AMDGPUELFObjectWriter(bool Is64Bit
, uint8_t OSABI
, bool HasRelocationAddend
,
25 unsigned getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
26 const MCFixup
&Fixup
, bool IsPCRel
) const override
;
30 } // end anonymous namespace
32 AMDGPUELFObjectWriter::AMDGPUELFObjectWriter(bool Is64Bit
,
34 bool HasRelocationAddend
,
36 : MCELFObjectTargetWriter(Is64Bit
, OSABI
, ELF::EM_AMDGPU
,
37 HasRelocationAddend
, ABIVersion
) {}
39 unsigned AMDGPUELFObjectWriter::getRelocType(MCContext
&Ctx
,
40 const MCValue
&Target
,
43 if (const auto *SymA
= Target
.getSymA()) {
44 // SCRATCH_RSRC_DWORD[01] is a special global variable that represents
45 // the scratch buffer.
46 if (SymA
->getSymbol().getName() == "SCRATCH_RSRC_DWORD0" ||
47 SymA
->getSymbol().getName() == "SCRATCH_RSRC_DWORD1")
48 return ELF::R_AMDGPU_ABS32_LO
;
51 switch (Target
.getAccessVariant()) {
54 case MCSymbolRefExpr::VK_GOTPCREL
:
55 return ELF::R_AMDGPU_GOTPCREL
;
56 case MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_LO
:
57 return ELF::R_AMDGPU_GOTPCREL32_LO
;
58 case MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_HI
:
59 return ELF::R_AMDGPU_GOTPCREL32_HI
;
60 case MCSymbolRefExpr::VK_AMDGPU_REL32_LO
:
61 return ELF::R_AMDGPU_REL32_LO
;
62 case MCSymbolRefExpr::VK_AMDGPU_REL32_HI
:
63 return ELF::R_AMDGPU_REL32_HI
;
64 case MCSymbolRefExpr::VK_AMDGPU_REL64
:
65 return ELF::R_AMDGPU_REL64
;
68 switch (Fixup
.getKind()) {
71 return ELF::R_AMDGPU_REL32
;
74 return ELF::R_AMDGPU_ABS32
;
76 return ELF::R_AMDGPU_ABS64
;
79 if (Fixup
.getTargetKind() == AMDGPU::fixup_si_sopp_br
) {
80 const auto *SymA
= Target
.getSymA();
83 if (SymA
->getSymbol().isUndefined()) {
84 Ctx
.reportError(Fixup
.getLoc(), Twine("undefined label '") +
85 SymA
->getSymbol().getName() + "'");
86 return ELF::R_AMDGPU_NONE
;
88 return ELF::R_AMDGPU_REL16
;
91 llvm_unreachable("unhandled relocation type");
94 std::unique_ptr
<MCObjectTargetWriter
>
95 llvm::createAMDGPUELFObjectWriter(bool Is64Bit
, uint8_t OSABI
,
96 bool HasRelocationAddend
,
98 return std::make_unique
<AMDGPUELFObjectWriter
>(Is64Bit
, OSABI
,