* tui/tui-stack.c (tui_make_status_line): Display PC as ?? if unknown.
[deliverable/binutils-gdb.git] / gold / fileread.h
CommitLineData
bae7f79e
ILT
1// fileread.h -- read files for gold -*- C++ -*-
2
0f7c0701 3// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6cb15b7f
ILT
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
bae7f79e
ILT
23// Classes used to read data from binary input files.
24
25#ifndef GOLD_FILEREAD_H
26#define GOLD_FILEREAD_H
27
bae7f79e 28#include <list>
ead1e424
ILT
29#include <map>
30#include <string>
0c0a7411 31#include <vector>
bae7f79e 32
17a1d0a9 33#include "token.h"
bae7f79e
ILT
34
35namespace gold
36{
37
98fa85cb
ILT
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
41struct 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
14144f39
ILT
55class Position_dependent_options;
56class Input_file_argument;
bae7f79e 57class Dirsearch;
bae7f79e
ILT
58class File_view;
59
2a00e4fb
ILT
60// File_read manages a file descriptor and mappings for a file we are
61// reading.
bae7f79e
ILT
62
63class File_read
64{
65 public:
66 File_read()
2a00e4fb
ILT
67 : name_(), descriptor_(-1), is_descriptor_opened_(false), object_count_(0),
68 size_(0), token_(false), views_(), saved_views_(), contents_(NULL),
69 mapped_bytes_(0), released_(true)
bae7f79e 70 { }
5a6f7e2d 71
bae7f79e
ILT
72 ~File_read();
73
74 // Open a file.
75 bool
17a1d0a9 76 open(const Task*, const std::string& name);
bae7f79e 77
5a6f7e2d
ILT
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
17a1d0a9
ILT
82 open(const Task*, const std::string& name, const unsigned char* contents,
83 off_t size);
5a6f7e2d 84
bae7f79e
ILT
85 // Return the file name.
86 const std::string&
87 filename() const
88 { return this->name_; }
89
cb295612
ILT
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
17a1d0a9 100 // Lock the file for exclusive access within a particular Task::run
2a00e4fb
ILT
101 // execution. This routine may only be called when the workqueue
102 // lock is held.
bae7f79e 103 void
17a1d0a9 104 lock(const Task* t);
bae7f79e 105
2a00e4fb 106 // Unlock the file.
bae7f79e 107 void
17a1d0a9 108 unlock(const Task* t);
4973341a 109
bae7f79e
ILT
110 // Test whether the object is locked.
111 bool
7004837e 112 is_locked() const;
bae7f79e 113
17a1d0a9
ILT
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
82dcae9d
ILT
129 // Return the size of the file.
130 off_t
131 filesize() const
132 { return this->size_; }
133
ba45d247 134 // Return a view into the file starting at file offset START for
39d0cb0e
ILT
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
9eb9fa57
ILT
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.
bae7f79e 145 const unsigned char*
39d0cb0e
ILT
146 get_view(off_t offset, off_t start, section_size_type size, bool aligned,
147 bool cache);
bae7f79e 148
ba45d247
ILT
149 // Read data from the file into the buffer P starting at file offset
150 // START for SIZE bytes.
151 void
2a00e4fb 152 read(off_t start, section_size_type size, void* p);
ba45d247 153
ba45d247
ILT
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
39d0cb0e
ILT
158 // error if we can not read enough data from the file. The OFFSET,
159 // ALIGNED and CACHE parameters are as in get_view.
bae7f79e 160 File_view*
39d0cb0e
ILT
161 get_lasting_view(off_t offset, off_t start, section_size_type size,
162 bool aligned, bool cache);
bae7f79e 163
cb295612
ILT
164 // Mark all views as no longer cached.
165 void
166 clear_view_cache_marks();
167
39d0cb0e
ILT
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
cb295612
ILT
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
e44fcf3b
ILT
199 // Dump statistical information to stderr.
200 static void
201 print_stats();
202
89fc3421
CC
203 // Return the open file descriptor (for plugins).
204 int
0f7c0701 205 descriptor()
89fc3421 206 {
0f7c0701 207 this->reopen_descriptor();
89fc3421
CC
208 return this->descriptor_;
209 }
98fa85cb
ILT
210
211 // Return the file last modification time. Calls gold_fatal if the stat
212 // system call failed.
213 Timespec
214 get_mtime();
89fc3421 215
bae7f79e
ILT
216 private:
217 // This class may not be copied.
218 File_read(const File_read&);
219 File_read& operator=(const File_read&);
220
17a1d0a9
ILT
221 // Total bytes mapped into memory during the link. This variable
222 // may not be accurate when running multi-threaded.
e44fcf3b
ILT
223 static unsigned long long total_mapped_bytes;
224
225 // Current number of bytes mapped into memory during the link. This
17a1d0a9 226 // variable may not be accurate when running multi-threaded.
e44fcf3b
ILT
227 static unsigned long long current_mapped_bytes;
228
229 // High water mark of bytes mapped into memory during the link.
17a1d0a9 230 // This variable may not be accurate when running multi-threaded.
e44fcf3b
ILT
231 static unsigned long long maximum_mapped_bytes;
232
d1038c21 233 // A view into the file.
bae7f79e
ILT
234 class View
235 {
236 public:
8383303e 237 View(off_t start, section_size_type size, const unsigned char* data,
39d0cb0e 238 unsigned int byteshift, bool cache, bool mapped)
9eb9fa57 239 : start_(start), size_(size), data_(data), lock_count_(0),
39d0cb0e 240 byteshift_(byteshift), cache_(cache), mapped_(mapped), accessed_(true)
bae7f79e
ILT
241 { }
242
243 ~View();
244
245 off_t
246 start() const
247 { return this->start_; }
248
8383303e 249 section_size_type
bae7f79e
ILT
250 size() const
251 { return this->size_; }
252
e214a02b 253 const unsigned char*
bae7f79e
ILT
254 data() const
255 { return this->data_; }
256
257 void
258 lock();
259
260 void
261 unlock();
262
263 bool
264 is_locked();
265
39d0cb0e
ILT
266 unsigned int
267 byteshift() const
268 { return this->byteshift_; }
269
9eb9fa57
ILT
270 void
271 set_cache()
272 { this->cache_ = true; }
273
cb295612
ILT
274 void
275 clear_cache()
276 { this->cache_ = false; }
277
9eb9fa57
ILT
278 bool
279 should_cache() const
280 { return this->cache_; }
281
cb295612
ILT
282 void
283 set_accessed()
284 { this->accessed_ = true; }
285
286 void
287 clear_accessed()
288 { this->accessed_= false; }
289
290 bool
291 accessed() const
292 { return this->accessed_; }
293
bae7f79e
ILT
294 private:
295 View(const View&);
296 View& operator=(const View&);
297
39d0cb0e 298 // The file offset of the start of the view.
bae7f79e 299 off_t start_;
39d0cb0e 300 // The size of the view.
8383303e 301 section_size_type size_;
39d0cb0e 302 // A pointer to the actual bytes.
e214a02b 303 const unsigned char* data_;
39d0cb0e 304 // The number of locks on this view.
bae7f79e 305 int lock_count_;
39d0cb0e
ILT
306 // The number of bytes that the view is shifted relative to the
307 // underlying file. This is used to align data. This is normally
308 // zero, except possibly for an object in an archive.
309 unsigned int byteshift_;
310 // Whether the view is cached.
9eb9fa57 311 bool cache_;
39d0cb0e
ILT
312 // Whether the view is mapped into memory. If not, data_ points
313 // to memory allocated using new[].
d1038c21 314 bool mapped_;
39d0cb0e 315 // Whether the view has been accessed recently.
cb295612 316 bool accessed_;
bae7f79e
ILT
317 };
318
e44fcf3b 319 friend class View;
bae7f79e
ILT
320 friend class File_view;
321
39d0cb0e
ILT
322 // The type of a mapping from page start and byte shift to views.
323 typedef std::map<std::pair<off_t, unsigned int>, View*> Views;
324
325 // A simple list of Views.
326 typedef std::list<View*> Saved_views;
327
2a00e4fb
ILT
328 // Open the descriptor if necessary.
329 void
330 reopen_descriptor();
331
ead1e424 332 // Find a view into the file.
bae7f79e 333 View*
39d0cb0e
ILT
334 find_view(off_t start, section_size_type size, unsigned int byteshift,
335 View** vshifted) const;
bae7f79e 336
ead1e424 337 // Read data from the file into a buffer.
82dcae9d 338 void
2a00e4fb 339 do_read(off_t start, section_size_type size, void* p);
bae7f79e 340
39d0cb0e
ILT
341 // Add a view.
342 void
343 add_view(View*);
344
345 // Make a view into the file.
346 View*
347 make_view(off_t start, section_size_type size, unsigned int byteshift,
348 bool cache);
349
ead1e424 350 // Find or make a view into the file.
bae7f79e 351 View*
39d0cb0e
ILT
352 find_or_make_view(off_t offset, off_t start, section_size_type size,
353 bool aligned, bool cache);
bae7f79e 354
ead1e424 355 // Clear the file views.
bae7f79e
ILT
356 void
357 clear_views(bool);
358
ead1e424
ILT
359 // The size of a file page for buffering data.
360 static const off_t page_size = 8192;
361
362 // Given a file offset, return the page offset.
363 static off_t
364 page_offset(off_t file_offset)
365 { return file_offset & ~ (page_size - 1); }
366
367 // Given a file size, return the size to read integral pages.
368 static off_t
369 pages(off_t file_size)
370 { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
371
cb295612
ILT
372 // The maximum number of entries we will pass to ::readv.
373 static const size_t max_readv_entries = 128;
374
375 // Use readv to read data.
376 void
377 do_readv(off_t base, const Read_multiple&, size_t start, size_t count);
378
ead1e424 379 // File name.
bae7f79e 380 std::string name_;
ead1e424 381 // File descriptor.
bae7f79e 382 int descriptor_;
2a00e4fb
ILT
383 // Whether we have regained the descriptor after releasing the file.
384 bool is_descriptor_opened_;
cb295612
ILT
385 // The number of objects associated with this file. This will be
386 // more than 1 in the case of an archive.
387 int object_count_;
82dcae9d
ILT
388 // File size.
389 off_t size_;
17a1d0a9
ILT
390 // A token used to lock the file.
391 Task_token token_;
ead1e424
ILT
392 // Buffered views into the file.
393 Views views_;
394 // List of views which were locked but had to be removed from views_
395 // because they were not large enough.
396 Saved_views saved_views_;
5a6f7e2d
ILT
397 // Specified file contents. Used only for testing purposes.
398 const unsigned char* contents_;
e44fcf3b
ILT
399 // Total amount of space mapped into memory. This is only changed
400 // while the file is locked. When we unlock the file, we transfer
401 // the total to total_mapped_bytes, and reset this to zero.
402 size_t mapped_bytes_;
17a1d0a9
ILT
403 // Whether the file was released.
404 bool released_;
bae7f79e
ILT
405};
406
407// A view of file data that persists even when the file is unlocked.
408// Callers should destroy these when no longer required. These are
409// obtained form File_read::get_lasting_view. They may only be
410// destroyed when the underlying File_read is locked.
411
412class File_view
413{
414 public:
415 // This may only be called when the underlying File_read is locked.
416 ~File_view();
417
418 // Return a pointer to the data associated with this view.
419 const unsigned char*
420 data() const
421 { return this->data_; }
422
423 private:
424 File_view(const File_view&);
425 File_view& operator=(const File_view&);
426
427 friend class File_read;
428
429 // Callers have to get these via File_read::get_lasting_view.
430 File_view(File_read& file, File_read::View* view, const unsigned char* data)
431 : file_(file), view_(view), data_(data)
432 { }
433
434 File_read& file_;
435 File_read::View* view_;
436 const unsigned char* data_;
437};
438
bae7f79e
ILT
439// All the information we hold for a single input file. This can be
440// an object file, a shared library, or an archive.
441
442class Input_file
443{
444 public:
5a6f7e2d 445 Input_file(const Input_file_argument* input_argument)
e2aacd2c
ILT
446 : input_argument_(input_argument), found_name_(), file_(),
447 is_in_sysroot_(false)
bae7f79e
ILT
448 { }
449
5a6f7e2d
ILT
450 // Create an input file with the contents already provided. This is
451 // only used for testing. With this path, don't call the open
452 // method.
17a1d0a9
ILT
453 Input_file(const Task*, const char* name, const unsigned char* contents,
454 off_t size);
5a6f7e2d 455
15f8229b
ILT
456 // Return the command line argument.
457 const Input_file_argument*
458 input_file_argument() const
459 { return this->input_argument_; }
460
461 // Return whether this is a file that we will search for in the list
462 // of directories.
463 bool
464 will_search_for() const;
465
75f2446e 466 // Open the file. If the open fails, this will report an error and
15f8229b
ILT
467 // return false. If there is a search, it starts at directory
468 // *PINDEX. *PINDEX should be initialized to zero. It may be
469 // restarted to find the next file with a matching name by
470 // incrementing the result and calling this again.
75f2446e 471 bool
15f8229b 472 open(const Dirsearch&, const Task*, int *pindex);
bae7f79e 473
e2aacd2c 474 // Return the name given by the user. For -lc this will return "c".
bae7f79e 475 const char*
14144f39 476 name() const;
bae7f79e 477
e2aacd2c
ILT
478 // Return the file name. For -lc this will return something like
479 // "/usr/lib/libc.so".
bae7f79e
ILT
480 const std::string&
481 filename() const
482 { return this->file_.filename(); }
483
e2aacd2c
ILT
484 // Return the name under which we found the file, corresponding to
485 // the command line. For -lc this will return something like
486 // "libc.so".
487 const std::string&
488 found_name() const
489 { return this->found_name_; }
490
4973341a
ILT
491 // Return the position dependent options.
492 const Position_dependent_options&
14144f39 493 options() const;
4973341a
ILT
494
495 // Return the file.
bae7f79e
ILT
496 File_read&
497 file()
498 { return this->file_; }
499
7004837e
ILT
500 const File_read&
501 file() const
502 { return this->file_; }
503
ad2d6943
ILT
504 // Whether we found the file in a directory in the system root.
505 bool
506 is_in_sysroot() const
507 { return this->is_in_sysroot_; }
508
fd9d194f
ILT
509 // Whether this file is in a system directory.
510 bool
511 is_in_system_directory() const;
512
88dd47ac
ILT
513 // Return whether this file is to be read only for its symbols.
514 bool
515 just_symbols() const;
516
bae7f79e 517 private:
ead1e424
ILT
518 Input_file(const Input_file&);
519 Input_file& operator=(const Input_file&);
520
bc644c6c
ILT
521 // Open a binary file.
522 bool
f1ed28fb 523 open_binary(const Task* task, const std::string& name);
bc644c6c 524
ad2d6943 525 // The argument from the command line.
5a6f7e2d 526 const Input_file_argument* input_argument_;
e2aacd2c
ILT
527 // The name under which we opened the file. This is like the name
528 // on the command line, but -lc turns into libc.so (or whatever).
529 // It only includes the full path if the path was on the command
530 // line.
531 std::string found_name_;
ad2d6943 532 // The file after we open it.
bae7f79e 533 File_read file_;
ad2d6943
ILT
534 // Whether we found the file in a directory in the system root.
535 bool is_in_sysroot_;
bae7f79e
ILT
536};
537
538} // end namespace gold
539
540#endif // !defined(GOLD_FILEREAD_H)
This page took 0.149057 seconds and 4 git commands to generate.