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