OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.20.1/] [gold/] [fileread.cc] - Blame information for rev 205

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 205 julius
// fileread.cc -- read files for gold
2
 
3
// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
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
 
23
#include "gold.h"
24
 
25
#include <cstring>
26
#include <cerrno>
27
#include <fcntl.h>
28
#include <unistd.h>
29
#include <sys/mman.h>
30
#include <sys/uio.h>
31
#include <sys/stat.h>
32
#include "filenames.h"
33
 
34
#include "debug.h"
35
#include "parameters.h"
36
#include "options.h"
37
#include "dirsearch.h"
38
#include "target.h"
39
#include "binary.h"
40
#include "descriptors.h"
41
#include "fileread.h"
42
 
43
#ifndef HAVE_READV
44
struct iovec { void* iov_base; size_t iov_len };
45
ssize_t
46
readv(int, const iovec*, int)
47
{
48
  gold_unreachable();
49
}
50
#endif
51
 
52
namespace gold
53
{
54
 
55
// Class File_read::View.
56
 
57
File_read::View::~View()
58
{
59
  gold_assert(!this->is_locked());
60
  if (!this->mapped_)
61
    delete[] this->data_;
62
  else
63
    {
64
      if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
65
        gold_warning(_("munmap failed: %s"), strerror(errno));
66
 
67
      File_read::current_mapped_bytes -= this->size_;
68
    }
69
}
70
 
71
void
72
File_read::View::lock()
73
{
74
  ++this->lock_count_;
75
}
76
 
77
void
78
File_read::View::unlock()
79
{
80
  gold_assert(this->lock_count_ > 0);
81
  --this->lock_count_;
82
}
83
 
84
bool
85
File_read::View::is_locked()
86
{
87
  return this->lock_count_ > 0;
88
}
89
 
90
// Class File_read.
91
 
92
// The File_read static variables.
93
unsigned long long File_read::total_mapped_bytes;
94
unsigned long long File_read::current_mapped_bytes;
95
unsigned long long File_read::maximum_mapped_bytes;
96
 
97
File_read::~File_read()
98
{
99
  gold_assert(this->token_.is_writable());
100
  if (this->is_descriptor_opened_)
101
    {
102
      release_descriptor(this->descriptor_, true);
103
      this->descriptor_ = -1;
104
      this->is_descriptor_opened_ = false;
105
    }
106
  this->name_.clear();
107
  this->clear_views(true);
108
}
109
 
110
// Open the file.
111
 
112
bool
113
File_read::open(const Task* task, const std::string& name)
114
{
115
  gold_assert(this->token_.is_writable()
116
              && this->descriptor_ < 0
117
              && !this->is_descriptor_opened_
118
              && this->name_.empty());
119
  this->name_ = name;
120
 
121
  this->descriptor_ = open_descriptor(-1, this->name_.c_str(),
122
                                      O_RDONLY);
123
 
124
  if (this->descriptor_ >= 0)
125
    {
126
      this->is_descriptor_opened_ = true;
127
      struct stat s;
128
      if (::fstat(this->descriptor_, &s) < 0)
129
        gold_error(_("%s: fstat failed: %s"),
130
                   this->name_.c_str(), strerror(errno));
131
      this->size_ = s.st_size;
132
      gold_debug(DEBUG_FILES, "Attempt to open %s succeeded",
133
                 this->name_.c_str());
134
 
135
      this->token_.add_writer(task);
136
    }
137
 
138
  return this->descriptor_ >= 0;
139
}
140
 
141
// Open the file with the contents in memory.
142
 
143
bool
144
File_read::open(const Task* task, const std::string& name,
145
                const unsigned char* contents, off_t size)
146
{
147
  gold_assert(this->token_.is_writable()
148
              && this->descriptor_ < 0
149
              && !this->is_descriptor_opened_
150
              && this->name_.empty());
151
  this->name_ = name;
152
  this->contents_ = contents;
153
  this->size_ = size;
154
  this->token_.add_writer(task);
155
  return true;
156
}
157
 
158
// Reopen a descriptor if necessary.
159
 
160
void
161
File_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
 
174
// Release the file.  This is called when we are done with the file in
175
// a Task.
176
 
177
void
178
File_read::release()
179
{
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
 
188
  // Only clear views if there is only one attached object.  Otherwise
189
  // we waste time trying to clear cached archive views.  Similarly
190
  // for releasing the descriptor.
191
  if (this->object_count_ <= 1)
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
    }
200
 
201
  this->released_ = true;
202
}
203
 
