• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   case FK_Data_8:
48     return IsPCRel ? ELF::R_X86_64_PC64 : ELF::R_X86_64_64;
49   case FK_Data_4:
50     return IsPCRel ? ELF::R_X86_64_PC32 : ELF::R_X86_64_32;
51   }
52 }
53 
createBPFELFObjectWriter(raw_pwrite_stream & OS,uint8_t OSABI,bool IsLittleEndian)54 MCObjectWriter *llvm::createBPFELFObjectWriter(raw_pwrite_stream &OS,
55                                                uint8_t OSABI, bool IsLittleEndian) {
56   MCELFObjectTargetWriter *MOTW = new BPFELFObjectWriter(OSABI);
57   return createELFObjectWriter(MOTW, OS, IsLittleEndian);
58 }
59