DynamicLibrary.inc revision 317969
1//===- Unix/DynamicLibrary.cpp - Unix DL Implementation ---------*- C++ -*-===//
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//
10// This file provides the UNIX specific implementation of DynamicLibrary.
11//
12//===----------------------------------------------------------------------===//
13
14#if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
15#include <dlfcn.h>
16
17DynamicLibrary::HandleSet::~HandleSet() {
18  for (void *Handle : Handles)
19    ::dlclose(Handle);
20  if (Process)
21    ::dlclose(Process);
22}
23
24void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
25  void *Handle = ::dlopen(File, RTLD_LAZY|RTLD_GLOBAL);
26  if (!Handle) {
27    if (Err) *Err = ::dlerror();
28    return &DynamicLibrary::Invalid;
29  }
30
31#ifdef __CYGWIN__
32  // Cygwin searches symbols only in the main
33  // with the handle of dlopen(NULL, RTLD_GLOBAL).
34  if (!File)
35    Handle = RTLD_DEFAULT;
36#endif
37
38  return Handle;
39}
40
41void DynamicLibrary::HandleSet::DLClose(void *Handle) {
42  ::dlclose(Handle);
43}
44
45void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
46  return ::dlsym(Handle, Symbol);
47}
48
49#else // !HAVE_DLOPEN
50
51DynamicLibrary::HandleSet::~HandleSet() {}
52
53void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
54  if (Err) *Err = "dlopen() not supported on this platform";
55  return &Invalid;
56}
57
58void DynamicLibrary::HandleSet::DLClose(void *Handle) {
59}
60
61void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
62  return nullptr;
63}
64
65#endif
66
67// Must declare the symbols in the global namespace.
68static void *DoSearch(const char* SymbolName) {
69#define EXPLICIT_SYMBOL(SYM) \
70   extern void *SYM; if (!strcmp(SymbolName, #SYM)) return &SYM
71
72  // If this is darwin, it has some funky issues, try to solve them here.  Some
73  // important symbols are marked 'private external' which doesn't allow
74  // SearchForAddressOfSymbol to find them.  As such, we special case them here,
75  // there is only a small handful of them.
76
77#ifdef __APPLE__
78  {
79    // __eprintf is sometimes used for assert() handling on x86.
80    //
81    // FIXME: Currently disabled when using Clang, as we don't always have our
82    // runtime support libraries available.
83#ifndef __clang__
84#ifdef __i386__
85    EXPLICIT_SYMBOL(__eprintf);
86#endif
87#endif
88  }
89#endif
90
91#ifdef __CYGWIN__
92  {
93    EXPLICIT_SYMBOL(_alloca);
94    EXPLICIT_SYMBOL(__main);
95  }
96#endif
97
98#undef EXPLICIT_SYMBOL
99
100// This macro returns the address of a well-known, explicit symbol
101#define EXPLICIT_SYMBOL(SYM) \
102   if (!strcmp(SymbolName, #SYM)) return &SYM
103
104// On linux we have a weird situation. The stderr/out/in symbols are both
105// macros and global variables because of standards requirements. So, we
106// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
107#if defined(__linux__) and !defined(__ANDROID__)
108  {
109    EXPLICIT_SYMBOL(stderr);
110    EXPLICIT_SYMBOL(stdout);
111    EXPLICIT_SYMBOL(stdin);
112  }
113#else
114  // For everything else, we want to check to make sure the symbol isn't defined
115  // as a macro before using EXPLICIT_SYMBOL.
116  {
117#ifndef stdin
118    EXPLICIT_SYMBOL(stdin);
119#endif
120#ifndef stdout
121    EXPLICIT_SYMBOL(stdout);
122#endif
123#ifndef stderr
124    EXPLICIT_SYMBOL(stderr);
125#endif
126  }
127#endif
128#undef EXPLICIT_SYMBOL
129
130  return nullptr;
131}
132