blob: 0e1c0a30b5c614e9185439724c58543e6a56192d [file] [log] [blame]
Reid Spencer0de02a62004-11-18 04:33:39 +00001//===-- 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"
Reid Spencer29ae1772004-11-29 12:39:10 +000015#include "ltdl.h"
Reid Spencer0de02a62004-11-18 04:33:39 +000016#include <cassert>
17
18//===----------------------------------------------------------------------===//
19//=== WARNING: Implementation here must contain only TRULY operating system
20//=== independent code.
21//===----------------------------------------------------------------------===//
22
Reid Spencer0de02a62004-11-18 04:33:39 +000023namespace llvm {
24
25using namespace sys;
26
Reid Spencer441cc2a2004-11-29 10:39:46 +000027DynamicLibrary::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
Reid Spencer0de02a62004-11-18 04:33:39 +000037DynamicLibrary::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
50DynamicLibrary::~DynamicLibrary() {
51 if (handle)
52 lt_dlclose((lt_dlhandle)handle);
53
54 lt_dlexit();
55}
56
57void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
58 assert(handle != 0 && "Invalid DynamicLibrary handle");
59 return lt_dlsym((lt_dlhandle) handle,symbolName);
60}
61
Reid Spencer29ae1772004-11-29 12:39:10 +000062#if 0
Reid Spencer0de02a62004-11-18 04:33:39 +000063DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
64 assert(!"Have ltdl.h but not libltdl.a!");
65}
66
67DynamicLibrary::~DynamicLibrary() {
68 assert(!"Have ltdl.h but not libltdl.a!");
69}
70
71void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
72 assert(!"Have ltdl.h but not libltdl.a!");
73 return 0;
74}
75
Reid Spencer29ae1772004-11-29 12:39:10 +000076#endif
Reid Spencer0de02a62004-11-18 04:33:39 +000077
78} // namespace llvm