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