* gold/incremental.cc
[external/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 <set>
26 #include <cstdarg>
27 #include "libiberty.h"
28
29 #include "elfcpp.h"
30 #include "options.h"
31 #include "output.h"
32 #include "symtab.h"
33 #include "incremental.h"
34 #include "archive.h"
35 #include "object.h"
36 #include "output.h"
37 #include "target-select.h"
38 #include "target.h"
39 #include "fileread.h"
40 #include "script.h"
41
42 namespace gold {
43
44 // Version information. Will change frequently during the development, later
45 // we could think about backward (and forward?) compatibility.
46 const unsigned int INCREMENTAL_LINK_VERSION = 1;
47
48 // This class manages the .gnu_incremental_inputs section, which holds
49 // the header information, a directory of input files, and separate
50 // entries for each input file.
51
52 template<int size, bool big_endian>
53 class Output_section_incremental_inputs : public Output_section_data
54 {
55  public:
56   Output_section_incremental_inputs(const Incremental_inputs* inputs,
57                                     const Symbol_table* symtab)
58     : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab)
59   { }
60
61  protected:
62   // This is called to update the section size prior to assigning
63   // the address and file offset.
64   void
65   update_data_size()
66   { this->set_final_data_size(); }
67
68   // Set the final data size.
69   void
70   set_final_data_size();
71
72   // Write the data to the file.
73   void
74   do_write(Output_file*);
75
76   // Write to a map file.
77   void
78   do_print_to_mapfile(Mapfile* mapfile) const
79   { mapfile->print_output_data(this, _("** incremental_inputs")); }
80
81  private:
82   // Write the section header.
83   unsigned char*
84   write_header(unsigned char* pov, unsigned int input_file_count,
85                section_offset_type command_line_offset);
86
87   // Write the input file entries.
88   unsigned char*
89   write_input_files(unsigned char* oview, unsigned char* pov,
90                     Stringpool* strtab);
91
92   // Write the supplemental information blocks.
93   unsigned char*
94   write_info_blocks(unsigned char* oview, unsigned char* pov,
95                     Stringpool* strtab, unsigned int* global_syms,
96                     unsigned int global_sym_count);
97
98   // Write the contents of the .gnu_incremental_symtab section.
99   void
100   write_symtab(unsigned char* pov, unsigned int* global_syms,
101                unsigned int global_sym_count);
102
103   // Write the contents of the .gnu_incremental_got_plt section.
104   void
105   write_got_plt(unsigned char* pov, off_t view_size);
106
107   // Typedefs for writing the data to the output sections.
108   typedef elfcpp::Swap<size, big_endian> Swap;
109   typedef elfcpp::Swap<16, big_endian> Swap16;
110   typedef elfcpp::Swap<32, big_endian> Swap32;
111   typedef elfcpp::Swap<64, big_endian> Swap64;
112
113   // Sizes of various structures.
114   static const int sizeof_addr = size / 8;
115   static const int header_size = 16;
116   static const int input_entry_size = 24;
117
118   // The Incremental_inputs object.
119   const Incremental_inputs* inputs_;
120
121   // The symbol table.
122   const Symbol_table* symtab_;
123 };
124
125 // Inform the user why we don't do an incremental link.  Not called in
126 // the obvious case of missing output file.  TODO: Is this helpful?
127
128 void
129 vexplain_no_incremental(const char* format, va_list args)
130 {
131   char* buf = NULL;
132   if (vasprintf(&buf, format, args) < 0)
133     gold_nomem();
134   gold_info(_("the link might take longer: "
135               "cannot perform incremental link: %s"), buf);
136   free(buf);
137 }
138
139 void
140 explain_no_incremental(const char* format, ...)
141 {
142   va_list args;
143   va_start(args, format);
144   vexplain_no_incremental(format, args);
145   va_end(args);
146 }
147
148 // Report an error.
149
150 void
151 Incremental_binary::error(const char* format, ...) const
152 {
153   va_list args;
154   va_start(args, format);
155   // Current code only checks if the file can be used for incremental linking,
156   // so errors shouldn't fail the build, but only result in a fallback to a
157   // full build.
158   // TODO: when we implement incremental editing of the file, we may need a
159   // flag that will cause errors to be treated seriously.
160   vexplain_no_incremental(format, args);
161   va_end(args);
162 }
163
164 // Find the .gnu_incremental_inputs section and related sections.
165
166 template<int size, bool big_endian>
167 bool
168 Sized_incremental_binary<size, big_endian>::find_incremental_inputs_sections(
169     unsigned int* p_inputs_shndx,
170     unsigned int* p_symtab_shndx,
171     unsigned int* p_relocs_shndx,
172     unsigned int* p_got_plt_shndx,
173     unsigned int* p_strtab_shndx)
174 {
175   unsigned int inputs_shndx =
176       this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
177   if (inputs_shndx == elfcpp::SHN_UNDEF)  // Not found.
178     return false;
179
180   unsigned int symtab_shndx =
181       this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB);
182   if (symtab_shndx == elfcpp::SHN_UNDEF)  // Not found.
183     return false;
184   if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx)
185     return false;
186
187   unsigned int relocs_shndx =
188       this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS);
189   if (relocs_shndx == elfcpp::SHN_UNDEF)  // Not found.
190     return false;
191   if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx)
192     return false;
193
194   unsigned int got_plt_shndx =
195       this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT);
196   if (got_plt_shndx == elfcpp::SHN_UNDEF)  // Not found.
197     return false;
198   if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx)
199     return false;
200
201   unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx);
202   if (strtab_shndx == elfcpp::SHN_UNDEF
203       || strtab_shndx > this->elf_file_.shnum()
204       || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
205     return false;
206
207   if (p_inputs_shndx != NULL)
208     *p_inputs_shndx = inputs_shndx;
209   if (p_symtab_shndx != NULL)
210     *p_symtab_shndx = symtab_shndx;
211   if (p_relocs_shndx != NULL)
212     *p_relocs_shndx = relocs_shndx;
213   if (p_got_plt_shndx != NULL)
214     *p_got_plt_shndx = got_plt_shndx;
215   if (p_strtab_shndx != NULL)
216     *p_strtab_shndx = strtab_shndx;
217   return true;
218 }
219
220 // Set up the readers into the incremental info sections.
221
222 template<int size, bool big_endian>
223 void
224 Sized_incremental_binary<size, big_endian>::setup_readers()
225 {
226   unsigned int inputs_shndx;
227   unsigned int symtab_shndx;
228   unsigned int relocs_shndx;
229   unsigned int got_plt_shndx;
230   unsigned int strtab_shndx;
231
232   if (!this->find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx,
233                                               &relocs_shndx, &got_plt_shndx,
234                                               &strtab_shndx))
235     return;
236
237   Location inputs_location(this->elf_file_.section_contents(inputs_shndx));
238   Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
239   Location relocs_location(this->elf_file_.section_contents(relocs_shndx));
240   Location got_plt_location(this->elf_file_.section_contents(got_plt_shndx));
241   Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
242
243   View inputs_view = this->view(inputs_location);
244   View symtab_view = this->view(symtab_location);
245   View relocs_view = this->view(relocs_location);
246   View got_plt_view = this->view(got_plt_location);
247   View strtab_view = this->view(strtab_location);
248
249   elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
250
251   this->inputs_reader_ =
252       Incremental_inputs_reader<size, big_endian>(inputs_view.data(), strtab);
253   this->symtab_reader_ =
254       Incremental_symtab_reader<big_endian>(symtab_view.data(),
255                                             symtab_location.data_size);
256   this->relocs_reader_ =
257       Incremental_relocs_reader<size, big_endian>(relocs_view.data(),
258                                                   relocs_location.data_size);
259   this->got_plt_reader_ =
260       Incremental_got_plt_reader<big_endian>(got_plt_view.data());
261
262   // Find the main symbol table.
263   unsigned int main_symtab_shndx =
264       this->elf_file_.find_section_by_type(elfcpp::SHT_SYMTAB);
265   gold_assert(main_symtab_shndx != elfcpp::SHN_UNDEF);
266   this->main_symtab_loc_ = this->elf_file_.section_contents(main_symtab_shndx);
267
268   // Find the main symbol string table.
269   unsigned int main_strtab_shndx =
270       this->elf_file_.section_link(main_symtab_shndx);
271   gold_assert(main_strtab_shndx != elfcpp::SHN_UNDEF
272               && main_strtab_shndx < this->elf_file_.shnum());
273   this->main_strtab_loc_ = this->elf_file_.section_contents(main_strtab_shndx);
274
275   // Walk the list of input files (a) to setup an Input_reader for each
276   // input file, and (b) to record maps of files added from archive
277   // libraries and scripts.
278   Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
279   unsigned int count = inputs.input_file_count();
280   this->input_objects_.resize(count);
281   this->input_entry_readers_.reserve(count);
282   this->library_map_.resize(count);
283   this->script_map_.resize(count);
284   for (unsigned int i = 0; i < count; i++)
285     {
286       Input_entry_reader input_file = inputs.input_file(i);
287       this->input_entry_readers_.push_back(Sized_input_reader(input_file));
288       switch (input_file.type())
289         {
290         case INCREMENTAL_INPUT_OBJECT:
291         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
292         case INCREMENTAL_INPUT_SHARED_LIBRARY:
293           // No special treatment necessary.
294           break;
295         case INCREMENTAL_INPUT_ARCHIVE:
296           {
297             Incremental_library* lib =
298                 new Incremental_library(input_file.filename(), i,
299                                         &this->input_entry_readers_[i]);
300             this->library_map_[i] = lib;
301             unsigned int member_count = input_file.get_member_count();
302             for (unsigned int j = 0; j < member_count; j++)
303               {
304                 int member_offset = input_file.get_member_offset(j);
305                 int member_index = inputs.input_file_index(member_offset);
306                 this->library_map_[member_index] = lib;
307               }
308           }
309           break;
310         case INCREMENTAL_INPUT_SCRIPT:
311           {
312             Script_info* script = new Script_info(input_file.filename(), i);
313             this->script_map_[i] = script;
314             unsigned int object_count = input_file.get_object_count();
315             for (unsigned int j = 0; j < object_count; j++)
316               {
317                 int object_offset = input_file.get_object_offset(j);
318                 int object_index = inputs.input_file_index(object_offset);
319                 this->script_map_[object_index] = script;
320               }
321           }
322           break;
323         default:
324           gold_unreachable();
325         }
326     }
327
328   // Initialize the map of global symbols.
329   unsigned int nglobals = this->symtab_reader_.symbol_count();
330   this->symbol_map_.resize(nglobals);
331
332   this->has_incremental_info_ = true;
333 }
334
335 // Walk the list of input files given on the command line, and build
336 // a direct map of file index to the corresponding input argument.
337
338 void
339 check_input_args(std::vector<const Input_argument*>& input_args_map,
340                  Input_arguments::const_iterator begin,
341                  Input_arguments::const_iterator end)
342 {
343   for (Input_arguments::const_iterator p = begin;
344        p != end;
345        ++p)
346     {
347       if (p->is_group())
348         {
349           const Input_file_group* group = p->group();
350           check_input_args(input_args_map, group->begin(), group->end());
351         }
352       else if (p->is_lib())
353         {
354           const Input_file_lib* lib = p->lib();
355           check_input_args(input_args_map, lib->begin(), lib->end());
356         }
357       else
358         {
359           gold_assert(p->is_file());
360           unsigned int arg_serial = p->file().arg_serial();
361           if (arg_serial > 0)
362             {
363               gold_assert(arg_serial <= input_args_map.size());
364               gold_assert(input_args_map[arg_serial - 1] == 0);
365               input_args_map[arg_serial - 1] = &*p;
366             }
367         }
368     }
369 }
370
371 // Determine whether an incremental link based on the existing output file
372 // can be done.
373
374 template<int size, bool big_endian>
375 bool
376 Sized_incremental_binary<size, big_endian>::do_check_inputs(
377     const Command_line& cmdline,
378     Incremental_inputs* incremental_inputs)
379 {
380   Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
381
382   if (!this->has_incremental_info_)
383     {
384       explain_no_incremental(_("no incremental data from previous build"));
385       return false;
386     }
387
388   if (inputs.version() != INCREMENTAL_LINK_VERSION)
389     {
390       explain_no_incremental(_("different version of incremental build data"));
391       return false;
392     }
393
394   if (incremental_inputs->command_line() != inputs.command_line())
395     {
396       gold_debug(DEBUG_INCREMENTAL,
397                  "old command line: %s",
398                  inputs.command_line());
399       gold_debug(DEBUG_INCREMENTAL,
400                  "new command line: %s",
401                  incremental_inputs->command_line().c_str());
402       explain_no_incremental(_("command line changed"));
403       return false;
404     }
405
406   // Walk the list of input files given on the command line, and build
407   // a direct map of argument serial numbers to the corresponding input
408   // arguments.
409   this->input_args_map_.resize(cmdline.number_of_input_files());
410   check_input_args(this->input_args_map_, cmdline.begin(), cmdline.end());
411
412   // Walk the list of input files to check for conditions that prevent
413   // an incremental update link.
414   unsigned int count = inputs.input_file_count();
415   for (unsigned int i = 0; i < count; i++)
416     {
417       Input_entry_reader input_file = inputs.input_file(i);
418       switch (input_file.type())
419         {
420         case INCREMENTAL_INPUT_OBJECT:
421         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
422         case INCREMENTAL_INPUT_SHARED_LIBRARY:
423         case INCREMENTAL_INPUT_ARCHIVE:
424           // No special treatment necessary.
425           break;
426         case INCREMENTAL_INPUT_SCRIPT:
427           if (this->do_file_has_changed(i))
428             {
429               explain_no_incremental(_("%s: script file changed"),
430                                      input_file.filename());
431               return false;
432             }
433           break;
434         default:
435           gold_unreachable();
436         }
437     }
438
439   return true;
440 }
441
442 // Return TRUE if input file N has changed since the last incremental link.
443
444 template<int size, bool big_endian>
445 bool
446 Sized_incremental_binary<size, big_endian>::do_file_has_changed(
447     unsigned int n) const
448 {
449   Input_entry_reader input_file = this->inputs_reader_.input_file(n);
450   Incremental_disposition disp = INCREMENTAL_CHECK;
451
452   // For files named in scripts, find the file that was actually named
453   // on the command line, so that we can get the incremental disposition
454   // flag.
455   Script_info* script = this->get_script_info(n);
456   if (script != NULL)
457     n = script->input_file_index();
458
459   const Input_argument* input_argument = this->get_input_argument(n);
460   if (input_argument != NULL)
461     disp = input_argument->file().options().incremental_disposition();
462
463   // For files at the beginning of the command line (i.e., those added
464   // implicitly by gcc), check whether the --incremental-startup-unchanged
465   // option was used.
466   if (disp == INCREMENTAL_STARTUP)
467     disp = parameters->options().incremental_startup_disposition();
468
469   if (disp != INCREMENTAL_CHECK)
470     return disp == INCREMENTAL_CHANGED;
471
472   const char* filename = input_file.filename();
473   Timespec old_mtime = input_file.get_mtime();
474   Timespec new_mtime;
475   if (!get_mtime(filename, &new_mtime))
476     {
477       // If we can't open get the current modification time, assume it has
478       // changed.  If the file doesn't exist, we'll issue an error when we
479       // try to open it later.
480       return true;
481     }
482
483   if (new_mtime.seconds > old_mtime.seconds)
484     return true;
485   if (new_mtime.seconds == old_mtime.seconds
486       && new_mtime.nanoseconds > old_mtime.nanoseconds)
487     return true;
488   return false;
489 }
490
491 // Initialize the layout of the output file based on the existing
492 // output file.
493
494 template<int size, bool big_endian>
495 void
496 Sized_incremental_binary<size, big_endian>::do_init_layout(Layout* layout)
497 {
498   typedef elfcpp::Shdr<size, big_endian> Shdr;
499   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
500
501   // Get views of the section headers and the section string table.
502   const off_t shoff = this->elf_file_.shoff();
503   const unsigned int shnum = this->elf_file_.shnum();
504   const unsigned int shstrndx = this->elf_file_.shstrndx();
505   Location shdrs_location(shoff, shnum * shdr_size);
506   Location shstrndx_location(this->elf_file_.section_contents(shstrndx));
507   View shdrs_view = this->view(shdrs_location);
508   View shstrndx_view = this->view(shstrndx_location);
509   elfcpp::Elf_strtab shstrtab(shstrndx_view.data(),
510                               shstrndx_location.data_size);
511
512   layout->set_incremental_base(this);
513
514   // Initialize the layout.
515   this->section_map_.resize(shnum);
516   const unsigned char* pshdr = shdrs_view.data() + shdr_size;
517   for (unsigned int i = 1; i < shnum; i++)
518     {
519       Shdr shdr(pshdr);
520       const char* name;
521       if (!shstrtab.get_c_string(shdr.get_sh_name(), &name))
522         name = NULL;
523       gold_debug(DEBUG_INCREMENTAL,
524                  "Output section: %2d %08lx %08lx %08lx %3d %s",
525                  i,
526                  static_cast<long>(shdr.get_sh_addr()),
527                  static_cast<long>(shdr.get_sh_offset()),
528                  static_cast<long>(shdr.get_sh_size()),
529                  shdr.get_sh_type(), name ? name : "<null>");
530       this->section_map_[i] = layout->init_fixed_output_section(name, shdr);
531       pshdr += shdr_size;
532     }
533 }
534
535 // Mark regions of the input file that must be kept unchanged.
536
537 template<int size, bool big_endian>
538 void
539 Sized_incremental_binary<size, big_endian>::do_reserve_layout(
540     unsigned int input_file_index)
541 {
542   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
543
544   Input_entry_reader input_file =
545       this->inputs_reader_.input_file(input_file_index);
546
547   if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
548     {
549       // Reserve the BSS space used for COPY relocations.
550       unsigned int nsyms = input_file.get_global_symbol_count();
551       Incremental_binary::View symtab_view(NULL);
552       unsigned int symtab_count;
553       elfcpp::Elf_strtab strtab(NULL, 0);
554       this->get_symtab_view(&symtab_view, &symtab_count, &strtab);
555       for (unsigned int i = 0; i < nsyms; ++i)
556         {
557           bool is_def;
558           bool is_copy;
559           unsigned int output_symndx =
560               input_file.get_output_symbol_index(i, &is_def, &is_copy);
561           if (is_copy)
562             {
563               const unsigned char* sym_p = (symtab_view.data()
564                                             + output_symndx * sym_size);
565               elfcpp::Sym<size, big_endian> gsym(sym_p);
566               unsigned int shndx = gsym.get_st_shndx();
567               if (shndx < 1 || shndx >= this->section_map_.size())
568                 continue;
569               Output_section* os = this->section_map_[shndx];
570               off_t offset = gsym.get_st_value() - os->address();
571               os->reserve(offset, gsym.get_st_size());
572               gold_debug(DEBUG_INCREMENTAL,
573                          "Reserve for COPY reloc: %s, off %d, size %d",
574                          os->name(),
575                          static_cast<int>(offset),
576                          static_cast<int>(gsym.get_st_size()));
577             }
578         }
579       return;
580     }
581
582   unsigned int shnum = input_file.get_input_section_count();
583   for (unsigned int i = 0; i < shnum; i++)
584     {
585       typename Input_entry_reader::Input_section_info sect =
586           input_file.get_input_section(i);
587       if (sect.output_shndx == 0 || sect.sh_offset == -1)
588         continue;
589       Output_section* os = this->section_map_[sect.output_shndx];
590       gold_assert(os != NULL);
591       os->reserve(sect.sh_offset, sect.sh_size);
592     }
593 }
594
595 // Process the GOT and PLT entries from the existing output file.
596
597 template<int size, bool big_endian>
598 void
599 Sized_incremental_binary<size, big_endian>::do_process_got_plt(
600     Symbol_table* symtab,
601     Layout* layout)
602 {
603   Incremental_got_plt_reader<big_endian> got_plt_reader(this->got_plt_reader());
604   Sized_target<size, big_endian>* target =
605       parameters->sized_target<size, big_endian>();
606
607   // Get the number of symbols in the main symbol table and in the
608   // incremental symbol table.  The difference between the two counts
609   // is the index of the first forced-local or global symbol in the
610   // main symbol table.
611   unsigned int symtab_count =
612       this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
613   unsigned int isym_count = this->symtab_reader_.symbol_count();
614   unsigned int first_global = symtab_count - isym_count;
615
616   // Tell the target how big the GOT and PLT sections are.
617   unsigned int got_count = got_plt_reader.get_got_entry_count();
618   unsigned int plt_count = got_plt_reader.get_plt_entry_count();
619   Output_data_got<size, big_endian>* got =
620       target->init_got_plt_for_update(symtab, layout, got_count, plt_count);
621
622   // Read the GOT entries from the base file and build the outgoing GOT.
623   for (unsigned int i = 0; i < got_count; ++i)
624     {
625       unsigned int got_type = got_plt_reader.get_got_type(i);
626       if ((got_type & 0x7f) == 0x7f)
627         {
628           // This is the second entry of a pair.
629           got->reserve_slot(i);
630           continue;
631         }
632       unsigned int symndx = got_plt_reader.get_got_symndx(i);
633       if (got_type & 0x80)
634         {
635           // This is an entry for a local symbol.  Ignore this entry if
636           // the object file was replaced.
637           unsigned int input_index = got_plt_reader.get_got_input_index(i);
638           gold_debug(DEBUG_INCREMENTAL,
639                      "GOT entry %d, type %02x: (local symbol)",
640                      i, got_type & 0x7f);
641           Sized_relobj_incr<size, big_endian>* obj =
642               this->input_object(input_index);
643           if (obj != NULL)
644             target->reserve_local_got_entry(i, obj, symndx, got_type & 0x7f);
645         }
646       else
647         {
648           // This is an entry for a global symbol.  GOT_DESC is the symbol
649           // table index.
650           // FIXME: This should really be a fatal error (corrupt input).
651           gold_assert(symndx >= first_global && symndx < symtab_count);
652           Symbol* sym = this->global_symbol(symndx - first_global);
653           // Add the GOT entry only if the symbol is still referenced.
654           if (sym != NULL && sym->in_reg())
655             {
656               gold_debug(DEBUG_INCREMENTAL,
657                          "GOT entry %d, type %02x: %s",
658                          i, got_type, sym->name());
659               target->reserve_global_got_entry(i, sym, got_type);
660             }
661         }
662     }
663
664   // Read the PLT entries from the base file and pass each to the target.
665   for (unsigned int i = 0; i < plt_count; ++i)
666     {
667       unsigned int plt_desc = got_plt_reader.get_plt_desc(i);
668       // FIXME: This should really be a fatal error (corrupt input).
669       gold_assert(plt_desc >= first_global && plt_desc < symtab_count);
670       Symbol* sym = this->global_symbol(plt_desc - first_global);
671       // Add the PLT entry only if the symbol is still referenced.
672       if (sym->in_reg())
673         {
674           gold_debug(DEBUG_INCREMENTAL,
675                      "PLT entry %d: %s",
676                      i, sym->name());
677           target->register_global_plt_entry(i, sym);
678         }
679     }
680 }
681
682 // Emit COPY relocations from the existing output file.
683
684 template<int size, bool big_endian>
685 void
686 Sized_incremental_binary<size, big_endian>::do_emit_copy_relocs(
687     Symbol_table* symtab)
688 {
689   Sized_target<size, big_endian>* target =
690       parameters->sized_target<size, big_endian>();
691
692   for (typename Copy_relocs::iterator p = this->copy_relocs_.begin();
693        p != this->copy_relocs_.end();
694        ++p)
695     {
696       if (!(*p).symbol->is_copied_from_dynobj())
697         target->emit_copy_reloc(symtab, (*p).symbol, (*p).output_section,
698                                 (*p).offset);
699     }
700 }
701
702 // Apply incremental relocations for symbols whose values have changed.
703
704 template<int size, bool big_endian>
705 void
706 Sized_incremental_binary<size, big_endian>::do_apply_incremental_relocs(
707     const Symbol_table* symtab,
708     Layout* layout,
709     Output_file* of)
710 {
711   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
712   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
713   Incremental_symtab_reader<big_endian> isymtab(this->symtab_reader());
714   Incremental_relocs_reader<size, big_endian> irelocs(this->relocs_reader());
715   unsigned int nglobals = isymtab.symbol_count();
716   const unsigned int incr_reloc_size = irelocs.reloc_size;
717
718   Relocate_info<size, big_endian> relinfo;
719   relinfo.symtab = symtab;
720   relinfo.layout = layout;
721   relinfo.object = NULL;
722   relinfo.reloc_shndx = 0;
723   relinfo.reloc_shdr = NULL;
724   relinfo.data_shndx = 0;
725   relinfo.data_shdr = NULL;
726
727   Sized_target<size, big_endian>* target =
728       parameters->sized_target<size, big_endian>();
729
730   for (unsigned int i = 0; i < nglobals; i++)
731     {
732       const Symbol* gsym = this->global_symbol(i);
733
734       // If the symbol is not referenced from any unchanged input files,
735       // we do not need to reapply any of its relocations.
736       if (gsym == NULL)
737         continue;
738
739       // If the symbol is defined in an unchanged file, we do not need to
740       // reapply any of its relocations.
741       if (gsym->source() == Symbol::FROM_OBJECT
742           && gsym->object()->is_incremental())
743         continue;
744
745       gold_debug(DEBUG_INCREMENTAL,
746                  "Applying incremental relocations for global symbol %s [%d]",
747                  gsym->name(), i);
748
749       // Follow the linked list of input symbol table entries for this symbol.
750       // We don't bother to figure out whether the symbol table entry belongs
751       // to a changed or unchanged file because it's easier just to apply all
752       // the relocations -- although we might scribble over an area that has
753       // been reallocated, we do this before copying any new data into the
754       // output file.
755       unsigned int offset = isymtab.get_list_head(i);
756       while (offset > 0)
757         {
758           Incremental_global_symbol_reader<big_endian> sym_info =
759               this->inputs_reader().global_symbol_reader_at_offset(offset);
760           unsigned int r_base = sym_info.reloc_offset();
761           unsigned int r_count = sym_info.reloc_count();
762
763           // Apply each relocation for this symbol table entry.
764           for (unsigned int j = 0; j < r_count;
765                ++j, r_base += incr_reloc_size)
766             {
767               unsigned int r_type = irelocs.get_r_type(r_base);
768               unsigned int r_shndx = irelocs.get_r_shndx(r_base);
769               Address r_offset = irelocs.get_r_offset(r_base);
770               Addend r_addend = irelocs.get_r_addend(r_base);
771               Output_section* os = this->output_section(r_shndx);
772               Address address = os->address();
773               off_t section_offset = os->offset();
774               size_t view_size = os->data_size();
775               unsigned char* const view = of->get_output_view(section_offset,
776                                                               view_size);
777
778               gold_debug(DEBUG_INCREMENTAL,
779                          "  %08lx: %s + %d: type %d addend %ld",
780                          (long)(section_offset + r_offset),
781                          os->name(),
782                          (int)r_offset,
783                          r_type,
784                          (long)r_addend);
785
786               target->apply_relocation(&relinfo, r_offset, r_type, r_addend,
787                                        gsym, view, address, view_size);
788
789               // FIXME: Do something more efficient if write_output_view
790               // ever becomes more than a no-op.
791               of->write_output_view(section_offset, view_size, view);
792             }
793           offset = sym_info.next_offset();
794         }
795     }
796 }
797
798 // Get a view of the main symbol table and the symbol string table.
799
800 template<int size, bool big_endian>
801 void
802 Sized_incremental_binary<size, big_endian>::get_symtab_view(
803     View* symtab_view,
804     unsigned int* nsyms,
805     elfcpp::Elf_strtab* strtab)
806 {
807   *symtab_view = this->view(this->main_symtab_loc_);
808   *nsyms = this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
809
810   View strtab_view(this->view(this->main_strtab_loc_));
811   *strtab = elfcpp::Elf_strtab(strtab_view.data(),
812                                this->main_strtab_loc_.data_size);
813 }
814
815 namespace
816 {
817
818 // Create a Sized_incremental_binary object of the specified size and
819 // endianness. Fails if the target architecture is not supported.
820
821 template<int size, bool big_endian>
822 Incremental_binary*
823 make_sized_incremental_binary(Output_file* file,
824                               const elfcpp::Ehdr<size, big_endian>& ehdr)
825 {
826   Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
827                                  ehdr.get_e_ident()[elfcpp::EI_OSABI],
828                                  ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
829   if (target == NULL)
830     {
831       explain_no_incremental(_("unsupported ELF machine number %d"),
832                ehdr.get_e_machine());
833       return NULL;
834     }
835
836   if (!parameters->target_valid())
837     set_parameters_target(target);
838   else if (target != &parameters->target())
839     gold_error(_("%s: incompatible target"), file->filename());
840
841   return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
842 }
843
844 }  // End of anonymous namespace.
845
846 // Create an Incremental_binary object for FILE.  Returns NULL is this is not
847 // possible, e.g. FILE is not an ELF file or has an unsupported target.  FILE
848 // should be opened.
849
850 Incremental_binary*
851 open_incremental_binary(Output_file* file)
852 {
853   off_t filesize = file->filesize();
854   int want = elfcpp::Elf_recognizer::max_header_size;
855   if (filesize < want)
856     want = filesize;
857
858   const unsigned char* p = file->get_input_view(0, want);
859   if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
860     {
861       explain_no_incremental(_("output is not an ELF file."));
862       return NULL;
863     }
864
865   int size = 0;
866   bool big_endian = false;
867   std::string error;
868   if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
869                                                &error))
870     {
871       explain_no_incremental(error.c_str());
872       return NULL;
873     }
874
875   Incremental_binary* result = NULL;
876   if (size == 32)
877     {
878       if (big_endian)
879         {
880 #ifdef HAVE_TARGET_32_BIG
881           result = make_sized_incremental_binary<32, true>(
882               file, elfcpp::Ehdr<32, true>(p));
883 #else
884           explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
885 #endif
886         }
887       else
888         {
889 #ifdef HAVE_TARGET_32_LITTLE
890           result = make_sized_incremental_binary<32, false>(
891               file, elfcpp::Ehdr<32, false>(p));
892 #else
893           explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
894 #endif
895         }
896     }
897   else if (size == 64)
898     {
899       if (big_endian)
900         {
901 #ifdef HAVE_TARGET_64_BIG
902           result = make_sized_incremental_binary<64, true>(
903               file, elfcpp::Ehdr<64, true>(p));
904 #else
905           explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
906 #endif
907         }
908       else
909         {
910 #ifdef HAVE_TARGET_64_LITTLE
911           result = make_sized_incremental_binary<64, false>(
912               file, elfcpp::Ehdr<64, false>(p));
913 #else
914           explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
915 #endif
916         }
917     }
918   else
919     gold_unreachable();
920
921   return result;
922 }
923
924 // Class Incremental_inputs.
925
926 // Add the command line to the string table, setting
927 // command_line_key_.  In incremental builds, the command line is
928 // stored in .gnu_incremental_inputs so that the next linker run can
929 // check if the command line options didn't change.
930
931 void
932 Incremental_inputs::report_command_line(int argc, const char* const* argv)
933 {
934   // Always store 'gold' as argv[0] to avoid a full relink if the user used a
935   // different path to the linker.
936   std::string args("gold");
937   // Copied from collect_argv in main.cc.
938   for (int i = 1; i < argc; ++i)
939     {
940       // Adding/removing these options should not result in a full relink.
941       if (strcmp(argv[i], "--incremental") == 0
942           || strcmp(argv[i], "--incremental-full") == 0
943           || strcmp(argv[i], "--incremental-update") == 0
944           || strcmp(argv[i], "--incremental-changed") == 0
945           || strcmp(argv[i], "--incremental-unchanged") == 0
946           || strcmp(argv[i], "--incremental-unknown") == 0
947           || strcmp(argv[i], "--incremental-startup-unchanged") == 0
948           || is_prefix_of("--incremental-base=", argv[i])
949           || is_prefix_of("--incremental-patch=", argv[i])
950           || is_prefix_of("--debug=", argv[i]))
951         continue;
952       if (strcmp(argv[i], "--incremental-base") == 0
953           || strcmp(argv[i], "--incremental-patch") == 0
954           || strcmp(argv[i], "--debug") == 0)
955         {
956           // When these options are used without the '=', skip the
957           // following parameter as well.
958           ++i;
959           continue;
960         }
961
962       args.append(" '");
963       // Now append argv[i], but with all single-quotes escaped
964       const char* argpos = argv[i];
965       while (1)
966         {
967           const int len = strcspn(argpos, "'");
968           args.append(argpos, len);
969           if (argpos[len] == '\0')
970             break;
971           args.append("'\"'\"'");
972           argpos += len + 1;
973         }
974       args.append("'");
975     }
976
977   this->command_line_ = args;
978   this->strtab_->add(this->command_line_.c_str(), false,
979                      &this->command_line_key_);
980 }
981
982 // Record the input archive file ARCHIVE.  This is called by the
983 // Add_archive_symbols task before determining which archive members
984 // to include.  We create the Incremental_archive_entry here and
985 // attach it to the Archive, but we do not add it to the list of
986 // input objects until report_archive_end is called.
987
988 void
989 Incremental_inputs::report_archive_begin(Library_base* arch,
990                                          unsigned int arg_serial,
991                                          Script_info* script_info)
992 {
993   Stringpool::Key filename_key;
994   Timespec mtime = arch->get_mtime();
995
996   // For a file loaded from a script, don't record its argument serial number.
997   if (script_info != NULL)
998     arg_serial = 0;
999
1000   this->strtab_->add(arch->filename().c_str(), false, &filename_key);
1001   Incremental_archive_entry* entry =
1002       new Incremental_archive_entry(filename_key, arg_serial, mtime);
1003   arch->set_incremental_info(entry);
1004
1005   if (script_info != NULL)
1006     {
1007       Incremental_script_entry* script_entry = script_info->incremental_info();
1008       gold_assert(script_entry != NULL);
1009       script_entry->add_object(entry);
1010     }
1011 }
1012
1013 // Visitor class for processing the unused global symbols in a library.
1014 // An instance of this class is passed to the library's
1015 // for_all_unused_symbols() iterator, which will call the visit()
1016 // function for each global symbol defined in each unused library
1017 // member.  We add those symbol names to the incremental info for the
1018 // library.
1019
1020 class Unused_symbol_visitor : public Library_base::Symbol_visitor_base
1021 {
1022  public:
1023   Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab)
1024     : entry_(entry), strtab_(strtab)
1025   { }
1026
1027   void
1028   visit(const char* sym)
1029   {
1030     Stringpool::Key symbol_key;
1031     this->strtab_->add(sym, true, &symbol_key);
1032     this->entry_->add_unused_global_symbol(symbol_key);
1033   }
1034
1035  private:
1036   Incremental_archive_entry* entry_;
1037   Stringpool* strtab_;
1038 };
1039
1040 // Finish recording the input archive file ARCHIVE.  This is called by the
1041 // Add_archive_symbols task after determining which archive members
1042 // to include.
1043
1044 void
1045 Incremental_inputs::report_archive_end(Library_base* arch)
1046 {
1047   Incremental_archive_entry* entry = arch->incremental_info();
1048
1049   gold_assert(entry != NULL);
1050   this->inputs_.push_back(entry);
1051
1052   // Collect unused global symbols.
1053   Unused_symbol_visitor v(entry, this->strtab_);
1054   arch->for_all_unused_symbols(&v);
1055 }
1056
1057 // Record the input object file OBJ.  If ARCH is not NULL, attach
1058 // the object file to the archive.  This is called by the
1059 // Add_symbols task after finding out the type of the file.
1060
1061 void
1062 Incremental_inputs::report_object(Object* obj, unsigned int arg_serial,
1063                                   Library_base* arch, Script_info* script_info)
1064 {
1065   Stringpool::Key filename_key;
1066   Timespec mtime = obj->get_mtime();
1067
1068   // For a file loaded from a script, don't record its argument serial number.
1069   if (script_info != NULL)
1070     arg_serial = 0;
1071
1072   this->strtab_->add(obj->name().c_str(), false, &filename_key);
1073
1074   Incremental_input_entry* input_entry;
1075
1076   this->current_object_ = obj;
1077
1078   if (!obj->is_dynamic())
1079     {
1080       this->current_object_entry_ =
1081           new Incremental_object_entry(filename_key, obj, arg_serial, mtime);
1082       input_entry = this->current_object_entry_;
1083       if (arch != NULL)
1084         {
1085           Incremental_archive_entry* arch_entry = arch->incremental_info();
1086           gold_assert(arch_entry != NULL);
1087           arch_entry->add_object(this->current_object_entry_);
1088         }
1089     }
1090   else
1091     {
1092       this->current_object_entry_ = NULL;
1093       Stringpool::Key soname_key;
1094       Dynobj* dynobj = obj->dynobj();
1095       gold_assert(dynobj != NULL);
1096       this->strtab_->add(dynobj->soname(), false, &soname_key);
1097       input_entry = new Incremental_dynobj_entry(filename_key, soname_key, obj,
1098                                                  arg_serial, mtime);
1099     }
1100
1101   if (obj->is_in_system_directory())
1102     input_entry->set_is_in_system_directory();
1103
1104   if (obj->as_needed())
1105     input_entry->set_as_needed();
1106
1107   this->inputs_.push_back(input_entry);
1108
1109   if (script_info != NULL)
1110     {
1111       Incremental_script_entry* script_entry = script_info->incremental_info();
1112       gold_assert(script_entry != NULL);
1113       script_entry->add_object(input_entry);
1114     }
1115 }
1116
1117 // Record an input section SHNDX from object file OBJ.
1118
1119 void
1120 Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
1121                                          const char* name, off_t sh_size)
1122 {
1123   Stringpool::Key key = 0;
1124
1125   if (name != NULL)
1126     this->strtab_->add(name, true, &key);
1127
1128   gold_assert(obj == this->current_object_);
1129   gold_assert(this->current_object_entry_ != NULL);
1130   this->current_object_entry_->add_input_section(shndx, key, sh_size);
1131 }
1132
1133 // Record a kept COMDAT group belonging to object file OBJ.
1134
1135 void
1136 Incremental_inputs::report_comdat_group(Object* obj, const char* name)
1137 {
1138   Stringpool::Key key = 0;
1139
1140   if (name != NULL)
1141     this->strtab_->add(name, true, &key);
1142   gold_assert(obj == this->current_object_);
1143   gold_assert(this->current_object_entry_ != NULL);
1144   this->current_object_entry_->add_comdat_group(key);
1145 }
1146
1147 // Record that the input argument INPUT is a script SCRIPT.  This is
1148 // called by read_script after parsing the script and reading the list
1149 // of inputs added by this script.
1150
1151 void
1152 Incremental_inputs::report_script(Script_info* script,
1153                                   unsigned int arg_serial,
1154                                   Timespec mtime)
1155 {
1156   Stringpool::Key filename_key;
1157
1158   this->strtab_->add(script->filename().c_str(), false, &filename_key);
1159   Incremental_script_entry* entry =
1160       new Incremental_script_entry(filename_key, arg_serial, script, mtime);
1161   this->inputs_.push_back(entry);
1162   script->set_incremental_info(entry);
1163 }
1164
1165 // Finalize the incremental link information.  Called from
1166 // Layout::finalize.
1167
1168 void
1169 Incremental_inputs::finalize()
1170 {
1171   // Finalize the string table.
1172   this->strtab_->set_string_offsets();
1173 }
1174
1175 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
1176
1177 void
1178 Incremental_inputs::create_data_sections(Symbol_table* symtab)
1179 {
1180   switch (parameters->size_and_endianness())
1181     {
1182 #ifdef HAVE_TARGET_32_LITTLE
1183     case Parameters::TARGET_32_LITTLE:
1184       this->inputs_section_ =
1185           new Output_section_incremental_inputs<32, false>(this, symtab);
1186       break;
1187 #endif
1188 #ifdef HAVE_TARGET_32_BIG
1189     case Parameters::TARGET_32_BIG:
1190       this->inputs_section_ =
1191           new Output_section_incremental_inputs<32, true>(this, symtab);
1192       break;
1193 #endif
1194 #ifdef HAVE_TARGET_64_LITTLE
1195     case Parameters::TARGET_64_LITTLE:
1196       this->inputs_section_ =
1197           new Output_section_incremental_inputs<64, false>(this, symtab);
1198       break;
1199 #endif
1200 #ifdef HAVE_TARGET_64_BIG
1201     case Parameters::TARGET_64_BIG:
1202       this->inputs_section_ =
1203           new Output_section_incremental_inputs<64, true>(this, symtab);
1204       break;
1205 #endif
1206     default:
1207       gold_unreachable();
1208     }
1209   this->symtab_section_ = new Output_data_space(4, "** incremental_symtab");
1210   this->relocs_section_ = new Output_data_space(4, "** incremental_relocs");
1211   this->got_plt_section_ = new Output_data_space(4, "** incremental_got_plt");
1212 }
1213
1214 // Return the sh_entsize value for the .gnu_incremental_relocs section.
1215 unsigned int
1216 Incremental_inputs::relocs_entsize() const
1217 {
1218   return 8 + 2 * parameters->target().get_size() / 8;
1219 }
1220
1221 // Class Output_section_incremental_inputs.
1222
1223 // Finalize the offsets for each input section and supplemental info block,
1224 // and set the final data size of the incremental output sections.
1225
1226 template<int size, bool big_endian>
1227 void
1228 Output_section_incremental_inputs<size, big_endian>::set_final_data_size()
1229 {
1230   const Incremental_inputs* inputs = this->inputs_;
1231   const unsigned int sizeof_addr = size / 8;
1232   const unsigned int rel_size = 8 + 2 * sizeof_addr;
1233
1234   // Offset of each input entry.
1235   unsigned int input_offset = this->header_size;
1236
1237   // Offset of each supplemental info block.
1238   unsigned int file_index = 0;
1239   unsigned int info_offset = this->header_size;
1240   info_offset += this->input_entry_size * inputs->input_file_count();
1241
1242   // Count each input file and its supplemental information block.
1243   for (Incremental_inputs::Input_list::const_iterator p =
1244            inputs->input_files().begin();
1245        p != inputs->input_files().end();
1246        ++p)
1247     {
1248       // Set the index and offset of the input file entry.
1249       (*p)->set_offset(file_index, input_offset);
1250       ++file_index;
1251       input_offset += this->input_entry_size;
1252
1253       // Set the offset of the supplemental info block.
1254       switch ((*p)->type())
1255         {
1256         case INCREMENTAL_INPUT_SCRIPT:
1257           {
1258             Incremental_script_entry *entry = (*p)->script_entry();
1259             gold_assert(entry != NULL);
1260             (*p)->set_info_offset(info_offset);
1261             // Object count.
1262             info_offset += 4;
1263             // Each member.
1264             info_offset += (entry->get_object_count() * 4);
1265           }
1266           break;
1267         case INCREMENTAL_INPUT_OBJECT:
1268         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
1269           {
1270             Incremental_object_entry* entry = (*p)->object_entry();
1271             gold_assert(entry != NULL);
1272             (*p)->set_info_offset(info_offset);
1273             // Input section count, global symbol count, local symbol offset,
1274             // local symbol count, first dynamic reloc, dynamic reloc count,
1275             // comdat group count.
1276             info_offset += 28;
1277             // Each input section.
1278             info_offset += (entry->get_input_section_count()
1279                             * (8 + 2 * sizeof_addr));
1280             // Each global symbol.
1281             const Object::Symbols* syms = entry->object()->get_global_symbols();
1282             info_offset += syms->size() * 20;
1283             // Each comdat group.
1284             info_offset += entry->get_comdat_group_count() * 4;
1285           }
1286           break;
1287         case INCREMENTAL_INPUT_SHARED_LIBRARY:
1288           {
1289             Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
1290             gold_assert(entry != NULL);
1291             (*p)->set_info_offset(info_offset);
1292             // Global symbol count, soname index.
1293             info_offset += 8;
1294             // Each global symbol.
1295             const Object::Symbols* syms = entry->object()->get_global_symbols();
1296             gold_assert(syms != NULL);
1297             unsigned int nsyms = syms->size();
1298             unsigned int nsyms_out = 0;
1299             for (unsigned int i = 0; i < nsyms; ++i)
1300               {
1301                 const Symbol* sym = (*syms)[i];
1302                 if (sym == NULL)
1303                   continue;
1304                 if (sym->is_forwarder())
1305                   sym = this->symtab_->resolve_forwards(sym);
1306                 if (sym->symtab_index() != -1U)
1307                   ++nsyms_out;
1308               }
1309             info_offset += nsyms_out * 4;
1310           }
1311           break;
1312         case INCREMENTAL_INPUT_ARCHIVE:
1313           {
1314             Incremental_archive_entry* entry = (*p)->archive_entry();
1315             gold_assert(entry != NULL);
1316             (*p)->set_info_offset(info_offset);
1317             // Member count + unused global symbol count.
1318             info_offset += 8;
1319             // Each member.
1320             info_offset += (entry->get_member_count() * 4);
1321             // Each global symbol.
1322             info_offset += (entry->get_unused_global_symbol_count() * 4);
1323           }
1324           break;
1325         default:
1326           gold_unreachable();
1327         }
1328     }
1329
1330   this->set_data_size(info_offset);
1331
1332   // Set the size of the .gnu_incremental_symtab section.
1333   inputs->symtab_section()->set_current_data_size(this->symtab_->output_count()
1334                                                   * sizeof(unsigned int));
1335
1336   // Set the size of the .gnu_incremental_relocs section.
1337   inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count()
1338                                                   * rel_size);
1339
1340   // Set the size of the .gnu_incremental_got_plt section.
1341   Sized_target<size, big_endian>* target =
1342     parameters->sized_target<size, big_endian>();
1343   unsigned int got_count = target->got_entry_count();
1344   unsigned int plt_count = target->plt_entry_count();
1345   unsigned int got_plt_size = 8;  // GOT entry count, PLT entry count.
1346   got_plt_size = (got_plt_size + got_count + 3) & ~3;  // GOT type array.
1347   got_plt_size += got_count * 8 + plt_count * 4;  // GOT array, PLT array.
1348   inputs->got_plt_section()->set_current_data_size(got_plt_size);
1349 }
1350
1351 // Write the contents of the .gnu_incremental_inputs and
1352 // .gnu_incremental_symtab sections.
1353
1354 template<int size, bool big_endian>
1355 void
1356 Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of)
1357 {
1358   const Incremental_inputs* inputs = this->inputs_;
1359   Stringpool* strtab = inputs->get_stringpool();
1360
1361   // Get a view into the .gnu_incremental_inputs section.
1362   const off_t off = this->offset();
1363   const off_t oview_size = this->data_size();
1364   unsigned char* const oview = of->get_output_view(off, oview_size);
1365   unsigned char* pov = oview;
1366
1367   // Get a view into the .gnu_incremental_symtab section.
1368   const off_t symtab_off = inputs->symtab_section()->offset();
1369   const off_t symtab_size = inputs->symtab_section()->data_size();
1370   unsigned char* const symtab_view = of->get_output_view(symtab_off,
1371                                                          symtab_size);
1372
1373   // Allocate an array of linked list heads for the .gnu_incremental_symtab
1374   // section.  Each element corresponds to a global symbol in the output
1375   // symbol table, and points to the head of the linked list that threads
1376   // through the object file input entries.  The value of each element
1377   // is the section-relative offset to a global symbol entry in a
1378   // supplemental information block.
1379   unsigned int global_sym_count = this->symtab_->output_count();
1380   unsigned int* global_syms = new unsigned int[global_sym_count];
1381   memset(global_syms, 0, global_sym_count * sizeof(unsigned int));
1382
1383   // Write the section header.
1384   Stringpool::Key command_line_key = inputs->command_line_key();
1385   pov = this->write_header(pov, inputs->input_file_count(),
1386                            strtab->get_offset_from_key(command_line_key));
1387
1388   // Write the list of input files.
1389   pov = this->write_input_files(oview, pov, strtab);
1390
1391   // Write the supplemental information blocks for each input file.
1392   pov = this->write_info_blocks(oview, pov, strtab, global_syms,
1393                                 global_sym_count);
1394
1395   gold_assert(pov - oview == oview_size);
1396
1397   // Write the .gnu_incremental_symtab section.
1398   gold_assert(global_sym_count * 4 == symtab_size);
1399   this->write_symtab(symtab_view, global_syms, global_sym_count);
1400
1401   delete[] global_syms;
1402
1403   // Write the .gnu_incremental_got_plt section.
1404   const off_t got_plt_off = inputs->got_plt_section()->offset();
1405   const off_t got_plt_size = inputs->got_plt_section()->data_size();
1406   unsigned char* const got_plt_view = of->get_output_view(got_plt_off,
1407                                                           got_plt_size);
1408   this->write_got_plt(got_plt_view, got_plt_size);
1409
1410   of->write_output_view(off, oview_size, oview);
1411   of->write_output_view(symtab_off, symtab_size, symtab_view);
1412   of->write_output_view(got_plt_off, got_plt_size, got_plt_view);
1413 }
1414
1415 // Write the section header: version, input file count, offset of command line
1416 // in the string table, and 4 bytes of padding.
1417
1418 template<int size, bool big_endian>
1419 unsigned char*
1420 Output_section_incremental_inputs<size, big_endian>::write_header(
1421     unsigned char* pov,
1422     unsigned int input_file_count,
1423     section_offset_type command_line_offset)
1424 {
1425   Swap32::writeval(pov, INCREMENTAL_LINK_VERSION);
1426   Swap32::writeval(pov + 4, input_file_count);
1427   Swap32::writeval(pov + 8, command_line_offset);
1428   Swap32::writeval(pov + 12, 0);
1429   return pov + this->header_size;
1430 }
1431
1432 // Write the input file entries.
1433
1434 template<int size, bool big_endian>
1435 unsigned char*
1436 Output_section_incremental_inputs<size, big_endian>::write_input_files(
1437     unsigned char* oview,
1438     unsigned char* pov,
1439     Stringpool* strtab)
1440 {
1441   const Incremental_inputs* inputs = this->inputs_;
1442
1443   for (Incremental_inputs::Input_list::const_iterator p =
1444            inputs->input_files().begin();
1445        p != inputs->input_files().end();
1446        ++p)
1447     {
1448       gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset());
1449       section_offset_type filename_offset =
1450           strtab->get_offset_from_key((*p)->get_filename_key());
1451       const Timespec& mtime = (*p)->get_mtime();
1452       unsigned int flags = (*p)->type();
1453       if ((*p)->is_in_system_directory())
1454         flags |= INCREMENTAL_INPUT_IN_SYSTEM_DIR;
1455       if ((*p)->as_needed())
1456         flags |= INCREMENTAL_INPUT_AS_NEEDED;
1457       Swap32::writeval(pov, filename_offset);
1458       Swap32::writeval(pov + 4, (*p)->get_info_offset());
1459       Swap64::writeval(pov + 8, mtime.seconds);
1460       Swap32::writeval(pov + 16, mtime.nanoseconds);
1461       Swap16::writeval(pov + 20, flags);
1462       Swap16::writeval(pov + 22, (*p)->arg_serial());
1463       pov += this->input_entry_size;
1464     }
1465   return pov;
1466 }
1467
1468 // Write the supplemental information blocks.
1469
1470 template<int size, bool big_endian>
1471 unsigned char*
1472 Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
1473     unsigned char* oview,
1474     unsigned char* pov,
1475     Stringpool* strtab,
1476     unsigned int* global_syms,
1477     unsigned int global_sym_count)
1478 {
1479   const Incremental_inputs* inputs = this->inputs_;
1480   unsigned int first_global_index = this->symtab_->first_global_index();
1481
1482   for (Incremental_inputs::Input_list::const_iterator p =
1483            inputs->input_files().begin();
1484        p != inputs->input_files().end();
1485        ++p)
1486     {
1487       switch ((*p)->type())
1488         {
1489         case INCREMENTAL_INPUT_SCRIPT:
1490           {
1491             gold_assert(static_cast<unsigned int>(pov - oview)
1492                         == (*p)->get_info_offset());
1493             Incremental_script_entry* entry = (*p)->script_entry();
1494             gold_assert(entry != NULL);
1495
1496             // Write the object count.
1497             unsigned int nobjects = entry->get_object_count();
1498             Swap32::writeval(pov, nobjects);
1499             pov += 4;
1500
1501             // For each object, write the offset to its input file entry.
1502             for (unsigned int i = 0; i < nobjects; ++i)
1503               {
1504                 Incremental_input_entry* obj = entry->get_object(i);
1505                 Swap32::writeval(pov, obj->get_offset());
1506                 pov += 4;
1507               }
1508           }
1509           break;
1510
1511         case INCREMENTAL_INPUT_OBJECT:
1512         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
1513           {
1514             gold_assert(static_cast<unsigned int>(pov - oview)
1515                         == (*p)->get_info_offset());
1516             Incremental_object_entry* entry = (*p)->object_entry();
1517             gold_assert(entry != NULL);
1518             const Object* obj = entry->object();
1519             const Relobj* relobj = static_cast<const Relobj*>(obj);
1520             const Object::Symbols* syms = obj->get_global_symbols();
1521             // Write the input section count and global symbol count.
1522             unsigned int nsections = entry->get_input_section_count();
1523             unsigned int nsyms = syms->size();
1524             off_t locals_offset = relobj->local_symbol_offset();
1525             unsigned int nlocals = relobj->output_local_symbol_count();
1526             unsigned int first_dynrel = relobj->first_dyn_reloc();
1527             unsigned int ndynrel = relobj->dyn_reloc_count();
1528             unsigned int ncomdat = entry->get_comdat_group_count();
1529             Swap32::writeval(pov, nsections);
1530             Swap32::writeval(pov + 4, nsyms);
1531             Swap32::writeval(pov + 8, static_cast<unsigned int>(locals_offset));
1532             Swap32::writeval(pov + 12, nlocals);
1533             Swap32::writeval(pov + 16, first_dynrel);
1534             Swap32::writeval(pov + 20, ndynrel);
1535             Swap32::writeval(pov + 24, ncomdat);
1536             pov += 28;
1537
1538             // Build a temporary array to map input section indexes
1539             // from the original object file index to the index in the
1540             // incremental info table.
1541             unsigned int* index_map = new unsigned int[obj->shnum()];
1542             memset(index_map, 0, obj->shnum() * sizeof(unsigned int));
1543
1544             // For each input section, write the name, output section index,
1545             // offset within output section, and input section size.
1546             for (unsigned int i = 0; i < nsections; i++)
1547               {
1548                 unsigned int shndx = entry->get_input_section_index(i);
1549                 index_map[shndx] = i + 1;
1550                 Stringpool::Key key = entry->get_input_section_name_key(i);
1551                 off_t name_offset = 0;
1552                 if (key != 0)
1553                   name_offset = strtab->get_offset_from_key(key);
1554                 int out_shndx = 0;
1555                 off_t out_offset = 0;
1556                 off_t sh_size = 0;
1557                 Output_section* os = obj->output_section(shndx);
1558                 if (os != NULL)
1559                   {
1560                     out_shndx = os->out_shndx();
1561                     out_offset = obj->output_section_offset(shndx);
1562                     sh_size = entry->get_input_section_size(i);
1563                   }
1564                 Swap32::writeval(pov, name_offset);
1565                 Swap32::writeval(pov + 4, out_shndx);
1566                 Swap::writeval(pov + 8, out_offset);
1567                 Swap::writeval(pov + 8 + sizeof_addr, sh_size);
1568                 pov += 8 + 2 * sizeof_addr;
1569               }
1570
1571             // For each global symbol, write its associated relocations,
1572             // add it to the linked list of globals, then write the
1573             // supplemental information:  global symbol table index,
1574             // input section index, linked list chain pointer, relocation
1575             // count, and offset to the relocations.
1576             for (unsigned int i = 0; i < nsyms; i++)
1577               {
1578                 const Symbol* sym = (*syms)[i];
1579                 if (sym->is_forwarder())
1580                   sym = this->symtab_->resolve_forwards(sym);
1581                 unsigned int shndx = 0;
1582                 if (sym->source() != Symbol::FROM_OBJECT)
1583                   {
1584                     // The symbol was defined by the linker (e.g., common).
1585                     // We mark these symbols with a special SHNDX of -1,
1586                     // but exclude linker-predefined symbols and symbols
1587                     // copied from shared objects.
1588                     if (!sym->is_predefined()
1589                         && !sym->is_copied_from_dynobj())
1590                       shndx = -1U;
1591                   }
1592                 else if (sym->object() == obj && sym->is_defined())
1593                   {
1594                     bool is_ordinary;
1595                     unsigned int orig_shndx = sym->shndx(&is_ordinary);
1596                     if (is_ordinary)
1597                       shndx = index_map[orig_shndx];
1598                     else
1599                       shndx = 1;
1600                   }
1601                 unsigned int symtab_index = sym->symtab_index();
1602                 unsigned int chain = 0;
1603                 unsigned int first_reloc = 0;
1604                 unsigned int nrelocs = obj->get_incremental_reloc_count(i);
1605                 if (nrelocs > 0)
1606                   {
1607                     gold_assert(symtab_index != -1U
1608                                 && (symtab_index - first_global_index
1609                                     < global_sym_count));
1610                     first_reloc = obj->get_incremental_reloc_base(i);
1611                     chain = global_syms[symtab_index - first_global_index];
1612                     global_syms[symtab_index - first_global_index] =
1613                         pov - oview;
1614                   }
1615                 Swap32::writeval(pov, symtab_index);
1616                 Swap32::writeval(pov + 4, shndx);
1617                 Swap32::writeval(pov + 8, chain);
1618                 Swap32::writeval(pov + 12, nrelocs);
1619                 Swap32::writeval(pov + 16, first_reloc * 3 * sizeof_addr);
1620                 pov += 20;
1621               }
1622
1623             // For each kept COMDAT group, write the group signature.
1624             for (unsigned int i = 0; i < ncomdat; i++)
1625               {
1626                 Stringpool::Key key = entry->get_comdat_signature_key(i);
1627                 off_t name_offset = 0;
1628                 if (key != 0)
1629                   name_offset = strtab->get_offset_from_key(key);
1630                 Swap32::writeval(pov, name_offset);
1631                 pov += 4;
1632               }
1633
1634             delete[] index_map;
1635           }
1636           break;
1637
1638         case INCREMENTAL_INPUT_SHARED_LIBRARY:
1639           {
1640             gold_assert(static_cast<unsigned int>(pov - oview)
1641                         == (*p)->get_info_offset());
1642             Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
1643             gold_assert(entry != NULL);
1644             Object* obj = entry->object();
1645             Dynobj* dynobj = obj->dynobj();
1646             gold_assert(dynobj != NULL);
1647             const Object::Symbols* syms = obj->get_global_symbols();
1648
1649             // Write the soname string table index.
1650             section_offset_type soname_offset =
1651                 strtab->get_offset_from_key(entry->get_soname_key());
1652             Swap32::writeval(pov, soname_offset);
1653             pov += 4;
1654
1655             // Skip the global symbol count for now.
1656             unsigned char* orig_pov = pov;
1657             pov += 4;
1658
1659             // For each global symbol, write the global symbol table index.
1660             unsigned int nsyms = syms->size();
1661             unsigned int nsyms_out = 0;
1662             for (unsigned int i = 0; i < nsyms; i++)
1663               {
1664                 const Symbol* sym = (*syms)[i];
1665                 if (sym == NULL)
1666                   continue;
1667                 if (sym->is_forwarder())
1668                   sym = this->symtab_->resolve_forwards(sym);
1669                 if (sym->symtab_index() == -1U)
1670                   continue;
1671                 unsigned int flags = 0;
1672                 // If the symbol has hidden or internal visibility, we
1673                 // mark it as defined in the shared object so we don't
1674                 // try to resolve it during an incremental update.
1675                 if (sym->visibility() == elfcpp::STV_HIDDEN
1676                     || sym->visibility() == elfcpp::STV_INTERNAL)
1677                   flags = INCREMENTAL_SHLIB_SYM_DEF;
1678                 else if (sym->source() == Symbol::FROM_OBJECT
1679                          && sym->object() == obj
1680                          && sym->is_defined())
1681                   flags = INCREMENTAL_SHLIB_SYM_DEF;
1682                 else if (sym->is_copied_from_dynobj()
1683                          && this->symtab_->get_copy_source(sym) == dynobj)
1684                   flags = INCREMENTAL_SHLIB_SYM_COPY;
1685                 flags <<= INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
1686                 Swap32::writeval(pov, sym->symtab_index() | flags);
1687                 pov += 4;
1688                 ++nsyms_out;
1689               }
1690
1691             // Now write the global symbol count.
1692             Swap32::writeval(orig_pov, nsyms_out);
1693           }
1694           break;
1695
1696         case INCREMENTAL_INPUT_ARCHIVE:
1697           {
1698             gold_assert(static_cast<unsigned int>(pov - oview)
1699                         == (*p)->get_info_offset());
1700             Incremental_archive_entry* entry = (*p)->archive_entry();
1701             gold_assert(entry != NULL);
1702
1703             // Write the member count and unused global symbol count.
1704             unsigned int nmembers = entry->get_member_count();
1705             unsigned int nsyms = entry->get_unused_global_symbol_count();
1706             Swap32::writeval(pov, nmembers);
1707             Swap32::writeval(pov + 4, nsyms);
1708             pov += 8;
1709
1710             // For each member, write the offset to its input file entry.
1711             for (unsigned int i = 0; i < nmembers; ++i)
1712               {
1713                 Incremental_object_entry* member = entry->get_member(i);
1714                 Swap32::writeval(pov, member->get_offset());
1715                 pov += 4;
1716               }
1717
1718             // For each global symbol, write the name offset.
1719             for (unsigned int i = 0; i < nsyms; ++i)
1720               {
1721                 Stringpool::Key key = entry->get_unused_global_symbol(i);
1722                 Swap32::writeval(pov, strtab->get_offset_from_key(key));
1723                 pov += 4;
1724               }
1725           }
1726           break;
1727
1728         default:
1729           gold_unreachable();
1730         }
1731     }
1732   return pov;
1733 }
1734
1735 // Write the contents of the .gnu_incremental_symtab section.
1736
1737 template<int size, bool big_endian>
1738 void
1739 Output_section_incremental_inputs<size, big_endian>::write_symtab(
1740     unsigned char* pov,
1741     unsigned int* global_syms,
1742     unsigned int global_sym_count)
1743 {
1744   for (unsigned int i = 0; i < global_sym_count; ++i)
1745     {
1746       Swap32::writeval(pov, global_syms[i]);
1747       pov += 4;
1748     }
1749 }
1750
1751 // This struct holds the view information needed to write the
1752 // .gnu_incremental_got_plt section.
1753
1754 struct Got_plt_view_info
1755 {
1756   // Start of the GOT type array in the output view.
1757   unsigned char* got_type_p;
1758   // Start of the GOT descriptor array in the output view.
1759   unsigned char* got_desc_p;
1760   // Start of the PLT descriptor array in the output view.
1761   unsigned char* plt_desc_p;
1762   // Number of GOT entries.
1763   unsigned int got_count;
1764   // Number of PLT entries.
1765   unsigned int plt_count;
1766   // Offset of the first non-reserved PLT entry (this is a target-dependent value).
1767   unsigned int first_plt_entry_offset;
1768   // Size of a PLT entry (this is a target-dependent value).
1769   unsigned int plt_entry_size;
1770   // Symbol index to write in the GOT descriptor array.  For global symbols,
1771   // this is the global symbol table index; for local symbols, it is the
1772   // local symbol table index.
1773   unsigned int sym_index;
1774   // Input file index to write in the GOT descriptor array.  For global
1775   // symbols, this is 0; for local symbols, it is the index of the input
1776   // file entry in the .gnu_incremental_inputs section.
1777   unsigned int input_index;
1778 };
1779
1780 // Functor class for processing a GOT offset list for local symbols.
1781 // Writes the GOT type and symbol index into the GOT type and descriptor
1782 // arrays in the output section.
1783
1784 template<int size, bool big_endian>
1785 class Local_got_offset_visitor : public Got_offset_list::Visitor
1786 {
1787  public:
1788   Local_got_offset_visitor(struct Got_plt_view_info& info)
1789     : info_(info)
1790   { }
1791
1792   void
1793   visit(unsigned int got_type, unsigned int got_offset)
1794   {
1795     unsigned int got_index = got_offset / this->got_entry_size_;
1796     gold_assert(got_index < this->info_.got_count);
1797     // We can only handle GOT entry types in the range 0..0x7e
1798     // because we use a byte array to store them, and we use the
1799     // high bit to flag a local symbol.
1800     gold_assert(got_type < 0x7f);
1801     this->info_.got_type_p[got_index] = got_type | 0x80;
1802     unsigned char* pov = this->info_.got_desc_p + got_index * 8;
1803     elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
1804     elfcpp::Swap<32, big_endian>::writeval(pov + 4, this->info_.input_index);
1805   }
1806
1807  private:
1808   static const unsigned int got_entry_size_ = size / 8;
1809   struct Got_plt_view_info& info_;
1810 };
1811
1812 // Functor class for processing a GOT offset list.  Writes the GOT type
1813 // and symbol index into the GOT type and descriptor arrays in the output
1814 // section.
1815
1816 template<int size, bool big_endian>
1817 class Global_got_offset_visitor : public Got_offset_list::Visitor
1818 {
1819  public:
1820   Global_got_offset_visitor(struct Got_plt_view_info& info)
1821     : info_(info)
1822   { }
1823
1824   void
1825   visit(unsigned int got_type, unsigned int got_offset)
1826   {
1827     unsigned int got_index = got_offset / this->got_entry_size_;
1828     gold_assert(got_index < this->info_.got_count);
1829     // We can only handle GOT entry types in the range 0..0x7e
1830     // because we use a byte array to store them, and we use the
1831     // high bit to flag a local symbol.
1832     gold_assert(got_type < 0x7f);
1833     this->info_.got_type_p[got_index] = got_type;
1834     unsigned char* pov = this->info_.got_desc_p + got_index * 8;
1835     elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
1836     elfcpp::Swap<32, big_endian>::writeval(pov + 4, 0);
1837   }
1838
1839  private:
1840   static const unsigned int got_entry_size_ = size / 8;
1841   struct Got_plt_view_info& info_;
1842 };
1843
1844 // Functor class for processing the global symbol table.  Processes the
1845 // GOT offset list for the symbol, and writes the symbol table index
1846 // into the PLT descriptor array in the output section.
1847
1848 template<int size, bool big_endian>
1849 class Global_symbol_visitor_got_plt
1850 {
1851  public:
1852   Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
1853     : info_(info)
1854   { }
1855
1856   void
1857   operator()(const Sized_symbol<size>* sym)
1858   {
1859     typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
1860     const Got_offset_list* got_offsets = sym->got_offset_list();
1861     if (got_offsets != NULL)
1862       {
1863         this->info_.sym_index = sym->symtab_index();
1864         this->info_.input_index = 0;
1865         Got_visitor v(this->info_);
1866         got_offsets->for_all_got_offsets(&v);
1867       }
1868     if (sym->has_plt_offset())
1869       {
1870         unsigned int plt_index =
1871             ((sym->plt_offset() - this->info_.first_plt_entry_offset)
1872              / this->info_.plt_entry_size);
1873         gold_assert(plt_index < this->info_.plt_count);
1874         unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
1875         elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
1876       }
1877   }
1878
1879  private:
1880   struct Got_plt_view_info& info_;
1881 };
1882
1883 // Write the contents of the .gnu_incremental_got_plt section.
1884
1885 template<int size, bool big_endian>
1886 void
1887 Output_section_incremental_inputs<size, big_endian>::write_got_plt(
1888     unsigned char* pov,
1889     off_t view_size)
1890 {
1891   Sized_target<size, big_endian>* target =
1892     parameters->sized_target<size, big_endian>();
1893
1894   // Set up the view information for the functors.
1895   struct Got_plt_view_info view_info;
1896   view_info.got_count = target->got_entry_count();
1897   view_info.plt_count = target->plt_entry_count();
1898   view_info.first_plt_entry_offset = target->first_plt_entry_offset();
1899   view_info.plt_entry_size = target->plt_entry_size();
1900   view_info.got_type_p = pov + 8;
1901   view_info.got_desc_p = (view_info.got_type_p
1902                           + ((view_info.got_count + 3) & ~3));
1903   view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 8;
1904
1905   gold_assert(pov + view_size ==
1906               view_info.plt_desc_p + view_info.plt_count * 4);
1907
1908   // Write the section header.
1909   Swap32::writeval(pov, view_info.got_count);
1910   Swap32::writeval(pov + 4, view_info.plt_count);
1911
1912   // Initialize the GOT type array to 0xff (reserved).
1913   memset(view_info.got_type_p, 0xff, view_info.got_count);
1914
1915   // Write the incremental GOT descriptors for local symbols.
1916   typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
1917   for (Incremental_inputs::Input_list::const_iterator p =
1918            this->inputs_->input_files().begin();
1919        p != this->inputs_->input_files().end();
1920        ++p)
1921     {
1922       if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
1923           && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
1924         continue;
1925       Incremental_object_entry* entry = (*p)->object_entry();
1926       gold_assert(entry != NULL);
1927       const Object* obj = entry->object();
1928       gold_assert(obj != NULL);
1929       view_info.input_index = (*p)->get_file_index();
1930       Got_visitor v(view_info);
1931       obj->for_all_local_got_entries(&v);
1932     }
1933
1934   // Write the incremental GOT and PLT descriptors for global symbols.
1935   typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
1936   symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
1937 }
1938
1939 // Class Sized_relobj_incr.  Most of these methods are not used for
1940 // Incremental objects, but are required to be implemented by the
1941 // base class Object.
1942
1943 template<int size, bool big_endian>
1944 Sized_relobj_incr<size, big_endian>::Sized_relobj_incr(
1945     const std::string& name,
1946     Sized_incremental_binary<size, big_endian>* ibase,
1947     unsigned int input_file_index)
1948   : Sized_relobj<size, big_endian>(name, NULL), ibase_(ibase),
1949     input_file_index_(input_file_index),
1950     input_reader_(ibase->inputs_reader().input_file(input_file_index)),
1951     local_symbol_count_(0), output_local_dynsym_count_(0),
1952     local_symbol_index_(0), local_symbol_offset_(0), local_dynsym_offset_(0),
1953     symbols_(), incr_reloc_offset_(-1U), incr_reloc_count_(0),
1954     incr_reloc_output_index_(0), incr_relocs_(NULL), local_symbols_()
1955 {
1956   if (this->input_reader_.is_in_system_directory())
1957     this->set_is_in_system_directory();
1958   const unsigned int shnum = this->input_reader_.get_input_section_count() + 1;
1959   this->set_shnum(shnum);
1960   ibase->set_input_object(input_file_index, this);
1961 }
1962
1963 // Read the symbols.
1964
1965 template<int size, bool big_endian>
1966 void
1967 Sized_relobj_incr<size, big_endian>::do_read_symbols(Read_symbols_data*)
1968 {
1969   gold_unreachable();
1970 }
1971
1972 // Lay out the input sections.
1973
1974 template<int size, bool big_endian>
1975 void
1976 Sized_relobj_incr<size, big_endian>::do_layout(
1977     Symbol_table*,
1978     Layout* layout,
1979     Read_symbols_data*)
1980 {
1981   const unsigned int shnum = this->shnum();
1982   Incremental_inputs* incremental_inputs = layout->incremental_inputs();
1983   gold_assert(incremental_inputs != NULL);
1984   Output_sections& out_sections(this->output_sections());
1985   out_sections.resize(shnum);
1986   this->section_offsets().resize(shnum);
1987   for (unsigned int i = 1; i < shnum; i++)
1988     {
1989       typename Input_entry_reader::Input_section_info sect =
1990           this->input_reader_.get_input_section(i - 1);
1991       // Add the section to the incremental inputs layout.
1992       incremental_inputs->report_input_section(this, i, sect.name,
1993                                                sect.sh_size);
1994       if (sect.output_shndx == 0 || sect.sh_offset == -1)
1995         continue;
1996       Output_section* os = this->ibase_->output_section(sect.output_shndx);
1997       gold_assert(os != NULL);
1998       out_sections[i] = os;
1999       this->section_offsets()[i] = static_cast<Address>(sect.sh_offset);
2000     }
2001
2002   // Process the COMDAT groups.
2003   unsigned int ncomdat = this->input_reader_.get_comdat_group_count();
2004   for (unsigned int i = 0; i < ncomdat; i++)
2005     {
2006       const char* signature = this->input_reader_.get_comdat_group_signature(i);
2007       if (signature == NULL || signature[0] == '\0')
2008         this->error(_("COMDAT group has no signature"));
2009       bool keep = layout->find_or_add_kept_section(signature, this, i, true,
2010                                                    true, NULL);
2011       if (!keep)
2012         this->error(_("COMDAT group %s included twice in incremental link"),
2013                     signature);
2014     }
2015 }
2016
2017 // Layout sections whose layout was deferred while waiting for
2018 // input files from a plugin.
2019 template<int size, bool big_endian>
2020 void
2021 Sized_relobj_incr<size, big_endian>::do_layout_deferred_sections(Layout*)
2022 {
2023 }
2024
2025 // Add the symbols to the symbol table.
2026
2027 template<int size, bool big_endian>
2028 void
2029 Sized_relobj_incr<size, big_endian>::do_add_symbols(
2030     Symbol_table* symtab,
2031     Read_symbols_data*,
2032     Layout*)
2033 {
2034   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2035   unsigned char symbuf[sym_size];
2036   elfcpp::Sym<size, big_endian> sym(symbuf);
2037   elfcpp::Sym_write<size, big_endian> osym(symbuf);
2038
2039   typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2040
2041   unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2042   this->symbols_.resize(nsyms);
2043
2044   Incremental_binary::View symtab_view(NULL);
2045   unsigned int symtab_count;
2046   elfcpp::Elf_strtab strtab(NULL, 0);
2047   this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2048
2049   Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2050   unsigned int isym_count = isymtab.symbol_count();
2051   unsigned int first_global = symtab_count - isym_count;
2052
2053   const unsigned char* sym_p;
2054   for (unsigned int i = 0; i < nsyms; ++i)
2055     {
2056       Incremental_global_symbol_reader<big_endian> info =
2057           this->input_reader_.get_global_symbol_reader(i);
2058       unsigned int output_symndx = info.output_symndx();
2059       sym_p = symtab_view.data() + output_symndx * sym_size;
2060       elfcpp::Sym<size, big_endian> gsym(sym_p);
2061       const char* name;
2062       if (!strtab.get_c_string(gsym.get_st_name(), &name))
2063         name = "";
2064
2065       typename elfcpp::Elf_types<size>::Elf_Addr v = gsym.get_st_value();
2066       unsigned int shndx = gsym.get_st_shndx();
2067       elfcpp::STB st_bind = gsym.get_st_bind();
2068       elfcpp::STT st_type = gsym.get_st_type();
2069
2070       // Local hidden symbols start out as globals, but get converted to
2071       // to local during output.
2072       if (st_bind == elfcpp::STB_LOCAL)
2073         st_bind = elfcpp::STB_GLOBAL;
2074
2075       unsigned int input_shndx = info.shndx();
2076       if (input_shndx == 0 || input_shndx == -1U)
2077         {
2078           shndx = elfcpp::SHN_UNDEF;
2079           v = 0;
2080         }
2081       else if (shndx != elfcpp::SHN_ABS)
2082         {
2083           // Find the input section and calculate the section-relative value.
2084           gold_assert(shndx != elfcpp::SHN_UNDEF);
2085           Output_section* os = this->ibase_->output_section(shndx);
2086           gold_assert(os != NULL && os->has_fixed_layout());
2087           typename Input_entry_reader::Input_section_info sect =
2088               this->input_reader_.get_input_section(input_shndx - 1);
2089           gold_assert(sect.output_shndx == shndx);
2090           if (st_type != elfcpp::STT_TLS)
2091             v -= os->address();
2092           v -= sect.sh_offset;
2093           shndx = input_shndx;
2094         }
2095
2096       osym.put_st_name(0);
2097       osym.put_st_value(v);
2098       osym.put_st_size(gsym.get_st_size());
2099       osym.put_st_info(st_bind, st_type);
2100       osym.put_st_other(gsym.get_st_other());
2101       osym.put_st_shndx(shndx);
2102
2103       Symbol* res = symtab->add_from_incrobj(this, name, NULL, &sym);
2104
2105       // If this is a linker-defined symbol that hasn't yet been defined,
2106       // define it now.
2107       if (input_shndx == -1U && !res->is_defined())
2108         {
2109           shndx = gsym.get_st_shndx();
2110           v = gsym.get_st_value();
2111           Elf_size_type symsize = gsym.get_st_size();
2112           if (shndx == elfcpp::SHN_ABS)
2113             {
2114               symtab->define_as_constant(name, NULL,
2115                                          Symbol_table::INCREMENTAL_BASE,
2116                                          v, symsize, st_type, st_bind,
2117                                          gsym.get_st_visibility(), 0,
2118                                          false, false);
2119             }
2120           else
2121             {
2122               Output_section* os = this->ibase_->output_section(shndx);
2123               gold_assert(os != NULL && os->has_fixed_layout());
2124               v -= os->address();
2125               if (symsize > 0)
2126                 os->reserve(v, symsize);
2127               symtab->define_in_output_data(name, NULL,
2128                                             Symbol_table::INCREMENTAL_BASE,
2129                                             os, v, symsize, st_type, st_bind,
2130                                             gsym.get_st_visibility(), 0,
2131                                             false, false);
2132             }
2133         }
2134
2135       this->symbols_[i] = res;
2136       this->ibase_->add_global_symbol(output_symndx - first_global, res);
2137     }
2138 }
2139
2140 // Return TRUE if we should include this object from an archive library.
2141
2142 template<int size, bool big_endian>
2143 Archive::Should_include
2144 Sized_relobj_incr<size, big_endian>::do_should_include_member(
2145     Symbol_table*,
2146     Layout*,
2147     Read_symbols_data*,
2148     std::string*)
2149 {
2150   gold_unreachable();
2151 }
2152
2153 // Iterate over global symbols, calling a visitor class V for each.
2154
2155 template<int size, bool big_endian>
2156 void
2157 Sized_relobj_incr<size, big_endian>::do_for_all_global_symbols(
2158     Read_symbols_data*,
2159     Library_base::Symbol_visitor_base*)
2160 {
2161   // This routine is not used for incremental objects.
2162 }
2163
2164 // Get the size of a section.
2165
2166 template<int size, bool big_endian>
2167 uint64_t
2168 Sized_relobj_incr<size, big_endian>::do_section_size(unsigned int)
2169 {
2170   gold_unreachable();
2171 }
2172
2173 // Get the name of a section.
2174
2175 template<int size, bool big_endian>
2176 std::string
2177 Sized_relobj_incr<size, big_endian>::do_section_name(unsigned int)
2178 {
2179   gold_unreachable();
2180 }
2181
2182 // Return a view of the contents of a section.
2183
2184 template<int size, bool big_endian>
2185 Object::Location
2186 Sized_relobj_incr<size, big_endian>::do_section_contents(unsigned int)
2187 {
2188   gold_unreachable();
2189 }
2190
2191 // Return section flags.
2192
2193 template<int size, bool big_endian>
2194 uint64_t
2195 Sized_relobj_incr<size, big_endian>::do_section_flags(unsigned int)
2196 {
2197   gold_unreachable();
2198 }
2199
2200 // Return section entsize.
2201
2202 template<int size, bool big_endian>
2203 uint64_t
2204 Sized_relobj_incr<size, big_endian>::do_section_entsize(unsigned int)
2205 {
2206   gold_unreachable();
2207 }
2208
2209 // Return section address.
2210
2211 template<int size, bool big_endian>
2212 uint64_t
2213 Sized_relobj_incr<size, big_endian>::do_section_address(unsigned int)
2214 {
2215   gold_unreachable();
2216 }
2217
2218 // Return section type.
2219
2220 template<int size, bool big_endian>
2221 unsigned int
2222 Sized_relobj_incr<size, big_endian>::do_section_type(unsigned int)
2223 {
2224   gold_unreachable();
2225 }
2226
2227 // Return the section link field.
2228
2229 template<int size, bool big_endian>
2230 unsigned int
2231 Sized_relobj_incr<size, big_endian>::do_section_link(unsigned int)
2232 {
2233   gold_unreachable();
2234 }
2235
2236 // Return the section link field.
2237
2238 template<int size, bool big_endian>
2239 unsigned int
2240 Sized_relobj_incr<size, big_endian>::do_section_info(unsigned int)
2241 {
2242   gold_unreachable();
2243 }
2244
2245 // Return the section alignment.
2246
2247 template<int size, bool big_endian>
2248 uint64_t
2249 Sized_relobj_incr<size, big_endian>::do_section_addralign(unsigned int)
2250 {
2251   gold_unreachable();
2252 }
2253
2254 // Return the Xindex structure to use.
2255
2256 template<int size, bool big_endian>
2257 Xindex*
2258 Sized_relobj_incr<size, big_endian>::do_initialize_xindex()
2259 {
2260   gold_unreachable();
2261 }
2262
2263 // Get symbol counts.
2264
2265 template<int size, bool big_endian>
2266 void
2267 Sized_relobj_incr<size, big_endian>::do_get_global_symbol_counts(
2268     const Symbol_table*, size_t*, size_t*) const
2269 {
2270   gold_unreachable();
2271 }
2272
2273 // Read the relocs.
2274
2275 template<int size, bool big_endian>
2276 void
2277 Sized_relobj_incr<size, big_endian>::do_read_relocs(Read_relocs_data*)
2278 {
2279 }
2280
2281 // Process the relocs to find list of referenced sections. Used only
2282 // during garbage collection.
2283
2284 template<int size, bool big_endian>
2285 void
2286 Sized_relobj_incr<size, big_endian>::do_gc_process_relocs(Symbol_table*,
2287                                                           Layout*,
2288                                                           Read_relocs_data*)
2289 {
2290   gold_unreachable();
2291 }
2292
2293 // Scan the relocs and adjust the symbol table.
2294
2295 template<int size, bool big_endian>
2296 void
2297 Sized_relobj_incr<size, big_endian>::do_scan_relocs(Symbol_table*,
2298                                                     Layout* layout,
2299                                                     Read_relocs_data*)
2300 {
2301   // Count the incremental relocations for this object.
2302   unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2303   this->allocate_incremental_reloc_counts();
2304   for (unsigned int i = 0; i < nsyms; i++)
2305     {
2306       Incremental_global_symbol_reader<big_endian> sym =
2307           this->input_reader_.get_global_symbol_reader(i);
2308       unsigned int reloc_count = sym.reloc_count();
2309       if (reloc_count > 0 && this->incr_reloc_offset_ == -1U)
2310         this->incr_reloc_offset_ = sym.reloc_offset();
2311       this->incr_reloc_count_ += reloc_count;
2312       for (unsigned int j = 0; j < reloc_count; j++)
2313         this->count_incremental_reloc(i);
2314     }
2315   this->incr_reloc_output_index_ =
2316       layout->incremental_inputs()->get_reloc_count();
2317   this->finalize_incremental_relocs(layout, false);
2318
2319   // The incoming incremental relocations may not end up in the same
2320   // location after the incremental update, because the incremental info
2321   // is regenerated in each link.  Because the new location may overlap
2322   // with other data in the updated output file, we need to copy the
2323   // relocations into a buffer so that we can still read them safely
2324   // after we start writing updates to the output file.
2325   if (this->incr_reloc_count_ > 0)
2326     {
2327       const Incremental_relocs_reader<size, big_endian>& relocs_reader =
2328           this->ibase_->relocs_reader();
2329       const unsigned int incr_reloc_size = relocs_reader.reloc_size;
2330       unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2331       this->incr_relocs_ = new unsigned char[len];
2332       memcpy(this->incr_relocs_,
2333              relocs_reader.data(this->incr_reloc_offset_),
2334              len);
2335     }
2336 }
2337
2338 // Count the local symbols.
2339
2340 template<int size, bool big_endian>
2341 void
2342 Sized_relobj_incr<size, big_endian>::do_count_local_symbols(
2343     Stringpool_template<char>* pool,
2344     Stringpool_template<char>*)
2345 {
2346   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2347
2348   // Set the count of local symbols based on the incremental info.
2349   unsigned int nlocals = this->input_reader_.get_local_symbol_count();
2350   this->local_symbol_count_ = nlocals;
2351   this->local_symbols_.reserve(nlocals);
2352
2353   // Get views of the base file's symbol table and string table.
2354   Incremental_binary::View symtab_view(NULL);
2355   unsigned int symtab_count;
2356   elfcpp::Elf_strtab strtab(NULL, 0);
2357   this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2358
2359   // Read the local symbols from the base file's symbol table.
2360   off_t off = this->input_reader_.get_local_symbol_offset();
2361   const unsigned char* symp = symtab_view.data() + off;
2362   for (unsigned int i = 0; i < nlocals; ++i, symp += sym_size)
2363     {
2364       elfcpp::Sym<size, big_endian> sym(symp);
2365       const char* name;
2366       if (!strtab.get_c_string(sym.get_st_name(), &name))
2367         name = "";
2368       gold_debug(DEBUG_INCREMENTAL, "Local symbol %d: %s", i, name);
2369       name = pool->add(name, true, NULL);
2370       this->local_symbols_.push_back(Local_symbol(name,
2371                                                   sym.get_st_value(),
2372                                                   sym.get_st_size(),
2373                                                   sym.get_st_shndx(),
2374                                                   sym.get_st_type(),
2375                                                   false));
2376     }
2377 }
2378
2379 // Finalize the local symbols.
2380
2381 template<int size, bool big_endian>
2382 unsigned int
2383 Sized_relobj_incr<size, big_endian>::do_finalize_local_symbols(
2384     unsigned int index,
2385     off_t off,
2386     Symbol_table*)
2387 {
2388   this->local_symbol_index_ = index;
2389   this->local_symbol_offset_ = off;
2390   return index + this->local_symbol_count_;
2391 }
2392
2393 // Set the offset where local dynamic symbol information will be stored.
2394
2395 template<int size, bool big_endian>
2396 unsigned int
2397 Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_indexes(
2398     unsigned int index)
2399 {
2400   // FIXME: set local dynsym indexes.
2401   return index;
2402 }
2403
2404 // Set the offset where local dynamic symbol information will be stored.
2405
2406 template<int size, bool big_endian>
2407 unsigned int
2408 Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_offset(off_t)
2409 {
2410   return 0;
2411 }
2412
2413 // Relocate the input sections and write out the local symbols.
2414 // We don't actually do any relocation here.  For unchanged input files,
2415 // we reapply relocations only for symbols that have changed; that happens
2416 // in queue_final_tasks.  We do need to rewrite the incremental relocations
2417 // for this object.
2418
2419 template<int size, bool big_endian>
2420 void
2421 Sized_relobj_incr<size, big_endian>::do_relocate(const Symbol_table*,
2422                                                  const Layout* layout,
2423                                                  Output_file* of)
2424 {
2425   if (this->incr_reloc_count_ == 0)
2426     return;
2427
2428   const unsigned int incr_reloc_size =
2429       Incremental_relocs_reader<size, big_endian>::reloc_size;
2430
2431   // Get a view for the .gnu_incremental_relocs section.
2432   Incremental_inputs* inputs = layout->incremental_inputs();
2433   gold_assert(inputs != NULL);
2434   const off_t relocs_off = inputs->relocs_section()->offset();
2435   const off_t relocs_size = inputs->relocs_section()->data_size();
2436   unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
2437
2438   // Copy the relocations from the buffer.
2439   off_t off = this->incr_reloc_output_index_ * incr_reloc_size;
2440   unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2441   memcpy(view + off, this->incr_relocs_, len);
2442
2443   // The output section table may have changed, so we need to map
2444   // the old section index to the new section index for each relocation.
2445   for (unsigned int i = 0; i < this->incr_reloc_count_; ++i)
2446     {
2447       unsigned char* pov = view + off + i * incr_reloc_size;
2448       unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(pov + 4);
2449       Output_section* os = this->ibase_->output_section(shndx);
2450       gold_assert(os != NULL);
2451       shndx = os->out_shndx();
2452       elfcpp::Swap<32, big_endian>::writeval(pov + 4, shndx);
2453     }
2454
2455   of->write_output_view(off, len, view);
2456
2457   // Get views into the output file for the portions of the symbol table
2458   // and the dynamic symbol table that we will be writing.
2459   off_t symtab_off = layout->symtab_section()->offset();
2460   off_t output_size = this->local_symbol_count_ * This::sym_size;
2461   unsigned char* oview = NULL;
2462   if (output_size > 0)
2463     oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
2464                                 output_size);
2465
2466   off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
2467   unsigned char* dyn_oview = NULL;
2468   if (dyn_output_size > 0)
2469     dyn_oview = of->get_output_view(this->local_dynsym_offset_,
2470                                     dyn_output_size);
2471
2472   // Write the local symbols.
2473   unsigned char* ov = oview;
2474   unsigned char* dyn_ov = dyn_oview;
2475   const Stringpool* sympool = layout->sympool();
2476   const Stringpool* dynpool = layout->dynpool();
2477   Output_symtab_xindex* symtab_xindex = layout->symtab_xindex();
2478   Output_symtab_xindex* dynsym_xindex = layout->dynsym_xindex();
2479   for (unsigned int i = 0; i < this->local_symbol_count_; ++i)
2480     {
2481       Local_symbol& lsym(this->local_symbols_[i]);
2482
2483       bool is_ordinary;
2484       unsigned int st_shndx = this->adjust_sym_shndx(i, lsym.st_shndx,
2485                                                      &is_ordinary);
2486       if (is_ordinary)
2487         {
2488           Output_section* os = this->ibase_->output_section(st_shndx);
2489           st_shndx = os->out_shndx();
2490           if (st_shndx >= elfcpp::SHN_LORESERVE)
2491             {
2492               symtab_xindex->add(this->local_symbol_index_ + i, st_shndx);
2493               if (lsym.needs_dynsym_entry)
2494                 dynsym_xindex->add(lsym.output_dynsym_index, st_shndx);
2495               st_shndx = elfcpp::SHN_XINDEX;
2496             }
2497         }
2498
2499       // Write the symbol to the output symbol table.
2500       {
2501         elfcpp::Sym_write<size, big_endian> osym(ov);
2502         osym.put_st_name(sympool->get_offset(lsym.name));
2503         osym.put_st_value(lsym.st_value);
2504         osym.put_st_size(lsym.st_size);
2505         osym.put_st_info(elfcpp::STB_LOCAL,
2506                          static_cast<elfcpp::STT>(lsym.st_type));
2507         osym.put_st_other(0);
2508         osym.put_st_shndx(st_shndx);
2509         ov += sym_size;
2510       }
2511
2512       // Write the symbol to the output dynamic symbol table.
2513       if (lsym.needs_dynsym_entry)
2514         {
2515           gold_assert(dyn_ov < dyn_oview + dyn_output_size);
2516           elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
2517           osym.put_st_name(dynpool->get_offset(lsym.name));
2518           osym.put_st_value(lsym.st_value);
2519           osym.put_st_size(lsym.st_size);
2520           osym.put_st_info(elfcpp::STB_LOCAL,
2521                            static_cast<elfcpp::STT>(lsym.st_type));
2522           osym.put_st_other(0);
2523           osym.put_st_shndx(st_shndx);
2524           dyn_ov += sym_size;
2525         }
2526     }
2527
2528   if (output_size > 0)
2529     {
2530       gold_assert(ov - oview == output_size);
2531       of->write_output_view(symtab_off + this->local_symbol_offset_,
2532                             output_size, oview);
2533     }
2534
2535   if (dyn_output_size > 0)
2536     {
2537       gold_assert(dyn_ov - dyn_oview == dyn_output_size);
2538       of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
2539                             dyn_oview);
2540     }
2541 }
2542
2543 // Set the offset of a section.
2544
2545 template<int size, bool big_endian>
2546 void
2547 Sized_relobj_incr<size, big_endian>::do_set_section_offset(unsigned int,
2548                                                            uint64_t)
2549 {
2550 }
2551
2552 // Class Sized_incr_dynobj.  Most of these methods are not used for
2553 // Incremental objects, but are required to be implemented by the
2554 // base class Object.
2555
2556 template<int size, bool big_endian>
2557 Sized_incr_dynobj<size, big_endian>::Sized_incr_dynobj(
2558     const std::string& name,
2559     Sized_incremental_binary<size, big_endian>* ibase,
2560     unsigned int input_file_index)
2561   : Dynobj(name, NULL), ibase_(ibase),
2562     input_file_index_(input_file_index),
2563     input_reader_(ibase->inputs_reader().input_file(input_file_index)),
2564     symbols_()
2565 {
2566   if (this->input_reader_.is_in_system_directory())
2567     this->set_is_in_system_directory();
2568   if (this->input_reader_.as_needed())
2569     this->set_as_needed();
2570   this->set_soname_string(this->input_reader_.get_soname());
2571   this->set_shnum(0);
2572 }
2573
2574 // Read the symbols.
2575
2576 template<int size, bool big_endian>
2577 void
2578 Sized_incr_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
2579 {
2580   gold_unreachable();
2581 }
2582
2583 // Lay out the input sections.
2584
2585 template<int size, bool big_endian>
2586 void
2587 Sized_incr_dynobj<size, big_endian>::do_layout(
2588     Symbol_table*,
2589     Layout*,
2590     Read_symbols_data*)
2591 {
2592 }
2593
2594 // Add the symbols to the symbol table.
2595
2596 template<int size, bool big_endian>
2597 void
2598 Sized_incr_dynobj<size, big_endian>::do_add_symbols(
2599     Symbol_table* symtab,
2600     Read_symbols_data*,
2601     Layout*)
2602 {
2603   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2604   unsigned char symbuf[sym_size];
2605   elfcpp::Sym<size, big_endian> sym(symbuf);
2606   elfcpp::Sym_write<size, big_endian> osym(symbuf);
2607
2608   typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2609
2610   unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2611   this->symbols_.resize(nsyms);
2612
2613   Incremental_binary::View symtab_view(NULL);
2614   unsigned int symtab_count;
2615   elfcpp::Elf_strtab strtab(NULL, 0);
2616   this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2617
2618   Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2619   unsigned int isym_count = isymtab.symbol_count();
2620   unsigned int first_global = symtab_count - isym_count;
2621
2622   // We keep a set of symbols that we have generated COPY relocations
2623   // for, indexed by the symbol value. We do not need more than one
2624   // COPY relocation per address.
2625   typedef typename std::set<Address> Copied_symbols;
2626   Copied_symbols copied_symbols;
2627
2628   const unsigned char* sym_p;
2629   for (unsigned int i = 0; i < nsyms; ++i)
2630     {
2631       bool is_def;
2632       bool is_copy;
2633       unsigned int output_symndx =
2634           this->input_reader_.get_output_symbol_index(i, &is_def, &is_copy);
2635       sym_p = symtab_view.data() + output_symndx * sym_size;
2636       elfcpp::Sym<size, big_endian> gsym(sym_p);
2637       const char* name;
2638       if (!strtab.get_c_string(gsym.get_st_name(), &name))
2639         name = "";
2640
2641       Address v;
2642       unsigned int shndx;
2643       elfcpp::STB st_bind = gsym.get_st_bind();
2644       elfcpp::STT st_type = gsym.get_st_type();
2645
2646       // Local hidden symbols start out as globals, but get converted to
2647       // to local during output.
2648       if (st_bind == elfcpp::STB_LOCAL)
2649         st_bind = elfcpp::STB_GLOBAL;
2650
2651       if (!is_def)
2652         {
2653           shndx = elfcpp::SHN_UNDEF;
2654           v = 0;
2655         }
2656       else
2657         {
2658           // For a symbol defined in a shared object, the section index
2659           // is meaningless, as long as it's not SHN_UNDEF.
2660           shndx = 1;
2661           v = gsym.get_st_value();
2662         }
2663
2664       osym.put_st_name(0);
2665       osym.put_st_value(v);
2666       osym.put_st_size(gsym.get_st_size());
2667       osym.put_st_info(st_bind, st_type);
2668       osym.put_st_other(gsym.get_st_other());
2669       osym.put_st_shndx(shndx);
2670
2671       Sized_symbol<size>* res =
2672           symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
2673       this->symbols_[i] = res;
2674       this->ibase_->add_global_symbol(output_symndx - first_global,
2675                                       this->symbols_[i]);
2676
2677       if (is_copy)
2678         {
2679           std::pair<typename Copied_symbols::iterator, bool> ins =
2680               copied_symbols.insert(v);
2681           if (ins.second)
2682             {
2683               unsigned int shndx = gsym.get_st_shndx();
2684               Output_section* os = this->ibase_->output_section(shndx);
2685               off_t offset = v - os->address();
2686               this->ibase_->add_copy_reloc(this->symbols_[i], os, offset);
2687             }
2688         }
2689     }
2690 }
2691
2692 // Return TRUE if we should include this object from an archive library.
2693
2694 template<int size, bool big_endian>
2695 Archive::Should_include
2696 Sized_incr_dynobj<size, big_endian>::do_should_include_member(
2697     Symbol_table*,
2698     Layout*,
2699     Read_symbols_data*,
2700     std::string*)
2701 {
2702   gold_unreachable();
2703 }
2704
2705 // Iterate over global symbols, calling a visitor class V for each.
2706
2707 template<int size, bool big_endian>
2708 void
2709 Sized_incr_dynobj<size, big_endian>::do_for_all_global_symbols(
2710     Read_symbols_data*,
2711     Library_base::Symbol_visitor_base*)
2712 {
2713   // This routine is not used for dynamic libraries.
2714 }
2715
2716 // Iterate over local symbols, calling a visitor class V for each GOT offset
2717 // associated with a local symbol.
2718
2719 template<int size, bool big_endian>
2720 void
2721 Sized_incr_dynobj<size, big_endian>::do_for_all_local_got_entries(
2722     Got_offset_list::Visitor*) const
2723 {
2724 }
2725
2726 // Get the size of a section.
2727
2728 template<int size, bool big_endian>
2729 uint64_t
2730 Sized_incr_dynobj<size, big_endian>::do_section_size(unsigned int)
2731 {
2732   gold_unreachable();
2733 }
2734
2735 // Get the name of a section.
2736
2737 template<int size, bool big_endian>
2738 std::string
2739 Sized_incr_dynobj<size, big_endian>::do_section_name(unsigned int)
2740 {
2741   gold_unreachable();
2742 }
2743
2744 // Return a view of the contents of a section.
2745
2746 template<int size, bool big_endian>
2747 Object::Location
2748 Sized_incr_dynobj<size, big_endian>::do_section_contents(unsigned int)
2749 {
2750   gold_unreachable();
2751 }
2752
2753 // Return section flags.
2754
2755 template<int size, bool big_endian>
2756 uint64_t
2757 Sized_incr_dynobj<size, big_endian>::do_section_flags(unsigned int)
2758 {
2759   gold_unreachable();
2760 }
2761
2762 // Return section entsize.
2763
2764 template<int size, bool big_endian>
2765 uint64_t
2766 Sized_incr_dynobj<size, big_endian>::do_section_entsize(unsigned int)
2767 {
2768   gold_unreachable();
2769 }
2770
2771 // Return section address.
2772
2773 template<int size, bool big_endian>
2774 uint64_t
2775 Sized_incr_dynobj<size, big_endian>::do_section_address(unsigned int)
2776 {
2777   gold_unreachable();
2778 }
2779
2780 // Return section type.
2781
2782 template<int size, bool big_endian>
2783 unsigned int
2784 Sized_incr_dynobj<size, big_endian>::do_section_type(unsigned int)
2785 {
2786   gold_unreachable();
2787 }
2788
2789 // Return the section link field.
2790
2791 template<int size, bool big_endian>
2792 unsigned int
2793 Sized_incr_dynobj<size, big_endian>::do_section_link(unsigned int)
2794 {
2795   gold_unreachable();
2796 }
2797
2798 // Return the section link field.
2799
2800 template<int size, bool big_endian>
2801 unsigned int
2802 Sized_incr_dynobj<size, big_endian>::do_section_info(unsigned int)
2803 {
2804   gold_unreachable();
2805 }
2806
2807 // Return the section alignment.
2808
2809 template<int size, bool big_endian>
2810 uint64_t
2811 Sized_incr_dynobj<size, big_endian>::do_section_addralign(unsigned int)
2812 {
2813   gold_unreachable();
2814 }
2815
2816 // Return the Xindex structure to use.
2817
2818 template<int size, bool big_endian>
2819 Xindex*
2820 Sized_incr_dynobj<size, big_endian>::do_initialize_xindex()
2821 {
2822   gold_unreachable();
2823 }
2824
2825 // Get symbol counts.
2826
2827 template<int size, bool big_endian>
2828 void
2829 Sized_incr_dynobj<size, big_endian>::do_get_global_symbol_counts(
2830     const Symbol_table*, size_t*, size_t*) const
2831 {
2832   gold_unreachable();
2833 }
2834
2835 // Allocate an incremental object of the appropriate size and endianness.
2836
2837 Object*
2838 make_sized_incremental_object(
2839     Incremental_binary* ibase,
2840     unsigned int input_file_index,
2841     Incremental_input_type input_type,
2842     const Incremental_binary::Input_reader* input_reader)
2843 {
2844   Object* obj = NULL;
2845   std::string name(input_reader->filename());
2846
2847   switch (parameters->size_and_endianness())
2848     {
2849 #ifdef HAVE_TARGET_32_LITTLE
2850     case Parameters::TARGET_32_LITTLE:
2851       {
2852         Sized_incremental_binary<32, false>* sized_ibase =
2853             static_cast<Sized_incremental_binary<32, false>*>(ibase);
2854         if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2855           obj = new Sized_incr_dynobj<32, false>(name, sized_ibase,
2856                                                  input_file_index);
2857         else
2858           obj = new Sized_relobj_incr<32, false>(name, sized_ibase,
2859                                                  input_file_index);
2860       }
2861       break;
2862 #endif
2863 #ifdef HAVE_TARGET_32_BIG
2864     case Parameters::TARGET_32_BIG:
2865       {
2866         Sized_incremental_binary<32, true>* sized_ibase =
2867             static_cast<Sized_incremental_binary<32, true>*>(ibase);
2868         if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2869           obj = new Sized_incr_dynobj<32, true>(name, sized_ibase,
2870                                                 input_file_index);
2871         else
2872           obj = new Sized_relobj_incr<32, true>(name, sized_ibase,
2873                                                 input_file_index);
2874       }
2875       break;
2876 #endif
2877 #ifdef HAVE_TARGET_64_LITTLE
2878     case Parameters::TARGET_64_LITTLE:
2879       {
2880         Sized_incremental_binary<64, false>* sized_ibase =
2881             static_cast<Sized_incremental_binary<64, false>*>(ibase);
2882         if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2883           obj = new Sized_incr_dynobj<64, false>(name, sized_ibase,
2884                                                  input_file_index);
2885         else
2886           obj = new Sized_relobj_incr<64, false>(name, sized_ibase,
2887                                                  input_file_index);
2888      }
2889       break;
2890 #endif
2891 #ifdef HAVE_TARGET_64_BIG
2892     case Parameters::TARGET_64_BIG:
2893       {
2894         Sized_incremental_binary<64, true>* sized_ibase =
2895             static_cast<Sized_incremental_binary<64, true>*>(ibase);
2896         if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2897           obj = new Sized_incr_dynobj<64, true>(name, sized_ibase,
2898                                                 input_file_index);
2899         else
2900           obj = new Sized_relobj_incr<64, true>(name, sized_ibase,
2901                                                 input_file_index);
2902       }
2903       break;
2904 #endif
2905     default:
2906       gold_unreachable();
2907     }
2908
2909   gold_assert(obj != NULL);
2910   return obj;
2911 }
2912
2913 // Copy the unused symbols from the incremental input info.
2914 // We need to do this because we may be overwriting the incremental
2915 // input info in the base file before we write the new incremental
2916 // info.
2917 void
2918 Incremental_library::copy_unused_symbols()
2919 {
2920   unsigned int symcount = this->input_reader_->get_unused_symbol_count();
2921   this->unused_symbols_.reserve(symcount);
2922   for (unsigned int i = 0; i < symcount; ++i)
2923     {
2924       std::string name(this->input_reader_->get_unused_symbol(i));
2925       this->unused_symbols_.push_back(name);
2926     }
2927 }
2928
2929 // Iterator for unused global symbols in the library.
2930 void
2931 Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base* v) const
2932 {
2933   for (Symbol_list::const_iterator p = this->unused_symbols_.begin();
2934        p != this->unused_symbols_.end();
2935        ++p)
2936   v->visit(p->c_str());
2937 }
2938
2939 // Instantiate the templates we need.
2940
2941 #ifdef HAVE_TARGET_32_LITTLE
2942 template
2943 class Sized_incremental_binary<32, false>;
2944
2945 template
2946 class Sized_relobj_incr<32, false>;
2947
2948 template
2949 class Sized_incr_dynobj<32, false>;
2950 #endif
2951
2952 #ifdef HAVE_TARGET_32_BIG
2953 template
2954 class Sized_incremental_binary<32, true>;
2955
2956 template
2957 class Sized_relobj_incr<32, true>;
2958
2959 template
2960 class Sized_incr_dynobj<32, true>;
2961 #endif
2962
2963 #ifdef HAVE_TARGET_64_LITTLE
2964 template
2965 class Sized_incremental_binary<64, false>;
2966
2967 template
2968 class Sized_relobj_incr<64, false>;
2969
2970 template
2971 class Sized_incr_dynobj<64, false>;
2972 #endif
2973
2974 #ifdef HAVE_TARGET_64_BIG
2975 template
2976 class Sized_incremental_binary<64, true>;
2977
2978 template
2979 class Sized_relobj_incr<64, true>;
2980
2981 template
2982 class Sized_incr_dynobj<64, true>;
2983 #endif
2984
2985 } // End namespace gold.