LLVM  8.0.1
AMDGPUELFObjectWriter.cpp
Go to the documentation of this file.
1 //===- AMDGPUELFObjectWriter.cpp - AMDGPU ELF Writer ----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "AMDGPUMCTargetDesc.h"
11 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCFixup.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCSymbol.h"
17 #include "llvm/MC/MCValue.h"
19 
20 using namespace llvm;
21 
22 namespace {
23 
24 class AMDGPUELFObjectWriter : public MCELFObjectTargetWriter {
25 public:
26  AMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI, bool HasRelocationAddend);
27 
28 protected:
29  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
30  const MCFixup &Fixup, bool IsPCRel) const override;
31 };
32 
33 
34 } // end anonymous namespace
35 
36 AMDGPUELFObjectWriter::AMDGPUELFObjectWriter(bool Is64Bit,
37  uint8_t OSABI,
38  bool HasRelocationAddend)
39  : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_AMDGPU,
40  HasRelocationAddend) {}
41 
43  const MCValue &Target,
44  const MCFixup &Fixup,
45  bool IsPCRel) const {
46  if (const auto *SymA = Target.getSymA()) {
47  // SCRATCH_RSRC_DWORD[01] is a special global variable that represents
48  // the scratch buffer.
49  if (SymA->getSymbol().getName() == "SCRATCH_RSRC_DWORD0" ||
50  SymA->getSymbol().getName() == "SCRATCH_RSRC_DWORD1")
51  return ELF::R_AMDGPU_ABS32_LO;
52  }
53 
54  switch (Target.getAccessVariant()) {
55  default:
56  break;
58  return ELF::R_AMDGPU_GOTPCREL;
60  return ELF::R_AMDGPU_GOTPCREL32_LO;
62  return ELF::R_AMDGPU_GOTPCREL32_HI;
64  return ELF::R_AMDGPU_REL32_LO;
66  return ELF::R_AMDGPU_REL32_HI;
68  return ELF::R_AMDGPU_REL64;
69  }
70 
71  switch (Fixup.getKind()) {
72  default: break;
73  case FK_PCRel_4:
74  return ELF::R_AMDGPU_REL32;
75  case FK_Data_4:
76  case FK_SecRel_4:
77  return ELF::R_AMDGPU_ABS32;
78  case FK_Data_8:
79  return ELF::R_AMDGPU_ABS64;
80  }
81 
82  llvm_unreachable("unhandled relocation type");
83 }
84 
85 std::unique_ptr<MCObjectTargetWriter>
86 llvm::createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI,
87  bool HasRelocationAddend) {
88  return llvm::make_unique<AMDGPUELFObjectWriter>(Is64Bit, OSABI,
89  HasRelocationAddend);
90 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbolRefExpr::VariantKind getAccessVariant() const
Definition: MCValue.cpp:47
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:74
static unsigned getRelocType(const MCValue &Target, const MCFixupKind FixupKind, const bool IsPCRel)
Translates generic PPC fixup kind to Mach-O/PPC relocation type enum.
A four-byte section relative fixup.
Definition: MCFixup.h:42
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:63
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:48
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A four-byte pc relative fixup.
Definition: MCFixup.h:30
Target - Wrapper for Target specific information.
Provides AMDGPU specific target descriptions.
std::unique_ptr< MCObjectTargetWriter > createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI, bool HasRelocationAddend)
A eight-byte fixup.
Definition: MCFixup.h:27
MCFixupKind getKind() const
Definition: MCFixup.h:123