LLVM  10.0.0svn
BPFELFObjectWriter.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCFixup.h"
13 #include "llvm/MC/MCObjectWriter.h"
14 #include "llvm/MC/MCValue.h"
16 #include <cstdint>
17 
18 using namespace llvm;
19 
20 namespace {
21 
22 class BPFELFObjectWriter : public MCELFObjectTargetWriter {
23 public:
24  BPFELFObjectWriter(uint8_t OSABI);
25  ~BPFELFObjectWriter() override = default;
26 
27 protected:
28  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
29  const MCFixup &Fixup, bool IsPCRel) const override;
30 };
31 
32 } // end anonymous namespace
33 
34 BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI)
35  : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF,
36  /*HasRelocationAddend*/ false) {}
37 
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  return ELF::R_BPF_64_64;
47  case FK_PCRel_4:
48  case FK_SecRel_4:
49  return ELF::R_BPF_64_32;
50  case FK_Data_8:
51  return ELF::R_BPF_64_64;
52  case FK_Data_4:
53  if (const MCSymbolRefExpr *A = Target.getSymA()) {
54  const MCSymbol &Sym = A->getSymbol();
55 
56  if (Sym.isDefined()) {
57  MCSection &Section = Sym.getSection();
58  const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
59  assert(SectionELF && "Null section for reloc symbol");
60 
61  unsigned Flags = SectionELF->getFlags();
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  }
80  }
81  }
82  return ELF::R_BPF_64_32;
83  }
84 }
85 
86 std::unique_ptr<MCObjectTargetWriter>
88  return std::make_unique<BPFELFObjectWriter>(OSABI);
89 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::unique_ptr< MCObjectTargetWriter > createBPFELFObjectWriter(uint8_t OSABI)
This represents an "assembler immediate".
Definition: MCValue.h:39
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
block Block Frequency true
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:77
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
static unsigned getRelocType(const MCValue &Target, const MCFixupKind FixupKind, const bool IsPCRel)
Translates generic PPC fixup kind to Mach-O/PPC relocation type enum.
A four-byte section relative fixup.
Definition: MCFixup.h:43
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:65
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:221
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A four-byte pc relative fixup.
Definition: MCFixup.h:31
bool isDefined() const
isDefined - Check if this symbol is defined (i.e., it has an address).
Definition: MCSymbol.h:249
Target - Wrapper for Target specific information.
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:268
A eight-byte section relative fixup.
Definition: MCFixup.h:44
A eight-byte fixup.
Definition: MCFixup.h:27
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:27
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getFlags() const
Definition: MCSectionELF.h:72
MCFixupKind getKind() const
Definition: MCFixup.h:126