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