Remove another memory leak from ABCD by using Edges by value instead of
[oota-llvm.git] / lib / System / DynamicLibrary.cpp
1 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file implements the operating system DynamicLibrary concept.
11 //
12 // FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
13 // not thread safe!
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/System/DynamicLibrary.h"
18 #include "llvm/Config/config.h"
19 #include <cstdio>
20 #include <cstring>
21 #include <map>
22 #include <vector>
23
24 // Collection of symbol name/value pairs to be searched prior to any libraries.
25 static std::map<std::string, void*> *ExplicitSymbols = 0;
26
27 static struct ExplicitSymbolsDeleter {
28   ~ExplicitSymbolsDeleter() {
29     if (ExplicitSymbols)
30       delete ExplicitSymbols;
31   }
32 } Dummy;
33
34 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
35                                           void *symbolValue) {
36   if (ExplicitSymbols == 0)
37     ExplicitSymbols = new std::map<std::string, void*>();
38   (*ExplicitSymbols)[symbolName] = symbolValue;
39 }
40
41 #ifdef LLVM_ON_WIN32
42
43 #include "Win32/DynamicLibrary.inc"
44
45 #else
46
47 #include <dlfcn.h>
48 using namespace llvm;
49 using namespace llvm::sys;
50
51 //===----------------------------------------------------------------------===//
52 //=== WARNING: Implementation here must contain only TRULY operating system
53 //===          independent code.
54 //===----------------------------------------------------------------------===//
55
56 static std::vector<void *> *OpenedHandles = 0;
57
58
59 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
60                                             std::string *ErrMsg) {
61   void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
62   if (H == 0) {
63     if (ErrMsg) *ErrMsg = dlerror();
64     return true;
65   }
66   if (OpenedHandles == 0)
67     OpenedHandles = new std::vector<void *>();
68   OpenedHandles->push_back(H);
69   return false;
70 }
71
72 namespace llvm {
73 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
74 }
75
76 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
77   // First check symbols added via AddSymbol().
78   if (ExplicitSymbols) {
79     std::map<std::string, void *>::iterator I =
80       ExplicitSymbols->find(symbolName);
81     std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
82   
83     if (I != E)
84       return I->second;
85   }
86
87   // Now search the libraries.
88   if (OpenedHandles) {
89     for (std::vector<void *>::iterator I = OpenedHandles->begin(),
90          E = OpenedHandles->end(); I != E; ++I) {
91       //lt_ptr ptr = lt_dlsym(*I, symbolName);
92       void *ptr = dlsym(*I, symbolName);
93       if (ptr) {
94         return ptr;
95       }
96     }
97   }
98
99   if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
100     return Result;
101
102 // This macro returns the address of a well-known, explicit symbol
103 #define EXPLICIT_SYMBOL(SYM) \
104    if (!strcmp(symbolName, #SYM)) return &SYM
105
106 // On linux we have a weird situation. The stderr/out/in symbols are both
107 // macros and global variables because of standards requirements. So, we 
108 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
109 #if defined(__linux__)
110   {
111     EXPLICIT_SYMBOL(stderr);
112     EXPLICIT_SYMBOL(stdout);
113     EXPLICIT_SYMBOL(stdin);
114   }
115 #else
116   // For everything else, we want to check to make sure the symbol isn't defined
117   // as a macro before using EXPLICIT_SYMBOL.
118   {
119 #ifndef stdin
120     EXPLICIT_SYMBOL(stdin);
121 #endif
122 #ifndef stdout
123     EXPLICIT_SYMBOL(stdout);
124 #endif
125 #ifndef stderr
126     EXPLICIT_SYMBOL(stderr);
127 #endif
128   }
129 #endif
130 #undef EXPLICIT_SYMBOL
131
132   return 0;
133 }
134
135 #endif // LLVM_ON_WIN32