204
// Lock the file.
205
 
206
void
207
File_read::lock(const Task* task)
208
{
209
  gold_assert(this->released_);
210
  this->token_.add_writer(task);
211
  this->released_ = false;
212
}
213
 
214
// Unlock the file.
215
 
216
void
217
File_read::unlock(const Task* task)
218
{
219
  this->release();
220
  this->token_.remove_writer(task);
221
}
222
 
223
// Return whether the file is locked.
224
 
225
bool
226
File_read::is_locked() const
227
{
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;
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.
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.
241
 
242
inline File_read::View*
243
File_read::find_view(off_t start, section_size_type size,
244
                     unsigned int byteshift, File_read::View** vshifted) const
245
{
246
  if (vshifted != NULL)
247
    *vshifted = NULL;
248
 
249
  off_t page = File_read::page_offset(start);
250
 
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)
256
    {
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
            }
266
 
267
          if (vshifted != NULL && *vshifted == NULL)
268
            *vshifted = p->second;
269
        }
270
 
271
      ++p;
272
    }
273
 
274
  return NULL;
275
}
276
 
277
// Read SIZE bytes from the file starting at offset START.  Read into
278
// the buffer at P.
279
 
280
void
281
File_read::do_read(off_t start, section_size_type size, void* p)
282
{
283
  ssize_t bytes;
284
  if (this->contents_ != NULL)
285
    {
286
      bytes = this->size_ - start;
287
      if (static_cast<section_size_type>(bytes) >= size)
288
        {
289
          memcpy(p, this->contents_ + start, size);
290
          return;
291
        }
292
    }
293
  else
294
    {
295
      this->reopen_descriptor();
296
      bytes = ::pread(this->descriptor_, p, size, start);
297
      if (static_cast<section_size_type>(bytes) == size)
298
        return;
299
 
300
      if (bytes < 0)
301
        {
302
          gold_fatal(_("%s: pread failed: %s"),
303
                     this->filename().c_str(), strerror(errno));
304
          return;
305
        }
306
    }
307
 
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));
313
}
314
 
315
// Read data from the file.
316
 
317
void
318
File_read::read(off_t start, section_size_type size, void* p)
319
{
320
  const File_read::View* pv = this->find_view(start, size, -1U, NULL);
321
  if (pv != NULL)
322
    {
323
      memcpy(p, pv->data() + (start - pv->start() + pv->byteshift()), size);
324
      return;
325
    }
326
 
327
  this->do_read(start, size, p);
328
}
329
 
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.
333
 
334
void
335
File_read::add_view(File_read::View* v)
336
{
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())
350
    {
351
      v->set_cache();
352
      vold->clear_cache();
353
    }
354
  this->saved_views_.push_back(vold);
355
 
356
  ins.first->second = v;
357
}
358
 
359
// Make a new view with a specified byteshift, reading the data from
360
// the file.
361
 
362
File_read::View*
363
File_read::make_view(off_t start, section_size_type size,
364
                     unsigned int byteshift, bool cache)
365
{
366
  gold_assert(size > 0);
367
 
368
  // Check that start and end of the view are within the file.
369
  if (start > this->size_
370
      || (static_cast<unsigned long long>(size)
371
          > static_cast<unsigned long long>(this->size_ - start)))
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
 
378
  off_t poff = File_read::page_offset(start);
379
 
380
  section_size_type psize = File_read::pages(size + (start - poff));
381
 
382
  if (poff + static_cast<off_t>(psize) >= this->size_)
383
    {
384
      psize = this->size_ - poff;
385
      gold_assert(psize >= size);
386
    }
387
 
388
  File_read::View* v;
389
  if (this->contents_ != NULL || byteshift != 0)
390
    {
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);
395
    }
396
  else
397
    {
398
      this->reopen_descriptor();
399
      void* p = ::mmap(NULL, psize, PROT_READ, MAP_PRIVATE,
400
                       this->descriptor_, poff);
401
      if (p == MAP_FAILED)
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));
407
 
408
      this->mapped_bytes_ += psize;
409
 
410
      const unsigned char* pbytes = static_cast<const unsigned char*>(p);
411
      v = new File_read::View(poff, psize, pbytes, 0, cache, true);
412
    }
413
 
414
  this->add_view(v);
415
 
416
  return v;
417
}
418
 
419
// Find a View or make a new one, shifted as required by the file
420
// offset OFFSET and ALIGNED.
421
 
422
File_read::View*
423
File_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
 
481
// Get a view into the file.
482
 
