Put all LLVM code into the llvm namespace, as per bug 109.
[oota-llvm.git] / lib / Support / DynamicLinker.cpp
1 //===-- DynamicLinker.cpp - Implement DynamicLinker interface -------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // Lightweight interface to dynamic library linking and loading, and dynamic
11 // symbol lookup functionality, in whatever form the operating system
12 // provides it.
13 //
14 // Possible future extensions include support for the HPUX shl_load()
15 // interface, the Mac OS X NSLinkModule() interface, and the Windows
16 // LoadLibrary() interface.
17 //
18 // Note that we assume that if dlopen() is available, then dlsym() is too.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "Support/DynamicLinker.h"
23 #include "Config/dlfcn.h"
24 #include <cassert>
25
26 namespace llvm {
27
28 bool LinkDynamicObject (const char *filename, std::string *ErrorMessage) {
29 #if defined (HAVE_DLOPEN)
30   if (dlopen (filename, RTLD_NOW | RTLD_GLOBAL) == 0) {
31     if (ErrorMessage) *ErrorMessage = dlerror ();
32     return true;
33   }
34   return false;
35 #else
36   assert (0 && "Dynamic object linking not implemented for this platform");
37 #endif
38 }
39
40 void *GetAddressOfSymbol (const char *symbolName) {
41 #if defined (HAVE_DLOPEN)
42 #ifdef RTLD_DEFAULT
43   return dlsym (RTLD_DEFAULT, symbolName);
44 #else
45   static void* CurHandle = dlopen(0, RTLD_LAZY);
46   return dlsym(CurHandle, symbolName);
47 #endif
48 #else
49   assert (0 && "Dynamic symbol lookup not implemented for this platform");
50 #endif
51 }
52
53 // soft, cushiony C++ interface.
54 void *GetAddressOfSymbol (const std::string &symbolName) {
55   return GetAddressOfSymbol (symbolName.c_str ());
56 }
57
58 } // End llvm namespace