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