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