llvm.org GIT mirror llvm / b4cc031
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125128 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 9 years ago
8 changed file(s) with 59 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
6868 int64_t FileSize = -1);
6969
7070 /// getOpenFile - Given an already-open file descriptor, read the file and
71 /// return a MemoryBuffer. This takes ownership of the descriptor,
72 /// immediately closing it after reading the file.
71 /// return a MemoryBuffer.
7372 static error_code getOpenFile(int FD, const char *Filename,
7473 OwningPtr &result,
7574 int64_t FileSize = -1);
1717
1818 #include
1919 #include
20 #include
2021
2122 #define LTO_API_VERSION 4
2223
120121 extern lto_module_t
121122 lto_module_create_from_memory(const void* mem, size_t length);
122123
124 /**
125 * Loads an object file from disk. The seek point of fd is not preserved.
126 * Returns NULL on error (check lto_get_error_message() for details).
127 */
128 extern lto_module_t
129 lto_module_create_from_fd(int fd, const char *path, off_t size);
130
123131
124132 /**
125133 * Frees all memory internally allocated by the module.
178178 sys::Path::UnMapFilePages(getBufferStart(), getBufferSize());
179179 }
180180 };
181
182 /// FileCloser - RAII object to make sure an FD gets closed properly.
183 class FileCloser {
184 int FD;
185 public:
186 explicit FileCloser(int FD) : FD(FD) {}
187 ~FileCloser() { ::close(FD); }
188 };
189181 }
190182
191183 error_code MemoryBuffer::getFile(StringRef Filename,
207199 if (FD == -1) {
208200 return error_code(errno, posix_category());
209201 }
210
211 return getOpenFile(FD, Filename, result, FileSize);
202 error_code ret = getOpenFile(FD, Filename, result, FileSize);
203 close(FD);
204 return ret;
212205 }
213206
214207 error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
215208 OwningPtr &result,
216209 int64_t FileSize) {
217 FileCloser FC(FD); // Close FD on return.
218
219210 // If we don't know the file size, use fstat to find out. fstat on an open
220211 // file descriptor is cheaper than stat on a random path.
221212 if (FileSize == -1) {
240240 /// with add_symbol if possible.
241241 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
242242 int *claimed) {
243 void *buf = NULL;
243 lto_module_t M;
244
244245 if (file->offset) {
245246 // Gold has found what might be IR part-way inside of a file, such as
246247 // an .a archive.
251252 file->offset, sys::StrError(errno).c_str());
252253 return LDPS_ERR;
253254 }
254 buf = malloc(file->filesize);
255 void *buf = malloc(file->filesize);
255256 if (!buf) {
256257 (*message)(LDPL_ERROR,
257258 "Failed to allocate buffer for archive member of size: %d\n",
271272 free(buf);
272273 return LDPS_OK;
273274 }
274 } else if (!lto_module_is_object_file(file->name))
275 return LDPS_OK;
275 M = lto_module_create_from_memory(buf, file->filesize);
276 free(buf);
277 } else {
278 // FIXME: We should not need to pass -1 as the file size, but there
279 // is a bug in BFD that causes it to pass 0 to us. Remove this once
280 // that is fixed.
281 off_t size = file->filesize ? file->filesize : -1;
282
283 // FIXME: We should not need to reset the position in the file, but there
284 // is a bug in BFD. Remove this once that is fixed.
285 off_t old_pos = lseek(file->fd, 0, SEEK_CUR);
286
287 lseek(file->fd, 0, SEEK_SET);
288 M = lto_module_create_from_fd(file->fd, file->name, size);
289
290 lseek(file->fd, old_pos, SEEK_SET);
291 if (!M)
292 return LDPS_OK;
293 }
276294
277295 *claimed = 1;
278296 Modules.resize(Modules.size() + 1);
279297 claimed_file &cf = Modules.back();
280
281 cf.M = buf ? lto_module_create_from_memory(buf, file->filesize) :
282 lto_module_create(file->name);
283 free(buf);
298 cf.M = M;
299
284300 if (!cf.M) {
285301 (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
286302 lto_get_error_message());
8080 std::string &errMsg) {
8181 OwningPtr buffer;
8282 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
83 errMsg = ec.message();
84 return NULL;
85 }
86 return makeLTOModule(buffer.get(), errMsg);
87 }
88
89 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
90 off_t size,
91 std::string &errMsg) {
92 OwningPtr buffer;
93 if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, size)) {
8394 errMsg = ec.message();
8495 return NULL;
8596 }
5050
5151 static LTOModule* makeLTOModule(const char* path,
5252 std::string& errMsg);
53 static LTOModule* makeLTOModule(int fd, const char *path,
54 off_t size,
55 std::string& errMsg);
5356 static LTOModule* makeLTOModule(const void* mem, size_t length,
5457 std::string& errMsg);
5558
9090 return LTOModule::makeLTOModule(path, sLastErrorString);
9191 }
9292
93 //
94 // loads an object file from disk
95 // returns NULL on error (check lto_get_error_message() for details)
96 //
97 lto_module_t lto_module_create_from_fd(int fd, const char *path, off_t size)
98 {
99 return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
100 }
93101
94102 //
95103 // loads an object file from memory
0 lto_get_error_message
11 lto_get_version
22 lto_module_create
3 lto_module_create_from_fd
34 lto_module_create_from_memory
45 lto_module_get_num_symbols
56 lto_module_get_symbol_attribute