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