blob: 3bf172c22b3f376302cc30365fcc088f5a8376fb [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"
15#include "llvm/Config/config.h"
Duncan Sands05f68372008-10-08 07:23:46 +000016#include <cstdio>
Duncan Sandsfca20142008-01-09 19:42:09 +000017#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include <map>
19
20// Collection of symbol name/value pairs to be searched prior to any libraries.
Chris Lattner4b57e5a2009-01-29 04:43:42 +000021std::map<std::string, void *> &g_symbols() {
22 static std::map<std::string, void *> symbols;
23 return symbols;
24}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025
26void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
27 void *symbolValue) {
Chris Lattner4b57e5a2009-01-29 04:43:42 +000028 g_symbols()[symbolName] = symbolValue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029}
30
31// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
32// license and special exception would cause all of LLVM to be placed under
33// the LGPL. This is because the exception applies only when libtool is
34// used, and obviously libtool is not used with Visual Studio. An entirely
35// separate implementation is provided in win32/DynamicLibrary.cpp.
36
37#ifdef LLVM_ON_WIN32
38
39#include "Win32/DynamicLibrary.inc"
40
41#else
42
Devang Patelaa4ea5a2008-02-13 17:11:39 +000043//#include "ltdl.h"
44#include <dlfcn.h>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045#include <cassert>
46using namespace llvm;
47using namespace llvm::sys;
48
49//===----------------------------------------------------------------------===//
50//=== WARNING: Implementation here must contain only TRULY operating system
51//=== independent code.
52//===----------------------------------------------------------------------===//
53
Devang Patelaa4ea5a2008-02-13 17:11:39 +000054//static std::vector<lt_dlhandle> OpenedHandles;
55static std::vector<void *> OpenedHandles;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056
Devang Patel79df1c62008-03-13 16:55:34 +000057DynamicLibrary::DynamicLibrary() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59DynamicLibrary::~DynamicLibrary() {
Devang Patelaa4ea5a2008-02-13 17:11:39 +000060 while(!OpenedHandles.empty()) {
61 void *H = OpenedHandles.back(); OpenedHandles.pop_back();
62 dlclose(H);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 }
64}
65
66bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
67 std::string *ErrMsg) {
Chris Lattner859cf632008-07-10 00:52:20 +000068 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
Devang Patelaa4ea5a2008-02-13 17:11:39 +000069 if (H == 0) {
Chris Lattneracc81932008-03-12 00:50:01 +000070 if (ErrMsg)
71 *ErrMsg = dlerror();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 return true;
73 }
Devang Patelaa4ea5a2008-02-13 17:11:39 +000074 OpenedHandles.push_back(H);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 return false;
76}
77
78void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Devang Patelaa4ea5a2008-02-13 17:11:39 +000079 // check_ltdl_initialization();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080
81 // First check symbols added via AddSymbol().
Chris Lattner4b57e5a2009-01-29 04:43:42 +000082 std::map<std::string, void *>::iterator I = g_symbols().find(symbolName);
83 if (I != g_symbols().end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 return I->second;
85
86 // Now search the libraries.
Devang Patelaa4ea5a2008-02-13 17:11:39 +000087 for (std::vector<void *>::iterator I = OpenedHandles.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 E = OpenedHandles.end(); I != E; ++I) {
Devang Patelaa4ea5a2008-02-13 17:11:39 +000089 //lt_ptr ptr = lt_dlsym(*I, symbolName);
90 void *ptr = dlsym(*I, symbolName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 if (ptr)
92 return ptr;
93 }
94
Anton Korobeynikov942cbab2007-12-03 05:30:41 +000095#define EXPLICIT_SYMBOL(SYM) \
96 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
97
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.
102
103#ifdef __APPLE__
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 {
105 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);
120 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121#endif
122
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000123#ifdef __CYGWIN__
124 {
125 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000126 EXPLICIT_SYMBOL(__main);
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000127 }
128#endif
129
130#undef EXPLICIT_SYMBOL
131
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