* incremental-dump.cc (dump_incremental_inputs): Change signature
[external/binutils.git] / gold / incremental.h
1 // inremental.h -- incremental linking support for gold   -*- C++ -*-
2
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Mikolaj Zalewski <mikolajz@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #ifndef GOLD_INCREMENTAL_H
24 #define GOLD_INCREMENTAL_H
25
26 #include <map>
27 #include <vector>
28
29 #include "elfcpp_file.h"
30 #include "stringpool.h"
31 #include "workqueue.h"
32 #include "fileread.h"
33 #include "output.h"
34
35 namespace gold
36 {
37
38 class Archive;
39 class Input_argument;
40 class Incremental_inputs_checker;
41 class Incremental_script_entry;
42 class Incremental_object_entry;
43 class Incremental_archive_entry;
44 class Incremental_inputs;
45 class Incremental_binary;
46 class Object;
47
48 // Incremental input type as stored in .gnu_incremental_inputs.
49
50 enum Incremental_input_type
51 {
52   INCREMENTAL_INPUT_OBJECT = 1,
53   INCREMENTAL_INPUT_ARCHIVE_MEMBER = 2,
54   INCREMENTAL_INPUT_ARCHIVE = 3,
55   INCREMENTAL_INPUT_SHARED_LIBRARY = 4,
56   INCREMENTAL_INPUT_SCRIPT = 5
57 };
58
59 // Create an Incremental_binary object for FILE. Returns NULL is this is not
60 // possible, e.g. FILE is not an ELF file or has an unsupported target.
61
62 Incremental_binary*
63 open_incremental_binary(Output_file* file);
64
65 // Code invoked early during an incremental link that checks what files need
66 // to be relinked.
67
68 class Incremental_checker
69 {
70  public:
71   // Check if the file named OUTPUT_NAME can be linked incrementally.
72   // INCREMENTAL_INPUTS must have the canonical form of the command line
73   // and input arguments filled - at this point of linking other fields are
74   // probably not filled yet.  TODO: for inputs that don't need to be
75   // rebuilt, this function should fill the incremental input information.
76   Incremental_checker(const char* output_name,
77                       Incremental_inputs* incremental_inputs)
78     : output_name_(output_name), incremental_inputs_(incremental_inputs)
79   { }
80
81   // Analyzes the output file to check if incremental linking is possible and
82   // what files needs to be relinked.
83   bool
84   can_incrementally_link_output_file();
85
86  private:
87   // Name of the output file to analyze.
88   const char* output_name_;
89
90   // The Incremental_inputs object. At this stage of link, only the command
91   // line and inputs are filled.
92   Incremental_inputs* incremental_inputs_;
93 };
94
95 // Base class for recording each input file.
96
97 class Incremental_input_entry
98 {
99  public:
100   Incremental_input_entry(Stringpool::Key filename_key, Timespec mtime)
101     : filename_key_(filename_key), offset_(0), info_offset_(0), mtime_(mtime)
102   { }
103
104   virtual
105   ~Incremental_input_entry()
106   { }
107
108   // Return the type of input file.
109   Incremental_input_type
110   type() const
111   { return this->do_type(); }
112
113   // Set the section offset of this input file entry.
114   void
115   set_offset(unsigned int offset)
116   { this->offset_ = offset; }
117
118   // Set the section offset of the supplemental information for this entry.
119   void
120   set_info_offset(unsigned int info_offset)
121   { this->info_offset_ = info_offset; }
122
123   // Get the section offset of this input file entry.
124   unsigned int
125   get_offset() const
126   { return this->offset_; }
127
128   // Get the section offset of the supplemental information for this entry.
129   unsigned int
130   get_info_offset() const
131   { return this->info_offset_; }
132
133   // Get the stringpool key for the input filename.
134   Stringpool::Key
135   get_filename_key() const
136   { return this->filename_key_; }
137
138   // Get the modification time of the input file.
139   const Timespec&
140   get_mtime() const
141   { return this->mtime_; }
142
143   // Return a pointer to the derived Incremental_script_entry object.
144   // Return NULL for input entries that are not script files.
145   Incremental_script_entry*
146   script_entry()
147   { return this->do_script_entry(); }
148
149   // Return a pointer to the derived Incremental_object_entry object.
150   // Return NULL for input entries that are not object files.
151   Incremental_object_entry*
152   object_entry()
153   { return this->do_object_entry(); }
154
155   // Return a pointer to the derived Incremental_archive_entry object.
156   // Return NULL for input entries that are not archive files.
157   Incremental_archive_entry*
158   archive_entry()
159   { return this->do_archive_entry(); }
160
161  protected:
162   // Return the type of input file.
163   virtual Incremental_input_type
164   do_type() const = 0;
165
166   // Return a pointer to the derived Incremental_script_entry object.
167   // Return NULL for input entries that are not script files.
168   virtual Incremental_script_entry*
169   do_script_entry()
170   { return NULL; }
171
172   // Return a pointer to the derived Incremental_object_entry object.
173   // Return NULL for input entries that are not object files.
174   virtual Incremental_object_entry*
175   do_object_entry()
176   { return NULL; }
177
178   // Return a pointer to the derived Incremental_archive_entry object.
179   // Return NULL for input entries that are not archive files.
180   virtual Incremental_archive_entry*
181   do_archive_entry()
182   { return NULL; }
183
184  private:
185   // Key of the filename string in the section stringtable.
186   Stringpool::Key filename_key_;
187
188   // Offset of the entry in the output section.
189   unsigned int offset_;
190
191   // Offset of the extra information in the output section.
192   unsigned int info_offset_;
193
194   // Last modification time of the file.
195   Timespec mtime_;
196 };
197
198 // Class for recording input scripts.
199
200 class Incremental_script_entry : public Incremental_input_entry
201 {
202  public:
203   Incremental_script_entry(Stringpool::Key filename_key, Script_info* script,
204                            Timespec mtime)
205     : Incremental_input_entry(filename_key, mtime), script_(script), objects_()
206   { }
207
208   // Add a member object to the archive.
209   void
210   add_object(Incremental_input_entry* obj_entry)
211   {
212     this->objects_.push_back(obj_entry);
213   }
214
215   // Return the number of objects included by this script.
216   unsigned int
217   get_object_count()
218   { return this->objects_.size(); }
219
220   // Return the Nth object.
221   Incremental_input_entry*
222   get_object(unsigned int n)
223   {
224     gold_assert(n < this->objects_.size());
225     return this->objects_[n];
226   }
227
228  protected:
229   virtual Incremental_input_type
230   do_type() const
231   { return INCREMENTAL_INPUT_SCRIPT; }
232
233   // Return a pointer to the derived Incremental_script_entry object.
234   virtual Incremental_script_entry*
235   do_script_entry()
236   { return this; }
237
238  private:
239   // Information about the script file.
240   Script_info* script_;
241   // Objects that have been included by this script.
242   std::vector<Incremental_input_entry*> objects_;
243 };
244
245 // Class for recording input object files.
246
247 class Incremental_object_entry : public Incremental_input_entry
248 {
249  public:
250   Incremental_object_entry(Stringpool::Key filename_key, Object* obj,
251                            Timespec mtime)
252     : Incremental_input_entry(filename_key, mtime), obj_(obj),
253       is_member_(false), sections_()
254   {
255     if (!obj_->is_dynamic())
256       this->sections_.reserve(obj->shnum());
257   }
258
259   // Get the object.
260   Object*
261   object() const
262   { return this->obj_; }
263
264   // Record that this object is an archive member.
265   void
266   set_is_member()
267   { this->is_member_ = true; }
268
269   // Return true if this object is an archive member.
270   bool
271   is_member() const
272   { return this->is_member_; }
273
274   // Add an input section.
275   void
276   add_input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
277   {
278     if (shndx >= this->sections_.size())
279       this->sections_.resize(shndx + 1);
280     this->sections_[shndx].name_key = name_key;
281     this->sections_[shndx].sh_size = sh_size;
282   }
283
284   // Return the number of input sections in this object.
285   unsigned int
286   get_input_section_count() const
287   { return this->sections_.size(); }
288
289   // Return the stringpool key of the Nth input section.
290   Stringpool::Key
291   get_input_section_name_key(unsigned int n) const
292   { return this->sections_[n].name_key; }
293
294   // Return the size of the Nth input section.
295   off_t
296   get_input_section_size(unsigned int n) const
297   { return this->sections_[n].sh_size; }
298
299  protected:
300   virtual Incremental_input_type
301   do_type() const
302   {
303     return (this->is_member_
304             ? INCREMENTAL_INPUT_ARCHIVE_MEMBER
305             : (this->obj_->is_dynamic()
306                ? INCREMENTAL_INPUT_SHARED_LIBRARY
307                : INCREMENTAL_INPUT_OBJECT));
308   }
309
310   // Return a pointer to the derived Incremental_object_entry object.
311   virtual Incremental_object_entry*
312   do_object_entry()
313   { return this; }
314
315  private:
316   // The object file itself.
317   Object* obj_;
318
319   // Whether this object is an archive member.
320   bool is_member_;
321
322   // Input sections.
323   struct Input_section
324   {
325     Stringpool::Key name_key;
326     off_t sh_size;
327   };
328   std::vector<Input_section> sections_;
329 };
330
331 // Class for recording archive library input files.
332
333 class Incremental_archive_entry : public Incremental_input_entry
334 {
335  public:
336   Incremental_archive_entry(Stringpool::Key filename_key, Timespec mtime)
337     : Incremental_input_entry(filename_key, mtime), members_(), unused_syms_()
338   { }
339
340   // Add a member object to the archive.
341   void
342   add_object(Incremental_object_entry* obj_entry)
343   {
344     this->members_.push_back(obj_entry);
345     obj_entry->set_is_member();
346   }
347
348   // Add an unused global symbol to the archive.
349   void
350   add_unused_global_symbol(Stringpool::Key symbol_key)
351   { this->unused_syms_.push_back(symbol_key); }
352
353   // Return the number of member objects included in the link.
354   unsigned int
355   get_member_count()
356   { return this->members_.size(); }
357
358   // Return the Nth member object.
359   Incremental_object_entry*
360   get_member(unsigned int n)
361   { return this->members_[n]; }
362
363   // Return the number of unused global symbols in this archive.
364   unsigned int
365   get_unused_global_symbol_count()
366   { return this->unused_syms_.size(); }
367
368   // Return the Nth unused global symbol.
369   Stringpool::Key
370   get_unused_global_symbol(unsigned int n)
371   { return this->unused_syms_[n]; }
372
373  protected:
374   virtual Incremental_input_type
375   do_type() const
376   { return INCREMENTAL_INPUT_ARCHIVE; }
377
378   // Return a pointer to the derived Incremental_archive_entry object.
379   virtual Incremental_archive_entry*
380   do_archive_entry()
381   { return this; }
382
383  private:
384   // Members of the archive that have been included in the link.
385   std::vector<Incremental_object_entry*> members_;
386
387   // Unused global symbols from this archive.
388   std::vector<Stringpool::Key> unused_syms_;
389 };
390
391 // This class contains the information needed during an incremental
392 // build about the inputs necessary to build the .gnu_incremental_inputs.
393
394 class Incremental_inputs
395 {
396  public:
397   typedef std::vector<Incremental_input_entry*> Input_list;
398
399   Incremental_inputs()
400     : inputs_(), command_line_(), command_line_key_(0),
401       strtab_(new Stringpool()), current_object_(NULL),
402       current_object_entry_(NULL), inputs_section_(NULL),
403       symtab_section_(NULL), relocs_section_(NULL),
404       reloc_count_(0)
405   { }
406
407   ~Incremental_inputs() { delete this->strtab_; }
408
409   // Record the command line.
410   void
411   report_command_line(int argc, const char* const* argv);
412
413   // Record the initial info for archive file ARCHIVE.
414   void
415   report_archive_begin(Library_base* arch, Script_info* script_info);
416
417   // Record the final info for archive file ARCHIVE.
418   void
419   report_archive_end(Library_base* arch);
420
421   // Record the info for object file OBJ.  If ARCH is not NULL,
422   // attach the object file to the archive.
423   void
424   report_object(Object* obj, Library_base* arch, Script_info* script_info);
425
426   // Record an input section belonging to object file OBJ.
427   void
428   report_input_section(Object* obj, unsigned int shndx, const char* name,
429                        off_t sh_size);
430
431   // Record the info for input script SCRIPT.
432   void
433   report_script(const std::string& filename, Script_info* script,
434                 Timespec mtime);
435
436   // Return the running count of incremental relocations.
437   unsigned int
438   get_reloc_count() const
439   { return this->reloc_count_; }
440
441   // Update the running count of incremental relocations.
442   void
443   set_reloc_count(unsigned int count)
444   { this->reloc_count_ = count; }
445
446   // Prepare for layout.  Called from Layout::finalize.
447   void
448   finalize();
449
450   // Create the .gnu_incremental_inputs and related sections.
451   void
452   create_data_sections(Symbol_table* symtab);
453
454   // Return the .gnu_incremental_inputs section.
455   Output_section_data*
456   inputs_section() const
457   { return this->inputs_section_; }
458
459   // Return the .gnu_incremental_symtab section.
460   Output_data_space*
461   symtab_section() const
462   { return this->symtab_section_; }
463
464   // Return the .gnu_incremental_relocs section.
465   Output_data_space*
466   relocs_section() const
467   { return this->relocs_section_; }
468
469   // Return the .gnu_incremental_got_plt section.
470   Output_data_space*
471   got_plt_section() const
472   { return this->got_plt_section_; }
473
474   // Return the .gnu_incremental_strtab stringpool.
475   Stringpool*
476   get_stringpool() const
477   { return this->strtab_; }
478
479   // Return the canonical form of the command line, as will be stored in
480   // .gnu_incremental_strtab.
481   const std::string&
482   command_line() const
483   { return this->command_line_; }
484
485   // Return the stringpool key of the command line.
486   Stringpool::Key
487   command_line_key() const
488   { return this->command_line_key_; }
489
490   // Return the number of input files.
491   int
492   input_file_count() const
493   { return this->inputs_.size(); }
494
495   // Return the input files.
496   const Input_list&
497   input_files() const
498   { return this->inputs_; }
499
500   // Return the sh_entsize value for the .gnu_incremental_relocs section.
501   unsigned int
502   relocs_entsize() const;
503
504  private:
505   // The list of input files.
506   Input_list inputs_;
507
508   // Canonical form of the command line, as will be stored in
509   // .gnu_incremental_strtab.
510   std::string command_line_;
511
512   // The key of the command line string in the string pool.
513   Stringpool::Key command_line_key_;
514
515   // The .gnu_incremental_strtab string pool associated with the
516   // .gnu_incremental_inputs.
517   Stringpool* strtab_;
518
519   // Keep track of the object currently being processed.
520   Object* current_object_;
521   Incremental_object_entry* current_object_entry_;
522
523   // The .gnu_incremental_inputs section.
524   Output_section_data* inputs_section_;
525
526   // The .gnu_incremental_symtab section.
527   Output_data_space* symtab_section_;
528
529   // The .gnu_incremental_relocs section.
530   Output_data_space* relocs_section_;
531
532   // The .gnu_incremental_got_plt section.
533   Output_data_space* got_plt_section_;
534
535   // Total count of incremental relocations.  Updated during Scan_relocs
536   // phase at the completion of each object file.
537   unsigned int reloc_count_;
538 };
539
540 // Reader class for .gnu_incremental_inputs section.
541
542 template<int size, bool big_endian>
543 class Incremental_inputs_reader
544 {
545  private:
546   typedef elfcpp::Swap<size, big_endian> Swap;
547   typedef elfcpp::Swap<16, big_endian> Swap16;
548   typedef elfcpp::Swap<32, big_endian> Swap32;
549   typedef elfcpp::Swap<64, big_endian> Swap64;
550
551  public:
552   Incremental_inputs_reader()
553     : p_(NULL), strtab_(NULL, 0), input_file_count_(0)
554   { }
555
556   Incremental_inputs_reader(const unsigned char* p,
557                             const elfcpp::Elf_strtab& strtab)
558     : p_(p), strtab_(strtab)
559   { this->input_file_count_ = Swap32::readval(this->p_ + 4); }
560
561   // Return the version number.
562   unsigned int
563   version() const
564   { return Swap32::readval(this->p_); }
565
566   // Return the count of input file entries.
567   unsigned int
568   input_file_count() const
569   { return this->input_file_count_; }
570
571   // Return the command line.
572   const char*
573   command_line() const
574   {
575     unsigned int offset = Swap32::readval(this->p_ + 8);
576     return this->get_string(offset);
577   }
578
579   // Reader class for an input file entry and its supplemental info.
580   class Incremental_input_entry_reader
581   {
582    public:
583     Incremental_input_entry_reader(const Incremental_inputs_reader* inputs,
584                                    unsigned int offset)
585       : inputs_(inputs), offset_(offset)
586     {
587       this->info_offset_ = Swap32::readval(inputs->p_ + offset + 4);
588       int type = Swap16::readval(this->inputs_->p_ + offset + 20);
589       this->type_ = static_cast<Incremental_input_type>(type);
590     }
591
592     // Return the filename.
593     const char*
594     filename() const
595     {
596       unsigned int offset = Swap32::readval(this->inputs_->p_ + this->offset_);
597       return this->inputs_->get_string(offset);
598     }
599
600     // Return the timestamp.
601     Timespec
602     get_mtime() const
603     {
604       Timespec t;
605       const unsigned char* p = this->inputs_->p_ + this->offset_ + 8;
606       t.seconds = Swap64::readval(p);
607       t.nanoseconds = Swap32::readval(p+8);
608       return t;
609     }
610
611     // Return the type of input file.
612     Incremental_input_type
613     type() const
614     { return this->type_; }
615
616     // Return the input section count -- for objects only.
617     unsigned int
618     get_input_section_count() const
619     {
620       gold_assert(this->type_ == INCREMENTAL_INPUT_OBJECT
621                   || this->type_ == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
622       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
623     }
624
625     // Return the offset of the supplemental info for symbol SYMNDX --
626     // for objects only.
627     unsigned int
628     get_symbol_offset(unsigned int symndx) const
629     {
630       gold_assert(this->type_ == INCREMENTAL_INPUT_OBJECT
631                   || this->type_ == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
632
633       unsigned int section_count = this->get_input_section_count();
634       return (this->info_offset_ + 8
635               + section_count * input_section_entry_size
636               + symndx * 16);
637     }
638
639     // Return the global symbol count -- for objects & shared libraries only.
640     unsigned int
641     get_global_symbol_count() const
642     {
643       switch (this->type_)
644         {
645         case INCREMENTAL_INPUT_OBJECT:
646         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
647           return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
648         case INCREMENTAL_INPUT_SHARED_LIBRARY:
649           return Swap32::readval(this->inputs_->p_ + this->info_offset_);
650         default:
651           gold_unreachable();
652         }
653     }
654
655     // Return the object count -- for scripts only.
656     unsigned int
657     get_object_count() const
658     {
659       gold_assert(this->type_ == INCREMENTAL_INPUT_SCRIPT);
660       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
661     }
662
663     // Return the input file offset for object N -- for scripts only.
664     unsigned int
665     get_object_offset(unsigned int n) const
666     {
667       gold_assert(this->type_ == INCREMENTAL_INPUT_SCRIPT);
668       return Swap32::readval(this->inputs_->p_ + this->info_offset_
669                              + 4 + n * 4);
670     }
671
672     // Return the member count -- for archives only.
673     unsigned int
674     get_member_count() const
675     {
676       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
677       return Swap32::readval(this->inputs_->p_ + this->info_offset_);
678     }
679
680     // Return the unused symbol count -- for archives only.
681     unsigned int
682     get_unused_symbol_count() const
683     {
684       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
685       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
686     }
687
688     // Return the input file offset for archive member N -- for archives only.
689     unsigned int
690     get_member_offset(unsigned int n) const
691     {
692       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
693       return Swap32::readval(this->inputs_->p_ + this->info_offset_
694                              + 8 + n * 4);
695     }
696
697     // Return the Nth unused global symbol -- for archives only.
698     const char*
699     get_unused_symbol(unsigned int n) const
700     {
701       gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE);
702       unsigned int member_count = this->get_member_count();
703       unsigned int offset = Swap32::readval(this->inputs_->p_
704                                             + this->info_offset_ + 8
705                                             + member_count * 4
706                                             + n * 4);
707       return this->inputs_->get_string(offset);
708     }
709
710     // Information about an input section.
711     struct Input_section_info
712     {
713       const char* name;
714       unsigned int output_shndx;
715       off_t sh_offset;
716       off_t sh_size;
717     };
718
719     // Return info about the Nth input section -- for objects only.
720     Input_section_info
721     get_input_section(unsigned int n) const
722     {
723       Input_section_info info;
724       const unsigned char* p = (this->inputs_->p_
725                                 + this->info_offset_ + 8
726                                 + n * input_section_entry_size);
727       unsigned int name_offset = Swap32::readval(p);
728       info.name = this->inputs_->get_string(name_offset);
729       info.output_shndx = Swap32::readval(p + 4);
730       info.sh_offset = Swap::readval(p + 8);
731       info.sh_size = Swap::readval(p + 8 + size / 8);
732       return info;
733     }
734
735     // Information about a global symbol.
736     struct Global_symbol_info
737     {
738       unsigned int output_symndx;
739       unsigned int next_offset;
740       unsigned int reloc_count;
741       unsigned int reloc_offset;
742     };
743
744     // Return info about the Nth global symbol -- for objects only.
745     Global_symbol_info
746     get_global_symbol_info(unsigned int n) const
747     {
748       Global_symbol_info info;
749       unsigned int section_count = this->get_input_section_count();
750       const unsigned char* p = (this->inputs_->p_
751                                 + this->info_offset_ + 8
752                                 + section_count * input_section_entry_size
753                                 + n * 16);
754       info.output_symndx = Swap32::readval(p);
755       info.next_offset = Swap32::readval(p + 4);
756       info.reloc_count = Swap::readval(p + 8);
757       info.reloc_offset = Swap::readval(p + 12);
758       return info;
759     }
760
761    private:
762     // Size of an input section entry.
763     static const unsigned int input_section_entry_size = 8 + 2 * size / 8;
764     // The reader instance for the containing section.
765     const Incremental_inputs_reader* inputs_;
766     // The type of input file.
767     Incremental_input_type type_;
768     // Section offset to the input file entry.
769     unsigned int offset_;
770     // Section offset to the supplemental info for the input file.
771     unsigned int info_offset_;
772   };
773
774   // Return a reader for the Nth input file entry.
775   Incremental_input_entry_reader
776   input_file(unsigned int n) const
777   {
778     gold_assert(n < this->input_file_count_);
779     Incremental_input_entry_reader input(this, 16 + n * 24);
780     return input;
781   }
782
783  private:
784   // Lookup a string in the ELF string table.
785   const char* get_string(unsigned int offset) const
786   {
787     const char* s;
788     if (this->strtab_.get_c_string(offset, &s))
789       return s;
790     return NULL;
791   }
792
793   // Base address of the .gnu_incremental_inputs section.
794   const unsigned char* p_;
795   // The associated ELF string table.
796   elfcpp::Elf_strtab strtab_;
797   // The number of input file entries in this section.
798   unsigned int input_file_count_;
799 };
800
801 // Reader class for the .gnu_incremental_symtab section.
802
803 template<bool big_endian>
804 class Incremental_symtab_reader
805 {
806  public:
807   Incremental_symtab_reader()
808     : p_(NULL), len_(0)
809   { }
810
811   Incremental_symtab_reader(const unsigned char* p, off_t len)
812     : p_(p), len_(len)
813   { }
814
815   // Return the count of symbols in this section.
816   unsigned int
817   symbol_count() const
818   { return static_cast<unsigned int>(this->len_ / 4); }
819
820   // Return the list head for symbol table entry N.
821   unsigned int
822   get_list_head(unsigned int n) const
823   { return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4 * n); }
824
825  private:
826   // Base address of the .gnu_incremental_relocs section.
827   const unsigned char* p_;
828   // Size of the section.
829   off_t len_;
830 };
831
832 // Reader class for the .gnu_incremental_relocs section.
833
834 template<int size, bool big_endian>
835 class Incremental_relocs_reader
836 {
837  private:
838   // Size of each field.
839   static const unsigned int field_size = size / 8;
840
841  public:
842   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
843   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
844
845   // Size of each entry.
846   static const unsigned int reloc_size = 8 + 2 * field_size;
847
848   Incremental_relocs_reader()
849     : p_(NULL), len_(0)
850   { }
851
852   Incremental_relocs_reader(const unsigned char* p, off_t len)
853     : p_(p), len_(len)
854   { }
855
856   // Return the count of relocations in this section.
857   unsigned int
858   reloc_count() const
859   { return static_cast<unsigned int>(this->len_ / reloc_size); }
860
861   // Return the relocation type for relocation entry at offset OFF.
862   unsigned int
863   get_r_type(unsigned int off) const
864   {
865     return elfcpp::Swap<32, big_endian>::readval(this->p_ + off);
866   }
867
868   // Return the output section index for relocation entry at offset OFF.
869   unsigned int
870   get_r_shndx(unsigned int off) const
871   {
872     return elfcpp::Swap<32, big_endian>::readval(this->p_ + off + 4);
873   }
874
875   // Return the output section offset for relocation entry at offset OFF.
876   Address
877   get_r_offset(unsigned int off) const
878   {
879     return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8);
880   }
881
882   // Return the addend for relocation entry at offset OFF.
883   Addend
884   get_r_addend(unsigned int off) const
885   {
886     return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8
887                                                    + this->field_size);
888   }
889
890  private:
891   // Base address of the .gnu_incremental_relocs section.
892   const unsigned char* p_;
893   // Size of the section.
894   off_t len_;
895 };
896
897 // Reader class for the .gnu_incremental_got_plt section.
898
899 template<bool big_endian>
900 class Incremental_got_plt_reader
901 {
902  public:
903   Incremental_got_plt_reader()
904     : p_(NULL), got_count_(0), got_desc_p_(NULL), plt_desc_p_(NULL)
905   { }
906
907   Incremental_got_plt_reader(const unsigned char* p) : p_(p)
908   {
909     this->got_count_ = elfcpp::Swap<32, big_endian>::readval(p);
910     this->got_desc_p_ = p + 8 + ((this->got_count_ + 3) & ~3);
911     this->plt_desc_p_ = this->got_desc_p_ + this->got_count_ * 4;
912   }
913
914   // Return the GOT entry count.
915   unsigned int
916   get_got_entry_count() const
917   {
918     return this->got_count_;
919   }
920
921   // Return the PLT entry count.
922   unsigned int
923   get_plt_entry_count() const
924   {
925     return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4);
926   }
927
928   // Return the GOT type for GOT entry N.
929   unsigned int
930   get_got_type(unsigned int n)
931   {
932     return this->p_[8 + n];
933   }
934
935   // Return the GOT descriptor for GOT entry N.
936   unsigned int
937   get_got_desc(unsigned int n)
938   {
939     return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 4);
940   }
941
942   // Return the PLT descriptor for PLT entry N.
943   unsigned int
944   get_plt_desc(unsigned int n)
945   {
946     return elfcpp::Swap<32, big_endian>::readval(this->plt_desc_p_ + n * 4);
947   }
948
949  private:
950   // Base address of the .gnu_incremental_got_plt section.
951   const unsigned char* p_;
952   // GOT entry count.
953   unsigned int got_count_;
954   // Base address of the GOT descriptor array.
955   const unsigned char* got_desc_p_;
956   // Base address of the PLT descriptor array.
957   const unsigned char* plt_desc_p_;
958 };
959
960 // An object representing the ELF file we edit during an incremental build.
961 // Similar to Object or Dynobj, but operates on Output_file and contains
962 // methods to support incremental updating. This is the abstract parent class
963 // implemented in Sized_incremental_binary<size, big_endian> for a specific
964 // endianness and size.
965
966 class Incremental_binary
967 {
968  public:
969   Incremental_binary(Output_file* output, Target* target)
970     : output_(output), target_(target)
971   { }
972
973   virtual
974   ~Incremental_binary()
975   { }
976
977   // Check the .gnu_incremental_inputs section to see whether an incremental
978   // build is possible.
979   bool
980   check_inputs(Incremental_inputs* incremental_inputs)
981   { return this->do_check_inputs(incremental_inputs); }
982
983   // Return TRUE if the file specified by INPUT_ARGUMENT is unchanged
984   // with respect to the base file.
985   bool
986   file_is_unchanged(const Input_argument* input_argument) const
987   { return this->do_file_is_unchanged(input_argument); }
988
989   // Report an error.
990   void
991   error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
992
993   // Wrapper class for a sized Incremental_input_entry_reader.
994   
995   class Input_reader
996   {
997    public:
998     Input_reader()
999     { }
1000
1001     virtual
1002     ~Input_reader()
1003     { }
1004
1005     const char*
1006     filename() const
1007     { return this->do_filename(); }
1008
1009     Timespec
1010     get_mtime() const
1011     { return this->do_get_mtime(); }
1012
1013     Incremental_input_type
1014     type() const
1015     { return this->do_type(); }
1016
1017    protected:
1018     virtual const char*
1019     do_filename() const = 0;
1020
1021     virtual Timespec
1022     do_get_mtime() const = 0;
1023
1024     virtual Incremental_input_type
1025     do_type() const = 0;
1026   };
1027
1028   Input_reader*
1029   get_input_reader(const char* filename)
1030   { return this->do_get_input_reader(filename); }
1031
1032   // Functions and types for the elfcpp::Elf_file interface.  This
1033   // permit us to use Incremental_binary as the File template parameter for
1034   // elfcpp::Elf_file.
1035
1036   // The View class is returned by view.  It must support a single
1037   // method, data().  This is trivial, because Output_file::get_output_view
1038   // does what we need.
1039   class View
1040   {
1041    public:
1042     View(const unsigned char* p)
1043       : p_(p)
1044     { }
1045
1046     const unsigned char*
1047     data() const
1048     { return this->p_; }
1049
1050    private:
1051     const unsigned char* p_;
1052   };
1053
1054   // Return a View.
1055   View
1056   view(off_t file_offset, section_size_type data_size)
1057   { return View(this->output_->get_input_view(file_offset, data_size)); }
1058
1059   // A location in the file.
1060   struct Location
1061   {
1062     off_t file_offset;
1063     off_t data_size;
1064
1065     Location(off_t fo, section_size_type ds)
1066       : file_offset(fo), data_size(ds)
1067     { }
1068
1069     Location()
1070       : file_offset(0), data_size(0)
1071     { }
1072   };
1073
1074   // Get a View given a Location.
1075   View
1076   view(Location loc)
1077   { return View(this->view(loc.file_offset, loc.data_size)); }
1078
1079  protected:
1080   // Check the .gnu_incremental_inputs section to see whether an incremental
1081   // build is possible.
1082   virtual bool
1083   do_check_inputs(Incremental_inputs* incremental_inputs) = 0;
1084
1085   // Return TRUE if the file specified by INPUT_ARGUMENT is unchanged
1086   // with respect to the base file.
1087   virtual bool
1088   do_file_is_unchanged(const Input_argument* input_argument) const = 0;
1089
1090   virtual Input_reader*
1091   do_get_input_reader(const char* filename) = 0;
1092
1093  private:
1094   // Edited output file object.
1095   Output_file* output_;
1096   // Target of the output file.
1097   Target* target_;
1098 };
1099
1100 template<int size, bool big_endian>
1101 class Sized_incremental_binary : public Incremental_binary
1102 {
1103  public:
1104   Sized_incremental_binary(Output_file* output,
1105                            const elfcpp::Ehdr<size, big_endian>& ehdr,
1106                            Target* target)
1107     : Incremental_binary(output, target), elf_file_(this, ehdr),
1108       has_incremental_info_(false), inputs_reader_(), symtab_reader_(),
1109       relocs_reader_(), got_plt_reader_(), current_input_file_(0)
1110   { this->setup_readers(); }
1111
1112   // Returns TRUE if the file contains incremental info.
1113   bool
1114   has_incremental_info() const
1115   { return this->has_incremental_info_; }
1116
1117   // Readers for the incremental info sections.
1118
1119   Incremental_inputs_reader<size, big_endian>
1120   inputs_reader() const
1121   { return this->inputs_reader_; }
1122
1123   Incremental_symtab_reader<big_endian>
1124   symtab_reader() const
1125   { return this->symtab_reader_; }
1126
1127   Incremental_relocs_reader<size, big_endian>
1128   relocs_reader() const
1129   { return this->relocs_reader_; }
1130
1131   Incremental_got_plt_reader<big_endian>
1132   got_plt_reader() const
1133   { return this->got_plt_reader_; }
1134
1135  protected:
1136   virtual bool
1137   do_check_inputs(Incremental_inputs* incremental_inputs);
1138
1139   // Return TRUE if the file specified by INPUT_ARGUMENT is unchanged
1140   // with respect to the base file.
1141   virtual bool
1142   do_file_is_unchanged(const Input_argument* input_argument) const;
1143
1144   // Wrapper class for a sized Incremental_input_entry_reader.
1145   
1146   class Sized_input_reader : public Input_reader
1147   {
1148    public:
1149     typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
1150     typedef typename Inputs_reader::Incremental_input_entry_reader
1151         Input_entry_reader;
1152
1153     Sized_input_reader(Input_entry_reader r)
1154       : Input_reader(), reader_(r)
1155     { }
1156
1157     virtual
1158     ~Sized_input_reader()
1159     { }
1160
1161    private:
1162     const char*
1163     do_filename() const
1164     { return this->reader_.filename(); }
1165
1166     Timespec
1167     do_get_mtime() const
1168     { return this->reader_.get_mtime(); }
1169
1170     Incremental_input_type
1171     do_type() const
1172     { return this->reader_.type(); }
1173
1174     Input_entry_reader reader_;
1175   };
1176
1177   virtual Input_reader*
1178   do_get_input_reader(const char* filename);
1179
1180  private:
1181   bool
1182   find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
1183                                    unsigned int* p_symtab_shndx,
1184                                    unsigned int* p_relocs_shndx,
1185                                    unsigned int* p_got_plt_shndx,
1186                                    unsigned int* p_strtab_shndx);
1187
1188   void
1189   setup_readers();
1190
1191   // Output as an ELF file.
1192   elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
1193
1194   // Readers for the incremental info sections.
1195   bool has_incremental_info_;
1196   Incremental_inputs_reader<size, big_endian> inputs_reader_;
1197   Incremental_symtab_reader<big_endian> symtab_reader_;
1198   Incremental_relocs_reader<size, big_endian> relocs_reader_;
1199   Incremental_got_plt_reader<big_endian> got_plt_reader_;
1200
1201   // Index of the current input file entry.
1202   int current_input_file_;
1203 };
1204
1205 } // End namespace gold.
1206
1207 #endif // !defined(GOLD_INCREMENTAL_H)