LLVM  9.0.0svn
RTDyldMemoryManager.cpp
Go to the documentation of this file.
1 //===-- RTDyldMemoryManager.cpp - Memory manager for MC-JIT -----*- C++ -*-===//
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 //
9 // Implementation of the runtime dynamic memory manager base class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Config/config.h"
15 #include "llvm/Support/Compiler.h"
18 #include <cstdlib>
19 
20 #ifdef __linux__
21  // These includes used by RTDyldMemoryManager::getPointerToNamedFunction()
22  // for Glibc trickery. See comments in this function for more information.
23  #ifdef HAVE_SYS_STAT_H
24  #include <sys/stat.h>
25  #endif
26  #include <fcntl.h>
27  #include <unistd.h>
28 #endif
29 
30 namespace llvm {
31 
33 
34 // Determine whether we can register EH tables.
35 #if (defined(__GNUC__) && !defined(__ARM_EABI__) && !defined(__ia64__) && \
36  !defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__))
37 #define HAVE_EHTABLE_SUPPORT 1
38 #else
39 #define HAVE_EHTABLE_SUPPORT 0
40 #endif
41 
42 #if HAVE_EHTABLE_SUPPORT
43 extern "C" void __register_frame(void *);
44 extern "C" void __deregister_frame(void *);
45 #else
46 // The building compiler does not have __(de)register_frame but
47 // it may be found at runtime in a dynamically-loaded library.
48 // For example, this happens when building LLVM with Visual C++
49 // but using the MingW runtime.
50 static void __register_frame(void *p) {
51  static bool Searched = false;
52  static void((*rf)(void *)) = 0;
53 
54  if (!Searched) {
55  Searched = true;
56  *(void **)&rf =
58  }
59  if (rf)
60  rf(p);
61 }
62 
63 static void __deregister_frame(void *p) {
64  static bool Searched = false;
65  static void((*df)(void *)) = 0;
66 
67  if (!Searched) {
68  Searched = true;
70  "__deregister_frame");
71  }
72  if (df)
73  df(p);
74 }
75 #endif
76 
77 #ifdef __APPLE__
78 
79 static const char *processFDE(const char *Entry, bool isDeregister) {
80  const char *P = Entry;
81  uint32_t Length = *((const uint32_t *)P);
82  P += 4;
83  uint32_t Offset = *((const uint32_t *)P);
84  if (Offset != 0) {
85  if (isDeregister)
86  __deregister_frame(const_cast<char *>(Entry));
87  else
88  __register_frame(const_cast<char *>(Entry));
89  }
90  return P + Length;
91 }
92 
93 // This implementation handles frame registration for local targets.
94 // Memory managers for remote targets should re-implement this function
95 // and use the LoadAddr parameter.
97  size_t Size) {
98  // On OS X OS X __register_frame takes a single FDE as an argument.
99  // See http://lists.llvm.org/pipermail/llvm-dev/2013-April/061737.html
100  // and projects/libunwind/src/UnwindLevel1-gcc-ext.c.
101  const char *P = (const char *)Addr;
102  const char *End = P + Size;
103  do {
104  P = processFDE(P, false);
105  } while(P != End);
106 }
107 
109  size_t Size) {
110  const char *P = (const char *)Addr;
111  const char *End = P + Size;
112  do {
113  P = processFDE(P, true);
114  } while(P != End);
115 }
116 
117 #else
118 
120  size_t Size) {
121  // On Linux __register_frame takes a single argument:
122  // a pointer to the start of the .eh_frame section.
123 
124  // How can it find the end? Because crtendS.o is linked
125  // in and it has an .eh_frame section with four zero chars.
126  __register_frame(Addr);
127 }
128 
130  size_t Size) {
131  __deregister_frame(Addr);
132 }
133 
134 #endif
135 
136 void RTDyldMemoryManager::registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
137  size_t Size) {
138  registerEHFramesInProcess(Addr, Size);
139  EHFrames.push_back({Addr, Size});
140 }
141 
143  for (auto &Frame : EHFrames)
144  deregisterEHFramesInProcess(Frame.Addr, Frame.Size);
145  EHFrames.clear();
146 }
147 
148 static int jit_noop() {
149  return 0;
150 }
151 
152 // ARM math functions are statically linked on Android from libgcc.a, but not
153 // available at runtime for dynamic linking. On Linux these are usually placed
154 // in libgcc_s.so so can be found by normal dynamic lookup.
155 #if defined(__BIONIC__) && defined(__arm__)
156 // List of functions which are statically linked on Android and can be generated
157 // by LLVM. This is done as a nested macro which is used once to declare the
158 // imported functions with ARM_MATH_DECL and once to compare them to the
159 // user-requested symbol in getSymbolAddress with ARM_MATH_CHECK. The test
160 // assumes that all functions start with __aeabi_ and getSymbolAddress must be
161 // modified if that changes.
162 #define ARM_MATH_IMPORTS(PP) \
163  PP(__aeabi_d2f) \
164  PP(__aeabi_d2iz) \
165  PP(__aeabi_d2lz) \
166  PP(__aeabi_d2uiz) \
167  PP(__aeabi_d2ulz) \
168  PP(__aeabi_dadd) \
169  PP(__aeabi_dcmpeq) \
170  PP(__aeabi_dcmpge) \
171  PP(__aeabi_dcmpgt) \
172  PP(__aeabi_dcmple) \
173  PP(__aeabi_dcmplt) \
174  PP(__aeabi_dcmpun) \
175  PP(__aeabi_ddiv) \
176  PP(__aeabi_dmul) \
177  PP(__aeabi_dsub) \
178  PP(__aeabi_f2d) \
179  PP(__aeabi_f2iz) \
180  PP(__aeabi_f2lz) \
181  PP(__aeabi_f2uiz) \
182  PP(__aeabi_f2ulz) \
183  PP(__aeabi_fadd) \
184  PP(__aeabi_fcmpeq) \
185  PP(__aeabi_fcmpge) \
186  PP(__aeabi_fcmpgt) \
187  PP(__aeabi_fcmple) \
188  PP(__aeabi_fcmplt) \
189  PP(__aeabi_fcmpun) \
190  PP(__aeabi_fdiv) \
191  PP(__aeabi_fmul) \
192  PP(__aeabi_fsub) \
193  PP(__aeabi_i2d) \
194  PP(__aeabi_i2f) \
195  PP(__aeabi_idiv) \
196  PP(__aeabi_idivmod) \
197  PP(__aeabi_l2d) \
198  PP(__aeabi_l2f) \
199  PP(__aeabi_lasr) \
200  PP(__aeabi_ldivmod) \
201  PP(__aeabi_llsl) \
202  PP(__aeabi_llsr) \
203  PP(__aeabi_lmul) \
204  PP(__aeabi_ui2d) \
205  PP(__aeabi_ui2f) \
206  PP(__aeabi_uidiv) \
207  PP(__aeabi_uidivmod) \
208  PP(__aeabi_ul2d) \
209  PP(__aeabi_ul2f) \
210  PP(__aeabi_uldivmod)
211 
212 // Declare statically linked math functions on ARM. The function declarations
213 // here do not have the correct prototypes for each function in
214 // ARM_MATH_IMPORTS, but it doesn't matter because only the symbol addresses are
215 // needed. In particular the __aeabi_*divmod functions do not have calling
216 // conventions which match any C prototype.
217 #define ARM_MATH_DECL(name) extern "C" void name();
218 ARM_MATH_IMPORTS(ARM_MATH_DECL)
219 #undef ARM_MATH_DECL
220 #endif
221 
222 #if defined(__linux__) && defined(__GLIBC__) && \
223  (defined(__i386__) || defined(__x86_64__))
224 extern "C" LLVM_ATTRIBUTE_WEAK void __morestack();
225 #endif
226 
227 uint64_t
229  // This implementation assumes that the host program is the target.
230  // Clients generating code for a remote target should implement their own
231  // memory manager.
232 #if defined(__linux__) && defined(__GLIBC__)
233  //===--------------------------------------------------------------------===//
234  // Function stubs that are invoked instead of certain library calls
235  //
236  // Force the following functions to be linked in to anything that uses the
237  // JIT. This is a hack designed to work around the all-too-clever Glibc
238  // strategy of making these functions work differently when inlined vs. when
239  // not inlined, and hiding their real definitions in a separate archive file
240  // that the dynamic linker can't see. For more info, search for
241  // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
242  if (Name == "stat") return (uint64_t)&stat;
243  if (Name == "fstat") return (uint64_t)&fstat;
244  if (Name == "lstat") return (uint64_t)&lstat;
245  if (Name == "stat64") return (uint64_t)&stat64;
246  if (Name == "fstat64") return (uint64_t)&fstat64;
247  if (Name == "lstat64") return (uint64_t)&lstat64;
248  if (Name == "atexit") return (uint64_t)&atexit;
249  if (Name == "mknod") return (uint64_t)&mknod;
250 
251 #if defined(__i386__) || defined(__x86_64__)
252  // __morestack lives in libgcc, a static library.
253  if (&__morestack && Name == "__morestack")
254  return (uint64_t)&__morestack;
255 #endif
256 #endif // __linux__ && __GLIBC__
257 
258  // See ARM_MATH_IMPORTS definition for explanation
259 #if defined(__BIONIC__) && defined(__arm__)
260  if (Name.compare(0, 8, "__aeabi_") == 0) {
261  // Check if the user has requested any of the functions listed in
262  // ARM_MATH_IMPORTS, and if so redirect to the statically linked symbol.
263 #define ARM_MATH_CHECK(fn) if (Name == #fn) return (uint64_t)&fn;
264  ARM_MATH_IMPORTS(ARM_MATH_CHECK)
265 #undef ARM_MATH_CHECK
266  }
267 #endif
268 
269  // We should not invoke parent's ctors/dtors from generated main()!
270  // On Mingw and Cygwin, the symbol __main is resolved to
271  // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
272  // (and register wrong callee's dtors with atexit(3)).
273  // We expect ExecutionEngine::runStaticConstructorsDestructors()
274  // is called before ExecutionEngine::runFunctionAsMain() is called.
275  if (Name == "__main") return (uint64_t)&jit_noop;
276 
277  const char *NameStr = Name.c_str();
278 
279  // DynamicLibrary::SearchForAddresOfSymbol expects an unmangled 'C' symbol
280  // name so ff we're on Darwin, strip the leading '_' off.
281 #ifdef __APPLE__
282  if (NameStr[0] == '_')
283  ++NameStr;
284 #endif
285 
286  return (uint64_t)sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
287 }
288 
290  bool AbortOnFailure) {
291  uint64_t Addr = getSymbolAddress(Name);
292 
293  if (!Addr && AbortOnFailure)
294  report_fatal_error("Program used external function '" + Name +
295  "' which could not be resolved!");
296 
297  return (void*)Addr;
298 }
299 
300 void RTDyldMemoryManager::anchor() {}
301 void MCJITMemoryManager::anchor() {}
302 } // namespace llvm
static void * SearchForAddressOfSymbol(const char *symbolName)
This function will search through all previously loaded dynamic libraries for the symbol symbolName...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static void registerEHFramesInProcess(uint8_t *Addr, size_t Size)
Register EH frames in the current process.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override
Register the EH frames with the runtime so that c++ exceptions work.
virtual uint64_t getSymbolAddress(const std::string &Name)
Legacy symbol lookup - DEPRECATED! Please override findSymbol instead.
static void __register_frame(void *p)
static void deregisterEHFramesInProcess(uint8_t *Addr, size_t Size)
Deregister EH frames in the current proces.
#define LLVM_ATTRIBUTE_WEAK
Definition: Compiler.h:167
static void __deregister_frame(void *p)
#define P(N)
uint32_t Size
Definition: Profile.cpp:46
virtual void * getPointerToNamedFunction(const std::string &Name, bool AbortOnFailure=true)
This method returns the address of the specified function.
static int jit_noop()
static uint64_t getSymbolAddressInProcess(const std::string &Name)
This method returns the address of the specified function or variable in the current process...