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 // Find the main symbol table.
262 unsigned int main_symtab_shndx
=
263 this->elf_file_
.find_section_by_type(elfcpp::SHT_SYMTAB
);
264 gold_assert(main_symtab_shndx
!= elfcpp::SHN_UNDEF
);
265 this->main_symtab_loc_
= this->elf_file_
.section_contents(main_symtab_shndx
);
267 // Find the main symbol string table.
268 unsigned int main_strtab_shndx
=
269 this->elf_file_
.section_link(main_symtab_shndx
);
270 gold_assert(main_strtab_shndx
!= elfcpp::SHN_UNDEF
271 && main_strtab_shndx
< this->elf_file_
.shnum());
272 this->main_strtab_loc_
= this->elf_file_
.section_contents(main_strtab_shndx
);
274 // Walk the list of input files (a) to setup an Input_reader for each
275 // input file, and (b) to record maps of files added from archive
276 // libraries and scripts.
277 Incremental_inputs_reader
<size
, big_endian
>& inputs
= this->inputs_reader_
;
278 unsigned int count
= inputs
.input_file_count();
279 this->input_entry_readers_
.reserve(count
);
280 this->library_map_
.resize(count
);
281 this->script_map_
.resize(count
);
282 for (unsigned int i
= 0; i
< count
; i
++)
284 Input_entry_reader input_file
= inputs
.input_file(i
);
285 this->input_entry_readers_
.push_back(Sized_input_reader(input_file
));
286 switch (input_file
.type())
288 case INCREMENTAL_INPUT_OBJECT
:
289 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
290 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
291 // No special treatment necessary.
293 case INCREMENTAL_INPUT_ARCHIVE
:
295 Incremental_library
* lib
=
296 new Incremental_library(input_file
.filename(), i
,
297 &this->input_entry_readers_
[i
]);
298 this->library_map_
[i
] = lib
;
299 unsigned int member_count
= input_file
.get_member_count();
300 for (unsigned int j
= 0; j
< member_count
; j
++)
302 int member_offset
= input_file
.get_member_offset(j
);
303 int member_index
= inputs
.input_file_index(member_offset
);
304 this->library_map_
[member_index
] = lib
;
308 case INCREMENTAL_INPUT_SCRIPT
:
310 Script_info
* script
= new Script_info(input_file
.filename());
311 this->script_map_
[i
] = script
;
312 unsigned int object_count
= input_file
.get_object_count();
313 for (unsigned int j
= 0; j
< object_count
; j
++)
315 int object_offset
= input_file
.get_object_offset(j
);
316 int object_index
= inputs
.input_file_index(object_offset
);
317 this->script_map_
[object_index
] = script
;
326 // Initialize the map of global symbols.
327 unsigned int nglobals
= this->symtab_reader_
.symbol_count();
328 this->symbol_map_
.resize(nglobals
);
330 // Initialize the status of each input file.
331 this->file_status_
= new unsigned char[(count
+ 7) / 8];
332 memset(this->file_status_
, 0, (count
+ 7) / 8);
334 this->has_incremental_info_
= true;
337 // Walk the list of input files given on the command line, and build
338 // a direct map of file index to the corresponding input argument.
341 check_input_args(std::vector
<const Input_argument
*>& input_args_map
,
342 Input_arguments::const_iterator begin
,
343 Input_arguments::const_iterator end
)
345 for (Input_arguments::const_iterator p
= begin
;
351 const Input_file_group
* group
= p
->group();
352 check_input_args(input_args_map
, group
->begin(), group
->end());
354 else if (p
->is_lib())
356 const Input_file_lib
* lib
= p
->lib();
357 check_input_args(input_args_map
, lib
->begin(), lib
->end());
361 gold_assert(p
->is_file());
362 unsigned int arg_serial
= p
->file().arg_serial();
365 gold_assert(arg_serial
<= input_args_map
.size());
366 gold_assert(input_args_map
[arg_serial
- 1] == 0);
367 input_args_map
[arg_serial
- 1] = &*p
;
373 // Determine whether an incremental link based on the existing output file
376 template<int size
, bool big_endian
>
378 Sized_incremental_binary
<size
, big_endian
>::do_check_inputs(
379 const Command_line
& cmdline
,
380 Incremental_inputs
* incremental_inputs
)
382 Incremental_inputs_reader
<size
, big_endian
>& inputs
= this->inputs_reader_
;
384 if (!this->has_incremental_info_
)
386 explain_no_incremental(_("no incremental data from previous build"));
390 if (inputs
.version() != INCREMENTAL_LINK_VERSION
)
392 explain_no_incremental(_("different version of incremental build data"));
396 if (incremental_inputs
->command_line() != inputs
.command_line())
398 explain_no_incremental(_("command line changed"));
402 // Walk the list of input files given on the command line, and build
403 // a direct map of argument serial numbers to the corresponding input
405 this->input_args_map_
.resize(cmdline
.number_of_input_files());
406 check_input_args(this->input_args_map_
, cmdline
.begin(), cmdline
.end());
408 // Walk the list of input files to check for conditions that prevent
409 // an incremental update link.
410 unsigned int count
= inputs
.input_file_count();
411 for (unsigned int i
= 0; i
< count
; i
++)
413 Input_entry_reader input_file
= inputs
.input_file(i
);
414 switch (input_file
.type())
416 case INCREMENTAL_INPUT_OBJECT
:
417 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
418 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
419 case INCREMENTAL_INPUT_ARCHIVE
:
420 // No special treatment necessary.
422 case INCREMENTAL_INPUT_SCRIPT
:
423 if (this->do_file_has_changed(i
))
425 explain_no_incremental(_("%s: script file changed"),
426 input_file
.filename());
438 // Return TRUE if input file N has changed since the last incremental link.
440 template<int size
, bool big_endian
>
442 Sized_incremental_binary
<size
, big_endian
>::do_file_has_changed(
443 unsigned int n
) const
445 Input_entry_reader input_file
= this->inputs_reader_
.input_file(n
);
446 Incremental_disposition disp
= INCREMENTAL_CHECK
;
447 const Input_argument
* input_argument
= this->get_input_argument(n
);
448 if (input_argument
!= NULL
)
449 disp
= input_argument
->file().options().incremental_disposition();
451 if (disp
!= INCREMENTAL_CHECK
)
452 return disp
== INCREMENTAL_CHANGED
;
454 const char* filename
= input_file
.filename();
455 Timespec old_mtime
= input_file
.get_mtime();
457 if (!get_mtime(filename
, &new_mtime
))
459 // If we can't open get the current modification time, assume it has
460 // changed. If the file doesn't exist, we'll issue an error when we
461 // try to open it later.
465 if (new_mtime
.seconds
> old_mtime
.seconds
)
467 if (new_mtime
.seconds
== old_mtime
.seconds
468 && new_mtime
.nanoseconds
> old_mtime
.nanoseconds
)
473 // Initialize the layout of the output file based on the existing
476 template<int size
, bool big_endian
>
478 Sized_incremental_binary
<size
, big_endian
>::do_init_layout(Layout
* layout
)
480 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
481 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
483 // Get views of the section headers and the section string table.
484 const off_t shoff
= this->elf_file_
.shoff();
485 const unsigned int shnum
= this->elf_file_
.shnum();
486 const unsigned int shstrndx
= this->elf_file_
.shstrndx();
487 Location
shdrs_location(shoff
, shnum
* shdr_size
);
488 Location
shstrndx_location(this->elf_file_
.section_contents(shstrndx
));
489 View shdrs_view
= this->view(shdrs_location
);
490 View shstrndx_view
= this->view(shstrndx_location
);
491 elfcpp::Elf_strtab
shstrtab(shstrndx_view
.data(),
492 shstrndx_location
.data_size
);
494 layout
->set_incremental_base(this);
496 // Initialize the layout.
497 this->section_map_
.resize(shnum
);
498 const unsigned char* pshdr
= shdrs_view
.data() + shdr_size
;
499 for (unsigned int i
= 1; i
< shnum
; i
++)
503 if (!shstrtab
.get_c_string(shdr
.get_sh_name(), &name
))
505 gold_debug(DEBUG_INCREMENTAL
,
506 "Output section: %2d %08lx %08lx %08lx %3d %s",
508 static_cast<long>(shdr
.get_sh_addr()),
509 static_cast<long>(shdr
.get_sh_offset()),
510 static_cast<long>(shdr
.get_sh_size()),
511 shdr
.get_sh_type(), name
? name
: "<null>");
512 this->section_map_
[i
] = layout
->init_fixed_output_section(name
, shdr
);
517 // Mark regions of the input file that must be kept unchanged.
519 template<int size
, bool big_endian
>
521 Sized_incremental_binary
<size
, big_endian
>::do_reserve_layout(
522 unsigned int input_file_index
)
524 Input_entry_reader input_file
=
525 this->inputs_reader_
.input_file(input_file_index
);
527 this->set_file_is_unchanged(input_file_index
);
529 if (input_file
.type() == INCREMENTAL_INPUT_SHARED_LIBRARY
)
532 unsigned int shnum
= input_file
.get_input_section_count();
533 for (unsigned int i
= 0; i
< shnum
; i
++)
535 typename
Input_entry_reader::Input_section_info sect
=
536 input_file
.get_input_section(i
);
537 if (sect
.output_shndx
== 0 || sect
.sh_offset
== -1)
539 Output_section
* os
= this->section_map_
[sect
.output_shndx
];
540 gold_assert(os
!= NULL
);
541 os
->reserve(sect
.sh_offset
, sect
.sh_size
);
545 // Process the GOT and PLT entries from the existing output file.
547 template<int size
, bool big_endian
>
549 Sized_incremental_binary
<size
, big_endian
>::do_process_got_plt(
550 Symbol_table
* symtab
,
553 Incremental_got_plt_reader
<big_endian
> got_plt_reader(this->got_plt_reader());
554 Sized_target
<size
, big_endian
>* target
=
555 parameters
->sized_target
<size
, big_endian
>();
557 // Get the number of symbols in the main symbol table and in the
558 // incremental symbol table. The difference between the two counts
559 // is the index of the first forced-local or global symbol in the
560 // main symbol table.
561 unsigned int symtab_count
=
562 this->main_symtab_loc_
.data_size
/ elfcpp::Elf_sizes
<size
>::sym_size
;
563 unsigned int isym_count
= this->symtab_reader_
.symbol_count();
564 unsigned int first_global
= symtab_count
- isym_count
;
566 // Tell the target how big the GOT and PLT sections are.
567 unsigned int got_count
= got_plt_reader
.get_got_entry_count();
568 unsigned int plt_count
= got_plt_reader
.get_plt_entry_count();
569 Output_data_got
<size
, big_endian
>* got
=
570 target
->init_got_plt_for_update(symtab
, layout
, got_count
, plt_count
);
572 // Read the GOT entries from the base file and build the outgoing GOT.
573 for (unsigned int i
= 0; i
< got_count
; ++i
)
575 unsigned int got_type
= got_plt_reader
.get_got_type(i
);
576 if ((got_type
& 0x7f) == 0x7f)
578 // This is the second entry of a pair.
579 got
->reserve_slot(i
);
582 unsigned int got_desc
= got_plt_reader
.get_got_desc(i
);
585 // This is an entry for a local symbol. GOT_DESC is the index
586 // of the object file entry in the list of input files. Ignore
587 // this entry if the object file was replaced.
588 gold_debug(DEBUG_INCREMENTAL
,
589 "GOT entry %d, type %02x: (local symbol)",
591 if (this->file_is_unchanged(got_desc
))
592 got
->reserve_slot(i
);
596 // This is an entry for a global symbol. GOT_DESC is the symbol
598 // FIXME: This should really be a fatal error (corrupt input).
599 gold_assert(got_desc
>= first_global
&& got_desc
< symtab_count
);
600 Symbol
* sym
= this->global_symbol(got_desc
- first_global
);
601 gold_debug(DEBUG_INCREMENTAL
,
602 "GOT entry %d, type %02x: %s",
603 i
, got_type
, sym
->name());
604 got
->reserve_slot_for_global(i
, sym
, got_type
);
608 // Read the PLT entries from the base file and pass each to the target.
609 for (unsigned int i
= 0; i
< plt_count
; ++i
)
611 unsigned int plt_desc
= got_plt_reader
.get_plt_desc(i
);
612 // FIXME: This should really be a fatal error (corrupt input).
613 gold_assert(plt_desc
>= first_global
&& plt_desc
< symtab_count
);
614 Symbol
* sym
= this->global_symbol(plt_desc
- first_global
);
615 gold_debug(DEBUG_INCREMENTAL
,
618 target
->register_global_plt_entry(i
, sym
);
622 // Apply incremental relocations for symbols whose values have changed.
624 template<int size
, bool big_endian
>
626 Sized_incremental_binary
<size
, big_endian
>::do_apply_incremental_relocs(
627 const Symbol_table
* symtab
,
631 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
632 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Addend
;
633 Incremental_symtab_reader
<big_endian
> isymtab(this->symtab_reader());
634 Incremental_relocs_reader
<size
, big_endian
> irelocs(this->relocs_reader());
635 unsigned int nglobals
= isymtab
.symbol_count();
636 const unsigned int incr_reloc_size
= irelocs
.reloc_size
;
638 Relocate_info
<size
, big_endian
> relinfo
;
639 relinfo
.symtab
= symtab
;
640 relinfo
.layout
= layout
;
641 relinfo
.object
= NULL
;
642 relinfo
.reloc_shndx
= 0;
643 relinfo
.reloc_shdr
= NULL
;
644 relinfo
.data_shndx
= 0;
645 relinfo
.data_shdr
= NULL
;
647 Sized_target
<size
, big_endian
>* target
=
648 parameters
->sized_target
<size
, big_endian
>();
650 for (unsigned int i
= 0; i
< nglobals
; i
++)
652 const Symbol
* gsym
= this->global_symbol(i
);
654 // If the symbol is not referenced from any unchanged input files,
655 // we do not need to reapply any of its relocations.
659 // If the symbol is defined in an unchanged file, we do not need to
660 // reapply any of its relocations.
661 if (gsym
->source() == Symbol::FROM_OBJECT
662 && gsym
->object()->is_incremental())
665 gold_debug(DEBUG_INCREMENTAL
,
666 "Applying incremental relocations for global symbol %s [%d]",
669 // Follow the linked list of input symbol table entries for this symbol.
670 // We don't bother to figure out whether the symbol table entry belongs
671 // to a changed or unchanged file because it's easier just to apply all
672 // the relocations -- although we might scribble over an area that has
673 // been reallocated, we do this before copying any new data into the
675 unsigned int offset
= isymtab
.get_list_head(i
);
678 Incremental_global_symbol_reader
<big_endian
> sym_info
=
679 this->inputs_reader().global_symbol_reader_at_offset(offset
);
680 unsigned int r_base
= sym_info
.reloc_offset();
681 unsigned int r_count
= sym_info
.reloc_count();
683 // Apply each relocation for this symbol table entry.
684 for (unsigned int j
= 0; j
< r_count
;
685 ++j
, r_base
+= incr_reloc_size
)
687 unsigned int r_type
= irelocs
.get_r_type(r_base
);
688 unsigned int r_shndx
= irelocs
.get_r_shndx(r_base
);
689 Address r_offset
= irelocs
.get_r_offset(r_base
);
690 Addend r_addend
= irelocs
.get_r_addend(r_base
);
691 Output_section
* os
= this->output_section(r_shndx
);
692 Address address
= os
->address();
693 off_t section_offset
= os
->offset();
694 size_t view_size
= os
->data_size();
695 unsigned char* const view
= of
->get_output_view(section_offset
,
698 gold_debug(DEBUG_INCREMENTAL
,
699 " %08lx: %s + %d: type %d addend %ld",
700 (long)(section_offset
+ r_offset
),
706 target
->apply_relocation(&relinfo
, r_offset
, r_type
, r_addend
,
707 gsym
, view
, address
, view_size
);
709 // FIXME: Do something more efficient if write_output_view
710 // ever becomes more than a no-op.
711 of
->write_output_view(section_offset
, view_size
, view
);
713 offset
= sym_info
.next_offset();
718 // Get a view of the main symbol table and the symbol string table.
720 template<int size
, bool big_endian
>
722 Sized_incremental_binary
<size
, big_endian
>::get_symtab_view(
725 elfcpp::Elf_strtab
* strtab
)
727 *symtab_view
= this->view(this->main_symtab_loc_
);
728 *nsyms
= this->main_symtab_loc_
.data_size
/ elfcpp::Elf_sizes
<size
>::sym_size
;
730 View
strtab_view(this->view(this->main_strtab_loc_
));
731 *strtab
= elfcpp::Elf_strtab(strtab_view
.data(),
732 this->main_strtab_loc_
.data_size
);
738 // Create a Sized_incremental_binary object of the specified size and
739 // endianness. Fails if the target architecture is not supported.
741 template<int size
, bool big_endian
>
743 make_sized_incremental_binary(Output_file
* file
,
744 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
746 Target
* target
= select_target(ehdr
.get_e_machine(), size
, big_endian
,
747 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
748 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
751 explain_no_incremental(_("unsupported ELF machine number %d"),
752 ehdr
.get_e_machine());
756 if (!parameters
->target_valid())
757 set_parameters_target(target
);
758 else if (target
!= ¶meters
->target())
759 gold_error(_("%s: incompatible target"), file
->filename());
761 return new Sized_incremental_binary
<size
, big_endian
>(file
, ehdr
, target
);
764 } // End of anonymous namespace.
766 // Create an Incremental_binary object for FILE. Returns NULL is this is not
767 // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
771 open_incremental_binary(Output_file
* file
)
773 off_t filesize
= file
->filesize();
774 int want
= elfcpp::Elf_recognizer::max_header_size
;
778 const unsigned char* p
= file
->get_input_view(0, want
);
779 if (!elfcpp::Elf_recognizer::is_elf_file(p
, want
))
781 explain_no_incremental(_("output is not an ELF file."));
786 bool big_endian
= false;
788 if (!elfcpp::Elf_recognizer::is_valid_header(p
, want
, &size
, &big_endian
,
791 explain_no_incremental(error
.c_str());
795 Incremental_binary
* result
= NULL
;
800 #ifdef HAVE_TARGET_32_BIG
801 result
= make_sized_incremental_binary
<32, true>(
802 file
, elfcpp::Ehdr
<32, true>(p
));
804 explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
809 #ifdef HAVE_TARGET_32_LITTLE
810 result
= make_sized_incremental_binary
<32, false>(
811 file
, elfcpp::Ehdr
<32, false>(p
));
813 explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
821 #ifdef HAVE_TARGET_64_BIG
822 result
= make_sized_incremental_binary
<64, true>(
823 file
, elfcpp::Ehdr
<64, true>(p
));
825 explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
830 #ifdef HAVE_TARGET_64_LITTLE
831 result
= make_sized_incremental_binary
<64, false>(
832 file
, elfcpp::Ehdr
<64, false>(p
));
834 explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
844 // Class Incremental_inputs.
846 // Add the command line to the string table, setting
847 // command_line_key_. In incremental builds, the command line is
848 // stored in .gnu_incremental_inputs so that the next linker run can
849 // check if the command line options didn't change.
852 Incremental_inputs::report_command_line(int argc
, const char* const* argv
)
854 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
855 // different path to the linker.
856 std::string
args("gold");
857 // Copied from collect_argv in main.cc.
858 for (int i
= 1; i
< argc
; ++i
)
860 // Adding/removing these options should not result in a full relink.
861 if (strcmp(argv
[i
], "--incremental") == 0
862 || strcmp(argv
[i
], "--incremental-full") == 0
863 || strcmp(argv
[i
], "--incremental-update") == 0
864 || strcmp(argv
[i
], "--incremental-changed") == 0
865 || strcmp(argv
[i
], "--incremental-unchanged") == 0
866 || strcmp(argv
[i
], "--incremental-unknown") == 0
867 || is_prefix_of("--debug=", argv
[i
]))
871 // Now append argv[i], but with all single-quotes escaped
872 const char* argpos
= argv
[i
];
875 const int len
= strcspn(argpos
, "'");
876 args
.append(argpos
, len
);
877 if (argpos
[len
] == '\0')
879 args
.append("'\"'\"'");
885 this->command_line_
= args
;
886 this->strtab_
->add(this->command_line_
.c_str(), false,
887 &this->command_line_key_
);
890 // Record the input archive file ARCHIVE. This is called by the
891 // Add_archive_symbols task before determining which archive members
892 // to include. We create the Incremental_archive_entry here and
893 // attach it to the Archive, but we do not add it to the list of
894 // input objects until report_archive_end is called.
897 Incremental_inputs::report_archive_begin(Library_base
* arch
,
898 unsigned int arg_serial
,
899 Script_info
* script_info
)
901 Stringpool::Key filename_key
;
902 Timespec mtime
= arch
->get_mtime();
904 // For a file loaded from a script, don't record its argument serial number.
905 if (script_info
!= NULL
)
908 this->strtab_
->add(arch
->filename().c_str(), false, &filename_key
);
909 Incremental_archive_entry
* entry
=
910 new Incremental_archive_entry(filename_key
, arg_serial
, mtime
);
911 arch
->set_incremental_info(entry
);
913 if (script_info
!= NULL
)
915 Incremental_script_entry
* script_entry
= script_info
->incremental_info();
916 gold_assert(script_entry
!= NULL
);
917 script_entry
->add_object(entry
);
921 // Visitor class for processing the unused global symbols in a library.
922 // An instance of this class is passed to the library's
923 // for_all_unused_symbols() iterator, which will call the visit()
924 // function for each global symbol defined in each unused library
925 // member. We add those symbol names to the incremental info for the
928 class Unused_symbol_visitor
: public Library_base::Symbol_visitor_base
931 Unused_symbol_visitor(Incremental_archive_entry
* entry
, Stringpool
* strtab
)
932 : entry_(entry
), strtab_(strtab
)
936 visit(const char* sym
)
938 Stringpool::Key symbol_key
;
939 this->strtab_
->add(sym
, true, &symbol_key
);
940 this->entry_
->add_unused_global_symbol(symbol_key
);
944 Incremental_archive_entry
* entry_
;
948 // Finish recording the input archive file ARCHIVE. This is called by the
949 // Add_archive_symbols task after determining which archive members
953 Incremental_inputs::report_archive_end(Library_base
* arch
)
955 Incremental_archive_entry
* entry
= arch
->incremental_info();
957 gold_assert(entry
!= NULL
);
958 this->inputs_
.push_back(entry
);
960 // Collect unused global symbols.
961 Unused_symbol_visitor
v(entry
, this->strtab_
);
962 arch
->for_all_unused_symbols(&v
);
965 // Record the input object file OBJ. If ARCH is not NULL, attach
966 // the object file to the archive. This is called by the
967 // Add_symbols task after finding out the type of the file.
970 Incremental_inputs::report_object(Object
* obj
, unsigned int arg_serial
,
971 Library_base
* arch
, Script_info
* script_info
)
973 Stringpool::Key filename_key
;
974 Timespec mtime
= obj
->get_mtime();
976 // For a file loaded from a script, don't record its argument serial number.
977 if (script_info
!= NULL
)
980 this->strtab_
->add(obj
->name().c_str(), false, &filename_key
);
981 Incremental_object_entry
* obj_entry
=
982 new Incremental_object_entry(filename_key
, obj
, arg_serial
, mtime
);
983 if (obj
->is_in_system_directory())
984 obj_entry
->set_is_in_system_directory();
985 this->inputs_
.push_back(obj_entry
);
989 Incremental_archive_entry
* arch_entry
= arch
->incremental_info();
990 gold_assert(arch_entry
!= NULL
);
991 arch_entry
->add_object(obj_entry
);
994 if (script_info
!= NULL
)
996 Incremental_script_entry
* script_entry
= script_info
->incremental_info();
997 gold_assert(script_entry
!= NULL
);
998 script_entry
->add_object(obj_entry
);
1001 this->current_object_
= obj
;
1002 this->current_object_entry_
= obj_entry
;
1005 // Record the input object file OBJ. If ARCH is not NULL, attach
1006 // the object file to the archive. This is called by the
1007 // Add_symbols task after finding out the type of the file.
1010 Incremental_inputs::report_input_section(Object
* obj
, unsigned int shndx
,
1011 const char* name
, off_t sh_size
)
1013 Stringpool::Key key
= 0;
1016 this->strtab_
->add(name
, true, &key
);
1018 gold_assert(obj
== this->current_object_
);
1019 this->current_object_entry_
->add_input_section(shndx
, key
, sh_size
);
1022 // Record that the input argument INPUT is a script SCRIPT. This is
1023 // called by read_script after parsing the script and reading the list
1024 // of inputs added by this script.
1027 Incremental_inputs::report_script(Script_info
* script
,
1028 unsigned int arg_serial
,
1031 Stringpool::Key filename_key
;
1033 this->strtab_
->add(script
->filename().c_str(), false, &filename_key
);
1034 Incremental_script_entry
* entry
=
1035 new Incremental_script_entry(filename_key
, arg_serial
, script
, mtime
);
1036 this->inputs_
.push_back(entry
);
1037 script
->set_incremental_info(entry
);
1040 // Finalize the incremental link information. Called from
1041 // Layout::finalize.
1044 Incremental_inputs::finalize()
1046 // Finalize the string table.
1047 this->strtab_
->set_string_offsets();
1050 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
1053 Incremental_inputs::create_data_sections(Symbol_table
* symtab
)
1055 switch (parameters
->size_and_endianness())
1057 #ifdef HAVE_TARGET_32_LITTLE
1058 case Parameters::TARGET_32_LITTLE
:
1059 this->inputs_section_
=
1060 new Output_section_incremental_inputs
<32, false>(this, symtab
);
1063 #ifdef HAVE_TARGET_32_BIG
1064 case Parameters::TARGET_32_BIG
:
1065 this->inputs_section_
=
1066 new Output_section_incremental_inputs
<32, true>(this, symtab
);
1069 #ifdef HAVE_TARGET_64_LITTLE
1070 case Parameters::TARGET_64_LITTLE
:
1071 this->inputs_section_
=
1072 new Output_section_incremental_inputs
<64, false>(this, symtab
);
1075 #ifdef HAVE_TARGET_64_BIG
1076 case Parameters::TARGET_64_BIG
:
1077 this->inputs_section_
=
1078 new Output_section_incremental_inputs
<64, true>(this, symtab
);
1084 this->symtab_section_
= new Output_data_space(4, "** incremental_symtab");
1085 this->relocs_section_
= new Output_data_space(4, "** incremental_relocs");
1086 this->got_plt_section_
= new Output_data_space(4, "** incremental_got_plt");
1089 // Return the sh_entsize value for the .gnu_incremental_relocs section.
1091 Incremental_inputs::relocs_entsize() const
1093 return 8 + 2 * parameters
->target().get_size() / 8;
1096 // Class Output_section_incremental_inputs.
1098 // Finalize the offsets for each input section and supplemental info block,
1099 // and set the final data size of the incremental output sections.
1101 template<int size
, bool big_endian
>
1103 Output_section_incremental_inputs
<size
, big_endian
>::set_final_data_size()
1105 const Incremental_inputs
* inputs
= this->inputs_
;
1106 const unsigned int sizeof_addr
= size
/ 8;
1107 const unsigned int rel_size
= 8 + 2 * sizeof_addr
;
1109 // Offset of each input entry.
1110 unsigned int input_offset
= this->header_size
;
1112 // Offset of each supplemental info block.
1113 unsigned int file_index
= 0;
1114 unsigned int info_offset
= this->header_size
;
1115 info_offset
+= this->input_entry_size
* inputs
->input_file_count();
1117 // Count each input file and its supplemental information block.
1118 for (Incremental_inputs::Input_list::const_iterator p
=
1119 inputs
->input_files().begin();
1120 p
!= inputs
->input_files().end();
1123 // Set the index and offset of the input file entry.
1124 (*p
)->set_offset(file_index
, input_offset
);
1126 input_offset
+= this->input_entry_size
;
1128 // Set the offset of the supplemental info block.
1129 switch ((*p
)->type())
1131 case INCREMENTAL_INPUT_SCRIPT
:
1133 Incremental_script_entry
*entry
= (*p
)->script_entry();
1134 gold_assert(entry
!= NULL
);
1135 (*p
)->set_info_offset(info_offset
);
1139 info_offset
+= (entry
->get_object_count() * 4);
1142 case INCREMENTAL_INPUT_OBJECT
:
1143 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
1145 Incremental_object_entry
* entry
= (*p
)->object_entry();
1146 gold_assert(entry
!= NULL
);
1147 (*p
)->set_info_offset(info_offset
);
1148 // Input section count, global symbol count, local symbol offset,
1149 // local symbol count.
1151 // Each input section.
1152 info_offset
+= (entry
->get_input_section_count()
1153 * (8 + 2 * sizeof_addr
));
1154 // Each global symbol.
1155 const Object::Symbols
* syms
= entry
->object()->get_global_symbols();
1156 info_offset
+= syms
->size() * 20;
1159 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
1161 Incremental_object_entry
* entry
= (*p
)->object_entry();
1162 gold_assert(entry
!= NULL
);
1163 (*p
)->set_info_offset(info_offset
);
1164 // Global symbol count.
1166 // Each global symbol.
1167 const Object::Symbols
* syms
= entry
->object()->get_global_symbols();
1168 gold_assert(syms
!= NULL
);
1169 unsigned int nsyms
= syms
->size();
1170 unsigned int nsyms_out
= 0;
1171 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1173 const Symbol
* sym
= (*syms
)[i
];
1176 if (sym
->is_forwarder())
1177 sym
= this->symtab_
->resolve_forwards(sym
);
1178 if (sym
->symtab_index() != -1U)
1181 info_offset
+= nsyms_out
* 4;
1184 case INCREMENTAL_INPUT_ARCHIVE
:
1186 Incremental_archive_entry
* entry
= (*p
)->archive_entry();
1187 gold_assert(entry
!= NULL
);
1188 (*p
)->set_info_offset(info_offset
);
1189 // Member count + unused global symbol count.
1192 info_offset
+= (entry
->get_member_count() * 4);
1193 // Each global symbol.
1194 info_offset
+= (entry
->get_unused_global_symbol_count() * 4);
1202 this->set_data_size(info_offset
);
1204 // Set the size of the .gnu_incremental_symtab section.
1205 inputs
->symtab_section()->set_current_data_size(this->symtab_
->output_count()
1206 * sizeof(unsigned int));
1208 // Set the size of the .gnu_incremental_relocs section.
1209 inputs
->relocs_section()->set_current_data_size(inputs
->get_reloc_count()
1212 // Set the size of the .gnu_incremental_got_plt section.
1213 Sized_target
<size
, big_endian
>* target
=
1214 parameters
->sized_target
<size
, big_endian
>();
1215 unsigned int got_count
= target
->got_entry_count();
1216 unsigned int plt_count
= target
->plt_entry_count();
1217 unsigned int got_plt_size
= 8; // GOT entry count, PLT entry count.
1218 got_plt_size
= (got_plt_size
+ got_count
+ 3) & ~3; // GOT type array.
1219 got_plt_size
+= got_count
* 4 + plt_count
* 4; // GOT array, PLT array.
1220 inputs
->got_plt_section()->set_current_data_size(got_plt_size
);
1223 // Write the contents of the .gnu_incremental_inputs and
1224 // .gnu_incremental_symtab sections.
1226 template<int size
, bool big_endian
>
1228 Output_section_incremental_inputs
<size
, big_endian
>::do_write(Output_file
* of
)
1230 const Incremental_inputs
* inputs
= this->inputs_
;
1231 Stringpool
* strtab
= inputs
->get_stringpool();
1233 // Get a view into the .gnu_incremental_inputs section.
1234 const off_t off
= this->offset();
1235 const off_t oview_size
= this->data_size();
1236 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1237 unsigned char* pov
= oview
;
1239 // Get a view into the .gnu_incremental_symtab section.
1240 const off_t symtab_off
= inputs
->symtab_section()->offset();
1241 const off_t symtab_size
= inputs
->symtab_section()->data_size();
1242 unsigned char* const symtab_view
= of
->get_output_view(symtab_off
,
1245 // Allocate an array of linked list heads for the .gnu_incremental_symtab
1246 // section. Each element corresponds to a global symbol in the output
1247 // symbol table, and points to the head of the linked list that threads
1248 // through the object file input entries. The value of each element
1249 // is the section-relative offset to a global symbol entry in a
1250 // supplemental information block.
1251 unsigned int global_sym_count
= this->symtab_
->output_count();
1252 unsigned int* global_syms
= new unsigned int[global_sym_count
];
1253 memset(global_syms
, 0, global_sym_count
* sizeof(unsigned int));
1255 // Write the section header.
1256 Stringpool::Key command_line_key
= inputs
->command_line_key();
1257 pov
= this->write_header(pov
, inputs
->input_file_count(),
1258 strtab
->get_offset_from_key(command_line_key
));
1260 // Write the list of input files.
1261 pov
= this->write_input_files(oview
, pov
, strtab
);
1263 // Write the supplemental information blocks for each input file.
1264 pov
= this->write_info_blocks(oview
, pov
, strtab
, global_syms
,
1267 gold_assert(pov
- oview
== oview_size
);
1269 // Write the .gnu_incremental_symtab section.
1270 gold_assert(global_sym_count
* 4 == symtab_size
);
1271 this->write_symtab(symtab_view
, global_syms
, global_sym_count
);
1273 delete[] global_syms
;
1275 // Write the .gnu_incremental_got_plt section.
1276 const off_t got_plt_off
= inputs
->got_plt_section()->offset();
1277 const off_t got_plt_size
= inputs
->got_plt_section()->data_size();
1278 unsigned char* const got_plt_view
= of
->get_output_view(got_plt_off
,
1280 this->write_got_plt(got_plt_view
, got_plt_size
);
1282 of
->write_output_view(off
, oview_size
, oview
);
1283 of
->write_output_view(symtab_off
, symtab_size
, symtab_view
);
1284 of
->write_output_view(got_plt_off
, got_plt_size
, got_plt_view
);
1287 // Write the section header: version, input file count, offset of command line
1288 // in the string table, and 4 bytes of padding.
1290 template<int size
, bool big_endian
>
1292 Output_section_incremental_inputs
<size
, big_endian
>::write_header(
1294 unsigned int input_file_count
,
1295 section_offset_type command_line_offset
)
1297 Swap32::writeval(pov
, INCREMENTAL_LINK_VERSION
);
1298 Swap32::writeval(pov
+ 4, input_file_count
);
1299 Swap32::writeval(pov
+ 8, command_line_offset
);
1300 Swap32::writeval(pov
+ 12, 0);
1301 return pov
+ this->header_size
;
1304 // Write the input file entries.
1306 template<int size
, bool big_endian
>
1308 Output_section_incremental_inputs
<size
, big_endian
>::write_input_files(
1309 unsigned char* oview
,
1313 const Incremental_inputs
* inputs
= this->inputs_
;
1315 for (Incremental_inputs::Input_list::const_iterator p
=
1316 inputs
->input_files().begin();
1317 p
!= inputs
->input_files().end();
1320 gold_assert(static_cast<unsigned int>(pov
- oview
) == (*p
)->get_offset());
1321 section_offset_type filename_offset
=
1322 strtab
->get_offset_from_key((*p
)->get_filename_key());
1323 const Timespec
& mtime
= (*p
)->get_mtime();
1324 unsigned int flags
= (*p
)->type();
1325 if ((*p
)->is_in_system_directory())
1326 flags
|= INCREMENTAL_INPUT_IN_SYSTEM_DIR
;
1327 Swap32::writeval(pov
, filename_offset
);
1328 Swap32::writeval(pov
+ 4, (*p
)->get_info_offset());
1329 Swap64::writeval(pov
+ 8, mtime
.seconds
);
1330 Swap32::writeval(pov
+ 16, mtime
.nanoseconds
);
1331 Swap16::writeval(pov
+ 20, flags
);
1332 Swap16::writeval(pov
+ 22, (*p
)->arg_serial());
1333 pov
+= this->input_entry_size
;
1338 // Write the supplemental information blocks.
1340 template<int size
, bool big_endian
>
1342 Output_section_incremental_inputs
<size
, big_endian
>::write_info_blocks(
1343 unsigned char* oview
,
1346 unsigned int* global_syms
,
1347 unsigned int global_sym_count
)
1349 const Incremental_inputs
* inputs
= this->inputs_
;
1350 unsigned int first_global_index
= this->symtab_
->first_global_index();
1352 for (Incremental_inputs::Input_list::const_iterator p
=
1353 inputs
->input_files().begin();
1354 p
!= inputs
->input_files().end();
1357 switch ((*p
)->type())
1359 case INCREMENTAL_INPUT_SCRIPT
:
1361 gold_assert(static_cast<unsigned int>(pov
- oview
)
1362 == (*p
)->get_info_offset());
1363 Incremental_script_entry
* entry
= (*p
)->script_entry();
1364 gold_assert(entry
!= NULL
);
1366 // Write the object count.
1367 unsigned int nobjects
= entry
->get_object_count();
1368 Swap32::writeval(pov
, nobjects
);
1371 // For each object, write the offset to its input file entry.
1372 for (unsigned int i
= 0; i
< nobjects
; ++i
)
1374 Incremental_input_entry
* obj
= entry
->get_object(i
);
1375 Swap32::writeval(pov
, obj
->get_offset());
1381 case INCREMENTAL_INPUT_OBJECT
:
1382 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
1384 gold_assert(static_cast<unsigned int>(pov
- oview
)
1385 == (*p
)->get_info_offset());
1386 Incremental_object_entry
* entry
= (*p
)->object_entry();
1387 gold_assert(entry
!= NULL
);
1388 const Object
* obj
= entry
->object();
1389 const Relobj
* relobj
= static_cast<const Relobj
*>(obj
);
1390 const Object::Symbols
* syms
= obj
->get_global_symbols();
1391 // Write the input section count and global symbol count.
1392 unsigned int nsections
= entry
->get_input_section_count();
1393 unsigned int nsyms
= syms
->size();
1394 off_t locals_offset
= relobj
->local_symbol_offset();
1395 unsigned int nlocals
= relobj
->output_local_symbol_count();
1396 Swap32::writeval(pov
, nsections
);
1397 Swap32::writeval(pov
+ 4, nsyms
);
1398 Swap32::writeval(pov
+ 8, static_cast<unsigned int>(locals_offset
));
1399 Swap32::writeval(pov
+ 12, nlocals
);
1402 // Build a temporary array to map input section indexes
1403 // from the original object file index to the index in the
1404 // incremental info table.
1405 unsigned int* index_map
= new unsigned int[obj
->shnum()];
1406 memset(index_map
, 0, obj
->shnum() * sizeof(unsigned int));
1408 // For each input section, write the name, output section index,
1409 // offset within output section, and input section size.
1410 for (unsigned int i
= 0; i
< nsections
; i
++)
1412 unsigned int shndx
= entry
->get_input_section_index(i
);
1413 index_map
[shndx
] = i
+ 1;
1414 Stringpool::Key key
= entry
->get_input_section_name_key(i
);
1415 off_t name_offset
= 0;
1417 name_offset
= strtab
->get_offset_from_key(key
);
1419 off_t out_offset
= 0;
1421 Output_section
* os
= obj
->output_section(shndx
);
1424 out_shndx
= os
->out_shndx();
1425 out_offset
= obj
->output_section_offset(shndx
);
1426 sh_size
= entry
->get_input_section_size(i
);
1428 Swap32::writeval(pov
, name_offset
);
1429 Swap32::writeval(pov
+ 4, out_shndx
);
1430 Swap::writeval(pov
+ 8, out_offset
);
1431 Swap::writeval(pov
+ 8 + sizeof_addr
, sh_size
);
1432 pov
+= 8 + 2 * sizeof_addr
;
1435 // For each global symbol, write its associated relocations,
1436 // add it to the linked list of globals, then write the
1437 // supplemental information: global symbol table index,
1438 // input section index, linked list chain pointer, relocation
1439 // count, and offset to the relocations.
1440 for (unsigned int i
= 0; i
< nsyms
; i
++)
1442 const Symbol
* sym
= (*syms
)[i
];
1443 if (sym
->is_forwarder())
1444 sym
= this->symtab_
->resolve_forwards(sym
);
1445 unsigned int shndx
= 0;
1446 if (sym
->source() == Symbol::FROM_OBJECT
1447 && sym
->object() == obj
1448 && sym
->is_defined())
1451 unsigned int orig_shndx
= sym
->shndx(&is_ordinary
);
1453 shndx
= index_map
[orig_shndx
];
1455 unsigned int symtab_index
= sym
->symtab_index();
1456 unsigned int chain
= 0;
1457 unsigned int first_reloc
= 0;
1458 unsigned int nrelocs
= obj
->get_incremental_reloc_count(i
);
1461 gold_assert(symtab_index
!= -1U
1462 && (symtab_index
- first_global_index
1463 < global_sym_count
));
1464 first_reloc
= obj
->get_incremental_reloc_base(i
);
1465 chain
= global_syms
[symtab_index
- first_global_index
];
1466 global_syms
[symtab_index
- first_global_index
] =
1469 Swap32::writeval(pov
, symtab_index
);
1470 Swap32::writeval(pov
+ 4, shndx
);
1471 Swap32::writeval(pov
+ 8, chain
);
1472 Swap32::writeval(pov
+ 12, nrelocs
);
1473 Swap32::writeval(pov
+ 16, first_reloc
* 3 * sizeof_addr
);
1481 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
1483 gold_assert(static_cast<unsigned int>(pov
- oview
)
1484 == (*p
)->get_info_offset());
1485 Incremental_object_entry
* entry
= (*p
)->object_entry();
1486 gold_assert(entry
!= NULL
);
1487 const Object
* obj
= entry
->object();
1488 const Object::Symbols
* syms
= obj
->get_global_symbols();
1490 // Skip the global symbol count for now.
1491 unsigned char* orig_pov
= pov
;
1494 // For each global symbol, write the global symbol table index.
1495 unsigned int nsyms
= syms
->size();
1496 unsigned int nsyms_out
= 0;
1497 for (unsigned int i
= 0; i
< nsyms
; i
++)
1499 const Symbol
* sym
= (*syms
)[i
];
1502 if (sym
->is_forwarder())
1503 sym
= this->symtab_
->resolve_forwards(sym
);
1504 if (sym
->symtab_index() == -1U)
1506 unsigned int def_flag
= 0;
1507 if (sym
->source() == Symbol::FROM_OBJECT
1508 && sym
->object() == obj
1509 && sym
->is_defined())
1510 def_flag
= 1U << 31;
1511 Swap32::writeval(pov
, sym
->symtab_index() | def_flag
);
1516 // Now write the global symbol count.
1517 Swap32::writeval(orig_pov
, nsyms_out
);
1521 case INCREMENTAL_INPUT_ARCHIVE
:
1523 gold_assert(static_cast<unsigned int>(pov
- oview
)
1524 == (*p
)->get_info_offset());
1525 Incremental_archive_entry
* entry
= (*p
)->archive_entry();
1526 gold_assert(entry
!= NULL
);
1528 // Write the member count and unused global symbol count.
1529 unsigned int nmembers
= entry
->get_member_count();
1530 unsigned int nsyms
= entry
->get_unused_global_symbol_count();
1531 Swap32::writeval(pov
, nmembers
);
1532 Swap32::writeval(pov
+ 4, nsyms
);
1535 // For each member, write the offset to its input file entry.
1536 for (unsigned int i
= 0; i
< nmembers
; ++i
)
1538 Incremental_object_entry
* member
= entry
->get_member(i
);
1539 Swap32::writeval(pov
, member
->get_offset());
1543 // For each global symbol, write the name offset.
1544 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1546 Stringpool::Key key
= entry
->get_unused_global_symbol(i
);
1547 Swap32::writeval(pov
, strtab
->get_offset_from_key(key
));
1560 // Write the contents of the .gnu_incremental_symtab section.
1562 template<int size
, bool big_endian
>
1564 Output_section_incremental_inputs
<size
, big_endian
>::write_symtab(
1566 unsigned int* global_syms
,
1567 unsigned int global_sym_count
)
1569 for (unsigned int i
= 0; i
< global_sym_count
; ++i
)
1571 Swap32::writeval(pov
, global_syms
[i
]);
1576 // This struct holds the view information needed to write the
1577 // .gnu_incremental_got_plt section.
1579 struct Got_plt_view_info
1581 // Start of the GOT type array in the output view.
1582 unsigned char* got_type_p
;
1583 // Start of the GOT descriptor array in the output view.
1584 unsigned char* got_desc_p
;
1585 // Start of the PLT descriptor array in the output view.
1586 unsigned char* plt_desc_p
;
1587 // Number of GOT entries.
1588 unsigned int got_count
;
1589 // Number of PLT entries.
1590 unsigned int plt_count
;
1591 // Offset of the first non-reserved PLT entry (this is a target-dependent value).
1592 unsigned int first_plt_entry_offset
;
1593 // Size of a PLT entry (this is a target-dependent value).
1594 unsigned int plt_entry_size
;
1595 // Value to write in the GOT descriptor array. For global symbols,
1596 // this is the global symbol table index; for local symbols, it is
1597 // the offset of the input file entry in the .gnu_incremental_inputs
1599 unsigned int got_descriptor
;
1602 // Functor class for processing a GOT offset list for local symbols.
1603 // Writes the GOT type and symbol index into the GOT type and descriptor
1604 // arrays in the output section.
1606 template<int size
, bool big_endian
>
1607 class Local_got_offset_visitor
: public Got_offset_list::Visitor
1610 Local_got_offset_visitor(struct Got_plt_view_info
& info
)
1615 visit(unsigned int got_type
, unsigned int got_offset
)
1617 unsigned int got_index
= got_offset
/ this->got_entry_size_
;
1618 gold_assert(got_index
< this->info_
.got_count
);
1619 // We can only handle GOT entry types in the range 0..0x7e
1620 // because we use a byte array to store them, and we use the
1621 // high bit to flag a local symbol.
1622 gold_assert(got_type
< 0x7f);
1623 this->info_
.got_type_p
[got_index
] = got_type
| 0x80;
1624 unsigned char* pov
= this->info_
.got_desc_p
+ got_index
* 4;
1625 elfcpp::Swap
<32, big_endian
>::writeval(pov
, this->info_
.got_descriptor
);
1629 static const unsigned int got_entry_size_
= size
/ 8;
1630 struct Got_plt_view_info
& info_
;
1633 // Functor class for processing a GOT offset list. Writes the GOT type
1634 // and symbol index into the GOT type and descriptor arrays in the output
1637 template<int size
, bool big_endian
>
1638 class Global_got_offset_visitor
: public Got_offset_list::Visitor
1641 Global_got_offset_visitor(struct Got_plt_view_info
& info
)
1646 visit(unsigned int got_type
, unsigned int got_offset
)
1648 unsigned int got_index
= got_offset
/ this->got_entry_size_
;
1649 gold_assert(got_index
< this->info_
.got_count
);
1650 // We can only handle GOT entry types in the range 0..0x7e
1651 // because we use a byte array to store them, and we use the
1652 // high bit to flag a local symbol.
1653 gold_assert(got_type
< 0x7f);
1654 this->info_
.got_type_p
[got_index
] = got_type
;
1655 unsigned char* pov
= this->info_
.got_desc_p
+ got_index
* 4;
1656 elfcpp::Swap
<32, big_endian
>::writeval(pov
, this->info_
.got_descriptor
);
1660 static const unsigned int got_entry_size_
= size
/ 8;
1661 struct Got_plt_view_info
& info_
;
1664 // Functor class for processing the global symbol table. Processes the
1665 // GOT offset list for the symbol, and writes the symbol table index
1666 // into the PLT descriptor array in the output section.
1668 template<int size
, bool big_endian
>
1669 class Global_symbol_visitor_got_plt
1672 Global_symbol_visitor_got_plt(struct Got_plt_view_info
& info
)
1677 operator()(const Sized_symbol
<size
>* sym
)
1679 typedef Global_got_offset_visitor
<size
, big_endian
> Got_visitor
;
1680 const Got_offset_list
* got_offsets
= sym
->got_offset_list();
1681 if (got_offsets
!= NULL
)
1683 this->info_
.got_descriptor
= sym
->symtab_index();
1684 Got_visitor
v(this->info_
);
1685 got_offsets
->for_all_got_offsets(&v
);
1687 if (sym
->has_plt_offset())
1689 unsigned int plt_index
=
1690 ((sym
->plt_offset() - this->info_
.first_plt_entry_offset
)
1691 / this->info_
.plt_entry_size
);
1692 gold_assert(plt_index
< this->info_
.plt_count
);
1693 unsigned char* pov
= this->info_
.plt_desc_p
+ plt_index
* 4;
1694 elfcpp::Swap
<32, big_endian
>::writeval(pov
, sym
->symtab_index());
1699 struct Got_plt_view_info
& info_
;
1702 // Write the contents of the .gnu_incremental_got_plt section.
1704 template<int size
, bool big_endian
>
1706 Output_section_incremental_inputs
<size
, big_endian
>::write_got_plt(
1710 Sized_target
<size
, big_endian
>* target
=
1711 parameters
->sized_target
<size
, big_endian
>();
1713 // Set up the view information for the functors.
1714 struct Got_plt_view_info view_info
;
1715 view_info
.got_count
= target
->got_entry_count();
1716 view_info
.plt_count
= target
->plt_entry_count();
1717 view_info
.first_plt_entry_offset
= target
->first_plt_entry_offset();
1718 view_info
.plt_entry_size
= target
->plt_entry_size();
1719 view_info
.got_type_p
= pov
+ 8;
1720 view_info
.got_desc_p
= (view_info
.got_type_p
1721 + ((view_info
.got_count
+ 3) & ~3));
1722 view_info
.plt_desc_p
= view_info
.got_desc_p
+ view_info
.got_count
* 4;
1724 gold_assert(pov
+ view_size
==
1725 view_info
.plt_desc_p
+ view_info
.plt_count
* 4);
1727 // Write the section header.
1728 Swap32::writeval(pov
, view_info
.got_count
);
1729 Swap32::writeval(pov
+ 4, view_info
.plt_count
);
1731 // Initialize the GOT type array to 0xff (reserved).
1732 memset(view_info
.got_type_p
, 0xff, view_info
.got_count
);
1734 // Write the incremental GOT descriptors for local symbols.
1735 typedef Local_got_offset_visitor
<size
, big_endian
> Got_visitor
;
1736 for (Incremental_inputs::Input_list::const_iterator p
=
1737 this->inputs_
->input_files().begin();
1738 p
!= this->inputs_
->input_files().end();
1741 if ((*p
)->type() != INCREMENTAL_INPUT_OBJECT
1742 && (*p
)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
)
1744 Incremental_object_entry
* entry
= (*p
)->object_entry();
1745 gold_assert(entry
!= NULL
);
1746 const Object
* obj
= entry
->object();
1747 gold_assert(obj
!= NULL
);
1748 view_info
.got_descriptor
= (*p
)->get_file_index();
1749 Got_visitor
v(view_info
);
1750 obj
->for_all_local_got_entries(&v
);
1753 // Write the incremental GOT and PLT descriptors for global symbols.
1754 typedef Global_symbol_visitor_got_plt
<size
, big_endian
> Symbol_visitor
;
1755 symtab_
->for_all_symbols
<size
, Symbol_visitor
>(Symbol_visitor(view_info
));
1758 // Class Sized_incr_relobj. Most of these methods are not used for
1759 // Incremental objects, but are required to be implemented by the
1760 // base class Object.
1762 template<int size
, bool big_endian
>
1763 Sized_incr_relobj
<size
, big_endian
>::Sized_incr_relobj(
1764 const std::string
& name
,
1765 Sized_incremental_binary
<size
, big_endian
>* ibase
,
1766 unsigned int input_file_index
)
1767 : Sized_relobj_base
<size
, big_endian
>(name
, NULL
), ibase_(ibase
),
1768 input_file_index_(input_file_index
),
1769 input_reader_(ibase
->inputs_reader().input_file(input_file_index
)),
1770 local_symbol_count_(0), output_local_dynsym_count_(0),
1771 local_symbol_index_(0), local_symbol_offset_(0), local_dynsym_offset_(0),
1772 symbols_(), section_offsets_(), incr_reloc_offset_(-1U),
1773 incr_reloc_count_(0), incr_reloc_output_index_(0), incr_relocs_(NULL
),
1776 if (this->input_reader_
.is_in_system_directory())
1777 this->set_is_in_system_directory();
1778 const unsigned int shnum
= this->input_reader_
.get_input_section_count() + 1;
1779 this->set_shnum(shnum
);
1782 // Read the symbols.
1784 template<int size
, bool big_endian
>
1786 Sized_incr_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
1791 // Lay out the input sections.
1793 template<int size
, bool big_endian
>
1795 Sized_incr_relobj
<size
, big_endian
>::do_layout(
1800 const unsigned int shnum
= this->shnum();
1801 Incremental_inputs
* incremental_inputs
= layout
->incremental_inputs();
1802 gold_assert(incremental_inputs
!= NULL
);
1803 Output_sections
& out_sections(this->output_sections());
1804 out_sections
.resize(shnum
);
1805 this->section_offsets_
.resize(shnum
);
1806 for (unsigned int i
= 1; i
< shnum
; i
++)
1808 typename
Input_entry_reader::Input_section_info sect
=
1809 this->input_reader_
.get_input_section(i
- 1);
1810 // Add the section to the incremental inputs layout.
1811 incremental_inputs
->report_input_section(this, i
, sect
.name
,
1813 if (sect
.output_shndx
== 0 || sect
.sh_offset
== -1)
1815 Output_section
* os
= this->ibase_
->output_section(sect
.output_shndx
);
1816 gold_assert(os
!= NULL
);
1817 out_sections
[i
] = os
;
1818 this->section_offsets_
[i
] = static_cast<Address
>(sect
.sh_offset
);
1822 // Layout sections whose layout was deferred while waiting for
1823 // input files from a plugin.
1824 template<int size
, bool big_endian
>
1826 Sized_incr_relobj
<size
, big_endian
>::do_layout_deferred_sections(Layout
*)
1830 // Add the symbols to the symbol table.
1832 template<int size
, bool big_endian
>
1834 Sized_incr_relobj
<size
, big_endian
>::do_add_symbols(
1835 Symbol_table
* symtab
,
1839 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1840 unsigned char symbuf
[sym_size
];
1841 elfcpp::Sym
<size
, big_endian
> sym(symbuf
);
1842 elfcpp::Sym_write
<size
, big_endian
> osym(symbuf
);
1844 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Elf_size_type
;
1846 unsigned int nsyms
= this->input_reader_
.get_global_symbol_count();
1847 this->symbols_
.resize(nsyms
);
1849 Incremental_binary::View
symtab_view(NULL
);
1850 unsigned int symtab_count
;
1851 elfcpp::Elf_strtab
strtab(NULL
, 0);
1852 this->ibase_
->get_symtab_view(&symtab_view
, &symtab_count
, &strtab
);
1854 Incremental_symtab_reader
<big_endian
> isymtab(this->ibase_
->symtab_reader());
1855 unsigned int isym_count
= isymtab
.symbol_count();
1856 unsigned int first_global
= symtab_count
- isym_count
;
1858 const unsigned char* sym_p
;
1859 for (unsigned int i
= 0; i
< nsyms
; ++i
)
1861 Incremental_global_symbol_reader
<big_endian
> info
=
1862 this->input_reader_
.get_global_symbol_reader(i
);
1863 unsigned int output_symndx
= info
.output_symndx();
1864 sym_p
= symtab_view
.data() + output_symndx
* sym_size
;
1865 elfcpp::Sym
<size
, big_endian
> gsym(sym_p
);
1867 if (!strtab
.get_c_string(gsym
.get_st_name(), &name
))
1870 typename
elfcpp::Elf_types
<size
>::Elf_Addr v
= gsym
.get_st_value();
1871 unsigned int shndx
= gsym
.get_st_shndx();
1872 elfcpp::STB st_bind
= gsym
.get_st_bind();
1873 elfcpp::STT st_type
= gsym
.get_st_type();
1875 // Local hidden symbols start out as globals, but get converted to
1876 // to local during output.
1877 if (st_bind
== elfcpp::STB_LOCAL
)
1878 st_bind
= elfcpp::STB_GLOBAL
;
1880 unsigned int input_shndx
= info
.shndx();
1881 if (input_shndx
== 0)
1883 shndx
= elfcpp::SHN_UNDEF
;
1886 else if (shndx
!= elfcpp::SHN_ABS
)
1888 // Find the input section and calculate the section-relative value.
1889 gold_assert(shndx
!= elfcpp::SHN_UNDEF
);
1890 Output_section
* os
= this->ibase_
->output_section(shndx
);
1891 gold_assert(os
!= NULL
&& os
->has_fixed_layout());
1892 typename
Input_entry_reader::Input_section_info sect
=
1893 this->input_reader_
.get_input_section(input_shndx
- 1);
1894 gold_assert(sect
.output_shndx
== shndx
);
1895 if (st_type
!= elfcpp::STT_TLS
)
1897 v
-= sect
.sh_offset
;
1898 shndx
= input_shndx
;
1901 osym
.put_st_name(0);
1902 osym
.put_st_value(v
);
1903 osym
.put_st_size(gsym
.get_st_size());
1904 osym
.put_st_info(st_bind
, st_type
);
1905 osym
.put_st_other(gsym
.get_st_other());
1906 osym
.put_st_shndx(shndx
);
1909 symtab
->add_from_incrobj(this, name
, NULL
, &sym
);
1910 this->ibase_
->add_global_symbol(output_symndx
- first_global
,
1915 // Return TRUE if we should include this object from an archive library.
1917 template<int size
, bool big_endian
>
1918 Archive::Should_include
1919 Sized_incr_relobj
<size
, big_endian
>::do_should_include_member(
1928 // Iterate over global symbols, calling a visitor class V for each.
1930 template<int size
, bool big_endian
>
1932 Sized_incr_relobj
<size
, big_endian
>::do_for_all_global_symbols(
1934 Library_base::Symbol_visitor_base
*)
1936 // This routine is not used for incremental objects.
1939 // Iterate over local symbols, calling a visitor class V for each GOT offset
1940 // associated with a local symbol.
1942 template<int size
, bool big_endian
>
1944 Sized_incr_relobj
<size
, big_endian
>::do_for_all_local_got_entries(
1945 Got_offset_list::Visitor
*) const
1947 // FIXME: Implement Sized_incr_relobj::do_for_all_local_got_entries.
1950 // Get the size of a section.
1952 template<int size
, bool big_endian
>
1954 Sized_incr_relobj
<size
, big_endian
>::do_section_size(unsigned int)
1959 // Get the name of a section.
1961 template<int size
, bool big_endian
>
1963 Sized_incr_relobj
<size
, big_endian
>::do_section_name(unsigned int)
1968 // Return a view of the contents of a section.
1970 template<int size
, bool big_endian
>
1972 Sized_incr_relobj
<size
, big_endian
>::do_section_contents(unsigned int)
1977 // Return section flags.
1979 template<int size
, bool big_endian
>
1981 Sized_incr_relobj
<size
, big_endian
>::do_section_flags(unsigned int)
1986 // Return section entsize.
1988 template<int size
, bool big_endian
>
1990 Sized_incr_relobj
<size
, big_endian
>::do_section_entsize(unsigned int)
1995 // Return section address.
1997 template<int size
, bool big_endian
>
1999 Sized_incr_relobj
<size
, big_endian
>::do_section_address(unsigned int)
2004 // Return section type.
2006 template<int size
, bool big_endian
>
2008 Sized_incr_relobj
<size
, big_endian
>::do_section_type(unsigned int)
2013 // Return the section link field.
2015 template<int size
, bool big_endian
>
2017 Sized_incr_relobj
<size
, big_endian
>::do_section_link(unsigned int)
2022 // Return the section link field.
2024 template<int size
, bool big_endian
>
2026 Sized_incr_relobj
<size
, big_endian
>::do_section_info(unsigned int)
2031 // Return the section alignment.
2033 template<int size
, bool big_endian
>
2035 Sized_incr_relobj
<size
, big_endian
>::do_section_addralign(unsigned int)
2040 // Return the Xindex structure to use.
2042 template<int size
, bool big_endian
>
2044 Sized_incr_relobj
<size
, big_endian
>::do_initialize_xindex()
2049 // Get symbol counts.
2051 template<int size
, bool big_endian
>
2053 Sized_incr_relobj
<size
, big_endian
>::do_get_global_symbol_counts(
2054 const Symbol_table
*, size_t*, size_t*) const
2061 template<int size
, bool big_endian
>
2063 Sized_incr_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
*)
2067 // Process the relocs to find list of referenced sections. Used only
2068 // during garbage collection.
2070 template<int size
, bool big_endian
>
2072 Sized_incr_relobj
<size
, big_endian
>::do_gc_process_relocs(Symbol_table
*,
2079 // Scan the relocs and adjust the symbol table.
2081 template<int size
, bool big_endian
>
2083 Sized_incr_relobj
<size
, big_endian
>::do_scan_relocs(Symbol_table
*,
2087 // Count the incremental relocations for this object.
2088 unsigned int nsyms
= this->input_reader_
.get_global_symbol_count();
2089 this->allocate_incremental_reloc_counts();
2090 for (unsigned int i
= 0; i
< nsyms
; i
++)
2092 Incremental_global_symbol_reader
<big_endian
> sym
=
2093 this->input_reader_
.get_global_symbol_reader(i
);
2094 unsigned int reloc_count
= sym
.reloc_count();
2095 if (reloc_count
> 0 && this->incr_reloc_offset_
== -1U)
2096 this->incr_reloc_offset_
= sym
.reloc_offset();
2097 this->incr_reloc_count_
+= reloc_count
;
2098 for (unsigned int j
= 0; j
< reloc_count
; j
++)
2099 this->count_incremental_reloc(i
);
2101 this->incr_reloc_output_index_
=
2102 layout
->incremental_inputs()->get_reloc_count();
2103 this->finalize_incremental_relocs(layout
, false);
2105 // The incoming incremental relocations may not end up in the same
2106 // location after the incremental update, because the incremental info
2107 // is regenerated in each link. Because the new location may overlap
2108 // with other data in the updated output file, we need to copy the
2109 // relocations into a buffer so that we can still read them safely
2110 // after we start writing updates to the output file.
2111 if (this->incr_reloc_count_
> 0)
2113 const Incremental_relocs_reader
<size
, big_endian
>& relocs_reader
=
2114 this->ibase_
->relocs_reader();
2115 const unsigned int incr_reloc_size
= relocs_reader
.reloc_size
;
2116 unsigned int len
= this->incr_reloc_count_
* incr_reloc_size
;
2117 this->incr_relocs_
= new unsigned char[len
];
2118 memcpy(this->incr_relocs_
,
2119 relocs_reader
.data(this->incr_reloc_offset_
),
2124 // Count the local symbols.
2126 template<int size
, bool big_endian
>
2128 Sized_incr_relobj
<size
, big_endian
>::do_count_local_symbols(
2129 Stringpool_template
<char>* pool
,
2130 Stringpool_template
<char>*)
2132 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2134 // Set the count of local symbols based on the incremental info.
2135 unsigned int nlocals
= this->input_reader_
.get_local_symbol_count();
2136 this->local_symbol_count_
= nlocals
;
2137 this->local_symbols_
.reserve(nlocals
);
2139 // Get views of the base file's symbol table and string table.
2140 Incremental_binary::View
symtab_view(NULL
);
2141 unsigned int symtab_count
;
2142 elfcpp::Elf_strtab
strtab(NULL
, 0);
2143 this->ibase_
->get_symtab_view(&symtab_view
, &symtab_count
, &strtab
);
2145 // Read the local symbols from the base file's symbol table.
2146 off_t off
= this->input_reader_
.get_local_symbol_offset();
2147 const unsigned char* symp
= symtab_view
.data() + off
;
2148 for (unsigned int i
= 0; i
< nlocals
; ++i
, symp
+= sym_size
)
2150 elfcpp::Sym
<size
, big_endian
> sym(symp
);
2152 if (!strtab
.get_c_string(sym
.get_st_name(), &name
))
2154 gold_debug(DEBUG_INCREMENTAL
, "Local symbol %d: %s", i
, name
);
2155 name
= pool
->add(name
, true, NULL
);
2156 this->local_symbols_
.push_back(Local_symbol(name
,
2165 // Finalize the local symbols.
2167 template<int size
, bool big_endian
>
2169 Sized_incr_relobj
<size
, big_endian
>::do_finalize_local_symbols(
2174 this->local_symbol_index_
= index
;
2175 this->local_symbol_offset_
= off
;
2176 return index
+ this->local_symbol_count_
;
2179 // Set the offset where local dynamic symbol information will be stored.
2181 template<int size
, bool big_endian
>
2183 Sized_incr_relobj
<size
, big_endian
>::do_set_local_dynsym_indexes(
2186 // FIXME: set local dynsym indexes.
2190 // Set the offset where local dynamic symbol information will be stored.
2192 template<int size
, bool big_endian
>
2194 Sized_incr_relobj
<size
, big_endian
>::do_set_local_dynsym_offset(off_t
)
2199 // Relocate the input sections and write out the local symbols.
2200 // We don't actually do any relocation here. For unchanged input files,
2201 // we reapply relocations only for symbols that have changed; that happens
2202 // in queue_final_tasks. We do need to rewrite the incremental relocations
2205 template<int size
, bool big_endian
>
2207 Sized_incr_relobj
<size
, big_endian
>::do_relocate(const Symbol_table
*,
2208 const Layout
* layout
,
2211 if (this->incr_reloc_count_
== 0)
2214 const unsigned int incr_reloc_size
=
2215 Incremental_relocs_reader
<size
, big_endian
>::reloc_size
;
2217 // Get a view for the .gnu_incremental_relocs section.
2218 Incremental_inputs
* inputs
= layout
->incremental_inputs();
2219 gold_assert(inputs
!= NULL
);
2220 const off_t relocs_off
= inputs
->relocs_section()->offset();
2221 const off_t relocs_size
= inputs
->relocs_section()->data_size();
2222 unsigned char* const view
= of
->get_output_view(relocs_off
, relocs_size
);
2224 // Copy the relocations from the buffer.
2225 off_t off
= this->incr_reloc_output_index_
* incr_reloc_size
;
2226 unsigned int len
= this->incr_reloc_count_
* incr_reloc_size
;
2227 memcpy(view
+ off
, this->incr_relocs_
, len
);
2229 // The output section table may have changed, so we need to map
2230 // the old section index to the new section index for each relocation.
2231 for (unsigned int i
= 0; i
< this->incr_reloc_count_
; ++i
)
2233 unsigned char* pov
= view
+ off
+ i
* incr_reloc_size
;
2234 unsigned int shndx
= elfcpp::Swap
<32, big_endian
>::readval(pov
+ 4);
2235 Output_section
* os
= this->ibase_
->output_section(shndx
);
2236 gold_assert(os
!= NULL
);
2237 shndx
= os
->out_shndx();
2238 elfcpp::Swap
<32, big_endian
>::writeval(pov
+ 4, shndx
);
2241 of
->write_output_view(off
, len
, view
);
2243 // Get views into the output file for the portions of the symbol table
2244 // and the dynamic symbol table that we will be writing.
2245 off_t symtab_off
= layout
->symtab_section()->offset();
2246 off_t output_size
= this->local_symbol_count_
* This::sym_size
;
2247 unsigned char* oview
= NULL
;
2248 if (output_size
> 0)
2249 oview
= of
->get_output_view(symtab_off
+ this->local_symbol_offset_
,
2252 off_t dyn_output_size
= this->output_local_dynsym_count_
* sym_size
;
2253 unsigned char* dyn_oview
= NULL
;
2254 if (dyn_output_size
> 0)
2255 dyn_oview
= of
->get_output_view(this->local_dynsym_offset_
,
2258 // Write the local symbols.
2259 unsigned char* ov
= oview
;
2260 unsigned char* dyn_ov
= dyn_oview
;
2261 const Stringpool
* sympool
= layout
->sympool();
2262 const Stringpool
* dynpool
= layout
->dynpool();
2263 Output_symtab_xindex
* symtab_xindex
= layout
->symtab_xindex();
2264 Output_symtab_xindex
* dynsym_xindex
= layout
->dynsym_xindex();
2265 for (unsigned int i
= 0; i
< this->local_symbol_count_
; ++i
)
2267 Local_symbol
& lsym(this->local_symbols_
[i
]);
2270 unsigned int st_shndx
= this->adjust_sym_shndx(i
, lsym
.st_shndx
,
2274 Output_section
* os
= this->ibase_
->output_section(st_shndx
);
2275 st_shndx
= os
->out_shndx();
2276 if (st_shndx
>= elfcpp::SHN_LORESERVE
)
2278 symtab_xindex
->add(this->local_symbol_index_
+ i
, st_shndx
);
2279 if (lsym
.needs_dynsym_entry
)
2280 dynsym_xindex
->add(lsym
.output_dynsym_index
, st_shndx
);
2281 st_shndx
= elfcpp::SHN_XINDEX
;
2285 // Write the symbol to the output symbol table.
2287 elfcpp::Sym_write
<size
, big_endian
> osym(ov
);
2288 osym
.put_st_name(sympool
->get_offset(lsym
.name
));
2289 osym
.put_st_value(lsym
.st_value
);
2290 osym
.put_st_size(lsym
.st_size
);
2291 osym
.put_st_info(elfcpp::STB_LOCAL
,
2292 static_cast<elfcpp::STT
>(lsym
.st_type
));
2293 osym
.put_st_other(0);
2294 osym
.put_st_shndx(st_shndx
);
2298 // Write the symbol to the output dynamic symbol table.
2299 if (lsym
.needs_dynsym_entry
)
2301 gold_assert(dyn_ov
< dyn_oview
+ dyn_output_size
);
2302 elfcpp::Sym_write
<size
, big_endian
> osym(dyn_ov
);
2303 osym
.put_st_name(dynpool
->get_offset(lsym
.name
));
2304 osym
.put_st_value(lsym
.st_value
);
2305 osym
.put_st_size(lsym
.st_size
);
2306 osym
.put_st_info(elfcpp::STB_LOCAL
,
2307 static_cast<elfcpp::STT
>(lsym
.st_type
));
2308 osym
.put_st_other(0);
2309 osym
.put_st_shndx(st_shndx
);
2314 if (output_size
> 0)
2316 gold_assert(ov
- oview
== output_size
);
2317 of
->write_output_view(symtab_off
+ this->local_symbol_offset_
,
2318 output_size
, oview
);
2321 if (dyn_output_size
> 0)
2323 gold_assert(dyn_ov
- dyn_oview
== dyn_output_size
);
2324 of
->write_output_view(this->local_dynsym_offset_
, dyn_output_size
,
2329 // Set the offset of a section.
2331 template<int size
, bool big_endian
>
2333 Sized_incr_relobj
<size
, big_endian
>::do_set_section_offset(unsigned int,
2338 // Class Sized_incr_dynobj. Most of these methods are not used for
2339 // Incremental objects, but are required to be implemented by the
2340 // base class Object.
2342 template<int size
, bool big_endian
>
2343 Sized_incr_dynobj
<size
, big_endian
>::Sized_incr_dynobj(
2344 const std::string
& name
,
2345 Sized_incremental_binary
<size
, big_endian
>* ibase
,
2346 unsigned int input_file_index
)
2347 : Dynobj(name
, NULL
), ibase_(ibase
),
2348 input_file_index_(input_file_index
),
2349 input_reader_(ibase
->inputs_reader().input_file(input_file_index
)),
2352 if (this->input_reader_
.is_in_system_directory())
2353 this->set_is_in_system_directory();
2357 // Read the symbols.
2359 template<int size
, bool big_endian
>
2361 Sized_incr_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
2366 // Lay out the input sections.
2368 template<int size
, bool big_endian
>
2370 Sized_incr_dynobj
<size
, big_endian
>::do_layout(
2377 // Add the symbols to the symbol table.
2379 template<int size
, bool big_endian
>
2381 Sized_incr_dynobj
<size
, big_endian
>::do_add_symbols(
2382 Symbol_table
* symtab
,
2386 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2387 unsigned char symbuf
[sym_size
];
2388 elfcpp::Sym
<size
, big_endian
> sym(symbuf
);
2389 elfcpp::Sym_write
<size
, big_endian
> osym(symbuf
);
2391 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Elf_size_type
;
2393 unsigned int nsyms
= this->input_reader_
.get_global_symbol_count();
2394 this->symbols_
.resize(nsyms
);
2396 Incremental_binary::View
symtab_view(NULL
);
2397 unsigned int symtab_count
;
2398 elfcpp::Elf_strtab
strtab(NULL
, 0);
2399 this->ibase_
->get_symtab_view(&symtab_view
, &symtab_count
, &strtab
);
2401 Incremental_symtab_reader
<big_endian
> isymtab(this->ibase_
->symtab_reader());
2402 unsigned int isym_count
= isymtab
.symbol_count();
2403 unsigned int first_global
= symtab_count
- isym_count
;
2405 const unsigned char* sym_p
;
2406 for (unsigned int i
= 0; i
< nsyms
; ++i
)
2409 unsigned int output_symndx
=
2410 this->input_reader_
.get_output_symbol_index(i
, &is_def
);
2411 sym_p
= symtab_view
.data() + output_symndx
* sym_size
;
2412 elfcpp::Sym
<size
, big_endian
> gsym(sym_p
);
2414 if (!strtab
.get_c_string(gsym
.get_st_name(), &name
))
2417 typename
elfcpp::Elf_types
<size
>::Elf_Addr v
;
2419 elfcpp::STB st_bind
= gsym
.get_st_bind();
2420 elfcpp::STT st_type
= gsym
.get_st_type();
2422 // Local hidden symbols start out as globals, but get converted to
2423 // to local during output.
2424 if (st_bind
== elfcpp::STB_LOCAL
)
2425 st_bind
= elfcpp::STB_GLOBAL
;
2429 shndx
= elfcpp::SHN_UNDEF
;
2434 // For a symbol defined in a shared object, the section index
2435 // is meaningless, as long as it's not SHN_UNDEF.
2437 v
= gsym
.get_st_value();
2440 osym
.put_st_name(0);
2441 osym
.put_st_value(v
);
2442 osym
.put_st_size(gsym
.get_st_size());
2443 osym
.put_st_info(st_bind
, st_type
);
2444 osym
.put_st_other(gsym
.get_st_other());
2445 osym
.put_st_shndx(shndx
);
2448 symtab
->add_from_incrobj
<size
, big_endian
>(this, name
, NULL
, &sym
);
2449 this->ibase_
->add_global_symbol(output_symndx
- first_global
,
2454 // Return TRUE if we should include this object from an archive library.
2456 template<int size
, bool big_endian
>
2457 Archive::Should_include
2458 Sized_incr_dynobj
<size
, big_endian
>::do_should_include_member(
2467 // Iterate over global symbols, calling a visitor class V for each.
2469 template<int size
, bool big_endian
>
2471 Sized_incr_dynobj
<size
, big_endian
>::do_for_all_global_symbols(
2473 Library_base::Symbol_visitor_base
*)
2475 // This routine is not used for dynamic libraries.
2478 // Iterate over local symbols, calling a visitor class V for each GOT offset
2479 // associated with a local symbol.
2481 template<int size
, bool big_endian
>
2483 Sized_incr_dynobj
<size
, big_endian
>::do_for_all_local_got_entries(
2484 Got_offset_list::Visitor
*) const
2488 // Get the size of a section.
2490 template<int size
, bool big_endian
>
2492 Sized_incr_dynobj
<size
, big_endian
>::do_section_size(unsigned int)
2497 // Get the name of a section.
2499 template<int size
, bool big_endian
>
2501 Sized_incr_dynobj
<size
, big_endian
>::do_section_name(unsigned int)
2506 // Return a view of the contents of a section.
2508 template<int size
, bool big_endian
>
2510 Sized_incr_dynobj
<size
, big_endian
>::do_section_contents(unsigned int)
2515 // Return section flags.
2517 template<int size
, bool big_endian
>
2519 Sized_incr_dynobj
<size
, big_endian
>::do_section_flags(unsigned int)
2524 // Return section entsize.
2526 template<int size
, bool big_endian
>
2528 Sized_incr_dynobj
<size
, big_endian
>::do_section_entsize(unsigned int)
2533 // Return section address.
2535 template<int size
, bool big_endian
>
2537 Sized_incr_dynobj
<size
, big_endian
>::do_section_address(unsigned int)
2542 // Return section type.
2544 template<int size
, bool big_endian
>
2546 Sized_incr_dynobj
<size
, big_endian
>::do_section_type(unsigned int)
2551 // Return the section link field.
2553 template<int size
, bool big_endian
>
2555 Sized_incr_dynobj
<size
, big_endian
>::do_section_link(unsigned int)
2560 // Return the section link field.
2562 template<int size
, bool big_endian
>
2564 Sized_incr_dynobj
<size
, big_endian
>::do_section_info(unsigned int)
2569 // Return the section alignment.
2571 template<int size
, bool big_endian
>
2573 Sized_incr_dynobj
<size
, big_endian
>::do_section_addralign(unsigned int)
2578 // Return the Xindex structure to use.
2580 template<int size
, bool big_endian
>
2582 Sized_incr_dynobj
<size
, big_endian
>::do_initialize_xindex()
2587 // Get symbol counts.
2589 template<int size
, bool big_endian
>
2591 Sized_incr_dynobj
<size
, big_endian
>::do_get_global_symbol_counts(
2592 const Symbol_table
*, size_t*, size_t*) const
2597 // Allocate an incremental object of the appropriate size and endianness.
2600 make_sized_incremental_object(
2601 Incremental_binary
* ibase
,
2602 unsigned int input_file_index
,
2603 Incremental_input_type input_type
,
2604 const Incremental_binary::Input_reader
* input_reader
)
2607 std::string
name(input_reader
->filename());
2609 switch (parameters
->size_and_endianness())
2611 #ifdef HAVE_TARGET_32_LITTLE
2612 case Parameters::TARGET_32_LITTLE
:
2614 Sized_incremental_binary
<32, false>* sized_ibase
=
2615 static_cast<Sized_incremental_binary
<32, false>*>(ibase
);
2616 if (input_type
== INCREMENTAL_INPUT_SHARED_LIBRARY
)
2617 obj
= new Sized_incr_dynobj
<32, false>(name
, sized_ibase
,
2620 obj
= new Sized_incr_relobj
<32, false>(name
, sized_ibase
,
2625 #ifdef HAVE_TARGET_32_BIG
2626 case Parameters::TARGET_32_BIG
:
2628 Sized_incremental_binary
<32, true>* sized_ibase
=
2629 static_cast<Sized_incremental_binary
<32, true>*>(ibase
);
2630 if (input_type
== INCREMENTAL_INPUT_SHARED_LIBRARY
)
2631 obj
= new Sized_incr_dynobj
<32, true>(name
, sized_ibase
,
2634 obj
= new Sized_incr_relobj
<32, true>(name
, sized_ibase
,
2639 #ifdef HAVE_TARGET_64_LITTLE
2640 case Parameters::TARGET_64_LITTLE
:
2642 Sized_incremental_binary
<64, false>* sized_ibase
=
2643 static_cast<Sized_incremental_binary
<64, false>*>(ibase
);
2644 if (input_type
== INCREMENTAL_INPUT_SHARED_LIBRARY
)
2645 obj
= new Sized_incr_dynobj
<64, false>(name
, sized_ibase
,
2648 obj
= new Sized_incr_relobj
<64, false>(name
, sized_ibase
,
2653 #ifdef HAVE_TARGET_64_BIG
2654 case Parameters::TARGET_64_BIG
:
2656 Sized_incremental_binary
<64, true>* sized_ibase
=
2657 static_cast<Sized_incremental_binary
<64, true>*>(ibase
);
2658 if (input_type
== INCREMENTAL_INPUT_SHARED_LIBRARY
)
2659 obj
= new Sized_incr_dynobj
<64, true>(name
, sized_ibase
,
2662 obj
= new Sized_incr_relobj
<64, true>(name
, sized_ibase
,
2671 gold_assert(obj
!= NULL
);
2675 // Copy the unused symbols from the incremental input info.
2676 // We need to do this because we may be overwriting the incremental
2677 // input info in the base file before we write the new incremental
2680 Incremental_library::copy_unused_symbols()
2682 unsigned int symcount
= this->input_reader_
->get_unused_symbol_count();
2683 this->unused_symbols_
.reserve(symcount
);
2684 for (unsigned int i
= 0; i
< symcount
; ++i
)
2686 std::string
name(this->input_reader_
->get_unused_symbol(i
));
2687 this->unused_symbols_
.push_back(name
);
2691 // Iterator for unused global symbols in the library.
2693 Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base
* v
) const
2695 for (Symbol_list::const_iterator p
= this->unused_symbols_
.begin();
2696 p
!= this->unused_symbols_
.end();
2698 v
->visit(p
->c_str());
2701 // Instantiate the templates we need.
2703 #ifdef HAVE_TARGET_32_LITTLE
2705 class Sized_incremental_binary
<32, false>;
2708 class Sized_incr_relobj
<32, false>;
2711 class Sized_incr_dynobj
<32, false>;
2714 #ifdef HAVE_TARGET_32_BIG
2716 class Sized_incremental_binary
<32, true>;
2719 class Sized_incr_relobj
<32, true>;
2722 class Sized_incr_dynobj
<32, true>;
2725 #ifdef HAVE_TARGET_64_LITTLE
2727 class Sized_incremental_binary
<64, false>;
2730 class Sized_incr_relobj
<64, false>;
2733 class Sized_incr_dynobj
<64, false>;
2736 #ifdef HAVE_TARGET_64_BIG
2738 class Sized_incremental_binary
<64, true>;
2741 class Sized_incr_relobj
<64, true>;
2744 class Sized_incr_dynobj
<64, true>;
2747 } // End namespace gold.