1 // inremental.cc -- incremental linking support for gold
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Mikolaj Zalewski <mikolajz@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
26 #include "libiberty.h"
31 #include "incremental.h"
34 #include "target-select.h"
39 // Version information. Will change frequently during the development, later
40 // we could think about backward (and forward?) compatibility.
41 const unsigned int INCREMENTAL_LINK_VERSION
= 1;
43 // This class manages the .gnu_incremental_inputs section, which holds
44 // the header information, a directory of input files, and separate
45 // entries for each input file.
47 template<int size
, bool big_endian
>
48 class Output_section_incremental_inputs
: public Output_section_data
51 Output_section_incremental_inputs(const Incremental_inputs
* inputs
,
52 const Symbol_table
* symtab
)
53 : Output_section_data(size
/ 8), inputs_(inputs
), symtab_(symtab
)
57 // Set the final data size.
59 set_final_data_size();
61 // Write the data to the file.
63 do_write(Output_file
*);
65 // Write to a map file.
67 do_print_to_mapfile(Mapfile
* mapfile
) const
68 { mapfile
->print_output_data(this, _("** incremental_inputs")); }
71 // Write the section header.
73 write_header(unsigned char* pov
, unsigned int input_file_count
,
74 section_offset_type command_line_offset
);
76 // Write the input file entries.
78 write_input_files(unsigned char* oview
, unsigned char* pov
,
81 // Write the supplemental information blocks.
83 write_info_blocks(unsigned char* oview
, unsigned char* pov
,
84 Stringpool
* strtab
, unsigned int* global_syms
,
85 unsigned int global_sym_count
);
87 // Write the contents of the .gnu_incremental_symtab section.
89 write_symtab(unsigned char* pov
, unsigned int* global_syms
,
90 unsigned int global_sym_count
);
92 // Write the contents of the .gnu_incremental_got_plt section.
94 write_got_plt(unsigned char* pov
, off_t view_size
);
96 // Typedefs for writing the data to the output sections.
97 typedef elfcpp::Swap
<size
, big_endian
> Swap
;
98 typedef elfcpp::Swap
<16, big_endian
> Swap16
;
99 typedef elfcpp::Swap
<32, big_endian
> Swap32
;
100 typedef elfcpp::Swap
<64, big_endian
> Swap64
;
102 // Sizes of various structures.
103 static const int sizeof_addr
= size
/ 8;
104 static const int header_size
= 16;
105 static const int input_entry_size
= 24;
107 // The Incremental_inputs object.
108 const Incremental_inputs
* inputs_
;
111 const Symbol_table
* symtab_
;
114 // Inform the user why we don't do an incremental link. Not called in
115 // the obvious case of missing output file. TODO: Is this helpful?
118 vexplain_no_incremental(const char* format
, va_list args
)
121 if (vasprintf(&buf
, format
, args
) < 0)
123 gold_info(_("the link might take longer: "
124 "cannot perform incremental link: %s"), buf
);
129 explain_no_incremental(const char* format
, ...)
132 va_start(args
, format
);
133 vexplain_no_incremental(format
, args
);
140 Incremental_binary::error(const char* format
, ...) const
143 va_start(args
, format
);
144 // Current code only checks if the file can be used for incremental linking,
145 // so errors shouldn't fail the build, but only result in a fallback to a
147 // TODO: when we implement incremental editing of the file, we may need a
148 // flag that will cause errors to be treated seriously.
149 vexplain_no_incremental(format
, args
);
153 // Find the .gnu_incremental_inputs section and related sections.
155 template<int size
, bool big_endian
>
157 Sized_incremental_binary
<size
, big_endian
>::find_incremental_inputs_sections(
158 unsigned int* p_inputs_shndx
,
159 unsigned int* p_symtab_shndx
,
160 unsigned int* p_relocs_shndx
,
161 unsigned int* p_got_plt_shndx
,
162 unsigned int* p_strtab_shndx
)
164 unsigned int inputs_shndx
=
165 this->elf_file_
.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS
);
166 if (inputs_shndx
== elfcpp::SHN_UNDEF
) // Not found.
169 unsigned int symtab_shndx
=
170 this->elf_file_
.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB
);
171 if (symtab_shndx
== elfcpp::SHN_UNDEF
) // Not found.
173 if (this->elf_file_
.section_link(symtab_shndx
) != inputs_shndx
)
176 unsigned int relocs_shndx
=
177 this->elf_file_
.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS
);
178 if (relocs_shndx
== elfcpp::SHN_UNDEF
) // Not found.
180 if (this->elf_file_
.section_link(relocs_shndx
) != inputs_shndx
)
183 unsigned int got_plt_shndx
=
184 this->elf_file_
.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT
);
185 if (got_plt_shndx
== elfcpp::SHN_UNDEF
) // Not found.
187 if (this->elf_file_
.section_link(got_plt_shndx
) != inputs_shndx
)
190 unsigned int strtab_shndx
= this->elf_file_
.section_link(inputs_shndx
);
191 if (strtab_shndx
== elfcpp::SHN_UNDEF
192 || strtab_shndx
> this->elf_file_
.shnum()
193 || this->elf_file_
.section_type(strtab_shndx
) != elfcpp::SHT_STRTAB
)
196 if (p_inputs_shndx
!= NULL
)
197 *p_inputs_shndx
= inputs_shndx
;
198 if (p_symtab_shndx
!= NULL
)
199 *p_symtab_shndx
= symtab_shndx
;
200 if (p_relocs_shndx
!= NULL
)
201 *p_relocs_shndx
= relocs_shndx
;
202 if (p_got_plt_shndx
!= NULL
)
203 *p_got_plt_shndx
= got_plt_shndx
;
204 if (p_strtab_shndx
!= NULL
)
205 *p_strtab_shndx
= strtab_shndx
;
209 // Set up the readers into the incremental info sections.
211 template<int size
, bool big_endian
>
213 Sized_incremental_binary
<size
, big_endian
>::setup_readers()
215 unsigned int inputs_shndx
;
216 unsigned int symtab_shndx
;
217 unsigned int relocs_shndx
;
218 unsigned int got_plt_shndx
;
219 unsigned int strtab_shndx
;
221 if (!this->find_incremental_inputs_sections(&inputs_shndx
, &symtab_shndx
,
222 &relocs_shndx
, &got_plt_shndx
,
226 Location
inputs_location(this->elf_file_
.section_contents(inputs_shndx
));
227 Location
symtab_location(this->elf_file_
.section_contents(symtab_shndx
));
228 Location
relocs_location(this->elf_file_
.section_contents(relocs_shndx
));
229 Location
got_plt_location(this->elf_file_
.section_contents(got_plt_shndx
));
230 Location
strtab_location(this->elf_file_
.section_contents(strtab_shndx
));
232 View inputs_view
= this->view(inputs_location
);
233 View symtab_view
= this->view(symtab_location
);
234 View relocs_view
= this->view(relocs_location
);
235 View got_plt_view
= this->view(got_plt_location
);
236 View strtab_view
= this->view(strtab_location
);
238 elfcpp::Elf_strtab
strtab(strtab_view
.data(), strtab_location
.data_size
);
240 this->inputs_reader_
=
241 Incremental_inputs_reader
<size
, big_endian
>(inputs_view
.data(), strtab
);
242 this->symtab_reader_
=
243 Incremental_symtab_reader
<big_endian
>(symtab_view
.data(),
244 symtab_location
.data_size
);
245 this->relocs_reader_
=
246 Incremental_relocs_reader
<size
, big_endian
>(relocs_view
.data(),
247 relocs_location
.data_size
);
248 this->got_plt_reader_
=
249 Incremental_got_plt_reader
<big_endian
>(got_plt_view
.data());
250 this->has_incremental_info_
= true;
253 // Determine whether an incremental link based on the existing output file
256 template<int size
, bool big_endian
>
258 Sized_incremental_binary
<size
, big_endian
>::do_check_inputs(
259 Incremental_inputs
* incremental_inputs
)
261 if (!this->has_incremental_info_
)
263 explain_no_incremental(_("no incremental data from previous build"));
267 if (this->inputs_reader_
.version() != INCREMENTAL_LINK_VERSION
)
269 explain_no_incremental(_("different version of incremental build data"));
273 if (incremental_inputs
->command_line() != this->inputs_reader_
.command_line())
275 explain_no_incremental(_("command line changed"));
282 // Return TRUE if the file specified by INPUT_ARGUMENT is unchanged
283 // with respect to the base file.
285 template<int size
, bool big_endian
>
287 Sized_incremental_binary
<size
, big_endian
>::do_file_is_unchanged(
288 const Input_argument
* input_argument
) const
290 Incremental_disposition disp
=
291 input_argument
->file().options().incremental_disposition();
293 if (disp
!= INCREMENTAL_CHECK
)
294 return disp
== INCREMENTAL_UNCHANGED
;
296 // FIXME: Handle INCREMENTAL_CHECK.
301 template<int size
, bool big_endian
>
302 Incremental_binary::Input_reader
*
303 Sized_incremental_binary
<size
, big_endian
>::do_get_input_reader(
306 unsigned int file_index
= this->current_input_file_
++;
307 gold_assert(file_index
< this->inputs_reader_
.input_file_count());
308 return new Sized_input_reader(this->inputs_reader_
.input_file(file_index
));
314 // Create a Sized_incremental_binary object of the specified size and
315 // endianness. Fails if the target architecture is not supported.
317 template<int size
, bool big_endian
>
319 make_sized_incremental_binary(Output_file
* file
,
320 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
322 Target
* target
= select_target(ehdr
.get_e_machine(), size
, big_endian
,
323 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
324 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
327 explain_no_incremental(_("unsupported ELF machine number %d"),
328 ehdr
.get_e_machine());
332 if (!parameters
->target_valid())
333 set_parameters_target(target
);
334 else if (target
!= ¶meters
->target())
335 gold_error(_("%s: incompatible target"), file
->filename());
337 return new Sized_incremental_binary
<size
, big_endian
>(file
, ehdr
, target
);
340 } // End of anonymous namespace.
342 // Create an Incremental_binary object for FILE. Returns NULL is this is not
343 // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
347 open_incremental_binary(Output_file
* file
)
349 off_t filesize
= file
->filesize();
350 int want
= elfcpp::Elf_recognizer::max_header_size
;
354 const unsigned char* p
= file
->get_input_view(0, want
);
355 if (!elfcpp::Elf_recognizer::is_elf_file(p
, want
))
357 explain_no_incremental(_("output is not an ELF file."));
362 bool big_endian
= false;
364 if (!elfcpp::Elf_recognizer::is_valid_header(p
, want
, &size
, &big_endian
,
367 explain_no_incremental(error
.c_str());
371 Incremental_binary
* result
= NULL
;
376 #ifdef HAVE_TARGET_32_BIG
377 result
= make_sized_incremental_binary
<32, true>(
378 file
, elfcpp::Ehdr
<32, true>(p
));
380 explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
385 #ifdef HAVE_TARGET_32_LITTLE
386 result
= make_sized_incremental_binary
<32, false>(
387 file
, elfcpp::Ehdr
<32, false>(p
));
389 explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
397 #ifdef HAVE_TARGET_64_BIG
398 result
= make_sized_incremental_binary
<64, true>(
399 file
, elfcpp::Ehdr
<64, true>(p
));
401 explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
406 #ifdef HAVE_TARGET_64_LITTLE
407 result
= make_sized_incremental_binary
<64, false>(
408 file
, elfcpp::Ehdr
<64, false>(p
));
410 explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
420 // Analyzes the output file to check if incremental linking is possible and
421 // (to be done) what files need to be relinked.
424 Incremental_checker::can_incrementally_link_output_file()
426 Output_file
output(this->output_name_
);
427 if (!output
.open_for_modification())
429 Incremental_binary
* binary
= open_incremental_binary(&output
);
432 return binary
->check_inputs(this->incremental_inputs_
);
435 // Class Incremental_inputs.
437 // Add the command line to the string table, setting
438 // command_line_key_. In incremental builds, the command line is
439 // stored in .gnu_incremental_inputs so that the next linker run can
440 // check if the command line options didn't change.
443 Incremental_inputs::report_command_line(int argc
, const char* const* argv
)
445 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
446 // different path to the linker.
447 std::string
args("gold");
448 // Copied from collect_argv in main.cc.
449 for (int i
= 1; i
< argc
; ++i
)
451 // Adding/removing these options should not result in a full relink.
452 if (strcmp(argv
[i
], "--incremental") == 0
453 || strcmp(argv
[i
], "--incremental-full") == 0
454 || strcmp(argv
[i
], "--incremental-update") == 0
455 || strcmp(argv
[i
], "--incremental-changed") == 0
456 || strcmp(argv
[i
], "--incremental-unchanged") == 0
457 || strcmp(argv
[i
], "--incremental-unknown") == 0)
461 // Now append argv[i], but with all single-quotes escaped
462 const char* argpos
= argv
[i
];
465 const int len
= strcspn(argpos
, "'");
466 args
.append(argpos
, len
);
467 if (argpos
[len
] == '\0')
469 args
.append("'\"'\"'");
475 this->command_line_
= args
;
476 this->strtab_
->add(this->command_line_
.c_str(), false,
477 &this->command_line_key_
);
480 // Record the input archive file ARCHIVE. This is called by the
481 // Add_archive_symbols task before determining which archive members
482 // to include. We create the Incremental_archive_entry here and
483 // attach it to the Archive, but we do not add it to the list of
484 // input objects until report_archive_end is called.
487 Incremental_inputs::report_archive_begin(Library_base
* arch
,
488 Script_info
* script_info
)
490 Stringpool::Key filename_key
;
491 Timespec mtime
= arch
->get_mtime();
493 this->strtab_
->add(arch
->filename().c_str(), false, &filename_key
);
494 Incremental_archive_entry
* entry
=
495 new Incremental_archive_entry(filename_key
, mtime
);
496 arch
->set_incremental_info(entry
);
497 this->inputs_
.push_back(entry
);
499 if (script_info
!= NULL
)
501 Incremental_script_entry
* script_entry
= script_info
->incremental_info();
502 gold_assert(script_entry
!= NULL
);
503 script_entry
->add_object(entry
);
507 // Visitor class for processing the unused global symbols in a library.
508 // An instance of this class is passed to the library's
509 // for_all_unused_symbols() iterator, which will call the visit()
510 // function for each global symbol defined in each unused library
511 // member. We add those symbol names to the incremental info for the
514 class Unused_symbol_visitor
: public Library_base::Symbol_visitor_base
517 Unused_symbol_visitor(Incremental_archive_entry
* entry
, Stringpool
* strtab
)
518 : entry_(entry
), strtab_(strtab
)
522 visit(const char* sym
)
524 Stringpool::Key symbol_key
;
525 this->strtab_
->add(sym
, true, &symbol_key
);
526 this->entry_
->add_unused_global_symbol(symbol_key
);
530 Incremental_archive_entry
* entry_
;
534 // Finish recording the input archive file ARCHIVE. This is called by the
535 // Add_archive_symbols task after determining which archive members
539 Incremental_inputs::report_archive_end(Library_base
* arch
)
541 Incremental_archive_entry
* entry
= arch
->incremental_info();
543 gold_assert(entry
!= NULL
);
545 // Collect unused global symbols.
546 Unused_symbol_visitor
v(entry
, this->strtab_
);
547 arch
->for_all_unused_symbols(&v
);
550 // Record the input object file OBJ. If ARCH is not NULL, attach
551 // the object file to the archive. This is called by the
552 // Add_symbols task after finding out the type of the file.
555 Incremental_inputs::report_object(Object
* obj
, Library_base
* arch
,
556 Script_info
* script_info
)
558 Stringpool::Key filename_key
;
559 Timespec mtime
= obj
->input_file()->file().get_mtime();
561 this->strtab_
->add(obj
->name().c_str(), false, &filename_key
);
562 Incremental_object_entry
* obj_entry
=
563 new Incremental_object_entry(filename_key
, obj
, mtime
);
564 this->inputs_
.push_back(obj_entry
);
568 Incremental_archive_entry
* arch_entry
= arch
->incremental_info();
569 gold_assert(arch_entry
!= NULL
);
570 arch_entry
->add_object(obj_entry
);
573 if (script_info
!= NULL
)
575 Incremental_script_entry
* script_entry
= script_info
->incremental_info();
576 gold_assert(script_entry
!= NULL
);
577 script_entry
->add_object(obj_entry
);
580 this->current_object_
= obj
;
581 this->current_object_entry_
= obj_entry
;
584 // Record the input object file OBJ. If ARCH is not NULL, attach
585 // the object file to the archive. This is called by the
586 // Add_symbols task after finding out the type of the file.
589 Incremental_inputs::report_input_section(Object
* obj
, unsigned int shndx
,
590 const char* name
, off_t sh_size
)
592 Stringpool::Key key
= 0;
595 this->strtab_
->add(name
, true, &key
);
597 gold_assert(obj
== this->current_object_
);
598 this->current_object_entry_
->add_input_section(shndx
, key
, sh_size
);
601 // Record that the input argument INPUT is a script SCRIPT. This is
602 // called by read_script after parsing the script and reading the list
603 // of inputs added by this script.
606 Incremental_inputs::report_script(const std::string
& filename
,
607 Script_info
* script
, Timespec mtime
)
609 Stringpool::Key filename_key
;
611 this->strtab_
->add(filename
.c_str(), false, &filename_key
);
612 Incremental_script_entry
* entry
=
613 new Incremental_script_entry(filename_key
, script
, mtime
);
614 this->inputs_
.push_back(entry
);
615 script
->set_incremental_info(entry
);
618 // Finalize the incremental link information. Called from
622 Incremental_inputs::finalize()
624 // Finalize the string table.
625 this->strtab_
->set_string_offsets();
628 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
631 Incremental_inputs::create_data_sections(Symbol_table
* symtab
)
633 switch (parameters
->size_and_endianness())
635 #ifdef HAVE_TARGET_32_LITTLE
636 case Parameters::TARGET_32_LITTLE
:
637 this->inputs_section_
=
638 new Output_section_incremental_inputs
<32, false>(this, symtab
);
641 #ifdef HAVE_TARGET_32_BIG
642 case Parameters::TARGET_32_BIG
:
643 this->inputs_section_
=
644 new Output_section_incremental_inputs
<32, true>(this, symtab
);
647 #ifdef HAVE_TARGET_64_LITTLE
648 case Parameters::TARGET_64_LITTLE
:
649 this->inputs_section_
=
650 new Output_section_incremental_inputs
<64, false>(this, symtab
);
653 #ifdef HAVE_TARGET_64_BIG
654 case Parameters::TARGET_64_BIG
:
655 this->inputs_section_
=
656 new Output_section_incremental_inputs
<64, true>(this, symtab
);
662 this->symtab_section_
= new Output_data_space(4, "** incremental_symtab");
663 this->relocs_section_
= new Output_data_space(4, "** incremental_relocs");
664 this->got_plt_section_
= new Output_data_space(4, "** incremental_got_plt");
667 // Return the sh_entsize value for the .gnu_incremental_relocs section.
669 Incremental_inputs::relocs_entsize() const
671 return 8 + 2 * parameters
->target().get_size() / 8;
674 // Class Output_section_incremental_inputs.
676 // Finalize the offsets for each input section and supplemental info block,
677 // and set the final data size of the incremental output sections.
679 template<int size
, bool big_endian
>
681 Output_section_incremental_inputs
<size
, big_endian
>::set_final_data_size()
683 const Incremental_inputs
* inputs
= this->inputs_
;
684 const unsigned int sizeof_addr
= size
/ 8;
685 const unsigned int rel_size
= 8 + 2 * sizeof_addr
;
687 // Offset of each input entry.
688 unsigned int input_offset
= this->header_size
;
690 // Offset of each supplemental info block.
691 unsigned int info_offset
= this->header_size
;
692 info_offset
+= this->input_entry_size
* inputs
->input_file_count();
694 // Count each input file and its supplemental information block.
695 for (Incremental_inputs::Input_list::const_iterator p
=
696 inputs
->input_files().begin();
697 p
!= inputs
->input_files().end();
700 // Set the offset of the input file entry.
701 (*p
)->set_offset(input_offset
);
702 input_offset
+= this->input_entry_size
;
704 // Set the offset of the supplemental info block.
705 switch ((*p
)->type())
707 case INCREMENTAL_INPUT_SCRIPT
:
709 Incremental_script_entry
*entry
= (*p
)->script_entry();
710 gold_assert(entry
!= NULL
);
711 (*p
)->set_info_offset(info_offset
);
715 info_offset
+= (entry
->get_object_count() * 4);
718 case INCREMENTAL_INPUT_OBJECT
:
719 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
721 Incremental_object_entry
* entry
= (*p
)->object_entry();
722 gold_assert(entry
!= NULL
);
723 (*p
)->set_info_offset(info_offset
);
724 // Input section count + global symbol count.
726 // Each input section.
727 info_offset
+= (entry
->get_input_section_count()
728 * (8 + 2 * sizeof_addr
));
729 // Each global symbol.
730 const Object::Symbols
* syms
= entry
->object()->get_global_symbols();
731 info_offset
+= syms
->size() * 16;
734 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
736 Incremental_object_entry
* entry
= (*p
)->object_entry();
737 gold_assert(entry
!= NULL
);
738 (*p
)->set_info_offset(info_offset
);
739 // Global symbol count.
741 // Each global symbol.
742 const Object::Symbols
* syms
= entry
->object()->get_global_symbols();
743 unsigned int nsyms
= syms
!= NULL
? syms
->size() : 0;
744 info_offset
+= nsyms
* 4;
747 case INCREMENTAL_INPUT_ARCHIVE
:
749 Incremental_archive_entry
* entry
= (*p
)->archive_entry();
750 gold_assert(entry
!= NULL
);
751 (*p
)->set_info_offset(info_offset
);
752 // Member count + unused global symbol count.
755 info_offset
+= (entry
->get_member_count() * 4);
756 // Each global symbol.
757 info_offset
+= (entry
->get_unused_global_symbol_count() * 4);
765 this->set_data_size(info_offset
);
767 // Set the size of the .gnu_incremental_symtab section.
768 inputs
->symtab_section()->set_current_data_size(this->symtab_
->output_count()
769 * sizeof(unsigned int));
771 // Set the size of the .gnu_incremental_relocs section.
772 inputs
->relocs_section()->set_current_data_size(inputs
->get_reloc_count()
775 // Set the size of the .gnu_incremental_got_plt section.
776 Sized_target
<size
, big_endian
>* target
=
777 parameters
->sized_target
<size
, big_endian
>();
778 unsigned int got_count
= target
->got_entry_count();
779 unsigned int plt_count
= target
->plt_entry_count();
780 unsigned int got_plt_size
= 8; // GOT entry count, PLT entry count.
781 got_plt_size
= (got_plt_size
+ got_count
+ 3) & ~3; // GOT type array.
782 got_plt_size
+= got_count
* 4 + plt_count
* 4; // GOT array, PLT array.
783 inputs
->got_plt_section()->set_current_data_size(got_plt_size
);
786 // Write the contents of the .gnu_incremental_inputs and
787 // .gnu_incremental_symtab sections.
789 template<int size
, bool big_endian
>
791 Output_section_incremental_inputs
<size
, big_endian
>::do_write(Output_file
* of
)
793 const Incremental_inputs
* inputs
= this->inputs_
;
794 Stringpool
* strtab
= inputs
->get_stringpool();
796 // Get a view into the .gnu_incremental_inputs section.
797 const off_t off
= this->offset();
798 const off_t oview_size
= this->data_size();
799 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
800 unsigned char* pov
= oview
;
802 // Get a view into the .gnu_incremental_symtab section.
803 const off_t symtab_off
= inputs
->symtab_section()->offset();
804 const off_t symtab_size
= inputs
->symtab_section()->data_size();
805 unsigned char* const symtab_view
= of
->get_output_view(symtab_off
,
808 // Allocate an array of linked list heads for the .gnu_incremental_symtab
809 // section. Each element corresponds to a global symbol in the output
810 // symbol table, and points to the head of the linked list that threads
811 // through the object file input entries. The value of each element
812 // is the section-relative offset to a global symbol entry in a
813 // supplemental information block.
814 unsigned int global_sym_count
= this->symtab_
->output_count();
815 unsigned int* global_syms
= new unsigned int[global_sym_count
];
816 memset(global_syms
, 0, global_sym_count
* sizeof(unsigned int));
818 // Write the section header.
819 Stringpool::Key command_line_key
= inputs
->command_line_key();
820 pov
= this->write_header(pov
, inputs
->input_file_count(),
821 strtab
->get_offset_from_key(command_line_key
));
823 // Write the list of input files.
824 pov
= this->write_input_files(oview
, pov
, strtab
);
826 // Write the supplemental information blocks for each input file.
827 pov
= this->write_info_blocks(oview
, pov
, strtab
, global_syms
,
830 gold_assert(pov
- oview
== oview_size
);
832 // Write the .gnu_incremental_symtab section.
833 gold_assert(global_sym_count
* 4 == symtab_size
);
834 this->write_symtab(symtab_view
, global_syms
, global_sym_count
);
836 delete[] global_syms
;
838 // Write the .gnu_incremental_got_plt section.
839 const off_t got_plt_off
= inputs
->got_plt_section()->offset();
840 const off_t got_plt_size
= inputs
->got_plt_section()->data_size();
841 unsigned char* const got_plt_view
= of
->get_output_view(got_plt_off
,
843 this->write_got_plt(got_plt_view
, got_plt_size
);
845 of
->write_output_view(off
, oview_size
, oview
);
846 of
->write_output_view(symtab_off
, symtab_size
, symtab_view
);
847 of
->write_output_view(got_plt_off
, got_plt_size
, got_plt_view
);
850 // Write the section header: version, input file count, offset of command line
851 // in the string table, and 4 bytes of padding.
853 template<int size
, bool big_endian
>
855 Output_section_incremental_inputs
<size
, big_endian
>::write_header(
857 unsigned int input_file_count
,
858 section_offset_type command_line_offset
)
860 Swap32::writeval(pov
, INCREMENTAL_LINK_VERSION
);
861 Swap32::writeval(pov
+ 4, input_file_count
);
862 Swap32::writeval(pov
+ 8, command_line_offset
);
863 Swap32::writeval(pov
+ 12, 0);
864 return pov
+ this->header_size
;
867 // Write the input file entries.
869 template<int size
, bool big_endian
>
871 Output_section_incremental_inputs
<size
, big_endian
>::write_input_files(
872 unsigned char* oview
,
876 const Incremental_inputs
* inputs
= this->inputs_
;
878 for (Incremental_inputs::Input_list::const_iterator p
=
879 inputs
->input_files().begin();
880 p
!= inputs
->input_files().end();
883 gold_assert(static_cast<unsigned int>(pov
- oview
) == (*p
)->get_offset());
884 section_offset_type filename_offset
=
885 strtab
->get_offset_from_key((*p
)->get_filename_key());
886 const Timespec
& mtime
= (*p
)->get_mtime();
887 Swap32::writeval(pov
, filename_offset
);
888 Swap32::writeval(pov
+ 4, (*p
)->get_info_offset());
889 Swap64::writeval(pov
+ 8, mtime
.seconds
);
890 Swap32::writeval(pov
+ 16, mtime
.nanoseconds
);
891 Swap16::writeval(pov
+ 20, (*p
)->type());
892 Swap16::writeval(pov
+ 22, 0);
893 pov
+= this->input_entry_size
;
898 // Write the supplemental information blocks.
900 template<int size
, bool big_endian
>
902 Output_section_incremental_inputs
<size
, big_endian
>::write_info_blocks(
903 unsigned char* oview
,
906 unsigned int* global_syms
,
907 unsigned int global_sym_count
)
909 const Incremental_inputs
* inputs
= this->inputs_
;
910 unsigned int first_global_index
= this->symtab_
->first_global_index();
912 for (Incremental_inputs::Input_list::const_iterator p
=
913 inputs
->input_files().begin();
914 p
!= inputs
->input_files().end();
917 switch ((*p
)->type())
919 case INCREMENTAL_INPUT_SCRIPT
:
921 gold_assert(static_cast<unsigned int>(pov
- oview
)
922 == (*p
)->get_info_offset());
923 Incremental_script_entry
* entry
= (*p
)->script_entry();
924 gold_assert(entry
!= NULL
);
926 // Write the object count.
927 unsigned int nobjects
= entry
->get_object_count();
928 Swap32::writeval(pov
, nobjects
);
931 // For each object, write the offset to its input file entry.
932 for (unsigned int i
= 0; i
< nobjects
; ++i
)
934 Incremental_input_entry
* obj
= entry
->get_object(i
);
935 Swap32::writeval(pov
, obj
->get_offset());
941 case INCREMENTAL_INPUT_OBJECT
:
942 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
944 gold_assert(static_cast<unsigned int>(pov
- oview
)
945 == (*p
)->get_info_offset());
946 Incremental_object_entry
* entry
= (*p
)->object_entry();
947 gold_assert(entry
!= NULL
);
948 const Object
* obj
= entry
->object();
949 const Object::Symbols
* syms
= obj
->get_global_symbols();
950 // Write the input section count and global symbol count.
951 unsigned int nsections
= entry
->get_input_section_count();
952 unsigned int nsyms
= syms
->size();
953 Swap32::writeval(pov
, nsections
);
954 Swap32::writeval(pov
+ 4, nsyms
);
957 // For each input section, write the name, output section index,
958 // offset within output section, and input section size.
959 for (unsigned int i
= 0; i
< nsections
; i
++)
961 Stringpool::Key key
= entry
->get_input_section_name_key(i
);
962 off_t name_offset
= 0;
964 name_offset
= strtab
->get_offset_from_key(key
);
966 off_t out_offset
= 0;
968 Output_section
* os
= obj
->output_section(i
);
971 out_shndx
= os
->out_shndx();
972 out_offset
= obj
->output_section_offset(i
);
973 sh_size
= entry
->get_input_section_size(i
);
975 Swap32::writeval(pov
, name_offset
);
976 Swap32::writeval(pov
+ 4, out_shndx
);
977 Swap::writeval(pov
+ 8, out_offset
);
978 Swap::writeval(pov
+ 8 + sizeof_addr
, sh_size
);
979 pov
+= 8 + 2 * sizeof_addr
;
982 // For each global symbol, write its associated relocations,
983 // add it to the linked list of globals, then write the
984 // supplemental information: global symbol table index,
985 // linked list chain pointer, relocation count, and offset
986 // to the relocations.
987 for (unsigned int i
= 0; i
< nsyms
; i
++)
989 const Symbol
* sym
= (*syms
)[i
];
990 if (sym
->is_forwarder())
991 sym
= this->symtab_
->resolve_forwards(sym
);
992 unsigned int symtab_index
= sym
->symtab_index();
993 unsigned int chain
= 0;
994 unsigned int first_reloc
= 0;
995 unsigned int nrelocs
= obj
->get_incremental_reloc_count(i
);
998 gold_assert(symtab_index
!= -1U
999 && (symtab_index
- first_global_index
1000 < global_sym_count
));
1001 first_reloc
= obj
->get_incremental_reloc_base(i
);
1002 chain
= global_syms
[symtab_index
- first_global_index
];
1003 global_syms
[symtab_index
- first_global_index
] =
1006 Swap32::writeval(pov
, symtab_index
);
1007 Swap32::writeval(pov
+ 4, chain
);
1008 Swap32::writeval(pov
+ 8, nrelocs
);
1009 Swap32::writeval(pov
+ 12, first_reloc
* 3 * sizeof_addr
);
1015 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
1017 gold_assert(static_cast<unsigned int>(pov
- oview
)
1018 == (*p
)->get_info_offset());
1019 Incremental_object_entry
* entry
= (*p
)->object_entry();
1020 gold_assert(entry
!= NULL
);
1021 const Object
* obj
= entry
->object();
1022 const Object::Symbols
* syms
= obj
->get_global_symbols();
1024 // Write the global symbol count.
1025 unsigned int nsyms
= syms
!= NULL
? syms
->size() : 0;
1026 Swap32::writeval(pov
, nsyms
);
1029 // For each global symbol, write the global symbol table index.
1030 for (unsigned int i
= 0; i
< nsyms
; i
++)
1032 const Symbol
* sym
= (*syms
)[i
];
1033 Swap32::writeval(pov
, sym
->symtab_index());
1039 case INCREMENTAL_INPUT_ARCHIVE
:
1041 gold_assert(static_cast<unsigned int>(pov
- oview
)
1042 == (*p
)->get_info_offset());
1043 Incremental_archive_entry
* entry
= (*p
)->archive_entry();
1044 gold_assert(entry
!= NULL
);
1046 // Write the member count and unused global symbol count.
1047 unsigned int nmembers
= entry
->get_member_count();
1048 unsigned int nsyms
= entry
->get_unused_global_symbol_count();
1049 Swap32::writeval(pov
, nmembers
);
1050 Swap32::writeval(pov
+ 4, nsyms
);
1053 // For each member, write the offset to its input file entry.
1054 for (unsigned int i
= 0; i
< nmembers
; ++i
)
1056 Incremental_object_entry
* member
= entry
->get_member(i
);
1057 Swap32::writeval(pov
, member
->get_offset());
1061 // For each global symbol, write the name offset.
1062 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1064 Stringpool::Key key
= entry
->get_unused_global_symbol(i
);
1065 Swap32::writeval(pov
, strtab
->get_offset_from_key(key
));
1078 // Write the contents of the .gnu_incremental_symtab section.
1080 template<int size
, bool big_endian
>
1082 Output_section_incremental_inputs
<size
, big_endian
>::write_symtab(
1084 unsigned int* global_syms
,
1085 unsigned int global_sym_count
)
1087 for (unsigned int i
= 0; i
< global_sym_count
; ++i
)
1089 Swap32::writeval(pov
, global_syms
[i
]);
1094 // This struct holds the view information needed to write the
1095 // .gnu_incremental_got_plt section.
1097 struct Got_plt_view_info
1099 // Start of the GOT type array in the output view.
1100 unsigned char* got_type_p
;
1101 // Start of the GOT descriptor array in the output view.
1102 unsigned char* got_desc_p
;
1103 // Start of the PLT descriptor array in the output view.
1104 unsigned char* plt_desc_p
;
1105 // Number of GOT entries.
1106 unsigned int got_count
;
1107 // Number of PLT entries.
1108 unsigned int plt_count
;
1109 // Offset of the first non-reserved PLT entry (this is a target-dependent value).
1110 unsigned int first_plt_entry_offset
;
1111 // Size of a PLT entry (this is a target-dependent value).
1112 unsigned int plt_entry_size
;
1113 // Value to write in the GOT descriptor array. For global symbols,
1114 // this is the global symbol table index; for local symbols, it is
1115 // the offset of the input file entry in the .gnu_incremental_inputs
1117 unsigned int got_descriptor
;
1120 // Functor class for processing a GOT offset list for local symbols.
1121 // Writes the GOT type and symbol index into the GOT type and descriptor
1122 // arrays in the output section.
1124 template<int size
, bool big_endian
>
1125 class Local_got_offset_visitor
1128 Local_got_offset_visitor(struct Got_plt_view_info
& info
)
1133 operator()(unsigned int got_type
, unsigned int got_offset
)
1135 unsigned int got_index
= got_offset
/ this->got_entry_size_
;
1136 gold_assert(got_index
< this->info_
.got_count
);
1137 // We can only handle GOT entry types in the range 0..0x7e
1138 // because we use a byte array to store them, and we use the
1139 // high bit to flag a local symbol.
1140 gold_assert(got_type
< 0x7f);
1141 this->info_
.got_type_p
[got_index
] = got_type
| 0x80;
1142 unsigned char* pov
= this->info_
.got_desc_p
+ got_index
* 4;
1143 elfcpp::Swap
<32, big_endian
>::writeval(pov
, this->info_
.got_descriptor
);
1147 static const unsigned int got_entry_size_
= size
/ 8;
1148 struct Got_plt_view_info
& info_
;
1151 // Functor class for processing a GOT offset list. Writes the GOT type
1152 // and symbol index into the GOT type and descriptor arrays in the output
1155 template<int size
, bool big_endian
>
1156 class Global_got_offset_visitor
1159 Global_got_offset_visitor(struct Got_plt_view_info
& info
)
1164 operator()(unsigned int got_type
, unsigned int got_offset
)
1166 unsigned int got_index
= got_offset
/ this->got_entry_size_
;
1167 gold_assert(got_index
< this->info_
.got_count
);
1168 // We can only handle GOT entry types in the range 0..0x7e
1169 // because we use a byte array to store them, and we use the
1170 // high bit to flag a local symbol.
1171 gold_assert(got_type
< 0x7f);
1172 this->info_
.got_type_p
[got_index
] = got_type
;
1173 unsigned char* pov
= this->info_
.got_desc_p
+ got_index
* 4;
1174 elfcpp::Swap
<32, big_endian
>::writeval(pov
, this->info_
.got_descriptor
);
1178 static const unsigned int got_entry_size_
= size
/ 8;
1179 struct Got_plt_view_info
& info_
;
1182 // Functor class for processing the global symbol table. Processes the
1183 // GOT offset list for the symbol, and writes the symbol table index
1184 // into the PLT descriptor array in the output section.
1186 template<int size
, bool big_endian
>
1187 class Global_symbol_visitor_got_plt
1190 Global_symbol_visitor_got_plt(struct Got_plt_view_info
& info
)
1195 operator()(const Sized_symbol
<size
>* sym
)
1197 typedef Global_got_offset_visitor
<size
, big_endian
> Got_visitor
;
1198 const Got_offset_list
* got_offsets
= sym
->got_offset_list();
1199 if (got_offsets
!= NULL
)
1201 info_
.got_descriptor
= sym
->symtab_index();
1202 got_offsets
->for_all_got_offsets(Got_visitor(info_
));
1204 if (sym
->has_plt_offset())
1206 unsigned int plt_index
=
1207 ((sym
->plt_offset() - this->info_
.first_plt_entry_offset
)
1208 / this->info_
.plt_entry_size
);
1209 gold_assert(plt_index
< this->info_
.plt_count
);
1210 unsigned char* pov
= this->info_
.plt_desc_p
+ plt_index
* 4;
1211 elfcpp::Swap
<32, big_endian
>::writeval(pov
, sym
->symtab_index());
1216 struct Got_plt_view_info
& info_
;
1219 // Write the contents of the .gnu_incremental_got_plt section.
1221 template<int size
, bool big_endian
>
1223 Output_section_incremental_inputs
<size
, big_endian
>::write_got_plt(
1227 Sized_target
<size
, big_endian
>* target
=
1228 parameters
->sized_target
<size
, big_endian
>();
1230 // Set up the view information for the functors.
1231 struct Got_plt_view_info view_info
;
1232 view_info
.got_count
= target
->got_entry_count();
1233 view_info
.plt_count
= target
->plt_entry_count();
1234 view_info
.first_plt_entry_offset
= target
->first_plt_entry_offset();
1235 view_info
.plt_entry_size
= target
->plt_entry_size();
1236 view_info
.got_type_p
= pov
+ 8;
1237 view_info
.got_desc_p
= (view_info
.got_type_p
1238 + ((view_info
.got_count
+ 3) & ~3));
1239 view_info
.plt_desc_p
= view_info
.got_desc_p
+ view_info
.got_count
* 4;
1241 gold_assert(pov
+ view_size
==
1242 view_info
.plt_desc_p
+ view_info
.plt_count
* 4);
1244 // Write the section header.
1245 Swap32::writeval(pov
, view_info
.got_count
);
1246 Swap32::writeval(pov
+ 4, view_info
.plt_count
);
1248 // Initialize the GOT type array to 0xff (reserved).
1249 memset(view_info
.got_type_p
, 0xff, view_info
.got_count
);
1251 // Write the incremental GOT descriptors for local symbols.
1252 for (Incremental_inputs::Input_list::const_iterator p
=
1253 this->inputs_
->input_files().begin();
1254 p
!= this->inputs_
->input_files().end();
1257 if ((*p
)->type() != INCREMENTAL_INPUT_OBJECT
1258 && (*p
)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
)
1260 Incremental_object_entry
* entry
= (*p
)->object_entry();
1261 gold_assert(entry
!= NULL
);
1262 const Sized_relobj
<size
, big_endian
>* obj
=
1263 static_cast<Sized_relobj
<size
, big_endian
>*>(entry
->object());
1264 gold_assert(obj
!= NULL
);
1265 unsigned int nsyms
= obj
->local_symbol_count();
1266 for (unsigned int i
= 0; i
< nsyms
; i
++)
1268 const Got_offset_list
* got_offsets
= obj
->local_got_offset_list(i
);
1269 if (got_offsets
!= NULL
)
1271 typedef Local_got_offset_visitor
<size
, big_endian
> Got_visitor
;
1272 view_info
.got_descriptor
= (*p
)->get_offset();
1273 got_offsets
->for_all_got_offsets(Got_visitor(view_info
));
1278 // Write the incremental GOT and PLT descriptors for global symbols.
1279 typedef Global_symbol_visitor_got_plt
<size
, big_endian
> Symbol_visitor
;
1280 symtab_
->for_all_symbols
<size
, Symbol_visitor
>(Symbol_visitor(view_info
));
1283 // Instantiate the templates we need.
1285 #ifdef HAVE_TARGET_32_LITTLE
1287 class Sized_incremental_binary
<32, false>;
1290 #ifdef HAVE_TARGET_32_BIG
1292 class Sized_incremental_binary
<32, true>;
1295 #ifdef HAVE_TARGET_64_LITTLE
1297 class Sized_incremental_binary
<64, false>;
1300 #ifdef HAVE_TARGET_64_BIG
1302 class Sized_incremental_binary
<64, true>;
1305 } // End namespace gold.