blob: 3da50a28b656b97e12fe768c2d6ce3d20ee2a2f3 [file] [log] [blame]
Reid Spencer0de02a62004-11-18 04:33:39 +00001//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencer0de02a62004-11-18 04:33:39 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencer0de02a62004-11-18 04:33:39 +00008//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system DynamicLibrary concept.
11//
Chris Lattnera9b7d602009-07-07 18:17:07 +000012// FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
13// not thread safe!
14//
Reid Spencer0de02a62004-11-18 04:33:39 +000015//===----------------------------------------------------------------------===//
16
17#include "llvm/System/DynamicLibrary.h"
Owen Anderson9ac18d52010-11-09 00:36:06 +000018#include "llvm/System/Mutex.h"
Jeff Cohena4c97512004-12-24 16:26:47 +000019#include "llvm/Config/config.h"
Duncan Sands4520dd22008-10-08 07:23:46 +000020#include <cstdio>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000021#include <cstring>
Jeff Cohen85046902006-01-30 04:33:51 +000022#include <map>
Chris Lattner72501b42009-07-07 18:01:58 +000023#include <vector>
Jeff Cohen85046902006-01-30 04:33:51 +000024
25// Collection of symbol name/value pairs to be searched prior to any libraries.
Chris Lattnera9b7d602009-07-07 18:17:07 +000026static std::map<std::string, void*> *ExplicitSymbols = 0;
Owen Andersonf37feb92009-06-25 18:12:44 +000027
Dan Gohmanb3579832010-04-15 17:08:50 +000028namespace {
29
30struct ExplicitSymbolsDeleter {
Torok Edwin72ddf7b2009-08-31 16:12:29 +000031 ~ExplicitSymbolsDeleter() {
32 if (ExplicitSymbols)
33 delete ExplicitSymbols;
34 }
Dan Gohmanb3579832010-04-15 17:08:50 +000035};
36
37}
38
39static ExplicitSymbolsDeleter Dummy;
Torok Edwin72ddf7b2009-08-31 16:12:29 +000040
Chris Lattneradcbce02006-07-07 17:12:36 +000041void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
42 void *symbolValue) {
Chris Lattnera9b7d602009-07-07 18:17:07 +000043 if (ExplicitSymbols == 0)
44 ExplicitSymbols = new std::map<std::string, void*>();
45 (*ExplicitSymbols)[symbolName] = symbolValue;
Jeff Cohen85046902006-01-30 04:33:51 +000046}
Jeff Cohen1a466352004-12-24 07:57:09 +000047
Jeff Cohena4c97512004-12-24 16:26:47 +000048#ifdef LLVM_ON_WIN32
Jeff Cohen1a466352004-12-24 07:57:09 +000049
Reid Spencerbccc8ab2005-01-09 23:29:00 +000050#include "Win32/DynamicLibrary.inc"
Jeff Cohen1a466352004-12-24 07:57:09 +000051
52#else
53
Chris Lattner21aa3472010-04-09 20:45:04 +000054#if HAVE_DLFCN_H
Devang Patele45252e2008-02-13 17:11:39 +000055#include <dlfcn.h>
Chris Lattner28dabf72004-12-03 23:02:42 +000056using namespace llvm;
57using namespace llvm::sys;
Reid Spencer0de02a62004-11-18 04:33:39 +000058
59//===----------------------------------------------------------------------===//
60//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000061//=== independent code.
Reid Spencer0de02a62004-11-18 04:33:39 +000062//===----------------------------------------------------------------------===//
63
Owen Anderson9ac18d52010-11-09 00:36:06 +000064static SmartMutex<true> HandlesMutex;
Chris Lattnera9b7d602009-07-07 18:17:07 +000065static std::vector<void *> *OpenedHandles = 0;
Reid Spencer19cd4a92004-11-29 13:33:28 +000066
Reid Spencer19cd4a92004-11-29 13:33:28 +000067
Chris Lattneradcbce02006-07-07 17:12:36 +000068bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
69 std::string *ErrMsg) {
Chris Lattnerae55e042008-07-10 00:52:20 +000070 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
Devang Patele45252e2008-02-13 17:11:39 +000071 if (H == 0) {
Chris Lattnera9b7d602009-07-07 18:17:07 +000072 if (ErrMsg) *ErrMsg = dlerror();
Chris Lattneradcbce02006-07-07 17:12:36 +000073 return true;
74 }
Chris Lattner6ccd0da2010-08-17 15:42:43 +000075#ifdef __CYGWIN__
76 // Cygwin searches symbols only in the main
77 // with the handle of dlopen(NULL, RTLD_GLOBAL).
78 if (Filename == NULL)
79 H = RTLD_DEFAULT;
80#endif
Owen Anderson9ac18d52010-11-09 00:36:06 +000081 SmartScopedLock<true> Lock(HandlesMutex);
Chris Lattnera9b7d602009-07-07 18:17:07 +000082 if (OpenedHandles == 0)
83 OpenedHandles = new std::vector<void *>();
84 OpenedHandles->push_back(H);
Chris Lattneradcbce02006-07-07 17:12:36 +000085 return false;
Reid Spencer19cd4a92004-11-29 13:33:28 +000086}
Chris Lattner21aa3472010-04-09 20:45:04 +000087#else
88
89using namespace llvm;
90using namespace llvm::sys;
91
92bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
93 std::string *ErrMsg) {
94 if (ErrMsg) *ErrMsg = "dlopen() not supported on this platform";
95 return true;
96}
97#endif
Reid Spencer19cd4a92004-11-29 13:33:28 +000098
Jeffrey Yasskin62207542010-03-11 06:14:32 +000099namespace llvm {
100void *SearchForAddressOfSpecialSymbol(const char* symbolName);
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000101}
102
103void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
104 // First check symbols added via AddSymbol().
105 if (ExplicitSymbols) {
106 std::map<std::string, void *>::iterator I =
107 ExplicitSymbols->find(symbolName);
108 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
109
110 if (I != E)
111 return I->second;
112 }
113
Chris Lattner21aa3472010-04-09 20:45:04 +0000114#if HAVE_DLFCN_H
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000115 // Now search the libraries.
Owen Anderson9ac18d52010-11-09 00:36:06 +0000116 SmartScopedLock<true> Lock(HandlesMutex);
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000117 if (OpenedHandles) {
118 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
119 E = OpenedHandles->end(); I != E; ++I) {
120 //lt_ptr ptr = lt_dlsym(*I, symbolName);
121 void *ptr = dlsym(*I, symbolName);
122 if (ptr) {
123 return ptr;
124 }
125 }
126 }
Chris Lattner21aa3472010-04-09 20:45:04 +0000127#endif
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000128
Jeffrey Yasskin62207542010-03-11 06:14:32 +0000129 if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
Douglas Gregor37b7bae2009-12-23 19:12:50 +0000130 return Result;
Anton Korobeynikov96ea2092007-12-03 05:30:41 +0000131
Reid Spencer02f20d32007-01-19 21:41:04 +0000132// This macro returns the address of a well-known, explicit symbol
Reid Spencer11f457a2007-01-10 19:50:43 +0000133#define EXPLICIT_SYMBOL(SYM) \
134 if (!strcmp(symbolName, #SYM)) return &SYM
Reid Spencer02f20d32007-01-19 21:41:04 +0000135
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.
Reid Spencer11f457a2007-01-10 19:50:43 +0000148 {
Reid Spencer65de7422007-01-11 00:35:10 +0000149#ifndef stdin
Reid Spencer11f457a2007-01-10 19:50:43 +0000150 EXPLICIT_SYMBOL(stdin);
Reid Spencer81e39542007-01-19 21:30:39 +0000151#endif
152#ifndef stdout
Reid Spencer11f457a2007-01-10 19:50:43 +0000153 EXPLICIT_SYMBOL(stdout);
Reid Spencer81e39542007-01-19 21:30:39 +0000154#endif
155#ifndef stderr
Reid Spencer11f457a2007-01-10 19:50:43 +0000156 EXPLICIT_SYMBOL(stderr);
Reid Spencer65de7422007-01-11 00:35:10 +0000157#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000158 }
Reid Spencer02f20d32007-01-19 21:41:04 +0000159#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000160#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000161
Reid Spencer19cd4a92004-11-29 13:33:28 +0000162 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000163}
164
Jeff Cohena4c97512004-12-24 16:26:47 +0000165#endif // LLVM_ON_WIN32