blob: 157f1735f137878f9067fbb53ebe1b2a266fdbcd [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"
Reid Spencer29ae1772004-11-29 12:39:10 +000015#include "ltdl.h"
Reid Spencer0de02a62004-11-18 04:33:39 +000016#include <cassert>
Chris Lattner28dabf72004-12-03 23:02:42 +000017using namespace llvm;
18using namespace llvm::sys;
Reid Spencer0de02a62004-11-18 04:33:39 +000019
20//===----------------------------------------------------------------------===//
21//=== WARNING: Implementation here must contain only TRULY operating system
22//=== independent code.
23//===----------------------------------------------------------------------===//
24
Reid Spencer19cd4a92004-11-29 13:33:28 +000025static bool did_initialize_ltdl = false;
26
27static inline void check_ltdl_initialization() {
28 if (!did_initialize_ltdl) {
29 if (0 != lt_dlinit())
30 throw std::string(lt_dlerror());
31 did_initialize_ltdl = true;
32 }
33}
34
35static std::vector<lt_dlhandle> OpenedHandles;
36
Reid Spencer441cc2a2004-11-29 10:39:46 +000037DynamicLibrary::DynamicLibrary() : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000038 check_ltdl_initialization();
Reid Spencer441cc2a2004-11-29 10:39:46 +000039
Reid Spencer19cd4a92004-11-29 13:33:28 +000040 lt_dlhandle a_handle = lt_dlopen(0);
Reid Spencer441cc2a2004-11-29 10:39:46 +000041
Reid Spencer19cd4a92004-11-29 13:33:28 +000042 if (a_handle == 0)
Reid Spencer441cc2a2004-11-29 10:39:46 +000043 throw std::string("Can't open program as dynamic library");
Reid Spencer19cd4a92004-11-29 13:33:28 +000044
45 handle = a_handle;
46 OpenedHandles.push_back(a_handle);
Reid Spencer441cc2a2004-11-29 10:39:46 +000047}
48
Reid Spencer0de02a62004-11-18 04:33:39 +000049DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000050 check_ltdl_initialization();
Reid Spencer0de02a62004-11-18 04:33:39 +000051
Reid Spencer19cd4a92004-11-29 13:33:28 +000052 lt_dlhandle a_handle = lt_dlopen(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000053
Reid Spencer19cd4a92004-11-29 13:33:28 +000054 if (a_handle == 0)
55 a_handle = lt_dlopenext(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000056
Reid Spencer19cd4a92004-11-29 13:33:28 +000057 if (a_handle == 0)
58 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
59
60 handle = a_handle;
61 OpenedHandles.push_back(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000062}
63
64DynamicLibrary::~DynamicLibrary() {
Reid Spencer19cd4a92004-11-29 13:33:28 +000065 lt_dlhandle a_handle = (lt_dlhandle) handle;
66 if (a_handle) {
67 lt_dlclose(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000068
Reid Spencer19cd4a92004-11-29 13:33:28 +000069 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
70 E = OpenedHandles.end(); I != E; ++I) {
71 if (*I == a_handle) {
72 // Note: don't use the swap/pop_back trick here. Order is important.
73 OpenedHandles.erase(I);
74 }
75 }
76 }
77}
78
79void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
80 check_ltdl_initialization();
81 lt_dlhandle a_handle = lt_dlopen(filename);
82
83 if (a_handle == 0)
84 a_handle = lt_dlopenext(filename);
85
86 if (a_handle == 0)
87 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
88
89 lt_dlmakeresident(a_handle);
90
91 OpenedHandles.push_back(a_handle);
92}
93
94void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
95 check_ltdl_initialization();
96 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
97 E = OpenedHandles.end(); I != E; ++I) {
98 lt_ptr ptr = lt_dlsym(*I, symbolName);
99 if (ptr)
100 return ptr;
101 }
Chris Lattner28dabf72004-12-03 23:02:42 +0000102
103 // If this is darwin, it has some funky issues, try to solve them here. Some
104 // important symbols are marked 'private external' which doesn't allow
105 // SearchForAddressOfSymbol to find them. As such, we special case them here,
106 // there is only a small handful of them.
107#ifdef __APPLE__
108 {
109 extern void *__ashldi3; if (Name == "__ashldi3") return &__ashldi3;
110 extern void *__ashrdi3; if (Name == "__ashrdi3") return &__ashrdi3;
111 extern void *__cmpdi2; if (Name == "__cmpdi2") return &__cmpdi2;
112 extern void *__divdi3; if (Name == "__divdi3") return &__divdi3;
113 extern void *__eprintf; if (Name == "__eprintf") return &__eprintf;
114 extern void *__fixdfdi; if (Name == "__fixdfdi") return &__fixdfdi;
115 extern void *__fixsfdi; if (Name == "__fixsfdi") return &__fixsfdi;
116 extern void *__fixunsdfdi; if (Name == "__fixunsdfdi") return &__fixunsdfdi;
117 extern void *__fixunssfdi; if (Name == "__fixunssfdi") return &__fixunssfdi;
118 extern void *__floatdidf; if (Name == "__floatdidf") return &__floatdidf;
119 extern void *__floatdisf; if (Name == "__floatdisf") return &__floatdisf;
120 extern void *__lshrdi3; if (Name == "__lshrdi3") return &__lshrdi3;
121 extern void *__moddi3; if (Name == "__moddi3") return &__moddi3;
122 extern void *__udivdi3; if (Name == "__udivdi3") return &__udivdi3;
123 extern void *__umoddi3; if (Name == "__umoddi3") return &__umoddi3;
124 }
125#endif
126
Reid Spencer19cd4a92004-11-29 13:33:28 +0000127 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000128}
129
130void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
131 assert(handle != 0 && "Invalid DynamicLibrary handle");
Reid Spencer19cd4a92004-11-29 13:33:28 +0000132 return lt_dlsym((lt_dlhandle) handle, symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +0000133}
134