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