1 // fileread.cc -- read files for gold
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
32 #include "filenames.h"
35 #include "parameters.h"
37 #include "dirsearch.h"
40 #include "descriptors.h"
46 // Class File_read::View.
48 File_read::View::~View()
50 gold_assert(!this->is_locked());
55 if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
56 gold_warning(_("munmap failed: %s"), strerror(errno));
58 File_read::current_mapped_bytes -= this->size_;
63 File_read::View::lock()
69 File_read::View::unlock()
71 gold_assert(this->lock_count_ > 0);
76 File_read::View::is_locked()
78 return this->lock_count_ > 0;
83 // The File_read static variables.
84 unsigned long long File_read::total_mapped_bytes;
85 unsigned long long File_read::current_mapped_bytes;
86 unsigned long long File_read::maximum_mapped_bytes;
88 File_read::~File_read()
90 gold_assert(this->token_.is_writable());
91 if (this->is_descriptor_opened_)
93 release_descriptor(this->descriptor_, true);
94 this->descriptor_ = -1;
95 this->is_descriptor_opened_ = false;
98 this->clear_views(true);
104 File_read::open(const Task* task, const std::string& name)
106 gold_assert(this->token_.is_writable()
107 && this->descriptor_ < 0
108 && !this->is_descriptor_opened_
109 && this->name_.empty());
112 this->descriptor_ = open_descriptor(-1, this->name_.c_str(),
115 if (this->descriptor_ >= 0)
117 this->is_descriptor_opened_ = true;
119 if (::fstat(this->descriptor_, &s) < 0)
120 gold_error(_("%s: fstat failed: %s"),
121 this->name_.c_str(), strerror(errno));
122 this->size_ = s.st_size;
123 gold_debug(DEBUG_FILES, "Attempt to open %s succeeded",
124 this->name_.c_str());
126 this->token_.add_writer(task);
129 return this->descriptor_ >= 0;
132 // Open the file with the contents in memory.
135 File_read::open(const Task* task, const std::string& name,
136 const unsigned char* contents, off_t size)
138 gold_assert(this->token_.is_writable()
139 && this->descriptor_ < 0
140 && !this->is_descriptor_opened_
141 && this->name_.empty());
143 this->contents_ = contents;
145 this->token_.add_writer(task);
149 // Reopen a descriptor if necessary.
152 File_read::reopen_descriptor()
154 if (!this->is_descriptor_opened_)
156 this->descriptor_ = open_descriptor(this->descriptor_,
159 if (this->descriptor_ < 0)
160 gold_fatal(_("could not reopen file %s"), this->name_.c_str());
161 this->is_descriptor_opened_ = true;
165 // Release the file. This is called when we are done with the file in
171 gold_assert(this->is_locked());
173 File_read::total_mapped_bytes += this->mapped_bytes_;
174 File_read::current_mapped_bytes += this->mapped_bytes_;
175 this->mapped_bytes_ = 0;
176 if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes)
177 File_read::maximum_mapped_bytes = File_read::current_mapped_bytes;
179 // Only clear views if there is only one attached object. Otherwise
180 // we waste time trying to clear cached archive views. Similarly
181 // for releasing the descriptor.
182 if (this->object_count_ <= 1)
184 this->clear_views(false);
185 if (this->is_descriptor_opened_)
187 release_descriptor(this->descriptor_, false);
188 this->is_descriptor_opened_ = false;
192 this->released_ = true;
198 File_read::lock(const Task* task)
200 gold_assert(this->released_);
201 this->token_.add_writer(task);
202 this->released_ = false;
208 File_read::unlock(const Task* task)
211 this->token_.remove_writer(task);
214 // Return whether the file is locked.
217 File_read::is_locked() const
219 if (!this->token_.is_writable())
221 // The file is not locked, so it should have been released.
222 gold_assert(this->released_);
226 // See if we have a view which covers the file starting at START for
227 // SIZE bytes. Return a pointer to the View if found, NULL if not.
228 // If BYTESHIFT is not -1U, the returned View must have the specified
229 // byte shift; otherwise, it may have any byte shift. If VSHIFTED is
230 // not NULL, this sets *VSHIFTED to a view which would have worked if
231 // not for the requested BYTESHIFT.
233 inline File_read::View*
234 File_read::find_view(off_t start, section_size_type size,
235 unsigned int byteshift, File_read::View** vshifted) const
237 if (vshifted != NULL)
240 off_t page = File_read::page_offset(start);
242 unsigned int bszero = 0;
243 Views::const_iterator p = this->views_.upper_bound(std::make_pair(page - 1,
246 while (p != this->views_.end() && p->first.first <= page)
248 if (p->second->start() <= start
249 && (p->second->start() + static_cast<off_t>(p->second->size())
250 >= start + static_cast<off_t>(size)))
252 if (byteshift == -1U || byteshift == p->second->byteshift())
254 p->second->set_accessed();
258 if (vshifted != NULL && *vshifted == NULL)
259 *vshifted = p->second;
268 // Read SIZE bytes from the file starting at offset START. Read into
272 File_read::do_read(off_t start, section_size_type size, void* p)
275 if (this->contents_ != NULL)
277 bytes = this->size_ - start;
278 if (static_cast<section_size_type>(bytes) >= size)
280 memcpy(p, this->contents_ + start, size);
286 this->reopen_descriptor();
287 bytes = ::pread(this->descriptor_, p, size, start);
288 if (static_cast<section_size_type>(bytes) == size)
293 gold_fatal(_("%s: pread failed: %s"),
294 this->filename().c_str(), strerror(errno));
299 gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
300 this->filename().c_str(),
301 static_cast<long long>(bytes),
302 static_cast<long long>(size),
303 static_cast<long long>(start));
306 // Read data from the file.
309 File_read::read(off_t start, section_size_type size, void* p)
311 const File_read::View* pv = this->find_view(start, size, -1U, NULL);
314 memcpy(p, pv->data() + (start - pv->start() + pv->byteshift()), size);
318 this->do_read(start, size, p);
321 // Add a new view. There may already be an existing view at this
322 // offset. If there is, the new view will be larger, and should
323 // replace the old view.
326 File_read::add_view(File_read::View* v)
328 std::pair<Views::iterator, bool> ins =
329 this->views_.insert(std::make_pair(std::make_pair(v->start(),
335 // There was an existing view at this offset. It must not be large
336 // enough. We can't delete it here, since something might be using
337 // it; we put it on a list to be deleted when the file is unlocked.
338 File_read::View* vold = ins.first->second;
339 gold_assert(vold->size() < v->size());
340 if (vold->should_cache())
345 this->saved_views_.push_back(vold);
347 ins.first->second = v;
350 // Make a new view with a specified byteshift, reading the data from
354 File_read::make_view(off_t start, section_size_type size,
355 unsigned int byteshift, bool cache)
357 gold_assert(size > 0);
359 // Check that start and end of the view are within the file.
360 if (start > this->size_
361 || (static_cast<unsigned long long>(size)
362 > static_cast<unsigned long long>(this->size_ - start)))
363 gold_fatal(_("%s: attempt to map %lld bytes at offset %lld exceeds "
364 "size of file; the file may be corrupt"),
365 this->filename().c_str(),
366 static_cast<long long>(size),
367 static_cast<long long>(start));
369 off_t poff = File_read::page_offset(start);
371 section_size_type psize = File_read::pages(size + (start - poff));
373 if (poff + static_cast<off_t>(psize) >= this->size_)
375 psize = this->size_ - poff;
376 gold_assert(psize >= size);
380 if (this->contents_ != NULL || byteshift != 0)
382 unsigned char* p = new unsigned char[psize + byteshift];
383 memset(p, 0, byteshift);
384 this->do_read(poff, psize, p + byteshift);
385 v = new File_read::View(poff, psize, p, byteshift, cache, false);
389 this->reopen_descriptor();
390 void* p = ::mmap(NULL, psize, PROT_READ, MAP_PRIVATE,
391 this->descriptor_, poff);
393 gold_fatal(_("%s: mmap offset %lld size %lld failed: %s"),
394 this->filename().c_str(),
395 static_cast<long long>(poff),
396 static_cast<long long>(psize),
399 this->mapped_bytes_ += psize;
401 const unsigned char* pbytes = static_cast<const unsigned char*>(p);
402 v = new File_read::View(poff, psize, pbytes, 0, cache, true);
410 // Find a View or make a new one, shifted as required by the file
411 // offset OFFSET and ALIGNED.
414 File_read::find_or_make_view(off_t offset, off_t start,
415 section_size_type size, bool aligned, bool cache)
417 unsigned int byteshift;
422 unsigned int target_size = (!parameters->target_valid()
424 : parameters->target().get_size());
425 byteshift = offset & ((target_size / 8) - 1);
427 // Set BYTESHIFT to the number of dummy bytes which must be
428 // inserted before the data in order for this data to be
431 byteshift = (target_size / 8) - byteshift;
434 // Try to find a View with the required BYTESHIFT.
435 File_read::View* vshifted;
436 File_read::View* v = this->find_view(offset + start, size,
437 aligned ? byteshift : -1U,
446 // If VSHIFTED is not NULL, then it has the data we need, but with
447 // the wrong byteshift.
451 gold_assert(aligned);
453 unsigned char* pbytes = new unsigned char[v->size() + byteshift];
454 memset(pbytes, 0, byteshift);
455 memcpy(pbytes + byteshift, v->data() + v->byteshift(), v->size());
457 File_read::View* shifted_view = new File_read::View(v->start(), v->size(),
461 this->add_view(shifted_view);
465 // Make a new view. If we don't need an aligned view, use a
466 // byteshift of 0, so that we can use mmap.
467 return this->make_view(offset + start, size,
468 aligned ? byteshift : 0,
472 // Get a view into the file.
475 File_read::get_view(off_t offset, off_t start, section_size_type size,
476 bool aligned, bool cache)
478 File_read::View* pv = this->find_or_make_view(offset, start, size,
480 return pv->data() + (offset + start - pv->start() + pv->byteshift());
484 File_read::get_lasting_view(off_t offset, off_t start, section_size_type size,
485 bool aligned, bool cache)
487 File_read::View* pv = this->find_or_make_view(offset, start, size,
490 return new File_view(*this, pv,
492 + (offset + start - pv->start() + pv->byteshift())));
495 // Use readv to read COUNT entries from RM starting at START. BASE
496 // must be added to all file offsets in RM.
499 File_read::do_readv(off_t base, const Read_multiple& rm, size_t start,
502 unsigned char discard[File_read::page_size];
503 iovec iov[File_read::max_readv_entries * 2];
504 size_t iov_index = 0;
506 off_t first_offset = rm[start].file_offset;
507 off_t last_offset = first_offset;
509 for (size_t i = 0; i < count; ++i)
511 const Read_multiple_entry& i_entry(rm[start + i]);
513 if (i_entry.file_offset > last_offset)
515 size_t skip = i_entry.file_offset - last_offset;
516 gold_assert(skip <= sizeof discard);
518 iov[iov_index].iov_base = discard;
519 iov[iov_index].iov_len = skip;
525 iov[iov_index].iov_base = i_entry.buffer;
526 iov[iov_index].iov_len = i_entry.size;
529 want += i_entry.size;
531 last_offset = i_entry.file_offset + i_entry.size;
534 this->reopen_descriptor();
536 gold_assert(iov_index < sizeof iov / sizeof iov[0]);
538 if (::lseek(this->descriptor_, base + first_offset, SEEK_SET) < 0)
539 gold_fatal(_("%s: lseek failed: %s"),
540 this->filename().c_str(), strerror(errno));
542 ssize_t got = ::readv(this->descriptor_, iov, iov_index);
545 gold_fatal(_("%s: readv failed: %s"),
546 this->filename().c_str(), strerror(errno));
548 gold_fatal(_("%s: file too short: read only %zd of %zd bytes at %lld"),
549 this->filename().c_str(),
550 got, want, static_cast<long long>(base + first_offset));
553 // Read several pieces of data from the file.
556 File_read::read_multiple(off_t base, const Read_multiple& rm)
558 size_t count = rm.size();
562 // Find up to MAX_READV_ENTRIES consecutive entries which are
563 // less than one page apart.
564 const Read_multiple_entry& i_entry(rm[i]);
565 off_t i_off = i_entry.file_offset;
566 off_t end_off = i_off + i_entry.size;
568 for (j = i + 1; j < count; ++j)
570 if (j - i >= File_read::max_readv_entries)
572 const Read_multiple_entry& j_entry(rm[j]);
573 off_t j_off = j_entry.file_offset;
574 gold_assert(j_off >= end_off);
575 off_t j_end_off = j_off + j_entry.size;
576 if (j_end_off - end_off >= File_read::page_size)
582 this->read(base + i_off, i_entry.size, i_entry.buffer);
585 File_read::View* view = this->find_view(base + i_off,
589 this->do_readv(base, rm, i, j - i);
592 const unsigned char* v = (view->data()
593 + (base + i_off - view->start()
594 + view->byteshift()));
595 for (size_t k = i; k < j; ++k)
597 const Read_multiple_entry& k_entry(rm[k]);
598 gold_assert((convert_to_section_size_type(k_entry.file_offset
601 <= convert_to_section_size_type(end_off
603 memcpy(k_entry.buffer,
604 v + (k_entry.file_offset - i_off),
614 // Mark all views as no longer cached.
617 File_read::clear_view_cache_marks()
619 // Just ignore this if there are multiple objects associated with
620 // the file. Otherwise we will wind up uncaching and freeing some
621 // views for other objects.
622 if (this->object_count_ > 1)
625 for (Views::iterator p = this->views_.begin();
626 p != this->views_.end();
628 p->second->clear_cache();
629 for (Saved_views::iterator p = this->saved_views_.begin();
630 p != this->saved_views_.end();
635 // Remove all the file views. For a file which has multiple
636 // associated objects (i.e., an archive), we keep accessed views
637 // around until next time, in the hopes that they will be useful for
641 File_read::clear_views(bool destroying)
643 Views::iterator p = this->views_.begin();
644 while (p != this->views_.end())
647 if (p->second->is_locked())
648 should_delete = false;
650 should_delete = true;
651 else if (p->second->should_cache())
652 should_delete = false;
653 else if (this->object_count_ > 1 && p->second->accessed())
654 should_delete = false;
656 should_delete = true;
662 // map::erase invalidates only the iterator to the deleted
664 Views::iterator pe = p;
666 this->views_.erase(pe);
670 gold_assert(!destroying);
671 p->second->clear_accessed();
676 Saved_views::iterator q = this->saved_views_.begin();
677 while (q != this->saved_views_.end())
679 if (!(*q)->is_locked())
682 q = this->saved_views_.erase(q);
686 gold_assert(!destroying);
692 // Print statistical information to stderr. This is used for --stats.
695 File_read::print_stats()
697 fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"),
698 program_name, File_read::total_mapped_bytes);
699 fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"),
700 program_name, File_read::maximum_mapped_bytes);
705 File_view::~File_view()
707 gold_assert(this->file_.is_locked());
708 this->view_->unlock();
713 // Create a file for testing.
715 Input_file::Input_file(const Task* task, const char* name,
716 const unsigned char* contents, off_t size)
719 this->input_argument_ =
720 new Input_file_argument(name, false, "", false,
721 Position_dependent_options());
722 bool ok = this->file_.open(task, name, contents, size);
726 // Return the position dependent options in force for this file.
728 const Position_dependent_options&
729 Input_file::options() const
731 return this->input_argument_->options();
734 // Return the name given by the user. For -lc this will return "c".
737 Input_file::name() const
739 return this->input_argument_->name();
742 // Return whether this file is in a system directory.
745 Input_file::is_in_system_directory() const
747 if (this->is_in_sysroot())
749 return parameters->options().is_in_system_directory(this->filename());
752 // Return whether we are only reading symbols.
755 Input_file::just_symbols() const
757 return this->input_argument_->just_symbols();
760 // Return whether this is a file that we will search for in the list
764 Input_file::will_search_for() const
766 return (!IS_ABSOLUTE_PATH(this->input_argument_->name())
767 && (this->input_argument_->is_lib()
768 || this->input_argument_->extra_search_path() != NULL));
773 // If the filename is not absolute, we assume it is in the current
774 // directory *except* when:
775 // A) input_argument_->is_lib() is true; or
776 // B) input_argument_->extra_search_path() is not empty.
777 // In both cases, we look in extra_search_path + library_path to find
778 // the file location, rather than the current directory.
781 Input_file::open(const Dirsearch& dirpath, const Task* task, int *pindex)
785 // Case 1: name is an absolute file, just try to open it
786 // Case 2: name is relative but is_lib is false and extra_search_path
788 if (IS_ABSOLUTE_PATH(this->input_argument_->name())
789 || (!this->input_argument_->is_lib()
790 && this->input_argument_->extra_search_path() == NULL))
792 name = this->input_argument_->name();
793 this->found_name_ = name;
795 // Case 3: is_lib is true
796 else if (this->input_argument_->is_lib())
798 // We don't yet support extra_search_path with -l.
799 gold_assert(this->input_argument_->extra_search_path() == NULL);
800 std::string n1("lib");
801 n1 += this->input_argument_->name();
803 if (parameters->options().is_static()
804 || !this->input_argument_->options().Bdynamic())
811 name = dirpath.find(n1, n2, &this->is_in_sysroot_, pindex);
814 gold_error(_("cannot find -l%s"),
815 this->input_argument_->name());
818 if (n2.empty() || name[name.length() - 1] == 'o')
819 this->found_name_ = n1;
821 this->found_name_ = n2;
823 // Case 4: extra_search_path is not empty
826 gold_assert(this->input_argument_->extra_search_path() != NULL);
828 // First, check extra_search_path.
829 name = this->input_argument_->extra_search_path();
830 if (!IS_DIR_SEPARATOR (name[name.length() - 1]))
832 name += this->input_argument_->name();
833 struct stat dummy_stat;
834 if (*pindex > 0 || ::stat(name.c_str(), &dummy_stat) < 0)
836 // extra_search_path failed, so check the normal search-path.
840 name = dirpath.find(this->input_argument_->name(), "",
841 &this->is_in_sysroot_, &index);
844 gold_error(_("cannot find %s"),
845 this->input_argument_->name());
850 this->found_name_ = this->input_argument_->name();
853 // Now that we've figured out where the file lives, try to open it.
855 General_options::Object_format format =
856 this->input_argument_->options().format_enum();
858 if (format == General_options::OBJECT_FORMAT_ELF)
859 ok = this->file_.open(task, name);
862 gold_assert(format == General_options::OBJECT_FORMAT_BINARY);
863 ok = this->open_binary(task, name);
868 gold_error(_("cannot open %s: %s"),
869 name.c_str(), strerror(errno));
876 // Open a file for --format binary.
879 Input_file::open_binary(const Task* task, const std::string& name)
881 // In order to open a binary file, we need machine code, size, and
882 // endianness. We may not have a valid target at this point, in
883 // which case we use the default target.
884 const Target* target;
885 if (parameters->target_valid())
886 target = ¶meters->target();
888 target = ¶meters->default_target();
890 Binary_to_elf binary_to_elf(target->machine_code(),
892 target->is_big_endian(),
894 if (!binary_to_elf.convert(task))
896 return this->file_.open(task, name, binary_to_elf.converted_data_leak(),
897 binary_to_elf.converted_size());
900 } // End namespace gold.