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"
32 #include "incremental.h"
36 #include "target-select.h"
43 // Version information. Will change frequently during the development, later
44 // we could think about backward (and forward?) compatibility.
45 const unsigned int INCREMENTAL_LINK_VERSION
= 1;
47 // This class manages the .gnu_incremental_inputs section, which holds
48 // the header information, a directory of input files, and separate
49 // entries for each input file.
51 template<int size
, bool big_endian
>
52 class Output_section_incremental_inputs
: public Output_section_data
55 Output_section_incremental_inputs(const Incremental_inputs
* inputs
,
56 const Symbol_table
* symtab
)
57 : Output_section_data(size
/ 8), inputs_(inputs
), symtab_(symtab
)
61 // This is called to update the section size prior to assigning
62 // the address and file offset.
65 { this->set_final_data_size(); }
67 // Set the final data size.
69 set_final_data_size();
71 // Write the data to the file.
73 do_write(Output_file
*);
75 // Write to a map file.
77 do_print_to_mapfile(Mapfile
* mapfile
) const
78 { mapfile
->print_output_data(this, _("** incremental_inputs")); }
81 // Write the section header.
83 write_header(unsigned char* pov
, unsigned int input_file_count
,
84 section_offset_type command_line_offset
);
86 // Write the input file entries.
88 write_input_files(unsigned char* oview
, unsigned char* pov
,
91 // Write the supplemental information blocks.
93 write_info_blocks(unsigned char* oview
, unsigned char* pov
,
94 Stringpool
* strtab
, unsigned int* global_syms
,
95 unsigned int global_sym_count
);
97 // Write the contents of the .gnu_incremental_symtab section.
99 write_symtab(unsigned char* pov
, unsigned int* global_syms
,
100 unsigned int global_sym_count
);
102 // Write the contents of the .gnu_incremental_got_plt section.
104 write_got_plt(unsigned char* pov
, off_t view_size
);
106 // Typedefs for writing the data to the output sections.
107 typedef elfcpp::Swap
<size
, big_endian
> Swap
;
108 typedef elfcpp::Swap
<16, big_endian
> Swap16
;
109 typedef elfcpp::Swap
<32, big_endian
> Swap32
;
110 typedef elfcpp::Swap
<64, big_endian
> Swap64
;
112 // Sizes of various structures.
113 static const int sizeof_addr
= size
/ 8;
114 static const int header_size
= 16;
115 static const int input_entry_size
= 24;
117 // The Incremental_inputs object.
118 const Incremental_inputs
* inputs_
;
121 const Symbol_table
* symtab_
;
124 // Inform the user why we don't do an incremental link. Not called in
125 // the obvious case of missing output file. TODO: Is this helpful?
128 vexplain_no_incremental(const char* format
, va_list args
)
131 if (vasprintf(&buf
, format
, args
) < 0)
133 gold_info(_("the link might take longer: "
134 "cannot perform incremental link: %s"), buf
);
139 explain_no_incremental(const char* format
, ...)
142 va_start(args
, format
);
143 vexplain_no_incremental(format
, args
);
150 Incremental_binary::error(const char* format
, ...) const
153 va_start(args
, format
);
154 // Current code only checks if the file can be used for incremental linking,
155 // so errors shouldn't fail the build, but only result in a fallback to a
157 // TODO: when we implement incremental editing of the file, we may need a
158 // flag that will cause errors to be treated seriously.
159 vexplain_no_incremental(format
, args
);
163 // Find the .gnu_incremental_inputs section and related sections.
165 template<int size
, bool big_endian
>
167 Sized_incremental_binary
<size
, big_endian
>::find_incremental_inputs_sections(
168 unsigned int* p_inputs_shndx
,
169 unsigned int* p_symtab_shndx
,
170 unsigned int* p_relocs_shndx
,
171 unsigned int* p_got_plt_shndx
,
172 unsigned int* p_strtab_shndx
)
174 unsigned int inputs_shndx
=
175 this->elf_file_
.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS
);
176 if (inputs_shndx
== elfcpp::SHN_UNDEF
) // Not found.
179 unsigned int symtab_shndx
=
180 this->elf_file_
.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB
);
181 if (symtab_shndx
== elfcpp::SHN_UNDEF
) // Not found.
183 if (this->elf_file_
.section_link(symtab_shndx
) != inputs_shndx
)
186 unsigned int relocs_shndx
=
187 this->elf_file_
.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS
);
188 if (relocs_shndx
== elfcpp::SHN_UNDEF
) // Not found.
190 if (this->elf_file_
.section_link(relocs_shndx
) != inputs_shndx
)
193 unsigned int got_plt_shndx
=
194 this->elf_file_
.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT
);
195 if (got_plt_shndx
== elfcpp::SHN_UNDEF
) // Not found.
197 if (this->elf_file_
.section_link(got_plt_shndx
) != inputs_shndx
)
200 unsigned int strtab_shndx
= this->elf_file_
.section_link(inputs_shndx
);
201 if (strtab_shndx
== elfcpp::SHN_UNDEF
202 || strtab_shndx
> this->elf_file_
.shnum()
203 || this->elf_file_
.section_type(strtab_shndx
) != elfcpp::SHT_STRTAB
)
206 if (p_inputs_shndx
!= NULL
)
207 *p_inputs_shndx
= inputs_shndx
;
208 if (p_symtab_shndx
!= NULL
)
209 *p_symtab_shndx
= symtab_shndx
;
210 if (p_relocs_shndx
!= NULL
)
211 *p_relocs_shndx
= relocs_shndx
;
212 if (p_got_plt_shndx
!= NULL
)
213 *p_got_plt_shndx
= got_plt_shndx
;
214 if (p_strtab_shndx
!= NULL
)
215 *p_strtab_shndx
= strtab_shndx
;
219 // Set up the readers into the incremental info sections.
221 template<int size
, bool big_endian
>
223 Sized_incremental_binary
<size
, big_endian
>::setup_readers()
225 unsigned int inputs_shndx
;
226 unsigned int symtab_shndx
;
227 unsigned int relocs_shndx
;
228 unsigned int got_plt_shndx
;
229 unsigned int strtab_shndx
;
231 if (!this->find_incremental_inputs_sections(&inputs_shndx
, &symtab_shndx
,
232 &relocs_shndx
, &got_plt_shndx
,
236 Location
inputs_location(this->elf_file_
.section_contents(inputs_shndx
));
237 Location
symtab_location(this->elf_file_
.section_contents(symtab_shndx
));
238 Location
relocs_location(this->elf_file_
.section_contents(relocs_shndx
));
239 Location
got_plt_location(this->elf_file_
.section_contents(got_plt_shndx
));
240 Location
strtab_location(this->elf_file_
.section_contents(strtab_shndx
));
242 View inputs_view
= this->view(inputs_location
);
243 View symtab_view
= this->view(symtab_location
);
244 View relocs_view
= this->view(relocs_location
);
245 View got_plt_view
= this->view(got_plt_location
);
246 View strtab_view
= this->view(strtab_location
);
248 elfcpp::Elf_strtab
strtab(strtab_view
.data(), strtab_location
.data_size
);
250 this->inputs_reader_
=
251 Incremental_inputs_reader
<size
, big_endian
>(inputs_view
.data(), strtab
);
252 this->symtab_reader_
=
253 Incremental_symtab_reader
<big_endian
>(symtab_view
.data(),
254 symtab_location
.data_size
);
255 this->relocs_reader_
=
256 Incremental_relocs_reader
<size
, big_endian
>(relocs_view
.data(),
257 relocs_location
.data_size
);
258 this->got_plt_reader_
=
259 Incremental_got_plt_reader
<big_endian
>(got_plt_view
.data());
261 // Walk the list of input files (a) to setup an Input_reader for each
262 // input file, and (b) to record maps of files added from archive
263 // libraries and scripts.
264 Incremental_inputs_reader
<size
, big_endian
>& inputs
= this->inputs_reader_
;
265 unsigned int count
= inputs
.input_file_count();
266 this->input_entry_readers_
.reserve(count
);
267 this->library_map_
.resize(count
);
268 this->script_map_
.resize(count
);
269 for (unsigned int i
= 0; i
< count
; i
++)
271 Input_entry_reader input_file
= inputs
.input_file(i
);
272 this->input_entry_readers_
.push_back(Sized_input_reader(input_file
));
273 switch (input_file
.type())
275 case INCREMENTAL_INPUT_OBJECT
:
276 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
277 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
278 // No special treatment necessary.
280 case INCREMENTAL_INPUT_ARCHIVE
:
282 Incremental_library
* lib
=
283 new Incremental_library(input_file
.filename(), i
,
284 &this->input_entry_readers_
[i
]);
285 this->library_map_
[i
] = lib
;
286 unsigned int member_count
= input_file
.get_member_count();
287 for (unsigned int j
= 0; j
< member_count
; j
++)
289 int member_offset
= input_file
.get_member_offset(j
);
290 int member_index
= inputs
.input_file_index(member_offset
);
291 this->library_map_
[member_index
] = lib
;
295 case INCREMENTAL_INPUT_SCRIPT
:
297 Script_info
* script
= new Script_info(input_file
.filename());
298 this->script_map_
[i
] = script
;
299 unsigned int object_count
= input_file
.get_object_count();
300 for (unsigned int j
= 0; j
< object_count
; j
++)
302 int object_offset
= input_file
.get_object_offset(j
);
303 int object_index
= inputs
.input_file_index(object_offset
);
304 this->script_map_
[object_index
] = script
;
313 // Initialize the map of global symbols.
314 unsigned int nglobals
= this->symtab_reader_
.symbol_count();
315 this->symbol_map_
.resize(nglobals
);
317 this->has_incremental_info_
= true;
320 // Walk the list of input files given on the command line, and build
321 // a direct map of file index to the corresponding input argument.
324 check_input_args(std::vector
<const Input_argument
*>& input_args_map
,
325 Input_arguments::const_iterator begin
,
326 Input_arguments::const_iterator end
)
328 for (Input_arguments::const_iterator p
= begin
;
334 const Input_file_group
* group
= p
->group();
335 check_input_args(input_args_map
, group
->begin(), group
->end());
337 else if (p
->is_lib())
339 const Input_file_lib
* lib
= p
->lib();
340 check_input_args(input_args_map
, lib
->begin(), lib
->end());
344 gold_assert(p
->is_file());
345 unsigned int arg_serial
= p
->file().arg_serial();
348 gold_assert(arg_serial
<= input_args_map
.size());
349 gold_assert(input_args_map
[arg_serial
- 1] == 0);
350 input_args_map
[arg_serial
- 1] = &*p
;
356 // Determine whether an incremental link based on the existing output file
359 template<int size
, bool big_endian
>
361 Sized_incremental_binary
<size
, big_endian
>::do_check_inputs(
362 const Command_line
& cmdline
,
363 Incremental_inputs
* incremental_inputs
)
365 Incremental_inputs_reader
<size
, big_endian
>& inputs
= this->inputs_reader_
;
367 if (!this->has_incremental_info_
)
369 explain_no_incremental(_("no incremental data from previous build"));
373 if (inputs
.version() != INCREMENTAL_LINK_VERSION
)
375 explain_no_incremental(_("different version of incremental build data"));
379 if (incremental_inputs
->command_line() != inputs
.command_line())
381 explain_no_incremental(_("command line changed"));
385 // Walk the list of input files given on the command line, and build
386 // a direct map of argument serial numbers to the corresponding input
388 this->input_args_map_
.resize(cmdline
.number_of_input_files());
389 check_input_args(this->input_args_map_
, cmdline
.begin(), cmdline
.end());
391 // Walk the list of input files to check for conditions that prevent
392 // an incremental update link.
393 unsigned int count
= inputs
.input_file_count();
394 for (unsigned int i
= 0; i
< count
; i
++)
396 Input_entry_reader input_file
= inputs
.input_file(i
);
397 switch (input_file
.type())
399 case INCREMENTAL_INPUT_OBJECT
:
400 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
401 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
402 case INCREMENTAL_INPUT_ARCHIVE
:
403 // No special treatment necessary.
405 case INCREMENTAL_INPUT_SCRIPT
:
406 if (this->do_file_has_changed(i
))
408 explain_no_incremental(_("%s: script file changed"),
409 input_file
.filename());
421 // Return TRUE if input file N has changed since the last incremental link.
423 template<int size
, bool big_endian
>
425 Sized_incremental_binary
<size
, big_endian
>::do_file_has_changed(
426 unsigned int n
) const
428 Input_entry_reader input_file
= this->inputs_reader_
.input_file(n
);
429 Incremental_disposition disp
= INCREMENTAL_CHECK
;
430 const Input_argument
* input_argument
= this->get_input_argument(n
);
431 if (input_argument
!= NULL
)
432 disp
= input_argument
->file().options().incremental_disposition();
434 if (disp
!= INCREMENTAL_CHECK
)
435 return disp
== INCREMENTAL_CHANGED
;
437 const char* filename
= input_file
.filename();
438 Timespec old_mtime
= input_file
.get_mtime();
440 if (!get_mtime(filename
, &new_mtime
))
442 // If we can't open get the current modification time, assume it has
443 // changed. If the file doesn't exist, we'll issue an error when we
444 // try to open it later.
448 if (new_mtime
.seconds
> old_mtime
.seconds
)
450 if (new_mtime
.seconds
== old_mtime
.seconds
451 && new_mtime
.nanoseconds
> old_mtime
.nanoseconds
)
456 // Initialize the layout of the output file based on the existing
459 template<int size
, bool big_endian
>
461 Sized_incremental_binary
<size
, big_endian
>::do_init_layout(Layout
* layout
)
463 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
464 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
466 // Get views of the section headers and the section string table.
467 const off_t shoff
= this->elf_file_
.shoff();
468 const unsigned int shnum
= this->elf_file_
.shnum();
469 const unsigned int shstrndx
= this->elf_file_
.shstrndx();
470 Location
shdrs_location(shoff
, shnum
* shdr_size
);
471 Location
shstrndx_location(this->elf_file_
.section_contents(shstrndx
));
472 View shdrs_view
= this->view(shdrs_location
);
473 View shstrndx_view
= this->view(shstrndx_location
);
474 elfcpp::Elf_strtab
shstrtab(shstrndx_view
.data(),
475 shstrndx_location
.data_size
);
477 layout
->set_incremental_base(this);
479 // Initialize the layout.
480 this->section_map_
.resize(shnum
);
481 const unsigned char* pshdr
= shdrs_view
.data() + shdr_size
;
482 for (unsigned int i
= 1; i
< shnum
; i
++)
486 if (!shstrtab
.get_c_string(shdr
.get_sh_name(), &name
))
488 gold_debug(DEBUG_INCREMENTAL
,
489 "Output section: %2d %08lx %08lx %08lx %3d %s",
491 static_cast<long>(shdr
.get_sh_addr()),
492 static_cast<long>(shdr
.get_sh_offset()),
493 static_cast<long>(shdr
.get_sh_size()),
494 shdr
.get_sh_type(), name
? name
: "<null>");
495 this->section_map_
[i
] = layout
->init_fixed_output_section(name
, shdr
);
500 // Mark regions of the input file that must be kept unchanged.
502 template<int size
, bool big_endian
>
504 Sized_incremental_binary
<size
, big_endian
>::do_reserve_layout(
505 unsigned int input_file_index
)
507 Input_entry_reader input_file
=
508 this->inputs_reader_
.input_file(input_file_index
);
510 if (input_file
.type() == INCREMENTAL_INPUT_SHARED_LIBRARY
)
513 unsigned int shnum
= input_file
.get_input_section_count();
514 for (unsigned int i
= 0; i
< shnum
; i
++)
516 typename
Input_entry_reader::Input_section_info sect
=
517 input_file
.get_input_section(i
);
518 if (sect
.output_shndx
== 0 || sect
.sh_offset
== -1)
520 Output_section
* os
= this->section_map_
[sect
.output_shndx
];
521 gold_assert(os
!= NULL
);
522 os
->reserve(sect
.sh_offset
, sect
.sh_size
);
526 // Apply incremental relocations for symbols whose values have changed.
528 template<int size
, bool big_endian
>
530 Sized_incremental_binary
<size
, big_endian
>::do_apply_incremental_relocs(
531 const Symbol_table
* symtab
,
535 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
536 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Addend
;
537 Incremental_symtab_reader
<big_endian
> isymtab(this->symtab_reader());
538 Incremental_relocs_reader
<size
, big_endian
> irelocs(this->relocs_reader());
539 unsigned int nglobals
= isymtab
.symbol_count();
540 const unsigned int incr_reloc_size
= irelocs
.reloc_size
;
542 Relocate_info
<size
, big_endian
> relinfo
;
543 relinfo
.symtab
= symtab
;
544 relinfo
.layout
= layout
;
545 relinfo
.object
= NULL
;
546 relinfo
.reloc_shndx
= 0;
547 relinfo
.reloc_shdr
= NULL
;
548 relinfo
.data_shndx
= 0;
549 relinfo
.data_shdr
= NULL
;
551 Sized_target
<size
, big_endian
>* target
=
552 parameters
->sized_target
<size
, big_endian
>();
554 for (unsigned int i
= 0; i
< nglobals
; i
++)
556 const Symbol
* gsym
= this->global_symbol(i
);
558 // If the symbol is not referenced from any unchanged input files,
559 // we do not need to reapply any of its relocations.
563 // If the symbol is defined in an unchanged file, we do not need to
564 // reapply any of its relocations.
565 if (gsym
->source() == Symbol::FROM_OBJECT
566 && gsym
->object()->is_incremental())
569 gold_debug(DEBUG_INCREMENTAL
,
570 "Applying incremental relocations for global symbol %s [%d]",
573 // Follow the linked list of input symbol table entries for this symbol.
574 // We don't bother to figure out whether the symbol table entry belongs
575 // to a changed or unchanged file because it's easier just to apply all
576 // the relocations -- although we might scribble over an area that has
577 // been reallocated, we do this before copying any new data into the
579 unsigned int offset
= isymtab
.get_list_head(i
);
582 Incremental_global_symbol_reader
<big_endian
> sym_info
=
583 this->inputs_reader().global_symbol_reader_at_offset(offset
);
584 unsigned int r_base
= sym_info
.reloc_offset();
585 unsigned int r_count
= sym_info
.reloc_count();
587 // Apply each relocation for this symbol table entry.
588 for (unsigned int j
= 0; j
< r_count
;
589 ++j
, r_base
+= incr_reloc_size
)
591 unsigned int r_type
= irelocs
.get_r_type(r_base
);
592 unsigned int r_shndx
= irelocs
.get_r_shndx(r_base
);
593 Address r_offset
= irelocs
.get_r_offset(r_base
);
594 Addend r_addend
= irelocs
.get_r_addend(r_base
);
595 Output_section
* os
= this->output_section(r_shndx
);
596 Address address
= os
->address();
597 off_t section_offset
= os
->offset();
598 size_t view_size
= os
->data_size();
599 unsigned char* const view
= of
->get_output_view(section_offset
,
602 gold_debug(DEBUG_INCREMENTAL
,
603 " %08lx: %s + %d: type %d addend %ld",
604 (long)(section_offset
+ r_offset
),
610 target
->apply_relocation(&relinfo
, r_offset
, r_type
, r_addend
,
611 gsym
, view
, address
, view_size
);
613 // FIXME: Do something more efficient if write_output_view
614 // ever becomes more than a no-op.
615 of
->write_output_view(section_offset
, view_size
, view
);
617 offset
= sym_info
.next_offset();
622 // Get a view of the main symbol table and the symbol string table.
624 template<int size
, bool big_endian
>
626 Sized_incremental_binary
<size
, big_endian
>::get_symtab_view(
629 elfcpp::Elf_strtab
* strtab
)
631 unsigned int symtab_shndx
=
632 this->elf_file_
.find_section_by_type(elfcpp::SHT_SYMTAB
);
633 gold_assert(symtab_shndx
!= elfcpp::SHN_UNDEF
);
634 Location
symtab_location(this->elf_file_
.section_contents(symtab_shndx
));
635 *symtab_view
= this->view(symtab_location
);
636 *nsyms
= symtab_location
.data_size
/ elfcpp::Elf_sizes
<size
>::sym_size
;
638 unsigned int strtab_shndx
= this->elf_file_
.section_link(symtab_shndx
);
639 gold_assert(strtab_shndx
!= elfcpp::SHN_UNDEF
640 && strtab_shndx
< this->elf_file_
.shnum());
642 Location
strtab_location(this->elf_file_
.section_contents(strtab_shndx
));
643 View
strtab_view(this->view(strtab_location
));
644 *strtab
= elfcpp::Elf_strtab(strtab_view
.data(), strtab_location
.data_size
);
650 // Create a Sized_incremental_binary object of the specified size and
651 // endianness. Fails if the target architecture is not supported.
653 template<int size
, bool big_endian
>
655 make_sized_incremental_binary(Output_file
* file
,
656 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
658 Target
* target
= select_target(ehdr
.get_e_machine(), size
, big_endian
,
659 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
660 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
663 explain_no_incremental(_("unsupported ELF machine number %d"),
664 ehdr
.get_e_machine());
668 if (!parameters
->target_valid())
669 set_parameters_target(target
);
670 else if (target
!= ¶meters
->target())
671 gold_error(_("%s: incompatible target"), file
->filename());
673 return new Sized_incremental_binary
<size
, big_endian
>(file
, ehdr
, target
);
676 } // End of anonymous namespace.
678 // Create an Incremental_binary object for FILE. Returns NULL is this is not
679 // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
683 open_incremental_binary(Output_file
* file
)
685 off_t filesize
= file
->filesize();
686 int want
= elfcpp::Elf_recognizer::max_header_size
;
690 const unsigned char* p
= file
->get_input_view(0, want
);
691 if (!elfcpp::Elf_recognizer::is_elf_file(p
, want
))
693 explain_no_incremental(_("output is not an ELF file."));
698 bool big_endian
= false;
700 if (!elfcpp::Elf_recognizer::is_valid_header(p
, want
, &size
, &big_endian
,
703 explain_no_incremental(error
.c_str());
707 Incremental_binary
* result
= NULL
;
712 #ifdef HAVE_TARGET_32_BIG
713 result
= make_sized_incremental_binary
<32, true>(
714 file
, elfcpp::Ehdr
<32, true>(p
));
716 explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
721 #ifdef HAVE_TARGET_32_LITTLE
722 result
= make_sized_incremental_binary
<32, false>(
723 file
, elfcpp::Ehdr
<32, false>(p
));
725 explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
733 #ifdef HAVE_TARGET_64_BIG
734 result
= make_sized_incremental_binary
<64, true>(
735 file
, elfcpp::Ehdr
<64, true>(p
));
737 explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
742 #ifdef HAVE_TARGET_64_LITTLE
743 result
= make_sized_incremental_binary
<64, false>(
744 file
, elfcpp::Ehdr
<64, false>(p
));
746 explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
756 // Class Incremental_inputs.
758 // Add the command line to the string table, setting
759 // command_line_key_. In incremental builds, the command line is
760 // stored in .gnu_incremental_inputs so that the next linker run can
761 // check if the command line options didn't change.
764 Incremental_inputs::report_command_line(int argc
, const char* const* argv
)
766 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
767 // different path to the linker.
768 std::string
args("gold");
769 // Copied from collect_argv in main.cc.
770 for (int i
= 1; i
< argc
; ++i
)
772 // Adding/removing these options should not result in a full relink.
773 if (strcmp(argv
[i
], "--incremental") == 0
774 || strcmp(argv
[i
], "--incremental-full") == 0
775 || strcmp(argv
[i
], "--incremental-update") == 0
776 || strcmp(argv
[i
], "--incremental-changed") == 0
777 || strcmp(argv
[i
], "--incremental-unchanged") == 0
778 || strcmp(argv
[i
], "--incremental-unknown") == 0
779 || is_prefix_of("--debug=", argv
[i
]))
783 // Now append argv[i], but with all single-quotes escaped
784 const char* argpos
= argv
[i
];
787 const int len
= strcspn(argpos
, "'");
788 args
.append(argpos
, len
);
789 if (argpos
[len
] == '\0')
791 args
.append("'\"'\"'");
797 this->command_line_
= args
;
798 this->strtab_
->add(this->command_line_
.c_str(), false,
799 &this->command_line_key_
);
802 // Record the input archive file ARCHIVE. This is called by the
803 // Add_archive_symbols task before determining which archive members
804 // to include. We create the Incremental_archive_entry here and
805 // attach it to the Archive, but we do not add it to the list of
806 // input objects until report_archive_end is called.
809 Incremental_inputs::report_archive_begin(Library_base
* arch
,
810 unsigned int arg_serial
,
811 Script_info
* script_info
)
813 Stringpool::Key filename_key
;
814 Timespec mtime
= arch
->get_mtime();
816 // For a file loaded from a script, don't record its argument serial number.
817 if (script_info
!= NULL
)
820 this->strtab_
->add(arch
->filename().c_str(), false, &filename_key
);
821 Incremental_archive_entry
* entry
=
822 new Incremental_archive_entry(filename_key
, arg_serial
, mtime
);
823 arch
->set_incremental_info(entry
);
825 if (script_info
!= NULL
)
827 Incremental_script_entry
* script_entry
= script_info
->incremental_info();
828 gold_assert(script_entry
!= NULL
);
829 script_entry
->add_object(entry
);
833 // Visitor class for processing the unused global symbols in a library.
834 // An instance of this class is passed to the library's
835 // for_all_unused_symbols() iterator, which will call the visit()
836 // function for each global symbol defined in each unused library
837 // member. We add those symbol names to the incremental info for the
840 class Unused_symbol_visitor
: public Library_base::Symbol_visitor_base
843 Unused_symbol_visitor(Incremental_archive_entry
* entry
, Stringpool
* strtab
)
844 : entry_(entry
), strtab_(strtab
)
848 visit(const char* sym
)
850 Stringpool::Key symbol_key
;
851 this->strtab_
->add(sym
, true, &symbol_key
);
852 this->entry_
->add_unused_global_symbol(symbol_key
);
856 Incremental_archive_entry
* entry_
;
860 // Finish recording the input archive file ARCHIVE. This is called by the
861 // Add_archive_symbols task after determining which archive members
865 Incremental_inputs::report_archive_end(Library_base
* arch
)
867 Incremental_archive_entry
* entry
= arch
->incremental_info();
869 gold_assert(entry
!= NULL
);
870 this->inputs_
.push_back(entry
);
872 // Collect unused global symbols.
873 Unused_symbol_visitor
v(entry
, this->strtab_
);
874 arch
->for_all_unused_symbols(&v
);
877 // Record the input object file OBJ. If ARCH is not NULL, attach
878 // the object file to the archive. This is called by the
879 // Add_symbols task after finding out the type of the file.
882 Incremental_inputs::report_object(Object
* obj
, unsigned int arg_serial
,
883 Library_base
* arch
, Script_info
* script_info
)
885 Stringpool::Key filename_key
;
886 Timespec mtime
= obj
->get_mtime();
888 // For a file loaded from a script, don't record its argument serial number.
889 if (script_info
!= NULL
)
892 this->strtab_
->add(obj
->name().c_str(), false, &filename_key
);
893 Incremental_object_entry
* obj_entry
=
894 new Incremental_object_entry(filename_key
, obj
, arg_serial
, mtime
);
895 if (obj
->is_in_system_directory())
896 obj_entry
->set_is_in_system_directory();
897 this->inputs_
.push_back(obj_entry
);
901 Incremental_archive_entry
* arch_entry
= arch
->incremental_info();
902 gold_assert(arch_entry
!= NULL
);
903 arch_entry
->add_object(obj_entry
);
906 if (script_info
!= NULL
)
908 Incremental_script_entry
* script_entry
= script_info
->incremental_info();
909 gold_assert(script_entry
!= NULL
);
910 script_entry
->add_object(obj_entry
);
913 this->current_object_
= obj
;
914 this->current_object_entry_
= obj_entry
;
917 // Record the input object file OBJ. If ARCH is not NULL, attach
918 // the object file to the archive. This is called by the
919 // Add_symbols task after finding out the type of the file.
922 Incremental_inputs::report_input_section(Object
* obj
, unsigned int shndx
,
923 const char* name
, off_t sh_size
)
925 Stringpool::Key key
= 0;
928 this->strtab_
->add(name
, true, &key
);
930 gold_assert(obj
== this->current_object_
);
931 this->current_object_entry_
->add_input_section(shndx
, key
, sh_size
);
934 // Record that the input argument INPUT is a script SCRIPT. This is
935 // called by read_script after parsing the script and reading the list
936 // of inputs added by this script.
939 Incremental_inputs::report_script(Script_info
* script
,
940 unsigned int arg_serial
,
943 Stringpool::Key filename_key
;
945 this->strtab_
->add(script
->filename().c_str(), false, &filename_key
);
946 Incremental_script_entry
* entry
=
947 new Incremental_script_entry(filename_key
, arg_serial
, script
, mtime
);
948 this->inputs_
.push_back(entry
);
949 script
->set_incremental_info(entry
);
952 // Finalize the incremental link information. Called from
956 Incremental_inputs::finalize()
958 // Finalize the string table.
959 this->strtab_
->set_string_offsets();
962 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
965 Incremental_inputs::create_data_sections(Symbol_table
* symtab
)
967 switch (parameters
->size_and_endianness())
969 #ifdef HAVE_TARGET_32_LITTLE
970 case Parameters::TARGET_32_LITTLE
:
971 this->inputs_section_
=
972 new Output_section_incremental_inputs
<32, false>(this, symtab
);
975 #ifdef HAVE_TARGET_32_BIG
976 case Parameters::TARGET_32_BIG
:
977 this->inputs_section_
=
978 new Output_section_incremental_inputs
<32, true>(this, symtab
);
981 #ifdef HAVE_TARGET_64_LITTLE
982 case Parameters::TARGET_64_LITTLE
:
983 this->inputs_section_
=
984 new Output_section_incremental_inputs
<64, false>(this, symtab
);
987 #ifdef HAVE_TARGET_64_BIG
988 case Parameters::TARGET_64_BIG
:
989 this->inputs_section_
=
990 new Output_section_incremental_inputs
<64, true>(this, symtab
);
996 this->symtab_section_
= new Output_data_space(4, "** incremental_symtab");
997 this->relocs_section_
= new Output_data_space(4, "** incremental_relocs");
998 this->got_plt_section_
= new Output_data_space(4, "** incremental_got_plt");
1001 // Return the sh_entsize value for the .gnu_incremental_relocs section.
1003 Incremental_inputs::relocs_entsize() const
1005 return 8 + 2 * parameters
->target().get_size() / 8;
1008 // Class Output_section_incremental_inputs.
1010 // Finalize the offsets for each input section and supplemental info block,
1011 // and set the final data size of the incremental output sections.
1013 template<int size
, bool big_endian
>
1015 Output_section_incremental_inputs
<size
, big_endian
>::set_final_data_size()
1017 const Incremental_inputs
* inputs
= this->inputs_
;
1018 const unsigned int sizeof_addr
= size
/ 8;
1019 const unsigned int rel_size
= 8 + 2 * sizeof_addr
;
1021 // Offset of each input entry.
1022 unsigned int input_offset
= this->header_size
;
1024 // Offset of each supplemental info block.
1025 unsigned int info_offset
= this->header_size
;
1026 info_offset
+= this->input_entry_size
* inputs
->input_file_count();
1028 // Count each input file and its supplemental information block.
1029 for (Incremental_inputs::Input_list::const_iterator p
=
1030 inputs
->input_files().begin();
1031 p
!= inputs
->input_files().end();
1034 // Set the offset of the input file entry.
1035 (*p
)->set_offset(input_offset
);
1036 input_offset
+= this->input_entry_size
;
1038 // Set the offset of the supplemental info block.
1039 switch ((*p
)->type())
1041 case INCREMENTAL_INPUT_SCRIPT
:
1043 Incremental_script_entry
*entry
= (*p
)->script_entry();
1044 gold_assert(entry
!= NULL
);
1045 (*p
)->set_info_offset(info_offset
);
1049 info_offset
+= (entry
->get_object_count() * 4);
1052 case INCREMENTAL_INPUT_OBJECT
:
1053 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
1055 Incremental_object_entry
* entry
= (*p
)->object_entry();
1056 gold_assert(entry
!= NULL
);
1057 (*p
)->set_info_offset(info_offset
);
1058 // Input section count + global symbol count.
1060 // Each input section.
1061 info_offset
+= (entry
->get_input_section_count()
1062 * (8 + 2 * sizeof_addr
));
1063 // Each global symbol.
1064 const Object::Symbols
* syms
= entry
->object()->get_global_symbols();
1065 info_offset
+= syms
->size() * 20;
1068 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
1070 Incremental_object_entry
* entry
= (*p
)->object_entry();
1071 gold_assert(entry
!= NULL
);
1072 (*p
)->set_info_offset(info_offset
);
1073 // Global symbol count.
1075 // Each global symbol.
1076 const Object::Symbols
* syms
= entry
->object()->get_global_symbols();
1077 gold_assert(syms
!= NULL
);
1078 unsigned int nsyms
= syms
->size();
1079 unsigned int nsyms_out
= 0;
1080 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1082 const Symbol
* sym
= (*syms
)[i
];
1085 if (sym
->is_forwarder())
1086 sym
= this->symtab_
->resolve_forwards(sym
);
1087 if (sym
->symtab_index() != -1U)
1090 info_offset
+= nsyms_out
* 4;
1093 case INCREMENTAL_INPUT_ARCHIVE
:
1095 Incremental_archive_entry
* entry
= (*p
)->archive_entry();
1096 gold_assert(entry
!= NULL
);
1097 (*p
)->set_info_offset(info_offset
);
1098 // Member count + unused global symbol count.
1101 info_offset
+= (entry
->get_member_count() * 4);
1102 // Each global symbol.
1103 info_offset
+= (entry
->get_unused_global_symbol_count() * 4);
1111 this->set_data_size(info_offset
);
1113 // Set the size of the .gnu_incremental_symtab section.
1114 inputs
->symtab_section()->set_current_data_size(this->symtab_
->output_count()
1115 * sizeof(unsigned int));
1117 // Set the size of the .gnu_incremental_relocs section.
1118 inputs
->relocs_section()->set_current_data_size(inputs
->get_reloc_count()
1121 // Set the size of the .gnu_incremental_got_plt section.
1122 Sized_target
<size
, big_endian
>* target
=
1123 parameters
->sized_target
<size
, big_endian
>();
1124 unsigned int got_count
= target
->got_entry_count();
1125 unsigned int plt_count
= target
->plt_entry_count();
1126 unsigned int got_plt_size
= 8; // GOT entry count, PLT entry count.
1127 got_plt_size
= (got_plt_size
+ got_count
+ 3) & ~3; // GOT type array.
1128 got_plt_size
+= got_count
* 4 + plt_count
* 4; // GOT array, PLT array.
1129 inputs
->got_plt_section()->set_current_data_size(got_plt_size
);
1132 // Write the contents of the .gnu_incremental_inputs and
1133 // .gnu_incremental_symtab sections.
1135 template<int size
, bool big_endian
>
1137 Output_section_incremental_inputs
<size
, big_endian
>::do_write(Output_file
* of
)
1139 const Incremental_inputs
* inputs
= this->inputs_
;
1140 Stringpool
* strtab
= inputs
->get_stringpool();
1142 // Get a view into the .gnu_incremental_inputs section.
1143 const off_t off
= this->offset();
1144 const off_t oview_size
= this->data_size();
1145 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1146 unsigned char* pov
= oview
;
1148 // Get a view into the .gnu_incremental_symtab section.
1149 const off_t symtab_off
= inputs
->symtab_section()->offset();
1150 const off_t symtab_size
= inputs
->symtab_section()->data_size();
1151 unsigned char* const symtab_view
= of
->get_output_view(symtab_off
,
1154 // Allocate an array of linked list heads for the .gnu_incremental_symtab
1155 // section. Each element corresponds to a global symbol in the output
1156 // symbol table, and points to the head of the linked list that threads
1157 // through the object file input entries. The value of each element
1158 // is the section-relative offset to a global symbol entry in a
1159 // supplemental information block.
1160 unsigned int global_sym_count
= this->symtab_
->output_count();
1161 unsigned int* global_syms
= new unsigned int[global_sym_count
];
1162 memset(global_syms
, 0, global_sym_count
* sizeof(unsigned int));
1164 // Write the section header.
1165 Stringpool::Key command_line_key
= inputs
->command_line_key();
1166 pov
= this->write_header(pov
, inputs
->input_file_count(),
1167 strtab
->get_offset_from_key(command_line_key
));
1169 // Write the list of input files.
1170 pov
= this->write_input_files(oview
, pov
, strtab
);
1172 // Write the supplemental information blocks for each input file.
1173 pov
= this->write_info_blocks(oview
, pov
, strtab
, global_syms
,
1176 gold_assert(pov
- oview
== oview_size
);
1178 // Write the .gnu_incremental_symtab section.
1179 gold_assert(global_sym_count
* 4 == symtab_size
);
1180 this->write_symtab(symtab_view
, global_syms
, global_sym_count
);
1182 delete[] global_syms
;
1184 // Write the .gnu_incremental_got_plt section.
1185 const off_t got_plt_off
= inputs
->got_plt_section()->offset();
1186 const off_t got_plt_size
= inputs
->got_plt_section()->data_size();
1187 unsigned char* const got_plt_view
= of
->get_output_view(got_plt_off
,
1189 this->write_got_plt(got_plt_view
, got_plt_size
);
1191 of
->write_output_view(off
, oview_size
, oview
);
1192 of
->write_output_view(symtab_off
, symtab_size
, symtab_view
);
1193 of
->write_output_view(got_plt_off
, got_plt_size
, got_plt_view
);
1196 // Write the section header: version, input file count, offset of command line
1197 // in the string table, and 4 bytes of padding.
1199 template<int size
, bool big_endian
>
1201 Output_section_incremental_inputs
<size
, big_endian
>::write_header(
1203 unsigned int input_file_count
,
1204 section_offset_type command_line_offset
)
1206 Swap32::writeval(pov
, INCREMENTAL_LINK_VERSION
);
1207 Swap32::writeval(pov
+ 4, input_file_count
);
1208 Swap32::writeval(pov
+ 8, command_line_offset
);
1209 Swap32::writeval(pov
+ 12, 0);
1210 return pov
+ this->header_size
;
1213 // Write the input file entries.
1215 template<int size
, bool big_endian
>
1217 Output_section_incremental_inputs
<size
, big_endian
>::write_input_files(
1218 unsigned char* oview
,
1222 const Incremental_inputs
* inputs
= this->inputs_
;
1224 for (Incremental_inputs::Input_list::const_iterator p
=
1225 inputs
->input_files().begin();
1226 p
!= inputs
->input_files().end();
1229 gold_assert(static_cast<unsigned int>(pov
- oview
) == (*p
)->get_offset());
1230 section_offset_type filename_offset
=
1231 strtab
->get_offset_from_key((*p
)->get_filename_key());
1232 const Timespec
& mtime
= (*p
)->get_mtime();
1233 unsigned int flags
= (*p
)->type();
1234 if ((*p
)->is_in_system_directory())
1235 flags
|= INCREMENTAL_INPUT_IN_SYSTEM_DIR
;
1236 Swap32::writeval(pov
, filename_offset
);
1237 Swap32::writeval(pov
+ 4, (*p
)->get_info_offset());
1238 Swap64::writeval(pov
+ 8, mtime
.seconds
);
1239 Swap32::writeval(pov
+ 16, mtime
.nanoseconds
);
1240 Swap16::writeval(pov
+ 20, flags
);
1241 Swap16::writeval(pov
+ 22, (*p
)->arg_serial());
1242 pov
+= this->input_entry_size
;
1247 // Write the supplemental information blocks.
1249 template<int size
, bool big_endian
>
1251 Output_section_incremental_inputs
<size
, big_endian
>::write_info_blocks(
1252 unsigned char* oview
,
1255 unsigned int* global_syms
,
1256 unsigned int global_sym_count
)
1258 const Incremental_inputs
* inputs
= this->inputs_
;
1259 unsigned int first_global_index
= this->symtab_
->first_global_index();
1261 for (Incremental_inputs::Input_list::const_iterator p
=
1262 inputs
->input_files().begin();
1263 p
!= inputs
->input_files().end();
1266 switch ((*p
)->type())
1268 case INCREMENTAL_INPUT_SCRIPT
:
1270 gold_assert(static_cast<unsigned int>(pov
- oview
)
1271 == (*p
)->get_info_offset());
1272 Incremental_script_entry
* entry
= (*p
)->script_entry();
1273 gold_assert(entry
!= NULL
);
1275 // Write the object count.
1276 unsigned int nobjects
= entry
->get_object_count();
1277 Swap32::writeval(pov
, nobjects
);
1280 // For each object, write the offset to its input file entry.
1281 for (unsigned int i
= 0; i
< nobjects
; ++i
)
1283 Incremental_input_entry
* obj
= entry
->get_object(i
);
1284 Swap32::writeval(pov
, obj
->get_offset());
1290 case INCREMENTAL_INPUT_OBJECT
:
1291 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
1293 gold_assert(static_cast<unsigned int>(pov
- oview
)
1294 == (*p
)->get_info_offset());
1295 Incremental_object_entry
* entry
= (*p
)->object_entry();
1296 gold_assert(entry
!= NULL
);
1297 const Object
* obj
= entry
->object();
1298 const Object::Symbols
* syms
= obj
->get_global_symbols();
1299 // Write the input section count and global symbol count.
1300 unsigned int nsections
= entry
->get_input_section_count();
1301 unsigned int nsyms
= syms
->size();
1302 Swap32::writeval(pov
, nsections
);
1303 Swap32::writeval(pov
+ 4, nsyms
);
1306 // Build a temporary array to map input section indexes
1307 // from the original object file index to the index in the
1308 // incremental info table.
1309 unsigned int* index_map
= new unsigned int[obj
->shnum()];
1310 memset(index_map
, 0, obj
->shnum() * sizeof(unsigned int));
1312 // For each input section, write the name, output section index,
1313 // offset within output section, and input section size.
1314 for (unsigned int i
= 0; i
< nsections
; i
++)
1316 unsigned int shndx
= entry
->get_input_section_index(i
);
1317 index_map
[shndx
] = i
+ 1;
1318 Stringpool::Key key
= entry
->get_input_section_name_key(i
);
1319 off_t name_offset
= 0;
1321 name_offset
= strtab
->get_offset_from_key(key
);
1323 off_t out_offset
= 0;
1325 Output_section
* os
= obj
->output_section(shndx
);
1328 out_shndx
= os
->out_shndx();
1329 out_offset
= obj
->output_section_offset(shndx
);
1330 sh_size
= entry
->get_input_section_size(i
);
1332 Swap32::writeval(pov
, name_offset
);
1333 Swap32::writeval(pov
+ 4, out_shndx
);
1334 Swap::writeval(pov
+ 8, out_offset
);
1335 Swap::writeval(pov
+ 8 + sizeof_addr
, sh_size
);
1336 pov
+= 8 + 2 * sizeof_addr
;
1339 // For each global symbol, write its associated relocations,
1340 // add it to the linked list of globals, then write the
1341 // supplemental information: global symbol table index,
1342 // input section index, linked list chain pointer, relocation
1343 // count, and offset to the relocations.
1344 for (unsigned int i
= 0; i
< nsyms
; i
++)
1346 const Symbol
* sym
= (*syms
)[i
];
1347 if (sym
->is_forwarder())
1348 sym
= this->symtab_
->resolve_forwards(sym
);
1349 unsigned int shndx
= 0;
1350 if (sym
->source() == Symbol::FROM_OBJECT
1351 && sym
->object() == obj
1352 && sym
->is_defined())
1355 unsigned int orig_shndx
= sym
->shndx(&is_ordinary
);
1357 shndx
= index_map
[orig_shndx
];
1359 unsigned int symtab_index
= sym
->symtab_index();
1360 unsigned int chain
= 0;
1361 unsigned int first_reloc
= 0;
1362 unsigned int nrelocs
= obj
->get_incremental_reloc_count(i
);
1365 gold_assert(symtab_index
!= -1U
1366 && (symtab_index
- first_global_index
1367 < global_sym_count
));
1368 first_reloc
= obj
->get_incremental_reloc_base(i
);
1369 chain
= global_syms
[symtab_index
- first_global_index
];
1370 global_syms
[symtab_index
- first_global_index
] =
1373 Swap32::writeval(pov
, symtab_index
);
1374 Swap32::writeval(pov
+ 4, shndx
);
1375 Swap32::writeval(pov
+ 8, chain
);
1376 Swap32::writeval(pov
+ 12, nrelocs
);
1377 Swap32::writeval(pov
+ 16, first_reloc
* 3 * sizeof_addr
);
1385 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
1387 gold_assert(static_cast<unsigned int>(pov
- oview
)
1388 == (*p
)->get_info_offset());
1389 Incremental_object_entry
* entry
= (*p
)->object_entry();
1390 gold_assert(entry
!= NULL
);
1391 const Object
* obj
= entry
->object();
1392 const Object::Symbols
* syms
= obj
->get_global_symbols();
1394 // Skip the global symbol count for now.
1395 unsigned char* orig_pov
= pov
;
1398 // For each global symbol, write the global symbol table index.
1399 unsigned int nsyms
= syms
->size();
1400 unsigned int nsyms_out
= 0;
1401 for (unsigned int i
= 0; i
< nsyms
; i
++)
1403 const Symbol
* sym
= (*syms
)[i
];
1406 if (sym
->is_forwarder())
1407 sym
= this->symtab_
->resolve_forwards(sym
);
1408 if (sym
->symtab_index() == -1U)
1410 unsigned int def_flag
= 0;
1411 if (sym
->source() == Symbol::FROM_OBJECT
1412 && sym
->object() == obj
1413 && sym
->is_defined())
1414 def_flag
= 1U << 31;
1415 Swap32::writeval(pov
, sym
->symtab_index() | def_flag
);
1420 // Now write the global symbol count.
1421 Swap32::writeval(orig_pov
, nsyms_out
);
1425 case INCREMENTAL_INPUT_ARCHIVE
:
1427 gold_assert(static_cast<unsigned int>(pov
- oview
)
1428 == (*p
)->get_info_offset());
1429 Incremental_archive_entry
* entry
= (*p
)->archive_entry();
1430 gold_assert(entry
!= NULL
);
1432 // Write the member count and unused global symbol count.
1433 unsigned int nmembers
= entry
->get_member_count();
1434 unsigned int nsyms
= entry
->get_unused_global_symbol_count();
1435 Swap32::writeval(pov
, nmembers
);
1436 Swap32::writeval(pov
+ 4, nsyms
);
1439 // For each member, write the offset to its input file entry.
1440 for (unsigned int i
= 0; i
< nmembers
; ++i
)
1442 Incremental_object_entry
* member
= entry
->get_member(i
);
1443 Swap32::writeval(pov
, member
->get_offset());
1447 // For each global symbol, write the name offset.
1448 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1450 Stringpool::Key key
= entry
->get_unused_global_symbol(i
);
1451 Swap32::writeval(pov
, strtab
->get_offset_from_key(key
));
1464 // Write the contents of the .gnu_incremental_symtab section.
1466 template<int size
, bool big_endian
>
1468 Output_section_incremental_inputs
<size
, big_endian
>::write_symtab(
1470 unsigned int* global_syms
,
1471 unsigned int global_sym_count
)
1473 for (unsigned int i
= 0; i
< global_sym_count
; ++i
)
1475 Swap32::writeval(pov
, global_syms
[i
]);
1480 // This struct holds the view information needed to write the
1481 // .gnu_incremental_got_plt section.
1483 struct Got_plt_view_info
1485 // Start of the GOT type array in the output view.
1486 unsigned char* got_type_p
;
1487 // Start of the GOT descriptor array in the output view.
1488 unsigned char* got_desc_p
;
1489 // Start of the PLT descriptor array in the output view.
1490 unsigned char* plt_desc_p
;
1491 // Number of GOT entries.
1492 unsigned int got_count
;
1493 // Number of PLT entries.
1494 unsigned int plt_count
;
1495 // Offset of the first non-reserved PLT entry (this is a target-dependent value).
1496 unsigned int first_plt_entry_offset
;
1497 // Size of a PLT entry (this is a target-dependent value).
1498 unsigned int plt_entry_size
;
1499 // Value to write in the GOT descriptor array. For global symbols,
1500 // this is the global symbol table index; for local symbols, it is
1501 // the offset of the input file entry in the .gnu_incremental_inputs
1503 unsigned int got_descriptor
;
1506 // Functor class for processing a GOT offset list for local symbols.
1507 // Writes the GOT type and symbol index into the GOT type and descriptor
1508 // arrays in the output section.
1510 template<int size
, bool big_endian
>
1511 class Local_got_offset_visitor
: public Got_offset_list::Visitor
1514 Local_got_offset_visitor(struct Got_plt_view_info
& info
)
1519 visit(unsigned int got_type
, unsigned int got_offset
)
1521 unsigned int got_index
= got_offset
/ this->got_entry_size_
;
1522 gold_assert(got_index
< this->info_
.got_count
);
1523 // We can only handle GOT entry types in the range 0..0x7e
1524 // because we use a byte array to store them, and we use the
1525 // high bit to flag a local symbol.
1526 gold_assert(got_type
< 0x7f);
1527 this->info_
.got_type_p
[got_index
] = got_type
| 0x80;
1528 unsigned char* pov
= this->info_
.got_desc_p
+ got_index
* 4;
1529 elfcpp::Swap
<32, big_endian
>::writeval(pov
, this->info_
.got_descriptor
);
1533 static const unsigned int got_entry_size_
= size
/ 8;
1534 struct Got_plt_view_info
& info_
;
1537 // Functor class for processing a GOT offset list. Writes the GOT type
1538 // and symbol index into the GOT type and descriptor arrays in the output
1541 template<int size
, bool big_endian
>
1542 class Global_got_offset_visitor
: public Got_offset_list::Visitor
1545 Global_got_offset_visitor(struct Got_plt_view_info
& info
)
1550 visit(unsigned int got_type
, unsigned int got_offset
)
1552 unsigned int got_index
= got_offset
/ this->got_entry_size_
;
1553 gold_assert(got_index
< this->info_
.got_count
);
1554 // We can only handle GOT entry types in the range 0..0x7e
1555 // because we use a byte array to store them, and we use the
1556 // high bit to flag a local symbol.
1557 gold_assert(got_type
< 0x7f);
1558 this->info_
.got_type_p
[got_index
] = got_type
;
1559 unsigned char* pov
= this->info_
.got_desc_p
+ got_index
* 4;
1560 elfcpp::Swap
<32, big_endian
>::writeval(pov
, this->info_
.got_descriptor
);
1564 static const unsigned int got_entry_size_
= size
/ 8;
1565 struct Got_plt_view_info
& info_
;
1568 // Functor class for processing the global symbol table. Processes the
1569 // GOT offset list for the symbol, and writes the symbol table index
1570 // into the PLT descriptor array in the output section.
1572 template<int size
, bool big_endian
>
1573 class Global_symbol_visitor_got_plt
1576 Global_symbol_visitor_got_plt(struct Got_plt_view_info
& info
)
1581 operator()(const Sized_symbol
<size
>* sym
)
1583 typedef Global_got_offset_visitor
<size
, big_endian
> Got_visitor
;
1584 const Got_offset_list
* got_offsets
= sym
->got_offset_list();
1585 if (got_offsets
!= NULL
)
1587 this->info_
.got_descriptor
= sym
->symtab_index();
1588 Got_visitor
v(this->info_
);
1589 got_offsets
->for_all_got_offsets(&v
);
1591 if (sym
->has_plt_offset())
1593 unsigned int plt_index
=
1594 ((sym
->plt_offset() - this->info_
.first_plt_entry_offset
)
1595 / this->info_
.plt_entry_size
);
1596 gold_assert(plt_index
< this->info_
.plt_count
);
1597 unsigned char* pov
= this->info_
.plt_desc_p
+ plt_index
* 4;
1598 elfcpp::Swap
<32, big_endian
>::writeval(pov
, sym
->symtab_index());
1603 struct Got_plt_view_info
& info_
;
1606 // Write the contents of the .gnu_incremental_got_plt section.
1608 template<int size
, bool big_endian
>
1610 Output_section_incremental_inputs
<size
, big_endian
>::write_got_plt(
1614 Sized_target
<size
, big_endian
>* target
=
1615 parameters
->sized_target
<size
, big_endian
>();
1617 // Set up the view information for the functors.
1618 struct Got_plt_view_info view_info
;
1619 view_info
.got_count
= target
->got_entry_count();
1620 view_info
.plt_count
= target
->plt_entry_count();
1621 view_info
.first_plt_entry_offset
= target
->first_plt_entry_offset();
1622 view_info
.plt_entry_size
= target
->plt_entry_size();
1623 view_info
.got_type_p
= pov
+ 8;
1624 view_info
.got_desc_p
= (view_info
.got_type_p
1625 + ((view_info
.got_count
+ 3) & ~3));
1626 view_info
.plt_desc_p
= view_info
.got_desc_p
+ view_info
.got_count
* 4;
1628 gold_assert(pov
+ view_size
==
1629 view_info
.plt_desc_p
+ view_info
.plt_count
* 4);
1631 // Write the section header.
1632 Swap32::writeval(pov
, view_info
.got_count
);
1633 Swap32::writeval(pov
+ 4, view_info
.plt_count
);
1635 // Initialize the GOT type array to 0xff (reserved).
1636 memset(view_info
.got_type_p
, 0xff, view_info
.got_count
);
1638 // Write the incremental GOT descriptors for local symbols.
1639 typedef Local_got_offset_visitor
<size
, big_endian
> Got_visitor
;
1640 for (Incremental_inputs::Input_list::const_iterator p
=
1641 this->inputs_
->input_files().begin();
1642 p
!= this->inputs_
->input_files().end();
1645 if ((*p
)->type() != INCREMENTAL_INPUT_OBJECT
1646 && (*p
)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
)
1648 Incremental_object_entry
* entry
= (*p
)->object_entry();
1649 gold_assert(entry
!= NULL
);
1650 const Object
* obj
= entry
->object();
1651 gold_assert(obj
!= NULL
);
1652 view_info
.got_descriptor
= (*p
)->get_offset();
1653 Got_visitor
v(view_info
);
1654 obj
->for_all_local_got_entries(&v
);
1657 // Write the incremental GOT and PLT descriptors for global symbols.
1658 typedef Global_symbol_visitor_got_plt
<size
, big_endian
> Symbol_visitor
;
1659 symtab_
->for_all_symbols
<size
, Symbol_visitor
>(Symbol_visitor(view_info
));
1662 // Class Sized_incr_relobj. Most of these methods are not used for
1663 // Incremental objects, but are required to be implemented by the
1664 // base class Object.
1666 template<int size
, bool big_endian
>
1667 Sized_incr_relobj
<size
, big_endian
>::Sized_incr_relobj(
1668 const std::string
& name
,
1669 Sized_incremental_binary
<size
, big_endian
>* ibase
,
1670 unsigned int input_file_index
)
1671 : Sized_relobj_base
<size
, big_endian
>(name
, NULL
), ibase_(ibase
),
1672 input_file_index_(input_file_index
),
1673 input_reader_(ibase
->inputs_reader().input_file(input_file_index
)),
1674 symbols_(), section_offsets_(), incr_reloc_offset_(-1U),
1675 incr_reloc_count_(0), incr_reloc_output_index_(0), incr_relocs_(NULL
)
1677 if (this->input_reader_
.is_in_system_directory())
1678 this->set_is_in_system_directory();
1679 const unsigned int shnum
= this->input_reader_
.get_input_section_count() + 1;
1680 this->set_shnum(shnum
);
1683 // Read the symbols.
1685 template<int size
, bool big_endian
>
1687 Sized_incr_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
1692 // Lay out the input sections.
1694 template<int size
, bool big_endian
>
1696 Sized_incr_relobj
<size
, big_endian
>::do_layout(
1701 const unsigned int shnum
= this->shnum();
1702 Incremental_inputs
* incremental_inputs
= layout
->incremental_inputs();
1703 gold_assert(incremental_inputs
!= NULL
);
1704 Output_sections
& out_sections(this->output_sections());
1705 out_sections
.resize(shnum
);
1706 this->section_offsets_
.resize(shnum
);
1707 for (unsigned int i
= 1; i
< shnum
; i
++)
1709 typename
Input_entry_reader::Input_section_info sect
=
1710 this->input_reader_
.get_input_section(i
- 1);
1711 // Add the section to the incremental inputs layout.
1712 incremental_inputs
->report_input_section(this, i
, sect
.name
,
1714 if (sect
.output_shndx
== 0 || sect
.sh_offset
== -1)
1716 Output_section
* os
= this->ibase_
->output_section(sect
.output_shndx
);
1717 gold_assert(os
!= NULL
);
1718 out_sections
[i
] = os
;
1719 this->section_offsets_
[i
] = static_cast<Address
>(sect
.sh_offset
);
1723 // Layout sections whose layout was deferred while waiting for
1724 // input files from a plugin.
1725 template<int size
, bool big_endian
>
1727 Sized_incr_relobj
<size
, big_endian
>::do_layout_deferred_sections(Layout
*)
1731 // Add the symbols to the symbol table.
1733 template<int size
, bool big_endian
>
1735 Sized_incr_relobj
<size
, big_endian
>::do_add_symbols(
1736 Symbol_table
* symtab
,
1740 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1741 unsigned char symbuf
[sym_size
];
1742 elfcpp::Sym
<size
, big_endian
> sym(symbuf
);
1743 elfcpp::Sym_write
<size
, big_endian
> osym(symbuf
);
1745 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Elf_size_type
;
1747 unsigned int nsyms
= this->input_reader_
.get_global_symbol_count();
1748 this->symbols_
.resize(nsyms
);
1750 Incremental_binary::View
symtab_view(NULL
);
1751 unsigned int symtab_count
;
1752 elfcpp::Elf_strtab
strtab(NULL
, 0);
1753 this->ibase_
->get_symtab_view(&symtab_view
, &symtab_count
, &strtab
);
1755 Incremental_symtab_reader
<big_endian
> isymtab(this->ibase_
->symtab_reader());
1756 unsigned int isym_count
= isymtab
.symbol_count();
1757 unsigned int first_global
= symtab_count
- isym_count
;
1759 unsigned const char* sym_p
;
1760 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1762 Incremental_global_symbol_reader
<big_endian
> info
=
1763 this->input_reader_
.get_global_symbol_reader(i
);
1764 unsigned int output_symndx
= info
.output_symndx();
1765 sym_p
= symtab_view
.data() + output_symndx
* sym_size
;
1766 elfcpp::Sym
<size
, big_endian
> gsym(sym_p
);
1768 if (!strtab
.get_c_string(gsym
.get_st_name(), &name
))
1771 typename
elfcpp::Elf_types
<size
>::Elf_Addr v
= gsym
.get_st_value();
1772 unsigned int shndx
= gsym
.get_st_shndx();
1773 elfcpp::STB st_bind
= gsym
.get_st_bind();
1774 elfcpp::STT st_type
= gsym
.get_st_type();
1776 // Local hidden symbols start out as globals, but get converted to
1777 // to local during output.
1778 if (st_bind
== elfcpp::STB_LOCAL
)
1779 st_bind
= elfcpp::STB_GLOBAL
;
1781 unsigned int input_shndx
= info
.shndx();
1782 if (input_shndx
== 0)
1784 shndx
= elfcpp::SHN_UNDEF
;
1787 else if (shndx
!= elfcpp::SHN_ABS
)
1789 // Find the input section and calculate the section-relative value.
1790 gold_assert(shndx
!= elfcpp::SHN_UNDEF
);
1791 Output_section
* os
= this->ibase_
->output_section(shndx
);
1792 gold_assert(os
!= NULL
&& os
->has_fixed_layout());
1793 typename
Input_entry_reader::Input_section_info sect
=
1794 this->input_reader_
.get_input_section(input_shndx
- 1);
1795 gold_assert(sect
.output_shndx
== shndx
);
1796 if (st_type
!= elfcpp::STT_TLS
)
1798 v
-= sect
.sh_offset
;
1799 shndx
= input_shndx
;
1802 osym
.put_st_name(0);
1803 osym
.put_st_value(v
);
1804 osym
.put_st_size(gsym
.get_st_size());
1805 osym
.put_st_info(st_bind
, st_type
);
1806 osym
.put_st_other(gsym
.get_st_other());
1807 osym
.put_st_shndx(shndx
);
1810 symtab
->add_from_incrobj(this, name
, NULL
, &sym
);
1811 this->ibase_
->add_global_symbol(output_symndx
- first_global
,
1816 // Return TRUE if we should include this object from an archive library.
1818 template<int size
, bool big_endian
>
1819 Archive::Should_include
1820 Sized_incr_relobj
<size
, big_endian
>::do_should_include_member(
1829 // Iterate over global symbols, calling a visitor class V for each.
1831 template<int size
, bool big_endian
>
1833 Sized_incr_relobj
<size
, big_endian
>::do_for_all_global_symbols(
1835 Library_base::Symbol_visitor_base
*)
1837 // This routine is not used for incremental objects.
1840 // Iterate over local symbols, calling a visitor class V for each GOT offset
1841 // associated with a local symbol.
1843 template<int size
, bool big_endian
>
1845 Sized_incr_relobj
<size
, big_endian
>::do_for_all_local_got_entries(
1846 Got_offset_list::Visitor
*) const
1848 // FIXME: Implement Sized_incr_relobj::do_for_all_local_got_entries.
1851 // Get the size of a section.
1853 template<int size
, bool big_endian
>
1855 Sized_incr_relobj
<size
, big_endian
>::do_section_size(unsigned int)
1860 // Get the name of a section.
1862 template<int size
, bool big_endian
>
1864 Sized_incr_relobj
<size
, big_endian
>::do_section_name(unsigned int)
1869 // Return a view of the contents of a section.
1871 template<int size
, bool big_endian
>
1873 Sized_incr_relobj
<size
, big_endian
>::do_section_contents(unsigned int)
1878 // Return section flags.
1880 template<int size
, bool big_endian
>
1882 Sized_incr_relobj
<size
, big_endian
>::do_section_flags(unsigned int)
1887 // Return section entsize.
1889 template<int size
, bool big_endian
>
1891 Sized_incr_relobj
<size
, big_endian
>::do_section_entsize(unsigned int)
1896 // Return section address.
1898 template<int size
, bool big_endian
>
1900 Sized_incr_relobj
<size
, big_endian
>::do_section_address(unsigned int)
1905 // Return section type.
1907 template<int size
, bool big_endian
>
1909 Sized_incr_relobj
<size
, big_endian
>::do_section_type(unsigned int)
1914 // Return the section link field.
1916 template<int size
, bool big_endian
>
1918 Sized_incr_relobj
<size
, big_endian
>::do_section_link(unsigned int)
1923 // Return the section link field.
1925 template<int size
, bool big_endian
>
1927 Sized_incr_relobj
<size
, big_endian
>::do_section_info(unsigned int)
1932 // Return the section alignment.
1934 template<int size
, bool big_endian
>
1936 Sized_incr_relobj
<size
, big_endian
>::do_section_addralign(unsigned int)
1941 // Return the Xindex structure to use.
1943 template<int size
, bool big_endian
>
1945 Sized_incr_relobj
<size
, big_endian
>::do_initialize_xindex()
1950 // Get symbol counts.
1952 template<int size
, bool big_endian
>
1954 Sized_incr_relobj
<size
, big_endian
>::do_get_global_symbol_counts(
1955 const Symbol_table
*, size_t*, size_t*) const
1962 template<int size
, bool big_endian
>
1964 Sized_incr_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
*)
1968 // Process the relocs to find list of referenced sections. Used only
1969 // during garbage collection.
1971 template<int size
, bool big_endian
>
1973 Sized_incr_relobj
<size
, big_endian
>::do_gc_process_relocs(Symbol_table
*,
1980 // Scan the relocs and adjust the symbol table.
1982 template<int size
, bool big_endian
>
1984 Sized_incr_relobj
<size
, big_endian
>::do_scan_relocs(Symbol_table
*,
1988 // Count the incremental relocations for this object.
1989 unsigned int nsyms
= this->input_reader_
.get_global_symbol_count();
1990 this->allocate_incremental_reloc_counts();
1991 for (unsigned int i
= 0; i
< nsyms
; i
++)
1993 Incremental_global_symbol_reader
<big_endian
> sym
=
1994 this->input_reader_
.get_global_symbol_reader(i
);
1995 unsigned int reloc_count
= sym
.reloc_count();
1996 if (reloc_count
> 0 && this->incr_reloc_offset_
== -1U)
1997 this->incr_reloc_offset_
= sym
.reloc_offset();
1998 this->incr_reloc_count_
+= reloc_count
;
1999 for (unsigned int j
= 0; j
< reloc_count
; j
++)
2000 this->count_incremental_reloc(i
);
2002 this->incr_reloc_output_index_
=
2003 layout
->incremental_inputs()->get_reloc_count();
2004 this->finalize_incremental_relocs(layout
, false);
2006 // The incoming incremental relocations may not end up in the same
2007 // location after the incremental update, because the incremental info
2008 // is regenerated in each link. Because the new location may overlap
2009 // with other data in the updated output file, we need to copy the
2010 // relocations into a buffer so that we can still read them safely
2011 // after we start writing updates to the output file.
2012 if (this->incr_reloc_count_
> 0)
2014 const Incremental_relocs_reader
<size
, big_endian
>& relocs_reader
=
2015 this->ibase_
->relocs_reader();
2016 const unsigned int incr_reloc_size
= relocs_reader
.reloc_size
;
2017 unsigned int len
= this->incr_reloc_count_
* incr_reloc_size
;
2018 this->incr_relocs_
= new unsigned char[len
];
2019 memcpy(this->incr_relocs_
,
2020 relocs_reader
.data(this->incr_reloc_offset_
),
2025 // Count the local symbols.
2027 template<int size
, bool big_endian
>
2029 Sized_incr_relobj
<size
, big_endian
>::do_count_local_symbols(
2030 Stringpool_template
<char>*,
2031 Stringpool_template
<char>*)
2033 // FIXME: Count local symbols.
2036 // Finalize the local symbols.
2038 template<int size
, bool big_endian
>
2040 Sized_incr_relobj
<size
, big_endian
>::do_finalize_local_symbols(
2045 // FIXME: Finalize local symbols.
2049 // Set the offset where local dynamic symbol information will be stored.
2051 template<int size
, bool big_endian
>
2053 Sized_incr_relobj
<size
, big_endian
>::do_set_local_dynsym_indexes(
2056 // FIXME: set local dynsym indexes.
2060 // Set the offset where local dynamic symbol information will be stored.
2062 template<int size
, bool big_endian
>
2064 Sized_incr_relobj
<size
, big_endian
>::do_set_local_dynsym_offset(off_t
)
2069 // Relocate the input sections and write out the local symbols.
2070 // We don't actually do any relocation here. For unchanged input files,
2071 // we reapply relocations only for symbols that have changed; that happens
2072 // in queue_final_tasks. We do need to rewrite the incremental relocations
2075 template<int size
, bool big_endian
>
2077 Sized_incr_relobj
<size
, big_endian
>::do_relocate(const Symbol_table
*,
2078 const Layout
* layout
,
2081 if (this->incr_reloc_count_
== 0)
2084 const unsigned int incr_reloc_size
=
2085 Incremental_relocs_reader
<size
, big_endian
>::reloc_size
;
2087 // Get a view for the .gnu_incremental_relocs section.
2088 Incremental_inputs
* inputs
= layout
->incremental_inputs();
2089 gold_assert(inputs
!= NULL
);
2090 const off_t relocs_off
= inputs
->relocs_section()->offset();
2091 const off_t relocs_size
= inputs
->relocs_section()->data_size();
2092 unsigned char* const view
= of
->get_output_view(relocs_off
, relocs_size
);
2094 // Copy the relocations from the buffer.
2095 off_t off
= this->incr_reloc_output_index_
* incr_reloc_size
;
2096 unsigned int len
= this->incr_reloc_count_
* incr_reloc_size
;
2097 memcpy(view
+ off
, this->incr_relocs_
, len
);
2099 // The output section table may have changed, so we need to map
2100 // the old section index to the new section index for each relocation.
2101 for (unsigned int i
= 0; i
< this->incr_reloc_count_
; ++i
)
2103 unsigned char* pov
= view
+ off
+ i
* incr_reloc_size
;
2104 unsigned int shndx
= elfcpp::Swap
<32, big_endian
>::readval(pov
+ 4);
2105 Output_section
* os
= this->ibase_
->output_section(shndx
);
2106 gold_assert(os
!= NULL
);
2107 shndx
= os
->out_shndx();
2108 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, shndx
);
2111 of
->write_output_view(off
, len
, view
);
2114 // Set the offset of a section.
2116 template<int size
, bool big_endian
>
2118 Sized_incr_relobj
<size
, big_endian
>::do_set_section_offset(unsigned int,
2123 // Class Sized_incr_dynobj. Most of these methods are not used for
2124 // Incremental objects, but are required to be implemented by the
2125 // base class Object.
2127 template<int size
, bool big_endian
>
2128 Sized_incr_dynobj
<size
, big_endian
>::Sized_incr_dynobj(
2129 const std::string
& name
,
2130 Sized_incremental_binary
<size
, big_endian
>* ibase
,
2131 unsigned int input_file_index
)
2132 : Dynobj(name
, NULL
), ibase_(ibase
),
2133 input_file_index_(input_file_index
),
2134 input_reader_(ibase
->inputs_reader().input_file(input_file_index
)),
2137 if (this->input_reader_
.is_in_system_directory())
2138 this->set_is_in_system_directory();
2142 // Read the symbols.
2144 template<int size
, bool big_endian
>
2146 Sized_incr_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
2151 // Lay out the input sections.
2153 template<int size
, bool big_endian
>
2155 Sized_incr_dynobj
<size
, big_endian
>::do_layout(
2162 // Add the symbols to the symbol table.
2164 template<int size
, bool big_endian
>
2166 Sized_incr_dynobj
<size
, big_endian
>::do_add_symbols(
2167 Symbol_table
* symtab
,
2171 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2172 unsigned char symbuf
[sym_size
];
2173 elfcpp::Sym
<size
, big_endian
> sym(symbuf
);
2174 elfcpp::Sym_write
<size
, big_endian
> osym(symbuf
);
2176 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Elf_size_type
;
2178 unsigned int nsyms
= this->input_reader_
.get_global_symbol_count();
2179 this->symbols_
.resize(nsyms
);
2181 Incremental_binary::View
symtab_view(NULL
);
2182 unsigned int symtab_count
;
2183 elfcpp::Elf_strtab
strtab(NULL
, 0);
2184 this->ibase_
->get_symtab_view(&symtab_view
, &symtab_count
, &strtab
);
2186 Incremental_symtab_reader
<big_endian
> isymtab(this->ibase_
->symtab_reader());
2187 unsigned int isym_count
= isymtab
.symbol_count();
2188 unsigned int first_global
= symtab_count
- isym_count
;
2190 unsigned const char* sym_p
;
2191 for (unsigned int i
= 0; i
< nsyms
; ++i
)
2194 unsigned int output_symndx
=
2195 this->input_reader_
.get_output_symbol_index(i
, &is_def
);
2196 sym_p
= symtab_view
.data() + output_symndx
* sym_size
;
2197 elfcpp::Sym
<size
, big_endian
> gsym(sym_p
);
2199 if (!strtab
.get_c_string(gsym
.get_st_name(), &name
))
2202 typename
elfcpp::Elf_types
<size
>::Elf_Addr v
;
2204 elfcpp::STB st_bind
= gsym
.get_st_bind();
2205 elfcpp::STT st_type
= gsym
.get_st_type();
2207 // Local hidden symbols start out as globals, but get converted to
2208 // to local during output.
2209 if (st_bind
== elfcpp::STB_LOCAL
)
2210 st_bind
= elfcpp::STB_GLOBAL
;
2214 shndx
= elfcpp::SHN_UNDEF
;
2219 // For a symbol defined in a shared object, the section index
2220 // is meaningless, as long as it's not SHN_UNDEF.
2222 v
= gsym
.get_st_value();
2225 osym
.put_st_name(0);
2226 osym
.put_st_value(v
);
2227 osym
.put_st_size(gsym
.get_st_size());
2228 osym
.put_st_info(st_bind
, st_type
);
2229 osym
.put_st_other(gsym
.get_st_other());
2230 osym
.put_st_shndx(shndx
);
2233 symtab
->add_from_incrobj
<size
, big_endian
>(this, name
, NULL
, &sym
);
2234 this->ibase_
->add_global_symbol(output_symndx
- first_global
,
2239 // Return TRUE if we should include this object from an archive library.
2241 template<int size
, bool big_endian
>
2242 Archive::Should_include
2243 Sized_incr_dynobj
<size
, big_endian
>::do_should_include_member(
2252 // Iterate over global symbols, calling a visitor class V for each.
2254 template<int size
, bool big_endian
>
2256 Sized_incr_dynobj
<size
, big_endian
>::do_for_all_global_symbols(
2258 Library_base::Symbol_visitor_base
*)
2260 // This routine is not used for dynamic libraries.
2263 // Iterate over local symbols, calling a visitor class V for each GOT offset
2264 // associated with a local symbol.
2266 template<int size
, bool big_endian
>
2268 Sized_incr_dynobj
<size
, big_endian
>::do_for_all_local_got_entries(
2269 Got_offset_list::Visitor
*) const
2273 // Get the size of a section.
2275 template<int size
, bool big_endian
>
2277 Sized_incr_dynobj
<size
, big_endian
>::do_section_size(unsigned int)
2282 // Get the name of a section.
2284 template<int size
, bool big_endian
>
2286 Sized_incr_dynobj
<size
, big_endian
>::do_section_name(unsigned int)
2291 // Return a view of the contents of a section.
2293 template<int size
, bool big_endian
>
2295 Sized_incr_dynobj
<size
, big_endian
>::do_section_contents(unsigned int)
2300 // Return section flags.
2302 template<int size
, bool big_endian
>
2304 Sized_incr_dynobj
<size
, big_endian
>::do_section_flags(unsigned int)
2309 // Return section entsize.
2311 template<int size
, bool big_endian
>
2313 Sized_incr_dynobj
<size
, big_endian
>::do_section_entsize(unsigned int)
2318 // Return section address.
2320 template<int size
, bool big_endian
>
2322 Sized_incr_dynobj
<size
, big_endian
>::do_section_address(unsigned int)
2327 // Return section type.
2329 template<int size
, bool big_endian
>
2331 Sized_incr_dynobj
<size
, big_endian
>::do_section_type(unsigned int)
2336 // Return the section link field.
2338 template<int size
, bool big_endian
>
2340 Sized_incr_dynobj
<size
, big_endian
>::do_section_link(unsigned int)
2345 // Return the section link field.
2347 template<int size
, bool big_endian
>
2349 Sized_incr_dynobj
<size
, big_endian
>::do_section_info(unsigned int)
2354 // Return the section alignment.
2356 template<int size
, bool big_endian
>
2358 Sized_incr_dynobj
<size
, big_endian
>::do_section_addralign(unsigned int)
2363 // Return the Xindex structure to use.
2365 template<int size
, bool big_endian
>
2367 Sized_incr_dynobj
<size
, big_endian
>::do_initialize_xindex()
2372 // Get symbol counts.
2374 template<int size
, bool big_endian
>
2376 Sized_incr_dynobj
<size
, big_endian
>::do_get_global_symbol_counts(
2377 const Symbol_table
*, size_t*, size_t*) const
2382 // Allocate an incremental object of the appropriate size and endianness.
2385 make_sized_incremental_object(
2386 Incremental_binary
* ibase
,
2387 unsigned int input_file_index
,
2388 Incremental_input_type input_type
,
2389 const Incremental_binary::Input_reader
* input_reader
)
2392 std::string
name(input_reader
->filename());
2394 switch (parameters
->size_and_endianness())
2396 #ifdef HAVE_TARGET_32_LITTLE
2397 case Parameters::TARGET_32_LITTLE
:
2399 Sized_incremental_binary
<32, false>* sized_ibase
=
2400 static_cast<Sized_incremental_binary
<32, false>*>(ibase
);
2401 if (input_type
== INCREMENTAL_INPUT_SHARED_LIBRARY
)
2402 obj
= new Sized_incr_dynobj
<32, false>(name
, sized_ibase
,
2405 obj
= new Sized_incr_relobj
<32, false>(name
, sized_ibase
,
2410 #ifdef HAVE_TARGET_32_BIG
2411 case Parameters::TARGET_32_BIG
:
2413 Sized_incremental_binary
<32, true>* sized_ibase
=
2414 static_cast<Sized_incremental_binary
<32, true>*>(ibase
);
2415 if (input_type
== INCREMENTAL_INPUT_SHARED_LIBRARY
)
2416 obj
= new Sized_incr_dynobj
<32, true>(name
, sized_ibase
,
2419 obj
= new Sized_incr_relobj
<32, true>(name
, sized_ibase
,
2424 #ifdef HAVE_TARGET_64_LITTLE
2425 case Parameters::TARGET_64_LITTLE
:
2427 Sized_incremental_binary
<64, false>* sized_ibase
=
2428 static_cast<Sized_incremental_binary
<64, false>*>(ibase
);
2429 if (input_type
== INCREMENTAL_INPUT_SHARED_LIBRARY
)
2430 obj
= new Sized_incr_dynobj
<64, false>(name
, sized_ibase
,
2433 obj
= new Sized_incr_relobj
<64, false>(name
, sized_ibase
,
2438 #ifdef HAVE_TARGET_64_BIG
2439 case Parameters::TARGET_64_BIG
:
2441 Sized_incremental_binary
<64, true>* sized_ibase
=
2442 static_cast<Sized_incremental_binary
<64, true>*>(ibase
);
2443 if (input_type
== INCREMENTAL_INPUT_SHARED_LIBRARY
)
2444 obj
= new Sized_incr_dynobj
<64, true>(name
, sized_ibase
,
2447 obj
= new Sized_incr_relobj
<64, true>(name
, sized_ibase
,
2456 gold_assert(obj
!= NULL
);
2460 // Copy the unused symbols from the incremental input info.
2461 // We need to do this because we may be overwriting the incremental
2462 // input info in the base file before we write the new incremental
2465 Incremental_library::copy_unused_symbols()
2467 unsigned int symcount
= this->input_reader_
->get_unused_symbol_count();
2468 this->unused_symbols_
.reserve(symcount
);
2469 for (unsigned int i
= 0; i
< symcount
; ++i
)
2471 std::string
name(this->input_reader_
->get_unused_symbol(i
));
2472 this->unused_symbols_
.push_back(name
);
2476 // Iterator for unused global symbols in the library.
2478 Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base
* v
) const
2480 for (Symbol_list::const_iterator p
= this->unused_symbols_
.begin();
2481 p
!= this->unused_symbols_
.end();
2483 v
->visit(p
->c_str());
2486 // Instantiate the templates we need.
2488 #ifdef HAVE_TARGET_32_LITTLE
2490 class Sized_incremental_binary
<32, false>;
2493 class Sized_incr_relobj
<32, false>;
2496 class Sized_incr_dynobj
<32, false>;
2499 #ifdef HAVE_TARGET_32_BIG
2501 class Sized_incremental_binary
<32, true>;
2504 class Sized_incr_relobj
<32, true>;
2507 class Sized_incr_dynobj
<32, true>;
2510 #ifdef HAVE_TARGET_64_LITTLE
2512 class Sized_incremental_binary
<64, false>;
2515 class Sized_incr_relobj
<64, false>;
2518 class Sized_incr_dynobj
<64, false>;
2521 #ifdef HAVE_TARGET_64_BIG
2523 class Sized_incremental_binary
<64, true>;
2526 class Sized_incr_relobj
<64, true>;
2529 class Sized_incr_dynobj
<64, true>;
2532 } // End namespace gold.