2008-01-02 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / gold / fileread.cc
CommitLineData
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>
cb295612 30#include <sys/uio.h>
51dee2fe 31#include "filenames.h"
bae7f79e
ILT
32
33#include "options.h"
34#include "dirsearch.h"
35#include "fileread.h"
36
37namespace gold
38{
39
40// Class File_read::View.
41
42File_read::View::~View()
43{
a3ad94ed 44 gold_assert(!this->is_locked());
d1038c21
ILT
45 if (!this->mapped_)
46 delete[] this->data_;
47 else
48 {
49 if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
75f2446e 50 gold_warning(_("munmap failed: %s"), strerror(errno));
e44fcf3b
ILT
51
52 File_read::current_mapped_bytes -= this->size_;
d1038c21 53 }
bae7f79e
ILT
54}
55
56void
57File_read::View::lock()
58{
59 ++this->lock_count_;
60}
61
62void
63File_read::View::unlock()
64{
a3ad94ed 65 gold_assert(this->lock_count_ > 0);
bae7f79e
ILT
66 --this->lock_count_;
67}
68
69bool
70File_read::View::is_locked()
71{
72 return this->lock_count_ > 0;
73}
74
75// Class File_read.
76
e44fcf3b
ILT
77// The File_read static variables.
78unsigned long long File_read::total_mapped_bytes;
79unsigned long long File_read::current_mapped_bytes;
80unsigned long long File_read::maximum_mapped_bytes;
81
bae7f79e
ILT
82// The File_read class is designed to support file descriptor caching,
83// but this is not currently implemented.
84
85File_read::~File_read()
86{
17a1d0a9 87 gold_assert(this->token_.is_writable());
bae7f79e
ILT
88 if (this->descriptor_ >= 0)
89 {
90 if (close(this->descriptor_) < 0)
75f2446e
ILT
91 gold_warning(_("close of %s failed: %s"),
92 this->name_.c_str(), strerror(errno));
bae7f79e
ILT
93 this->descriptor_ = -1;
94 }
95 this->name_.clear();
96 this->clear_views(true);
97}
98
5a6f7e2d
ILT
99// Open the file.
100
bae7f79e 101bool
17a1d0a9 102File_read::open(const Task* task, const std::string& name)
bae7f79e 103{
17a1d0a9 104 gold_assert(this->token_.is_writable()
a3ad94ed
ILT
105 && this->descriptor_ < 0
106 && this->name_.empty());
bae7f79e 107 this->name_ = name;
82dcae9d 108
bae7f79e 109 this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
82dcae9d
ILT
110
111 if (this->descriptor_ >= 0)
112 {
113 struct stat s;
114 if (::fstat(this->descriptor_, &s) < 0)
75f2446e
ILT
115 gold_error(_("%s: fstat failed: %s"),
116 this->name_.c_str(), strerror(errno));
82dcae9d
ILT
117 this->size_ = s.st_size;
118 }
119
17a1d0a9 120 this->token_.add_writer(task);
82dcae9d 121
bae7f79e
ILT
122 return this->descriptor_ >= 0;
123}
124
5a6f7e2d
ILT
125// Open the file for testing purposes.
126
127bool
17a1d0a9
ILT
128File_read::open(const Task* task, const std::string& name,
129 const unsigned char* contents, off_t size)
bae7f79e 130{
17a1d0a9 131 gold_assert(this->token_.is_writable()
5a6f7e2d
ILT
132 && this->descriptor_ < 0
133 && this->name_.empty());
134 this->name_ = name;
135 this->contents_ = contents;
82dcae9d 136 this->size_ = size;
17a1d0a9 137 this->token_.add_writer(task);
5a6f7e2d 138 return true;
bae7f79e
ILT
139}
140
17a1d0a9
ILT
141// Release the file. This is called when we are done with the file in
142// a Task.
143
bae7f79e 144void
17a1d0a9 145File_read::release()
bae7f79e 146{
17a1d0a9
ILT
147 gold_assert(this->is_locked());
148
149 File_read::total_mapped_bytes += this->mapped_bytes_;
150 File_read::current_mapped_bytes += this->mapped_bytes_;
151 this->mapped_bytes_ = 0;
152 if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes)
153 File_read::maximum_mapped_bytes = File_read::current_mapped_bytes;
154
155 this->clear_views(false);
156
157 this->released_ = true;
bae7f79e
ILT
158}
159
17a1d0a9
ILT
160// Lock the file.
161
bae7f79e 162void
17a1d0a9 163File_read::lock(const Task* task)
bae7f79e 164{
17a1d0a9
ILT
165 gold_assert(this->released_);
166 this->token_.add_writer(task);
167 this->released_ = false;
168}
e44fcf3b 169
17a1d0a9
ILT
170// Unlock the file.
171
172void
173File_read::unlock(const Task* task)
174{
175 this->release();
176 this->token_.remove_writer(task);
bae7f79e
ILT
177}
178
17a1d0a9
ILT
179// Return whether the file is locked.
180
bae7f79e 181bool
7004837e 182File_read::is_locked() const
bae7f79e 183{
17a1d0a9
ILT
184 if (!this->token_.is_writable())
185 return true;
186 // The file is not locked, so it should have been released.
187 gold_assert(this->released_);
188 return false;
bae7f79e
ILT
189}
190
191// See if we have a view which covers the file starting at START for
192// SIZE bytes. Return a pointer to the View if found, NULL if not.
193
ead1e424 194inline File_read::View*
8383303e 195File_read::find_view(off_t start, section_size_type size) const
bae7f79e 196{
ead1e424 197 off_t page = File_read::page_offset(start);
cb295612
ILT
198
199 Views::const_iterator p = this->views_.lower_bound(page);
200 if (p == this->views_.end() || p->first > page)
201 {
202 if (p == this->views_.begin())
203 return NULL;
204 --p;
205 }
206
207 if (p->second->start() + static_cast<off_t>(p->second->size())
208 < start + static_cast<off_t>(size))
ead1e424 209 return NULL;
cb295612
ILT
210
211 p->second->set_accessed();
212
ead1e424 213 return p->second;
bae7f79e
ILT
214}
215
82dcae9d 216// Read SIZE bytes from the file starting at offset START. Read into
9eb9fa57 217// the buffer at P.
bae7f79e 218
9eb9fa57 219void
fe8718a4 220File_read::do_read(off_t start, section_size_type size, void* p) const
bae7f79e 221{
8cce6718 222 ssize_t bytes;
82dcae9d 223 if (this->contents_ != NULL)
bae7f79e 224 {
9eb9fa57 225 bytes = this->size_ - start;
8cce6718 226 if (static_cast<section_size_type>(bytes) >= size)
9eb9fa57
ILT
227 {
228 memcpy(p, this->contents_ + start, size);
229 return;
230 }
bae7f79e 231 }
9eb9fa57 232 else
82dcae9d 233 {
8cce6718
ILT
234 bytes = ::pread(this->descriptor_, p, size, start);
235 if (static_cast<section_size_type>(bytes) == size)
236 return;
237
238 if (bytes < 0)
9eb9fa57 239 {
75f2446e
ILT
240 gold_fatal(_("%s: pread failed: %s"),
241 this->filename().c_str(), strerror(errno));
242 return;
9eb9fa57 243 }
bae7f79e 244 }
9eb9fa57 245
75f2446e
ILT
246 gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
247 this->filename().c_str(),
248 static_cast<long long>(bytes),
249 static_cast<long long>(size),
250 static_cast<long long>(start));
bae7f79e
ILT
251}
252
ba45d247
ILT
253// Read data from the file.
254
bae7f79e 255void
fe8718a4 256File_read::read(off_t start, section_size_type size, void* p) const
ba45d247 257{
cb295612 258 const File_read::View* pv = this->find_view(start, size);
ba45d247
ILT
259 if (pv != NULL)
260 {
261 memcpy(p, pv->data() + (start - pv->start()), size);
262 return;
263 }
264
9eb9fa57 265 this->do_read(start, size, p);
bae7f79e
ILT
266}
267
268// Find an existing view or make a new one.
269
270File_read::View*
8383303e 271File_read::find_or_make_view(off_t start, section_size_type size, bool cache)
bae7f79e 272{
17a1d0a9
ILT
273 gold_assert(!this->token_.is_writable());
274 this->released_ = false;
bae7f79e 275
cb295612
ILT
276 File_read::View* v = this->find_view(start, size);
277 if (v != NULL)
278 {
279 if (cache)
280 v->set_cache();
281 return v;
282 }
283
ead1e424
ILT
284 off_t poff = File_read::page_offset(start);
285
286 File_read::View* const vnull = NULL;
287 std::pair<Views::iterator, bool> ins =
288 this->views_.insert(std::make_pair(poff, vnull));
289
290 if (!ins.second)
291 {
cb295612
ILT
292 // There was an existing view at this offset. It must not be
293 // large enough. We can't delete it here, since something might
294 // be using it; put it on a list to be deleted when the file is
295 // unlocked.
296 v = ins.first->second;
297 gold_assert(v->size() - (start - v->start()) < size);
298 if (v->should_cache())
299 cache = true;
300 v->clear_cache();
ead1e424
ILT
301 this->saved_views_.push_back(v);
302 }
303
cb295612 304 // We need to map data from the file.
ead1e424 305
fe8718a4 306 section_size_type psize = File_read::pages(size + (start - poff));
bae7f79e 307
8d32f935 308 if (poff + static_cast<off_t>(psize) >= this->size_)
82dcae9d
ILT
309 {
310 psize = this->size_ - poff;
fe8718a4 311 gold_assert(psize >= size);
82dcae9d 312 }
ead1e424 313
d1038c21
ILT
314 if (this->contents_ != NULL)
315 {
316 unsigned char* p = new unsigned char[psize];
317 this->do_read(poff, psize, p);
318 v = new File_read::View(poff, psize, p, cache, false);
319 }
320 else
321 {
cb295612 322 void* p = ::mmap(NULL, psize, PROT_READ, MAP_PRIVATE,
d1038c21
ILT
323 this->descriptor_, poff);
324 if (p == MAP_FAILED)
75f2446e
ILT
325 gold_fatal(_("%s: mmap offset %lld size %lld failed: %s"),
326 this->filename().c_str(),
327 static_cast<long long>(poff),
328 static_cast<long long>(psize),
329 strerror(errno));
d1038c21 330
e44fcf3b
ILT
331 this->mapped_bytes_ += psize;
332
d1038c21
ILT
333 const unsigned char* pbytes = static_cast<const unsigned char*>(p);
334 v = new File_read::View(poff, psize, pbytes, cache, true);
335 }
ead1e424 336
82dcae9d
ILT
337 ins.first->second = v;
338 return v;
bae7f79e
ILT
339}
340
17a1d0a9 341// Get a view into the file.
bae7f79e
ILT
342
343const unsigned char*
8383303e 344File_read::get_view(off_t start, section_size_type size, bool cache)
ba45d247 345{
9eb9fa57 346 File_read::View* pv = this->find_or_make_view(start, size, cache);
bae7f79e
ILT
347 return pv->data() + (start - pv->start());
348}
349
350File_view*
8383303e 351File_read::get_lasting_view(off_t start, section_size_type size, bool cache)
bae7f79e 352{
9eb9fa57 353 File_read::View* pv = this->find_or_make_view(start, size, cache);
bae7f79e
ILT
354 pv->lock();
355 return new File_view(*this, pv, pv->data() + (start - pv->start()));
356}
357
cb295612
ILT
358// Use readv to read COUNT entries from RM starting at START. BASE
359// must be added to all file offsets in RM.
360
361void
362File_read::do_readv(off_t base, const Read_multiple& rm, size_t start,
363 size_t count)
364{
365 unsigned char discard[File_read::page_size];
366 iovec iov[File_read::max_readv_entries * 2];
367 size_t iov_index = 0;
368
369 off_t first_offset = rm[start].file_offset;
370 off_t last_offset = first_offset;
371 ssize_t want = 0;
372 for (size_t i = 0; i < count; ++i)
373 {
374 const Read_multiple_entry& i_entry(rm[start + i]);
375
376 if (i_entry.file_offset > last_offset)
377 {
378 size_t skip = i_entry.file_offset - last_offset;
379 gold_assert(skip <= sizeof discard);
380
381 iov[iov_index].iov_base = discard;
382 iov[iov_index].iov_len = skip;
383 ++iov_index;
384
385 want += skip;
386 }
387
388 iov[iov_index].iov_base = i_entry.buffer;
389 iov[iov_index].iov_len = i_entry.size;
390 ++iov_index;
391
392 want += i_entry.size;
393
394 last_offset = i_entry.file_offset + i_entry.size;
395 }
396
397 gold_assert(iov_index < sizeof iov / sizeof iov[0]);
398
399 if (::lseek(this->descriptor_, base + first_offset, SEEK_SET) < 0)
400 gold_fatal(_("%s: lseek failed: %s"),
401 this->filename().c_str(), strerror(errno));
402
403 ssize_t got = ::readv(this->descriptor_, iov, iov_index);
404
405 if (got < 0)
406 gold_fatal(_("%s: readv failed: %s"),
407 this->filename().c_str(), strerror(errno));
408 if (got != want)
409 gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"),
410 this->filename().c_str(),
411 got, want, static_cast<long long>(base + first_offset));
412}
413
414// Read several pieces of data from the file.
415
416void
417File_read::read_multiple(off_t base, const Read_multiple& rm)
418{
419 size_t count = rm.size();
420 size_t i = 0;
421 while (i < count)
422 {
423 // Find up to MAX_READV_ENTRIES consecutive entries which are
424 // less than one page apart.
425 const Read_multiple_entry& i_entry(rm[i]);
426 off_t i_off = i_entry.file_offset;
427 off_t end_off = i_off + i_entry.size;
428 size_t j;
429 for (j = i + 1; j < count; ++j)
430 {
431 if (j - i >= File_read::max_readv_entries)
432 break;
433 const Read_multiple_entry& j_entry(rm[j]);
434 off_t j_off = j_entry.file_offset;
435 gold_assert(j_off >= end_off);
436 off_t j_end_off = j_off + j_entry.size;
437 if (j_end_off - end_off >= File_read::page_size)
438 break;
439 end_off = j_end_off;
440 }
441
442 if (j == i + 1)
443 this->read(base + i_off, i_entry.size, i_entry.buffer);
444 else
445 {
446 File_read::View* view = this->find_view(base + i_off,
447 end_off - i_off);
448 if (view == NULL)
449 this->do_readv(base, rm, i, j - i);
450 else
451 {
452 const unsigned char* v = (view->data()
453 + (base + i_off - view->start()));
454 for (size_t k = i; k < j; ++k)
455 {
456 const Read_multiple_entry& k_entry(rm[k]);
457 gold_assert(k_entry.file_offset - i_off + k_entry.size
458 <= end_off - i_off);
459 memcpy(k_entry.buffer,
460 v + (k_entry.file_offset - i_off),
461 k_entry.size);
462 }
463 }
464 }
465
466 i = j;
467 }
468}
469
470// Mark all views as no longer cached.
471
472void
473File_read::clear_view_cache_marks()
474{
475 // Just ignore this if there are multiple objects associated with
476 // the file. Otherwise we will wind up uncaching and freeing some
477 // views for other objects.
478 if (this->object_count_ > 1)
479 return;
480
481 for (Views::iterator p = this->views_.begin();
482 p != this->views_.end();
483 ++p)
484 p->second->clear_cache();
485 for (Saved_views::iterator p = this->saved_views_.begin();
486 p != this->saved_views_.end();
487 ++p)
488 (*p)->clear_cache();
489}
490
491// Remove all the file views. For a file which has multiple
492// associated objects (i.e., an archive), we keep accessed views
493// around until next time, in the hopes that they will be useful for
494// the next object.
bae7f79e
ILT
495
496void
497File_read::clear_views(bool destroying)
498{
fcf29b24
ILT
499 Views::iterator p = this->views_.begin();
500 while (p != this->views_.end())
bae7f79e 501 {
cb295612
ILT
502 bool should_delete;
503 if (p->second->is_locked())
504 should_delete = false;
505 else if (destroying)
506 should_delete = true;
507 else if (p->second->should_cache())
508 should_delete = false;
509 else if (this->object_count_ > 1 && p->second->accessed())
510 should_delete = false;
511 else
512 should_delete = true;
513
514 if (should_delete)
fcf29b24
ILT
515 {
516 delete p->second;
517
518 // map::erase invalidates only the iterator to the deleted
519 // element.
520 Views::iterator pe = p;
521 ++p;
522 this->views_.erase(pe);
523 }
ead1e424 524 else
bae7f79e 525 {
a3ad94ed 526 gold_assert(!destroying);
cb295612 527 p->second->clear_accessed();
fcf29b24 528 ++p;
bae7f79e 529 }
ead1e424 530 }
ead1e424 531
fcf29b24
ILT
532 Saved_views::iterator q = this->saved_views_.begin();
533 while (q != this->saved_views_.end())
ead1e424 534 {
cb295612 535 if (!(*q)->is_locked())
bae7f79e 536 {
fcf29b24
ILT
537 delete *q;
538 q = this->saved_views_.erase(q);
ead1e424
ILT
539 }
540 else
541 {
a3ad94ed 542 gold_assert(!destroying);
fcf29b24 543 ++q;
bae7f79e
ILT
544 }
545 }
546}
547
e44fcf3b
ILT
548// Print statistical information to stderr. This is used for --stats.
549
550void
551File_read::print_stats()
552{
553 fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"),
554 program_name, File_read::total_mapped_bytes);
555 fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"),
556 program_name, File_read::maximum_mapped_bytes);
557}
558
bae7f79e
ILT
559// Class File_view.
560
561File_view::~File_view()
562{
a3ad94ed 563 gold_assert(this->file_.is_locked());
bae7f79e
ILT
564 this->view_->unlock();
565}
566
567// Class Input_file.
568
5a6f7e2d
ILT
569// Create a file for testing.
570
17a1d0a9
ILT
571Input_file::Input_file(const Task* task, const char* name,
572 const unsigned char* contents, off_t size)
5a6f7e2d
ILT
573 : file_()
574{
575 this->input_argument_ =
51dee2fe 576 new Input_file_argument(name, false, "", Position_dependent_options());
17a1d0a9 577 bool ok = file_.open(task, name, contents, size);
5a6f7e2d
ILT
578 gold_assert(ok);
579}
580
581// Open the file.
582
51dee2fe
ILT
583// If the filename is not absolute, we assume it is in the current
584// directory *except* when:
585// A) input_argument_->is_lib() is true; or
586// B) input_argument_->extra_search_path() is not empty.
587// In both cases, we look in extra_search_path + library_path to find
588// the file location, rather than the current directory.
589
75f2446e 590bool
17a1d0a9
ILT
591Input_file::open(const General_options& options, const Dirsearch& dirpath,
592 const Task* task)
bae7f79e
ILT
593{
594 std::string name;
51dee2fe
ILT
595
596 // Case 1: name is an absolute file, just try to open it
597 // Case 2: name is relative but is_lib is false and extra_search_path
598 // is empty
599 if (IS_ABSOLUTE_PATH (this->input_argument_->name())
600 || (!this->input_argument_->is_lib()
601 && this->input_argument_->extra_search_path() == NULL))
e2aacd2c
ILT
602 {
603 name = this->input_argument_->name();
604 this->found_name_ = name;
605 }
51dee2fe
ILT
606 // Case 3: is_lib is true
607 else if (this->input_argument_->is_lib())
bae7f79e 608 {
51dee2fe
ILT
609 // We don't yet support extra_search_path with -l.
610 gold_assert(this->input_argument_->extra_search_path() == NULL);
bae7f79e 611 std::string n1("lib");
5a6f7e2d 612 n1 += this->input_argument_->name();
bae7f79e 613 std::string n2;
61611222
ILT
614 if (options.is_static()
615 || this->input_argument_->options().do_static_search())
f6ce93d6
ILT
616 n1 += ".a";
617 else
618 {
619 n2 = n1 + ".a";
620 n1 += ".so";
621 }
ad2d6943 622 name = dirpath.find(n1, n2, &this->is_in_sysroot_);
bae7f79e
ILT
623 if (name.empty())
624 {
a0c4fb0a 625 gold_error(_("cannot find -l%s"),
75f2446e
ILT
626 this->input_argument_->name());
627 return false;
bae7f79e 628 }
e2aacd2c
ILT
629 if (n2.empty() || name[name.length() - 1] == 'o')
630 this->found_name_ = n1;
631 else
632 this->found_name_ = n2;
bae7f79e 633 }
51dee2fe
ILT
634 // Case 4: extra_search_path is not empty
635 else
636 {
637 gold_assert(this->input_argument_->extra_search_path() != NULL);
638
639 // First, check extra_search_path.
640 name = this->input_argument_->extra_search_path();
641 if (!IS_DIR_SEPARATOR (name[name.length() - 1]))
642 name += '/';
643 name += this->input_argument_->name();
644 struct stat dummy_stat;
645 if (::stat(name.c_str(), &dummy_stat) < 0)
646 {
647 // extra_search_path failed, so check the normal search-path.
ad2d6943
ILT
648 name = dirpath.find(this->input_argument_->name(), "",
649 &this->is_in_sysroot_);
51dee2fe
ILT
650 if (name.empty())
651 {
a0c4fb0a 652 gold_error(_("cannot find %s"),
75f2446e
ILT
653 this->input_argument_->name());
654 return false;
51dee2fe
ILT
655 }
656 }
e2aacd2c 657 this->found_name_ = this->input_argument_->name();
51dee2fe
ILT
658 }
659
660 // Now that we've figured out where the file lives, try to open it.
17a1d0a9 661 if (!this->file_.open(task, name))
bae7f79e 662 {
a0c4fb0a 663 gold_error(_("cannot open %s: %s"),
75f2446e
ILT
664 name.c_str(), strerror(errno));
665 return false;
bae7f79e 666 }
75f2446e
ILT
667
668 return true;
bae7f79e
ILT
669}
670
671} // End namespace gold.
This page took 0.100624 seconds and 4 git commands to generate.