Allow LLI, in interpreter mode, to find stdin, stdout, and stderr. This is
[oota-llvm.git] / lib / System / DynamicLibrary.cpp
index 859092a52af2ac15b69b93d6cb23bc2a8b010ab7..f0ab51b0f6150b409e20cc9657809177c916b55e 100644 (file)
@@ -45,12 +45,11 @@ using namespace llvm::sys;
 //===          independent code.
 //===----------------------------------------------------------------------===//
 
-static bool did_initialize_ltdl = false;
-
 static inline void check_ltdl_initialization() {
+  static bool did_initialize_ltdl = false;
   if (!did_initialize_ltdl) {
-    if (0 != lt_dlinit())
-      throw std::string(lt_dlerror());
+    int Err = lt_dlinit();
+    assert(0 == Err && "Can't init the ltdl library");
     did_initialize_ltdl = true;
   }
 }
@@ -62,13 +61,13 @@ DynamicLibrary::DynamicLibrary() : handle(0) {
 
   lt_dlhandle a_handle = lt_dlopen(0);
 
-  if (a_handle == 0)
-    throw std::string("Can't open program as dynamic library");
+  assert(a_handle == 0 || "Can't open program as dynamic library");
 
   handle = a_handle;
   OpenedHandles.push_back(a_handle);
 }
 
+/*
 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
   check_ltdl_initialization();
 
@@ -83,6 +82,7 @@ DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
   handle = a_handle;
   OpenedHandles.push_back(a_handle);
 }
+*/
 
 DynamicLibrary::~DynamicLibrary() {
   lt_dlhandle a_handle = (lt_dlhandle) handle;
@@ -141,10 +141,11 @@ void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
   // important symbols are marked 'private external' which doesn't allow
   // SearchForAddressOfSymbol to find them.  As such, we special case them here,
   // there is only a small handful of them.
+
 #ifdef __APPLE__
-  {
 #define EXPLICIT_SYMBOL(SYM) \
    extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
+  {
     EXPLICIT_SYMBOL(__ashldi3);
     EXPLICIT_SYMBOL(__ashrdi3);
     EXPLICIT_SYMBOL(__cmpdi2);
@@ -160,9 +161,18 @@ void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
     EXPLICIT_SYMBOL(__moddi3);
     EXPLICIT_SYMBOL(__udivdi3);
     EXPLICIT_SYMBOL(__umoddi3);
-#undef EXPLICIT_SYMBOL
   }
+#undef EXPLICIT_SYMBOL
 #endif
+#define EXPLICIT_SYMBOL(SYM) \
+   if (!strcmp(symbolName, #SYM)) return &SYM
+  // Try a few well known symbols just to give lli a shot at working.
+  {
+    EXPLICIT_SYMBOL(stdin);
+    EXPLICIT_SYMBOL(stdout);
+    EXPLICIT_SYMBOL(stderr);
+  }
+#undef EXPLICIT_SYMBOL
 
   return 0;
 }