blob: 32e79d0f527ee66e6623b7fba3e2165615e6f22c [file] [log] [blame]
Alexei Starovoitove4c8c802015-01-24 17:51:26 +00001//===-- 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 Turner264b5d92017-06-07 03:48:56 +000011#include "llvm/BinaryFormat/ELF.h"
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000012#include "llvm/MC/MCELFObjectWriter.h"
13#include "llvm/MC/MCFixup.h"
Lang Hames60fbc7c2017-10-10 16:28:07 +000014#include "llvm/MC/MCObjectWriter.h"
Yonghong Song821c93d2018-12-20 17:40:23 +000015#include "llvm/MC/MCValue.h"
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000016#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko4282c402017-01-06 23:06:25 +000017#include <cstdint>
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000018
19using namespace llvm;
20
21namespace {
Eugene Zelenko4282c402017-01-06 23:06:25 +000022
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000023class BPFELFObjectWriter : public MCELFObjectTargetWriter {
24public:
25 BPFELFObjectWriter(uint8_t OSABI);
Eugene Zelenko4282c402017-01-06 23:06:25 +000026 ~BPFELFObjectWriter() override = default;
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000027
28protected:
Rafael Espindola8340f942016-01-13 22:56:57 +000029 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
30 const MCFixup &Fixup, bool IsPCRel) const override;
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000031};
Eugene Zelenko4282c402017-01-06 23:06:25 +000032
33} // end anonymous namespace
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000034
35BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI)
Alexei Starovoitovcfb51f52016-07-15 22:27:55 +000036 : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF,
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000037 /*HasRelocationAddend*/ false) {}
38
Rafael Espindola8340f942016-01-13 22:56:57 +000039unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000040 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 Starovoitov7ab125d2016-11-21 06:21:23 +000047 return ELF::R_BPF_64_64;
Alexei Starovoitov9a672452017-11-19 01:35:00 +000048 case FK_PCRel_4:
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000049 case FK_SecRel_4:
Alexei Starovoitov7ab125d2016-11-21 06:21:23 +000050 return ELF::R_BPF_64_32;
Alexei Starovoitov01886a02015-07-24 03:17:08 +000051 case FK_Data_8:
Alexei Starovoitov7ab125d2016-11-21 06:21:23 +000052 return ELF::R_BPF_64_64;
Alexei Starovoitov01886a02015-07-24 03:17:08 +000053 case FK_Data_4:
Yonghong Song821c93d2018-12-20 17:40:23 +000054 // .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 Song0d990312019-01-08 16:36:06 +000060 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 Song821c93d2018-12-20 17:40:23 +000070 }
Alexei Starovoitov7ab125d2016-11-21 06:21:23 +000071 return ELF::R_BPF_64_32;
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000072 }
73}
74
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000075std::unique_ptr<MCObjectTargetWriter>
76llvm::createBPFELFObjectWriter(uint8_t OSABI) {
77 return llvm::make_unique<BPFELFObjectWriter>(OSABI);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000078}