LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DynamicLibrary.cpp
Go to the documentation of this file.
1 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- 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 header file implements the operating system DynamicLibrary concept.
11 //
12 // FIXME: This file leaks ExplicitSymbols and OpenedHandles!
13 //
14 //===----------------------------------------------------------------------===//
15 
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/Support/Mutex.h"
22 #include "llvm-c/Support.h"
23 #include <cstdio>
24 #include <cstring>
25 
26 // Collection of symbol name/value pairs to be searched prior to any libraries.
29 
31  void *symbolValue) {
32  SmartScopedLock<true> lock(*SymbolsMutex);
33  (*ExplicitSymbols)[symbolName] = symbolValue;
34 }
35 
36 char llvm::sys::DynamicLibrary::Invalid = 0;
37 
38 #ifdef LLVM_ON_WIN32
39 
41 
42 #else
43 
44 #if HAVE_DLFCN_H
45 #include <dlfcn.h>
46 using namespace llvm;
47 using namespace llvm::sys;
48 
49 //===----------------------------------------------------------------------===//
50 //=== WARNING: Implementation here must contain only TRULY operating system
51 //=== independent code.
52 //===----------------------------------------------------------------------===//
53 
55 
57  std::string *errMsg) {
58  SmartScopedLock<true> lock(*SymbolsMutex);
59 
60  void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL);
61  if (handle == 0) {
62  if (errMsg) *errMsg = dlerror();
63  return DynamicLibrary();
64  }
65 
66 #ifdef __CYGWIN__
67  // Cygwin searches symbols only in the main
68  // with the handle of dlopen(NULL, RTLD_GLOBAL).
69  if (filename == NULL)
70  handle = RTLD_DEFAULT;
71 #endif
72 
73  if (OpenedHandles == 0)
75 
76  // If we've already loaded this library, dlclose() the handle in order to
77  // keep the internal refcount at +1.
78  if (!OpenedHandles->insert(handle).second)
79  dlclose(handle);
80 
81  return DynamicLibrary(handle);
82 }
83 
84 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
85  if (!isValid())
86  return NULL;
87  return dlsym(Data, symbolName);
88 }
89 
90 #else
91 
92 using namespace llvm;
93 using namespace llvm::sys;
94 
96  std::string *errMsg) {
97  if (errMsg) *errMsg = "dlopen() not supported on this platform";
98  return DynamicLibrary();
99 }
100 
101 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
102  return NULL;
103 }
104 
105 #endif
106 
107 namespace llvm {
108 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
109 }
110 
111 void* DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) {
112  SmartScopedLock<true> Lock(*SymbolsMutex);
113 
114  // First check symbols added via AddSymbol().
115  if (ExplicitSymbols.isConstructed()) {
116  StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
117 
118  if (i != ExplicitSymbols->end())
119  return i->second;
120  }
121 
122 #if HAVE_DLFCN_H
123  // Now search the libraries.
124  if (OpenedHandles) {
126  E = OpenedHandles->end(); I != E; ++I) {
127  //lt_ptr ptr = lt_dlsym(*I, symbolName);
128  void *ptr = dlsym(*I, symbolName);
129  if (ptr) {
130  return ptr;
131  }
132  }
133  }
134 #endif
135 
136  if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
137  return Result;
138 
139 // This macro returns the address of a well-known, explicit symbol
140 #define EXPLICIT_SYMBOL(SYM) \
141  if (!strcmp(symbolName, #SYM)) return &SYM
142 
143 // On linux we have a weird situation. The stderr/out/in symbols are both
144 // macros and global variables because of standards requirements. So, we
145 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
146 #if defined(__linux__) and !defined(__ANDROID__)
147  {
148  EXPLICIT_SYMBOL(stderr);
149  EXPLICIT_SYMBOL(stdout);
150  EXPLICIT_SYMBOL(stdin);
151  }
152 #else
153  // For everything else, we want to check to make sure the symbol isn't defined
154  // as a macro before using EXPLICIT_SYMBOL.
155  {
156 #ifndef stdin
157  EXPLICIT_SYMBOL(stdin);
158 #endif
159 #ifndef stdout
160  EXPLICIT_SYMBOL(stdout);
161 #endif
162 #ifndef stderr
163  EXPLICIT_SYMBOL(stderr);
164 #endif
165  }
166 #endif
167 #undef EXPLICIT_SYMBOL
168 
169  return 0;
170 }
171 
172 #endif // LLVM_ON_WIN32
173 
174 //===----------------------------------------------------------------------===//
175 // C API.
176 //===----------------------------------------------------------------------===//
177 
178 LLVMBool LLVMLoadLibraryPermanently(const char* Filename) {
180 }
bool isConstructed() const
isConstructed - Return true if this object has not been created yet.
Definition: ManagedStatic.h:50
static void * SearchForAddressOfSymbol(const char *symbolName)
Search through libraries for address of a symbol.
int LLVMBool
Definition: Core.h:65
static llvm::ManagedStatic< llvm::sys::SmartMutex< true > > SymbolsMutex
void * getAddressOfSymbol(const char *symbolName)
static DenseSet< void * > * OpenedHandles
static ManagedStatic< sys::SmartRWMutex< true > > Lock
static void AddSymbol(StringRef symbolName, void *symbolValue)
Add searchable symbol/value pair.
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=0)
void * SearchForAddressOfSpecialSymbol(const char *symbolName)
#define EXPLICIT_SYMBOL(SYM)
static llvm::ManagedStatic< llvm::StringMap< void * > > ExplicitSymbols
LLVMBool LLVMLoadLibraryPermanently(const char *Filename)
#define I(x, y, z)
Definition: MD5.cpp:54
static DynamicLibrary getPermanentLibrary(const char *filename, std::string *errMsg=0)
Open a dynamic library permanently.
const StringRef filename(StringRef path)
Get filename.
Definition: Path.cpp:467