summaryrefslogtreecommitdiffstats
path: root/tools/gold/gold-plugin.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/gold/gold-plugin.cpp')
-rw-r--r--tools/gold/gold-plugin.cpp41
1 files changed, 32 insertions, 9 deletions
diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp
index 9e43bef..6f547b3 100644
--- a/tools/gold/gold-plugin.cpp
+++ b/tools/gold/gold-plugin.cpp
@@ -17,6 +17,9 @@
#include "llvm-c/lto.h"
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/Support/system_error.h"
+#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/Path.h"
@@ -235,25 +238,43 @@ ld_plugin_status onload(ld_plugin_tv *tv) {
static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
int *claimed) {
lto_module_t M;
-
+ const void *view;
+ OwningPtr<MemoryBuffer> buffer;
if (get_view) {
- const void *view;
if (get_view(file->handle, &view) != LDPS_OK) {
(*message)(LDPL_ERROR, "Failed to get a view of %s", file->name);
return LDPS_ERR;
}
- M = lto_module_create_from_memory(view, file->filesize);
- } else if (file->offset) {
+ } else {
+ int64_t offset = 0;
// Gold has found what might be IR part-way inside of a file, such as
// an .a archive.
- M = lto_module_create_from_fd_at_offset(file->fd, file->name, -1,
- file->filesize, file->offset);
- } else {
- M = lto_module_create_from_fd(file->fd, file->name, file->filesize);
+ if (file->offset) {
+ offset = file->offset;
+ }
+ if (error_code ec =
+ MemoryBuffer::getOpenFile(file->fd, file->name, buffer, file->filesize,
+ -1, offset, false)) {
+ (*message)(LDPL_ERROR, ec.message().c_str());
+ return LDPS_ERR;
+ }
+ view = buffer->getBufferStart();
}
- if (!M)
+
+ if (!lto_module_is_object_file_in_memory(view, file->filesize))
return LDPS_OK;
+ M = lto_module_create_from_memory(view, file->filesize);
+ if (!M) {
+ if (const char* msg = lto_get_error_message()) {
+ (*message)(LDPL_ERROR,
+ "LLVM gold plugin has failed to create LTO module: %s",
+ msg);
+ return LDPS_ERR;
+ }
+ return LDPS_OK;
+ }
+
*claimed = 1;
Modules.resize(Modules.size() + 1);
claimed_file &cf = Modules.back();
@@ -360,6 +381,8 @@ static ld_plugin_status all_symbols_read_hook(void) {
bool anySymbolsPreserved = false;
for (std::list<claimed_file>::iterator I = Modules.begin(),
E = Modules.end(); I != E; ++I) {
+ if (I->syms.empty())
+ continue;
(*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
OpenPOWER on IntegriCloud