Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // fileread.cc -- read files for gold |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
bae7f79e ILT |
23 | #include "gold.h" |
24 | ||
bae7f79e ILT |
25 | #include <cstring> |
26 | #include <cerrno> | |
27 | #include <fcntl.h> | |
28 | #include <unistd.h> | |
d1038c21 | 29 | #include <sys/mman.h> |
51dee2fe | 30 | #include "filenames.h" |
bae7f79e ILT |
31 | |
32 | #include "options.h" | |
33 | #include "dirsearch.h" | |
34 | #include "fileread.h" | |
35 | ||
36 | namespace gold | |
37 | { | |
38 | ||
39 | // Class File_read::View. | |
40 | ||
41 | File_read::View::~View() | |
42 | { | |
a3ad94ed | 43 | gold_assert(!this->is_locked()); |
d1038c21 ILT |
44 | if (!this->mapped_) |
45 | delete[] this->data_; | |
46 | else | |
47 | { | |
48 | if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0) | |
75f2446e | 49 | gold_warning(_("munmap failed: %s"), strerror(errno)); |
e44fcf3b ILT |
50 | |
51 | File_read::current_mapped_bytes -= this->size_; | |
d1038c21 | 52 | } |
bae7f79e ILT |
53 | } |
54 | ||
55 | void | |
56 | File_read::View::lock() | |
57 | { | |
58 | ++this->lock_count_; | |
59 | } | |
60 | ||
61 | void | |
62 | File_read::View::unlock() | |
63 | { | |
a3ad94ed | 64 | gold_assert(this->lock_count_ > 0); |
bae7f79e ILT |
65 | --this->lock_count_; |
66 | } | |
67 | ||
68 | bool | |
69 | File_read::View::is_locked() | |
70 | { | |
71 | return this->lock_count_ > 0; | |
72 | } | |
73 | ||
74 | // Class File_read. | |
75 | ||
e44fcf3b ILT |
76 | // The File_read static variables. |
77 | unsigned long long File_read::total_mapped_bytes; | |
78 | unsigned long long File_read::current_mapped_bytes; | |
79 | unsigned long long File_read::maximum_mapped_bytes; | |
80 | ||
bae7f79e ILT |
81 | // The File_read class is designed to support file descriptor caching, |
82 | // but this is not currently implemented. | |
83 | ||
84 | File_read::~File_read() | |
85 | { | |
a3ad94ed | 86 | gold_assert(this->lock_count_ == 0); |
bae7f79e ILT |
87 | if (this->descriptor_ >= 0) |
88 | { | |
89 | if (close(this->descriptor_) < 0) | |
75f2446e ILT |
90 | gold_warning(_("close of %s failed: %s"), |
91 | this->name_.c_str(), strerror(errno)); | |
bae7f79e ILT |
92 | this->descriptor_ = -1; |
93 | } | |
94 | this->name_.clear(); | |
95 | this->clear_views(true); | |
96 | } | |
97 | ||
5a6f7e2d ILT |
98 | // Open the file. |
99 | ||
bae7f79e ILT |
100 | bool |
101 | File_read::open(const std::string& name) | |
102 | { | |
a3ad94ed ILT |
103 | gold_assert(this->lock_count_ == 0 |
104 | && this->descriptor_ < 0 | |
105 | && this->name_.empty()); | |
bae7f79e | 106 | this->name_ = name; |
82dcae9d | 107 | |
bae7f79e | 108 | this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY); |
82dcae9d ILT |
109 | |
110 | if (this->descriptor_ >= 0) | |
111 | { | |
112 | struct stat s; | |
113 | if (::fstat(this->descriptor_, &s) < 0) | |
75f2446e ILT |
114 | gold_error(_("%s: fstat failed: %s"), |
115 | this->name_.c_str(), strerror(errno)); | |
82dcae9d ILT |
116 | this->size_ = s.st_size; |
117 | } | |
118 | ||
bae7f79e | 119 | ++this->lock_count_; |
82dcae9d | 120 | |
bae7f79e ILT |
121 | return this->descriptor_ >= 0; |
122 | } | |
123 | ||
5a6f7e2d ILT |
124 | // Open the file for testing purposes. |
125 | ||
126 | bool | |
127 | File_read::open(const std::string& name, const unsigned char* contents, | |
82dcae9d | 128 | off_t size) |
bae7f79e | 129 | { |
5a6f7e2d ILT |
130 | gold_assert(this->lock_count_ == 0 |
131 | && this->descriptor_ < 0 | |
132 | && this->name_.empty()); | |
133 | this->name_ = name; | |
134 | this->contents_ = contents; | |
82dcae9d | 135 | this->size_ = size; |
5a6f7e2d ILT |
136 | ++this->lock_count_; |
137 | return true; | |
bae7f79e ILT |
138 | } |
139 | ||
140 | void | |
141 | File_read::lock() | |
142 | { | |
143 | ++this->lock_count_; | |
144 | } | |
145 | ||
146 | void | |
147 | File_read::unlock() | |
148 | { | |
a3ad94ed | 149 | gold_assert(this->lock_count_ > 0); |
bae7f79e | 150 | --this->lock_count_; |
9eb9fa57 | 151 | if (this->lock_count_ == 0) |
e44fcf3b ILT |
152 | { |
153 | File_read::total_mapped_bytes += this->mapped_bytes_; | |
154 | File_read::current_mapped_bytes += this->mapped_bytes_; | |
155 | this->mapped_bytes_ = 0; | |
156 | if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes) | |
157 | File_read::maximum_mapped_bytes = File_read::current_mapped_bytes; | |
158 | ||
159 | this->clear_views(false); | |
160 | } | |
bae7f79e ILT |
161 | } |
162 | ||
163 | bool | |
7004837e | 164 | File_read::is_locked() const |
bae7f79e ILT |
165 | { |
166 | return this->lock_count_ > 0; | |
167 | } | |
168 | ||
169 | // See if we have a view which covers the file starting at START for | |
170 | // SIZE bytes. Return a pointer to the View if found, NULL if not. | |
171 | ||
ead1e424 | 172 | inline File_read::View* |
c71c6f56 | 173 | File_read::find_view(off_t start, off_t size) const |
bae7f79e | 174 | { |
ead1e424 | 175 | off_t page = File_read::page_offset(start); |
c71c6f56 | 176 | Views::const_iterator p = this->views_.find(page); |
ead1e424 ILT |
177 | if (p == this->views_.end()) |
178 | return NULL; | |
179 | if (p->second->size() - (start - page) < size) | |
180 | return NULL; | |
181 | return p->second; | |
bae7f79e ILT |
182 | } |
183 | ||
82dcae9d | 184 | // Read SIZE bytes from the file starting at offset START. Read into |
9eb9fa57 | 185 | // the buffer at P. |
bae7f79e | 186 | |
9eb9fa57 | 187 | void |
c71c6f56 | 188 | File_read::do_read(off_t start, off_t size, void* p) const |
bae7f79e | 189 | { |
9eb9fa57 | 190 | off_t bytes; |
82dcae9d | 191 | if (this->contents_ != NULL) |
bae7f79e | 192 | { |
9eb9fa57 ILT |
193 | bytes = this->size_ - start; |
194 | if (bytes >= size) | |
195 | { | |
196 | memcpy(p, this->contents_ + start, size); | |
197 | return; | |
198 | } | |
bae7f79e | 199 | } |
9eb9fa57 | 200 | else |
82dcae9d | 201 | { |
9eb9fa57 ILT |
202 | bytes = ::pread(this->descriptor_, p, size, start); |
203 | if (bytes == size) | |
204 | return; | |
82dcae9d | 205 | |
9eb9fa57 ILT |
206 | if (bytes < 0) |
207 | { | |
75f2446e ILT |
208 | gold_fatal(_("%s: pread failed: %s"), |
209 | this->filename().c_str(), strerror(errno)); | |
210 | return; | |
9eb9fa57 | 211 | } |
bae7f79e | 212 | } |
9eb9fa57 | 213 | |
75f2446e ILT |
214 | gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"), |
215 | this->filename().c_str(), | |
216 | static_cast<long long>(bytes), | |
217 | static_cast<long long>(size), | |
218 | static_cast<long long>(start)); | |
bae7f79e ILT |
219 | } |
220 | ||
ba45d247 ILT |
221 | // Read data from the file. |
222 | ||
bae7f79e | 223 | void |
c71c6f56 | 224 | File_read::read(off_t start, off_t size, void* p) const |
ba45d247 | 225 | { |
ba45d247 ILT |
226 | File_read::View* pv = this->find_view(start, size); |
227 | if (pv != NULL) | |
228 | { | |
229 | memcpy(p, pv->data() + (start - pv->start()), size); | |
230 | return; | |
231 | } | |
232 | ||
9eb9fa57 | 233 | this->do_read(start, size, p); |
bae7f79e ILT |
234 | } |
235 | ||
236 | // Find an existing view or make a new one. | |
237 | ||
238 | File_read::View* | |
9eb9fa57 | 239 | File_read::find_or_make_view(off_t start, off_t size, bool cache) |
bae7f79e | 240 | { |
a3ad94ed | 241 | gold_assert(this->lock_count_ > 0); |
bae7f79e | 242 | |
ead1e424 ILT |
243 | off_t poff = File_read::page_offset(start); |
244 | ||
245 | File_read::View* const vnull = NULL; | |
246 | std::pair<Views::iterator, bool> ins = | |
247 | this->views_.insert(std::make_pair(poff, vnull)); | |
248 | ||
249 | if (!ins.second) | |
250 | { | |
251 | // There was an existing view at this offset. | |
252 | File_read::View* v = ins.first->second; | |
253 | if (v->size() - (start - v->start()) >= size) | |
9eb9fa57 ILT |
254 | { |
255 | if (cache) | |
256 | v->set_cache(); | |
257 | return v; | |
258 | } | |
ead1e424 ILT |
259 | |
260 | // This view is not large enough. | |
261 | this->saved_views_.push_back(v); | |
262 | } | |
263 | ||
82dcae9d ILT |
264 | // We need to read data from the file. We read full pages for |
265 | // greater efficiency on small files. | |
ead1e424 ILT |
266 | |
267 | off_t psize = File_read::pages(size + (start - poff)); | |
bae7f79e | 268 | |
82dcae9d ILT |
269 | if (poff + psize >= this->size_) |
270 | { | |
271 | psize = this->size_ - poff; | |
272 | gold_assert(psize >= size); | |
273 | } | |
ead1e424 | 274 | |
d1038c21 | 275 | File_read::View* v; |
ead1e424 | 276 | |
d1038c21 ILT |
277 | if (this->contents_ != NULL) |
278 | { | |
279 | unsigned char* p = new unsigned char[psize]; | |
280 | this->do_read(poff, psize, p); | |
281 | v = new File_read::View(poff, psize, p, cache, false); | |
282 | } | |
283 | else | |
284 | { | |
285 | void* p = ::mmap(NULL, psize, PROT_READ, MAP_SHARED, | |
286 | this->descriptor_, poff); | |
287 | if (p == MAP_FAILED) | |
75f2446e ILT |
288 | gold_fatal(_("%s: mmap offset %lld size %lld failed: %s"), |
289 | this->filename().c_str(), | |
290 | static_cast<long long>(poff), | |
291 | static_cast<long long>(psize), | |
292 | strerror(errno)); | |
d1038c21 | 293 | |
e44fcf3b ILT |
294 | this->mapped_bytes_ += psize; |
295 | ||
d1038c21 ILT |
296 | const unsigned char* pbytes = static_cast<const unsigned char*>(p); |
297 | v = new File_read::View(poff, psize, pbytes, cache, true); | |
298 | } | |
ead1e424 | 299 | |
82dcae9d ILT |
300 | ins.first->second = v; |
301 | return v; | |
bae7f79e ILT |
302 | } |
303 | ||
304 | // This implementation of get_view just reads into a memory buffer, | |
305 | // which we store on view_list_. At some point we should support | |
306 | // mmap. | |
307 | ||
308 | const unsigned char* | |
9eb9fa57 | 309 | File_read::get_view(off_t start, off_t size, bool cache) |
ba45d247 ILT |
310 | { |
311 | gold_assert(this->lock_count_ > 0); | |
9eb9fa57 | 312 | File_read::View* pv = this->find_or_make_view(start, size, cache); |
bae7f79e ILT |
313 | return pv->data() + (start - pv->start()); |
314 | } | |
315 | ||
316 | File_view* | |
9eb9fa57 | 317 | File_read::get_lasting_view(off_t start, off_t size, bool cache) |
bae7f79e | 318 | { |
a3ad94ed | 319 | gold_assert(this->lock_count_ > 0); |
9eb9fa57 | 320 | File_read::View* pv = this->find_or_make_view(start, size, cache); |
bae7f79e ILT |
321 | pv->lock(); |
322 | return new File_view(*this, pv, pv->data() + (start - pv->start())); | |
323 | } | |
324 | ||
325 | // Remove all the file views. | |
326 | ||
327 | void | |
328 | File_read::clear_views(bool destroying) | |
329 | { | |
fcf29b24 ILT |
330 | Views::iterator p = this->views_.begin(); |
331 | while (p != this->views_.end()) | |
bae7f79e | 332 | { |
9eb9fa57 ILT |
333 | if (!p->second->is_locked() |
334 | && (destroying || !p->second->should_cache())) | |
fcf29b24 ILT |
335 | { |
336 | delete p->second; | |
337 | ||
338 | // map::erase invalidates only the iterator to the deleted | |
339 | // element. | |
340 | Views::iterator pe = p; | |
341 | ++p; | |
342 | this->views_.erase(pe); | |
343 | } | |
ead1e424 | 344 | else |
bae7f79e | 345 | { |
a3ad94ed | 346 | gold_assert(!destroying); |
fcf29b24 | 347 | ++p; |
bae7f79e | 348 | } |
ead1e424 | 349 | } |
ead1e424 | 350 | |
fcf29b24 ILT |
351 | Saved_views::iterator q = this->saved_views_.begin(); |
352 | while (q != this->saved_views_.end()) | |
ead1e424 | 353 | { |
fcf29b24 ILT |
354 | if (!(*q)->is_locked() |
355 | && (destroying || !(*q)->should_cache())) | |
bae7f79e | 356 | { |
fcf29b24 ILT |
357 | delete *q; |
358 | q = this->saved_views_.erase(q); | |
ead1e424 ILT |
359 | } |
360 | else | |
361 | { | |
a3ad94ed | 362 | gold_assert(!destroying); |
fcf29b24 | 363 | ++q; |
bae7f79e ILT |
364 | } |
365 | } | |
366 | } | |
367 | ||
e44fcf3b ILT |
368 | // Print statistical information to stderr. This is used for --stats. |
369 | ||
370 | void | |
371 | File_read::print_stats() | |
372 | { | |
373 | fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"), | |
374 | program_name, File_read::total_mapped_bytes); | |
375 | fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"), | |
376 | program_name, File_read::maximum_mapped_bytes); | |
377 | } | |
378 | ||
bae7f79e ILT |
379 | // Class File_view. |
380 | ||
381 | File_view::~File_view() | |
382 | { | |
a3ad94ed | 383 | gold_assert(this->file_.is_locked()); |
bae7f79e ILT |
384 | this->view_->unlock(); |
385 | } | |
386 | ||
387 | // Class Input_file. | |
388 | ||
5a6f7e2d ILT |
389 | // Create a file for testing. |
390 | ||
391 | Input_file::Input_file(const char* name, const unsigned char* contents, | |
392 | off_t size) | |
393 | : file_() | |
394 | { | |
395 | this->input_argument_ = | |
51dee2fe | 396 | new Input_file_argument(name, false, "", Position_dependent_options()); |
5a6f7e2d ILT |
397 | bool ok = file_.open(name, contents, size); |
398 | gold_assert(ok); | |
399 | } | |
400 | ||
401 | // Open the file. | |
402 | ||
51dee2fe ILT |
403 | // If the filename is not absolute, we assume it is in the current |
404 | // directory *except* when: | |
405 | // A) input_argument_->is_lib() is true; or | |
406 | // B) input_argument_->extra_search_path() is not empty. | |
407 | // In both cases, we look in extra_search_path + library_path to find | |
408 | // the file location, rather than the current directory. | |
409 | ||
75f2446e | 410 | bool |
bae7f79e ILT |
411 | Input_file::open(const General_options& options, const Dirsearch& dirpath) |
412 | { | |
413 | std::string name; | |
51dee2fe ILT |
414 | |
415 | // Case 1: name is an absolute file, just try to open it | |
416 | // Case 2: name is relative but is_lib is false and extra_search_path | |
417 | // is empty | |
418 | if (IS_ABSOLUTE_PATH (this->input_argument_->name()) | |
419 | || (!this->input_argument_->is_lib() | |
420 | && this->input_argument_->extra_search_path() == NULL)) | |
e2aacd2c ILT |
421 | { |
422 | name = this->input_argument_->name(); | |
423 | this->found_name_ = name; | |
424 | } | |
51dee2fe ILT |
425 | // Case 3: is_lib is true |
426 | else if (this->input_argument_->is_lib()) | |
bae7f79e | 427 | { |
51dee2fe ILT |
428 | // We don't yet support extra_search_path with -l. |
429 | gold_assert(this->input_argument_->extra_search_path() == NULL); | |
bae7f79e | 430 | std::string n1("lib"); |
5a6f7e2d | 431 | n1 += this->input_argument_->name(); |
bae7f79e | 432 | std::string n2; |
61611222 ILT |
433 | if (options.is_static() |
434 | || this->input_argument_->options().do_static_search()) | |
f6ce93d6 ILT |
435 | n1 += ".a"; |
436 | else | |
437 | { | |
438 | n2 = n1 + ".a"; | |
439 | n1 += ".so"; | |
440 | } | |
ad2d6943 | 441 | name = dirpath.find(n1, n2, &this->is_in_sysroot_); |
bae7f79e ILT |
442 | if (name.empty()) |
443 | { | |
a0c4fb0a | 444 | gold_error(_("cannot find -l%s"), |
75f2446e ILT |
445 | this->input_argument_->name()); |
446 | return false; | |
bae7f79e | 447 | } |
e2aacd2c ILT |
448 | if (n2.empty() || name[name.length() - 1] == 'o') |
449 | this->found_name_ = n1; | |
450 | else | |
451 | this->found_name_ = n2; | |
bae7f79e | 452 | } |
51dee2fe ILT |
453 | // Case 4: extra_search_path is not empty |
454 | else | |
455 | { | |
456 | gold_assert(this->input_argument_->extra_search_path() != NULL); | |
457 | ||
458 | // First, check extra_search_path. | |
459 | name = this->input_argument_->extra_search_path(); | |
460 | if (!IS_DIR_SEPARATOR (name[name.length() - 1])) | |
461 | name += '/'; | |
462 | name += this->input_argument_->name(); | |
463 | struct stat dummy_stat; | |
464 | if (::stat(name.c_str(), &dummy_stat) < 0) | |
465 | { | |
466 | // extra_search_path failed, so check the normal search-path. | |
ad2d6943 ILT |
467 | name = dirpath.find(this->input_argument_->name(), "", |
468 | &this->is_in_sysroot_); | |
51dee2fe ILT |
469 | if (name.empty()) |
470 | { | |
a0c4fb0a | 471 | gold_error(_("cannot find %s"), |
75f2446e ILT |
472 | this->input_argument_->name()); |
473 | return false; | |
51dee2fe ILT |
474 | } |
475 | } | |
e2aacd2c | 476 | this->found_name_ = this->input_argument_->name(); |
51dee2fe ILT |
477 | } |
478 | ||
479 | // Now that we've figured out where the file lives, try to open it. | |
bae7f79e ILT |
480 | if (!this->file_.open(name)) |
481 | { | |
a0c4fb0a | 482 | gold_error(_("cannot open %s: %s"), |
75f2446e ILT |
483 | name.c_str(), strerror(errno)); |
484 | return false; | |
bae7f79e | 485 | } |
75f2446e ILT |
486 | |
487 | return true; | |
bae7f79e ILT |
488 | } |
489 | ||
490 | } // End namespace gold. |