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