blob: 970266f3438768e09c4a505725e116389d336e6d [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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/config.h"
Duncan Sands05f68372008-10-08 07:23:46 +000016#include <cstdio>
Duncan Sandsfca20142008-01-09 19:42:09 +000017#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include <map>
19
20// Collection of symbol name/value pairs to be searched prior to any libraries.
21static std::map<std::string, void *> g_symbols;
22
23void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
24 void *symbolValue) {
25 g_symbols[symbolName] = symbolValue;
26}
27
28// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
29// license and special exception would cause all of LLVM to be placed under
30// the LGPL. This is because the exception applies only when libtool is
31// used, and obviously libtool is not used with Visual Studio. An entirely
32// separate implementation is provided in win32/DynamicLibrary.cpp.
33
34#ifdef LLVM_ON_WIN32
35
36#include "Win32/DynamicLibrary.inc"
37
38#else
39
Devang Patelaa4ea5a2008-02-13 17:11:39 +000040//#include "ltdl.h"
41#include <dlfcn.h>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include <cassert>
43using namespace llvm;
44using namespace llvm::sys;
45
46//===----------------------------------------------------------------------===//
47//=== WARNING: Implementation here must contain only TRULY operating system
48//=== independent code.
49//===----------------------------------------------------------------------===//
50
Devang Patelaa4ea5a2008-02-13 17:11:39 +000051//static std::vector<lt_dlhandle> OpenedHandles;
52static std::vector<void *> OpenedHandles;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
Devang Patel79df1c62008-03-13 16:55:34 +000054DynamicLibrary::DynamicLibrary() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
56DynamicLibrary::~DynamicLibrary() {
Devang Patelaa4ea5a2008-02-13 17:11:39 +000057 while(!OpenedHandles.empty()) {
58 void *H = OpenedHandles.back(); OpenedHandles.pop_back();
59 dlclose(H);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 }
61}
62
63bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
64 std::string *ErrMsg) {
Chris Lattner859cf632008-07-10 00:52:20 +000065 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
Devang Patelaa4ea5a2008-02-13 17:11:39 +000066 if (H == 0) {
Chris Lattneracc81932008-03-12 00:50:01 +000067 if (ErrMsg)
68 *ErrMsg = dlerror();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 return true;
70 }
Devang Patelaa4ea5a2008-02-13 17:11:39 +000071 OpenedHandles.push_back(H);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 return false;
73}
74
75void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Devang Patelaa4ea5a2008-02-13 17:11:39 +000076 // check_ltdl_initialization();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077
78 // First check symbols added via AddSymbol().
79 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
80 if (I != g_symbols.end())
81 return I->second;
82
83 // Now search the libraries.
Devang Patelaa4ea5a2008-02-13 17:11:39 +000084 for (std::vector<void *>::iterator I = OpenedHandles.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 E = OpenedHandles.end(); I != E; ++I) {
Devang Patelaa4ea5a2008-02-13 17:11:39 +000086 //lt_ptr ptr = lt_dlsym(*I, symbolName);
87 void *ptr = dlsym(*I, symbolName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 if (ptr)
89 return ptr;
90 }
91
Anton Korobeynikov942cbab2007-12-03 05:30:41 +000092#define EXPLICIT_SYMBOL(SYM) \
93 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
94
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 // If this is darwin, it has some funky issues, try to solve them here. Some
96 // important symbols are marked 'private external' which doesn't allow
97 // SearchForAddressOfSymbol to find them. As such, we special case them here,
98 // there is only a small handful of them.
99
100#ifdef __APPLE__
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 {
102 EXPLICIT_SYMBOL(__ashldi3);
103 EXPLICIT_SYMBOL(__ashrdi3);
104 EXPLICIT_SYMBOL(__cmpdi2);
105 EXPLICIT_SYMBOL(__divdi3);
106 EXPLICIT_SYMBOL(__eprintf);
107 EXPLICIT_SYMBOL(__fixdfdi);
108 EXPLICIT_SYMBOL(__fixsfdi);
109 EXPLICIT_SYMBOL(__fixunsdfdi);
110 EXPLICIT_SYMBOL(__fixunssfdi);
111 EXPLICIT_SYMBOL(__floatdidf);
112 EXPLICIT_SYMBOL(__floatdisf);
113 EXPLICIT_SYMBOL(__lshrdi3);
114 EXPLICIT_SYMBOL(__moddi3);
115 EXPLICIT_SYMBOL(__udivdi3);
116 EXPLICIT_SYMBOL(__umoddi3);
117 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118#endif
119
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000120#ifdef __CYGWIN__
121 {
122 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000123 EXPLICIT_SYMBOL(__main);
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000124 }
125#endif
126
127#undef EXPLICIT_SYMBOL
128
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129// This macro returns the address of a well-known, explicit symbol
130#define EXPLICIT_SYMBOL(SYM) \
131 if (!strcmp(symbolName, #SYM)) return &SYM
132
133// On linux we have a weird situation. The stderr/out/in symbols are both
134// macros and global variables because of standards requirements. So, we
135// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
136#if defined(__linux__)
137 {
138 EXPLICIT_SYMBOL(stderr);
139 EXPLICIT_SYMBOL(stdout);
140 EXPLICIT_SYMBOL(stdin);
141 }
142#else
143 // For everything else, we want to check to make sure the symbol isn't defined
144 // as a macro before using EXPLICIT_SYMBOL.
145 {
146#ifndef stdin
147 EXPLICIT_SYMBOL(stdin);
148#endif
149#ifndef stdout
150 EXPLICIT_SYMBOL(stdout);
151#endif
152#ifndef stderr
153 EXPLICIT_SYMBOL(stderr);
154#endif
155 }
156#endif
157#undef EXPLICIT_SYMBOL
158
159 return 0;
160}
161
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162#endif // LLVM_ON_WIN32