xref: /aosp_15_r20/external/llvm/lib/Support/SearchForAddressOfSpecialSymbol.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- SearchForAddressOfSpecialSymbol.cpp - Function addresses -*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker //  This file pulls the addresses of certain symbols out of the linker.  It must
11*9880d681SAndroid Build Coastguard Worker //  include as few header files as possible because it declares the symbols as
12*9880d681SAndroid Build Coastguard Worker //  void*, which would conflict with the actual symbol type if any header
13*9880d681SAndroid Build Coastguard Worker //  declared it.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include <string.h>
18*9880d681SAndroid Build Coastguard Worker 
19*9880d681SAndroid Build Coastguard Worker // Must declare the symbols in the global namespace.
DoSearch(const char * symbolName)20*9880d681SAndroid Build Coastguard Worker static void *DoSearch(const char* symbolName) {
21*9880d681SAndroid Build Coastguard Worker #define EXPLICIT_SYMBOL(SYM) \
22*9880d681SAndroid Build Coastguard Worker    extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
23*9880d681SAndroid Build Coastguard Worker 
24*9880d681SAndroid Build Coastguard Worker   // If this is darwin, it has some funky issues, try to solve them here.  Some
25*9880d681SAndroid Build Coastguard Worker   // important symbols are marked 'private external' which doesn't allow
26*9880d681SAndroid Build Coastguard Worker   // SearchForAddressOfSymbol to find them.  As such, we special case them here,
27*9880d681SAndroid Build Coastguard Worker   // there is only a small handful of them.
28*9880d681SAndroid Build Coastguard Worker 
29*9880d681SAndroid Build Coastguard Worker #ifdef __APPLE__
30*9880d681SAndroid Build Coastguard Worker   {
31*9880d681SAndroid Build Coastguard Worker     // __eprintf is sometimes used for assert() handling on x86.
32*9880d681SAndroid Build Coastguard Worker     //
33*9880d681SAndroid Build Coastguard Worker     // FIXME: Currently disabled when using Clang, as we don't always have our
34*9880d681SAndroid Build Coastguard Worker     // runtime support libraries available.
35*9880d681SAndroid Build Coastguard Worker #ifndef __clang__
36*9880d681SAndroid Build Coastguard Worker #ifdef __i386__
37*9880d681SAndroid Build Coastguard Worker     EXPLICIT_SYMBOL(__eprintf);
38*9880d681SAndroid Build Coastguard Worker #endif
39*9880d681SAndroid Build Coastguard Worker #endif
40*9880d681SAndroid Build Coastguard Worker   }
41*9880d681SAndroid Build Coastguard Worker #endif
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker #ifdef __CYGWIN__
44*9880d681SAndroid Build Coastguard Worker   {
45*9880d681SAndroid Build Coastguard Worker     EXPLICIT_SYMBOL(_alloca);
46*9880d681SAndroid Build Coastguard Worker     EXPLICIT_SYMBOL(__main);
47*9880d681SAndroid Build Coastguard Worker   }
48*9880d681SAndroid Build Coastguard Worker #endif
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker #undef EXPLICIT_SYMBOL
51*9880d681SAndroid Build Coastguard Worker   return nullptr;
52*9880d681SAndroid Build Coastguard Worker }
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker namespace llvm {
SearchForAddressOfSpecialSymbol(const char * symbolName)55*9880d681SAndroid Build Coastguard Worker void *SearchForAddressOfSpecialSymbol(const char* symbolName) {
56*9880d681SAndroid Build Coastguard Worker   return DoSearch(symbolName);
57*9880d681SAndroid Build Coastguard Worker }
58*9880d681SAndroid Build Coastguard Worker }  // namespace llvm
59