blob: 7eb9f5f3ef8e1439679f1647341723483d237310 [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
72void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 // First check symbols added via AddSymbol().
Chris Lattner71ac96d2009-07-07 18:17:07 +000074 if (ExplicitSymbols) {
75 std::map<std::string, void *>::iterator I =
76 ExplicitSymbols->find(symbolName);
77 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
Owen Anderson132a2f22009-06-25 18:12:44 +000078
Chris Lattner71ac96d2009-07-07 18:17:07 +000079 if (I != E)
80 return I->second;
81 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
83 // Now search the libraries.
Chris Lattner71ac96d2009-07-07 18:17:07 +000084 if (OpenedHandles) {
85 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
86 E = OpenedHandles->end(); I != E; ++I) {
87 //lt_ptr ptr = lt_dlsym(*I, symbolName);
88 void *ptr = dlsym(*I, symbolName);
89 if (ptr) {
90 return ptr;
91 }
Owen Anderson132a2f22009-06-25 18:12:44 +000092 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 }
94
Anton Korobeynikov942cbab2007-12-03 05:30:41 +000095#define EXPLICIT_SYMBOL(SYM) \
Douglas Gregorda09e582009-12-23 19:04:10 +000096 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
Anton Korobeynikov942cbab2007-12-03 05:30:41 +000097
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 // If this is darwin, it has some funky issues, try to solve them here. Some
99 // important symbols are marked 'private external' which doesn't allow
100 // SearchForAddressOfSymbol to find them. As such, we special case them here,
101 // there is only a small handful of them.
Douglas Gregorda09e582009-12-23 19:04:10 +0000102
103#ifdef __APPLE__
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 {
Douglas Gregorda09e582009-12-23 19:04:10 +0000105 EXPLICIT_SYMBOL(__ashldi3);
106 EXPLICIT_SYMBOL(__ashrdi3);
107 EXPLICIT_SYMBOL(__cmpdi2);
108 EXPLICIT_SYMBOL(__divdi3);
109 EXPLICIT_SYMBOL(__eprintf);
110 EXPLICIT_SYMBOL(__fixdfdi);
111 EXPLICIT_SYMBOL(__fixsfdi);
112 EXPLICIT_SYMBOL(__fixunsdfdi);
113 EXPLICIT_SYMBOL(__fixunssfdi);
114 EXPLICIT_SYMBOL(__floatdidf);
115 EXPLICIT_SYMBOL(__floatdisf);
116 EXPLICIT_SYMBOL(__lshrdi3);
117 EXPLICIT_SYMBOL(__moddi3);
118 EXPLICIT_SYMBOL(__udivdi3);
119 EXPLICIT_SYMBOL(__umoddi3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 }
Douglas Gregorda09e582009-12-23 19:04:10 +0000121#endif
122
123#ifdef __CYGWIN__
124 {
125 EXPLICIT_SYMBOL(_alloca);
126 EXPLICIT_SYMBOL(__main);
127 }
128#endif
129
130#undef EXPLICIT_SYMBOL
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000131
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132// This macro returns the address of a well-known, explicit symbol
133#define EXPLICIT_SYMBOL(SYM) \
134 if (!strcmp(symbolName, #SYM)) return &SYM
135
136// On linux we have a weird situation. The stderr/out/in symbols are both
137// macros and global variables because of standards requirements. So, we
138// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
139#if defined(__linux__)
140 {
141 EXPLICIT_SYMBOL(stderr);
142 EXPLICIT_SYMBOL(stdout);
143 EXPLICIT_SYMBOL(stdin);
144 }
145#else
146 // For everything else, we want to check to make sure the symbol isn't defined
147 // as a macro before using EXPLICIT_SYMBOL.
148 {
149#ifndef stdin
150 EXPLICIT_SYMBOL(stdin);
151#endif
152#ifndef stdout
153 EXPLICIT_SYMBOL(stdout);
154#endif
155#ifndef stderr
156 EXPLICIT_SYMBOL(stderr);
157#endif
158 }
159#endif
160#undef EXPLICIT_SYMBOL
161
162 return 0;
163}
164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165#endif // LLVM_ON_WIN32