LLVM  8.0.1
BPFELFObjectWriter.cpp
Go to the documentation of this file.
1 //===-- 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 
11 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/MC/MCFixup.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include "llvm/MC/MCValue.h"
17 #include <cstdint>
18 
19 using namespace llvm;
20 
21 namespace {
22 
23 class BPFELFObjectWriter : public MCELFObjectTargetWriter {
24 public:
25  BPFELFObjectWriter(uint8_t OSABI);
26  ~BPFELFObjectWriter() override = default;
27 
28 protected:
29  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
30  const MCFixup &Fixup, bool IsPCRel) const override;
31 };
32 
33 } // end anonymous namespace
34 
35 BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI)
36  : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF,
37  /*HasRelocationAddend*/ false) {}
38 
40  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:
47  return ELF::R_BPF_64_64;
48  case FK_PCRel_4:
49  case FK_SecRel_4:
50  return ELF::R_BPF_64_32;
51  case FK_Data_8:
52  return ELF::R_BPF_64_64;
53  case FK_Data_4:
54  // .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()) {
60  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  }
70  }
71  return ELF::R_BPF_64_32;
72  }
73 }
74 
75 std::unique_ptr<MCObjectTargetWriter>
77  return llvm::make_unique<BPFELFObjectWriter>(OSABI);
78 }
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:24
std::unique_ptr< MCObjectTargetWriter > createBPFELFObjectWriter(uint8_t OSABI)
This represents an "assembler immediate".
Definition: MCValue.h:40
block Block Frequency true
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:74
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:166
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:42
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:63
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:48
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A four-byte pc relative fixup.
Definition: MCFixup.h:30
Target - Wrapper for Target specific information.
A eight-byte section relative fixup.
Definition: MCFixup.h:43
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:28
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:323
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getFlags() const
Definition: MCSectionELF.h:75
MCFixupKind getKind() const
Definition: MCFixup.h:123