• 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/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCELFObjectWriter.h"
13 #include "llvm/MC/MCFixup.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include <cstdint>
17 
18 using namespace llvm;
19 
20 namespace {
21 
22 class BPFELFObjectWriter : public MCELFObjectTargetWriter {
23 public:
24   BPFELFObjectWriter(uint8_t OSABI);
25   ~BPFELFObjectWriter() override = default;
26 
27 protected:
28   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
29                         const MCFixup &Fixup, bool IsPCRel) const override;
30 };
31 
32 } // end anonymous namespace
33 
BPFELFObjectWriter(uint8_t OSABI)34 BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI)
35     : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF,
36                               /*HasRelocationAddend*/ false) {}
37 
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const38 unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
39                                           const MCFixup &Fixup,
40                                           bool IsPCRel) const {
41   // determine the type of the relocation
42   switch ((unsigned)Fixup.getKind()) {
43   default:
44     llvm_unreachable("invalid fixup kind!");
45   case FK_SecRel_8:
46     return ELF::R_BPF_64_64;
47   case FK_PCRel_4:
48   case FK_SecRel_4:
49     return ELF::R_BPF_64_32;
50   case FK_Data_8:
51     return ELF::R_BPF_64_64;
52   case FK_Data_4:
53     return ELF::R_BPF_64_32;
54   }
55 }
56 
57 std::unique_ptr<MCObjectTargetWriter>
createBPFELFObjectWriter(uint8_t OSABI)58 llvm::createBPFELFObjectWriter(uint8_t OSABI) {
59   return llvm::make_unique<BPFELFObjectWriter>(OSABI);
60 }
61