aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/lib/ExecutionEngine/JITLink/EHFrameSupportImpl.h
blob: 55cf7fc63ee7957e75c0b150430625d474d37f2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//===------- EHFrameSupportImpl.h - JITLink eh-frame utils ------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// EHFrame registration support for JITLink.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_EXECUTIONENGINE_JITLINK_EHFRAMESUPPORTIMPL_H
#define LLVM_LIB_EXECUTIONENGINE_JITLINK_EHFRAMESUPPORTIMPL_H

#include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"

#include "llvm/ExecutionEngine/JITLink/JITLink.h"
#include "llvm/Support/BinaryStreamReader.h"

namespace llvm {
namespace jitlink {

/// A LinkGraph pass that adds missing FDE-to-CIE, FDE-to-PC and FDE-to-LSDA
/// edges.
class EHFrameEdgeFixer {
public:
  /// Create an eh-frame edge fixer.
  /// If a given edge-kind is not supported on the target architecture then
  /// Edge::Invalid should be used.
  EHFrameEdgeFixer(StringRef EHFrameSectionName, unsigned PointerSize,
                   Edge::Kind Pointer32, Edge::Kind Pointer64,
                   Edge::Kind Delta32, Edge::Kind Delta64,
                   Edge::Kind NegDelta32);
  Error operator()(LinkGraph &G);

private:

  struct AugmentationInfo {
    bool AugmentationDataPresent = false;
    bool EHDataFieldPresent = false;
    uint8_t Fields[4] = {0x0, 0x0, 0x0, 0x0};
  };

  struct CIEInformation {
    CIEInformation() = default;
    CIEInformation(Symbol &CIESymbol) : CIESymbol(&CIESymbol) {}
    Symbol *CIESymbol = nullptr;
    bool AugmentationDataPresent = false;
    bool LSDAPresent = false;
    uint8_t LSDAEncoding = 0;
    uint8_t AddressEncoding = 0;
  };

  struct EdgeTarget {
    EdgeTarget() = default;
    EdgeTarget(const Edge &E) : Target(&E.getTarget()), Addend(E.getAddend()) {}

    Symbol *Target = nullptr;
    Edge::AddendT Addend = 0;
  };

  using BlockEdgeMap = DenseMap<Edge::OffsetT, EdgeTarget>;
  using CIEInfosMap = DenseMap<orc::ExecutorAddr, CIEInformation>;

  struct ParseContext {
    ParseContext(LinkGraph &G) : G(G) {}

    Expected<CIEInformation *> findCIEInfo(orc::ExecutorAddr Address) {
      auto I = CIEInfos.find(Address);
      if (I == CIEInfos.end())
        return make_error<JITLinkError>("No CIE found at address " +
                                        formatv("{0:x16}", Address));
      return &I->second;
    }

    LinkGraph &G;
    CIEInfosMap CIEInfos;
    BlockAddressMap AddrToBlock;
    DenseMap<orc::ExecutorAddr, Symbol *> AddrToSym;
  };

  Error processBlock(ParseContext &PC, Block &B);
  Error processCIE(ParseContext &PC, Block &B, size_t RecordOffset,
                   size_t RecordLength, size_t CIEDeltaFieldOffset,
                   const BlockEdgeMap &BlockEdges);
  Error processFDE(ParseContext &PC, Block &B, size_t RecordOffset,
                   size_t RecordLength, size_t CIEDeltaFieldOffset,
                   uint32_t CIEDelta, const BlockEdgeMap &BlockEdges);

  Expected<AugmentationInfo>
  parseAugmentationString(BinaryStreamReader &RecordReader);

  Expected<uint8_t> readPointerEncoding(BinaryStreamReader &RecordReader,
                                        Block &InBlock, const char *FieldName);
  Error skipEncodedPointer(uint8_t PointerEncoding,
                           BinaryStreamReader &RecordReader);
  Expected<Symbol *> getOrCreateEncodedPointerEdge(
      ParseContext &PC, const BlockEdgeMap &BlockEdges, uint8_t PointerEncoding,
      BinaryStreamReader &RecordReader, Block &BlockToFix,
      size_t PointerFieldOffset, const char *FieldName);

  Expected<Symbol &> getOrCreateSymbol(ParseContext &PC,
                                       orc::ExecutorAddr Addr);

  StringRef EHFrameSectionName;
  unsigned PointerSize;
  Edge::Kind Pointer32;
  Edge::Kind Pointer64;
  Edge::Kind Delta32;
  Edge::Kind Delta64;
  Edge::Kind NegDelta32;
};

/// Add a 32-bit null-terminator to the end of the eh-frame section.
class EHFrameNullTerminator {
public:
  EHFrameNullTerminator(StringRef EHFrameSectionName);
  Error operator()(LinkGraph &G);

private:
  static char NullTerminatorBlockContent[];
  StringRef EHFrameSectionName;
};

} // end namespace jitlink
} // end namespace llvm

#endif // LLVM_LIB_EXECUTIONENGINE_JITLINK_EHFRAMESUPPORTIMPL_H