blob: d4bf0f797fc4472b543a012747b8bc8a8dbb5751 [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//
Misha Brukmanf976c852005-04-21 22:55:34 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencer0de02a62004-11-18 04:33:39 +00006// University of Illinois Open Source 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//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/System/DynamicLibrary.h"
Jeff Cohena4c97512004-12-24 16:26:47 +000015#include "llvm/Config/config.h"
Jeff Cohen85046902006-01-30 04:33:51 +000016#include <map>
17
18// Collection of symbol name/value pairs to be searched prior to any libraries.
19static std::map<std::string, void *> g_symbols;
20
Chris Lattneradcbce02006-07-07 17:12:36 +000021void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
22 void *symbolValue) {
Jeff Cohen85046902006-01-30 04:33:51 +000023 g_symbols[symbolName] = symbolValue;
24}
Jeff Cohen1a466352004-12-24 07:57:09 +000025
26// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
27// license and special exception would cause all of LLVM to be placed under
28// the LGPL. This is because the exception applies only when libtool is
29// used, and obviously libtool is not used with Visual Studio. An entirely
30// separate implementation is provided in win32/DynamicLibrary.cpp.
31
Jeff Cohena4c97512004-12-24 16:26:47 +000032#ifdef LLVM_ON_WIN32
Jeff Cohen1a466352004-12-24 07:57:09 +000033
Reid Spencerbccc8ab2005-01-09 23:29:00 +000034#include "Win32/DynamicLibrary.inc"
Jeff Cohen1a466352004-12-24 07:57:09 +000035
36#else
37
Reid Spencer29ae1772004-11-29 12:39:10 +000038#include "ltdl.h"
Reid Spencer0de02a62004-11-18 04:33:39 +000039#include <cassert>
Chris Lattner28dabf72004-12-03 23:02:42 +000040using namespace llvm;
41using namespace llvm::sys;
Reid Spencer0de02a62004-11-18 04:33:39 +000042
43//===----------------------------------------------------------------------===//
44//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000045//=== independent code.
Reid Spencer0de02a62004-11-18 04:33:39 +000046//===----------------------------------------------------------------------===//
47
Reid Spencer19cd4a92004-11-29 13:33:28 +000048static inline void check_ltdl_initialization() {
Reid Spencer99655e12006-08-25 19:54:53 +000049 static bool did_initialize_ltdl = false;
Reid Spencer19cd4a92004-11-29 13:33:28 +000050 if (!did_initialize_ltdl) {
Chris Lattner9b0d6f42006-08-30 20:37:06 +000051 int Err = lt_dlinit();
52 assert(0 == Err && "Can't init the ltdl library");
Reid Spencer19cd4a92004-11-29 13:33:28 +000053 did_initialize_ltdl = true;
54 }
55}
56
57static std::vector<lt_dlhandle> OpenedHandles;
58
Reid Spencer441cc2a2004-11-29 10:39:46 +000059DynamicLibrary::DynamicLibrary() : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000060 check_ltdl_initialization();
Reid Spencer441cc2a2004-11-29 10:39:46 +000061
Reid Spencer19cd4a92004-11-29 13:33:28 +000062 lt_dlhandle a_handle = lt_dlopen(0);
Reid Spencer441cc2a2004-11-29 10:39:46 +000063
Reid Spencer99655e12006-08-25 19:54:53 +000064 assert(a_handle == 0 || "Can't open program as dynamic library");
Misha Brukmanf976c852005-04-21 22:55:34 +000065
Reid Spencer19cd4a92004-11-29 13:33:28 +000066 handle = a_handle;
67 OpenedHandles.push_back(a_handle);
Reid Spencer441cc2a2004-11-29 10:39:46 +000068}
69
Reid Spencer99655e12006-08-25 19:54:53 +000070/*
Reid Spencer0de02a62004-11-18 04:33:39 +000071DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000072 check_ltdl_initialization();
Reid Spencer0de02a62004-11-18 04:33:39 +000073
Reid Spencer19cd4a92004-11-29 13:33:28 +000074 lt_dlhandle a_handle = lt_dlopen(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000075
Reid Spencer19cd4a92004-11-29 13:33:28 +000076 if (a_handle == 0)
77 a_handle = lt_dlopenext(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000078
Reid Spencer19cd4a92004-11-29 13:33:28 +000079 if (a_handle == 0)
80 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
81
82 handle = a_handle;
83 OpenedHandles.push_back(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000084}
Reid Spencer99655e12006-08-25 19:54:53 +000085*/
Reid Spencer0de02a62004-11-18 04:33:39 +000086
87DynamicLibrary::~DynamicLibrary() {
Reid Spencer19cd4a92004-11-29 13:33:28 +000088 lt_dlhandle a_handle = (lt_dlhandle) handle;
89 if (a_handle) {
90 lt_dlclose(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000091
Reid Spencer19cd4a92004-11-29 13:33:28 +000092 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
93 E = OpenedHandles.end(); I != E; ++I) {
94 if (*I == a_handle) {
95 // Note: don't use the swap/pop_back trick here. Order is important.
96 OpenedHandles.erase(I);
Chris Lattner2b80e8d2006-05-12 18:13:11 +000097 return;
Reid Spencer19cd4a92004-11-29 13:33:28 +000098 }
99 }
100 }
101}
102
Chris Lattneradcbce02006-07-07 17:12:36 +0000103bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
104 std::string *ErrMsg) {
Reid Spencer19cd4a92004-11-29 13:33:28 +0000105 check_ltdl_initialization();
Chris Lattneradcbce02006-07-07 17:12:36 +0000106 lt_dlhandle a_handle = lt_dlopen(Filename);
Reid Spencer19cd4a92004-11-29 13:33:28 +0000107
108 if (a_handle == 0)
Chris Lattneradcbce02006-07-07 17:12:36 +0000109 a_handle = lt_dlopenext(Filename);
Reid Spencer19cd4a92004-11-29 13:33:28 +0000110
Chris Lattneradcbce02006-07-07 17:12:36 +0000111 if (a_handle == 0) {
112 if (ErrMsg)
113 *ErrMsg = std::string("Can't open :") +
114 (Filename ? Filename : "<current process>") + ": " + lt_dlerror();
115 return true;
116 }
Reid Spencer19cd4a92004-11-29 13:33:28 +0000117
118 lt_dlmakeresident(a_handle);
119
120 OpenedHandles.push_back(a_handle);
Chris Lattneradcbce02006-07-07 17:12:36 +0000121 return false;
Reid Spencer19cd4a92004-11-29 13:33:28 +0000122}
123
124void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
125 check_ltdl_initialization();
Jeff Cohen85046902006-01-30 04:33:51 +0000126
127 // First check symbols added via AddSymbol().
128 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
129 if (I != g_symbols.end())
130 return I->second;
131
132 // Now search the libraries.
Reid Spencer19cd4a92004-11-29 13:33:28 +0000133 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
134 E = OpenedHandles.end(); I != E; ++I) {
135 lt_ptr ptr = lt_dlsym(*I, symbolName);
136 if (ptr)
137 return ptr;
138 }
Chris Lattner28dabf72004-12-03 23:02:42 +0000139
140 // If this is darwin, it has some funky issues, try to solve them here. Some
141 // important symbols are marked 'private external' which doesn't allow
142 // SearchForAddressOfSymbol to find them. As such, we special case them here,
143 // there is only a small handful of them.
Reid Spencer11f457a2007-01-10 19:50:43 +0000144
Chris Lattner28dabf72004-12-03 23:02:42 +0000145#ifdef __APPLE__
Chris Lattner368cb8e2004-12-04 04:17:20 +0000146#define EXPLICIT_SYMBOL(SYM) \
147 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
Reid Spencer11f457a2007-01-10 19:50:43 +0000148 {
Chris Lattner368cb8e2004-12-04 04:17:20 +0000149 EXPLICIT_SYMBOL(__ashldi3);
150 EXPLICIT_SYMBOL(__ashrdi3);
151 EXPLICIT_SYMBOL(__cmpdi2);
152 EXPLICIT_SYMBOL(__divdi3);
153 EXPLICIT_SYMBOL(__eprintf);
154 EXPLICIT_SYMBOL(__fixdfdi);
155 EXPLICIT_SYMBOL(__fixsfdi);
156 EXPLICIT_SYMBOL(__fixunsdfdi);
157 EXPLICIT_SYMBOL(__fixunssfdi);
158 EXPLICIT_SYMBOL(__floatdidf);
159 EXPLICIT_SYMBOL(__floatdisf);
160 EXPLICIT_SYMBOL(__lshrdi3);
161 EXPLICIT_SYMBOL(__moddi3);
162 EXPLICIT_SYMBOL(__udivdi3);
163 EXPLICIT_SYMBOL(__umoddi3);
Chris Lattner28dabf72004-12-03 23:02:42 +0000164 }
Reid Spencer11f457a2007-01-10 19:50:43 +0000165#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000166#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000167#define EXPLICIT_SYMBOL(SYM) \
168 if (!strcmp(symbolName, #SYM)) return &SYM
169 // Try a few well known symbols just to give lli a shot at working.
Reid Spencer65de7422007-01-11 00:35:10 +0000170 // Note that on some systems stdin, etc. are macros so we have to
171 // avoid attempting to take the address of a macro :)
Reid Spencer11f457a2007-01-10 19:50:43 +0000172 {
Reid Spencer65de7422007-01-11 00:35:10 +0000173#ifndef stdin
Reid Spencer11f457a2007-01-10 19:50:43 +0000174 EXPLICIT_SYMBOL(stdin);
Reid Spencer81e39542007-01-19 21:30:39 +0000175#endif
176#ifndef stdout
Reid Spencer11f457a2007-01-10 19:50:43 +0000177 EXPLICIT_SYMBOL(stdout);
Reid Spencer81e39542007-01-19 21:30:39 +0000178#endif
179#ifndef stderr
Reid Spencer11f457a2007-01-10 19:50:43 +0000180 EXPLICIT_SYMBOL(stderr);
Reid Spencer65de7422007-01-11 00:35:10 +0000181#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000182 }
183#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000184
Reid Spencer19cd4a92004-11-29 13:33:28 +0000185 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000186}
187
188void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
189 assert(handle != 0 && "Invalid DynamicLibrary handle");
Reid Spencer19cd4a92004-11-29 13:33:28 +0000190 return lt_dlsym((lt_dlhandle) handle, symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +0000191}
192
Jeff Cohena4c97512004-12-24 16:26:47 +0000193#endif // LLVM_ON_WIN32
Reid Spencer23dd3322006-07-26 16:55:39 +0000194
195DEFINING_FILE_FOR(SystemDynamicLibrary)