xref: /netbsd-src/external/gpl3/binutils.old/dist/gold/incremental.cc (revision e992f068c547fd6e84b3f104dc2340adcc955732)
1 // inremental.cc -- incremental linking support for gold
2 
3 // Copyright (C) 2009-2022 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:
Output_section_incremental_inputs(const Incremental_inputs * inputs,const Symbol_table * symtab)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
update_data_size()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
do_print_to_mapfile(Mapfile * mapfile) const78   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
vexplain_no_incremental(const char * format,va_list args)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
explain_no_incremental(const char * format,...)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
error(const char * format,...) const161 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
can_incremental_update(unsigned int sh_type)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
find_incremental_inputs_sections(unsigned int * p_inputs_shndx,unsigned int * p_symtab_shndx,unsigned int * p_relocs_shndx,unsigned int * p_got_plt_shndx,unsigned int * p_strtab_shndx)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
setup_readers()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
check_input_args(std::vector<const Input_argument * > & input_args_map,Input_arguments::const_iterator begin,Input_arguments::const_iterator end)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
do_check_inputs(const Command_line & cmdline,Incremental_inputs * incremental_inputs)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
do_file_has_changed(unsigned int n) const477 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
do_init_layout(Layout * layout)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
do_reserve_layout(unsigned int input_file_index)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
do_process_got_plt(Symbol_table * symtab,Layout * layout)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
do_emit_copy_relocs(Symbol_table * symtab)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
do_apply_incremental_relocs(const Symbol_table * symtab,Layout * layout,Output_file * of)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
get_symtab_view(View * symtab_view,unsigned int * nsyms,elfcpp::Elf_strtab * strtab)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*
make_sized_incremental_binary(Output_file * file,const elfcpp::Ehdr<size,big_endian> & ehdr)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_ei_osabi(),
860 				 ehdr.get_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*
open_incremental_binary(Output_file * file)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
report_command_line(int argc,const char * const * argv)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
report_archive_begin(Library_base * arch,unsigned int arg_serial,Script_info * script_info)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:
Unused_symbol_visitor(Incremental_archive_entry * entry,Stringpool * strtab)1055   Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab)
1056     : entry_(entry), strtab_(strtab)
1057   { }
1058 
1059   void
visit(const char * sym)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
report_archive_end(Library_base * arch)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
report_object(Object * obj,unsigned int arg_serial,Library_base * arch,Script_info * script_info)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
report_input_section(Object * obj,unsigned int shndx,const char * name,off_t sh_size)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
report_comdat_group(Object * obj,const char * name)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
report_script(Script_info * script,unsigned int arg_serial,Timespec mtime)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
finalize()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
create_data_sections(Symbol_table * symtab)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
relocs_entsize() const1255 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
set_final_data_size()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
do_write(Output_file * of)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*
write_header(unsigned char * pov,unsigned int input_file_count,section_offset_type command_line_offset)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*
write_input_files(unsigned char * oview,unsigned char * pov,Stringpool * strtab)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*
write_info_blocks(unsigned char * oview,unsigned char * pov,Stringpool * strtab,unsigned int * global_syms,unsigned int global_sym_count)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
write_symtab(unsigned char * pov,unsigned int * global_syms,unsigned int global_sym_count)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:
Local_got_offset_visitor(struct Got_plt_view_info & info)1846   Local_got_offset_visitor(struct Got_plt_view_info& info)
1847     : info_(info)
1848   { }
1849 
1850   void
visit(unsigned int got_type,unsigned int got_offset,uint64_t)1851   visit(unsigned int got_type, unsigned int got_offset, uint64_t)
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     // FIXME: the uint64_t addend should be written here if powerpc64
1864     // sym+addend got entries are to be supported, with similar changes
1865     // to Global_got_offset_visitor and support to read them back in
1866     // do_process_got_plt.
1867     // FIXME: don't we need this for section symbol plus addend anyway?
1868     // (See 2015-12-03 commit 7ef8ae7c5f35)
1869   }
1870 
1871  private:
1872   struct Got_plt_view_info& info_;
1873 };
1874 
1875 // Functor class for processing a GOT offset list.  Writes the GOT type
1876 // and symbol index into the GOT type and descriptor arrays in the output
1877 // section.
1878 
1879 template<int size, bool big_endian>
1880 class Global_got_offset_visitor : public Got_offset_list::Visitor
1881 {
1882  public:
Global_got_offset_visitor(struct Got_plt_view_info & info)1883   Global_got_offset_visitor(struct Got_plt_view_info& info)
1884     : info_(info)
1885   { }
1886 
1887   void
visit(unsigned int got_type,unsigned int got_offset,uint64_t)1888   visit(unsigned int got_type, unsigned int got_offset, uint64_t)
1889   {
1890     unsigned int got_index = got_offset / this->info_.got_entry_size;
1891     gold_assert(got_index < this->info_.got_count);
1892     // We can only handle GOT entry types in the range 0..0x7e
1893     // because we use a byte array to store them, and we use the
1894     // high bit to flag a local symbol.
1895     gold_assert(got_type < 0x7f);
1896     this->info_.got_type_p[got_index] = got_type;
1897     unsigned char* pov = this->info_.got_desc_p + got_index * 8;
1898     elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
1899     elfcpp::Swap<32, big_endian>::writeval(pov + 4, 0);
1900   }
1901 
1902  private:
1903   struct Got_plt_view_info& info_;
1904 };
1905 
1906 // Functor class for processing the global symbol table.  Processes the
1907 // GOT offset list for the symbol, and writes the symbol table index
1908 // into the PLT descriptor array in the output section.
1909 
1910 template<int size, bool big_endian>
1911 class Global_symbol_visitor_got_plt
1912 {
1913  public:
Global_symbol_visitor_got_plt(struct Got_plt_view_info & info)1914   Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
1915     : info_(info)
1916   { }
1917 
1918   void
operator ()(const Sized_symbol<size> * sym)1919   operator()(const Sized_symbol<size>* sym)
1920   {
1921     typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
1922     const Got_offset_list* got_offsets = sym->got_offset_list();
1923     if (got_offsets != NULL)
1924       {
1925 	this->info_.sym_index = sym->symtab_index();
1926 	this->info_.input_index = 0;
1927 	Got_visitor v(this->info_);
1928 	got_offsets->for_all_got_offsets(&v);
1929       }
1930     if (sym->has_plt_offset())
1931       {
1932 	unsigned int plt_index =
1933 	    ((sym->plt_offset() - this->info_.first_plt_entry_offset)
1934 	     / this->info_.plt_entry_size);
1935 	gold_assert(plt_index < this->info_.plt_count);
1936 	unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
1937 	elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
1938       }
1939   }
1940 
1941  private:
1942   struct Got_plt_view_info& info_;
1943 };
1944 
1945 // Write the contents of the .gnu_incremental_got_plt section.
1946 
1947 template<int size, bool big_endian>
1948 void
write_got_plt(unsigned char * pov,off_t view_size)1949 Output_section_incremental_inputs<size, big_endian>::write_got_plt(
1950     unsigned char* pov,
1951     off_t view_size)
1952 {
1953   Sized_target<size, big_endian>* target =
1954     parameters->sized_target<size, big_endian>();
1955 
1956   // Set up the view information for the functors.
1957   struct Got_plt_view_info view_info;
1958   view_info.got_count = target->got_entry_count();
1959   view_info.plt_count = target->plt_entry_count();
1960   view_info.first_plt_entry_offset = target->first_plt_entry_offset();
1961   view_info.plt_entry_size = target->plt_entry_size();
1962   view_info.got_entry_size = target->got_entry_size();
1963   view_info.got_type_p = pov + 8;
1964   view_info.got_desc_p = (view_info.got_type_p
1965 			  + ((view_info.got_count + 3) & ~3));
1966   view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 8;
1967 
1968   gold_assert(pov + view_size ==
1969 	      view_info.plt_desc_p + view_info.plt_count * 4);
1970 
1971   // Write the section header.
1972   Swap32::writeval(pov, view_info.got_count);
1973   Swap32::writeval(pov + 4, view_info.plt_count);
1974 
1975   // Initialize the GOT type array to 0xff (reserved).
1976   memset(view_info.got_type_p, 0xff, view_info.got_count);
1977 
1978   // Write the incremental GOT descriptors for local symbols.
1979   typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
1980   for (Incremental_inputs::Input_list::const_iterator p =
1981 	   this->inputs_->input_files().begin();
1982        p != this->inputs_->input_files().end();
1983        ++p)
1984     {
1985       if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
1986 	  && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
1987 	continue;
1988       Incremental_object_entry* entry = (*p)->object_entry();
1989       gold_assert(entry != NULL);
1990       const Object* obj = entry->object();
1991       gold_assert(obj != NULL);
1992       view_info.input_index = (*p)->get_file_index();
1993       Got_visitor v(view_info);
1994       obj->for_all_local_got_entries(&v);
1995     }
1996 
1997   // Write the incremental GOT and PLT descriptors for global symbols.
1998   typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
1999   symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
2000 }
2001 
2002 // Class Sized_relobj_incr.  Most of these methods are not used for
2003 // Incremental objects, but are required to be implemented by the
2004 // base class Object.
2005 
2006 template<int size, bool big_endian>
Sized_relobj_incr(const std::string & name,Sized_incremental_binary<size,big_endian> * ibase,unsigned int input_file_index)2007 Sized_relobj_incr<size, big_endian>::Sized_relobj_incr(
2008     const std::string& name,
2009     Sized_incremental_binary<size, big_endian>* ibase,
2010     unsigned int input_file_index)
2011   : Sized_relobj<size, big_endian>(name, NULL), ibase_(ibase),
2012     input_file_index_(input_file_index),
2013     input_reader_(ibase->inputs_reader().input_file(input_file_index)),
2014     local_symbol_count_(0), output_local_dynsym_count_(0),
2015     local_symbol_index_(0), local_symbol_offset_(0), local_dynsym_offset_(0),
2016     symbols_(), defined_count_(0), incr_reloc_offset_(-1U),
2017     incr_reloc_count_(0), incr_reloc_output_index_(0), incr_relocs_(NULL),
2018     local_symbols_()
2019 {
2020   if (this->input_reader_.is_in_system_directory())
2021     this->set_is_in_system_directory();
2022   const unsigned int shnum = this->input_reader_.get_input_section_count() + 1;
2023   this->set_shnum(shnum);
2024   ibase->set_input_object(input_file_index, this);
2025 }
2026 
2027 // Read the symbols.
2028 
2029 template<int size, bool big_endian>
2030 void
do_read_symbols(Read_symbols_data *)2031 Sized_relobj_incr<size, big_endian>::do_read_symbols(Read_symbols_data*)
2032 {
2033   gold_unreachable();
2034 }
2035 
2036 // Lay out the input sections.
2037 
2038 template<int size, bool big_endian>
2039 void
do_layout(Symbol_table *,Layout * layout,Read_symbols_data *)2040 Sized_relobj_incr<size, big_endian>::do_layout(
2041     Symbol_table*,
2042     Layout* layout,
2043     Read_symbols_data*)
2044 {
2045   const unsigned int shnum = this->shnum();
2046   Incremental_inputs* incremental_inputs = layout->incremental_inputs();
2047   gold_assert(incremental_inputs != NULL);
2048   Output_sections& out_sections(this->output_sections());
2049   out_sections.resize(shnum);
2050   this->section_offsets().resize(shnum);
2051 
2052   // Keep track of .debug_info and .debug_types sections.
2053   std::vector<unsigned int> debug_info_sections;
2054   std::vector<unsigned int> debug_types_sections;
2055 
2056   for (unsigned int i = 1; i < shnum; i++)
2057     {
2058       typename Input_entry_reader::Input_section_info sect =
2059 	  this->input_reader_.get_input_section(i - 1);
2060       // Add the section to the incremental inputs layout.
2061       incremental_inputs->report_input_section(this, i, sect.name,
2062 					       sect.sh_size);
2063       if (sect.output_shndx == 0 || sect.sh_offset == -1)
2064 	continue;
2065       Output_section* os = this->ibase_->output_section(sect.output_shndx);
2066       gold_assert(os != NULL);
2067       out_sections[i] = os;
2068       this->section_offsets()[i] = static_cast<Address>(sect.sh_offset);
2069 
2070       // When generating a .gdb_index section, we do additional
2071       // processing of .debug_info and .debug_types sections after all
2072       // the other sections.
2073       if (parameters->options().gdb_index())
2074 	{
2075 	  const char* name = os->name();
2076 	  if (strcmp(name, ".debug_info") == 0)
2077 	    debug_info_sections.push_back(i);
2078 	  else if (strcmp(name, ".debug_types") == 0)
2079 	    debug_types_sections.push_back(i);
2080 	}
2081     }
2082 
2083   // Process the COMDAT groups.
2084   unsigned int ncomdat = this->input_reader_.get_comdat_group_count();
2085   for (unsigned int i = 0; i < ncomdat; i++)
2086     {
2087       const char* signature = this->input_reader_.get_comdat_group_signature(i);
2088       if (signature == NULL || signature[0] == '\0')
2089 	this->error(_("COMDAT group has no signature"));
2090       bool keep = layout->find_or_add_kept_section(signature, this, i, true,
2091 						   true, NULL);
2092       if (keep)
2093 	incremental_inputs->report_comdat_group(this, signature);
2094       else
2095 	this->error(_("COMDAT group %s included twice in incremental link"),
2096 		    signature);
2097     }
2098 
2099   // When building a .gdb_index section, scan the .debug_info and
2100   // .debug_types sections.
2101   for (std::vector<unsigned int>::const_iterator p
2102 	   = debug_info_sections.begin();
2103        p != debug_info_sections.end();
2104        ++p)
2105     {
2106       unsigned int i = *p;
2107       layout->add_to_gdb_index(false, this, NULL, 0, i, 0, 0);
2108     }
2109   for (std::vector<unsigned int>::const_iterator p
2110 	   = debug_types_sections.begin();
2111        p != debug_types_sections.end();
2112        ++p)
2113     {
2114       unsigned int i = *p;
2115       layout->add_to_gdb_index(true, this, 0, 0, i, 0, 0);
2116     }
2117 }
2118 
2119 // Layout sections whose layout was deferred while waiting for
2120 // input files from a plugin.
2121 template<int size, bool big_endian>
2122 void
do_layout_deferred_sections(Layout *)2123 Sized_relobj_incr<size, big_endian>::do_layout_deferred_sections(Layout*)
2124 {
2125 }
2126 
2127 // Add the symbols to the symbol table.
2128 
2129 template<int size, bool big_endian>
2130 void
do_add_symbols(Symbol_table * symtab,Read_symbols_data *,Layout *)2131 Sized_relobj_incr<size, big_endian>::do_add_symbols(
2132     Symbol_table* symtab,
2133     Read_symbols_data*,
2134     Layout*)
2135 {
2136   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2137   unsigned char symbuf[sym_size];
2138   elfcpp::Sym_write<size, big_endian> osym(symbuf);
2139 
2140   typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2141 
2142   unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2143   this->symbols_.resize(nsyms);
2144 
2145   Incremental_binary::View symtab_view(NULL);
2146   unsigned int symtab_count;
2147   elfcpp::Elf_strtab strtab(NULL, 0);
2148   this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2149 
2150   Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2151   unsigned int isym_count = isymtab.symbol_count();
2152   unsigned int first_global = symtab_count - isym_count;
2153 
2154   const unsigned char* sym_p;
2155   for (unsigned int i = 0; i < nsyms; ++i)
2156     {
2157       Incremental_global_symbol_reader<big_endian> info =
2158 	  this->input_reader_.get_global_symbol_reader(i);
2159       unsigned int output_symndx = info.output_symndx();
2160       sym_p = symtab_view.data() + output_symndx * sym_size;
2161       elfcpp::Sym<size, big_endian> gsym(sym_p);
2162       const char* name;
2163       if (!strtab.get_c_string(gsym.get_st_name(), &name))
2164 	name = "";
2165 
2166       typename elfcpp::Elf_types<size>::Elf_Addr v = gsym.get_st_value();
2167       unsigned int shndx = gsym.get_st_shndx();
2168       elfcpp::STB st_bind = gsym.get_st_bind();
2169       elfcpp::STT st_type = gsym.get_st_type();
2170 
2171       // Local hidden symbols start out as globals, but get converted to
2172       // to local during output.
2173       if (st_bind == elfcpp::STB_LOCAL)
2174 	st_bind = elfcpp::STB_GLOBAL;
2175 
2176       unsigned int input_shndx = info.shndx();
2177       if (input_shndx == 0 || input_shndx == -1U)
2178 	{
2179 	  shndx = elfcpp::SHN_UNDEF;
2180 	  v = 0;
2181 	}
2182       else if (shndx != elfcpp::SHN_ABS)
2183 	{
2184 	  // Find the input section and calculate the section-relative value.
2185 	  gold_assert(shndx != elfcpp::SHN_UNDEF);
2186 	  Output_section* os = this->ibase_->output_section(shndx);
2187 	  gold_assert(os != NULL && os->has_fixed_layout());
2188 	  typename Input_entry_reader::Input_section_info sect =
2189 	      this->input_reader_.get_input_section(input_shndx - 1);
2190 	  gold_assert(sect.output_shndx == shndx);
2191 	  if (st_type != elfcpp::STT_TLS)
2192 	    v -= os->address();
2193 	  v -= sect.sh_offset;
2194 	  shndx = input_shndx;
2195 	}
2196 
2197       osym.put_st_name(0);
2198       osym.put_st_value(v);
2199       osym.put_st_size(gsym.get_st_size());
2200       osym.put_st_info(st_bind, st_type);
2201       osym.put_st_other(gsym.get_st_other());
2202       osym.put_st_shndx(shndx);
2203 
2204       elfcpp::Sym<size, big_endian> sym(symbuf);
2205       Symbol* res = symtab->add_from_incrobj(this, name, NULL, &sym);
2206 
2207       if (shndx != elfcpp::SHN_UNDEF)
2208 	++this->defined_count_;
2209 
2210       // If this is a linker-defined symbol that hasn't yet been defined,
2211       // define it now.
2212       if (input_shndx == -1U && !res->is_defined())
2213 	{
2214 	  shndx = gsym.get_st_shndx();
2215 	  v = gsym.get_st_value();
2216 	  Elf_size_type symsize = gsym.get_st_size();
2217 	  if (shndx == elfcpp::SHN_ABS)
2218 	    {
2219 	      symtab->define_as_constant(name, NULL,
2220 					 Symbol_table::INCREMENTAL_BASE,
2221 					 v, symsize, st_type, st_bind,
2222 					 gsym.get_st_visibility(), 0,
2223 					 false, false);
2224 	    }
2225 	  else
2226 	    {
2227 	      Output_section* os = this->ibase_->output_section(shndx);
2228 	      gold_assert(os != NULL && os->has_fixed_layout());
2229 	      v -= os->address();
2230 	      if (symsize > 0)
2231 		os->reserve(v, symsize);
2232 	      symtab->define_in_output_data(name, NULL,
2233 					    Symbol_table::INCREMENTAL_BASE,
2234 					    os, v, symsize, st_type, st_bind,
2235 					    gsym.get_st_visibility(), 0,
2236 					    false, false);
2237 	    }
2238 	}
2239 
2240       this->symbols_[i] = res;
2241       this->ibase_->add_global_symbol(output_symndx - first_global, res);
2242     }
2243 }
2244 
2245 // Return TRUE if we should include this object from an archive library.
2246 
2247 template<int size, bool big_endian>
2248 Archive::Should_include
do_should_include_member(Symbol_table *,Layout *,Read_symbols_data *,std::string *)2249 Sized_relobj_incr<size, big_endian>::do_should_include_member(
2250     Symbol_table*,
2251     Layout*,
2252     Read_symbols_data*,
2253     std::string*)
2254 {
2255   gold_unreachable();
2256 }
2257 
2258 // Iterate over global symbols, calling a visitor class V for each.
2259 
2260 template<int size, bool big_endian>
2261 void
do_for_all_global_symbols(Read_symbols_data *,Library_base::Symbol_visitor_base *)2262 Sized_relobj_incr<size, big_endian>::do_for_all_global_symbols(
2263     Read_symbols_data*,
2264     Library_base::Symbol_visitor_base*)
2265 {
2266   // This routine is not used for incremental objects.
2267 }
2268 
2269 // Get the size of a section.
2270 
2271 template<int size, bool big_endian>
2272 uint64_t
do_section_size(unsigned int)2273 Sized_relobj_incr<size, big_endian>::do_section_size(unsigned int)
2274 {
2275   gold_unreachable();
2276 }
2277 
2278 // Get the name of a section.  This returns the name of the output
2279 // section, because we don't usually track the names of the input
2280 // sections.
2281 
2282 template<int size, bool big_endian>
2283 std::string
do_section_name(unsigned int shndx) const2284 Sized_relobj_incr<size, big_endian>::do_section_name(unsigned int shndx) const
2285 {
2286   const Output_sections& out_sections(this->output_sections());
2287   const Output_section* os = out_sections[shndx];
2288   if (os == NULL)
2289     return std::string();
2290   return os->name();
2291 }
2292 
2293 // Return a view of the contents of a section.
2294 
2295 template<int size, bool big_endian>
2296 const unsigned char*
do_section_contents(unsigned int shndx,section_size_type * plen,bool)2297 Sized_relobj_incr<size, big_endian>::do_section_contents(
2298     unsigned int shndx,
2299     section_size_type* plen,
2300     bool)
2301 {
2302   Output_sections& out_sections(this->output_sections());
2303   Output_section* os = out_sections[shndx];
2304   gold_assert(os != NULL);
2305   off_t section_offset = os->offset();
2306   typename Input_entry_reader::Input_section_info sect =
2307       this->input_reader_.get_input_section(shndx - 1);
2308   section_offset += sect.sh_offset;
2309   *plen = sect.sh_size;
2310   return this->ibase_->view(section_offset, sect.sh_size).data();
2311 }
2312 
2313 // Return section flags.
2314 
2315 template<int size, bool big_endian>
2316 uint64_t
do_section_flags(unsigned int)2317 Sized_relobj_incr<size, big_endian>::do_section_flags(unsigned int)
2318 {
2319   gold_unreachable();
2320 }
2321 
2322 // Return section entsize.
2323 
2324 template<int size, bool big_endian>
2325 uint64_t
do_section_entsize(unsigned int)2326 Sized_relobj_incr<size, big_endian>::do_section_entsize(unsigned int)
2327 {
2328   gold_unreachable();
2329 }
2330 
2331 // Return section address.
2332 
2333 template<int size, bool big_endian>
2334 uint64_t
do_section_address(unsigned int)2335 Sized_relobj_incr<size, big_endian>::do_section_address(unsigned int)
2336 {
2337   gold_unreachable();
2338 }
2339 
2340 // Return section type.
2341 
2342 template<int size, bool big_endian>
2343 unsigned int
do_section_type(unsigned int)2344 Sized_relobj_incr<size, big_endian>::do_section_type(unsigned int)
2345 {
2346   gold_unreachable();
2347 }
2348 
2349 // Return the section link field.
2350 
2351 template<int size, bool big_endian>
2352 unsigned int
do_section_link(unsigned int)2353 Sized_relobj_incr<size, big_endian>::do_section_link(unsigned int)
2354 {
2355   gold_unreachable();
2356 }
2357 
2358 // Return the section link field.
2359 
2360 template<int size, bool big_endian>
2361 unsigned int
do_section_info(unsigned int)2362 Sized_relobj_incr<size, big_endian>::do_section_info(unsigned int)
2363 {
2364   gold_unreachable();
2365 }
2366 
2367 // Return the section alignment.
2368 
2369 template<int size, bool big_endian>
2370 uint64_t
do_section_addralign(unsigned int)2371 Sized_relobj_incr<size, big_endian>::do_section_addralign(unsigned int)
2372 {
2373   gold_unreachable();
2374 }
2375 
2376 // Return the Xindex structure to use.
2377 
2378 template<int size, bool big_endian>
2379 Xindex*
do_initialize_xindex()2380 Sized_relobj_incr<size, big_endian>::do_initialize_xindex()
2381 {
2382   gold_unreachable();
2383 }
2384 
2385 // Get symbol counts.
2386 
2387 template<int size, bool big_endian>
2388 void
do_get_global_symbol_counts(const Symbol_table *,size_t * defined,size_t * used) const2389 Sized_relobj_incr<size, big_endian>::do_get_global_symbol_counts(
2390     const Symbol_table*,
2391     size_t* defined,
2392     size_t* used) const
2393 {
2394   *defined = this->defined_count_;
2395   size_t count = 0;
2396   for (typename Symbols::const_iterator p = this->symbols_.begin();
2397        p != this->symbols_.end();
2398        ++p)
2399     if (*p != NULL
2400 	&& (*p)->source() == Symbol::FROM_OBJECT
2401 	&& (*p)->object() == this
2402 	&& (*p)->is_defined())
2403       ++count;
2404   *used = count;
2405 }
2406 
2407 // Read the relocs.
2408 
2409 template<int size, bool big_endian>
2410 void
do_read_relocs(Read_relocs_data *)2411 Sized_relobj_incr<size, big_endian>::do_read_relocs(Read_relocs_data*)
2412 {
2413 }
2414 
2415 // Process the relocs to find list of referenced sections. Used only
2416 // during garbage collection.
2417 
2418 template<int size, bool big_endian>
2419 void
do_gc_process_relocs(Symbol_table *,Layout *,Read_relocs_data *)2420 Sized_relobj_incr<size, big_endian>::do_gc_process_relocs(Symbol_table*,
2421 							  Layout*,
2422 							  Read_relocs_data*)
2423 {
2424   gold_unreachable();
2425 }
2426 
2427 // Scan the relocs and adjust the symbol table.
2428 
2429 template<int size, bool big_endian>
2430 void
do_scan_relocs(Symbol_table *,Layout * layout,Read_relocs_data *)2431 Sized_relobj_incr<size, big_endian>::do_scan_relocs(Symbol_table*,
2432 						    Layout* layout,
2433 						    Read_relocs_data*)
2434 {
2435   // Count the incremental relocations for this object.
2436   unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2437   this->allocate_incremental_reloc_counts();
2438   for (unsigned int i = 0; i < nsyms; i++)
2439     {
2440       Incremental_global_symbol_reader<big_endian> sym =
2441 	  this->input_reader_.get_global_symbol_reader(i);
2442       unsigned int reloc_count = sym.reloc_count();
2443       if (reloc_count > 0 && this->incr_reloc_offset_ == -1U)
2444 	this->incr_reloc_offset_ = sym.reloc_offset();
2445       this->incr_reloc_count_ += reloc_count;
2446       for (unsigned int j = 0; j < reloc_count; j++)
2447 	this->count_incremental_reloc(i);
2448     }
2449   this->incr_reloc_output_index_ =
2450       layout->incremental_inputs()->get_reloc_count();
2451   this->finalize_incremental_relocs(layout, false);
2452 
2453   // The incoming incremental relocations may not end up in the same
2454   // location after the incremental update, because the incremental info
2455   // is regenerated in each link.  Because the new location may overlap
2456   // with other data in the updated output file, we need to copy the
2457   // relocations into a buffer so that we can still read them safely
2458   // after we start writing updates to the output file.
2459   if (this->incr_reloc_count_ > 0)
2460     {
2461       const Incremental_relocs_reader<size, big_endian>& relocs_reader =
2462 	  this->ibase_->relocs_reader();
2463       const unsigned int incr_reloc_size = relocs_reader.reloc_size;
2464       unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2465       this->incr_relocs_ = new unsigned char[len];
2466       memcpy(this->incr_relocs_,
2467 	     relocs_reader.data(this->incr_reloc_offset_),
2468 	     len);
2469     }
2470 }
2471 
2472 // Count the local symbols.
2473 
2474 template<int size, bool big_endian>
2475 void
do_count_local_symbols(Stringpool_template<char> * pool,Stringpool_template<char> *)2476 Sized_relobj_incr<size, big_endian>::do_count_local_symbols(
2477     Stringpool_template<char>* pool,
2478     Stringpool_template<char>*)
2479 {
2480   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2481 
2482   // Set the count of local symbols based on the incremental info.
2483   unsigned int nlocals = this->input_reader_.get_local_symbol_count();
2484   this->local_symbol_count_ = nlocals;
2485   this->local_symbols_.reserve(nlocals);
2486 
2487   // Get views of the base file's symbol table and string table.
2488   Incremental_binary::View symtab_view(NULL);
2489   unsigned int symtab_count;
2490   elfcpp::Elf_strtab strtab(NULL, 0);
2491   this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2492 
2493   // Read the local symbols from the base file's symbol table.
2494   off_t off = this->input_reader_.get_local_symbol_offset();
2495   const unsigned char* symp = symtab_view.data() + off;
2496   for (unsigned int i = 0; i < nlocals; ++i, symp += sym_size)
2497     {
2498       elfcpp::Sym<size, big_endian> sym(symp);
2499       const char* name;
2500       if (!strtab.get_c_string(sym.get_st_name(), &name))
2501 	name = "";
2502       gold_debug(DEBUG_INCREMENTAL, "Local symbol %d: %s", i, name);
2503       name = pool->add(name, true, NULL);
2504       this->local_symbols_.push_back(Local_symbol(name,
2505 						  sym.get_st_value(),
2506 						  sym.get_st_size(),
2507 						  sym.get_st_shndx(),
2508 						  sym.get_st_type(),
2509 						  false));
2510     }
2511 }
2512 
2513 // Finalize the local symbols.
2514 
2515 template<int size, bool big_endian>
2516 unsigned int
do_finalize_local_symbols(unsigned int index,off_t off,Symbol_table *)2517 Sized_relobj_incr<size, big_endian>::do_finalize_local_symbols(
2518     unsigned int index,
2519     off_t off,
2520     Symbol_table*)
2521 {
2522   this->local_symbol_index_ = index;
2523   this->local_symbol_offset_ = off;
2524   return index + this->local_symbol_count_;
2525 }
2526 
2527 // Set the offset where local dynamic symbol information will be stored.
2528 
2529 template<int size, bool big_endian>
2530 unsigned int
do_set_local_dynsym_indexes(unsigned int index)2531 Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_indexes(
2532     unsigned int index)
2533 {
2534   // FIXME: set local dynsym indexes.
2535   return index;
2536 }
2537 
2538 // Set the offset where local dynamic symbol information will be stored.
2539 
2540 template<int size, bool big_endian>
2541 unsigned int
do_set_local_dynsym_offset(off_t)2542 Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_offset(off_t)
2543 {
2544   return 0;
2545 }
2546 
2547 // Relocate the input sections and write out the local symbols.
2548 // We don't actually do any relocation here.  For unchanged input files,
2549 // we reapply relocations only for symbols that have changed; that happens
2550 // in Layout_task_runner::run().  We do need to rewrite the incremental
2551 // relocations for this object.
2552 
2553 template<int size, bool big_endian>
2554 void
do_relocate(const Symbol_table *,const Layout * layout,Output_file * of)2555 Sized_relobj_incr<size, big_endian>::do_relocate(const Symbol_table*,
2556 						 const Layout* layout,
2557 						 Output_file* of)
2558 {
2559   if (this->incr_reloc_count_ == 0)
2560     return;
2561 
2562   const unsigned int incr_reloc_size =
2563       Incremental_relocs_reader<size, big_endian>::reloc_size;
2564 
2565   // Get a view for the .gnu_incremental_relocs section.
2566   Incremental_inputs* inputs = layout->incremental_inputs();
2567   gold_assert(inputs != NULL);
2568   const off_t relocs_off = inputs->relocs_section()->offset();
2569   const off_t relocs_size = inputs->relocs_section()->data_size();
2570   unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
2571 
2572   // Copy the relocations from the buffer.
2573   off_t off = this->incr_reloc_output_index_ * incr_reloc_size;
2574   unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2575   memcpy(view + off, this->incr_relocs_, len);
2576 
2577   // The output section table may have changed, so we need to map
2578   // the old section index to the new section index for each relocation.
2579   for (unsigned int i = 0; i < this->incr_reloc_count_; ++i)
2580     {
2581       unsigned char* pov = view + off + i * incr_reloc_size;
2582       unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(pov + 4);
2583       Output_section* os = this->ibase_->output_section(shndx);
2584       gold_assert(os != NULL);
2585       shndx = os->out_shndx();
2586       elfcpp::Swap<32, big_endian>::writeval(pov + 4, shndx);
2587     }
2588 
2589   of->write_output_view(off, len, view);
2590 
2591   // Get views into the output file for the portions of the symbol table
2592   // and the dynamic symbol table that we will be writing.
2593   off_t symtab_off = layout->symtab_section()->offset();
2594   off_t output_size = this->local_symbol_count_ * This::sym_size;
2595   unsigned char* oview = NULL;
2596   if (output_size > 0)
2597     oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
2598 				output_size);
2599 
2600   off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
2601   unsigned char* dyn_oview = NULL;
2602   if (dyn_output_size > 0)
2603     dyn_oview = of->get_output_view(this->local_dynsym_offset_,
2604 				    dyn_output_size);
2605 
2606   // Write the local symbols.
2607   unsigned char* ov = oview;
2608   unsigned char* dyn_ov = dyn_oview;
2609   const Stringpool* sympool = layout->sympool();
2610   const Stringpool* dynpool = layout->dynpool();
2611   Output_symtab_xindex* symtab_xindex = layout->symtab_xindex();
2612   Output_symtab_xindex* dynsym_xindex = layout->dynsym_xindex();
2613   for (unsigned int i = 0; i < this->local_symbol_count_; ++i)
2614     {
2615       Local_symbol& lsym(this->local_symbols_[i]);
2616 
2617       bool is_ordinary;
2618       unsigned int st_shndx = this->adjust_sym_shndx(i, lsym.st_shndx,
2619 						     &is_ordinary);
2620       if (is_ordinary)
2621 	{
2622 	  Output_section* os = this->ibase_->output_section(st_shndx);
2623 	  st_shndx = os->out_shndx();
2624 	  if (st_shndx >= elfcpp::SHN_LORESERVE)
2625 	    {
2626 	      symtab_xindex->add(this->local_symbol_index_ + i, st_shndx);
2627 	      if (lsym.needs_dynsym_entry)
2628 		dynsym_xindex->add(lsym.output_dynsym_index, st_shndx);
2629 	      st_shndx = elfcpp::SHN_XINDEX;
2630 	    }
2631 	}
2632 
2633       // Write the symbol to the output symbol table.
2634       {
2635 	elfcpp::Sym_write<size, big_endian> osym(ov);
2636 	osym.put_st_name(sympool->get_offset(lsym.name));
2637 	osym.put_st_value(lsym.st_value);
2638 	osym.put_st_size(lsym.st_size);
2639 	osym.put_st_info(elfcpp::STB_LOCAL,
2640 			 static_cast<elfcpp::STT>(lsym.st_type));
2641 	osym.put_st_other(0);
2642 	osym.put_st_shndx(st_shndx);
2643 	ov += sym_size;
2644       }
2645 
2646       // Write the symbol to the output dynamic symbol table.
2647       if (lsym.needs_dynsym_entry)
2648 	{
2649 	  gold_assert(dyn_ov < dyn_oview + dyn_output_size);
2650 	  elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
2651 	  osym.put_st_name(dynpool->get_offset(lsym.name));
2652 	  osym.put_st_value(lsym.st_value);
2653 	  osym.put_st_size(lsym.st_size);
2654 	  osym.put_st_info(elfcpp::STB_LOCAL,
2655 			   static_cast<elfcpp::STT>(lsym.st_type));
2656 	  osym.put_st_other(0);
2657 	  osym.put_st_shndx(st_shndx);
2658 	  dyn_ov += sym_size;
2659 	}
2660     }
2661 
2662   if (output_size > 0)
2663     {
2664       gold_assert(ov - oview == output_size);
2665       of->write_output_view(symtab_off + this->local_symbol_offset_,
2666 			    output_size, oview);
2667     }
2668 
2669   if (dyn_output_size > 0)
2670     {
2671       gold_assert(dyn_ov - dyn_oview == dyn_output_size);
2672       of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
2673 			    dyn_oview);
2674     }
2675 }
2676 
2677 // Set the offset of a section.
2678 
2679 template<int size, bool big_endian>
2680 void
do_set_section_offset(unsigned int,uint64_t)2681 Sized_relobj_incr<size, big_endian>::do_set_section_offset(unsigned int,
2682 							   uint64_t)
2683 {
2684 }
2685 
2686 // Class Sized_incr_dynobj.  Most of these methods are not used for
2687 // Incremental objects, but are required to be implemented by the
2688 // base class Object.
2689 
2690 template<int size, bool big_endian>
Sized_incr_dynobj(const std::string & name,Sized_incremental_binary<size,big_endian> * ibase,unsigned int input_file_index)2691 Sized_incr_dynobj<size, big_endian>::Sized_incr_dynobj(
2692     const std::string& name,
2693     Sized_incremental_binary<size, big_endian>* ibase,
2694     unsigned int input_file_index)
2695   : Dynobj(name, NULL), ibase_(ibase),
2696     input_file_index_(input_file_index),
2697     input_reader_(ibase->inputs_reader().input_file(input_file_index)),
2698     symbols_(), defined_count_(0)
2699 {
2700   if (this->input_reader_.is_in_system_directory())
2701     this->set_is_in_system_directory();
2702   if (this->input_reader_.as_needed())
2703     this->set_as_needed();
2704   this->set_soname_string(this->input_reader_.get_soname());
2705   this->set_shnum(0);
2706 }
2707 
2708 // Read the symbols.
2709 
2710 template<int size, bool big_endian>
2711 void
do_read_symbols(Read_symbols_data *)2712 Sized_incr_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
2713 {
2714   gold_unreachable();
2715 }
2716 
2717 // Lay out the input sections.
2718 
2719 template<int size, bool big_endian>
2720 void
do_layout(Symbol_table *,Layout *,Read_symbols_data *)2721 Sized_incr_dynobj<size, big_endian>::do_layout(
2722     Symbol_table*,
2723     Layout*,
2724     Read_symbols_data*)
2725 {
2726 }
2727 
2728 // Add the symbols to the symbol table.
2729 
2730 template<int size, bool big_endian>
2731 void
do_add_symbols(Symbol_table * symtab,Read_symbols_data *,Layout *)2732 Sized_incr_dynobj<size, big_endian>::do_add_symbols(
2733     Symbol_table* symtab,
2734     Read_symbols_data*,
2735     Layout*)
2736 {
2737   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2738   unsigned char symbuf[sym_size];
2739   elfcpp::Sym_write<size, big_endian> osym(symbuf);
2740 
2741   unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2742   this->symbols_.resize(nsyms);
2743 
2744   Incremental_binary::View symtab_view(NULL);
2745   unsigned int symtab_count;
2746   elfcpp::Elf_strtab strtab(NULL, 0);
2747   this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2748 
2749   Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2750   unsigned int isym_count = isymtab.symbol_count();
2751   unsigned int first_global = symtab_count - isym_count;
2752 
2753   // We keep a set of symbols that we have generated COPY relocations
2754   // for, indexed by the symbol value. We do not need more than one
2755   // COPY relocation per address.
2756   typedef typename std::set<Address> Copied_symbols;
2757   Copied_symbols copied_symbols;
2758 
2759   const unsigned char* sym_p;
2760   for (unsigned int i = 0; i < nsyms; ++i)
2761     {
2762       bool is_def;
2763       bool is_copy;
2764       unsigned int output_symndx =
2765 	  this->input_reader_.get_output_symbol_index(i, &is_def, &is_copy);
2766       sym_p = symtab_view.data() + output_symndx * sym_size;
2767       elfcpp::Sym<size, big_endian> gsym(sym_p);
2768       const char* name;
2769       if (!strtab.get_c_string(gsym.get_st_name(), &name))
2770 	name = "";
2771 
2772       Address v;
2773       unsigned int shndx;
2774       elfcpp::STB st_bind = gsym.get_st_bind();
2775       elfcpp::STT st_type = gsym.get_st_type();
2776 
2777       // Local hidden symbols start out as globals, but get converted to
2778       // to local during output.
2779       if (st_bind == elfcpp::STB_LOCAL)
2780 	st_bind = elfcpp::STB_GLOBAL;
2781 
2782       if (!is_def)
2783 	{
2784 	  shndx = elfcpp::SHN_UNDEF;
2785 	  v = 0;
2786 	}
2787       else
2788 	{
2789 	  // For a symbol defined in a shared object, the section index
2790 	  // is meaningless, as long as it's not SHN_UNDEF.
2791 	  shndx = 1;
2792 	  v = gsym.get_st_value();
2793 	  ++this->defined_count_;
2794 	}
2795 
2796       osym.put_st_name(0);
2797       osym.put_st_value(v);
2798       osym.put_st_size(gsym.get_st_size());
2799       osym.put_st_info(st_bind, st_type);
2800       osym.put_st_other(gsym.get_st_other());
2801       osym.put_st_shndx(shndx);
2802 
2803       elfcpp::Sym<size, big_endian> sym(symbuf);
2804       Sized_symbol<size>* res =
2805 	  symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
2806       this->symbols_[i] = res;
2807       this->ibase_->add_global_symbol(output_symndx - first_global,
2808 				      this->symbols_[i]);
2809 
2810       if (is_copy)
2811 	{
2812 	  std::pair<typename Copied_symbols::iterator, bool> ins =
2813 	      copied_symbols.insert(v);
2814 	  if (ins.second)
2815 	    {
2816 	      unsigned int shndx = gsym.get_st_shndx();
2817 	      Output_section* os = this->ibase_->output_section(shndx);
2818 	      off_t offset = v - os->address();
2819 	      this->ibase_->add_copy_reloc(this->symbols_[i], os, offset);
2820 	    }
2821 	}
2822     }
2823 }
2824 
2825 // Return TRUE if we should include this object from an archive library.
2826 
2827 template<int size, bool big_endian>
2828 Archive::Should_include
do_should_include_member(Symbol_table *,Layout *,Read_symbols_data *,std::string *)2829 Sized_incr_dynobj<size, big_endian>::do_should_include_member(
2830     Symbol_table*,
2831     Layout*,
2832     Read_symbols_data*,
2833     std::string*)
2834 {
2835   gold_unreachable();
2836 }
2837 
2838 // Iterate over global symbols, calling a visitor class V for each.
2839 
2840 template<int size, bool big_endian>
2841 void
do_for_all_global_symbols(Read_symbols_data *,Library_base::Symbol_visitor_base *)2842 Sized_incr_dynobj<size, big_endian>::do_for_all_global_symbols(
2843     Read_symbols_data*,
2844     Library_base::Symbol_visitor_base*)
2845 {
2846   // This routine is not used for dynamic libraries.
2847 }
2848 
2849 // Iterate over local symbols, calling a visitor class V for each GOT offset
2850 // associated with a local symbol.
2851 
2852 template<int size, bool big_endian>
2853 void
do_for_all_local_got_entries(Got_offset_list::Visitor *) const2854 Sized_incr_dynobj<size, big_endian>::do_for_all_local_got_entries(
2855     Got_offset_list::Visitor*) const
2856 {
2857 }
2858 
2859 // Get the size of a section.
2860 
2861 template<int size, bool big_endian>
2862 uint64_t
do_section_size(unsigned int)2863 Sized_incr_dynobj<size, big_endian>::do_section_size(unsigned int)
2864 {
2865   gold_unreachable();
2866 }
2867 
2868 // Get the name of a section.
2869 
2870 template<int size, bool big_endian>
2871 std::string
do_section_name(unsigned int) const2872 Sized_incr_dynobj<size, big_endian>::do_section_name(unsigned int) const
2873 {
2874   gold_unreachable();
2875 }
2876 
2877 // Return a view of the contents of a section.
2878 
2879 template<int size, bool big_endian>
2880 const unsigned char*
do_section_contents(unsigned int,section_size_type *,bool)2881 Sized_incr_dynobj<size, big_endian>::do_section_contents(
2882     unsigned int,
2883     section_size_type*,
2884     bool)
2885 {
2886   gold_unreachable();
2887 }
2888 
2889 // Return section flags.
2890 
2891 template<int size, bool big_endian>
2892 uint64_t
do_section_flags(unsigned int)2893 Sized_incr_dynobj<size, big_endian>::do_section_flags(unsigned int)
2894 {
2895   gold_unreachable();
2896 }
2897 
2898 // Return section entsize.
2899 
2900 template<int size, bool big_endian>
2901 uint64_t
do_section_entsize(unsigned int)2902 Sized_incr_dynobj<size, big_endian>::do_section_entsize(unsigned int)
2903 {
2904   gold_unreachable();
2905 }
2906 
2907 // Return section address.
2908 
2909 template<int size, bool big_endian>
2910 uint64_t
do_section_address(unsigned int)2911 Sized_incr_dynobj<size, big_endian>::do_section_address(unsigned int)
2912 {
2913   gold_unreachable();
2914 }
2915 
2916 // Return section type.
2917 
2918 template<int size, bool big_endian>
2919 unsigned int
do_section_type(unsigned int)2920 Sized_incr_dynobj<size, big_endian>::do_section_type(unsigned int)
2921 {
2922   gold_unreachable();
2923 }
2924 
2925 // Return the section link field.
2926 
2927 template<int size, bool big_endian>
2928 unsigned int
do_section_link(unsigned int)2929 Sized_incr_dynobj<size, big_endian>::do_section_link(unsigned int)
2930 {
2931   gold_unreachable();
2932 }
2933 
2934 // Return the section link field.
2935 
2936 template<int size, bool big_endian>
2937 unsigned int
do_section_info(unsigned int)2938 Sized_incr_dynobj<size, big_endian>::do_section_info(unsigned int)
2939 {
2940   gold_unreachable();
2941 }
2942 
2943 // Return the section alignment.
2944 
2945 template<int size, bool big_endian>
2946 uint64_t
do_section_addralign(unsigned int)2947 Sized_incr_dynobj<size, big_endian>::do_section_addralign(unsigned int)
2948 {
2949   gold_unreachable();
2950 }
2951 
2952 // Return the Xindex structure to use.
2953 
2954 template<int size, bool big_endian>
2955 Xindex*
do_initialize_xindex()2956 Sized_incr_dynobj<size, big_endian>::do_initialize_xindex()
2957 {
2958   gold_unreachable();
2959 }
2960 
2961 // Get symbol counts.
2962 
2963 template<int size, bool big_endian>
2964 void
do_get_global_symbol_counts(const Symbol_table *,size_t * defined,size_t * used) const2965 Sized_incr_dynobj<size, big_endian>::do_get_global_symbol_counts(
2966     const Symbol_table*,
2967     size_t* defined,
2968     size_t* used) const
2969 {
2970   *defined = this->defined_count_;
2971   size_t count = 0;
2972   for (typename Symbols::const_iterator p = this->symbols_.begin();
2973        p != this->symbols_.end();
2974        ++p)
2975     if (*p != NULL
2976 	&& (*p)->source() == Symbol::FROM_OBJECT
2977 	&& (*p)->object() == this
2978 	&& (*p)->is_defined()
2979 	&& (*p)->dynsym_index() != -1U)
2980       ++count;
2981   *used = count;
2982 }
2983 
2984 // Allocate an incremental object of the appropriate size and endianness.
2985 
2986 Object*
make_sized_incremental_object(Incremental_binary * ibase,unsigned int input_file_index,Incremental_input_type input_type,const Incremental_binary::Input_reader * input_reader)2987 make_sized_incremental_object(
2988     Incremental_binary* ibase,
2989     unsigned int input_file_index,
2990     Incremental_input_type input_type,
2991     const Incremental_binary::Input_reader* input_reader)
2992 {
2993   Object* obj = NULL;
2994   std::string name(input_reader->filename());
2995 
2996   switch (parameters->size_and_endianness())
2997     {
2998 #ifdef HAVE_TARGET_32_LITTLE
2999     case Parameters::TARGET_32_LITTLE:
3000       {
3001 	Sized_incremental_binary<32, false>* sized_ibase =
3002 	    static_cast<Sized_incremental_binary<32, false>*>(ibase);
3003 	if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
3004 	  obj = new Sized_incr_dynobj<32, false>(name, sized_ibase,
3005 						 input_file_index);
3006 	else
3007 	  obj = new Sized_relobj_incr<32, false>(name, sized_ibase,
3008 						 input_file_index);
3009       }
3010       break;
3011 #endif
3012 #ifdef HAVE_TARGET_32_BIG
3013     case Parameters::TARGET_32_BIG:
3014       {
3015 	Sized_incremental_binary<32, true>* sized_ibase =
3016 	    static_cast<Sized_incremental_binary<32, true>*>(ibase);
3017 	if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
3018 	  obj = new Sized_incr_dynobj<32, true>(name, sized_ibase,
3019 						input_file_index);
3020 	else
3021 	  obj = new Sized_relobj_incr<32, true>(name, sized_ibase,
3022 						input_file_index);
3023       }
3024       break;
3025 #endif
3026 #ifdef HAVE_TARGET_64_LITTLE
3027     case Parameters::TARGET_64_LITTLE:
3028       {
3029 	Sized_incremental_binary<64, false>* sized_ibase =
3030 	    static_cast<Sized_incremental_binary<64, false>*>(ibase);
3031 	if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
3032 	  obj = new Sized_incr_dynobj<64, false>(name, sized_ibase,
3033 						 input_file_index);
3034 	else
3035 	  obj = new Sized_relobj_incr<64, false>(name, sized_ibase,
3036 						 input_file_index);
3037      }
3038       break;
3039 #endif
3040 #ifdef HAVE_TARGET_64_BIG
3041     case Parameters::TARGET_64_BIG:
3042       {
3043 	Sized_incremental_binary<64, true>* sized_ibase =
3044 	    static_cast<Sized_incremental_binary<64, true>*>(ibase);
3045 	if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
3046 	  obj = new Sized_incr_dynobj<64, true>(name, sized_ibase,
3047 						input_file_index);
3048 	else
3049 	  obj = new Sized_relobj_incr<64, true>(name, sized_ibase,
3050 						input_file_index);
3051       }
3052       break;
3053 #endif
3054     default:
3055       gold_unreachable();
3056     }
3057 
3058   gold_assert(obj != NULL);
3059   return obj;
3060 }
3061 
3062 // Copy the unused symbols from the incremental input info.
3063 // We need to do this because we may be overwriting the incremental
3064 // input info in the base file before we write the new incremental
3065 // info.
3066 void
copy_unused_symbols()3067 Incremental_library::copy_unused_symbols()
3068 {
3069   unsigned int symcount = this->input_reader_->get_unused_symbol_count();
3070   this->unused_symbols_.reserve(symcount);
3071   for (unsigned int i = 0; i < symcount; ++i)
3072     {
3073       std::string name(this->input_reader_->get_unused_symbol(i));
3074       this->unused_symbols_.push_back(name);
3075     }
3076 }
3077 
3078 // Iterator for unused global symbols in the library.
3079 void
do_for_all_unused_symbols(Symbol_visitor_base * v) const3080 Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base* v) const
3081 {
3082   for (Symbol_list::const_iterator p = this->unused_symbols_.begin();
3083        p != this->unused_symbols_.end();
3084        ++p)
3085   v->visit(p->c_str());
3086 }
3087 
3088 // Instantiate the templates we need.
3089 
3090 #ifdef HAVE_TARGET_32_LITTLE
3091 template
3092 class Sized_incremental_binary<32, false>;
3093 
3094 template
3095 class Sized_relobj_incr<32, false>;
3096 
3097 template
3098 class Sized_incr_dynobj<32, false>;
3099 #endif
3100 
3101 #ifdef HAVE_TARGET_32_BIG
3102 template
3103 class Sized_incremental_binary<32, true>;
3104 
3105 template
3106 class Sized_relobj_incr<32, true>;
3107 
3108 template
3109 class Sized_incr_dynobj<32, true>;
3110 #endif
3111 
3112 #ifdef HAVE_TARGET_64_LITTLE
3113 template
3114 class Sized_incremental_binary<64, false>;
3115 
3116 template
3117 class Sized_relobj_incr<64, false>;
3118 
3119 template
3120 class Sized_incr_dynobj<64, false>;
3121 #endif
3122 
3123 #ifdef HAVE_TARGET_64_BIG
3124 template
3125 class Sized_incremental_binary<64, true>;
3126 
3127 template
3128 class Sized_relobj_incr<64, true>;
3129 
3130 template
3131 class Sized_incr_dynobj<64, true>;
3132 #endif
3133 
3134 } // End namespace gold.
3135