blob: 801f93b5ba38f8467252096d73de994fe4448323 [file] [log] [blame]
Brian Gaekef212e472003-10-10 16:55:42 +00001//===-- DynamicLinker.cpp - Implement DynamicLinker interface -------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Brian Gaekef212e472003-10-10 16:55:42 +00009//
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>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000025using namespace llvm;
Brian Gaekef212e472003-10-10 16:55:42 +000026
Chris Lattner2cdd21c2003-12-14 21:35:53 +000027bool llvm::LinkDynamicObject (const char *filename, std::string *ErrorMessage) {
Brian Gaekef212e472003-10-10 16:55:42 +000028#if defined (HAVE_DLOPEN)
29 if (dlopen (filename, RTLD_NOW | RTLD_GLOBAL) == 0) {
30 if (ErrorMessage) *ErrorMessage = dlerror ();
31 return true;
32 }
33 return false;
34#else
35 assert (0 && "Dynamic object linking not implemented for this platform");
36#endif
37}
38
Chris Lattner2cdd21c2003-12-14 21:35:53 +000039void *llvm::GetAddressOfSymbol (const char *symbolName) {
Brian Gaekef212e472003-10-10 16:55:42 +000040#if defined (HAVE_DLOPEN)
Chris Lattner764486f2003-10-25 16:55:32 +000041#ifdef RTLD_DEFAULT
Brian Gaekef212e472003-10-10 16:55:42 +000042 return dlsym (RTLD_DEFAULT, symbolName);
43#else
Chris Lattner764486f2003-10-25 16:55:32 +000044 static void* CurHandle = dlopen(0, RTLD_LAZY);
45 return dlsym(CurHandle, symbolName);
46#endif
47#else
Brian Gaekef212e472003-10-10 16:55:42 +000048 assert (0 && "Dynamic symbol lookup not implemented for this platform");
49#endif
50}
51
52// soft, cushiony C++ interface.
Chris Lattner2cdd21c2003-12-14 21:35:53 +000053void *llvm::GetAddressOfSymbol (const std::string &symbolName) {
Brian Gaekef212e472003-10-10 16:55:42 +000054 return GetAddressOfSymbol (symbolName.c_str ());
55}