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