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