blob: 477e3051c032c713a70c41453b1ab9c39226b4eb [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"
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
25namespace llvm {
26
27using namespace sys;
28
29#ifdef HAVE_LT_DLOPEN
30
Reid Spencer441cc2a2004-11-29 10:39:46 +000031DynamicLibrary::DynamicLibrary() : handle(0) {
32 if (0 != lt_dlinit())
33 throw std::string(lt_dlerror());
34
35 handle = lt_dlopen(0);
36
37 if (handle == 0)
38 throw std::string("Can't open program as dynamic library");
39}
40
Reid Spencer0de02a62004-11-18 04:33:39 +000041DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
42 if (0 != lt_dlinit())
43 throw std::string(lt_dlerror());
44
45 handle = lt_dlopen(filename);
46
47 if (handle == 0)
48 handle = lt_dlopenext(filename);
49
50 if (handle == 0)
51 throw std::string("Can't open dynamic library:") + filename;
52}
53
54DynamicLibrary::~DynamicLibrary() {
55 if (handle)
56 lt_dlclose((lt_dlhandle)handle);
57
58 lt_dlexit();
59}
60
61void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
62 assert(handle != 0 && "Invalid DynamicLibrary handle");
63 return lt_dlsym((lt_dlhandle) handle,symbolName);
64}
65
66#else
67DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
68 assert(!"Have ltdl.h but not libltdl.a!");
69}
70
71DynamicLibrary::~DynamicLibrary() {
72 assert(!"Have ltdl.h but not libltdl.a!");
73}
74
75void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
76 assert(!"Have ltdl.h but not libltdl.a!");
77 return 0;
78}
79
80#endif // HAVE_DLOPEN
81
82} // namespace llvm
83
84#else // HAVE_LTDL_H
85
86#include "platform/DynamicLibrary.cpp"
87
88#endif