diff options
-rw-r--r-- | include/llvm-c/lto.h | 10 | ||||
-rw-r--r-- | tools/gold/gold-plugin.cpp | 41 | ||||
-rw-r--r-- | tools/lto/LTOModule.cpp | 13 | ||||
-rw-r--r-- | tools/lto/LTOModule.h | 7 | ||||
-rw-r--r-- | tools/lto/lto.cpp | 15 | ||||
-rw-r--r-- | tools/lto/lto.exports | 1 |
6 files changed, 45 insertions, 42 deletions
diff --git a/include/llvm-c/lto.h b/include/llvm-c/lto.h index 1c42ce0cec..be08c4eb19 100644 --- a/include/llvm-c/lto.h +++ b/include/llvm-c/lto.h @@ -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); /** diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp index e959d9566b..7aa8c9109f 100644 --- a/tools/gold/gold-plugin.cpp +++ b/tools/gold/gold-plugin.cpp @@ -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); diff --git a/tools/lto/LTOModule.cpp b/tools/lto/LTOModule.cpp index bdea0c31a6..9de3d5ffce 100644 --- a/tools/lto/LTOModule.cpp +++ b/tools/lto/LTOModule.cpp @@ -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; } diff --git a/tools/lto/LTOModule.h b/tools/lto/LTOModule.h index 21e8475177..303151293b 100644 --- a/tools/lto/LTOModule.h +++ b/tools/lto/LTOModule.h @@ -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); diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp index f48570c149..cbac047c75 100644 --- a/tools/lto/lto.cpp +++ b/tools/lto/lto.cpp @@ -95,12 +95,25 @@ 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) // diff --git a/tools/lto/lto.exports b/tools/lto/lto.exports index a3740911ed..04b37e1f45 100644 --- a/tools/lto/lto.exports +++ b/tools/lto/lto.exports @@ -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 |