[llvm-commits] [llvm] r125128 - in /llvm/trunk: include/llvm-c/lto.h include/llvm/Support/MemoryBuffer.h lib/Support/MemoryBuffer.cpp tools/gold/gold-plugin.cpp tools/lto/LTOModule.cpp tools/lto/LTOModule.h tools/lto/lto.cpp tools/lto/lto.exports

Rafael Espindola rafael.espindola at gmail.com
Tue Feb 8 14:40:47 PST 2011


Author: rafael
Date: Tue Feb  8 16:40:47 2011
New Revision: 125128

URL: http://llvm.org/viewvc/llvm-project?rev=125128&view=rev
Log:
Don't open the file again in the gold plugin. To be able to do this, update
MemoryBuffer::getOpenFile to not close the file descriptor.

Modified:
    llvm/trunk/include/llvm-c/lto.h
    llvm/trunk/include/llvm/Support/MemoryBuffer.h
    llvm/trunk/lib/Support/MemoryBuffer.cpp
    llvm/trunk/tools/gold/gold-plugin.cpp
    llvm/trunk/tools/lto/LTOModule.cpp
    llvm/trunk/tools/lto/LTOModule.h
    llvm/trunk/tools/lto/lto.cpp
    llvm/trunk/tools/lto/lto.exports

Modified: llvm/trunk/include/llvm-c/lto.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/lto.h?rev=125128&r1=125127&r2=125128&view=diff
==============================================================================
--- llvm/trunk/include/llvm-c/lto.h (original)
+++ llvm/trunk/include/llvm-c/lto.h Tue Feb  8 16:40:47 2011
@@ -18,6 +18,7 @@
 
 #include <stdbool.h>
 #include <stddef.h>
+#include <unistd.h>
 
 #define LTO_API_VERSION 4
 
@@ -121,6 +122,13 @@
 extern lto_module_t
 lto_module_create_from_memory(const void* mem, size_t length);
 
