blob: b88d03f49439334a0503c56dc6170a3e6bf4a421 [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"
Owen Anderson132a2f22009-06-25 18:12:44 +000015#include "llvm/Support/ManagedStatic.h"
16#include "llvm/System/RWMutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Config/config.h"
Duncan Sands05f68372008-10-08 07:23:46 +000018#include <cstdio>
Duncan Sandsfca20142008-01-09 19:42:09 +000019#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include <map>
Chris Lattner0d4e1292009-07-07 18:01:58 +000021#include <vector>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022
23// Collection of symbol name/value pairs to be searched prior to any libraries.
Owen Anderson132a2f22009-06-25 18:12:44 +000024static std::map<std::string, void*> symbols;
25static llvm::sys::SmartRWMutex<true> SymbolsLock;
26
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
28 void *symbolValue) {
Owen Anderson132a2f22009-06-25 18:12:44 +000029 llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
30 symbols[symbolName] = symbolValue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031}
32
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#ifdef LLVM_ON_WIN32
34
35#include "Win32/DynamicLibrary.inc"
36
37#else
38
Devang Patelaa4ea5a2008-02-13 17:11:39 +000039#include <dlfcn.h>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include <cassert>
41using namespace llvm;
42using namespace llvm::sys;
43
44//===----------------------------------------------------------------------===//
45//=== WARNING: Implementation here must contain only TRULY operating system
46//=== independent code.
47//===----------------------------------------------------------------------===//
48
Devang Patelaa4ea5a2008-02-13 17:11:39 +000049static std::vector<void *> OpenedHandles;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
53 std::string *ErrMsg) {
Owen Anderson132a2f22009-06-25 18:12:44 +000054 SmartScopedWriter<true> Writer(&SymbolsLock);
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 Lattneracc81932008-03-12 00:50:01 +000057 if (ErrMsg)
58 *ErrMsg = dlerror();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 return true;
60 }
Devang Patelaa4ea5a2008-02-13 17:11:39 +000061 OpenedHandles.push_back(H);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 return false;
63}
64
65void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 // First check symbols added via AddSymbol().
Owen Anderson132a2f22009-06-25 18:12:44 +000067 SymbolsLock.reader_acquire();
68 std::map<std::string, void *>::iterator I = symbols.find(symbolName);
69 std::map<std::string, void *>::iterator E = symbols.end();
70 SymbolsLock.reader_release();
71
72 if (I != E)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 return I->second;
74
Owen Anderson132a2f22009-06-25 18:12:44 +000075 SymbolsLock.writer_acquire();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 // Now search the libraries.
Devang Patelaa4ea5a2008-02-13 17:11:39 +000077 for (std::vector<void *>::iterator I = OpenedHandles.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 E = OpenedHandles.end(); I != E; ++I) {
Devang Patelaa4ea5a2008-02-13 17:11:39 +000079 //lt_ptr ptr = lt_dlsym(*I, symbolName);
80 void *ptr = dlsym(*I, symbolName);
Owen Anderson132a2f22009-06-25 18:12:44 +000081 if (ptr) {
82 SymbolsLock.writer_release();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 return ptr;
Owen Anderson132a2f22009-06-25 18:12:44 +000084 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 }
Owen Anderson132a2f22009-06-25 18:12:44 +000086 SymbolsLock.writer_release();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
Anton Korobeynikov942cbab2007-12-03 05:30:41 +000088#define EXPLICIT_SYMBOL(SYM) \
89 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
90
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 // If this is darwin, it has some funky issues, try to solve them here. Some
92 // important symbols are marked 'private external' which doesn't allow
93 // SearchForAddressOfSymbol to find them. As such, we special case them here,
94 // there is only a small handful of them.
95
96#ifdef __APPLE__
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 {
98 EXPLICIT_SYMBOL(__ashldi3);
99 EXPLICIT_SYMBOL(__ashrdi3);
100 EXPLICIT_SYMBOL(__cmpdi2);
101 EXPLICIT_SYMBOL(__divdi3);
102 EXPLICIT_SYMBOL(__eprintf);
103 EXPLICIT_SYMBOL(__fixdfdi);
104 EXPLICIT_SYMBOL(__fixsfdi);
105 EXPLICIT_SYMBOL(__fixunsdfdi);
106 EXPLICIT_SYMBOL(__fixunssfdi);
107 EXPLICIT_SYMBOL(__floatdidf);
108 EXPLICIT_SYMBOL(__floatdisf);
109 EXPLICIT_SYMBOL(__lshrdi3);
110 EXPLICIT_SYMBOL(__moddi3);
111 EXPLICIT_SYMBOL(__udivdi3);
112 EXPLICIT_SYMBOL(__umoddi3);
113 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114#endif
115
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000116#ifdef __CYGWIN__
117 {
118 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000119 EXPLICIT_SYMBOL(__main);
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000120 }
121#endif
122
123#undef EXPLICIT_SYMBOL
124
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125// This macro returns the address of a well-known, explicit symbol
126#define EXPLICIT_SYMBOL(SYM) \
127 if (!strcmp(symbolName, #SYM)) return &SYM
128
129// On linux we have a weird situation. The stderr/out/in symbols are both
130// macros and global variables because of standards requirements. So, we
131// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
132#if defined(__linux__)
133 {
134 EXPLICIT_SYMBOL(stderr);
135 EXPLICIT_SYMBOL(stdout);
136 EXPLICIT_SYMBOL(stdin);
137 }
138#else
139 // For everything else, we want to check to make sure the symbol isn't defined
140 // as a macro before using EXPLICIT_SYMBOL.
141 {
142#ifndef stdin
143 EXPLICIT_SYMBOL(stdin);
144#endif
145#ifndef stdout
146 EXPLICIT_SYMBOL(stdout);
147#endif
148#ifndef stderr
149 EXPLICIT_SYMBOL(stderr);
150#endif
151 }
152#endif
153#undef EXPLICIT_SYMBOL
154
155 return 0;
156}
157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158#endif // LLVM_ON_WIN32