[llvm-commits] [llvm] r129448 - /llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp

Jim Grosbach grosbach at apple.com
Wed Apr 13 08:49:40 PDT 2011


Author: grosbach
Date: Wed Apr 13 10:49:40 2011
New Revision: 129448

URL: http://llvm.org/viewvc/llvm-project?rev=129448&view=rev
Log:
Load multiple object files and link them via RuntimeDyld in llvm-rtdyld.

Relocations between the object modules are properly resolved, as in the
following trivial example:

$ cat t.c
int foo();
int main() {
    return foo();
}
$ cat foo.c
int foo() {
    return 65;
}
$ clang -c t.c -fno-asynchronous-unwind-tables
$ clang -c foo.c -fno-asynchronous-unwind-tables
$ llvm-rtdyld t.o foo.o ; echo $?
loaded '_main' at: 0x10015c000
65


Modified:
    llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp

Modified: llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp?rev=129448&r1=129447&r2=129448&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp (original)
+++ llvm/trunk/tools/llvm-rtdyld/llvm-rtdyld.cpp Wed Apr 13 10:49:40 2011
@@ -24,8 +24,9 @@
 using namespace llvm;
 using namespace llvm::object;
 
-static cl::opt<std::string>
-InputFile(cl::Positional, cl::desc("<input file>"), cl::init("-"));
+static cl::list<std::string>
+InputFileList(cl::Positional, cl::ZeroOrMore,
+              cl::desc("<input file>"));
 
 enum ActionType {
   AC_Execute
@@ -82,22 +83,31 @@
 /* *** */
 
 static int executeInput() {
-  // Load the input memory buffer.
-  OwningPtr<MemoryBuffer> InputBuffer;
-  if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer))
-    return Error("unable to read input: '" + ec.message() + "'");
-
   // Instantiate a dynamic linker.
   TrivialMemoryManager *MemMgr = new TrivialMemoryManager;
   RuntimeDyld Dyld(MemMgr);
 
-  // Load the object file into it.
-  if (Dyld.loadObject(InputBuffer.take())) {
-    return Error(Dyld.getErrorString());
+  // If we don't have any input files, read from stdin.
+  if (!InputFileList.size())
+    InputFileList.push_back("-");
+  for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) {
+    // Load the input memory buffer.
+    OwningPtr<MemoryBuffer> InputBuffer;
+    if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFileList[i],
+                                                     InputBuffer))
+      return Error("unable to read input: '" + ec.message() + "'");
+
+    // Load the object file into it.
+    if (Dyld.loadObject(InputBuffer.take())) {
+      return Error(Dyld.getErrorString());
+    }
   }
+
   // Resolve all the relocations we can.
   Dyld.resolveRelocations();
 
+  // FIXME: Error out if there are unresolved relocations.
+
   // Get the address of the entry point (_main by default).
   void *MainAddress = Dyld.getSymbolAddress(EntryPoint);
   if (MainAddress == 0)
@@ -113,14 +123,14 @@
       return Error("unable to mark function executable: '" + ErrorStr + "'");
   }
 
-
   // Dispatch to _main().
-  errs() << "loaded '_main' at: " << (void*)MainAddress << "\n";
+  errs() << "loaded '" << EntryPoint << "' at: " << (void*)MainAddress << "\n";
 
   int (*Main)(int, const char**) =
     (int(*)(int,const char**)) uintptr_t(MainAddress);
   const char **Argv = new const char*[2];
-  Argv[0] = InputFile.c_str();
+  // Use the name of the first input object module as argv[0] for the target.
+  Argv[0] = InputFileList[0].c_str();
   Argv[1] = 0;
   return Main(1, Argv);
 }





More information about the llvm-commits mailing list