blob: 0c3caac0f10bc2e42433de6c36469408c0a9a34d [file] [log] [blame]
Reid Spencer0de02a62004-11-18 04:33:39 +00001//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
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 Cohen1a466352004-12-24 07:57:09 +000016
17// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
18// license and special exception would cause all of LLVM to be placed under
19// the LGPL. This is because the exception applies only when libtool is
20// used, and obviously libtool is not used with Visual Studio. An entirely
21// separate implementation is provided in win32/DynamicLibrary.cpp.
22
Jeff Cohena4c97512004-12-24 16:26:47 +000023#ifdef LLVM_ON_WIN32
Jeff Cohen1a466352004-12-24 07:57:09 +000024
25#include "win32/DynamicLibrary.cpp"
26
27#else
28
Reid Spencer29ae1772004-11-29 12:39:10 +000029#include "ltdl.h"
Reid Spencer0de02a62004-11-18 04:33:39 +000030#include <cassert>
Chris Lattner28dabf72004-12-03 23:02:42 +000031using namespace llvm;
32using namespace llvm::sys;
Reid Spencer0de02a62004-11-18 04:33:39 +000033
34//===----------------------------------------------------------------------===//
35//=== WARNING: Implementation here must contain only TRULY operating system
36//=== independent code.
37//===----------------------------------------------------------------------===//
38
Reid Spencer19cd4a92004-11-29 13:33:28 +000039static bool did_initialize_ltdl = false;
40
41static inline void check_ltdl_initialization() {
42 if (!did_initialize_ltdl) {
43 if (0 != lt_dlinit())
44 throw std::string(lt_dlerror());
45 did_initialize_ltdl = true;
46 }
47}
48
49static std::vector<lt_dlhandle> OpenedHandles;
50
Reid Spencer441cc2a2004-11-29 10:39:46 +000051DynamicLibrary::DynamicLibrary() : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000052 check_ltdl_initialization();
Reid Spencer441cc2a2004-11-29 10:39:46 +000053
Reid Spencer19cd4a92004-11-29 13:33:28 +000054 lt_dlhandle a_handle = lt_dlopen(0);
Reid Spencer441cc2a2004-11-29 10:39:46 +000055
Reid Spencer19cd4a92004-11-29 13:33:28 +000056 if (a_handle == 0)
Reid Spencer441cc2a2004-11-29 10:39:46 +000057 throw std::string("Can't open program as dynamic library");
Reid Spencer19cd4a92004-11-29 13:33:28 +000058
59 handle = a_handle;
60 OpenedHandles.push_back(a_handle);
Reid Spencer441cc2a2004-11-29 10:39:46 +000061}
62
Reid Spencer0de02a62004-11-18 04:33:39 +000063DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000064 check_ltdl_initialization();
Reid Spencer0de02a62004-11-18 04:33:39 +000065
Reid Spencer19cd4a92004-11-29 13:33:28 +000066 lt_dlhandle a_handle = lt_dlopen(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000067
Reid Spencer19cd4a92004-11-29 13:33:28 +000068 if (a_handle == 0)
69 a_handle = lt_dlopenext(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000070
Reid Spencer19cd4a92004-11-29 13:33:28 +000071 if (a_handle == 0)
72 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
73
74 handle = a_handle;
75 OpenedHandles.push_back(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000076}
77
78DynamicLibrary::~DynamicLibrary() {
Reid Spencer19cd4a92004-11-29 13:33:28 +000079 lt_dlhandle a_handle = (lt_dlhandle) handle;
80 if (a_handle) {
81 lt_dlclose(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000082
Reid Spencer19cd4a92004-11-29 13:33:28 +000083 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
84 E = OpenedHandles.end(); I != E; ++I) {
85 if (*I == a_handle) {
86 // Note: don't use the swap/pop_back trick here. Order is important.
87 OpenedHandles.erase(I);
88 }
89 }
90 }
91}
92
93void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
94 check_ltdl_initialization();
95 lt_dlhandle a_handle = lt_dlopen(filename);
96
97 if (a_handle == 0)
98 a_handle = lt_dlopenext(filename);
99
100 if (a_handle == 0)
101 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
102
103 lt_dlmakeresident(a_handle);
104
105 OpenedHandles.push_back(a_handle);
106}
107
108void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
109 check_ltdl_initialization();
110 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
111 E = OpenedHandles.end(); I != E; ++I) {
112 lt_ptr ptr = lt_dlsym(*I, symbolName);
113 if (ptr)
114 return ptr;
115 }
Chris Lattner28dabf72004-12-03 23:02:42 +0000116
117 // If this is darwin, it has some funky issues, try to solve them here. Some
118 // important symbols are marked 'private external' which doesn't allow
119 // SearchForAddressOfSymbol to find them. As such, we special case them here,
120 // there is only a small handful of them.
121#ifdef __APPLE__
122 {
Chris Lattner368cb8e2004-12-04 04:17:20 +0000123#define EXPLICIT_SYMBOL(SYM) \
124 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
125 EXPLICIT_SYMBOL(__ashldi3);
126 EXPLICIT_SYMBOL(__ashrdi3);
127 EXPLICIT_SYMBOL(__cmpdi2);
128 EXPLICIT_SYMBOL(__divdi3);
129 EXPLICIT_SYMBOL(__eprintf);
130 EXPLICIT_SYMBOL(__fixdfdi);
131 EXPLICIT_SYMBOL(__fixsfdi);
132 EXPLICIT_SYMBOL(__fixunsdfdi);
133 EXPLICIT_SYMBOL(__fixunssfdi);
134 EXPLICIT_SYMBOL(__floatdidf);
135 EXPLICIT_SYMBOL(__floatdisf);
136 EXPLICIT_SYMBOL(__lshrdi3);
137 EXPLICIT_SYMBOL(__moddi3);
138 EXPLICIT_SYMBOL(__udivdi3);
139 EXPLICIT_SYMBOL(__umoddi3);
140#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000141 }
142#endif
143
Reid Spencer19cd4a92004-11-29 13:33:28 +0000144 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000145}
146
147void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
148 assert(handle != 0 && "Invalid DynamicLibrary handle");
Reid Spencer19cd4a92004-11-29 13:33:28 +0000149 return lt_dlsym((lt_dlhandle) handle, symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +0000150}
151
Jeff Cohena4c97512004-12-24 16:26:47 +0000152#endif // LLVM_ON_WIN32