LLVM 24.0.0git
AMDGPUELFObjectWriter.cpp
Go to the documentation of this file.
1//===- AMDGPUELFObjectWriter.cpp - AMDGPU ELF Writer ----------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "AMDGPUFixupKinds.h"
10#include "AMDGPUMCTargetDesc.h"
12#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCSymbolELF.h"
15#include "llvm/MC/MCValue.h"
16
17using namespace llvm;
18
19namespace {
20
21class AMDGPUELFObjectWriter : public MCELFObjectTargetWriter {
22public:
23 AMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI, bool HasRelocationAddend);
24
25protected:
26 unsigned getRelocType(const MCFixup &, const MCValue &,
27 bool IsPCRel) const override;
28};
29
30
31} // end anonymous namespace
32
33AMDGPUELFObjectWriter::AMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI,
34 bool HasRelocationAddend)
35 : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_AMDGPU,
36 HasRelocationAddend) {}
37
38unsigned AMDGPUELFObjectWriter::getRelocType(const MCFixup &Fixup,
39 const MCValue &Target,
40 bool IsPCRel) const {
41 if (const auto *SymA = Target.getAddSym()) {
42 // SCRATCH_RSRC_DWORD[01] is a special global variable that represents
43 // the scratch buffer.
44 if (SymA->getName() == "SCRATCH_RSRC_DWORD0" ||
45 SymA->getName() == "SCRATCH_RSRC_DWORD1")
46 return ELF::R_AMDGPU_ABS32_LO;
47 }
48
49 switch (AMDGPUMCExpr::Specifier(Target.getSpecifier())) {
50 default:
51 break;
53 return ELF::R_AMDGPU_GOTPCREL;
55 return ELF::R_AMDGPU_GOTPCREL32_LO;
57 return ELF::R_AMDGPU_GOTPCREL32_HI;
59 return ELF::R_AMDGPU_REL32_LO;
61 return ELF::R_AMDGPU_REL32_HI;
63 return ELF::R_AMDGPU_REL64;
65 return ELF::R_AMDGPU_ABS32_LO;
67 return ELF::R_AMDGPU_ABS32_HI;
69 return ELF::R_AMDGPU_ABS64;
70 }
71
72 MCFixupKind Kind = Fixup.getKind();
73 switch (Kind) {
74 default: break;
75 case FK_Data_4:
76 case FK_SecRel_4:
77 return IsPCRel ? ELF::R_AMDGPU_REL32 : ELF::R_AMDGPU_ABS32;
78 case FK_Data_8:
79 return IsPCRel ? ELF::R_AMDGPU_REL64 : ELF::R_AMDGPU_ABS64;
80 }
81
82 if (Fixup.getKind() == AMDGPU::fixup_si_sopp_br ||
84 const auto *SymA = Target.getAddSym();
85 if (!SymA) {
86 reportError(Fixup.getLoc(), "expected a label");
87 return ELF::R_AMDGPU_NONE;
88 }
89
90 if (SymA->isUndefined()) {
91 reportError(Fixup.getLoc(),
92 Twine("undefined label '") + SymA->getName() + "'");
93 return ELF::R_AMDGPU_NONE;
94 }
95 // No ELF relocation describes the 24-bit field at bits [55:32].
96 // A PC-relative fixup is left unresolved for a non-local symbol and for a
97 // local symbol defined in another section.
98 if (Fixup.getKind() == AMDGPU::fixup_si_smem_pcrel) {
99 const auto *SymELF = static_cast<const MCSymbolELF *>(SymA);
101 Fixup.getLoc(),
102 SymELF->getBinding() == ELF::STB_LOCAL
103 ? "pc-relative prefetch target must be in the same section"
104 : "pc-relative prefetch target must be a local label");
105 return ELF::R_AMDGPU_NONE;
106 }
107 return ELF::R_AMDGPU_REL16;
108 }
109
110 llvm_unreachable("unhandled relocation type");
111}
112
113std::unique_ptr<MCObjectTargetWriter>
115 bool HasRelocationAddend) {
116 return std::make_unique<AMDGPUELFObjectWriter>(Is64Bit, OSABI,
117 HasRelocationAddend);
118}
Provides AMDGPU specific target descriptions.
static Error reportError(StringRef Message)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:65
Target - Wrapper for Target specific information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ fixup_si_sopp_br
16-bit PC relative fixup for SOPP branch instructions.
@ fixup_si_smem_pcrel
24-bit PC-relative byte offset for s_prefetch_inst_pc_rel.
@ STB_LOCAL
Definition ELF.h:1260
This is an optimization pass for GlobalISel generic memory operations.
std::unique_ptr< MCObjectTargetWriter > createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI, bool HasRelocationAddend)
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:41
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:40
@ FK_SecRel_4
A four-byte section relative fixup.
Definition MCFixup.h:45