Fix a typo in ChangeLog.
[binutils.git] / gold / fileread.cc
blobdefb3a02560f9a683ef3e2267c5958502af5d2c4
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.
23 #include "gold.h"
25 #include <cstring>
26 #include <cerrno>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30 #include "filenames.h"
32 #include "options.h"
33 #include "dirsearch.h"
34 #include "fileread.h"
36 namespace gold
39 // Class File_read::View.
41 File_read::View::~View()
43 gold_assert(!this->is_locked());
44 if (!this->mapped_)
45 delete[] this->data_;
46 else
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_;
55 void
56 File_read::View::lock()
58 ++this->lock_count_;
61 void
62 File_read::View::unlock()
64 gold_assert(this->lock_count_ > 0);
65 --this->lock_count_;
68 bool
69 File_read::View::is_locked()
71 return this->lock_count_ > 0;
74 // Class File_read.
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;
94 this->name_.clear();
95 this->clear_views(true);
98 // Open the file.
100 bool
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());
106 this->name_ = name;
108 this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
110 if (this->descriptor_ >= 0)
112 struct stat s;
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.
126 bool
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());
133 this->name_ = name;
134 this->contents_ = contents;
135 this->size_ = size;
136 this->token_.add_writer(task);
137 return true;
140 // Release the file. This is called when we are done with the file in
141 // a Task.
143 void
144 File_read::release()
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;
159 // Lock the file.
161 void
162 File_read::lock(const Task* task)
164 gold_assert(this->released_);
165 this->token_.add_writer(task);
166 this->released_ = false;
169 // Unlock the file.
171 void
172 File_read::unlock(const Task* task)
174 this->release();
175 this->token_.remove_writer(task);
178 // Return whether the file is locked.
180 bool
181 File_read::is_locked() const
183 if (!this->token_.is_writable())
184 return true;
185 // The file is not locked, so it should have been released.
186 gold_assert(this->released_);
187 return false;
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())
199 return NULL;
200 if (p->second->size() - (start - page) < size)
201 return NULL;
202 return p->second;
205 // Read SIZE bytes from the file starting at offset START. Read into
206 // the buffer at P.
208 void
209 File_read::do_read(off_t start, section_size_type size, void* p) const
211 ssize_t bytes;
212 if (this->contents_ != NULL)
214 bytes = this->size_ - start;
215 if (static_cast<section_size_type>(bytes) >= size)
217 memcpy(p, this->contents_ + start, size);
218 return;
221 else
223 bytes = ::pread(this->descriptor_, p, size, start);
224 if (static_cast<section_size_type>(bytes) == size)
225 return;
227 if (bytes < 0)
229 gold_fatal(_("%s: pread failed: %s"),
230 this->filename().c_str(), strerror(errno));
231 return;
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.
244 void
245 File_read::read(off_t start, section_size_type size, void* p) const
247 File_read::View* pv = this->find_view(start, size);
248 if (pv != NULL)
250 memcpy(p, pv->data() + (start - pv->start()), size);
251 return;
254 this->do_read(start, size, p);
257 // Find an existing view or make a new one.
259 File_read::View*
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));
271 if (!ins.second)
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)
277 if (cache)
278 v->set_cache();
279 return v;
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 + static_cast<off_t>(psize) >= this->size_)
293 psize = this->size_ - poff;
294 gold_assert(psize >= size);
297 File_read::View* v;
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);
305 else
307 void* p = ::mmap(NULL, psize, PROT_READ, MAP_SHARED,
308 this->descriptor_, poff);
309 if (p == MAP_FAILED)
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),
314 strerror(errno));
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;
323 return v;
326 // Get a view into the file.
328 const unsigned char*
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());
335 File_view*
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);
339 pv->lock();
340 return new File_view(*this, pv, pv->data() + (start - pv->start()));
343 // Remove all the file views.
345 void
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()))
354 delete p->second;
356 // map::erase invalidates only the iterator to the deleted
357 // element.
358 Views::iterator pe = p;
359 ++p;
360 this->views_.erase(pe);
362 else
364 gold_assert(!destroying);
365 ++p;
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()))
375 delete *q;
376 q = this->saved_views_.erase(q);
378 else
380 gold_assert(!destroying);
381 ++q;
386 // Print statistical information to stderr. This is used for --stats.
388 void
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);
397 // Class File_view.
399 File_view::~File_view()
401 gold_assert(this->file_.is_locked());
402 this->view_->unlock();
405 // Class Input_file.
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)
411 : file_()
413 this->input_argument_ =
414 new Input_file_argument(name, false, "", Position_dependent_options());
415 bool ok = file_.open(task, name, contents, size);
416 gold_assert(ok);
419 // Open the file.
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.
428 bool
429 Input_file::open(const General_options& options, const Dirsearch& dirpath,
430 const Task* task)
432 std::string name;
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
436 // is empty
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();
451 std::string n2;
452 if (options.is_static()
453 || this->input_argument_->options().do_static_search())
454 n1 += ".a";
455 else
457 n2 = n1 + ".a";
458 n1 += ".so";
460 name = dirpath.find(n1, n2, &this->is_in_sysroot_);
461 if (name.empty())
463 gold_error(_("cannot find -l%s"),
464 this->input_argument_->name());
465 return false;
467 if (n2.empty() || name[name.length() - 1] == 'o')
468 this->found_name_ = n1;
469 else
470 this->found_name_ = n2;
472 // Case 4: extra_search_path is not empty
473 else
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]))
480 name += '/';
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_);
488 if (name.empty())
490 gold_error(_("cannot find %s"),
491 this->input_argument_->name());
492 return false;
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));
503 return false;
506 return true;
509 } // End namespace gold.