483
const unsigned char*
484
File_read::get_view(off_t offset, off_t start, section_size_type size,
485
                    bool aligned, bool cache)
486
{
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());
490
}
491
 
492
File_view*
493
File_read::get_lasting_view(off_t offset, off_t start, section_size_type size,
494
                            bool aligned, bool cache)
495
{
496
  File_read::View* pv = this->find_or_make_view(offset, start, size,
497
                                                aligned, cache);
498
  pv->lock();
499
  return new File_view(*this, pv,
500
                       (pv->data()
501
                        + (offset + start - pv->start() + pv->byteshift())));
502
}
503
 
504
// Use readv to read COUNT entries from RM starting at START.  BASE
505
// must be added to all file offsets in RM.
506
 
507
void
508
File_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
 
543
  this->reopen_descriptor();
544
 
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
 
564
void
565
File_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,
595
                                                  end_off - i_off,
596
                                                  -1U, NULL);
597
          if (view == NULL)
598
            this->do_readv(base, rm, i, j - i);
599
          else
600
            {
601
              const unsigned char* v = (view->data()
602
                                        + (base + i_off - view->start()
603
                                           + view->byteshift()));
604
              for (size_t k = i; k < j; ++k)
605
                {
606
                  const Read_multiple_entry& k_entry(rm[k]);
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));
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
 
625
void
626
File_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.
648
 
649
void
650
File_read::clear_views(bool destroying)
651
{
652
  Views::iterator p = this->views_.begin();
653
  while (p != this->views_.end())
654
    {
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)
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
        }
677
      else
678
        {
679
          gold_assert(!destroying);
680
          p->second->clear_accessed();
681
          ++p;
682
        }
683
    }
684
 
685
  Saved_views::iterator q = this->saved_views_.begin();
686
  while (q != this->saved_views_.end())
687
    {
688
      if (!(*q)->is_locked())
689
        {
690
          delete *q;
691
          q = this->saved_views_.erase(q);
692
        }
693
      else
694
        {
695
          gold_assert(!destroying);
696
          ++q;
697
        }
698
    }
699
}
700
 
701
// Print statistical information to stderr.  This is used for --stats.
702
 
703
void
704
File_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
 
712
// Class File_view.
713
 
714
File_view::~File_view()
715
{
716
  gold_assert(this->file_.is_locked());
717
  this->view_->unlock();
718
}
719
 
720
// Class Input_file.
721
 
722
// Create a file for testing.
723
 
724
Input_file::Input_file(const Task* task, const char* name,
725
                       const unsigned char* contents, off_t size)
726
  : file_()
727
{
728
  this->input_argument_ =
729
    new Input_file_argument(name, Input_file_argument::INPUT_FILE_TYPE_FILE,
730
                            "", false, Position_dependent_options());
731
  bool ok = this->file_.open(task, name, contents, size);
732
  gold_assert(ok);
733
}
734
 
735
// Return the position dependent options in force for this file.
736
 
737
const Position_dependent_options&
738
Input_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
 
745
const char*
746
Input_file::name() const
747
{
748
  return this->input_argument_->name();
749
}
750
 
751
// Return whether this file is in a system directory.
752
 
753
bool
754
Input_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
 
761
// Return whether we are only reading symbols.
762
 
763
bool
764
Input_file::just_symbols() const
765
{
766
  return this->input_argument_->just_symbols();
767
}
768
 
769
// Return whether this is a file that we will search for in the list
770
// of directories.
771
 
772
bool
773
Input_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_->is_searched_file()
778
              || this->input_argument_->extra_search_path() != NULL));
779
}
780
 
781
// Return the file last modification time.  Calls gold_fatal if the stat
782
// system call failed.
783
 
784
Timespec
785
File_read::get_mtime()
786
{
787
  struct stat file_stat;
788
  this->reopen_descriptor();
789
 
790
  if (fstat(this->descriptor_, &file_stat) < 0)
791
    gold_fatal(_("%s: stat failed: %s"), this->name_.c_str(),
792
               strerror(errno));
793
  // TODO: do a configure check if st_mtim is present and get the
794
  // nanoseconds part if it is.
795
  return Timespec(file_stat.st_mtime, 0);
796
}
797
 
798
// Open the file.
799
 
800
// If the filename is not absolute, we assume it is in the current
801
// directory *except* when:
802
//    A) input_argument_->is_lib() is true;
803
//    B) input_argument_->is_searched_file() is true; or
804
//    C) input_argument_->extra_search_path() is not empty.
805
// In each, we look in extra_search_path + library_path to find
806
// the file location, rather than the current directory.
807
 
