Rework File_read interface. Get file size. Use pread when
[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 }
140
141 bool
142 File_read::is_locked()
143 {
144   return this->lock_count_ > 0;
145 }
146
147 // See if we have a view which covers the file starting at START for
148 // SIZE bytes.  Return a pointer to the View if found, NULL if not.
149
150 inline File_read::View*
151 File_read::find_view(off_t start, off_t size)
152 {
153   off_t page = File_read::page_offset(start);
154   Views::iterator p = this->views_.find(page);
155   if (p == this->views_.end())
156     return NULL;
157   if (p->second->size() - (start - page) < size)
158     return NULL;
159   return p->second;
160 }
161
162 // Read SIZE bytes from the file starting at offset START.  Read into
163 // the buffer at P.  Return the number of bytes read, which should
164 // always be at least SIZE except at the end of the file.
165
166 off_t
167 File_read::do_read(off_t start, off_t size, void* p)
168 {
169   gold_assert(this->lock_count_ > 0);
170
171   if (this->contents_ != NULL)
172     {
173       off_t bytes = this->size_ - start;
174       if (bytes < 0)
175         bytes = 0;
176       else if (bytes > size)
177         bytes = size;
178       memcpy(p, this->contents_ + start, bytes);
179       return bytes;
180     }
181
182   off_t bytes = ::pread(this->descriptor_, p, size, start);
183   if (bytes < 0)
184     {
185       fprintf(stderr, _("%s: %s: pread failed: %s\n"),
186               program_name, this->filename().c_str(), strerror(errno));
187       gold_exit(false);
188     }
189
190   return bytes;
191 }
192
193 // Read exactly SIZE bytes from the file starting at offset START.
194 // Read into the buffer at P.
195
196 void
197 File_read::do_read_exact(off_t start, off_t size, void* p)
198 {
199   off_t bytes = this->do_read(start, size, p);
200   if (bytes != size)
201     {
202       fprintf(stderr,
203               _("%s: %s: file too short: read only %lld of %lld "
204                 "bytes at %lld\n"),
205               program_name, this->filename().c_str(),
206               static_cast<long long>(bytes),
207               static_cast<long long>(size),
208               static_cast<long long>(start));
209       gold_exit(false);
210     }
211 }
212
213 // Read data from the file.
214
215 void
216 File_read::read(off_t start, off_t size, void* p)
217 {
218   gold_assert(this->lock_count_ > 0);
219
220   File_read::View* pv = this->find_view(start, size);
221   if (pv != NULL)
222     {
223       memcpy(p, pv->data() + (start - pv->start()), size);
224       return;
225     }
226
227   this->do_read_exact(start, size, p);
228 }
229
230 // Find an existing view or make a new one.
231
232 File_read::View*
233 File_read::find_or_make_view(off_t start, off_t size)
234 {
235   gold_assert(this->lock_count_ > 0);
236
237   off_t poff = File_read::page_offset(start);
238
239   File_read::View* const vnull = NULL;
240   std::pair<Views::iterator, bool> ins =
241     this->views_.insert(std::make_pair(poff, vnull));
242
243   if (!ins.second)
244     {
245       // There was an existing view at this offset.
246       File_read::View* v = ins.first->second;
247       if (v->size() - (start - v->start()) >= size)
248         return v;
249
250       // This view is not large enough.
251       this->saved_views_.push_back(v);
252     }
253
254   // We need to read data from the file.  We read full pages for
255   // greater efficiency on small files.
256
257   off_t psize = File_read::pages(size + (start - poff));
258
259   if (poff + psize >= this->size_)
260     {
261       psize = this->size_ - poff;
262       gold_assert(psize >= size);
263     }
264
265   unsigned char* p = new unsigned char[psize];
266
267   this->do_read_exact(poff, psize, p);
268
269   File_read::View* v = new File_read::View(poff, psize, p);
270   ins.first->second = v;
271   return v;
272 }
273
274 // This implementation of get_view just reads into a memory buffer,
275 // which we store on view_list_.  At some point we should support
276 // mmap.
277
278 const unsigned char*
279 File_read::get_view(off_t start, off_t size)
280 {
281   gold_assert(this->lock_count_ > 0);
282   File_read::View* pv = this->find_or_make_view(start, size);
283   return pv->data() + (start - pv->start());
284 }
285
286 File_view*
287 File_read::get_lasting_view(off_t start, off_t size)
288 {
289   gold_assert(this->lock_count_ > 0);
290   File_read::View* pv = this->find_or_make_view(start, size);
291   pv->lock();
292   return new File_view(*this, pv, pv->data() + (start - pv->start()));
293 }
294
295 // Remove all the file views.
296
297 void
298 File_read::clear_views(bool destroying)
299 {
300   for (Views::iterator p = this->views_.begin();
301        p != this->views_.end();
302        ++p)
303     {
304       if (!p->second->is_locked())
305         delete p->second;
306       else
307         {
308           gold_assert(!destroying);
309           this->saved_views_.push_back(p->second);
310         }
311     }
312   this->views_.clear();
313
314   Saved_views::iterator p = this->saved_views_.begin();
315   while (p != this->saved_views_.end())
316     {
317       if (!(*p)->is_locked())
318         {
319           delete *p;
320           p = this->saved_views_.erase(p);
321         }
322       else
323         {
324           gold_assert(!destroying);
325           ++p;
326         }
327     }
328 }
329
330 // Class File_view.
331
332 File_view::~File_view()
333 {
334   gold_assert(this->file_.is_locked());
335   this->view_->unlock();
336 }
337
338 // Class Input_file.
339
340 // Create a file for testing.
341
342 Input_file::Input_file(const char* name, const unsigned char* contents,
343                        off_t size)
344   : file_()
345 {
346   this->input_argument_ =
347     new Input_file_argument(name, false, Position_dependent_options());
348   bool ok = file_.open(name, contents, size);
349   gold_assert(ok);
350 }
351
352 // Open the file.
353
354 void
355 Input_file::open(const General_options& options, const Dirsearch& dirpath)
356 {
357   std::string name;
358   if (!this->input_argument_->is_lib())
359     name = this->input_argument_->name();
360   else
361     {
362       std::string n1("lib");
363       n1 += this->input_argument_->name();
364       std::string n2;
365       if (options.is_static())
366         n1 += ".a";
367       else
368         {
369           n2 = n1 + ".a";
370           n1 += ".so";
371         }
372       name = dirpath.find(n1, n2);
373       if (name.empty())
374         {
375           fprintf(stderr, _("%s: cannot find %s\n"), program_name,
376                   this->input_argument_->name());
377           gold_exit(false);
378         }
379     }
380
381   if (!this->file_.open(name))
382     {
383       fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
384               name.c_str(), strerror(errno));
385       gold_exit(false);
386     }
387 }
388
389 } // End namespace gold.