From f212e472710f8799810d188c174b5b5cc5dcf7b0 Mon Sep 17 00:00:00 2001 From: Brian Gaeke Date: Fri, 10 Oct 2003 16:55:42 +0000 Subject: [PATCH] Add my abstracted dynamic linker support files. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9008 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/Support/DynamicLinker.h | 29 +++++++++++++++++++ include/llvm/Support/DynamicLinker.h | 29 +++++++++++++++++++ lib/Support/DynamicLinker.cpp | 42 ++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 include/Support/DynamicLinker.h create mode 100644 include/llvm/Support/DynamicLinker.h create mode 100644 lib/Support/DynamicLinker.cpp diff --git a/include/Support/DynamicLinker.h b/include/Support/DynamicLinker.h new file mode 100644 index 00000000000..9b0e749aeba --- /dev/null +++ b/include/Support/DynamicLinker.h @@ -0,0 +1,29 @@ +//===-- DynamicLinker.h - System-indep. DynamicLinker interface -*- C++ -*-===// +// +// Lightweight interface to dynamic library linking and loading, and dynamic +// symbol lookup functionality, in whatever form the operating system +// provides it. +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_DYNAMICLINKER_H +#define SUPPORT_DYNAMICLINKER_H + +#include + +/// LinkDynamicObject - Load the named file as a dynamic library +/// and link it with the currently running process. Returns false +/// on success, true if there is an error (and sets ErrorMessage +/// if it is not NULL). Analogous to dlopen(). +/// +bool LinkDynamicObject (const char *filename, std::string *ErrorMessage); + +/// GetAddressOfSymbol - Returns the address of the named symbol in +/// the currently running process, as reported by the dynamic linker, +/// or NULL if the symbol does not exist or some other error has +/// occurred. +/// +void *GetAddressOfSymbol (const char *symbolName); +void *GetAddressOfSymbol (const std::string &symbolName); + +#endif // SUPPORT_DYNAMICLINKER_H diff --git a/include/llvm/Support/DynamicLinker.h b/include/llvm/Support/DynamicLinker.h new file mode 100644 index 00000000000..9b0e749aeba --- /dev/null +++ b/include/llvm/Support/DynamicLinker.h @@ -0,0 +1,29 @@ +//===-- DynamicLinker.h - System-indep. DynamicLinker interface -*- C++ -*-===// +// +// Lightweight interface to dynamic library linking and loading, and dynamic +// symbol lookup functionality, in whatever form the operating system +// provides it. +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_DYNAMICLINKER_H +#define SUPPORT_DYNAMICLINKER_H + +#include + +/// LinkDynamicObject - Load the named file as a dynamic library +/// and link it with the currently running process. Returns false +/// on success, true if there is an error (and sets ErrorMessage +/// if it is not NULL). Analogous to dlopen(). +/// +bool LinkDynamicObject (const char *filename, std::string *ErrorMessage); + +/// GetAddressOfSymbol - Returns the address of the named symbol in +/// the currently running process, as reported by the dynamic linker, +/// or NULL if the symbol does not exist or some other error has +/// occurred. +/// +void *GetAddressOfSymbol (const char *symbolName); +void *GetAddressOfSymbol (const std::string &symbolName); + +#endif // SUPPORT_DYNAMICLINKER_H diff --git a/lib/Support/DynamicLinker.cpp b/lib/Support/DynamicLinker.cpp new file mode 100644 index 00000000000..7c52d3bb5b1 --- /dev/null +++ b/lib/Support/DynamicLinker.cpp @@ -0,0 +1,42 @@ +//===-- DynamicLinker.cpp - Implement DynamicLinker interface -------------===// +// +// Lightweight interface to dynamic library linking and loading, and dynamic +// symbol lookup functionality, in whatever form the operating system +// provides it. +// +// Possible future extensions include support for the HPUX shl_load() +// interface, the Mac OS X NSLinkModule() interface, and the Windows +// LoadLibrary() interface. +// +// Note that we assume that if dlopen() is available, then dlsym() is too. +// +//===----------------------------------------------------------------------===// + +#include "Support/DynamicLinker.h" +#include "Config/dlfcn.h" +#include + +bool LinkDynamicObject (const char *filename, std::string *ErrorMessage) { +#if defined (HAVE_DLOPEN) + if (dlopen (filename, RTLD_NOW | RTLD_GLOBAL) == 0) { + if (ErrorMessage) *ErrorMessage = dlerror (); + return true; + } + return false; +#else + assert (0 && "Dynamic object linking not implemented for this platform"); +#endif +} + +void *GetAddressOfSymbol (const char *symbolName) { +#if defined (HAVE_DLOPEN) + return dlsym (RTLD_DEFAULT, symbolName); +#else + assert (0 && "Dynamic symbol lookup not implemented for this platform"); +#endif +} + +// soft, cushiony C++ interface. +void *GetAddressOfSymbol (const std::string &symbolName) { + return GetAddressOfSymbol (symbolName.c_str ()); +} -- 2.34.1