| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 1 | //===-- BPFELFObjectWriter.cpp - BPF ELF Writer ---------------------------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "MCTargetDesc/BPFMCTargetDesc.h" |
| Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 10 | #include "llvm/BinaryFormat/ELF.h" |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCELFObjectWriter.h" |
| 12 | #include "llvm/MC/MCFixup.h" |
| Lang Hames | 60fbc7c | 2017-10-10 16:28:07 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCObjectWriter.h" |
| Yonghong Song | 821c93d | 2018-12-20 17:40:23 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCValue.h" |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ErrorHandling.h" |
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 16 | #include <cstdint> |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | namespace { |
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 21 | |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 22 | class BPFELFObjectWriter : public MCELFObjectTargetWriter { |
| 23 | public: |
| 24 | BPFELFObjectWriter(uint8_t OSABI); |
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 25 | ~BPFELFObjectWriter() override = default; |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 26 | |
| 27 | protected: |
| Rafael Espindola | 8340f94 | 2016-01-13 22:56:57 +0000 | [diff] [blame] | 28 | unsigned getRelocType(MCContext &Ctx, const MCValue &Target, |
| 29 | const MCFixup &Fixup, bool IsPCRel) const override; |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 30 | }; |
| Eugene Zelenko | 4282c40 | 2017-01-06 23:06:25 +0000 | [diff] [blame] | 31 | |
| 32 | } // end anonymous namespace |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 33 | |
| 34 | BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI) |
| Alexei Starovoitov | cfb51f5 | 2016-07-15 22:27:55 +0000 | [diff] [blame] | 35 | : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF, |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 36 | /*HasRelocationAddend*/ false) {} |
| 37 | |
| Rafael Espindola | 8340f94 | 2016-01-13 22:56:57 +0000 | [diff] [blame] | 38 | unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target, |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 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: |
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 46 | return ELF::R_BPF_64_64; |
| Alexei Starovoitov | 9a67245 | 2017-11-19 01:35:00 +0000 | [diff] [blame] | 47 | case FK_PCRel_4: |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 48 | case FK_SecRel_4: |
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 49 | return ELF::R_BPF_64_32; |
| Alexei Starovoitov | 01886a0 | 2015-07-24 03:17:08 +0000 | [diff] [blame] | 50 | case FK_Data_8: |
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 51 | return ELF::R_BPF_64_64; |
| Alexei Starovoitov | 01886a0 | 2015-07-24 03:17:08 +0000 | [diff] [blame] | 52 | case FK_Data_4: |
| Yonghong Song | 821c93d | 2018-12-20 17:40:23 +0000 | [diff] [blame] | 53 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
| Yonghong Song | e698958 | 2019-05-26 21:26:06 +0000 | [diff] [blame] | 54 | const MCSymbol &Sym = A->getSymbol(); |
| 55 | |
| 56 | if (Sym.isDefined()) { |
| 57 | MCSection &Section = Sym.getSection(); |
| Yonghong Song | 0d99031 | 2019-01-08 16:36:06 +0000 | [diff] [blame] | 58 | const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section); |
| 59 | assert(SectionELF && "Null section for reloc symbol"); |
| 60 | |
| Yonghong Song | 0d99031 | 2019-01-08 16:36:06 +0000 | [diff] [blame] | 61 | unsigned Flags = SectionELF->getFlags(); |
| Yonghong Song | e698958 | 2019-05-26 21:26:06 +0000 | [diff] [blame] | 62 | |
| 63 | if (Sym.isTemporary()) { |
| 64 | // .BTF.ext generates FK_Data_4 relocations for |
| 65 | // insn offset by creating temporary labels. |
| 66 | // The insn offset is within the code section and |
| 67 | // already been fulfilled by applyFixup(). No |
| 68 | // further relocation is needed. |
| 69 | // The reloc symbol should be in text section. |
| 70 | if ((Flags & ELF::SHF_ALLOC) && (Flags & ELF::SHF_EXECINSTR)) |
| 71 | return ELF::R_BPF_NONE; |
| 72 | } else { |
| 73 | // .BTF generates FK_Data_4 relocations for variable |
| 74 | // offset in DataSec kind. Similar to the above .BTF.ext |
| 75 | // insn offset, no further relocation is needed. |
| 76 | // The reloc symbol should be in data section. |
| 77 | if ((Flags & ELF::SHF_ALLOC) && (Flags & ELF::SHF_WRITE)) |
| 78 | return ELF::R_BPF_NONE; |
| 79 | } |
| Yonghong Song | 0d99031 | 2019-01-08 16:36:06 +0000 | [diff] [blame] | 80 | } |
| Yonghong Song | 821c93d | 2018-12-20 17:40:23 +0000 | [diff] [blame] | 81 | } |
| Alexei Starovoitov | 7ab125d | 2016-11-21 06:21:23 +0000 | [diff] [blame] | 82 | return ELF::R_BPF_64_32; |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| Peter Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 86 | std::unique_ptr<MCObjectTargetWriter> |
| 87 | llvm::createBPFELFObjectWriter(uint8_t OSABI) { |
| Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 88 | return std::make_unique<BPFELFObjectWriter>(OSABI); |
| Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 89 | } |