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