| 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" |
| Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 11 | #include "llvm/BinaryFormat/ELF.h" |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCELFObjectWriter.h" |
| 13 | #include "llvm/MC/MCFixup.h" |
| Lang Hames | 60fbc7c | 2017-10-10 16:28:07 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCObjectWriter.h" |
| Yonghong Song | 821c93d | 2018-12-20 17:40:23 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCValue.h" |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 17 | #include <cstdint> |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 22 | |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 23 | class BPFELFObjectWriter : public MCELFObjectTargetWriter { |
| 24 | public: |
| 25 | BPFELFObjectWriter(uint8_t OSABI); |
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 26 | ~BPFELFObjectWriter() override = default; |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 27 | |
| 28 | protected: |
| Rafael Espindola | 8340f94 | 2016-01-13 22:56:57 +0000 | [diff] [blame] | 29 | unsigned getRelocType(MCContext &Ctx, const MCValue &Target, |
| 30 | const MCFixup &Fixup, bool IsPCRel) const override; |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 31 | }; |
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 32 | |
| 33 | } // end anonymous namespace |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 34 | |
| 35 | BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI) |
| Alexei Starovoitov | cfb51f5 | 2016-07-15 22:27:55 +0000 | [diff] [blame] | 36 | : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF, |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 37 | /*HasRelocationAddend*/ false) {} |
| 38 | |
| Rafael Espindola | 8340f94 | 2016-01-13 22:56:57 +0000 | [diff] [blame] | 39 | unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 40 | const MCFixup &Fixup, |
| 41 | bool IsPCRel) const { |
| 42 | // determine the type of the relocation |
| 43 | switch ((unsigned)Fixup.getKind()) { |
| 44 | default: |
| 45 | llvm_unreachable("invalid fixup kind!"); |
| 46 | case FK_SecRel_8: |
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 47 | return ELF::R_BPF_64_64; |
| Alexei Starovoitov | 9a67245 | 2017-11-19 01:35:00 +0000 | [diff] [blame] | 48 | case FK_PCRel_4: |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 49 | case FK_SecRel_4: |
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 50 | return ELF::R_BPF_64_32; |
| Alexei Starovoitov | 01886a0 | 2015-07-24 03:17:08 +0000 | [diff] [blame] | 51 | case FK_Data_8: |
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 52 | return ELF::R_BPF_64_64; |
| Alexei Starovoitov | 01886a0 | 2015-07-24 03:17:08 +0000 | [diff] [blame] | 53 | case FK_Data_4: |
| Yonghong Song | 821c93d | 2018-12-20 17:40:23 +0000 | [diff] [blame] | 54 | // .BTF.ext generates FK_Data_4 relocations for |
| 55 | // insn offset by creating temporary labels. |
| 56 | // The insn offset is within the code section and |
| 57 | // already been fulfilled by applyFixup(). No |
| 58 | // further relocation is needed. |
| 59 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
| Yonghong Song | 0d99031 | 2019-01-08 16:36:06 +0000 | [diff] [blame^] | 60 | if (A->getSymbol().isTemporary()) { |
| 61 | MCSection &Section = A->getSymbol().getSection(); |
| 62 | const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section); |
| 63 | assert(SectionELF && "Null section for reloc symbol"); |
| 64 | |
| 65 | // The reloc symbol should be in text section. |
| 66 | unsigned Flags = SectionELF->getFlags(); |
| 67 | if ((Flags & ELF::SHF_ALLOC) && (Flags & ELF::SHF_EXECINSTR)) |
| 68 | return ELF::R_BPF_NONE; |
| 69 | } |
| Yonghong Song | 821c93d | 2018-12-20 17:40:23 +0000 | [diff] [blame] | 70 | } |
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 71 | return ELF::R_BPF_64_32; |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| Peter Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 75 | std::unique_ptr<MCObjectTargetWriter> |
| 76 | llvm::createBPFELFObjectWriter(uint8_t OSABI) { |
| 77 | return llvm::make_unique<BPFELFObjectWriter>(OSABI); |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 78 | } |