blob: 4260d192e850b704ee873c9ac252484a03a5951f [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system DynamicLibrary concept.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/System/DynamicLibrary.h"
Owen Anderson132a2f22009-06-25 18:12:44 +000015#include "llvm/Support/ManagedStatic.h"
16#include "llvm/System/RWMutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Config/config.h"
Duncan Sands05f68372008-10-08 07:23:46 +000018#include <cstdio>
Duncan Sandsfca20142008-01-09 19:42:09 +000019#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include <map>
21
22// Collection of symbol name/value pairs to be searched prior to any libraries.
Owen Anderson132a2f22009-06-25 18:12:44 +000023static std::map<std::string, void*> symbols;
24static llvm::sys::SmartRWMutex<true> SymbolsLock;
25
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
27 void *symbolValue) {
Owen Anderson132a2f22009-06-25 18:12:44 +000028 llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
29 symbols[symbolName] = symbolValue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030}
31
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#ifdef LLVM_ON_WIN32
33
34#include "Win32/DynamicLibrary.inc"
35
36#else
37
Devang Patelaa4ea5a2008-02-13 17:11:39 +000038#include <dlfcn.h>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include <cassert>
40using namespace llvm;
41using namespace llvm::sys;
42
43//===----------------------------------------------------------------------===//
44//=== WARNING: Implementation here must contain only TRULY operating system
45//=== independent code.
46//===----------------------------------------------------------------------===//
47
Devang Patelaa4ea5a2008-02-13 17:11:39 +000048static std::vector<void *> OpenedHandles;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049
Devang Patel79df1c62008-03-13 16:55:34 +000050DynamicLibrary::DynamicLibrary() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52DynamicLibrary::~DynamicLibrary() {
Owen Anderson132a2f22009-06-25 18:12:44 +000053 SmartScopedWriter<true> Writer(&SymbolsLock);
Devang Patelaa4ea5a2008-02-13 17:11:39 +000054 while(!OpenedHandles.empty()) {
Chris Lattnerfce334d2009-07-07 17:50:11 +000055 void *H = OpenedHandles.back();
56 OpenedHandles.pop_back();
Devang Patelaa4ea5a2008-02-13 17:11:39 +000057 dlclose(H);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 }
59}
60
61bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
62 std::string *ErrMsg) {
Owen Anderson132a2f22009-06-25 18:12:44 +000063 SmartScopedWriter<true> Writer(&SymbolsLock);
Chris Lattner859cf632008-07-10 00:52:20 +000064 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
Devang Patelaa4ea5a2008-02-13 17:11:39 +000065 if (H == 0) {
Chris Lattneracc81932008-03-12 00:50:01 +000066 if (ErrMsg)
67 *ErrMsg = dlerror();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 return true;
69 }
Devang Patelaa4ea5a2008-02-13 17:11:39 +000070 OpenedHandles.push_back(H);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 return false;
72}
73
74void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 // First check symbols added via AddSymbol().
Owen Anderson132a2f22009-06-25 18:12:44 +000076 SymbolsLock.reader_acquire();
77 std::map<std::string, void *>::iterator I = symbols.find(symbolName);
78 std::map<std::string, void *>::iterator E = symbols.end();
79 SymbolsLock.reader_release();
80
81 if (I != E)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 return I->second;
83
Owen Anderson132a2f22009-06-25 18:12:44 +000084 SymbolsLock.writer_acquire();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 // Now search the libraries.
Devang Patelaa4ea5a2008-02-13 17:11:39 +000086 for (std::vector<void *>::iterator I = OpenedHandles.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 E = OpenedHandles.end(); I != E; ++I) {
Devang Patelaa4ea5a2008-02-13 17:11:39 +000088 //lt_ptr ptr = lt_dlsym(*I, symbolName);
89 void *ptr = dlsym(*I, symbolName);
Owen Anderson132a2f22009-06-25 18:12:44 +000090 if (ptr) {
91 SymbolsLock.writer_release();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 return ptr;
Owen Anderson132a2f22009-06-25 18:12:44 +000093 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 }
Owen Anderson132a2f22009-06-25 18:12:44 +000095 SymbolsLock.writer_release();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
Anton Korobeynikov942cbab2007-12-03 05:30:41 +000097#define EXPLICIT_SYMBOL(SYM) \
98 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
99
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 // If this is darwin, it has some funky issues, try to solve them here. Some
101 // important symbols are marked 'private external' which doesn't allow
102 // SearchForAddressOfSymbol to find them. As such, we special case them here,
103 // there is only a small handful of them.
104
105#ifdef __APPLE__
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 {
107 EXPLICIT_SYMBOL(__ashldi3);
108 EXPLICIT_SYMBOL(__ashrdi3);
109 EXPLICIT_SYMBOL(__cmpdi2);
110 EXPLICIT_SYMBOL(__divdi3);
111 EXPLICIT_SYMBOL(__eprintf);
112 EXPLICIT_SYMBOL(__fixdfdi);
113 EXPLICIT_SYMBOL(__fixsfdi);
114 EXPLICIT_SYMBOL(__fixunsdfdi);
115 EXPLICIT_SYMBOL(__fixunssfdi);
116 EXPLICIT_SYMBOL(__floatdidf);
117 EXPLICIT_SYMBOL(__floatdisf);
118 EXPLICIT_SYMBOL(__lshrdi3);
119 EXPLICIT_SYMBOL(__moddi3);
120 EXPLICIT_SYMBOL(__udivdi3);
121 EXPLICIT_SYMBOL(__umoddi3);
122 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123#endif
124
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000125#ifdef __CYGWIN__
126 {
127 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000128 EXPLICIT_SYMBOL(__main);
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000129 }
130#endif
131
132#undef EXPLICIT_SYMBOL
133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134// This macro returns the address of a well-known, explicit symbol
135#define EXPLICIT_SYMBOL(SYM) \
136 if (!strcmp(symbolName, #SYM)) return &SYM
137
138// On linux we have a weird situation. The stderr/out/in symbols are both
139// macros and global variables because of standards requirements. So, we
140// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
141#if defined(__linux__)
142 {
143 EXPLICIT_SYMBOL(stderr);
144 EXPLICIT_SYMBOL(stdout);
145 EXPLICIT_SYMBOL(stdin);
146 }
147#else
148 // For everything else, we want to check to make sure the symbol isn't defined
149 // as a macro before using EXPLICIT_SYMBOL.
150 {
151#ifndef stdin
152 EXPLICIT_SYMBOL(stdin);
153#endif
154#ifndef stdout
155 EXPLICIT_SYMBOL(stdout);
156#endif
157#ifndef stderr
158 EXPLICIT_SYMBOL(stderr);
159#endif
160 }
161#endif
162#undef EXPLICIT_SYMBOL
163
164 return 0;
165}
166
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167#endif // LLVM_ON_WIN32