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 fprintf(stderr, _("%s: munmap failed: %s\n"),
50 program_name, strerror(errno));
52 File_read::current_mapped_bytes -= this->size_;
57 File_read::View::lock()
63 File_read::View::unlock()
65 gold_assert(this->lock_count_ > 0);
70 File_read::View::is_locked()
72 return this->lock_count_ > 0;
77 // The File_read static variables.
78 unsigned long long File_read::total_mapped_bytes;
79 unsigned long long File_read::current_mapped_bytes;
80 unsigned long long File_read::maximum_mapped_bytes;
82 // The File_read class is designed to support file descriptor caching,
83 // but this is not currently implemented.
85 File_read::~File_read()
87 gold_assert(this->lock_count_ == 0);
88 if (this->descriptor_ >= 0)
90 if (close(this->descriptor_) < 0)
91 fprintf(stderr, _("%s: warning: close(%s) failed: %s"),
92 program_name, this->name_.c_str(), strerror(errno));
93 this->descriptor_ = -1;
96 this->clear_views(true);
102 File_read::open(const std::string& name)
104 gold_assert(this->lock_count_ == 0
105 && this->descriptor_ < 0
106 && this->name_.empty());
109 this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
111 if (this->descriptor_ >= 0)
114 if (::fstat(this->descriptor_, &s) < 0)
116 fprintf(stderr, _("%s: %s: fstat failed: %s"), program_name,
117 this->name_.c_str(), strerror(errno));
120 this->size_ = s.st_size;
125 return this->descriptor_ >= 0;
128 // Open the file for testing purposes.
131 File_read::open(const std::string& name, const unsigned char* contents,
134 gold_assert(this->lock_count_ == 0
135 && this->descriptor_ < 0
136 && this->name_.empty());
138 this->contents_ = contents;
153 gold_assert(this->lock_count_ > 0);
155 if (this->lock_count_ == 0)
157 File_read::total_mapped_bytes += this->mapped_bytes_;
158 File_read::current_mapped_bytes += this->mapped_bytes_;
159 this->mapped_bytes_ = 0;
160 if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes)
161 File_read::maximum_mapped_bytes = File_read::current_mapped_bytes;
163 this->clear_views(false);
168 File_read::is_locked()
170 return this->lock_count_ > 0;
173 // See if we have a view which covers the file starting at START for
174 // SIZE bytes. Return a pointer to the View if found, NULL if not.
176 inline File_read::View*
177 File_read::find_view(off_t start, off_t size)
179 off_t page = File_read::page_offset(start);
180 Views::iterator p = this->views_.find(page);
181 if (p == this->views_.end())
183 if (p->second->size() - (start - page) < size)
188 // Read SIZE bytes from the file starting at offset START. Read into
192 File_read::do_read(off_t start, off_t size, void* p)
194 gold_assert(this->lock_count_ > 0);
197 if (this->contents_ != NULL)
199 bytes = this->size_ - start;
202 memcpy(p, this->contents_ + start, size);
208 bytes = ::pread(this->descriptor_, p, size, start);
214 fprintf(stderr, _("%s: %s: pread failed: %s\n"),
215 program_name, this->filename().c_str(), strerror(errno));
221 _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
222 program_name, this->filename().c_str(),
223 static_cast<long long>(bytes),
224 static_cast<long long>(size),
225 static_cast<long long>(start));
229 // Read data from the file.
232 File_read::read(off_t start, off_t size, void* p)
234 gold_assert(this->lock_count_ > 0);
236 File_read::View* pv = this->find_view(start, size);
239 memcpy(p, pv->data() + (start - pv->start()), size);
243 this->do_read(start, size, p);
246 // Find an existing view or make a new one.
249 File_read::find_or_make_view(off_t start, off_t size, bool cache)
251 gold_assert(this->lock_count_ > 0);
253 off_t poff = File_read::page_offset(start);
255 File_read::View* const vnull = NULL;
256 std::pair<Views::iterator, bool> ins =
257 this->views_.insert(std::make_pair(poff, vnull));
261 // There was an existing view at this offset.
262 File_read::View* v = ins.first->second;
263 if (v->size() - (start - v->start()) >= size)
270 // This view is not large enough.
271 this->saved_views_.push_back(v);
274 // We need to read data from the file. We read full pages for
275 // greater efficiency on small files.
277 off_t psize = File_read::pages(size + (start - poff));
279 if (poff + psize >= this->size_)
281 psize = this->size_ - poff;
282 gold_assert(psize >= size);
287 if (this->contents_ != NULL)
289 unsigned char* p = new unsigned char[psize];
290 this->do_read(poff, psize, p);
291 v = new File_read::View(poff, psize, p, cache, false);
295 void* p = ::mmap(NULL, psize, PROT_READ, MAP_SHARED,
296 this->descriptor_, poff);
299 fprintf(stderr, _("%s: %s: mmap offset %lld size %lld failed: %s\n"),
300 program_name, this->filename().c_str(),
301 static_cast<long long>(poff),
302 static_cast<long long>(psize),
307 this->mapped_bytes_ += psize;
309 const unsigned char* pbytes = static_cast<const unsigned char*>(p);
310 v = new File_read::View(poff, psize, pbytes, cache, true);
313 ins.first->second = v;
317 // This implementation of get_view just reads into a memory buffer,
318 // which we store on view_list_. At some point we should support
322 File_read::get_view(off_t start, off_t size, bool cache)
324 gold_assert(this->lock_count_ > 0);
325 File_read::View* pv = this->find_or_make_view(start, size, cache);
326 return pv->data() + (start - pv->start());
330 File_read::get_lasting_view(off_t start, off_t size, bool cache)
332 gold_assert(this->lock_count_ > 0);
333 File_read::View* pv = this->find_or_make_view(start, size, cache);
335 return new File_view(*this, pv, pv->data() + (start - pv->start()));
338 // Remove all the file views.
341 File_read::clear_views(bool destroying)
343 for (Views::iterator p = this->views_.begin();
344 p != this->views_.end();
347 if (!p->second->is_locked()
348 && (destroying || !p->second->should_cache()))
352 gold_assert(!destroying);
353 this->saved_views_.push_back(p->second);
356 this->views_.clear();
358 Saved_views::iterator p = this->saved_views_.begin();
359 while (p != this->saved_views_.end())
361 if (!(*p)->is_locked()
362 && (destroying || !(*p)->should_cache()))
365 p = this->saved_views_.erase(p);
369 gold_assert(!destroying);
375 // Print statistical information to stderr. This is used for --stats.
378 File_read::print_stats()
380 fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"),
381 program_name, File_read::total_mapped_bytes);
382 fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"),
383 program_name, File_read::maximum_mapped_bytes);
388 File_view::~File_view()
390 gold_assert(this->file_.is_locked());
391 this->view_->unlock();
396 // Create a file for testing.
398 Input_file::Input_file(const char* name, const unsigned char* contents,
402 this->input_argument_ =
403 new Input_file_argument(name, false, "", Position_dependent_options());
404 bool ok = file_.open(name, contents, size);
410 // If the filename is not absolute, we assume it is in the current
411 // directory *except* when:
412 // A) input_argument_->is_lib() is true; or
413 // B) input_argument_->extra_search_path() is not empty.
414 // In both cases, we look in extra_search_path + library_path to find
415 // the file location, rather than the current directory.
418 Input_file::open(const General_options& options, const Dirsearch& dirpath)
422 // Case 1: name is an absolute file, just try to open it
423 // Case 2: name is relative but is_lib is false and extra_search_path
425 if (IS_ABSOLUTE_PATH (this->input_argument_->name())
426 || (!this->input_argument_->is_lib()
427 && this->input_argument_->extra_search_path() == NULL))
429 name = this->input_argument_->name();
430 this->found_name_ = name;
432 // Case 3: is_lib is true
433 else if (this->input_argument_->is_lib())
435 // We don't yet support extra_search_path with -l.
436 gold_assert(this->input_argument_->extra_search_path() == NULL);
437 std::string n1("lib");
438 n1 += this->input_argument_->name();
440 if (options.is_static())
447 name = dirpath.find(n1, n2, &this->is_in_sysroot_);
450 fprintf(stderr, _("%s: cannot find -l%s\n"), program_name,
451 this->input_argument_->name());
454 if (n2.empty() || name[name.length() - 1] == 'o')
455 this->found_name_ = n1;
457 this->found_name_ = n2;
459 // Case 4: extra_search_path is not empty
462 gold_assert(this->input_argument_->extra_search_path() != NULL);
464 // First, check extra_search_path.
465 name = this->input_argument_->extra_search_path();
466 if (!IS_DIR_SEPARATOR (name[name.length() - 1]))
468 name += this->input_argument_->name();
469 struct stat dummy_stat;
470 if (::stat(name.c_str(), &dummy_stat) < 0)
472 // extra_search_path failed, so check the normal search-path.
473 name = dirpath.find(this->input_argument_->name(), "",
474 &this->is_in_sysroot_);
477 fprintf(stderr, _("%s: cannot find %s\n"), program_name,
478 this->input_argument_->name());
482 this->found_name_ = this->input_argument_->name();
485 // Now that we've figured out where the file lives, try to open it.
486 if (!this->file_.open(name))
488 fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
489 name.c_str(), strerror(errno));
494 } // End namespace gold.