822618d5c909cb107ce0e9b4d1f1dcdabb953934
[oota-llvm.git] / lib / System / Unix / DynamicLibrary.cpp
1 //===- Unix/DynamicLibrary.cpp - Generic UNIX Dynamic Library ---*- 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 file implements the generic UNIX variant of DynamicLibrary
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Unix.h"
15 #include <sys/stat.h>
16
17 namespace llvm {
18 using namespace sys;
19
20 DynamicLibrary::DynamicLibrary() : handle(0) {
21 #if defined (HAVE_DLOPEN)
22   if ((handle = dlopen(0, RTLD_NOW | RTLD_GLOBAL)) == 0)
23     throw std::string( dlerror() );
24 #else
25   assert(!"Dynamic object linking not implemented for this platform");
26 #endif
27 }
28
29 DynamicLibrary::DynamicLibrary(const char *filename) : handle(0) {
30 #if defined (HAVE_DLOPEN)
31   if ((handle = dlopen (filename, RTLD_NOW | RTLD_GLOBAL)) == 0)
32     throw std::string( dlerror() );
33 #else
34   assert (!"Dynamic object linking not implemented for this platform");
35 #endif
36 }
37
38 DynamicLibrary::~DynamicLibrary() {
39   assert(handle != 0 && "Invalid DynamicLibrary handle");
40 #if defined (HAVE_DLOPEN)
41   dlclose(handle);
42 #else
43   assert (!"Dynamic object linking not implemented for this platform");
44 #endif
45 }
46
47 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
48   assert(handle != 0 && "Invalid DynamicLibrary handle");
49 #if defined(HAVE_DLOPEN)
50     return dlsym (handle, symbolName);
51 #else
52   assert (0 && "Dynamic symbol lookup not implemented for this platform");
53 #endif
54 }
55
56 }