|
| 1 | +//===- lib/ReaderWriter/ELF/AMDGPU/AMDGPUTargetHandler.cpp ----------------===// |
| 2 | +// |
| 3 | +// The LLVM Linker |
| 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 "TargetLayout.h" |
| 11 | +#include "AMDGPUExecutableWriter.h" |
| 12 | +#include "AMDGPULinkingContext.h" |
| 13 | +#include "AMDGPUTargetHandler.h" |
| 14 | +#include "llvm/Support/ELF.h" |
| 15 | + |
| 16 | +namespace lld { |
| 17 | +namespace elf { |
| 18 | + |
| 19 | +AMDGPUTargetHandler::AMDGPUTargetHandler(AMDGPULinkingContext &ctx) |
| 20 | + : _ctx(ctx), _targetLayout(new AMDGPUTargetLayout(ctx)), |
| 21 | + _relocationHandler(new AMDGPUTargetRelocationHandler(*_targetLayout)) {} |
| 22 | + |
| 23 | +std::unique_ptr<Writer> AMDGPUTargetHandler::getWriter() { |
| 24 | + switch (_ctx.getOutputELFType()) { |
| 25 | + case llvm::ELF::ET_EXEC: |
| 26 | + return llvm::make_unique<AMDGPUExecutableWriter>(_ctx, *_targetLayout); |
| 27 | + case llvm::ELF::ET_DYN: |
| 28 | + llvm_unreachable("TODO: support dynamic libraries"); |
| 29 | + case llvm::ELF::ET_REL: |
| 30 | + llvm_unreachable("TODO: support -r mode"); |
| 31 | + default: |
| 32 | + llvm_unreachable("unsupported output type"); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +HSATextSection::HSATextSection(const ELFLinkingContext &ctx) |
| 37 | + : AtomSection(ctx, ".text", DefinedAtom::typeCode, 0, 0) { |
| 38 | + _type = SHT_PROGBITS; |
| 39 | + _flags = SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR | SHF_AMDGPU_HSA_AGENT | |
| 40 | + SHF_AMDGPU_HSA_CODE; |
| 41 | + |
| 42 | + // FIXME: What alignment should we use here? |
| 43 | + _alignment = 4096; |
| 44 | +} |
| 45 | + |
| 46 | +void AMDGPUTargetLayout::assignSectionsToSegments() { |
| 47 | + |
| 48 | + TargetLayout::assignSectionsToSegments(); |
| 49 | + for (OutputSection<ELF64LE> *osi : _outputSections) { |
| 50 | + for (Section<ELF64LE> *section : osi->sections()) { |
| 51 | + StringRef InputSectionName = section->inputSectionName(); |
| 52 | + if (InputSectionName != ".text") |
| 53 | + continue; |
| 54 | + |
| 55 | + Segment<ELF64LE> *segment = new (_allocator) Segment<ELF64LE>( |
| 56 | + _ctx, "PT_AMDGPU_HSA_LOAD_CODE_AGENT", PT_AMDGPU_HSA_LOAD_CODE_AGENT); |
| 57 | + _segments.push_back(segment); |
| 58 | + assert(segment); |
| 59 | + segment->append(section); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +} // namespace elf |
| 65 | +} // namespace lld |
0 commit comments