Add --stats option to print runtime and memory usage statistics.
[deliverable/binutils-gdb.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 #include <sys/mman.h>
30 #include "filenames.h"
31
32 #include "options.h"
33 #include "dirsearch.h"
34 #include "fileread.h"
35
36 namespace gold
37 {
38
39 // Class File_read::View.
40
41 File_read::View::~View()
42 {
43 gold_assert(!this->is_locked());
44 if (!this->mapped_)
45 delete[] this->data_;
46 else
47 {
48 if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
49 fprintf(stderr, _("%s: munmap failed: %s\n"),
50 program_name, strerror(errno));
51
52 File_read::current_mapped_bytes -= this->size_;
53 }
54 }
55
56 void
57 File_read::View::lock()
58 {
59 ++this->lock_count_;
60 }
61
62 void
63 File_read::View::unlock()
64 {
65 gold_assert(this->lock_count_ > 0);
66 --this->lock_count_;
67 }
68
69 bool
70 File_read::View::is_locked()
71 {
72 return this->lock_count_ > 0;
73 }
74
75 // Class File_read.
76
77 // The File_read static variables.
78 unsigned long long File_read::total_mapped_bytes;
79 unsigned long long File_read::current_mapped_bytes;
80 unsigned long long File_read::maximum_mapped_bytes;
81
82 // The File_read class is designed to support file descriptor caching,
83 // but this is not currently implemented.
84
85 File_read::~File_read()
86 {
87 gold_assert(this->lock_count_ == 0);
88 if (this->descriptor_ >= 0)
89 {
90 if (close(this->descriptor_) < 0)
91 fprintf(stderr, _("%s: warning: close(%s) failed: %s"),
92 program_name, this->name_.c_str(), strerror(errno));
93 this->descriptor_ = -1;
94 }
95 this->name_.clear();
96 this->clear_views(true);
97 }
98
99 // Open the file.
100
101 bool
102 File_read::open(const std::string& name)
103 {
104 gold_assert(this->lock_count_ == 0
105 && this->descriptor_ < 0
106 && this->name_.empty());
107 this->name_ = name;
108
109 this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
110
111 if (this->descriptor_ >= 0)
112 {
113 struct stat s;
114 if (::fstat(this->descriptor_, &s) < 0)
115 {
116 fprintf(stderr, _("%s: %s: fstat failed: %s"), program_name,
117 this->name_.c_str(), strerror(errno));
118 gold_exit(false);
119 }
120 this->size_ = s.st_size;
121 }
122
123 ++this->lock_count_;
124
125 return this->descriptor_ >= 0;
126 }
127
128 // Open the file for testing purposes.
129
130 bool
131 File_read::open(const std::string& name, const unsigned char* contents,
132 off_t size)
133 {
134 gold_assert(this->lock_count_ == 0
135 && this->descriptor_ < 0
136 && this->name_.empty());
137 this->name_ = name;
138 this->contents_ = contents;
139 this->size_ = size;
140 ++this->lock_count_;
141 return true;
142 }
143
144 void
145 File_read::lock()
146 {
147 ++this->lock_count_;
148 }
149
150 void
151 File_read::unlock()
152 {
153 gold_assert(this->lock_count_ > 0);
154 --this->lock_count_;
155 if (this->lock_count_ == 0)
156 {
157 File_read::total_mapped_bytes += this->mapped_bytes_;
158 File_read::current_mapped_bytes += this->mapped_bytes_;
159 this->mapped_bytes_ = 0;
160 if (File_read::current_mapped_bytes > File_read::maximum_mapped_bytes)
161 File_read::maximum_mapped_bytes = File_read::current_mapped_bytes;
162
163 this->clear_views(false);
164 }
165 }
166
167 bool
168 File_read::is_locked()
169 {
170 return this->lock_count_ > 0;
171 }
172
173 // See if we have a view which covers the file starting at START for
174 // SIZE bytes. Return a pointer to the View if found, NULL if not.
175
176 inline File_read::View*
177 File_read::find_view(off_t start, off_t size)
178 {
179 off_t page = File_read::page_offset(start);
180 Views::iterator p = this->views_.find(page);
181 if (p == this->views_.end())
182 return NULL;
183 if (p->second->size() - (start - page) < size)
184 return NULL;
185 return p->second;
186 }
187
188 // Read SIZE bytes from the file starting at offset START. Read into
189 // the buffer at P.
190
191 void
192 File_read::do_read(off_t start, off_t size, void* p)
193 {
194 gold_assert(this->lock_count_ > 0);
195
196 off_t bytes;
197 if (this->contents_ != NULL)
198 {
199 bytes = this->size_ - start;
200 if (bytes >= size)
201 {
202 memcpy(p, this->contents_ + start, size);
203 return;
204 }
205 }
206 else
207 {
208 bytes = ::pread(this->descriptor_, p, size, start);
209 if (bytes == size)
210 return;
211
212 if (bytes < 0)
213 {
214 fprintf(stderr, _("%s: %s: pread failed: %s\n"),
215 program_name, this->filename().c_str(), strerror(errno));
216 gold_exit(false);
217 }
218 }
219
220 fprintf(stderr,
221 _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
222 program_name, this->filename().c_str(),
223 static_cast<long long>(bytes),
224 static_cast<long long>(size),
225 static_cast<long long>(start));
226 gold_exit(false);
227 }
228
229 // Read data from the file.
230
231 void
232 File_read::read(off_t start, off_t size, void* p)
233 {
234 gold_assert(this->lock_count_ > 0);
235
236 File_read::View* pv = this->find_view(start, size);
237 if (pv != NULL)
238 {
239 memcpy(p, pv->data() + (start - pv->start()), size);
240 return;
241 }
242
243 this->do_read(start, size, p);
244 }
245
246 // Find an existing view or make a new one.
247
248 File_read::View*
249 File_read::find_or_make_view(off_t start, off_t size, bool cache)
250 {
251 gold_assert(this->lock_count_ > 0);
252
253 off_t poff = File_read::page_offset(start);
254
255 File_read::View* const vnull = NULL;
256 std::pair<Views::iterator, bool> ins =
257 this->views_.insert(std::make_pair(poff, vnull));
258
259 if (!ins.second)
260 {
261 // There was an existing view at this offset.
262 File_read::View* v = ins.first->second;
263 if (v->size() - (start - v->start()) >= size)
264 {
265 if (cache)
266 v->set_cache();
267 return v;
268 }
269
270 // This view is not large enough.
271 this->saved_views_.push_back(v);
272 }
273
274 // We need to read data from the file. We read full pages for
275 // greater efficiency on small files.
276
277 off_t psize = File_read::pages(size + (start - poff));
278
279 if (poff + psize >= this->size_)
280 {
281 psize = this->size_ - poff;
282 gold_assert(psize >= size);
283 }
284
285 File_read::View* v;
286
287 if (this->contents_ != NULL)
288 {
289 unsigned char* p = new unsigned char[psize];
290 this->do_read(poff, psize, p);
291 v = new File_read::View(poff, psize, p, cache, false);
292 }
293 else
294 {
295 void* p = ::mmap(NULL, psize, PROT_READ, MAP_SHARED,
296 this->descriptor_, poff);
297 if (p == MAP_FAILED)
298 {
299 fprintf(stderr, _("%s: %s: mmap offset %lld size %lld failed: %s\n"),
300 program_name, this->filename().c_str(),
301 static_cast<long long>(poff),
302 static_cast<long long>(psize),
303 strerror(errno));
304 gold_exit(false);
305 }
306
307 this->mapped_bytes_ += psize;
308
309 const unsigned char* pbytes = static_cast<const unsigned char*>(p);
310 v = new File_read::View(poff, psize, pbytes, cache, true);
311 }
312
313 ins.first->second = v;
314 return v;
315 }
316
317 // This implementation of get_view just reads into a memory buffer,
318 // which we store on view_list_. At some point we should support
319 // mmap.
320
321 const unsigned char*
322 File_read::get_view(off_t start, off_t size, bool cache)
323 {
324 gold_assert(this->lock_count_ > 0);
325 File_read::View* pv = this->find_or_make_view(start, size, cache);
326 return pv->data() + (start - pv->start());
327 }
328
329 File_view*
330 File_read::get_lasting_view(off_t start, off_t size, bool cache)
331 {
332 gold_assert(this->lock_count_ > 0);
333 File_read::View* pv = this->find_or_make_view(start, size, cache);
334 pv->lock();
335 return new File_view(*this, pv, pv->data() + (start - pv->start()));
336 }
337
338 // Remove all the file views.
339
340 void
341 File_read::clear_views(bool destroying)
342 {
343 for (Views::iterator p = this->views_.begin();
344 p != this->views_.end();
345 ++p)
346 {
347 if (!p->second->is_locked()
348 && (destroying || !p->second->should_cache()))
349 delete p->second;
350 else
351 {
352 gold_assert(!destroying);
353 this->saved_views_.push_back(p->second);
354 }
355 }
356 this->views_.clear();
357
358 Saved_views::iterator p = this->saved_views_.begin();
359 while (p != this->saved_views_.end())
360 {
361 if (!(*p)->is_locked()
362 && (destroying || !(*p)->should_cache()))
363 {
364 delete *p;
365 p = this->saved_views_.erase(p);
366 }
367 else
368 {
369 gold_assert(!destroying);
370 ++p;
371 }
372 }
373 }
374
375 // Print statistical information to stderr. This is used for --stats.
376
377 void
378 File_read::print_stats()
379 {
380 fprintf(stderr, _("%s: total bytes mapped for read: %llu\n"),
381 program_name, File_read::total_mapped_bytes);
382 fprintf(stderr, _("%s: maximum bytes mapped for read at one time: %llu\n"),
383 program_name, File_read::maximum_mapped_bytes);
384 }
385
386 // Class File_view.
387
388 File_view::~File_view()
389 {
390 gold_assert(this->file_.is_locked());
391 this->view_->unlock();
392 }
393
394 // Class Input_file.
395
396 // Create a file for testing.
397
398 Input_file::Input_file(const char* name, const unsigned char* contents,
399 off_t size)
400 : file_()
401 {
402 this->input_argument_ =
403 new Input_file_argument(name, false, "", Position_dependent_options());
404 bool ok = file_.open(name, contents, size);
405 gold_assert(ok);
406 }
407
408 // Open the file.
409
410 // If the filename is not absolute, we assume it is in the current
411 // directory *except* when:
412 // A) input_argument_->is_lib() is true; or
413 // B) input_argument_->extra_search_path() is not empty.
414 // In both cases, we look in extra_search_path + library_path to find
415 // the file location, rather than the current directory.
416
417 void
418 Input_file::open(const General_options& options, const Dirsearch& dirpath)
419 {
420 std::string name;
421
422 // Case 1: name is an absolute file, just try to open it
423 // Case 2: name is relative but is_lib is false and extra_search_path
424 // is empty
425 if (IS_ABSOLUTE_PATH (this->input_argument_->name())
426 || (!this->input_argument_->is_lib()
427 && this->input_argument_->extra_search_path() == NULL))
428 {
429 name = this->input_argument_->name();
430 this->found_name_ = name;
431 }
432 // Case 3: is_lib is true
433 else if (this->input_argument_->is_lib())
434 {
435 // We don't yet support extra_search_path with -l.
436 gold_assert(this->input_argument_->extra_search_path() == NULL);
437 std::string n1("lib");
438 n1 += this->input_argument_->name();
439 std::string n2;
440 if (options.is_static())
441 n1 += ".a";
442 else
443 {
444 n2 = n1 + ".a";
445 n1 += ".so";
446 }
447 name = dirpath.find(n1, n2, &this->is_in_sysroot_);
448 if (name.empty())
449 {
450 fprintf(stderr, _("%s: cannot find -l%s\n"), program_name,
451 this->input_argument_->name());
452 gold_exit(false);
453 }
454 if (n2.empty() || name[name.length() - 1] == 'o')
455 this->found_name_ = n1;
456 else
457 this->found_name_ = n2;
458 }
459 // Case 4: extra_search_path is not empty
460 else
461 {
462 gold_assert(this->input_argument_->extra_search_path() != NULL);
463
464 // First, check extra_search_path.
465 name = this->input_argument_->extra_search_path();
466 if (!IS_DIR_SEPARATOR (name[name.length() - 1]))
467 name += '/';
468 name += this->input_argument_->name();
469 struct stat dummy_stat;
470 if (::stat(name.c_str(), &dummy_stat) < 0)
471 {
472 // extra_search_path failed, so check the normal search-path.
473 name = dirpath.find(this->input_argument_->name(), "",
474 &this->is_in_sysroot_);
475 if (name.empty())
476 {
477 fprintf(stderr, _("%s: cannot find %s\n"), program_name,
478 this->input_argument_->name());
479 gold_exit(false);
480 }
481 }
482 this->found_name_ = this->input_argument_->name();
483 }
484
485 // Now that we've figured out where the file lives, try to open it.
486 if (!this->file_.open(name))
487 {
488 fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
489 name.c_str(), strerror(errno));
490 gold_exit(false);
491 }
492 }
493
494 } // End namespace gold.
This page took 0.041054 seconds and 5 git commands to generate.