Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/Unix/DynamicLibrary.inc
Line
Count
Source (jump to first uncovered line)
1
//===- Unix/DynamicLibrary.cpp - Unix DL Implementation ---------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file provides the UNIX specific implementation of DynamicLibrary.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
14
#include <dlfcn.h>
15
16
166
DynamicLibrary::HandleSet::~HandleSet() {
17
166
  // Close the libraries in reverse order.
18
166
  for (void *Handle : llvm::reverse(Handles))
19
121
    ::dlclose(Handle);
20
166
  if (Process)
21
47
    ::dlclose(Process);
22
166
23
166
  // llvm_shutdown called, Return to default
24
166
  DynamicLibrary::SearchOrder = DynamicLibrary::SO_Linker;
25
166
}
26
27
532
void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
28
532
  void *Handle = ::dlopen(File, RTLD_LAZY|RTLD_GLOBAL);
29
532
  if (!Handle) {
30
0
    if (Err) *Err = ::dlerror();
31
0
    return &DynamicLibrary::Invalid;
32
0
  }
33
532
34
#ifdef __CYGWIN__
35
  // Cygwin searches symbols only in the main
36
  // with the handle of dlopen(NULL, RTLD_GLOBAL).
37
  if (!File)
38
    Handle = RTLD_DEFAULT;
39
#endif
40
41
532
  return Handle;
42
532
}
43
44
141
void DynamicLibrary::HandleSet::DLClose(void *Handle) {
45
141
  ::dlclose(Handle);
46
141
}
47
48
296
void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
49
296
  return ::dlsym(Handle, Symbol);
50
296
}
51
52
#else // !HAVE_DLOPEN
53
54
DynamicLibrary::HandleSet::~HandleSet() {}
55
56
void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
57
  if (Err) *Err = "dlopen() not supported on this platform";
58
  return &Invalid;
59
}
60
61
void DynamicLibrary::HandleSet::DLClose(void *Handle) {
62
}
63
64
void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
65
  return nullptr;
66
}
67
68
#endif
69
70
// Must declare the symbols in the global namespace.
71
22
static void *DoSearch(const char* SymbolName) {
72
22
#define EXPLICIT_SYMBOL(SYM) \
73
22
   extern void *SYM; if (!strcmp(SymbolName, #SYM)) return (void*)&SYM
74
22
75
22
  // If this is darwin, it has some funky issues, try to solve them here.  Some
76
22
  // important symbols are marked 'private external' which doesn't allow
77
22
  // SearchForAddressOfSymbol to find them.  As such, we special case them here,
78
22
  // there is only a small handful of them.
79
22
80
22
#ifdef __APPLE__
81
22
  {
82
22
    // __eprintf is sometimes used for assert() handling on x86.
83
22
    //
84
22
    // FIXME: Currently disabled when using Clang, as we don't always have our
85
22
    // runtime support libraries available.
86
#ifndef __clang__
87
#ifdef __i386__
88
    EXPLICIT_SYMBOL(__eprintf);
89
#endif
90
#endif
91
  }
92
22
#endif
93
22
94
#ifdef __CYGWIN__
95
  {
96
    EXPLICIT_SYMBOL(_alloca);
97
    EXPLICIT_SYMBOL(__main);
98
  }
99
#endif
100
101
22
#undef EXPLICIT_SYMBOL
102
22
103
22
// This macro returns the address of a well-known, explicit symbol
104
22
#define EXPLICIT_SYMBOL(SYM) \
105
22
   if (!strcmp(SymbolName, #SYM)) return &SYM
106
22
107
22
// Under glibc we have a weird situation. The stderr/out/in symbols are both
108
22
// macros and global variables because of standards requirements. So, we
109
22
// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
110
#if defined(__GLIBC__)
111
  {
112
    EXPLICIT_SYMBOL(stderr);
113
    EXPLICIT_SYMBOL(stdout);
114
    EXPLICIT_SYMBOL(stdin);
115
  }
116
#else
117
  // For everything else, we want to check to make sure the symbol isn't defined
118
22
  // as a macro before using EXPLICIT_SYMBOL.
119
22
  {
120
#ifndef stdin
121
    EXPLICIT_SYMBOL(stdin);
122
#endif
123
#ifndef stdout
124
    EXPLICIT_SYMBOL(stdout);
125
#endif
126
#ifndef stderr
127
    EXPLICIT_SYMBOL(stderr);
128
#endif
129
  }
130
22
#endif
131
22
#undef EXPLICIT_SYMBOL
132
22
133
22
  return nullptr;
134
22
}