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