1 // inremental.h -- incremental linking support for gold   -*- C++ -*-
2 
3 // Copyright (C) 2009-2016 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_dynobj_entry;
44 class Incremental_archive_entry;
45 class Incremental_inputs;
46 class Incremental_binary;
47 class Incremental_library;
48 class Object;
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 = 0x8000,
67   INCREMENTAL_INPUT_AS_NEEDED = 0x4000
68 };
69 
70 // Symbol flags for the incremental symbol table.
71 // These flags are stored in the top two bits of
72 // the symbol index field.
73 
74 enum Incremental_shlib_symbol_flags
75 {
76   // Symbol is defined in this library.
77   INCREMENTAL_SHLIB_SYM_DEF = 2,
78   // Symbol is defined in this library, with a COPY relocation.
79   INCREMENTAL_SHLIB_SYM_COPY = 3
80 };
81 
82 static const int INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT = 30;
83 
84 // Return TRUE if a section of type SH_TYPE can be updated in place
85 // during an incremental update.
86 bool
87 can_incremental_update(unsigned int sh_type);
88 
89 // Create an Incremental_binary object for FILE. Returns NULL is this is not
90 // possible, e.g. FILE is not an ELF file or has an unsupported target.
91 
92 Incremental_binary*
93 open_incremental_binary(Output_file* file);
94 
95 // Base class for recording each input file.
96 
97 class Incremental_input_entry
98 {
99  public:
Incremental_input_entry(Stringpool::Key filename_key,unsigned int arg_serial,Timespec mtime)100   Incremental_input_entry(Stringpool::Key filename_key, unsigned int arg_serial,
101 			  Timespec mtime)
102     : filename_key_(filename_key), file_index_(0), offset_(0), info_offset_(0),
103       arg_serial_(arg_serial), mtime_(mtime), is_in_system_directory_(false),
104       as_needed_(false)
105   { }
106 
107   virtual
~Incremental_input_entry()108   ~Incremental_input_entry()
109   { }
110 
111   // Return the type of input file.
112   Incremental_input_type
type()113   type() const
114   { return this->do_type(); }
115 
116   // Set the index and section offset of this input file entry.
117   void
set_offset(unsigned int file_index,unsigned int offset)118   set_offset(unsigned int file_index, unsigned int offset)
119   {
120     this->file_index_ = file_index;
121     this->offset_ = offset;
122   }
123 
124   // Set the section offset of the supplemental information for this entry.
125   void
set_info_offset(unsigned int info_offset)126   set_info_offset(unsigned int info_offset)
127   { this->info_offset_ = info_offset; }
128 
129   // Get the index of this input file entry.
130   unsigned int
get_file_index()131   get_file_index() const
132   { return this->file_index_; }
133 
134   // Get the section offset of this input file entry.
135   unsigned int
get_offset()136   get_offset() const
137   { return this->offset_; }
138 
139   // Get the section offset of the supplemental information for this entry.
140   unsigned int
get_info_offset()141   get_info_offset() const
142   { return this->info_offset_; }
143 
144   // Get the stringpool key for the input filename.
145   Stringpool::Key
get_filename_key()146   get_filename_key() const
147   { return this->filename_key_; }
148 
149   // Get the serial number of the input file.
150   unsigned int
arg_serial()151   arg_serial() const
152   { return this->arg_serial_; }
153 
154   // Get the modification time of the input file.
155   const Timespec&
get_mtime()156   get_mtime() const
157   { return this->mtime_; }
158 
159   // Record that the file was found in a system directory.
160   void
set_is_in_system_directory()161   set_is_in_system_directory()
162   { this->is_in_system_directory_ = true; }
163 
164   // Return TRUE if the file was found in a system directory.
165   bool
is_in_system_directory()166   is_in_system_directory() const
167   { return this->is_in_system_directory_; }
168 
169   // Record that the file was linked with --as-needed.
170   void
set_as_needed()171   set_as_needed()
172   { this->as_needed_ = true; }
173 
174   // Return TRUE if the file was linked with --as-needed.
175   bool
as_needed()176   as_needed() const
177   { return this->as_needed_; }
178 
179   // Return a pointer to the derived Incremental_script_entry object.
180   // Return NULL for input entries that are not script files.
181   Incremental_script_entry*
script_entry()182   script_entry()
183   { return this->do_script_entry(); }
184 
185   // Return a pointer to the derived Incremental_object_entry object.
186   // Return NULL for input entries that are not object files.
187   Incremental_object_entry*
object_entry()188   object_entry()
189   { return this->do_object_entry(); }
190 
191   // Return a pointer to the derived Incremental_dynobj_entry object.
192   // Return NULL for input entries that are not shared object files.
193   Incremental_dynobj_entry*
dynobj_entry()194   dynobj_entry()
195   { return this->do_dynobj_entry(); }
196 
197   // Return a pointer to the derived Incremental_archive_entry object.
198   // Return NULL for input entries that are not archive files.
199   Incremental_archive_entry*
archive_entry()200   archive_entry()
201   { return this->do_archive_entry(); }
202 
203  protected:
204   // Return the type of input file.
205   virtual Incremental_input_type
206   do_type() const = 0;
207 
208   // Return a pointer to the derived Incremental_script_entry object.
209   // Return NULL for input entries that are not script files.
210   virtual Incremental_script_entry*
do_script_entry()211   do_script_entry()
212   { return NULL; }
213 
214   // Return a pointer to the derived Incremental_object_entry object.
215   // Return NULL for input entries that are not object files.
216   virtual Incremental_object_entry*
do_object_entry()217   do_object_entry()
218   { return NULL; }
219 
220   // Return a pointer to the derived Incremental_dynobj_entry object.
221   // Return NULL for input entries that are not shared object files.
222   virtual Incremental_dynobj_entry*
do_dynobj_entry()223   do_dynobj_entry()
224   { return NULL; }
225 
226   // Return a pointer to the derived Incremental_archive_entry object.
227   // Return NULL for input entries that are not archive files.
228   virtual Incremental_archive_entry*
do_archive_entry()229   do_archive_entry()
230   { return NULL; }
231 
232  private:
233   // Key of the filename string in the section stringtable.
234   Stringpool::Key filename_key_;
235 
236   // Index of the entry in the output section.
237   unsigned int file_index_;
238 
239   // Offset of the entry in the output section.
240   unsigned int offset_;
241 
242   // Offset of the extra information in the output section.
243   unsigned int info_offset_;
244 
245   // Serial number of the file in the argument list.
246   unsigned int arg_serial_;
247 
248   // Last modification time of the file.
249   Timespec mtime_;
250 
251   // TRUE if the file was found in a system directory.
252   bool is_in_system_directory_;
253 
254   // TRUE if the file was linked with --as-needed.
255   bool as_needed_;
256 };
257 
258 // Information about a script input that will persist during the whole linker
259 // run.  Needed only during an incremental build to retrieve the input files
260 // added by this script.
261 
262 class Script_info
263 {
264  public:
Script_info(const std::string & filename)265   Script_info(const std::string& filename)
266     : filename_(filename), input_file_index_(0),
267       incremental_script_entry_(NULL)
268   { }
269 
Script_info(const std::string & filename,unsigned int input_file_index)270   Script_info(const std::string& filename, unsigned int input_file_index)
271     : filename_(filename), input_file_index_(input_file_index),
272       incremental_script_entry_(NULL)
273   { }
274 
275   // Store a pointer to the incremental information for this script.
276   void
set_incremental_info(Incremental_script_entry * entry)277   set_incremental_info(Incremental_script_entry* entry)
278   { this->incremental_script_entry_ = entry; }
279 
280   // Return the filename.
281   const std::string&
filename()282   filename() const
283   { return this->filename_; }
284 
285   // Return the input file index.
286   unsigned int
input_file_index()287   input_file_index() const
288   { return this->input_file_index_; }
289 
290   // Return the pointer to the incremental information for this script.
291   Incremental_script_entry*
incremental_info()292   incremental_info() const
293   { return this->incremental_script_entry_; }
294 
295  private:
296   const std::string filename_;
297   unsigned int input_file_index_;
298   Incremental_script_entry* incremental_script_entry_;
299 };
300 
301 // Class for recording input scripts.
302 
303 class Incremental_script_entry : public Incremental_input_entry
304 {
305  public:
Incremental_script_entry(Stringpool::Key filename_key,unsigned int arg_serial,Script_info *,Timespec mtime)306   Incremental_script_entry(Stringpool::Key filename_key,
307 			   unsigned int arg_serial, Script_info* /*script*/,
308 			   Timespec mtime)
309     : Incremental_input_entry(filename_key, arg_serial, mtime),
310       objects_()
311   { }
312 
313   // Add a member object to the archive.
314   void
add_object(Incremental_input_entry * obj_entry)315   add_object(Incremental_input_entry* obj_entry)
316   {
317     this->objects_.push_back(obj_entry);
318   }
319 
320   // Return the number of objects included by this script.
321   unsigned int
get_object_count()322   get_object_count()
323   { return this->objects_.size(); }
324 
325   // Return the Nth object.
326   Incremental_input_entry*
get_object(unsigned int n)327   get_object(unsigned int n)
328   {
329     gold_assert(n < this->objects_.size());
330     return this->objects_[n];
331   }
332 
333  protected:
334   virtual Incremental_input_type
do_type()335   do_type() const
336   { return INCREMENTAL_INPUT_SCRIPT; }
337 
338   // Return a pointer to the derived Incremental_script_entry object.
339   virtual Incremental_script_entry*
do_script_entry()340   do_script_entry()
341   { return this; }
342 
343  private:
344   // Objects that have been included by this script.
345   std::vector<Incremental_input_entry*> objects_;
346 };
347 
348 // Class for recording input object files.
349 
350 class Incremental_object_entry : public Incremental_input_entry
351 {
352  public:
Incremental_object_entry(Stringpool::Key filename_key,Object * obj,unsigned int arg_serial,Timespec mtime)353   Incremental_object_entry(Stringpool::Key filename_key, Object* obj,
354 			   unsigned int arg_serial, Timespec mtime)
355     : Incremental_input_entry(filename_key, arg_serial, mtime), obj_(obj),
356       is_member_(false), sections_(), groups_()
357   { this->sections_.reserve(obj->shnum()); }
358 
359   // Get the object.
360   Object*
object()361   object() const
362   { return this->obj_; }
363 
364   // Record that this object is an archive member.
365   void
set_is_member()366   set_is_member()
367   { this->is_member_ = true; }
368 
369   // Return true if this object is an archive member.
370   bool
is_member()371   is_member() const
372   { return this->is_member_; }
373 
374   // Add an input section.
375   void
add_input_section(unsigned int shndx,Stringpool::Key name_key,off_t sh_size)376   add_input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
377   { this->sections_.push_back(Input_section(shndx, name_key, sh_size)); }
378 
379   // Return the number of input sections in this object.
380   unsigned int
get_input_section_count()381   get_input_section_count() const
382   { return this->sections_.size(); }
383 
384   // Return the input section index for the Nth input section.
385   Stringpool::Key
get_input_section_index(unsigned int n)386   get_input_section_index(unsigned int n) const
387   { return this->sections_[n].shndx_; }
388 
389   // Return the stringpool key of the Nth input section.
390   Stringpool::Key
get_input_section_name_key(unsigned int n)391   get_input_section_name_key(unsigned int n) const
392   { return this->sections_[n].name_key_; }
393 
394   // Return the size of the Nth input section.
395   off_t
get_input_section_size(unsigned int n)396   get_input_section_size(unsigned int n) const
397   { return this->sections_[n].sh_size_; }
398 
399   // Add a kept COMDAT group.
400   void
add_comdat_group(Stringpool::Key signature_key)401   add_comdat_group(Stringpool::Key signature_key)
402   { this->groups_.push_back(signature_key); }
403 
404   // Return the number of COMDAT groups.
405   unsigned int
get_comdat_group_count()406   get_comdat_group_count() const
407   { return this->groups_.size(); }
408 
409   // Return the stringpool key for the signature of the Nth comdat group.
410   Stringpool::Key
get_comdat_signature_key(unsigned int n)411   get_comdat_signature_key(unsigned int n) const
412   { return this->groups_[n]; }
413 
414  protected:
415   virtual Incremental_input_type
do_type()416   do_type() const
417   {
418     return (this->is_member_
419 	    ? INCREMENTAL_INPUT_ARCHIVE_MEMBER
420 	    : INCREMENTAL_INPUT_OBJECT);
421   }
422 
423   // Return a pointer to the derived Incremental_object_entry object.
424   virtual Incremental_object_entry*
do_object_entry()425   do_object_entry()
426   { return this; }
427 
428  private:
429   // The object file itself.
430   Object* obj_;
431 
432   // Whether this object is an archive member.
433   bool is_member_;
434 
435   // Input sections.
436   struct Input_section
437   {
Input_sectionInput_section438     Input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
439       : shndx_(shndx), name_key_(name_key), sh_size_(sh_size)
440     { }
441     unsigned int shndx_;
442     Stringpool::Key name_key_;
443     off_t sh_size_;
444   };
445   std::vector<Input_section> sections_;
446 
447   // COMDAT groups.
448   std::vector<Stringpool::Key> groups_;
449 };
450 
451 // Class for recording shared library input files.
452 
453 class Incremental_dynobj_entry : public Incremental_input_entry
454 {
455  public:
Incremental_dynobj_entry(Stringpool::Key filename_key,Stringpool::Key soname_key,Object * obj,unsigned int arg_serial,Timespec mtime)456   Incremental_dynobj_entry(Stringpool::Key filename_key,
457   			   Stringpool::Key soname_key, Object* obj,
458 			   unsigned int arg_serial, Timespec mtime)
459     : Incremental_input_entry(filename_key, arg_serial, mtime),
460       soname_key_(soname_key), obj_(obj)
461   { }
462 
463   // Get the object.
464   Object*
object()465   object() const
466   { return this->obj_; }
467 
468   // Get the stringpool key for the soname.
469   Stringpool::Key
get_soname_key()470   get_soname_key() const
471   { return this->soname_key_; }
472 
473  protected:
474   virtual Incremental_input_type
do_type()475   do_type() const
476   { return INCREMENTAL_INPUT_SHARED_LIBRARY; }
477 
478   // Return a pointer to the derived Incremental_dynobj_entry object.
479   virtual Incremental_dynobj_entry*
do_dynobj_entry()480   do_dynobj_entry()
481   { return this; }
482 
483  private:
484   // Key of the soname string in the section stringtable.
485   Stringpool::Key soname_key_;
486 
487   // The object file itself.
488   Object* obj_;
489 };
490 
491 // Class for recording archive library input files.
492 
493 class Incremental_archive_entry : public Incremental_input_entry
494 {
495  public:
Incremental_archive_entry(Stringpool::Key filename_key,unsigned int arg_serial,Timespec mtime)496   Incremental_archive_entry(Stringpool::Key filename_key,
497 			    unsigned int arg_serial, Timespec mtime)
498     : Incremental_input_entry(filename_key, arg_serial, mtime), members_(),
499       unused_syms_()
500   { }
501 
502   // Add a member object to the archive.
503   void
add_object(Incremental_object_entry * obj_entry)504   add_object(Incremental_object_entry* obj_entry)
505   {
506     this->members_.push_back(obj_entry);
507     obj_entry->set_is_member();
508   }
509 
510   // Add an unused global symbol to the archive.
511   void
add_unused_global_symbol(Stringpool::Key symbol_key)512   add_unused_global_symbol(Stringpool::Key symbol_key)
513   { this->unused_syms_.push_back(symbol_key); }
514 
515   // Return the number of member objects included in the link.
516   unsigned int
get_member_count()517   get_member_count()
518   { return this->members_.size(); }
519 
520   // Return the Nth member object.
521   Incremental_object_entry*
get_member(unsigned int n)522   get_member(unsigned int n)
523   { return this->members_[n]; }
524 
525   // Return the number of unused global symbols in this archive.
526   unsigned int
get_unused_global_symbol_count()527   get_unused_global_symbol_count()
528   { return this->unused_syms_.size(); }
529 
530   // Return the Nth unused global symbol.
531   Stringpool::Key
get_unused_global_symbol(unsigned int n)532   get_unused_global_symbol(unsigned int n)
533   { return this->unused_syms_[n]; }
534 
535  protected:
536   virtual Incremental_input_type
do_type()537   do_type() const
538   { return INCREMENTAL_INPUT_ARCHIVE; }
539 
540   // Return a pointer to the derived Incremental_archive_entry object.
541   virtual Incremental_archive_entry*
do_archive_entry()542   do_archive_entry()
543   { return this; }
544 
545  private:
546   // Members of the archive that have been included in the link.
547   std::vector<Incremental_object_entry*> members_;
548 
549   // Unused global symbols from this archive.
550   std::vector<Stringpool::Key> unused_syms_;
551 };
552 
553 // This class contains the information needed during an incremental
554 // build about the inputs necessary to build the .gnu_incremental_inputs.
555 
556 class Incremental_inputs
557 {
558  public:
559   typedef std::vector<Incremental_input_entry*> Input_list;
560 
Incremental_inputs()561   Incremental_inputs()
562     : inputs_(), command_line_(), command_line_key_(0),
563       strtab_(new Stringpool()), current_object_(NULL),
564       current_object_entry_(NULL), inputs_section_(NULL),
565       symtab_section_(NULL), relocs_section_(NULL),
566       reloc_count_(0)
567   { }
568 
~Incremental_inputs()569   ~Incremental_inputs() { delete this->strtab_; }
570 
571   // Record the command line.
572   void
573   report_command_line(int argc, const char* const* argv);
574 
575   // Record the initial info for archive file ARCHIVE.
576   void
577   report_archive_begin(Library_base* arch, unsigned int arg_serial,
578 		       Script_info* script_info);
579 
580   // Record the final info for archive file ARCHIVE.
581   void
582   report_archive_end(Library_base* arch);
583 
584   // Record the info for object file OBJ.  If ARCH is not NULL,
585   // attach the object file to the archive.
586   void
587   report_object(Object* obj, unsigned int arg_serial, Library_base* arch,
588 		Script_info* script_info);
589 
590   // Record an input section belonging to object file OBJ.
591   void
592   report_input_section(Object* obj, unsigned int shndx, const char* name,
593 		       off_t sh_size);
594 
595   // Record a kept COMDAT group belonging to object file OBJ.
596   void
597   report_comdat_group(Object* obj, const char* name);
598 
599   // Record the info for input script SCRIPT.
600   void
601   report_script(Script_info* script, unsigned int arg_serial,
602 		Timespec mtime);
603 
604   // Return the running count of incremental relocations.
605   unsigned int
get_reloc_count()606   get_reloc_count() const
607   { return this->reloc_count_; }
608 
609   // Update the running count of incremental relocations.
610   void
set_reloc_count(unsigned int count)611   set_reloc_count(unsigned int count)
612   { this->reloc_count_ = count; }
613 
614   // Prepare for layout.  Called from Layout::finalize.
615   void
616   finalize();
617 
618   // Create the .gnu_incremental_inputs and related sections.
619   void
620   create_data_sections(Symbol_table* symtab);
621 
622   // Return the .gnu_incremental_inputs section.
623   Output_section_data*
inputs_section()624   inputs_section() const
625   { return this->inputs_section_; }
626 
627   // Return the .gnu_incremental_symtab section.
628   Output_data_space*
symtab_section()629   symtab_section() const
630   { return this->symtab_section_; }
631 
632   // Return the .gnu_incremental_relocs section.
633   Output_data_space*
relocs_section()634   relocs_section() const
635   { return this->relocs_section_; }
636 
637   // Return the .gnu_incremental_got_plt section.
638   Output_data_space*
got_plt_section()639   got_plt_section() const
640   { return this->got_plt_section_; }
641 
642   // Return the .gnu_incremental_strtab stringpool.
643   Stringpool*
get_stringpool()644   get_stringpool() const
645   { return this->strtab_; }
646 
647   // Return the canonical form of the command line, as will be stored in
648   // .gnu_incremental_strtab.
649   const std::string&
command_line()650   command_line() const
651   { return this->command_line_; }
652 
653   // Return the stringpool key of the command line.
654   Stringpool::Key
command_line_key()655   command_line_key() const
656   { return this->command_line_key_; }
657 
658   // Return the number of input files.
659   int
input_file_count()660   input_file_count() const
661   { return this->inputs_.size(); }
662 
663   // Return the input files.
664   const Input_list&
input_files()665   input_files() const
666   { return this->inputs_; }
667 
668   // Return the sh_entsize value for the .gnu_incremental_relocs section.
669   unsigned int
670   relocs_entsize() const;
671 
672  private:
673   // The list of input files.
674   Input_list inputs_;
675 
676   // Canonical form of the command line, as will be stored in
677   // .gnu_incremental_strtab.
678   std::string command_line_;
679 
680   // The key of the command line string in the string pool.
681   Stringpool::Key command_line_key_;
682 
683   // The .gnu_incremental_strtab string pool associated with the
684   // .gnu_incremental_inputs.
685   Stringpool* strtab_;
686 
687   // Keep track of the object currently being processed.
688   Object* current_object_;
689   Incremental_object_entry* current_object_entry_;
690 
691   // The .gnu_incremental_inputs section.
692   Output_section_data* inputs_section_;
693 
694   // The .gnu_incremental_symtab section.
695   Output_data_space* symtab_section_;
696 
697   // The .gnu_incremental_relocs section.
698   Output_data_space* relocs_section_;
699 
700   // The .gnu_incremental_got_plt section.
701   Output_data_space* got_plt_section_;
702 
703   // Total count of incremental relocations.  Updated during Scan_relocs
704   // phase at the completion of each object file.
705   unsigned int reloc_count_;
706 };
707 
708 // Reader class for global symbol info from an object file entry in
709 // the .gnu_incremental_inputs section.
710 
711 template<bool big_endian>
712 class Incremental_global_symbol_reader
713 {
714  private:
715   typedef elfcpp::Swap<32, big_endian> Swap32;
716 
717  public:
Incremental_global_symbol_reader(const unsigned char * p)718   Incremental_global_symbol_reader(const unsigned char* p)
719     : p_(p)
720   { }
721 
722   unsigned int
output_symndx()723   output_symndx() const
724   { return Swap32::readval(this->p_); }
725 
726   unsigned int
shndx()727   shndx() const
728   { return Swap32::readval(this->p_ + 4); }
729 
730   unsigned int
next_offset()731   next_offset() const
732   { return Swap32::readval(this->p_ + 8); }
733 
734   unsigned int
reloc_count()735   reloc_count() const
736   { return Swap32::readval(this->p_ + 12); }
737 
738   unsigned int
reloc_offset()739   reloc_offset() const
740   { return Swap32::readval(this->p_ + 16); }
741 
742  private:
743   // Base address of the symbol entry.
744   const unsigned char* p_;
745 };
746 
747 // Reader class for .gnu_incremental_inputs section.
748 
749 template<int size, bool big_endian>
750 class Incremental_inputs_reader
751 {
752  private:
753   typedef elfcpp::Swap<size, big_endian> Swap;
754   typedef elfcpp::Swap<16, big_endian> Swap16;
755   typedef elfcpp::Swap<32, big_endian> Swap32;
756   typedef elfcpp::Swap<64, big_endian> Swap64;
757 
758  public:
759   // Size of the .gnu_incremental_inputs header.
760   // (3 x 4-byte fields, plus 4 bytes padding.)
761   static const unsigned int header_size = 16;
762   // Size of an input file entry.
763   // (2 x 4-byte fields, 1 x 12-byte field, 2 x 2-byte fields.)
764   static const unsigned int input_entry_size = 24;
765   // Size of the first part of the supplemental info block for
766   // relocatable objects and archive members.
767   // (7 x 4-byte fields, plus 4 bytes padding.)
768   static const unsigned int object_info_size = 32;
769   // Size of an input section entry.
770   // (2 x 4-byte fields, 2 x address-sized fields.)
771   static const unsigned int input_section_entry_size = 8 + 2 * size / 8;
772   // Size of a global symbol entry in the supplemental info block.
773   // (5 x 4-byte fields.)
774   static const unsigned int global_sym_entry_size = 20;
775 
Incremental_inputs_reader()776   Incremental_inputs_reader()
777     : p_(NULL), strtab_(NULL, 0), input_file_count_(0)
778   { }
779 
Incremental_inputs_reader(const unsigned char * p,const elfcpp::Elf_strtab & strtab)780   Incremental_inputs_reader(const unsigned char* p,
781 			    const elfcpp::Elf_strtab& strtab)
782     : p_(p), strtab_(strtab)
783   { this->input_file_count_ = Swap32::readval(this->p_ + 4); }
784 
785   // Return the version number.
786   unsigned int
version()787   version() const
788   { return Swap32::readval(this->p_); }
789 
790   // Return the count of input file entries.
791   unsigned int
input_file_count()792   input_file_count() const
793   { return this->input_file_count_; }
794 
795   // Return the command line.
796   const char*
command_line()797   command_line() const
798   {
799     unsigned int offset = Swap32::readval(this->p_ + 8);
800     return this->get_string(offset);
801   }
802 
803   // Reader class for an input file entry and its supplemental info.
804   class Incremental_input_entry_reader
805   {
806    private:
807     static const unsigned int object_info_size =
808 	Incremental_inputs_reader<size, big_endian>::object_info_size;
809     static const unsigned int input_section_entry_size =
810 	Incremental_inputs_reader<size, big_endian>::input_section_entry_size;
811     static const unsigned int global_sym_entry_size =
812 	Incremental_inputs_reader<size, big_endian>::global_sym_entry_size;
813 
814    public:
Incremental_input_entry_reader(const Incremental_inputs_reader * inputs,unsigned int offset)815     Incremental_input_entry_reader(const Incremental_inputs_reader* inputs,
816 				   unsigned int offset)
817       : inputs_(inputs), offset_(offset)
818     {
819       this->info_offset_ = Swap32::readval(inputs->p_ + offset + 4);
820       this->flags_ = Swap16::readval(this->inputs_->p_ + offset + 20);
821     }
822 
823     // Return the filename.
824     const char*
filename()825     filename() const
826     {
827       unsigned int offset = Swap32::readval(this->inputs_->p_ + this->offset_);
828       return this->inputs_->get_string(offset);
829     }
830 
831     // Return the argument serial number.
832     unsigned int
arg_serial()833     arg_serial() const
834     {
835       return Swap16::readval(this->inputs_->p_ + this->offset_ + 22);
836     }
837 
838     // Return the timestamp.
839     Timespec
get_mtime()840     get_mtime() const
841     {
842       Timespec t;
843       const unsigned char* p = this->inputs_->p_ + this->offset_ + 8;
844       t.seconds = Swap64::readval(p);
845       t.nanoseconds = Swap32::readval(p+8);
846       return t;
847     }
848 
849     // Return the type of input file.
850     Incremental_input_type
type()851     type() const
852     { return static_cast<Incremental_input_type>(this->flags_ & 0xff); }
853 
854     // Return TRUE if the file was found in a system directory.
855     bool
is_in_system_directory()856     is_in_system_directory() const
857     { return (this->flags_ & INCREMENTAL_INPUT_IN_SYSTEM_DIR) != 0; }
858 
859     // Return TRUE if the file was linked with --as-needed.
860     bool
as_needed()861     as_needed() const
862     { return (this->flags_ & INCREMENTAL_INPUT_AS_NEEDED) != 0; }
863 
864     // Return the input section count -- for objects only.
865     unsigned int
get_input_section_count()866     get_input_section_count() const
867     {
868       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
869 		  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
870       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
871     }
872 
873     // Return the soname -- for shared libraries only.
874     const char*
get_soname()875     get_soname() const
876     {
877       gold_assert(this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
878       unsigned int offset = Swap32::readval(this->inputs_->p_
879 					    + this->info_offset_);
880       return this->inputs_->get_string(offset);
881     }
882 
883     // Return the offset of the supplemental info for symbol SYMNDX --
884     // for objects only.
885     unsigned int
get_symbol_offset(unsigned int symndx)886     get_symbol_offset(unsigned int symndx) const
887     {
888       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
889 		  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
890 
891       unsigned int section_count = this->get_input_section_count();
892       return (this->info_offset_
893 	      + this->object_info_size
894 	      + section_count * this->input_section_entry_size
895 	      + symndx * this->global_sym_entry_size);
896     }
897 
898     // Return the global symbol count -- for objects & shared libraries only.
899     unsigned int
get_global_symbol_count()900     get_global_symbol_count() const
901     {
902       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
903 		  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER
904 		  || this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
905       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
906     }
907 
908     // Return the offset of the first local symbol -- for objects only.
909     unsigned int
get_local_symbol_offset()910     get_local_symbol_offset() const
911     {
912       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
913 		  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
914 
915       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 8);
916     }
917 
918     // Return the local symbol count -- for objects only.
919     unsigned int
get_local_symbol_count()920     get_local_symbol_count() const
921     {
922       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
923 		  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
924 
925       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 12);
926     }
927 
928     // Return the index of the first dynamic relocation -- for objects only.
929     unsigned int
get_first_dyn_reloc()930     get_first_dyn_reloc() const
931     {
932       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
933 		  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
934 
935       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 16);
936     }
937 
938     // Return the dynamic relocation count -- for objects only.
939     unsigned int
get_dyn_reloc_count()940     get_dyn_reloc_count() const
941     {
942       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
943 		  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
944 
945       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 20);
946     }
947 
948     // Return the COMDAT group count -- for objects only.
949     unsigned int
get_comdat_group_count()950     get_comdat_group_count() const
951     {
952       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
953 		  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
954 
955       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 24);
956     }
957 
958     // Return the object count -- for scripts only.
959     unsigned int
get_object_count()960     get_object_count() const
961     {
962       gold_assert(this->type() == INCREMENTAL_INPUT_SCRIPT);
963       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
964     }
965 
966     // Return the input file offset for object N -- for scripts only.
967     unsigned int
get_object_offset(unsigned int n)968     get_object_offset(unsigned int n) const
969     {
970       gold_assert(this->type() == INCREMENTAL_INPUT_SCRIPT);
971       return Swap32::readval(this->inputs_->p_ + this->info_offset_
972 			     + 4 + n * 4);
973     }
974 
975     // Return the member count -- for archives only.
976     unsigned int
get_member_count()977     get_member_count() const
978     {
979       gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
980       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
981     }
982 
983     // Return the unused symbol count -- for archives only.
984     unsigned int
get_unused_symbol_count()985     get_unused_symbol_count() const
986     {
987       gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
988       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
989     }
990 
991     // Return the input file offset for archive member N -- for archives only.
992     unsigned int
get_member_offset(unsigned int n)993     get_member_offset(unsigned int n) const
994     {
995       gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
996       return Swap32::readval(this->inputs_->p_ + this->info_offset_
997 			     + 8 + n * 4);
998     }
999 
1000     // Return the Nth unused global symbol -- for archives only.
1001     const char*
get_unused_symbol(unsigned int n)1002     get_unused_symbol(unsigned int n) const
1003     {
1004       gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
1005       unsigned int member_count = this->get_member_count();
1006       unsigned int offset = Swap32::readval(this->inputs_->p_
1007 					    + this->info_offset_ + 8
1008 					    + member_count * 4
1009 					    + n * 4);
1010       return this->inputs_->get_string(offset);
1011     }
1012 
1013     // Information about an input section.
1014     struct Input_section_info
1015     {
1016       const char* name;
1017       unsigned int output_shndx;
1018       off_t sh_offset;
1019       off_t sh_size;
1020     };
1021 
1022     // Return info about the Nth input section -- for objects only.
1023     Input_section_info
get_input_section(unsigned int n)1024     get_input_section(unsigned int n) const
1025     {
1026       Input_section_info info;
1027       const unsigned char* p = (this->inputs_->p_
1028 				+ this->info_offset_
1029 				+ this->object_info_size
1030 				+ n * this->input_section_entry_size);
1031       unsigned int name_offset = Swap32::readval(p);
1032       info.name = this->inputs_->get_string(name_offset);
1033       info.output_shndx = Swap32::readval(p + 4);
1034       info.sh_offset = Swap::readval(p + 8);
1035       info.sh_size = Swap::readval(p + 8 + size / 8);
1036       return info;
1037     }
1038 
1039     // Return info about the Nth global symbol -- for objects only.
1040     Incremental_global_symbol_reader<big_endian>
get_global_symbol_reader(unsigned int n)1041     get_global_symbol_reader(unsigned int n) const
1042     {
1043       gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
1044 		  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
1045       unsigned int section_count = this->get_input_section_count();
1046       const unsigned char* p = (this->inputs_->p_
1047 				+ this->info_offset_
1048 				+ this->object_info_size
1049 				+ section_count * this->input_section_entry_size
1050 				+ n * this->global_sym_entry_size);
1051       return Incremental_global_symbol_reader<big_endian>(p);
1052     }
1053 
1054     // Return the signature of the Nth comdat group -- for objects only.
1055     const char*
get_comdat_group_signature(unsigned int n)1056     get_comdat_group_signature(unsigned int n) const
1057     {
1058       unsigned int section_count = this->get_input_section_count();
1059       unsigned int symbol_count = this->get_global_symbol_count();
1060       const unsigned char* p = (this->inputs_->p_
1061 				+ this->info_offset_
1062 				+ this->object_info_size
1063 				+ section_count * this->input_section_entry_size
1064 				+ symbol_count * this->global_sym_entry_size
1065 				+ n * 4);
1066       unsigned int name_offset = Swap32::readval(p);
1067       return this->inputs_->get_string(name_offset);
1068     }
1069 
1070     // Return the output symbol index for the Nth global symbol -- for shared
1071     // libraries only.  Sets *IS_DEF to TRUE if the symbol is defined in this
1072     // input file.  Sets *IS_COPY to TRUE if the symbol was copied from this
1073     // input file with a COPY relocation.
1074     unsigned int
get_output_symbol_index(unsigned int n,bool * is_def,bool * is_copy)1075     get_output_symbol_index(unsigned int n, bool* is_def, bool* is_copy)
1076     {
1077       gold_assert(this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
1078       const unsigned char* p = (this->inputs_->p_
1079 				+ this->info_offset_ + 8
1080 				+ n * 4);
1081       unsigned int output_symndx = Swap32::readval(p);
1082       unsigned int flags = output_symndx >> INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
1083       output_symndx &= ((1U << INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT) - 1);
1084       switch (flags)
1085 	{
1086 	  case INCREMENTAL_SHLIB_SYM_DEF:
1087 	    *is_def = true;
1088 	    *is_copy = false;
1089 	    break;
1090 	  case INCREMENTAL_SHLIB_SYM_COPY:
1091 	    *is_def = true;
1092 	    *is_copy = true;
1093 	    break;
1094 	  default:
1095 	    *is_def = false;
1096 	    *is_copy = false;
1097 	}
1098       return output_symndx;
1099     }
1100 
1101    private:
1102     // The reader instance for the containing section.
1103     const Incremental_inputs_reader* inputs_;
1104     // The flags, including the type of input file.
1105     unsigned int flags_;
1106     // Section offset to the input file entry.
1107     unsigned int offset_;
1108     // Section offset to the supplemental info for the input file.
1109     unsigned int info_offset_;
1110   };
1111 
1112   // Return the offset of an input file entry given its index N.
1113   unsigned int
input_file_offset(unsigned int n)1114   input_file_offset(unsigned int n) const
1115   {
1116     gold_assert(n < this->input_file_count_);
1117     return this->header_size + n * this->input_entry_size;
1118   }
1119 
1120   // Return the index of an input file entry given its OFFSET.
1121   unsigned int
input_file_index(unsigned int offset)1122   input_file_index(unsigned int offset) const
1123   {
1124     int n = ((offset - this->header_size) / this->input_entry_size);
1125     gold_assert(input_file_offset(n) == offset);
1126     return n;
1127   }
1128 
1129   // Return a reader for the Nth input file entry.
1130   Incremental_input_entry_reader
input_file(unsigned int n)1131   input_file(unsigned int n) const
1132   { return Incremental_input_entry_reader(this, this->input_file_offset(n)); }
1133 
1134   // Return a reader for the input file entry at OFFSET.
1135   Incremental_input_entry_reader
input_file_at_offset(unsigned int offset)1136   input_file_at_offset(unsigned int offset) const
1137   {
1138     gold_assert(offset < (this->header_size
1139 			  + this->input_file_count_ * this->input_entry_size));
1140     return Incremental_input_entry_reader(this, offset);
1141   }
1142 
1143   // Return a reader for the global symbol info at OFFSET.
1144   Incremental_global_symbol_reader<big_endian>
global_symbol_reader_at_offset(unsigned int offset)1145   global_symbol_reader_at_offset(unsigned int offset) const
1146   {
1147     const unsigned char* p = this->p_ + offset;
1148     return Incremental_global_symbol_reader<big_endian>(p);
1149   }
1150 
1151  private:
1152   // Lookup a string in the ELF string table.
get_string(unsigned int offset)1153   const char* get_string(unsigned int offset) const
1154   {
1155     const char* s;
1156     if (this->strtab_.get_c_string(offset, &s))
1157       return s;
1158     return NULL;
1159   }
1160 
1161   // Base address of the .gnu_incremental_inputs section.
1162   const unsigned char* p_;
1163   // The associated ELF string table.
1164   elfcpp::Elf_strtab strtab_;
1165   // The number of input file entries in this section.
1166   unsigned int input_file_count_;
1167 };
1168 
1169 // Reader class for the .gnu_incremental_symtab section.
1170 
1171 template<bool big_endian>
1172 class Incremental_symtab_reader
1173 {
1174  public:
Incremental_symtab_reader()1175   Incremental_symtab_reader()
1176     : p_(NULL), len_(0)
1177   { }
1178 
Incremental_symtab_reader(const unsigned char * p,off_t len)1179   Incremental_symtab_reader(const unsigned char* p, off_t len)
1180     : p_(p), len_(len)
1181   { }
1182 
1183   // Return the count of symbols in this section.
1184   unsigned int
symbol_count()1185   symbol_count() const
1186   { return static_cast<unsigned int>(this->len_ / 4); }
1187 
1188   // Return the list head for symbol table entry N.
1189   unsigned int
get_list_head(unsigned int n)1190   get_list_head(unsigned int n) const
1191   { return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4 * n); }
1192 
1193  private:
1194   // Base address of the .gnu_incremental_relocs section.
1195   const unsigned char* p_;
1196   // Size of the section.
1197   off_t len_;
1198 };
1199 
1200 // Reader class for the .gnu_incremental_relocs section.
1201 
1202 template<int size, bool big_endian>
1203 class Incremental_relocs_reader
1204 {
1205  private:
1206   // Size of each field.
1207   static const unsigned int field_size = size / 8;
1208 
1209  public:
1210   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1211   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
1212 
1213   // Size of each entry.
1214   static const unsigned int reloc_size = 8 + 2 * field_size;
1215 
Incremental_relocs_reader()1216   Incremental_relocs_reader()
1217     : p_(NULL), len_(0)
1218   { }
1219 
Incremental_relocs_reader(const unsigned char * p,off_t len)1220   Incremental_relocs_reader(const unsigned char* p, off_t len)
1221     : p_(p), len_(len)
1222   { }
1223 
1224   // Return the count of relocations in this section.
1225   unsigned int
reloc_count()1226   reloc_count() const
1227   { return static_cast<unsigned int>(this->len_ / reloc_size); }
1228 
1229   // Return the relocation type for relocation entry at offset OFF.
1230   unsigned int
get_r_type(unsigned int off)1231   get_r_type(unsigned int off) const
1232   { return elfcpp::Swap<32, big_endian>::readval(this->p_ + off); }
1233 
1234   // Return the output section index for relocation entry at offset OFF.
1235   unsigned int
get_r_shndx(unsigned int off)1236   get_r_shndx(unsigned int off) const
1237   { return elfcpp::Swap<32, big_endian>::readval(this->p_ + off + 4); }
1238 
1239   // Return the output section offset for relocation entry at offset OFF.
1240   Address
get_r_offset(unsigned int off)1241   get_r_offset(unsigned int off) const
1242   { return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8); }
1243 
1244   // Return the addend for relocation entry at offset OFF.
1245   Addend
get_r_addend(unsigned int off)1246   get_r_addend(unsigned int off) const
1247   {
1248     return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8
1249 						   + this->field_size);
1250   }
1251 
1252   // Return a pointer to the relocation entry at offset OFF.
1253   const unsigned char*
data(unsigned int off)1254   data(unsigned int off) const
1255   { return this->p_ + off; }
1256 
1257  private:
1258   // Base address of the .gnu_incremental_relocs section.
1259   const unsigned char* p_;
1260   // Size of the section.
1261   off_t len_;
1262 };
1263 
1264 // Reader class for the .gnu_incremental_got_plt section.
1265 
1266 template<bool big_endian>
1267 class Incremental_got_plt_reader
1268 {
1269  public:
Incremental_got_plt_reader()1270   Incremental_got_plt_reader()
1271     : p_(NULL), got_count_(0), got_desc_p_(NULL), plt_desc_p_(NULL)
1272   { }
1273 
Incremental_got_plt_reader(const unsigned char * p)1274   Incremental_got_plt_reader(const unsigned char* p) : p_(p)
1275   {
1276     this->got_count_ = elfcpp::Swap<32, big_endian>::readval(p);
1277     this->got_desc_p_ = p + 8 + ((this->got_count_ + 3) & ~3);
1278     this->plt_desc_p_ = this->got_desc_p_ + this->got_count_ * 8;
1279   }
1280 
1281   // Return the GOT entry count.
1282   unsigned int
get_got_entry_count()1283   get_got_entry_count() const
1284   {
1285     return this->got_count_;
1286   }
1287 
1288   // Return the PLT entry count.
1289   unsigned int
get_plt_entry_count()1290   get_plt_entry_count() const
1291   {
1292     return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4);
1293   }
1294 
1295   // Return the GOT type for GOT entry N.
1296   unsigned int
get_got_type(unsigned int n)1297   get_got_type(unsigned int n)
1298   {
1299     return this->p_[8 + n];
1300   }
1301 
1302   // Return the symbol index for GOT entry N.
1303   unsigned int
get_got_symndx(unsigned int n)1304   get_got_symndx(unsigned int n)
1305   {
1306     return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 8);
1307   }
1308 
1309   // Return the input file index for GOT entry N.
1310   unsigned int
get_got_input_index(unsigned int n)1311   get_got_input_index(unsigned int n)
1312   {
1313     return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 8 + 4);
1314   }
1315 
1316   // Return the PLT descriptor for PLT entry N.
1317   unsigned int
get_plt_desc(unsigned int n)1318   get_plt_desc(unsigned int n)
1319   {
1320     return elfcpp::Swap<32, big_endian>::readval(this->plt_desc_p_ + n * 4);
1321   }
1322 
1323  private:
1324   // Base address of the .gnu_incremental_got_plt section.
1325   const unsigned char* p_;
1326   // GOT entry count.
1327   unsigned int got_count_;
1328   // Base address of the GOT descriptor array.
1329   const unsigned char* got_desc_p_;
1330   // Base address of the PLT descriptor array.
1331   const unsigned char* plt_desc_p_;
1332 };
1333 
1334 // An object representing the ELF file we edit during an incremental build.
1335 // Similar to Object or Dynobj, but operates on Output_file and contains
1336 // methods to support incremental updating. This is the abstract parent class
1337 // implemented in Sized_incremental_binary<size, big_endian> for a specific
1338 // endianness and size.
1339 
1340 class Incremental_binary
1341 {
1342  public:
Incremental_binary(Output_file * output,Target *)1343   Incremental_binary(Output_file* output, Target* /*target*/)
1344     : input_args_map_(), library_map_(), script_map_(),
1345       output_(output)
1346   { }
1347 
1348   virtual
~Incremental_binary()1349   ~Incremental_binary()
1350   { }
1351 
1352   // Check the .gnu_incremental_inputs section to see whether an incremental
1353   // build is possible.
1354   bool
check_inputs(const Command_line & cmdline,Incremental_inputs * incremental_inputs)1355   check_inputs(const Command_line& cmdline,
1356 	       Incremental_inputs* incremental_inputs)
1357   { return this->do_check_inputs(cmdline, incremental_inputs); }
1358 
1359   // Report an error.
1360   void
1361   error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
1362 
1363   // Proxy class for a sized Incremental_input_entry_reader.
1364 
1365   class Input_reader
1366   {
1367    public:
Input_reader()1368     Input_reader()
1369     { }
1370 
1371     virtual
~Input_reader()1372     ~Input_reader()
1373     { }
1374 
1375     const char*
filename()1376     filename() const
1377     { return this->do_filename(); }
1378 
1379     Timespec
get_mtime()1380     get_mtime() const
1381     { return this->do_get_mtime(); }
1382 
1383     Incremental_input_type
type()1384     type() const
1385     { return this->do_type(); }
1386 
1387     unsigned int
arg_serial()1388     arg_serial() const
1389     { return this->do_arg_serial(); }
1390 
1391     unsigned int
get_unused_symbol_count()1392     get_unused_symbol_count() const
1393     { return this->do_get_unused_symbol_count(); }
1394 
1395     const char*
get_unused_symbol(unsigned int n)1396     get_unused_symbol(unsigned int n) const
1397     { return this->do_get_unused_symbol(n); }
1398 
1399    protected:
1400     virtual const char*
1401     do_filename() const = 0;
1402 
1403     virtual Timespec
1404     do_get_mtime() const = 0;
1405 
1406     virtual Incremental_input_type
1407     do_type() const = 0;
1408 
1409     virtual unsigned int
1410     do_arg_serial() const = 0;
1411 
1412     virtual unsigned int
1413     do_get_unused_symbol_count() const = 0;
1414 
1415     virtual const char*
1416     do_get_unused_symbol(unsigned int n) const = 0;
1417   };
1418 
1419   // Return the number of input files.
1420   unsigned int
input_file_count()1421   input_file_count() const
1422   { return this->do_input_file_count(); }
1423 
1424   // Return an Input_reader for input file N.
1425   const Input_reader*
get_input_reader(unsigned int n)1426   get_input_reader(unsigned int n) const
1427   { return this->do_get_input_reader(n); }
1428 
1429   // Return TRUE if the input file N has changed since the last link.
1430   bool
file_has_changed(unsigned int n)1431   file_has_changed(unsigned int n) const
1432   { return this->do_file_has_changed(n); }
1433 
1434   // Return the Input_argument for input file N.  Returns NULL if
1435   // the Input_argument is not available.
1436   const Input_argument*
get_input_argument(unsigned int n)1437   get_input_argument(unsigned int n) const
1438   {
1439     const Input_reader* input_file = this->do_get_input_reader(n);
1440     unsigned int arg_serial = input_file->arg_serial();
1441     if (arg_serial == 0 || arg_serial > this->input_args_map_.size())
1442       return NULL;
1443     return this->input_args_map_[arg_serial - 1];
1444   }
1445 
1446   // Return an Incremental_library for the given input file.
1447   Incremental_library*
get_library(unsigned int n)1448   get_library(unsigned int n) const
1449   { return this->library_map_[n]; }
1450 
1451   // Return a Script_info for the given input file.
1452   Script_info*
get_script_info(unsigned int n)1453   get_script_info(unsigned int n) const
1454   { return this->script_map_[n]; }
1455 
1456   // Initialize the layout of the output file based on the existing
1457   // output file.
1458   void
init_layout(Layout * layout)1459   init_layout(Layout* layout)
1460   { this->do_init_layout(layout); }
1461 
1462   // Mark regions of the input file that must be kept unchanged.
1463   void
reserve_layout(unsigned int input_file_index)1464   reserve_layout(unsigned int input_file_index)
1465   { this->do_reserve_layout(input_file_index); }
1466 
1467   // Process the GOT and PLT entries from the existing output file.
1468   void
process_got_plt(Symbol_table * symtab,Layout * layout)1469   process_got_plt(Symbol_table* symtab, Layout* layout)
1470   { this->do_process_got_plt(symtab, layout); }
1471 
1472   // Emit COPY relocations from the existing output file.
1473   void
emit_copy_relocs(Symbol_table * symtab)1474   emit_copy_relocs(Symbol_table* symtab)
1475   { this->do_emit_copy_relocs(symtab); }
1476 
1477   // Apply incremental relocations for symbols whose values have changed.
1478   void
apply_incremental_relocs(const Symbol_table * symtab,Layout * layout,Output_file * of)1479   apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
1480 			   Output_file* of)
1481   { this->do_apply_incremental_relocs(symtab, layout, of); }
1482 
1483   // Functions and types for the elfcpp::Elf_file interface.  This
1484   // permit us to use Incremental_binary as the File template parameter for
1485   // elfcpp::Elf_file.
1486 
1487   // The View class is returned by view.  It must support a single
1488   // method, data().  This is trivial, because Output_file::get_output_view
1489   // does what we need.
1490   class View
1491   {
1492    public:
View(const unsigned char * p)1493     View(const unsigned char* p)
1494       : p_(p)
1495     { }
1496 
1497     const unsigned char*
data()1498     data() const
1499     { return this->p_; }
1500 
1501    private:
1502     const unsigned char* p_;
1503   };
1504 
1505   // Return a View.
1506   View
view(off_t file_offset,section_size_type data_size)1507   view(off_t file_offset, section_size_type data_size)
1508   { return View(this->output_->get_input_view(file_offset, data_size)); }
1509 
1510   // A location in the file.
1511   struct Location
1512   {
1513     off_t file_offset;
1514     off_t data_size;
1515 
LocationLocation1516     Location(off_t fo, section_size_type ds)
1517       : file_offset(fo), data_size(ds)
1518     { }
1519 
LocationLocation1520     Location()
1521       : file_offset(0), data_size(0)
1522     { }
1523   };
1524 
1525   // Get a View given a Location.
1526   View
view(Location loc)1527   view(Location loc)
1528   { return View(this->view(loc.file_offset, loc.data_size)); }
1529 
1530   // Return the Output_file.
1531   Output_file*
output_file()1532   output_file()
1533   { return this->output_; }
1534 
1535  protected:
1536   // Check the .gnu_incremental_inputs section to see whether an incremental
1537   // build is possible.
1538   virtual bool
1539   do_check_inputs(const Command_line& cmdline,
1540 		  Incremental_inputs* incremental_inputs) = 0;
1541 
1542   // Return TRUE if input file N has changed since the last incremental link.
1543   virtual bool
1544   do_file_has_changed(unsigned int n) const = 0;
1545 
1546   // Initialize the layout of the output file based on the existing
1547   // output file.
1548   virtual void
1549   do_init_layout(Layout* layout) = 0;
1550 
1551   // Mark regions of the input file that must be kept unchanged.
1552   virtual void
1553   do_reserve_layout(unsigned int input_file_index) = 0;
1554 
1555   // Process the GOT and PLT entries from the existing output file.
1556   virtual void
1557   do_process_got_plt(Symbol_table* symtab, Layout* layout) = 0;
1558 
1559   // Emit COPY relocations from the existing output file.
1560   virtual void
1561   do_emit_copy_relocs(Symbol_table* symtab) = 0;
1562 
1563   // Apply incremental relocations for symbols whose values have changed.
1564   virtual void
1565   do_apply_incremental_relocs(const Symbol_table*, Layout*, Output_file*) = 0;
1566 
1567   virtual unsigned int
1568   do_input_file_count() const = 0;
1569 
1570   virtual const Input_reader*
1571   do_get_input_reader(unsigned int) const = 0;
1572 
1573   // Map from input file index to Input_argument.
1574   std::vector<const Input_argument*> input_args_map_;
1575   // Map from an input file index to an Incremental_library.
1576   std::vector<Incremental_library*> library_map_;
1577   // Map from an input file index to a Script_info.
1578   std::vector<Script_info*> script_map_;
1579 
1580  private:
1581   // Edited output file object.
1582   Output_file* output_;
1583 };
1584 
1585 template<int size, bool big_endian>
1586 class Sized_relobj_incr;
1587 
1588 template<int size, bool big_endian>
1589 class Sized_incremental_binary : public Incremental_binary
1590 {
1591  public:
Sized_incremental_binary(Output_file * output,const elfcpp::Ehdr<size,big_endian> & ehdr,Target * target)1592   Sized_incremental_binary(Output_file* output,
1593                            const elfcpp::Ehdr<size, big_endian>& ehdr,
1594                            Target* target)
1595     : Incremental_binary(output, target), elf_file_(this, ehdr),
1596       input_objects_(), section_map_(), symbol_map_(), copy_relocs_(),
1597       main_symtab_loc_(), main_strtab_loc_(), has_incremental_info_(false),
1598       inputs_reader_(), symtab_reader_(), relocs_reader_(), got_plt_reader_(),
1599       input_entry_readers_()
1600   { this->setup_readers(); }
1601 
1602   // Returns TRUE if the file contains incremental info.
1603   bool
has_incremental_info()1604   has_incremental_info() const
1605   { return this->has_incremental_info_; }
1606 
1607   // Record a pointer to the object for input file N.
1608   void
set_input_object(unsigned int n,Sized_relobj_incr<size,big_endian> * obj)1609   set_input_object(unsigned int n,
1610 		   Sized_relobj_incr<size, big_endian>* obj)
1611   { this->input_objects_[n] = obj; }
1612 
1613   // Return a pointer to the object for input file N.
1614   Sized_relobj_incr<size, big_endian>*
input_object(unsigned int n)1615   input_object(unsigned int n) const
1616   {
1617     gold_assert(n < this->input_objects_.size());
1618     return this->input_objects_[n];
1619   }
1620 
1621   // Return the Output_section for section index SHNDX.
1622   Output_section*
output_section(unsigned int shndx)1623   output_section(unsigned int shndx)
1624   { return this->section_map_[shndx]; }
1625 
1626   // Map a symbol table entry from the base file to the output symbol table.
1627   // SYMNDX is relative to the first forced-local or global symbol in the
1628   // input file symbol table.
1629   void
add_global_symbol(unsigned int symndx,Symbol * gsym)1630   add_global_symbol(unsigned int symndx, Symbol* gsym)
1631   { this->symbol_map_[symndx] = gsym; }
1632 
1633   // Map a symbol table entry from the base file to the output symbol table.
1634   // SYMNDX is relative to the first forced-local or global symbol in the
1635   // input file symbol table.
1636   Symbol*
global_symbol(unsigned int symndx)1637   global_symbol(unsigned int symndx) const
1638   { return this->symbol_map_[symndx]; }
1639 
1640   // Add a COPY relocation for a global symbol.
1641   void
add_copy_reloc(Symbol * gsym,Output_section * os,off_t offset)1642   add_copy_reloc(Symbol* gsym, Output_section* os, off_t offset)
1643   { this->copy_relocs_.push_back(Copy_reloc(gsym, os, offset)); }
1644 
1645   // Readers for the incremental info sections.
1646 
1647   const Incremental_inputs_reader<size, big_endian>&
inputs_reader()1648   inputs_reader() const
1649   { return this->inputs_reader_; }
1650 
1651   const Incremental_symtab_reader<big_endian>&
symtab_reader()1652   symtab_reader() const
1653   { return this->symtab_reader_; }
1654 
1655   const Incremental_relocs_reader<size, big_endian>&
relocs_reader()1656   relocs_reader() const
1657   { return this->relocs_reader_; }
1658 
1659   const Incremental_got_plt_reader<big_endian>&
got_plt_reader()1660   got_plt_reader() const
1661   { return this->got_plt_reader_; }
1662 
1663   void
1664   get_symtab_view(View* symtab_view, unsigned int* sym_count,
1665 		  elfcpp::Elf_strtab* strtab);
1666 
1667  protected:
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   virtual bool
1673   do_check_inputs(const Command_line& cmdline,
1674 		  Incremental_inputs* incremental_inputs);
1675 
1676   // Return TRUE if input file N has changed since the last incremental link.
1677   virtual bool
1678   do_file_has_changed(unsigned int n) const;
1679 
1680   // Initialize the layout of the output file based on the existing
1681   // output file.
1682   virtual void
1683   do_init_layout(Layout* layout);
1684 
1685   // Mark regions of the input file that must be kept unchanged.
1686   virtual void
1687   do_reserve_layout(unsigned int input_file_index);
1688 
1689   // Process the GOT and PLT entries from the existing output file.
1690   virtual void
1691   do_process_got_plt(Symbol_table* symtab, Layout* layout);
1692 
1693   // Emit COPY relocations from the existing output file.
1694   virtual void
1695   do_emit_copy_relocs(Symbol_table* symtab);
1696 
1697   // Apply incremental relocations for symbols whose values have changed.
1698   virtual void
1699   do_apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
1700 			      Output_file* of);
1701 
1702   // Proxy class for a sized Incremental_input_entry_reader.
1703 
1704   class Sized_input_reader : public Input_reader
1705   {
1706    public:
Sized_input_reader(Input_entry_reader r)1707     Sized_input_reader(Input_entry_reader r)
1708       : Input_reader(), reader_(r)
1709     { }
1710 
1711     virtual
~Sized_input_reader()1712     ~Sized_input_reader()
1713     { }
1714 
1715    private:
1716     const char*
do_filename()1717     do_filename() const
1718     { return this->reader_.filename(); }
1719 
1720     Timespec
do_get_mtime()1721     do_get_mtime() const
1722     { return this->reader_.get_mtime(); }
1723 
1724     Incremental_input_type
do_type()1725     do_type() const
1726     { return this->reader_.type(); }
1727 
1728     unsigned int
do_arg_serial()1729     do_arg_serial() const
1730     { return this->reader_.arg_serial(); }
1731 
1732     unsigned int
do_get_unused_symbol_count()1733     do_get_unused_symbol_count() const
1734     { return this->reader_.get_unused_symbol_count(); }
1735 
1736     const char*
do_get_unused_symbol(unsigned int n)1737     do_get_unused_symbol(unsigned int n) const
1738     { return this->reader_.get_unused_symbol(n); }
1739 
1740     Input_entry_reader reader_;
1741   };
1742 
1743   virtual unsigned int
do_input_file_count()1744   do_input_file_count() const
1745   { return this->inputs_reader_.input_file_count(); }
1746 
1747   virtual const Input_reader*
do_get_input_reader(unsigned int n)1748   do_get_input_reader(unsigned int n) const
1749   {
1750     gold_assert(n < this->input_entry_readers_.size());
1751     return &this->input_entry_readers_[n];
1752   }
1753 
1754  private:
1755   // List of symbols that need COPY relocations.
1756   struct Copy_reloc
1757   {
Copy_relocCopy_reloc1758     Copy_reloc(Symbol* sym, Output_section* os, off_t off)
1759       : symbol(sym), output_section(os), offset(off)
1760     { }
1761 
1762     // The global symbol to copy.
1763     Symbol* symbol;
1764     // The output section into which the symbol was copied.
1765     Output_section* output_section;
1766     // The offset within that output section.
1767     off_t offset;
1768   };
1769   typedef std::vector<Copy_reloc> Copy_relocs;
1770 
1771   bool
1772   find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
1773 				   unsigned int* p_symtab_shndx,
1774 				   unsigned int* p_relocs_shndx,
1775 				   unsigned int* p_got_plt_shndx,
1776 				   unsigned int* p_strtab_shndx);
1777 
1778   void
1779   setup_readers();
1780 
1781   // Output as an ELF file.
1782   elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
1783 
1784   // Vector of pointers to the input objects for the unchanged files.
1785   // For replaced files, the corresponding pointer is NULL.
1786   std::vector<Sized_relobj_incr<size, big_endian>*> input_objects_;
1787 
1788   // Map section index to an Output_section in the updated layout.
1789   std::vector<Output_section*> section_map_;
1790 
1791   // Map global symbols from the input file to the symbol table.
1792   std::vector<Symbol*> symbol_map_;
1793 
1794   // List of symbols that need COPY relocations.
1795   Copy_relocs copy_relocs_;
1796 
1797   // Locations of the main symbol table and symbol string table.
1798   Location main_symtab_loc_;
1799   Location main_strtab_loc_;
1800 
1801   // Readers for the incremental info sections.
1802   bool has_incremental_info_;
1803   Incremental_inputs_reader<size, big_endian> inputs_reader_;
1804   Incremental_symtab_reader<big_endian> symtab_reader_;
1805   Incremental_relocs_reader<size, big_endian> relocs_reader_;
1806   Incremental_got_plt_reader<big_endian> got_plt_reader_;
1807   std::vector<Sized_input_reader> input_entry_readers_;
1808 };
1809 
1810 // An incremental Relobj.  This class represents a relocatable object
1811 // that has not changed since the last incremental link, and whose contents
1812 // can be used directly from the base file.
1813 
1814 template<int size, bool big_endian>
1815 class Sized_relobj_incr : public Sized_relobj<size, big_endian>
1816 {
1817  public:
1818   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1819   typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
1820 
1821   Sized_relobj_incr(const std::string& name,
1822 		    Sized_incremental_binary<size, big_endian>* ibase,
1823 		    unsigned int input_file_index);
1824 
1825  private:
1826   // For convenience.
1827   typedef Sized_relobj_incr<size, big_endian> This;
1828   static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1829 
1830   typedef typename Sized_relobj<size, big_endian>::Output_sections
1831       Output_sections;
1832   typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
1833   typedef typename Inputs_reader::Incremental_input_entry_reader
1834       Input_entry_reader;
1835 
1836   // A local symbol.
1837   struct Local_symbol
1838   {
Local_symbolLocal_symbol1839     Local_symbol(const char* name_, Address value_, unsigned int size_,
1840 		 unsigned int shndx_, unsigned int type_,
1841 		 bool needs_dynsym_entry_)
1842       : st_value(value_), name(name_), st_size(size_), st_shndx(shndx_),
1843 	st_type(type_), output_dynsym_index(0),
1844 	needs_dynsym_entry(needs_dynsym_entry_)
1845     { }
1846     // The symbol value.
1847     Address st_value;
1848     // The symbol name.  This points to the stringpool entry.
1849     const char* name;
1850     // The symbol size.
1851     unsigned int st_size;
1852     // The output section index.
1853     unsigned int st_shndx : 28;
1854     // The symbol type.
1855     unsigned int st_type : 4;
1856     // The index of the symbol in the output dynamic symbol table.
1857     unsigned int output_dynsym_index : 31;
1858     // TRUE if the symbol needs to appear in the dynamic symbol table.
1859     unsigned int needs_dynsym_entry : 1;
1860   };
1861 
1862   // Return TRUE if this is an incremental (unchanged) input file.
1863   bool
do_is_incremental()1864   do_is_incremental() const
1865   { return true; }
1866 
1867   // Return the last modified time of the file.
1868   Timespec
do_get_mtime()1869   do_get_mtime()
1870   { return this->input_reader_.get_mtime(); }
1871 
1872   // Read the symbols.
1873   void
1874   do_read_symbols(Read_symbols_data*);
1875 
1876   // Lay out the input sections.
1877   void
1878   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1879 
1880   // Layout sections whose layout was deferred while waiting for
1881   // input files from a plugin.
1882   void
1883   do_layout_deferred_sections(Layout*);
1884 
1885   // Add the symbols to the symbol table.
1886   void
1887   do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
1888 
1889   Archive::Should_include
1890   do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
1891                            std::string* why);
1892 
1893   // Iterate over global symbols, calling a visitor class V for each.
1894   void
1895   do_for_all_global_symbols(Read_symbols_data* sd,
1896 			    Library_base::Symbol_visitor_base* v);
1897 
1898   // Get the size of a section.
1899   uint64_t
1900   do_section_size(unsigned int shndx);
1901 
1902   // Get the name of a section.
1903   std::string
1904   do_section_name(unsigned int shndx) const;
1905 
1906   // Return a view of the contents of a section.
1907   const unsigned char*
1908   do_section_contents(unsigned int shndx, section_size_type* plen,
1909 		      bool cache);
1910 
1911   // Return section flags.
1912   uint64_t
1913   do_section_flags(unsigned int shndx);
1914 
1915   // Return section entsize.
1916   uint64_t
1917   do_section_entsize(unsigned int shndx);
1918 
1919   // Return section address.
1920   uint64_t
1921   do_section_address(unsigned int shndx);
1922 
1923   // Return section type.
1924   unsigned int
1925   do_section_type(unsigned int shndx);
1926 
1927   // Return the section link field.
1928   unsigned int
1929   do_section_link(unsigned int shndx);
1930 
1931   // Return the section link field.
1932   unsigned int
1933   do_section_info(unsigned int shndx);
1934 
1935   // Return the section alignment.
1936   uint64_t
1937   do_section_addralign(unsigned int shndx);
1938 
1939   // Return the Xindex structure to use.
1940   Xindex*
1941   do_initialize_xindex();
1942 
1943   // Get symbol counts.
1944   void
1945   do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1946 
1947   // Get global symbols.
1948   const Symbols*
do_get_global_symbols()1949   do_get_global_symbols() const
1950   { return &this->symbols_; }
1951 
1952   // Return the value of a local symbol.
1953   uint64_t
do_local_symbol_value(unsigned int,uint64_t)1954   do_local_symbol_value(unsigned int, uint64_t) const
1955   { gold_unreachable(); }
1956 
1957   unsigned int
do_local_plt_offset(unsigned int)1958   do_local_plt_offset(unsigned int) const
1959   { gold_unreachable(); }
1960 
1961   bool
do_local_is_tls(unsigned int)1962   do_local_is_tls(unsigned int) const
1963   { gold_unreachable(); }
1964 
1965   // Return the number of local symbols.
1966   unsigned int
do_local_symbol_count()1967   do_local_symbol_count() const
1968   { return this->local_symbol_count_; }
1969 
1970   // Return the number of local symbols in the output symbol table.
1971   unsigned int
do_output_local_symbol_count()1972   do_output_local_symbol_count() const
1973   { return this->local_symbol_count_; }
1974 
1975   // Return the file offset for local symbols in the output symbol table.
1976   off_t
do_local_symbol_offset()1977   do_local_symbol_offset() const
1978   { return this->local_symbol_offset_; }
1979 
1980   // Read the relocs.
1981   void
1982   do_read_relocs(Read_relocs_data*);
1983 
1984   // Process the relocs to find list of referenced sections. Used only
1985   // during garbage collection.
1986   void
1987   do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1988 
1989   // Scan the relocs and adjust the symbol table.
1990   void
1991   do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1992 
1993   // Count the local symbols.
1994   void
1995   do_count_local_symbols(Stringpool_template<char>*,
1996 			 Stringpool_template<char>*);
1997 
1998   // Finalize the local symbols.
1999   unsigned int
2000   do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
2001 
2002   // Set the offset where local dynamic symbol information will be stored.
2003   unsigned int
2004   do_set_local_dynsym_indexes(unsigned int);
2005 
2006   // Set the offset where local dynamic symbol information will be stored.
2007   unsigned int
2008   do_set_local_dynsym_offset(off_t);
2009 
2010   // Relocate the input sections and write out the local symbols.
2011   void
2012   do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
2013 
2014   // Set the offset of a section.
2015   void
2016   do_set_section_offset(unsigned int shndx, uint64_t off);
2017 
2018   // The Incremental_binary base file.
2019   Sized_incremental_binary<size, big_endian>* ibase_;
2020   // The index of the object in the input file list.
2021   unsigned int input_file_index_;
2022   // The reader for the input file.
2023   Input_entry_reader input_reader_;
2024   // The number of local symbols.
2025   unsigned int local_symbol_count_;
2026   // The number of local symbols which go into the output file's dynamic
2027   // symbol table.
2028   unsigned int output_local_dynsym_count_;
2029   // This starting symbol index in the output symbol table.
2030   unsigned int local_symbol_index_;
2031   // The file offset for local symbols in the output symbol table.
2032   unsigned int local_symbol_offset_;
2033   // The file offset for local symbols in the output symbol table.
2034   unsigned int local_dynsym_offset_;
2035   // The entries in the symbol table for the external symbols.
2036   Symbols symbols_;
2037   // Number of symbols defined in object file itself.
2038   size_t defined_count_;
2039   // The offset of the first incremental relocation for this object.
2040   unsigned int incr_reloc_offset_;
2041   // The number of incremental relocations for this object.
2042   unsigned int incr_reloc_count_;
2043   // The index of the first incremental relocation for this object in the
2044   // updated output file.
2045   unsigned int incr_reloc_output_index_;
2046   // A copy of the incremental relocations from this object.
2047   unsigned char* incr_relocs_;
2048   // The local symbols.
2049   std::vector<Local_symbol> local_symbols_;
2050 };
2051 
2052 // An incremental Dynobj.  This class represents a shared object that has
2053 // not changed since the last incremental link, and whose contents can be
2054 // used directly from the base file.
2055 
2056 template<int size, bool big_endian>
2057 class Sized_incr_dynobj : public Dynobj
2058 {
2059  public:
2060   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2061 
2062   static const Address invalid_address = static_cast<Address>(0) - 1;
2063 
2064   Sized_incr_dynobj(const std::string& name,
2065 		    Sized_incremental_binary<size, big_endian>* ibase,
2066 		    unsigned int input_file_index);
2067 
2068  private:
2069   typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
2070   typedef typename Inputs_reader::Incremental_input_entry_reader
2071       Input_entry_reader;
2072 
2073   // Return TRUE if this is an incremental (unchanged) input file.
2074   bool
do_is_incremental()2075   do_is_incremental() const
2076   { return true; }
2077 
2078   // Return the last modified time of the file.
2079   Timespec
do_get_mtime()2080   do_get_mtime()
2081   { return this->input_reader_.get_mtime(); }
2082 
2083   // Read the symbols.
2084   void
2085   do_read_symbols(Read_symbols_data*);
2086 
2087   // Lay out the input sections.
2088   void
2089   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
2090 
2091   // Add the symbols to the symbol table.
2092   void
2093   do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
2094 
2095   Archive::Should_include
2096   do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
2097                            std::string* why);
2098 
2099   // Iterate over global symbols, calling a visitor class V for each.
2100   void
2101   do_for_all_global_symbols(Read_symbols_data* sd,
2102 			    Library_base::Symbol_visitor_base* v);
2103 
2104   // Iterate over local symbols, calling a visitor class V for each GOT offset
2105   // associated with a local symbol.
2106   void
2107   do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
2108 
2109   // Get the size of a section.
2110   uint64_t
2111   do_section_size(unsigned int shndx);
2112 
2113   // Get the name of a section.
2114   std::string
2115   do_section_name(unsigned int shndx) const;
2116 
2117   // Return a view of the contents of a section.
2118   const unsigned char*
2119   do_section_contents(unsigned int shndx, section_size_type* plen,
2120 		      bool cache);
2121 
2122   // Return section flags.
2123   uint64_t
2124   do_section_flags(unsigned int shndx);
2125 
2126   // Return section entsize.
2127   uint64_t
2128   do_section_entsize(unsigned int shndx);
2129 
2130   // Return section address.
2131   uint64_t
2132   do_section_address(unsigned int shndx);
2133 
2134   // Return section type.
2135   unsigned int
2136   do_section_type(unsigned int shndx);
2137 
2138   // Return the section link field.
2139   unsigned int
2140   do_section_link(unsigned int shndx);
2141 
2142   // Return the section link field.
2143   unsigned int
2144   do_section_info(unsigned int shndx);
2145 
2146   // Return the section alignment.
2147   uint64_t
2148   do_section_addralign(unsigned int shndx);
2149 
2150   // Return the Xindex structure to use.
2151   Xindex*
2152   do_initialize_xindex();
2153 
2154   // Get symbol counts.
2155   void
2156   do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
2157 
2158   // Get global symbols.
2159   const Symbols*
do_get_global_symbols()2160   do_get_global_symbols() const
2161   { return &this->symbols_; }
2162 
2163   // The Incremental_binary base file.
2164   Sized_incremental_binary<size, big_endian>* ibase_;
2165   // The index of the object in the input file list.
2166   unsigned int input_file_index_;
2167   // The reader for the input file.
2168   Input_entry_reader input_reader_;
2169   // The entries in the symbol table for the external symbols.
2170   Symbols symbols_;
2171   // Number of symbols defined in object file itself.
2172   size_t defined_count_;
2173 };
2174 
2175 // Allocate an incremental object of the appropriate size and endianness.
2176 extern Object*
2177 make_sized_incremental_object(
2178     Incremental_binary* base,
2179     unsigned int input_file_index,
2180     Incremental_input_type input_type,
2181     const Incremental_binary::Input_reader* input_reader);
2182 
2183 // This class represents an Archive library (or --start-lib/--end-lib group)
2184 // that has not changed since the last incremental link.  Its contents come
2185 // from the incremental inputs entry in the base file.
2186 
2187 class Incremental_library : public Library_base
2188 {
2189  public:
Incremental_library(const char * filename,unsigned int input_file_index,const Incremental_binary::Input_reader * input_reader)2190   Incremental_library(const char* filename, unsigned int input_file_index,
2191 		      const Incremental_binary::Input_reader* input_reader)
2192     : Library_base(NULL), filename_(filename),
2193       input_file_index_(input_file_index), input_reader_(input_reader),
2194       unused_symbols_(), is_reported_(false)
2195   { }
2196 
2197   // Return the input file index.
2198   unsigned int
input_file_index()2199   input_file_index() const
2200   { return this->input_file_index_; }
2201 
2202   // Return the serial number of the input file.
2203   unsigned int
arg_serial()2204   arg_serial() const
2205   { return this->input_reader_->arg_serial(); }
2206 
2207   // Copy the unused symbols from the incremental input info.
2208   // We need to do this because we may be overwriting the incremental
2209   // input info in the base file before we write the new incremental
2210   // info.
2211   void
2212   copy_unused_symbols();
2213 
2214   // Return FALSE on the first call to indicate that the library needs
2215   // to be recorded; return TRUE subsequently.
2216   bool
is_reported()2217   is_reported()
2218   {
2219     bool was_reported = this->is_reported_;
2220     is_reported_ = true;
2221     return was_reported;
2222   }
2223 
2224  private:
2225   typedef std::vector<std::string> Symbol_list;
2226 
2227   // The file name.
2228   const std::string&
do_filename()2229   do_filename() const
2230   { return this->filename_; }
2231 
2232   // Return the modification time of the archive file.
2233   Timespec
do_get_mtime()2234   do_get_mtime()
2235   { return this->input_reader_->get_mtime(); }
2236 
2237   // Iterator for unused global symbols in the library.
2238   void
2239   do_for_all_unused_symbols(Symbol_visitor_base* v) const;
2240 
2241   // The name of the library.
2242   std::string filename_;
2243   // The input file index of this library.
2244   unsigned int input_file_index_;
2245   // A reader for the incremental input information.
2246   const Incremental_binary::Input_reader* input_reader_;
2247   // List of unused symbols defined in this library.
2248   Symbol_list unused_symbols_;
2249   // TRUE when this library has been reported to the new incremental info.
2250   bool is_reported_;
2251 };
2252 
2253 } // End namespace gold.
2254 
2255 #endif // !defined(GOLD_INCREMENTAL_H)
2256