elfcpp/ChangeLog:
[external/binutils.git] / gold / incremental.h
1 // inremental.h -- incremental linking support for gold   -*- C++ -*-
2
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Mikolaj Zalewski <mikolajz@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 #ifndef GOLD_INCREMENTAL_H
24 #define GOLD_INCREMENTAL_H
25
26 #include <map>
27 #include <vector>
28
29 #include "elfcpp_file.h"
30 #include "stringpool.h"
31 #include "workqueue.h"
32 #include "fileread.h"
33 #include "output.h"
34
35 namespace gold
36 {
37
38 class Archive;
39 class Input_argument;
40 class Incremental_inputs_checker;
41 class Incremental_script_entry;
42 class Incremental_object_entry;
43 class Incremental_archive_entry;
44 class Incremental_inputs;
45 class Object;
46
47 // Incremental input type as stored in .gnu_incremental_inputs.
48
49 enum Incremental_input_type
50 {
51   INCREMENTAL_INPUT_OBJECT = 1,
52   INCREMENTAL_INPUT_ARCHIVE_MEMBER = 2,
53   INCREMENTAL_INPUT_ARCHIVE = 3,
54   INCREMENTAL_INPUT_SHARED_LIBRARY = 4,
55   INCREMENTAL_INPUT_SCRIPT = 5
56 };
57
58 // An object representing the ELF file we edit during an incremental build.
59 // Similar to Object or Dynobj, but operates on Output_file and contains
60 // method specific to file edition (TBD). This is the abstract parent class
61 // implemented in Sized_incremental_binary<size, big_endian> for a specific
62 // endianness and size.
63
64 class Incremental_binary
65 {
66  public:
67   Incremental_binary(Output_file* output, Target* target)
68     : output_(output), target_(target)
69   { }
70
71   virtual
72   ~Incremental_binary()
73   { }
74
75   // Functions and types for the elfcpp::Elf_file interface.  This
76   // permit us to use Incremental_binary as the File template parameter for
77   // elfcpp::Elf_file.
78
79   // The View class is returned by view.  It must support a single
80   // method, data().  This is trivial, because Output_file::get_output_view
81   // does what we need.
82   class View
83   {
84    public:
85     View(const unsigned char* p)
86       : p_(p)
87     { }
88
89     const unsigned char*
90     data() const
91     { return this->p_; }
92
93    private:
94     const unsigned char* p_;
95   };
96
97   // Return a View.
98   View
99   view(off_t file_offset, section_size_type data_size)
100   { return View(this->output_->get_input_view(file_offset, data_size)); }
101
102   // A location in the file.
103   struct Location
104   {
105     off_t file_offset;
106     off_t data_size;
107
108     Location(off_t fo, section_size_type ds)
109       : file_offset(fo), data_size(ds)
110     { }
111
112     Location()
113       : file_offset(0), data_size(0)
114     { }
115   };
116
117   // Get a View given a Location.
118   View
119   view(Location loc)
120   { return View(this->view(loc.file_offset, loc.data_size)); }
121
122   // Report an error.
123   void
124   error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
125
126   // Find the .gnu_incremental_inputs and related sections.  It selects the
127   // first section of type SHT_GNU_INCREMENTAL_INPUTS,
128   // SHT_GNU_INCRMENTAL_SYMTAB, and SHT_GNU_INCREMENTAL_RELOCS.
129   // Returns false if the sections are not found.
130   bool
131   find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
132                                    unsigned int* p_symtab_shndx,
133                                    unsigned int* p_relocs_shndx,
134                                    unsigned int* p_strtab_shndx)
135   {
136     return do_find_incremental_inputs_sections(p_inputs_shndx, p_symtab_shndx,
137                                                p_relocs_shndx, p_strtab_shndx);
138   }
139
140   // Check the .gnu_incremental_inputs section to see whether an incremental
141   // build is possible.
142   // TODO: on success, should report what files needs to be rebuilt.
143   // INCREMENTAL_INPUTS is used to read the canonical form of the command line
144   // and read the input arguments.  TODO: for items that don't need to be
145   // rebuilt, we should also copy the incremental input information.
146   virtual bool
147   check_inputs(Incremental_inputs* incremental_inputs)
148   { return do_check_inputs(incremental_inputs); }
149
150  protected:
151   // Find incremental inputs section.
152   virtual bool
153   do_find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
154                                       unsigned int* p_symtab_shndx,
155                                       unsigned int* p_relocs_shndx,
156                                       unsigned int* p_strtab_shndx) = 0;
157
158   // Check the .gnu_incremental_inputs section to see whether an incremental
159   // build is possible.
160   virtual bool
161   do_check_inputs(Incremental_inputs* incremental_inputs) = 0;
162
163  private:
164   // Edited output file object.
165   Output_file* output_;
166   // Target of the output file.
167   Target* target_;
168 };
169
170 template<int size, bool big_endian>
171 class Sized_incremental_binary : public Incremental_binary
172 {
173  public:
174   Sized_incremental_binary(Output_file* output,
175                            const elfcpp::Ehdr<size, big_endian>& ehdr,
176                            Target* target)
177     : Incremental_binary(output, target), elf_file_(this, ehdr)
178   { }
179
180  protected:
181   virtual bool
182   do_find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
183                                       unsigned int* p_symtab_shndx,
184                                       unsigned int* p_relocs_shndx,
185                                       unsigned int* p_strtab_shndx);
186
187   virtual bool
188   do_check_inputs(Incremental_inputs* incremental_inputs);
189
190  private:
191   // Output as an ELF file.
192   elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
193 };
194
195 // Create an Incremental_binary object for FILE. Returns NULL is this is not
196 // possible, e.g. FILE is not an ELF file or has an unsupported target.
197
198 Incremental_binary*
199 open_incremental_binary(Output_file* file);
200
201 // Code invoked early during an incremental link that checks what files need
202 // to be relinked.
203
204 class Incremental_checker
205 {
206  public:
207   // Check if the file named OUTPUT_NAME can be linked incrementally.
208   // INCREMENTAL_INPUTS must have the canonical form of the command line
209   // and input arguments filled - at this point of linking other fields are
210   // probably not filled yet.  TODO: for inputs that don't need to be
211   // rebuilt, this function should fill the incremental input information.
212   Incremental_checker(const char* output_name,
213                       Incremental_inputs* incremental_inputs)
214     : output_name_(output_name), incremental_inputs_(incremental_inputs)
215   { }
216
217   // Analyzes the output file to check if incremental linking is possible and
218   // what files needs to be relinked.
219   bool
220   can_incrementally_link_output_file();
221
222  private:
223   // Name of the output file to analyze.
224   const char* output_name_;
225
226   // The Incremental_inputs object. At this stage of link, only the command
227   // line and inputs are filled.
228   Incremental_inputs* incremental_inputs_;
229 };
230
231 // Base class for recording each input file.
232
233 class Incremental_input_entry
234 {
235  public:
236   Incremental_input_entry(Stringpool::Key filename_key, Timespec mtime)
237     : filename_key_(filename_key), offset_(0), info_offset_(0), mtime_(mtime)
238   { }
239
240   // Return the type of input file.
241   Incremental_input_type
242   type() const
243   { return this->do_type(); }
244
245   // Set the section offset of this input file entry.
246   void
247   set_offset(unsigned int offset)
248   { this->offset_ = offset; }
249
250   // Set the section offset of the supplemental information for this entry.
251   void
252   set_info_offset(unsigned int info_offset)
253   { this->info_offset_ = info_offset; }
254
255   // Get the section offset of this input file entry.
256   unsigned int
257   get_offset() const
258   { return this->offset_; }
259
260   // Get the section offset of the supplemental information for this entry.
261   unsigned int
262   get_info_offset() const
263   { return this->info_offset_; }
264
265   // Get the stringpool key for the input filename.
266   Stringpool::Key
267   get_filename_key() const
268   { return this->filename_key_; }
269
270   // Get the modification time of the input file.
271   const Timespec&
272   get_mtime() const
273   { return this->mtime_; }
274
275   // Return a pointer to the derived Incremental_script_entry object.
276   // Return NULL for input entries that are not script files.
277   Incremental_script_entry*
278   script_entry()
279   { return this->do_script_entry(); }
280
281   // Return a pointer to the derived Incremental_object_entry object.
282   // Return NULL for input entries that are not object files.
283   Incremental_object_entry*
284   object_entry()
285   { return this->do_object_entry(); }
286
287   // Return a pointer to the derived Incremental_archive_entry object.
288   // Return NULL for input entries that are not archive files.
289   Incremental_archive_entry*
290   archive_entry()
291   { return this->do_archive_entry(); }
292
293  protected:
294   // Return the type of input file.
295   virtual Incremental_input_type
296   do_type() const = 0;
297
298   // Return a pointer to the derived Incremental_script_entry object.
299   // Return NULL for input entries that are not script files.
300   virtual Incremental_script_entry*
301   do_script_entry()
302   { return NULL; }
303
304   // Return a pointer to the derived Incremental_object_entry object.
305   // Return NULL for input entries that are not object files.
306   virtual Incremental_object_entry*
307   do_object_entry()
308   { return NULL; }
309
310   // Return a pointer to the derived Incremental_archive_entry object.
311   // Return NULL for input entries that are not archive files.
312   virtual Incremental_archive_entry*
313   do_archive_entry()
314   { return NULL; }
315
316  private:
317   // Key of the filename string in the section stringtable.
318   Stringpool::Key filename_key_;
319
320   // Offset of the entry in the output section.
321   unsigned int offset_;
322
323   // Offset of the extra information in the output section.
324   unsigned int info_offset_;
325
326   // Last modification time of the file.
327   Timespec mtime_;
328 };
329
330 // Class for recording input scripts.
331
332 class Incremental_script_entry : public Incremental_input_entry
333 {
334  public:
335   Incremental_script_entry(Stringpool::Key filename_key, Script_info* script,
336                            Timespec mtime)
337     : Incremental_input_entry(filename_key, mtime), script_(script)
338   { }
339
340  protected:
341   virtual Incremental_input_type
342   do_type() const
343   { return INCREMENTAL_INPUT_SCRIPT; }
344
345   // Return a pointer to the derived Incremental_script_entry object.
346   virtual Incremental_script_entry*
347   do_script_entry()
348   { return this; }
349
350  private:
351   // Information about the script file.
352   Script_info* script_;
353 };
354
355 // Class for recording input object files.
356
357 class Incremental_object_entry : public Incremental_input_entry
358 {
359  public:
360   Incremental_object_entry(Stringpool::Key filename_key, Object* obj,
361                            Timespec mtime)
362     : Incremental_input_entry(filename_key, mtime), obj_(obj),
363       is_member_(false), sections_()
364   {
365     if (!obj_->is_dynamic())
366       this->sections_.reserve(obj->shnum());
367   }
368
369   // Get the object.
370   Object*
371   object() const
372   { return this->obj_; }
373
374   // Record that this object is an archive member.
375   void
376   set_is_member()
377   { this->is_member_ = true; }
378
379   // Return true if this object is an archive member.
380   bool
381   is_member() const
382   { return this->is_member_; }
383
384   // Add an input section.
385   void
386   add_input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
387   {
388     if (shndx >= this->sections_.size())
389       this->sections_.resize(shndx + 1);
390     this->sections_[shndx].name_key = name_key;
391     this->sections_[shndx].sh_size = sh_size;
392   }
393
394   // Return the number of input sections in this object.
395   unsigned int
396   get_input_section_count() const
397   { return this->sections_.size(); }
398
399   // Return the stringpool key of the Nth input section.
400   Stringpool::Key
401   get_input_section_name_key(unsigned int n) const
402   { return this->sections_[n].name_key; }
403
404   // Return the size of the Nth input section.
405   off_t
406   get_input_section_size(unsigned int n) const
407   { return this->sections_[n].sh_size; }
408
409  protected:
410   virtual Incremental_input_type
411   do_type() const
412   {
413     return (this->is_member_
414             ? INCREMENTAL_INPUT_ARCHIVE_MEMBER
415             : (this->obj_->is_dynamic()
416                ? INCREMENTAL_INPUT_SHARED_LIBRARY
417                : INCREMENTAL_INPUT_OBJECT));
418   }
419
420   // Return a pointer to the derived Incremental_object_entry object.
421   virtual Incremental_object_entry*
422   do_object_entry()
423   { return this; }
424
425  private:
426   // The object file itself.
427   Object* obj_;
428
429   // Whether this object is an archive member.
430   bool is_member_;
431
432   // Input sections.
433   struct Input_section
434   {
435     Stringpool::Key name_key;
436     off_t sh_size;
437   };
438   std::vector<Input_section> sections_;
439 };
440
441 // Class for recording archive library input files.
442
443 class Incremental_archive_entry : public Incremental_input_entry
444 {
445  public:
446   Incremental_archive_entry(Stringpool::Key filename_key, Archive*,
447                             Timespec mtime)
448     : Incremental_input_entry(filename_key, mtime), members_(), unused_syms_()
449   { }
450
451   // Add a member object to the archive.
452   void
453   add_object(Incremental_object_entry* obj_entry)
454   {
455     this->members_.push_back(obj_entry);
456     obj_entry->set_is_member();
457   }
458
459   // Add an unused global symbol to the archive.
460   void
461   add_unused_global_symbol(Stringpool::Key symbol_key)
462   { this->unused_syms_.push_back(symbol_key); }
463
464   // Return the number of member objects included in the link.
465   unsigned int
466   get_member_count()
467   { return this->members_.size(); }
468
469   // Return the Nth member object.
470   Incremental_object_entry*
471   get_member(unsigned int n)
472   { return this->members_[n]; }
473
474   // Return the number of unused global symbols in this archive.
475   unsigned int
476   get_unused_global_symbol_count()
477   { return this->unused_syms_.size(); }
478
479   // Return the Nth unused global symbol.
480   Stringpool::Key
481   get_unused_global_symbol(unsigned int n)
482   { return this->unused_syms_[n]; }
483
484  protected:
485   virtual Incremental_input_type
486   do_type() const
487   { return INCREMENTAL_INPUT_ARCHIVE; }
488
489   // Return a pointer to the derived Incremental_archive_entry object.
490   virtual Incremental_archive_entry*
491   do_archive_entry()
492   { return this; }
493
494  private:
495   // Members of the archive that have been included in the link.
496   std::vector<Incremental_object_entry*> members_;
497
498   // Unused global symbols from this archive.
499   std::vector<Stringpool::Key> unused_syms_;
500 };
501
502 // This class contains the information needed during an incremental
503 // build about the inputs necessary to build the .gnu_incremental_inputs.
504
505 class Incremental_inputs
506 {
507  public:
508   typedef std::vector<Incremental_input_entry*> Input_list;
509
510   Incremental_inputs()
511     : inputs_(), command_line_(), command_line_key_(0),
512       strtab_(new Stringpool()), current_object_(NULL),
513       current_object_entry_(NULL), inputs_section_(NULL),
514       symtab_section_(NULL), relocs_section_(NULL),
515       reloc_count_(0)
516   { }
517
518   ~Incremental_inputs() { delete this->strtab_; }
519
520   // Record the command line.
521   void
522   report_command_line(int argc, const char* const* argv);
523
524   // Record the initial info for archive file ARCHIVE.
525   void
526   report_archive_begin(Archive* arch);
527
528   // Record the final info for archive file ARCHIVE.
529   void
530   report_archive_end(Archive* arch);
531
532   // Record the info for object file OBJ.  If ARCH is not NULL,
533   // attach the object file to the archive.
534   void
535   report_object(Object* obj, Archive* arch);
536
537   // Record an input section belonging to object file OBJ.
538   void
539   report_input_section(Object* obj, unsigned int shndx, const char* name,
540                        off_t sh_size);
541
542   // Record the info for input script SCRIPT.
543   void
544   report_script(const std::string& filename, Script_info* script,
545                 Timespec mtime);
546
547   // Return the running count of incremental relocations.
548   unsigned int
549   get_reloc_count() const
550   { return this->reloc_count_; }
551
552   // Update the running count of incremental relocations.
553   void
554   set_reloc_count(unsigned int count)
555   { this->reloc_count_ = count; }
556
557   // Prepare for layout.  Called from Layout::finalize.
558   void
559   finalize();
560
561   // Create the .gnu_incremental_inputs and related sections.
562   void
563   create_data_sections(Symbol_table* symtab);
564
565   // Return the .gnu_incremental_inputs section.
566   Output_section_data*
567   inputs_section() const
568   { return this->inputs_section_; }
569
570   // Return the .gnu_incremental_symtab section.
571   Output_data_space*
572   symtab_section() const
573   { return this->symtab_section_; }
574
575   // Return the .gnu_incremental_relocs section.
576   Output_data_space*
577   relocs_section() const
578   { return this->relocs_section_; }
579
580   // Return the .gnu_incremental_strtab stringpool.
581   Stringpool*
582   get_stringpool() const
583   { return this->strtab_; }
584
585   // Return the canonical form of the command line, as will be stored in
586   // .gnu_incremental_strtab.
587   const std::string&
588   command_line() const
589   { return this->command_line_; }
590
591   // Return the stringpool key of the command line.
592   Stringpool::Key
593   command_line_key() const
594   { return this->command_line_key_; }
595
596   // Return the number of input files.
597   int
598   input_file_count() const
599   { return this->inputs_.size(); }
600
601   // Return the input files.
602   const Input_list&
603   input_files() const
604   { return this->inputs_; }
605
606   // Return the sh_entsize value for the .gnu_incremental_relocs section.
607   unsigned int
608   relocs_entsize() const;
609
610  private:
611   // The list of input files.
612   Input_list inputs_;
613
614   // Canonical form of the command line, as will be stored in
615   // .gnu_incremental_strtab.
616   std::string command_line_;
617
618   // The key of the command line string in the string pool.
619   Stringpool::Key command_line_key_;
620
621   // The .gnu_incremental_strtab string pool associated with the
622   // .gnu_incremental_inputs.
623   Stringpool* strtab_;
624
625   // Keep track of the object currently being processed.
626   Object* current_object_;
627   Incremental_object_entry* current_object_entry_;
628
629   // The .gnu_incremental_inputs section.
630   Output_section_data* inputs_section_;
631
632   // The .gnu_incremental_symtab section.
633   Output_data_space* symtab_section_;
634
635   // The .gnu_incremental_relocs section.
636   Output_data_space* relocs_section_;
637
638   // Total count of incremental relocations.  Updated during Scan_relocs
639   // phase at the completion of each object file.
640   unsigned int reloc_count_;
641 };
642
643 // Reader class for .gnu_incremental_inputs section.
644
645 template<int size, bool big_endian>
646 class Incremental_inputs_reader
647 {
648  private:
649   typedef elfcpp::Swap<size, big_endian> Swap;
650   typedef elfcpp::Swap<16, big_endian> Swap16;
651   typedef elfcpp::Swap<32, big_endian> Swap32;
652   typedef elfcpp::Swap<64, big_endian> Swap64;
653
654  public:
655   Incremental_inputs_reader(const unsigned char* p, elfcpp::Elf_strtab& strtab)
656     : p_(p), strtab_(strtab)
657   { this->input_file_count_ = Swap32::readval(this->p_ + 4); }
658
659   // Return the version number.
660   unsigned int
661   version() const
662   { return Swap32::readval(this->p_); }
663
664   // Return the count of input file entries.
665   unsigned int
666   input_file_count() const
667   { return this->input_file_count_; }
668
669   // Return the command line.
670   const char*
671   command_line() const
672   {
673     unsigned int offset = Swap32::readval(this->p_ + 8);
674     return this->get_string(offset);
675   }
676
677   // Reader class for an input file entry and its supplemental info.
678   class Incremental_input_entry_reader
679   {
680    public:
681     Incremental_input_entry_reader(const Incremental_inputs_reader* inputs,
682                                    unsigned int offset)
683       : inputs_(inputs), offset_(offset)
684     {
685       this->info_offset_ = Swap32::readval(inputs->p_ + offset + 4);
686       int type = Swap16::readval(this->inputs_->p_ + offset + 20);
687       this->type_ = static_cast<Incremental_input_type>(type);
688     }
689
690     // Return the filename.
691     const char*
692     filename() const
693     {
694       unsigned int offset = Swap32::readval(this->inputs_->p_ + this->offset_);
695       return this->inputs_->get_string(offset);
696     }
697
698     // Return the timestamp.
699     Timespec
700     get_mtime() const
701     {
702       Timespec t;
703       const unsigned char* p = this->inputs_->p_ + this->offset_ + 8;
704       t.seconds = Swap64::readval(p);
705       t.nanoseconds = Swap32::readval(p+8);
706       return t;
707     }
708
709     // Return the type of input file.
710     Incremental_input_type
711     type() const
712     { return this->type_; }
713
714     // Return the input section count -- for objects only.
715     unsigned int
716     get_input_section_count() const
717     {
718       gold_assert(this->type_ == INCREMENTAL_INPUT_OBJECT
719                   || this->type_ == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
720       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
721     }
722
723     // Return the offset of the supplemental info for symbol SYMNDX --
724     // for objects only.
725     unsigned int
726     get_symbol_offset(unsigned int symndx) const
727     {
728       gold_assert(this->type_ == INCREMENTAL_INPUT_OBJECT
729                   || this->type_ == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
730
731       unsigned int section_count = this->get_input_section_count();
732       return (this->info_offset_ + 8
733               + section_count * input_section_entry_size
734               + symndx * 16);
735     }
736
737     // Return the global symbol count -- for objects & shared libraries only.
738     unsigned int
739     get_global_symbol_count() const
740     {
741       switch (this->type_)
742         {
743         case INCREMENTAL_INPUT_OBJECT:
744         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
745           return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
746         case INCREMENTAL_INPUT_SHARED_LIBRARY:
747           return Swap32::readval(this->inputs_->p_ + this->info_offset_);
748         default:
749           gold_unreachable();
750         }
751     }
752
753     // Return the member count -- for archives only.
754     unsigned int
755     get_member_count() const
756     {
757       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
758       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
759     }
760
761     // Return the unused symbol count -- for archives only.
762     unsigned int
763     get_unused_symbol_count() const
764     {
765       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
766       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
767     }
768
769     // Return the input file offset for archive member N -- for archives only.
770     unsigned int
771     get_member_offset(unsigned int n) const
772     {
773       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
774       return Swap32::readval(this->inputs_->p_ + this->info_offset_
775                              + 8 + n * 4);
776     }
777
778     // Return the Nth unused global symbol -- for archives only.
779     const char*
780     get_unused_symbol(unsigned int n) const
781     {
782       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
783       unsigned int member_count = this->get_member_count();
784       unsigned int offset = Swap32::readval(this->inputs_->p_
785                                             + this->info_offset_ + 8
786                                             + member_count * 4
787                                             + n * 4);
788       return this->inputs_->get_string(offset);
789     }
790
791     // Information about an input section.
792     struct Input_section_info
793     {
794       const char* name;
795       unsigned int output_shndx;
796       off_t sh_offset;
797       off_t sh_size;
798     };
799
800     // Return info about the Nth input section -- for objects only.
801     Input_section_info
802     get_input_section(unsigned int n) const
803     {
804       Input_section_info info;
805       const unsigned char* p = (this->inputs_->p_
806                                 + this->info_offset_ + 8
807                                 + n * input_section_entry_size);
808       unsigned int name_offset = Swap32::readval(p);
809       info.name = this->inputs_->get_string(name_offset);
810       info.output_shndx = Swap32::readval(p + 4);
811       info.sh_offset = Swap::readval(p + 8);
812       info.sh_size = Swap::readval(p + 8 + size / 8);
813       return info;
814     }
815
816     // Information about a global symbol.
817     struct Global_symbol_info
818     {
819       unsigned int output_symndx;
820       unsigned int next_offset;
821       unsigned int reloc_count;
822       unsigned int reloc_offset;
823     };
824
825     // Return info about the Nth global symbol -- for objects only.
826     Global_symbol_info
827     get_global_symbol_info(unsigned int n) const
828     {
829       Global_symbol_info info;
830       unsigned int section_count = this->get_input_section_count();
831       const unsigned char* p = (this->inputs_->p_
832                                 + this->info_offset_ + 8
833                                 + section_count * input_section_entry_size
834                                 + n * 16);
835       info.output_symndx = Swap32::readval(p);
836       info.next_offset = Swap32::readval(p + 4);
837       info.reloc_count = Swap::readval(p + 8);
838       info.reloc_offset = Swap::readval(p + 12);
839       return info;
840     }
841
842    private:
843     // Size of an input section entry.
844     static const unsigned int input_section_entry_size = 8 + 2 * size / 8;
845     // The reader instance for the containing section.
846     const Incremental_inputs_reader* inputs_;
847     // The type of input file.
848     Incremental_input_type type_;
849     // Section offset to the input file entry.
850     unsigned int offset_;
851     // Section offset to the supplemental info for the input file.
852     unsigned int info_offset_;
853   };
854
855   // Return a reader for the Nth input file entry.
856   Incremental_input_entry_reader
857   input_file(unsigned int n) const
858   {
859     gold_assert(n < this->input_file_count_);
860     Incremental_input_entry_reader input(this, 16 + n * 24);
861     return input;
862   }
863
864  private:
865   // Lookup a string in the ELF string table.
866   const char* get_string(unsigned int offset) const
867   {
868     const char* s;
869     if (this->strtab_.get_c_string(offset, &s))
870       return s;
871     return NULL;
872   }
873
874   // Base address of the .gnu_incremental_inputs section.
875   const unsigned char* p_;
876   // The associated ELF string table.
877   elfcpp::Elf_strtab strtab_;
878   // The number of input file entries in this section.
879   unsigned int input_file_count_;
880 };
881
882 // Reader class for the .gnu_incremental_symtab section.
883
884 template<bool big_endian>
885 class Incremental_symtab_reader
886 {
887  public:
888   Incremental_symtab_reader(const unsigned char* p) : p_(p)
889   { }
890
891   // Return the list head for symbol table entry N.
892   unsigned int get_list_head(unsigned int n)
893   { return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4 * n); }
894
895  private:
896   // Base address of the .gnu_incremental_relocs section.
897   const unsigned char* p_;
898 };
899
900 // Reader class for the .gnu_incremental_relocs section.
901
902 template<int size, bool big_endian>
903 class Incremental_relocs_reader
904 {
905  private:
906   // Size of each field.
907   static const unsigned int field_size = size / 8;
908
909  public:
910   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
911   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
912
913   // Size of each entry.
914   static const unsigned int reloc_size = 8 + 2 * field_size;
915
916   Incremental_relocs_reader(const unsigned char* p) : p_(p)
917   { }
918
919   // Return the relocation type for relocation entry at offset OFF.
920   unsigned int
921   get_r_type(unsigned int off)
922   {
923     return elfcpp::Swap<32, big_endian>::readval(this->p_ + off);
924   }
925
926   // Return the output section index for relocation entry at offset OFF.
927   unsigned int
928   get_r_shndx(unsigned int off)
929   {
930     return elfcpp::Swap<32, big_endian>::readval(this->p_ + off + 4);
931   }
932
933   // Return the output section offset for relocation entry at offset OFF.
934   Address
935   get_r_offset(unsigned int off)
936   {
937     return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8);
938   }
939
940   // Return the addend for relocation entry at offset OFF.
941   Addend
942   get_r_addend(unsigned int off)
943   {
944     return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8
945                                                    + this->field_size);
946   }
947
948  private:
949   // Base address of the .gnu_incremental_relocs section.
950   const unsigned char* p_;
951 };
952
953 } // End namespace gold.
954
955 #endif // !defined(GOLD_INCREMENTAL_H)