* incremental-dump.cc (dump_incremental_inputs): Change signature
[platform/upstream/binutils.git] / gold / incremental.cc
1 // inremental.cc -- incremental linking support for gold
2
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Mikolaj Zalewski <mikolajz@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <cstdarg>
26 #include "libiberty.h"
27
28 #include "elfcpp.h"
29 #include "output.h"
30 #include "symtab.h"
31 #include "incremental.h"
32 #include "archive.h"
33 #include "output.h"
34 #include "target-select.h"
35 #include "target.h"
36
37 namespace gold {
38
39 // Version information. Will change frequently during the development, later
40 // we could think about backward (and forward?) compatibility.
41 const unsigned int INCREMENTAL_LINK_VERSION = 1;
42
43 // This class manages the .gnu_incremental_inputs section, which holds
44 // the header information, a directory of input files, and separate
45 // entries for each input file.
46
47 template<int size, bool big_endian>
48 class Output_section_incremental_inputs : public Output_section_data
49 {
50  public:
51   Output_section_incremental_inputs(const Incremental_inputs* inputs,
52                                     const Symbol_table* symtab)
53     : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab)
54   { }
55
56  protected:
57   // Set the final data size.
58   void
59   set_final_data_size();
60
61   // Write the data to the file.
62   void
63   do_write(Output_file*);
64
65   // Write to a map file.
66   void
67   do_print_to_mapfile(Mapfile* mapfile) const
68   { mapfile->print_output_data(this, _("** incremental_inputs")); }
69
70  private:
71   // Write the section header.
72   unsigned char*
73   write_header(unsigned char* pov, unsigned int input_file_count,
74                section_offset_type command_line_offset);
75
76   // Write the input file entries.
77   unsigned char*
78   write_input_files(unsigned char* oview, unsigned char* pov,
79                     Stringpool* strtab);
80
81   // Write the supplemental information blocks.
82   unsigned char*
83   write_info_blocks(unsigned char* oview, unsigned char* pov,
84                     Stringpool* strtab, unsigned int* global_syms,
85                     unsigned int global_sym_count);
86
87   // Write the contents of the .gnu_incremental_symtab section.
88   void
89   write_symtab(unsigned char* pov, unsigned int* global_syms,
90                unsigned int global_sym_count);
91
92   // Write the contents of the .gnu_incremental_got_plt section.
93   void
94   write_got_plt(unsigned char* pov, off_t view_size);
95
96   // Typedefs for writing the data to the output sections.
97   typedef elfcpp::Swap<size, big_endian> Swap;
98   typedef elfcpp::Swap<16, big_endian> Swap16;
99   typedef elfcpp::Swap<32, big_endian> Swap32;
100   typedef elfcpp::Swap<64, big_endian> Swap64;
101
102   // Sizes of various structures.
103   static const int sizeof_addr = size / 8;
104   static const int header_size = 16;
105   static const int input_entry_size = 24;
106
107   // The Incremental_inputs object.
108   const Incremental_inputs* inputs_;
109
110   // The symbol table.
111   const Symbol_table* symtab_;
112 };
113
114 // Inform the user why we don't do an incremental link.  Not called in
115 // the obvious case of missing output file.  TODO: Is this helpful?
116
117 void
118 vexplain_no_incremental(const char* format, va_list args)
119 {
120   char* buf = NULL;
121   if (vasprintf(&buf, format, args) < 0)
122     gold_nomem();
123   gold_info(_("the link might take longer: "
124               "cannot perform incremental link: %s"), buf);
125   free(buf);
126 }
127
128 void
129 explain_no_incremental(const char* format, ...)
130 {
131   va_list args;
132   va_start(args, format);
133   vexplain_no_incremental(format, args);
134   va_end(args);
135 }
136
137 // Report an error.
138
139 void
140 Incremental_binary::error(const char* format, ...) const
141 {
142   va_list args;
143   va_start(args, format);
144   // Current code only checks if the file can be used for incremental linking,
145   // so errors shouldn't fail the build, but only result in a fallback to a
146   // full build.
147   // TODO: when we implement incremental editing of the file, we may need a
148   // flag that will cause errors to be treated seriously.
149   vexplain_no_incremental(format, args);
150   va_end(args);
151 }
152
153 // Find the .gnu_incremental_inputs section and related sections.
154
155 template<int size, bool big_endian>
156 bool
157 Sized_incremental_binary<size, big_endian>::find_incremental_inputs_sections(
158     unsigned int* p_inputs_shndx,
159     unsigned int* p_symtab_shndx,
160     unsigned int* p_relocs_shndx,
161     unsigned int* p_got_plt_shndx,
162     unsigned int* p_strtab_shndx)
163 {
164   unsigned int inputs_shndx =
165       this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
166   if (inputs_shndx == elfcpp::SHN_UNDEF)  // Not found.
167     return false;
168
169   unsigned int symtab_shndx =
170       this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB);
171   if (symtab_shndx == elfcpp::SHN_UNDEF)  // Not found.
172     return false;
173   if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx)
174     return false;
175
176   unsigned int relocs_shndx =
177       this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS);
178   if (relocs_shndx == elfcpp::SHN_UNDEF)  // Not found.
179     return false;
180   if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx)
181     return false;
182
183   unsigned int got_plt_shndx =
184       this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT);
185   if (got_plt_shndx == elfcpp::SHN_UNDEF)  // Not found.
186     return false;
187   if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx)
188     return false;
189
190   unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx);
191   if (strtab_shndx == elfcpp::SHN_UNDEF
192       || strtab_shndx > this->elf_file_.shnum()
193       || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
194     return false;
195
196   if (p_inputs_shndx != NULL)
197     *p_inputs_shndx = inputs_shndx;
198   if (p_symtab_shndx != NULL)
199     *p_symtab_shndx = symtab_shndx;
200   if (p_relocs_shndx != NULL)
201     *p_relocs_shndx = relocs_shndx;
202   if (p_got_plt_shndx != NULL)
203     *p_got_plt_shndx = got_plt_shndx;
204   if (p_strtab_shndx != NULL)
205     *p_strtab_shndx = strtab_shndx;
206   return true;
207 }
208
209 // Set up the readers into the incremental info sections.
210
211 template<int size, bool big_endian>
212 void
213 Sized_incremental_binary<size, big_endian>::setup_readers()
214 {
215   unsigned int inputs_shndx;
216   unsigned int symtab_shndx;
217   unsigned int relocs_shndx;
218   unsigned int got_plt_shndx;
219   unsigned int strtab_shndx;
220
221   if (!this->find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx,
222                                               &relocs_shndx, &got_plt_shndx,
223                                               &strtab_shndx))
224     return;
225
226   Location inputs_location(this->elf_file_.section_contents(inputs_shndx));
227   Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
228   Location relocs_location(this->elf_file_.section_contents(relocs_shndx));
229   Location got_plt_location(this->elf_file_.section_contents(got_plt_shndx));
230   Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
231
232   View inputs_view = this->view(inputs_location);
233   View symtab_view = this->view(symtab_location);
234   View relocs_view = this->view(relocs_location);
235   View got_plt_view = this->view(got_plt_location);
236   View strtab_view = this->view(strtab_location);
237
238   elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
239
240   this->inputs_reader_ =
241       Incremental_inputs_reader<size, big_endian>(inputs_view.data(), strtab);
242   this->symtab_reader_ =
243       Incremental_symtab_reader<big_endian>(symtab_view.data(),
244                                             symtab_location.data_size);
245   this->relocs_reader_ =
246       Incremental_relocs_reader<size, big_endian>(relocs_view.data(),
247                                                   relocs_location.data_size);
248   this->got_plt_reader_ =
249       Incremental_got_plt_reader<big_endian>(got_plt_view.data());
250   this->has_incremental_info_ = true;
251 }
252
253 // Determine whether an incremental link based on the existing output file
254 // can be done.
255
256 template<int size, bool big_endian>
257 bool
258 Sized_incremental_binary<size, big_endian>::do_check_inputs(
259     Incremental_inputs* incremental_inputs)
260 {
261   if (!this->has_incremental_info_)
262     {
263       explain_no_incremental(_("no incremental data from previous build"));
264       return false;
265     }
266
267   if (this->inputs_reader_.version() != INCREMENTAL_LINK_VERSION)
268     {
269       explain_no_incremental(_("different version of incremental build data"));
270       return false;
271     }
272
273   if (incremental_inputs->command_line() != this->inputs_reader_.command_line())
274     {
275       explain_no_incremental(_("command line changed"));
276       return false;
277     }
278
279   return true;
280 }
281
282 // Return TRUE if the file specified by INPUT_ARGUMENT is unchanged
283 // with respect to the base file.
284
285 template<int size, bool big_endian>
286 bool
287 Sized_incremental_binary<size, big_endian>::do_file_is_unchanged(
288     const Input_argument* input_argument) const
289 {
290   Incremental_disposition disp =
291       input_argument->file().options().incremental_disposition();
292
293   if (disp != INCREMENTAL_CHECK)
294     return disp == INCREMENTAL_UNCHANGED;
295
296   // FIXME: Handle INCREMENTAL_CHECK.
297   return false;
298 }
299
300
301 template<int size, bool big_endian>
302 Incremental_binary::Input_reader*
303 Sized_incremental_binary<size, big_endian>::do_get_input_reader(
304     const char*)
305 {
306   unsigned int file_index = this->current_input_file_++;
307   gold_assert(file_index < this->inputs_reader_.input_file_count());
308   return new Sized_input_reader(this->inputs_reader_.input_file(file_index));
309 }
310
311 namespace
312 {
313
314 // Create a Sized_incremental_binary object of the specified size and
315 // endianness. Fails if the target architecture is not supported.
316
317 template<int size, bool big_endian>
318 Incremental_binary*
319 make_sized_incremental_binary(Output_file* file,
320                               const elfcpp::Ehdr<size, big_endian>& ehdr)
321 {
322   Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
323                                  ehdr.get_e_ident()[elfcpp::EI_OSABI],
324                                  ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
325   if (target == NULL)
326     {
327       explain_no_incremental(_("unsupported ELF machine number %d"),
328                ehdr.get_e_machine());
329       return NULL;
330     }
331
332   if (!parameters->target_valid())
333     set_parameters_target(target);
334   else if (target != &parameters->target())
335     gold_error(_("%s: incompatible target"), file->filename());
336
337   return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
338 }
339
340 }  // End of anonymous namespace.
341
342 // Create an Incremental_binary object for FILE.  Returns NULL is this is not
343 // possible, e.g. FILE is not an ELF file or has an unsupported target.  FILE
344 // should be opened.
345
346 Incremental_binary*
347 open_incremental_binary(Output_file* file)
348 {
349   off_t filesize = file->filesize();
350   int want = elfcpp::Elf_recognizer::max_header_size;
351   if (filesize < want)
352     want = filesize;
353
354   const unsigned char* p = file->get_input_view(0, want);
355   if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
356     {
357       explain_no_incremental(_("output is not an ELF file."));
358       return NULL;
359     }
360
361   int size = 0;
362   bool big_endian = false;
363   std::string error;
364   if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
365                                                &error))
366     {
367       explain_no_incremental(error.c_str());
368       return NULL;
369     }
370
371   Incremental_binary* result = NULL;
372   if (size == 32)
373     {
374       if (big_endian)
375         {
376 #ifdef HAVE_TARGET_32_BIG
377           result = make_sized_incremental_binary<32, true>(
378               file, elfcpp::Ehdr<32, true>(p));
379 #else
380           explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
381 #endif
382         }
383       else
384         {
385 #ifdef HAVE_TARGET_32_LITTLE
386           result = make_sized_incremental_binary<32, false>(
387               file, elfcpp::Ehdr<32, false>(p));
388 #else
389           explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
390 #endif
391         }
392     }
393   else if (size == 64)
394     {
395       if (big_endian)
396         {
397 #ifdef HAVE_TARGET_64_BIG
398           result = make_sized_incremental_binary<64, true>(
399               file, elfcpp::Ehdr<64, true>(p));
400 #else
401           explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
402 #endif
403         }
404       else
405         {
406 #ifdef HAVE_TARGET_64_LITTLE
407           result = make_sized_incremental_binary<64, false>(
408               file, elfcpp::Ehdr<64, false>(p));
409 #else
410           explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
411 #endif
412         }
413     }
414   else
415     gold_unreachable();
416
417   return result;
418 }
419
420 // Analyzes the output file to check if incremental linking is possible and
421 // (to be done) what files need to be relinked.
422
423 bool
424 Incremental_checker::can_incrementally_link_output_file()
425 {
426   Output_file output(this->output_name_);
427   if (!output.open_for_modification())
428     return false;
429   Incremental_binary* binary = open_incremental_binary(&output);
430   if (binary == NULL)
431     return false;
432   return binary->check_inputs(this->incremental_inputs_);
433 }
434
435 // Class Incremental_inputs.
436
437 // Add the command line to the string table, setting
438 // command_line_key_.  In incremental builds, the command line is
439 // stored in .gnu_incremental_inputs so that the next linker run can
440 // check if the command line options didn't change.
441
442 void
443 Incremental_inputs::report_command_line(int argc, const char* const* argv)
444 {
445   // Always store 'gold' as argv[0] to avoid a full relink if the user used a
446   // different path to the linker.
447   std::string args("gold");
448   // Copied from collect_argv in main.cc.
449   for (int i = 1; i < argc; ++i)
450     {
451       // Adding/removing these options should not result in a full relink.
452       if (strcmp(argv[i], "--incremental") == 0
453           || strcmp(argv[i], "--incremental-full") == 0
454           || strcmp(argv[i], "--incremental-update") == 0
455           || strcmp(argv[i], "--incremental-changed") == 0
456           || strcmp(argv[i], "--incremental-unchanged") == 0
457           || strcmp(argv[i], "--incremental-unknown") == 0)
458         continue;
459
460       args.append(" '");
461       // Now append argv[i], but with all single-quotes escaped
462       const char* argpos = argv[i];
463       while (1)
464         {
465           const int len = strcspn(argpos, "'");
466           args.append(argpos, len);
467           if (argpos[len] == '\0')
468             break;
469           args.append("'\"'\"'");
470           argpos += len + 1;
471         }
472       args.append("'");
473     }
474
475   this->command_line_ = args;
476   this->strtab_->add(this->command_line_.c_str(), false,
477                      &this->command_line_key_);
478 }
479
480 // Record the input archive file ARCHIVE.  This is called by the
481 // Add_archive_symbols task before determining which archive members
482 // to include.  We create the Incremental_archive_entry here and
483 // attach it to the Archive, but we do not add it to the list of
484 // input objects until report_archive_end is called.
485
486 void
487 Incremental_inputs::report_archive_begin(Library_base* arch,
488                                          Script_info* script_info)
489 {
490   Stringpool::Key filename_key;
491   Timespec mtime = arch->get_mtime();
492
493   this->strtab_->add(arch->filename().c_str(), false, &filename_key);
494   Incremental_archive_entry* entry =
495       new Incremental_archive_entry(filename_key, mtime);
496   arch->set_incremental_info(entry);
497   this->inputs_.push_back(entry);
498
499   if (script_info != NULL)
500     {
501       Incremental_script_entry* script_entry = script_info->incremental_info();
502       gold_assert(script_entry != NULL);
503       script_entry->add_object(entry);
504     }
505 }
506
507 // Visitor class for processing the unused global symbols in a library.
508 // An instance of this class is passed to the library's
509 // for_all_unused_symbols() iterator, which will call the visit()
510 // function for each global symbol defined in each unused library
511 // member.  We add those symbol names to the incremental info for the
512 // library.
513
514 class Unused_symbol_visitor : public Library_base::Symbol_visitor_base
515 {
516  public:
517   Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab)
518     : entry_(entry), strtab_(strtab)
519   { }
520
521   void
522   visit(const char* sym)
523   {
524     Stringpool::Key symbol_key;
525     this->strtab_->add(sym, true, &symbol_key);
526     this->entry_->add_unused_global_symbol(symbol_key);
527   }
528
529  private:
530   Incremental_archive_entry* entry_;
531   Stringpool* strtab_;
532 };
533
534 // Finish recording the input archive file ARCHIVE.  This is called by the
535 // Add_archive_symbols task after determining which archive members
536 // to include.
537
538 void
539 Incremental_inputs::report_archive_end(Library_base* arch)
540 {
541   Incremental_archive_entry* entry = arch->incremental_info();
542
543   gold_assert(entry != NULL);
544
545   // Collect unused global symbols.
546   Unused_symbol_visitor v(entry, this->strtab_);
547   arch->for_all_unused_symbols(&v);
548 }
549
550 // Record the input object file OBJ.  If ARCH is not NULL, attach
551 // the object file to the archive.  This is called by the
552 // Add_symbols task after finding out the type of the file.
553
554 void
555 Incremental_inputs::report_object(Object* obj, Library_base* arch,
556                                   Script_info* script_info)
557 {
558   Stringpool::Key filename_key;
559   Timespec mtime = obj->input_file()->file().get_mtime();
560
561   this->strtab_->add(obj->name().c_str(), false, &filename_key);
562   Incremental_object_entry* obj_entry =
563       new Incremental_object_entry(filename_key, obj, mtime);
564   this->inputs_.push_back(obj_entry);
565
566   if (arch != NULL)
567     {
568       Incremental_archive_entry* arch_entry = arch->incremental_info();
569       gold_assert(arch_entry != NULL);
570       arch_entry->add_object(obj_entry);
571     }
572
573   if (script_info != NULL)
574     {
575       Incremental_script_entry* script_entry = script_info->incremental_info();
576       gold_assert(script_entry != NULL);
577       script_entry->add_object(obj_entry);
578     }
579
580   this->current_object_ = obj;
581   this->current_object_entry_ = obj_entry;
582 }
583
584 // Record the input object file OBJ.  If ARCH is not NULL, attach
585 // the object file to the archive.  This is called by the
586 // Add_symbols task after finding out the type of the file.
587
588 void
589 Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
590                                          const char* name, off_t sh_size)
591 {
592   Stringpool::Key key = 0;
593
594   if (name != NULL)
595       this->strtab_->add(name, true, &key);
596
597   gold_assert(obj == this->current_object_);
598   this->current_object_entry_->add_input_section(shndx, key, sh_size);
599 }
600
601 // Record that the input argument INPUT is a script SCRIPT.  This is
602 // called by read_script after parsing the script and reading the list
603 // of inputs added by this script.
604
605 void
606 Incremental_inputs::report_script(const std::string& filename,
607                                   Script_info* script, Timespec mtime)
608 {
609   Stringpool::Key filename_key;
610
611   this->strtab_->add(filename.c_str(), false, &filename_key);
612   Incremental_script_entry* entry =
613       new Incremental_script_entry(filename_key, script, mtime);
614   this->inputs_.push_back(entry);
615   script->set_incremental_info(entry);
616 }
617
618 // Finalize the incremental link information.  Called from
619 // Layout::finalize.
620
621 void
622 Incremental_inputs::finalize()
623 {
624   // Finalize the string table.
625   this->strtab_->set_string_offsets();
626 }
627
628 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
629
630 void
631 Incremental_inputs::create_data_sections(Symbol_table* symtab)
632 {
633   switch (parameters->size_and_endianness())
634     {
635 #ifdef HAVE_TARGET_32_LITTLE
636     case Parameters::TARGET_32_LITTLE:
637       this->inputs_section_ =
638           new Output_section_incremental_inputs<32, false>(this, symtab);
639       break;
640 #endif
641 #ifdef HAVE_TARGET_32_BIG
642     case Parameters::TARGET_32_BIG:
643       this->inputs_section_ =
644           new Output_section_incremental_inputs<32, true>(this, symtab);
645       break;
646 #endif
647 #ifdef HAVE_TARGET_64_LITTLE
648     case Parameters::TARGET_64_LITTLE:
649       this->inputs_section_ =
650           new Output_section_incremental_inputs<64, false>(this, symtab);
651       break;
652 #endif
653 #ifdef HAVE_TARGET_64_BIG
654     case Parameters::TARGET_64_BIG:
655       this->inputs_section_ =
656           new Output_section_incremental_inputs<64, true>(this, symtab);
657       break;
658 #endif
659     default:
660       gold_unreachable();
661     }
662   this->symtab_section_ = new Output_data_space(4, "** incremental_symtab");
663   this->relocs_section_ = new Output_data_space(4, "** incremental_relocs");
664   this->got_plt_section_ = new Output_data_space(4, "** incremental_got_plt");
665 }
666
667 // Return the sh_entsize value for the .gnu_incremental_relocs section.
668 unsigned int
669 Incremental_inputs::relocs_entsize() const
670 {
671   return 8 + 2 * parameters->target().get_size() / 8;
672 }
673
674 // Class Output_section_incremental_inputs.
675
676 // Finalize the offsets for each input section and supplemental info block,
677 // and set the final data size of the incremental output sections.
678
679 template<int size, bool big_endian>
680 void
681 Output_section_incremental_inputs<size, big_endian>::set_final_data_size()
682 {
683   const Incremental_inputs* inputs = this->inputs_;
684   const unsigned int sizeof_addr = size / 8;
685   const unsigned int rel_size = 8 + 2 * sizeof_addr;
686
687   // Offset of each input entry.
688   unsigned int input_offset = this->header_size;
689
690   // Offset of each supplemental info block.
691   unsigned int info_offset = this->header_size;
692   info_offset += this->input_entry_size * inputs->input_file_count();
693
694   // Count each input file and its supplemental information block.
695   for (Incremental_inputs::Input_list::const_iterator p =
696            inputs->input_files().begin();
697        p != inputs->input_files().end();
698        ++p)
699     {
700       // Set the offset of the input file entry.
701       (*p)->set_offset(input_offset);
702       input_offset += this->input_entry_size;
703
704       // Set the offset of the supplemental info block.
705       switch ((*p)->type())
706         {
707         case INCREMENTAL_INPUT_SCRIPT:
708           {
709             Incremental_script_entry *entry = (*p)->script_entry();
710             gold_assert(entry != NULL);
711             (*p)->set_info_offset(info_offset);
712             // Object count.
713             info_offset += 4;
714             // Each member.
715             info_offset += (entry->get_object_count() * 4);
716           }
717           break;
718         case INCREMENTAL_INPUT_OBJECT:
719         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
720           {
721             Incremental_object_entry* entry = (*p)->object_entry();
722             gold_assert(entry != NULL);
723             (*p)->set_info_offset(info_offset);
724             // Input section count + global symbol count.
725             info_offset += 8;
726             // Each input section.
727             info_offset += (entry->get_input_section_count()
728                             * (8 + 2 * sizeof_addr));
729             // Each global symbol.
730             const Object::Symbols* syms = entry->object()->get_global_symbols();
731             info_offset += syms->size() * 16;
732           }
733           break;
734         case INCREMENTAL_INPUT_SHARED_LIBRARY:
735           {
736             Incremental_object_entry* entry = (*p)->object_entry();
737             gold_assert(entry != NULL);
738             (*p)->set_info_offset(info_offset);
739             // Global symbol count.
740             info_offset += 4;
741             // Each global symbol.
742             const Object::Symbols* syms = entry->object()->get_global_symbols();
743             unsigned int nsyms = syms != NULL ? syms->size() : 0;
744             info_offset += nsyms * 4;
745           }
746           break;
747         case INCREMENTAL_INPUT_ARCHIVE:
748           {
749             Incremental_archive_entry* entry = (*p)->archive_entry();
750             gold_assert(entry != NULL);
751             (*p)->set_info_offset(info_offset);
752             // Member count + unused global symbol count.
753             info_offset += 8;
754             // Each member.
755             info_offset += (entry->get_member_count() * 4);
756             // Each global symbol.
757             info_offset += (entry->get_unused_global_symbol_count() * 4);
758           }
759           break;
760         default:
761           gold_unreachable();
762         }
763     }
764
765   this->set_data_size(info_offset);
766
767   // Set the size of the .gnu_incremental_symtab section.
768   inputs->symtab_section()->set_current_data_size(this->symtab_->output_count()
769                                                   * sizeof(unsigned int));
770
771   // Set the size of the .gnu_incremental_relocs section.
772   inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count()
773                                                   * rel_size);
774
775   // Set the size of the .gnu_incremental_got_plt section.
776   Sized_target<size, big_endian>* target =
777     parameters->sized_target<size, big_endian>();
778   unsigned int got_count = target->got_entry_count();
779   unsigned int plt_count = target->plt_entry_count();
780   unsigned int got_plt_size = 8;  // GOT entry count, PLT entry count.
781   got_plt_size = (got_plt_size + got_count + 3) & ~3;  // GOT type array.
782   got_plt_size += got_count * 4 + plt_count * 4;  // GOT array, PLT array.
783   inputs->got_plt_section()->set_current_data_size(got_plt_size);
784 }
785
786 // Write the contents of the .gnu_incremental_inputs and
787 // .gnu_incremental_symtab sections.
788
789 template<int size, bool big_endian>
790 void
791 Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of)
792 {
793   const Incremental_inputs* inputs = this->inputs_;
794   Stringpool* strtab = inputs->get_stringpool();
795
796   // Get a view into the .gnu_incremental_inputs section.
797   const off_t off = this->offset();
798   const off_t oview_size = this->data_size();
799   unsigned char* const oview = of->get_output_view(off, oview_size);
800   unsigned char* pov = oview;
801
802   // Get a view into the .gnu_incremental_symtab section.
803   const off_t symtab_off = inputs->symtab_section()->offset();
804   const off_t symtab_size = inputs->symtab_section()->data_size();
805   unsigned char* const symtab_view = of->get_output_view(symtab_off,
806                                                          symtab_size);
807
808   // Allocate an array of linked list heads for the .gnu_incremental_symtab
809   // section.  Each element corresponds to a global symbol in the output
810   // symbol table, and points to the head of the linked list that threads
811   // through the object file input entries.  The value of each element
812   // is the section-relative offset to a global symbol entry in a
813   // supplemental information block.
814   unsigned int global_sym_count = this->symtab_->output_count();
815   unsigned int* global_syms = new unsigned int[global_sym_count];
816   memset(global_syms, 0, global_sym_count * sizeof(unsigned int));
817
818   // Write the section header.
819   Stringpool::Key command_line_key = inputs->command_line_key();
820   pov = this->write_header(pov, inputs->input_file_count(),
821                            strtab->get_offset_from_key(command_line_key));
822
823   // Write the list of input files.
824   pov = this->write_input_files(oview, pov, strtab);
825
826   // Write the supplemental information blocks for each input file.
827   pov = this->write_info_blocks(oview, pov, strtab, global_syms,
828                                 global_sym_count);
829
830   gold_assert(pov - oview == oview_size);
831
832   // Write the .gnu_incremental_symtab section.
833   gold_assert(global_sym_count * 4 == symtab_size);
834   this->write_symtab(symtab_view, global_syms, global_sym_count);
835
836   delete[] global_syms;
837
838   // Write the .gnu_incremental_got_plt section.
839   const off_t got_plt_off = inputs->got_plt_section()->offset();
840   const off_t got_plt_size = inputs->got_plt_section()->data_size();
841   unsigned char* const got_plt_view = of->get_output_view(got_plt_off,
842                                                           got_plt_size);
843   this->write_got_plt(got_plt_view, got_plt_size);
844
845   of->write_output_view(off, oview_size, oview);
846   of->write_output_view(symtab_off, symtab_size, symtab_view);
847   of->write_output_view(got_plt_off, got_plt_size, got_plt_view);
848 }
849
850 // Write the section header: version, input file count, offset of command line
851 // in the string table, and 4 bytes of padding.
852
853 template<int size, bool big_endian>
854 unsigned char*
855 Output_section_incremental_inputs<size, big_endian>::write_header(
856     unsigned char* pov,
857     unsigned int input_file_count,
858     section_offset_type command_line_offset)
859 {
860   Swap32::writeval(pov, INCREMENTAL_LINK_VERSION);
861   Swap32::writeval(pov + 4, input_file_count);
862   Swap32::writeval(pov + 8, command_line_offset);
863   Swap32::writeval(pov + 12, 0);
864   return pov + this->header_size;
865 }
866
867 // Write the input file entries.
868
869 template<int size, bool big_endian>
870 unsigned char*
871 Output_section_incremental_inputs<size, big_endian>::write_input_files(
872     unsigned char* oview,
873     unsigned char* pov,
874     Stringpool* strtab)
875 {
876   const Incremental_inputs* inputs = this->inputs_;
877
878   for (Incremental_inputs::Input_list::const_iterator p =
879            inputs->input_files().begin();
880        p != inputs->input_files().end();
881        ++p)
882     {
883       gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset());
884       section_offset_type filename_offset =
885           strtab->get_offset_from_key((*p)->get_filename_key());
886       const Timespec& mtime = (*p)->get_mtime();
887       Swap32::writeval(pov, filename_offset);
888       Swap32::writeval(pov + 4, (*p)->get_info_offset());
889       Swap64::writeval(pov + 8, mtime.seconds);
890       Swap32::writeval(pov + 16, mtime.nanoseconds);
891       Swap16::writeval(pov + 20, (*p)->type());
892       Swap16::writeval(pov + 22, 0);
893       pov += this->input_entry_size;
894     }
895   return pov;
896 }
897
898 // Write the supplemental information blocks.
899
900 template<int size, bool big_endian>
901 unsigned char*
902 Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
903     unsigned char* oview,
904     unsigned char* pov,
905     Stringpool* strtab,
906     unsigned int* global_syms,
907     unsigned int global_sym_count)
908 {
909   const Incremental_inputs* inputs = this->inputs_;
910   unsigned int first_global_index = this->symtab_->first_global_index();
911
912   for (Incremental_inputs::Input_list::const_iterator p =
913            inputs->input_files().begin();
914        p != inputs->input_files().end();
915        ++p)
916     {
917       switch ((*p)->type())
918         {
919         case INCREMENTAL_INPUT_SCRIPT:
920           {
921             gold_assert(static_cast<unsigned int>(pov - oview)
922                         == (*p)->get_info_offset());
923             Incremental_script_entry* entry = (*p)->script_entry();
924             gold_assert(entry != NULL);
925
926             // Write the object count.
927             unsigned int nobjects = entry->get_object_count();
928             Swap32::writeval(pov, nobjects);
929             pov += 4;
930
931             // For each object, write the offset to its input file entry.
932             for (unsigned int i = 0; i < nobjects; ++i)
933               {
934                 Incremental_input_entry* obj = entry->get_object(i);
935                 Swap32::writeval(pov, obj->get_offset());
936                 pov += 4;
937               }
938           }
939           break;
940
941         case INCREMENTAL_INPUT_OBJECT:
942         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
943           {
944             gold_assert(static_cast<unsigned int>(pov - oview)
945                         == (*p)->get_info_offset());
946             Incremental_object_entry* entry = (*p)->object_entry();
947             gold_assert(entry != NULL);
948             const Object* obj = entry->object();
949             const Object::Symbols* syms = obj->get_global_symbols();
950             // Write the input section count and global symbol count.
951             unsigned int nsections = entry->get_input_section_count();
952             unsigned int nsyms = syms->size();
953             Swap32::writeval(pov, nsections);
954             Swap32::writeval(pov + 4, nsyms);
955             pov += 8;
956
957             // For each input section, write the name, output section index,
958             // offset within output section, and input section size.
959             for (unsigned int i = 0; i < nsections; i++)
960               {
961                 Stringpool::Key key = entry->get_input_section_name_key(i);
962                 off_t name_offset = 0;
963                 if (key != 0)
964                   name_offset = strtab->get_offset_from_key(key);
965                 int out_shndx = 0;
966                 off_t out_offset = 0;
967                 off_t sh_size = 0;
968                 Output_section* os = obj->output_section(i);
969                 if (os != NULL)
970                   {
971                     out_shndx = os->out_shndx();
972                     out_offset = obj->output_section_offset(i);
973                     sh_size = entry->get_input_section_size(i);
974                   }
975                 Swap32::writeval(pov, name_offset);
976                 Swap32::writeval(pov + 4, out_shndx);
977                 Swap::writeval(pov + 8, out_offset);
978                 Swap::writeval(pov + 8 + sizeof_addr, sh_size);
979                 pov += 8 + 2 * sizeof_addr;
980               }
981
982             // For each global symbol, write its associated relocations,
983             // add it to the linked list of globals, then write the
984             // supplemental information:  global symbol table index,
985             // linked list chain pointer, relocation count, and offset
986             // to the relocations.
987             for (unsigned int i = 0; i < nsyms; i++)
988               {
989                 const Symbol* sym = (*syms)[i];
990                 if (sym->is_forwarder())
991                   sym = this->symtab_->resolve_forwards(sym);
992                 unsigned int symtab_index = sym->symtab_index();
993                 unsigned int chain = 0;
994                 unsigned int first_reloc = 0;
995                 unsigned int nrelocs = obj->get_incremental_reloc_count(i);
996                 if (nrelocs > 0)
997                   {
998                     gold_assert(symtab_index != -1U
999                                 && (symtab_index - first_global_index
1000                                     < global_sym_count));
1001                     first_reloc = obj->get_incremental_reloc_base(i);
1002                     chain = global_syms[symtab_index - first_global_index];
1003                     global_syms[symtab_index - first_global_index] =
1004                         pov - oview;
1005                   }
1006                 Swap32::writeval(pov, symtab_index);
1007                 Swap32::writeval(pov + 4, chain);
1008                 Swap32::writeval(pov + 8, nrelocs);
1009                 Swap32::writeval(pov + 12, first_reloc * 3 * sizeof_addr);
1010                 pov += 16;
1011               }
1012           }
1013           break;
1014
1015         case INCREMENTAL_INPUT_SHARED_LIBRARY:
1016           {
1017             gold_assert(static_cast<unsigned int>(pov - oview)
1018                         == (*p)->get_info_offset());
1019             Incremental_object_entry* entry = (*p)->object_entry();
1020             gold_assert(entry != NULL);
1021             const Object* obj = entry->object();
1022             const Object::Symbols* syms = obj->get_global_symbols();
1023
1024             // Write the global symbol count.
1025             unsigned int nsyms = syms != NULL ? syms->size() : 0;
1026             Swap32::writeval(pov, nsyms);
1027             pov += 4;
1028
1029             // For each global symbol, write the global symbol table index.
1030             for (unsigned int i = 0; i < nsyms; i++)
1031               {
1032                 const Symbol* sym = (*syms)[i];
1033                 Swap32::writeval(pov, sym->symtab_index());
1034                 pov += 4;
1035               }
1036           }
1037           break;
1038
1039         case INCREMENTAL_INPUT_ARCHIVE:
1040           {
1041             gold_assert(static_cast<unsigned int>(pov - oview)
1042                         == (*p)->get_info_offset());
1043             Incremental_archive_entry* entry = (*p)->archive_entry();
1044             gold_assert(entry != NULL);
1045
1046             // Write the member count and unused global symbol count.
1047             unsigned int nmembers = entry->get_member_count();
1048             unsigned int nsyms = entry->get_unused_global_symbol_count();
1049             Swap32::writeval(pov, nmembers);
1050             Swap32::writeval(pov + 4, nsyms);
1051             pov += 8;
1052
1053             // For each member, write the offset to its input file entry.
1054             for (unsigned int i = 0; i < nmembers; ++i)
1055               {
1056                 Incremental_object_entry* member = entry->get_member(i);
1057                 Swap32::writeval(pov, member->get_offset());
1058                 pov += 4;
1059               }
1060
1061             // For each global symbol, write the name offset.
1062             for (unsigned int i = 0; i < nsyms; ++i)
1063               {
1064                 Stringpool::Key key = entry->get_unused_global_symbol(i);
1065                 Swap32::writeval(pov, strtab->get_offset_from_key(key));
1066                 pov += 4;
1067               }
1068           }
1069           break;
1070
1071         default:
1072           gold_unreachable();
1073         }
1074     }
1075   return pov;
1076 }
1077
1078 // Write the contents of the .gnu_incremental_symtab section.
1079
1080 template<int size, bool big_endian>
1081 void
1082 Output_section_incremental_inputs<size, big_endian>::write_symtab(
1083     unsigned char* pov,
1084     unsigned int* global_syms,
1085     unsigned int global_sym_count)
1086 {
1087   for (unsigned int i = 0; i < global_sym_count; ++i)
1088     {
1089       Swap32::writeval(pov, global_syms[i]);
1090       pov += 4;
1091     }
1092 }
1093
1094 // This struct holds the view information needed to write the
1095 // .gnu_incremental_got_plt section.
1096
1097 struct Got_plt_view_info
1098 {
1099   // Start of the GOT type array in the output view.
1100   unsigned char* got_type_p;
1101   // Start of the GOT descriptor array in the output view.
1102   unsigned char* got_desc_p;
1103   // Start of the PLT descriptor array in the output view.
1104   unsigned char* plt_desc_p;
1105   // Number of GOT entries.
1106   unsigned int got_count;
1107   // Number of PLT entries.
1108   unsigned int plt_count;
1109   // Offset of the first non-reserved PLT entry (this is a target-dependent value).
1110   unsigned int first_plt_entry_offset;
1111   // Size of a PLT entry (this is a target-dependent value).
1112   unsigned int plt_entry_size;
1113   // Value to write in the GOT descriptor array.  For global symbols,
1114   // this is the global symbol table index; for local symbols, it is
1115   // the offset of the input file entry in the .gnu_incremental_inputs
1116   // section.
1117   unsigned int got_descriptor;
1118 };
1119
1120 // Functor class for processing a GOT offset list for local symbols.
1121 // Writes the GOT type and symbol index into the GOT type and descriptor
1122 // arrays in the output section.
1123
1124 template<int size, bool big_endian>
1125 class Local_got_offset_visitor
1126 {
1127  public:
1128   Local_got_offset_visitor(struct Got_plt_view_info& info)
1129     : info_(info)
1130   { }
1131
1132   void
1133   operator()(unsigned int got_type, unsigned int got_offset)
1134   {
1135     unsigned int got_index = got_offset / this->got_entry_size_;
1136     gold_assert(got_index < this->info_.got_count);
1137     // We can only handle GOT entry types in the range 0..0x7e
1138     // because we use a byte array to store them, and we use the
1139     // high bit to flag a local symbol.
1140     gold_assert(got_type < 0x7f);
1141     this->info_.got_type_p[got_index] = got_type | 0x80;
1142     unsigned char* pov = this->info_.got_desc_p + got_index * 4;
1143     elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.got_descriptor);
1144   }
1145
1146  private:
1147   static const unsigned int got_entry_size_ = size / 8;
1148   struct Got_plt_view_info& info_;
1149 };
1150
1151 // Functor class for processing a GOT offset list.  Writes the GOT type
1152 // and symbol index into the GOT type and descriptor arrays in the output
1153 // section.
1154
1155 template<int size, bool big_endian>
1156 class Global_got_offset_visitor
1157 {
1158  public:
1159   Global_got_offset_visitor(struct Got_plt_view_info& info)
1160     : info_(info)
1161   { }
1162
1163   void
1164   operator()(unsigned int got_type, unsigned int got_offset)
1165   {
1166     unsigned int got_index = got_offset / this->got_entry_size_;
1167     gold_assert(got_index < this->info_.got_count);
1168     // We can only handle GOT entry types in the range 0..0x7e
1169     // because we use a byte array to store them, and we use the
1170     // high bit to flag a local symbol.
1171     gold_assert(got_type < 0x7f);
1172     this->info_.got_type_p[got_index] = got_type;
1173     unsigned char* pov = this->info_.got_desc_p + got_index * 4;
1174     elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.got_descriptor);
1175   }
1176
1177  private:
1178   static const unsigned int got_entry_size_ = size / 8;
1179   struct Got_plt_view_info& info_;
1180 };
1181
1182 // Functor class for processing the global symbol table.  Processes the
1183 // GOT offset list for the symbol, and writes the symbol table index
1184 // into the PLT descriptor array in the output section.
1185
1186 template<int size, bool big_endian>
1187 class Global_symbol_visitor_got_plt
1188 {
1189  public:
1190   Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
1191     : info_(info)
1192   { }
1193
1194   void
1195   operator()(const Sized_symbol<size>* sym)
1196   {
1197     typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
1198     const Got_offset_list* got_offsets = sym->got_offset_list();
1199     if (got_offsets != NULL)
1200       {
1201         info_.got_descriptor = sym->symtab_index();
1202         got_offsets->for_all_got_offsets(Got_visitor(info_));
1203       }
1204     if (sym->has_plt_offset())
1205       {
1206         unsigned int plt_index =
1207             ((sym->plt_offset() - this->info_.first_plt_entry_offset)
1208              / this->info_.plt_entry_size);
1209         gold_assert(plt_index < this->info_.plt_count);
1210         unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
1211         elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
1212       }
1213   }
1214
1215  private:
1216   struct Got_plt_view_info& info_;
1217 };
1218
1219 // Write the contents of the .gnu_incremental_got_plt section.
1220
1221 template<int size, bool big_endian>
1222 void
1223 Output_section_incremental_inputs<size, big_endian>::write_got_plt(
1224     unsigned char* pov,
1225     off_t view_size)
1226 {
1227   Sized_target<size, big_endian>* target =
1228     parameters->sized_target<size, big_endian>();
1229
1230   // Set up the view information for the functors.
1231   struct Got_plt_view_info view_info;
1232   view_info.got_count = target->got_entry_count();
1233   view_info.plt_count = target->plt_entry_count();
1234   view_info.first_plt_entry_offset = target->first_plt_entry_offset();
1235   view_info.plt_entry_size = target->plt_entry_size();
1236   view_info.got_type_p = pov + 8;
1237   view_info.got_desc_p = (view_info.got_type_p
1238                           + ((view_info.got_count + 3) & ~3));
1239   view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 4;
1240
1241   gold_assert(pov + view_size ==
1242               view_info.plt_desc_p + view_info.plt_count * 4);
1243
1244   // Write the section header.
1245   Swap32::writeval(pov, view_info.got_count);
1246   Swap32::writeval(pov + 4, view_info.plt_count);
1247
1248   // Initialize the GOT type array to 0xff (reserved).
1249   memset(view_info.got_type_p, 0xff, view_info.got_count);
1250
1251   // Write the incremental GOT descriptors for local symbols.
1252   for (Incremental_inputs::Input_list::const_iterator p =
1253            this->inputs_->input_files().begin();
1254        p != this->inputs_->input_files().end();
1255        ++p)
1256     {
1257       if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
1258           && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
1259         continue;
1260       Incremental_object_entry* entry = (*p)->object_entry();
1261       gold_assert(entry != NULL);
1262       const Sized_relobj<size, big_endian>* obj =
1263           static_cast<Sized_relobj<size, big_endian>*>(entry->object());
1264       gold_assert(obj != NULL);
1265       unsigned int nsyms = obj->local_symbol_count();
1266       for (unsigned int i = 0; i < nsyms; i++)
1267         {
1268           const Got_offset_list* got_offsets = obj->local_got_offset_list(i);
1269           if (got_offsets != NULL)
1270             {
1271               typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
1272               view_info.got_descriptor = (*p)->get_offset();
1273               got_offsets->for_all_got_offsets(Got_visitor(view_info));
1274             }
1275         }
1276     }
1277
1278   // Write the incremental GOT and PLT descriptors for global symbols.
1279   typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
1280   symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
1281 }
1282
1283 // Instantiate the templates we need.
1284
1285 #ifdef HAVE_TARGET_32_LITTLE
1286 template
1287 class Sized_incremental_binary<32, false>;
1288 #endif
1289
1290 #ifdef HAVE_TARGET_32_BIG
1291 template
1292 class Sized_incremental_binary<32, true>;
1293 #endif
1294
1295 #ifdef HAVE_TARGET_64_LITTLE
1296 template
1297 class Sized_incremental_binary<64, false>;
1298 #endif
1299
1300 #ifdef HAVE_TARGET_64_BIG
1301 template
1302 class Sized_incremental_binary<64, true>;
1303 #endif
1304
1305 } // End namespace gold.