2017-02-04 07:38:40 +08:00
|
|
|
//===- AMDGPUELFObjectWriter.cpp - AMDGPU ELF Writer ----------------------===//
|
2013-04-16 01:51:21 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDGPUMCTargetDesc.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2013-04-16 01:51:21 +08:00
|
|
|
#include "llvm/MC/MCELFObjectWriter.h"
|
2017-02-04 07:38:40 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2014-07-21 22:01:14 +08:00
|
|
|
#include "llvm/MC/MCFixup.h"
|
2017-10-11 00:28:07 +08:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
2017-02-04 07:38:40 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/MC/MCValue.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-04-16 01:51:21 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AMDGPUELFObjectWriter : public MCELFObjectTargetWriter {
|
|
|
|
public:
|
2017-10-05 06:44:13 +08:00
|
|
|
AMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI, bool HasRelocationAddend);
|
2017-02-04 07:38:40 +08:00
|
|
|
|
2013-04-16 01:51:21 +08:00
|
|
|
protected:
|
2016-01-14 06:56:57 +08:00
|
|
|
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
|
2016-06-21 00:59:44 +08:00
|
|
|
const MCFixup &Fixup, bool IsPCRel) const override;
|
2013-04-16 01:51:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-02-04 07:38:40 +08:00
|
|
|
} // end anonymous namespace
|
2013-04-16 01:51:21 +08:00
|
|
|
|
2016-06-15 04:29:59 +08:00
|
|
|
AMDGPUELFObjectWriter::AMDGPUELFObjectWriter(bool Is64Bit,
|
2017-10-05 06:44:13 +08:00
|
|
|
uint8_t OSABI,
|
2016-06-15 04:29:59 +08:00
|
|
|
bool HasRelocationAddend)
|
2017-10-05 06:44:13 +08:00
|
|
|
: MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_AMDGPU,
|
2017-02-04 07:38:40 +08:00
|
|
|
HasRelocationAddend) {}
|
2013-04-16 01:51:21 +08:00
|
|
|
|
2016-06-21 00:59:44 +08:00
|
|
|
unsigned AMDGPUELFObjectWriter::getRelocType(MCContext &Ctx,
|
|
|
|
const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const {
|
2016-10-07 00:20:41 +08:00
|
|
|
if (const auto *SymA = Target.getSymA()) {
|
|
|
|
// SCRATCH_RSRC_DWORD[01] is a special global variable that represents
|
|
|
|
// the scratch buffer.
|
|
|
|
if (SymA->getSymbol().getName() == "SCRATCH_RSRC_DWORD0")
|
|
|
|
return ELF::R_AMDGPU_ABS32_LO;
|
|
|
|
|
|
|
|
if (SymA->getSymbol().getName() == "SCRATCH_RSRC_DWORD1")
|
|
|
|
return ELF::R_AMDGPU_ABS32_HI;
|
|
|
|
}
|
2016-06-21 00:59:44 +08:00
|
|
|
|
2016-06-24 07:11:29 +08:00
|
|
|
switch (Target.getAccessVariant()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_GOTPCREL:
|
|
|
|
return ELF::R_AMDGPU_GOTPCREL;
|
2016-10-14 12:21:32 +08:00
|
|
|
case MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_LO:
|
|
|
|
return ELF::R_AMDGPU_GOTPCREL32_LO;
|
|
|
|
case MCSymbolRefExpr::VK_AMDGPU_GOTPCREL32_HI:
|
|
|
|
return ELF::R_AMDGPU_GOTPCREL32_HI;
|
|
|
|
case MCSymbolRefExpr::VK_AMDGPU_REL32_LO:
|
|
|
|
return ELF::R_AMDGPU_REL32_LO;
|
|
|
|
case MCSymbolRefExpr::VK_AMDGPU_REL32_HI:
|
|
|
|
return ELF::R_AMDGPU_REL32_HI;
|
2016-06-24 07:11:29 +08:00
|
|
|
}
|
|
|
|
|
2016-06-21 01:33:43 +08:00
|
|
|
switch (Fixup.getKind()) {
|
|
|
|
default: break;
|
|
|
|
case FK_PCRel_4:
|
|
|
|
return ELF::R_AMDGPU_REL32;
|
2016-07-21 23:29:19 +08:00
|
|
|
case FK_Data_4:
|
2016-06-26 02:24:16 +08:00
|
|
|
case FK_SecRel_4:
|
|
|
|
return ELF::R_AMDGPU_ABS32;
|
2016-10-08 04:36:58 +08:00
|
|
|
case FK_Data_8:
|
|
|
|
return ELF::R_AMDGPU_ABS64;
|
2016-06-21 01:33:43 +08:00
|
|
|
}
|
|
|
|
|
2016-06-21 00:59:44 +08:00
|
|
|
llvm_unreachable("unhandled relocation type");
|
|
|
|
}
|
|
|
|
|
2017-10-11 00:28:07 +08:00
|
|
|
std::unique_ptr<MCObjectWriter>
|
|
|
|
llvm::createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI,
|
|
|
|
bool HasRelocationAddend,
|
|
|
|
raw_pwrite_stream &OS) {
|
2017-10-10 07:53:15 +08:00
|
|
|
auto MOTW = llvm::make_unique<AMDGPUELFObjectWriter>(Is64Bit, OSABI,
|
|
|
|
HasRelocationAddend);
|
|
|
|
return createELFObjectWriter(std::move(MOTW), OS, true);
|
2013-04-16 01:51:21 +08:00
|
|
|
}
|