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.
31 #include "dirsearch.h"
37 // Class File_read::View.
39 File_read::View::~View()
41 gold_assert(!this->is_locked());
46 File_read::View::lock()
52 File_read::View::unlock()
54 gold_assert(this->lock_count_ > 0);
59 File_read::View::is_locked()
61 return this->lock_count_ > 0;
66 // The File_read class is designed to support file descriptor caching,
67 // but this is not currently implemented.
69 File_read::~File_read()
71 gold_assert(this->lock_count_ == 0);
72 if (this->descriptor_ >= 0)
74 if (close(this->descriptor_) < 0)
75 fprintf(stderr, _("%s: warning: close(%s) failed: %s"),
76 program_name, this->name_.c_str(), strerror(errno));
77 this->descriptor_ = -1;
80 this->clear_views(true);
86 File_read::open(const std::string& name)
88 gold_assert(this->lock_count_ == 0
89 && this->descriptor_ < 0
90 && this->name_.empty());
93 this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
95 if (this->descriptor_ >= 0)
98 if (::fstat(this->descriptor_, &s) < 0)
100 fprintf(stderr, _("%s: %s: fstat failed: %s"), program_name,
101 this->name_.c_str(), strerror(errno));
104 this->size_ = s.st_size;
109 return this->descriptor_ >= 0;
112 // Open the file for testing purposes.
115 File_read::open(const std::string& name, const unsigned char* contents,
118 gold_assert(this->lock_count_ == 0
119 && this->descriptor_ < 0
120 && this->name_.empty());
122 this->contents_ = contents;
137 gold_assert(this->lock_count_ > 0);
139 if (this->lock_count_ == 0)
140 this->clear_views(false);
144 File_read::is_locked()
146 return this->lock_count_ > 0;
149 // See if we have a view which covers the file starting at START for
150 // SIZE bytes. Return a pointer to the View if found, NULL if not.
152 inline File_read::View*
153 File_read::find_view(off_t start, off_t size)
155 off_t page = File_read::page_offset(start);
156 Views::iterator p = this->views_.find(page);
157 if (p == this->views_.end())
159 if (p->second->size() - (start - page) < size)
164 // Read SIZE bytes from the file starting at offset START. Read into
168 File_read::do_read(off_t start, off_t size, void* p)
170 gold_assert(this->lock_count_ > 0);
173 if (this->contents_ != NULL)
175 bytes = this->size_ - start;
178 memcpy(p, this->contents_ + start, size);
184 bytes = ::pread(this->descriptor_, p, size, start);
190 fprintf(stderr, _("%s: %s: pread failed: %s\n"),
191 program_name, this->filename().c_str(), strerror(errno));
197 _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
198 program_name, this->filename().c_str(),
199 static_cast<long long>(bytes),
200 static_cast<long long>(size),
201 static_cast<long long>(start));
205 // Read data from the file.
208 File_read::read(off_t start, off_t size, void* p)
210 gold_assert(this->lock_count_ > 0);
212 File_read::View* pv = this->find_view(start, size);
215 memcpy(p, pv->data() + (start - pv->start()), size);
219 this->do_read(start, size, p);
222 // Find an existing view or make a new one.
225 File_read::find_or_make_view(off_t start, off_t size, bool cache)
227 gold_assert(this->lock_count_ > 0);
229 off_t poff = File_read::page_offset(start);
231 File_read::View* const vnull = NULL;
232 std::pair<Views::iterator, bool> ins =
233 this->views_.insert(std::make_pair(poff, vnull));
237 // There was an existing view at this offset.
238 File_read::View* v = ins.first->second;
239 if (v->size() - (start - v->start()) >= size)
246 // This view is not large enough.
247 this->saved_views_.push_back(v);
250 // We need to read data from the file. We read full pages for
251 // greater efficiency on small files.
253 off_t psize = File_read::pages(size + (start - poff));
255 if (poff + psize >= this->size_)
257 psize = this->size_ - poff;
258 gold_assert(psize >= size);
261 unsigned char* p = new unsigned char[psize];
263 this->do_read(poff, psize, p);
265 File_read::View* v = new File_read::View(poff, psize, p, cache);
266 ins.first->second = v;
270 // This implementation of get_view just reads into a memory buffer,
271 // which we store on view_list_. At some point we should support
275 File_read::get_view(off_t start, off_t size, bool cache)
277 gold_assert(this->lock_count_ > 0);
278 File_read::View* pv = this->find_or_make_view(start, size, cache);
279 return pv->data() + (start - pv->start());
283 File_read::get_lasting_view(off_t start, off_t size, bool cache)
285 gold_assert(this->lock_count_ > 0);
286 File_read::View* pv = this->find_or_make_view(start, size, cache);
288 return new File_view(*this, pv, pv->data() + (start - pv->start()));
291 // Remove all the file views.
294 File_read::clear_views(bool destroying)
296 for (Views::iterator p = this->views_.begin();
297 p != this->views_.end();
300 if (!p->second->is_locked()
301 && (destroying || !p->second->should_cache()))
305 gold_assert(!destroying);
306 this->saved_views_.push_back(p->second);
309 this->views_.clear();
311 Saved_views::iterator p = this->saved_views_.begin();
312 while (p != this->saved_views_.end())
314 if (!(*p)->is_locked()
315 && (destroying || !(*p)->should_cache()))
318 p = this->saved_views_.erase(p);
322 gold_assert(!destroying);
330 File_view::~File_view()
332 gold_assert(this->file_.is_locked());
333 this->view_->unlock();
338 // Create a file for testing.
340 Input_file::Input_file(const char* name, const unsigned char* contents,
344 this->input_argument_ =
345 new Input_file_argument(name, false, Position_dependent_options());
346 bool ok = file_.open(name, contents, size);
353 Input_file::open(const General_options& options, const Dirsearch& dirpath)
356 if (!this->input_argument_->is_lib())
357 name = this->input_argument_->name();
360 std::string n1("lib");
361 n1 += this->input_argument_->name();
363 if (options.is_static())
370 name = dirpath.find(n1, n2);
373 fprintf(stderr, _("%s: cannot find %s\n"), program_name,
374 this->input_argument_->name());
379 if (!this->file_.open(name))
381 fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
382 name.c_str(), strerror(errno));
387 } // End namespace gold.