LLVM API Documentation

SearchForAddressOfSpecialSymbol.cpp
Go to the documentation of this file.
00001 //===- SearchForAddressOfSpecialSymbol.cpp - Function addresses -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file pulls the addresses of certain symbols out of the linker.  It must
00011 //  include as few header files as possible because it declares the symbols as
00012 //  void*, which would conflict with the actual symbol type if any header
00013 //  declared it.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include <string.h>
00018 
00019 // Must declare the symbols in the global namespace.
00020 static void *DoSearch(const char* symbolName) {
00021 #define EXPLICIT_SYMBOL(SYM) \
00022    extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
00023 
00024   // If this is darwin, it has some funky issues, try to solve them here.  Some
00025   // important symbols are marked 'private external' which doesn't allow
00026   // SearchForAddressOfSymbol to find them.  As such, we special case them here,
00027   // there is only a small handful of them.
00028 
00029 #ifdef __APPLE__
00030   {
00031     // __eprintf is sometimes used for assert() handling on x86.
00032     //
00033     // FIXME: Currently disabled when using Clang, as we don't always have our
00034     // runtime support libraries available.
00035 #ifndef __clang__
00036 #ifdef __i386__
00037     EXPLICIT_SYMBOL(__eprintf);
00038 #endif
00039 #endif
00040   }
00041 #endif
00042 
00043 #ifdef __CYGWIN__
00044   {
00045     EXPLICIT_SYMBOL(_alloca);
00046     EXPLICIT_SYMBOL(__main);
00047   }
00048 #endif
00049 
00050 #undef EXPLICIT_SYMBOL
00051   return nullptr;
00052 }
00053 
00054 namespace llvm {
00055 void *SearchForAddressOfSpecialSymbol(const char* symbolName) {
00056   return DoSearch(symbolName);
00057 }
00058 }  // namespace llvm