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());
92 this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
94 return this->descriptor_ >= 0;
97 // Open the file for testing purposes.
100 File_read::open(const std::string& name, const unsigned char* contents,
103 gold_assert(this->lock_count_ == 0
104 && this->descriptor_ < 0
105 && this->name_.empty());
107 this->contents_ = contents;
108 this->contents_size_ = contents_size;
122 gold_assert(this->lock_count_ > 0);
127 File_read::is_locked()
129 return this->lock_count_ > 0;
132 // See if we have a view which covers the file starting at START for
133 // SIZE bytes. Return a pointer to the View if found, NULL if not.
135 inline File_read::View*
136 File_read::find_view(off_t start, off_t size)
138 off_t page = File_read::page_offset(start);
139 Views::iterator p = this->views_.find(page);
140 if (p == this->views_.end())
142 if (p->second->size() - (start - page) < size)
147 // Read data from the file. Return the number of bytes read. If
148 // PBYTES is not NULL, store the number of bytes in *PBYTES, otherwise
149 // require that we read exactly the number of bytes requested.
152 File_read::do_read(off_t start, off_t size, void* p, off_t* pbytes)
154 gold_assert(this->lock_count_ > 0);
157 if (this->contents_ == NULL)
159 int o = this->descriptor_;
161 if (lseek(o, start, SEEK_SET) < 0)
163 fprintf(stderr, _("%s: %s: lseek to %lld failed: %s"),
164 program_name, this->filename().c_str(),
165 static_cast<long long>(start),
170 bytes = ::read(o, p, size);
173 fprintf(stderr, _("%s: %s: read failed: %s\n"),
174 program_name, this->filename().c_str(), strerror(errno));
180 bytes = this->contents_size_ - start;
183 else if (bytes > size)
185 memcpy(p, this->contents_ + start, bytes);
190 else if (bytes != size)
193 _("%s: %s: file too short: read only %lld of %lld "
195 program_name, this->filename().c_str(),
196 static_cast<long long>(bytes),
197 static_cast<long long>(size),
198 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, NULL);
223 File_read::read_up_to(off_t start, off_t size, void* p, off_t* pbytes)
225 gold_assert(this->lock_count_ > 0);
227 File_read::View* pv = this->find_view(start, size);
230 memcpy(p, pv->data() + (start - pv->start()), size);
236 this->do_read(start, size, p, pbytes);
239 // Find an existing view or make a new one.
242 File_read::find_or_make_view(off_t start, off_t size, off_t* pbytes)
244 gold_assert(this->lock_count_ > 0);
246 off_t poff = File_read::page_offset(start);
248 File_read::View* const vnull = NULL;
249 std::pair<Views::iterator, bool> ins =
250 this->views_.insert(std::make_pair(poff, vnull));
254 // There was an existing view at this offset.
255 File_read::View* v = ins.first->second;
256 if (v->size() - (start - v->start()) >= size)
263 // This view is not large enough.
264 this->saved_views_.push_back(v);
267 // We need to read data from the file.
269 off_t psize = File_read::pages(size + (start - poff));
270 unsigned char* p = new unsigned char[psize];
273 off_t bytes = this->do_read(poff, psize, p, &got_bytes);
275 File_read::View* v = new File_read::View(poff, bytes, p);
277 ins.first->second = v;
279 if (bytes - (start - poff) >= size)
288 *pbytes = bytes - (start - poff);
293 _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
294 program_name, this->filename().c_str(),
295 static_cast<long long>(bytes - (start - poff)),
296 static_cast<long long>(size),
297 static_cast<long long>(start));
301 // This implementation of get_view just reads into a memory buffer,
302 // which we store on view_list_. At some point we should support
306 File_read::get_view(off_t start, off_t size)
308 gold_assert(this->lock_count_ > 0);
309 File_read::View* pv = this->find_or_make_view(start, size, NULL);
310 return pv->data() + (start - pv->start());
314 File_read::get_view_and_size(off_t start, off_t size, off_t* pbytes)
316 gold_assert(this->lock_count_ > 0);
317 File_read::View* pv = this->find_or_make_view(start, size, pbytes);
318 return pv->data() + (start - pv->start());
322 File_read::get_lasting_view(off_t start, off_t size)
324 gold_assert(this->lock_count_ > 0);
325 File_read::View* pv = this->find_or_make_view(start, size, NULL);
327 return new File_view(*this, pv, pv->data() + (start - pv->start()));
330 // Remove all the file views.
333 File_read::clear_views(bool destroying)
335 for (Views::iterator p = this->views_.begin();
336 p != this->views_.end();
339 if (!p->second->is_locked())
343 gold_assert(!destroying);
344 this->saved_views_.push_back(p->second);
347 this->views_.clear();
349 Saved_views::iterator p = this->saved_views_.begin();
350 while (p != this->saved_views_.end())
352 if (!(*p)->is_locked())
355 p = this->saved_views_.erase(p);
359 gold_assert(!destroying);
367 File_view::~File_view()
369 gold_assert(this->file_.is_locked());
370 this->view_->unlock();
375 // Create a file for testing.
377 Input_file::Input_file(const char* name, const unsigned char* contents,
381 this->input_argument_ =
382 new Input_file_argument(name, false, Position_dependent_options());
383 bool ok = file_.open(name, contents, size);
390 Input_file::open(const General_options& options, const Dirsearch& dirpath)
393 if (!this->input_argument_->is_lib())
394 name = this->input_argument_->name();
397 std::string n1("lib");
398 n1 += this->input_argument_->name();
400 if (options.is_static())
407 name = dirpath.find(n1, n2);
410 fprintf(stderr, _("%s: cannot find %s\n"), program_name,
411 this->input_argument_->name());
416 if (!this->file_.open(name))
418 fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
419 name.c_str(), strerror(errno));
424 } // End namespace gold.