blob: 44666a334bb3a9f3c900aa1ecafe63a015805be4 [file] [log] [blame]
Reid Spencer0de02a62004-11-18 04:33:39 +00001//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencer0de02a62004-11-18 04:33:39 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencer0de02a62004-11-18 04:33:39 +00008//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system DynamicLibrary concept.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/System/DynamicLibrary.h"
Jeff Cohena4c97512004-12-24 16:26:47 +000015#include "llvm/Config/config.h"
Duncan Sandsf52e32a2008-01-09 19:42:09 +000016#include <cstring>
Jeff Cohen85046902006-01-30 04:33:51 +000017#include <map>
18
19// Collection of symbol name/value pairs to be searched prior to any libraries.
20static std::map<std::string, void *> g_symbols;
21
Chris Lattneradcbce02006-07-07 17:12:36 +000022void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
23 void *symbolValue) {
Jeff Cohen85046902006-01-30 04:33:51 +000024 g_symbols[symbolName] = symbolValue;
25}
Jeff Cohen1a466352004-12-24 07:57:09 +000026
27// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
28// license and special exception would cause all of LLVM to be placed under
29// the LGPL. This is because the exception applies only when libtool is
30// used, and obviously libtool is not used with Visual Studio. An entirely
31// separate implementation is provided in win32/DynamicLibrary.cpp.
32
Jeff Cohena4c97512004-12-24 16:26:47 +000033#ifdef LLVM_ON_WIN32
Jeff Cohen1a466352004-12-24 07:57:09 +000034
Reid Spencerbccc8ab2005-01-09 23:29:00 +000035#include "Win32/DynamicLibrary.inc"
Jeff Cohen1a466352004-12-24 07:57:09 +000036
37#else
38
Devang Patele45252e2008-02-13 17:11:39 +000039//#include "ltdl.h"
40#include <dlfcn.h>
Reid Spencer0de02a62004-11-18 04:33:39 +000041#include <cassert>
Chris Lattner28dabf72004-12-03 23:02:42 +000042using namespace llvm;
43using namespace llvm::sys;
Reid Spencer0de02a62004-11-18 04:33:39 +000044
45//===----------------------------------------------------------------------===//
46//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000047//=== independent code.
Reid Spencer0de02a62004-11-18 04:33:39 +000048//===----------------------------------------------------------------------===//
49
Devang Patele45252e2008-02-13 17:11:39 +000050//static std::vector<lt_dlhandle> OpenedHandles;
51static std::vector<void *> OpenedHandles;
Reid Spencer19cd4a92004-11-29 13:33:28 +000052
Devang Patele45252e2008-02-13 17:11:39 +000053DynamicLibrary::DynamicLibrary() : handle(0) {}
Reid Spencer0de02a62004-11-18 04:33:39 +000054
55DynamicLibrary::~DynamicLibrary() {
Devang Patele45252e2008-02-13 17:11:39 +000056 while(!OpenedHandles.empty()) {
57 void *H = OpenedHandles.back(); OpenedHandles.pop_back();
58 dlclose(H);
Reid Spencer19cd4a92004-11-29 13:33:28 +000059 }
60}
61
Chris Lattneradcbce02006-07-07 17:12:36 +000062bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
63 std::string *ErrMsg) {
Devang Patele45252e2008-02-13 17:11:39 +000064 void *H = dlopen(Filename, RTLD_LAZY);
65 if (H == 0) {
66 ErrMsg = new std::string(dlerror());
Chris Lattneradcbce02006-07-07 17:12:36 +000067 return true;
68 }
Devang Patele45252e2008-02-13 17:11:39 +000069 OpenedHandles.push_back(H);
Chris Lattneradcbce02006-07-07 17:12:36 +000070 return false;
Reid Spencer19cd4a92004-11-29 13:33:28 +000071}
72
73void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Devang Patele45252e2008-02-13 17:11:39 +000074 // check_ltdl_initialization();
Jeff Cohen85046902006-01-30 04:33:51 +000075
76 // First check symbols added via AddSymbol().
77 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
78 if (I != g_symbols.end())
79 return I->second;
80
81 // Now search the libraries.
Devang Patele45252e2008-02-13 17:11:39 +000082 for (std::vector<void *>::iterator I = OpenedHandles.begin(),
Reid Spencer19cd4a92004-11-29 13:33:28 +000083 E = OpenedHandles.end(); I != E; ++I) {
Devang Patele45252e2008-02-13 17:11:39 +000084 //lt_ptr ptr = lt_dlsym(*I, symbolName);
85 void *ptr = dlsym(*I, symbolName);
Reid Spencer19cd4a92004-11-29 13:33:28 +000086 if (ptr)
87 return ptr;
88 }
Chris Lattner28dabf72004-12-03 23:02:42 +000089
Anton Korobeynikov96ea2092007-12-03 05:30:41 +000090#define EXPLICIT_SYMBOL(SYM) \
91 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
92
Chris Lattner28dabf72004-12-03 23:02:42 +000093 // If this is darwin, it has some funky issues, try to solve them here. Some
94 // important symbols are marked 'private external' which doesn't allow
95 // SearchForAddressOfSymbol to find them. As such, we special case them here,
96 // there is only a small handful of them.
Reid Spencer11f457a2007-01-10 19:50:43 +000097
Chris Lattner28dabf72004-12-03 23:02:42 +000098#ifdef __APPLE__
Reid Spencer11f457a2007-01-10 19:50:43 +000099 {
Chris Lattner368cb8e2004-12-04 04:17:20 +0000100 EXPLICIT_SYMBOL(__ashldi3);
101 EXPLICIT_SYMBOL(__ashrdi3);
102 EXPLICIT_SYMBOL(__cmpdi2);
103 EXPLICIT_SYMBOL(__divdi3);
104 EXPLICIT_SYMBOL(__eprintf);
105 EXPLICIT_SYMBOL(__fixdfdi);
106 EXPLICIT_SYMBOL(__fixsfdi);
107 EXPLICIT_SYMBOL(__fixunsdfdi);
108 EXPLICIT_SYMBOL(__fixunssfdi);
109 EXPLICIT_SYMBOL(__floatdidf);
110 EXPLICIT_SYMBOL(__floatdisf);
111 EXPLICIT_SYMBOL(__lshrdi3);
112 EXPLICIT_SYMBOL(__moddi3);
113 EXPLICIT_SYMBOL(__udivdi3);
114 EXPLICIT_SYMBOL(__umoddi3);
Chris Lattner28dabf72004-12-03 23:02:42 +0000115 }
116#endif
Reid Spencer02f20d32007-01-19 21:41:04 +0000117
Anton Korobeynikov96ea2092007-12-03 05:30:41 +0000118#ifdef __CYGWIN__
119 {
120 EXPLICIT_SYMBOL(_alloca);
121 }
122#endif
123
124#undef EXPLICIT_SYMBOL
125
Reid Spencer02f20d32007-01-19 21:41:04 +0000126// This macro returns the address of a well-known, explicit symbol
Reid Spencer11f457a2007-01-10 19:50:43 +0000127#define EXPLICIT_SYMBOL(SYM) \
128 if (!strcmp(symbolName, #SYM)) return &SYM
Reid Spencer02f20d32007-01-19 21:41:04 +0000129
130// On linux we have a weird situation. The stderr/out/in symbols are both
131// macros and global variables because of standards requirements. So, we
132// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
133#if defined(__linux__)
134 {
135 EXPLICIT_SYMBOL(stderr);
136 EXPLICIT_SYMBOL(stdout);
137 EXPLICIT_SYMBOL(stdin);
138 }
139#else
140 // For everything else, we want to check to make sure the symbol isn't defined
141 // as a macro before using EXPLICIT_SYMBOL.
Reid Spencer11f457a2007-01-10 19:50:43 +0000142 {
Reid Spencer65de7422007-01-11 00:35:10 +0000143#ifndef stdin
Reid Spencer11f457a2007-01-10 19:50:43 +0000144 EXPLICIT_SYMBOL(stdin);
Reid Spencer81e39542007-01-19 21:30:39 +0000145#endif
146#ifndef stdout
Reid Spencer11f457a2007-01-10 19:50:43 +0000147 EXPLICIT_SYMBOL(stdout);
Reid Spencer81e39542007-01-19 21:30:39 +0000148#endif
149#ifndef stderr
Reid Spencer11f457a2007-01-10 19:50:43 +0000150 EXPLICIT_SYMBOL(stderr);
Reid Spencer65de7422007-01-11 00:35:10 +0000151#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000152 }
Reid Spencer02f20d32007-01-19 21:41:04 +0000153#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000154#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000155
Reid Spencer19cd4a92004-11-29 13:33:28 +0000156 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000157}
158
159void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
160 assert(handle != 0 && "Invalid DynamicLibrary handle");
Devang Patele45252e2008-02-13 17:11:39 +0000161 return dlsym(handle, symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +0000162}
163
Jeff Cohena4c97512004-12-24 16:26:47 +0000164#endif // LLVM_ON_WIN32
Reid Spencer23dd3322006-07-26 16:55:39 +0000165
166DEFINING_FILE_FOR(SystemDynamicLibrary)