[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / Target / BPF / MCTargetDesc / BPFELFObjectWriter.cpp
blob7c0c23a86de8d59c29927c176e2f9f6f0ecddaeb
1 //===-- BPFELFObjectWriter.cpp - BPF ELF Writer ---------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "MCTargetDesc/BPFMCTargetDesc.h"
10 #include "llvm/BinaryFormat/ELF.h"
11 #include "llvm/MC/MCELFObjectWriter.h"
12 #include "llvm/MC/MCFixup.h"
13 #include "llvm/MC/MCObjectWriter.h"
14 #include "llvm/MC/MCValue.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include <cstdint>
18 using namespace llvm;
20 namespace {
22 class BPFELFObjectWriter : public MCELFObjectTargetWriter {
23 public:
24 BPFELFObjectWriter(uint8_t OSABI);
25 ~BPFELFObjectWriter() override = default;
27 protected:
28 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
29 const MCFixup &Fixup, bool IsPCRel) const override;
32 } // end anonymous namespace
34 BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI)
35 : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF,
36 /*HasRelocationAddend*/ false) {}
38 unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
39 const MCFixup &Fixup,
40 bool IsPCRel) const {
41 // determine the type of the relocation
42 switch (Fixup.getKind()) {
43 default:
44 llvm_unreachable("invalid fixup kind!");
45 case FK_SecRel_8:
46 // LD_imm64 instruction.
47 return ELF::R_BPF_64_64;
48 case FK_PCRel_4:
49 // CALL instruction.
50 return ELF::R_BPF_64_32;
51 case FK_Data_8:
52 return ELF::R_BPF_64_ABS64;
53 case FK_Data_4:
54 if (const MCSymbolRefExpr *A = Target.getSymA()) {
55 const MCSymbol &Sym = A->getSymbol();
57 if (Sym.isDefined()) {
58 MCSection &Section = Sym.getSection();
59 const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
60 assert(SectionELF && "Null section for reloc symbol");
62 unsigned Flags = SectionELF->getFlags();
64 if (Sym.isTemporary()) {
65 // .BTF.ext generates FK_Data_4 relocations for
66 // insn offset by creating temporary labels.
67 // The reloc symbol should be in text section.
68 // Use a different relocation to instruct ExecutionEngine
69 // RuntimeDyld not to do relocation for it, yet still to
70 // allow lld to do proper adjustment when merging sections.
71 if ((Flags & ELF::SHF_ALLOC) && (Flags & ELF::SHF_EXECINSTR))
72 return ELF::R_BPF_64_NODYLD32;
73 } else {
74 // .BTF generates FK_Data_4 relocations for variable
75 // offset in DataSec kind.
76 // The reloc symbol should be in data section.
77 if ((Flags & ELF::SHF_ALLOC) && (Flags & ELF::SHF_WRITE))
78 return ELF::R_BPF_64_NODYLD32;
82 return ELF::R_BPF_64_ABS32;
86 std::unique_ptr<MCObjectTargetWriter>
87 llvm::createBPFELFObjectWriter(uint8_t OSABI) {
88 return std::make_unique<BPFELFObjectWriter>(OSABI);