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