aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/gold/gold-plugin.cpp30
-rw-r--r--tools/lto/LTOModule.cpp11
-rw-r--r--tools/lto/LTOModule.h3
-rw-r--r--tools/lto/lto.cpp8
-rw-r--r--tools/lto/lto.exports1
5 files changed, 46 insertions, 7 deletions
diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp
index 1639ac1..257c766 100644
--- a/tools/gold/gold-plugin.cpp
+++ b/tools/gold/gold-plugin.cpp
@@ -241,7 +241,8 @@ ld_plugin_status onload(ld_plugin_tv *tv) {
/// 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 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
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 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
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());
diff --git a/tools/lto/LTOModule.cpp b/tools/lto/LTOModule.cpp
index e2ecabc..ca937bf 100644
--- a/tools/lto/LTOModule.cpp
+++ b/tools/lto/LTOModule.cpp
@@ -87,6 +87,17 @@ LTOModule *LTOModule::makeLTOModule(const char *path,
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
diff --git a/tools/lto/LTOModule.h b/tools/lto/LTOModule.h
index a19acc0..1794d81 100644
--- a/tools/lto/LTOModule.h
+++ b/tools/lto/LTOModule.h
@@ -51,6 +51,9 @@ struct LTOModule {
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);
diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp
index 75b40f4..7d4871d 100644
--- a/tools/lto/lto.cpp
+++ b/tools/lto/lto.cpp
@@ -91,6 +91,14 @@ lto_module_t lto_module_create(const char* path)
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
diff --git a/tools/lto/lto.exports b/tools/lto/lto.exports
index 4dbf760..a374091 100644
--- a/tools/lto/lto.exports
+++ b/tools/lto/lto.exports
@@ -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