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