1 //===-- BPFELFObjectWriter.cpp - BPF 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 "MCTargetDesc/BPFMCTargetDesc.h"
11 #include "llvm/MC/MCELFObjectWriter.h"
12 #include "llvm/MC/MCFixup.h"
13 #include "llvm/Support/ErrorHandling.h"
14 
15 using namespace llvm;
16 
17 namespace {
18 class BPFELFObjectWriter : public MCELFObjectTargetWriter {
19 public:
20   BPFELFObjectWriter(uint8_t OSABI);
21 
22   ~BPFELFObjectWriter() override;
23 
24 protected:
25   unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
26                         bool IsPCRel) const override;
27 };
28 }
29 
BPFELFObjectWriter(uint8_t OSABI)30 BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI)
31     : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_NONE,
32                               /*HasRelocationAddend*/ false) {}
33 
~BPFELFObjectWriter()34 BPFELFObjectWriter::~BPFELFObjectWriter() {}
35 
GetRelocType(const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const36 unsigned BPFELFObjectWriter::GetRelocType(const MCValue &Target,
37                                           const MCFixup &Fixup,
38                                           bool IsPCRel) const {
39   // determine the type of the relocation
40   switch ((unsigned)Fixup.getKind()) {
41   default:
42     llvm_unreachable("invalid fixup kind!");
43   case FK_SecRel_8:
44     return ELF::R_X86_64_64;
45   case FK_SecRel_4:
46     return ELF::R_X86_64_PC32;
47   }
48 }
49 
createBPFELFObjectWriter(raw_pwrite_stream & OS,uint8_t OSABI)50 MCObjectWriter *llvm::createBPFELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI) {
51   MCELFObjectTargetWriter *MOTW = new BPFELFObjectWriter(OSABI);
52   return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/true);
53 }
54