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