1 // fileread.cc -- read files for gold
3 // Copyright 2006, 2007 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.
30 #include "filenames.h"
33 #include "dirsearch.h"
39 // Class File_read::View.
41 File_read::View::~View()
43 gold_assert(!this->is_locked());
48 if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
49 gold_warning(_("munmap failed: %s"), strerror(errno));
51 File_read::current_mapped_bytes -= this->size_;
56 File_read::View::lock()
62 File_read::View::unlock()
64 gold_assert(this->lock_count_ > 0);
69 File_read::View::is_locked()
71 return this->lock_count_ > 0;
76 // The File_read static variables.
77 unsigned long long File_read::total_mapped_bytes;
78 unsigned long long File_read::current_mapped_bytes;
79 unsigned long long File_read::maximum_mapped_bytes;
81 // The File_read class is designed to support file descriptor caching,
82 // but this is not currently implemented.
84 File_read::~File_read()
86 gold_assert(this->token_.is_writable());
87 if (this->descriptor_ >= 0)
89 if (close(this->descriptor_) < 0)
90 gold_warning(_("close of %s failed: %s"),
91 this->name_.c_str(), strerror(errno));
92 this->descriptor_ = -1;
95 this->clear_views(true);
101 File_read::open(const Task* task, const std::string& name)
103 gold_assert(this->token_.is_writable()
104 && this->descriptor_ < 0
105 && this->name_.empty());
108 this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
110 if (this->descriptor_ >= 0)
113 if (::fstat(this->descriptor_, &s) < 0)
114 gold_error(_("%s: fstat failed: %s"),
115 this->name_.c_str(), strerror(errno));
116 this->size_ = s.st_size;
119 this->token_.add_writer(task);
121 return this->descriptor_ >= 0;
124 // Open the file for testing purposes.
127 File_read::open(const Task* task, const std::string& name,
128 const unsigned char* contents, off_t size)
130 gold_assert(this->token_.is_writable()
131 && this->descriptor_ < 0
132 && this->name_.empty());
134 this->contents_ = contents;
136 this->token_.add_writer(task);
140 // Release the file. This is called when we are done with the file in
146 gold_assert(this->is_locked());
148 File_read::total_mapped_bytes += this->mapped_bytes_;
149 File_read::current_mapped_bytes += this->mapped_bytes_;
150 this->mapped_bytes_ = 0;
151 if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes)
152 File_read::maximum_mapped_bytes = File_read::current_mapped_bytes;
154 this->clear_views(false);
156 this->released_ = true;
162 File_read::lock(const Task* task)
164 gold_assert(this->released_);
165 this->token_.add_writer(task);
166 this->released_ = false;
172 File_read::unlock(const Task* task)
175 this->token_.remove_writer(task);
178 // Return whether the file is locked.
181 File_read::is_locked() const
183 if (!this->token_.is_writable())
185 // The file is not locked, so it should have been released.
186 gold_assert(this->released_);
190 // See if we have a view which covers the file starting at START for
191 // SIZE bytes. Return a pointer to the View if found, NULL if not.
193 inline File_read::View*
194 File_read::find_view(off_t start, section_size_type size) const
196 off_t page = File_read::page_offset(start);
197 Views::const_iterator p = this->views_.find(page);
198 if (p == this->views_.end())
200 if (p->second->size() - (start - page) < size)
205 // Read SIZE bytes from the file starting at offset START. Read into
209 File_read::do_read(off_t start, section_size_type size, void* p) const
211 section_size_type bytes;
212 if (this->contents_ != NULL)
214 bytes = this->size_ - start;
217 memcpy(p, this->contents_ + start, size);
223 ssize_t got = ::pread(this->descriptor_, p, size, start);
226 gold_fatal(_("%s: pread failed: %s"),
227 this->filename().c_str(), strerror(errno));
231 if (static_cast<section_size_type>(got) == size)
235 gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
236 this->filename().c_str(),
237 static_cast<long long>(bytes),
238 static_cast<long long>(size),
239 static_cast<long long>(start));
242 // Read data from the file.
245 File_read::read(off_t start, section_size_type size, void* p) const
247 File_read::View* pv = this->find_view(start, size);
250 memcpy(p, pv->data() + (start - pv->start()), size);
254 this->do_read(start, size, p);
257 // Find an existing view or make a new one.
260 File_read::find_or_make_view(off_t start, section_size_type size, bool cache)
262 gold_assert(!this->token_.is_writable());
263 this->released_ = false;
265 off_t poff = File_read::page_offset(start);
267 File_read::View* const vnull = NULL;
268 std::pair<Views::iterator, bool> ins =
269 this->views_.insert(std::make_pair(poff, vnull));
273 // There was an existing view at this offset.
274 File_read::View* v = ins.first->second;
275 if (v->size() - (start - v->start()) >= size)
282 // This view is not large enough.
283 this->saved_views_.push_back(v);
286 // We need to read data from the file. We read full pages for
287 // greater efficiency on small files.
289 section_size_type psize = File_read::pages(size + (start - poff));
291 if (poff + psize >= this->size_)
293 psize = this->size_ - poff;
294 gold_assert(psize >= size);
299 if (this->contents_ != NULL)
301 unsigned char* p = new unsigned char[psize];
302 this->do_read(poff, psize, p);
303 v = new File_read::View(poff, psize, p, cache, false);
307 void* p = ::mmap(NULL, psize, PROT_READ, MAP_SHARED,
308 this->descriptor_, poff);
310 gold_fatal(_("%s: mmap offset %lld size %lld failed: %s"),
311 this->filename().c_str(),
312 static_cast<long long>(poff),
313 static_cast<long long>(psize),
316 this->mapped_bytes_ += psize;
318 const unsigned char* pbytes = static_cast<const unsigned char*>(p);
319 v = new File_read::View(poff, psize, pbytes, cache, true);
322 ins.first->second = v;
326 // Get a view into the file.
329 File_read::get_view(off_t start, section_size_type size, bool cache)
331 File_read::View* pv = this->find_or_make_view(start, size, cache);
332 return pv->data() + (start - pv->start());
336 File_read::get_lasting_view(off_t start, section_size_type size, bool cache)
338 File_read::View* pv = this->find_or_make_view(start, size, cache);
340 return new File_view(*this, pv, pv->data() + (start - pv->start()));
343 // Remove all the file views.
346 File_read::clear_views(bool destroying)
348 Views::iterator p = this->views_.begin();
349 while (p != this->views_.end())
351 if (!p->second->is_locked()
352 && (destroying || !p->second->should_cache()))
356 // map::erase invalidates only the iterator to the deleted
358 Views::iterator pe = p;
360 this->views_.erase(pe);
364 gold_assert(!destroying);
369 Saved_views::iterator q = this->saved_views_.begin();
370 while (q != this->saved_views_.end())
372 if (!(*q)->is_locked()
373 && (destroying || !(*q)->should_cache()))
376 q = this->saved_views_.erase(q);
380 gold_assert(!destroying);
386 // Print statistical information to stderr. This is used for --stats.
389 File_read::print_stats()
391 fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"),
392 program_name, File_read::total_mapped_bytes);
393 fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"),
394 program_name, File_read::maximum_mapped_bytes);
399 File_view::~File_view()
401 gold_assert(this->file_.is_locked());
402 this->view_->unlock();
407 // Create a file for testing.
409 Input_file::Input_file(const Task* task, const char* name,
410 const unsigned char* contents, off_t size)
413 this->input_argument_ =
414 new Input_file_argument(name, false, "", Position_dependent_options());
415 bool ok = file_.open(task, name, contents, size);
421 // If the filename is not absolute, we assume it is in the current
422 // directory *except* when:
423 // A) input_argument_->is_lib() is true; or
424 // B) input_argument_->extra_search_path() is not empty.
425 // In both cases, we look in extra_search_path + library_path to find
426 // the file location, rather than the current directory.
429 Input_file::open(const General_options& options, const Dirsearch& dirpath,
434 // Case 1: name is an absolute file, just try to open it
435 // Case 2: name is relative but is_lib is false and extra_search_path
437 if (IS_ABSOLUTE_PATH (this->input_argument_->name())
438 || (!this->input_argument_->is_lib()
439 && this->input_argument_->extra_search_path() == NULL))
441 name = this->input_argument_->name();
442 this->found_name_ = name;
444 // Case 3: is_lib is true
445 else if (this->input_argument_->is_lib())
447 // We don't yet support extra_search_path with -l.
448 gold_assert(this->input_argument_->extra_search_path() == NULL);
449 std::string n1("lib");
450 n1 += this->input_argument_->name();
452 if (options.is_static()
453 || this->input_argument_->options().do_static_search())
460 name = dirpath.find(n1, n2, &this->is_in_sysroot_);
463 gold_error(_("cannot find -l%s"),
464 this->input_argument_->name());
467 if (n2.empty() || name[name.length() - 1] == 'o')
468 this->found_name_ = n1;
470 this->found_name_ = n2;
472 // Case 4: extra_search_path is not empty
475 gold_assert(this->input_argument_->extra_search_path() != NULL);
477 // First, check extra_search_path.
478 name = this->input_argument_->extra_search_path();
479 if (!IS_DIR_SEPARATOR (name[name.length() - 1]))
481 name += this->input_argument_->name();
482 struct stat dummy_stat;
483 if (::stat(name.c_str(), &dummy_stat) < 0)
485 // extra_search_path failed, so check the normal search-path.
486 name = dirpath.find(this->input_argument_->name(), "",
487 &this->is_in_sysroot_);
490 gold_error(_("cannot find %s"),
491 this->input_argument_->name());
495 this->found_name_ = this->input_argument_->name();
498 // Now that we've figured out where the file lives, try to open it.
499 if (!this->file_.open(task, name))
501 gold_error(_("cannot open %s: %s"),
502 name.c_str(), strerror(errno));
509 } // End namespace gold.