blob: 822618d5c909cb107ce0e9b4d1f1dcdabb953934 [file] [log] [blame]
Reid Spencer0de02a62004-11-18 04:33:39 +00001//===- 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"
Misha Brukman67828352004-12-19 19:27:11 +000015#include <sys/stat.h>
Reid Spencer0de02a62004-11-18 04:33:39 +000016
17namespace llvm {
18using namespace sys;
19
Reid Spencer441cc2a2004-11-29 10:39:46 +000020DynamicLibrary::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}
Reid Spencer0de02a62004-11-18 04:33:39 +000028
Reid Spencer441cc2a2004-11-29 10:39:46 +000029DynamicLibrary::DynamicLibrary(const char *filename) : handle(0) {
Reid Spencer0de02a62004-11-18 04:33:39 +000030#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
38DynamicLibrary::~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
47void *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}