blob: 970266f3438768e09c4a505725e116389d336e6d [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//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/System/DynamicLibrary.h"
Jeff Cohena4c97512004-12-24 16:26:47 +000015#include "llvm/Config/config.h"
Duncan Sands4520dd22008-10-08 07:23:46 +000016#include <cstdio>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000017#include <cstring>
Jeff Cohen85046902006-01-30 04:33:51 +000018#include <map>
19
20// Collection of symbol name/value pairs to be searched prior to any libraries.
21static std::map<std::string, void *> g_symbols;
22
Chris Lattneradcbce02006-07-07 17:12:36 +000023void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
24 void *symbolValue) {
Jeff Cohen85046902006-01-30 04:33:51 +000025 g_symbols[symbolName] = symbolValue;
26}
Jeff Cohen1a466352004-12-24 07:57:09 +000027
28// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
29// license and special exception would cause all of LLVM to be placed under
30// the LGPL. This is because the exception applies only when libtool is
31// used, and obviously libtool is not used with Visual Studio. An entirely
32// separate implementation is provided in win32/DynamicLibrary.cpp.
33
Jeff Cohena4c97512004-12-24 16:26:47 +000034#ifdef LLVM_ON_WIN32
Jeff Cohen1a466352004-12-24 07:57:09 +000035
Reid Spencerbccc8ab2005-01-09 23:29:00 +000036#include "Win32/DynamicLibrary.inc"
Jeff Cohen1a466352004-12-24 07:57:09 +000037
38#else
39
Devang Patele45252e2008-02-13 17:11:39 +000040//#include "ltdl.h"
41#include <dlfcn.h>
Reid Spencer0de02a62004-11-18 04:33:39 +000042#include <cassert>
Chris Lattner28dabf72004-12-03 23:02:42 +000043using namespace llvm;
44using namespace llvm::sys;
Reid Spencer0de02a62004-11-18 04:33:39 +000045
46//===----------------------------------------------------------------------===//
47//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000048//=== independent code.
Reid Spencer0de02a62004-11-18 04:33:39 +000049//===----------------------------------------------------------------------===//
50
Devang Patele45252e2008-02-13 17:11:39 +000051//static std::vector<lt_dlhandle> OpenedHandles;
52static std::vector<void *> OpenedHandles;
Reid Spencer19cd4a92004-11-29 13:33:28 +000053
Devang Patela1334172008-03-13 16:55:34 +000054DynamicLibrary::DynamicLibrary() {}
Reid Spencer0de02a62004-11-18 04:33:39 +000055
56DynamicLibrary::~DynamicLibrary() {
Devang Patele45252e2008-02-13 17:11:39 +000057 while(!OpenedHandles.empty()) {
58 void *H = OpenedHandles.back(); OpenedHandles.pop_back();
59 dlclose(H);
Reid Spencer19cd4a92004-11-29 13:33:28 +000060 }
61}
62
Chris Lattneradcbce02006-07-07 17:12:36 +000063bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
64 std::string *ErrMsg) {
Chris Lattnerae55e042008-07-10 00:52:20 +000065 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
Devang Patele45252e2008-02-13 17:11:39 +000066 if (H == 0) {
Chris Lattnerd5f16272008-03-12 00:50:01 +000067 if (ErrMsg)
68 *ErrMsg = dlerror();
Chris Lattneradcbce02006-07-07 17:12:36 +000069 return true;
70 }
Devang Patele45252e2008-02-13 17:11:39 +000071 OpenedHandles.push_back(H);
Chris Lattneradcbce02006-07-07 17:12:36 +000072 return false;
Reid Spencer19cd4a92004-11-29 13:33:28 +000073}
74
75void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Devang Patele45252e2008-02-13 17:11:39 +000076 // check_ltdl_initialization();
Jeff Cohen85046902006-01-30 04:33:51 +000077
78 // First check symbols added via AddSymbol().
79 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
80 if (I != g_symbols.end())
81 return I->second;
82
83 // Now search the libraries.
Devang Patele45252e2008-02-13 17:11:39 +000084 for (std::vector<void *>::iterator I = OpenedHandles.begin(),
Reid Spencer19cd4a92004-11-29 13:33:28 +000085 E = OpenedHandles.end(); I != E; ++I) {
Devang Patele45252e2008-02-13 17:11:39 +000086 //lt_ptr ptr = lt_dlsym(*I, symbolName);
87 void *ptr = dlsym(*I, symbolName);
Reid Spencer19cd4a92004-11-29 13:33:28 +000088 if (ptr)
89 return ptr;
90 }
Chris Lattner28dabf72004-12-03 23:02:42 +000091
Anton Korobeynikov96ea2092007-12-03 05:30:41 +000092#define EXPLICIT_SYMBOL(SYM) \
93 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
94
Chris Lattner28dabf72004-12-03 23:02:42 +000095 // If this is darwin, it has some funky issues, try to solve them here. Some
96 // important symbols are marked 'private external' which doesn't allow
97 // SearchForAddressOfSymbol to find them. As such, we special case them here,
98 // there is only a small handful of them.
Reid Spencer11f457a2007-01-10 19:50:43 +000099
Chris Lattner28dabf72004-12-03 23:02:42 +0000100#ifdef __APPLE__
Reid Spencer11f457a2007-01-10 19:50:43 +0000101 {
Chris Lattner368cb8e2004-12-04 04:17:20 +0000102 EXPLICIT_SYMBOL(__ashldi3);
103 EXPLICIT_SYMBOL(__ashrdi3);
104 EXPLICIT_SYMBOL(__cmpdi2);
105 EXPLICIT_SYMBOL(__divdi3);
106 EXPLICIT_SYMBOL(__eprintf);
107 EXPLICIT_SYMBOL(__fixdfdi);
108 EXPLICIT_SYMBOL(__fixsfdi);
109 EXPLICIT_SYMBOL(__fixunsdfdi);
110 EXPLICIT_SYMBOL(__fixunssfdi);
111 EXPLICIT_SYMBOL(__floatdidf);
112 EXPLICIT_SYMBOL(__floatdisf);
113 EXPLICIT_SYMBOL(__lshrdi3);
114 EXPLICIT_SYMBOL(__moddi3);
115 EXPLICIT_SYMBOL(__udivdi3);
116 EXPLICIT_SYMBOL(__umoddi3);
Chris Lattner28dabf72004-12-03 23:02:42 +0000117 }
118#endif
Reid Spencer02f20d32007-01-19 21:41:04 +0000119
Anton Korobeynikov96ea2092007-12-03 05:30:41 +0000120#ifdef __CYGWIN__
121 {
122 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov47ccf1a2008-02-22 10:08:31 +0000123 EXPLICIT_SYMBOL(__main);
Anton Korobeynikov96ea2092007-12-03 05:30:41 +0000124 }
125#endif
126
127#undef EXPLICIT_SYMBOL
128
Reid Spencer02f20d32007-01-19 21:41:04 +0000129// This macro returns the address of a well-known, explicit symbol
Reid Spencer11f457a2007-01-10 19:50:43 +0000130#define EXPLICIT_SYMBOL(SYM) \
131 if (!strcmp(symbolName, #SYM)) return &SYM
Reid Spencer02f20d32007-01-19 21:41:04 +0000132
133// On linux we have a weird situation. The stderr/out/in symbols are both
134// macros and global variables because of standards requirements. So, we
135// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
136#if defined(__linux__)
137 {
138 EXPLICIT_SYMBOL(stderr);
139 EXPLICIT_SYMBOL(stdout);
140 EXPLICIT_SYMBOL(stdin);
141 }
142#else
143 // For everything else, we want to check to make sure the symbol isn't defined
144 // as a macro before using EXPLICIT_SYMBOL.
Reid Spencer11f457a2007-01-10 19:50:43 +0000145 {
Reid Spencer65de7422007-01-11 00:35:10 +0000146#ifndef stdin
Reid Spencer11f457a2007-01-10 19:50:43 +0000147 EXPLICIT_SYMBOL(stdin);
Reid Spencer81e39542007-01-19 21:30:39 +0000148#endif
149#ifndef stdout
Reid Spencer11f457a2007-01-10 19:50:43 +0000150 EXPLICIT_SYMBOL(stdout);
Reid Spencer81e39542007-01-19 21:30:39 +0000151#endif
152#ifndef stderr
Reid Spencer11f457a2007-01-10 19:50:43 +0000153 EXPLICIT_SYMBOL(stderr);
Reid Spencer65de7422007-01-11 00:35:10 +0000154#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000155 }
Reid Spencer02f20d32007-01-19 21:41:04 +0000156#endif
Reid Spencer11f457a2007-01-10 19:50:43 +0000157#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000158
Reid Spencer19cd4a92004-11-29 13:33:28 +0000159 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000160}
161
Jeff Cohena4c97512004-12-24 16:26:47 +0000162#endif // LLVM_ON_WIN32