Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 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 | |
| 16 | namespace llvm { |
| 17 | using namespace sys; |
| 18 | |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 19 | DynamicLibrary::DynamicLibrary() : handle(0) { |
| 20 | #if defined (HAVE_DLOPEN) |
| 21 | if ((handle = dlopen(0, RTLD_NOW | RTLD_GLOBAL)) == 0) |
| 22 | throw std::string( dlerror() ); |
| 23 | #else |
| 24 | assert(!"Dynamic object linking not implemented for this platform"); |
| 25 | #endif |
| 26 | } |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 27 | |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 28 | DynamicLibrary::DynamicLibrary(const char *filename) : handle(0) { |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 29 | #if defined (HAVE_DLOPEN) |
| 30 | if ((handle = dlopen (filename, RTLD_NOW | RTLD_GLOBAL)) == 0) |
| 31 | throw std::string( dlerror() ); |
| 32 | #else |
| 33 | assert (!"Dynamic object linking not implemented for this platform"); |
| 34 | #endif |
| 35 | } |
| 36 | |
| 37 | DynamicLibrary::~DynamicLibrary() { |
| 38 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
| 39 | #if defined (HAVE_DLOPEN) |
| 40 | dlclose(handle); |
| 41 | #else |
| 42 | assert (!"Dynamic object linking not implemented for this platform"); |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
| 47 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
| 48 | #if defined(HAVE_DLOPEN) |
| 49 | return dlsym (handle, symbolName); |
| 50 | #else |
| 51 | assert (0 && "Dynamic symbol lookup not implemented for this platform"); |
| 52 | #endif |
| 53 | } |
| 54 | |
| 55 | } |