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
>::do_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 // Determine whether an incremental link based on the existing output file
212 template<int size
, bool big_endian
>
214 Sized_incremental_binary
<size
, big_endian
>::do_check_inputs(
215 Incremental_inputs
* incremental_inputs
)
217 unsigned int inputs_shndx
;
218 unsigned int symtab_shndx
;
219 unsigned int relocs_shndx
;
220 unsigned int plt_got_shndx
;
221 unsigned int strtab_shndx
;
223 if (!do_find_incremental_inputs_sections(&inputs_shndx
, &symtab_shndx
,
224 &relocs_shndx
, &plt_got_shndx
,
227 explain_no_incremental(_("no incremental data from previous build"));
231 Location
inputs_location(this->elf_file_
.section_contents(inputs_shndx
));
232 Location
symtab_location(this->elf_file_
.section_contents(symtab_shndx
));
233 Location
relocs_location(this->elf_file_
.section_contents(relocs_shndx
));
234 Location
strtab_location(this->elf_file_
.section_contents(strtab_shndx
));
236 View
inputs_view(view(inputs_location
));
237 View
symtab_view(view(symtab_location
));
238 View
relocs_view(view(relocs_location
));
239 View
strtab_view(view(strtab_location
));
241 elfcpp::Elf_strtab
strtab(strtab_view
.data(), strtab_location
.data_size
);
243 Incremental_inputs_reader
<size
, big_endian
>
244 incoming_inputs(inputs_view
.data(), strtab
);
246 if (incoming_inputs
.version() != INCREMENTAL_LINK_VERSION
)
248 explain_no_incremental(_("different version of incremental build data"));
252 if (incremental_inputs
->command_line() != incoming_inputs
.command_line())
254 explain_no_incremental(_("command line changed"));
258 // TODO: compare incremental_inputs->inputs() with entries in data_view.
266 // Create a Sized_incremental_binary object of the specified size and
267 // endianness. Fails if the target architecture is not supported.
269 template<int size
, bool big_endian
>
271 make_sized_incremental_binary(Output_file
* file
,
272 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
274 Target
* target
= select_target(ehdr
.get_e_machine(), size
, big_endian
,
275 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
276 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
279 explain_no_incremental(_("unsupported ELF machine number %d"),
280 ehdr
.get_e_machine());
284 if (!parameters
->target_valid())
285 set_parameters_target(target
);
286 else if (target
!= ¶meters
->target())
287 gold_error(_("%s: incompatible target"), file
->filename());
289 return new Sized_incremental_binary
<size
, big_endian
>(file
, ehdr
, target
);
292 } // End of anonymous namespace.
294 // Create an Incremental_binary object for FILE. Returns NULL is this is not
295 // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
299 open_incremental_binary(Output_file
* file
)
301 off_t filesize
= file
->filesize();
302 int want
= elfcpp::Elf_recognizer::max_header_size
;
306 const unsigned char* p
= file
->get_input_view(0, want
);
307 if (!elfcpp::Elf_recognizer::is_elf_file(p
, want
))
309 explain_no_incremental(_("output is not an ELF file."));
314 bool big_endian
= false;
316 if (!elfcpp::Elf_recognizer::is_valid_header(p
, want
, &size
, &big_endian
,
319 explain_no_incremental(error
.c_str());
323 Incremental_binary
* result
= NULL
;
328 #ifdef HAVE_TARGET_32_BIG
329 result
= make_sized_incremental_binary
<32, true>(
330 file
, elfcpp::Ehdr
<32, true>(p
));
332 explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
337 #ifdef HAVE_TARGET_32_LITTLE
338 result
= make_sized_incremental_binary
<32, false>(
339 file
, elfcpp::Ehdr
<32, false>(p
));
341 explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
349 #ifdef HAVE_TARGET_64_BIG
350 result
= make_sized_incremental_binary
<64, true>(
351 file
, elfcpp::Ehdr
<64, true>(p
));
353 explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
358 #ifdef HAVE_TARGET_64_LITTLE
359 result
= make_sized_incremental_binary
<64, false>(
360 file
, elfcpp::Ehdr
<64, false>(p
));
362 explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
372 // Analyzes the output file to check if incremental linking is possible and
373 // (to be done) what files need to be relinked.
376 Incremental_checker::can_incrementally_link_output_file()
378 Output_file
output(this->output_name_
);
379 if (!output
.open_for_modification())
381 Incremental_binary
* binary
= open_incremental_binary(&output
);
384 return binary
->check_inputs(this->incremental_inputs_
);
387 // Class Incremental_inputs.
389 // Add the command line to the string table, setting
390 // command_line_key_. In incremental builds, the command line is
391 // stored in .gnu_incremental_inputs so that the next linker run can
392 // check if the command line options didn't change.
395 Incremental_inputs::report_command_line(int argc
, const char* const* argv
)
397 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
398 // different path to the linker.
399 std::string
args("gold");
400 // Copied from collect_argv in main.cc.
401 for (int i
= 1; i
< argc
; ++i
)
403 // Adding/removing these options should not result in a full relink.
404 if (strcmp(argv
[i
], "--incremental-changed") == 0
405 || strcmp(argv
[i
], "--incremental-unchanged") == 0
406 || strcmp(argv
[i
], "--incremental-unknown") == 0)
410 // Now append argv[i], but with all single-quotes escaped
411 const char* argpos
= argv
[i
];
414 const int len
= strcspn(argpos
, "'");
415 args
.append(argpos
, len
);
416 if (argpos
[len
] == '\0')
418 args
.append("'\"'\"'");
424 this->command_line_
= args
;
425 this->strtab_
->add(this->command_line_
.c_str(), false,
426 &this->command_line_key_
);
429 // Record the input archive file ARCHIVE. This is called by the
430 // Add_archive_symbols task before determining which archive members
431 // to include. We create the Incremental_archive_entry here and
432 // attach it to the Archive, but we do not add it to the list of
433 // input objects until report_archive_end is called.
436 Incremental_inputs::report_archive_begin(Archive
* arch
)
438 Stringpool::Key filename_key
;
439 Timespec mtime
= arch
->file().get_mtime();
441 this->strtab_
->add(arch
->filename().c_str(), false, &filename_key
);
442 Incremental_archive_entry
* entry
=
443 new Incremental_archive_entry(filename_key
, arch
, mtime
);
444 arch
->set_incremental_info(entry
);
447 // Finish recording the input archive file ARCHIVE. This is called by the
448 // Add_archive_symbols task after determining which archive members
452 Incremental_inputs::report_archive_end(Archive
* arch
)
454 Incremental_archive_entry
* entry
= arch
->incremental_info();
456 gold_assert(entry
!= NULL
);
458 // Collect unused global symbols.
459 for (Archive::Unused_symbol_iterator p
= arch
->unused_symbols_begin();
460 p
!= arch
->unused_symbols_end();
463 Stringpool::Key symbol_key
;
464 this->strtab_
->add(*p
, true, &symbol_key
);
465 entry
->add_unused_global_symbol(symbol_key
);
467 this->inputs_
.push_back(entry
);
470 // Record the input object file OBJ. If ARCH is not NULL, attach
471 // the object file to the archive. This is called by the
472 // Add_symbols task after finding out the type of the file.
475 Incremental_inputs::report_object(Object
* obj
, Archive
* arch
)
477 Stringpool::Key filename_key
;
478 Timespec mtime
= obj
->input_file()->file().get_mtime();
480 this->strtab_
->add(obj
->name().c_str(), false, &filename_key
);
481 Incremental_object_entry
* obj_entry
=
482 new Incremental_object_entry(filename_key
, obj
, mtime
);
483 this->inputs_
.push_back(obj_entry
);
487 Incremental_archive_entry
* arch_entry
= arch
->incremental_info();
488 gold_assert(arch_entry
!= NULL
);
489 arch_entry
->add_object(obj_entry
);
492 this->current_object_
= obj
;
493 this->current_object_entry_
= obj_entry
;
496 // Record the input object file OBJ. If ARCH is not NULL, attach
497 // the object file to the archive. This is called by the
498 // Add_symbols task after finding out the type of the file.
501 Incremental_inputs::report_input_section(Object
* obj
, unsigned int shndx
,
502 const char* name
, off_t sh_size
)
504 Stringpool::Key key
= 0;
507 this->strtab_
->add(name
, true, &key
);
509 gold_assert(obj
== this->current_object_
);
510 this->current_object_entry_
->add_input_section(shndx
, key
, sh_size
);
513 // Record that the input argument INPUT is a script SCRIPT. This is
514 // called by read_script after parsing the script and reading the list
515 // of inputs added by this script.
518 Incremental_inputs::report_script(const std::string
& filename
,
519 Script_info
* script
, Timespec mtime
)
521 Stringpool::Key filename_key
;
523 this->strtab_
->add(filename
.c_str(), false, &filename_key
);
524 Incremental_script_entry
* entry
=
525 new Incremental_script_entry(filename_key
, script
, mtime
);
526 this->inputs_
.push_back(entry
);
529 // Finalize the incremental link information. Called from
533 Incremental_inputs::finalize()
535 // Finalize the string table.
536 this->strtab_
->set_string_offsets();
539 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
542 Incremental_inputs::create_data_sections(Symbol_table
* symtab
)
544 switch (parameters
->size_and_endianness())
546 #ifdef HAVE_TARGET_32_LITTLE
547 case Parameters::TARGET_32_LITTLE
:
548 this->inputs_section_
=
549 new Output_section_incremental_inputs
<32, false>(this, symtab
);
552 #ifdef HAVE_TARGET_32_BIG
553 case Parameters::TARGET_32_BIG
:
554 this->inputs_section_
=
555 new Output_section_incremental_inputs
<32, true>(this, symtab
);
558 #ifdef HAVE_TARGET_64_LITTLE
559 case Parameters::TARGET_64_LITTLE
:
560 this->inputs_section_
=
561 new Output_section_incremental_inputs
<64, false>(this, symtab
);
564 #ifdef HAVE_TARGET_64_BIG
565 case Parameters::TARGET_64_BIG
:
566 this->inputs_section_
=
567 new Output_section_incremental_inputs
<64, true>(this, symtab
);
573 this->symtab_section_
= new Output_data_space(4, "** incremental_symtab");
574 this->relocs_section_
= new Output_data_space(4, "** incremental_relocs");
575 this->got_plt_section_
= new Output_data_space(4, "** incremental_got_plt");
578 // Return the sh_entsize value for the .gnu_incremental_relocs section.
580 Incremental_inputs::relocs_entsize() const
582 return 8 + 2 * parameters
->target().get_size() / 8;
585 // Class Output_section_incremental_inputs.
587 // Finalize the offsets for each input section and supplemental info block,
588 // and set the final data size of the incremental output sections.
590 template<int size
, bool big_endian
>
592 Output_section_incremental_inputs
<size
, big_endian
>::set_final_data_size()
594 const Incremental_inputs
* inputs
= this->inputs_
;
595 const unsigned int sizeof_addr
= size
/ 8;
596 const unsigned int rel_size
= 8 + 2 * sizeof_addr
;
598 // Offset of each input entry.
599 unsigned int input_offset
= this->header_size
;
601 // Offset of each supplemental info block.
602 unsigned int info_offset
= this->header_size
;
603 info_offset
+= this->input_entry_size
* inputs
->input_file_count();
605 // Count each input file and its supplemental information block.
606 for (Incremental_inputs::Input_list::const_iterator p
=
607 inputs
->input_files().begin();
608 p
!= inputs
->input_files().end();
611 // Set the offset of the input file entry.
612 (*p
)->set_offset(input_offset
);
613 input_offset
+= this->input_entry_size
;
615 // Set the offset of the supplemental info block.
616 switch ((*p
)->type())
618 case INCREMENTAL_INPUT_SCRIPT
:
619 // No supplemental info for a script.
620 (*p
)->set_info_offset(0);
622 case INCREMENTAL_INPUT_OBJECT
:
623 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
625 Incremental_object_entry
*entry
= (*p
)->object_entry();
626 gold_assert(entry
!= NULL
);
627 (*p
)->set_info_offset(info_offset
);
628 // Input section count + global symbol count.
630 // Each input section.
631 info_offset
+= (entry
->get_input_section_count()
632 * (8 + 2 * sizeof_addr
));
633 // Each global symbol.
634 const Object::Symbols
* syms
= entry
->object()->get_global_symbols();
635 info_offset
+= syms
->size() * 16;
638 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
640 Incremental_object_entry
*entry
= (*p
)->object_entry();
641 gold_assert(entry
!= NULL
);
642 (*p
)->set_info_offset(info_offset
);
643 // Global symbol count.
645 // Each global symbol.
646 const Object::Symbols
* syms
= entry
->object()->get_global_symbols();
647 unsigned int nsyms
= syms
!= NULL
? syms
->size() : 0;
648 info_offset
+= nsyms
* 4;
651 case INCREMENTAL_INPUT_ARCHIVE
:
653 Incremental_archive_entry
*entry
= (*p
)->archive_entry();
654 gold_assert(entry
!= NULL
);
655 (*p
)->set_info_offset(info_offset
);
656 // Member count + unused global symbol count.
659 info_offset
+= (entry
->get_member_count() * 4);
660 // Each global symbol.
661 info_offset
+= (entry
->get_unused_global_symbol_count() * 4);
669 this->set_data_size(info_offset
);
671 // Set the size of the .gnu_incremental_symtab section.
672 inputs
->symtab_section()->set_current_data_size(this->symtab_
->output_count()
673 * sizeof(unsigned int));
675 // Set the size of the .gnu_incremental_relocs section.
676 inputs
->relocs_section()->set_current_data_size(inputs
->get_reloc_count()
679 // Set the size of the .gnu_incremental_got_plt section.
680 Sized_target
<size
, big_endian
>* target
=
681 parameters
->sized_target
<size
, big_endian
>();
682 unsigned int got_count
= target
->got_entry_count();
683 unsigned int plt_count
= target
->plt_entry_count();
684 unsigned int got_plt_size
= 8; // GOT entry count, PLT entry count.
685 got_plt_size
= (got_plt_size
+ got_count
+ 3) & ~3; // GOT type array.
686 got_plt_size
+= got_count
* 4 + plt_count
* 4; // GOT array, PLT array.
687 inputs
->got_plt_section()->set_current_data_size(got_plt_size
);
690 // Write the contents of the .gnu_incremental_inputs and
691 // .gnu_incremental_symtab sections.
693 template<int size
, bool big_endian
>
695 Output_section_incremental_inputs
<size
, big_endian
>::do_write(Output_file
* of
)
697 const Incremental_inputs
* inputs
= this->inputs_
;
698 Stringpool
* strtab
= inputs
->get_stringpool();
700 // Get a view into the .gnu_incremental_inputs section.
701 const off_t off
= this->offset();
702 const off_t oview_size
= this->data_size();
703 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
704 unsigned char* pov
= oview
;
706 // Get a view into the .gnu_incremental_symtab section.
707 const off_t symtab_off
= inputs
->symtab_section()->offset();
708 const off_t symtab_size
= inputs
->symtab_section()->data_size();
709 unsigned char* const symtab_view
= of
->get_output_view(symtab_off
,
712 // Allocate an array of linked list heads for the .gnu_incremental_symtab
713 // section. Each element corresponds to a global symbol in the output
714 // symbol table, and points to the head of the linked list that threads
715 // through the object file input entries. The value of each element
716 // is the section-relative offset to a global symbol entry in a
717 // supplemental information block.
718 unsigned int global_sym_count
= this->symtab_
->output_count();
719 unsigned int* global_syms
= new unsigned int[global_sym_count
];
720 memset(global_syms
, 0, global_sym_count
* sizeof(unsigned int));
722 // Write the section header.
723 Stringpool::Key command_line_key
= inputs
->command_line_key();
724 pov
= this->write_header(pov
, inputs
->input_file_count(),
725 strtab
->get_offset_from_key(command_line_key
));
727 // Write the list of input files.
728 pov
= this->write_input_files(oview
, pov
, strtab
);
730 // Write the supplemental information blocks for each input file.
731 pov
= this->write_info_blocks(oview
, pov
, strtab
, global_syms
,
734 gold_assert(pov
- oview
== oview_size
);
736 // Write the .gnu_incremental_symtab section.
737 gold_assert(global_sym_count
* 4 == symtab_size
);
738 this->write_symtab(symtab_view
, global_syms
, global_sym_count
);
740 delete[] global_syms
;
742 // Write the .gnu_incremental_got_plt section.
743 const off_t got_plt_off
= inputs
->got_plt_section()->offset();
744 const off_t got_plt_size
= inputs
->got_plt_section()->data_size();
745 unsigned char* const got_plt_view
= of
->get_output_view(got_plt_off
,
747 this->write_got_plt(got_plt_view
, got_plt_size
);
749 of
->write_output_view(off
, oview_size
, oview
);
750 of
->write_output_view(symtab_off
, symtab_size
, symtab_view
);
751 of
->write_output_view(got_plt_off
, got_plt_size
, got_plt_view
);
754 // Write the section header: version, input file count, offset of command line
755 // in the string table, and 4 bytes of padding.
757 template<int size
, bool big_endian
>
759 Output_section_incremental_inputs
<size
, big_endian
>::write_header(
761 unsigned int input_file_count
,
762 section_offset_type command_line_offset
)
764 Swap32::writeval(pov
, INCREMENTAL_LINK_VERSION
);
765 Swap32::writeval(pov
+ 4, input_file_count
);
766 Swap32::writeval(pov
+ 8, command_line_offset
);
767 Swap32::writeval(pov
+ 12, 0);
768 return pov
+ this->header_size
;
771 // Write the input file entries.
773 template<int size
, bool big_endian
>
775 Output_section_incremental_inputs
<size
, big_endian
>::write_input_files(
776 unsigned char* oview
,
780 const Incremental_inputs
* inputs
= this->inputs_
;
782 for (Incremental_inputs::Input_list::const_iterator p
=
783 inputs
->input_files().begin();
784 p
!= inputs
->input_files().end();
787 gold_assert(pov
- oview
== (*p
)->get_offset());
788 section_offset_type filename_offset
=
789 strtab
->get_offset_from_key((*p
)->get_filename_key());
790 const Timespec
& mtime
= (*p
)->get_mtime();
791 Swap32::writeval(pov
, filename_offset
);
792 Swap32::writeval(pov
+ 4, (*p
)->get_info_offset());
793 Swap64::writeval(pov
+ 8, mtime
.seconds
);
794 Swap32::writeval(pov
+ 16, mtime
.nanoseconds
);
795 Swap16::writeval(pov
+ 20, (*p
)->type());
796 Swap16::writeval(pov
+ 22, 0);
797 pov
+= this->input_entry_size
;
802 // Write the supplemental information blocks.
804 template<int size
, bool big_endian
>
806 Output_section_incremental_inputs
<size
, big_endian
>::write_info_blocks(
807 unsigned char* oview
,
810 unsigned int* global_syms
,
811 unsigned int global_sym_count
)
813 const Incremental_inputs
* inputs
= this->inputs_
;
814 unsigned int first_global_index
= this->symtab_
->first_global_index();
816 for (Incremental_inputs::Input_list::const_iterator p
=
817 inputs
->input_files().begin();
818 p
!= inputs
->input_files().end();
821 switch ((*p
)->type())
823 case INCREMENTAL_INPUT_SCRIPT
:
824 // No supplemental info for a script.
827 case INCREMENTAL_INPUT_OBJECT
:
828 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
830 gold_assert(pov
- oview
== (*p
)->get_info_offset());
831 Incremental_object_entry
* entry
= (*p
)->object_entry();
832 gold_assert(entry
!= NULL
);
833 const Object
* obj
= entry
->object();
834 const Object::Symbols
* syms
= obj
->get_global_symbols();
835 // Write the input section count and global symbol count.
836 unsigned int nsections
= entry
->get_input_section_count();
837 unsigned int nsyms
= syms
->size();
838 Swap32::writeval(pov
, nsections
);
839 Swap32::writeval(pov
+ 4, nsyms
);
842 // For each input section, write the name, output section index,
843 // offset within output section, and input section size.
844 for (unsigned int i
= 0; i
< nsections
; i
++)
846 Stringpool::Key key
= entry
->get_input_section_name_key(i
);
847 off_t name_offset
= 0;
849 name_offset
= strtab
->get_offset_from_key(key
);
851 off_t out_offset
= 0;
853 Output_section
* os
= obj
->output_section(i
);
856 out_shndx
= os
->out_shndx();
857 out_offset
= obj
->output_section_offset(i
);
858 sh_size
= entry
->get_input_section_size(i
);
860 Swap32::writeval(pov
, name_offset
);
861 Swap32::writeval(pov
+ 4, out_shndx
);
862 Swap::writeval(pov
+ 8, out_offset
);
863 Swap::writeval(pov
+ 8 + sizeof_addr
, sh_size
);
864 pov
+= 8 + 2 * sizeof_addr
;
867 // For each global symbol, write its associated relocations,
868 // add it to the linked list of globals, then write the
869 // supplemental information: global symbol table index,
870 // linked list chain pointer, relocation count, and offset
871 // to the relocations.
872 for (unsigned int i
= 0; i
< nsyms
; i
++)
874 const Symbol
* sym
= (*syms
)[i
];
875 unsigned int symtab_index
= sym
->symtab_index();
876 unsigned int chain
= 0;
877 unsigned int first_reloc
= 0;
878 unsigned int nrelocs
= obj
->get_incremental_reloc_count(i
);
881 gold_assert(symtab_index
!= -1U
882 && (symtab_index
- first_global_index
883 < global_sym_count
));
884 first_reloc
= obj
->get_incremental_reloc_base(i
);
885 chain
= global_syms
[symtab_index
- first_global_index
];
886 global_syms
[symtab_index
- first_global_index
] =
889 Swap32::writeval(pov
, symtab_index
);
890 Swap32::writeval(pov
+ 4, chain
);
891 Swap32::writeval(pov
+ 8, nrelocs
);
892 Swap32::writeval(pov
+ 12, first_reloc
* 3 * sizeof_addr
);
898 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
900 gold_assert(pov
- oview
== (*p
)->get_info_offset());
901 Incremental_object_entry
* entry
= (*p
)->object_entry();
902 gold_assert(entry
!= NULL
);
903 const Object
* obj
= entry
->object();
904 const Object::Symbols
* syms
= obj
->get_global_symbols();
906 // Write the global symbol count.
907 unsigned int nsyms
= syms
!= NULL
? syms
->size() : 0;
908 Swap32::writeval(pov
, nsyms
);
911 // For each global symbol, write the global symbol table index.
912 for (unsigned int i
= 0; i
< nsyms
; i
++)
914 const Symbol
* sym
= (*syms
)[i
];
915 Swap32::writeval(pov
, sym
->symtab_index());
921 case INCREMENTAL_INPUT_ARCHIVE
:
923 gold_assert(pov
- oview
== (*p
)->get_info_offset());
924 Incremental_archive_entry
* entry
= (*p
)->archive_entry();
925 gold_assert(entry
!= NULL
);
927 // Write the member count and unused global symbol count.
928 unsigned int nmembers
= entry
->get_member_count();
929 unsigned int nsyms
= entry
->get_unused_global_symbol_count();
930 Swap32::writeval(pov
, nmembers
);
931 Swap32::writeval(pov
+ 4, nsyms
);
934 // For each member, write the offset to its input file entry.
935 for (unsigned int i
= 0; i
< nmembers
; ++i
)
937 Incremental_object_entry
* member
= entry
->get_member(i
);
938 Swap32::writeval(pov
, member
->get_offset());
942 // For each global symbol, write the name offset.
943 for (unsigned int i
= 0; i
< nsyms
; ++i
)
945 Stringpool::Key key
= entry
->get_unused_global_symbol(i
);
946 Swap32::writeval(pov
, strtab
->get_offset_from_key(key
));
959 // Write the contents of the .gnu_incremental_symtab section.
961 template<int size
, bool big_endian
>
963 Output_section_incremental_inputs
<size
, big_endian
>::write_symtab(
965 unsigned int* global_syms
,
966 unsigned int global_sym_count
)
968 for (unsigned int i
= 0; i
< global_sym_count
; ++i
)
970 Swap32::writeval(pov
, global_syms
[i
]);
975 // This struct holds the view information needed to write the
976 // .gnu_incremental_got_plt section.
978 struct Got_plt_view_info
980 // Start of the GOT type array in the output view.
981 unsigned char* got_type_p
;
982 // Start of the GOT descriptor array in the output view.
983 unsigned char* got_desc_p
;
984 // Start of the PLT descriptor array in the output view.
985 unsigned char* plt_desc_p
;
986 // Number of GOT entries.
987 unsigned int got_count
;
988 // Number of PLT entries.
989 unsigned int plt_count
;
990 // Offset of the first non-reserved PLT entry (this is a target-dependent value).
991 unsigned int first_plt_entry_offset
;
992 // Size of a PLT entry (this is a target-dependent value).
993 unsigned int plt_entry_size
;
994 // Value to write in the GOT descriptor array. For global symbols,
995 // this is the global symbol table index; for local symbols, it is
996 // the offset of the input file entry in the .gnu_incremental_inputs
998 unsigned int got_descriptor
;
1001 // Functor class for processing a GOT offset list for local symbols.
1002 // Writes the GOT type and symbol index into the GOT type and descriptor
1003 // arrays in the output section.
1005 template<int size
, bool big_endian
>
1006 class Local_got_offset_visitor
1009 Local_got_offset_visitor(struct Got_plt_view_info
& info
)
1014 operator()(unsigned int got_type
, unsigned int got_offset
)
1016 unsigned int got_index
= got_offset
/ this->got_entry_size_
;
1017 gold_assert(got_index
< this->info_
.got_count
);
1018 // We can only handle GOT entry types in the range 0..0x7e
1019 // because we use a byte array to store them, and we use the
1020 // high bit to flag a local symbol.
1021 gold_assert(got_type
< 0x7f);
1022 this->info_
.got_type_p
[got_index
] = got_type
| 0x80;
1023 unsigned char* pov
= this->info_
.got_desc_p
+ got_index
* 4;
1024 elfcpp::Swap
<32, big_endian
>::writeval(pov
, this->info_
.got_descriptor
);
1028 static const unsigned int got_entry_size_
= size
/ 8;
1029 struct Got_plt_view_info
& info_
;
1032 // Functor class for processing a GOT offset list. Writes the GOT type
1033 // and symbol index into the GOT type and descriptor arrays in the output
1036 template<int size
, bool big_endian
>
1037 class Global_got_offset_visitor
1040 Global_got_offset_visitor(struct Got_plt_view_info
& info
)
1045 operator()(unsigned int got_type
, unsigned int got_offset
)
1047 unsigned int got_index
= got_offset
/ this->got_entry_size_
;
1048 gold_assert(got_index
< this->info_
.got_count
);
1049 // We can only handle GOT entry types in the range 0..0x7e
1050 // because we use a byte array to store them, and we use the
1051 // high bit to flag a local symbol.
1052 gold_assert(got_type
< 0x7f);
1053 this->info_
.got_type_p
[got_index
] = got_type
;
1054 unsigned char* pov
= this->info_
.got_desc_p
+ got_index
* 4;
1055 elfcpp::Swap
<32, big_endian
>::writeval(pov
, this->info_
.got_descriptor
);
1059 static const unsigned int got_entry_size_
= size
/ 8;
1060 struct Got_plt_view_info
& info_
;
1063 // Functor class for processing the global symbol table. Processes the
1064 // GOT offset list for the symbol, and writes the symbol table index
1065 // into the PLT descriptor array in the output section.
1067 template<int size
, bool big_endian
>
1068 class Global_symbol_visitor_got_plt
1071 Global_symbol_visitor_got_plt(struct Got_plt_view_info
& info
)
1076 operator()(const Sized_symbol
<size
>* sym
)
1078 typedef Global_got_offset_visitor
<size
, big_endian
> Got_visitor
;
1079 const Got_offset_list
* got_offsets
= sym
->got_offset_list();
1080 if (got_offsets
!= NULL
)
1082 info_
.got_descriptor
= sym
->symtab_index();
1083 got_offsets
->for_all_got_offsets(Got_visitor(info_
));
1085 if (sym
->has_plt_offset())
1087 unsigned int plt_index
=
1088 ((sym
->plt_offset() - this->info_
.first_plt_entry_offset
)
1089 / this->info_
.plt_entry_size
);
1090 gold_assert(plt_index
< this->info_
.plt_count
);
1091 unsigned char* pov
= this->info_
.plt_desc_p
+ plt_index
* 4;
1092 elfcpp::Swap
<32, big_endian
>::writeval(pov
, sym
->symtab_index());
1097 struct Got_plt_view_info
& info_
;
1100 // Write the contents of the .gnu_incremental_got_plt section.
1102 template<int size
, bool big_endian
>
1104 Output_section_incremental_inputs
<size
, big_endian
>::write_got_plt(
1108 Sized_target
<size
, big_endian
>* target
=
1109 parameters
->sized_target
<size
, big_endian
>();
1111 // Set up the view information for the functors.
1112 struct Got_plt_view_info view_info
;
1113 view_info
.got_count
= target
->got_entry_count();
1114 view_info
.plt_count
= target
->plt_entry_count();
1115 view_info
.first_plt_entry_offset
= target
->first_plt_entry_offset();
1116 view_info
.plt_entry_size
= target
->plt_entry_size();
1117 view_info
.got_type_p
= pov
+ 8;
1118 view_info
.got_desc_p
= (view_info
.got_type_p
1119 + ((view_info
.got_count
+ 3) & ~3));
1120 view_info
.plt_desc_p
= view_info
.got_desc_p
+ view_info
.got_count
* 4;
1122 gold_assert(pov
+ view_size
==
1123 view_info
.plt_desc_p
+ view_info
.plt_count
* 4);
1125 // Write the section header.
1126 Swap32::writeval(pov
, view_info
.got_count
);
1127 Swap32::writeval(pov
+ 4, view_info
.plt_count
);
1129 // Initialize the GOT type array to 0xff (reserved).
1130 memset(view_info
.got_type_p
, 0xff, view_info
.got_count
);
1132 // Write the incremental GOT descriptors for local symbols.
1133 for (Incremental_inputs::Input_list::const_iterator p
=
1134 this->inputs_
->input_files().begin();
1135 p
!= this->inputs_
->input_files().end();
1138 if ((*p
)->type() != INCREMENTAL_INPUT_OBJECT
1139 && (*p
)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
)
1141 Incremental_object_entry
* entry
= (*p
)->object_entry();
1142 gold_assert(entry
!= NULL
);
1143 const Sized_relobj
<size
, big_endian
>* obj
=
1144 static_cast<Sized_relobj
<size
, big_endian
>*>(entry
->object());
1145 gold_assert(obj
!= NULL
);
1146 unsigned int nsyms
= obj
->local_symbol_count();
1147 for (unsigned int i
= 0; i
< nsyms
; i
++)
1149 const Got_offset_list
* got_offsets
= obj
->local_got_offset_list(i
);
1150 if (got_offsets
!= NULL
)
1152 typedef Local_got_offset_visitor
<size
, big_endian
> Got_visitor
;
1153 view_info
.got_descriptor
= (*p
)->get_offset();
1154 got_offsets
->for_all_got_offsets(Got_visitor(view_info
));
1159 // Write the incremental GOT and PLT descriptors for global symbols.
1160 typedef Global_symbol_visitor_got_plt
<size
, big_endian
> Symbol_visitor
;
1161 symtab_
->for_all_symbols
<size
, Symbol_visitor
>(Symbol_visitor(view_info
));
1164 // Instantiate the templates we need.
1166 #ifdef HAVE_TARGET_32_LITTLE
1168 class Sized_incremental_binary
<32, false>;
1171 #ifdef HAVE_TARGET_32_BIG
1173 class Sized_incremental_binary
<32, true>;
1176 #ifdef HAVE_TARGET_64_LITTLE
1178 class Sized_incremental_binary
<64, false>;
1181 #ifdef HAVE_TARGET_64_BIG
1183 class Sized_incremental_binary
<64, true>;
1186 } // End namespace gold.