* archive.cc (Archive::include_member): Adjust call to report_object.
[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_INCREMENTAL_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_got_plt_shndx,
135                                    unsigned int* p_strtab_shndx)
136   {
137     return do_find_incremental_inputs_sections(p_inputs_shndx, p_symtab_shndx,
138                                                p_relocs_shndx, p_got_plt_shndx,
139                                                p_strtab_shndx);
140   }
141
142   // Check the .gnu_incremental_inputs section to see whether an incremental
143   // build is possible.
144   // TODO: on success, should report what files needs to be rebuilt.
145   // INCREMENTAL_INPUTS is used to read the canonical form of the command line
146   // and read the input arguments.  TODO: for items that don't need to be
147   // rebuilt, we should also copy the incremental input information.
148   virtual bool
149   check_inputs(Incremental_inputs* incremental_inputs)
150   { return do_check_inputs(incremental_inputs); }
151
152  protected:
153   // Find incremental inputs section.
154   virtual bool
155   do_find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
156                                       unsigned int* p_symtab_shndx,
157                                       unsigned int* p_relocs_shndx,
158                                       unsigned int* p_got_plt_shndx,
159                                       unsigned int* p_strtab_shndx) = 0;
160
161   // Check the .gnu_incremental_inputs section to see whether an incremental
162   // build is possible.
163   virtual bool
164   do_check_inputs(Incremental_inputs* incremental_inputs) = 0;
165
166  private:
167   // Edited output file object.
168   Output_file* output_;
169   // Target of the output file.
170   Target* target_;
171 };
172
173 template<int size, bool big_endian>
174 class Sized_incremental_binary : public Incremental_binary
175 {
176  public:
177   Sized_incremental_binary(Output_file* output,
178                            const elfcpp::Ehdr<size, big_endian>& ehdr,
179                            Target* target)
180     : Incremental_binary(output, target), elf_file_(this, ehdr)
181   { }
182
183  protected:
184   virtual bool
185   do_find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
186                                       unsigned int* p_symtab_shndx,
187                                       unsigned int* p_relocs_shndx,
188                                       unsigned int* p_got_plt_shndx,
189                                       unsigned int* p_strtab_shndx);
190
191   virtual bool
192   do_check_inputs(Incremental_inputs* incremental_inputs);
193
194  private:
195   // Output as an ELF file.
196   elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
197 };
198
199 // Create an Incremental_binary object for FILE. Returns NULL is this is not
200 // possible, e.g. FILE is not an ELF file or has an unsupported target.
201
202 Incremental_binary*
203 open_incremental_binary(Output_file* file);
204
205 // Code invoked early during an incremental link that checks what files need
206 // to be relinked.
207
208 class Incremental_checker
209 {
210  public:
211   // Check if the file named OUTPUT_NAME can be linked incrementally.
212   // INCREMENTAL_INPUTS must have the canonical form of the command line
213   // and input arguments filled - at this point of linking other fields are
214   // probably not filled yet.  TODO: for inputs that don't need to be
215   // rebuilt, this function should fill the incremental input information.
216   Incremental_checker(const char* output_name,
217                       Incremental_inputs* incremental_inputs)
218     : output_name_(output_name), incremental_inputs_(incremental_inputs)
219   { }
220
221   // Analyzes the output file to check if incremental linking is possible and
222   // what files needs to be relinked.
223   bool
224   can_incrementally_link_output_file();
225
226  private:
227   // Name of the output file to analyze.
228   const char* output_name_;
229
230   // The Incremental_inputs object. At this stage of link, only the command
231   // line and inputs are filled.
232   Incremental_inputs* incremental_inputs_;
233 };
234
235 // Base class for recording each input file.
236
237 class Incremental_input_entry
238 {
239  public:
240   Incremental_input_entry(Stringpool::Key filename_key, Timespec mtime)
241     : filename_key_(filename_key), offset_(0), info_offset_(0), mtime_(mtime)
242   { }
243
244   virtual
245   ~Incremental_input_entry()
246   { }
247
248   // Return the type of input file.
249   Incremental_input_type
250   type() const
251   { return this->do_type(); }
252
253   // Set the section offset of this input file entry.
254   void
255   set_offset(unsigned int offset)
256   { this->offset_ = offset; }
257
258   // Set the section offset of the supplemental information for this entry.
259   void
260   set_info_offset(unsigned int info_offset)
261   { this->info_offset_ = info_offset; }
262
263   // Get the section offset of this input file entry.
264   unsigned int
265   get_offset() const
266   { return this->offset_; }
267
268   // Get the section offset of the supplemental information for this entry.
269   unsigned int
270   get_info_offset() const
271   { return this->info_offset_; }
272
273   // Get the stringpool key for the input filename.
274   Stringpool::Key
275   get_filename_key() const
276   { return this->filename_key_; }
277
278   // Get the modification time of the input file.
279   const Timespec&
280   get_mtime() const
281   { return this->mtime_; }
282
283   // Return a pointer to the derived Incremental_script_entry object.
284   // Return NULL for input entries that are not script files.
285   Incremental_script_entry*
286   script_entry()
287   { return this->do_script_entry(); }
288
289   // Return a pointer to the derived Incremental_object_entry object.
290   // Return NULL for input entries that are not object files.
291   Incremental_object_entry*
292   object_entry()
293   { return this->do_object_entry(); }
294
295   // Return a pointer to the derived Incremental_archive_entry object.
296   // Return NULL for input entries that are not archive files.
297   Incremental_archive_entry*
298   archive_entry()
299   { return this->do_archive_entry(); }
300
301  protected:
302   // Return the type of input file.
303   virtual Incremental_input_type
304   do_type() const = 0;
305
306   // Return a pointer to the derived Incremental_script_entry object.
307   // Return NULL for input entries that are not script files.
308   virtual Incremental_script_entry*
309   do_script_entry()
310   { return NULL; }
311
312   // Return a pointer to the derived Incremental_object_entry object.
313   // Return NULL for input entries that are not object files.
314   virtual Incremental_object_entry*
315   do_object_entry()
316   { return NULL; }
317
318   // Return a pointer to the derived Incremental_archive_entry object.
319   // Return NULL for input entries that are not archive files.
320   virtual Incremental_archive_entry*
321   do_archive_entry()
322   { return NULL; }
323
324  private:
325   // Key of the filename string in the section stringtable.
326   Stringpool::Key filename_key_;
327
328   // Offset of the entry in the output section.
329   unsigned int offset_;
330
331   // Offset of the extra information in the output section.
332   unsigned int info_offset_;
333
334   // Last modification time of the file.
335   Timespec mtime_;
336 };
337
338 // Class for recording input scripts.
339
340 class Incremental_script_entry : public Incremental_input_entry
341 {
342  public:
343   Incremental_script_entry(Stringpool::Key filename_key, Script_info* script,
344                            Timespec mtime)
345     : Incremental_input_entry(filename_key, mtime), script_(script), objects_()
346   { }
347
348   // Add a member object to the archive.
349   void
350   add_object(Incremental_input_entry* obj_entry)
351   {
352     this->objects_.push_back(obj_entry);
353   }
354
355   // Return the number of objects included by this script.
356   unsigned int
357   get_object_count()
358   { return this->objects_.size(); }
359
360   // Return the Nth object.
361   Incremental_input_entry*
362   get_object(unsigned int n)
363   {
364     gold_assert(n < this->objects_.size());
365     return this->objects_[n];
366   }
367
368  protected:
369   virtual Incremental_input_type
370   do_type() const
371   { return INCREMENTAL_INPUT_SCRIPT; }
372
373   // Return a pointer to the derived Incremental_script_entry object.
374   virtual Incremental_script_entry*
375   do_script_entry()
376   { return this; }
377
378  private:
379   // Information about the script file.
380   Script_info* script_;
381   // Objects that have been included by this script.
382   std::vector<Incremental_input_entry*> objects_;
383 };
384
385 // Class for recording input object files.
386
387 class Incremental_object_entry : public Incremental_input_entry
388 {
389  public:
390   Incremental_object_entry(Stringpool::Key filename_key, Object* obj,
391                            Timespec mtime)
392     : Incremental_input_entry(filename_key, mtime), obj_(obj),
393       is_member_(false), sections_()
394   {
395     if (!obj_->is_dynamic())
396       this->sections_.reserve(obj->shnum());
397   }
398
399   // Get the object.
400   Object*
401   object() const
402   { return this->obj_; }
403
404   // Record that this object is an archive member.
405   void
406   set_is_member()
407   { this->is_member_ = true; }
408
409   // Return true if this object is an archive member.
410   bool
411   is_member() const
412   { return this->is_member_; }
413
414   // Add an input section.
415   void
416   add_input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
417   {
418     if (shndx >= this->sections_.size())
419       this->sections_.resize(shndx + 1);
420     this->sections_[shndx].name_key = name_key;
421     this->sections_[shndx].sh_size = sh_size;
422   }
423
424   // Return the number of input sections in this object.
425   unsigned int
426   get_input_section_count() const
427   { return this->sections_.size(); }
428
429   // Return the stringpool key of the Nth input section.
430   Stringpool::Key
431   get_input_section_name_key(unsigned int n) const
432   { return this->sections_[n].name_key; }
433
434   // Return the size of the Nth input section.
435   off_t
436   get_input_section_size(unsigned int n) const
437   { return this->sections_[n].sh_size; }
438
439  protected:
440   virtual Incremental_input_type
441   do_type() const
442   {
443     return (this->is_member_
444             ? INCREMENTAL_INPUT_ARCHIVE_MEMBER
445             : (this->obj_->is_dynamic()
446                ? INCREMENTAL_INPUT_SHARED_LIBRARY
447                : INCREMENTAL_INPUT_OBJECT));
448   }
449
450   // Return a pointer to the derived Incremental_object_entry object.
451   virtual Incremental_object_entry*
452   do_object_entry()
453   { return this; }
454
455  private:
456   // The object file itself.
457   Object* obj_;
458
459   // Whether this object is an archive member.
460   bool is_member_;
461
462   // Input sections.
463   struct Input_section
464   {
465     Stringpool::Key name_key;
466     off_t sh_size;
467   };
468   std::vector<Input_section> sections_;
469 };
470
471 // Class for recording archive library input files.
472
473 class Incremental_archive_entry : public Incremental_input_entry
474 {
475  public:
476   Incremental_archive_entry(Stringpool::Key filename_key, Timespec mtime)
477     : Incremental_input_entry(filename_key, mtime), members_(), unused_syms_()
478   { }
479
480   // Add a member object to the archive.
481   void
482   add_object(Incremental_object_entry* obj_entry)
483   {
484     this->members_.push_back(obj_entry);
485     obj_entry->set_is_member();
486   }
487
488   // Add an unused global symbol to the archive.
489   void
490   add_unused_global_symbol(Stringpool::Key symbol_key)
491   { this->unused_syms_.push_back(symbol_key); }
492
493   // Return the number of member objects included in the link.
494   unsigned int
495   get_member_count()
496   { return this->members_.size(); }
497
498   // Return the Nth member object.
499   Incremental_object_entry*
500   get_member(unsigned int n)
501   { return this->members_[n]; }
502
503   // Return the number of unused global symbols in this archive.
504   unsigned int
505   get_unused_global_symbol_count()
506   { return this->unused_syms_.size(); }
507
508   // Return the Nth unused global symbol.
509   Stringpool::Key
510   get_unused_global_symbol(unsigned int n)
511   { return this->unused_syms_[n]; }
512
513  protected:
514   virtual Incremental_input_type
515   do_type() const
516   { return INCREMENTAL_INPUT_ARCHIVE; }
517
518   // Return a pointer to the derived Incremental_archive_entry object.
519   virtual Incremental_archive_entry*
520   do_archive_entry()
521   { return this; }
522
523  private:
524   // Members of the archive that have been included in the link.
525   std::vector<Incremental_object_entry*> members_;
526
527   // Unused global symbols from this archive.
528   std::vector<Stringpool::Key> unused_syms_;
529 };
530
531 // This class contains the information needed during an incremental
532 // build about the inputs necessary to build the .gnu_incremental_inputs.
533
534 class Incremental_inputs
535 {
536  public:
537   typedef std::vector<Incremental_input_entry*> Input_list;
538
539   Incremental_inputs()
540     : inputs_(), command_line_(), command_line_key_(0),
541       strtab_(new Stringpool()), current_object_(NULL),
542       current_object_entry_(NULL), inputs_section_(NULL),
543       symtab_section_(NULL), relocs_section_(NULL),
544       reloc_count_(0)
545   { }
546
547   ~Incremental_inputs() { delete this->strtab_; }
548
549   // Record the command line.
550   void
551   report_command_line(int argc, const char* const* argv);
552
553   // Record the initial info for archive file ARCHIVE.
554   void
555   report_archive_begin(Library_base* arch, Script_info* script_info);
556
557   // Record the final info for archive file ARCHIVE.
558   void
559   report_archive_end(Library_base* arch);
560
561   // Record the info for object file OBJ.  If ARCH is not NULL,
562   // attach the object file to the archive.
563   void
564   report_object(Object* obj, Library_base* arch, Script_info* script_info);
565
566   // Record an input section belonging to object file OBJ.
567   void
568   report_input_section(Object* obj, unsigned int shndx, const char* name,
569                        off_t sh_size);
570
571   // Record the info for input script SCRIPT.
572   void
573   report_script(const std::string& filename, Script_info* script,
574                 Timespec mtime);
575
576   // Return the running count of incremental relocations.
577   unsigned int
578   get_reloc_count() const
579   { return this->reloc_count_; }
580
581   // Update the running count of incremental relocations.
582   void
583   set_reloc_count(unsigned int count)
584   { this->reloc_count_ = count; }
585
586   // Prepare for layout.  Called from Layout::finalize.
587   void
588   finalize();
589
590   // Create the .gnu_incremental_inputs and related sections.
591   void
592   create_data_sections(Symbol_table* symtab);
593
594   // Return the .gnu_incremental_inputs section.
595   Output_section_data*
596   inputs_section() const
597   { return this->inputs_section_; }
598
599   // Return the .gnu_incremental_symtab section.
600   Output_data_space*
601   symtab_section() const
602   { return this->symtab_section_; }
603
604   // Return the .gnu_incremental_relocs section.
605   Output_data_space*
606   relocs_section() const
607   { return this->relocs_section_; }
608
609   // Return the .gnu_incremental_got_plt section.
610   Output_data_space*
611   got_plt_section() const
612   { return this->got_plt_section_; }
613
614   // Return the .gnu_incremental_strtab stringpool.
615   Stringpool*
616   get_stringpool() const
617   { return this->strtab_; }
618
619   // Return the canonical form of the command line, as will be stored in
620   // .gnu_incremental_strtab.
621   const std::string&
622   command_line() const
623   { return this->command_line_; }
624
625   // Return the stringpool key of the command line.
626   Stringpool::Key
627   command_line_key() const
628   { return this->command_line_key_; }
629
630   // Return the number of input files.
631   int
632   input_file_count() const
633   { return this->inputs_.size(); }
634
635   // Return the input files.
636   const Input_list&
637   input_files() const
638   { return this->inputs_; }
639
640   // Return the sh_entsize value for the .gnu_incremental_relocs section.
641   unsigned int
642   relocs_entsize() const;
643
644  private:
645   // The list of input files.
646   Input_list inputs_;
647
648   // Canonical form of the command line, as will be stored in
649   // .gnu_incremental_strtab.
650   std::string command_line_;
651
652   // The key of the command line string in the string pool.
653   Stringpool::Key command_line_key_;
654
655   // The .gnu_incremental_strtab string pool associated with the
656   // .gnu_incremental_inputs.
657   Stringpool* strtab_;
658
659   // Keep track of the object currently being processed.
660   Object* current_object_;
661   Incremental_object_entry* current_object_entry_;
662
663   // The .gnu_incremental_inputs section.
664   Output_section_data* inputs_section_;
665
666   // The .gnu_incremental_symtab section.
667   Output_data_space* symtab_section_;
668
669   // The .gnu_incremental_relocs section.
670   Output_data_space* relocs_section_;
671
672   // The .gnu_incremental_got_plt section.
673   Output_data_space* got_plt_section_;
674
675   // Total count of incremental relocations.  Updated during Scan_relocs
676   // phase at the completion of each object file.
677   unsigned int reloc_count_;
678 };
679
680 // Reader class for .gnu_incremental_inputs section.
681
682 template<int size, bool big_endian>
683 class Incremental_inputs_reader
684 {
685  private:
686   typedef elfcpp::Swap<size, big_endian> Swap;
687   typedef elfcpp::Swap<16, big_endian> Swap16;
688   typedef elfcpp::Swap<32, big_endian> Swap32;
689   typedef elfcpp::Swap<64, big_endian> Swap64;
690
691  public:
692   Incremental_inputs_reader(const unsigned char* p, elfcpp::Elf_strtab& strtab)
693     : p_(p), strtab_(strtab)
694   { this->input_file_count_ = Swap32::readval(this->p_ + 4); }
695
696   // Return the version number.
697   unsigned int
698   version() const
699   { return Swap32::readval(this->p_); }
700
701   // Return the count of input file entries.
702   unsigned int
703   input_file_count() const
704   { return this->input_file_count_; }
705
706   // Return the command line.
707   const char*
708   command_line() const
709   {
710     unsigned int offset = Swap32::readval(this->p_ + 8);
711     return this->get_string(offset);
712   }
713
714   // Reader class for an input file entry and its supplemental info.
715   class Incremental_input_entry_reader
716   {
717    public:
718     Incremental_input_entry_reader(const Incremental_inputs_reader* inputs,
719                                    unsigned int offset)
720       : inputs_(inputs), offset_(offset)
721     {
722       this->info_offset_ = Swap32::readval(inputs->p_ + offset + 4);
723       int type = Swap16::readval(this->inputs_->p_ + offset + 20);
724       this->type_ = static_cast<Incremental_input_type>(type);
725     }
726
727     // Return the filename.
728     const char*
729     filename() const
730     {
731       unsigned int offset = Swap32::readval(this->inputs_->p_ + this->offset_);
732       return this->inputs_->get_string(offset);
733     }
734
735     // Return the timestamp.
736     Timespec
737     get_mtime() const
738     {
739       Timespec t;
740       const unsigned char* p = this->inputs_->p_ + this->offset_ + 8;
741       t.seconds = Swap64::readval(p);
742       t.nanoseconds = Swap32::readval(p+8);
743       return t;
744     }
745
746     // Return the type of input file.
747     Incremental_input_type
748     type() const
749     { return this->type_; }
750
751     // Return the input section count -- for objects only.
752     unsigned int
753     get_input_section_count() const
754     {
755       gold_assert(this->type_ == INCREMENTAL_INPUT_OBJECT
756                   || this->type_ == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
757       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
758     }
759
760     // Return the offset of the supplemental info for symbol SYMNDX --
761     // for objects only.
762     unsigned int
763     get_symbol_offset(unsigned int symndx) const
764     {
765       gold_assert(this->type_ == INCREMENTAL_INPUT_OBJECT
766                   || this->type_ == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
767
768       unsigned int section_count = this->get_input_section_count();
769       return (this->info_offset_ + 8
770               + section_count * input_section_entry_size
771               + symndx * 16);
772     }
773
774     // Return the global symbol count -- for objects & shared libraries only.
775     unsigned int
776     get_global_symbol_count() const
777     {
778       switch (this->type_)
779         {
780         case INCREMENTAL_INPUT_OBJECT:
781         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
782           return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
783         case INCREMENTAL_INPUT_SHARED_LIBRARY:
784           return Swap32::readval(this->inputs_->p_ + this->info_offset_);
785         default:
786           gold_unreachable();
787         }
788     }
789
790     // Return the object count -- for scripts only.
791     unsigned int
792     get_object_count() const
793     {
794       gold_assert(this->type_ == INCREMENTAL_INPUT_SCRIPT);
795       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
796     }
797
798     // Return the input file offset for object N -- for scripts only.
799     unsigned int
800     get_object_offset(unsigned int n) const
801     {
802       gold_assert(this->type_ == INCREMENTAL_INPUT_SCRIPT);
803       return Swap32::readval(this->inputs_->p_ + this->info_offset_
804                              + 4 + n * 4);
805     }
806
807     // Return the member count -- for archives only.
808     unsigned int
809     get_member_count() const
810     {
811       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
812       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
813     }
814
815     // Return the unused symbol count -- for archives only.
816     unsigned int
817     get_unused_symbol_count() const
818     {
819       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
820       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
821     }
822
823     // Return the input file offset for archive member N -- for archives only.
824     unsigned int
825     get_member_offset(unsigned int n) const
826     {
827       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
828       return Swap32::readval(this->inputs_->p_ + this->info_offset_
829                              + 8 + n * 4);
830     }
831
832     // Return the Nth unused global symbol -- for archives only.
833     const char*
834     get_unused_symbol(unsigned int n) const
835     {
836       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
837       unsigned int member_count = this->get_member_count();
838       unsigned int offset = Swap32::readval(this->inputs_->p_
839                                             + this->info_offset_ + 8
840                                             + member_count * 4
841                                             + n * 4);
842       return this->inputs_->get_string(offset);
843     }
844
845     // Information about an input section.
846     struct Input_section_info
847     {
848       const char* name;
849       unsigned int output_shndx;
850       off_t sh_offset;
851       off_t sh_size;
852     };
853
854     // Return info about the Nth input section -- for objects only.
855     Input_section_info
856     get_input_section(unsigned int n) const
857     {
858       Input_section_info info;
859       const unsigned char* p = (this->inputs_->p_
860                                 + this->info_offset_ + 8
861                                 + n * input_section_entry_size);
862       unsigned int name_offset = Swap32::readval(p);
863       info.name = this->inputs_->get_string(name_offset);
864       info.output_shndx = Swap32::readval(p + 4);
865       info.sh_offset = Swap::readval(p + 8);
866       info.sh_size = Swap::readval(p + 8 + size / 8);
867       return info;
868     }
869
870     // Information about a global symbol.
871     struct Global_symbol_info
872     {
873       unsigned int output_symndx;
874       unsigned int next_offset;
875       unsigned int reloc_count;
876       unsigned int reloc_offset;
877     };
878
879     // Return info about the Nth global symbol -- for objects only.
880     Global_symbol_info
881     get_global_symbol_info(unsigned int n) const
882     {
883       Global_symbol_info info;
884       unsigned int section_count = this->get_input_section_count();
885       const unsigned char* p = (this->inputs_->p_
886                                 + this->info_offset_ + 8
887                                 + section_count * input_section_entry_size
888                                 + n * 16);
889       info.output_symndx = Swap32::readval(p);
890       info.next_offset = Swap32::readval(p + 4);
891       info.reloc_count = Swap::readval(p + 8);
892       info.reloc_offset = Swap::readval(p + 12);
893       return info;
894     }
895
896    private:
897     // Size of an input section entry.
898     static const unsigned int input_section_entry_size = 8 + 2 * size / 8;
899     // The reader instance for the containing section.
900     const Incremental_inputs_reader* inputs_;
901     // The type of input file.
902     Incremental_input_type type_;
903     // Section offset to the input file entry.
904     unsigned int offset_;
905     // Section offset to the supplemental info for the input file.
906     unsigned int info_offset_;
907   };
908
909   // Return a reader for the Nth input file entry.
910   Incremental_input_entry_reader
911   input_file(unsigned int n) const
912   {
913     gold_assert(n < this->input_file_count_);
914     Incremental_input_entry_reader input(this, 16 + n * 24);
915     return input;
916   }
917
918  private:
919   // Lookup a string in the ELF string table.
920   const char* get_string(unsigned int offset) const
921   {
922     const char* s;
923     if (this->strtab_.get_c_string(offset, &s))
924       return s;
925     return NULL;
926   }
927
928   // Base address of the .gnu_incremental_inputs section.
929   const unsigned char* p_;
930   // The associated ELF string table.
931   elfcpp::Elf_strtab strtab_;
932   // The number of input file entries in this section.
933   unsigned int input_file_count_;
934 };
935
936 // Reader class for the .gnu_incremental_symtab section.
937
938 template<bool big_endian>
939 class Incremental_symtab_reader
940 {
941  public:
942   Incremental_symtab_reader(const unsigned char* p) : p_(p)
943   { }
944
945   // Return the list head for symbol table entry N.
946   unsigned int get_list_head(unsigned int n) const
947   { return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4 * n); }
948
949  private:
950   // Base address of the .gnu_incremental_relocs section.
951   const unsigned char* p_;
952 };
953
954 // Reader class for the .gnu_incremental_relocs section.
955
956 template<int size, bool big_endian>
957 class Incremental_relocs_reader
958 {
959  private:
960   // Size of each field.
961   static const unsigned int field_size = size / 8;
962
963  public:
964   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
965   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
966
967   // Size of each entry.
968   static const unsigned int reloc_size = 8 + 2 * field_size;
969
970   Incremental_relocs_reader(const unsigned char* p) : p_(p)
971   { }
972
973   // Return the relocation type for relocation entry at offset OFF.
974   unsigned int
975   get_r_type(unsigned int off) const
976   {
977     return elfcpp::Swap<32, big_endian>::readval(this->p_ + off);
978   }
979
980   // Return the output section index for relocation entry at offset OFF.
981   unsigned int
982   get_r_shndx(unsigned int off) const
983   {
984     return elfcpp::Swap<32, big_endian>::readval(this->p_ + off + 4);
985   }
986
987   // Return the output section offset for relocation entry at offset OFF.
988   Address
989   get_r_offset(unsigned int off) const
990   {
991     return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8);
992   }
993
994   // Return the addend for relocation entry at offset OFF.
995   Addend
996   get_r_addend(unsigned int off) const
997   {
998     return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8
999                                                    + this->field_size);
1000   }
1001
1002  private:
1003   // Base address of the .gnu_incremental_relocs section.
1004   const unsigned char* p_;
1005 };
1006
1007 // Reader class for the .gnu_incremental_got_plt section.
1008
1009 template<bool big_endian>
1010 class Incremental_got_plt_reader
1011 {
1012  public:
1013   Incremental_got_plt_reader(const unsigned char* p) : p_(p)
1014   {
1015     this->got_count_ = elfcpp::Swap<32, big_endian>::readval(p);
1016     this->got_desc_p_ = p + 8 + ((this->got_count_ + 3) & ~3);
1017     this->plt_desc_p_ = this->got_desc_p_ + this->got_count_ * 4;
1018   }
1019
1020   // Return the GOT entry count.
1021   unsigned int
1022   get_got_entry_count() const
1023   {
1024     return this->got_count_;
1025   }
1026
1027   // Return the PLT entry count.
1028   unsigned int
1029   get_plt_entry_count() const
1030   {
1031     return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4);
1032   }
1033
1034   // Return the GOT type for GOT entry N.
1035   unsigned int
1036   get_got_type(unsigned int n)
1037   {
1038     return this->p_[8 + n];
1039   }
1040
1041   // Return the GOT descriptor for GOT entry N.
1042   unsigned int
1043   get_got_desc(unsigned int n)
1044   {
1045     return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 4);
1046   }
1047
1048   // Return the PLT descriptor for PLT entry N.
1049   unsigned int
1050   get_plt_desc(unsigned int n)
1051   {
1052     return elfcpp::Swap<32, big_endian>::readval(this->plt_desc_p_ + n * 4);
1053   }
1054
1055  private:
1056   // Base address of the .gnu_incremental_got_plt section.
1057   const unsigned char* p_;
1058   // GOT entry count.
1059   unsigned int got_count_;
1060   // Base address of the GOT descriptor array.
1061   const unsigned char* got_desc_p_;
1062   // Base address of the PLT descriptor array.
1063   const unsigned char* plt_desc_p_;
1064 };
1065
1066 } // End namespace gold.
1067
1068 #endif // !defined(GOLD_INCREMENTAL_H)