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 /// \file
9 //===----------------------------------------------------------------------===//
10 
11 #include "AMDGPUMCTargetDesc.h"
12 #include "llvm/MC/MCELFObjectWriter.h"
13 #include "llvm/MC/MCFixup.h"
14 
15 using namespace llvm;
16 
17 namespace {
18 
19 class AMDGPUELFObjectWriter : public MCELFObjectTargetWriter {
20 public:
21   AMDGPUELFObjectWriter(bool Is64Bit, bool HasRelocationAddend);
22 protected:
23   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
24                         const MCFixup &Fixup, bool IsPCRel) const override;
25 };
26 
27 
28 } // End anonymous namespace
29 
AMDGPUELFObjectWriter(bool Is64Bit,bool HasRelocationAddend)30 AMDGPUELFObjectWriter::AMDGPUELFObjectWriter(bool Is64Bit,
31                                              bool HasRelocationAddend)
32   : MCELFObjectTargetWriter(Is64Bit,
33                             ELF::ELFOSABI_AMDGPU_HSA,
34                             ELF::EM_AMDGPU,
35                             HasRelocationAddend) { }
36 
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const37 unsigned AMDGPUELFObjectWriter::getRelocType(MCContext &Ctx,
38                                              const MCValue &Target,
39                                              const MCFixup &Fixup,
40                                              bool IsPCRel) const {
41   // SCRATCH_RSRC_DWORD[01] is a special global variable that represents
42   // the scratch buffer.
43   if (Target.getSymA()->getSymbol().getName() == "SCRATCH_RSRC_DWORD0")
44     return ELF::R_AMDGPU_ABS32_LO;
45   if (Target.getSymA()->getSymbol().getName() == "SCRATCH_RSRC_DWORD1")
46     return ELF::R_AMDGPU_ABS32_HI;
47 
48   switch (Target.getAccessVariant()) {
49   default:
50     break;
51   case MCSymbolRefExpr::VK_GOTPCREL:
52     return ELF::R_AMDGPU_GOTPCREL;
53   }
54 
55   switch (Fixup.getKind()) {
56   default: break;
57   case FK_PCRel_4:
58     return ELF::R_AMDGPU_REL32;
59   case FK_SecRel_4:
60     return ELF::R_AMDGPU_ABS32;
61   }
62 
63   llvm_unreachable("unhandled relocation type");
64 }
65 
66 
createAMDGPUELFObjectWriter(bool Is64Bit,bool HasRelocationAddend,raw_pwrite_stream & OS)67 MCObjectWriter *llvm::createAMDGPUELFObjectWriter(bool Is64Bit,
68                                                   bool HasRelocationAddend,
69                                                   raw_pwrite_stream &OS) {
70   MCELFObjectTargetWriter *MOTW =
71       new AMDGPUELFObjectWriter(Is64Bit, HasRelocationAddend);
72   return createELFObjectWriter(MOTW, OS, true);
73 }
74