+/**
+ * Loads an object file from disk. The seek point of fd is not preserved.
+ * Returns NULL on error (check lto_get_error_message() for details).
+ */
+extern lto_module_t
+lto_module_create_from_fd(int fd, const char *path, off_t size);
+
 
 /**
  * Frees all memory internally allocated by the module.

Modified: llvm/trunk/include/llvm/Support/MemoryBuffer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MemoryBuffer.h?rev=125128&r1=125127&r2=125128&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MemoryBuffer.h (original)
+++ llvm/trunk/include/llvm/Support/MemoryBuffer.h Tue Feb  8 16:40:47 2011
@@ -69,8 +69,7 @@
                             int64_t FileSize = -1);
 
   /// getOpenFile - Given an already-open file descriptor, read the file and
-  /// return a MemoryBuffer.  This takes ownership of the descriptor,
-  /// immediately closing it after reading the file.
+  /// return a MemoryBuffer.
   static error_code getOpenFile(int FD, const char *Filename,
                                 OwningPtr<MemoryBuffer> &result,
                                 int64_t FileSize = -1);

Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=125128&r1=125127&r2=125128&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Tue Feb  8 16:40:47 2011
@@ -179,14 +179,6 @@
     sys::Path::UnMapFilePages(getBufferStart(), getBufferSize());
   }
 };
-
-/// FileCloser - RAII object to make sure an FD gets closed properly.
-class FileCloser {
-  int FD;
-public:
-  explicit FileCloser(int FD) : FD(FD) {}
-  ~FileCloser() { ::close(FD); }
-};
 }
 
 error_code MemoryBuffer::getFile(StringRef Filename,
@@ -208,15 +200,14 @@
   if (FD == -1) {
     return error_code(errno, posix_category());
   }
-
-  return getOpenFile(FD, Filename, result, FileSize);
+  error_code ret = getOpenFile(FD, Filename, result, FileSize);
+  close(FD);
+  return ret;
 }
 
 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
                                      OwningPtr<MemoryBuffer> &result,
                                      int64_t FileSize) {
-  FileCloser FC(FD); // Close FD on return.
-
   // If we don't know the file size, use fstat to find out.  fstat on an open
   // file descriptor is cheaper than stat on a random path.
   if (FileSize == -1) {

Modified: llvm/trunk/tools/gold/gold-plugin.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/gold/gold-plugin.cpp?rev=125128&r1=125127&r2=125128&view=diff
==============================================================================
--- llvm/trunk/tools/gold/gold-plugin.cpp (original)
+++ llvm/trunk/tools/gold/gold-plugin.cpp Tue Feb  8 16:40:47 2011
@@ -241,7 +241,8 @@
 /// with add_symbol if possible.
 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
                                         int *claimed) {
-  void *buf = NULL;
+  lto_module_t M;
+
   if (file->offset) {
     // Gold has found what might be IR part-way inside of a file, such as
     // an .a archive.
@@ -252,7 +253,7 @@
                  file->offset, sys::StrError(errno).c_str());
       return LDPS_ERR;
     }
-    buf = malloc(file->filesize);
+    void *buf = malloc(file->filesize);
     if (!buf) {
       (*message)(LDPL_ERROR,
                  "Failed to allocate buffer for archive member of size: %d\n",
@@ -272,16 +273,31 @@
       free(buf);
       return LDPS_OK;
     }
-  } else if (!lto_module_is_object_file(file->name))
-    return LDPS_OK;
+    M = lto_module_create_from_memory(buf, file->filesize);
+    free(buf);
+  } else {
+    // FIXME: We should not need to pass -1 as the file size, but there
+    // is a bug in BFD that causes it to pass 0 to us. Remove this once
+    // that is fixed.
+    off_t size = file->filesize ? file->filesize : -1;
+
+    // FIXME: We should not need to reset the position in the file, but there
+    // is a bug in BFD. Remove this once that is fixed.
+    off_t old_pos = lseek(file->fd, 0, SEEK_CUR);
+
+    lseek(file->fd, 0, SEEK_SET);
+    M = lto_module_create_from_fd(file->fd, file->name, size);
+
+    lseek(file->fd, old_pos, SEEK_SET);
+    if (!M)
+      return LDPS_OK;
+  }
 
   *claimed = 1;
   Modules.resize(Modules.size() + 1);
   claimed_file &cf = Modules.back();
+  cf.M = M;
 
-  cf.M = buf ? lto_module_create_from_memory(buf, file->filesize) :
-               lto_module_create(file->name);
-  free(buf);
   if (!cf.M) {
     (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
                lto_get_error_message());

Modified: llvm/trunk/tools/lto/LTOModule.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/LTOModule.cpp?rev=125128&r1=125127&r2=125128&view=diff
==============================================================================
--- llvm/trunk/tools/lto/LTOModule.cpp (original)
+++ llvm/trunk/tools/lto/LTOModule.cpp Tue Feb  8 16:40:47 2011
@@ -87,6 +87,17 @@
   return makeLTOModule(buffer.get(), errMsg);
 }
 
+LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
+                                    off_t size,
+                                    std::string &errMsg) {
+  OwningPtr<MemoryBuffer> buffer;
+  if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, size)) {
+    errMsg = ec.message();
+    return NULL;
+  }
+  return makeLTOModule(buffer.get(), errMsg);
+}
+
 /// makeBuffer - Create a MemoryBuffer from a memory range.  MemoryBuffer
 /// requires the byte past end of the buffer to be a zero.  We might get lucky
 /// and already be that way, otherwise make a copy.  Also if next byte is on a

Modified: llvm/trunk/tools/lto/LTOModule.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/LTOModule.h?rev=125128&r1=125127&r2=125128&view=diff
==============================================================================
--- llvm/trunk/tools/lto/LTOModule.h (original)
+++ llvm/trunk/tools/lto/LTOModule.h Tue Feb  8 16:40:47 2011
@@ -51,6 +51,9 @@
 
     static LTOModule*        makeLTOModule(const char* path,
                                           std::string& errMsg);
+    static LTOModule*        makeLTOModule(int fd, const char *path,
+                                           off_t size,
+                                           std::string& errMsg);
     static LTOModule*        makeLTOModule(const void* mem, size_t length,
                                            std::string& errMsg);
 

Modified: llvm/trunk/tools/lto/lto.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/lto.cpp?rev=125128&r1=125127&r2=125128&view=diff
==============================================================================
--- llvm/trunk/tools/lto/lto.cpp (original)
+++ llvm/trunk/tools/lto/lto.cpp Tue Feb  8 16:40:47 2011
@@ -91,6 +91,14 @@
      return LTOModule::makeLTOModule(path, sLastErrorString);
 }
 
+//
+// loads an object file from disk
+// returns NULL on error (check lto_get_error_message() for details)
+//
+lto_module_t lto_module_create_from_fd(int fd, const char *path, off_t size)
+{
+     return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
+}
 
 //
 // loads an object file from memory 

Modified: llvm/trunk/tools/lto/lto.exports
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/lto.exports?rev=125128&r1=125127&r2=125128&view=diff
==============================================================================
--- llvm/trunk/tools/lto/lto.exports (original)
+++ llvm/trunk/tools/lto/lto.exports Tue Feb  8 16:40:47 2011
@@ -1,6 +1,7 @@
 lto_get_error_message
 lto_get_version
 lto_module_create
+lto_module_create_from_fd
 lto_module_create_from_memory
 lto_module_get_num_symbols
 lto_module_get_symbol_attribute





More information about the llvm-commits mailing list