LLVM  13.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"
11 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCValue.h"
14 
15 using namespace llvm;
16 
17 namespace {
18 
19 class AMDGPUELFObjectWriter : public MCELFObjectTargetWriter {
20 public:
21  AMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI, bool HasRelocationAddend,
22  uint8_t ABIVersion);
23 
24 protected:
25  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
26  const MCFixup &Fixup, bool IsPCRel) const override;
27 };
28 
29 
30 } // end anonymous namespace
31 
32 AMDGPUELFObjectWriter::AMDGPUELFObjectWriter(bool Is64Bit,
33  uint8_t OSABI,
34  bool HasRelocationAddend,
35  uint8_t ABIVersion)
36  : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_AMDGPU,
37  HasRelocationAddend, ABIVersion) {}
38 
39 unsigned AMDGPUELFObjectWriter::getRelocType(MCContext &Ctx,
40  const MCValue &Target,
41  const MCFixup &Fixup,
42  bool IsPCRel) const {
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;
49  }
50 
51  switch (Target.getAccessVariant()) {
52  default:
53  break;
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;
66  }
67 
68  switch (Fixup.getKind()) {
69  default: break;
70  case FK_PCRel_4:
71  return ELF::R_AMDGPU_REL32;
72  case FK_Data_4:
73  case FK_SecRel_4:
74  return ELF::R_AMDGPU_ABS32;
75  case FK_Data_8:
76  return ELF::R_AMDGPU_ABS64;
77  }
78 
79  if (Fixup.getTargetKind() == AMDGPU::fixup_si_sopp_br) {
80  const auto *SymA = Target.getSymA();
81  assert(SymA);
82 
83  Ctx.reportError(Fixup.getLoc(),
84  Twine("undefined label '") + SymA->getSymbol().getName() + "'");
85  return ELF::R_AMDGPU_NONE;
86  }
87 
88  llvm_unreachable("unhandled relocation type");
89 }
90 
91 std::unique_ptr<MCObjectTargetWriter>
92 llvm::createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI,
93  bool HasRelocationAddend,
94  uint8_t ABIVersion) {
95  return std::make_unique<AMDGPUELFObjectWriter>(Is64Bit, OSABI,
96  HasRelocationAddend,
97  ABIVersion);
98 }
llvm
Definition: AllocatorList.h:23
AMDGPUFixupKinds.h
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:71
llvm::AMDGPU::fixup_si_sopp_br
@ fixup_si_sopp_br
16-bit PC relative fixup for SOPP branch instructions.
Definition: AMDGPUFixupKinds.h:18
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:125
llvm::FK_Data_4
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
llvm::FK_SecRel_4
@ FK_SecRel_4
A four-byte section relative fixup.
Definition: MCFixup.h:42
MCContext.h
llvm::ELF::EM_AMDGPU
@ EM_AMDGPU
Definition: ELF.h:315
llvm::MCELFObjectTargetWriter
Definition: MCELFObjectWriter.h:53
AMDGPUMCTargetDesc.h
MCELFObjectWriter.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCContext::reportError
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:948
llvm::FK_PCRel_4
@ FK_PCRel_4
A four-byte pc relative fixup.
Definition: MCFixup.h:30
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:235
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
MCValue.h
llvm::FK_Data_8
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:37
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:81
llvm::createAMDGPUELFObjectWriter
std::unique_ptr< MCObjectTargetWriter > createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI, bool HasRelocationAddend, uint8_t ABIVersion)
Definition: AMDGPUELFObjectWriter.cpp:92