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