* elf32-spu.c (build_stub): Fix malloc under-allocation.
[binutils.git] / gold / incremental.h
blob0edb190885d9904fc5c4802987a2a4f81edcb612
1 // inremental.h -- incremental linking support for gold -*- C++ -*-
3 // Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Mikolaj Zalewski <mikolajz@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_INCREMENTAL_H
24 #define GOLD_INCREMENTAL_H
26 #include <map>
27 #include <vector>
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"
36 namespace gold
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;
50 // Incremental input type as stored in .gnu_incremental_inputs.
52 enum Incremental_input_type
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
61 // Incremental input file flags.
62 // The input file type is stored in the lower eight bits.
64 enum Incremental_input_flags
66 INCREMENTAL_INPUT_IN_SYSTEM_DIR = 0x8000,
67 INCREMENTAL_INPUT_AS_NEEDED = 0x4000
70 // Symbol flags for the incremental symbol table.
71 // These flags are stored in the top two bits of
72 // the symbol index field.
74 enum Incremental_shlib_symbol_flags
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
82 static const int INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT = 30;
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);
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.
92 Incremental_binary*
93 open_incremental_binary(Output_file* file);
95 // Base class for recording each input file.
97 class Incremental_input_entry
99 public:
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)
107 virtual
108 ~Incremental_input_entry()
111 // Return the type of input file.
112 Incremental_input_type
113 type() const
114 { return this->do_type(); }
116 // Set the index and section offset of this input file entry.
117 void
118 set_offset(unsigned int file_index, unsigned int offset)
120 this->file_index_ = file_index;
121 this->offset_ = offset;
124 // Set the section offset of the supplemental information for this entry.
125 void
126 set_info_offset(unsigned int info_offset)
127 { this->info_offset_ = info_offset; }
129 // Get the index of this input file entry.
130 unsigned int
131 get_file_index() const
132 { return this->file_index_; }
134 // Get the section offset of this input file entry.
135 unsigned int
136 get_offset() const
137 { return this->offset_; }
139 // Get the section offset of the supplemental information for this entry.
140 unsigned int
141 get_info_offset() const
142 { return this->info_offset_; }
144 // Get the stringpool key for the input filename.
145 Stringpool::Key
146 get_filename_key() const
147 { return this->filename_key_; }
149 // Get the serial number of the input file.
150 unsigned int
151 arg_serial() const
152 { return this->arg_serial_; }
154 // Get the modification time of the input file.
155 const Timespec&
156 get_mtime() const
157 { return this->mtime_; }
159 // Record that the file was found in a system directory.
160 void
161 set_is_in_system_directory()
162 { this->is_in_system_directory_ = true; }
164 // Return TRUE if the file was found in a system directory.
165 bool
166 is_in_system_directory() const
167 { return this->is_in_system_directory_; }
169 // Record that the file was linked with --as-needed.
170 void
171 set_as_needed()
172 { this->as_needed_ = true; }
174 // Return TRUE if the file was linked with --as-needed.
175 bool
176 as_needed() const
177 { return this->as_needed_; }
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*
182 script_entry()
183 { return this->do_script_entry(); }
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*
188 object_entry()
189 { return this->do_object_entry(); }
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*
194 dynobj_entry()
195 { return this->do_dynobj_entry(); }
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*
200 archive_entry()
201 { return this->do_archive_entry(); }
203 protected:
204 // Return the type of input file.
205 virtual Incremental_input_type
206 do_type() const = 0;
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*
211 do_script_entry()
212 { return NULL; }
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*
217 do_object_entry()
218 { return NULL; }
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*
223 do_dynobj_entry()
224 { return NULL; }
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*
229 do_archive_entry()
230 { return NULL; }
232 private:
233 // Key of the filename string in the section stringtable.
234 Stringpool::Key filename_key_;
236 // Index of the entry in the output section.
237 unsigned int file_index_;
239 // Offset of the entry in the output section.
240 unsigned int offset_;
242 // Offset of the extra information in the output section.
243 unsigned int info_offset_;
245 // Serial number of the file in the argument list.
246 unsigned int arg_serial_;
248 // Last modification time of the file.
249 Timespec mtime_;
251 // TRUE if the file was found in a system directory.
252 bool is_in_system_directory_;
254 // TRUE if the file was linked with --as-needed.
255 bool as_needed_;
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.
262 class Script_info
264 public:
265 Script_info(const std::string& filename)
266 : filename_(filename), input_file_index_(0),
267 incremental_script_entry_(NULL)
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)
275 // Store a pointer to the incremental information for this script.
276 void
277 set_incremental_info(Incremental_script_entry* entry)
278 { this->incremental_script_entry_ = entry; }
280 // Return the filename.
281 const std::string&
282 filename() const
283 { return this->filename_; }
285 // Return the input file index.
286 unsigned int
287 input_file_index() const
288 { return this->input_file_index_; }
290 // Return the pointer to the incremental information for this script.
291 Incremental_script_entry*
292 incremental_info() const
293 { return this->incremental_script_entry_; }
295 private:
296 const std::string filename_;
297 unsigned int input_file_index_;
298 Incremental_script_entry* incremental_script_entry_;
301 // Class for recording input scripts.
303 class Incremental_script_entry : public Incremental_input_entry
305 public:
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 script_(script), objects_()
313 // Add a member object to the archive.
314 void
315 add_object(Incremental_input_entry* obj_entry)
317 this->objects_.push_back(obj_entry);
320 // Return the number of objects included by this script.
321 unsigned int
322 get_object_count()
323 { return this->objects_.size(); }
325 // Return the Nth object.
326 Incremental_input_entry*
327 get_object(unsigned int n)
329 gold_assert(n < this->objects_.size());
330 return this->objects_[n];
333 protected:
334 virtual Incremental_input_type
335 do_type() const
336 { return INCREMENTAL_INPUT_SCRIPT; }
338 // Return a pointer to the derived Incremental_script_entry object.
339 virtual Incremental_script_entry*
340 do_script_entry()
341 { return this; }
343 private:
344 // Information about the script file.
345 Script_info* script_;
346 // Objects that have been included by this script.
347 std::vector<Incremental_input_entry*> objects_;
350 // Class for recording input object files.
352 class Incremental_object_entry : public Incremental_input_entry
354 public:
355 Incremental_object_entry(Stringpool::Key filename_key, Object* obj,
356 unsigned int arg_serial, Timespec mtime)
357 : Incremental_input_entry(filename_key, arg_serial, mtime), obj_(obj),
358 is_member_(false), sections_(), groups_()
359 { this->sections_.reserve(obj->shnum()); }
361 // Get the object.
362 Object*
363 object() const
364 { return this->obj_; }
366 // Record that this object is an archive member.
367 void
368 set_is_member()
369 { this->is_member_ = true; }
371 // Return true if this object is an archive member.
372 bool
373 is_member() const
374 { return this->is_member_; }
376 // Add an input section.
377 void
378 add_input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
379 { this->sections_.push_back(Input_section(shndx, name_key, sh_size)); }
381 // Return the number of input sections in this object.
382 unsigned int
383 get_input_section_count() const
384 { return this->sections_.size(); }
386 // Return the input section index for the Nth input section.
387 Stringpool::Key
388 get_input_section_index(unsigned int n) const
389 { return this->sections_[n].shndx_; }
391 // Return the stringpool key of the Nth input section.
392 Stringpool::Key
393 get_input_section_name_key(unsigned int n) const
394 { return this->sections_[n].name_key_; }
396 // Return the size of the Nth input section.
397 off_t
398 get_input_section_size(unsigned int n) const
399 { return this->sections_[n].sh_size_; }
401 // Add a kept COMDAT group.
402 void
403 add_comdat_group(Stringpool::Key signature_key)
404 { this->groups_.push_back(signature_key); }
406 // Return the number of COMDAT groups.
407 unsigned int
408 get_comdat_group_count() const
409 { return this->groups_.size(); }
411 // Return the stringpool key for the signature of the Nth comdat group.
412 Stringpool::Key
413 get_comdat_signature_key(unsigned int n) const
414 { return this->groups_[n]; }
416 protected:
417 virtual Incremental_input_type
418 do_type() const
420 return (this->is_member_
421 ? INCREMENTAL_INPUT_ARCHIVE_MEMBER
422 : INCREMENTAL_INPUT_OBJECT);
425 // Return a pointer to the derived Incremental_object_entry object.
426 virtual Incremental_object_entry*
427 do_object_entry()
428 { return this; }
430 private:
431 // The object file itself.
432 Object* obj_;
434 // Whether this object is an archive member.
435 bool is_member_;
437 // Input sections.
438 struct Input_section
440 Input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size)
441 : shndx_(shndx), name_key_(name_key), sh_size_(sh_size)
443 unsigned int shndx_;
444 Stringpool::Key name_key_;
445 off_t sh_size_;
447 std::vector<Input_section> sections_;
449 // COMDAT groups.
450 std::vector<Stringpool::Key> groups_;
453 // Class for recording shared library input files.
455 class Incremental_dynobj_entry : public Incremental_input_entry
457 public:
458 Incremental_dynobj_entry(Stringpool::Key filename_key,
459 Stringpool::Key soname_key, Object* obj,
460 unsigned int arg_serial, Timespec mtime)
461 : Incremental_input_entry(filename_key, arg_serial, mtime),
462 soname_key_(soname_key), obj_(obj)
465 // Get the object.
466 Object*
467 object() const
468 { return this->obj_; }
470 // Get the stringpool key for the soname.
471 Stringpool::Key
472 get_soname_key() const
473 { return this->soname_key_; }
475 protected:
476 virtual Incremental_input_type
477 do_type() const
478 { return INCREMENTAL_INPUT_SHARED_LIBRARY; }
480 // Return a pointer to the derived Incremental_dynobj_entry object.
481 virtual Incremental_dynobj_entry*
482 do_dynobj_entry()
483 { return this; }
485 private:
486 // Key of the soname string in the section stringtable.
487 Stringpool::Key soname_key_;
489 // The object file itself.
490 Object* obj_;
493 // Class for recording archive library input files.
495 class Incremental_archive_entry : public Incremental_input_entry
497 public:
498 Incremental_archive_entry(Stringpool::Key filename_key,
499 unsigned int arg_serial, Timespec mtime)
500 : Incremental_input_entry(filename_key, arg_serial, mtime), members_(),
501 unused_syms_()
504 // Add a member object to the archive.
505 void
506 add_object(Incremental_object_entry* obj_entry)
508 this->members_.push_back(obj_entry);
509 obj_entry->set_is_member();
512 // Add an unused global symbol to the archive.
513 void
514 add_unused_global_symbol(Stringpool::Key symbol_key)
515 { this->unused_syms_.push_back(symbol_key); }
517 // Return the number of member objects included in the link.
518 unsigned int
519 get_member_count()
520 { return this->members_.size(); }
522 // Return the Nth member object.
523 Incremental_object_entry*
524 get_member(unsigned int n)
525 { return this->members_[n]; }
527 // Return the number of unused global symbols in this archive.
528 unsigned int
529 get_unused_global_symbol_count()
530 { return this->unused_syms_.size(); }
532 // Return the Nth unused global symbol.
533 Stringpool::Key
534 get_unused_global_symbol(unsigned int n)
535 { return this->unused_syms_[n]; }
537 protected:
538 virtual Incremental_input_type
539 do_type() const
540 { return INCREMENTAL_INPUT_ARCHIVE; }
542 // Return a pointer to the derived Incremental_archive_entry object.
543 virtual Incremental_archive_entry*
544 do_archive_entry()
545 { return this; }
547 private:
548 // Members of the archive that have been included in the link.
549 std::vector<Incremental_object_entry*> members_;
551 // Unused global symbols from this archive.
552 std::vector<Stringpool::Key> unused_syms_;
555 // This class contains the information needed during an incremental
556 // build about the inputs necessary to build the .gnu_incremental_inputs.
558 class Incremental_inputs
560 public:
561 typedef std::vector<Incremental_input_entry*> Input_list;
563 Incremental_inputs()
564 : inputs_(), command_line_(), command_line_key_(0),
565 strtab_(new Stringpool()), current_object_(NULL),
566 current_object_entry_(NULL), inputs_section_(NULL),
567 symtab_section_(NULL), relocs_section_(NULL),
568 reloc_count_(0)
571 ~Incremental_inputs() { delete this->strtab_; }
573 // Record the command line.
574 void
575 report_command_line(int argc, const char* const* argv);
577 // Record the initial info for archive file ARCHIVE.
578 void
579 report_archive_begin(Library_base* arch, unsigned int arg_serial,
580 Script_info* script_info);
582 // Record the final info for archive file ARCHIVE.
583 void
584 report_archive_end(Library_base* arch);
586 // Record the info for object file OBJ. If ARCH is not NULL,
587 // attach the object file to the archive.
588 void
589 report_object(Object* obj, unsigned int arg_serial, Library_base* arch,
590 Script_info* script_info);
592 // Record an input section belonging to object file OBJ.
593 void
594 report_input_section(Object* obj, unsigned int shndx, const char* name,
595 off_t sh_size);
597 // Record a kept COMDAT group belonging to object file OBJ.
598 void
599 report_comdat_group(Object* obj, const char* name);
601 // Record the info for input script SCRIPT.
602 void
603 report_script(Script_info* script, unsigned int arg_serial,
604 Timespec mtime);
606 // Return the running count of incremental relocations.
607 unsigned int
608 get_reloc_count() const
609 { return this->reloc_count_; }
611 // Update the running count of incremental relocations.
612 void
613 set_reloc_count(unsigned int count)
614 { this->reloc_count_ = count; }
616 // Prepare for layout. Called from Layout::finalize.
617 void
618 finalize();
620 // Create the .gnu_incremental_inputs and related sections.
621 void
622 create_data_sections(Symbol_table* symtab);
624 // Return the .gnu_incremental_inputs section.
625 Output_section_data*
626 inputs_section() const
627 { return this->inputs_section_; }
629 // Return the .gnu_incremental_symtab section.
630 Output_data_space*
631 symtab_section() const
632 { return this->symtab_section_; }
634 // Return the .gnu_incremental_relocs section.
635 Output_data_space*
636 relocs_section() const
637 { return this->relocs_section_; }
639 // Return the .gnu_incremental_got_plt section.
640 Output_data_space*
641 got_plt_section() const
642 { return this->got_plt_section_; }
644 // Return the .gnu_incremental_strtab stringpool.
645 Stringpool*
646 get_stringpool() const
647 { return this->strtab_; }
649 // Return the canonical form of the command line, as will be stored in
650 // .gnu_incremental_strtab.
651 const std::string&
652 command_line() const
653 { return this->command_line_; }
655 // Return the stringpool key of the command line.
656 Stringpool::Key
657 command_line_key() const
658 { return this->command_line_key_; }
660 // Return the number of input files.
662 input_file_count() const
663 { return this->inputs_.size(); }
665 // Return the input files.
666 const Input_list&
667 input_files() const
668 { return this->inputs_; }
670 // Return the sh_entsize value for the .gnu_incremental_relocs section.
671 unsigned int
672 relocs_entsize() const;
674 private:
675 // The list of input files.
676 Input_list inputs_;
678 // Canonical form of the command line, as will be stored in
679 // .gnu_incremental_strtab.
680 std::string command_line_;
682 // The key of the command line string in the string pool.
683 Stringpool::Key command_line_key_;
685 // The .gnu_incremental_strtab string pool associated with the
686 // .gnu_incremental_inputs.
687 Stringpool* strtab_;
689 // Keep track of the object currently being processed.
690 Object* current_object_;
691 Incremental_object_entry* current_object_entry_;
693 // The .gnu_incremental_inputs section.
694 Output_section_data* inputs_section_;
696 // The .gnu_incremental_symtab section.
697 Output_data_space* symtab_section_;
699 // The .gnu_incremental_relocs section.
700 Output_data_space* relocs_section_;
702 // The .gnu_incremental_got_plt section.
703 Output_data_space* got_plt_section_;
705 // Total count of incremental relocations. Updated during Scan_relocs
706 // phase at the completion of each object file.
707 unsigned int reloc_count_;
710 // Reader class for global symbol info from an object file entry in
711 // the .gnu_incremental_inputs section.
713 template<bool big_endian>
714 class Incremental_global_symbol_reader
716 private:
717 typedef elfcpp::Swap<32, big_endian> Swap32;
719 public:
720 Incremental_global_symbol_reader(const unsigned char* p)
721 : p_(p)
724 unsigned int
725 output_symndx() const
726 { return Swap32::readval(this->p_); }
728 unsigned int
729 shndx() const
730 { return Swap32::readval(this->p_ + 4); }
732 unsigned int
733 next_offset() const
734 { return Swap32::readval(this->p_ + 8); }
736 unsigned int
737 reloc_count() const
738 { return Swap32::readval(this->p_ + 12); }
740 unsigned int
741 reloc_offset() const
742 { return Swap32::readval(this->p_ + 16); }
744 private:
745 // Base address of the symbol entry.
746 const unsigned char* p_;
749 // Reader class for .gnu_incremental_inputs section.
751 template<int size, bool big_endian>
752 class Incremental_inputs_reader
754 private:
755 typedef elfcpp::Swap<size, big_endian> Swap;
756 typedef elfcpp::Swap<16, big_endian> Swap16;
757 typedef elfcpp::Swap<32, big_endian> Swap32;
758 typedef elfcpp::Swap<64, big_endian> Swap64;
760 public:
761 Incremental_inputs_reader()
762 : p_(NULL), strtab_(NULL, 0), input_file_count_(0)
765 Incremental_inputs_reader(const unsigned char* p,
766 const elfcpp::Elf_strtab& strtab)
767 : p_(p), strtab_(strtab)
768 { this->input_file_count_ = Swap32::readval(this->p_ + 4); }
770 // Return the version number.
771 unsigned int
772 version() const
773 { return Swap32::readval(this->p_); }
775 // Return the count of input file entries.
776 unsigned int
777 input_file_count() const
778 { return this->input_file_count_; }
780 // Return the command line.
781 const char*
782 command_line() const
784 unsigned int offset = Swap32::readval(this->p_ + 8);
785 return this->get_string(offset);
788 // Reader class for an input file entry and its supplemental info.
789 class Incremental_input_entry_reader
791 public:
792 Incremental_input_entry_reader(const Incremental_inputs_reader* inputs,
793 unsigned int offset)
794 : inputs_(inputs), offset_(offset)
796 this->info_offset_ = Swap32::readval(inputs->p_ + offset + 4);
797 this->flags_ = Swap16::readval(this->inputs_->p_ + offset + 20);
800 // Return the filename.
801 const char*
802 filename() const
804 unsigned int offset = Swap32::readval(this->inputs_->p_ + this->offset_);
805 return this->inputs_->get_string(offset);
808 // Return the argument serial number.
809 unsigned int
810 arg_serial() const
812 return Swap16::readval(this->inputs_->p_ + this->offset_ + 22);
815 // Return the timestamp.
816 Timespec
817 get_mtime() const
819 Timespec t;
820 const unsigned char* p = this->inputs_->p_ + this->offset_ + 8;
821 t.seconds = Swap64::readval(p);
822 t.nanoseconds = Swap32::readval(p+8);
823 return t;
826 // Return the type of input file.
827 Incremental_input_type
828 type() const
829 { return static_cast<Incremental_input_type>(this->flags_ & 0xff); }
831 // Return TRUE if the file was found in a system directory.
832 bool
833 is_in_system_directory() const
834 { return (this->flags_ & INCREMENTAL_INPUT_IN_SYSTEM_DIR) != 0; }
836 // Return TRUE if the file was linked with --as-needed.
837 bool
838 as_needed() const
839 { return (this->flags_ & INCREMENTAL_INPUT_AS_NEEDED) != 0; }
841 // Return the input section count -- for objects only.
842 unsigned int
843 get_input_section_count() const
845 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
846 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
847 return Swap32::readval(this->inputs_->p_ + this->info_offset_);
850 // Return the soname -- for shared libraries only.
851 const char*
852 get_soname() const
854 gold_assert(this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
855 unsigned int offset = Swap32::readval(this->inputs_->p_
856 + this->info_offset_);
857 return this->inputs_->get_string(offset);
860 // Return the offset of the supplemental info for symbol SYMNDX --
861 // for objects only.
862 unsigned int
863 get_symbol_offset(unsigned int symndx) const
865 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
866 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
868 unsigned int section_count = this->get_input_section_count();
869 return (this->info_offset_ + 28
870 + section_count * input_section_entry_size
871 + symndx * 20);
874 // Return the global symbol count -- for objects & shared libraries only.
875 unsigned int
876 get_global_symbol_count() const
878 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
879 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER
880 || this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
881 return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
884 // Return the offset of the first local symbol -- for objects only.
885 unsigned int
886 get_local_symbol_offset() const
888 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
889 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
891 return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 8);
894 // Return the local symbol count -- for objects only.
895 unsigned int
896 get_local_symbol_count() const
898 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
899 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
901 return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 12);
904 // Return the index of the first dynamic relocation -- for objects only.
905 unsigned int
906 get_first_dyn_reloc() const
908 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
909 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
911 return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 16);
914 // Return the dynamic relocation count -- for objects only.
915 unsigned int
916 get_dyn_reloc_count() const
918 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
919 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
921 return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 20);
924 // Return the COMDAT group count -- for objects only.
925 unsigned int
926 get_comdat_group_count() const
928 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
929 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
931 return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 24);
934 // Return the object count -- for scripts only.
935 unsigned int
936 get_object_count() const
938 gold_assert(this->type() == INCREMENTAL_INPUT_SCRIPT);
939 return Swap32::readval(this->inputs_->p_ + this->info_offset_);
942 // Return the input file offset for object N -- for scripts only.
943 unsigned int
944 get_object_offset(unsigned int n) const
946 gold_assert(this->type() == INCREMENTAL_INPUT_SCRIPT);
947 return Swap32::readval(this->inputs_->p_ + this->info_offset_
948 + 4 + n * 4);
951 // Return the member count -- for archives only.
952 unsigned int
953 get_member_count() const
955 gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
956 return Swap32::readval(this->inputs_->p_ + this->info_offset_);
959 // Return the unused symbol count -- for archives only.
960 unsigned int
961 get_unused_symbol_count() const
963 gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
964 return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
967 // Return the input file offset for archive member N -- for archives only.
968 unsigned int
969 get_member_offset(unsigned int n) const
971 gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
972 return Swap32::readval(this->inputs_->p_ + this->info_offset_
973 + 8 + n * 4);
976 // Return the Nth unused global symbol -- for archives only.
977 const char*
978 get_unused_symbol(unsigned int n) const
980 gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
981 unsigned int member_count = this->get_member_count();
982 unsigned int offset = Swap32::readval(this->inputs_->p_
983 + this->info_offset_ + 8
984 + member_count * 4
985 + n * 4);
986 return this->inputs_->get_string(offset);
989 // Information about an input section.
990 struct Input_section_info
992 const char* name;
993 unsigned int output_shndx;
994 off_t sh_offset;
995 off_t sh_size;
998 // Return info about the Nth input section -- for objects only.
999 Input_section_info
1000 get_input_section(unsigned int n) const
1002 Input_section_info info;
1003 const unsigned char* p = (this->inputs_->p_
1004 + this->info_offset_ + 28
1005 + n * input_section_entry_size);
1006 unsigned int name_offset = Swap32::readval(p);
1007 info.name = this->inputs_->get_string(name_offset);
1008 info.output_shndx = Swap32::readval(p + 4);
1009 info.sh_offset = Swap::readval(p + 8);
1010 info.sh_size = Swap::readval(p + 8 + size / 8);
1011 return info;
1014 // Return info about the Nth global symbol -- for objects only.
1015 Incremental_global_symbol_reader<big_endian>
1016 get_global_symbol_reader(unsigned int n) const
1018 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
1019 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
1020 unsigned int section_count = this->get_input_section_count();
1021 const unsigned char* p = (this->inputs_->p_
1022 + this->info_offset_ + 28
1023 + section_count * input_section_entry_size
1024 + n * 20);
1025 return Incremental_global_symbol_reader<big_endian>(p);
1028 // Return the signature of the Nth comdat group -- for objects only.
1029 const char*
1030 get_comdat_group_signature(unsigned int n) const
1032 unsigned int section_count = this->get_input_section_count();
1033 unsigned int symbol_count = this->get_global_symbol_count();
1034 const unsigned char* p = (this->inputs_->p_
1035 + this->info_offset_ + 28
1036 + section_count * input_section_entry_size
1037 + symbol_count * 20
1038 + n * 4);
1039 unsigned int name_offset = Swap32::readval(p);
1040 return this->inputs_->get_string(name_offset);
1043 // Return the output symbol index for the Nth global symbol -- for shared
1044 // libraries only. Sets *IS_DEF to TRUE if the symbol is defined in this
1045 // input file. Sets *IS_COPY to TRUE if the symbol was copied from this
1046 // input file with a COPY relocation.
1047 unsigned int
1048 get_output_symbol_index(unsigned int n, bool* is_def, bool* is_copy)
1050 gold_assert(this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
1051 const unsigned char* p = (this->inputs_->p_
1052 + this->info_offset_ + 8
1053 + n * 4);
1054 unsigned int output_symndx = Swap32::readval(p);
1055 unsigned int flags = output_symndx >> INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
1056 output_symndx &= ((1U << INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT) - 1);
1057 switch (flags)
1059 case INCREMENTAL_SHLIB_SYM_DEF:
1060 *is_def = true;
1061 *is_copy = false;
1062 break;
1063 case INCREMENTAL_SHLIB_SYM_COPY:
1064 *is_def = true;
1065 *is_copy = true;
1066 break;
1067 default:
1068 *is_def = false;
1069 *is_copy = false;
1071 return output_symndx;
1074 private:
1075 // Size of an input section entry.
1076 static const unsigned int input_section_entry_size = 8 + 2 * size / 8;
1077 // The reader instance for the containing section.
1078 const Incremental_inputs_reader* inputs_;
1079 // The flags, including the type of input file.
1080 unsigned int flags_;
1081 // Section offset to the input file entry.
1082 unsigned int offset_;
1083 // Section offset to the supplemental info for the input file.
1084 unsigned int info_offset_;
1087 // Return the offset of an input file entry given its index N.
1088 unsigned int
1089 input_file_offset(unsigned int n) const
1091 gold_assert(n < this->input_file_count_);
1092 return 16 + n * 24;
1095 // Return the index of an input file entry given its OFFSET.
1096 unsigned int
1097 input_file_index(unsigned int offset) const
1099 int n = (offset - 16) / 24;
1100 gold_assert(input_file_offset(n) == offset);
1101 return n;
1104 // Return a reader for the Nth input file entry.
1105 Incremental_input_entry_reader
1106 input_file(unsigned int n) const
1107 { return Incremental_input_entry_reader(this, this->input_file_offset(n)); }
1109 // Return a reader for the input file entry at OFFSET.
1110 Incremental_input_entry_reader
1111 input_file_at_offset(unsigned int offset) const
1113 gold_assert(offset < 16 + this->input_file_count_ * 24);
1114 return Incremental_input_entry_reader(this, offset);
1117 // Return a reader for the global symbol info at OFFSET.
1118 Incremental_global_symbol_reader<big_endian>
1119 global_symbol_reader_at_offset(unsigned int offset) const
1121 const unsigned char* p = this->p_ + offset;
1122 return Incremental_global_symbol_reader<big_endian>(p);
1125 private:
1126 // Lookup a string in the ELF string table.
1127 const char* get_string(unsigned int offset) const
1129 const char* s;
1130 if (this->strtab_.get_c_string(offset, &s))
1131 return s;
1132 return NULL;
1135 // Base address of the .gnu_incremental_inputs section.
1136 const unsigned char* p_;
1137 // The associated ELF string table.
1138 elfcpp::Elf_strtab strtab_;
1139 // The number of input file entries in this section.
1140 unsigned int input_file_count_;
1143 // Reader class for the .gnu_incremental_symtab section.
1145 template<bool big_endian>
1146 class Incremental_symtab_reader
1148 public:
1149 Incremental_symtab_reader()
1150 : p_(NULL), len_(0)
1153 Incremental_symtab_reader(const unsigned char* p, off_t len)
1154 : p_(p), len_(len)
1157 // Return the count of symbols in this section.
1158 unsigned int
1159 symbol_count() const
1160 { return static_cast<unsigned int>(this->len_ / 4); }
1162 // Return the list head for symbol table entry N.
1163 unsigned int
1164 get_list_head(unsigned int n) const
1165 { return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4 * n); }
1167 private:
1168 // Base address of the .gnu_incremental_relocs section.
1169 const unsigned char* p_;
1170 // Size of the section.
1171 off_t len_;
1174 // Reader class for the .gnu_incremental_relocs section.
1176 template<int size, bool big_endian>
1177 class Incremental_relocs_reader
1179 private:
1180 // Size of each field.
1181 static const unsigned int field_size = size / 8;
1183 public:
1184 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1185 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
1187 // Size of each entry.
1188 static const unsigned int reloc_size = 8 + 2 * field_size;
1190 Incremental_relocs_reader()
1191 : p_(NULL), len_(0)
1194 Incremental_relocs_reader(const unsigned char* p, off_t len)
1195 : p_(p), len_(len)
1198 // Return the count of relocations in this section.
1199 unsigned int
1200 reloc_count() const
1201 { return static_cast<unsigned int>(this->len_ / reloc_size); }
1203 // Return the relocation type for relocation entry at offset OFF.
1204 unsigned int
1205 get_r_type(unsigned int off) const
1206 { return elfcpp::Swap<32, big_endian>::readval(this->p_ + off); }
1208 // Return the output section index for relocation entry at offset OFF.
1209 unsigned int
1210 get_r_shndx(unsigned int off) const
1211 { return elfcpp::Swap<32, big_endian>::readval(this->p_ + off + 4); }
1213 // Return the output section offset for relocation entry at offset OFF.
1214 Address
1215 get_r_offset(unsigned int off) const
1216 { return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8); }
1218 // Return the addend for relocation entry at offset OFF.
1219 Addend
1220 get_r_addend(unsigned int off) const
1222 return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8
1223 + this->field_size);
1226 // Return a pointer to the relocation entry at offset OFF.
1227 const unsigned char*
1228 data(unsigned int off) const
1229 { return this->p_ + off; }
1231 private:
1232 // Base address of the .gnu_incremental_relocs section.
1233 const unsigned char* p_;
1234 // Size of the section.
1235 off_t len_;
1238 // Reader class for the .gnu_incremental_got_plt section.
1240 template<bool big_endian>
1241 class Incremental_got_plt_reader
1243 public:
1244 Incremental_got_plt_reader()
1245 : p_(NULL), got_count_(0), got_desc_p_(NULL), plt_desc_p_(NULL)
1248 Incremental_got_plt_reader(const unsigned char* p) : p_(p)
1250 this->got_count_ = elfcpp::Swap<32, big_endian>::readval(p);
1251 this->got_desc_p_ = p + 8 + ((this->got_count_ + 3) & ~3);
1252 this->plt_desc_p_ = this->got_desc_p_ + this->got_count_ * 8;
1255 // Return the GOT entry count.
1256 unsigned int
1257 get_got_entry_count() const
1259 return this->got_count_;
1262 // Return the PLT entry count.
1263 unsigned int
1264 get_plt_entry_count() const
1266 return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4);
1269 // Return the GOT type for GOT entry N.
1270 unsigned int
1271 get_got_type(unsigned int n)
1273 return this->p_[8 + n];
1276 // Return the symbol index for GOT entry N.
1277 unsigned int
1278 get_got_symndx(unsigned int n)
1280 return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 8);
1283 // Return the input file index for GOT entry N.
1284 unsigned int
1285 get_got_input_index(unsigned int n)
1287 return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 8 + 4);
1290 // Return the PLT descriptor for PLT entry N.
1291 unsigned int
1292 get_plt_desc(unsigned int n)
1294 return elfcpp::Swap<32, big_endian>::readval(this->plt_desc_p_ + n * 4);
1297 private:
1298 // Base address of the .gnu_incremental_got_plt section.
1299 const unsigned char* p_;
1300 // GOT entry count.
1301 unsigned int got_count_;
1302 // Base address of the GOT descriptor array.
1303 const unsigned char* got_desc_p_;
1304 // Base address of the PLT descriptor array.
1305 const unsigned char* plt_desc_p_;
1308 // An object representing the ELF file we edit during an incremental build.
1309 // Similar to Object or Dynobj, but operates on Output_file and contains
1310 // methods to support incremental updating. This is the abstract parent class
1311 // implemented in Sized_incremental_binary<size, big_endian> for a specific
1312 // endianness and size.
1314 class Incremental_binary
1316 public:
1317 Incremental_binary(Output_file* output, Target* target)
1318 : input_args_map_(), library_map_(), script_map_(),
1319 output_(output), target_(target)
1322 virtual
1323 ~Incremental_binary()
1326 // Check the .gnu_incremental_inputs section to see whether an incremental
1327 // build is possible.
1328 bool
1329 check_inputs(const Command_line& cmdline,
1330 Incremental_inputs* incremental_inputs)
1331 { return this->do_check_inputs(cmdline, incremental_inputs); }
1333 // Report an error.
1334 void
1335 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
1337 // Proxy class for a sized Incremental_input_entry_reader.
1339 class Input_reader
1341 public:
1342 Input_reader()
1345 virtual
1346 ~Input_reader()
1349 const char*
1350 filename() const
1351 { return this->do_filename(); }
1353 Timespec
1354 get_mtime() const
1355 { return this->do_get_mtime(); }
1357 Incremental_input_type
1358 type() const
1359 { return this->do_type(); }
1361 unsigned int
1362 arg_serial() const
1363 { return this->do_arg_serial(); }
1365 unsigned int
1366 get_unused_symbol_count() const
1367 { return this->do_get_unused_symbol_count(); }
1369 const char*
1370 get_unused_symbol(unsigned int n) const
1371 { return this->do_get_unused_symbol(n); }
1373 protected:
1374 virtual const char*
1375 do_filename() const = 0;
1377 virtual Timespec
1378 do_get_mtime() const = 0;
1380 virtual Incremental_input_type
1381 do_type() const = 0;
1383 virtual unsigned int
1384 do_arg_serial() const = 0;
1386 virtual unsigned int
1387 do_get_unused_symbol_count() const = 0;
1389 virtual const char*
1390 do_get_unused_symbol(unsigned int n) const = 0;
1393 // Return the number of input files.
1394 unsigned int
1395 input_file_count() const
1396 { return this->do_input_file_count(); }
1398 // Return an Input_reader for input file N.
1399 const Input_reader*
1400 get_input_reader(unsigned int n) const
1401 { return this->do_get_input_reader(n); }
1403 // Return TRUE if the input file N has changed since the last link.
1404 bool
1405 file_has_changed(unsigned int n) const
1406 { return this->do_file_has_changed(n); }
1408 // Return the Input_argument for input file N. Returns NULL if
1409 // the Input_argument is not available.
1410 const Input_argument*
1411 get_input_argument(unsigned int n) const
1413 const Input_reader* input_file = this->do_get_input_reader(n);
1414 unsigned int arg_serial = input_file->arg_serial();
1415 if (arg_serial == 0 || arg_serial > this->input_args_map_.size())
1416 return NULL;
1417 return this->input_args_map_[arg_serial - 1];
1420 // Return an Incremental_library for the given input file.
1421 Incremental_library*
1422 get_library(unsigned int n) const
1423 { return this->library_map_[n]; }
1425 // Return a Script_info for the given input file.
1426 Script_info*
1427 get_script_info(unsigned int n) const
1428 { return this->script_map_[n]; }
1430 // Initialize the layout of the output file based on the existing
1431 // output file.
1432 void
1433 init_layout(Layout* layout)
1434 { this->do_init_layout(layout); }
1436 // Mark regions of the input file that must be kept unchanged.
1437 void
1438 reserve_layout(unsigned int input_file_index)
1439 { this->do_reserve_layout(input_file_index); }
1441 // Process the GOT and PLT entries from the existing output file.
1442 void
1443 process_got_plt(Symbol_table* symtab, Layout* layout)
1444 { this->do_process_got_plt(symtab, layout); }
1446 // Emit COPY relocations from the existing output file.
1447 void
1448 emit_copy_relocs(Symbol_table* symtab)
1449 { this->do_emit_copy_relocs(symtab); }
1451 // Apply incremental relocations for symbols whose values have changed.
1452 void
1453 apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
1454 Output_file* of)
1455 { this->do_apply_incremental_relocs(symtab, layout, of); }
1457 // Functions and types for the elfcpp::Elf_file interface. This
1458 // permit us to use Incremental_binary as the File template parameter for
1459 // elfcpp::Elf_file.
1461 // The View class is returned by view. It must support a single
1462 // method, data(). This is trivial, because Output_file::get_output_view
1463 // does what we need.
1464 class View
1466 public:
1467 View(const unsigned char* p)
1468 : p_(p)
1471 const unsigned char*
1472 data() const
1473 { return this->p_; }
1475 private:
1476 const unsigned char* p_;
1479 // Return a View.
1480 View
1481 view(off_t file_offset, section_size_type data_size)
1482 { return View(this->output_->get_input_view(file_offset, data_size)); }
1484 // A location in the file.
1485 struct Location
1487 off_t file_offset;
1488 off_t data_size;
1490 Location(off_t fo, section_size_type ds)
1491 : file_offset(fo), data_size(ds)
1494 Location()
1495 : file_offset(0), data_size(0)
1499 // Get a View given a Location.
1500 View
1501 view(Location loc)
1502 { return View(this->view(loc.file_offset, loc.data_size)); }
1504 // Return the Output_file.
1505 Output_file*
1506 output_file()
1507 { return this->output_; }
1509 protected:
1510 // Check the .gnu_incremental_inputs section to see whether an incremental
1511 // build is possible.
1512 virtual bool
1513 do_check_inputs(const Command_line& cmdline,
1514 Incremental_inputs* incremental_inputs) = 0;
1516 // Return TRUE if input file N has changed since the last incremental link.
1517 virtual bool
1518 do_file_has_changed(unsigned int n) const = 0;
1520 // Initialize the layout of the output file based on the existing
1521 // output file.
1522 virtual void
1523 do_init_layout(Layout* layout) = 0;
1525 // Mark regions of the input file that must be kept unchanged.
1526 virtual void
1527 do_reserve_layout(unsigned int input_file_index) = 0;
1529 // Process the GOT and PLT entries from the existing output file.
1530 virtual void
1531 do_process_got_plt(Symbol_table* symtab, Layout* layout) = 0;
1533 // Emit COPY relocations from the existing output file.
1534 virtual void
1535 do_emit_copy_relocs(Symbol_table* symtab) = 0;
1537 // Apply incremental relocations for symbols whose values have changed.
1538 virtual void
1539 do_apply_incremental_relocs(const Symbol_table*, Layout*, Output_file*) = 0;
1541 virtual unsigned int
1542 do_input_file_count() const = 0;
1544 virtual const Input_reader*
1545 do_get_input_reader(unsigned int) const = 0;
1547 // Map from input file index to Input_argument.
1548 std::vector<const Input_argument*> input_args_map_;
1549 // Map from an input file index to an Incremental_library.
1550 std::vector<Incremental_library*> library_map_;
1551 // Map from an input file index to a Script_info.
1552 std::vector<Script_info*> script_map_;
1554 private:
1555 // Edited output file object.
1556 Output_file* output_;
1557 // Target of the output file.
1558 Target* target_;
1561 template<int size, bool big_endian>
1562 class Sized_relobj_incr;
1564 template<int size, bool big_endian>
1565 class Sized_incremental_binary : public Incremental_binary
1567 public:
1568 Sized_incremental_binary(Output_file* output,
1569 const elfcpp::Ehdr<size, big_endian>& ehdr,
1570 Target* target)
1571 : Incremental_binary(output, target), elf_file_(this, ehdr),
1572 input_objects_(), section_map_(), symbol_map_(), copy_relocs_(),
1573 main_symtab_loc_(), main_strtab_loc_(), has_incremental_info_(false),
1574 inputs_reader_(), symtab_reader_(), relocs_reader_(), got_plt_reader_(),
1575 input_entry_readers_()
1576 { this->setup_readers(); }
1578 // Returns TRUE if the file contains incremental info.
1579 bool
1580 has_incremental_info() const
1581 { return this->has_incremental_info_; }
1583 // Record a pointer to the object for input file N.
1584 void
1585 set_input_object(unsigned int n,
1586 Sized_relobj_incr<size, big_endian>* obj)
1587 { this->input_objects_[n] = obj; }
1589 // Return a pointer to the object for input file N.
1590 Sized_relobj_incr<size, big_endian>*
1591 input_object(unsigned int n) const
1593 gold_assert(n < this->input_objects_.size());
1594 return this->input_objects_[n];
1597 // Return the Output_section for section index SHNDX.
1598 Output_section*
1599 output_section(unsigned int shndx)
1600 { return this->section_map_[shndx]; }
1602 // Map a symbol table entry from the base file to the output symbol table.
1603 // SYMNDX is relative to the first forced-local or global symbol in the
1604 // input file symbol table.
1605 void
1606 add_global_symbol(unsigned int symndx, Symbol* gsym)
1607 { this->symbol_map_[symndx] = gsym; }
1609 // Map a symbol table entry from the base file to the output symbol table.
1610 // SYMNDX is relative to the first forced-local or global symbol in the
1611 // input file symbol table.
1612 Symbol*
1613 global_symbol(unsigned int symndx) const
1614 { return this->symbol_map_[symndx]; }
1616 // Add a COPY relocation for a global symbol.
1617 void
1618 add_copy_reloc(Symbol* gsym, Output_section* os, off_t offset)
1619 { this->copy_relocs_.push_back(Copy_reloc(gsym, os, offset)); }
1621 // Readers for the incremental info sections.
1623 const Incremental_inputs_reader<size, big_endian>&
1624 inputs_reader() const
1625 { return this->inputs_reader_; }
1627 const Incremental_symtab_reader<big_endian>&
1628 symtab_reader() const
1629 { return this->symtab_reader_; }
1631 const Incremental_relocs_reader<size, big_endian>&
1632 relocs_reader() const
1633 { return this->relocs_reader_; }
1635 const Incremental_got_plt_reader<big_endian>&
1636 got_plt_reader() const
1637 { return this->got_plt_reader_; }
1639 void
1640 get_symtab_view(View* symtab_view, unsigned int* sym_count,
1641 elfcpp::Elf_strtab* strtab);
1643 protected:
1644 typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
1645 typedef typename Inputs_reader::Incremental_input_entry_reader
1646 Input_entry_reader;
1648 virtual bool
1649 do_check_inputs(const Command_line& cmdline,
1650 Incremental_inputs* incremental_inputs);
1652 // Return TRUE if input file N has changed since the last incremental link.
1653 virtual bool
1654 do_file_has_changed(unsigned int n) const;
1656 // Initialize the layout of the output file based on the existing
1657 // output file.
1658 virtual void
1659 do_init_layout(Layout* layout);
1661 // Mark regions of the input file that must be kept unchanged.
1662 virtual void
1663 do_reserve_layout(unsigned int input_file_index);
1665 // Process the GOT and PLT entries from the existing output file.
1666 virtual void
1667 do_process_got_plt(Symbol_table* symtab, Layout* layout);
1669 // Emit COPY relocations from the existing output file.
1670 virtual void
1671 do_emit_copy_relocs(Symbol_table* symtab);
1673 // Apply incremental relocations for symbols whose values have changed.
1674 virtual void
1675 do_apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
1676 Output_file* of);
1678 // Proxy class for a sized Incremental_input_entry_reader.
1680 class Sized_input_reader : public Input_reader
1682 public:
1683 Sized_input_reader(Input_entry_reader r)
1684 : Input_reader(), reader_(r)
1687 virtual
1688 ~Sized_input_reader()
1691 private:
1692 const char*
1693 do_filename() const
1694 { return this->reader_.filename(); }
1696 Timespec
1697 do_get_mtime() const
1698 { return this->reader_.get_mtime(); }
1700 Incremental_input_type
1701 do_type() const
1702 { return this->reader_.type(); }
1704 unsigned int
1705 do_arg_serial() const
1706 { return this->reader_.arg_serial(); }
1708 unsigned int
1709 do_get_unused_symbol_count() const
1710 { return this->reader_.get_unused_symbol_count(); }
1712 const char*
1713 do_get_unused_symbol(unsigned int n) const
1714 { return this->reader_.get_unused_symbol(n); }
1716 Input_entry_reader reader_;
1719 virtual unsigned int
1720 do_input_file_count() const
1721 { return this->inputs_reader_.input_file_count(); }
1723 virtual const Input_reader*
1724 do_get_input_reader(unsigned int n) const
1726 gold_assert(n < this->input_entry_readers_.size());
1727 return &this->input_entry_readers_[n];
1730 private:
1731 // List of symbols that need COPY relocations.
1732 struct Copy_reloc
1734 Copy_reloc(Symbol* sym, Output_section* os, off_t off)
1735 : symbol(sym), output_section(os), offset(off)
1738 // The global symbol to copy.
1739 Symbol* symbol;
1740 // The output section into which the symbol was copied.
1741 Output_section* output_section;
1742 // The offset within that output section.
1743 off_t offset;
1745 typedef std::vector<Copy_reloc> Copy_relocs;
1747 bool
1748 find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
1749 unsigned int* p_symtab_shndx,
1750 unsigned int* p_relocs_shndx,
1751 unsigned int* p_got_plt_shndx,
1752 unsigned int* p_strtab_shndx);
1754 void
1755 setup_readers();
1757 // Output as an ELF file.
1758 elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
1760 // Vector of pointers to the input objects for the unchanged files.
1761 // For replaced files, the corresponding pointer is NULL.
1762 std::vector<Sized_relobj_incr<size, big_endian>*> input_objects_;
1764 // Map section index to an Output_section in the updated layout.
1765 std::vector<Output_section*> section_map_;
1767 // Map global symbols from the input file to the symbol table.
1768 std::vector<Symbol*> symbol_map_;
1770 // List of symbols that need COPY relocations.
1771 Copy_relocs copy_relocs_;
1773 // Locations of the main symbol table and symbol string table.
1774 Location main_symtab_loc_;
1775 Location main_strtab_loc_;
1777 // Readers for the incremental info sections.
1778 bool has_incremental_info_;
1779 Incremental_inputs_reader<size, big_endian> inputs_reader_;
1780 Incremental_symtab_reader<big_endian> symtab_reader_;
1781 Incremental_relocs_reader<size, big_endian> relocs_reader_;
1782 Incremental_got_plt_reader<big_endian> got_plt_reader_;
1783 std::vector<Sized_input_reader> input_entry_readers_;
1786 // An incremental Relobj. This class represents a relocatable object
1787 // that has not changed since the last incremental link, and whose contents
1788 // can be used directly from the base file.
1790 template<int size, bool big_endian>
1791 class Sized_relobj_incr : public Sized_relobj<size, big_endian>
1793 public:
1794 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1795 typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
1797 Sized_relobj_incr(const std::string& name,
1798 Sized_incremental_binary<size, big_endian>* ibase,
1799 unsigned int input_file_index);
1801 private:
1802 // For convenience.
1803 typedef Sized_relobj_incr<size, big_endian> This;
1804 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1806 typedef typename Sized_relobj<size, big_endian>::Output_sections
1807 Output_sections;
1808 typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
1809 typedef typename Inputs_reader::Incremental_input_entry_reader
1810 Input_entry_reader;
1812 // A local symbol.
1813 struct Local_symbol
1815 Local_symbol(const char* name_, Address value_, unsigned int size_,
1816 unsigned int shndx_, unsigned int type_,
1817 bool needs_dynsym_entry_)
1818 : st_value(value_), name(name_), st_size(size_), st_shndx(shndx_),
1819 st_type(type_), output_dynsym_index(0),
1820 needs_dynsym_entry(needs_dynsym_entry_)
1822 // The symbol value.
1823 Address st_value;
1824 // The symbol name. This points to the stringpool entry.
1825 const char* name;
1826 // The symbol size.
1827 unsigned int st_size;
1828 // The output section index.
1829 unsigned int st_shndx : 28;
1830 // The symbol type.
1831 unsigned int st_type : 4;
1832 // The index of the symbol in the output dynamic symbol table.
1833 unsigned int output_dynsym_index : 31;
1834 // TRUE if the symbol needs to appear in the dynamic symbol table.
1835 unsigned int needs_dynsym_entry : 1;
1838 // Return TRUE if this is an incremental (unchanged) input file.
1839 bool
1840 do_is_incremental() const
1841 { return true; }
1843 // Return the last modified time of the file.
1844 Timespec
1845 do_get_mtime()
1846 { return this->input_reader_.get_mtime(); }
1848 // Read the symbols.
1849 void
1850 do_read_symbols(Read_symbols_data*);
1852 // Lay out the input sections.
1853 void
1854 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1856 // Layout sections whose layout was deferred while waiting for
1857 // input files from a plugin.
1858 void
1859 do_layout_deferred_sections(Layout*);
1861 // Add the symbols to the symbol table.
1862 void
1863 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
1865 Archive::Should_include
1866 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
1867 std::string* why);
1869 // Iterate over global symbols, calling a visitor class V for each.
1870 void
1871 do_for_all_global_symbols(Read_symbols_data* sd,
1872 Library_base::Symbol_visitor_base* v);
1874 // Get the size of a section.
1875 uint64_t
1876 do_section_size(unsigned int shndx);
1878 // Get the name of a section.
1879 std::string
1880 do_section_name(unsigned int shndx);
1882 // Return a view of the contents of a section.
1883 Object::Location
1884 do_section_contents(unsigned int shndx);
1886 // Return section flags.
1887 uint64_t
1888 do_section_flags(unsigned int shndx);
1890 // Return section entsize.
1891 uint64_t
1892 do_section_entsize(unsigned int shndx);
1894 // Return section address.
1895 uint64_t
1896 do_section_address(unsigned int shndx);
1898 // Return section type.
1899 unsigned int
1900 do_section_type(unsigned int shndx);
1902 // Return the section link field.
1903 unsigned int
1904 do_section_link(unsigned int shndx);
1906 // Return the section link field.
1907 unsigned int
1908 do_section_info(unsigned int shndx);
1910 // Return the section alignment.
1911 uint64_t
1912 do_section_addralign(unsigned int shndx);
1914 // Return the Xindex structure to use.
1915 Xindex*
1916 do_initialize_xindex();
1918 // Get symbol counts.
1919 void
1920 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1922 // Get global symbols.
1923 const Symbols*
1924 do_get_global_symbols() const
1925 { return &this->symbols_; }
1927 // Return the value of a local symbol.
1928 uint64_t
1929 do_local_symbol_value(unsigned int, uint64_t) const
1930 { gold_unreachable(); }
1932 unsigned int
1933 do_local_plt_offset(unsigned int) const
1934 { gold_unreachable(); }
1936 // Return the number of local symbols.
1937 unsigned int
1938 do_local_symbol_count() const
1939 { return this->local_symbol_count_; }
1941 // Return the number of local symbols in the output symbol table.
1942 unsigned int
1943 do_output_local_symbol_count() const
1944 { return this->local_symbol_count_; }
1946 // Return the file offset for local symbols in the output symbol table.
1947 off_t
1948 do_local_symbol_offset() const
1949 { return this->local_symbol_offset_; }
1951 // Read the relocs.
1952 void
1953 do_read_relocs(Read_relocs_data*);
1955 // Process the relocs to find list of referenced sections. Used only
1956 // during garbage collection.
1957 void
1958 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1960 // Scan the relocs and adjust the symbol table.
1961 void
1962 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1964 // Count the local symbols.
1965 void
1966 do_count_local_symbols(Stringpool_template<char>*,
1967 Stringpool_template<char>*);
1969 // Finalize the local symbols.
1970 unsigned int
1971 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
1973 // Set the offset where local dynamic symbol information will be stored.
1974 unsigned int
1975 do_set_local_dynsym_indexes(unsigned int);
1977 // Set the offset where local dynamic symbol information will be stored.
1978 unsigned int
1979 do_set_local_dynsym_offset(off_t);
1981 // Relocate the input sections and write out the local symbols.
1982 void
1983 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
1985 // Set the offset of a section.
1986 void
1987 do_set_section_offset(unsigned int shndx, uint64_t off);
1989 // The Incremental_binary base file.
1990 Sized_incremental_binary<size, big_endian>* ibase_;
1991 // The index of the object in the input file list.
1992 unsigned int input_file_index_;
1993 // The reader for the input file.
1994 Input_entry_reader input_reader_;
1995 // The number of local symbols.
1996 unsigned int local_symbol_count_;
1997 // The number of local symbols which go into the output file's dynamic
1998 // symbol table.
1999 unsigned int output_local_dynsym_count_;
2000 // This starting symbol index in the output symbol table.
2001 unsigned int local_symbol_index_;
2002 // The file offset for local symbols in the output symbol table.
2003 unsigned int local_symbol_offset_;
2004 // The file offset for local symbols in the output symbol table.
2005 unsigned int local_dynsym_offset_;
2006 // The entries in the symbol table for the external symbols.
2007 Symbols symbols_;
2008 // Number of symbols defined in object file itself.
2009 size_t defined_count_;
2010 // The offset of the first incremental relocation for this object.
2011 unsigned int incr_reloc_offset_;
2012 // The number of incremental relocations for this object.
2013 unsigned int incr_reloc_count_;
2014 // The index of the first incremental relocation for this object in the
2015 // updated output file.
2016 unsigned int incr_reloc_output_index_;
2017 // A copy of the incremental relocations from this object.
2018 unsigned char* incr_relocs_;
2019 // The local symbols.
2020 std::vector<Local_symbol> local_symbols_;
2023 // An incremental Dynobj. This class represents a shared object that has
2024 // not changed since the last incremental link, and whose contents can be
2025 // used directly from the base file.
2027 template<int size, bool big_endian>
2028 class Sized_incr_dynobj : public Dynobj
2030 public:
2031 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2033 static const Address invalid_address = static_cast<Address>(0) - 1;
2035 Sized_incr_dynobj(const std::string& name,
2036 Sized_incremental_binary<size, big_endian>* ibase,
2037 unsigned int input_file_index);
2039 private:
2040 typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
2041 typedef typename Inputs_reader::Incremental_input_entry_reader
2042 Input_entry_reader;
2044 // Return TRUE if this is an incremental (unchanged) input file.
2045 bool
2046 do_is_incremental() const
2047 { return true; }
2049 // Return the last modified time of the file.
2050 Timespec
2051 do_get_mtime()
2052 { return this->input_reader_.get_mtime(); }
2054 // Read the symbols.
2055 void
2056 do_read_symbols(Read_symbols_data*);
2058 // Lay out the input sections.
2059 void
2060 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
2062 // Add the symbols to the symbol table.
2063 void
2064 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
2066 Archive::Should_include
2067 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
2068 std::string* why);
2070 // Iterate over global symbols, calling a visitor class V for each.
2071 void
2072 do_for_all_global_symbols(Read_symbols_data* sd,
2073 Library_base::Symbol_visitor_base* v);
2075 // Iterate over local symbols, calling a visitor class V for each GOT offset
2076 // associated with a local symbol.
2077 void
2078 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
2080 // Get the size of a section.
2081 uint64_t
2082 do_section_size(unsigned int shndx);
2084 // Get the name of a section.
2085 std::string
2086 do_section_name(unsigned int shndx);
2088 // Return a view of the contents of a section.
2089 Object::Location
2090 do_section_contents(unsigned int shndx);
2092 // Return section flags.
2093 uint64_t
2094 do_section_flags(unsigned int shndx);
2096 // Return section entsize.
2097 uint64_t
2098 do_section_entsize(unsigned int shndx);
2100 // Return section address.
2101 uint64_t
2102 do_section_address(unsigned int shndx);
2104 // Return section type.
2105 unsigned int
2106 do_section_type(unsigned int shndx);
2108 // Return the section link field.
2109 unsigned int
2110 do_section_link(unsigned int shndx);
2112 // Return the section link field.
2113 unsigned int
2114 do_section_info(unsigned int shndx);
2116 // Return the section alignment.
2117 uint64_t
2118 do_section_addralign(unsigned int shndx);
2120 // Return the Xindex structure to use.
2121 Xindex*
2122 do_initialize_xindex();
2124 // Get symbol counts.
2125 void
2126 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
2128 // Get global symbols.
2129 const Symbols*
2130 do_get_global_symbols() const
2131 { return &this->symbols_; }
2133 // The Incremental_binary base file.
2134 Sized_incremental_binary<size, big_endian>* ibase_;
2135 // The index of the object in the input file list.
2136 unsigned int input_file_index_;
2137 // The reader for the input file.
2138 Input_entry_reader input_reader_;
2139 // The entries in the symbol table for the external symbols.
2140 Symbols symbols_;
2141 // Number of symbols defined in object file itself.
2142 size_t defined_count_;
2145 // Allocate an incremental object of the appropriate size and endianness.
2146 extern Object*
2147 make_sized_incremental_object(
2148 Incremental_binary* base,
2149 unsigned int input_file_index,
2150 Incremental_input_type input_type,
2151 const Incremental_binary::Input_reader* input_reader);
2153 // This class represents an Archive library (or --start-lib/--end-lib group)
2154 // that has not changed since the last incremental link. Its contents come
2155 // from the incremental inputs entry in the base file.
2157 class Incremental_library : public Library_base
2159 public:
2160 Incremental_library(const char* filename, unsigned int input_file_index,
2161 const Incremental_binary::Input_reader* input_reader)
2162 : Library_base(NULL), filename_(filename),
2163 input_file_index_(input_file_index), input_reader_(input_reader),
2164 unused_symbols_(), is_reported_(false)
2167 // Return the input file index.
2168 unsigned int
2169 input_file_index() const
2170 { return this->input_file_index_; }
2172 // Return the serial number of the input file.
2173 unsigned int
2174 arg_serial() const
2175 { return this->input_reader_->arg_serial(); }
2177 // Copy the unused symbols from the incremental input info.
2178 // We need to do this because we may be overwriting the incremental
2179 // input info in the base file before we write the new incremental
2180 // info.
2181 void
2182 copy_unused_symbols();
2184 // Return FALSE on the first call to indicate that the library needs
2185 // to be recorded; return TRUE subsequently.
2186 bool
2187 is_reported()
2189 bool was_reported = this->is_reported_;
2190 is_reported_ = true;
2191 return was_reported;
2194 private:
2195 typedef std::vector<std::string> Symbol_list;
2197 // The file name.
2198 const std::string&
2199 do_filename() const
2200 { return this->filename_; }
2202 // Return the modification time of the archive file.
2203 Timespec
2204 do_get_mtime()
2205 { return this->input_reader_->get_mtime(); }
2207 // Iterator for unused global symbols in the library.
2208 void
2209 do_for_all_unused_symbols(Symbol_visitor_base* v) const;
2211 // The name of the library.
2212 std::string filename_;
2213 // The input file index of this library.
2214 unsigned int input_file_index_;
2215 // A reader for the incremental input information.
2216 const Incremental_binary::Input_reader* input_reader_;
2217 // List of unused symbols defined in this library.
2218 Symbol_list unused_symbols_;
2219 // TRUE when this library has been reported to the new incremental info.
2220 bool is_reported_;
2223 } // End namespace gold.
2225 #endif // !defined(GOLD_INCREMENTAL_H)