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