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