* gold-threads.h (class Once): Define.
[deliverable/binutils-gdb.git] / gold / fileread.h
1 // fileread.h -- read files for gold -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 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 // Classes used to read data from binary input files.
24
25 #ifndef GOLD_FILEREAD_H
26 #define GOLD_FILEREAD_H
27
28 #include <list>
29 #include <map>
30 #include <string>
31 #include <vector>
32
33 #include "token.h"
34
35 namespace gold
36 {
37
38 // Since not all system supports stat.st_mtim and struct timespec,
39 // we define our own structure and fill the nanoseconds if we can.
40
41 struct Timespec
42 {
43 Timespec()
44 : seconds(0), nanoseconds(0)
45 { }
46
47 Timespec(time_t a_seconds, int a_nanoseconds)
48 : seconds(a_seconds), nanoseconds(a_nanoseconds)
49 { }
50
51 time_t seconds;
52 int nanoseconds;
53 };
54
55 class Position_dependent_options;
56 class Input_file_argument;
57 class Dirsearch;
58 class File_view;
59
60 // File_read manages a file descriptor and mappings for a file we are
61 // reading.
62
63 class File_read
64 {
65 public:
66 File_read()
67 : name_(), descriptor_(-1), is_descriptor_opened_(false), object_count_(0),
68 size_(0), token_(false), views_(), saved_views_(), mapped_bytes_(0),
69 released_(true), whole_file_view_(NULL)
70 { }
71
72 ~File_read();
73
74 // Open a file.
75 bool
76 open(const Task*, const std::string& name);
77
78 // Pretend to open the file, but provide the file contents. No
79 // actual file system activity will occur. This is used for
80 // testing.
81 bool
82 open(const Task*, const std::string& name, const unsigned char* contents,
83 off_t size);
84
85 // Return the file name.
86 const std::string&
87 filename() const
88 { return this->name_; }
89
90 // Add an object associated with a file.
91 void
92 add_object()
93 { ++this->object_count_; }
94
95 // Remove an object associated with a file.
96 void
97 remove_object()
98 { --this->object_count_; }
99
100 // Lock the file for exclusive access within a particular Task::run
101 // execution. This routine may only be called when the workqueue
102 // lock is held.
103 void
104 lock(const Task* t);
105
106 // Unlock the file.
107 void
108 unlock(const Task* t);
109
110 // Test whether the object is locked.
111 bool
112 is_locked() const;
113
114 // Return the token, so that the task can be queued.
115 Task_token*
116 token()
117 { return &this->token_; }
118
119 // Release the file. This indicates that we aren't going to do
120 // anything further with it until it is unlocked. This is used
121 // because a Task which locks the file never calls either lock or
122 // unlock; it just locks the token. The basic rule is that a Task
123 // which locks a file via the Task::locks interface must explicitly
124 // call release() when it is done. This is not necessary for code
125 // which calls unlock() on the file.
126 void
127 release();
128
129 // Return the size of the file.
130 off_t
131 filesize() const
132 { return this->size_; }
133
134 // Return a view into the file starting at file offset START for
135 // SIZE bytes. OFFSET is the offset into the input file for the
136 // file we are reading; this is zero for a normal object file,
137 // non-zero for an object file in an archive. ALIGNED is true if
138 // the data must be naturally aligned; this only matters when OFFSET
139 // is not zero. The pointer will remain valid until the File_read
140 // is unlocked. It is an error if we can not read enough data from
141 // the file. The CACHE parameter is a hint as to whether it will be
142 // useful to cache this data for later accesses--i.e., later calls
143 // to get_view, read, or get_lasting_view which retrieve the same
144 // data.
145 const unsigned char*
146 get_view(off_t offset, off_t start, section_size_type size, bool aligned,
147 bool cache);
148
149 // Read data from the file into the buffer P starting at file offset
150 // START for SIZE bytes.
151 void
152 read(off_t start, section_size_type size, void* p);
153
154 // Return a lasting view into the file starting at file offset START
155 // for SIZE bytes. This is allocated with new, and the caller is
156 // responsible for deleting it when done. The data associated with
157 // this view will remain valid until the view is deleted. It is an
158 // error if we can not read enough data from the file. The OFFSET,
159 // ALIGNED and CACHE parameters are as in get_view.
160 File_view*
161 get_lasting_view(off_t offset, off_t start, section_size_type size,
162 bool aligned, bool cache);
163
164 // Mark all views as no longer cached.
165 void
166 clear_view_cache_marks();
167
168 // Discard all uncached views. This is normally done by release(),
169 // but not for objects in archives. FIXME: This is a complicated
170 // interface, and it would be nice to have something more automatic.
171 void
172 clear_uncached_views()
173 { this->clear_views(false); }
174
175 // A struct used to do a multiple read.
176 struct Read_multiple_entry
177 {
178 // The file offset of the data to read.
179 off_t file_offset;
180 // The amount of data to read.
181 section_size_type size;
182 // The buffer where the data should be placed.
183 unsigned char* buffer;
184
185 Read_multiple_entry(off_t o, section_size_type s, unsigned char* b)
186 : file_offset(o), size(s), buffer(b)
187 { }
188 };
189
190 typedef std::vector<Read_multiple_entry> Read_multiple;
191
192 // Read a bunch of data from the file into various different
193 // locations. The vector must be sorted by ascending file_offset.
194 // BASE is a base offset to be added to all the offsets in the
195 // vector.
196 void
197 read_multiple(off_t base, const Read_multiple&);
198
199 // Dump statistical information to stderr.
200 static void
201 print_stats();
202
203 // Return the open file descriptor (for plugins).
204 int
205 descriptor()
206 {
207 this->reopen_descriptor();
208 return this->descriptor_;
209 }
210
211 // Return the file last modification time. Calls gold_fatal if the stat
212 // system call failed.
213 Timespec
214 get_mtime();
215
216 private:
217 // This class may not be copied.
218 File_read(const File_read&);
219 File_read& operator=(const File_read&);
220
221 // Total bytes mapped into memory during the link if --stats.
222 static unsigned long long total_mapped_bytes;
223
224 // Current number of bytes mapped into memory during the link if
225 // --stats.
226 static unsigned long long current_mapped_bytes;
227
228 // High water mark of bytes mapped into memory during the link if
229 // --stats.
230 static unsigned long long maximum_mapped_bytes;
231
232 // A view into the file.
233 class View
234 {
235 public:
236 // Specifies how to dispose the data on destruction of the view.
237 enum Data_ownership
238 {
239 // Data owned by File object - nothing done in destructor.
240 DATA_NOT_OWNED,
241 // Data alocated with new[] and owned by this object - should
242 // use delete[].
243 DATA_ALLOCATED_ARRAY,
244 // Data mmapped and owned by this object - should munmap.
245 DATA_MMAPPED
246 };
247
248 View(off_t start, section_size_type size, const unsigned char* data,
249 unsigned int byteshift, bool cache, Data_ownership data_ownership)
250 : start_(start), size_(size), data_(data), lock_count_(0),
251 byteshift_(byteshift), cache_(cache), data_ownership_(data_ownership),
252 accessed_(true)
253 { }
254
255 ~View();
256
257 off_t
258 start() const
259 { return this->start_; }
260
261 section_size_type
262 size() const
263 { return this->size_; }
264
265 const unsigned char*
266 data() const
267 { return this->data_; }
268
269 void
270 lock();
271
272 void
273 unlock();
274
275 bool
276 is_locked();
277
278 unsigned int
279 byteshift() const
280 { return this->byteshift_; }
281
282 void
283 set_cache()
284 { this->cache_ = true; }
285
286 void
287 clear_cache()
288 { this->cache_ = false; }
289
290 bool
291 should_cache() const
292 { return this->cache_; }
293
294 void
295 set_accessed()
296 { this->accessed_ = true; }
297
298 void
299 clear_accessed()
300 { this->accessed_= false; }
301
302 bool
303 accessed() const
304 { return this->accessed_; }
305
306 private:
307 View(const View&);
308 View& operator=(const View&);
309
310 // The file offset of the start of the view.
311 off_t start_;
312 // The size of the view.
313 section_size_type size_;
314 // A pointer to the actual bytes.
315 const unsigned char* data_;
316 // The number of locks on this view.
317 int lock_count_;
318 // The number of bytes that the view is shifted relative to the
319 // underlying file. This is used to align data. This is normally
320 // zero, except possibly for an object in an archive.
321 unsigned int byteshift_;
322 // Whether the view is cached.
323 bool cache_;
324 // Whether the view is mapped into memory. If not, data_ points
325 // to memory allocated using new[].
326 Data_ownership data_ownership_;
327 // Whether the view has been accessed recently.
328 bool accessed_;
329 };
330
331 friend class View;
332 friend class File_view;
333
334 // The type of a mapping from page start and byte shift to views.
335 typedef std::map<std::pair<off_t, unsigned int>, View*> Views;
336
337 // A simple list of Views.
338 typedef std::list<View*> Saved_views;
339
340 // Open the descriptor if necessary.
341 void
342 reopen_descriptor();
343
344 // Find a view into the file.
345 View*
346 find_view(off_t start, section_size_type size, unsigned int byteshift,
347 View** vshifted) const;
348
349 // Read data from the file into a buffer.
350 void
351 do_read(off_t start, section_size_type size, void* p);
352
353 // Add a view.
354 void
355 add_view(View*);
356
357 // Make a view into the file.
358 View*
359 make_view(off_t start, section_size_type size, unsigned int byteshift,
360 bool cache);
361
362 // Find or make a view into the file.
363 View*
364 find_or_make_view(off_t offset, off_t start, section_size_type size,
365 bool aligned, bool cache);
366
367 // Clear the file views.
368 void
369 clear_views(bool);
370
371 // The size of a file page for buffering data.
372 static const off_t page_size = 8192;
373
374 // Given a file offset, return the page offset.
375 static off_t
376 page_offset(off_t file_offset)
377 { return file_offset & ~ (page_size - 1); }
378
379 // Given a file size, return the size to read integral pages.
380 static off_t
381 pages(off_t file_size)
382 { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
383
384 // The maximum number of entries we will pass to ::readv.
385 #ifdef HAVE_READV
386 static const size_t max_readv_entries = 128;
387 #else
388 // On targets that don't have readv set the max to 1 so readv is not
389 // used.
390 static const size_t max_readv_entries = 1;
391 #endif
392
393 // Use readv to read data.
394 void
395 do_readv(off_t base, const Read_multiple&, size_t start, size_t count);
396
397 // File name.
398 std::string name_;
399 // File descriptor.
400 int descriptor_;
401 // Whether we have regained the descriptor after releasing the file.
402 bool is_descriptor_opened_;
403 // The number of objects associated with this file. This will be
404 // more than 1 in the case of an archive.
405 int object_count_;
406 // File size.
407 off_t size_;
408 // A token used to lock the file.
409 Task_token token_;
410 // Buffered views into the file.
411 Views views_;
412 // List of views which were locked but had to be removed from views_
413 // because they were not large enough.
414 Saved_views saved_views_;
415 // Total amount of space mapped into memory. This is only changed
416 // while the file is locked. When we unlock the file, we transfer
417 // the total to total_mapped_bytes, and reset this to zero.
418 size_t mapped_bytes_;
419 // Whether the file was released.
420 bool released_;
421 // A view containing the whole file. May be NULL if we mmap only
422 // the relevant parts of the file. Not NULL if:
423 // - Flag --mmap_whole_files is set (default on 64-bit hosts).
424 // - The contents was specified in the constructor. Used only for
425 // testing purposes).
426 View* whole_file_view_;
427 };
428
429 // A view of file data that persists even when the file is unlocked.
430 // Callers should destroy these when no longer required. These are
431 // obtained form File_read::get_lasting_view. They may only be
432 // destroyed when the underlying File_read is locked.
433
434 class File_view
435 {
436 public:
437 // This may only be called when the underlying File_read is locked.
438 ~File_view();
439
440 // Return a pointer to the data associated with this view.
441 const unsigned char*
442 data() const
443 { return this->data_; }
444
445 private:
446 File_view(const File_view&);
447 File_view& operator=(const File_view&);
448
449 friend class File_read;
450
451 // Callers have to get these via File_read::get_lasting_view.
452 File_view(File_read& file, File_read::View* view, const unsigned char* data)
453 : file_(file), view_(view), data_(data)
454 { }
455
456 File_read& file_;
457 File_read::View* view_;
458 const unsigned char* data_;
459 };
460
461 // All the information we hold for a single input file. This can be
462 // an object file, a shared library, or an archive.
463
464 class Input_file
465 {
466 public:
467 Input_file(const Input_file_argument* input_argument)
468 : input_argument_(input_argument), found_name_(), file_(),
469 is_in_sysroot_(false)
470 { }
471
472 // Create an input file with the contents already provided. This is
473 // only used for testing. With this path, don't call the open
474 // method.
475 Input_file(const Task*, const char* name, const unsigned char* contents,
476 off_t size);
477
478 // Return the command line argument.
479 const Input_file_argument*
480 input_file_argument() const
481 { return this->input_argument_; }
482
483 // Return whether this is a file that we will search for in the list
484 // of directories.
485 bool
486 will_search_for() const;
487
488 // Open the file. If the open fails, this will report an error and
489 // return false. If there is a search, it starts at directory
490 // *PINDEX. *PINDEX should be initialized to zero. It may be
491 // restarted to find the next file with a matching name by
492 // incrementing the result and calling this again.
493 bool
494 open(const Dirsearch&, const Task*, int *pindex);
495
496 // Return the name given by the user. For -lc this will return "c".
497 const char*
498 name() const;
499
500 // Return the file name. For -lc this will return something like
501 // "/usr/lib/libc.so".
502 const std::string&
503 filename() const
504 { return this->file_.filename(); }
505
506 // Return the name under which we found the file, corresponding to
507 // the command line. For -lc this will return something like
508 // "libc.so".
509 const std::string&
510 found_name() const
511 { return this->found_name_; }
512
513 // Return the position dependent options.
514 const Position_dependent_options&
515 options() const;
516
517 // Return the file.
518 File_read&
519 file()
520 { return this->file_; }
521
522 const File_read&
523 file() const
524 { return this->file_; }
525
526 // Whether we found the file in a directory in the system root.
527 bool
528 is_in_sysroot() const
529 { return this->is_in_sysroot_; }
530
531 // Whether this file is in a system directory.
532 bool
533 is_in_system_directory() const;
534
535 // Return whether this file is to be read only for its symbols.
536 bool
537 just_symbols() const;
538
539 private:
540 Input_file(const Input_file&);
541 Input_file& operator=(const Input_file&);
542
543 // Open a binary file.
544 bool
545 open_binary(const Task* task, const std::string& name);
546
547 // The argument from the command line.
548 const Input_file_argument* input_argument_;
549 // The name under which we opened the file. This is like the name
550 // on the command line, but -lc turns into libc.so (or whatever).
551 // It only includes the full path if the path was on the command
552 // line.
553 std::string found_name_;
554 // The file after we open it.
555 File_read file_;
556 // Whether we found the file in a directory in the system root.
557 bool is_in_sysroot_;
558 };
559
560 } // end namespace gold
561
562 #endif // !defined(GOLD_FILEREAD_H)
This page took 0.041825 seconds and 5 git commands to generate.