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