blob: cd9927a193a579408d745247bfb35bbed0a0cb28 [file] [log] [blame]
Reid Spencer0de02a62004-11-18 04:33:39 +00001//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencer0de02a62004-11-18 04:33:39 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencer0de02a62004-11-18 04:33:39 +00008//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system DynamicLibrary concept.
11//
Chris Lattnera9b7d602009-07-07 18:17:07 +000012// FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
13// not thread safe!
14//
Reid Spencer0de02a62004-11-18 04:33:39 +000015//===----------------------------------------------------------------------===//
16
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000017#include "llvm/Support/DynamicLibrary.h"
18#include "llvm/Support/Mutex.h"
Jeff Cohena4c97512004-12-24 16:26:47 +000019#include "llvm/Config/config.h"
Duncan Sands4520dd22008-10-08 07:23:46 +000020#include <cstdio>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000021#include <cstring>
Jeff Cohen85046902006-01-30 04:33:51 +000022#include <map>
Chris Lattner72501b42009-07-07 18:01:58 +000023#include <vector>
Jeff Cohen85046902006-01-30 04:33:51 +000024
25// Collection of symbol name/value pairs to be searched prior to any libraries.
Chris Lattnera9b7d602009-07-07 18:17:07 +000026static std::map<std::string, void*> *ExplicitSymbols = 0;
Owen Andersonf37feb92009-06-25 18:12:44 +000027
Dan Gohmanb3579832010-04-15 17:08:50 +000028namespace {
29
30struct ExplicitSymbolsDeleter {
Torok Edwin72ddf7b2009-08-31 16:12:29 +000031 ~ExplicitSymbolsDeleter() {
32 if (ExplicitSymbols)
33 delete ExplicitSymbols;
34 }
Dan Gohmanb3579832010-04-15 17:08:50 +000035};
36
37}
38
39static ExplicitSymbolsDeleter Dummy;
Torok Edwin72ddf7b2009-08-31 16:12:29 +000040
Chris Lattneradcbce02006-07-07 17:12:36 +000041void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
42 void *symbolValue) {
Chris Lattnera9b7d602009-07-07 18:17:07 +000043 if (ExplicitSymbols == 0)
44 ExplicitSymbols = new std::map<std::string, void*>();
45 (*ExplicitSymbols)[symbolName] = symbolValue;
Jeff Cohen85046902006-01-30 04:33:51 +000046}
Jeff Cohen1a466352004-12-24 07:57:09 +000047
Jeff Cohena4c97512004-12-24 16:26:47 +000048#ifdef LLVM_ON_WIN32
Jeff Cohen1a466352004-12-24 07:57:09 +000049
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000050#include "Windows/DynamicLibrary.inc"
Jeff Cohen1a466352004-12-24 07:57:09 +000051
52#else
53
Chris Lattner21aa3472010-04-09 20:45:04 +000054#if HAVE_DLFCN_H
Devang Patele45252e2008-02-13 17:11:39 +000055#include <dlfcn.h>
Chris Lattner28dabf72004-12-03 23:02:42 +000056using namespace llvm;
57using namespace llvm::sys;
Reid Spencer0de02a62004-11-18 04:33:39 +000058
59//===----------------------------------------------------------------------===//
60//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000061//=== independent code.
Reid Spencer0de02a62004-11-18 04:33:39 +000062//===----------------------------------------------------------------------===//
63
Owen Anderson27cb5e62010-11-18 18:49:05 +000064static SmartMutex<true>* HandlesMutex;
Chris Lattnera9b7d602009-07-07 18:17:07 +000065static std::vector<void *> *OpenedHandles = 0;
Reid Spencer19cd4a92004-11-29 13:33:28 +000066
Owen Anderson27cb5e62010-11-18 18:49:05 +000067static bool InitializeMutex() {
68 HandlesMutex = new SmartMutex<true>;
69 return HandlesMutex != 0;
70}
71
72static bool EnsureMutexInitialized() {
73 static bool result = InitializeMutex();
74 return result;
75}
76
Reid Spencer19cd4a92004-11-29 13:33:28 +000077
Chris Lattneradcbce02006-07-07 17:12:36 +000078bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
79 std::string *ErrMsg) {
Chris Lattnerae55e042008-07-10 00:52:20 +000080 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
Devang Patele45252e2008-02-13 17:11:39 +000081 if (H == 0) {
Chris Lattnera9b7d602009-07-07 18:17:07 +000082 if (ErrMsg) *ErrMsg = dlerror();
Chris Lattneradcbce02006-07-07 17:12:36 +000083 return true;
84 }
Chris Lattner6ccd0da2010-08-17 15:42:43 +000085#ifdef __CYGWIN__
86 // Cygwin searches symbols only in the main
87 // with the handle of dlopen(NULL, RTLD_GLOBAL).
88 if (Filename == NULL)
89 H = RTLD_DEFAULT;
90#endif
Owen Anderson27cb5e62010-11-18 18:49:05 +000091 EnsureMutexInitialized();
92 SmartScopedLock<true> Lock(*HandlesMutex);
Chris Lattnera9b7d602009-07-07 18:17:07 +000093 if (OpenedHandles == 0)
94 OpenedHandles = new std::vector<void *>();
95 OpenedHandles->push_back(H);
Chris Lattneradcbce02006-07-07 17:12:36 +000096 return false;
Reid Spencer19cd4a92004-11-29 13:33:28 +000097}
Chris Lattner21aa3472010-04-09 20:45:04 +000098#else
99
100using namespace llvm;
101using namespace llvm::sys;
102
103bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
104 std::string *ErrMsg) {
105 if (ErrMsg) *ErrMsg = "dlopen() not supported on this platform";
106 return true;
107}
108#endif
Reid Spencer19cd4a92004-11-29 13:33:28 +0000109
Jeffrey Yasskin62207542010-03-11 06:14:32 +0000110namespace llvm {
111void *SearchForAddressOfSpecialSymbol(const char* symbolName);
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000112}
113
114void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
115 // First check symbols added via AddSymbol().
116 if (ExplicitSymbols) {
117 std::map<std::string, void *>::iterator I =
118 ExplicitSymbols->find(symbolName);
119 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000120
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000121 if (I != E)
122 return I->second;
123 }
124
Chris Lattner21aa3472010-04-09 20:45:04 +0000125#if HAVE_DLFCN_H
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000126 // Now search the libraries.
Owen Anderson27cb5e62010-11-18 18:49:05 +0000127 EnsureMutexInitialized();
128 SmartScopedLock<true> Lock(*HandlesMutex);
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000129 if (OpenedHandles) {
130 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
131 E = OpenedHandles->end(); I != E; ++I) {
132 //lt_ptr ptr = lt_dlsym(*I, symbolName);
133 void *ptr = dlsym(*I, symbolName);
134 if (ptr) {
135 return ptr;
136 }
137 }
138 }
Chris Lattner21aa3472010-04-09 20:45:04 +0000139#endif
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000140
Jeffrey Yasskin62207542010-03-11 06:14:32 +0000141 if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000142 return Result;
Anton Korobeynikov96ea2092007-12-03 05:30:41 +0000143
Reid Spencer02f20d32007-01-19 21:41:04 +0000144// This macro returns the address of a well-known, explicit symbol
Reid Spencer11f457a2007-01-10 19:50:43 +0000145#define EXPLICIT_SYMBOL(SYM) \
146 if (!strcmp(symbolName, #SYM)) return &SYM
Reid Spencer02f20d32007-01-19 21:41:04 +0000147
148// On linux we have a weird situation. The stderr/out/in symbols are both
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000149// macros and global variables because of standards requirements. So, we
Reid Spencer02f20d32007-01-19 21:41:04 +0000150// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
151#if defined(__linux__)
152 {
153 EXPLICIT_SYMBOL(stderr);
154 EXPLICIT_SYMBOL(stdout);
155 EXPLICIT_SYMBOL(stdin);
156 }
157#else
158 // For everything else, we want to check to make sure the symbol isn't defined
159 // as a macro before using EXPLICIT_SYMBOL.
Reid Spencer11f457a2007-01-10 19:50:43 +0000160 {
Reid Spencer65de7422007-01-11 00:35:10 +0000161#ifndef stdin
Reid Spencer11f457a2007-01-10 19:50:43 +0000162 EXPLICIT_SYMBOL(stdin);
Reid Spencer81e39542007-01-19 21:30:39 +0000163#endif
164#ifndef stdout
Reid Spencer11f457a2007-01-10 19:50:43 +0000165 EXPLICIT_SYMBOL(stdout);
Reid Spencer81e39542007-01-19 21:30:39 +0000166#endif
167#ifndef stderr
Reid Spencer11f457a2007-01-10 19:50:43 +0000168 EXPLICIT_SYMBOL(stderr);
Reid Spencer65de7422007-01-11 00:35:10 +0000169#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000170 }
Reid Spencer02f20d32007-01-19 21:41:04 +0000171#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000172#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000173
Reid Spencer19cd4a92004-11-29 13:33:28 +0000174 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000175}
176
Jeff Cohena4c97512004-12-24 16:26:47 +0000177#endif // LLVM_ON_WIN32