Upload Tizen:Base source
[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 #include "archive.h"
35
36 namespace gold
37 {
38
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 Incremental_binary;
46 class Incremental_library;
47 class Object;
48 class Script_info;
49
50 // Incremental input type as stored in .gnu_incremental_inputs.
51
52 enum Incremental_input_type
53 {
54   INCREMENTAL_INPUT_OBJECT = 1,
55   INCREMENTAL_INPUT_ARCHIVE_MEMBER = 2,
56   INCREMENTAL_INPUT_ARCHIVE = 3,
57   INCREMENTAL_INPUT_SHARED_LIBRARY = 4,
58   INCREMENTAL_INPUT_SCRIPT = 5
59 };
60
61 // Incremental input file flags.
62 // The input file type is stored in the lower eight bits.
63
64 enum Incremental_input_flags
65 {
66   INCREMENTAL_INPUT_IN_SYSTEM_DIR = 0x0800
67 };
68
69 // Create an Incremental_binary object for FILE. Returns NULL is this is not
70 // possible, e.g. FILE is not an ELF file or has an unsupported target.
71
72 Incremental_binary*
73 open_incremental_binary(Output_file* file);
74
75 // Base class for recording each input file.
76
77 class Incremental_input_entry
78 {
79  public:
80   Incremental_input_entry(Stringpool::Key filename_key, unsigned int arg_serial,
81                           Timespec mtime)
82     : filename_key_(filename_key), offset_(0), info_offset_(0),
83       arg_serial_(arg_serial), mtime_(mtime), is_in_system_directory_(false)
84   { }
85
86   virtual
87   ~Incremental_input_entry()
88   { }
89
90   // Return the type of input file.
91   Incremental_input_type
92   type() const
93   { return this->do_type(); }
94
95   // Set the section offset of this input file entry.
96   void
97   set_offset(unsigned int offset)
98   { this->offset_ = offset; }
99
100   // Set the section offset of the supplemental information for this entry.
101   void
102   set_info_offset(unsigned int info_offset)
103   { this->info_offset_ = info_offset; }
104
105   // Get the section offset of this input file entry.
106   unsigned int
107   get_offset() const
108   { return this->offset_; }
109
110   // Get the section offset of the supplemental information for this entry.
111   unsigned int
112   get_info_offset() const
113   { return this->info_offset_; }
114
115   // Get the stringpool key for the input filename.
116   Stringpool::Key
117   get_filename_key() const
118   { return this->filename_key_; }
119
120   // Get the serial number of the input file.
121   unsigned int
122   arg_serial() const
123   { return this->arg_serial_; }
124
125   // Get the modification time of the input file.
126   const Timespec&
127   get_mtime() const
128   { return this->mtime_; }
129
130   // Record that the file was found in a system directory.
131   void
132   set_is_in_system_directory()
133   { this->is_in_system_directory_ = true; }
134
135   // Return TRUE if the file was found in a system directory.
136   bool
137   is_in_system_directory() const
138   { return this->is_in_system_directory_; }
139
140   // Return a pointer to the derived Incremental_script_entry object.
141   // Return NULL for input entries that are not script files.
142   Incremental_script_entry*
143   script_entry()
144   { return this->do_script_entry(); }
145
146   // Return a pointer to the derived Incremental_object_entry object.
147   // Return NULL for input entries that are not object files.
148   Incremental_object_entry*
149   object_entry()
150   { return this->do_object_entry(); }
151
152   // Return a pointer to the derived Incremental_archive_entry object.
153   // Return NULL for input entries that are not archive files.
154   Incremental_archive_entry*
155   archive_entry()
156   { return this->do_archive_entry(); }
157
158  protected:
159   // Return the type of input file.
160   virtual Incremental_input_type
161   do_type() const = 0;
162
163   // Return a pointer to the derived Incremental_script_entry object.
164   // Return NULL for input entries that are not script files.
165   virtual Incremental_script_entry*
166   do_script_entry()
167   { return NULL; }
168
169   // Return a pointer to the derived Incremental_object_entry object.
170   // Return NULL for input entries that are not object files.
171   virtual Incremental_object_entry*
172   do_object_entry()
173   { return NULL; }
174
175   // Return a pointer to the derived Incremental_archive_entry object.
176   // Return NULL for input entries that are not archive files.
177   virtual Incremental_archive_entry*
178   do_archive_entry()
179   { return NULL; }
180
181  private:
182   // Key of the filename string in the section stringtable.
183   Stringpool::Key filename_key_;
184
185   // Offset of the entry in the output section.
186   unsigned int offset_;
187
188   // Offset of the extra information in the output section.
189   unsigned int info_offset_;
190
191   // Serial number of the file in the argument list.
192   unsigned int arg_serial_;
193
194   // Last modification time of the file.
195   Timespec mtime_;
196
197   // TRUE if the file was found in a system directory.
198   bool is_in_system_directory_;
199 };
200
201 // Information about a script input that will persist during the whole linker
202 // run.  Needed only during an incremental build to retrieve the input files
203 // added by this script.
204
205 class Script_info
206 {
207  public:
208   Script_info(const std::string& filename)
209     : filename_(filename), incremental_script_entry_(NULL)
210   { }
211
212   // Store a pointer to the incremental information for this script.
213   void
214   set_incremental_info(Incremental_script_entry* entry)
215   { this->incremental_script_entry_ = entry; }
216
217   // Return the filename.
218   const std::string&
219   filename() const
220   { return this->filename_; }
221
222   // Return the pointer to the incremental information for this script.
223   Incremental_script_entry*
224   incremental_info() const
225   { return this->incremental_script_entry_; }
226
227  private:
228   const std::string filename_;
229   Incremental_script_entry* incremental_script_entry_;
230 };
231
232 // Class for recording input scripts.
233
234 class Incremental_script_entry : public Incremental_input_entry
235 {
236  public:
237   Incremental_script_entry(Stringpool::Key filename_key,
238                            unsigned int arg_serial, Script_info* script,
239                            Timespec mtime)
240     : Incremental_input_entry(filename_key, arg_serial, mtime),
241       script_(script), objects_()
242   { }
243
244   // Add a member object to the archive.
245   void
246   add_object(Incremental_input_entry* obj_entry)
247   {
248     this->objects_.push_back(obj_entry);
249   }
250
251   // Return the number of objects included by this script.
252   unsigned int
253   get_object_count()
254   { return this->objects_.size(); }
255
256   // Return the Nth object.
257   Incremental_input_entry*
258   get_object(unsigned int n)
259   {
260     gold_assert(n < this->objects_.size());
261     return this->objects_[n];
262   }
263
264  protected:
265   virtual Incremental_input_type
266   do_type() const
267   { return INCREMENTAL_INPUT_SCRIPT; }
268
269   // Return a pointer to the derived Incremental_script_entry object.
270   virtual Incremental_script_entry*
271   do_script_entry()
272   { return this; }
273
274  private:
275   // Information about the script file.
276   Script_info* script_;
277   // Objects that have been included by this script.
278   std::vector<Incremental_input_entry*> objects_;
279 };
280
281 // Class for recording input object files.
282
283 class Incremental_object_entry : public Incremental_input_entry
284 {
285  public:
286   Incremental_object_entry(Stringpool::Key filename_key, Object* obj,
287                            unsigned int arg_serial, Timespec mtime)
288     : Incremental_input_entry(filename_key, arg_serial, mtime), obj_(obj),
289       is_member_(false), sections_()
290   {
291     if (!obj_->is_dynamic())
292       this->sections_.reserve(obj->shnum());
293   }
294
295   // Get the object.
296   Object*
297   object() const
298   { return this->obj_; }
299
300   // Record that this object is an archive member.
301   void
302   set_is_member()
303   { this->is_member_ = true; }
304
305   // Return true if this object is an archive member.
306   bool
307   is_member() const
308   { return this->is_member_; }
309
310   // Add an input section.
311   void
312   add_input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
313   { this->sections_.push_back(Input_section(shndx, name_key, sh_size)); }
314
315   // Return the number of input sections in this object.
316   unsigned int
317   get_input_section_count() const
318   { return this->sections_.size(); }
319
320   // Return the input section index for the Nth input section.
321   Stringpool::Key
322   get_input_section_index(unsigned int n) const
323   { return this->sections_[n].shndx_; }
324
325   // Return the stringpool key of the Nth input section.
326   Stringpool::Key
327   get_input_section_name_key(unsigned int n) const
328   { return this->sections_[n].name_key_; }
329
330   // Return the size of the Nth input section.
331   off_t
332   get_input_section_size(unsigned int n) const
333   { return this->sections_[n].sh_size_; }
334
335  protected:
336   virtual Incremental_input_type
337   do_type() const
338   {
339     return (this->is_member_
340             ? INCREMENTAL_INPUT_ARCHIVE_MEMBER
341             : (this->obj_->is_dynamic()
342                ? INCREMENTAL_INPUT_SHARED_LIBRARY
343                : INCREMENTAL_INPUT_OBJECT));
344   }
345
346   // Return a pointer to the derived Incremental_object_entry object.
347   virtual Incremental_object_entry*
348   do_object_entry()
349   { return this; }
350
351  private:
352   // The object file itself.
353   Object* obj_;
354
355   // Whether this object is an archive member.
356   bool is_member_;
357
358   // Input sections.
359   struct Input_section
360   {
361     Input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
362       : shndx_(shndx), name_key_(name_key), sh_size_(sh_size)
363     { }
364     unsigned int shndx_;
365     Stringpool::Key name_key_;
366     off_t sh_size_;
367   };
368   std::vector<Input_section> sections_;
369 };
370
371 // Class for recording archive library input files.
372
373 class Incremental_archive_entry : public Incremental_input_entry
374 {
375  public:
376   Incremental_archive_entry(Stringpool::Key filename_key,
377                             unsigned int arg_serial, Timespec mtime)
378     : Incremental_input_entry(filename_key, arg_serial, mtime), members_(),
379       unused_syms_()
380   { }
381
382   // Add a member object to the archive.
383   void
384   add_object(Incremental_object_entry* obj_entry)
385   {
386     this->members_.push_back(obj_entry);
387     obj_entry->set_is_member();
388   }
389
390   // Add an unused global symbol to the archive.
391   void
392   add_unused_global_symbol(Stringpool::Key symbol_key)
393   { this->unused_syms_.push_back(symbol_key); }
394
395   // Return the number of member objects included in the link.
396   unsigned int
397   get_member_count()
398   { return this->members_.size(); }
399
400   // Return the Nth member object.
401   Incremental_object_entry*
402   get_member(unsigned int n)
403   { return this->members_[n]; }
404
405   // Return the number of unused global symbols in this archive.
406   unsigned int
407   get_unused_global_symbol_count()
408   { return this->unused_syms_.size(); }
409
410   // Return the Nth unused global symbol.
411   Stringpool::Key
412   get_unused_global_symbol(unsigned int n)
413   { return this->unused_syms_[n]; }
414
415  protected:
416   virtual Incremental_input_type
417   do_type() const
418   { return INCREMENTAL_INPUT_ARCHIVE; }
419
420   // Return a pointer to the derived Incremental_archive_entry object.
421   virtual Incremental_archive_entry*
422   do_archive_entry()
423   { return this; }
424
425  private:
426   // Members of the archive that have been included in the link.
427   std::vector<Incremental_object_entry*> members_;
428
429   // Unused global symbols from this archive.
430   std::vector<Stringpool::Key> unused_syms_;
431 };
432
433 // This class contains the information needed during an incremental
434 // build about the inputs necessary to build the .gnu_incremental_inputs.
435
436 class Incremental_inputs
437 {
438  public:
439   typedef std::vector<Incremental_input_entry*> Input_list;
440
441   Incremental_inputs()
442     : inputs_(), command_line_(), command_line_key_(0),
443       strtab_(new Stringpool()), current_object_(NULL),
444       current_object_entry_(NULL), inputs_section_(NULL),
445       symtab_section_(NULL), relocs_section_(NULL),
446       reloc_count_(0)
447   { }
448
449   ~Incremental_inputs() { delete this->strtab_; }
450
451   // Record the command line.
452   void
453   report_command_line(int argc, const char* const* argv);
454
455   // Record the initial info for archive file ARCHIVE.
456   void
457   report_archive_begin(Library_base* arch, unsigned int arg_serial,
458                        Script_info* script_info);
459
460   // Record the final info for archive file ARCHIVE.
461   void
462   report_archive_end(Library_base* arch);
463
464   // Record the info for object file OBJ.  If ARCH is not NULL,
465   // attach the object file to the archive.
466   void
467   report_object(Object* obj, unsigned int arg_serial, Library_base* arch,
468                 Script_info* script_info);
469
470   // Record an input section belonging to object file OBJ.
471   void
472   report_input_section(Object* obj, unsigned int shndx, const char* name,
473                        off_t sh_size);
474
475   // Record the info for input script SCRIPT.
476   void
477   report_script(Script_info* script, unsigned int arg_serial,
478                 Timespec mtime);
479
480   // Return the running count of incremental relocations.
481   unsigned int
482   get_reloc_count() const
483   { return this->reloc_count_; }
484
485   // Update the running count of incremental relocations.
486   void
487   set_reloc_count(unsigned int count)
488   { this->reloc_count_ = count; }
489
490   // Prepare for layout.  Called from Layout::finalize.
491   void
492   finalize();
493
494   // Create the .gnu_incremental_inputs and related sections.
495   void
496   create_data_sections(Symbol_table* symtab);
497
498   // Return the .gnu_incremental_inputs section.
499   Output_section_data*
500   inputs_section() const
501   { return this->inputs_section_; }
502
503   // Return the .gnu_incremental_symtab section.
504   Output_data_space*
505   symtab_section() const
506   { return this->symtab_section_; }
507
508   // Return the .gnu_incremental_relocs section.
509   Output_data_space*
510   relocs_section() const
511   { return this->relocs_section_; }
512
513   // Return the .gnu_incremental_got_plt section.
514   Output_data_space*
515   got_plt_section() const
516   { return this->got_plt_section_; }
517
518   // Return the .gnu_incremental_strtab stringpool.
519   Stringpool*
520   get_stringpool() const
521   { return this->strtab_; }
522
523   // Return the canonical form of the command line, as will be stored in
524   // .gnu_incremental_strtab.
525   const std::string&
526   command_line() const
527   { return this->command_line_; }
528
529   // Return the stringpool key of the command line.
530   Stringpool::Key
531   command_line_key() const
532   { return this->command_line_key_; }
533
534   // Return the number of input files.
535   int
536   input_file_count() const
537   { return this->inputs_.size(); }
538
539   // Return the input files.
540   const Input_list&
541   input_files() const
542   { return this->inputs_; }
543
544   // Return the sh_entsize value for the .gnu_incremental_relocs section.
545   unsigned int
546   relocs_entsize() const;
547
548  private:
549   // The list of input files.
550   Input_list inputs_;
551
552   // Canonical form of the command line, as will be stored in
553   // .gnu_incremental_strtab.
554   std::string command_line_;
555
556   // The key of the command line string in the string pool.
557   Stringpool::Key command_line_key_;
558
559   // The .gnu_incremental_strtab string pool associated with the
560   // .gnu_incremental_inputs.
561   Stringpool* strtab_;
562
563   // Keep track of the object currently being processed.
564   Object* current_object_;
565   Incremental_object_entry* current_object_entry_;
566
567   // The .gnu_incremental_inputs section.
568   Output_section_data* inputs_section_;
569
570   // The .gnu_incremental_symtab section.
571   Output_data_space* symtab_section_;
572
573   // The .gnu_incremental_relocs section.
574   Output_data_space* relocs_section_;
575
576   // The .gnu_incremental_got_plt section.
577   Output_data_space* got_plt_section_;
578
579   // Total count of incremental relocations.  Updated during Scan_relocs
580   // phase at the completion of each object file.
581   unsigned int reloc_count_;
582 };
583
584 // Reader class for global symbol info from an object file entry in
585 // the .gnu_incremental_inputs section.
586
587 template<bool big_endian>
588 class Incremental_global_symbol_reader
589 {
590  private:
591   typedef elfcpp::Swap<32, big_endian> Swap32;
592
593  public:
594   Incremental_global_symbol_reader(const unsigned char* p)
595     : p_(p)
596   { }
597
598   unsigned int
599   output_symndx() const
600   { return Swap32::readval(this->p_); }
601
602   unsigned int
603   shndx() const
604   { return Swap32::readval(this->p_ + 4); }
605
606   unsigned int
607   next_offset() const
608   { return Swap32::readval(this->p_ + 8); }
609
610   unsigned int
611   reloc_count() const
612   { return Swap32::readval(this->p_ + 12); }
613
614   unsigned int
615   reloc_offset() const
616   { return Swap32::readval(this->p_ + 16); }
617
618  private:
619   // Base address of the symbol entry.
620   const unsigned char* p_;
621 };
622
623 // Reader class for .gnu_incremental_inputs section.
624
625 template<int size, bool big_endian>
626 class Incremental_inputs_reader
627 {
628  private:
629   typedef elfcpp::Swap<size, big_endian> Swap;
630   typedef elfcpp::Swap<16, big_endian> Swap16;
631   typedef elfcpp::Swap<32, big_endian> Swap32;
632   typedef elfcpp::Swap<64, big_endian> Swap64;
633
634  public:
635   Incremental_inputs_reader()
636     : p_(NULL), strtab_(NULL, 0), input_file_count_(0)
637   { }
638
639   Incremental_inputs_reader(const unsigned char* p,
640                             const elfcpp::Elf_strtab& strtab)
641     : p_(p), strtab_(strtab)
642   { this->input_file_count_ = Swap32::readval(this->p_ + 4); }
643
644   // Return the version number.
645   unsigned int
646   version() const
647   { return Swap32::readval(this->p_); }
648
649   // Return the count of input file entries.
650   unsigned int
651   input_file_count() const
652   { return this->input_file_count_; }
653
654   // Return the command line.
655   const char*
656   command_line() const
657   {
658     unsigned int offset = Swap32::readval(this->p_ + 8);
659     return this->get_string(offset);
660   }
661
662   // Reader class for an input file entry and its supplemental info.
663   class Incremental_input_entry_reader
664   {
665    public:
666     Incremental_input_entry_reader(const Incremental_inputs_reader* inputs,
667                                    unsigned int offset)
668       : inputs_(inputs), offset_(offset)
669     {
670       this->info_offset_ = Swap32::readval(inputs->p_ + offset + 4);
671       this->flags_ = Swap16::readval(this->inputs_->p_ + offset + 20);
672     }
673
674     // Return the filename.
675     const char*
676     filename() const
677     {
678       unsigned int offset = Swap32::readval(this->inputs_->p_ + this->offset_);
679       return this->inputs_->get_string(offset);
680     }
681
682     // Return the argument serial number.
683     unsigned int
684     arg_serial() const
685     {
686       return Swap16::readval(this->inputs_->p_ + this->offset_ + 22);
687     }
688
689     // Return the timestamp.
690     Timespec
691     get_mtime() const
692     {
693       Timespec t;
694       const unsigned char* p = this->inputs_->p_ + this->offset_ + 8;
695       t.seconds = Swap64::readval(p);
696       t.nanoseconds = Swap32::readval(p+8);
697       return t;
698     }
699
700     // Return the type of input file.
701     Incremental_input_type
702     type() const
703     { return static_cast<Incremental_input_type>(this->flags_ & 0xff); }
704
705     // Return TRUE if the file was found in a system directory.
706     bool
707     is_in_system_directory() const
708     { return (this->flags_ & INCREMENTAL_INPUT_IN_SYSTEM_DIR) != 0; }
709
710     // Return the input section count -- for objects only.
711     unsigned int
712     get_input_section_count() const
713     {
714       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
715                   || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
716       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
717     }
718
719     // Return the offset of the supplemental info for symbol SYMNDX --
720     // for objects only.
721     unsigned int
722     get_symbol_offset(unsigned int symndx) const
723     {
724       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
725                   || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
726
727       unsigned int section_count = this->get_input_section_count();
728       return (this->info_offset_ + 8
729               + section_count * input_section_entry_size
730               + symndx * 20);
731     }
732
733     // Return the global symbol count -- for objects & shared libraries only.
734     unsigned int
735     get_global_symbol_count() const
736     {
737       switch (this->type())
738         {
739         case INCREMENTAL_INPUT_OBJECT:
740         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
741           return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
742         case INCREMENTAL_INPUT_SHARED_LIBRARY:
743           return Swap32::readval(this->inputs_->p_ + this->info_offset_);
744         default:
745           gold_unreachable();
746         }
747     }
748
749     // Return the object count -- for scripts only.
750     unsigned int
751     get_object_count() const
752     {
753       gold_assert(this->type() == INCREMENTAL_INPUT_SCRIPT);
754       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
755     }
756
757     // Return the input file offset for object N -- for scripts only.
758     unsigned int
759     get_object_offset(unsigned int n) const
760     {
761       gold_assert(this->type() == INCREMENTAL_INPUT_SCRIPT);
762       return Swap32::readval(this->inputs_->p_ + this->info_offset_
763                              + 4 + n * 4);
764     }
765
766     // Return the member count -- for archives only.
767     unsigned int
768     get_member_count() const
769     {
770       gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
771       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
772     }
773
774     // Return the unused symbol count -- for archives only.
775     unsigned int
776     get_unused_symbol_count() const
777     {
778       gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
779       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
780     }
781
782     // Return the input file offset for archive member N -- for archives only.
783     unsigned int
784     get_member_offset(unsigned int n) const
785     {
786       gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
787       return Swap32::readval(this->inputs_->p_ + this->info_offset_
788                              + 8 + n * 4);
789     }
790
791     // Return the Nth unused global symbol -- for archives only.
792     const char*
793     get_unused_symbol(unsigned int n) const
794     {
795       gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
796       unsigned int member_count = this->get_member_count();
797       unsigned int offset = Swap32::readval(this->inputs_->p_
798                                             + this->info_offset_ + 8
799                                             + member_count * 4
800                                             + n * 4);
801       return this->inputs_->get_string(offset);
802     }
803
804     // Information about an input section.
805     struct Input_section_info
806     {
807       const char* name;
808       unsigned int output_shndx;
809       off_t sh_offset;
810       off_t sh_size;
811     };
812
813     // Return info about the Nth input section -- for objects only.
814     Input_section_info
815     get_input_section(unsigned int n) const
816     {
817       Input_section_info info;
818       const unsigned char* p = (this->inputs_->p_
819                                 + this->info_offset_ + 8
820                                 + n * input_section_entry_size);
821       unsigned int name_offset = Swap32::readval(p);
822       info.name = this->inputs_->get_string(name_offset);
823       info.output_shndx = Swap32::readval(p + 4);
824       info.sh_offset = Swap::readval(p + 8);
825       info.sh_size = Swap::readval(p + 8 + size / 8);
826       return info;
827     }
828
829     // Return info about the Nth global symbol -- for objects only.
830     Incremental_global_symbol_reader<big_endian>
831     get_global_symbol_reader(unsigned int n) const
832     {
833       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
834                   || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
835       unsigned int section_count = this->get_input_section_count();
836       const unsigned char* p = (this->inputs_->p_
837                                 + this->info_offset_ + 8
838                                 + section_count * input_section_entry_size
839                                 + n * 20);
840       return Incremental_global_symbol_reader<big_endian>(p);
841     }
842
843     // Return the output symbol index for the Nth global symbol -- for shared
844     // libraries only.  Sets *IS_DEF to TRUE if the symbol is defined in this
845     // input file.
846     unsigned int
847     get_output_symbol_index(unsigned int n, bool* is_def)
848     {
849       gold_assert(this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
850       const unsigned char* p = (this->inputs_->p_
851                                 + this->info_offset_ + 4
852                                 + n * 4);
853       unsigned int output_symndx = Swap32::readval(p);
854       *is_def = (output_symndx & (1U << 31)) != 0;
855       return output_symndx & ((1U << 31) - 1);
856     }
857
858    private:
859     // Size of an input section entry.
860     static const unsigned int input_section_entry_size = 8 + 2 * size / 8;
861     // The reader instance for the containing section.
862     const Incremental_inputs_reader* inputs_;
863     // The flags, including the type of input file.
864     unsigned int flags_;
865     // Section offset to the input file entry.
866     unsigned int offset_;
867     // Section offset to the supplemental info for the input file.
868     unsigned int info_offset_;
869   };
870
871   // Return the offset of an input file entry given its index N.
872   unsigned int
873   input_file_offset(unsigned int n) const
874   {
875     gold_assert(n < this->input_file_count_);
876     return 16 + n * 24;
877   }
878
879   // Return the index of an input file entry given its OFFSET.
880   unsigned int
881   input_file_index(unsigned int offset) const
882   {
883     int n = (offset - 16) / 24;
884     gold_assert(input_file_offset(n) == offset);
885     return n;
886   }
887
888   // Return a reader for the Nth input file entry.
889   Incremental_input_entry_reader
890   input_file(unsigned int n) const
891   { return Incremental_input_entry_reader(this, this->input_file_offset(n)); }
892
893   // Return a reader for the input file entry at OFFSET.
894   Incremental_input_entry_reader
895   input_file_at_offset(unsigned int offset) const
896   {
897     gold_assert(offset < 16 + this->input_file_count_ * 24);
898     return Incremental_input_entry_reader(this, offset);
899   }
900
901  private:
902   // Lookup a string in the ELF string table.
903   const char* get_string(unsigned int offset) const
904   {
905     const char* s;
906     if (this->strtab_.get_c_string(offset, &s))
907       return s;
908     return NULL;
909   }
910
911   // Base address of the .gnu_incremental_inputs section.
912   const unsigned char* p_;
913   // The associated ELF string table.
914   elfcpp::Elf_strtab strtab_;
915   // The number of input file entries in this section.
916   unsigned int input_file_count_;
917 };
918
919 // Reader class for the .gnu_incremental_symtab section.
920
921 template<bool big_endian>
922 class Incremental_symtab_reader
923 {
924  public:
925   Incremental_symtab_reader()
926     : p_(NULL), len_(0)
927   { }
928
929   Incremental_symtab_reader(const unsigned char* p, off_t len)
930     : p_(p), len_(len)
931   { }
932
933   // Return the count of symbols in this section.
934   unsigned int
935   symbol_count() const
936   { return static_cast<unsigned int>(this->len_ / 4); }
937
938   // Return the list head for symbol table entry N.
939   unsigned int
940   get_list_head(unsigned int n) const
941   { return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4 * n); }
942
943  private:
944   // Base address of the .gnu_incremental_relocs section.
945   const unsigned char* p_;
946   // Size of the section.
947   off_t len_;
948 };
949
950 // Reader class for the .gnu_incremental_relocs section.
951
952 template<int size, bool big_endian>
953 class Incremental_relocs_reader
954 {
955  private:
956   // Size of each field.
957   static const unsigned int field_size = size / 8;
958
959  public:
960   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
961   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
962
963   // Size of each entry.
964   static const unsigned int reloc_size = 8 + 2 * field_size;
965
966   Incremental_relocs_reader()
967     : p_(NULL), len_(0)
968   { }
969
970   Incremental_relocs_reader(const unsigned char* p, off_t len)
971     : p_(p), len_(len)
972   { }
973
974   // Return the count of relocations in this section.
975   unsigned int
976   reloc_count() const
977   { return static_cast<unsigned int>(this->len_ / reloc_size); }
978
979   // Return the relocation type for relocation entry at offset OFF.
980   unsigned int
981   get_r_type(unsigned int off) const
982   { return elfcpp::Swap<32, big_endian>::readval(this->p_ + off); }
983
984   // Return the output section index for relocation entry at offset OFF.
985   unsigned int
986   get_r_shndx(unsigned int off) const
987   { return elfcpp::Swap<32, big_endian>::readval(this->p_ + off + 4); }
988
989   // Return the output section offset for relocation entry at offset OFF.
990   Address
991   get_r_offset(unsigned int off) const
992   { return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8); }
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   // Return a pointer to the relocation entry at offset OFF.
1003   const unsigned char*
1004   data(unsigned int off) const
1005   { return this->p_ + off; }
1006
1007  private:
1008   // Base address of the .gnu_incremental_relocs section.
1009   const unsigned char* p_;
1010   // Size of the section.
1011   off_t len_;
1012 };
1013
1014 // Reader class for the .gnu_incremental_got_plt section.
1015
1016 template<bool big_endian>
1017 class Incremental_got_plt_reader
1018 {
1019  public:
1020   Incremental_got_plt_reader()
1021     : p_(NULL), got_count_(0), got_desc_p_(NULL), plt_desc_p_(NULL)
1022   { }
1023
1024   Incremental_got_plt_reader(const unsigned char* p) : p_(p)
1025   {
1026     this->got_count_ = elfcpp::Swap<32, big_endian>::readval(p);
1027     this->got_desc_p_ = p + 8 + ((this->got_count_ + 3) & ~3);
1028     this->plt_desc_p_ = this->got_desc_p_ + this->got_count_ * 4;
1029   }
1030
1031   // Return the GOT entry count.
1032   unsigned int
1033   get_got_entry_count() const
1034   {
1035     return this->got_count_;
1036   }
1037
1038   // Return the PLT entry count.
1039   unsigned int
1040   get_plt_entry_count() const
1041   {
1042     return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4);
1043   }
1044
1045   // Return the GOT type for GOT entry N.
1046   unsigned int
1047   get_got_type(unsigned int n)
1048   {
1049     return this->p_[8 + n];
1050   }
1051
1052   // Return the GOT descriptor for GOT entry N.
1053   unsigned int
1054   get_got_desc(unsigned int n)
1055   {
1056     return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 4);
1057   }
1058
1059   // Return the PLT descriptor for PLT entry N.
1060   unsigned int
1061   get_plt_desc(unsigned int n)
1062   {
1063     return elfcpp::Swap<32, big_endian>::readval(this->plt_desc_p_ + n * 4);
1064   }
1065
1066  private:
1067   // Base address of the .gnu_incremental_got_plt section.
1068   const unsigned char* p_;
1069   // GOT entry count.
1070   unsigned int got_count_;
1071   // Base address of the GOT descriptor array.
1072   const unsigned char* got_desc_p_;
1073   // Base address of the PLT descriptor array.
1074   const unsigned char* plt_desc_p_;
1075 };
1076
1077 // An object representing the ELF file we edit during an incremental build.
1078 // Similar to Object or Dynobj, but operates on Output_file and contains
1079 // methods to support incremental updating. This is the abstract parent class
1080 // implemented in Sized_incremental_binary<size, big_endian> for a specific
1081 // endianness and size.
1082
1083 class Incremental_binary
1084 {
1085  public:
1086   Incremental_binary(Output_file* output, Target* target)
1087     : input_args_map_(), library_map_(), script_map_(),
1088       output_(output), target_(target)
1089   { }
1090
1091   virtual
1092   ~Incremental_binary()
1093   { }
1094
1095   // Check the .gnu_incremental_inputs section to see whether an incremental
1096   // build is possible.
1097   bool
1098   check_inputs(const Command_line& cmdline,
1099                Incremental_inputs* incremental_inputs)
1100   { return this->do_check_inputs(cmdline, incremental_inputs); }
1101
1102   // Report an error.
1103   void
1104   error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
1105
1106   // Proxy class for a sized Incremental_input_entry_reader.
1107
1108   class Input_reader
1109   {
1110    public:
1111     Input_reader()
1112     { }
1113
1114     virtual
1115     ~Input_reader()
1116     { }
1117
1118     const char*
1119     filename() const
1120     { return this->do_filename(); }
1121
1122     Timespec
1123     get_mtime() const
1124     { return this->do_get_mtime(); }
1125
1126     Incremental_input_type
1127     type() const
1128     { return this->do_type(); }
1129
1130     unsigned int
1131     arg_serial() const
1132     { return this->do_arg_serial(); }
1133
1134     unsigned int
1135     get_unused_symbol_count() const
1136     { return this->do_get_unused_symbol_count(); }
1137
1138     const char*
1139     get_unused_symbol(unsigned int n) const
1140     { return this->do_get_unused_symbol(n); }
1141
1142    protected:
1143     virtual const char*
1144     do_filename() const = 0;
1145
1146     virtual Timespec
1147     do_get_mtime() const = 0;
1148
1149     virtual Incremental_input_type
1150     do_type() const = 0;
1151
1152     virtual unsigned int
1153     do_arg_serial() const = 0;
1154
1155     virtual unsigned int
1156     do_get_unused_symbol_count() const = 0;
1157
1158     virtual const char*
1159     do_get_unused_symbol(unsigned int n) const = 0;
1160   };
1161
1162   // Return the number of input files.
1163   unsigned int
1164   input_file_count() const
1165   { return this->do_input_file_count(); }
1166
1167   // Return an Input_reader for input file N.
1168   const Input_reader*
1169   get_input_reader(unsigned int n) const
1170   { return this->do_get_input_reader(n); }
1171
1172   // Return TRUE if the input file N has changed since the last link.
1173   bool
1174   file_has_changed(unsigned int n) const
1175   { return this->do_file_has_changed(n); }
1176
1177   // Return the Input_argument for input file N.  Returns NULL if
1178   // the Input_argument is not available.
1179   const Input_argument*
1180   get_input_argument(unsigned int n) const
1181   {
1182     const Input_reader* input_file = this->do_get_input_reader(n);
1183     unsigned int arg_serial = input_file->arg_serial();
1184     if (arg_serial == 0 || arg_serial > this->input_args_map_.size())
1185       return NULL;
1186     return this->input_args_map_[arg_serial - 1];
1187   }
1188
1189   // Return an Incremental_library for the given input file.
1190   Incremental_library*
1191   get_library(unsigned int n)
1192   { return this->library_map_[n]; }
1193
1194   // Return a Script_info for the given input file.
1195   Script_info*
1196   get_script_info(unsigned int n)
1197   { return this->script_map_[n]; }
1198
1199   // Initialize the layout of the output file based on the existing
1200   // output file.
1201   void
1202   init_layout(Layout* layout)
1203   { this->do_init_layout(layout); }
1204
1205   // Mark regions of the input file that must be kept unchanged.
1206   void
1207   reserve_layout(unsigned int input_file_index)
1208   { this->do_reserve_layout(input_file_index); }
1209
1210   // Functions and types for the elfcpp::Elf_file interface.  This
1211   // permit us to use Incremental_binary as the File template parameter for
1212   // elfcpp::Elf_file.
1213
1214   // The View class is returned by view.  It must support a single
1215   // method, data().  This is trivial, because Output_file::get_output_view
1216   // does what we need.
1217   class View
1218   {
1219    public:
1220     View(const unsigned char* p)
1221       : p_(p)
1222     { }
1223
1224     const unsigned char*
1225     data() const
1226     { return this->p_; }
1227
1228    private:
1229     const unsigned char* p_;
1230   };
1231
1232   // Return a View.
1233   View
1234   view(off_t file_offset, section_size_type data_size)
1235   { return View(this->output_->get_input_view(file_offset, data_size)); }
1236
1237   // A location in the file.
1238   struct Location
1239   {
1240     off_t file_offset;
1241     off_t data_size;
1242
1243     Location(off_t fo, section_size_type ds)
1244       : file_offset(fo), data_size(ds)
1245     { }
1246
1247     Location()
1248       : file_offset(0), data_size(0)
1249     { }
1250   };
1251
1252   // Get a View given a Location.
1253   View
1254   view(Location loc)
1255   { return View(this->view(loc.file_offset, loc.data_size)); }
1256
1257   // Return the Output_file.
1258   Output_file*
1259   output_file()
1260   { return this->output_; }
1261
1262  protected:
1263   // Check the .gnu_incremental_inputs section to see whether an incremental
1264   // build is possible.
1265   virtual bool
1266   do_check_inputs(const Command_line& cmdline,
1267                   Incremental_inputs* incremental_inputs) = 0;
1268
1269   // Return TRUE if input file N has changed since the last incremental link.
1270   virtual bool
1271   do_file_has_changed(unsigned int n) const = 0;
1272
1273   // Initialize the layout of the output file based on the existing
1274   // output file.
1275   virtual void
1276   do_init_layout(Layout* layout) = 0;
1277
1278   // Mark regions of the input file that must be kept unchanged.
1279   virtual void
1280   do_reserve_layout(unsigned int input_file_index) = 0;
1281
1282   virtual unsigned int
1283   do_input_file_count() const = 0;
1284
1285   virtual const Input_reader*
1286   do_get_input_reader(unsigned int) const = 0;
1287
1288   // Map from input file index to Input_argument.
1289   std::vector<const Input_argument*> input_args_map_;
1290   // Map from an input file index to an Incremental_library.
1291   std::vector<Incremental_library*> library_map_;
1292   // Map from an input file index to a Script_info.
1293   std::vector<Script_info*> script_map_;
1294
1295  private:
1296   // Edited output file object.
1297   Output_file* output_;
1298   // Target of the output file.
1299   Target* target_;
1300 };
1301
1302 template<int size, bool big_endian>
1303 class Sized_incremental_binary : public Incremental_binary
1304 {
1305  public:
1306   Sized_incremental_binary(Output_file* output,
1307                            const elfcpp::Ehdr<size, big_endian>& ehdr,
1308                            Target* target)
1309     : Incremental_binary(output, target), elf_file_(this, ehdr),
1310       section_map_(), has_incremental_info_(false), inputs_reader_(),
1311       symtab_reader_(), relocs_reader_(), got_plt_reader_(),
1312       input_entry_readers_()
1313   { this->setup_readers(); }
1314
1315   // Returns TRUE if the file contains incremental info.
1316   bool
1317   has_incremental_info() const
1318   { return this->has_incremental_info_; }
1319
1320   // Return the Output_section for section index SHNDX.
1321   Output_section*
1322   output_section(unsigned int shndx)
1323   { return this->section_map_[shndx]; }
1324
1325   // Readers for the incremental info sections.
1326
1327   const Incremental_inputs_reader<size, big_endian>&
1328   inputs_reader() const
1329   { return this->inputs_reader_; }
1330
1331   const Incremental_symtab_reader<big_endian>&
1332   symtab_reader() const
1333   { return this->symtab_reader_; }
1334
1335   const Incremental_relocs_reader<size, big_endian>&
1336   relocs_reader() const
1337   { return this->relocs_reader_; }
1338
1339   const Incremental_got_plt_reader<big_endian>&
1340   got_plt_reader() const
1341   { return this->got_plt_reader_; }
1342
1343   void
1344   get_symtab_view(View* symtab_view, unsigned int* sym_count,
1345                   elfcpp::Elf_strtab* strtab);
1346
1347  protected:
1348   typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
1349   typedef typename Inputs_reader::Incremental_input_entry_reader
1350       Input_entry_reader;
1351
1352   virtual bool
1353   do_check_inputs(const Command_line& cmdline,
1354                   Incremental_inputs* incremental_inputs);
1355
1356   // Return TRUE if input file N has changed since the last incremental link.
1357   virtual bool
1358   do_file_has_changed(unsigned int n) const;
1359
1360   // Initialize the layout of the output file based on the existing
1361   // output file.
1362   virtual void
1363   do_init_layout(Layout* layout);
1364
1365   // Mark regions of the input file that must be kept unchanged.
1366   virtual void
1367   do_reserve_layout(unsigned int input_file_index);
1368
1369   // Proxy class for a sized Incremental_input_entry_reader.
1370
1371   class Sized_input_reader : public Input_reader
1372   {
1373    public:
1374     Sized_input_reader(Input_entry_reader r)
1375       : Input_reader(), reader_(r)
1376     { }
1377
1378     virtual
1379     ~Sized_input_reader()
1380     { }
1381
1382    private:
1383     const char*
1384     do_filename() const
1385     { return this->reader_.filename(); }
1386
1387     Timespec
1388     do_get_mtime() const
1389     { return this->reader_.get_mtime(); }
1390
1391     Incremental_input_type
1392     do_type() const
1393     { return this->reader_.type(); }
1394
1395     unsigned int
1396     do_arg_serial() const
1397     { return this->reader_.arg_serial(); }
1398
1399     unsigned int
1400     do_get_unused_symbol_count() const
1401     { return this->reader_.get_unused_symbol_count(); }
1402
1403     const char*
1404     do_get_unused_symbol(unsigned int n) const
1405     { return this->reader_.get_unused_symbol(n); }
1406
1407     Input_entry_reader reader_;
1408   };
1409
1410   virtual unsigned int
1411   do_input_file_count() const
1412   { return this->inputs_reader_.input_file_count(); }
1413
1414   virtual const Input_reader*
1415   do_get_input_reader(unsigned int n) const
1416   {
1417     gold_assert(n < this->input_entry_readers_.size());
1418     return &this->input_entry_readers_[n];
1419   }
1420
1421  private:
1422   bool
1423   find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
1424                                    unsigned int* p_symtab_shndx,
1425                                    unsigned int* p_relocs_shndx,
1426                                    unsigned int* p_got_plt_shndx,
1427                                    unsigned int* p_strtab_shndx);
1428
1429   void
1430   setup_readers();
1431
1432   // Output as an ELF file.
1433   elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
1434
1435   // Map section index to an Output_section in the updated layout.
1436   std::vector<Output_section*> section_map_;
1437
1438   // Readers for the incremental info sections.
1439   bool has_incremental_info_;
1440   Incremental_inputs_reader<size, big_endian> inputs_reader_;
1441   Incremental_symtab_reader<big_endian> symtab_reader_;
1442   Incremental_relocs_reader<size, big_endian> relocs_reader_;
1443   Incremental_got_plt_reader<big_endian> got_plt_reader_;
1444   std::vector<Sized_input_reader> input_entry_readers_;
1445 };
1446
1447 // An incremental Relobj.  This class represents a relocatable object
1448 // that has not changed since the last incremental link, and whose contents
1449 // can be used directly from the base file.
1450
1451 template<int size, bool big_endian>
1452 class Sized_incr_relobj : public Sized_relobj_base<size, big_endian>
1453 {
1454  public:
1455   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1456   typedef typename Sized_relobj_base<size, big_endian>::Symbols Symbols;
1457
1458   static const Address invalid_address = static_cast<Address>(0) - 1;
1459
1460   Sized_incr_relobj(const std::string& name,
1461                     Sized_incremental_binary<size, big_endian>* ibase,
1462                     unsigned int input_file_index);
1463
1464   // Checks if the offset of input section SHNDX within its output
1465   // section is invalid.
1466   bool
1467   is_output_section_offset_invalid(unsigned int shndx) const
1468   { return this->section_offsets_[shndx] == invalid_address; }
1469
1470   // Get the offset of input section SHNDX within its output section.
1471   // This is -1 if the input section requires a special mapping, such
1472   // as a merge section.  The output section can be found in the
1473   // output_sections_ field of the parent class Incrobj.
1474   uint64_t
1475   do_output_section_offset(unsigned int shndx) const
1476   {
1477     gold_assert(shndx < this->section_offsets_.size());
1478     Address off = this->section_offsets_[shndx];
1479     if (off == invalid_address)
1480       return -1ULL;
1481     return off;
1482   }
1483
1484  private:
1485   typedef typename Sized_relobj_base<size, big_endian>::Output_sections
1486       Output_sections;
1487   typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
1488   typedef typename Inputs_reader::Incremental_input_entry_reader
1489       Input_entry_reader;
1490
1491   // Return TRUE if this is an incremental (unchanged) input file.
1492   bool
1493   do_is_incremental() const
1494   { return true; }
1495
1496   // Return the last modified time of the file.
1497   Timespec
1498   do_get_mtime()
1499   { return this->input_reader_.get_mtime(); }
1500
1501   // Read the symbols.
1502   void
1503   do_read_symbols(Read_symbols_data*);
1504
1505   // Lay out the input sections.
1506   void
1507   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1508
1509   // Layout sections whose layout was deferred while waiting for
1510   // input files from a plugin.
1511   void
1512   do_layout_deferred_sections(Layout*);
1513
1514   // Add the symbols to the symbol table.
1515   void
1516   do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
1517
1518   Archive::Should_include
1519   do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
1520                            std::string* why);
1521
1522   // Iterate over global symbols, calling a visitor class V for each.
1523   void
1524   do_for_all_global_symbols(Read_symbols_data* sd,
1525                             Library_base::Symbol_visitor_base* v);
1526
1527   // Iterate over local symbols, calling a visitor class V for each GOT offset
1528   // associated with a local symbol.
1529   void
1530   do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
1531
1532   // Get the size of a section.
1533   uint64_t
1534   do_section_size(unsigned int shndx);
1535
1536   // Get the name of a section.
1537   std::string
1538   do_section_name(unsigned int shndx);
1539
1540   // Return a view of the contents of a section.
1541   Object::Location
1542   do_section_contents(unsigned int shndx);
1543
1544   // Return section flags.
1545   uint64_t
1546   do_section_flags(unsigned int shndx);
1547
1548   // Return section entsize.
1549   uint64_t
1550   do_section_entsize(unsigned int shndx);
1551
1552   // Return section address.
1553   uint64_t
1554   do_section_address(unsigned int shndx);
1555
1556   // Return section type.
1557   unsigned int
1558   do_section_type(unsigned int shndx);
1559
1560   // Return the section link field.
1561   unsigned int
1562   do_section_link(unsigned int shndx);
1563
1564   // Return the section link field.
1565   unsigned int
1566   do_section_info(unsigned int shndx);
1567
1568   // Return the section alignment.
1569   uint64_t
1570   do_section_addralign(unsigned int shndx);
1571
1572   // Return the Xindex structure to use.
1573   Xindex*
1574   do_initialize_xindex();
1575
1576   // Get symbol counts.
1577   void
1578   do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1579
1580   // Get global symbols.
1581   const Symbols*
1582   do_get_global_symbols() const
1583   { return &this->symbols_; }
1584
1585   // Return the number of local symbols.
1586   unsigned int
1587   do_local_symbol_count() const
1588   { return 0; }
1589
1590   // Read the relocs.
1591   void
1592   do_read_relocs(Read_relocs_data*);
1593
1594   // Process the relocs to find list of referenced sections. Used only
1595   // during garbage collection.
1596   void
1597   do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1598
1599   // Scan the relocs and adjust the symbol table.
1600   void
1601   do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1602
1603   // Count the local symbols.
1604   void
1605   do_count_local_symbols(Stringpool_template<char>*,
1606                          Stringpool_template<char>*);
1607
1608   // Finalize the local symbols.
1609   unsigned int
1610   do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
1611
1612   // Set the offset where local dynamic symbol information will be stored.
1613   unsigned int
1614   do_set_local_dynsym_indexes(unsigned int);
1615
1616   // Set the offset where local dynamic symbol information will be stored.
1617   unsigned int
1618   do_set_local_dynsym_offset(off_t);
1619
1620   // Relocate the input sections and write out the local symbols.
1621   void
1622   do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
1623
1624   // Set the offset of a section.
1625   void
1626   do_set_section_offset(unsigned int shndx, uint64_t off);
1627
1628   // The Incremental_binary base file.
1629   Sized_incremental_binary<size, big_endian>* ibase_;
1630   // The index of the object in the input file list.
1631   unsigned int input_file_index_;
1632   // The reader for the input file.
1633   Input_entry_reader input_reader_;
1634   // The entries in the symbol table for the external symbols.
1635   Symbols symbols_;
1636   // For each input section, the offset of the input section in its
1637   // output section.  This is INVALID_ADDRESS if the input section requires a
1638   // special mapping.
1639   std::vector<Address> section_offsets_;
1640   // The offset of the first incremental relocation for this object.
1641   unsigned int incr_reloc_offset_;
1642   // The number of incremental relocations for this object.
1643   unsigned int incr_reloc_count_;
1644   // The index of the first incremental relocation for this object in the
1645   // updated output file.
1646   unsigned int incr_reloc_output_index_;
1647   // A copy of the incremental relocations from this object.
1648   unsigned char* incr_relocs_;
1649 };
1650
1651 // An incremental Dynobj.  This class represents a shared object that has
1652 // not changed since the last incremental link, and whose contents can be
1653 // used directly from the base file.
1654
1655 template<int size, bool big_endian>
1656 class Sized_incr_dynobj : public Dynobj
1657 {
1658  public:
1659   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1660
1661   static const Address invalid_address = static_cast<Address>(0) - 1;
1662
1663   Sized_incr_dynobj(const std::string& name,
1664                     Sized_incremental_binary<size, big_endian>* ibase,
1665                     unsigned int input_file_index);
1666
1667  private:
1668   typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
1669   typedef typename Inputs_reader::Incremental_input_entry_reader
1670       Input_entry_reader;
1671
1672   // Return TRUE if this is an incremental (unchanged) input file.
1673   bool
1674   do_is_incremental() const
1675   { return true; }
1676
1677   // Return the last modified time of the file.
1678   Timespec
1679   do_get_mtime()
1680   { return this->input_reader_.get_mtime(); }
1681
1682   // Read the symbols.
1683   void
1684   do_read_symbols(Read_symbols_data*);
1685
1686   // Lay out the input sections.
1687   void
1688   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1689
1690   // Add the symbols to the symbol table.
1691   void
1692   do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
1693
1694   Archive::Should_include
1695   do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
1696                            std::string* why);
1697
1698   // Iterate over global symbols, calling a visitor class V for each.
1699   void
1700   do_for_all_global_symbols(Read_symbols_data* sd,
1701                             Library_base::Symbol_visitor_base* v);
1702
1703   // Iterate over local symbols, calling a visitor class V for each GOT offset
1704   // associated with a local symbol.
1705   void
1706   do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
1707
1708   // Get the size of a section.
1709   uint64_t
1710   do_section_size(unsigned int shndx);
1711
1712   // Get the name of a section.
1713   std::string
1714   do_section_name(unsigned int shndx);
1715
1716   // Return a view of the contents of a section.
1717   Object::Location
1718   do_section_contents(unsigned int shndx);
1719
1720   // Return section flags.
1721   uint64_t
1722   do_section_flags(unsigned int shndx);
1723
1724   // Return section entsize.
1725   uint64_t
1726   do_section_entsize(unsigned int shndx);
1727
1728   // Return section address.
1729   uint64_t
1730   do_section_address(unsigned int shndx);
1731
1732   // Return section type.
1733   unsigned int
1734   do_section_type(unsigned int shndx);
1735
1736   // Return the section link field.
1737   unsigned int
1738   do_section_link(unsigned int shndx);
1739
1740   // Return the section link field.
1741   unsigned int
1742   do_section_info(unsigned int shndx);
1743
1744   // Return the section alignment.
1745   uint64_t
1746   do_section_addralign(unsigned int shndx);
1747
1748   // Return the Xindex structure to use.
1749   Xindex*
1750   do_initialize_xindex();
1751
1752   // Get symbol counts.
1753   void
1754   do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1755
1756   // Get global symbols.
1757   const Symbols*
1758   do_get_global_symbols() const
1759   { return &this->symbols_; }
1760
1761   // The Incremental_binary base file.
1762   Sized_incremental_binary<size, big_endian>* ibase_;
1763   // The index of the object in the input file list.
1764   unsigned int input_file_index_;
1765   // The reader for the input file.
1766   Input_entry_reader input_reader_;
1767   // The entries in the symbol table for the external symbols.
1768   Symbols symbols_;
1769 };
1770
1771 // Allocate an incremental object of the appropriate size and endianness.
1772 extern Object*
1773 make_sized_incremental_object(
1774     Incremental_binary* base,
1775     unsigned int input_file_index,
1776     Incremental_input_type input_type,
1777     const Incremental_binary::Input_reader* input_reader);
1778
1779 // This class represents an Archive library (or --start-lib/--end-lib group)
1780 // that has not changed since the last incremental link.  Its contents come
1781 // from the incremental inputs entry in the base file.
1782
1783 class Incremental_library : public Library_base
1784 {
1785  public:
1786   Incremental_library(const char* filename, unsigned int input_file_index,
1787                       const Incremental_binary::Input_reader* input_reader)
1788     : Library_base(NULL), filename_(filename),
1789       input_file_index_(input_file_index), input_reader_(input_reader),
1790       unused_symbols_(), is_reported_(false)
1791   { }
1792
1793   // Return the input file index.
1794   unsigned int
1795   input_file_index() const
1796   { return this->input_file_index_; }
1797
1798   // Return the serial number of the input file.
1799   unsigned int
1800   arg_serial() const
1801   { return this->input_reader_->arg_serial(); }
1802
1803   // Copy the unused symbols from the incremental input info.
1804   // We need to do this because we may be overwriting the incremental
1805   // input info in the base file before we write the new incremental
1806   // info.
1807   void
1808   copy_unused_symbols();
1809
1810   // Return FALSE on the first call to indicate that the library needs
1811   // to be recorded; return TRUE subsequently.
1812   bool
1813   is_reported()
1814   {
1815     bool was_reported = this->is_reported_;
1816     is_reported_ = true;
1817     return was_reported;
1818   }
1819
1820  private:
1821   typedef std::vector<std::string> Symbol_list;
1822
1823   // The file name.
1824   const std::string&
1825   do_filename() const
1826   { return this->filename_; }
1827
1828   // Return the modification time of the archive file.
1829   Timespec
1830   do_get_mtime()
1831   { return this->input_reader_->get_mtime(); }
1832
1833   // Iterator for unused global symbols in the library.
1834   void
1835   do_for_all_unused_symbols(Symbol_visitor_base* v) const;
1836
1837   // The name of the library.
1838   std::string filename_;
1839   // The input file index of this library.
1840   unsigned int input_file_index_;
1841   // A reader for the incremental input information.
1842   const Incremental_binary::Input_reader* input_reader_;
1843   // List of unused symbols defined in this library.
1844   Symbol_list unused_symbols_;
1845   // TRUE when this library has been reported to the new incremental info.
1846   bool is_reported_;
1847 };
1848
1849 } // End namespace gold.
1850
1851 #endif // !defined(GOLD_INCREMENTAL_H)