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