blob: 711760b2479840eef7fcfde3291a5c4f8c394238 [file] [log] [blame]
Jeffrey Yasskin0069d722010-03-11 06:14:32 +00001//===- SearchForAddressOfSpecialSymbol.cpp - Function addresses -*- 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 file pulls the addresses of certain symbols out of the linker. It must
11// include as few header files as possible because it declares the symbols as
12// void*, which would conflict with the actual symbol type if any header
13// declared it.
14//
15//===----------------------------------------------------------------------===//
16
Eugene Zelenko1760dc22016-04-05 20:19:49 +000017#include <cstring>
18
19namespace {
Jeffrey Yasskin0069d722010-03-11 06:14:32 +000020
21// Must declare the symbols in the global namespace.
Eugene Zelenko1760dc22016-04-05 20:19:49 +000022void *DoSearch(const char* symbolName) {
Jeffrey Yasskin0069d722010-03-11 06:14:32 +000023#define EXPLICIT_SYMBOL(SYM) \
24 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
25
26 // If this is darwin, it has some funky issues, try to solve them here. Some
27 // important symbols are marked 'private external' which doesn't allow
28 // SearchForAddressOfSymbol to find them. As such, we special case them here,
29 // there is only a small handful of them.
30
31#ifdef __APPLE__
32 {
Daniel Dunbarfe2a8962010-10-11 21:34:24 +000033 // __eprintf is sometimes used for assert() handling on x86.
Daniel Dunbar82ebae72010-12-07 16:29:44 +000034 //
35 // FIXME: Currently disabled when using Clang, as we don't always have our
36 // runtime support libraries available.
37#ifndef __clang__
Daniel Dunbarfe2a8962010-10-11 21:34:24 +000038#ifdef __i386__
39 EXPLICIT_SYMBOL(__eprintf);
40#endif
Daniel Dunbar82ebae72010-12-07 16:29:44 +000041#endif
Jeffrey Yasskin0069d722010-03-11 06:14:32 +000042 }
43#endif
44
45#ifdef __CYGWIN__
46 {
47 EXPLICIT_SYMBOL(_alloca);
48 EXPLICIT_SYMBOL(__main);
49 }
50#endif
51
52#undef EXPLICIT_SYMBOL
Craig Topperc10719f2014-04-07 04:17:22 +000053 return nullptr;
Jeffrey Yasskin0069d722010-03-11 06:14:32 +000054}
55
Eugene Zelenko1760dc22016-04-05 20:19:49 +000056} // end anonymous namespace
57
Jeffrey Yasskin0069d722010-03-11 06:14:32 +000058namespace llvm {
Eugene Zelenko1760dc22016-04-05 20:19:49 +000059
Jeffrey Yasskin0069d722010-03-11 06:14:32 +000060void *SearchForAddressOfSpecialSymbol(const char* symbolName) {
61 return DoSearch(symbolName);
62}
Eugene Zelenko1760dc22016-04-05 20:19:49 +000063
64} // end namespace llvm