blob: a21f16ab327e71e45f68a4eb1cf35b8f882476c9 [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//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/System/DynamicLibrary.h"
Jeff Cohena4c97512004-12-24 16:26:47 +000015#include "llvm/Config/config.h"
Duncan Sandsf52e32a2008-01-09 19:42:09 +000016#include <cstring>
Jeff Cohen85046902006-01-30 04:33:51 +000017#include <map>
18
19// Collection of symbol name/value pairs to be searched prior to any libraries.
20static std::map<std::string, void *> g_symbols;
21
Chris Lattneradcbce02006-07-07 17:12:36 +000022void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
23 void *symbolValue) {
Jeff Cohen85046902006-01-30 04:33:51 +000024 g_symbols[symbolName] = symbolValue;
25}
Jeff Cohen1a466352004-12-24 07:57:09 +000026
27// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
28// license and special exception would cause all of LLVM to be placed under
29// the LGPL. This is because the exception applies only when libtool is
30// used, and obviously libtool is not used with Visual Studio. An entirely
31// separate implementation is provided in win32/DynamicLibrary.cpp.
32
Jeff Cohena4c97512004-12-24 16:26:47 +000033#ifdef LLVM_ON_WIN32
Jeff Cohen1a466352004-12-24 07:57:09 +000034
Reid Spencerbccc8ab2005-01-09 23:29:00 +000035#include "Win32/DynamicLibrary.inc"
Jeff Cohen1a466352004-12-24 07:57:09 +000036
37#else
38
Reid Spencer29ae1772004-11-29 12:39:10 +000039#include "ltdl.h"
Reid Spencer0de02a62004-11-18 04:33:39 +000040#include <cassert>
Chris Lattner28dabf72004-12-03 23:02:42 +000041using namespace llvm;
42using namespace llvm::sys;
Reid Spencer0de02a62004-11-18 04:33:39 +000043
44//===----------------------------------------------------------------------===//
45//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000046//=== independent code.
Reid Spencer0de02a62004-11-18 04:33:39 +000047//===----------------------------------------------------------------------===//
48
Reid Spencer19cd4a92004-11-29 13:33:28 +000049static inline void check_ltdl_initialization() {
Reid Spencer99655e12006-08-25 19:54:53 +000050 static bool did_initialize_ltdl = false;
Reid Spencer19cd4a92004-11-29 13:33:28 +000051 if (!did_initialize_ltdl) {
Chris Lattner9b0d6f42006-08-30 20:37:06 +000052 int Err = lt_dlinit();
Chris Lattneracf81452007-02-01 04:57:00 +000053 Err = Err; // Silence warning.
Chris Lattner9b0d6f42006-08-30 20:37:06 +000054 assert(0 == Err && "Can't init the ltdl library");
Reid Spencer19cd4a92004-11-29 13:33:28 +000055 did_initialize_ltdl = true;
56 }
57}
58
59static std::vector<lt_dlhandle> OpenedHandles;
60
Reid Spencer441cc2a2004-11-29 10:39:46 +000061DynamicLibrary::DynamicLibrary() : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000062 check_ltdl_initialization();
Reid Spencer441cc2a2004-11-29 10:39:46 +000063
Reid Spencer19cd4a92004-11-29 13:33:28 +000064 lt_dlhandle a_handle = lt_dlopen(0);
Reid Spencer441cc2a2004-11-29 10:39:46 +000065
Chris Lattner67454582007-09-28 20:50:50 +000066 assert(a_handle && "Can't open program as dynamic library");
Misha Brukmanf976c852005-04-21 22:55:34 +000067
Reid Spencer19cd4a92004-11-29 13:33:28 +000068 handle = a_handle;
69 OpenedHandles.push_back(a_handle);
Reid Spencer441cc2a2004-11-29 10:39:46 +000070}
71
Reid Spencer99655e12006-08-25 19:54:53 +000072/*
Reid Spencer0de02a62004-11-18 04:33:39 +000073DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000074 check_ltdl_initialization();
Reid Spencer0de02a62004-11-18 04:33:39 +000075
Reid Spencer19cd4a92004-11-29 13:33:28 +000076 lt_dlhandle a_handle = lt_dlopen(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000077
Reid Spencer19cd4a92004-11-29 13:33:28 +000078 if (a_handle == 0)
79 a_handle = lt_dlopenext(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000080
Reid Spencer19cd4a92004-11-29 13:33:28 +000081 if (a_handle == 0)
82 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
83
84 handle = a_handle;
85 OpenedHandles.push_back(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000086}
Reid Spencer99655e12006-08-25 19:54:53 +000087*/
Reid Spencer0de02a62004-11-18 04:33:39 +000088
89DynamicLibrary::~DynamicLibrary() {
Reid Spencer19cd4a92004-11-29 13:33:28 +000090 lt_dlhandle a_handle = (lt_dlhandle) handle;
91 if (a_handle) {
92 lt_dlclose(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000093
Reid Spencer19cd4a92004-11-29 13:33:28 +000094 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
95 E = OpenedHandles.end(); I != E; ++I) {
96 if (*I == a_handle) {
97 // Note: don't use the swap/pop_back trick here. Order is important.
98 OpenedHandles.erase(I);
Chris Lattner2b80e8d2006-05-12 18:13:11 +000099 return;
Reid Spencer19cd4a92004-11-29 13:33:28 +0000100 }
101 }
102 }
103}
104
Chris Lattneradcbce02006-07-07 17:12:36 +0000105bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
106 std::string *ErrMsg) {
Reid Spencer19cd4a92004-11-29 13:33:28 +0000107 check_ltdl_initialization();
Chris Lattneradcbce02006-07-07 17:12:36 +0000108 lt_dlhandle a_handle = lt_dlopen(Filename);
Reid Spencer19cd4a92004-11-29 13:33:28 +0000109
110 if (a_handle == 0)
Chris Lattneradcbce02006-07-07 17:12:36 +0000111 a_handle = lt_dlopenext(Filename);
Reid Spencer19cd4a92004-11-29 13:33:28 +0000112
Chris Lattneradcbce02006-07-07 17:12:36 +0000113 if (a_handle == 0) {
114 if (ErrMsg)
115 *ErrMsg = std::string("Can't open :") +
116 (Filename ? Filename : "<current process>") + ": " + lt_dlerror();
117 return true;
118 }
Reid Spencer19cd4a92004-11-29 13:33:28 +0000119
120 lt_dlmakeresident(a_handle);
121
122 OpenedHandles.push_back(a_handle);
Chris Lattneradcbce02006-07-07 17:12:36 +0000123 return false;
Reid Spencer19cd4a92004-11-29 13:33:28 +0000124}
125
126void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
127 check_ltdl_initialization();
Jeff Cohen85046902006-01-30 04:33:51 +0000128
129 // First check symbols added via AddSymbol().
130 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
131 if (I != g_symbols.end())
132 return I->second;
133
134 // Now search the libraries.
Reid Spencer19cd4a92004-11-29 13:33:28 +0000135 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
136 E = OpenedHandles.end(); I != E; ++I) {
137 lt_ptr ptr = lt_dlsym(*I, symbolName);
138 if (ptr)
139 return ptr;
140 }
Chris Lattner28dabf72004-12-03 23:02:42 +0000141
Anton Korobeynikov96ea2092007-12-03 05:30:41 +0000142#define EXPLICIT_SYMBOL(SYM) \
143 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
144
Chris Lattner28dabf72004-12-03 23:02:42 +0000145 // If this is darwin, it has some funky issues, try to solve them here. Some
146 // important symbols are marked 'private external' which doesn't allow
147 // SearchForAddressOfSymbol to find them. As such, we special case them here,
148 // there is only a small handful of them.
Reid Spencer11f457a2007-01-10 19:50:43 +0000149
Chris Lattner28dabf72004-12-03 23:02:42 +0000150#ifdef __APPLE__
Reid Spencer11f457a2007-01-10 19:50:43 +0000151 {
Chris Lattner368cb8e2004-12-04 04:17:20 +0000152 EXPLICIT_SYMBOL(__ashldi3);
153 EXPLICIT_SYMBOL(__ashrdi3);
154 EXPLICIT_SYMBOL(__cmpdi2);
155 EXPLICIT_SYMBOL(__divdi3);
156 EXPLICIT_SYMBOL(__eprintf);
157 EXPLICIT_SYMBOL(__fixdfdi);
158 EXPLICIT_SYMBOL(__fixsfdi);
159 EXPLICIT_SYMBOL(__fixunsdfdi);
160 EXPLICIT_SYMBOL(__fixunssfdi);
161 EXPLICIT_SYMBOL(__floatdidf);
162 EXPLICIT_SYMBOL(__floatdisf);
163 EXPLICIT_SYMBOL(__lshrdi3);
164 EXPLICIT_SYMBOL(__moddi3);
165 EXPLICIT_SYMBOL(__udivdi3);
166 EXPLICIT_SYMBOL(__umoddi3);
Chris Lattner28dabf72004-12-03 23:02:42 +0000167 }
168#endif
Reid Spencer02f20d32007-01-19 21:41:04 +0000169
Anton Korobeynikov96ea2092007-12-03 05:30:41 +0000170#ifdef __CYGWIN__
171 {
172 EXPLICIT_SYMBOL(_alloca);
173 }
174#endif
175
176#undef EXPLICIT_SYMBOL
177
Reid Spencer02f20d32007-01-19 21:41:04 +0000178// This macro returns the address of a well-known, explicit symbol
Reid Spencer11f457a2007-01-10 19:50:43 +0000179#define EXPLICIT_SYMBOL(SYM) \
180 if (!strcmp(symbolName, #SYM)) return &SYM
Reid Spencer02f20d32007-01-19 21:41:04 +0000181
182// On linux we have a weird situation. The stderr/out/in symbols are both
183// macros and global variables because of standards requirements. So, we
184// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
185#if defined(__linux__)
186 {
187 EXPLICIT_SYMBOL(stderr);
188 EXPLICIT_SYMBOL(stdout);
189 EXPLICIT_SYMBOL(stdin);
190 }
191#else
192 // For everything else, we want to check to make sure the symbol isn't defined
193 // as a macro before using EXPLICIT_SYMBOL.
Reid Spencer11f457a2007-01-10 19:50:43 +0000194 {
Reid Spencer65de7422007-01-11 00:35:10 +0000195#ifndef stdin
Reid Spencer11f457a2007-01-10 19:50:43 +0000196 EXPLICIT_SYMBOL(stdin);
Reid Spencer81e39542007-01-19 21:30:39 +0000197#endif
198#ifndef stdout
Reid Spencer11f457a2007-01-10 19:50:43 +0000199 EXPLICIT_SYMBOL(stdout);
Reid Spencer81e39542007-01-19 21:30:39 +0000200#endif
201#ifndef stderr
Reid Spencer11f457a2007-01-10 19:50:43 +0000202 EXPLICIT_SYMBOL(stderr);
Reid Spencer65de7422007-01-11 00:35:10 +0000203#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000204 }
Reid Spencer02f20d32007-01-19 21:41:04 +0000205#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000206#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000207
Reid Spencer19cd4a92004-11-29 13:33:28 +0000208 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000209}
210
211void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
212 assert(handle != 0 && "Invalid DynamicLibrary handle");
Reid Spencer19cd4a92004-11-29 13:33:28 +0000213 return lt_dlsym((lt_dlhandle) handle, symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +0000214}
215
Jeff Cohena4c97512004-12-24 16:26:47 +0000216#endif // LLVM_ON_WIN32
Reid Spencer23dd3322006-07-26 16:55:39 +0000217
218DEFINING_FILE_FOR(SystemDynamicLibrary)