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