blob: 6ebf95114120bcd88d9053a64a3cb024f409744e [file] [log] [blame]
Alexei Starovoitove4c8c802015-01-24 17:51:26 +00001//===-- BPFELFObjectWriter.cpp - BPF ELF Writer ---------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Starovoitove4c8c802015-01-24 17:51:26 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "MCTargetDesc/BPFMCTargetDesc.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000010#include "llvm/BinaryFormat/ELF.h"
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000011#include "llvm/MC/MCELFObjectWriter.h"
12#include "llvm/MC/MCFixup.h"
Lang Hames60fbc7c2017-10-10 16:28:07 +000013#include "llvm/MC/MCObjectWriter.h"
Yonghong Song821c93d2018-12-20 17:40:23 +000014#include "llvm/MC/MCValue.h"
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000015#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko4282c402017-01-06 23:06:25 +000016#include <cstdint>
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000017
18using namespace llvm;
19
20namespace {
Eugene Zelenko4282c402017-01-06 23:06:25 +000021
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000022class BPFELFObjectWriter : public MCELFObjectTargetWriter {
23public:
24 BPFELFObjectWriter(uint8_t OSABI);
Eugene Zelenko4282c402017-01-06 23:06:25 +000025 ~BPFELFObjectWriter() override = default;
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000026
27protected:
Rafael Espindola8340f942016-01-13 22:56:57 +000028 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
29 const MCFixup &Fixup, bool IsPCRel) const override;
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000030};
Eugene Zelenko4282c402017-01-06 23:06:25 +000031
32} // end anonymous namespace
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000033
34BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI)
Alexei Starovoitovcfb51f52016-07-15 22:27:55 +000035 : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF,
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000036 /*HasRelocationAddend*/ false) {}
37
Rafael Espindola8340f942016-01-13 22:56:57 +000038unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000039 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 Starovoitov7ab125d2016-11-21 06:21:23 +000046 return ELF::R_BPF_64_64;
Alexei Starovoitov9a672452017-11-19 01:35:00 +000047 case FK_PCRel_4:
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000048 case FK_SecRel_4:
Alexei Starovoitov7ab125d2016-11-21 06:21:23 +000049 return ELF::R_BPF_64_32;
Alexei Starovoitov01886a02015-07-24 03:17:08 +000050 case FK_Data_8:
Alexei Starovoitov7ab125d2016-11-21 06:21:23 +000051 return ELF::R_BPF_64_64;
Alexei Starovoitov01886a02015-07-24 03:17:08 +000052 case FK_Data_4:
Yonghong Song821c93d2018-12-20 17:40:23 +000053 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Yonghong Songe6989582019-05-26 21:26:06 +000054 const MCSymbol &Sym = A->getSymbol();
55
56 if (Sym.isDefined()) {
57 MCSection &Section = Sym.getSection();
Yonghong Song0d990312019-01-08 16:36:06 +000058 const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
59 assert(SectionELF && "Null section for reloc symbol");
60
Yonghong Song0d990312019-01-08 16:36:06 +000061 unsigned Flags = SectionELF->getFlags();
Yonghong Songe6989582019-05-26 21:26:06 +000062
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 Song0d990312019-01-08 16:36:06 +000080 }
Yonghong Song821c93d2018-12-20 17:40:23 +000081 }
Alexei Starovoitov7ab125d2016-11-21 06:21:23 +000082 return ELF::R_BPF_64_32;
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000083 }
84}
85
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +000086std::unique_ptr<MCObjectTargetWriter>
87llvm::createBPFELFObjectWriter(uint8_t OSABI) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000088 return std::make_unique<BPFELFObjectWriter>(OSABI);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000089}