blob: eebd2b1f169b949c7832071fcd70a06b38d65d0b [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"
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();
Chris Lattneracf81452007-02-01 04:57:00 +000052 Err = Err; // Silence warning.
Chris Lattner9b0d6f42006-08-30 20:37:06 +000053 assert(0 == Err && "Can't init the ltdl library");
Reid Spencer19cd4a92004-11-29 13:33:28 +000054 did_initialize_ltdl = true;
55 }
56}
57
58static std::vector<lt_dlhandle> OpenedHandles;
59
Reid Spencer441cc2a2004-11-29 10:39:46 +000060DynamicLibrary::DynamicLibrary() : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000061 check_ltdl_initialization();
Reid Spencer441cc2a2004-11-29 10:39:46 +000062
Reid Spencer19cd4a92004-11-29 13:33:28 +000063 lt_dlhandle a_handle = lt_dlopen(0);
Reid Spencer441cc2a2004-11-29 10:39:46 +000064
Chris Lattner67454582007-09-28 20:50:50 +000065 assert(a_handle && "Can't open program as dynamic library");
Misha Brukmanf976c852005-04-21 22:55:34 +000066
Reid Spencer19cd4a92004-11-29 13:33:28 +000067 handle = a_handle;
68 OpenedHandles.push_back(a_handle);
Reid Spencer441cc2a2004-11-29 10:39:46 +000069}
70
Reid Spencer99655e12006-08-25 19:54:53 +000071/*
Reid Spencer0de02a62004-11-18 04:33:39 +000072DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000073 check_ltdl_initialization();
Reid Spencer0de02a62004-11-18 04:33:39 +000074
Reid Spencer19cd4a92004-11-29 13:33:28 +000075 lt_dlhandle a_handle = lt_dlopen(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000076
Reid Spencer19cd4a92004-11-29 13:33:28 +000077 if (a_handle == 0)
78 a_handle = lt_dlopenext(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000079
Reid Spencer19cd4a92004-11-29 13:33:28 +000080 if (a_handle == 0)
81 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
82
83 handle = a_handle;
84 OpenedHandles.push_back(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000085}
Reid Spencer99655e12006-08-25 19:54:53 +000086*/
Reid Spencer0de02a62004-11-18 04:33:39 +000087
88DynamicLibrary::~DynamicLibrary() {
Reid Spencer19cd4a92004-11-29 13:33:28 +000089 lt_dlhandle a_handle = (lt_dlhandle) handle;
90 if (a_handle) {
91 lt_dlclose(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000092
Reid Spencer19cd4a92004-11-29 13:33:28 +000093 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
94 E = OpenedHandles.end(); I != E; ++I) {
95 if (*I == a_handle) {
96 // Note: don't use the swap/pop_back trick here. Order is important.
97 OpenedHandles.erase(I);
Chris Lattner2b80e8d2006-05-12 18:13:11 +000098 return;
Reid Spencer19cd4a92004-11-29 13:33:28 +000099 }
100 }
101 }
102}
103
Chris Lattneradcbce02006-07-07 17:12:36 +0000104bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
105 std::string *ErrMsg) {
Reid Spencer19cd4a92004-11-29 13:33:28 +0000106 check_ltdl_initialization();
Chris Lattneradcbce02006-07-07 17:12:36 +0000107 lt_dlhandle a_handle = lt_dlopen(Filename);
Reid Spencer19cd4a92004-11-29 13:33:28 +0000108
109 if (a_handle == 0)
Chris Lattneradcbce02006-07-07 17:12:36 +0000110 a_handle = lt_dlopenext(Filename);
Reid Spencer19cd4a92004-11-29 13:33:28 +0000111
Chris Lattneradcbce02006-07-07 17:12:36 +0000112 if (a_handle == 0) {
113 if (ErrMsg)
114 *ErrMsg = std::string("Can't open :") +
115 (Filename ? Filename : "<current process>") + ": " + lt_dlerror();
116 return true;
117 }
Reid Spencer19cd4a92004-11-29 13:33:28 +0000118
119 lt_dlmakeresident(a_handle);
120
121 OpenedHandles.push_back(a_handle);
Chris Lattneradcbce02006-07-07 17:12:36 +0000122 return false;
Reid Spencer19cd4a92004-11-29 13:33:28 +0000123}
124
125void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
126 check_ltdl_initialization();
Jeff Cohen85046902006-01-30 04:33:51 +0000127
128 // First check symbols added via AddSymbol().
129 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
130 if (I != g_symbols.end())
131 return I->second;
132
133 // Now search the libraries.
Reid Spencer19cd4a92004-11-29 13:33:28 +0000134 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
135 E = OpenedHandles.end(); I != E; ++I) {
136 lt_ptr ptr = lt_dlsym(*I, symbolName);
137 if (ptr)
138 return ptr;
139 }
Chris Lattner28dabf72004-12-03 23:02:42 +0000140
Anton Korobeynikov96ea2092007-12-03 05:30:41 +0000141#define EXPLICIT_SYMBOL(SYM) \
142 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
143
Chris Lattner28dabf72004-12-03 23:02:42 +0000144 // If this is darwin, it has some funky issues, try to solve them here. Some
145 // important symbols are marked 'private external' which doesn't allow
146 // SearchForAddressOfSymbol to find them. As such, we special case them here,
147 // there is only a small handful of them.
Reid Spencer11f457a2007-01-10 19:50:43 +0000148
Chris Lattner28dabf72004-12-03 23:02:42 +0000149#ifdef __APPLE__
Reid Spencer11f457a2007-01-10 19:50:43 +0000150 {
Chris Lattner368cb8e2004-12-04 04:17:20 +0000151 EXPLICIT_SYMBOL(__ashldi3);
152 EXPLICIT_SYMBOL(__ashrdi3);
153 EXPLICIT_SYMBOL(__cmpdi2);
154 EXPLICIT_SYMBOL(__divdi3);
155 EXPLICIT_SYMBOL(__eprintf);
156 EXPLICIT_SYMBOL(__fixdfdi);
157 EXPLICIT_SYMBOL(__fixsfdi);
158 EXPLICIT_SYMBOL(__fixunsdfdi);
159 EXPLICIT_SYMBOL(__fixunssfdi);
160 EXPLICIT_SYMBOL(__floatdidf);
161 EXPLICIT_SYMBOL(__floatdisf);
162 EXPLICIT_SYMBOL(__lshrdi3);
163 EXPLICIT_SYMBOL(__moddi3);
164 EXPLICIT_SYMBOL(__udivdi3);
165 EXPLICIT_SYMBOL(__umoddi3);
Chris Lattner28dabf72004-12-03 23:02:42 +0000166 }
167#endif
Reid Spencer02f20d32007-01-19 21:41:04 +0000168
Anton Korobeynikov96ea2092007-12-03 05:30:41 +0000169#ifdef __CYGWIN__
170 {
171 EXPLICIT_SYMBOL(_alloca);
172 }
173#endif
174
175#undef EXPLICIT_SYMBOL
176
Reid Spencer02f20d32007-01-19 21:41:04 +0000177// This macro returns the address of a well-known, explicit symbol
Reid Spencer11f457a2007-01-10 19:50:43 +0000178#define EXPLICIT_SYMBOL(SYM) \
179 if (!strcmp(symbolName, #SYM)) return &SYM
Reid Spencer02f20d32007-01-19 21:41:04 +0000180
181// On linux we have a weird situation. The stderr/out/in symbols are both
182// macros and global variables because of standards requirements. So, we
183// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
184#if defined(__linux__)
185 {
186 EXPLICIT_SYMBOL(stderr);
187 EXPLICIT_SYMBOL(stdout);
188 EXPLICIT_SYMBOL(stdin);
189 }
190#else
191 // For everything else, we want to check to make sure the symbol isn't defined
192 // as a macro before using EXPLICIT_SYMBOL.
Reid Spencer11f457a2007-01-10 19:50:43 +0000193 {
Reid Spencer65de7422007-01-11 00:35:10 +0000194#ifndef stdin
Reid Spencer11f457a2007-01-10 19:50:43 +0000195 EXPLICIT_SYMBOL(stdin);
Reid Spencer81e39542007-01-19 21:30:39 +0000196#endif
197#ifndef stdout
Reid Spencer11f457a2007-01-10 19:50:43 +0000198 EXPLICIT_SYMBOL(stdout);
Reid Spencer81e39542007-01-19 21:30:39 +0000199#endif
200#ifndef stderr
Reid Spencer11f457a2007-01-10 19:50:43 +0000201 EXPLICIT_SYMBOL(stderr);
Reid Spencer65de7422007-01-11 00:35:10 +0000202#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000203 }
Reid Spencer02f20d32007-01-19 21:41:04 +0000204#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000205#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000206
Reid Spencer19cd4a92004-11-29 13:33:28 +0000207 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000208}
209
210void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
211 assert(handle != 0 && "Invalid DynamicLibrary handle");
Reid Spencer19cd4a92004-11-29 13:33:28 +0000212 return lt_dlsym((lt_dlhandle) handle, symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +0000213}
214
Jeff Cohena4c97512004-12-24 16:26:47 +0000215#endif // LLVM_ON_WIN32
Reid Spencer23dd3322006-07-26 16:55:39 +0000216
217DEFINING_FILE_FOR(SystemDynamicLibrary)