aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.cpp
blob: aff7296cb6e3ff18d1f95c3dc100cd7352b48ed3 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//===--------- RegisterEHFrames.cpp - Register EH frame sections ----------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"

#include "llvm/Config/config.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/raw_ostream.h"

#include "llvm/Support/FormatVariadic.h"

#define DEBUG_TYPE "orc"

using namespace llvm;
using namespace llvm::orc;
using namespace llvm::orc::tpctypes;

namespace llvm {
namespace orc {

#if defined(HAVE_REGISTER_FRAME) && defined(HAVE_DEREGISTER_FRAME) &&          \
    !defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)

extern "C" void __register_frame(const void *);
extern "C" void __deregister_frame(const void *);

Error registerFrameWrapper(const void *P) {
  __register_frame(P);
  return Error::success();
}

Error deregisterFrameWrapper(const void *P) {
  __deregister_frame(P);
  return Error::success();
}

#else

// The building compiler does not have __(de)register_frame but
// it may be found at runtime in a dynamically-loaded library.
// For example, this happens when building LLVM with Visual C++
// but using the MingW runtime.
static Error registerFrameWrapper(const void *P) {
  static void((*RegisterFrame)(const void *)) = 0;

  if (!RegisterFrame)
    *(void **)&RegisterFrame =
        llvm::sys::DynamicLibrary::SearchForAddressOfSymbol("__register_frame");

  if (RegisterFrame) {
    RegisterFrame(P);
    return Error::success();
  }

  return make_error<StringError>("could not register eh-frame: "
                                 "__register_frame function not found",
                                 inconvertibleErrorCode());
}

static Error deregisterFrameWrapper(const void *P) {
  static void((*DeregisterFrame)(const void *)) = 0;

  if (!DeregisterFrame)
    *(void **)&DeregisterFrame =
        llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
            "__deregister_frame");

  if (DeregisterFrame) {
    DeregisterFrame(P);
    return Error::success();
  }

  return make_error<StringError>("could not deregister eh-frame: "
                                 "__deregister_frame function not found",
                                 inconvertibleErrorCode());
}
#endif

#ifdef __APPLE__

template <typename HandleFDEFn>
Error walkAppleEHFrameSection(const char *const SectionStart,
                              size_t SectionSize, HandleFDEFn HandleFDE) {
  const char *CurCFIRecord = SectionStart;
  const char *End = SectionStart + SectionSize;
  uint64_t Size = *reinterpret_cast<const uint32_t *>(CurCFIRecord);

  while (CurCFIRecord != End && Size != 0) {
    const char *OffsetField = CurCFIRecord + (Size == 0xffffffff ? 12 : 4);
    if (Size == 0xffffffff)
      Size = *reinterpret_cast<const uint64_t *>(CurCFIRecord + 4) + 12;
    else
      Size += 4;
    uint32_t Offset = *reinterpret_cast<const uint32_t *>(OffsetField);

    LLVM_DEBUG({
      dbgs() << "Registering eh-frame section:\n";
      dbgs() << "Processing " << (Offset ? "FDE" : "CIE") << " @"
             << (void *)CurCFIRecord << ": [";
      for (unsigned I = 0; I < Size; ++I)
        dbgs() << format(" 0x%02" PRIx8, *(CurCFIRecord + I));
      dbgs() << " ]\n";
    });

    if (Offset != 0)
      if (auto Err = HandleFDE(CurCFIRecord))
        return Err;

    CurCFIRecord += Size;

    Size = *reinterpret_cast<const uint32_t *>(CurCFIRecord);
  }

  return Error::success();
}

#endif // __APPLE__

Error registerEHFrameSection(const void *EHFrameSectionAddr,
                             size_t EHFrameSectionSize) {
#ifdef __APPLE__
  // On Darwin __register_frame has to be called for each FDE entry.
  return walkAppleEHFrameSection(static_cast<const char *>(EHFrameSectionAddr),
                                 EHFrameSectionSize, registerFrameWrapper);
#else
  // On Linux __register_frame takes a single argument:
  // a pointer to the start of the .eh_frame section.

  // How can it find the end? Because crtendS.o is linked
  // in and it has an .eh_frame section with four zero chars.
  return registerFrameWrapper(EHFrameSectionAddr);
#endif
}

Error deregisterEHFrameSection(const void *EHFrameSectionAddr,
                               size_t EHFrameSectionSize) {
#ifdef __APPLE__
  return walkAppleEHFrameSection(static_cast<const char *>(EHFrameSectionAddr),
                                 EHFrameSectionSize, deregisterFrameWrapper);
#else
  return deregisterFrameWrapper(EHFrameSectionAddr);
#endif
}

} // end namespace orc
} // end namespace llvm

extern "C" CWrapperFunctionResult
llvm_orc_registerEHFrameSectionWrapper(uint8_t *Data, uint64_t Size) {
  if (Size != sizeof(uint64_t) + sizeof(uint64_t))
    return WrapperFunctionResult::from(
               "Invalid arguments to llvm_orc_registerEHFrameSectionWrapper")
        .release();

  uint64_t EHFrameSectionAddr;
  uint64_t EHFrameSectionSize;

  {
    BinaryStreamReader ArgReader(ArrayRef<uint8_t>(Data, Size),
                                 support::endianness::big);
    cantFail(ArgReader.readInteger(EHFrameSectionAddr));
    cantFail(ArgReader.readInteger(EHFrameSectionSize));
  }

  if (auto Err = registerEHFrameSection(
          jitTargetAddressToPointer<void *>(EHFrameSectionAddr),
          EHFrameSectionSize)) {
    auto ErrMsg = toString(std::move(Err));
    return WrapperFunctionResult::from(ErrMsg).release();
  }
  return WrapperFunctionResult().release();
}

extern "C" CWrapperFunctionResult
llvm_orc_deregisterEHFrameSectionWrapper(uint8_t *Data, uint64_t Size) {
  if (Size != sizeof(uint64_t) + sizeof(uint64_t))
    return WrapperFunctionResult::from(
               "Invalid arguments to llvm_orc_registerEHFrameSectionWrapper")
        .release();

  uint64_t EHFrameSectionAddr;
  uint64_t EHFrameSectionSize;

  {
    BinaryStreamReader ArgReader(ArrayRef<uint8_t>(Data, Size),
                                 support::endianness::big);
    cantFail(ArgReader.readInteger(EHFrameSectionAddr));
    cantFail(ArgReader.readInteger(EHFrameSectionSize));
  }

  if (auto Err = deregisterEHFrameSection(
          jitTargetAddressToPointer<void *>(EHFrameSectionAddr),
          EHFrameSectionSize)) {
    auto ErrMsg = toString(std::move(Err));
    return WrapperFunctionResult::from(ErrMsg).release();
  }
  return WrapperFunctionResult().release();
}