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