blob: 63baa6d787c2ed4595657a596cdf287ec7416e45 [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//
Chris Lattner71ac96d2009-07-07 18:17:07 +000012// FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
13// not thread safe!
14//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015//===----------------------------------------------------------------------===//
16
17#include "llvm/System/DynamicLibrary.h"
18#include "llvm/Config/config.h"
Duncan Sands05f68372008-10-08 07:23:46 +000019#include <cstdio>
Duncan Sandsfca20142008-01-09 19:42:09 +000020#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include <map>
Chris Lattner0d4e1292009-07-07 18:01:58 +000022#include <vector>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023
24// Collection of symbol name/value pairs to be searched prior to any libraries.
Chris Lattner71ac96d2009-07-07 18:17:07 +000025static std::map<std::string, void*> *ExplicitSymbols = 0;
Owen Anderson132a2f22009-06-25 18:12:44 +000026
Edwin Török6d5f43b2009-08-31 16:12:29 +000027static struct ExplicitSymbolsDeleter {
28 ~ExplicitSymbolsDeleter() {
29 if (ExplicitSymbols)
30 delete ExplicitSymbols;
31 }
32} Dummy;
33
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
35 void *symbolValue) {
Chris Lattner71ac96d2009-07-07 18:17:07 +000036 if (ExplicitSymbols == 0)
37 ExplicitSymbols = new std::map<std::string, void*>();
38 (*ExplicitSymbols)[symbolName] = symbolValue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039}
40
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#ifdef LLVM_ON_WIN32
42
43#include "Win32/DynamicLibrary.inc"
44
45#else
46
Devang Patelaa4ea5a2008-02-13 17:11:39 +000047#include <dlfcn.h>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048using namespace llvm;
49using namespace llvm::sys;
50
51//===----------------------------------------------------------------------===//
52//=== WARNING: Implementation here must contain only TRULY operating system
53//=== independent code.
54//===----------------------------------------------------------------------===//
55
Chris Lattner71ac96d2009-07-07 18:17:07 +000056static std::vector<void *> *OpenedHandles = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
60 std::string *ErrMsg) {
Chris Lattner859cf632008-07-10 00:52:20 +000061 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
Devang Patelaa4ea5a2008-02-13 17:11:39 +000062 if (H == 0) {
Chris Lattner71ac96d2009-07-07 18:17:07 +000063 if (ErrMsg) *ErrMsg = dlerror();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 return true;
65 }
Chris Lattner71ac96d2009-07-07 18:17:07 +000066 if (OpenedHandles == 0)
67 OpenedHandles = new std::vector<void *>();
68 OpenedHandles->push_back(H);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 return false;
70}
71
Douglas Gregorb4f02ed2009-12-23 19:12:50 +000072static void *SearchForAddressOfSpecialSymbol(const char* symbolName) {
Anton Korobeynikov942cbab2007-12-03 05:30:41 +000073#define EXPLICIT_SYMBOL(SYM) \
Douglas Gregorda09e582009-12-23 19:04:10 +000074 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
Anton Korobeynikov942cbab2007-12-03 05:30:41 +000075
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 // If this is darwin, it has some funky issues, try to solve them here. Some
77 // important symbols are marked 'private external' which doesn't allow
78 // SearchForAddressOfSymbol to find them. As such, we special case them here,
79 // there is only a small handful of them.
Douglas Gregorda09e582009-12-23 19:04:10 +000080
81#ifdef __APPLE__
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 {
Douglas Gregorda09e582009-12-23 19:04:10 +000083 EXPLICIT_SYMBOL(__ashldi3);
84 EXPLICIT_SYMBOL(__ashrdi3);
85 EXPLICIT_SYMBOL(__cmpdi2);
86 EXPLICIT_SYMBOL(__divdi3);
87 EXPLICIT_SYMBOL(__eprintf);
88 EXPLICIT_SYMBOL(__fixdfdi);
89 EXPLICIT_SYMBOL(__fixsfdi);
90 EXPLICIT_SYMBOL(__fixunsdfdi);
91 EXPLICIT_SYMBOL(__fixunssfdi);
92 EXPLICIT_SYMBOL(__floatdidf);
93 EXPLICIT_SYMBOL(__floatdisf);
94 EXPLICIT_SYMBOL(__lshrdi3);
95 EXPLICIT_SYMBOL(__moddi3);
96 EXPLICIT_SYMBOL(__udivdi3);
97 EXPLICIT_SYMBOL(__umoddi3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 }
Douglas Gregorda09e582009-12-23 19:04:10 +000099#endif
100
101#ifdef __CYGWIN__
102 {
103 EXPLICIT_SYMBOL(_alloca);
104 EXPLICIT_SYMBOL(__main);
105 }
106#endif
107
108#undef EXPLICIT_SYMBOL
Douglas Gregorb4f02ed2009-12-23 19:12:50 +0000109 return 0;
110}
111
112void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
113 // First check symbols added via AddSymbol().
114 if (ExplicitSymbols) {
115 std::map<std::string, void *>::iterator I =
116 ExplicitSymbols->find(symbolName);
117 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
118
119 if (I != E)
120 return I->second;
121 }
122
123 // Now search the libraries.
124 if (OpenedHandles) {
125 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
126 E = OpenedHandles->end(); I != E; ++I) {
127 //lt_ptr ptr = lt_dlsym(*I, symbolName);
128 void *ptr = dlsym(*I, symbolName);
129 if (ptr) {
130 return ptr;
131 }
132 }
133 }
134
135 if (void *Result = SearchForAddressOfSpecialSymbol(symbolName))
136 return Result;
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138// This macro returns the address of a well-known, explicit symbol
139#define EXPLICIT_SYMBOL(SYM) \
140 if (!strcmp(symbolName, #SYM)) return &SYM
141
142// On linux we have a weird situation. The stderr/out/in symbols are both
143// macros and global variables because of standards requirements. So, we
144// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
145#if defined(__linux__)
146 {
147 EXPLICIT_SYMBOL(stderr);
148 EXPLICIT_SYMBOL(stdout);
149 EXPLICIT_SYMBOL(stdin);
150 }
151#else
152 // For everything else, we want to check to make sure the symbol isn't defined
153 // as a macro before using EXPLICIT_SYMBOL.
154 {
155#ifndef stdin
156 EXPLICIT_SYMBOL(stdin);
157#endif
158#ifndef stdout
159 EXPLICIT_SYMBOL(stdout);
160#endif
161#ifndef stderr
162 EXPLICIT_SYMBOL(stderr);
163#endif
164 }
165#endif
166#undef EXPLICIT_SYMBOL
167
168 return 0;
169}
170
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171#endif // LLVM_ON_WIN32