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