blob: 669422c8427cca85d0a8048b067ed17c109a1526 [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"
Owen Anderson132a2f22009-06-25 18:12:44 +000018#include "llvm/Support/ManagedStatic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Config/config.h"
Duncan Sands05f68372008-10-08 07:23:46 +000020#include <cstdio>
Duncan Sandsfca20142008-01-09 19:42:09 +000021#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include <map>
Chris Lattner0d4e1292009-07-07 18:01:58 +000023#include <vector>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024
25// Collection of symbol name/value pairs to be searched prior to any libraries.
Chris Lattner71ac96d2009-07-07 18:17:07 +000026static std::map<std::string, void*> *ExplicitSymbols = 0;
Owen Anderson132a2f22009-06-25 18:12:44 +000027
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
29 void *symbolValue) {
Chris Lattner71ac96d2009-07-07 18:17:07 +000030 if (ExplicitSymbols == 0)
31 ExplicitSymbols = new std::map<std::string, void*>();
32 (*ExplicitSymbols)[symbolName] = symbolValue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033}
34
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#ifdef LLVM_ON_WIN32
36
37#include "Win32/DynamicLibrary.inc"
38
39#else
40
Devang Patelaa4ea5a2008-02-13 17:11:39 +000041#include <dlfcn.h>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43using namespace llvm::sys;
44
45//===----------------------------------------------------------------------===//
46//=== WARNING: Implementation here must contain only TRULY operating system
47//=== independent code.
48//===----------------------------------------------------------------------===//
49
Chris Lattner71ac96d2009-07-07 18:17:07 +000050static std::vector<void *> *OpenedHandles = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052
53bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
54 std::string *ErrMsg) {
Chris Lattner859cf632008-07-10 00:52:20 +000055 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
Devang Patelaa4ea5a2008-02-13 17:11:39 +000056 if (H == 0) {
Chris Lattner71ac96d2009-07-07 18:17:07 +000057 if (ErrMsg) *ErrMsg = dlerror();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 return true;
59 }
Chris Lattner71ac96d2009-07-07 18:17:07 +000060 if (OpenedHandles == 0)
61 OpenedHandles = new std::vector<void *>();
62 OpenedHandles->push_back(H);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 return false;
64}
65
66void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 // First check symbols added via AddSymbol().
Chris Lattner71ac96d2009-07-07 18:17:07 +000068 if (ExplicitSymbols) {
69 std::map<std::string, void *>::iterator I =
70 ExplicitSymbols->find(symbolName);
71 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
Owen Anderson132a2f22009-06-25 18:12:44 +000072
Chris Lattner71ac96d2009-07-07 18:17:07 +000073 if (I != E)
74 return I->second;
75 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076
77 // Now search the libraries.
Chris Lattner71ac96d2009-07-07 18:17:07 +000078 if (OpenedHandles) {
79 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
80 E = OpenedHandles->end(); I != E; ++I) {
81 //lt_ptr ptr = lt_dlsym(*I, symbolName);
82 void *ptr = dlsym(*I, symbolName);
83 if (ptr) {
84 return ptr;
85 }
Owen Anderson132a2f22009-06-25 18:12:44 +000086 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 }
88
Anton Korobeynikov942cbab2007-12-03 05:30:41 +000089#define EXPLICIT_SYMBOL(SYM) \
90 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
91
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 // If this is darwin, it has some funky issues, try to solve them here. Some
93 // important symbols are marked 'private external' which doesn't allow
94 // SearchForAddressOfSymbol to find them. As such, we special case them here,
95 // there is only a small handful of them.
96
97#ifdef __APPLE__
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 {
99 EXPLICIT_SYMBOL(__ashldi3);
100 EXPLICIT_SYMBOL(__ashrdi3);
101 EXPLICIT_SYMBOL(__cmpdi2);
102 EXPLICIT_SYMBOL(__divdi3);
103 EXPLICIT_SYMBOL(__eprintf);
104 EXPLICIT_SYMBOL(__fixdfdi);
105 EXPLICIT_SYMBOL(__fixsfdi);
106 EXPLICIT_SYMBOL(__fixunsdfdi);
107 EXPLICIT_SYMBOL(__fixunssfdi);
108 EXPLICIT_SYMBOL(__floatdidf);
109 EXPLICIT_SYMBOL(__floatdisf);
110 EXPLICIT_SYMBOL(__lshrdi3);
111 EXPLICIT_SYMBOL(__moddi3);
112 EXPLICIT_SYMBOL(__udivdi3);
113 EXPLICIT_SYMBOL(__umoddi3);
114 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115#endif
116
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000117#ifdef __CYGWIN__
118 {
119 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000120 EXPLICIT_SYMBOL(__main);
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000121 }
122#endif
123
124#undef EXPLICIT_SYMBOL
125
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126// This macro returns the address of a well-known, explicit symbol
127#define EXPLICIT_SYMBOL(SYM) \
128 if (!strcmp(symbolName, #SYM)) return &SYM
129
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.
142 {
143#ifndef stdin
144 EXPLICIT_SYMBOL(stdin);
145#endif
146#ifndef stdout
147 EXPLICIT_SYMBOL(stdout);
148#endif
149#ifndef stderr
150 EXPLICIT_SYMBOL(stderr);
151#endif
152 }
153#endif
154#undef EXPLICIT_SYMBOL
155
156 return 0;
157}
158
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159#endif // LLVM_ON_WIN32