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