Update copyright years
[deliverable/binutils-gdb.git] / gold / fileread.cc
CommitLineData
bae7f79e
ILT
1// fileread.cc -- read files for gold
2
4b95cf5c 3// Copyright (C) 2006-2014 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)
4c8a1de1 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 211 gold_debug(DEBUG_FILES, "Attempt to open %s succeeded",
4c8a1de1 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 230 this->whole_file_view_ = new View(0, size, contents, 0, false,
4c8a1de1 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{
cdd7e244
CC
332 gold_assert(start <= this->size_
333 && (static_cast<unsigned long long>(size)
334 <= static_cast<unsigned long long>(this->size_ - start)));
335
39d0cb0e
ILT
336 if (vshifted != NULL)
337 *vshifted = NULL;
338
2c849493
ILT
339 // If we have the whole file mmapped, and the alignment is right,
340 // we can return it.
341 if (this->whole_file_view_)
342 if (byteshift == -1U || byteshift == 0)
343 return this->whole_file_view_;
344
ead1e424 345 off_t page = File_read::page_offset(start);
cb295612 346
39d0cb0e
ILT
347 unsigned int bszero = 0;
348 Views::const_iterator p = this->views_.upper_bound(std::make_pair(page - 1,
349 bszero));
350
351 while (p != this->views_.end() && p->first.first <= page)
cb295612 352 {
39d0cb0e
ILT
353 if (p->second->start() <= start
354 && (p->second->start() + static_cast<off_t>(p->second->size())
355 >= start + static_cast<off_t>(size)))
356 {
357 if (byteshift == -1U || byteshift == p->second->byteshift())
358 {
359 p->second->set_accessed();
360 return p->second;
361 }
cb295612 362
39d0cb0e
ILT
363 if (vshifted != NULL && *vshifted == NULL)
364 *vshifted = p->second;
365 }
cb295612 366
39d0cb0e
ILT
367 ++p;
368 }
cb295612 369
39d0cb0e 370 return NULL;
bae7f79e
ILT
371}
372
82dcae9d 373// Read SIZE bytes from the file starting at offset START. Read into
9eb9fa57 374// the buffer at P.
bae7f79e 375
9eb9fa57 376void
2a00e4fb 377File_read::do_read(off_t start, section_size_type size, void* p)
bae7f79e 378{
8cce6718 379 ssize_t bytes;
2c849493 380 if (this->whole_file_view_ != NULL)
bae7f79e 381 {
9eb9fa57 382 bytes = this->size_ - start;
8cce6718 383 if (static_cast<section_size_type>(bytes) >= size)
9eb9fa57 384 {
2c849493 385 memcpy(p, this->whole_file_view_->data() + start, size);
9eb9fa57
ILT
386 return;
387 }
bae7f79e 388 }
9eb9fa57 389 else
82dcae9d 390 {
2a00e4fb 391 this->reopen_descriptor();
8cce6718 392
4c8a1de1
RM
393 char *read_ptr = static_cast<char *>(p);
394 off_t read_pos = start;
395 size_t to_read = size;
396 do
9eb9fa57 397 {
4c8a1de1
RM
398 bytes = ::pread(this->descriptor_, read_ptr, to_read, read_pos);
399 if (bytes < 0)
400 gold_fatal(_("%s: pread failed: %s"),
401 this->filename().c_str(), strerror(errno));
402
403 read_pos += bytes;
404 read_ptr += bytes;
405 to_read -= bytes;
406 if (to_read == 0)
407 return;
9eb9fa57 408 }
4c8a1de1
RM
409 while (bytes > 0);
410
411 bytes = size - to_read;
bae7f79e 412 }
9eb9fa57 413
75f2446e
ILT
414 gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
415 this->filename().c_str(),
416 static_cast<long long>(bytes),
417 static_cast<long long>(size),
418 static_cast<long long>(start));
bae7f79e
ILT
419}
420
ba45d247
ILT
421// Read data from the file.
422
bae7f79e 423void
2a00e4fb 424File_read::read(off_t start, section_size_type size, void* p)
ba45d247 425{
39d0cb0e 426 const File_read::View* pv = this->find_view(start, size, -1U, NULL);
ba45d247
ILT
427 if (pv != NULL)
428 {
39d0cb0e 429 memcpy(p, pv->data() + (start - pv->start() + pv->byteshift()), size);
ba45d247
ILT
430 return;
431 }
432
9eb9fa57 433 this->do_read(start, size, p);
bae7f79e
ILT
434}
435
39d0cb0e
ILT
436// Add a new view. There may already be an existing view at this
437// offset. If there is, the new view will be larger, and should
438// replace the old view.
bae7f79e 439
39d0cb0e
ILT
440void
441File_read::add_view(File_read::View* v)
bae7f79e 442{
39d0cb0e
ILT
443 std::pair<Views::iterator, bool> ins =
444 this->views_.insert(std::make_pair(std::make_pair(v->start(),
445 v->byteshift()),
446 v));
447 if (ins.second)
448 return;
449
450 // There was an existing view at this offset. It must not be large
451 // enough. We can't delete it here, since something might be using
452 // it; we put it on a list to be deleted when the file is unlocked.
453 File_read::View* vold = ins.first->second;
454 gold_assert(vold->size() < v->size());
455 if (vold->should_cache())
cb295612 456 {
39d0cb0e
ILT
457 v->set_cache();
458 vold->clear_cache();
cb295612 459 }
39d0cb0e 460 this->saved_views_.push_back(vold);
cb295612 461
39d0cb0e
ILT
462 ins.first->second = v;
463}
ead1e424 464
39d0cb0e
ILT
465// Make a new view with a specified byteshift, reading the data from
466// the file.
ead1e424 467
39d0cb0e
ILT
468File_read::View*
469File_read::make_view(off_t start, section_size_type size,
470 unsigned int byteshift, bool cache)
471{
3f2e6a2d 472 gold_assert(size > 0);
cdd7e244
CC
473 gold_assert(start <= this->size_
474 && (static_cast<unsigned long long>(size)
475 <= static_cast<unsigned long long>(this->size_ - start)));
a9caad02 476
39d0cb0e 477 off_t poff = File_read::page_offset(start);
ead1e424 478
fe8718a4 479 section_size_type psize = File_read::pages(size + (start - poff));
bae7f79e 480
8d32f935 481 if (poff + static_cast<off_t>(psize) >= this->size_)
82dcae9d
ILT
482 {
483 psize = this->size_ - poff;
fe8718a4 484 gold_assert(psize >= size);
82dcae9d 485 }
ead1e424 486
88597d34
ILT
487 void* p;
488 View::Data_ownership ownership;
a2a5469e 489 if (byteshift != 0)
d1038c21 490 {
88597d34
ILT
491 p = malloc(psize + byteshift);
492 if (p == NULL)
493 gold_nomem();
39d0cb0e 494 memset(p, 0, byteshift);
88597d34
ILT
495 this->do_read(poff, psize, static_cast<unsigned char*>(p) + byteshift);
496 ownership = View::DATA_ALLOCATED_ARRAY;
d1038c21
ILT
497 }
498 else
499 {
2a00e4fb 500 this->reopen_descriptor();
88597d34
ILT
501 p = ::mmap(NULL, psize, PROT_READ, MAP_PRIVATE, this->descriptor_, poff);
502 if (p != MAP_FAILED)
503 {
504 ownership = View::DATA_MMAPPED;
505 this->mapped_bytes_ += psize;
506 }
507 else
508 {
509 p = malloc(psize);
510 if (p == NULL)
511 gold_nomem();
512 this->do_read(poff, psize, p);
513 ownership = View::DATA_ALLOCATED_ARRAY;
514 }
d1038c21 515 }
ead1e424 516
88597d34
ILT
517 const unsigned char* pbytes = static_cast<const unsigned char*>(p);
518 File_read::View* v = new File_read::View(poff, psize, pbytes, byteshift,
519 cache, ownership);
520
39d0cb0e
ILT
521 this->add_view(v);
522
82dcae9d 523 return v;
bae7f79e
ILT
524}
525
39d0cb0e
ILT
526// Find a View or make a new one, shifted as required by the file
527// offset OFFSET and ALIGNED.
528
529File_read::View*
530File_read::find_or_make_view(off_t offset, off_t start,
531 section_size_type size, bool aligned, bool cache)
532{
cdd7e244
CC
533 // Check that start and end of the view are within the file.
534 if (start > this->size_
535 || (static_cast<unsigned long long>(size)
4c8a1de1 536 > static_cast<unsigned long long>(this->size_ - start)))
cdd7e244 537 gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds "
4c8a1de1 538 "size of file; the file may be corrupt"),
cdd7e244
CC
539 this->filename().c_str(),
540 static_cast<long long>(size),
541 static_cast<long long>(start));
542
39d0cb0e
ILT
543 unsigned int byteshift;
544 if (offset == 0)
545 byteshift = 0;
546 else
547 {
548 unsigned int target_size = (!parameters->target_valid()
549 ? 64
550 : parameters->target().get_size());
551 byteshift = offset & ((target_size / 8) - 1);
552
553 // Set BYTESHIFT to the number of dummy bytes which must be
554 // inserted before the data in order for this data to be
555 // aligned.
556 if (byteshift != 0)
557 byteshift = (target_size / 8) - byteshift;
558 }
559
a2a5469e
CC
560 // If --map-whole-files is set, make sure we have a
561 // whole file view. Options may not yet be ready, e.g.,
562 // when reading a version script. We then default to
4a599bdd 563 // --no-map-whole-files.
a2a5469e
CC
564 if (this->whole_file_view_ == NULL
565 && parameters->options_valid()
566 && parameters->options().map_whole_files())
567 this->whole_file_view_ = this->make_view(0, this->size_, 0, cache);
568
39d0cb0e
ILT
569 // Try to find a View with the required BYTESHIFT.
570 File_read::View* vshifted;
571 File_read::View* v = this->find_view(offset + start, size,
572 aligned ? byteshift : -1U,
573 &vshifted);
574 if (v != NULL)
575 {
576 if (cache)
577 v->set_cache();
578 return v;
579 }
580
581 // If VSHIFTED is not NULL, then it has the data we need, but with
582 // the wrong byteshift.
583 v = vshifted;
584 if (v != NULL)
585 {
586 gold_assert(aligned);
587
88597d34
ILT
588 unsigned char* pbytes;
589 pbytes = static_cast<unsigned char*>(malloc(v->size() + byteshift));
590 if (pbytes == NULL)
591 gold_nomem();
39d0cb0e
ILT
592 memset(pbytes, 0, byteshift);
593 memcpy(pbytes + byteshift, v->data() + v->byteshift(), v->size());
594
2c849493 595 File_read::View* shifted_view =
4c8a1de1 596 new File_read::View(v->start(), v->size(), pbytes, byteshift,
2c849493 597 cache, View::DATA_ALLOCATED_ARRAY);
39d0cb0e
ILT
598
599 this->add_view(shifted_view);
600 return shifted_view;
601 }
602
603 // Make a new view. If we don't need an aligned view, use a
604 // byteshift of 0, so that we can use mmap.
605 return this->make_view(offset + start, size,
606 aligned ? byteshift : 0,
607 cache);
608}
609
17a1d0a9 610// Get a view into the file.
bae7f79e
ILT
611
612const unsigned char*
39d0cb0e
ILT
613File_read::get_view(off_t offset, off_t start, section_size_type size,
614 bool aligned, bool cache)
ba45d247 615{
39d0cb0e
ILT
616 File_read::View* pv = this->find_or_make_view(offset, start, size,
617 aligned, cache);
618 return pv->data() + (offset + start - pv->start() + pv->byteshift());
bae7f79e
ILT
619}
620
621File_view*
39d0cb0e
ILT
622File_read::get_lasting_view(off_t offset, off_t start, section_size_type size,
623 bool aligned, bool cache)
bae7f79e 624{
39d0cb0e
ILT
625 File_read::View* pv = this->find_or_make_view(offset, start, size,
626 aligned, cache);
bae7f79e 627 pv->lock();
39d0cb0e
ILT
628 return new File_view(*this, pv,
629 (pv->data()
630 + (offset + start - pv->start() + pv->byteshift())));
bae7f79e
ILT
631}
632
cb295612
ILT
633// Use readv to read COUNT entries from RM starting at START. BASE
634// must be added to all file offsets in RM.
635
636void
637File_read::do_readv(off_t base, const Read_multiple& rm, size_t start,
638 size_t count)
639{
640 unsigned char discard[File_read::page_size];
641 iovec iov[File_read::max_readv_entries * 2];
642 size_t iov_index = 0;
643
644 off_t first_offset = rm[start].file_offset;
645 off_t last_offset = first_offset;
646 ssize_t want = 0;
647 for (size_t i = 0; i < count; ++i)
648 {
649 const Read_multiple_entry& i_entry(rm[start + i]);
650
651 if (i_entry.file_offset > last_offset)
652 {
653 size_t skip = i_entry.file_offset - last_offset;
654 gold_assert(skip <= sizeof discard);
655
656 iov[iov_index].iov_base = discard;
657 iov[iov_index].iov_len = skip;
658 ++iov_index;
659
660 want += skip;
661 }
662
663 iov[iov_index].iov_base = i_entry.buffer;
664 iov[iov_index].iov_len = i_entry.size;
665 ++iov_index;
666
667 want += i_entry.size;
668
669 last_offset = i_entry.file_offset + i_entry.size;
670 }
671
2a00e4fb
ILT
672 this->reopen_descriptor();
673
cb295612
ILT
674 gold_assert(iov_index < sizeof iov / sizeof iov[0]);
675
676 if (::lseek(this->descriptor_, base + first_offset, SEEK_SET) < 0)
677 gold_fatal(_("%s: lseek failed: %s"),
678 this->filename().c_str(), strerror(errno));
679
680 ssize_t got = ::readv(this->descriptor_, iov, iov_index);
681
682 if (got < 0)
683 gold_fatal(_("%s: readv failed: %s"),
684 this->filename().c_str(), strerror(errno));
685 if (got != want)
686 gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"),
687 this->filename().c_str(),
688 got, want, static_cast<long long>(base + first_offset));
689}
690
ecb351e9
ILT
691// Portable IOV_MAX.
692
693#if !defined(HAVE_READV)
694#define GOLD_IOV_MAX 1
695#elif defined(IOV_MAX)
696#define GOLD_IOV_MAX IOV_MAX
697#else
698#define GOLD_IOV_MAX (File_read::max_readv_entries * 2)
699#endif
700
cb295612
ILT
701// Read several pieces of data from the file.
702
703void
704File_read::read_multiple(off_t base, const Read_multiple& rm)
705{
ecb351e9 706 static size_t iov_max = GOLD_IOV_MAX;
cb295612
ILT
707 size_t count = rm.size();
708 size_t i = 0;
709 while (i < count)
710 {
711 // Find up to MAX_READV_ENTRIES consecutive entries which are
712 // less than one page apart.
713 const Read_multiple_entry& i_entry(rm[i]);
714 off_t i_off = i_entry.file_offset;
715 off_t end_off = i_off + i_entry.size;
716 size_t j;
717 for (j = i + 1; j < count; ++j)
718 {
ecb351e9 719 if (j - i >= File_read::max_readv_entries || j - i >= iov_max / 2)
cb295612
ILT
720 break;
721 const Read_multiple_entry& j_entry(rm[j]);
722 off_t j_off = j_entry.file_offset;
723 gold_assert(j_off >= end_off);
724 off_t j_end_off = j_off + j_entry.size;
725 if (j_end_off - end_off >= File_read::page_size)
726 break;
727 end_off = j_end_off;
728 }
729
730 if (j == i + 1)
731 this->read(base + i_off, i_entry.size, i_entry.buffer);
732 else
733 {
734 File_read::View* view = this->find_view(base + i_off,
39d0cb0e
ILT
735 end_off - i_off,
736 -1U, NULL);
cb295612
ILT
737 if (view == NULL)
738 this->do_readv(base, rm, i, j - i);
739 else
740 {
741 const unsigned char* v = (view->data()
39d0cb0e
ILT
742 + (base + i_off - view->start()
743 + view->byteshift()));
cb295612
ILT
744 for (size_t k = i; k < j; ++k)
745 {
746 const Read_multiple_entry& k_entry(rm[k]);
be2f3dec 747 gold_assert((convert_to_section_size_type(k_entry.file_offset
4c8a1de1
RM
748 - i_off)
749 + k_entry.size)
be2f3dec 750 <= convert_to_section_size_type(end_off
4c8a1de1 751 - i_off));
cb295612
ILT
752 memcpy(k_entry.buffer,
753 v + (k_entry.file_offset - i_off),
754 k_entry.size);
755 }
756 }
757 }
758
759 i = j;
760 }
761}
762
763// Mark all views as no longer cached.
764
765void
766File_read::clear_view_cache_marks()
767{
768 // Just ignore this if there are multiple objects associated with
769 // the file. Otherwise we will wind up uncaching and freeing some
770 // views for other objects.
771 if (this->object_count_ > 1)
772 return;
773
774 for (Views::iterator p = this->views_.begin();
775 p != this->views_.end();
776 ++p)
777 p->second->clear_cache();
778 for (Saved_views::iterator p = this->saved_views_.begin();
779 p != this->saved_views_.end();
780 ++p)
781 (*p)->clear_cache();
782}
783
784// Remove all the file views. For a file which has multiple
785// associated objects (i.e., an archive), we keep accessed views
786// around until next time, in the hopes that they will be useful for
787// the next object.
bae7f79e
ILT
788
789void
a2a5469e 790File_read::clear_views(Clear_views_mode mode)
bae7f79e 791{
a2a5469e
CC
792 bool keep_files_mapped = (parameters->options_valid()
793 && parameters->options().keep_files_mapped());
fcf29b24
ILT
794 Views::iterator p = this->views_.begin();
795 while (p != this->views_.end())
bae7f79e 796 {
cb295612 797 bool should_delete;
a2a5469e 798 if (p->second->is_locked() || p->second->is_permanent_view())
cb295612 799 should_delete = false;
a2a5469e 800 else if (mode == CLEAR_VIEWS_ALL)
cb295612 801 should_delete = true;
a19fefdc
ILT
802 else if ((p->second->should_cache()
803 || p->second == this->whole_file_view_)
804 && keep_files_mapped)
cb295612 805 should_delete = false;
a2a5469e 806 else if (this->object_count_ > 1
4c8a1de1
RM
807 && p->second->accessed()
808 && mode != CLEAR_VIEWS_ARCHIVE)
cb295612
ILT
809 should_delete = false;
810 else
811 should_delete = true;
812
813 if (should_delete)
fcf29b24 814 {
a2a5469e
CC
815 if (p->second == this->whole_file_view_)
816 this->whole_file_view_ = NULL;
fcf29b24
ILT
817 delete p->second;
818
819 // map::erase invalidates only the iterator to the deleted
820 // element.
821 Views::iterator pe = p;
822 ++p;
823 this->views_.erase(pe);
824 }
ead1e424 825 else
bae7f79e 826 {
cb295612 827 p->second->clear_accessed();
fcf29b24 828 ++p;
bae7f79e 829 }
ead1e424 830 }
ead1e424 831
fcf29b24
ILT
832 Saved_views::iterator q = this->saved_views_.begin();
833 while (q != this->saved_views_.end())
ead1e424 834 {
cb295612 835 if (!(*q)->is_locked())
bae7f79e 836 {
fcf29b24
ILT
837 delete *q;
838 q = this->saved_views_.erase(q);
ead1e424
ILT
839 }
840 else
841 {
a2a5469e 842 gold_assert(mode != CLEAR_VIEWS_ALL);
fcf29b24 843 ++q;
bae7f79e
ILT
844 }
845 }
846}
847
e44fcf3b
ILT
848// Print statistical information to stderr. This is used for --stats.
849
850void
851File_read::print_stats()
852{
853 fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"),
854 program_name, File_read::total_mapped_bytes);
855 fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"),
856 program_name, File_read::maximum_mapped_bytes);
857}
858
bae7f79e
ILT
859// Class File_view.
860
861File_view::~File_view()
862{
a3ad94ed 863 gold_assert(this->file_.is_locked());
bae7f79e
ILT
864 this->view_->unlock();
865}
866
867// Class Input_file.
868
effe8365
CC
869// Create a file given just the filename.
870
871Input_file::Input_file(const char* name)
872 : found_name_(), file_(), is_in_sysroot_(false), format_(FORMAT_NONE)
873{
874 this->input_argument_ =
875 new Input_file_argument(name, Input_file_argument::INPUT_FILE_TYPE_FILE,
4c8a1de1 876 "", false, Position_dependent_options());
effe8365
CC
877}
878
5a6f7e2d
ILT
879// Create a file for testing.
880
2ea97941 881Input_file::Input_file(const Task* task, const char* name,
17a1d0a9 882 const unsigned char* contents, off_t size)
5a6f7e2d
ILT
883 : file_()
884{
885 this->input_argument_ =
2ea97941 886 new Input_file_argument(name, Input_file_argument::INPUT_FILE_TYPE_FILE,
4c8a1de1 887 "", false, Position_dependent_options());
2ea97941 888 bool ok = this->file_.open(task, name, contents, size);
5a6f7e2d
ILT
889 gold_assert(ok);
890}
891
14144f39
ILT
892// Return the position dependent options in force for this file.
893
894const Position_dependent_options&
895Input_file::options() const
896{
897 return this->input_argument_->options();
898}
899
900// Return the name given by the user. For -lc this will return "c".
901
902const char*
903Input_file::name() const
88dd47ac
ILT
904{
905 return this->input_argument_->name();
906}
907
fd9d194f
ILT
908// Return whether this file is in a system directory.
909
910bool
911Input_file::is_in_system_directory() const
912{
913 if (this->is_in_sysroot())
914 return true;
915 return parameters->options().is_in_system_directory(this->filename());
916}
917
88dd47ac
ILT
918// Return whether we are only reading symbols.
919
920bool
921Input_file::just_symbols() const
922{
923 return this->input_argument_->just_symbols();
924}
14144f39 925
15f8229b
ILT
926// Return whether this is a file that we will search for in the list
927// of directories.
928
929bool
930Input_file::will_search_for() const
931{
932 return (!IS_ABSOLUTE_PATH(this->input_argument_->name())
933 && (this->input_argument_->is_lib()
ae3b5189 934 || this->input_argument_->is_searched_file()
15f8229b
ILT
935 || this->input_argument_->extra_search_path() != NULL));
936}
937
98fa85cb
ILT
938// Return the file last modification time. Calls gold_fatal if the stat
939// system call failed.
940
941Timespec
942File_read::get_mtime()
943{
944 struct stat file_stat;
945 this->reopen_descriptor();
cdc29364 946
98fa85cb
ILT
947 if (fstat(this->descriptor_, &file_stat) < 0)
948 gold_fatal(_("%s: stat failed: %s"), this->name_.c_str(),
949 strerror(errno));
5d329b7d
ILT
950#ifdef HAVE_STAT_ST_MTIM
951 return Timespec(file_stat.st_mtim.tv_sec, file_stat.st_mtim.tv_nsec);
952#else
98fa85cb 953 return Timespec(file_stat.st_mtime, 0);
5d329b7d 954#endif
98fa85cb
ILT
955}
956
69517287 957// Try to find a file in the extra search dirs. Returns true on success.
42218b9f 958
69517287
RÁE
959bool
960Input_file::try_extra_search_path(int* pindex,
961 const Input_file_argument* input_argument,
962 std::string filename, std::string* found_name,
963 std::string* namep)
964{
42218b9f
RÁE
965 if (input_argument->extra_search_path() == NULL)
966 return false;
967
968 std::string name = input_argument->extra_search_path();
69517287 969 if (!IS_DIR_SEPARATOR(name[name.length() - 1]))
42218b9f
RÁE
970 name += '/';
971 name += filename;
972
973 struct stat dummy_stat;
974 if (*pindex > 0 || ::stat(name.c_str(), &dummy_stat) < 0)
975 return false;
976
977 *found_name = filename;
978 *namep = name;
979 return true;
980}
5a6f7e2d 981
42218b9f 982// Find the actual file.
51dee2fe
ILT
983// If the filename is not absolute, we assume it is in the current
984// directory *except* when:
ae3b5189
CD
985// A) input_argument_->is_lib() is true;
986// B) input_argument_->is_searched_file() is true; or
987// C) input_argument_->extra_search_path() is not empty.
988// In each, we look in extra_search_path + library_path to find
51dee2fe
ILT
989// the file location, rather than the current directory.
990
69517287
RÁE
991bool
992Input_file::find_file(const Dirsearch& dirpath, int* pindex,
993 const Input_file_argument* input_argument,
994 bool* is_in_sysroot,
995 std::string* found_name, std::string* namep)
bae7f79e 996{
2ea97941 997 std::string name;
51dee2fe
ILT
998
999 // Case 1: name is an absolute file, just try to open it
ae3b5189
CD
1000 // Case 2: name is relative but is_lib is false, is_searched_file is false,
1001 // and extra_search_path is empty
42218b9f
RÁE
1002 if (IS_ABSOLUTE_PATH(input_argument->name())
1003 || (!input_argument->is_lib()
1004 && !input_argument->is_searched_file()
1005 && input_argument->extra_search_path() == NULL))
e2aacd2c 1006 {
42218b9f
RÁE
1007 name = input_argument->name();
1008 *found_name = name;
1009 *namep = name;
1010 return true;
e2aacd2c 1011 }
ae3b5189 1012 // Case 3: is_lib is true or is_searched_file is true
42218b9f
RÁE
1013 else if (input_argument->is_lib()
1014 || input_argument->is_searched_file())
bae7f79e 1015 {
1706a06f
ILT
1016 std::vector<std::string> names;
1017 names.reserve(2);
42218b9f 1018 if (input_argument->is_lib())
f6ce93d6 1019 {
1706a06f
ILT
1020 std::string prefix = "lib";
1021 prefix += input_argument->name();
ae3b5189 1022 if (parameters->options().is_static()
42218b9f 1023 || !input_argument->options().Bdynamic())
1706a06f 1024 names.push_back(prefix + ".a");
ae3b5189
CD
1025 else
1026 {
1706a06f
ILT
1027 names.push_back(prefix + ".so");
1028 names.push_back(prefix + ".a");
ae3b5189 1029 }
f6ce93d6 1030 }
ae3b5189 1031 else
1706a06f 1032 names.push_back(input_argument->name());
42218b9f 1033
1706a06f
ILT
1034 for (std::vector<std::string>::const_iterator n = names.begin();
1035 n != names.end();
1036 ++n)
1037 if (Input_file::try_extra_search_path(pindex, input_argument, *n,
1038 found_name, namep))
1039 return true;
42218b9f
RÁE
1040
1041 // It is not in the extra_search_path.
1706a06f 1042 name = dirpath.find(names, is_in_sysroot, pindex, found_name);
2ea97941 1043 if (name.empty())
bae7f79e 1044 {
ae3b5189 1045 gold_error(_("cannot find %s%s"),
1706a06f 1046 input_argument->is_lib() ? "-l" : "",
42218b9f 1047 input_argument->name());
75f2446e 1048 return false;
bae7f79e 1049 }
42218b9f
RÁE
1050 *namep = name;
1051 return true;
bae7f79e 1052 }
51dee2fe
ILT
1053 // Case 4: extra_search_path is not empty
1054 else
1055 {
42218b9f
RÁE
1056 gold_assert(input_argument->extra_search_path() != NULL);
1057
1058 if (try_extra_search_path(pindex, input_argument, input_argument->name(),
1706a06f
ILT
1059 found_name, namep))
1060 return true;
42218b9f
RÁE
1061
1062 // extra_search_path failed, so check the normal search-path.
1063 int index = *pindex;
1064 if (index > 0)
1706a06f
ILT
1065 --index;
1066 name = dirpath.find(std::vector<std::string>(1, input_argument->name()),
1067 is_in_sysroot, &index, found_name);
42218b9f 1068 if (name.empty())
1706a06f
ILT
1069 {
1070 gold_error(_("cannot find %s"),
1071 input_argument->name());
1072 return false;
1073 }
7411e9a8 1074 *namep = name;
42218b9f
RÁE
1075 *pindex = index + 1;
1076 return true;
51dee2fe 1077 }
42218b9f
RÁE
1078}
1079
1080// Open the file.
1081
1082bool
ca09d69a 1083Input_file::open(const Dirsearch& dirpath, const Task* task, int* pindex)
42218b9f
RÁE
1084{
1085 std::string name;
69517287
RÁE
1086 if (!Input_file::find_file(dirpath, pindex, this->input_argument_,
1087 &this->is_in_sysroot_, &this->found_name_, &name))
42218b9f 1088 return false;
51dee2fe
ILT
1089
1090 // Now that we've figured out where the file lives, try to open it.
bc644c6c
ILT
1091
1092 General_options::Object_format format =
7cc619c3 1093 this->input_argument_->options().format_enum();
bc644c6c
ILT
1094 bool ok;
1095 if (format == General_options::OBJECT_FORMAT_ELF)
10165461
DK
1096 {
1097 ok = this->file_.open(task, name);
1098 this->format_ = FORMAT_ELF;
1099 }
bc644c6c
ILT
1100 else
1101 {
1102 gold_assert(format == General_options::OBJECT_FORMAT_BINARY);
2ea97941 1103 ok = this->open_binary(task, name);
10165461 1104 this->format_ = FORMAT_BINARY;
bc644c6c
ILT
1105 }
1106
1107 if (!ok)
bae7f79e 1108 {
a0c4fb0a 1109 gold_error(_("cannot open %s: %s"),
2ea97941 1110 name.c_str(), strerror(errno));
10165461 1111 this->format_ = FORMAT_NONE;
75f2446e 1112 return false;
bae7f79e 1113 }
75f2446e
ILT
1114
1115 return true;
bae7f79e
ILT
1116}
1117
bc644c6c
ILT
1118// Open a file for --format binary.
1119
1120bool
2ea97941 1121Input_file::open_binary(const Task* task, const std::string& name)
bc644c6c
ILT
1122{
1123 // In order to open a binary file, we need machine code, size, and
0daa6f62
ILT
1124 // endianness. We may not have a valid target at this point, in
1125 // which case we use the default target.
029ba973
ILT
1126 parameters_force_valid_target();
1127 const Target& target(parameters->target());
bc644c6c 1128
029ba973
ILT
1129 Binary_to_elf binary_to_elf(target.machine_code(),
1130 target.get_size(),
1131 target.is_big_endian(),
2ea97941 1132 name);
bc644c6c
ILT
1133 if (!binary_to_elf.convert(task))
1134 return false;
2ea97941 1135 return this->file_.open(task, name, binary_to_elf.converted_data_leak(),
bc644c6c
ILT
1136 binary_to_elf.converted_size());
1137}
1138
bae7f79e 1139} // End namespace gold.
This page took 0.357984 seconds and 4 git commands to generate.