Checking this in for: Jonas Maebe <jonas.maebe@elis.ugent.be>
[deliverable/binutils-gdb.git] / gold / fileread.cc
CommitLineData
bae7f79e
ILT
1// fileread.cc -- read files for gold
2
0f7c0701 3// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6cb15b7f
ILT
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>
3d857b98 31#include <sys/stat.h>
51dee2fe 32#include "filenames.h"
bae7f79e 33
2285a610 34#include "debug.h"
bc644c6c 35#include "parameters.h"
bae7f79e
ILT
36#include "options.h"
37#include "dirsearch.h"
bc644c6c
ILT
38#include "target.h"
39#include "binary.h"
2a00e4fb 40#include "descriptors.h"
bae7f79e
ILT
41#include "fileread.h"
42
d9a893b8
ILT
43#ifndef HAVE_READV
44struct iovec { void* iov_base; size_t iov_len };
45ssize_t
46readv(int, const iovec*, int)
47{
48 gold_unreachable();
49}
50#endif
51
bae7f79e
ILT
52namespace gold
53{
54
55// Class File_read::View.
56
57File_read::View::~View()
58{
a3ad94ed 59 gold_assert(!this->is_locked());
2c849493 60 switch (this->data_ownership_)
d1038c21 61 {
2c849493
ILT
62 case DATA_ALLOCATED_ARRAY:
63 delete[] this->data_;
64 break;
65 case DATA_MMAPPED:
d1038c21 66 if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
75f2446e 67 gold_warning(_("munmap failed: %s"), strerror(errno));
e44fcf3b 68 File_read::current_mapped_bytes -= this->size_;
2c849493
ILT
69 break;
70 case DATA_NOT_OWNED:
71 break;
72 default:
73 gold_unreachable();
d1038c21 74 }
bae7f79e
ILT
75}
76
77void
78File_read::View::lock()
79{
80 ++this->lock_count_;
81}
82
83void
84File_read::View::unlock()
85{
a3ad94ed 86 gold_assert(this->lock_count_ > 0);
bae7f79e
ILT
87 --this->lock_count_;
88}
89
90bool
91File_read::View::is_locked()
92{
93 return this->lock_count_ > 0;
94}
95
96// Class File_read.
97
e44fcf3b
ILT
98// The File_read static variables.
99unsigned long long File_read::total_mapped_bytes;
100unsigned long long File_read::current_mapped_bytes;
101unsigned long long File_read::maximum_mapped_bytes;
102
bae7f79e
ILT
103File_read::~File_read()
104{
17a1d0a9 105 gold_assert(this->token_.is_writable());
2a00e4fb 106 if (this->is_descriptor_opened_)
bae7f79e 107 {
2a00e4fb 108 release_descriptor(this->descriptor_, true);
bae7f79e 109 this->descriptor_ = -1;
2a00e4fb 110 this->is_descriptor_opened_ = false;
bae7f79e
ILT
111 }
112 this->name_.clear();
113 this->clear_views(true);
2c849493
ILT
114 if (this->whole_file_view_)
115 delete this->whole_file_view_;
bae7f79e
ILT
116}
117
5a6f7e2d
ILT
118// Open the file.
119
bae7f79e 120bool
17a1d0a9 121File_read::open(const Task* task, const std::string& name)
bae7f79e 122{
17a1d0a9 123 gold_assert(this->token_.is_writable()
a3ad94ed 124 && this->descriptor_ < 0
2a00e4fb 125 && !this->is_descriptor_opened_
a3ad94ed 126 && this->name_.empty());
bae7f79e 127 this->name_ = name;
82dcae9d 128
2a00e4fb
ILT
129 this->descriptor_ = open_descriptor(-1, this->name_.c_str(),
130 O_RDONLY);
82dcae9d
ILT
131
132 if (this->descriptor_ >= 0)
133 {
2a00e4fb 134 this->is_descriptor_opened_ = true;
82dcae9d
ILT
135 struct stat s;
136 if (::fstat(this->descriptor_, &s) < 0)
75f2446e
ILT
137 gold_error(_("%s: fstat failed: %s"),
138 this->name_.c_str(), strerror(errno));
82dcae9d 139 this->size_ = s.st_size;
2285a610
ILT
140 gold_debug(DEBUG_FILES, "Attempt to open %s succeeded",
141 this->name_.c_str());
82dcae9d 142
2c849493
ILT
143 // Options may not yet be ready e.g. when reading a version
144 // script. We then default to --no-keep-files-mapped.
145 if (parameters->options_valid()
146 && parameters->options().keep_files_mapped())
147 {
148 const unsigned char* contents = static_cast<const unsigned char*>(
149 ::mmap(NULL, this->size_, PROT_READ, MAP_PRIVATE,
150 this->descriptor_, 0));
151 if (contents == MAP_FAILED)
152 gold_fatal(_("%s: mmap failed: %s"), this->filename().c_str(),
153 strerror(errno));
154 this->whole_file_view_ = new View(0, this->size_, contents, 0, false,
155 View::DATA_MMAPPED);
156 this->mapped_bytes_ += this->size_;
157 }
158
1e52a9c1
CS
159 this->token_.add_writer(task);
160 }
82dcae9d 161
bae7f79e
ILT
162 return this->descriptor_ >= 0;
163}
164
bc644c6c 165// Open the file with the contents in memory.
5a6f7e2d
ILT
166
167bool
17a1d0a9
ILT
168File_read::open(const Task* task, const std::string& name,
169 const unsigned char* contents, off_t size)
bae7f79e 170{
17a1d0a9 171 gold_assert(this->token_.is_writable()
5a6f7e2d 172 && this->descriptor_ < 0
2a00e4fb 173 && !this->is_descriptor_opened_
5a6f7e2d
ILT
174 && this->name_.empty());
175 this->name_ = name;
2c849493
ILT
176 this->whole_file_view_ = new View(0, size, contents, 0, false,
177 View::DATA_NOT_OWNED);
82dcae9d 178 this->size_ = size;
17a1d0a9 179 this->token_.add_writer(task);
5a6f7e2d 180 return true;
bae7f79e
ILT
181}
182
2a00e4fb
ILT
183// Reopen a descriptor if necessary.
184
185void
186File_read::reopen_descriptor()
187{
188 if (!this->is_descriptor_opened_)
189 {
190 this->descriptor_ = open_descriptor(this->descriptor_,
191 this->name_.c_str(),
192 O_RDONLY);
193 if (this->descriptor_ < 0)
194 gold_fatal(_("could not reopen file %s"), this->name_.c_str());
195 this->is_descriptor_opened_ = true;
196 }
197}
198
17a1d0a9
ILT
199// Release the file. This is called when we are done with the file in
200// a Task.
201
bae7f79e 202void
17a1d0a9 203File_read::release()
bae7f79e 204{
17a1d0a9
ILT
205 gold_assert(this->is_locked());
206
207 File_read::total_mapped_bytes += this->mapped_bytes_;
208 File_read::current_mapped_bytes += this->mapped_bytes_;
209 this->mapped_bytes_ = 0;
210 if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes)
211 File_read::maximum_mapped_bytes = File_read::current_mapped_bytes;
212
39d0cb0e 213 // Only clear views if there is only one attached object. Otherwise
2a00e4fb
ILT
214 // we waste time trying to clear cached archive views. Similarly
215 // for releasing the descriptor.
39d0cb0e 216 if (this->object_count_ <= 1)
2a00e4fb
ILT
217 {
218 this->clear_views(false);
219 if (this->is_descriptor_opened_)
220 {
221 release_descriptor(this->descriptor_, false);
222 this->is_descriptor_opened_ = false;
223 }
224 }
17a1d0a9
ILT
225
226 this->released_ = true;
bae7f79e
ILT
227}
228
17a1d0a9
ILT
229// Lock the file.
230
bae7f79e 231void
17a1d0a9 232File_read::lock(const Task* task)
bae7f79e 233{
17a1d0a9
ILT
234 gold_assert(this->released_);
235 this->token_.add_writer(task);
236 this->released_ = false;
237}
e44fcf3b 238
17a1d0a9
ILT
239// Unlock the file.
240
241void
242File_read::unlock(const Task* task)
243{
244 this->release();
245 this->token_.remove_writer(task);
bae7f79e
ILT
246}
247
17a1d0a9
ILT
248// Return whether the file is locked.
249
bae7f79e 250bool
7004837e 251File_read::is_locked() const
bae7f79e 252{
17a1d0a9
ILT
253 if (!this->token_.is_writable())
254 return true;
255 // The file is not locked, so it should have been released.
256 gold_assert(this->released_);
257 return false;
bae7f79e
ILT
258}
259
260// See if we have a view which covers the file starting at START for
261// SIZE bytes. Return a pointer to the View if found, NULL if not.
39d0cb0e
ILT
262// If BYTESHIFT is not -1U, the returned View must have the specified
263// byte shift; otherwise, it may have any byte shift. If VSHIFTED is
264// not NULL, this sets *VSHIFTED to a view which would have worked if
265// not for the requested BYTESHIFT.
bae7f79e 266
ead1e424 267inline File_read::View*
39d0cb0e
ILT
268File_read::find_view(off_t start, section_size_type size,
269 unsigned int byteshift, File_read::View** vshifted) const
bae7f79e 270{
39d0cb0e
ILT
271 if (vshifted != NULL)
272 *vshifted = NULL;
273
2c849493
ILT
274 // If we have the whole file mmapped, and the alignment is right,
275 // we can return it.
276 if (this->whole_file_view_)
277 if (byteshift == -1U || byteshift == 0)
278 return this->whole_file_view_;
279
ead1e424 280 off_t page = File_read::page_offset(start);
cb295612 281
39d0cb0e
ILT
282 unsigned int bszero = 0;
283 Views::const_iterator p = this->views_.upper_bound(std::make_pair(page - 1,
284 bszero));
285
286 while (p != this->views_.end() && p->first.first <= page)
cb295612 287 {
39d0cb0e
ILT
288 if (p->second->start() <= start
289 && (p->second->start() + static_cast<off_t>(p->second->size())
290 >= start + static_cast<off_t>(size)))
291 {
292 if (byteshift == -1U || byteshift == p->second->byteshift())
293 {
294 p->second->set_accessed();
295 return p->second;
296 }
cb295612 297
39d0cb0e
ILT
298 if (vshifted != NULL && *vshifted == NULL)
299 *vshifted = p->second;
300 }
cb295612 301
39d0cb0e
ILT
302 ++p;
303 }
cb295612 304
39d0cb0e 305 return NULL;
bae7f79e
ILT
306}
307
82dcae9d 308// Read SIZE bytes from the file starting at offset START. Read into
9eb9fa57 309// the buffer at P.
bae7f79e 310
9eb9fa57 311void
2a00e4fb 312File_read::do_read(off_t start, section_size_type size, void* p)
bae7f79e 313{
8cce6718 314 ssize_t bytes;
2c849493 315 if (this->whole_file_view_ != NULL)
bae7f79e 316 {
9eb9fa57 317 bytes = this->size_ - start;
8cce6718 318 if (static_cast<section_size_type>(bytes) >= size)
9eb9fa57 319 {
2c849493 320 memcpy(p, this->whole_file_view_->data() + start, size);
9eb9fa57
ILT
321 return;
322 }
bae7f79e 323 }
9eb9fa57 324 else
82dcae9d 325 {
2a00e4fb 326 this->reopen_descriptor();
8cce6718
ILT
327 bytes = ::pread(this->descriptor_, p, size, start);
328 if (static_cast<section_size_type>(bytes) == size)
329 return;
330
331 if (bytes < 0)
9eb9fa57 332 {
75f2446e
ILT
333 gold_fatal(_("%s: pread failed: %s"),
334 this->filename().c_str(), strerror(errno));
335 return;
9eb9fa57 336 }
bae7f79e 337 }
9eb9fa57 338
75f2446e
ILT
339 gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
340 this->filename().c_str(),
341 static_cast<long long>(bytes),
342 static_cast<long long>(size),
343 static_cast<long long>(start));
bae7f79e
ILT
344}
345
ba45d247
ILT
346// Read data from the file.
347
bae7f79e 348void
2a00e4fb 349File_read::read(off_t start, section_size_type size, void* p)
ba45d247 350{
39d0cb0e 351 const File_read::View* pv = this->find_view(start, size, -1U, NULL);
ba45d247
ILT
352 if (pv != NULL)
353 {
39d0cb0e 354 memcpy(p, pv->data() + (start - pv->start() + pv->byteshift()), size);
ba45d247
ILT
355 return;
356 }
357
9eb9fa57 358 this->do_read(start, size, p);
bae7f79e
ILT
359}
360
39d0cb0e
ILT
361// Add a new view. There may already be an existing view at this
362// offset. If there is, the new view will be larger, and should
363// replace the old view.
bae7f79e 364
39d0cb0e
ILT
365void
366File_read::add_view(File_read::View* v)
bae7f79e 367{
39d0cb0e
ILT
368 std::pair<Views::iterator, bool> ins =
369 this->views_.insert(std::make_pair(std::make_pair(v->start(),
370 v->byteshift()),
371 v));
372 if (ins.second)
373 return;
374
375 // There was an existing view at this offset. It must not be large
376 // enough. We can't delete it here, since something might be using
377 // it; we put it on a list to be deleted when the file is unlocked.
378 File_read::View* vold = ins.first->second;
379 gold_assert(vold->size() < v->size());
380 if (vold->should_cache())
cb295612 381 {
39d0cb0e
ILT
382 v->set_cache();
383 vold->clear_cache();
cb295612 384 }
39d0cb0e 385 this->saved_views_.push_back(vold);
cb295612 386
39d0cb0e
ILT
387 ins.first->second = v;
388}
ead1e424 389
39d0cb0e
ILT
390// Make a new view with a specified byteshift, reading the data from
391// the file.
ead1e424 392
39d0cb0e
ILT
393File_read::View*
394File_read::make_view(off_t start, section_size_type size,
395 unsigned int byteshift, bool cache)
396{
3f2e6a2d
CC
397 gold_assert(size > 0);
398
a9caad02 399 // Check that start and end of the view are within the file.
de31bda5
ILT
400 if (start > this->size_
401 || (static_cast<unsigned long long>(size)
402 > static_cast<unsigned long long>(this->size_ - start)))
a9caad02
CC
403 gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds "
404 "size of file; the file may be corrupt"),
405 this->filename().c_str(),
406 static_cast<long long>(size),
407 static_cast<long long>(start));
408
39d0cb0e 409 off_t poff = File_read::page_offset(start);
ead1e424 410
fe8718a4 411 section_size_type psize = File_read::pages(size + (start - poff));
bae7f79e 412
8d32f935 413 if (poff + static_cast<off_t>(psize) >= this->size_)
82dcae9d
ILT
414 {
415 psize = this->size_ - poff;
fe8718a4 416 gold_assert(psize >= size);
82dcae9d 417 }
ead1e424 418
39d0cb0e 419 File_read::View* v;
2c849493 420 if (this->whole_file_view_ != NULL || byteshift != 0)
d1038c21 421 {
39d0cb0e
ILT
422 unsigned char* p = new unsigned char[psize + byteshift];
423 memset(p, 0, byteshift);
424 this->do_read(poff, psize, p + byteshift);
2c849493
ILT
425 v = new File_read::View(poff, psize, p, byteshift, cache,
426 View::DATA_ALLOCATED_ARRAY);
d1038c21
ILT
427 }
428 else
429 {
2a00e4fb 430 this->reopen_descriptor();
cb295612 431 void* p = ::mmap(NULL, psize, PROT_READ, MAP_PRIVATE,
d1038c21
ILT
432 this->descriptor_, poff);
433 if (p == MAP_FAILED)
75f2446e
ILT
434 gold_fatal(_("%s: mmap offset %lld size %lld failed: %s"),
435 this->filename().c_str(),
436 static_cast<long long>(poff),
437 static_cast<long long>(psize),
438 strerror(errno));
d1038c21 439
e44fcf3b
ILT
440 this->mapped_bytes_ += psize;
441
d1038c21 442 const unsigned char* pbytes = static_cast<const unsigned char*>(p);
2c849493
ILT
443 v = new File_read::View(poff, psize, pbytes, 0, cache,
444 View::DATA_MMAPPED);
d1038c21 445 }
ead1e424 446
39d0cb0e
ILT
447 this->add_view(v);
448
82dcae9d 449 return v;
bae7f79e
ILT
450}
451
39d0cb0e
ILT
452// Find a View or make a new one, shifted as required by the file
453// offset OFFSET and ALIGNED.
454
455File_read::View*
456File_read::find_or_make_view(off_t offset, off_t start,
457 section_size_type size, bool aligned, bool cache)
458{
459 unsigned int byteshift;
460 if (offset == 0)
461 byteshift = 0;
462 else
463 {
464 unsigned int target_size = (!parameters->target_valid()
465 ? 64
466 : parameters->target().get_size());
467 byteshift = offset & ((target_size / 8) - 1);
468
469 // Set BYTESHIFT to the number of dummy bytes which must be
470 // inserted before the data in order for this data to be
471 // aligned.
472 if (byteshift != 0)
473 byteshift = (target_size / 8) - byteshift;
474 }
475
476 // Try to find a View with the required BYTESHIFT.
477 File_read::View* vshifted;
478 File_read::View* v = this->find_view(offset + start, size,
479 aligned ? byteshift : -1U,
480 &vshifted);
481 if (v != NULL)
482 {
483 if (cache)
484 v->set_cache();
485 return v;
486 }
487
488 // If VSHIFTED is not NULL, then it has the data we need, but with
489 // the wrong byteshift.
490 v = vshifted;
491 if (v != NULL)
492 {
493 gold_assert(aligned);
494
495 unsigned char* pbytes = new unsigned char[v->size() + byteshift];
496 memset(pbytes, 0, byteshift);
497 memcpy(pbytes + byteshift, v->data() + v->byteshift(), v->size());
498
2c849493
ILT
499 File_read::View* shifted_view =
500 new File_read::View(v->start(), v->size(), pbytes, byteshift,
501 cache, View::DATA_ALLOCATED_ARRAY);
39d0cb0e
ILT
502
503 this->add_view(shifted_view);
504 return shifted_view;
505 }
506
507 // Make a new view. If we don't need an aligned view, use a
508 // byteshift of 0, so that we can use mmap.
509 return this->make_view(offset + start, size,
510 aligned ? byteshift : 0,
511 cache);
512}
513
17a1d0a9 514// Get a view into the file.
bae7f79e
ILT
515
516const unsigned char*
39d0cb0e
ILT
517File_read::get_view(off_t offset, off_t start, section_size_type size,
518 bool aligned, bool cache)
ba45d247 519{
39d0cb0e
ILT
520 File_read::View* pv = this->find_or_make_view(offset, start, size,
521 aligned, cache);
522 return pv->data() + (offset + start - pv->start() + pv->byteshift());
bae7f79e
ILT
523}
524
525File_view*
39d0cb0e
ILT
526File_read::get_lasting_view(off_t offset, off_t start, section_size_type size,
527 bool aligned, bool cache)
bae7f79e 528{
39d0cb0e
ILT
529 File_read::View* pv = this->find_or_make_view(offset, start, size,
530 aligned, cache);
bae7f79e 531 pv->lock();
39d0cb0e
ILT
532 return new File_view(*this, pv,
533 (pv->data()
534 + (offset + start - pv->start() + pv->byteshift())));
bae7f79e
ILT
535}
536
cb295612
ILT
537// Use readv to read COUNT entries from RM starting at START. BASE
538// must be added to all file offsets in RM.
539
540void
541File_read::do_readv(off_t base, const Read_multiple& rm, size_t start,
542 size_t count)
543{
544 unsigned char discard[File_read::page_size];
545 iovec iov[File_read::max_readv_entries * 2];
546 size_t iov_index = 0;
547
548 off_t first_offset = rm[start].file_offset;
549 off_t last_offset = first_offset;
550 ssize_t want = 0;
551 for (size_t i = 0; i < count; ++i)
552 {
553 const Read_multiple_entry& i_entry(rm[start + i]);
554
555 if (i_entry.file_offset > last_offset)
556 {
557 size_t skip = i_entry.file_offset - last_offset;
558 gold_assert(skip <= sizeof discard);
559
560 iov[iov_index].iov_base = discard;
561 iov[iov_index].iov_len = skip;
562 ++iov_index;
563
564 want += skip;
565 }
566
567 iov[iov_index].iov_base = i_entry.buffer;
568 iov[iov_index].iov_len = i_entry.size;
569 ++iov_index;
570
571 want += i_entry.size;
572
573 last_offset = i_entry.file_offset + i_entry.size;
574 }
575
2a00e4fb
ILT
576 this->reopen_descriptor();
577
cb295612
ILT
578 gold_assert(iov_index < sizeof iov / sizeof iov[0]);
579
580 if (::lseek(this->descriptor_, base + first_offset, SEEK_SET) < 0)
581 gold_fatal(_("%s: lseek failed: %s"),
582 this->filename().c_str(), strerror(errno));
583
584 ssize_t got = ::readv(this->descriptor_, iov, iov_index);
585
586 if (got < 0)
587 gold_fatal(_("%s: readv failed: %s"),
588 this->filename().c_str(), strerror(errno));
589 if (got != want)
590 gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"),
591 this->filename().c_str(),
592 got, want, static_cast<long long>(base + first_offset));
593}
594
595// Read several pieces of data from the file.
596
597void
598File_read::read_multiple(off_t base, const Read_multiple& rm)
599{
600 size_t count = rm.size();
601 size_t i = 0;
602 while (i < count)
603 {
604 // Find up to MAX_READV_ENTRIES consecutive entries which are
605 // less than one page apart.
606 const Read_multiple_entry& i_entry(rm[i]);
607 off_t i_off = i_entry.file_offset;
608 off_t end_off = i_off + i_entry.size;
609 size_t j;
610 for (j = i + 1; j < count; ++j)
611 {
612 if (j - i >= File_read::max_readv_entries)
613 break;
614 const Read_multiple_entry& j_entry(rm[j]);
615 off_t j_off = j_entry.file_offset;
616 gold_assert(j_off >= end_off);
617 off_t j_end_off = j_off + j_entry.size;
618 if (j_end_off - end_off >= File_read::page_size)
619 break;
620 end_off = j_end_off;
621 }
622
623 if (j == i + 1)
624 this->read(base + i_off, i_entry.size, i_entry.buffer);
625 else
626 {
627 File_read::View* view = this->find_view(base + i_off,
39d0cb0e
ILT
628 end_off - i_off,
629 -1U, NULL);
cb295612
ILT
630 if (view == NULL)
631 this->do_readv(base, rm, i, j - i);
632 else
633 {
634 const unsigned char* v = (view->data()
39d0cb0e
ILT
635 + (base + i_off - view->start()
636 + view->byteshift()));
cb295612
ILT
637 for (size_t k = i; k < j; ++k)
638 {
639 const Read_multiple_entry& k_entry(rm[k]);
be2f3dec
ILT
640 gold_assert((convert_to_section_size_type(k_entry.file_offset
641 - i_off)
642 + k_entry.size)
643 <= convert_to_section_size_type(end_off
644 - i_off));
cb295612
ILT
645 memcpy(k_entry.buffer,
646 v + (k_entry.file_offset - i_off),
647 k_entry.size);
648 }
649 }
650 }
651
652 i = j;
653 }
654}
655
656// Mark all views as no longer cached.
657
658void
659File_read::clear_view_cache_marks()
660{
661 // Just ignore this if there are multiple objects associated with
662 // the file. Otherwise we will wind up uncaching and freeing some
663 // views for other objects.
664 if (this->object_count_ > 1)
665 return;
666
667 for (Views::iterator p = this->views_.begin();
668 p != this->views_.end();
669 ++p)
670 p->second->clear_cache();
671 for (Saved_views::iterator p = this->saved_views_.begin();
672 p != this->saved_views_.end();
673 ++p)
674 (*p)->clear_cache();
675}
676
677// Remove all the file views. For a file which has multiple
678// associated objects (i.e., an archive), we keep accessed views
679// around until next time, in the hopes that they will be useful for
680// the next object.
bae7f79e
ILT
681
682void
683File_read::clear_views(bool destroying)
684{
fcf29b24
ILT
685 Views::iterator p = this->views_.begin();
686 while (p != this->views_.end())
bae7f79e 687 {
cb295612
ILT
688 bool should_delete;
689 if (p->second->is_locked())
690 should_delete = false;
691 else if (destroying)
692 should_delete = true;
693 else if (p->second->should_cache())
694 should_delete = false;
695 else if (this->object_count_ > 1 && p->second->accessed())
696 should_delete = false;
697 else
698 should_delete = true;
699
700 if (should_delete)
fcf29b24
ILT
701 {
702 delete p->second;
703
704 // map::erase invalidates only the iterator to the deleted
705 // element.
706 Views::iterator pe = p;
707 ++p;
708 this->views_.erase(pe);
709 }
ead1e424 710 else
bae7f79e 711 {
a3ad94ed 712 gold_assert(!destroying);
cb295612 713 p->second->clear_accessed();
fcf29b24 714 ++p;
bae7f79e 715 }
ead1e424 716 }
ead1e424 717
fcf29b24
ILT
718 Saved_views::iterator q = this->saved_views_.begin();
719 while (q != this->saved_views_.end())
ead1e424 720 {
cb295612 721 if (!(*q)->is_locked())
bae7f79e 722 {
fcf29b24
ILT
723 delete *q;
724 q = this->saved_views_.erase(q);
ead1e424
ILT
725 }
726 else
727 {
a3ad94ed 728 gold_assert(!destroying);
fcf29b24 729 ++q;
bae7f79e
ILT
730 }
731 }
732}
733
e44fcf3b
ILT
734// Print statistical information to stderr. This is used for --stats.
735
736void
737File_read::print_stats()
738{
739 fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"),
740 program_name, File_read::total_mapped_bytes);
741 fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"),
742 program_name, File_read::maximum_mapped_bytes);
743}
744
bae7f79e
ILT
745// Class File_view.
746
747File_view::~File_view()
748{
a3ad94ed 749 gold_assert(this->file_.is_locked());
bae7f79e
ILT
750 this->view_->unlock();
751}
752
753// Class Input_file.
754
5a6f7e2d
ILT
755// Create a file for testing.
756
17a1d0a9
ILT
757Input_file::Input_file(const Task* task, const char* name,
758 const unsigned char* contents, off_t size)
5a6f7e2d
ILT
759 : file_()
760{
761 this->input_argument_ =
ae3b5189
CD
762 new Input_file_argument(name, Input_file_argument::INPUT_FILE_TYPE_FILE,
763 "", false, Position_dependent_options());
15f8229b 764 bool ok = this->file_.open(task, name, contents, size);
5a6f7e2d
ILT
765 gold_assert(ok);
766}
767
14144f39
ILT
768// Return the position dependent options in force for this file.
769
770const Position_dependent_options&
771Input_file::options() const
772{
773 return this->input_argument_->options();
774}
775
776// Return the name given by the user. For -lc this will return "c".
777
778const char*
779Input_file::name() const
88dd47ac
ILT
780{
781 return this->input_argument_->name();
782}
783
fd9d194f
ILT
784// Return whether this file is in a system directory.
785
786bool
787Input_file::is_in_system_directory() const
788{
789 if (this->is_in_sysroot())
790 return true;
791 return parameters->options().is_in_system_directory(this->filename());
792}
793
88dd47ac
ILT
794// Return whether we are only reading symbols.
795
796bool
797Input_file::just_symbols() const
798{
799 return this->input_argument_->just_symbols();
800}
14144f39 801
15f8229b
ILT
802// Return whether this is a file that we will search for in the list
803// of directories.
804
805bool
806Input_file::will_search_for() const
807{
808 return (!IS_ABSOLUTE_PATH(this->input_argument_->name())
809 && (this->input_argument_->is_lib()
ae3b5189 810 || this->input_argument_->is_searched_file()
15f8229b
ILT
811 || this->input_argument_->extra_search_path() != NULL));
812}
813
98fa85cb
ILT
814// Return the file last modification time. Calls gold_fatal if the stat
815// system call failed.
816
817Timespec
818File_read::get_mtime()
819{
820 struct stat file_stat;
821 this->reopen_descriptor();
822
823 if (fstat(this->descriptor_, &file_stat) < 0)
824 gold_fatal(_("%s: stat failed: %s"), this->name_.c_str(),
825 strerror(errno));
826 // TODO: do a configure check if st_mtim is present and get the
827 // nanoseconds part if it is.
828 return Timespec(file_stat.st_mtime, 0);
829}
830
5a6f7e2d
ILT
831// Open the file.
832
51dee2fe
ILT
833// If the filename is not absolute, we assume it is in the current
834// directory *except* when:
ae3b5189
CD
835// A) input_argument_->is_lib() is true;
836// B) input_argument_->is_searched_file() is true; or
837// C) input_argument_->extra_search_path() is not empty.
838// In each, we look in extra_search_path + library_path to find
51dee2fe
ILT
839// the file location, rather than the current directory.
840
75f2446e 841bool
15f8229b 842Input_file::open(const Dirsearch& dirpath, const Task* task, int *pindex)
bae7f79e
ILT
843{
844 std::string name;
51dee2fe
ILT
845
846 // Case 1: name is an absolute file, just try to open it
ae3b5189
CD
847 // Case 2: name is relative but is_lib is false, is_searched_file is false,
848 // and extra_search_path is empty
15f8229b 849 if (IS_ABSOLUTE_PATH(this->input_argument_->name())
51dee2fe 850 || (!this->input_argument_->is_lib()
ae3b5189 851 && !this->input_argument_->is_searched_file()
51dee2fe 852 && this->input_argument_->extra_search_path() == NULL))
e2aacd2c
ILT
853 {
854 name = this->input_argument_->name();
855 this->found_name_ = name;
856 }
ae3b5189
CD
857 // Case 3: is_lib is true or is_searched_file is true
858 else if (this->input_argument_->is_lib()
859 || this->input_argument_->is_searched_file())
bae7f79e 860 {
51dee2fe
ILT
861 // We don't yet support extra_search_path with -l.
862 gold_assert(this->input_argument_->extra_search_path() == NULL);
ae3b5189
CD
863 std::string n1, n2;
864 if (this->input_argument_->is_lib())
f6ce93d6 865 {
ae3b5189
CD
866 n1 = "lib";
867 n1 += this->input_argument_->name();
868 if (parameters->options().is_static()
869 || !this->input_argument_->options().Bdynamic())
870 n1 += ".a";
871 else
872 {
873 n2 = n1 + ".a";
874 n1 += ".so";
875 }
f6ce93d6 876 }
ae3b5189
CD
877 else
878 n1 = this->input_argument_->name();
15f8229b 879 name = dirpath.find(n1, n2, &this->is_in_sysroot_, pindex);
bae7f79e
ILT
880 if (name.empty())
881 {
ae3b5189
CD
882 gold_error(_("cannot find %s%s"),
883 this->input_argument_->is_lib() ? "-l" : "",
75f2446e
ILT
884 this->input_argument_->name());
885 return false;
bae7f79e 886 }
e2aacd2c
ILT
887 if (n2.empty() || name[name.length() - 1] == 'o')
888 this->found_name_ = n1;
889 else
890 this->found_name_ = n2;
bae7f79e 891 }
51dee2fe
ILT
892 // Case 4: extra_search_path is not empty
893 else
894 {
895 gold_assert(this->input_argument_->extra_search_path() != NULL);
896
897 // First, check extra_search_path.
898 name = this->input_argument_->extra_search_path();
899 if (!IS_DIR_SEPARATOR (name[name.length() - 1]))
900 name += '/';
901 name += this->input_argument_->name();
902 struct stat dummy_stat;
15f8229b 903 if (*pindex > 0 || ::stat(name.c_str(), &dummy_stat) < 0)
51dee2fe
ILT
904 {
905 // extra_search_path failed, so check the normal search-path.
15f8229b
ILT
906 int index = *pindex;
907 if (index > 0)
908 --index;
ad2d6943 909 name = dirpath.find(this->input_argument_->name(), "",
15f8229b 910 &this->is_in_sysroot_, &index);
51dee2fe
ILT
911 if (name.empty())
912 {
a0c4fb0a 913 gold_error(_("cannot find %s"),
75f2446e
ILT
914 this->input_argument_->name());
915 return false;
51dee2fe 916 }
15f8229b 917 *pindex = index + 1;
51dee2fe 918 }
e2aacd2c 919 this->found_name_ = this->input_argument_->name();
51dee2fe
ILT
920 }
921
922 // Now that we've figured out where the file lives, try to open it.
bc644c6c
ILT
923
924 General_options::Object_format format =
7cc619c3 925 this->input_argument_->options().format_enum();
bc644c6c
ILT
926 bool ok;
927 if (format == General_options::OBJECT_FORMAT_ELF)
928 ok = this->file_.open(task, name);
929 else
930 {
931 gold_assert(format == General_options::OBJECT_FORMAT_BINARY);
f1ed28fb 932 ok = this->open_binary(task, name);
bc644c6c
ILT
933 }
934
935 if (!ok)
bae7f79e 936 {
a0c4fb0a 937 gold_error(_("cannot open %s: %s"),
75f2446e
ILT
938 name.c_str(), strerror(errno));
939 return false;
bae7f79e 940 }
75f2446e
ILT
941
942 return true;
bae7f79e
ILT
943}
944
bc644c6c
ILT
945// Open a file for --format binary.
946
947bool
f1ed28fb 948Input_file::open_binary(const Task* task, const std::string& name)
bc644c6c
ILT
949{
950 // In order to open a binary file, we need machine code, size, and
0daa6f62
ILT
951 // endianness. We may not have a valid target at this point, in
952 // which case we use the default target.
029ba973
ILT
953 parameters_force_valid_target();
954 const Target& target(parameters->target());
bc644c6c 955
029ba973
ILT
956 Binary_to_elf binary_to_elf(target.machine_code(),
957 target.get_size(),
958 target.is_big_endian(),
0daa6f62 959 name);
bc644c6c
ILT
960 if (!binary_to_elf.convert(task))
961 return false;
962 return this->file_.open(task, name, binary_to_elf.converted_data_leak(),
963 binary_to_elf.converted_size());
964}
965
bae7f79e 966} // End namespace gold.
This page took 0.204894 seconds and 4 git commands to generate.