Add support in the LTO library for loading an object from the middle
authorRafael Espindola <rafael.espindola@gmail.com>
Thu, 17 Mar 2011 00:36:11 +0000 (00:36 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Thu, 17 Mar 2011 00:36:11 +0000 (00:36 +0000)
of an file.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@127781 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm-c/lto.h
tools/gold/gold-plugin.cpp
tools/lto/LTOModule.cpp
tools/lto/LTOModule.h
tools/lto/lto.cpp
tools/lto/lto.exports

index 1c42ce0cec7765845b3843976da64891e5b76a1c..be08c4eb196433b10824ca9fa99a826126db0d73 100644 (file)
@@ -127,7 +127,15 @@ lto_module_create_from_memory(const void* mem, size_t length);
  * 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);
+lto_module_create_from_fd(int fd, const char *path, size_t file_size);
+
+/**
+ * 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_at_offset(int fd, const char *path, size_t file_size,
+                                    size_t map_size, off_t offset);
 
 
 /**
index e959d9566b297ec76190970c774df025d53d2a58..7aa8c9109f64070206f8588885d0e5302908be35 100644 (file)
@@ -235,46 +235,13 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
   if (file->offset) {
     // Gold has found what might be IR part-way inside of a file, such as
     // an .a archive.
-    if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
-      (*message)(LDPL_ERROR,
-                 "Failed to seek to archive member of %s at offset %d: %s\n",
-                 file->name,
-                 file->offset, sys::StrError(errno).c_str());
-      return LDPS_ERR;
-    }
-    void *buf = malloc(file->filesize);
-    if (!buf) {
-      (*message)(LDPL_ERROR,
-                 "Failed to allocate buffer for archive member of size: %d\n",
-                 file->filesize);
-      return LDPS_ERR;
-    }
-    if (read(file->fd, buf, file->filesize) != file->filesize) {
-      (*message)(LDPL_ERROR,
-                 "Failed to read archive member of %s at offset %d: %s\n",
-                 file->name,
-                 file->offset,
-                 sys::StrError(errno).c_str());
-      free(buf);
-      return LDPS_ERR;
-    }
-    if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
-      free(buf);
-      return LDPS_OK;
-    }
-    M = lto_module_create_from_memory(buf, file->filesize);
-    if (!M) {
-      (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
-                 lto_get_error_message());
-      return LDPS_ERR;
-    }
-    free(buf);
+    M = lto_module_create_from_fd_at_offset(file->fd, file->name, -1,
+                                            file->filesize, file->offset);
   } else {
-    lseek(file->fd, 0, SEEK_SET);
     M = lto_module_create_from_fd(file->fd, file->name, file->filesize);
-    if (!M)
-      return LDPS_OK;
   }
+  if (!M)
+    return LDPS_OK;
 
   *claimed = 1;
   Modules.resize(Modules.size() + 1);
index bdea0c31a67440b6354593a3eb79bc9729dd9f54..9de3d5ffceeda59455510406c7d4ae3009782ab1 100644 (file)
@@ -95,10 +95,19 @@ LTOModule *LTOModule::makeLTOModule(const char *path,
 }
 
 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
-                                    off_t size,
+                                    size_t size,
+                                    std::string &errMsg) {
+  return makeLTOModule(fd, path, size, size, 0, errMsg);
+}
+
+LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
+                                    size_t file_size,
+                                    size_t map_size,
+                                    off_t offset,
                                     std::string &errMsg) {
   OwningPtr<MemoryBuffer> buffer;
-  if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, size)) {
+  if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size,
+                                                map_size, offset, false)) {
     errMsg = ec.message();
     return NULL;
   }
index 21e8475177e182b6bc9ac5e7f9c7b610b2fe4920..303151293b28586641be7f2f2d78509cde1cb7b2 100644 (file)
@@ -52,7 +52,12 @@ struct LTOModule {
     static LTOModule*        makeLTOModule(const char* path,
                                           std::string& errMsg);
     static LTOModule*        makeLTOModule(int fd, const char *path,
-                                           off_t size,
+                                           size_t size,
+                                           std::string& errMsg);
+    static LTOModule*        makeLTOModule(int fd, const char *path,
+                                           size_t file_size,
+                                           size_t map_size,
+                                           off_t offset,
                                            std::string& errMsg);
     static LTOModule*        makeLTOModule(const void* mem, size_t length,
                                            std::string& errMsg);
index f48570c1495dc4a83607fb8006bb49d1b4dd205c..cbac047c752e70572240ddd423178cae5be54a00 100644 (file)
@@ -95,11 +95,24 @@ lto_module_t lto_module_create(const char* path)
 // 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)
+lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size)
 {
      return LTOModule::makeLTOModule(fd, path, size, 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_at_offset(int fd, const char *path,
+                                                 size_t file_size,
+                                                 size_t map_size,
+                                                 off_t offset)
+{
+     return LTOModule::makeLTOModule(fd, path, file_size, map_size,
+                                     offset, sLastErrorString);
+}
+
 //
 // loads an object file from memory 
 // returns NULL on error (check lto_get_error_message() for details)
index a3740911edc1967bc04ffd4ee43501505c4b72b6..04b37e1f45cfd827bc71ec10783e4f6f877592dd 100644 (file)
@@ -2,6 +2,7 @@ lto_get_error_message
 lto_get_version
 lto_module_create
 lto_module_create_from_fd
+lto_module_create_from_fd_at_offset
 lto_module_create_from_memory
 lto_module_get_num_symbols
 lto_module_get_symbol_attribute