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