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