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