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