We just use ltdl's implementation for this abstraction now. Its portable to
[oota-llvm.git] / lib / System / DynamicLibrary.cpp
1 //===-- 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"
15 #include "ltdl.h"
16 #include <cassert>
17
18 //===----------------------------------------------------------------------===//
19 //=== WARNING: Implementation here must contain only TRULY operating system
20 //===          independent code. 
21 //===----------------------------------------------------------------------===//
22
23 namespace llvm {
24
25 using namespace sys;
26
27 DynamicLibrary::DynamicLibrary() : handle(0) {
28   if (0 != lt_dlinit())
29     throw std::string(lt_dlerror());
30
31   handle = lt_dlopen(0);
32
33   if (handle == 0)
34     throw std::string("Can't open program as dynamic library");
35 }
36
37 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
38   if (0 != lt_dlinit())
39     throw std::string(lt_dlerror());
40
41   handle = lt_dlopen(filename);
42
43   if (handle == 0)
44     handle = lt_dlopenext(filename);
45
46   if (handle == 0)
47     throw std::string("Can't open dynamic library:") + filename;
48 }
49
50 DynamicLibrary::~DynamicLibrary() {
51   if (handle)
52     lt_dlclose((lt_dlhandle)handle);
53
54   lt_dlexit();
55 }
56
57 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
58   assert(handle != 0 && "Invalid DynamicLibrary handle");
59   return lt_dlsym((lt_dlhandle) handle,symbolName);
60 }
61
62 #if 0 
63 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
64   assert(!"Have ltdl.h but not libltdl.a!");
65 }
66
67 DynamicLibrary::~DynamicLibrary() {
68   assert(!"Have ltdl.h but not libltdl.a!");
69 }
70
71 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
72   assert(!"Have ltdl.h but not libltdl.a!");
73   return 0;
74 }
75
76 #endif
77
78 } // namespace llvm