llvm.org GIT mirror llvm / 30b9e32
Move getPointerToNamedFunction() from JIT/MCJIT to JITMemoryManager. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153607 91177308-0d34-0410-b5e6-96231b3b80d8 Danil Malyshev 8 years ago
14 changed file(s) with 233 addition(s) and 332 deletion(s). Raw diff Collapse all Expand all
4545 /// garbage values over freed memory. This is useful for testing and
4646 /// debugging, and may be turned on by default in debug mode.
4747 virtual void setPoisonMemory(bool poison) = 0;
48
49 /// getPointerToNamedFunction - This method returns the address of the
50 /// specified function. As such it is only useful for resolving library
51 /// symbols, not code generated symbols.
52 ///
53 /// If AbortOnFailure is false and no function with the given name is
54 /// found, this function silently returns a null pointer. Otherwise,
55 /// it prints a message to stderr and aborts.
56 ///
57 virtual void *getPointerToNamedFunction(const std::string &Name,
58 bool AbortOnFailure = true) = 0;
4859
4960 //===--------------------------------------------------------------------===//
5061 // Global Offset Table Management
5353 // memory was actually used.
5454 virtual void endFunctionBody(const char *Name, uint8_t *FunctionStart,
5555 uint8_t *FunctionEnd) = 0;
56
57
58 virtual void *getPointerToNamedFunction(const std::string &Name,
59 bool AbortOnFailure = true) = 0;
5660 };
5761
5862 class RuntimeDyld {
11 add_definitions(-DENABLE_X86_JIT)
22
33 add_llvm_library(LLVMJIT
4 Intercept.cpp
54 JIT.cpp
65 JITDwarfEmitter.cpp
76 JITEmitter.cpp
+0
-162
lib/ExecutionEngine/JIT/Intercept.cpp less more
None //===-- Intercept.cpp - System function interception routines -------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // If a function call occurs to an external function, the JIT is designed to use
10 // the dynamic loader interface to find a function to call. This is useful for
11 // calling system calls and library functions that are not available in LLVM.
12 // Some system calls, however, need to be handled specially. For this reason,
13 // we intercept some of them here and use our own stubs to handle them.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "JIT.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/DynamicLibrary.h"
20 #include "llvm/Config/config.h"
21 using namespace llvm;
22
23 // AtExitHandlers - List of functions to call when the program exits,
24 // registered with the atexit() library function.
25 static std::vector AtExitHandlers;
26
27 /// runAtExitHandlers - Run any functions registered by the program's
28 /// calls to atexit(3), which we intercept and store in
29 /// AtExitHandlers.
30 ///
31 static void runAtExitHandlers() {
32 while (!AtExitHandlers.empty()) {
33 void (*Fn)() = AtExitHandlers.back();
34 AtExitHandlers.pop_back();
35 Fn();
36 }
37 }
38
39 //===----------------------------------------------------------------------===//
40 // Function stubs that are invoked instead of certain library calls
41 //===----------------------------------------------------------------------===//
42
43 // Force the following functions to be linked in to anything that uses the
44 // JIT. This is a hack designed to work around the all-too-clever Glibc
45 // strategy of making these functions work differently when inlined vs. when
46 // not inlined, and hiding their real definitions in a separate archive file
47 // that the dynamic linker can't see. For more info, search for
48 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
49 #if defined(__linux__)
50 #if defined(HAVE_SYS_STAT_H)
51 #include
52 #endif
53 #include
54 #include
55 /* stat functions are redirecting to __xstat with a version number. On x86-64
56 * linking with libc_nonshared.a and -Wl,--export-dynamic doesn't make 'stat'
57 * available as an exported symbol, so we have to add it explicitly.
58 */
59 namespace {
60 class StatSymbols {
61 public:
62 StatSymbols() {
63 sys::DynamicLibrary::AddSymbol("stat", (void*)(intptr_t)stat);
64 sys::DynamicLibrary::AddSymbol("fstat", (void*)(intptr_t)fstat);
65 sys::DynamicLibrary::AddSymbol("lstat", (void*)(intptr_t)lstat);
66 sys::DynamicLibrary::AddSymbol("stat64", (void*)(intptr_t)stat64);
67 sys::DynamicLibrary::AddSymbol("\x1stat64", (void*)(intptr_t)stat64);
68 sys::DynamicLibrary::AddSymbol("\x1open64", (void*)(intptr_t)open64);
69 sys::DynamicLibrary::AddSymbol("\x1lseek64", (void*)(intptr_t)lseek64);
70 sys::DynamicLibrary::AddSymbol("fstat64", (void*)(intptr_t)fstat64);
71 sys::DynamicLibrary::AddSymbol("lstat64", (void*)(intptr_t)lstat64);
72 sys::DynamicLibrary::AddSymbol("atexit", (void*)(intptr_t)atexit);
73 sys::DynamicLibrary::AddSymbol("mknod", (void*)(intptr_t)mknod);
74 }
75 };
76 }
77 static StatSymbols initStatSymbols;
78 #endif // __linux__
79
80 // jit_exit - Used to intercept the "exit" library call.
81 static void jit_exit(int Status) {
82 runAtExitHandlers(); // Run atexit handlers...
83 exit(Status);
84 }
85
86 // jit_atexit - Used to intercept the "atexit" library call.
87 static int jit_atexit(void (*Fn)()) {
88 AtExitHandlers.push_back(Fn); // Take note of atexit handler...
89 return 0; // Always successful
90 }
91
92 static int jit_noop() {
93 return 0;
94 }
95
96 //===----------------------------------------------------------------------===//
97 //
98 /// getPointerToNamedFunction - This method returns the address of the specified
99 /// function by using the dynamic loader interface. As such it is only useful
100 /// for resolving library symbols, not code generated symbols.
101 ///
102 void *JIT::getPointerToNamedFunction(const std::string &Name,
103 bool AbortOnFailure) {
104 if (!isSymbolSearchingDisabled()) {
105 // Check to see if this is one of the functions we want to intercept. Note,
106 // we cast to intptr_t here to silence a -pedantic warning that complains
107 // about casting a function pointer to a normal pointer.
108 if (Name == "exit") return (void*)(intptr_t)&jit_exit;
109 if (Name == "atexit") return (void*)(intptr_t)&jit_atexit;
110
111 // We should not invoke parent's ctors/dtors from generated main()!
112 // On Mingw and Cygwin, the symbol __main is resolved to
113 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
114 // (and register wrong callee's dtors with atexit(3)).
115 // We expect ExecutionEngine::runStaticConstructorsDestructors()
116 // is called before ExecutionEngine::runFunctionAsMain() is called.
117 if (Name == "__main") return (void*)(intptr_t)&jit_noop;
118
119 const char *NameStr = Name.c_str();
120 // If this is an asm specifier, skip the sentinal.
121 if (NameStr[0] == 1) ++NameStr;
122
123 // If it's an external function, look it up in the process image...
124 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
125 if (Ptr) return Ptr;
126
127 // If it wasn't found and if it starts with an underscore ('_') character,
128 // and has an asm specifier, try again without the underscore.
129 if (Name[0] == 1 && NameStr[0] == '_') {
130 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
131 if (Ptr) return Ptr;
132 }
133
134 // Darwin/PPC adds $LDBLStub suffixes to various symbols like printf. These
135 // are references to hidden visibility symbols that dlsym cannot resolve.
136 // If we have one of these, strip off $LDBLStub and try again.
137 #if defined(__APPLE__) && defined(__ppc__)
138 if (Name.size() > 9 && Name[Name.size()-9] == '$' &&
139 memcmp(&Name[Name.size()-8], "LDBLStub", 8) == 0) {
140 // First try turning $LDBLStub into $LDBL128. If that fails, strip it off.
141 // This mirrors logic in libSystemStubs.a.
142 std::string Prefix = std::string(Name.begin(), Name.end()-9);
143 if (void *Ptr = getPointerToNamedFunction(Prefix+"$LDBL128", false))
144 return Ptr;
145 if (void *Ptr = getPointerToNamedFunction(Prefix, false))
146 return Ptr;
147 }
148 #endif
149 }
150
151 /// If a LazyFunctionCreator is installed, use it to get/create the function.
152 if (LazyFunctionCreator)
153 if (void *RP = LazyFunctionCreator(Name))
154 return RP;
155
156 if (AbortOnFailure) {
157 report_fatal_error("Program used external function '"+Name+
158 "' which could not be resolved!");
159 }
160 return 0;
161 }
2222 #include "llvm/CodeGen/MachineCodeInfo.h"
2323 #include "llvm/ExecutionEngine/GenericValue.h"
2424 #include "llvm/ExecutionEngine/JITEventListener.h"
25 #include "llvm/ExecutionEngine/JITMemoryManager.h"
2526 #include "llvm/Target/TargetData.h"
2627 #include "llvm/Target/TargetMachine.h"
2728 #include "llvm/Target/TargetJITInfo.h"
266267 }
267268
268269 JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
269 JITMemoryManager *JMM, bool GVsWithCode)
270 : ExecutionEngine(M), TM(tm), TJI(tji), AllocateGVsWithCode(GVsWithCode),
271 isAlreadyCodeGenerating(false) {
270 JITMemoryManager *jmm, bool GVsWithCode)
271 : ExecutionEngine(M), TM(tm), TJI(tji),
272 JMM(jmm ? jmm : JITMemoryManager::CreateDefaultMemManager()),
273 AllocateGVsWithCode(GVsWithCode), isAlreadyCodeGenerating(false) {
272274 setTargetData(TM.getTargetData());
273275
274276 jitstate = new JITState(M);
321323 AllJits->Remove(this);
322324 delete jitstate;
323325 delete JCE;
326 // JMM is a ownership of JCE, so we no need delete JMM here.
324327 delete &TM;
325328 }
326329
710713 }
711714 }
712715
716 void *JIT::getPointerToNamedFunction(const std::string &Name,
717 bool AbortOnFailure){
718 if (!isSymbolSearchingDisabled()) {
719 void *ptr = JMM->getPointerToNamedFunction(Name, false);
720 if (ptr)
721 return ptr;
722 }
723
724 /// If a LazyFunctionCreator is installed, use it to get/create the function.
725 if (LazyFunctionCreator)
726 if (void *RP = LazyFunctionCreator(Name))
727 return RP;
728
729 if (AbortOnFailure) {
730 report_fatal_error("Program used external function '"+Name+
731 "' which could not be resolved!");
732 }
733 return 0;
734 }
735
736
713737 /// getOrEmitGlobalVariable - Return the address of the specified global
714738 /// variable, possibly emitting it to memory if needed. This is used by the
715739 /// Emitter.
5757 TargetMachine &TM; // The current target we are compiling to
5858 TargetJITInfo &TJI; // The JITInfo for the target we are compiling to
5959 JITCodeEmitter *JCE; // JCE object
60 JITMemoryManager *JMM;
6061 std::vector EventListeners;
6162
6263 /// AllocateGVsWithCode - Some applications require that global variables and
116117 const std::vector &ArgValues);
117118
118119 /// getPointerToNamedFunction - This method returns the address of the
119 /// specified function by using the dlsym function call. As such it is only
120 /// specified function by using the MemoryManager. As such it is only
120121 /// useful for resolving library symbols, not code generated symbols.
121122 ///
122123 /// If AbortOnFailure is false and no function with the given name is
2222 #include "llvm/Support/ErrorHandling.h"
2323 #include "llvm/Support/raw_ostream.h"
2424 #include "llvm/Support/Memory.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/DynamicLibrary.h"
27 #include "llvm/Config/config.h"
2528 #include
2629 #include
2730 #include
2831 #include
32
33 #if defined(__linux__)
34 #if defined(HAVE_SYS_STAT_H)
35 #include
36 #endif
37 #include
38 #include
39 #endif
40
2941 using namespace llvm;
3042
3143 STATISTIC(NumSlabs, "Number of slabs of memory allocated by the JIT");
312324 /// DefaultSizeThreshold - For any allocation larger than this threshold, we
313325 /// should allocate a separate slab.
314326 static const size_t DefaultSizeThreshold;
327
328 /// getPointerToNamedFunction - This method returns the address of the
329 /// specified function by using the dlsym function call.
330 virtual void *getPointerToNamedFunction(const std::string &Name,
331 bool AbortOnFailure = true);
315332
316333 void AllocateGOT();
317334
756773 return true;
757774 }
758775
776 //===----------------------------------------------------------------------===//
777 // getPointerToNamedFunction() implementation.
778 //===----------------------------------------------------------------------===//
779
780 // AtExitHandlers - List of functions to call when the program exits,
781 // registered with the atexit() library function.
782 static std::vector AtExitHandlers;
783
784 /// runAtExitHandlers - Run any functions registered by the program's
785 /// calls to atexit(3), which we intercept and store in
786 /// AtExitHandlers.
787 ///
788 static void runAtExitHandlers() {
789 while (!AtExitHandlers.empty()) {
790 void (*Fn)() = AtExitHandlers.back();
791 AtExitHandlers.pop_back();
792 Fn();
793 }
794 }
795
796 //===----------------------------------------------------------------------===//
797 // Function stubs that are invoked instead of certain library calls
798 //
799 // Force the following functions to be linked in to anything that uses the
800 // JIT. This is a hack designed to work around the all-too-clever Glibc
801 // strategy of making these functions work differently when inlined vs. when
802 // not inlined, and hiding their real definitions in a separate archive file
803 // that the dynamic linker can't see. For more info, search for
804 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
805 #if defined(__linux__)
806 /* stat functions are redirecting to __xstat with a version number. On x86-64
807 * linking with libc_nonshared.a and -Wl,--export-dynamic doesn't make 'stat'
808 * available as an exported symbol, so we have to add it explicitly.
809 */
810 namespace {
811 class StatSymbols {
812 public:
813 StatSymbols() {
814 sys::DynamicLibrary::AddSymbol("stat", (void*)(intptr_t)stat);
815 sys::DynamicLibrary::AddSymbol("fstat", (void*)(intptr_t)fstat);
816 sys::DynamicLibrary::AddSymbol("lstat", (void*)(intptr_t)lstat);
817 sys::DynamicLibrary::AddSymbol("stat64", (void*)(intptr_t)stat64);
818 sys::DynamicLibrary::AddSymbol("\x1stat64", (void*)(intptr_t)stat64);
819 sys::DynamicLibrary::AddSymbol("\x1open64", (void*)(intptr_t)open64);
820 sys::DynamicLibrary::AddSymbol("\x1lseek64", (void*)(intptr_t)lseek64);
821 sys::DynamicLibrary::AddSymbol("fstat64", (void*)(intptr_t)fstat64);
822 sys::DynamicLibrary::AddSymbol("lstat64", (void*)(intptr_t)lstat64);
823 sys::DynamicLibrary::AddSymbol("atexit", (void*)(intptr_t)atexit);
824 sys::DynamicLibrary::AddSymbol("mknod", (void*)(intptr_t)mknod);
825 }
826 };
827 }
828 static StatSymbols initStatSymbols;
829 #endif // __linux__
830
831 // jit_exit - Used to intercept the "exit" library call.
832 static void jit_exit(int Status) {
833 runAtExitHandlers(); // Run atexit handlers...
834 exit(Status);
835 }
836
837 // jit_atexit - Used to intercept the "atexit" library call.
838 static int jit_atexit(void (*Fn)()) {
839 AtExitHandlers.push_back(Fn); // Take note of atexit handler...
840 return 0; // Always successful
841 }
842
843 static int jit_noop() {
844 return 0;
845 }
846
847 //===----------------------------------------------------------------------===//
848 //
849 /// getPointerToNamedFunction - This method returns the address of the specified
850 /// function by using the dynamic loader interface. As such it is only useful
851 /// for resolving library symbols, not code generated symbols.
852 ///
853 void *DefaultJITMemoryManager::getPointerToNamedFunction(const std::string &Name,
854 bool AbortOnFailure) {
855 // Check to see if this is one of the functions we want to intercept. Note,
856 // we cast to intptr_t here to silence a -pedantic warning that complains
857 // about casting a function pointer to a normal pointer.
858 if (Name == "exit") return (void*)(intptr_t)&jit_exit;
859 if (Name == "atexit") return (void*)(intptr_t)&jit_atexit;
860
861 // We should not invoke parent's ctors/dtors from generated main()!
862 // On Mingw and Cygwin, the symbol __main is resolved to
863 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
864 // (and register wrong callee's dtors with atexit(3)).
865 // We expect ExecutionEngine::runStaticConstructorsDestructors()
866 // is called before ExecutionEngine::runFunctionAsMain() is called.
867 if (Name == "__main") return (void*)(intptr_t)&jit_noop;
868
869 const char *NameStr = Name.c_str();
870 // If this is an asm specifier, skip the sentinal.
871 if (NameStr[0] == 1) ++NameStr;
872
873 // If it's an external function, look it up in the process image...
874 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
875 if (Ptr) return Ptr;
876
877 // If it wasn't found and if it starts with an underscore ('_') character,
878 // try again without the underscore.
879 if (NameStr[0] == '_') {
880 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
881 if (Ptr) return Ptr;
882 }
883
884 // Darwin/PPC adds $LDBLStub suffixes to various symbols like printf. These
885 // are references to hidden visibility symbols that dlsym cannot resolve.
886 // If we have one of these, strip off $LDBLStub and try again.
887 #if defined(__APPLE__) && defined(__ppc__)
888 if (Name.size() > 9 && Name[Name.size()-9] == '$' &&
889 memcmp(&Name[Name.size()-8], "LDBLStub", 8) == 0) {
890 // First try turning $LDBLStub into $LDBL128. If that fails, strip it off.
891 // This mirrors logic in libSystemStubs.a.
892 std::string Prefix = std::string(Name.begin(), Name.end()-9);
893 if (void *Ptr = getPointerToNamedFunction(Prefix+"$LDBL128", false))
894 return Ptr;
895 if (void *Ptr = getPointerToNamedFunction(Prefix, false))
896 return Ptr;
897 }
898 #endif
899
900 if (AbortOnFailure) {
901 report_fatal_error("Program used external function '"+Name+
902 "' which could not be resolved!");
903 }
904 return 0;
905 }
906
907
908
759909 JITMemoryManager *JITMemoryManager::CreateDefaultMemManager() {
760910 return new DefaultJITMemoryManager();
761911 }
0 add_llvm_library(LLVMMCJIT
11 MCJIT.cpp
22 MCJITMemoryManager.cpp
3 Intercept.cpp
43 )
+0
-162
lib/ExecutionEngine/MCJIT/Intercept.cpp less more
None //===-- Intercept.cpp - System function interception routines -------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // If a function call occurs to an external function, the JIT is designed to use
10 // the dynamic loader interface to find a function to call. This is useful for
11 // calling system calls and library functions that are not available in LLVM.
12 // Some system calls, however, need to be handled specially. For this reason,
13 // we intercept some of them here and use our own stubs to handle them.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "MCJIT.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/DynamicLibrary.h"
20 #include "llvm/Config/config.h"
21 using namespace llvm;
22
23 // AtExitHandlers - List of functions to call when the program exits,
24 // registered with the atexit() library function.
25 static std::vector AtExitHandlers;
26
27 /// runAtExitHandlers - Run any functions registered by the program's
28 /// calls to atexit(3), which we intercept and store in
29 /// AtExitHandlers.
30 ///
31 static void runAtExitHandlers() {
32 while (!AtExitHandlers.empty()) {
33 void (*Fn)() = AtExitHandlers.back();
34 AtExitHandlers.pop_back();
35 Fn();
36 }
37 }
38
39 //===----------------------------------------------------------------------===//
40 // Function stubs that are invoked instead of certain library calls
41 //===----------------------------------------------------------------------===//
42
43 // Force the following functions to be linked in to anything that uses the
44 // JIT. This is a hack designed to work around the all-too-clever Glibc
45 // strategy of making these functions work differently when inlined vs. when
46 // not inlined, and hiding their real definitions in a separate archive file
47 // that the dynamic linker can't see. For more info, search for
48 // 'libc_nonshared.a' on Google, or read http://llvm.org/PR274.
49 #if defined(__linux__)
50 #if defined(HAVE_SYS_STAT_H)
51 #include
52 #endif
53 #include
54 #include
55 /* stat functions are redirecting to __xstat with a version number. On x86-64
56 * linking with libc_nonshared.a and -Wl,--export-dynamic doesn't make 'stat'
57 * available as an exported symbol, so we have to add it explicitly.
58 */
59 namespace {
60 class StatSymbols {
61 public:
62 StatSymbols() {
63 sys::DynamicLibrary::AddSymbol("stat", (void*)(intptr_t)stat);
64 sys::DynamicLibrary::AddSymbol("fstat", (void*)(intptr_t)fstat);
65 sys::DynamicLibrary::AddSymbol("lstat", (void*)(intptr_t)lstat);
66 sys::DynamicLibrary::AddSymbol("stat64", (void*)(intptr_t)stat64);
67 sys::DynamicLibrary::AddSymbol("\x1stat64", (void*)(intptr_t)stat64);
68 sys::DynamicLibrary::AddSymbol("\x1open64", (void*)(intptr_t)open64);
69 sys::DynamicLibrary::AddSymbol("\x1lseek64", (void*)(intptr_t)lseek64);
70 sys::DynamicLibrary::AddSymbol("fstat64", (void*)(intptr_t)fstat64);
71 sys::DynamicLibrary::AddSymbol("lstat64", (void*)(intptr_t)lstat64);
72 sys::DynamicLibrary::AddSymbol("atexit", (void*)(intptr_t)atexit);
73 sys::DynamicLibrary::AddSymbol("mknod", (void*)(intptr_t)mknod);
74 }
75 };
76 }
77 static StatSymbols initStatSymbols;
78 #endif // __linux__
79
80 // jit_exit - Used to intercept the "exit" library call.
81 static void jit_exit(int Status) {
82 runAtExitHandlers(); // Run atexit handlers...
83 exit(Status);
84 }
85
86 // jit_atexit - Used to intercept the "atexit" library call.
87 static int jit_atexit(void (*Fn)()) {
88 AtExitHandlers.push_back(Fn); // Take note of atexit handler...
89 return 0; // Always successful
90 }
91
92 static int jit_noop() {
93 return 0;
94 }
95
96 //===----------------------------------------------------------------------===//
97 //
98 /// getPointerToNamedFunction - This method returns the address of the specified
99 /// function by using the dynamic loader interface. As such it is only useful
100 /// for resolving library symbols, not code generated symbols.
101 ///
102 void *MCJIT::getPointerToNamedFunction(const std::string &Name,
103 bool AbortOnFailure) {
104 if (!isSymbolSearchingDisabled()) {
105 // Check to see if this is one of the functions we want to intercept. Note,
106 // we cast to intptr_t here to silence a -pedantic warning that complains
107 // about casting a function pointer to a normal pointer.
108 if (Name == "exit") return (void*)(intptr_t)&jit_exit;
109 if (Name == "atexit") return (void*)(intptr_t)&jit_atexit;
110
111 // We should not invoke parent's ctors/dtors from generated main()!
112 // On Mingw and Cygwin, the symbol __main is resolved to
113 // callee's(eg. tools/lli) one, to invoke wrong duplicated ctors
114 // (and register wrong callee's dtors with atexit(3)).
115 // We expect ExecutionEngine::runStaticConstructorsDestructors()
116 // is called before ExecutionEngine::runFunctionAsMain() is called.
117 if (Name == "__main") return (void*)(intptr_t)&jit_noop;
118
119 const char *NameStr = Name.c_str();
120 // If this is an asm specifier, skip the sentinal.
121 if (NameStr[0] == 1) ++NameStr;
122
123 // If it's an external function, look it up in the process image...
124 void *Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr);
125 if (Ptr) return Ptr;
126
127 // If it wasn't found and if it starts with an underscore ('_') character,
128 // and has an asm specifier, try again without the underscore.
129 if (Name[0] == 1 && NameStr[0] == '_') {
130 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(NameStr+1);
131 if (Ptr) return Ptr;
132 }
133
134 // Darwin/PPC adds $LDBLStub suffixes to various symbols like printf. These
135 // are references to hidden visibility symbols that dlsym cannot resolve.
136 // If we have one of these, strip off $LDBLStub and try again.
137 #if defined(__APPLE__) && defined(__ppc__)
138 if (Name.size() > 9 && Name[Name.size()-9] == '$' &&
139 memcmp(&Name[Name.size()-8], "LDBLStub", 8) == 0) {
140 // First try turning $LDBLStub into $LDBL128. If that fails, strip it off.
141 // This mirrors logic in libSystemStubs.a.
142 std::string Prefix = std::string(Name.begin(), Name.end()-9);
143 if (void *Ptr = getPointerToNamedFunction(Prefix+"$LDBL128", false))
144 return Ptr;
145 if (void *Ptr = getPointerToNamedFunction(Prefix, false))
146 return Ptr;
147 }
148 #endif
149 }
150
151 /// If a LazyFunctionCreator is installed, use it to get/create the function.
152 if (LazyFunctionCreator)
153 if (void *RP = LazyFunctionCreator(Name))
154 return RP;
155
156 if (AbortOnFailure) {
157 report_fatal_error("Program used external function '"+Name+
158 "' which could not be resolved!");
159 }
160 return 0;
161 }
214214
215215 llvm_unreachable("Full-featured argument passing not supported yet!");
216216 }
217
218 void *MCJIT::getPointerToNamedFunction(const std::string &Name,
219 bool AbortOnFailure){
220 if (!isSymbolSearchingDisabled() && MemMgr) {
221 void *ptr = MemMgr->getPointerToNamedFunction(Name, false);
222 if (ptr)
223 return ptr;
224 }
225
226 /// If a LazyFunctionCreator is installed, use it to get/create the function.
227 if (LazyFunctionCreator)
228 if (void *RP = LazyFunctionCreator(Name))
229 return RP;
230
231 if (AbortOnFailure) {
232 report_fatal_error("Program used external function '"+Name+
233 "' which could not be resolved!");
234 }
235 return 0;
236 }
6666 ///
6767 virtual void *getPointerToNamedFunction(const std::string &Name,
6868 bool AbortOnFailure = true);
69
6970 /// mapSectionAddress - map a section to its target address space value.
7071 /// Map the address of a JIT section as returned from the memory manager
7172 /// to the address in the target process as the running code will see it.
2626 // FIXME: Multiple modules.
2727 Module *M;
2828 public:
29 MCJITMemoryManager(JITMemoryManager *jmm, Module *m) : JMM(jmm), M(m) {}
29 MCJITMemoryManager(JITMemoryManager *jmm, Module *m) :
30 JMM(jmm?jmm:JITMemoryManager::CreateDefaultMemManager()), M(m) {}
3031 // We own the JMM, so make sure to delete it.
3132 ~MCJITMemoryManager() { delete JMM; }
3233
4041 return JMM->allocateCodeSection(Size, Alignment, SectionID);
4142 }
4243
44 virtual void *getPointerToNamedFunction(const std::string &Name,
45 bool AbortOnFailure = true) {
46 return JMM->getPointerToNamedFunction(Name, AbortOnFailure);
47 }
48
4349 // Allocate ActualSize bytes, or more, for the named function. Return
4450 // a pointer to the allocated memory and update Size to reflect how much
4551 // memory was acutally allocated.
4955 if (Name[0] == '_') ++Name;
5056 Function *F = M->getFunction(Name);
5157 // Some ObjC names have a prefixed \01 in the IR. If we failed to find
52 // the symbol and it's of the ObjC conventions (starts with "-" or
58 // the symbol and it's of the ObjC conventions (starts with "-" or
5359 // "+"), try prepending a \01 and see if we can find it that way.
5460 if (!F && (Name[0] == '-' || Name[0] == '+'))
5561 F = M->getFunction((Twine("\1") + Name).str());
6060 uint8_t *startFunctionBody(const char *Name, uintptr_t &Size);
6161 void endFunctionBody(const char *Name, uint8_t *FunctionStart,
6262 uint8_t *FunctionEnd);
63
64 virtual void *getPointerToNamedFunction(const std::string &Name,
65 bool AbortOnFailure = true) {
66 return 0;
67 }
68
6369 };
6470
6571 uint8_t *TrivialMemoryManager::allocateCodeSection(uintptr_t Size,
6262 RecordingJITMemoryManager()
6363 : Base(JITMemoryManager::CreateDefaultMemManager()) {
6464 stubsAllocated = 0;
65 }
66 virtual void *getPointerToNamedFunction(const std::string &Name,
67 bool AbortOnFailure = true) {
68 return Base->getPointerToNamedFunction(Name, AbortOnFailure);
6569 }
6670
6771 virtual void setMemoryWritable() { Base->setMemoryWritable(); }