Commit | Line | Data |
---|---|---|
0e879927 ILT |
1 | // inremental.cc -- incremental linking support for gold |
2 | ||
09ec0418 | 3 | // Copyright 2009, 2010 Free Software Foundation, Inc. |
0e879927 ILT |
4 | // Written by Mikolaj Zalewski <mikolajz@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
23 | #include "gold.h" | |
c549a694 ILT |
24 | |
25 | #include <cstdarg> | |
a81ee015 | 26 | #include "libiberty.h" |
c549a694 | 27 | |
0e879927 | 28 | #include "elfcpp.h" |
3ce2c28e | 29 | #include "output.h" |
09ec0418 | 30 | #include "symtab.h" |
3ce2c28e | 31 | #include "incremental.h" |
98fa85cb | 32 | #include "archive.h" |
44453f85 | 33 | #include "output.h" |
c549a694 | 34 | #include "target-select.h" |
0e879927 | 35 | |
0e879927 ILT |
36 | namespace gold { |
37 | ||
38 | // Version information. Will change frequently during the development, later | |
39 | // we could think about backward (and forward?) compatibility. | |
c4aa1e2d | 40 | const unsigned int INCREMENTAL_LINK_VERSION = 1; |
0e879927 | 41 | |
09ec0418 CC |
42 | // This class manages the .gnu_incremental_inputs section, which holds |
43 | // the header information, a directory of input files, and separate | |
44 | // entries for each input file. | |
45 | ||
46 | template<int size, bool big_endian> | |
47 | class Output_section_incremental_inputs : public Output_section_data | |
48 | { | |
49 | public: | |
50 | Output_section_incremental_inputs(const Incremental_inputs* inputs, | |
51 | const Symbol_table* symtab) | |
52 | : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab) | |
53 | { } | |
54 | ||
55 | protected: | |
56 | // Set the final data size. | |
57 | void | |
58 | set_final_data_size(); | |
59 | ||
60 | // Write the data to the file. | |
61 | void | |
62 | do_write(Output_file*); | |
63 | ||
64 | // Write to a map file. | |
65 | void | |
66 | do_print_to_mapfile(Mapfile* mapfile) const | |
67 | { mapfile->print_output_data(this, _("** incremental_inputs")); } | |
68 | ||
69 | private: | |
70 | // Write the section header. | |
71 | unsigned char* | |
72 | write_header(unsigned char* pov, unsigned int input_file_count, | |
73 | section_offset_type command_line_offset); | |
74 | ||
75 | // Write the input file entries. | |
76 | unsigned char* | |
77 | write_input_files(unsigned char* oview, unsigned char* pov, | |
78 | Stringpool* strtab); | |
79 | ||
80 | // Write the supplemental information blocks. | |
81 | unsigned char* | |
82 | write_info_blocks(unsigned char* oview, unsigned char* pov, | |
83 | Stringpool* strtab, unsigned int* global_syms, | |
84 | unsigned int global_sym_count); | |
85 | ||
86 | // Write the contents of the .gnu_incremental_symtab section. | |
87 | void | |
88 | write_symtab(unsigned char* pov, unsigned int* global_syms, | |
89 | unsigned int global_sym_count); | |
90 | ||
91 | // Typedefs for writing the data to the output sections. | |
92 | typedef elfcpp::Swap<size, big_endian> Swap; | |
93 | typedef elfcpp::Swap<16, big_endian> Swap16; | |
94 | typedef elfcpp::Swap<32, big_endian> Swap32; | |
95 | typedef elfcpp::Swap<64, big_endian> Swap64; | |
96 | ||
97 | // Sizes of various structures. | |
98 | static const int sizeof_addr = size / 8; | |
99 | static const int header_size = 16; | |
100 | static const int input_entry_size = 24; | |
101 | ||
102 | // The Incremental_inputs object. | |
103 | const Incremental_inputs* inputs_; | |
104 | ||
105 | // The symbol table. | |
106 | const Symbol_table* symtab_; | |
107 | }; | |
108 | ||
c549a694 ILT |
109 | // Inform the user why we don't do an incremental link. Not called in |
110 | // the obvious case of missing output file. TODO: Is this helpful? | |
111 | ||
112 | void | |
113 | vexplain_no_incremental(const char* format, va_list args) | |
114 | { | |
115 | char* buf = NULL; | |
116 | if (vasprintf(&buf, format, args) < 0) | |
117 | gold_nomem(); | |
118 | gold_info(_("the link might take longer: " | |
119 | "cannot perform incremental link: %s"), buf); | |
120 | free(buf); | |
121 | } | |
122 | ||
123 | void | |
124 | explain_no_incremental(const char* format, ...) | |
125 | { | |
126 | va_list args; | |
127 | va_start(args, format); | |
128 | vexplain_no_incremental(format, args); | |
129 | va_end(args); | |
130 | } | |
131 | ||
132 | // Report an error. | |
133 | ||
134 | void | |
135 | Incremental_binary::error(const char* format, ...) const | |
136 | { | |
137 | va_list args; | |
138 | va_start(args, format); | |
139 | // Current code only checks if the file can be used for incremental linking, | |
140 | // so errors shouldn't fail the build, but only result in a fallback to a | |
141 | // full build. | |
142 | // TODO: when we implement incremental editing of the file, we may need a | |
143 | // flag that will cause errors to be treated seriously. | |
144 | vexplain_no_incremental(format, args); | |
145 | va_end(args); | |
146 | } | |
147 | ||
09ec0418 CC |
148 | // Find the .gnu_incremental_inputs section and related sections. |
149 | ||
c549a694 ILT |
150 | template<int size, bool big_endian> |
151 | bool | |
09ec0418 CC |
152 | Sized_incremental_binary<size, big_endian>::do_find_incremental_inputs_sections( |
153 | unsigned int* p_inputs_shndx, | |
154 | unsigned int* p_symtab_shndx, | |
155 | unsigned int* p_relocs_shndx, | |
156 | unsigned int* p_strtab_shndx) | |
c549a694 | 157 | { |
09ec0418 CC |
158 | unsigned int inputs_shndx = |
159 | this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS); | |
160 | if (inputs_shndx == elfcpp::SHN_UNDEF) // Not found. | |
161 | return false; | |
162 | ||
163 | unsigned int symtab_shndx = | |
164 | this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB); | |
165 | if (symtab_shndx == elfcpp::SHN_UNDEF) // Not found. | |
166 | return false; | |
167 | if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx) | |
c549a694 | 168 | return false; |
09ec0418 CC |
169 | |
170 | unsigned int relocs_shndx = | |
171 | this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS); | |
172 | if (relocs_shndx == elfcpp::SHN_UNDEF) // Not found. | |
173 | return false; | |
174 | if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx) | |
175 | return false; | |
176 | ||
177 | unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx); | |
178 | if (strtab_shndx == elfcpp::SHN_UNDEF | |
179 | || strtab_shndx > this->elf_file_.shnum() | |
180 | || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB) | |
181 | return false; | |
182 | ||
183 | if (p_inputs_shndx != NULL) | |
184 | *p_inputs_shndx = inputs_shndx; | |
185 | if (p_symtab_shndx != NULL) | |
186 | *p_symtab_shndx = symtab_shndx; | |
187 | if (p_relocs_shndx != NULL) | |
188 | *p_relocs_shndx = relocs_shndx; | |
189 | if (p_strtab_shndx != NULL) | |
190 | *p_strtab_shndx = strtab_shndx; | |
c549a694 ILT |
191 | return true; |
192 | } | |
193 | ||
09ec0418 CC |
194 | // Determine whether an incremental link based on the existing output file |
195 | // can be done. | |
196 | ||
c4aa1e2d ILT |
197 | template<int size, bool big_endian> |
198 | bool | |
199 | Sized_incremental_binary<size, big_endian>::do_check_inputs( | |
200 | Incremental_inputs* incremental_inputs) | |
201 | { | |
09ec0418 CC |
202 | unsigned int inputs_shndx; |
203 | unsigned int symtab_shndx; | |
204 | unsigned int relocs_shndx; | |
c4aa1e2d | 205 | unsigned int strtab_shndx; |
c4aa1e2d | 206 | |
09ec0418 CC |
207 | if (!do_find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx, |
208 | &relocs_shndx, &strtab_shndx)) | |
c4aa1e2d ILT |
209 | { |
210 | explain_no_incremental(_("no incremental data from previous build")); | |
211 | return false; | |
212 | } | |
c4aa1e2d | 213 | |
09ec0418 CC |
214 | Location inputs_location(this->elf_file_.section_contents(inputs_shndx)); |
215 | Location symtab_location(this->elf_file_.section_contents(symtab_shndx)); | |
216 | Location relocs_location(this->elf_file_.section_contents(relocs_shndx)); | |
c4aa1e2d | 217 | Location strtab_location(this->elf_file_.section_contents(strtab_shndx)); |
09ec0418 CC |
218 | |
219 | View inputs_view(view(inputs_location)); | |
220 | View symtab_view(view(symtab_location)); | |
221 | View relocs_view(view(relocs_location)); | |
c4aa1e2d | 222 | View strtab_view(view(strtab_location)); |
09ec0418 | 223 | |
c4aa1e2d | 224 | elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size); |
c4aa1e2d | 225 | |
09ec0418 CC |
226 | Incremental_inputs_reader<size, big_endian> |
227 | incoming_inputs(inputs_view.data(), strtab); | |
c4aa1e2d | 228 | |
09ec0418 | 229 | if (incoming_inputs.version() != INCREMENTAL_LINK_VERSION) |
c4aa1e2d | 230 | { |
09ec0418 | 231 | explain_no_incremental(_("different version of incremental build data")); |
c4aa1e2d ILT |
232 | return false; |
233 | } | |
234 | ||
09ec0418 | 235 | if (incremental_inputs->command_line() != incoming_inputs.command_line()) |
c4aa1e2d ILT |
236 | { |
237 | explain_no_incremental(_("command line changed")); | |
238 | return false; | |
239 | } | |
240 | ||
241 | // TODO: compare incremental_inputs->inputs() with entries in data_view. | |
09ec0418 | 242 | |
c4aa1e2d ILT |
243 | return true; |
244 | } | |
245 | ||
c549a694 ILT |
246 | namespace |
247 | { | |
248 | ||
249 | // Create a Sized_incremental_binary object of the specified size and | |
250 | // endianness. Fails if the target architecture is not supported. | |
251 | ||
252 | template<int size, bool big_endian> | |
253 | Incremental_binary* | |
254 | make_sized_incremental_binary(Output_file* file, | |
255 | const elfcpp::Ehdr<size, big_endian>& ehdr) | |
256 | { | |
257 | Target* target = select_target(ehdr.get_e_machine(), size, big_endian, | |
258 | ehdr.get_e_ident()[elfcpp::EI_OSABI], | |
259 | ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]); | |
260 | if (target == NULL) | |
261 | { | |
262 | explain_no_incremental(_("unsupported ELF machine number %d"), | |
263 | ehdr.get_e_machine()); | |
264 | return NULL; | |
265 | } | |
266 | ||
3aec4f9c RÁE |
267 | if (!parameters->target_valid()) |
268 | set_parameters_target(target); | |
269 | else if (target != ¶meters->target()) | |
270 | gold_error(_("%s: incompatible target"), file->filename()); | |
271 | ||
c549a694 ILT |
272 | return new Sized_incremental_binary<size, big_endian>(file, ehdr, target); |
273 | } | |
274 | ||
275 | } // End of anonymous namespace. | |
276 | ||
09ec0418 CC |
277 | // Create an Incremental_binary object for FILE. Returns NULL is this is not |
278 | // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE | |
c549a694 ILT |
279 | // should be opened. |
280 | ||
281 | Incremental_binary* | |
282 | open_incremental_binary(Output_file* file) | |
283 | { | |
284 | off_t filesize = file->filesize(); | |
285 | int want = elfcpp::Elf_recognizer::max_header_size; | |
286 | if (filesize < want) | |
287 | want = filesize; | |
288 | ||
289 | const unsigned char* p = file->get_input_view(0, want); | |
290 | if (!elfcpp::Elf_recognizer::is_elf_file(p, want)) | |
291 | { | |
292 | explain_no_incremental(_("output is not an ELF file.")); | |
293 | return NULL; | |
294 | } | |
295 | ||
ac33a407 DK |
296 | int size = 0; |
297 | bool big_endian = false; | |
c549a694 ILT |
298 | std::string error; |
299 | if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian, | |
300 | &error)) | |
301 | { | |
302 | explain_no_incremental(error.c_str()); | |
303 | return NULL; | |
304 | } | |
305 | ||
306 | Incremental_binary* result = NULL; | |
307 | if (size == 32) | |
308 | { | |
309 | if (big_endian) | |
310 | { | |
311 | #ifdef HAVE_TARGET_32_BIG | |
312 | result = make_sized_incremental_binary<32, true>( | |
313 | file, elfcpp::Ehdr<32, true>(p)); | |
314 | #else | |
315 | explain_no_incremental(_("unsupported file: 32-bit, big-endian")); | |
316 | #endif | |
317 | } | |
318 | else | |
319 | { | |
320 | #ifdef HAVE_TARGET_32_LITTLE | |
321 | result = make_sized_incremental_binary<32, false>( | |
322 | file, elfcpp::Ehdr<32, false>(p)); | |
323 | #else | |
324 | explain_no_incremental(_("unsupported file: 32-bit, little-endian")); | |
325 | #endif | |
326 | } | |
327 | } | |
328 | else if (size == 64) | |
329 | { | |
330 | if (big_endian) | |
331 | { | |
332 | #ifdef HAVE_TARGET_64_BIG | |
333 | result = make_sized_incremental_binary<64, true>( | |
334 | file, elfcpp::Ehdr<64, true>(p)); | |
335 | #else | |
336 | explain_no_incremental(_("unsupported file: 64-bit, big-endian")); | |
337 | #endif | |
338 | } | |
339 | else | |
340 | { | |
341 | #ifdef HAVE_TARGET_64_LITTLE | |
342 | result = make_sized_incremental_binary<64, false>( | |
343 | file, elfcpp::Ehdr<64, false>(p)); | |
344 | #else | |
345 | explain_no_incremental(_("unsupported file: 64-bit, little-endian")); | |
346 | #endif | |
347 | } | |
348 | } | |
349 | else | |
350 | gold_unreachable(); | |
351 | ||
352 | return result; | |
353 | } | |
354 | ||
44453f85 ILT |
355 | // Analyzes the output file to check if incremental linking is possible and |
356 | // (to be done) what files need to be relinked. | |
357 | ||
358 | bool | |
359 | Incremental_checker::can_incrementally_link_output_file() | |
360 | { | |
361 | Output_file output(this->output_name_); | |
362 | if (!output.open_for_modification()) | |
363 | return false; | |
c549a694 ILT |
364 | Incremental_binary* binary = open_incremental_binary(&output); |
365 | if (binary == NULL) | |
366 | return false; | |
c4aa1e2d | 367 | return binary->check_inputs(this->incremental_inputs_); |
44453f85 ILT |
368 | } |
369 | ||
09ec0418 CC |
370 | // Class Incremental_inputs. |
371 | ||
3ce2c28e ILT |
372 | // Add the command line to the string table, setting |
373 | // command_line_key_. In incremental builds, the command line is | |
374 | // stored in .gnu_incremental_inputs so that the next linker run can | |
375 | // check if the command line options didn't change. | |
376 | ||
377 | void | |
378 | Incremental_inputs::report_command_line(int argc, const char* const* argv) | |
379 | { | |
380 | // Always store 'gold' as argv[0] to avoid a full relink if the user used a | |
381 | // different path to the linker. | |
382 | std::string args("gold"); | |
383 | // Copied from collect_argv in main.cc. | |
384 | for (int i = 1; i < argc; ++i) | |
385 | { | |
09ec0418 | 386 | // Adding/removing these options should not result in a full relink. |
b19b0c6d ILT |
387 | if (strcmp(argv[i], "--incremental-changed") == 0 |
388 | || strcmp(argv[i], "--incremental-unchanged") == 0 | |
389 | || strcmp(argv[i], "--incremental-unknown") == 0) | |
390 | continue; | |
391 | ||
3ce2c28e ILT |
392 | args.append(" '"); |
393 | // Now append argv[i], but with all single-quotes escaped | |
394 | const char* argpos = argv[i]; | |
395 | while (1) | |
396 | { | |
397 | const int len = strcspn(argpos, "'"); | |
398 | args.append(argpos, len); | |
399 | if (argpos[len] == '\0') | |
400 | break; | |
401 | args.append("'\"'\"'"); | |
402 | argpos += len + 1; | |
403 | } | |
404 | args.append("'"); | |
405 | } | |
c4aa1e2d ILT |
406 | |
407 | this->command_line_ = args; | |
408 | this->strtab_->add(this->command_line_.c_str(), false, | |
409 | &this->command_line_key_); | |
3ce2c28e ILT |
410 | } |
411 | ||
09ec0418 CC |
412 | // Record the input archive file ARCHIVE. This is called by the |
413 | // Add_archive_symbols task before determining which archive members | |
414 | // to include. We create the Incremental_archive_entry here and | |
415 | // attach it to the Archive, but we do not add it to the list of | |
416 | // input objects until report_archive_end is called. | |
072fe7ce ILT |
417 | |
418 | void | |
09ec0418 | 419 | Incremental_inputs::report_archive_begin(Archive* arch) |
072fe7ce | 420 | { |
09ec0418 CC |
421 | Stringpool::Key filename_key; |
422 | Timespec mtime = arch->file().get_mtime(); | |
072fe7ce | 423 | |
09ec0418 CC |
424 | this->strtab_->add(arch->filename().c_str(), false, &filename_key); |
425 | Incremental_archive_entry* entry = | |
426 | new Incremental_archive_entry(filename_key, arch, mtime); | |
427 | arch->set_incremental_info(entry); | |
072fe7ce ILT |
428 | } |
429 | ||
09ec0418 CC |
430 | // Finish recording the input archive file ARCHIVE. This is called by the |
431 | // Add_archive_symbols task after determining which archive members | |
432 | // to include. | |
072fe7ce ILT |
433 | |
434 | void | |
09ec0418 | 435 | Incremental_inputs::report_archive_end(Archive* arch) |
072fe7ce | 436 | { |
09ec0418 CC |
437 | Incremental_archive_entry* entry = arch->incremental_info(); |
438 | ||
439 | gold_assert(entry != NULL); | |
440 | ||
441 | // Collect unused global symbols. | |
442 | for (Archive::Unused_symbol_iterator p = arch->unused_symbols_begin(); | |
443 | p != arch->unused_symbols_end(); | |
444 | ++p) | |
445 | { | |
446 | Stringpool::Key symbol_key; | |
447 | this->strtab_->add(*p, true, &symbol_key); | |
448 | entry->add_unused_global_symbol(symbol_key); | |
449 | } | |
450 | this->inputs_.push_back(entry); | |
072fe7ce ILT |
451 | } |
452 | ||
09ec0418 CC |
453 | // Record the input object file OBJ. If ARCH is not NULL, attach |
454 | // the object file to the archive. This is called by the | |
455 | // Add_symbols task after finding out the type of the file. | |
072fe7ce ILT |
456 | |
457 | void | |
09ec0418 | 458 | Incremental_inputs::report_object(Object* obj, Archive* arch) |
072fe7ce | 459 | { |
09ec0418 CC |
460 | Stringpool::Key filename_key; |
461 | Timespec mtime = obj->input_file()->file().get_mtime(); | |
462 | ||
463 | this->strtab_->add(obj->name().c_str(), false, &filename_key); | |
464 | Incremental_object_entry* obj_entry = | |
465 | new Incremental_object_entry(filename_key, obj, mtime); | |
466 | this->inputs_.push_back(obj_entry); | |
072fe7ce | 467 | |
09ec0418 CC |
468 | if (arch != NULL) |
469 | { | |
470 | Incremental_archive_entry* arch_entry = arch->incremental_info(); | |
471 | gold_assert(arch_entry != NULL); | |
472 | arch_entry->add_object(obj_entry); | |
473 | } | |
474 | ||
475 | this->current_object_ = obj; | |
476 | this->current_object_entry_ = obj_entry; | |
072fe7ce ILT |
477 | } |
478 | ||
09ec0418 CC |
479 | // Record the input object file OBJ. If ARCH is not NULL, attach |
480 | // the object file to the archive. This is called by the | |
481 | // Add_symbols task after finding out the type of the file. | |
072fe7ce ILT |
482 | |
483 | void | |
09ec0418 CC |
484 | Incremental_inputs::report_input_section(Object* obj, unsigned int shndx, |
485 | const char* name, off_t sh_size) | |
072fe7ce | 486 | { |
09ec0418 | 487 | Stringpool::Key key = 0; |
072fe7ce | 488 | |
09ec0418 CC |
489 | if (name != NULL) |
490 | this->strtab_->add(name, true, &key); | |
491 | ||
492 | gold_assert(obj == this->current_object_); | |
493 | this->current_object_entry_->add_input_section(shndx, key, sh_size); | |
494 | } | |
495 | ||
496 | // Record that the input argument INPUT is a script SCRIPT. This is | |
497 | // called by read_script after parsing the script and reading the list | |
498 | // of inputs added by this script. | |
499 | ||
500 | void | |
501 | Incremental_inputs::report_script(const std::string& filename, | |
502 | Script_info* script, Timespec mtime) | |
503 | { | |
504 | Stringpool::Key filename_key; | |
505 | ||
506 | this->strtab_->add(filename.c_str(), false, &filename_key); | |
507 | Incremental_script_entry* entry = | |
508 | new Incremental_script_entry(filename_key, script, mtime); | |
509 | this->inputs_.push_back(entry); | |
072fe7ce ILT |
510 | } |
511 | ||
3ce2c28e ILT |
512 | // Finalize the incremental link information. Called from |
513 | // Layout::finalize. | |
514 | ||
515 | void | |
516 | Incremental_inputs::finalize() | |
517 | { | |
09ec0418 | 518 | // Finalize the string table. |
3ce2c28e ILT |
519 | this->strtab_->set_string_offsets(); |
520 | } | |
521 | ||
09ec0418 | 522 | // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections. |
3ce2c28e | 523 | |
09ec0418 CC |
524 | void |
525 | Incremental_inputs::create_data_sections(Symbol_table* symtab) | |
3ce2c28e ILT |
526 | { |
527 | switch (parameters->size_and_endianness()) | |
528 | { | |
529 | #ifdef HAVE_TARGET_32_LITTLE | |
530 | case Parameters::TARGET_32_LITTLE: | |
09ec0418 CC |
531 | this->inputs_section_ = |
532 | new Output_section_incremental_inputs<32, false>(this, symtab); | |
533 | break; | |
3ce2c28e ILT |
534 | #endif |
535 | #ifdef HAVE_TARGET_32_BIG | |
536 | case Parameters::TARGET_32_BIG: | |
09ec0418 CC |
537 | this->inputs_section_ = |
538 | new Output_section_incremental_inputs<32, true>(this, symtab); | |
539 | break; | |
3ce2c28e ILT |
540 | #endif |
541 | #ifdef HAVE_TARGET_64_LITTLE | |
542 | case Parameters::TARGET_64_LITTLE: | |
09ec0418 CC |
543 | this->inputs_section_ = |
544 | new Output_section_incremental_inputs<64, false>(this, symtab); | |
545 | break; | |
3ce2c28e ILT |
546 | #endif |
547 | #ifdef HAVE_TARGET_64_BIG | |
548 | case Parameters::TARGET_64_BIG: | |
09ec0418 CC |
549 | this->inputs_section_ = |
550 | new Output_section_incremental_inputs<64, true>(this, symtab); | |
551 | break; | |
3ce2c28e ILT |
552 | #endif |
553 | default: | |
554 | gold_unreachable(); | |
072fe7ce | 555 | } |
09ec0418 CC |
556 | this->symtab_section_ = new Output_data_space(4, "** incremental_symtab"); |
557 | this->relocs_section_ = new Output_data_space(4, "** incremental_relocs"); | |
3ce2c28e ILT |
558 | } |
559 | ||
09ec0418 CC |
560 | // Return the sh_entsize value for the .gnu_incremental_relocs section. |
561 | unsigned int | |
562 | Incremental_inputs::relocs_entsize() const | |
563 | { | |
564 | return 8 + 2 * parameters->target().get_size() / 8; | |
565 | } | |
566 | ||
567 | // Class Output_section_incremental_inputs. | |
568 | ||
569 | // Finalize the offsets for each input section and supplemental info block, | |
570 | // and set the final data size of the incremental output sections. | |
3ce2c28e ILT |
571 | |
572 | template<int size, bool big_endian> | |
09ec0418 CC |
573 | void |
574 | Output_section_incremental_inputs<size, big_endian>::set_final_data_size() | |
072fe7ce | 575 | { |
09ec0418 CC |
576 | const Incremental_inputs* inputs = this->inputs_; |
577 | const unsigned int sizeof_addr = size / 8; | |
578 | const unsigned int rel_size = 8 + 2 * sizeof_addr; | |
579 | ||
580 | // Offset of each input entry. | |
581 | unsigned int input_offset = this->header_size; | |
582 | ||
583 | // Offset of each supplemental info block. | |
584 | unsigned int info_offset = this->header_size; | |
585 | info_offset += this->input_entry_size * inputs->input_file_count(); | |
586 | ||
587 | // Count each input file and its supplemental information block. | |
588 | for (Incremental_inputs::Input_list::const_iterator p = | |
589 | inputs->input_files().begin(); | |
590 | p != inputs->input_files().end(); | |
591 | ++p) | |
072fe7ce | 592 | { |
09ec0418 CC |
593 | // Set the offset of the input file entry. |
594 | (*p)->set_offset(input_offset); | |
595 | input_offset += this->input_entry_size; | |
596 | ||
597 | // Set the offset of the supplemental info block. | |
598 | switch ((*p)->type()) | |
599 | { | |
600 | case INCREMENTAL_INPUT_SCRIPT: | |
601 | // No supplemental info for a script. | |
602 | (*p)->set_info_offset(0); | |
603 | break; | |
604 | case INCREMENTAL_INPUT_OBJECT: | |
605 | case INCREMENTAL_INPUT_ARCHIVE_MEMBER: | |
606 | { | |
607 | Incremental_object_entry *entry = (*p)->object_entry(); | |
608 | gold_assert(entry != NULL); | |
609 | (*p)->set_info_offset(info_offset); | |
610 | // Input section count + global symbol count. | |
611 | info_offset += 8; | |
612 | // Each input section. | |
613 | info_offset += (entry->get_input_section_count() | |
614 | * (8 + 2 * sizeof_addr)); | |
615 | // Each global symbol. | |
616 | const Object::Symbols* syms = entry->object()->get_global_symbols(); | |
617 | info_offset += syms->size() * 16; | |
618 | } | |
619 | break; | |
620 | case INCREMENTAL_INPUT_SHARED_LIBRARY: | |
621 | { | |
622 | Incremental_object_entry *entry = (*p)->object_entry(); | |
623 | gold_assert(entry != NULL); | |
624 | (*p)->set_info_offset(info_offset); | |
625 | // Global symbol count. | |
626 | info_offset += 4; | |
627 | // Each global symbol. | |
628 | const Object::Symbols* syms = entry->object()->get_global_symbols(); | |
629 | unsigned int nsyms = syms != NULL ? syms->size() : 0; | |
630 | info_offset += nsyms * 4; | |
631 | } | |
632 | break; | |
633 | case INCREMENTAL_INPUT_ARCHIVE: | |
634 | { | |
635 | Incremental_archive_entry *entry = (*p)->archive_entry(); | |
636 | gold_assert(entry != NULL); | |
637 | (*p)->set_info_offset(info_offset); | |
638 | // Member count + unused global symbol count. | |
639 | info_offset += 8; | |
640 | // Each member. | |
641 | info_offset += (entry->get_member_count() * 4); | |
642 | // Each global symbol. | |
643 | info_offset += (entry->get_unused_global_symbol_count() * 4); | |
644 | } | |
645 | break; | |
646 | default: | |
647 | gold_unreachable(); | |
648 | } | |
072fe7ce ILT |
649 | } |
650 | ||
09ec0418 CC |
651 | this->set_data_size(info_offset); |
652 | ||
653 | // Set the size of the .gnu_incremental_symtab section. | |
654 | inputs->symtab_section()->set_current_data_size(this->symtab_->output_count() | |
655 | * sizeof(unsigned int)); | |
656 | ||
657 | // Set the size of the .gnu_incremental_relocs section. | |
658 | inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count() | |
659 | * rel_size); | |
660 | } | |
661 | ||
662 | // Write the contents of the .gnu_incremental_inputs and | |
663 | // .gnu_incremental_symtab sections. | |
664 | ||
665 | template<int size, bool big_endian> | |
666 | void | |
667 | Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of) | |
668 | { | |
669 | const Incremental_inputs* inputs = this->inputs_; | |
670 | Stringpool* strtab = inputs->get_stringpool(); | |
671 | ||
672 | // Get a view into the .gnu_incremental_inputs section. | |
673 | const off_t off = this->offset(); | |
674 | const off_t oview_size = this->data_size(); | |
675 | unsigned char* const oview = of->get_output_view(off, oview_size); | |
676 | unsigned char* pov = oview; | |
677 | ||
678 | // Get a view into the .gnu_incremental_symtab section. | |
679 | const off_t symtab_off = inputs->symtab_section()->offset(); | |
680 | const off_t symtab_size = inputs->symtab_section()->data_size(); | |
681 | unsigned char* const symtab_view = of->get_output_view(symtab_off, | |
682 | symtab_size); | |
683 | ||
684 | // Allocate an array of linked list heads for the .gnu_incremental_symtab | |
685 | // section. Each element corresponds to a global symbol in the output | |
686 | // symbol table, and points to the head of the linked list that threads | |
687 | // through the object file input entries. The value of each element | |
688 | // is the section-relative offset to a global symbol entry in a | |
689 | // supplemental information block. | |
690 | unsigned int global_sym_count = this->symtab_->output_count(); | |
691 | unsigned int* global_syms = new unsigned int[global_sym_count]; | |
692 | memset(global_syms, 0, global_sym_count * sizeof(unsigned int)); | |
693 | ||
694 | // Write the section header. | |
695 | Stringpool::Key command_line_key = inputs->command_line_key(); | |
696 | pov = this->write_header(pov, inputs->input_file_count(), | |
697 | strtab->get_offset_from_key(command_line_key)); | |
698 | ||
699 | // Write the list of input files. | |
700 | pov = this->write_input_files(oview, pov, strtab); | |
701 | ||
702 | // Write the supplemental information blocks for each input file. | |
703 | pov = this->write_info_blocks(oview, pov, strtab, global_syms, | |
704 | global_sym_count); | |
705 | ||
706 | gold_assert(pov - oview == oview_size); | |
707 | ||
708 | // Write the .gnu_incremental_symtab section. | |
709 | gold_assert(global_sym_count * 4 == symtab_size); | |
710 | this->write_symtab(symtab_view, global_syms, global_sym_count); | |
711 | ||
712 | delete[] global_syms; | |
713 | ||
714 | of->write_output_view(off, oview_size, oview); | |
715 | of->write_output_view(symtab_off, symtab_size, symtab_view); | |
716 | } | |
717 | ||
718 | // Write the section header: version, input file count, offset of command line | |
719 | // in the string table, and 4 bytes of padding. | |
720 | ||
721 | template<int size, bool big_endian> | |
722 | unsigned char* | |
723 | Output_section_incremental_inputs<size, big_endian>::write_header( | |
724 | unsigned char* pov, | |
725 | unsigned int input_file_count, | |
726 | section_offset_type command_line_offset) | |
727 | { | |
728 | Swap32::writeval(pov, INCREMENTAL_LINK_VERSION); | |
729 | Swap32::writeval(pov + 4, input_file_count); | |
730 | Swap32::writeval(pov + 8, command_line_offset); | |
731 | Swap32::writeval(pov + 12, 0); | |
732 | return pov + this->header_size; | |
733 | } | |
734 | ||
735 | // Write the input file entries. | |
736 | ||
737 | template<int size, bool big_endian> | |
738 | unsigned char* | |
739 | Output_section_incremental_inputs<size, big_endian>::write_input_files( | |
740 | unsigned char* oview, | |
741 | unsigned char* pov, | |
742 | Stringpool* strtab) | |
743 | { | |
744 | const Incremental_inputs* inputs = this->inputs_; | |
745 | ||
746 | for (Incremental_inputs::Input_list::const_iterator p = | |
747 | inputs->input_files().begin(); | |
748 | p != inputs->input_files().end(); | |
749 | ++p) | |
750 | { | |
751 | gold_assert(pov - oview == (*p)->get_offset()); | |
752 | section_offset_type filename_offset = | |
753 | strtab->get_offset_from_key((*p)->get_filename_key()); | |
754 | const Timespec& mtime = (*p)->get_mtime(); | |
755 | Swap32::writeval(pov, filename_offset); | |
756 | Swap32::writeval(pov + 4, (*p)->get_info_offset()); | |
757 | Swap64::writeval(pov + 8, mtime.seconds); | |
758 | Swap32::writeval(pov + 16, mtime.nanoseconds); | |
759 | Swap16::writeval(pov + 20, (*p)->type()); | |
760 | Swap16::writeval(pov + 22, 0); | |
761 | pov += this->input_entry_size; | |
762 | } | |
763 | return pov; | |
764 | } | |
765 | ||
766 | // Write the supplemental information blocks. | |
767 | ||
768 | template<int size, bool big_endian> | |
769 | unsigned char* | |
770 | Output_section_incremental_inputs<size, big_endian>::write_info_blocks( | |
771 | unsigned char* oview, | |
772 | unsigned char* pov, | |
773 | Stringpool* strtab, | |
774 | unsigned int* global_syms, | |
775 | unsigned int global_sym_count) | |
776 | { | |
777 | const Incremental_inputs* inputs = this->inputs_; | |
778 | unsigned int first_global_index = this->symtab_->first_global_index(); | |
779 | ||
780 | for (Incremental_inputs::Input_list::const_iterator p = | |
781 | inputs->input_files().begin(); | |
782 | p != inputs->input_files().end(); | |
783 | ++p) | |
784 | { | |
785 | switch ((*p)->type()) | |
786 | { | |
787 | case INCREMENTAL_INPUT_SCRIPT: | |
788 | // No supplemental info for a script. | |
789 | break; | |
790 | ||
791 | case INCREMENTAL_INPUT_OBJECT: | |
792 | case INCREMENTAL_INPUT_ARCHIVE_MEMBER: | |
793 | { | |
794 | gold_assert(pov - oview == (*p)->get_info_offset()); | |
795 | Incremental_object_entry* entry = (*p)->object_entry(); | |
796 | gold_assert(entry != NULL); | |
797 | const Object* obj = entry->object(); | |
798 | const Object::Symbols* syms = obj->get_global_symbols(); | |
799 | // Write the input section count and global symbol count. | |
800 | unsigned int nsections = entry->get_input_section_count(); | |
801 | unsigned int nsyms = syms->size(); | |
802 | Swap32::writeval(pov, nsections); | |
803 | Swap32::writeval(pov + 4, nsyms); | |
804 | pov += 8; | |
805 | ||
806 | // For each input section, write the name, output section index, | |
807 | // offset within output section, and input section size. | |
808 | for (unsigned int i = 0; i < nsections; i++) | |
809 | { | |
810 | Stringpool::Key key = entry->get_input_section_name_key(i); | |
811 | off_t name_offset = 0; | |
812 | if (key != 0) | |
813 | name_offset = strtab->get_offset_from_key(key); | |
814 | int out_shndx = 0; | |
815 | off_t out_offset = 0; | |
816 | off_t sh_size = 0; | |
817 | Output_section* os = obj->output_section(i); | |
818 | if (os != NULL) | |
819 | { | |
820 | out_shndx = os->out_shndx(); | |
821 | out_offset = obj->output_section_offset(i); | |
822 | sh_size = entry->get_input_section_size(i); | |
823 | } | |
824 | Swap32::writeval(pov, name_offset); | |
825 | Swap32::writeval(pov + 4, out_shndx); | |
826 | Swap::writeval(pov + 8, out_offset); | |
827 | Swap::writeval(pov + 8 + sizeof_addr, sh_size); | |
828 | pov += 8 + 2 * sizeof_addr; | |
829 | } | |
830 | ||
831 | // For each global symbol, write its associated relocations, | |
832 | // add it to the linked list of globals, then write the | |
833 | // supplemental information: global symbol table index, | |
834 | // linked list chain pointer, relocation count, and offset | |
835 | // to the relocations. | |
836 | for (unsigned int i = 0; i < nsyms; i++) | |
837 | { | |
838 | const Symbol* sym = (*syms)[i]; | |
839 | unsigned int symtab_index = sym->symtab_index(); | |
840 | unsigned int chain = 0; | |
841 | unsigned int first_reloc = 0; | |
842 | unsigned int nrelocs = obj->get_incremental_reloc_count(i); | |
843 | if (nrelocs > 0) | |
844 | { | |
845 | gold_assert(symtab_index != -1U | |
846 | && (symtab_index - first_global_index | |
847 | < global_sym_count)); | |
848 | first_reloc = obj->get_incremental_reloc_base(i); | |
849 | chain = global_syms[symtab_index - first_global_index]; | |
850 | global_syms[symtab_index - first_global_index] = | |
851 | pov - oview; | |
852 | } | |
853 | Swap32::writeval(pov, symtab_index); | |
854 | Swap32::writeval(pov + 4, chain); | |
855 | Swap32::writeval(pov + 8, nrelocs); | |
856 | Swap32::writeval(pov + 12, first_reloc * 3 * sizeof_addr); | |
857 | pov += 16; | |
858 | } | |
859 | } | |
860 | break; | |
861 | ||
862 | case INCREMENTAL_INPUT_SHARED_LIBRARY: | |
863 | { | |
864 | gold_assert(pov - oview == (*p)->get_info_offset()); | |
865 | Incremental_object_entry* entry = (*p)->object_entry(); | |
866 | gold_assert(entry != NULL); | |
867 | const Object* obj = entry->object(); | |
868 | const Object::Symbols* syms = obj->get_global_symbols(); | |
869 | ||
870 | // Write the global symbol count. | |
871 | unsigned int nsyms = syms != NULL ? syms->size() : 0; | |
872 | Swap32::writeval(pov, nsyms); | |
873 | pov += 4; | |
874 | ||
875 | // For each global symbol, write the global symbol table index. | |
876 | for (unsigned int i = 0; i < nsyms; i++) | |
877 | { | |
878 | const Symbol* sym = (*syms)[i]; | |
879 | Swap32::writeval(pov, sym->symtab_index()); | |
880 | pov += 4; | |
881 | } | |
882 | } | |
883 | break; | |
884 | ||
885 | case INCREMENTAL_INPUT_ARCHIVE: | |
886 | { | |
887 | gold_assert(pov - oview == (*p)->get_info_offset()); | |
888 | Incremental_archive_entry* entry = (*p)->archive_entry(); | |
889 | gold_assert(entry != NULL); | |
890 | ||
891 | // Write the member count and unused global symbol count. | |
892 | unsigned int nmembers = entry->get_member_count(); | |
893 | unsigned int nsyms = entry->get_unused_global_symbol_count(); | |
894 | Swap32::writeval(pov, nmembers); | |
895 | Swap32::writeval(pov + 4, nsyms); | |
896 | pov += 8; | |
897 | ||
898 | // For each member, write the offset to its input file entry. | |
899 | for (unsigned int i = 0; i < nmembers; ++i) | |
900 | { | |
901 | Incremental_object_entry* member = entry->get_member(i); | |
902 | Swap32::writeval(pov, member->get_offset()); | |
903 | pov += 4; | |
904 | } | |
905 | ||
906 | // For each global symbol, write the name offset. | |
907 | for (unsigned int i = 0; i < nsyms; ++i) | |
908 | { | |
909 | Stringpool::Key key = entry->get_unused_global_symbol(i); | |
910 | Swap32::writeval(pov, strtab->get_offset_from_key(key)); | |
911 | pov += 4; | |
912 | } | |
913 | } | |
914 | break; | |
915 | ||
916 | default: | |
917 | gold_unreachable(); | |
918 | } | |
919 | } | |
920 | return pov; | |
921 | } | |
922 | ||
923 | // Write the contents of the .gnu_incremental_symtab section. | |
924 | ||
925 | template<int size, bool big_endian> | |
926 | void | |
927 | Output_section_incremental_inputs<size, big_endian>::write_symtab( | |
928 | unsigned char* pov, | |
929 | unsigned int* global_syms, | |
930 | unsigned int global_sym_count) | |
931 | { | |
932 | for (unsigned int i = 0; i < global_sym_count; ++i) | |
933 | { | |
934 | Swap32::writeval(pov, global_syms[i]); | |
935 | pov += 4; | |
936 | } | |
3ce2c28e ILT |
937 | } |
938 | ||
c549a694 ILT |
939 | // Instantiate the templates we need. |
940 | ||
941 | #ifdef HAVE_TARGET_32_LITTLE | |
942 | template | |
943 | class Sized_incremental_binary<32, false>; | |
944 | #endif | |
945 | ||
946 | #ifdef HAVE_TARGET_32_BIG | |
947 | template | |
948 | class Sized_incremental_binary<32, true>; | |
949 | #endif | |
950 | ||
951 | #ifdef HAVE_TARGET_64_LITTLE | |
952 | template | |
953 | class Sized_incremental_binary<64, false>; | |
954 | #endif | |
955 | ||
956 | #ifdef HAVE_TARGET_64_BIG | |
957 | template | |
958 | class Sized_incremental_binary<64, true>; | |
959 | #endif | |
960 | ||
0e879927 | 961 | } // End namespace gold. |