* incremental-dump.cc (dump_incremental_inputs): Print dynamic reloc
[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_ + 24
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 index of the first dynamic relocation -- for objects only.
781 unsigned int
782 get_first_dyn_reloc() const
783 {
784 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
785 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
786
787 return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 16);
788 }
789
790 // Return the dynamic relocation count -- for objects only.
791 unsigned int
792 get_dyn_reloc_count() const
793 {
794 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
795 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
796
797 return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 20);
798 }
799
800 // Return the object count -- for scripts only.
801 unsigned int
802 get_object_count() const
803 {
804 gold_assert(this->type() == INCREMENTAL_INPUT_SCRIPT);
805 return Swap32::readval(this->inputs_->p_ + this->info_offset_);
806 }
807
808 // Return the input file offset for object N -- for scripts only.
809 unsigned int
810 get_object_offset(unsigned int n) const
811 {
812 gold_assert(this->type() == INCREMENTAL_INPUT_SCRIPT);
813 return Swap32::readval(this->inputs_->p_ + this->info_offset_
814 + 4 + n * 4);
815 }
816
817 // Return the member count -- for archives only.
818 unsigned int
819 get_member_count() const
820 {
821 gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
822 return Swap32::readval(this->inputs_->p_ + this->info_offset_);
823 }
824
825 // Return the unused symbol count -- for archives only.
826 unsigned int
827 get_unused_symbol_count() const
828 {
829 gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
830 return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4);
831 }
832
833 // Return the input file offset for archive member N -- for archives only.
834 unsigned int
835 get_member_offset(unsigned int n) const
836 {
837 gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
838 return Swap32::readval(this->inputs_->p_ + this->info_offset_
839 + 8 + n * 4);
840 }
841
842 // Return the Nth unused global symbol -- for archives only.
843 const char*
844 get_unused_symbol(unsigned int n) const
845 {
846 gold_assert(this->type() == INCREMENTAL_INPUT_ARCHIVE);
847 unsigned int member_count = this->get_member_count();
848 unsigned int offset = Swap32::readval(this->inputs_->p_
849 + this->info_offset_ + 8
850 + member_count * 4
851 + n * 4);
852 return this->inputs_->get_string(offset);
853 }
854
855 // Information about an input section.
856 struct Input_section_info
857 {
858 const char* name;
859 unsigned int output_shndx;
860 off_t sh_offset;
861 off_t sh_size;
862 };
863
864 // Return info about the Nth input section -- for objects only.
865 Input_section_info
866 get_input_section(unsigned int n) const
867 {
868 Input_section_info info;
869 const unsigned char* p = (this->inputs_->p_
870 + this->info_offset_ + 24
871 + n * input_section_entry_size);
872 unsigned int name_offset = Swap32::readval(p);
873 info.name = this->inputs_->get_string(name_offset);
874 info.output_shndx = Swap32::readval(p + 4);
875 info.sh_offset = Swap::readval(p + 8);
876 info.sh_size = Swap::readval(p + 8 + size / 8);
877 return info;
878 }
879
880 // Return info about the Nth global symbol -- for objects only.
881 Incremental_global_symbol_reader<big_endian>
882 get_global_symbol_reader(unsigned int n) const
883 {
884 gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
885 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
886 unsigned int section_count = this->get_input_section_count();
887 const unsigned char* p = (this->inputs_->p_
888 + this->info_offset_ + 24
889 + section_count * input_section_entry_size
890 + n * 20);
891 return Incremental_global_symbol_reader<big_endian>(p);
892 }
893
894 // Return the output symbol index for the Nth global symbol -- for shared
895 // libraries only. Sets *IS_DEF to TRUE if the symbol is defined in this
896 // input file.
897 unsigned int
898 get_output_symbol_index(unsigned int n, bool* is_def)
899 {
900 gold_assert(this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
901 const unsigned char* p = (this->inputs_->p_
902 + this->info_offset_ + 4
903 + n * 4);
904 unsigned int output_symndx = Swap32::readval(p);
905 *is_def = (output_symndx & (1U << 31)) != 0;
906 return output_symndx & ((1U << 31) - 1);
907 }
908
909 private:
910 // Size of an input section entry.
911 static const unsigned int input_section_entry_size = 8 + 2 * size / 8;
912 // The reader instance for the containing section.
913 const Incremental_inputs_reader* inputs_;
914 // The flags, including the type of input file.
915 unsigned int flags_;
916 // Section offset to the input file entry.
917 unsigned int offset_;
918 // Section offset to the supplemental info for the input file.
919 unsigned int info_offset_;
920 };
921
922 // Return the offset of an input file entry given its index N.
923 unsigned int
924 input_file_offset(unsigned int n) const
925 {
926 gold_assert(n < this->input_file_count_);
927 return 16 + n * 24;
928 }
929
930 // Return the index of an input file entry given its OFFSET.
931 unsigned int
932 input_file_index(unsigned int offset) const
933 {
934 int n = (offset - 16) / 24;
935 gold_assert(input_file_offset(n) == offset);
936 return n;
937 }
938
939 // Return a reader for the Nth input file entry.
940 Incremental_input_entry_reader
941 input_file(unsigned int n) const
942 { return Incremental_input_entry_reader(this, this->input_file_offset(n)); }
943
944 // Return a reader for the input file entry at OFFSET.
945 Incremental_input_entry_reader
946 input_file_at_offset(unsigned int offset) const
947 {
948 gold_assert(offset < 16 + this->input_file_count_ * 24);
949 return Incremental_input_entry_reader(this, offset);
950 }
951
952 // Return a reader for the global symbol info at OFFSET.
953 Incremental_global_symbol_reader<big_endian>
954 global_symbol_reader_at_offset(unsigned int offset) const
955 {
956 const unsigned char* p = this->p_ + offset;
957 return Incremental_global_symbol_reader<big_endian>(p);
958 }
959
960 private:
961 // Lookup a string in the ELF string table.
962 const char* get_string(unsigned int offset) const
963 {
964 const char* s;
965 if (this->strtab_.get_c_string(offset, &s))
966 return s;
967 return NULL;
968 }
969
970 // Base address of the .gnu_incremental_inputs section.
971 const unsigned char* p_;
972 // The associated ELF string table.
973 elfcpp::Elf_strtab strtab_;
974 // The number of input file entries in this section.
975 unsigned int input_file_count_;
976 };
977
978 // Reader class for the .gnu_incremental_symtab section.
979
980 template<bool big_endian>
981 class Incremental_symtab_reader
982 {
983 public:
984 Incremental_symtab_reader()
985 : p_(NULL), len_(0)
986 { }
987
988 Incremental_symtab_reader(const unsigned char* p, off_t len)
989 : p_(p), len_(len)
990 { }
991
992 // Return the count of symbols in this section.
993 unsigned int
994 symbol_count() const
995 { return static_cast<unsigned int>(this->len_ / 4); }
996
997 // Return the list head for symbol table entry N.
998 unsigned int
999 get_list_head(unsigned int n) const
1000 { return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4 * n); }
1001
1002 private:
1003 // Base address of the .gnu_incremental_relocs section.
1004 const unsigned char* p_;
1005 // Size of the section.
1006 off_t len_;
1007 };
1008
1009 // Reader class for the .gnu_incremental_relocs section.
1010
1011 template<int size, bool big_endian>
1012 class Incremental_relocs_reader
1013 {
1014 private:
1015 // Size of each field.
1016 static const unsigned int field_size = size / 8;
1017
1018 public:
1019 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1020 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
1021
1022 // Size of each entry.
1023 static const unsigned int reloc_size = 8 + 2 * field_size;
1024
1025 Incremental_relocs_reader()
1026 : p_(NULL), len_(0)
1027 { }
1028
1029 Incremental_relocs_reader(const unsigned char* p, off_t len)
1030 : p_(p), len_(len)
1031 { }
1032
1033 // Return the count of relocations in this section.
1034 unsigned int
1035 reloc_count() const
1036 { return static_cast<unsigned int>(this->len_ / reloc_size); }
1037
1038 // Return the relocation type for relocation entry at offset OFF.
1039 unsigned int
1040 get_r_type(unsigned int off) const
1041 { return elfcpp::Swap<32, big_endian>::readval(this->p_ + off); }
1042
1043 // Return the output section index for relocation entry at offset OFF.
1044 unsigned int
1045 get_r_shndx(unsigned int off) const
1046 { return elfcpp::Swap<32, big_endian>::readval(this->p_ + off + 4); }
1047
1048 // Return the output section offset for relocation entry at offset OFF.
1049 Address
1050 get_r_offset(unsigned int off) const
1051 { return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8); }
1052
1053 // Return the addend for relocation entry at offset OFF.
1054 Addend
1055 get_r_addend(unsigned int off) const
1056 {
1057 return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8
1058 + this->field_size);
1059 }
1060
1061 // Return a pointer to the relocation entry at offset OFF.
1062 const unsigned char*
1063 data(unsigned int off) const
1064 { return this->p_ + off; }
1065
1066 private:
1067 // Base address of the .gnu_incremental_relocs section.
1068 const unsigned char* p_;
1069 // Size of the section.
1070 off_t len_;
1071 };
1072
1073 // Reader class for the .gnu_incremental_got_plt section.
1074
1075 template<bool big_endian>
1076 class Incremental_got_plt_reader
1077 {
1078 public:
1079 Incremental_got_plt_reader()
1080 : p_(NULL), got_count_(0), got_desc_p_(NULL), plt_desc_p_(NULL)
1081 { }
1082
1083 Incremental_got_plt_reader(const unsigned char* p) : p_(p)
1084 {
1085 this->got_count_ = elfcpp::Swap<32, big_endian>::readval(p);
1086 this->got_desc_p_ = p + 8 + ((this->got_count_ + 3) & ~3);
1087 this->plt_desc_p_ = this->got_desc_p_ + this->got_count_ * 8;
1088 }
1089
1090 // Return the GOT entry count.
1091 unsigned int
1092 get_got_entry_count() const
1093 {
1094 return this->got_count_;
1095 }
1096
1097 // Return the PLT entry count.
1098 unsigned int
1099 get_plt_entry_count() const
1100 {
1101 return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4);
1102 }
1103
1104 // Return the GOT type for GOT entry N.
1105 unsigned int
1106 get_got_type(unsigned int n)
1107 {
1108 return this->p_[8 + n];
1109 }
1110
1111 // Return the symbol index for GOT entry N.
1112 unsigned int
1113 get_got_symndx(unsigned int n)
1114 {
1115 return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 8);
1116 }
1117
1118 // Return the input file index for GOT entry N.
1119 unsigned int
1120 get_got_input_index(unsigned int n)
1121 {
1122 return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 8 + 4);
1123 }
1124
1125 // Return the PLT descriptor for PLT entry N.
1126 unsigned int
1127 get_plt_desc(unsigned int n)
1128 {
1129 return elfcpp::Swap<32, big_endian>::readval(this->plt_desc_p_ + n * 4);
1130 }
1131
1132 private:
1133 // Base address of the .gnu_incremental_got_plt section.
1134 const unsigned char* p_;
1135 // GOT entry count.
1136 unsigned int got_count_;
1137 // Base address of the GOT descriptor array.
1138 const unsigned char* got_desc_p_;
1139 // Base address of the PLT descriptor array.
1140 const unsigned char* plt_desc_p_;
1141 };
1142
1143 // An object representing the ELF file we edit during an incremental build.
1144 // Similar to Object or Dynobj, but operates on Output_file and contains
1145 // methods to support incremental updating. This is the abstract parent class
1146 // implemented in Sized_incremental_binary<size, big_endian> for a specific
1147 // endianness and size.
1148
1149 class Incremental_binary
1150 {
1151 public:
1152 Incremental_binary(Output_file* output, Target* target)
1153 : input_args_map_(), library_map_(), script_map_(),
1154 output_(output), target_(target)
1155 { }
1156
1157 virtual
1158 ~Incremental_binary()
1159 { }
1160
1161 // Check the .gnu_incremental_inputs section to see whether an incremental
1162 // build is possible.
1163 bool
1164 check_inputs(const Command_line& cmdline,
1165 Incremental_inputs* incremental_inputs)
1166 { return this->do_check_inputs(cmdline, incremental_inputs); }
1167
1168 // Report an error.
1169 void
1170 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
1171
1172 // Proxy class for a sized Incremental_input_entry_reader.
1173
1174 class Input_reader
1175 {
1176 public:
1177 Input_reader()
1178 { }
1179
1180 virtual
1181 ~Input_reader()
1182 { }
1183
1184 const char*
1185 filename() const
1186 { return this->do_filename(); }
1187
1188 Timespec
1189 get_mtime() const
1190 { return this->do_get_mtime(); }
1191
1192 Incremental_input_type
1193 type() const
1194 { return this->do_type(); }
1195
1196 unsigned int
1197 arg_serial() const
1198 { return this->do_arg_serial(); }
1199
1200 unsigned int
1201 get_unused_symbol_count() const
1202 { return this->do_get_unused_symbol_count(); }
1203
1204 const char*
1205 get_unused_symbol(unsigned int n) const
1206 { return this->do_get_unused_symbol(n); }
1207
1208 protected:
1209 virtual const char*
1210 do_filename() const = 0;
1211
1212 virtual Timespec
1213 do_get_mtime() const = 0;
1214
1215 virtual Incremental_input_type
1216 do_type() const = 0;
1217
1218 virtual unsigned int
1219 do_arg_serial() const = 0;
1220
1221 virtual unsigned int
1222 do_get_unused_symbol_count() const = 0;
1223
1224 virtual const char*
1225 do_get_unused_symbol(unsigned int n) const = 0;
1226 };
1227
1228 // Return the number of input files.
1229 unsigned int
1230 input_file_count() const
1231 { return this->do_input_file_count(); }
1232
1233 // Return an Input_reader for input file N.
1234 const Input_reader*
1235 get_input_reader(unsigned int n) const
1236 { return this->do_get_input_reader(n); }
1237
1238 // Return TRUE if the input file N has changed since the last link.
1239 bool
1240 file_has_changed(unsigned int n) const
1241 { return this->do_file_has_changed(n); }
1242
1243 // Return the Input_argument for input file N. Returns NULL if
1244 // the Input_argument is not available.
1245 const Input_argument*
1246 get_input_argument(unsigned int n) const
1247 {
1248 const Input_reader* input_file = this->do_get_input_reader(n);
1249 unsigned int arg_serial = input_file->arg_serial();
1250 if (arg_serial == 0 || arg_serial > this->input_args_map_.size())
1251 return NULL;
1252 return this->input_args_map_[arg_serial - 1];
1253 }
1254
1255 // Return an Incremental_library for the given input file.
1256 Incremental_library*
1257 get_library(unsigned int n)
1258 { return this->library_map_[n]; }
1259
1260 // Return a Script_info for the given input file.
1261 Script_info*
1262 get_script_info(unsigned int n)
1263 { return this->script_map_[n]; }
1264
1265 // Initialize the layout of the output file based on the existing
1266 // output file.
1267 void
1268 init_layout(Layout* layout)
1269 { this->do_init_layout(layout); }
1270
1271 // Mark regions of the input file that must be kept unchanged.
1272 void
1273 reserve_layout(unsigned int input_file_index)
1274 { this->do_reserve_layout(input_file_index); }
1275
1276 // Process the GOT and PLT entries from the existing output file.
1277 void
1278 process_got_plt(Symbol_table* symtab, Layout* layout)
1279 { this->do_process_got_plt(symtab, layout); }
1280
1281 // Apply incremental relocations for symbols whose values have changed.
1282 void
1283 apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
1284 Output_file* of)
1285 { this->do_apply_incremental_relocs(symtab, layout, of); }
1286
1287 // Functions and types for the elfcpp::Elf_file interface. This
1288 // permit us to use Incremental_binary as the File template parameter for
1289 // elfcpp::Elf_file.
1290
1291 // The View class is returned by view. It must support a single
1292 // method, data(). This is trivial, because Output_file::get_output_view
1293 // does what we need.
1294 class View
1295 {
1296 public:
1297 View(const unsigned char* p)
1298 : p_(p)
1299 { }
1300
1301 const unsigned char*
1302 data() const
1303 { return this->p_; }
1304
1305 private:
1306 const unsigned char* p_;
1307 };
1308
1309 // Return a View.
1310 View
1311 view(off_t file_offset, section_size_type data_size)
1312 { return View(this->output_->get_input_view(file_offset, data_size)); }
1313
1314 // A location in the file.
1315 struct Location
1316 {
1317 off_t file_offset;
1318 off_t data_size;
1319
1320 Location(off_t fo, section_size_type ds)
1321 : file_offset(fo), data_size(ds)
1322 { }
1323
1324 Location()
1325 : file_offset(0), data_size(0)
1326 { }
1327 };
1328
1329 // Get a View given a Location.
1330 View
1331 view(Location loc)
1332 { return View(this->view(loc.file_offset, loc.data_size)); }
1333
1334 // Return the Output_file.
1335 Output_file*
1336 output_file()
1337 { return this->output_; }
1338
1339 protected:
1340 // Check the .gnu_incremental_inputs section to see whether an incremental
1341 // build is possible.
1342 virtual bool
1343 do_check_inputs(const Command_line& cmdline,
1344 Incremental_inputs* incremental_inputs) = 0;
1345
1346 // Return TRUE if input file N has changed since the last incremental link.
1347 virtual bool
1348 do_file_has_changed(unsigned int n) const = 0;
1349
1350 // Initialize the layout of the output file based on the existing
1351 // output file.
1352 virtual void
1353 do_init_layout(Layout* layout) = 0;
1354
1355 // Mark regions of the input file that must be kept unchanged.
1356 virtual void
1357 do_reserve_layout(unsigned int input_file_index) = 0;
1358
1359 // Process the GOT and PLT entries from the existing output file.
1360 virtual void
1361 do_process_got_plt(Symbol_table* symtab, Layout* layout) = 0;
1362
1363 // Apply incremental relocations for symbols whose values have changed.
1364 virtual void
1365 do_apply_incremental_relocs(const Symbol_table*, Layout*, Output_file*) = 0;
1366
1367 virtual unsigned int
1368 do_input_file_count() const = 0;
1369
1370 virtual const Input_reader*
1371 do_get_input_reader(unsigned int) const = 0;
1372
1373 // Map from input file index to Input_argument.
1374 std::vector<const Input_argument*> input_args_map_;
1375 // Map from an input file index to an Incremental_library.
1376 std::vector<Incremental_library*> library_map_;
1377 // Map from an input file index to a Script_info.
1378 std::vector<Script_info*> script_map_;
1379
1380 private:
1381 // Edited output file object.
1382 Output_file* output_;
1383 // Target of the output file.
1384 Target* target_;
1385 };
1386
1387 template<int size, bool big_endian>
1388 class Sized_relobj_incr;
1389
1390 template<int size, bool big_endian>
1391 class Sized_incremental_binary : public Incremental_binary
1392 {
1393 public:
1394 Sized_incremental_binary(Output_file* output,
1395 const elfcpp::Ehdr<size, big_endian>& ehdr,
1396 Target* target)
1397 : Incremental_binary(output, target), elf_file_(this, ehdr),
1398 input_objects_(), section_map_(), symbol_map_(), main_symtab_loc_(),
1399 main_strtab_loc_(), has_incremental_info_(false), inputs_reader_(),
1400 symtab_reader_(), relocs_reader_(), got_plt_reader_(),
1401 input_entry_readers_()
1402 { this->setup_readers(); }
1403
1404 // Returns TRUE if the file contains incremental info.
1405 bool
1406 has_incremental_info() const
1407 { return this->has_incremental_info_; }
1408
1409 // Record a pointer to the object for input file N.
1410 void
1411 set_input_object(unsigned int n,
1412 Sized_relobj_incr<size, big_endian>* obj)
1413 { this->input_objects_[n] = obj; }
1414
1415 // Return a pointer to the object for input file N.
1416 Sized_relobj_incr<size, big_endian>*
1417 input_object(unsigned int n) const
1418 {
1419 gold_assert(n < this->input_objects_.size());
1420 return this->input_objects_[n];
1421 }
1422
1423 // Return the Output_section for section index SHNDX.
1424 Output_section*
1425 output_section(unsigned int shndx)
1426 { return this->section_map_[shndx]; }
1427
1428 // Map a symbol table entry from the base file to the output symbol table.
1429 // SYMNDX is relative to the first forced-local or global symbol in the
1430 // input file symbol table.
1431 void
1432 add_global_symbol(unsigned int symndx, Symbol* gsym)
1433 { this->symbol_map_[symndx] = gsym; }
1434
1435 // Map a symbol table entry from the base file to the output symbol table.
1436 // SYMNDX is relative to the first forced-local or global symbol in the
1437 // input file symbol table.
1438 Symbol*
1439 global_symbol(unsigned int symndx) const
1440 { return this->symbol_map_[symndx]; }
1441
1442 // Readers for the incremental info sections.
1443
1444 const Incremental_inputs_reader<size, big_endian>&
1445 inputs_reader() const
1446 { return this->inputs_reader_; }
1447
1448 const Incremental_symtab_reader<big_endian>&
1449 symtab_reader() const
1450 { return this->symtab_reader_; }
1451
1452 const Incremental_relocs_reader<size, big_endian>&
1453 relocs_reader() const
1454 { return this->relocs_reader_; }
1455
1456 const Incremental_got_plt_reader<big_endian>&
1457 got_plt_reader() const
1458 { return this->got_plt_reader_; }
1459
1460 void
1461 get_symtab_view(View* symtab_view, unsigned int* sym_count,
1462 elfcpp::Elf_strtab* strtab);
1463
1464 protected:
1465 typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
1466 typedef typename Inputs_reader::Incremental_input_entry_reader
1467 Input_entry_reader;
1468
1469 virtual bool
1470 do_check_inputs(const Command_line& cmdline,
1471 Incremental_inputs* incremental_inputs);
1472
1473 // Return TRUE if input file N has changed since the last incremental link.
1474 virtual bool
1475 do_file_has_changed(unsigned int n) const;
1476
1477 // Initialize the layout of the output file based on the existing
1478 // output file.
1479 virtual void
1480 do_init_layout(Layout* layout);
1481
1482 // Mark regions of the input file that must be kept unchanged.
1483 virtual void
1484 do_reserve_layout(unsigned int input_file_index);
1485
1486 // Process the GOT and PLT entries from the existing output file.
1487 virtual void
1488 do_process_got_plt(Symbol_table* symtab, Layout* layout);
1489
1490 // Apply incremental relocations for symbols whose values have changed.
1491 virtual void
1492 do_apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
1493 Output_file* of);
1494
1495 // Proxy class for a sized Incremental_input_entry_reader.
1496
1497 class Sized_input_reader : public Input_reader
1498 {
1499 public:
1500 Sized_input_reader(Input_entry_reader r)
1501 : Input_reader(), reader_(r)
1502 { }
1503
1504 virtual
1505 ~Sized_input_reader()
1506 { }
1507
1508 private:
1509 const char*
1510 do_filename() const
1511 { return this->reader_.filename(); }
1512
1513 Timespec
1514 do_get_mtime() const
1515 { return this->reader_.get_mtime(); }
1516
1517 Incremental_input_type
1518 do_type() const
1519 { return this->reader_.type(); }
1520
1521 unsigned int
1522 do_arg_serial() const
1523 { return this->reader_.arg_serial(); }
1524
1525 unsigned int
1526 do_get_unused_symbol_count() const
1527 { return this->reader_.get_unused_symbol_count(); }
1528
1529 const char*
1530 do_get_unused_symbol(unsigned int n) const
1531 { return this->reader_.get_unused_symbol(n); }
1532
1533 Input_entry_reader reader_;
1534 };
1535
1536 virtual unsigned int
1537 do_input_file_count() const
1538 { return this->inputs_reader_.input_file_count(); }
1539
1540 virtual const Input_reader*
1541 do_get_input_reader(unsigned int n) const
1542 {
1543 gold_assert(n < this->input_entry_readers_.size());
1544 return &this->input_entry_readers_[n];
1545 }
1546
1547 private:
1548 bool
1549 find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
1550 unsigned int* p_symtab_shndx,
1551 unsigned int* p_relocs_shndx,
1552 unsigned int* p_got_plt_shndx,
1553 unsigned int* p_strtab_shndx);
1554
1555 void
1556 setup_readers();
1557
1558 // Output as an ELF file.
1559 elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
1560
1561 // Vector of pointers to the input objects for the unchanged files.
1562 // For replaced files, the corresponding pointer is NULL.
1563 std::vector<Sized_relobj_incr<size, big_endian>*> input_objects_;
1564
1565 // Map section index to an Output_section in the updated layout.
1566 std::vector<Output_section*> section_map_;
1567
1568 // Map global symbols from the input file to the symbol table.
1569 std::vector<Symbol*> symbol_map_;
1570
1571 // Locations of the main symbol table and symbol string table.
1572 Location main_symtab_loc_;
1573 Location main_strtab_loc_;
1574
1575 // Readers for the incremental info sections.
1576 bool has_incremental_info_;
1577 Incremental_inputs_reader<size, big_endian> inputs_reader_;
1578 Incremental_symtab_reader<big_endian> symtab_reader_;
1579 Incremental_relocs_reader<size, big_endian> relocs_reader_;
1580 Incremental_got_plt_reader<big_endian> got_plt_reader_;
1581 std::vector<Sized_input_reader> input_entry_readers_;
1582 };
1583
1584 // An incremental Relobj. This class represents a relocatable object
1585 // that has not changed since the last incremental link, and whose contents
1586 // can be used directly from the base file.
1587
1588 template<int size, bool big_endian>
1589 class Sized_relobj_incr : public Sized_relobj<size, big_endian>
1590 {
1591 public:
1592 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1593 typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
1594
1595 Sized_relobj_incr(const std::string& name,
1596 Sized_incremental_binary<size, big_endian>* ibase,
1597 unsigned int input_file_index);
1598
1599 private:
1600 // For convenience.
1601 typedef Sized_relobj_incr<size, big_endian> This;
1602 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1603
1604 typedef typename Sized_relobj<size, big_endian>::Output_sections
1605 Output_sections;
1606 typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
1607 typedef typename Inputs_reader::Incremental_input_entry_reader
1608 Input_entry_reader;
1609
1610 // A local symbol.
1611 struct Local_symbol
1612 {
1613 Local_symbol(const char* name_, Address value_, unsigned int size_,
1614 unsigned int shndx_, unsigned int type_,
1615 bool needs_dynsym_entry_)
1616 : st_value(value_), name(name_), st_size(size_), st_shndx(shndx_),
1617 st_type(type_), output_dynsym_index(0),
1618 needs_dynsym_entry(needs_dynsym_entry_)
1619 { }
1620 // The symbol value.
1621 Address st_value;
1622 // The symbol name. This points to the stringpool entry.
1623 const char* name;
1624 // The symbol size.
1625 unsigned int st_size;
1626 // The output section index.
1627 unsigned int st_shndx : 28;
1628 // The symbol type.
1629 unsigned int st_type : 4;
1630 // The index of the symbol in the output dynamic symbol table.
1631 unsigned int output_dynsym_index : 31;
1632 // TRUE if the symbol needs to appear in the dynamic symbol table.
1633 unsigned int needs_dynsym_entry : 1;
1634 };
1635
1636 // Return TRUE if this is an incremental (unchanged) input file.
1637 bool
1638 do_is_incremental() const
1639 { return true; }
1640
1641 // Return the last modified time of the file.
1642 Timespec
1643 do_get_mtime()
1644 { return this->input_reader_.get_mtime(); }
1645
1646 // Read the symbols.
1647 void
1648 do_read_symbols(Read_symbols_data*);
1649
1650 // Lay out the input sections.
1651 void
1652 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1653
1654 // Layout sections whose layout was deferred while waiting for
1655 // input files from a plugin.
1656 void
1657 do_layout_deferred_sections(Layout*);
1658
1659 // Add the symbols to the symbol table.
1660 void
1661 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
1662
1663 Archive::Should_include
1664 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
1665 std::string* why);
1666
1667 // Iterate over global symbols, calling a visitor class V for each.
1668 void
1669 do_for_all_global_symbols(Read_symbols_data* sd,
1670 Library_base::Symbol_visitor_base* v);
1671
1672 // Get the size of a section.
1673 uint64_t
1674 do_section_size(unsigned int shndx);
1675
1676 // Get the name of a section.
1677 std::string
1678 do_section_name(unsigned int shndx);
1679
1680 // Return a view of the contents of a section.
1681 Object::Location
1682 do_section_contents(unsigned int shndx);
1683
1684 // Return section flags.
1685 uint64_t
1686 do_section_flags(unsigned int shndx);
1687
1688 // Return section entsize.
1689 uint64_t
1690 do_section_entsize(unsigned int shndx);
1691
1692 // Return section address.
1693 uint64_t
1694 do_section_address(unsigned int shndx);
1695
1696 // Return section type.
1697 unsigned int
1698 do_section_type(unsigned int shndx);
1699
1700 // Return the section link field.
1701 unsigned int
1702 do_section_link(unsigned int shndx);
1703
1704 // Return the section link field.
1705 unsigned int
1706 do_section_info(unsigned int shndx);
1707
1708 // Return the section alignment.
1709 uint64_t
1710 do_section_addralign(unsigned int shndx);
1711
1712 // Return the Xindex structure to use.
1713 Xindex*
1714 do_initialize_xindex();
1715
1716 // Get symbol counts.
1717 void
1718 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1719
1720 // Get global symbols.
1721 const Symbols*
1722 do_get_global_symbols() const
1723 { return &this->symbols_; }
1724
1725 // Return the number of local symbols.
1726 unsigned int
1727 do_local_symbol_count() const
1728 { return this->local_symbol_count_; }
1729
1730 // Return the number of local symbols in the output symbol table.
1731 unsigned int
1732 do_output_local_symbol_count() const
1733 { return this->local_symbol_count_; }
1734
1735 // Return the file offset for local symbols in the output symbol table.
1736 off_t
1737 do_local_symbol_offset() const
1738 { return this->local_symbol_offset_; }
1739
1740 // Read the relocs.
1741 void
1742 do_read_relocs(Read_relocs_data*);
1743
1744 // Process the relocs to find list of referenced sections. Used only
1745 // during garbage collection.
1746 void
1747 do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1748
1749 // Scan the relocs and adjust the symbol table.
1750 void
1751 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
1752
1753 // Count the local symbols.
1754 void
1755 do_count_local_symbols(Stringpool_template<char>*,
1756 Stringpool_template<char>*);
1757
1758 // Finalize the local symbols.
1759 unsigned int
1760 do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
1761
1762 // Set the offset where local dynamic symbol information will be stored.
1763 unsigned int
1764 do_set_local_dynsym_indexes(unsigned int);
1765
1766 // Set the offset where local dynamic symbol information will be stored.
1767 unsigned int
1768 do_set_local_dynsym_offset(off_t);
1769
1770 // Relocate the input sections and write out the local symbols.
1771 void
1772 do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
1773
1774 // Set the offset of a section.
1775 void
1776 do_set_section_offset(unsigned int shndx, uint64_t off);
1777
1778 // The Incremental_binary base file.
1779 Sized_incremental_binary<size, big_endian>* ibase_;
1780 // The index of the object in the input file list.
1781 unsigned int input_file_index_;
1782 // The reader for the input file.
1783 Input_entry_reader input_reader_;
1784 // The number of local symbols.
1785 unsigned int local_symbol_count_;
1786 // The number of local symbols which go into the output file's dynamic
1787 // symbol table.
1788 unsigned int output_local_dynsym_count_;
1789 // This starting symbol index in the output symbol table.
1790 unsigned int local_symbol_index_;
1791 // The file offset for local symbols in the output symbol table.
1792 unsigned int local_symbol_offset_;
1793 // The file offset for local symbols in the output symbol table.
1794 unsigned int local_dynsym_offset_;
1795 // The entries in the symbol table for the external symbols.
1796 Symbols symbols_;
1797 // The offset of the first incremental relocation for this object.
1798 unsigned int incr_reloc_offset_;
1799 // The number of incremental relocations for this object.
1800 unsigned int incr_reloc_count_;
1801 // The index of the first incremental relocation for this object in the
1802 // updated output file.
1803 unsigned int incr_reloc_output_index_;
1804 // A copy of the incremental relocations from this object.
1805 unsigned char* incr_relocs_;
1806 // The local symbols.
1807 std::vector<Local_symbol> local_symbols_;
1808 };
1809
1810 // An incremental Dynobj. This class represents a shared object that has
1811 // not changed since the last incremental link, and whose contents can be
1812 // used directly from the base file.
1813
1814 template<int size, bool big_endian>
1815 class Sized_incr_dynobj : public Dynobj
1816 {
1817 public:
1818 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1819
1820 static const Address invalid_address = static_cast<Address>(0) - 1;
1821
1822 Sized_incr_dynobj(const std::string& name,
1823 Sized_incremental_binary<size, big_endian>* ibase,
1824 unsigned int input_file_index);
1825
1826 private:
1827 typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
1828 typedef typename Inputs_reader::Incremental_input_entry_reader
1829 Input_entry_reader;
1830
1831 // Return TRUE if this is an incremental (unchanged) input file.
1832 bool
1833 do_is_incremental() const
1834 { return true; }
1835
1836 // Return the last modified time of the file.
1837 Timespec
1838 do_get_mtime()
1839 { return this->input_reader_.get_mtime(); }
1840
1841 // Read the symbols.
1842 void
1843 do_read_symbols(Read_symbols_data*);
1844
1845 // Lay out the input sections.
1846 void
1847 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
1848
1849 // Add the symbols to the symbol table.
1850 void
1851 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
1852
1853 Archive::Should_include
1854 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
1855 std::string* why);
1856
1857 // Iterate over global symbols, calling a visitor class V for each.
1858 void
1859 do_for_all_global_symbols(Read_symbols_data* sd,
1860 Library_base::Symbol_visitor_base* v);
1861
1862 // Iterate over local symbols, calling a visitor class V for each GOT offset
1863 // associated with a local symbol.
1864 void
1865 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
1866
1867 // Get the size of a section.
1868 uint64_t
1869 do_section_size(unsigned int shndx);
1870
1871 // Get the name of a section.
1872 std::string
1873 do_section_name(unsigned int shndx);
1874
1875 // Return a view of the contents of a section.
1876 Object::Location
1877 do_section_contents(unsigned int shndx);
1878
1879 // Return section flags.
1880 uint64_t
1881 do_section_flags(unsigned int shndx);
1882
1883 // Return section entsize.
1884 uint64_t
1885 do_section_entsize(unsigned int shndx);
1886
1887 // Return section address.
1888 uint64_t
1889 do_section_address(unsigned int shndx);
1890
1891 // Return section type.
1892 unsigned int
1893 do_section_type(unsigned int shndx);
1894
1895 // Return the section link field.
1896 unsigned int
1897 do_section_link(unsigned int shndx);
1898
1899 // Return the section link field.
1900 unsigned int
1901 do_section_info(unsigned int shndx);
1902
1903 // Return the section alignment.
1904 uint64_t
1905 do_section_addralign(unsigned int shndx);
1906
1907 // Return the Xindex structure to use.
1908 Xindex*
1909 do_initialize_xindex();
1910
1911 // Get symbol counts.
1912 void
1913 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
1914
1915 // Get global symbols.
1916 const Symbols*
1917 do_get_global_symbols() const
1918 { return &this->symbols_; }
1919
1920 // The Incremental_binary base file.
1921 Sized_incremental_binary<size, big_endian>* ibase_;
1922 // The index of the object in the input file list.
1923 unsigned int input_file_index_;
1924 // The reader for the input file.
1925 Input_entry_reader input_reader_;
1926 // The entries in the symbol table for the external symbols.
1927 Symbols symbols_;
1928 };
1929
1930 // Allocate an incremental object of the appropriate size and endianness.
1931 extern Object*
1932 make_sized_incremental_object(
1933 Incremental_binary* base,
1934 unsigned int input_file_index,
1935 Incremental_input_type input_type,
1936 const Incremental_binary::Input_reader* input_reader);
1937
1938 // This class represents an Archive library (or --start-lib/--end-lib group)
1939 // that has not changed since the last incremental link. Its contents come
1940 // from the incremental inputs entry in the base file.
1941
1942 class Incremental_library : public Library_base
1943 {
1944 public:
1945 Incremental_library(const char* filename, unsigned int input_file_index,
1946 const Incremental_binary::Input_reader* input_reader)
1947 : Library_base(NULL), filename_(filename),
1948 input_file_index_(input_file_index), input_reader_(input_reader),
1949 unused_symbols_(), is_reported_(false)
1950 { }
1951
1952 // Return the input file index.
1953 unsigned int
1954 input_file_index() const
1955 { return this->input_file_index_; }
1956
1957 // Return the serial number of the input file.
1958 unsigned int
1959 arg_serial() const
1960 { return this->input_reader_->arg_serial(); }
1961
1962 // Copy the unused symbols from the incremental input info.
1963 // We need to do this because we may be overwriting the incremental
1964 // input info in the base file before we write the new incremental
1965 // info.
1966 void
1967 copy_unused_symbols();
1968
1969 // Return FALSE on the first call to indicate that the library needs
1970 // to be recorded; return TRUE subsequently.
1971 bool
1972 is_reported()
1973 {
1974 bool was_reported = this->is_reported_;
1975 is_reported_ = true;
1976 return was_reported;
1977 }
1978
1979 private:
1980 typedef std::vector<std::string> Symbol_list;
1981
1982 // The file name.
1983 const std::string&
1984 do_filename() const
1985 { return this->filename_; }
1986
1987 // Return the modification time of the archive file.
1988 Timespec
1989 do_get_mtime()
1990 { return this->input_reader_->get_mtime(); }
1991
1992 // Iterator for unused global symbols in the library.
1993 void
1994 do_for_all_unused_symbols(Symbol_visitor_base* v) const;
1995
1996 // The name of the library.
1997 std::string filename_;
1998 // The input file index of this library.
1999 unsigned int input_file_index_;
2000 // A reader for the incremental input information.
2001 const Incremental_binary::Input_reader* input_reader_;
2002 // List of unused symbols defined in this library.
2003 Symbol_list unused_symbols_;
2004 // TRUE when this library has been reported to the new incremental info.
2005 bool is_reported_;
2006 };
2007
2008 } // End namespace gold.
2009
2010 #endif // !defined(GOLD_INCREMENTAL_H)
This page took 0.092265 seconds and 5 git commands to generate.