blob: 63baa6d787c2ed4595657a596cdf287ec7416e45 [file] [log] [blame]
Shih-wei Liaoe264f622010-02-10 11:10:31 -08001//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system DynamicLibrary concept.
11//
12// FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
13// not thread safe!
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/System/DynamicLibrary.h"
18#include "llvm/Config/config.h"
19#include <cstdio>
20#include <cstring>
21#include <map>
22#include <vector>
23
24// Collection of symbol name/value pairs to be searched prior to any libraries.
25static std::map<std::string, void*> *ExplicitSymbols = 0;
26
27static struct ExplicitSymbolsDeleter {
28 ~ExplicitSymbolsDeleter() {
29 if (ExplicitSymbols)
30 delete ExplicitSymbols;
31 }
32} Dummy;
33
34void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
35 void *symbolValue) {
36 if (ExplicitSymbols == 0)
37 ExplicitSymbols = new std::map<std::string, void*>();
38 (*ExplicitSymbols)[symbolName] = symbolValue;
39}
40
41#ifdef LLVM_ON_WIN32
42
43#include "Win32/DynamicLibrary.inc"
44
45#else
46
47#include <dlfcn.h>
48using namespace llvm;
49using namespace llvm::sys;
50
51//===----------------------------------------------------------------------===//
52//=== WARNING: Implementation here must contain only TRULY operating system
53//=== independent code.
54//===----------------------------------------------------------------------===//
55
56static std::vector<void *> *OpenedHandles = 0;
57
58
59bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
60 std::string *ErrMsg) {
61 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
62 if (H == 0) {
63 if (ErrMsg) *ErrMsg = dlerror();
64 return true;
65 }
66 if (OpenedHandles == 0)
67 OpenedHandles = new std::vector<void *>();
68 OpenedHandles->push_back(H);
69 return false;
70}
71
72static void *SearchForAddressOfSpecialSymbol(const char* symbolName) {
73#define EXPLICIT_SYMBOL(SYM) \
74 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
75
76 // 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.
80
81#ifdef __APPLE__
82 {
83 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);
98 }
99#endif
100
101#ifdef __CYGWIN__
102 {
103 EXPLICIT_SYMBOL(_alloca);
104 EXPLICIT_SYMBOL(__main);
105 }
106#endif
107
108#undef EXPLICIT_SYMBOL
109 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;
137
138// 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
171#endif // LLVM_ON_WIN32