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