| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 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" | 
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 13 | #include "llvm/Support/ELF.h" | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 14 | #include "llvm/Support/ErrorHandling.h" | 
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 15 | #include <cstdint> | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 16 |  | 
|  | 17 | using namespace llvm; | 
|  | 18 |  | 
|  | 19 | namespace { | 
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 20 |  | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 21 | class BPFELFObjectWriter : public MCELFObjectTargetWriter { | 
|  | 22 | public: | 
|  | 23 | BPFELFObjectWriter(uint8_t OSABI); | 
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 24 | ~BPFELFObjectWriter() override = default; | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 25 |  | 
|  | 26 | protected: | 
| Rafael Espindola | 8340f94 | 2016-01-13 22:56:57 +0000 | [diff] [blame] | 27 | unsigned getRelocType(MCContext &Ctx, const MCValue &Target, | 
|  | 28 | const MCFixup &Fixup, bool IsPCRel) const override; | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 29 | }; | 
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 30 |  | 
|  | 31 | } // end anonymous namespace | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 32 |  | 
|  | 33 | BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI) | 
| Alexei Starovoitov | cfb51f5 | 2016-07-15 22:27:55 +0000 | [diff] [blame] | 34 | : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF, | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 35 | /*HasRelocationAddend*/ false) {} | 
|  | 36 |  | 
| Rafael Espindola | 8340f94 | 2016-01-13 22:56:57 +0000 | [diff] [blame] | 37 | unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 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: | 
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 45 | return ELF::R_BPF_64_64; | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 46 | case FK_SecRel_4: | 
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 47 | return ELF::R_BPF_64_32; | 
| Alexei Starovoitov | 01886a0 | 2015-07-24 03:17:08 +0000 | [diff] [blame] | 48 | case FK_Data_8: | 
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 49 | return ELF::R_BPF_64_64; | 
| Alexei Starovoitov | 01886a0 | 2015-07-24 03:17:08 +0000 | [diff] [blame] | 50 | case FK_Data_4: | 
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 51 | return ELF::R_BPF_64_32; | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 52 | } | 
|  | 53 | } | 
|  | 54 |  | 
| Alexei Starovoitov | 310dead | 2015-06-04 19:15:05 +0000 | [diff] [blame] | 55 | MCObjectWriter *llvm::createBPFELFObjectWriter(raw_pwrite_stream &OS, | 
|  | 56 | uint8_t OSABI, bool IsLittleEndian) { | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 57 | MCELFObjectTargetWriter *MOTW = new BPFELFObjectWriter(OSABI); | 
| Alexei Starovoitov | 310dead | 2015-06-04 19:15:05 +0000 | [diff] [blame] | 58 | return createELFObjectWriter(MOTW, OS, IsLittleEndian); | 
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 59 | } |