1 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This header file implements the operating system DynamicLibrary concept.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/System/DynamicLibrary.h"
18 //===----------------------------------------------------------------------===//
19 //=== WARNING: Implementation here must contain only TRULY operating system
20 //=== independent code.
21 //===----------------------------------------------------------------------===//
23 static bool did_initialize_ltdl = false;
25 static inline void check_ltdl_initialization() {
26 if (!did_initialize_ltdl) {
28 throw std::string(lt_dlerror());
29 did_initialize_ltdl = true;
33 static std::vector<lt_dlhandle> OpenedHandles;
39 DynamicLibrary::DynamicLibrary() : handle(0) {
40 check_ltdl_initialization();
42 lt_dlhandle a_handle = lt_dlopen(0);
45 throw std::string("Can't open program as dynamic library");
48 OpenedHandles.push_back(a_handle);
51 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
52 check_ltdl_initialization();
54 lt_dlhandle a_handle = lt_dlopen(filename);
57 a_handle = lt_dlopenext(filename);
60 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
63 OpenedHandles.push_back(a_handle);
66 DynamicLibrary::~DynamicLibrary() {
67 lt_dlhandle a_handle = (lt_dlhandle) handle;
71 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
72 E = OpenedHandles.end(); I != E; ++I) {
74 // Note: don't use the swap/pop_back trick here. Order is important.
75 OpenedHandles.erase(I);
81 void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
82 check_ltdl_initialization();
83 lt_dlhandle a_handle = lt_dlopen(filename);
86 a_handle = lt_dlopenext(filename);
89 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
91 lt_dlmakeresident(a_handle);
93 OpenedHandles.push_back(a_handle);
96 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
97 check_ltdl_initialization();
98 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
99 E = OpenedHandles.end(); I != E; ++I) {
100 lt_ptr ptr = lt_dlsym(*I, symbolName);
107 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
108 assert(handle != 0 && "Invalid DynamicLibrary handle");
109 return lt_dlsym((lt_dlhandle) handle, symbolName);