* ld-elf/flags1.d: Don't xfail tic6x.
[binutils.git] / gold / incremental.cc
blob1250d70889c7cbc38690ea0a9a7576af5c0ab8a8
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.
23 #include "gold.h"
25 #include <cstdarg>
26 #include "libiberty.h"
28 #include "elfcpp.h"
29 #include "options.h"
30 #include "output.h"
31 #include "symtab.h"
32 #include "incremental.h"
33 #include "archive.h"
34 #include "object.h"
35 #include "output.h"
36 #include "target-select.h"
37 #include "target.h"
38 #include "fileread.h"
39 #include "script.h"
41 namespace gold {
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
54 public:
55 Output_section_incremental_inputs(const Incremental_inputs* inputs,
56 const Symbol_table* symtab)
57 : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab)
58 { }
60 protected:
61 // This is called to update the section size prior to assigning
62 // the address and file offset.
63 void
64 update_data_size()
65 { this->set_final_data_size(); }
67 // Set the final data size.
68 void
69 set_final_data_size();
71 // Write the data to the file.
72 void
73 do_write(Output_file*);
75 // Write to a map file.
76 void
77 do_print_to_mapfile(Mapfile* mapfile) const
78 { mapfile->print_output_data(this, _("** incremental_inputs")); }
80 private:
81 // Write the section header.
82 unsigned char*
83 write_header(unsigned char* pov, unsigned int input_file_count,
84 section_offset_type command_line_offset);
86 // Write the input file entries.
87 unsigned char*
88 write_input_files(unsigned char* oview, unsigned char* pov,
89 Stringpool* strtab);
91 // Write the supplemental information blocks.
92 unsigned char*
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.
98 void
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.
103 void
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_;
120 // The symbol table.
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?
127 void
128 vexplain_no_incremental(const char* format, va_list args)
130 char* buf = NULL;
131 if (vasprintf(&buf, format, args) < 0)
132 gold_nomem();
133 gold_info(_("the link might take longer: "
134 "cannot perform incremental link: %s"), buf);
135 free(buf);
138 void
139 explain_no_incremental(const char* format, ...)
141 va_list args;
142 va_start(args, format);
143 vexplain_no_incremental(format, args);
144 va_end(args);
147 // Report an error.
149 void
150 Incremental_binary::error(const char* format, ...) const
152 va_list args;
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
156 // full build.
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);
160 va_end(args);
163 // Find the .gnu_incremental_inputs section and related sections.
165 template<int size, bool big_endian>
166 bool
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.
177 return false;
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.
182 return false;
183 if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx)
184 return false;
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.
189 return false;
190 if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx)
191 return false;
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.
196 return false;
197 if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx)
198 return false;
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)
204 return false;
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;
216 return true;
219 // Set up the readers into the incremental info sections.
221 template<int size, bool big_endian>
222 void
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,
233 &strtab_shndx))
234 return;
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.
279 break;
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;
294 break;
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;
307 break;
308 default:
309 gold_unreachable();
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.
323 void
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;
329 p != end;
330 ++p)
332 if (p->is_group())
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());
342 else
344 gold_assert(p->is_file());
345 unsigned int arg_serial = p->file().arg_serial();
346 if (arg_serial > 0)
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
357 // can be done.
359 template<int size, bool big_endian>
360 bool
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"));
370 return false;
373 if (inputs.version() != INCREMENTAL_LINK_VERSION)
375 explain_no_incremental(_("different version of incremental build data"));
376 return false;
379 if (incremental_inputs->command_line() != inputs.command_line())
381 explain_no_incremental(_("command line changed"));
382 return false;
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
387 // arguments.
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.
404 break;
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());
410 return false;
412 break;
413 default:
414 gold_unreachable();
418 return true;
421 // Return TRUE if input file N has changed since the last incremental link.
423 template<int size, bool big_endian>
424 bool
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();
439 Timespec new_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.
445 return true;
448 if (new_mtime.seconds > old_mtime.seconds)
449 return true;
450 if (new_mtime.seconds == old_mtime.seconds
451 && new_mtime.nanoseconds > old_mtime.nanoseconds)
452 return true;
453 return false;
456 // Initialize the layout of the output file based on the existing
457 // output file.
459 template<int size, bool big_endian>
460 void
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++)
484 Shdr shdr(pshdr);
485 const char* name;
486 if (!shstrtab.get_c_string(shdr.get_sh_name(), &name))
487 name = NULL;
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);
496 pshdr += shdr_size;
500 // Mark regions of the input file that must be kept unchanged.
502 template<int size, bool big_endian>
503 void
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)
511 return;
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)
519 continue;
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>
529 void
530 Sized_incremental_binary<size, big_endian>::do_apply_incremental_relocs(
531 const Symbol_table* symtab,
532 Layout* layout,
533 Output_file* of)
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.
560 if (gsym == NULL)
561 continue;
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())
567 continue;
569 gold_debug(DEBUG_INCREMENTAL,
570 "Applying incremental relocations for global symbol %s [%d]",
571 gsym->name(), i);
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
578 // output file.
579 unsigned int offset = isymtab.get_list_head(i);
580 while (offset > 0)
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,
600 view_size);
602 gold_debug(DEBUG_INCREMENTAL,
603 " %08lx: %s + %d: type %d addend %ld",
604 (long)(section_offset + r_offset),
605 os->name(),
606 (int)r_offset,
607 r_type,
608 (long)r_addend);
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>
625 void
626 Sized_incremental_binary<size, big_endian>::get_symtab_view(
627 View* symtab_view,
628 unsigned int* nsyms,
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);
647 namespace
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>
654 Incremental_binary*
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]);
661 if (target == NULL)
663 explain_no_incremental(_("unsupported ELF machine number %d"),
664 ehdr.get_e_machine());
665 return NULL;
668 if (!parameters->target_valid())
669 set_parameters_target(target);
670 else if (target != &parameters->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
680 // should be opened.
682 Incremental_binary*
683 open_incremental_binary(Output_file* file)
685 off_t filesize = file->filesize();
686 int want = elfcpp::Elf_recognizer::max_header_size;
687 if (filesize < want)
688 want = filesize;
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."));
694 return NULL;
697 int size = 0;
698 bool big_endian = false;
699 std::string error;
700 if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
701 &error))
703 explain_no_incremental(error.c_str());
704 return NULL;
707 Incremental_binary* result = NULL;
708 if (size == 32)
710 if (big_endian)
712 #ifdef HAVE_TARGET_32_BIG
713 result = make_sized_incremental_binary<32, true>(
714 file, elfcpp::Ehdr<32, true>(p));
715 #else
716 explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
717 #endif
719 else
721 #ifdef HAVE_TARGET_32_LITTLE
722 result = make_sized_incremental_binary<32, false>(
723 file, elfcpp::Ehdr<32, false>(p));
724 #else
725 explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
726 #endif
729 else if (size == 64)
731 if (big_endian)
733 #ifdef HAVE_TARGET_64_BIG
734 result = make_sized_incremental_binary<64, true>(
735 file, elfcpp::Ehdr<64, true>(p));
736 #else
737 explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
738 #endif
740 else
742 #ifdef HAVE_TARGET_64_LITTLE
743 result = make_sized_incremental_binary<64, false>(
744 file, elfcpp::Ehdr<64, false>(p));
745 #else
746 explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
747 #endif
750 else
751 gold_unreachable();
753 return result;
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.
763 void
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]))
780 continue;
782 args.append(" '");
783 // Now append argv[i], but with all single-quotes escaped
784 const char* argpos = argv[i];
785 while (1)
787 const int len = strcspn(argpos, "'");
788 args.append(argpos, len);
789 if (argpos[len] == '\0')
790 break;
791 args.append("'\"'\"'");
792 argpos += len + 1;
794 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.
808 void
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)
818 arg_serial = 0;
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
838 // library.
840 class Unused_symbol_visitor : public Library_base::Symbol_visitor_base
842 public:
843 Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab)
844 : entry_(entry), strtab_(strtab)
847 void
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);
855 private:
856 Incremental_archive_entry* entry_;
857 Stringpool* strtab_;
860 // Finish recording the input archive file ARCHIVE. This is called by the
861 // Add_archive_symbols task after determining which archive members
862 // to include.
864 void
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.
881 void
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)
890 arg_serial = 0;
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);
899 if (arch != NULL)
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.
921 void
922 Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
923 const char* name, off_t sh_size)
925 Stringpool::Key key = 0;
927 if (name != NULL)
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.
938 void
939 Incremental_inputs::report_script(Script_info* script,
940 unsigned int arg_serial,
941 Timespec mtime)
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
953 // Layout::finalize.
955 void
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.
964 void
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);
973 break;
974 #endif
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);
979 break;
980 #endif
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);
985 break;
986 #endif
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);
991 break;
992 #endif
993 default:
994 gold_unreachable();
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.
1002 unsigned int
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>
1014 void
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();
1032 ++p)
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);
1046 // Object count.
1047 info_offset += 4;
1048 // Each member.
1049 info_offset += (entry->get_object_count() * 4);
1051 break;
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, local symbol offset,
1059 // local symbol count.
1060 info_offset += 16;
1061 // Each input section.
1062 info_offset += (entry->get_input_section_count()
1063 * (8 + 2 * sizeof_addr));
1064 // Each global symbol.
1065 const Object::Symbols* syms = entry->object()->get_global_symbols();
1066 info_offset += syms->size() * 20;
1068 break;
1069 case INCREMENTAL_INPUT_SHARED_LIBRARY:
1071 Incremental_object_entry* entry = (*p)->object_entry();
1072 gold_assert(entry != NULL);
1073 (*p)->set_info_offset(info_offset);
1074 // Global symbol count.
1075 info_offset += 4;
1076 // Each global symbol.
1077 const Object::Symbols* syms = entry->object()->get_global_symbols();
1078 gold_assert(syms != NULL);
1079 unsigned int nsyms = syms->size();
1080 unsigned int nsyms_out = 0;
1081 for (unsigned int i = 0; i < nsyms; ++i)
1083 const Symbol* sym = (*syms)[i];
1084 if (sym == NULL)
1085 continue;
1086 if (sym->is_forwarder())
1087 sym = this->symtab_->resolve_forwards(sym);
1088 if (sym->symtab_index() != -1U)
1089 ++nsyms_out;
1091 info_offset += nsyms_out * 4;
1093 break;
1094 case INCREMENTAL_INPUT_ARCHIVE:
1096 Incremental_archive_entry* entry = (*p)->archive_entry();
1097 gold_assert(entry != NULL);
1098 (*p)->set_info_offset(info_offset);
1099 // Member count + unused global symbol count.
1100 info_offset += 8;
1101 // Each member.
1102 info_offset += (entry->get_member_count() * 4);
1103 // Each global symbol.
1104 info_offset += (entry->get_unused_global_symbol_count() * 4);
1106 break;
1107 default:
1108 gold_unreachable();
1112 this->set_data_size(info_offset);
1114 // Set the size of the .gnu_incremental_symtab section.
1115 inputs->symtab_section()->set_current_data_size(this->symtab_->output_count()
1116 * sizeof(unsigned int));
1118 // Set the size of the .gnu_incremental_relocs section.
1119 inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count()
1120 * rel_size);
1122 // Set the size of the .gnu_incremental_got_plt section.
1123 Sized_target<size, big_endian>* target =
1124 parameters->sized_target<size, big_endian>();
1125 unsigned int got_count = target->got_entry_count();
1126 unsigned int plt_count = target->plt_entry_count();
1127 unsigned int got_plt_size = 8; // GOT entry count, PLT entry count.
1128 got_plt_size = (got_plt_size + got_count + 3) & ~3; // GOT type array.
1129 got_plt_size += got_count * 4 + plt_count * 4; // GOT array, PLT array.
1130 inputs->got_plt_section()->set_current_data_size(got_plt_size);
1133 // Write the contents of the .gnu_incremental_inputs and
1134 // .gnu_incremental_symtab sections.
1136 template<int size, bool big_endian>
1137 void
1138 Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of)
1140 const Incremental_inputs* inputs = this->inputs_;
1141 Stringpool* strtab = inputs->get_stringpool();
1143 // Get a view into the .gnu_incremental_inputs section.
1144 const off_t off = this->offset();
1145 const off_t oview_size = this->data_size();
1146 unsigned char* const oview = of->get_output_view(off, oview_size);
1147 unsigned char* pov = oview;
1149 // Get a view into the .gnu_incremental_symtab section.
1150 const off_t symtab_off = inputs->symtab_section()->offset();
1151 const off_t symtab_size = inputs->symtab_section()->data_size();
1152 unsigned char* const symtab_view = of->get_output_view(symtab_off,
1153 symtab_size);
1155 // Allocate an array of linked list heads for the .gnu_incremental_symtab
1156 // section. Each element corresponds to a global symbol in the output
1157 // symbol table, and points to the head of the linked list that threads
1158 // through the object file input entries. The value of each element
1159 // is the section-relative offset to a global symbol entry in a
1160 // supplemental information block.
1161 unsigned int global_sym_count = this->symtab_->output_count();
1162 unsigned int* global_syms = new unsigned int[global_sym_count];
1163 memset(global_syms, 0, global_sym_count * sizeof(unsigned int));
1165 // Write the section header.
1166 Stringpool::Key command_line_key = inputs->command_line_key();
1167 pov = this->write_header(pov, inputs->input_file_count(),
1168 strtab->get_offset_from_key(command_line_key));
1170 // Write the list of input files.
1171 pov = this->write_input_files(oview, pov, strtab);
1173 // Write the supplemental information blocks for each input file.
1174 pov = this->write_info_blocks(oview, pov, strtab, global_syms,
1175 global_sym_count);
1177 gold_assert(pov - oview == oview_size);
1179 // Write the .gnu_incremental_symtab section.
1180 gold_assert(global_sym_count * 4 == symtab_size);
1181 this->write_symtab(symtab_view, global_syms, global_sym_count);
1183 delete[] global_syms;
1185 // Write the .gnu_incremental_got_plt section.
1186 const off_t got_plt_off = inputs->got_plt_section()->offset();
1187 const off_t got_plt_size = inputs->got_plt_section()->data_size();
1188 unsigned char* const got_plt_view = of->get_output_view(got_plt_off,
1189 got_plt_size);
1190 this->write_got_plt(got_plt_view, got_plt_size);
1192 of->write_output_view(off, oview_size, oview);
1193 of->write_output_view(symtab_off, symtab_size, symtab_view);
1194 of->write_output_view(got_plt_off, got_plt_size, got_plt_view);
1197 // Write the section header: version, input file count, offset of command line
1198 // in the string table, and 4 bytes of padding.
1200 template<int size, bool big_endian>
1201 unsigned char*
1202 Output_section_incremental_inputs<size, big_endian>::write_header(
1203 unsigned char* pov,
1204 unsigned int input_file_count,
1205 section_offset_type command_line_offset)
1207 Swap32::writeval(pov, INCREMENTAL_LINK_VERSION);
1208 Swap32::writeval(pov + 4, input_file_count);
1209 Swap32::writeval(pov + 8, command_line_offset);
1210 Swap32::writeval(pov + 12, 0);
1211 return pov + this->header_size;
1214 // Write the input file entries.
1216 template<int size, bool big_endian>
1217 unsigned char*
1218 Output_section_incremental_inputs<size, big_endian>::write_input_files(
1219 unsigned char* oview,
1220 unsigned char* pov,
1221 Stringpool* strtab)
1223 const Incremental_inputs* inputs = this->inputs_;
1225 for (Incremental_inputs::Input_list::const_iterator p =
1226 inputs->input_files().begin();
1227 p != inputs->input_files().end();
1228 ++p)
1230 gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset());
1231 section_offset_type filename_offset =
1232 strtab->get_offset_from_key((*p)->get_filename_key());
1233 const Timespec& mtime = (*p)->get_mtime();
1234 unsigned int flags = (*p)->type();
1235 if ((*p)->is_in_system_directory())
1236 flags |= INCREMENTAL_INPUT_IN_SYSTEM_DIR;
1237 Swap32::writeval(pov, filename_offset);
1238 Swap32::writeval(pov + 4, (*p)->get_info_offset());
1239 Swap64::writeval(pov + 8, mtime.seconds);
1240 Swap32::writeval(pov + 16, mtime.nanoseconds);
1241 Swap16::writeval(pov + 20, flags);
1242 Swap16::writeval(pov + 22, (*p)->arg_serial());
1243 pov += this->input_entry_size;
1245 return pov;
1248 // Write the supplemental information blocks.
1250 template<int size, bool big_endian>
1251 unsigned char*
1252 Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
1253 unsigned char* oview,
1254 unsigned char* pov,
1255 Stringpool* strtab,
1256 unsigned int* global_syms,
1257 unsigned int global_sym_count)
1259 const Incremental_inputs* inputs = this->inputs_;
1260 unsigned int first_global_index = this->symtab_->first_global_index();
1262 for (Incremental_inputs::Input_list::const_iterator p =
1263 inputs->input_files().begin();
1264 p != inputs->input_files().end();
1265 ++p)
1267 switch ((*p)->type())
1269 case INCREMENTAL_INPUT_SCRIPT:
1271 gold_assert(static_cast<unsigned int>(pov - oview)
1272 == (*p)->get_info_offset());
1273 Incremental_script_entry* entry = (*p)->script_entry();
1274 gold_assert(entry != NULL);
1276 // Write the object count.
1277 unsigned int nobjects = entry->get_object_count();
1278 Swap32::writeval(pov, nobjects);
1279 pov += 4;
1281 // For each object, write the offset to its input file entry.
1282 for (unsigned int i = 0; i < nobjects; ++i)
1284 Incremental_input_entry* obj = entry->get_object(i);
1285 Swap32::writeval(pov, obj->get_offset());
1286 pov += 4;
1289 break;
1291 case INCREMENTAL_INPUT_OBJECT:
1292 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
1294 gold_assert(static_cast<unsigned int>(pov - oview)
1295 == (*p)->get_info_offset());
1296 Incremental_object_entry* entry = (*p)->object_entry();
1297 gold_assert(entry != NULL);
1298 const Object* obj = entry->object();
1299 const Relobj* relobj = static_cast<const Relobj*>(obj);
1300 const Object::Symbols* syms = obj->get_global_symbols();
1301 // Write the input section count and global symbol count.
1302 unsigned int nsections = entry->get_input_section_count();
1303 unsigned int nsyms = syms->size();
1304 off_t locals_offset = relobj->local_symbol_offset();
1305 unsigned int nlocals = relobj->output_local_symbol_count();
1306 Swap32::writeval(pov, nsections);
1307 Swap32::writeval(pov + 4, nsyms);
1308 Swap32::writeval(pov + 8, static_cast<unsigned int>(locals_offset));
1309 Swap32::writeval(pov + 12, nlocals);
1310 pov += 16;
1312 // Build a temporary array to map input section indexes
1313 // from the original object file index to the index in the
1314 // incremental info table.
1315 unsigned int* index_map = new unsigned int[obj->shnum()];
1316 memset(index_map, 0, obj->shnum() * sizeof(unsigned int));
1318 // For each input section, write the name, output section index,
1319 // offset within output section, and input section size.
1320 for (unsigned int i = 0; i < nsections; i++)
1322 unsigned int shndx = entry->get_input_section_index(i);
1323 index_map[shndx] = i + 1;
1324 Stringpool::Key key = entry->get_input_section_name_key(i);
1325 off_t name_offset = 0;
1326 if (key != 0)
1327 name_offset = strtab->get_offset_from_key(key);
1328 int out_shndx = 0;
1329 off_t out_offset = 0;
1330 off_t sh_size = 0;
1331 Output_section* os = obj->output_section(shndx);
1332 if (os != NULL)
1334 out_shndx = os->out_shndx();
1335 out_offset = obj->output_section_offset(shndx);
1336 sh_size = entry->get_input_section_size(i);
1338 Swap32::writeval(pov, name_offset);
1339 Swap32::writeval(pov + 4, out_shndx);
1340 Swap::writeval(pov + 8, out_offset);
1341 Swap::writeval(pov + 8 + sizeof_addr, sh_size);
1342 pov += 8 + 2 * sizeof_addr;
1345 // For each global symbol, write its associated relocations,
1346 // add it to the linked list of globals, then write the
1347 // supplemental information: global symbol table index,
1348 // input section index, linked list chain pointer, relocation
1349 // count, and offset to the relocations.
1350 for (unsigned int i = 0; i < nsyms; i++)
1352 const Symbol* sym = (*syms)[i];
1353 if (sym->is_forwarder())
1354 sym = this->symtab_->resolve_forwards(sym);
1355 unsigned int shndx = 0;
1356 if (sym->source() == Symbol::FROM_OBJECT
1357 && sym->object() == obj
1358 && sym->is_defined())
1360 bool is_ordinary;
1361 unsigned int orig_shndx = sym->shndx(&is_ordinary);
1362 if (is_ordinary)
1363 shndx = index_map[orig_shndx];
1365 unsigned int symtab_index = sym->symtab_index();
1366 unsigned int chain = 0;
1367 unsigned int first_reloc = 0;
1368 unsigned int nrelocs = obj->get_incremental_reloc_count(i);
1369 if (nrelocs > 0)
1371 gold_assert(symtab_index != -1U
1372 && (symtab_index - first_global_index
1373 < global_sym_count));
1374 first_reloc = obj->get_incremental_reloc_base(i);
1375 chain = global_syms[symtab_index - first_global_index];
1376 global_syms[symtab_index - first_global_index] =
1377 pov - oview;
1379 Swap32::writeval(pov, symtab_index);
1380 Swap32::writeval(pov + 4, shndx);
1381 Swap32::writeval(pov + 8, chain);
1382 Swap32::writeval(pov + 12, nrelocs);
1383 Swap32::writeval(pov + 16, first_reloc * 3 * sizeof_addr);
1384 pov += 20;
1387 delete[] index_map;
1389 break;
1391 case INCREMENTAL_INPUT_SHARED_LIBRARY:
1393 gold_assert(static_cast<unsigned int>(pov - oview)
1394 == (*p)->get_info_offset());
1395 Incremental_object_entry* entry = (*p)->object_entry();
1396 gold_assert(entry != NULL);
1397 const Object* obj = entry->object();
1398 const Object::Symbols* syms = obj->get_global_symbols();
1400 // Skip the global symbol count for now.
1401 unsigned char* orig_pov = pov;
1402 pov += 4;
1404 // For each global symbol, write the global symbol table index.
1405 unsigned int nsyms = syms->size();
1406 unsigned int nsyms_out = 0;
1407 for (unsigned int i = 0; i < nsyms; i++)
1409 const Symbol* sym = (*syms)[i];
1410 if (sym == NULL)
1411 continue;
1412 if (sym->is_forwarder())
1413 sym = this->symtab_->resolve_forwards(sym);
1414 if (sym->symtab_index() == -1U)
1415 continue;
1416 unsigned int def_flag = 0;
1417 if (sym->source() == Symbol::FROM_OBJECT
1418 && sym->object() == obj
1419 && sym->is_defined())
1420 def_flag = 1U << 31;
1421 Swap32::writeval(pov, sym->symtab_index() | def_flag);
1422 pov += 4;
1423 ++nsyms_out;
1426 // Now write the global symbol count.
1427 Swap32::writeval(orig_pov, nsyms_out);
1429 break;
1431 case INCREMENTAL_INPUT_ARCHIVE:
1433 gold_assert(static_cast<unsigned int>(pov - oview)
1434 == (*p)->get_info_offset());
1435 Incremental_archive_entry* entry = (*p)->archive_entry();
1436 gold_assert(entry != NULL);
1438 // Write the member count and unused global symbol count.
1439 unsigned int nmembers = entry->get_member_count();
1440 unsigned int nsyms = entry->get_unused_global_symbol_count();
1441 Swap32::writeval(pov, nmembers);
1442 Swap32::writeval(pov + 4, nsyms);
1443 pov += 8;
1445 // For each member, write the offset to its input file entry.
1446 for (unsigned int i = 0; i < nmembers; ++i)
1448 Incremental_object_entry* member = entry->get_member(i);
1449 Swap32::writeval(pov, member->get_offset());
1450 pov += 4;
1453 // For each global symbol, write the name offset.
1454 for (unsigned int i = 0; i < nsyms; ++i)
1456 Stringpool::Key key = entry->get_unused_global_symbol(i);
1457 Swap32::writeval(pov, strtab->get_offset_from_key(key));
1458 pov += 4;
1461 break;
1463 default:
1464 gold_unreachable();
1467 return pov;
1470 // Write the contents of the .gnu_incremental_symtab section.
1472 template<int size, bool big_endian>
1473 void
1474 Output_section_incremental_inputs<size, big_endian>::write_symtab(
1475 unsigned char* pov,
1476 unsigned int* global_syms,
1477 unsigned int global_sym_count)
1479 for (unsigned int i = 0; i < global_sym_count; ++i)
1481 Swap32::writeval(pov, global_syms[i]);
1482 pov += 4;
1486 // This struct holds the view information needed to write the
1487 // .gnu_incremental_got_plt section.
1489 struct Got_plt_view_info
1491 // Start of the GOT type array in the output view.
1492 unsigned char* got_type_p;
1493 // Start of the GOT descriptor array in the output view.
1494 unsigned char* got_desc_p;
1495 // Start of the PLT descriptor array in the output view.
1496 unsigned char* plt_desc_p;
1497 // Number of GOT entries.
1498 unsigned int got_count;
1499 // Number of PLT entries.
1500 unsigned int plt_count;
1501 // Offset of the first non-reserved PLT entry (this is a target-dependent value).
1502 unsigned int first_plt_entry_offset;
1503 // Size of a PLT entry (this is a target-dependent value).
1504 unsigned int plt_entry_size;
1505 // Value to write in the GOT descriptor array. For global symbols,
1506 // this is the global symbol table index; for local symbols, it is
1507 // the offset of the input file entry in the .gnu_incremental_inputs
1508 // section.
1509 unsigned int got_descriptor;
1512 // Functor class for processing a GOT offset list for local symbols.
1513 // Writes the GOT type and symbol index into the GOT type and descriptor
1514 // arrays in the output section.
1516 template<int size, bool big_endian>
1517 class Local_got_offset_visitor : public Got_offset_list::Visitor
1519 public:
1520 Local_got_offset_visitor(struct Got_plt_view_info& info)
1521 : info_(info)
1524 void
1525 visit(unsigned int got_type, unsigned int got_offset)
1527 unsigned int got_index = got_offset / this->got_entry_size_;
1528 gold_assert(got_index < this->info_.got_count);
1529 // We can only handle GOT entry types in the range 0..0x7e
1530 // because we use a byte array to store them, and we use the
1531 // high bit to flag a local symbol.
1532 gold_assert(got_type < 0x7f);
1533 this->info_.got_type_p[got_index] = got_type | 0x80;
1534 unsigned char* pov = this->info_.got_desc_p + got_index * 4;
1535 elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.got_descriptor);
1538 private:
1539 static const unsigned int got_entry_size_ = size / 8;
1540 struct Got_plt_view_info& info_;
1543 // Functor class for processing a GOT offset list. Writes the GOT type
1544 // and symbol index into the GOT type and descriptor arrays in the output
1545 // section.
1547 template<int size, bool big_endian>
1548 class Global_got_offset_visitor : public Got_offset_list::Visitor
1550 public:
1551 Global_got_offset_visitor(struct Got_plt_view_info& info)
1552 : info_(info)
1555 void
1556 visit(unsigned int got_type, unsigned int got_offset)
1558 unsigned int got_index = got_offset / this->got_entry_size_;
1559 gold_assert(got_index < this->info_.got_count);
1560 // We can only handle GOT entry types in the range 0..0x7e
1561 // because we use a byte array to store them, and we use the
1562 // high bit to flag a local symbol.
1563 gold_assert(got_type < 0x7f);
1564 this->info_.got_type_p[got_index] = got_type;
1565 unsigned char* pov = this->info_.got_desc_p + got_index * 4;
1566 elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.got_descriptor);
1569 private:
1570 static const unsigned int got_entry_size_ = size / 8;
1571 struct Got_plt_view_info& info_;
1574 // Functor class for processing the global symbol table. Processes the
1575 // GOT offset list for the symbol, and writes the symbol table index
1576 // into the PLT descriptor array in the output section.
1578 template<int size, bool big_endian>
1579 class Global_symbol_visitor_got_plt
1581 public:
1582 Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
1583 : info_(info)
1586 void
1587 operator()(const Sized_symbol<size>* sym)
1589 typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
1590 const Got_offset_list* got_offsets = sym->got_offset_list();
1591 if (got_offsets != NULL)
1593 this->info_.got_descriptor = sym->symtab_index();
1594 Got_visitor v(this->info_);
1595 got_offsets->for_all_got_offsets(&v);
1597 if (sym->has_plt_offset())
1599 unsigned int plt_index =
1600 ((sym->plt_offset() - this->info_.first_plt_entry_offset)
1601 / this->info_.plt_entry_size);
1602 gold_assert(plt_index < this->info_.plt_count);
1603 unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
1604 elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
1608 private:
1609 struct Got_plt_view_info& info_;
1612 // Write the contents of the .gnu_incremental_got_plt section.
1614 template<int size, bool big_endian>
1615 void
1616 Output_section_incremental_inputs<size, big_endian>::write_got_plt(
1617 unsigned char* pov,
1618 off_t view_size)
1620 Sized_target<size, big_endian>* target =
1621 parameters->sized_target<size, big_endian>();
1623 // Set up the view information for the functors.
1624 struct Got_plt_view_info view_info;
1625 view_info.got_count = target->got_entry_count();
1626 view_info.plt_count = target->plt_entry_count();
1627 view_info.first_plt_entry_offset = target->first_plt_entry_offset();
1628 view_info.plt_entry_size = target->plt_entry_size();
1629 view_info.got_type_p = pov + 8;
1630 view_info.got_desc_p = (view_info.got_type_p
1631 + ((view_info.got_count + 3) & ~3));
1632 view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 4;
1634 gold_assert(pov + view_size ==
1635 view_info.plt_desc_p + view_info.plt_count * 4);
1637 // Write the section header.
1638 Swap32::writeval(pov, view_info.got_count);
1639 Swap32::writeval(pov + 4, view_info.plt_count);
1641 // Initialize the GOT type array to 0xff (reserved).
1642 memset(view_info.got_type_p, 0xff, view_info.got_count);
1644 // Write the incremental GOT descriptors for local symbols.
1645 typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
1646 for (Incremental_inputs::Input_list::const_iterator p =
1647 this->inputs_->input_files().begin();
1648 p != this->inputs_->input_files().end();
1649 ++p)
1651 if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
1652 && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
1653 continue;
1654 Incremental_object_entry* entry = (*p)->object_entry();
1655 gold_assert(entry != NULL);
1656 const Object* obj = entry->object();
1657 gold_assert(obj != NULL);
1658 view_info.got_descriptor = (*p)->get_offset();
1659 Got_visitor v(view_info);
1660 obj->for_all_local_got_entries(&v);
1663 // Write the incremental GOT and PLT descriptors for global symbols.
1664 typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
1665 symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
1668 // Class Sized_incr_relobj. Most of these methods are not used for
1669 // Incremental objects, but are required to be implemented by the
1670 // base class Object.
1672 template<int size, bool big_endian>
1673 Sized_incr_relobj<size, big_endian>::Sized_incr_relobj(
1674 const std::string& name,
1675 Sized_incremental_binary<size, big_endian>* ibase,
1676 unsigned int input_file_index)
1677 : Sized_relobj_base<size, big_endian>(name, NULL), ibase_(ibase),
1678 input_file_index_(input_file_index),
1679 input_reader_(ibase->inputs_reader().input_file(input_file_index)),
1680 local_symbol_count_(0), output_local_dynsym_count_(0),
1681 local_symbol_index_(0), local_symbol_offset_(0), local_dynsym_offset_(0),
1682 symbols_(), section_offsets_(), incr_reloc_offset_(-1U),
1683 incr_reloc_count_(0), incr_reloc_output_index_(0), incr_relocs_(NULL),
1684 local_symbols_()
1686 if (this->input_reader_.is_in_system_directory())
1687 this->set_is_in_system_directory();
1688 const unsigned int shnum = this->input_reader_.get_input_section_count() + 1;
1689 this->set_shnum(shnum);
1692 // Read the symbols.
1694 template<int size, bool big_endian>
1695 void
1696 Sized_incr_relobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1698 gold_unreachable();
1701 // Lay out the input sections.
1703 template<int size, bool big_endian>
1704 void
1705 Sized_incr_relobj<size, big_endian>::do_layout(
1706 Symbol_table*,
1707 Layout* layout,
1708 Read_symbols_data*)
1710 const unsigned int shnum = this->shnum();
1711 Incremental_inputs* incremental_inputs = layout->incremental_inputs();
1712 gold_assert(incremental_inputs != NULL);
1713 Output_sections& out_sections(this->output_sections());
1714 out_sections.resize(shnum);
1715 this->section_offsets_.resize(shnum);
1716 for (unsigned int i = 1; i < shnum; i++)
1718 typename Input_entry_reader::Input_section_info sect =
1719 this->input_reader_.get_input_section(i - 1);
1720 // Add the section to the incremental inputs layout.
1721 incremental_inputs->report_input_section(this, i, sect.name,
1722 sect.sh_size);
1723 if (sect.output_shndx == 0 || sect.sh_offset == -1)
1724 continue;
1725 Output_section* os = this->ibase_->output_section(sect.output_shndx);
1726 gold_assert(os != NULL);
1727 out_sections[i] = os;
1728 this->section_offsets_[i] = static_cast<Address>(sect.sh_offset);
1732 // Layout sections whose layout was deferred while waiting for
1733 // input files from a plugin.
1734 template<int size, bool big_endian>
1735 void
1736 Sized_incr_relobj<size, big_endian>::do_layout_deferred_sections(Layout*)
1740 // Add the symbols to the symbol table.
1742 template<int size, bool big_endian>
1743 void
1744 Sized_incr_relobj<size, big_endian>::do_add_symbols(
1745 Symbol_table* symtab,
1746 Read_symbols_data*,
1747 Layout*)
1749 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1750 unsigned char symbuf[sym_size];
1751 elfcpp::Sym<size, big_endian> sym(symbuf);
1752 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1754 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
1756 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
1757 this->symbols_.resize(nsyms);
1759 Incremental_binary::View symtab_view(NULL);
1760 unsigned int symtab_count;
1761 elfcpp::Elf_strtab strtab(NULL, 0);
1762 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
1764 Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
1765 unsigned int isym_count = isymtab.symbol_count();
1766 unsigned int first_global = symtab_count - isym_count;
1768 const unsigned char* sym_p;
1769 for (unsigned int i = 0; i < nsyms; ++i)
1771 Incremental_global_symbol_reader<big_endian> info =
1772 this->input_reader_.get_global_symbol_reader(i);
1773 unsigned int output_symndx = info.output_symndx();
1774 sym_p = symtab_view.data() + output_symndx * sym_size;
1775 elfcpp::Sym<size, big_endian> gsym(sym_p);
1776 const char* name;
1777 if (!strtab.get_c_string(gsym.get_st_name(), &name))
1778 name = "";
1780 typename elfcpp::Elf_types<size>::Elf_Addr v = gsym.get_st_value();
1781 unsigned int shndx = gsym.get_st_shndx();
1782 elfcpp::STB st_bind = gsym.get_st_bind();
1783 elfcpp::STT st_type = gsym.get_st_type();
1785 // Local hidden symbols start out as globals, but get converted to
1786 // to local during output.
1787 if (st_bind == elfcpp::STB_LOCAL)
1788 st_bind = elfcpp::STB_GLOBAL;
1790 unsigned int input_shndx = info.shndx();
1791 if (input_shndx == 0)
1793 shndx = elfcpp::SHN_UNDEF;
1794 v = 0;
1796 else if (shndx != elfcpp::SHN_ABS)
1798 // Find the input section and calculate the section-relative value.
1799 gold_assert(shndx != elfcpp::SHN_UNDEF);
1800 Output_section* os = this->ibase_->output_section(shndx);
1801 gold_assert(os != NULL && os->has_fixed_layout());
1802 typename Input_entry_reader::Input_section_info sect =
1803 this->input_reader_.get_input_section(input_shndx - 1);
1804 gold_assert(sect.output_shndx == shndx);
1805 if (st_type != elfcpp::STT_TLS)
1806 v -= os->address();
1807 v -= sect.sh_offset;
1808 shndx = input_shndx;
1811 osym.put_st_name(0);
1812 osym.put_st_value(v);
1813 osym.put_st_size(gsym.get_st_size());
1814 osym.put_st_info(st_bind, st_type);
1815 osym.put_st_other(gsym.get_st_other());
1816 osym.put_st_shndx(shndx);
1818 this->symbols_[i] =
1819 symtab->add_from_incrobj(this, name, NULL, &sym);
1820 this->ibase_->add_global_symbol(output_symndx - first_global,
1821 this->symbols_[i]);
1825 // Return TRUE if we should include this object from an archive library.
1827 template<int size, bool big_endian>
1828 Archive::Should_include
1829 Sized_incr_relobj<size, big_endian>::do_should_include_member(
1830 Symbol_table*,
1831 Layout*,
1832 Read_symbols_data*,
1833 std::string*)
1835 gold_unreachable();
1838 // Iterate over global symbols, calling a visitor class V for each.
1840 template<int size, bool big_endian>
1841 void
1842 Sized_incr_relobj<size, big_endian>::do_for_all_global_symbols(
1843 Read_symbols_data*,
1844 Library_base::Symbol_visitor_base*)
1846 // This routine is not used for incremental objects.
1849 // Iterate over local symbols, calling a visitor class V for each GOT offset
1850 // associated with a local symbol.
1852 template<int size, bool big_endian>
1853 void
1854 Sized_incr_relobj<size, big_endian>::do_for_all_local_got_entries(
1855 Got_offset_list::Visitor*) const
1857 // FIXME: Implement Sized_incr_relobj::do_for_all_local_got_entries.
1860 // Get the size of a section.
1862 template<int size, bool big_endian>
1863 uint64_t
1864 Sized_incr_relobj<size, big_endian>::do_section_size(unsigned int)
1866 gold_unreachable();
1869 // Get the name of a section.
1871 template<int size, bool big_endian>
1872 std::string
1873 Sized_incr_relobj<size, big_endian>::do_section_name(unsigned int)
1875 gold_unreachable();
1878 // Return a view of the contents of a section.
1880 template<int size, bool big_endian>
1881 Object::Location
1882 Sized_incr_relobj<size, big_endian>::do_section_contents(unsigned int)
1884 gold_unreachable();
1887 // Return section flags.
1889 template<int size, bool big_endian>
1890 uint64_t
1891 Sized_incr_relobj<size, big_endian>::do_section_flags(unsigned int)
1893 gold_unreachable();
1896 // Return section entsize.
1898 template<int size, bool big_endian>
1899 uint64_t
1900 Sized_incr_relobj<size, big_endian>::do_section_entsize(unsigned int)
1902 gold_unreachable();
1905 // Return section address.
1907 template<int size, bool big_endian>
1908 uint64_t
1909 Sized_incr_relobj<size, big_endian>::do_section_address(unsigned int)
1911 gold_unreachable();
1914 // Return section type.
1916 template<int size, bool big_endian>
1917 unsigned int
1918 Sized_incr_relobj<size, big_endian>::do_section_type(unsigned int)
1920 gold_unreachable();
1923 // Return the section link field.
1925 template<int size, bool big_endian>
1926 unsigned int
1927 Sized_incr_relobj<size, big_endian>::do_section_link(unsigned int)
1929 gold_unreachable();
1932 // Return the section link field.
1934 template<int size, bool big_endian>
1935 unsigned int
1936 Sized_incr_relobj<size, big_endian>::do_section_info(unsigned int)
1938 gold_unreachable();
1941 // Return the section alignment.
1943 template<int size, bool big_endian>
1944 uint64_t
1945 Sized_incr_relobj<size, big_endian>::do_section_addralign(unsigned int)
1947 gold_unreachable();
1950 // Return the Xindex structure to use.
1952 template<int size, bool big_endian>
1953 Xindex*
1954 Sized_incr_relobj<size, big_endian>::do_initialize_xindex()
1956 gold_unreachable();
1959 // Get symbol counts.
1961 template<int size, bool big_endian>
1962 void
1963 Sized_incr_relobj<size, big_endian>::do_get_global_symbol_counts(
1964 const Symbol_table*, size_t*, size_t*) const
1966 gold_unreachable();
1969 // Read the relocs.
1971 template<int size, bool big_endian>
1972 void
1973 Sized_incr_relobj<size, big_endian>::do_read_relocs(Read_relocs_data*)
1977 // Process the relocs to find list of referenced sections. Used only
1978 // during garbage collection.
1980 template<int size, bool big_endian>
1981 void
1982 Sized_incr_relobj<size, big_endian>::do_gc_process_relocs(Symbol_table*,
1983 Layout*,
1984 Read_relocs_data*)
1986 gold_unreachable();
1989 // Scan the relocs and adjust the symbol table.
1991 template<int size, bool big_endian>
1992 void
1993 Sized_incr_relobj<size, big_endian>::do_scan_relocs(Symbol_table*,
1994 Layout* layout,
1995 Read_relocs_data*)
1997 // Count the incremental relocations for this object.
1998 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
1999 this->allocate_incremental_reloc_counts();
2000 for (unsigned int i = 0; i < nsyms; i++)
2002 Incremental_global_symbol_reader<big_endian> sym =
2003 this->input_reader_.get_global_symbol_reader(i);
2004 unsigned int reloc_count = sym.reloc_count();
2005 if (reloc_count > 0 && this->incr_reloc_offset_ == -1U)
2006 this->incr_reloc_offset_ = sym.reloc_offset();
2007 this->incr_reloc_count_ += reloc_count;
2008 for (unsigned int j = 0; j < reloc_count; j++)
2009 this->count_incremental_reloc(i);
2011 this->incr_reloc_output_index_ =
2012 layout->incremental_inputs()->get_reloc_count();
2013 this->finalize_incremental_relocs(layout, false);
2015 // The incoming incremental relocations may not end up in the same
2016 // location after the incremental update, because the incremental info
2017 // is regenerated in each link. Because the new location may overlap
2018 // with other data in the updated output file, we need to copy the
2019 // relocations into a buffer so that we can still read them safely
2020 // after we start writing updates to the output file.
2021 if (this->incr_reloc_count_ > 0)
2023 const Incremental_relocs_reader<size, big_endian>& relocs_reader =
2024 this->ibase_->relocs_reader();
2025 const unsigned int incr_reloc_size = relocs_reader.reloc_size;
2026 unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2027 this->incr_relocs_ = new unsigned char[len];
2028 memcpy(this->incr_relocs_,
2029 relocs_reader.data(this->incr_reloc_offset_),
2030 len);
2034 // Count the local symbols.
2036 template<int size, bool big_endian>
2037 void
2038 Sized_incr_relobj<size, big_endian>::do_count_local_symbols(
2039 Stringpool_template<char>* pool,
2040 Stringpool_template<char>*)
2042 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2044 // Set the count of local symbols based on the incremental info.
2045 unsigned int nlocals = this->input_reader_.get_local_symbol_count();
2046 this->local_symbol_count_ = nlocals;
2047 this->local_symbols_.reserve(nlocals);
2049 // Get views of the base file's symbol table and string table.
2050 Incremental_binary::View symtab_view(NULL);
2051 unsigned int symtab_count;
2052 elfcpp::Elf_strtab strtab(NULL, 0);
2053 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2055 // Read the local symbols from the base file's symbol table.
2056 off_t off = this->input_reader_.get_local_symbol_offset();
2057 const unsigned char* symp = symtab_view.data() + off;
2058 for (unsigned int i = 0; i < nlocals; ++i, symp += sym_size)
2060 elfcpp::Sym<size, big_endian> sym(symp);
2061 const char* name;
2062 if (!strtab.get_c_string(sym.get_st_name(), &name))
2063 name = "";
2064 gold_debug(DEBUG_INCREMENTAL, "Local symbol %d: %s", i, name);
2065 name = pool->add(name, true, NULL);
2066 this->local_symbols_.push_back(Local_symbol(name,
2067 sym.get_st_value(),
2068 sym.get_st_size(),
2069 sym.get_st_shndx(),
2070 sym.get_st_type(),
2071 false));
2075 // Finalize the local symbols.
2077 template<int size, bool big_endian>
2078 unsigned int
2079 Sized_incr_relobj<size, big_endian>::do_finalize_local_symbols(
2080 unsigned int index,
2081 off_t off,
2082 Symbol_table*)
2084 this->local_symbol_index_ = index;
2085 this->local_symbol_offset_ = off;
2086 return index + this->local_symbol_count_;
2089 // Set the offset where local dynamic symbol information will be stored.
2091 template<int size, bool big_endian>
2092 unsigned int
2093 Sized_incr_relobj<size, big_endian>::do_set_local_dynsym_indexes(
2094 unsigned int index)
2096 // FIXME: set local dynsym indexes.
2097 return index;
2100 // Set the offset where local dynamic symbol information will be stored.
2102 template<int size, bool big_endian>
2103 unsigned int
2104 Sized_incr_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t)
2106 return 0;
2109 // Relocate the input sections and write out the local symbols.
2110 // We don't actually do any relocation here. For unchanged input files,
2111 // we reapply relocations only for symbols that have changed; that happens
2112 // in queue_final_tasks. We do need to rewrite the incremental relocations
2113 // for this object.
2115 template<int size, bool big_endian>
2116 void
2117 Sized_incr_relobj<size, big_endian>::do_relocate(const Symbol_table*,
2118 const Layout* layout,
2119 Output_file* of)
2121 if (this->incr_reloc_count_ == 0)
2122 return;
2124 const unsigned int incr_reloc_size =
2125 Incremental_relocs_reader<size, big_endian>::reloc_size;
2127 // Get a view for the .gnu_incremental_relocs section.
2128 Incremental_inputs* inputs = layout->incremental_inputs();
2129 gold_assert(inputs != NULL);
2130 const off_t relocs_off = inputs->relocs_section()->offset();
2131 const off_t relocs_size = inputs->relocs_section()->data_size();
2132 unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
2134 // Copy the relocations from the buffer.
2135 off_t off = this->incr_reloc_output_index_ * incr_reloc_size;
2136 unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2137 memcpy(view + off, this->incr_relocs_, len);
2139 // The output section table may have changed, so we need to map
2140 // the old section index to the new section index for each relocation.
2141 for (unsigned int i = 0; i < this->incr_reloc_count_; ++i)
2143 unsigned char* pov = view + off + i * incr_reloc_size;
2144 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(pov + 4);
2145 Output_section* os = this->ibase_->output_section(shndx);
2146 gold_assert(os != NULL);
2147 shndx = os->out_shndx();
2148 elfcpp::Swap<32, big_endian>::writeval(pov + 4, shndx);
2151 of->write_output_view(off, len, view);
2153 // Get views into the output file for the portions of the symbol table
2154 // and the dynamic symbol table that we will be writing.
2155 off_t symtab_off = layout->symtab_section()->offset();
2156 off_t output_size = this->local_symbol_count_ * This::sym_size;
2157 unsigned char* oview = NULL;
2158 if (output_size > 0)
2159 oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
2160 output_size);
2162 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
2163 unsigned char* dyn_oview = NULL;
2164 if (dyn_output_size > 0)
2165 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
2166 dyn_output_size);
2168 // Write the local symbols.
2169 unsigned char* ov = oview;
2170 unsigned char* dyn_ov = dyn_oview;
2171 const Stringpool* sympool = layout->sympool();
2172 const Stringpool* dynpool = layout->dynpool();
2173 Output_symtab_xindex* symtab_xindex = layout->symtab_xindex();
2174 Output_symtab_xindex* dynsym_xindex = layout->dynsym_xindex();
2175 for (unsigned int i = 0; i < this->local_symbol_count_; ++i)
2177 Local_symbol& lsym(this->local_symbols_[i]);
2179 bool is_ordinary;
2180 unsigned int st_shndx = this->adjust_sym_shndx(i, lsym.st_shndx,
2181 &is_ordinary);
2182 if (is_ordinary)
2184 Output_section* os = this->ibase_->output_section(st_shndx);
2185 st_shndx = os->out_shndx();
2186 if (st_shndx >= elfcpp::SHN_LORESERVE)
2188 symtab_xindex->add(this->local_symbol_index_ + i, st_shndx);
2189 if (lsym.needs_dynsym_entry)
2190 dynsym_xindex->add(lsym.output_dynsym_index, st_shndx);
2191 st_shndx = elfcpp::SHN_XINDEX;
2195 // Write the symbol to the output symbol table.
2197 elfcpp::Sym_write<size, big_endian> osym(ov);
2198 osym.put_st_name(sympool->get_offset(lsym.name));
2199 osym.put_st_value(lsym.st_value);
2200 osym.put_st_size(lsym.st_size);
2201 osym.put_st_info(elfcpp::STB_LOCAL,
2202 static_cast<elfcpp::STT>(lsym.st_type));
2203 osym.put_st_other(0);
2204 osym.put_st_shndx(st_shndx);
2205 ov += sym_size;
2208 // Write the symbol to the output dynamic symbol table.
2209 if (lsym.needs_dynsym_entry)
2211 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
2212 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
2213 osym.put_st_name(dynpool->get_offset(lsym.name));
2214 osym.put_st_value(lsym.st_value);
2215 osym.put_st_size(lsym.st_size);
2216 osym.put_st_info(elfcpp::STB_LOCAL,
2217 static_cast<elfcpp::STT>(lsym.st_type));
2218 osym.put_st_other(0);
2219 osym.put_st_shndx(st_shndx);
2220 dyn_ov += sym_size;
2224 if (output_size > 0)
2226 gold_assert(ov - oview == output_size);
2227 of->write_output_view(symtab_off + this->local_symbol_offset_,
2228 output_size, oview);
2231 if (dyn_output_size > 0)
2233 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
2234 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
2235 dyn_oview);
2239 // Set the offset of a section.
2241 template<int size, bool big_endian>
2242 void
2243 Sized_incr_relobj<size, big_endian>::do_set_section_offset(unsigned int,
2244 uint64_t)
2248 // Class Sized_incr_dynobj. Most of these methods are not used for
2249 // Incremental objects, but are required to be implemented by the
2250 // base class Object.
2252 template<int size, bool big_endian>
2253 Sized_incr_dynobj<size, big_endian>::Sized_incr_dynobj(
2254 const std::string& name,
2255 Sized_incremental_binary<size, big_endian>* ibase,
2256 unsigned int input_file_index)
2257 : Dynobj(name, NULL), ibase_(ibase),
2258 input_file_index_(input_file_index),
2259 input_reader_(ibase->inputs_reader().input_file(input_file_index)),
2260 symbols_()
2262 if (this->input_reader_.is_in_system_directory())
2263 this->set_is_in_system_directory();
2264 this->set_shnum(0);
2267 // Read the symbols.
2269 template<int size, bool big_endian>
2270 void
2271 Sized_incr_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
2273 gold_unreachable();
2276 // Lay out the input sections.
2278 template<int size, bool big_endian>
2279 void
2280 Sized_incr_dynobj<size, big_endian>::do_layout(
2281 Symbol_table*,
2282 Layout*,
2283 Read_symbols_data*)
2287 // Add the symbols to the symbol table.
2289 template<int size, bool big_endian>
2290 void
2291 Sized_incr_dynobj<size, big_endian>::do_add_symbols(
2292 Symbol_table* symtab,
2293 Read_symbols_data*,
2294 Layout*)
2296 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2297 unsigned char symbuf[sym_size];
2298 elfcpp::Sym<size, big_endian> sym(symbuf);
2299 elfcpp::Sym_write<size, big_endian> osym(symbuf);
2301 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2303 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2304 this->symbols_.resize(nsyms);
2306 Incremental_binary::View symtab_view(NULL);
2307 unsigned int symtab_count;
2308 elfcpp::Elf_strtab strtab(NULL, 0);
2309 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2311 Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2312 unsigned int isym_count = isymtab.symbol_count();
2313 unsigned int first_global = symtab_count - isym_count;
2315 const unsigned char* sym_p;
2316 for (unsigned int i = 0; i < nsyms; ++i)
2318 bool is_def;
2319 unsigned int output_symndx =
2320 this->input_reader_.get_output_symbol_index(i, &is_def);
2321 sym_p = symtab_view.data() + output_symndx * sym_size;
2322 elfcpp::Sym<size, big_endian> gsym(sym_p);
2323 const char* name;
2324 if (!strtab.get_c_string(gsym.get_st_name(), &name))
2325 name = "";
2327 typename elfcpp::Elf_types<size>::Elf_Addr v;
2328 unsigned int shndx;
2329 elfcpp::STB st_bind = gsym.get_st_bind();
2330 elfcpp::STT st_type = gsym.get_st_type();
2332 // Local hidden symbols start out as globals, but get converted to
2333 // to local during output.
2334 if (st_bind == elfcpp::STB_LOCAL)
2335 st_bind = elfcpp::STB_GLOBAL;
2337 if (!is_def)
2339 shndx = elfcpp::SHN_UNDEF;
2340 v = 0;
2342 else
2344 // For a symbol defined in a shared object, the section index
2345 // is meaningless, as long as it's not SHN_UNDEF.
2346 shndx = 1;
2347 v = gsym.get_st_value();
2350 osym.put_st_name(0);
2351 osym.put_st_value(v);
2352 osym.put_st_size(gsym.get_st_size());
2353 osym.put_st_info(st_bind, st_type);
2354 osym.put_st_other(gsym.get_st_other());
2355 osym.put_st_shndx(shndx);
2357 this->symbols_[i] =
2358 symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
2359 this->ibase_->add_global_symbol(output_symndx - first_global,
2360 this->symbols_[i]);
2364 // Return TRUE if we should include this object from an archive library.
2366 template<int size, bool big_endian>
2367 Archive::Should_include
2368 Sized_incr_dynobj<size, big_endian>::do_should_include_member(
2369 Symbol_table*,
2370 Layout*,
2371 Read_symbols_data*,
2372 std::string*)
2374 gold_unreachable();
2377 // Iterate over global symbols, calling a visitor class V for each.
2379 template<int size, bool big_endian>
2380 void
2381 Sized_incr_dynobj<size, big_endian>::do_for_all_global_symbols(
2382 Read_symbols_data*,
2383 Library_base::Symbol_visitor_base*)
2385 // This routine is not used for dynamic libraries.
2388 // Iterate over local symbols, calling a visitor class V for each GOT offset
2389 // associated with a local symbol.
2391 template<int size, bool big_endian>
2392 void
2393 Sized_incr_dynobj<size, big_endian>::do_for_all_local_got_entries(
2394 Got_offset_list::Visitor*) const
2398 // Get the size of a section.
2400 template<int size, bool big_endian>
2401 uint64_t
2402 Sized_incr_dynobj<size, big_endian>::do_section_size(unsigned int)
2404 gold_unreachable();
2407 // Get the name of a section.
2409 template<int size, bool big_endian>
2410 std::string
2411 Sized_incr_dynobj<size, big_endian>::do_section_name(unsigned int)
2413 gold_unreachable();
2416 // Return a view of the contents of a section.
2418 template<int size, bool big_endian>
2419 Object::Location
2420 Sized_incr_dynobj<size, big_endian>::do_section_contents(unsigned int)
2422 gold_unreachable();
2425 // Return section flags.
2427 template<int size, bool big_endian>
2428 uint64_t
2429 Sized_incr_dynobj<size, big_endian>::do_section_flags(unsigned int)
2431 gold_unreachable();
2434 // Return section entsize.
2436 template<int size, bool big_endian>
2437 uint64_t
2438 Sized_incr_dynobj<size, big_endian>::do_section_entsize(unsigned int)
2440 gold_unreachable();
2443 // Return section address.
2445 template<int size, bool big_endian>
2446 uint64_t
2447 Sized_incr_dynobj<size, big_endian>::do_section_address(unsigned int)
2449 gold_unreachable();
2452 // Return section type.
2454 template<int size, bool big_endian>
2455 unsigned int
2456 Sized_incr_dynobj<size, big_endian>::do_section_type(unsigned int)
2458 gold_unreachable();
2461 // Return the section link field.
2463 template<int size, bool big_endian>
2464 unsigned int
2465 Sized_incr_dynobj<size, big_endian>::do_section_link(unsigned int)
2467 gold_unreachable();
2470 // Return the section link field.
2472 template<int size, bool big_endian>
2473 unsigned int
2474 Sized_incr_dynobj<size, big_endian>::do_section_info(unsigned int)
2476 gold_unreachable();
2479 // Return the section alignment.
2481 template<int size, bool big_endian>
2482 uint64_t
2483 Sized_incr_dynobj<size, big_endian>::do_section_addralign(unsigned int)
2485 gold_unreachable();
2488 // Return the Xindex structure to use.
2490 template<int size, bool big_endian>
2491 Xindex*
2492 Sized_incr_dynobj<size, big_endian>::do_initialize_xindex()
2494 gold_unreachable();
2497 // Get symbol counts.
2499 template<int size, bool big_endian>
2500 void
2501 Sized_incr_dynobj<size, big_endian>::do_get_global_symbol_counts(
2502 const Symbol_table*, size_t*, size_t*) const
2504 gold_unreachable();
2507 // Allocate an incremental object of the appropriate size and endianness.
2509 Object*
2510 make_sized_incremental_object(
2511 Incremental_binary* ibase,
2512 unsigned int input_file_index,
2513 Incremental_input_type input_type,
2514 const Incremental_binary::Input_reader* input_reader)
2516 Object* obj = NULL;
2517 std::string name(input_reader->filename());
2519 switch (parameters->size_and_endianness())
2521 #ifdef HAVE_TARGET_32_LITTLE
2522 case Parameters::TARGET_32_LITTLE:
2524 Sized_incremental_binary<32, false>* sized_ibase =
2525 static_cast<Sized_incremental_binary<32, false>*>(ibase);
2526 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2527 obj = new Sized_incr_dynobj<32, false>(name, sized_ibase,
2528 input_file_index);
2529 else
2530 obj = new Sized_incr_relobj<32, false>(name, sized_ibase,
2531 input_file_index);
2533 break;
2534 #endif
2535 #ifdef HAVE_TARGET_32_BIG
2536 case Parameters::TARGET_32_BIG:
2538 Sized_incremental_binary<32, true>* sized_ibase =
2539 static_cast<Sized_incremental_binary<32, true>*>(ibase);
2540 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2541 obj = new Sized_incr_dynobj<32, true>(name, sized_ibase,
2542 input_file_index);
2543 else
2544 obj = new Sized_incr_relobj<32, true>(name, sized_ibase,
2545 input_file_index);
2547 break;
2548 #endif
2549 #ifdef HAVE_TARGET_64_LITTLE
2550 case Parameters::TARGET_64_LITTLE:
2552 Sized_incremental_binary<64, false>* sized_ibase =
2553 static_cast<Sized_incremental_binary<64, false>*>(ibase);
2554 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2555 obj = new Sized_incr_dynobj<64, false>(name, sized_ibase,
2556 input_file_index);
2557 else
2558 obj = new Sized_incr_relobj<64, false>(name, sized_ibase,
2559 input_file_index);
2561 break;
2562 #endif
2563 #ifdef HAVE_TARGET_64_BIG
2564 case Parameters::TARGET_64_BIG:
2566 Sized_incremental_binary<64, true>* sized_ibase =
2567 static_cast<Sized_incremental_binary<64, true>*>(ibase);
2568 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2569 obj = new Sized_incr_dynobj<64, true>(name, sized_ibase,
2570 input_file_index);
2571 else
2572 obj = new Sized_incr_relobj<64, true>(name, sized_ibase,
2573 input_file_index);
2575 break;
2576 #endif
2577 default:
2578 gold_unreachable();
2581 gold_assert(obj != NULL);
2582 return obj;
2585 // Copy the unused symbols from the incremental input info.
2586 // We need to do this because we may be overwriting the incremental
2587 // input info in the base file before we write the new incremental
2588 // info.
2589 void
2590 Incremental_library::copy_unused_symbols()
2592 unsigned int symcount = this->input_reader_->get_unused_symbol_count();
2593 this->unused_symbols_.reserve(symcount);
2594 for (unsigned int i = 0; i < symcount; ++i)
2596 std::string name(this->input_reader_->get_unused_symbol(i));
2597 this->unused_symbols_.push_back(name);
2601 // Iterator for unused global symbols in the library.
2602 void
2603 Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base* v) const
2605 for (Symbol_list::const_iterator p = this->unused_symbols_.begin();
2606 p != this->unused_symbols_.end();
2607 ++p)
2608 v->visit(p->c_str());
2611 // Instantiate the templates we need.
2613 #ifdef HAVE_TARGET_32_LITTLE
2614 template
2615 class Sized_incremental_binary<32, false>;
2617 template
2618 class Sized_incr_relobj<32, false>;
2620 template
2621 class Sized_incr_dynobj<32, false>;
2622 #endif
2624 #ifdef HAVE_TARGET_32_BIG
2625 template
2626 class Sized_incremental_binary<32, true>;
2628 template
2629 class Sized_incr_relobj<32, true>;
2631 template
2632 class Sized_incr_dynobj<32, true>;
2633 #endif
2635 #ifdef HAVE_TARGET_64_LITTLE
2636 template
2637 class Sized_incremental_binary<64, false>;
2639 template
2640 class Sized_incr_relobj<64, false>;
2642 template
2643 class Sized_incr_dynobj<64, false>;
2644 #endif
2646 #ifdef HAVE_TARGET_64_BIG
2647 template
2648 class Sized_incremental_binary<64, true>;
2650 template
2651 class Sized_incr_relobj<64, true>;
2653 template
2654 class Sized_incr_dynobj<64, true>;
2655 #endif
2657 } // End namespace gold.