808
bool
809
Input_file::open(const Dirsearch& dirpath, const Task* task, int *pindex)
810
{
811
  std::string name;
812
 
813
  // Case 1: name is an absolute file, just try to open it
814
  // Case 2: name is relative but is_lib is false, is_searched_file is false,
815
  //         and extra_search_path is empty
816
  if (IS_ABSOLUTE_PATH(this->input_argument_->name())
817
      || (!this->input_argument_->is_lib()
818
          && !this->input_argument_->is_searched_file()
819
          && this->input_argument_->extra_search_path() == NULL))
820
    {
821
      name = this->input_argument_->name();
822
      this->found_name_ = name;
823
    }
824
  // Case 3: is_lib is true or is_searched_file is true
825
  else if (this->input_argument_->is_lib()
826
           || this->input_argument_->is_searched_file())
827
    {
828
      // We don't yet support extra_search_path with -l.
829
      gold_assert(this->input_argument_->extra_search_path() == NULL);
830
      std::string n1, n2;
831
      if (this->input_argument_->is_lib())
832
        {
833
          n1 = "lib";
834
          n1 += this->input_argument_->name();
835
          if (parameters->options().is_static()
836
              || !this->input_argument_->options().Bdynamic())
837
            n1 += ".a";
838
          else
839
            {
840
              n2 = n1 + ".a";
841
              n1 += ".so";
842
            }
843
        }
844
      else
845
        n1 = this->input_argument_->name();
846
      name = dirpath.find(n1, n2, &this->is_in_sysroot_, pindex);
847
      if (name.empty())
848
        {
849
          gold_error(_("cannot find %s%s"),
850
                     this->input_argument_->is_lib() ? "-l" : "",
851
                     this->input_argument_->name());
852
          return false;
853
        }
854
      if (n2.empty() || name[name.length() - 1] == 'o')
855
        this->found_name_ = n1;
856
      else
857
        this->found_name_ = n2;
858
    }
859
  // Case 4: extra_search_path is not empty
860
  else
861
    {
862
      gold_assert(this->input_argument_->extra_search_path() != NULL);
863
 
864
      // First, check extra_search_path.
865
      name = this->input_argument_->extra_search_path();
866
      if (!IS_DIR_SEPARATOR (name[name.length() - 1]))
867
        name += '/';
868
      name += this->input_argument_->name();
869
      struct stat dummy_stat;
870
      if (*pindex > 0 || ::stat(name.c_str(), &dummy_stat) < 0)
871
        {
872
          // extra_search_path failed, so check the normal search-path.
873
          int index = *pindex;
874
          if (index > 0)
875
            --index;
876
          name = dirpath.find(this->input_argument_->name(), "",
877
                              &this->is_in_sysroot_, &index);
878
          if (name.empty())
879
            {
880
              gold_error(_("cannot find %s"),
881
                         this->input_argument_->name());
882
              return false;
883
            }
884
          *pindex = index + 1;
885
        }
886
      this->found_name_ = this->input_argument_->name();
887
    }
888
 
889
  // Now that we've figured out where the file lives, try to open it.
890
 
891
  General_options::Object_format format =
892
    this->input_argument_->options().format_enum();
893
  bool ok;
894
  if (format == General_options::OBJECT_FORMAT_ELF)
895
    ok = this->file_.open(task, name);
896
  else
897
    {
898
      gold_assert(format == General_options::OBJECT_FORMAT_BINARY);
899
      ok = this->open_binary(task, name);
900
    }
901
 
902
  if (!ok)
903
    {
904
      gold_error(_("cannot open %s: %s"),
905
                 name.c_str(), strerror(errno));
906
      return false;
907
    }
908
 
909
  return true;
910
}
911
 
912
// Open a file for --format binary.
913
 
914
bool
915
Input_file::open_binary(const Task* task, const std::string& name)
916
{
917
  // In order to open a binary file, we need machine code, size, and
918
  // endianness.  We may not have a valid target at this point, in
919
  // which case we use the default target.
920
  parameters_force_valid_target();
921
  const Target& target(parameters->target());
922
 
923
  Binary_to_elf binary_to_elf(target.machine_code(),
924
                              target.get_size(),
925
                              target.is_big_endian(),
926
                              name);
927
  if (!binary_to_elf.convert(task))
928
    return false;
929
  return this->file_.open(task, name, binary_to_elf.converted_data_leak(),
930
                          binary_to_elf.converted_size());
931
}
932
 
933
} // End namespace gold.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.