Add cache parameter to get_view. Discard uncached views on unlock.
[external/binutils.git] / gold / fileread.cc
1 // fileread.cc -- read files for gold
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <cstring>
26 #include <cerrno>
27 #include <fcntl.h>
28 #include <unistd.h>
29
30 #include "options.h"
31 #include "dirsearch.h"
32 #include "fileread.h"
33
34 namespace gold
35 {
36
37 // Class File_read::View.
38
39 File_read::View::~View()
40 {
41   gold_assert(!this->is_locked());
42   delete[] this->data_;
43 }
44
45 void
46 File_read::View::lock()
47 {
48   ++this->lock_count_;
49 }
50
51 void
52 File_read::View::unlock()
53 {
54   gold_assert(this->lock_count_ > 0);
55   --this->lock_count_;
56 }
57
58 bool
59 File_read::View::is_locked()
60 {
61   return this->lock_count_ > 0;
62 }
63
64 // Class File_read.
65
66 // The File_read class is designed to support file descriptor caching,
67 // but this is not currently implemented.
68
69 File_read::~File_read()
70 {
71   gold_assert(this->lock_count_ == 0);
72   if (this->descriptor_ >= 0)
73     {
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;
78     }
79   this->name_.clear();
80   this->clear_views(true);
81 }
82
83 // Open the file.
84
85 bool
86 File_read::open(const std::string& name)
87 {
88   gold_assert(this->lock_count_ == 0
89               && this->descriptor_ < 0
90               && this->name_.empty());
91   this->name_ = name;
92
93   this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
94
95   if (this->descriptor_ >= 0)
96     {
97       struct stat s;
98       if (::fstat(this->descriptor_, &s) < 0)
99         {
100           fprintf(stderr, _("%s: %s: fstat failed: %s"), program_name,
101                   this->name_.c_str(), strerror(errno));
102           gold_exit(false);
103         }
104       this->size_ = s.st_size;
105     }
106
107   ++this->lock_count_;
108
109   return this->descriptor_ >= 0;
110 }
111
112 // Open the file for testing purposes.
113
114 bool
115 File_read::open(const std::string& name, const unsigned char* contents,
116                 off_t size)
117 {
118   gold_assert(this->lock_count_ == 0
119               && this->descriptor_ < 0
120               && this->name_.empty());
121   this->name_ = name;
122   this->contents_ = contents;
123   this->size_ = size;
124   ++this->lock_count_;
125   return true;
126 }
127
128 void
129 File_read::lock()
130 {
131   ++this->lock_count_;
132 }
133
134 void
135 File_read::unlock()
136 {
137   gold_assert(this->lock_count_ > 0);
138   --this->lock_count_;
139   if (this->lock_count_ == 0)
140     this->clear_views(false);
141 }
142
143 bool
144 File_read::is_locked()
145 {
146   return this->lock_count_ > 0;
147 }
148
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.
151
152 inline File_read::View*
153 File_read::find_view(off_t start, off_t size)
154 {
155   off_t page = File_read::page_offset(start);
156   Views::iterator p = this->views_.find(page);
157   if (p == this->views_.end())
158     return NULL;
159   if (p->second->size() - (start - page) < size)
160     return NULL;
161   return p->second;
162 }
163
164 // Read SIZE bytes from the file starting at offset START.  Read into
165 // the buffer at P.
166
167 void
168 File_read::do_read(off_t start, off_t size, void* p)
169 {
170   gold_assert(this->lock_count_ > 0);
171
172   off_t bytes;
173   if (this->contents_ != NULL)
174     {
175       bytes = this->size_ - start;
176       if (bytes >= size)
177         {
178           memcpy(p, this->contents_ + start, size);
179           return;
180         }
181     }
182   else
183     {
184       bytes = ::pread(this->descriptor_, p, size, start);
185       if (bytes == size)
186         return;
187
188       if (bytes < 0)
189         {
190           fprintf(stderr, _("%s: %s: pread failed: %s\n"),
191                   program_name, this->filename().c_str(), strerror(errno));
192           gold_exit(false);
193         }
194     }
195
196   fprintf(stderr,
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));
202   gold_exit(false);
203 }
204
205 // Read data from the file.
206
207 void
208 File_read::read(off_t start, off_t size, void* p)
209 {
210   gold_assert(this->lock_count_ > 0);
211
212   File_read::View* pv = this->find_view(start, size);
213   if (pv != NULL)
214     {
215       memcpy(p, pv->data() + (start - pv->start()), size);
216       return;
217     }
218
219   this->do_read(start, size, p);
220 }
221
222 // Find an existing view or make a new one.
223
224 File_read::View*
225 File_read::find_or_make_view(off_t start, off_t size, bool cache)
226 {
227   gold_assert(this->lock_count_ > 0);
228
229   off_t poff = File_read::page_offset(start);
230
231   File_read::View* const vnull = NULL;
232   std::pair<Views::iterator, bool> ins =
233     this->views_.insert(std::make_pair(poff, vnull));
234
235   if (!ins.second)
236     {
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)
240         {
241           if (cache)
242             v->set_cache();
243           return v;
244         }
245
246       // This view is not large enough.
247       this->saved_views_.push_back(v);
248     }
249
250   // We need to read data from the file.  We read full pages for
251   // greater efficiency on small files.
252
253   off_t psize = File_read::pages(size + (start - poff));
254
255   if (poff + psize >= this->size_)
256     {
257       psize = this->size_ - poff;
258       gold_assert(psize >= size);
259     }
260
261   unsigned char* p = new unsigned char[psize];
262
263   this->do_read(poff, psize, p);
264
265   File_read::View* v = new File_read::View(poff, psize, p, cache);
266   ins.first->second = v;
267   return v;
268 }
269
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
272 // mmap.
273
274 const unsigned char*
275 File_read::get_view(off_t start, off_t size, bool cache)
276 {
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());
280 }
281
282 File_view*
283 File_read::get_lasting_view(off_t start, off_t size, bool cache)
284 {
285   gold_assert(this->lock_count_ > 0);
286   File_read::View* pv = this->find_or_make_view(start, size, cache);
287   pv->lock();
288   return new File_view(*this, pv, pv->data() + (start - pv->start()));
289 }
290
291 // Remove all the file views.
292
293 void
294 File_read::clear_views(bool destroying)
295 {
296   for (Views::iterator p = this->views_.begin();
297        p != this->views_.end();
298        ++p)
299     {
300       if (!p->second->is_locked()
301           && (destroying || !p->second->should_cache()))
302         delete p->second;
303       else
304         {
305           gold_assert(!destroying);
306           this->saved_views_.push_back(p->second);
307         }
308     }
309   this->views_.clear();
310
311   Saved_views::iterator p = this->saved_views_.begin();
312   while (p != this->saved_views_.end())
313     {
314       if (!(*p)->is_locked()
315           && (destroying || !(*p)->should_cache()))
316         {
317           delete *p;
318           p = this->saved_views_.erase(p);
319         }
320       else
321         {
322           gold_assert(!destroying);
323           ++p;
324         }
325     }
326 }
327
328 // Class File_view.
329
330 File_view::~File_view()
331 {
332   gold_assert(this->file_.is_locked());
333   this->view_->unlock();
334 }
335
336 // Class Input_file.
337
338 // Create a file for testing.
339
340 Input_file::Input_file(const char* name, const unsigned char* contents,
341                        off_t size)
342   : file_()
343 {
344   this->input_argument_ =
345     new Input_file_argument(name, false, Position_dependent_options());
346   bool ok = file_.open(name, contents, size);
347   gold_assert(ok);
348 }
349
350 // Open the file.
351
352 void
353 Input_file::open(const General_options& options, const Dirsearch& dirpath)
354 {
355   std::string name;
356   if (!this->input_argument_->is_lib())
357     name = this->input_argument_->name();
358   else
359     {
360       std::string n1("lib");
361       n1 += this->input_argument_->name();
362       std::string n2;
363       if (options.is_static())
364         n1 += ".a";
365       else
366         {
367           n2 = n1 + ".a";
368           n1 += ".so";
369         }
370       name = dirpath.find(n1, n2);
371       if (name.empty())
372         {
373           fprintf(stderr, _("%s: cannot find %s\n"), program_name,
374                   this->input_argument_->name());
375           gold_exit(false);
376         }
377     }
378
379   if (!this->file_.open(name))
380     {
381       fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
382               name.c_str(), strerror(errno));
383       gold_exit(false);
384     }
385 }
386
387 } // End namespace gold.