* common.cc (Symbol_table::do_allocate_commons_list): For incremental
[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("--debug=", argv[i]))
929 continue;
930 if (strcmp(argv[i], "--incremental-base") == 0
931 || strcmp(argv[i], "--debug") == 0)
932 {
933 // When these options are used without the '=', skip the
934 // following parameter as well.
935 ++i;
936 continue;
937 }
938
939 args.append(" '");
940 // Now append argv[i], but with all single-quotes escaped
941 const char* argpos = argv[i];
942 while (1)
943 {
944 const int len = strcspn(argpos, "'");
945 args.append(argpos, len);
946 if (argpos[len] == '\0')
947 break;
948 args.append("'\"'\"'");
949 argpos += len + 1;
950 }
951 args.append("'");
952 }
953
954 this->command_line_ = args;
955 this->strtab_->add(this->command_line_.c_str(), false,
956 &this->command_line_key_);
957 }
958
959 // Record the input archive file ARCHIVE. This is called by the
960 // Add_archive_symbols task before determining which archive members
961 // to include. We create the Incremental_archive_entry here and
962 // attach it to the Archive, but we do not add it to the list of
963 // input objects until report_archive_end is called.
964
965 void
966 Incremental_inputs::report_archive_begin(Library_base* arch,
967 unsigned int arg_serial,
968 Script_info* script_info)
969 {
970 Stringpool::Key filename_key;
971 Timespec mtime = arch->get_mtime();
972
973 // For a file loaded from a script, don't record its argument serial number.
974 if (script_info != NULL)
975 arg_serial = 0;
976
977 this->strtab_->add(arch->filename().c_str(), false, &filename_key);
978 Incremental_archive_entry* entry =
979 new Incremental_archive_entry(filename_key, arg_serial, mtime);
980 arch->set_incremental_info(entry);
981
982 if (script_info != NULL)
983 {
984 Incremental_script_entry* script_entry = script_info->incremental_info();
985 gold_assert(script_entry != NULL);
986 script_entry->add_object(entry);
987 }
988 }
989
990 // Visitor class for processing the unused global symbols in a library.
991 // An instance of this class is passed to the library's
992 // for_all_unused_symbols() iterator, which will call the visit()
993 // function for each global symbol defined in each unused library
994 // member. We add those symbol names to the incremental info for the
995 // library.
996
997 class Unused_symbol_visitor : public Library_base::Symbol_visitor_base
998 {
999 public:
1000 Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab)
1001 : entry_(entry), strtab_(strtab)
1002 { }
1003
1004 void
1005 visit(const char* sym)
1006 {
1007 Stringpool::Key symbol_key;
1008 this->strtab_->add(sym, true, &symbol_key);
1009 this->entry_->add_unused_global_symbol(symbol_key);
1010 }
1011
1012 private:
1013 Incremental_archive_entry* entry_;
1014 Stringpool* strtab_;
1015 };
1016
1017 // Finish recording the input archive file ARCHIVE. This is called by the
1018 // Add_archive_symbols task after determining which archive members
1019 // to include.
1020
1021 void
1022 Incremental_inputs::report_archive_end(Library_base* arch)
1023 {
1024 Incremental_archive_entry* entry = arch->incremental_info();
1025
1026 gold_assert(entry != NULL);
1027 this->inputs_.push_back(entry);
1028
1029 // Collect unused global symbols.
1030 Unused_symbol_visitor v(entry, this->strtab_);
1031 arch->for_all_unused_symbols(&v);
1032 }
1033
1034 // Record the input object file OBJ. If ARCH is not NULL, attach
1035 // the object file to the archive. This is called by the
1036 // Add_symbols task after finding out the type of the file.
1037
1038 void
1039 Incremental_inputs::report_object(Object* obj, unsigned int arg_serial,
1040 Library_base* arch, Script_info* script_info)
1041 {
1042 Stringpool::Key filename_key;
1043 Timespec mtime = obj->get_mtime();
1044
1045 // For a file loaded from a script, don't record its argument serial number.
1046 if (script_info != NULL)
1047 arg_serial = 0;
1048
1049 this->strtab_->add(obj->name().c_str(), false, &filename_key);
1050
1051 Incremental_input_entry* input_entry;
1052
1053 this->current_object_ = obj;
1054
1055 if (!obj->is_dynamic())
1056 {
1057 this->current_object_entry_ =
1058 new Incremental_object_entry(filename_key, obj, arg_serial, mtime);
1059 input_entry = this->current_object_entry_;
1060 if (arch != NULL)
1061 {
1062 Incremental_archive_entry* arch_entry = arch->incremental_info();
1063 gold_assert(arch_entry != NULL);
1064 arch_entry->add_object(this->current_object_entry_);
1065 }
1066 }
1067 else
1068 {
1069 this->current_object_entry_ = NULL;
1070 Stringpool::Key soname_key;
1071 Dynobj* dynobj = obj->dynobj();
1072 gold_assert(dynobj != NULL);
1073 this->strtab_->add(dynobj->soname(), false, &soname_key);
1074 input_entry = new Incremental_dynobj_entry(filename_key, soname_key, obj,
1075 arg_serial, mtime);
1076 }
1077
1078 if (obj->is_in_system_directory())
1079 input_entry->set_is_in_system_directory();
1080
1081 if (obj->as_needed())
1082 input_entry->set_as_needed();
1083
1084 this->inputs_.push_back(input_entry);
1085
1086 if (script_info != NULL)
1087 {
1088 Incremental_script_entry* script_entry = script_info->incremental_info();
1089 gold_assert(script_entry != NULL);
1090 script_entry->add_object(input_entry);
1091 }
1092 }
1093
1094 // Record an input section SHNDX from object file OBJ.
1095
1096 void
1097 Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
1098 const char* name, off_t sh_size)
1099 {
1100 Stringpool::Key key = 0;
1101
1102 if (name != NULL)
1103 this->strtab_->add(name, true, &key);
1104
1105 gold_assert(obj == this->current_object_);
1106 gold_assert(this->current_object_entry_ != NULL);
1107 this->current_object_entry_->add_input_section(shndx, key, sh_size);
1108 }
1109
1110 // Record a kept COMDAT group belonging to object file OBJ.
1111
1112 void
1113 Incremental_inputs::report_comdat_group(Object* obj, const char* name)
1114 {
1115 Stringpool::Key key = 0;
1116
1117 if (name != NULL)
1118 this->strtab_->add(name, true, &key);
1119 gold_assert(obj == this->current_object_);
1120 gold_assert(this->current_object_entry_ != NULL);
1121 this->current_object_entry_->add_comdat_group(key);
1122 }
1123
1124 // Record that the input argument INPUT is a script SCRIPT. This is
1125 // called by read_script after parsing the script and reading the list
1126 // of inputs added by this script.
1127
1128 void
1129 Incremental_inputs::report_script(Script_info* script,
1130 unsigned int arg_serial,
1131 Timespec mtime)
1132 {
1133 Stringpool::Key filename_key;
1134
1135 this->strtab_->add(script->filename().c_str(), false, &filename_key);
1136 Incremental_script_entry* entry =
1137 new Incremental_script_entry(filename_key, arg_serial, script, mtime);
1138 this->inputs_.push_back(entry);
1139 script->set_incremental_info(entry);
1140 }
1141
1142 // Finalize the incremental link information. Called from
1143 // Layout::finalize.
1144
1145 void
1146 Incremental_inputs::finalize()
1147 {
1148 // Finalize the string table.
1149 this->strtab_->set_string_offsets();
1150 }
1151
1152 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
1153
1154 void
1155 Incremental_inputs::create_data_sections(Symbol_table* symtab)
1156 {
1157 switch (parameters->size_and_endianness())
1158 {
1159 #ifdef HAVE_TARGET_32_LITTLE
1160 case Parameters::TARGET_32_LITTLE:
1161 this->inputs_section_ =
1162 new Output_section_incremental_inputs<32, false>(this, symtab);
1163 break;
1164 #endif
1165 #ifdef HAVE_TARGET_32_BIG
1166 case Parameters::TARGET_32_BIG:
1167 this->inputs_section_ =
1168 new Output_section_incremental_inputs<32, true>(this, symtab);
1169 break;
1170 #endif
1171 #ifdef HAVE_TARGET_64_LITTLE
1172 case Parameters::TARGET_64_LITTLE:
1173 this->inputs_section_ =
1174 new Output_section_incremental_inputs<64, false>(this, symtab);
1175 break;
1176 #endif
1177 #ifdef HAVE_TARGET_64_BIG
1178 case Parameters::TARGET_64_BIG:
1179 this->inputs_section_ =
1180 new Output_section_incremental_inputs<64, true>(this, symtab);
1181 break;
1182 #endif
1183 default:
1184 gold_unreachable();
1185 }
1186 this->symtab_section_ = new Output_data_space(4, "** incremental_symtab");
1187 this->relocs_section_ = new Output_data_space(4, "** incremental_relocs");
1188 this->got_plt_section_ = new Output_data_space(4, "** incremental_got_plt");
1189 }
1190
1191 // Return the sh_entsize value for the .gnu_incremental_relocs section.
1192 unsigned int
1193 Incremental_inputs::relocs_entsize() const
1194 {
1195 return 8 + 2 * parameters->target().get_size() / 8;
1196 }
1197
1198 // Class Output_section_incremental_inputs.
1199
1200 // Finalize the offsets for each input section and supplemental info block,
1201 // and set the final data size of the incremental output sections.
1202
1203 template<int size, bool big_endian>
1204 void
1205 Output_section_incremental_inputs<size, big_endian>::set_final_data_size()
1206 {
1207 const Incremental_inputs* inputs = this->inputs_;
1208 const unsigned int sizeof_addr = size / 8;
1209 const unsigned int rel_size = 8 + 2 * sizeof_addr;
1210
1211 // Offset of each input entry.
1212 unsigned int input_offset = this->header_size;
1213
1214 // Offset of each supplemental info block.
1215 unsigned int file_index = 0;
1216 unsigned int info_offset = this->header_size;
1217 info_offset += this->input_entry_size * inputs->input_file_count();
1218
1219 // Count each input file and its supplemental information block.
1220 for (Incremental_inputs::Input_list::const_iterator p =
1221 inputs->input_files().begin();
1222 p != inputs->input_files().end();
1223 ++p)
1224 {
1225 // Set the index and offset of the input file entry.
1226 (*p)->set_offset(file_index, input_offset);
1227 ++file_index;
1228 input_offset += this->input_entry_size;
1229
1230 // Set the offset of the supplemental info block.
1231 switch ((*p)->type())
1232 {
1233 case INCREMENTAL_INPUT_SCRIPT:
1234 {
1235 Incremental_script_entry *entry = (*p)->script_entry();
1236 gold_assert(entry != NULL);
1237 (*p)->set_info_offset(info_offset);
1238 // Object count.
1239 info_offset += 4;
1240 // Each member.
1241 info_offset += (entry->get_object_count() * 4);
1242 }
1243 break;
1244 case INCREMENTAL_INPUT_OBJECT:
1245 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
1246 {
1247 Incremental_object_entry* entry = (*p)->object_entry();
1248 gold_assert(entry != NULL);
1249 (*p)->set_info_offset(info_offset);
1250 // Input section count, global symbol count, local symbol offset,
1251 // local symbol count, first dynamic reloc, dynamic reloc count,
1252 // comdat group count.
1253 info_offset += 28;
1254 // Each input section.
1255 info_offset += (entry->get_input_section_count()
1256 * (8 + 2 * sizeof_addr));
1257 // Each global symbol.
1258 const Object::Symbols* syms = entry->object()->get_global_symbols();
1259 info_offset += syms->size() * 20;
1260 // Each comdat group.
1261 info_offset += entry->get_comdat_group_count() * 4;
1262 }
1263 break;
1264 case INCREMENTAL_INPUT_SHARED_LIBRARY:
1265 {
1266 Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
1267 gold_assert(entry != NULL);
1268 (*p)->set_info_offset(info_offset);
1269 // Global symbol count, soname index.
1270 info_offset += 8;
1271 // Each global symbol.
1272 const Object::Symbols* syms = entry->object()->get_global_symbols();
1273 gold_assert(syms != NULL);
1274 unsigned int nsyms = syms->size();
1275 unsigned int nsyms_out = 0;
1276 for (unsigned int i = 0; i < nsyms; ++i)
1277 {
1278 const Symbol* sym = (*syms)[i];
1279 if (sym == NULL)
1280 continue;
1281 if (sym->is_forwarder())
1282 sym = this->symtab_->resolve_forwards(sym);
1283 if (sym->symtab_index() != -1U)
1284 ++nsyms_out;
1285 }
1286 info_offset += nsyms_out * 4;
1287 }
1288 break;
1289 case INCREMENTAL_INPUT_ARCHIVE:
1290 {
1291 Incremental_archive_entry* entry = (*p)->archive_entry();
1292 gold_assert(entry != NULL);
1293 (*p)->set_info_offset(info_offset);
1294 // Member count + unused global symbol count.
1295 info_offset += 8;
1296 // Each member.
1297 info_offset += (entry->get_member_count() * 4);
1298 // Each global symbol.
1299 info_offset += (entry->get_unused_global_symbol_count() * 4);
1300 }
1301 break;
1302 default:
1303 gold_unreachable();
1304 }
1305 }
1306
1307 this->set_data_size(info_offset);
1308
1309 // Set the size of the .gnu_incremental_symtab section.
1310 inputs->symtab_section()->set_current_data_size(this->symtab_->output_count()
1311 * sizeof(unsigned int));
1312
1313 // Set the size of the .gnu_incremental_relocs section.
1314 inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count()
1315 * rel_size);
1316
1317 // Set the size of the .gnu_incremental_got_plt section.
1318 Sized_target<size, big_endian>* target =
1319 parameters->sized_target<size, big_endian>();
1320 unsigned int got_count = target->got_entry_count();
1321 unsigned int plt_count = target->plt_entry_count();
1322 unsigned int got_plt_size = 8; // GOT entry count, PLT entry count.
1323 got_plt_size = (got_plt_size + got_count + 3) & ~3; // GOT type array.
1324 got_plt_size += got_count * 8 + plt_count * 4; // GOT array, PLT array.
1325 inputs->got_plt_section()->set_current_data_size(got_plt_size);
1326 }
1327
1328 // Write the contents of the .gnu_incremental_inputs and
1329 // .gnu_incremental_symtab sections.
1330
1331 template<int size, bool big_endian>
1332 void
1333 Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of)
1334 {
1335 const Incremental_inputs* inputs = this->inputs_;
1336 Stringpool* strtab = inputs->get_stringpool();
1337
1338 // Get a view into the .gnu_incremental_inputs section.
1339 const off_t off = this->offset();
1340 const off_t oview_size = this->data_size();
1341 unsigned char* const oview = of->get_output_view(off, oview_size);
1342 unsigned char* pov = oview;
1343
1344 // Get a view into the .gnu_incremental_symtab section.
1345 const off_t symtab_off = inputs->symtab_section()->offset();
1346 const off_t symtab_size = inputs->symtab_section()->data_size();
1347 unsigned char* const symtab_view = of->get_output_view(symtab_off,
1348 symtab_size);
1349
1350 // Allocate an array of linked list heads for the .gnu_incremental_symtab
1351 // section. Each element corresponds to a global symbol in the output
1352 // symbol table, and points to the head of the linked list that threads
1353 // through the object file input entries. The value of each element
1354 // is the section-relative offset to a global symbol entry in a
1355 // supplemental information block.
1356 unsigned int global_sym_count = this->symtab_->output_count();
1357 unsigned int* global_syms = new unsigned int[global_sym_count];
1358 memset(global_syms, 0, global_sym_count * sizeof(unsigned int));
1359
1360 // Write the section header.
1361 Stringpool::Key command_line_key = inputs->command_line_key();
1362 pov = this->write_header(pov, inputs->input_file_count(),
1363 strtab->get_offset_from_key(command_line_key));
1364
1365 // Write the list of input files.
1366 pov = this->write_input_files(oview, pov, strtab);
1367
1368 // Write the supplemental information blocks for each input file.
1369 pov = this->write_info_blocks(oview, pov, strtab, global_syms,
1370 global_sym_count);
1371
1372 gold_assert(pov - oview == oview_size);
1373
1374 // Write the .gnu_incremental_symtab section.
1375 gold_assert(global_sym_count * 4 == symtab_size);
1376 this->write_symtab(symtab_view, global_syms, global_sym_count);
1377
1378 delete[] global_syms;
1379
1380 // Write the .gnu_incremental_got_plt section.
1381 const off_t got_plt_off = inputs->got_plt_section()->offset();
1382 const off_t got_plt_size = inputs->got_plt_section()->data_size();
1383 unsigned char* const got_plt_view = of->get_output_view(got_plt_off,
1384 got_plt_size);
1385 this->write_got_plt(got_plt_view, got_plt_size);
1386
1387 of->write_output_view(off, oview_size, oview);
1388 of->write_output_view(symtab_off, symtab_size, symtab_view);
1389 of->write_output_view(got_plt_off, got_plt_size, got_plt_view);
1390 }
1391
1392 // Write the section header: version, input file count, offset of command line
1393 // in the string table, and 4 bytes of padding.
1394
1395 template<int size, bool big_endian>
1396 unsigned char*
1397 Output_section_incremental_inputs<size, big_endian>::write_header(
1398 unsigned char* pov,
1399 unsigned int input_file_count,
1400 section_offset_type command_line_offset)
1401 {
1402 Swap32::writeval(pov, INCREMENTAL_LINK_VERSION);
1403 Swap32::writeval(pov + 4, input_file_count);
1404 Swap32::writeval(pov + 8, command_line_offset);
1405 Swap32::writeval(pov + 12, 0);
1406 return pov + this->header_size;
1407 }
1408
1409 // Write the input file entries.
1410
1411 template<int size, bool big_endian>
1412 unsigned char*
1413 Output_section_incremental_inputs<size, big_endian>::write_input_files(
1414 unsigned char* oview,
1415 unsigned char* pov,
1416 Stringpool* strtab)
1417 {
1418 const Incremental_inputs* inputs = this->inputs_;
1419
1420 for (Incremental_inputs::Input_list::const_iterator p =
1421 inputs->input_files().begin();
1422 p != inputs->input_files().end();
1423 ++p)
1424 {
1425 gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset());
1426 section_offset_type filename_offset =
1427 strtab->get_offset_from_key((*p)->get_filename_key());
1428 const Timespec& mtime = (*p)->get_mtime();
1429 unsigned int flags = (*p)->type();
1430 if ((*p)->is_in_system_directory())
1431 flags |= INCREMENTAL_INPUT_IN_SYSTEM_DIR;
1432 if ((*p)->as_needed())
1433 flags |= INCREMENTAL_INPUT_AS_NEEDED;
1434 Swap32::writeval(pov, filename_offset);
1435 Swap32::writeval(pov + 4, (*p)->get_info_offset());
1436 Swap64::writeval(pov + 8, mtime.seconds);
1437 Swap32::writeval(pov + 16, mtime.nanoseconds);
1438 Swap16::writeval(pov + 20, flags);
1439 Swap16::writeval(pov + 22, (*p)->arg_serial());
1440 pov += this->input_entry_size;
1441 }
1442 return pov;
1443 }
1444
1445 // Write the supplemental information blocks.
1446
1447 template<int size, bool big_endian>
1448 unsigned char*
1449 Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
1450 unsigned char* oview,
1451 unsigned char* pov,
1452 Stringpool* strtab,
1453 unsigned int* global_syms,
1454 unsigned int global_sym_count)
1455 {
1456 const Incremental_inputs* inputs = this->inputs_;
1457 unsigned int first_global_index = this->symtab_->first_global_index();
1458
1459 for (Incremental_inputs::Input_list::const_iterator p =
1460 inputs->input_files().begin();
1461 p != inputs->input_files().end();
1462 ++p)
1463 {
1464 switch ((*p)->type())
1465 {
1466 case INCREMENTAL_INPUT_SCRIPT:
1467 {
1468 gold_assert(static_cast<unsigned int>(pov - oview)
1469 == (*p)->get_info_offset());
1470 Incremental_script_entry* entry = (*p)->script_entry();
1471 gold_assert(entry != NULL);
1472
1473 // Write the object count.
1474 unsigned int nobjects = entry->get_object_count();
1475 Swap32::writeval(pov, nobjects);
1476 pov += 4;
1477
1478 // For each object, write the offset to its input file entry.
1479 for (unsigned int i = 0; i < nobjects; ++i)
1480 {
1481 Incremental_input_entry* obj = entry->get_object(i);
1482 Swap32::writeval(pov, obj->get_offset());
1483 pov += 4;
1484 }
1485 }
1486 break;
1487
1488 case INCREMENTAL_INPUT_OBJECT:
1489 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
1490 {
1491 gold_assert(static_cast<unsigned int>(pov - oview)
1492 == (*p)->get_info_offset());
1493 Incremental_object_entry* entry = (*p)->object_entry();
1494 gold_assert(entry != NULL);
1495 const Object* obj = entry->object();
1496 const Relobj* relobj = static_cast<const Relobj*>(obj);
1497 const Object::Symbols* syms = obj->get_global_symbols();
1498 // Write the input section count and global symbol count.
1499 unsigned int nsections = entry->get_input_section_count();
1500 unsigned int nsyms = syms->size();
1501 off_t locals_offset = relobj->local_symbol_offset();
1502 unsigned int nlocals = relobj->output_local_symbol_count();
1503 unsigned int first_dynrel = relobj->first_dyn_reloc();
1504 unsigned int ndynrel = relobj->dyn_reloc_count();
1505 unsigned int ncomdat = entry->get_comdat_group_count();
1506 Swap32::writeval(pov, nsections);
1507 Swap32::writeval(pov + 4, nsyms);
1508 Swap32::writeval(pov + 8, static_cast<unsigned int>(locals_offset));
1509 Swap32::writeval(pov + 12, nlocals);
1510 Swap32::writeval(pov + 16, first_dynrel);
1511 Swap32::writeval(pov + 20, ndynrel);
1512 Swap32::writeval(pov + 24, ncomdat);
1513 pov += 28;
1514
1515 // Build a temporary array to map input section indexes
1516 // from the original object file index to the index in the
1517 // incremental info table.
1518 unsigned int* index_map = new unsigned int[obj->shnum()];
1519 memset(index_map, 0, obj->shnum() * sizeof(unsigned int));
1520
1521 // For each input section, write the name, output section index,
1522 // offset within output section, and input section size.
1523 for (unsigned int i = 0; i < nsections; i++)
1524 {
1525 unsigned int shndx = entry->get_input_section_index(i);
1526 index_map[shndx] = i + 1;
1527 Stringpool::Key key = entry->get_input_section_name_key(i);
1528 off_t name_offset = 0;
1529 if (key != 0)
1530 name_offset = strtab->get_offset_from_key(key);
1531 int out_shndx = 0;
1532 off_t out_offset = 0;
1533 off_t sh_size = 0;
1534 Output_section* os = obj->output_section(shndx);
1535 if (os != NULL)
1536 {
1537 out_shndx = os->out_shndx();
1538 out_offset = obj->output_section_offset(shndx);
1539 sh_size = entry->get_input_section_size(i);
1540 }
1541 Swap32::writeval(pov, name_offset);
1542 Swap32::writeval(pov + 4, out_shndx);
1543 Swap::writeval(pov + 8, out_offset);
1544 Swap::writeval(pov + 8 + sizeof_addr, sh_size);
1545 pov += 8 + 2 * sizeof_addr;
1546 }
1547
1548 // For each global symbol, write its associated relocations,
1549 // add it to the linked list of globals, then write the
1550 // supplemental information: global symbol table index,
1551 // input section index, linked list chain pointer, relocation
1552 // count, and offset to the relocations.
1553 for (unsigned int i = 0; i < nsyms; i++)
1554 {
1555 const Symbol* sym = (*syms)[i];
1556 if (sym->is_forwarder())
1557 sym = this->symtab_->resolve_forwards(sym);
1558 unsigned int shndx = 0;
1559 if (sym->source() != Symbol::FROM_OBJECT)
1560 {
1561 // The symbol was defined by the linker (e.g., common).
1562 // We mark these symbols with a special SHNDX of -1,
1563 // but exclude linker-predefined symbols and symbols
1564 // copied from shared objects.
1565 if (!sym->is_predefined()
1566 && !sym->is_copied_from_dynobj())
1567 shndx = -1U;
1568 }
1569 else if (sym->object() == obj && sym->is_defined())
1570 {
1571 bool is_ordinary;
1572 unsigned int orig_shndx = sym->shndx(&is_ordinary);
1573 if (is_ordinary)
1574 shndx = index_map[orig_shndx];
1575 else
1576 shndx = 1;
1577 }
1578 unsigned int symtab_index = sym->symtab_index();
1579 unsigned int chain = 0;
1580 unsigned int first_reloc = 0;
1581 unsigned int nrelocs = obj->get_incremental_reloc_count(i);
1582 if (nrelocs > 0)
1583 {
1584 gold_assert(symtab_index != -1U
1585 && (symtab_index - first_global_index
1586 < global_sym_count));
1587 first_reloc = obj->get_incremental_reloc_base(i);
1588 chain = global_syms[symtab_index - first_global_index];
1589 global_syms[symtab_index - first_global_index] =
1590 pov - oview;
1591 }
1592 Swap32::writeval(pov, symtab_index);
1593 Swap32::writeval(pov + 4, shndx);
1594 Swap32::writeval(pov + 8, chain);
1595 Swap32::writeval(pov + 12, nrelocs);
1596 Swap32::writeval(pov + 16, first_reloc * 3 * sizeof_addr);
1597 pov += 20;
1598 }
1599
1600 // For each kept COMDAT group, write the group signature.
1601 for (unsigned int i = 0; i < ncomdat; i++)
1602 {
1603 Stringpool::Key key = entry->get_comdat_signature_key(i);
1604 off_t name_offset = 0;
1605 if (key != 0)
1606 name_offset = strtab->get_offset_from_key(key);
1607 Swap32::writeval(pov, name_offset);
1608 pov += 4;
1609 }
1610
1611 delete[] index_map;
1612 }
1613 break;
1614
1615 case INCREMENTAL_INPUT_SHARED_LIBRARY:
1616 {
1617 gold_assert(static_cast<unsigned int>(pov - oview)
1618 == (*p)->get_info_offset());
1619 Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
1620 gold_assert(entry != NULL);
1621 Object* obj = entry->object();
1622 Dynobj* dynobj = obj->dynobj();
1623 gold_assert(dynobj != NULL);
1624 const Object::Symbols* syms = obj->get_global_symbols();
1625
1626 // Write the soname string table index.
1627 section_offset_type soname_offset =
1628 strtab->get_offset_from_key(entry->get_soname_key());
1629 Swap32::writeval(pov, soname_offset);
1630 pov += 4;
1631
1632 // Skip the global symbol count for now.
1633 unsigned char* orig_pov = pov;
1634 pov += 4;
1635
1636 // For each global symbol, write the global symbol table index.
1637 unsigned int nsyms = syms->size();
1638 unsigned int nsyms_out = 0;
1639 for (unsigned int i = 0; i < nsyms; i++)
1640 {
1641 const Symbol* sym = (*syms)[i];
1642 if (sym == NULL)
1643 continue;
1644 if (sym->is_forwarder())
1645 sym = this->symtab_->resolve_forwards(sym);
1646 if (sym->symtab_index() == -1U)
1647 continue;
1648 unsigned int flags = 0;
1649 if (sym->source() == Symbol::FROM_OBJECT
1650 && sym->object() == obj
1651 && sym->is_defined())
1652 flags = INCREMENTAL_SHLIB_SYM_DEF;
1653 else if (sym->is_copied_from_dynobj()
1654 && this->symtab_->get_copy_source(sym) == dynobj)
1655 flags = INCREMENTAL_SHLIB_SYM_COPY;
1656 flags <<= INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
1657 Swap32::writeval(pov, sym->symtab_index() | flags);
1658 pov += 4;
1659 ++nsyms_out;
1660 }
1661
1662 // Now write the global symbol count.
1663 Swap32::writeval(orig_pov, nsyms_out);
1664 }
1665 break;
1666
1667 case INCREMENTAL_INPUT_ARCHIVE:
1668 {
1669 gold_assert(static_cast<unsigned int>(pov - oview)
1670 == (*p)->get_info_offset());
1671 Incremental_archive_entry* entry = (*p)->archive_entry();
1672 gold_assert(entry != NULL);
1673
1674 // Write the member count and unused global symbol count.
1675 unsigned int nmembers = entry->get_member_count();
1676 unsigned int nsyms = entry->get_unused_global_symbol_count();
1677 Swap32::writeval(pov, nmembers);
1678 Swap32::writeval(pov + 4, nsyms);
1679 pov += 8;
1680
1681 // For each member, write the offset to its input file entry.
1682 for (unsigned int i = 0; i < nmembers; ++i)
1683 {
1684 Incremental_object_entry* member = entry->get_member(i);
1685 Swap32::writeval(pov, member->get_offset());
1686 pov += 4;
1687 }
1688
1689 // For each global symbol, write the name offset.
1690 for (unsigned int i = 0; i < nsyms; ++i)
1691 {
1692 Stringpool::Key key = entry->get_unused_global_symbol(i);
1693 Swap32::writeval(pov, strtab->get_offset_from_key(key));
1694 pov += 4;
1695 }
1696 }
1697 break;
1698
1699 default:
1700 gold_unreachable();
1701 }
1702 }
1703 return pov;
1704 }
1705
1706 // Write the contents of the .gnu_incremental_symtab section.
1707
1708 template<int size, bool big_endian>
1709 void
1710 Output_section_incremental_inputs<size, big_endian>::write_symtab(
1711 unsigned char* pov,
1712 unsigned int* global_syms,
1713 unsigned int global_sym_count)
1714 {
1715 for (unsigned int i = 0; i < global_sym_count; ++i)
1716 {
1717 Swap32::writeval(pov, global_syms[i]);
1718 pov += 4;
1719 }
1720 }
1721
1722 // This struct holds the view information needed to write the
1723 // .gnu_incremental_got_plt section.
1724
1725 struct Got_plt_view_info
1726 {
1727 // Start of the GOT type array in the output view.
1728 unsigned char* got_type_p;
1729 // Start of the GOT descriptor array in the output view.
1730 unsigned char* got_desc_p;
1731 // Start of the PLT descriptor array in the output view.
1732 unsigned char* plt_desc_p;
1733 // Number of GOT entries.
1734 unsigned int got_count;
1735 // Number of PLT entries.
1736 unsigned int plt_count;
1737 // Offset of the first non-reserved PLT entry (this is a target-dependent value).
1738 unsigned int first_plt_entry_offset;
1739 // Size of a PLT entry (this is a target-dependent value).
1740 unsigned int plt_entry_size;
1741 // Symbol index to write in the GOT descriptor array. For global symbols,
1742 // this is the global symbol table index; for local symbols, it is the
1743 // local symbol table index.
1744 unsigned int sym_index;
1745 // Input file index to write in the GOT descriptor array. For global
1746 // symbols, this is 0; for local symbols, it is the index of the input
1747 // file entry in the .gnu_incremental_inputs section.
1748 unsigned int input_index;
1749 };
1750
1751 // Functor class for processing a GOT offset list for local symbols.
1752 // Writes the GOT type and symbol index into the GOT type and descriptor
1753 // arrays in the output section.
1754
1755 template<int size, bool big_endian>
1756 class Local_got_offset_visitor : public Got_offset_list::Visitor
1757 {
1758 public:
1759 Local_got_offset_visitor(struct Got_plt_view_info& info)
1760 : info_(info)
1761 { }
1762
1763 void
1764 visit(unsigned int got_type, unsigned int got_offset)
1765 {
1766 unsigned int got_index = got_offset / this->got_entry_size_;
1767 gold_assert(got_index < this->info_.got_count);
1768 // We can only handle GOT entry types in the range 0..0x7e
1769 // because we use a byte array to store them, and we use the
1770 // high bit to flag a local symbol.
1771 gold_assert(got_type < 0x7f);
1772 this->info_.got_type_p[got_index] = got_type | 0x80;
1773 unsigned char* pov = this->info_.got_desc_p + got_index * 8;
1774 elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
1775 elfcpp::Swap<32, big_endian>::writeval(pov + 4, this->info_.input_index);
1776 }
1777
1778 private:
1779 static const unsigned int got_entry_size_ = size / 8;
1780 struct Got_plt_view_info& info_;
1781 };
1782
1783 // Functor class for processing a GOT offset list. Writes the GOT type
1784 // and symbol index into the GOT type and descriptor arrays in the output
1785 // section.
1786
1787 template<int size, bool big_endian>
1788 class Global_got_offset_visitor : public Got_offset_list::Visitor
1789 {
1790 public:
1791 Global_got_offset_visitor(struct Got_plt_view_info& info)
1792 : info_(info)
1793 { }
1794
1795 void
1796 visit(unsigned int got_type, unsigned int got_offset)
1797 {
1798 unsigned int got_index = got_offset / this->got_entry_size_;
1799 gold_assert(got_index < this->info_.got_count);
1800 // We can only handle GOT entry types in the range 0..0x7e
1801 // because we use a byte array to store them, and we use the
1802 // high bit to flag a local symbol.
1803 gold_assert(got_type < 0x7f);
1804 this->info_.got_type_p[got_index] = got_type;
1805 unsigned char* pov = this->info_.got_desc_p + got_index * 8;
1806 elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
1807 elfcpp::Swap<32, big_endian>::writeval(pov + 4, 0);
1808 }
1809
1810 private:
1811 static const unsigned int got_entry_size_ = size / 8;
1812 struct Got_plt_view_info& info_;
1813 };
1814
1815 // Functor class for processing the global symbol table. Processes the
1816 // GOT offset list for the symbol, and writes the symbol table index
1817 // into the PLT descriptor array in the output section.
1818
1819 template<int size, bool big_endian>
1820 class Global_symbol_visitor_got_plt
1821 {
1822 public:
1823 Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
1824 : info_(info)
1825 { }
1826
1827 void
1828 operator()(const Sized_symbol<size>* sym)
1829 {
1830 typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
1831 const Got_offset_list* got_offsets = sym->got_offset_list();
1832 if (got_offsets != NULL)
1833 {
1834 this->info_.sym_index = sym->symtab_index();
1835 this->info_.input_index = 0;
1836 Got_visitor v(this->info_);
1837 got_offsets->for_all_got_offsets(&v);
1838 }
1839 if (sym->has_plt_offset())
1840 {
1841 unsigned int plt_index =
1842 ((sym->plt_offset() - this->info_.first_plt_entry_offset)
1843 / this->info_.plt_entry_size);
1844 gold_assert(plt_index < this->info_.plt_count);
1845 unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
1846 elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
1847 }
1848 }
1849
1850 private:
1851 struct Got_plt_view_info& info_;
1852 };
1853
1854 // Write the contents of the .gnu_incremental_got_plt section.
1855
1856 template<int size, bool big_endian>
1857 void
1858 Output_section_incremental_inputs<size, big_endian>::write_got_plt(
1859 unsigned char* pov,
1860 off_t view_size)
1861 {
1862 Sized_target<size, big_endian>* target =
1863 parameters->sized_target<size, big_endian>();
1864
1865 // Set up the view information for the functors.
1866 struct Got_plt_view_info view_info;
1867 view_info.got_count = target->got_entry_count();
1868 view_info.plt_count = target->plt_entry_count();
1869 view_info.first_plt_entry_offset = target->first_plt_entry_offset();
1870 view_info.plt_entry_size = target->plt_entry_size();
1871 view_info.got_type_p = pov + 8;
1872 view_info.got_desc_p = (view_info.got_type_p
1873 + ((view_info.got_count + 3) & ~3));
1874 view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 8;
1875
1876 gold_assert(pov + view_size ==
1877 view_info.plt_desc_p + view_info.plt_count * 4);
1878
1879 // Write the section header.
1880 Swap32::writeval(pov, view_info.got_count);
1881 Swap32::writeval(pov + 4, view_info.plt_count);
1882
1883 // Initialize the GOT type array to 0xff (reserved).
1884 memset(view_info.got_type_p, 0xff, view_info.got_count);
1885
1886 // Write the incremental GOT descriptors for local symbols.
1887 typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
1888 for (Incremental_inputs::Input_list::const_iterator p =
1889 this->inputs_->input_files().begin();
1890 p != this->inputs_->input_files().end();
1891 ++p)
1892 {
1893 if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
1894 && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
1895 continue;
1896 Incremental_object_entry* entry = (*p)->object_entry();
1897 gold_assert(entry != NULL);
1898 const Object* obj = entry->object();
1899 gold_assert(obj != NULL);
1900 view_info.input_index = (*p)->get_file_index();
1901 Got_visitor v(view_info);
1902 obj->for_all_local_got_entries(&v);
1903 }
1904
1905 // Write the incremental GOT and PLT descriptors for global symbols.
1906 typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
1907 symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
1908 }
1909
1910 // Class Sized_relobj_incr. Most of these methods are not used for
1911 // Incremental objects, but are required to be implemented by the
1912 // base class Object.
1913
1914 template<int size, bool big_endian>
1915 Sized_relobj_incr<size, big_endian>::Sized_relobj_incr(
1916 const std::string& name,
1917 Sized_incremental_binary<size, big_endian>* ibase,
1918 unsigned int input_file_index)
1919 : Sized_relobj<size, big_endian>(name, NULL), ibase_(ibase),
1920 input_file_index_(input_file_index),
1921 input_reader_(ibase->inputs_reader().input_file(input_file_index)),
1922 local_symbol_count_(0), output_local_dynsym_count_(0),
1923 local_symbol_index_(0), local_symbol_offset_(0), local_dynsym_offset_(0),
1924 symbols_(), incr_reloc_offset_(-1U), incr_reloc_count_(0),
1925 incr_reloc_output_index_(0), incr_relocs_(NULL), local_symbols_()
1926 {
1927 if (this->input_reader_.is_in_system_directory())
1928 this->set_is_in_system_directory();
1929 const unsigned int shnum = this->input_reader_.get_input_section_count() + 1;
1930 this->set_shnum(shnum);
1931 ibase->set_input_object(input_file_index, this);
1932 }
1933
1934 // Read the symbols.
1935
1936 template<int size, bool big_endian>
1937 void
1938 Sized_relobj_incr<size, big_endian>::do_read_symbols(Read_symbols_data*)
1939 {
1940 gold_unreachable();
1941 }
1942
1943 // Lay out the input sections.
1944
1945 template<int size, bool big_endian>
1946 void
1947 Sized_relobj_incr<size, big_endian>::do_layout(
1948 Symbol_table*,
1949 Layout* layout,
1950 Read_symbols_data*)
1951 {
1952 const unsigned int shnum = this->shnum();
1953 Incremental_inputs* incremental_inputs = layout->incremental_inputs();
1954 gold_assert(incremental_inputs != NULL);
1955 Output_sections& out_sections(this->output_sections());
1956 out_sections.resize(shnum);
1957 this->section_offsets().resize(shnum);
1958 for (unsigned int i = 1; i < shnum; i++)
1959 {
1960 typename Input_entry_reader::Input_section_info sect =
1961 this->input_reader_.get_input_section(i - 1);
1962 // Add the section to the incremental inputs layout.
1963 incremental_inputs->report_input_section(this, i, sect.name,
1964 sect.sh_size);
1965 if (sect.output_shndx == 0 || sect.sh_offset == -1)
1966 continue;
1967 Output_section* os = this->ibase_->output_section(sect.output_shndx);
1968 gold_assert(os != NULL);
1969 out_sections[i] = os;
1970 this->section_offsets()[i] = static_cast<Address>(sect.sh_offset);
1971 }
1972
1973 // Process the COMDAT groups.
1974 unsigned int ncomdat = this->input_reader_.get_comdat_group_count();
1975 for (unsigned int i = 0; i < ncomdat; i++)
1976 {
1977 const char* signature = this->input_reader_.get_comdat_group_signature(i);
1978 if (signature == NULL || signature[0] == '\0')
1979 this->error(_("COMDAT group has no signature"));
1980 bool keep = layout->find_or_add_kept_section(signature, this, i, true,
1981 true, NULL);
1982 if (!keep)
1983 this->error(_("COMDAT group %s included twice in incremental link"),
1984 signature);
1985 }
1986 }
1987
1988 // Layout sections whose layout was deferred while waiting for
1989 // input files from a plugin.
1990 template<int size, bool big_endian>
1991 void
1992 Sized_relobj_incr<size, big_endian>::do_layout_deferred_sections(Layout*)
1993 {
1994 }
1995
1996 // Add the symbols to the symbol table.
1997
1998 template<int size, bool big_endian>
1999 void
2000 Sized_relobj_incr<size, big_endian>::do_add_symbols(
2001 Symbol_table* symtab,
2002 Read_symbols_data*,
2003 Layout*)
2004 {
2005 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2006 unsigned char symbuf[sym_size];
2007 elfcpp::Sym<size, big_endian> sym(symbuf);
2008 elfcpp::Sym_write<size, big_endian> osym(symbuf);
2009
2010 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2011
2012 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2013 this->symbols_.resize(nsyms);
2014
2015 Incremental_binary::View symtab_view(NULL);
2016 unsigned int symtab_count;
2017 elfcpp::Elf_strtab strtab(NULL, 0);
2018 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2019
2020 Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2021 unsigned int isym_count = isymtab.symbol_count();
2022 unsigned int first_global = symtab_count - isym_count;
2023
2024 const unsigned char* sym_p;
2025 for (unsigned int i = 0; i < nsyms; ++i)
2026 {
2027 Incremental_global_symbol_reader<big_endian> info =
2028 this->input_reader_.get_global_symbol_reader(i);
2029 unsigned int output_symndx = info.output_symndx();
2030 sym_p = symtab_view.data() + output_symndx * sym_size;
2031 elfcpp::Sym<size, big_endian> gsym(sym_p);
2032 const char* name;
2033 if (!strtab.get_c_string(gsym.get_st_name(), &name))
2034 name = "";
2035
2036 typename elfcpp::Elf_types<size>::Elf_Addr v = gsym.get_st_value();
2037 unsigned int shndx = gsym.get_st_shndx();
2038 elfcpp::STB st_bind = gsym.get_st_bind();
2039 elfcpp::STT st_type = gsym.get_st_type();
2040
2041 // Local hidden symbols start out as globals, but get converted to
2042 // to local during output.
2043 if (st_bind == elfcpp::STB_LOCAL)
2044 st_bind = elfcpp::STB_GLOBAL;
2045
2046 unsigned int input_shndx = info.shndx();
2047 if (input_shndx == 0 || input_shndx == -1U)
2048 {
2049 shndx = elfcpp::SHN_UNDEF;
2050 v = 0;
2051 }
2052 else if (shndx != elfcpp::SHN_ABS)
2053 {
2054 // Find the input section and calculate the section-relative value.
2055 gold_assert(shndx != elfcpp::SHN_UNDEF);
2056 Output_section* os = this->ibase_->output_section(shndx);
2057 gold_assert(os != NULL && os->has_fixed_layout());
2058 typename Input_entry_reader::Input_section_info sect =
2059 this->input_reader_.get_input_section(input_shndx - 1);
2060 gold_assert(sect.output_shndx == shndx);
2061 if (st_type != elfcpp::STT_TLS)
2062 v -= os->address();
2063 v -= sect.sh_offset;
2064 shndx = input_shndx;
2065 }
2066
2067 osym.put_st_name(0);
2068 osym.put_st_value(v);
2069 osym.put_st_size(gsym.get_st_size());
2070 osym.put_st_info(st_bind, st_type);
2071 osym.put_st_other(gsym.get_st_other());
2072 osym.put_st_shndx(shndx);
2073
2074 Symbol* res = symtab->add_from_incrobj(this, name, NULL, &sym);
2075
2076 // If this is a linker-defined symbol that hasn't yet been defined,
2077 // define it now.
2078 if (input_shndx == -1U && !res->is_defined())
2079 {
2080 shndx = gsym.get_st_shndx();
2081 v = gsym.get_st_value();
2082 Elf_size_type symsize = gsym.get_st_size();
2083 if (shndx == elfcpp::SHN_ABS)
2084 {
2085 symtab->define_as_constant(name, NULL,
2086 Symbol_table::INCREMENTAL_BASE,
2087 v, symsize, st_type, st_bind,
2088 gsym.get_st_visibility(), 0,
2089 false, false);
2090 }
2091 else
2092 {
2093 Output_section* os = this->ibase_->output_section(shndx);
2094 gold_assert(os != NULL && os->has_fixed_layout());
2095 v -= os->address();
2096 if (symsize > 0)
2097 os->reserve(v, symsize);
2098 symtab->define_in_output_data(name, NULL,
2099 Symbol_table::INCREMENTAL_BASE,
2100 os, v, symsize, st_type, st_bind,
2101 gsym.get_st_visibility(), 0,
2102 false, false);
2103 }
2104 }
2105
2106 this->symbols_[i] = res;
2107 this->ibase_->add_global_symbol(output_symndx - first_global, res);
2108 }
2109 }
2110
2111 // Return TRUE if we should include this object from an archive library.
2112
2113 template<int size, bool big_endian>
2114 Archive::Should_include
2115 Sized_relobj_incr<size, big_endian>::do_should_include_member(
2116 Symbol_table*,
2117 Layout*,
2118 Read_symbols_data*,
2119 std::string*)
2120 {
2121 gold_unreachable();
2122 }
2123
2124 // Iterate over global symbols, calling a visitor class V for each.
2125
2126 template<int size, bool big_endian>
2127 void
2128 Sized_relobj_incr<size, big_endian>::do_for_all_global_symbols(
2129 Read_symbols_data*,
2130 Library_base::Symbol_visitor_base*)
2131 {
2132 // This routine is not used for incremental objects.
2133 }
2134
2135 // Get the size of a section.
2136
2137 template<int size, bool big_endian>
2138 uint64_t
2139 Sized_relobj_incr<size, big_endian>::do_section_size(unsigned int)
2140 {
2141 gold_unreachable();
2142 }
2143
2144 // Get the name of a section.
2145
2146 template<int size, bool big_endian>
2147 std::string
2148 Sized_relobj_incr<size, big_endian>::do_section_name(unsigned int)
2149 {
2150 gold_unreachable();
2151 }
2152
2153 // Return a view of the contents of a section.
2154
2155 template<int size, bool big_endian>
2156 Object::Location
2157 Sized_relobj_incr<size, big_endian>::do_section_contents(unsigned int)
2158 {
2159 gold_unreachable();
2160 }
2161
2162 // Return section flags.
2163
2164 template<int size, bool big_endian>
2165 uint64_t
2166 Sized_relobj_incr<size, big_endian>::do_section_flags(unsigned int)
2167 {
2168 gold_unreachable();
2169 }
2170
2171 // Return section entsize.
2172
2173 template<int size, bool big_endian>
2174 uint64_t
2175 Sized_relobj_incr<size, big_endian>::do_section_entsize(unsigned int)
2176 {
2177 gold_unreachable();
2178 }
2179
2180 // Return section address.
2181
2182 template<int size, bool big_endian>
2183 uint64_t
2184 Sized_relobj_incr<size, big_endian>::do_section_address(unsigned int)
2185 {
2186 gold_unreachable();
2187 }
2188
2189 // Return section type.
2190
2191 template<int size, bool big_endian>
2192 unsigned int
2193 Sized_relobj_incr<size, big_endian>::do_section_type(unsigned int)
2194 {
2195 gold_unreachable();
2196 }
2197
2198 // Return the section link field.
2199
2200 template<int size, bool big_endian>
2201 unsigned int
2202 Sized_relobj_incr<size, big_endian>::do_section_link(unsigned int)
2203 {
2204 gold_unreachable();
2205 }
2206
2207 // Return the section link field.
2208
2209 template<int size, bool big_endian>
2210 unsigned int
2211 Sized_relobj_incr<size, big_endian>::do_section_info(unsigned int)
2212 {
2213 gold_unreachable();
2214 }
2215
2216 // Return the section alignment.
2217
2218 template<int size, bool big_endian>
2219 uint64_t
2220 Sized_relobj_incr<size, big_endian>::do_section_addralign(unsigned int)
2221 {
2222 gold_unreachable();
2223 }
2224
2225 // Return the Xindex structure to use.
2226
2227 template<int size, bool big_endian>
2228 Xindex*
2229 Sized_relobj_incr<size, big_endian>::do_initialize_xindex()
2230 {
2231 gold_unreachable();
2232 }
2233
2234 // Get symbol counts.
2235
2236 template<int size, bool big_endian>
2237 void
2238 Sized_relobj_incr<size, big_endian>::do_get_global_symbol_counts(
2239 const Symbol_table*, size_t*, size_t*) const
2240 {
2241 gold_unreachable();
2242 }
2243
2244 // Read the relocs.
2245
2246 template<int size, bool big_endian>
2247 void
2248 Sized_relobj_incr<size, big_endian>::do_read_relocs(Read_relocs_data*)
2249 {
2250 }
2251
2252 // Process the relocs to find list of referenced sections. Used only
2253 // during garbage collection.
2254
2255 template<int size, bool big_endian>
2256 void
2257 Sized_relobj_incr<size, big_endian>::do_gc_process_relocs(Symbol_table*,
2258 Layout*,
2259 Read_relocs_data*)
2260 {
2261 gold_unreachable();
2262 }
2263
2264 // Scan the relocs and adjust the symbol table.
2265
2266 template<int size, bool big_endian>
2267 void
2268 Sized_relobj_incr<size, big_endian>::do_scan_relocs(Symbol_table*,
2269 Layout* layout,
2270 Read_relocs_data*)
2271 {
2272 // Count the incremental relocations for this object.
2273 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2274 this->allocate_incremental_reloc_counts();
2275 for (unsigned int i = 0; i < nsyms; i++)
2276 {
2277 Incremental_global_symbol_reader<big_endian> sym =
2278 this->input_reader_.get_global_symbol_reader(i);
2279 unsigned int reloc_count = sym.reloc_count();
2280 if (reloc_count > 0 && this->incr_reloc_offset_ == -1U)
2281 this->incr_reloc_offset_ = sym.reloc_offset();
2282 this->incr_reloc_count_ += reloc_count;
2283 for (unsigned int j = 0; j < reloc_count; j++)
2284 this->count_incremental_reloc(i);
2285 }
2286 this->incr_reloc_output_index_ =
2287 layout->incremental_inputs()->get_reloc_count();
2288 this->finalize_incremental_relocs(layout, false);
2289
2290 // The incoming incremental relocations may not end up in the same
2291 // location after the incremental update, because the incremental info
2292 // is regenerated in each link. Because the new location may overlap
2293 // with other data in the updated output file, we need to copy the
2294 // relocations into a buffer so that we can still read them safely
2295 // after we start writing updates to the output file.
2296 if (this->incr_reloc_count_ > 0)
2297 {
2298 const Incremental_relocs_reader<size, big_endian>& relocs_reader =
2299 this->ibase_->relocs_reader();
2300 const unsigned int incr_reloc_size = relocs_reader.reloc_size;
2301 unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2302 this->incr_relocs_ = new unsigned char[len];
2303 memcpy(this->incr_relocs_,
2304 relocs_reader.data(this->incr_reloc_offset_),
2305 len);
2306 }
2307 }
2308
2309 // Count the local symbols.
2310
2311 template<int size, bool big_endian>
2312 void
2313 Sized_relobj_incr<size, big_endian>::do_count_local_symbols(
2314 Stringpool_template<char>* pool,
2315 Stringpool_template<char>*)
2316 {
2317 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2318
2319 // Set the count of local symbols based on the incremental info.
2320 unsigned int nlocals = this->input_reader_.get_local_symbol_count();
2321 this->local_symbol_count_ = nlocals;
2322 this->local_symbols_.reserve(nlocals);
2323
2324 // Get views of the base file's symbol table and string table.
2325 Incremental_binary::View symtab_view(NULL);
2326 unsigned int symtab_count;
2327 elfcpp::Elf_strtab strtab(NULL, 0);
2328 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2329
2330 // Read the local symbols from the base file's symbol table.
2331 off_t off = this->input_reader_.get_local_symbol_offset();
2332 const unsigned char* symp = symtab_view.data() + off;
2333 for (unsigned int i = 0; i < nlocals; ++i, symp += sym_size)
2334 {
2335 elfcpp::Sym<size, big_endian> sym(symp);
2336 const char* name;
2337 if (!strtab.get_c_string(sym.get_st_name(), &name))
2338 name = "";
2339 gold_debug(DEBUG_INCREMENTAL, "Local symbol %d: %s", i, name);
2340 name = pool->add(name, true, NULL);
2341 this->local_symbols_.push_back(Local_symbol(name,
2342 sym.get_st_value(),
2343 sym.get_st_size(),
2344 sym.get_st_shndx(),
2345 sym.get_st_type(),
2346 false));
2347 }
2348 }
2349
2350 // Finalize the local symbols.
2351
2352 template<int size, bool big_endian>
2353 unsigned int
2354 Sized_relobj_incr<size, big_endian>::do_finalize_local_symbols(
2355 unsigned int index,
2356 off_t off,
2357 Symbol_table*)
2358 {
2359 this->local_symbol_index_ = index;
2360 this->local_symbol_offset_ = off;
2361 return index + this->local_symbol_count_;
2362 }
2363
2364 // Set the offset where local dynamic symbol information will be stored.
2365
2366 template<int size, bool big_endian>
2367 unsigned int
2368 Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_indexes(
2369 unsigned int index)
2370 {
2371 // FIXME: set local dynsym indexes.
2372 return index;
2373 }
2374
2375 // Set the offset where local dynamic symbol information will be stored.
2376
2377 template<int size, bool big_endian>
2378 unsigned int
2379 Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_offset(off_t)
2380 {
2381 return 0;
2382 }
2383
2384 // Relocate the input sections and write out the local symbols.
2385 // We don't actually do any relocation here. For unchanged input files,
2386 // we reapply relocations only for symbols that have changed; that happens
2387 // in queue_final_tasks. We do need to rewrite the incremental relocations
2388 // for this object.
2389
2390 template<int size, bool big_endian>
2391 void
2392 Sized_relobj_incr<size, big_endian>::do_relocate(const Symbol_table*,
2393 const Layout* layout,
2394 Output_file* of)
2395 {
2396 if (this->incr_reloc_count_ == 0)
2397 return;
2398
2399 const unsigned int incr_reloc_size =
2400 Incremental_relocs_reader<size, big_endian>::reloc_size;
2401
2402 // Get a view for the .gnu_incremental_relocs section.
2403 Incremental_inputs* inputs = layout->incremental_inputs();
2404 gold_assert(inputs != NULL);
2405 const off_t relocs_off = inputs->relocs_section()->offset();
2406 const off_t relocs_size = inputs->relocs_section()->data_size();
2407 unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
2408
2409 // Copy the relocations from the buffer.
2410 off_t off = this->incr_reloc_output_index_ * incr_reloc_size;
2411 unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2412 memcpy(view + off, this->incr_relocs_, len);
2413
2414 // The output section table may have changed, so we need to map
2415 // the old section index to the new section index for each relocation.
2416 for (unsigned int i = 0; i < this->incr_reloc_count_; ++i)
2417 {
2418 unsigned char* pov = view + off + i * incr_reloc_size;
2419 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(pov + 4);
2420 Output_section* os = this->ibase_->output_section(shndx);
2421 gold_assert(os != NULL);
2422 shndx = os->out_shndx();
2423 elfcpp::Swap<32, big_endian>::writeval(pov + 4, shndx);
2424 }
2425
2426 of->write_output_view(off, len, view);
2427
2428 // Get views into the output file for the portions of the symbol table
2429 // and the dynamic symbol table that we will be writing.
2430 off_t symtab_off = layout->symtab_section()->offset();
2431 off_t output_size = this->local_symbol_count_ * This::sym_size;
2432 unsigned char* oview = NULL;
2433 if (output_size > 0)
2434 oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
2435 output_size);
2436
2437 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
2438 unsigned char* dyn_oview = NULL;
2439 if (dyn_output_size > 0)
2440 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
2441 dyn_output_size);
2442
2443 // Write the local symbols.
2444 unsigned char* ov = oview;
2445 unsigned char* dyn_ov = dyn_oview;
2446 const Stringpool* sympool = layout->sympool();
2447 const Stringpool* dynpool = layout->dynpool();
2448 Output_symtab_xindex* symtab_xindex = layout->symtab_xindex();
2449 Output_symtab_xindex* dynsym_xindex = layout->dynsym_xindex();
2450 for (unsigned int i = 0; i < this->local_symbol_count_; ++i)
2451 {
2452 Local_symbol& lsym(this->local_symbols_[i]);
2453
2454 bool is_ordinary;
2455 unsigned int st_shndx = this->adjust_sym_shndx(i, lsym.st_shndx,
2456 &is_ordinary);
2457 if (is_ordinary)
2458 {
2459 Output_section* os = this->ibase_->output_section(st_shndx);
2460 st_shndx = os->out_shndx();
2461 if (st_shndx >= elfcpp::SHN_LORESERVE)
2462 {
2463 symtab_xindex->add(this->local_symbol_index_ + i, st_shndx);
2464 if (lsym.needs_dynsym_entry)
2465 dynsym_xindex->add(lsym.output_dynsym_index, st_shndx);
2466 st_shndx = elfcpp::SHN_XINDEX;
2467 }
2468 }
2469
2470 // Write the symbol to the output symbol table.
2471 {
2472 elfcpp::Sym_write<size, big_endian> osym(ov);
2473 osym.put_st_name(sympool->get_offset(lsym.name));
2474 osym.put_st_value(lsym.st_value);
2475 osym.put_st_size(lsym.st_size);
2476 osym.put_st_info(elfcpp::STB_LOCAL,
2477 static_cast<elfcpp::STT>(lsym.st_type));
2478 osym.put_st_other(0);
2479 osym.put_st_shndx(st_shndx);
2480 ov += sym_size;
2481 }
2482
2483 // Write the symbol to the output dynamic symbol table.
2484 if (lsym.needs_dynsym_entry)
2485 {
2486 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
2487 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
2488 osym.put_st_name(dynpool->get_offset(lsym.name));
2489 osym.put_st_value(lsym.st_value);
2490 osym.put_st_size(lsym.st_size);
2491 osym.put_st_info(elfcpp::STB_LOCAL,
2492 static_cast<elfcpp::STT>(lsym.st_type));
2493 osym.put_st_other(0);
2494 osym.put_st_shndx(st_shndx);
2495 dyn_ov += sym_size;
2496 }
2497 }
2498
2499 if (output_size > 0)
2500 {
2501 gold_assert(ov - oview == output_size);
2502 of->write_output_view(symtab_off + this->local_symbol_offset_,
2503 output_size, oview);
2504 }
2505
2506 if (dyn_output_size > 0)
2507 {
2508 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
2509 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
2510 dyn_oview);
2511 }
2512 }
2513
2514 // Set the offset of a section.
2515
2516 template<int size, bool big_endian>
2517 void
2518 Sized_relobj_incr<size, big_endian>::do_set_section_offset(unsigned int,
2519 uint64_t)
2520 {
2521 }
2522
2523 // Class Sized_incr_dynobj. Most of these methods are not used for
2524 // Incremental objects, but are required to be implemented by the
2525 // base class Object.
2526
2527 template<int size, bool big_endian>
2528 Sized_incr_dynobj<size, big_endian>::Sized_incr_dynobj(
2529 const std::string& name,
2530 Sized_incremental_binary<size, big_endian>* ibase,
2531 unsigned int input_file_index)
2532 : Dynobj(name, NULL), ibase_(ibase),
2533 input_file_index_(input_file_index),
2534 input_reader_(ibase->inputs_reader().input_file(input_file_index)),
2535 symbols_()
2536 {
2537 if (this->input_reader_.is_in_system_directory())
2538 this->set_is_in_system_directory();
2539 if (this->input_reader_.as_needed())
2540 this->set_as_needed();
2541 this->set_soname_string(this->input_reader_.get_soname());
2542 this->set_shnum(0);
2543 }
2544
2545 // Read the symbols.
2546
2547 template<int size, bool big_endian>
2548 void
2549 Sized_incr_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
2550 {
2551 gold_unreachable();
2552 }
2553
2554 // Lay out the input sections.
2555
2556 template<int size, bool big_endian>
2557 void
2558 Sized_incr_dynobj<size, big_endian>::do_layout(
2559 Symbol_table*,
2560 Layout*,
2561 Read_symbols_data*)
2562 {
2563 }
2564
2565 // Add the symbols to the symbol table.
2566
2567 template<int size, bool big_endian>
2568 void
2569 Sized_incr_dynobj<size, big_endian>::do_add_symbols(
2570 Symbol_table* symtab,
2571 Read_symbols_data*,
2572 Layout*)
2573 {
2574 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2575 unsigned char symbuf[sym_size];
2576 elfcpp::Sym<size, big_endian> sym(symbuf);
2577 elfcpp::Sym_write<size, big_endian> osym(symbuf);
2578
2579 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2580
2581 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2582 this->symbols_.resize(nsyms);
2583
2584 Incremental_binary::View symtab_view(NULL);
2585 unsigned int symtab_count;
2586 elfcpp::Elf_strtab strtab(NULL, 0);
2587 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2588
2589 Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2590 unsigned int isym_count = isymtab.symbol_count();
2591 unsigned int first_global = symtab_count - isym_count;
2592
2593 // We keep a set of symbols that we have generated COPY relocations
2594 // for, indexed by the symbol value. We do not need more than one
2595 // COPY relocation per address.
2596 typedef typename std::set<Address> Copied_symbols;
2597 Copied_symbols copied_symbols;
2598
2599 const unsigned char* sym_p;
2600 for (unsigned int i = 0; i < nsyms; ++i)
2601 {
2602 bool is_def;
2603 bool is_copy;
2604 unsigned int output_symndx =
2605 this->input_reader_.get_output_symbol_index(i, &is_def, &is_copy);
2606 sym_p = symtab_view.data() + output_symndx * sym_size;
2607 elfcpp::Sym<size, big_endian> gsym(sym_p);
2608 const char* name;
2609 if (!strtab.get_c_string(gsym.get_st_name(), &name))
2610 name = "";
2611
2612 Address v;
2613 unsigned int shndx;
2614 elfcpp::STB st_bind = gsym.get_st_bind();
2615 elfcpp::STT st_type = gsym.get_st_type();
2616
2617 // Local hidden symbols start out as globals, but get converted to
2618 // to local during output.
2619 if (st_bind == elfcpp::STB_LOCAL)
2620 st_bind = elfcpp::STB_GLOBAL;
2621
2622 if (!is_def)
2623 {
2624 shndx = elfcpp::SHN_UNDEF;
2625 v = 0;
2626 }
2627 else
2628 {
2629 // For a symbol defined in a shared object, the section index
2630 // is meaningless, as long as it's not SHN_UNDEF.
2631 shndx = 1;
2632 v = gsym.get_st_value();
2633 }
2634
2635 osym.put_st_name(0);
2636 osym.put_st_value(v);
2637 osym.put_st_size(gsym.get_st_size());
2638 osym.put_st_info(st_bind, st_type);
2639 osym.put_st_other(gsym.get_st_other());
2640 osym.put_st_shndx(shndx);
2641
2642 Sized_symbol<size>* res =
2643 symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
2644 this->symbols_[i] = res;
2645 this->ibase_->add_global_symbol(output_symndx - first_global,
2646 this->symbols_[i]);
2647
2648 if (is_copy)
2649 {
2650 std::pair<typename Copied_symbols::iterator, bool> ins =
2651 copied_symbols.insert(v);
2652 if (ins.second)
2653 {
2654 unsigned int shndx = gsym.get_st_shndx();
2655 Output_section* os = this->ibase_->output_section(shndx);
2656 off_t offset = v - os->address();
2657 this->ibase_->add_copy_reloc(this->symbols_[i], os, offset);
2658 }
2659 }
2660 }
2661 }
2662
2663 // Return TRUE if we should include this object from an archive library.
2664
2665 template<int size, bool big_endian>
2666 Archive::Should_include
2667 Sized_incr_dynobj<size, big_endian>::do_should_include_member(
2668 Symbol_table*,
2669 Layout*,
2670 Read_symbols_data*,
2671 std::string*)
2672 {
2673 gold_unreachable();
2674 }
2675
2676 // Iterate over global symbols, calling a visitor class V for each.
2677
2678 template<int size, bool big_endian>
2679 void
2680 Sized_incr_dynobj<size, big_endian>::do_for_all_global_symbols(
2681 Read_symbols_data*,
2682 Library_base::Symbol_visitor_base*)
2683 {
2684 // This routine is not used for dynamic libraries.
2685 }
2686
2687 // Iterate over local symbols, calling a visitor class V for each GOT offset
2688 // associated with a local symbol.
2689
2690 template<int size, bool big_endian>
2691 void
2692 Sized_incr_dynobj<size, big_endian>::do_for_all_local_got_entries(
2693 Got_offset_list::Visitor*) const
2694 {
2695 }
2696
2697 // Get the size of a section.
2698
2699 template<int size, bool big_endian>
2700 uint64_t
2701 Sized_incr_dynobj<size, big_endian>::do_section_size(unsigned int)
2702 {
2703 gold_unreachable();
2704 }
2705
2706 // Get the name of a section.
2707
2708 template<int size, bool big_endian>
2709 std::string
2710 Sized_incr_dynobj<size, big_endian>::do_section_name(unsigned int)
2711 {
2712 gold_unreachable();
2713 }
2714
2715 // Return a view of the contents of a section.
2716
2717 template<int size, bool big_endian>
2718 Object::Location
2719 Sized_incr_dynobj<size, big_endian>::do_section_contents(unsigned int)
2720 {
2721 gold_unreachable();
2722 }
2723
2724 // Return section flags.
2725
2726 template<int size, bool big_endian>
2727 uint64_t
2728 Sized_incr_dynobj<size, big_endian>::do_section_flags(unsigned int)
2729 {
2730 gold_unreachable();
2731 }
2732
2733 // Return section entsize.
2734
2735 template<int size, bool big_endian>
2736 uint64_t
2737 Sized_incr_dynobj<size, big_endian>::do_section_entsize(unsigned int)
2738 {
2739 gold_unreachable();
2740 }
2741
2742 // Return section address.
2743
2744 template<int size, bool big_endian>
2745 uint64_t
2746 Sized_incr_dynobj<size, big_endian>::do_section_address(unsigned int)
2747 {
2748 gold_unreachable();
2749 }
2750
2751 // Return section type.
2752
2753 template<int size, bool big_endian>
2754 unsigned int
2755 Sized_incr_dynobj<size, big_endian>::do_section_type(unsigned int)
2756 {
2757 gold_unreachable();
2758 }
2759
2760 // Return the section link field.
2761
2762 template<int size, bool big_endian>
2763 unsigned int
2764 Sized_incr_dynobj<size, big_endian>::do_section_link(unsigned int)
2765 {
2766 gold_unreachable();
2767 }
2768
2769 // Return the section link field.
2770
2771 template<int size, bool big_endian>
2772 unsigned int
2773 Sized_incr_dynobj<size, big_endian>::do_section_info(unsigned int)
2774 {
2775 gold_unreachable();
2776 }
2777
2778 // Return the section alignment.
2779
2780 template<int size, bool big_endian>
2781 uint64_t
2782 Sized_incr_dynobj<size, big_endian>::do_section_addralign(unsigned int)
2783 {
2784 gold_unreachable();
2785 }
2786
2787 // Return the Xindex structure to use.
2788
2789 template<int size, bool big_endian>
2790 Xindex*
2791 Sized_incr_dynobj<size, big_endian>::do_initialize_xindex()
2792 {
2793 gold_unreachable();
2794 }
2795
2796 // Get symbol counts.
2797
2798 template<int size, bool big_endian>
2799 void
2800 Sized_incr_dynobj<size, big_endian>::do_get_global_symbol_counts(
2801 const Symbol_table*, size_t*, size_t*) const
2802 {
2803 gold_unreachable();
2804 }
2805
2806 // Allocate an incremental object of the appropriate size and endianness.
2807
2808 Object*
2809 make_sized_incremental_object(
2810 Incremental_binary* ibase,
2811 unsigned int input_file_index,
2812 Incremental_input_type input_type,
2813 const Incremental_binary::Input_reader* input_reader)
2814 {
2815 Object* obj = NULL;
2816 std::string name(input_reader->filename());
2817
2818 switch (parameters->size_and_endianness())
2819 {
2820 #ifdef HAVE_TARGET_32_LITTLE
2821 case Parameters::TARGET_32_LITTLE:
2822 {
2823 Sized_incremental_binary<32, false>* sized_ibase =
2824 static_cast<Sized_incremental_binary<32, false>*>(ibase);
2825 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2826 obj = new Sized_incr_dynobj<32, false>(name, sized_ibase,
2827 input_file_index);
2828 else
2829 obj = new Sized_relobj_incr<32, false>(name, sized_ibase,
2830 input_file_index);
2831 }
2832 break;
2833 #endif
2834 #ifdef HAVE_TARGET_32_BIG
2835 case Parameters::TARGET_32_BIG:
2836 {
2837 Sized_incremental_binary<32, true>* sized_ibase =
2838 static_cast<Sized_incremental_binary<32, true>*>(ibase);
2839 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2840 obj = new Sized_incr_dynobj<32, true>(name, sized_ibase,
2841 input_file_index);
2842 else
2843 obj = new Sized_relobj_incr<32, true>(name, sized_ibase,
2844 input_file_index);
2845 }
2846 break;
2847 #endif
2848 #ifdef HAVE_TARGET_64_LITTLE
2849 case Parameters::TARGET_64_LITTLE:
2850 {
2851 Sized_incremental_binary<64, false>* sized_ibase =
2852 static_cast<Sized_incremental_binary<64, false>*>(ibase);
2853 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2854 obj = new Sized_incr_dynobj<64, false>(name, sized_ibase,
2855 input_file_index);
2856 else
2857 obj = new Sized_relobj_incr<64, false>(name, sized_ibase,
2858 input_file_index);
2859 }
2860 break;
2861 #endif
2862 #ifdef HAVE_TARGET_64_BIG
2863 case Parameters::TARGET_64_BIG:
2864 {
2865 Sized_incremental_binary<64, true>* sized_ibase =
2866 static_cast<Sized_incremental_binary<64, true>*>(ibase);
2867 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2868 obj = new Sized_incr_dynobj<64, true>(name, sized_ibase,
2869 input_file_index);
2870 else
2871 obj = new Sized_relobj_incr<64, true>(name, sized_ibase,
2872 input_file_index);
2873 }
2874 break;
2875 #endif
2876 default:
2877 gold_unreachable();
2878 }
2879
2880 gold_assert(obj != NULL);
2881 return obj;
2882 }
2883
2884 // Copy the unused symbols from the incremental input info.
2885 // We need to do this because we may be overwriting the incremental
2886 // input info in the base file before we write the new incremental
2887 // info.
2888 void
2889 Incremental_library::copy_unused_symbols()
2890 {
2891 unsigned int symcount = this->input_reader_->get_unused_symbol_count();
2892 this->unused_symbols_.reserve(symcount);
2893 for (unsigned int i = 0; i < symcount; ++i)
2894 {
2895 std::string name(this->input_reader_->get_unused_symbol(i));
2896 this->unused_symbols_.push_back(name);
2897 }
2898 }
2899
2900 // Iterator for unused global symbols in the library.
2901 void
2902 Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base* v) const
2903 {
2904 for (Symbol_list::const_iterator p = this->unused_symbols_.begin();
2905 p != this->unused_symbols_.end();
2906 ++p)
2907 v->visit(p->c_str());
2908 }
2909
2910 // Instantiate the templates we need.
2911
2912 #ifdef HAVE_TARGET_32_LITTLE
2913 template
2914 class Sized_incremental_binary<32, false>;
2915
2916 template
2917 class Sized_relobj_incr<32, false>;
2918
2919 template
2920 class Sized_incr_dynobj<32, false>;
2921 #endif
2922
2923 #ifdef HAVE_TARGET_32_BIG
2924 template
2925 class Sized_incremental_binary<32, true>;
2926
2927 template
2928 class Sized_relobj_incr<32, true>;
2929
2930 template
2931 class Sized_incr_dynobj<32, true>;
2932 #endif
2933
2934 #ifdef HAVE_TARGET_64_LITTLE
2935 template
2936 class Sized_incremental_binary<64, false>;
2937
2938 template
2939 class Sized_relobj_incr<64, false>;
2940
2941 template
2942 class Sized_incr_dynobj<64, false>;
2943 #endif
2944
2945 #ifdef HAVE_TARGET_64_BIG
2946 template
2947 class Sized_incremental_binary<64, true>;
2948
2949 template
2950 class Sized_relobj_incr<64, true>;
2951
2952 template
2953 class Sized_incr_dynobj<64, true>;
2954 #endif
2955
2956 } // End namespace gold.
This page took 0.124128 seconds and 5 git commands to generate.