]>
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" |
0e70b911 | 35 | #include "target.h" |
0e879927 | 36 | |
0e879927 ILT |
37 | namespace gold { |
38 | ||
39 | // Version information. Will change frequently during the development, later | |
40 | // we could think about backward (and forward?) compatibility. | |
c4aa1e2d | 41 | const unsigned int INCREMENTAL_LINK_VERSION = 1; |
0e879927 | 42 | |
09ec0418 CC |
43 | // This class manages the .gnu_incremental_inputs section, which holds |
44 | // the header information, a directory of input files, and separate | |
45 | // entries for each input file. | |
46 | ||
47 | template<int size, bool big_endian> | |
48 | class Output_section_incremental_inputs : public Output_section_data | |
49 | { | |
50 | public: | |
51 | Output_section_incremental_inputs(const Incremental_inputs* inputs, | |
52 | const Symbol_table* symtab) | |
53 | : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab) | |
54 | { } | |
55 | ||
56 | protected: | |
57 | // Set the final data size. | |
58 | void | |
59 | set_final_data_size(); | |
60 | ||
61 | // Write the data to the file. | |
62 | void | |
63 | do_write(Output_file*); | |
64 | ||
65 | // Write to a map file. | |
66 | void | |
67 | do_print_to_mapfile(Mapfile* mapfile) const | |
68 | { mapfile->print_output_data(this, _("** incremental_inputs")); } | |
69 | ||
70 | private: | |
71 | // Write the section header. | |
72 | unsigned char* | |
73 | write_header(unsigned char* pov, unsigned int input_file_count, | |
74 | section_offset_type command_line_offset); | |
75 | ||
76 | // Write the input file entries. | |
77 | unsigned char* | |
78 | write_input_files(unsigned char* oview, unsigned char* pov, | |
79 | Stringpool* strtab); | |
80 | ||
81 | // Write the supplemental information blocks. | |
82 | unsigned char* | |
83 | write_info_blocks(unsigned char* oview, unsigned char* pov, | |
84 | Stringpool* strtab, unsigned int* global_syms, | |
85 | unsigned int global_sym_count); | |
86 | ||
87 | // Write the contents of the .gnu_incremental_symtab section. | |
88 | void | |
89 | write_symtab(unsigned char* pov, unsigned int* global_syms, | |
90 | unsigned int global_sym_count); | |
91 | ||
0e70b911 CC |
92 | // Write the contents of the .gnu_incremental_got_plt section. |
93 | void | |
94 | write_got_plt(unsigned char* pov, off_t view_size); | |
95 | ||
09ec0418 CC |
96 | // Typedefs for writing the data to the output sections. |
97 | typedef elfcpp::Swap<size, big_endian> Swap; | |
98 | typedef elfcpp::Swap<16, big_endian> Swap16; | |
99 | typedef elfcpp::Swap<32, big_endian> Swap32; | |
100 | typedef elfcpp::Swap<64, big_endian> Swap64; | |
101 | ||
102 | // Sizes of various structures. | |
103 | static const int sizeof_addr = size / 8; | |
104 | static const int header_size = 16; | |
105 | static const int input_entry_size = 24; | |
106 | ||
107 | // The Incremental_inputs object. | |
108 | const Incremental_inputs* inputs_; | |
109 | ||
110 | // The symbol table. | |
111 | const Symbol_table* symtab_; | |
112 | }; | |
113 | ||
c549a694 ILT |
114 | // Inform the user why we don't do an incremental link. Not called in |
115 | // the obvious case of missing output file. TODO: Is this helpful? | |
116 | ||
117 | void | |
118 | vexplain_no_incremental(const char* format, va_list args) | |
119 | { | |
120 | char* buf = NULL; | |
121 | if (vasprintf(&buf, format, args) < 0) | |
122 | gold_nomem(); | |
123 | gold_info(_("the link might take longer: " | |
124 | "cannot perform incremental link: %s"), buf); | |
125 | free(buf); | |
126 | } | |
127 | ||
128 | void | |
129 | explain_no_incremental(const char* format, ...) | |
130 | { | |
131 | va_list args; | |
132 | va_start(args, format); | |
133 | vexplain_no_incremental(format, args); | |
134 | va_end(args); | |
135 | } | |
136 | ||
137 | // Report an error. | |
138 | ||
139 | void | |
140 | Incremental_binary::error(const char* format, ...) const | |
141 | { | |
142 | va_list args; | |
143 | va_start(args, format); | |
144 | // Current code only checks if the file can be used for incremental linking, | |
145 | // so errors shouldn't fail the build, but only result in a fallback to a | |
146 | // full build. | |
147 | // TODO: when we implement incremental editing of the file, we may need a | |
148 | // flag that will cause errors to be treated seriously. | |
149 | vexplain_no_incremental(format, args); | |
150 | va_end(args); | |
151 | } | |
152 | ||
09ec0418 CC |
153 | // Find the .gnu_incremental_inputs section and related sections. |
154 | ||
c549a694 ILT |
155 | template<int size, bool big_endian> |
156 | bool | |
09ec0418 CC |
157 | Sized_incremental_binary<size, big_endian>::do_find_incremental_inputs_sections( |
158 | unsigned int* p_inputs_shndx, | |
159 | unsigned int* p_symtab_shndx, | |
160 | unsigned int* p_relocs_shndx, | |
0e70b911 | 161 | unsigned int* p_got_plt_shndx, |
09ec0418 | 162 | unsigned int* p_strtab_shndx) |
c549a694 | 163 | { |
09ec0418 CC |
164 | unsigned int inputs_shndx = |
165 | this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS); | |
166 | if (inputs_shndx == elfcpp::SHN_UNDEF) // Not found. | |
167 | return false; | |
168 | ||
169 | unsigned int symtab_shndx = | |
170 | this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB); | |
171 | if (symtab_shndx == elfcpp::SHN_UNDEF) // Not found. | |
172 | return false; | |
173 | if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx) | |
c549a694 | 174 | return false; |
09ec0418 CC |
175 | |
176 | unsigned int relocs_shndx = | |
177 | this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS); | |
178 | if (relocs_shndx == elfcpp::SHN_UNDEF) // Not found. | |
179 | return false; | |
180 | if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx) | |
181 | return false; | |
182 | ||
0e70b911 CC |
183 | unsigned int got_plt_shndx = |
184 | this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT); | |
185 | if (got_plt_shndx == elfcpp::SHN_UNDEF) // Not found. | |
186 | return false; | |
187 | if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx) | |
188 | return false; | |
189 | ||
09ec0418 CC |
190 | unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx); |
191 | if (strtab_shndx == elfcpp::SHN_UNDEF | |
192 | || strtab_shndx > this->elf_file_.shnum() | |
193 | || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB) | |
194 | return false; | |
195 | ||
196 | if (p_inputs_shndx != NULL) | |
197 | *p_inputs_shndx = inputs_shndx; | |
198 | if (p_symtab_shndx != NULL) | |
199 | *p_symtab_shndx = symtab_shndx; | |
200 | if (p_relocs_shndx != NULL) | |
201 | *p_relocs_shndx = relocs_shndx; | |
0e70b911 CC |
202 | if (p_got_plt_shndx != NULL) |
203 | *p_got_plt_shndx = got_plt_shndx; | |
09ec0418 CC |
204 | if (p_strtab_shndx != NULL) |
205 | *p_strtab_shndx = strtab_shndx; | |
c549a694 ILT |
206 | return true; |
207 | } | |
208 | ||
09ec0418 CC |
209 | // Determine whether an incremental link based on the existing output file |
210 | // can be done. | |
211 | ||
c4aa1e2d ILT |
212 | template<int size, bool big_endian> |
213 | bool | |
214 | Sized_incremental_binary<size, big_endian>::do_check_inputs( | |
215 | Incremental_inputs* incremental_inputs) | |
216 | { | |
09ec0418 CC |
217 | unsigned int inputs_shndx; |
218 | unsigned int symtab_shndx; | |
219 | unsigned int relocs_shndx; | |
0e70b911 | 220 | unsigned int plt_got_shndx; |
c4aa1e2d | 221 | unsigned int strtab_shndx; |
c4aa1e2d | 222 | |
09ec0418 | 223 | if (!do_find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx, |
0e70b911 CC |
224 | &relocs_shndx, &plt_got_shndx, |
225 | &strtab_shndx)) | |
c4aa1e2d ILT |
226 | { |
227 | explain_no_incremental(_("no incremental data from previous build")); | |
228 | return false; | |
229 | } | |
c4aa1e2d | 230 | |
09ec0418 CC |
231 | Location inputs_location(this->elf_file_.section_contents(inputs_shndx)); |
232 | Location symtab_location(this->elf_file_.section_contents(symtab_shndx)); | |
233 | Location relocs_location(this->elf_file_.section_contents(relocs_shndx)); | |
c4aa1e2d | 234 | Location strtab_location(this->elf_file_.section_contents(strtab_shndx)); |
09ec0418 CC |
235 | |
236 | View inputs_view(view(inputs_location)); | |
237 | View symtab_view(view(symtab_location)); | |
238 | View relocs_view(view(relocs_location)); | |
c4aa1e2d | 239 | View strtab_view(view(strtab_location)); |
09ec0418 | 240 | |
c4aa1e2d | 241 | elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size); |
c4aa1e2d | 242 | |
09ec0418 CC |
243 | Incremental_inputs_reader<size, big_endian> |
244 | incoming_inputs(inputs_view.data(), strtab); | |
c4aa1e2d | 245 | |
09ec0418 | 246 | if (incoming_inputs.version() != INCREMENTAL_LINK_VERSION) |
c4aa1e2d | 247 | { |
09ec0418 | 248 | explain_no_incremental(_("different version of incremental build data")); |
c4aa1e2d ILT |
249 | return false; |
250 | } | |
251 | ||
09ec0418 | 252 | if (incremental_inputs->command_line() != incoming_inputs.command_line()) |
c4aa1e2d ILT |
253 | { |
254 | explain_no_incremental(_("command line changed")); | |
255 | return false; | |
256 | } | |
257 | ||
258 | // TODO: compare incremental_inputs->inputs() with entries in data_view. | |
09ec0418 | 259 | |
c4aa1e2d ILT |
260 | return true; |
261 | } | |
262 | ||
c549a694 ILT |
263 | namespace |
264 | { | |
265 | ||
266 | // Create a Sized_incremental_binary object of the specified size and | |
267 | // endianness. Fails if the target architecture is not supported. | |
268 | ||
269 | template<int size, bool big_endian> | |
270 | Incremental_binary* | |
271 | make_sized_incremental_binary(Output_file* file, | |
272 | const elfcpp::Ehdr<size, big_endian>& ehdr) | |
273 | { | |
274 | Target* target = select_target(ehdr.get_e_machine(), size, big_endian, | |
275 | ehdr.get_e_ident()[elfcpp::EI_OSABI], | |
276 | ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]); | |
277 | if (target == NULL) | |
278 | { | |
279 | explain_no_incremental(_("unsupported ELF machine number %d"), | |
280 | ehdr.get_e_machine()); | |
281 | return NULL; | |
282 | } | |
283 | ||
3aec4f9c RÁE |
284 | if (!parameters->target_valid()) |
285 | set_parameters_target(target); | |
286 | else if (target != ¶meters->target()) | |
287 | gold_error(_("%s: incompatible target"), file->filename()); | |
288 | ||
c549a694 ILT |
289 | return new Sized_incremental_binary<size, big_endian>(file, ehdr, target); |
290 | } | |
291 | ||
292 | } // End of anonymous namespace. | |
293 | ||
09ec0418 CC |
294 | // Create an Incremental_binary object for FILE. Returns NULL is this is not |
295 | // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE | |
c549a694 ILT |
296 | // should be opened. |
297 | ||
298 | Incremental_binary* | |
299 | open_incremental_binary(Output_file* file) | |
300 | { | |
301 | off_t filesize = file->filesize(); | |
302 | int want = elfcpp::Elf_recognizer::max_header_size; | |
303 | if (filesize < want) | |
304 | want = filesize; | |
305 | ||
306 | const unsigned char* p = file->get_input_view(0, want); | |
307 | if (!elfcpp::Elf_recognizer::is_elf_file(p, want)) | |
308 | { | |
309 | explain_no_incremental(_("output is not an ELF file.")); | |
310 | return NULL; | |
311 | } | |
312 | ||
ac33a407 DK |
313 | int size = 0; |
314 | bool big_endian = false; | |
c549a694 ILT |
315 | std::string error; |
316 | if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian, | |
317 | &error)) | |
318 | { | |
319 | explain_no_incremental(error.c_str()); | |
320 | return NULL; | |
321 | } | |
322 | ||
323 | Incremental_binary* result = NULL; | |
324 | if (size == 32) | |
325 | { | |
326 | if (big_endian) | |
327 | { | |
328 | #ifdef HAVE_TARGET_32_BIG | |
329 | result = make_sized_incremental_binary<32, true>( | |
330 | file, elfcpp::Ehdr<32, true>(p)); | |
331 | #else | |
332 | explain_no_incremental(_("unsupported file: 32-bit, big-endian")); | |
333 | #endif | |
334 | } | |
335 | else | |
336 | { | |
337 | #ifdef HAVE_TARGET_32_LITTLE | |
338 | result = make_sized_incremental_binary<32, false>( | |
339 | file, elfcpp::Ehdr<32, false>(p)); | |
340 | #else | |
341 | explain_no_incremental(_("unsupported file: 32-bit, little-endian")); | |
342 | #endif | |
343 | } | |
344 | } | |
345 | else if (size == 64) | |
346 | { | |
347 | if (big_endian) | |
348 | { | |
349 | #ifdef HAVE_TARGET_64_BIG | |
350 | result = make_sized_incremental_binary<64, true>( | |
351 | file, elfcpp::Ehdr<64, true>(p)); | |
352 | #else | |
353 | explain_no_incremental(_("unsupported file: 64-bit, big-endian")); | |
354 | #endif | |
355 | } | |
356 | else | |
357 | { | |
358 | #ifdef HAVE_TARGET_64_LITTLE | |
359 | result = make_sized_incremental_binary<64, false>( | |
360 | file, elfcpp::Ehdr<64, false>(p)); | |
361 | #else | |
362 | explain_no_incremental(_("unsupported file: 64-bit, little-endian")); | |
363 | #endif | |
364 | } | |
365 | } | |
366 | else | |
367 | gold_unreachable(); | |
368 | ||
369 | return result; | |
370 | } | |
371 | ||
44453f85 ILT |
372 | // Analyzes the output file to check if incremental linking is possible and |
373 | // (to be done) what files need to be relinked. | |
374 | ||
375 | bool | |
376 | Incremental_checker::can_incrementally_link_output_file() | |
377 | { | |
378 | Output_file output(this->output_name_); | |
379 | if (!output.open_for_modification()) | |
380 | return false; | |
c549a694 ILT |
381 | Incremental_binary* binary = open_incremental_binary(&output); |
382 | if (binary == NULL) | |
383 | return false; | |
c4aa1e2d | 384 | return binary->check_inputs(this->incremental_inputs_); |
44453f85 ILT |
385 | } |
386 | ||
09ec0418 CC |
387 | // Class Incremental_inputs. |
388 | ||
3ce2c28e ILT |
389 | // Add the command line to the string table, setting |
390 | // command_line_key_. In incremental builds, the command line is | |
391 | // stored in .gnu_incremental_inputs so that the next linker run can | |
392 | // check if the command line options didn't change. | |
393 | ||
394 | void | |
395 | Incremental_inputs::report_command_line(int argc, const char* const* argv) | |
396 | { | |
397 | // Always store 'gold' as argv[0] to avoid a full relink if the user used a | |
398 | // different path to the linker. | |
399 | std::string args("gold"); | |
400 | // Copied from collect_argv in main.cc. | |
401 | for (int i = 1; i < argc; ++i) | |
402 | { | |
09ec0418 | 403 | // Adding/removing these options should not result in a full relink. |
8c21d9d3 CC |
404 | if (strcmp(argv[i], "--incremental") == 0 |
405 | || strcmp(argv[i], "--incremental-full") == 0 | |
406 | || strcmp(argv[i], "--incremental-update") == 0 | |
407 | || strcmp(argv[i], "--incremental-changed") == 0 | |
b19b0c6d ILT |
408 | || strcmp(argv[i], "--incremental-unchanged") == 0 |
409 | || strcmp(argv[i], "--incremental-unknown") == 0) | |
410 | continue; | |
411 | ||
3ce2c28e ILT |
412 | args.append(" '"); |
413 | // Now append argv[i], but with all single-quotes escaped | |
414 | const char* argpos = argv[i]; | |
415 | while (1) | |
416 | { | |
417 | const int len = strcspn(argpos, "'"); | |
418 | args.append(argpos, len); | |
419 | if (argpos[len] == '\0') | |
420 | break; | |
421 | args.append("'\"'\"'"); | |
422 | argpos += len + 1; | |
423 | } | |
424 | args.append("'"); | |
425 | } | |
c4aa1e2d ILT |
426 | |
427 | this->command_line_ = args; | |
428 | this->strtab_->add(this->command_line_.c_str(), false, | |
429 | &this->command_line_key_); | |
3ce2c28e ILT |
430 | } |
431 | ||
09ec0418 CC |
432 | // Record the input archive file ARCHIVE. This is called by the |
433 | // Add_archive_symbols task before determining which archive members | |
434 | // to include. We create the Incremental_archive_entry here and | |
435 | // attach it to the Archive, but we do not add it to the list of | |
436 | // input objects until report_archive_end is called. | |
072fe7ce ILT |
437 | |
438 | void | |
c7975edd CC |
439 | Incremental_inputs::report_archive_begin(Library_base* arch, |
440 | Script_info* script_info) | |
072fe7ce | 441 | { |
09ec0418 | 442 | Stringpool::Key filename_key; |
e0c52780 | 443 | Timespec mtime = arch->get_mtime(); |
072fe7ce | 444 | |
09ec0418 CC |
445 | this->strtab_->add(arch->filename().c_str(), false, &filename_key); |
446 | Incremental_archive_entry* entry = | |
e0c52780 | 447 | new Incremental_archive_entry(filename_key, mtime); |
09ec0418 | 448 | arch->set_incremental_info(entry); |
e0c52780 | 449 | this->inputs_.push_back(entry); |
c7975edd CC |
450 | |
451 | if (script_info != NULL) | |
452 | { | |
453 | Incremental_script_entry* script_entry = script_info->incremental_info(); | |
454 | gold_assert(script_entry != NULL); | |
455 | script_entry->add_object(entry); | |
456 | } | |
072fe7ce ILT |
457 | } |
458 | ||
e0c52780 CC |
459 | // Visitor class for processing the unused global symbols in a library. |
460 | // An instance of this class is passed to the library's | |
461 | // for_all_unused_symbols() iterator, which will call the visit() | |
462 | // function for each global symbol defined in each unused library | |
463 | // member. We add those symbol names to the incremental info for the | |
464 | // library. | |
465 | ||
466 | class Unused_symbol_visitor : public Library_base::Symbol_visitor_base | |
467 | { | |
468 | public: | |
469 | Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab) | |
470 | : entry_(entry), strtab_(strtab) | |
471 | { } | |
472 | ||
473 | void | |
474 | visit(const char* sym) | |
475 | { | |
476 | Stringpool::Key symbol_key; | |
477 | this->strtab_->add(sym, true, &symbol_key); | |
478 | this->entry_->add_unused_global_symbol(symbol_key); | |
479 | } | |
480 | ||
481 | private: | |
482 | Incremental_archive_entry* entry_; | |
483 | Stringpool* strtab_; | |
484 | }; | |
485 | ||
09ec0418 CC |
486 | // Finish recording the input archive file ARCHIVE. This is called by the |
487 | // Add_archive_symbols task after determining which archive members | |
488 | // to include. | |
072fe7ce ILT |
489 | |
490 | void | |
e0c52780 | 491 | Incremental_inputs::report_archive_end(Library_base* arch) |
072fe7ce | 492 | { |
09ec0418 CC |
493 | Incremental_archive_entry* entry = arch->incremental_info(); |
494 | ||
495 | gold_assert(entry != NULL); | |
496 | ||
497 | // Collect unused global symbols. | |
e0c52780 CC |
498 | Unused_symbol_visitor v(entry, this->strtab_); |
499 | arch->for_all_unused_symbols(&v); | |
072fe7ce ILT |
500 | } |
501 | ||
09ec0418 CC |
502 | // Record the input object file OBJ. If ARCH is not NULL, attach |
503 | // the object file to the archive. This is called by the | |
504 | // Add_symbols task after finding out the type of the file. | |
072fe7ce ILT |
505 | |
506 | void | |
c7975edd CC |
507 | Incremental_inputs::report_object(Object* obj, Library_base* arch, |
508 | Script_info* script_info) | |
072fe7ce | 509 | { |
09ec0418 CC |
510 | Stringpool::Key filename_key; |
511 | Timespec mtime = obj->input_file()->file().get_mtime(); | |
512 | ||
513 | this->strtab_->add(obj->name().c_str(), false, &filename_key); | |
514 | Incremental_object_entry* obj_entry = | |
515 | new Incremental_object_entry(filename_key, obj, mtime); | |
516 | this->inputs_.push_back(obj_entry); | |
072fe7ce | 517 | |
09ec0418 CC |
518 | if (arch != NULL) |
519 | { | |
520 | Incremental_archive_entry* arch_entry = arch->incremental_info(); | |
521 | gold_assert(arch_entry != NULL); | |
522 | arch_entry->add_object(obj_entry); | |
523 | } | |
524 | ||
c7975edd CC |
525 | if (script_info != NULL) |
526 | { | |
527 | Incremental_script_entry* script_entry = script_info->incremental_info(); | |
528 | gold_assert(script_entry != NULL); | |
529 | script_entry->add_object(obj_entry); | |
530 | } | |
531 | ||
09ec0418 CC |
532 | this->current_object_ = obj; |
533 | this->current_object_entry_ = obj_entry; | |
072fe7ce ILT |
534 | } |
535 | ||
09ec0418 CC |
536 | // Record the input object file OBJ. If ARCH is not NULL, attach |
537 | // the object file to the archive. This is called by the | |
538 | // Add_symbols task after finding out the type of the file. | |
072fe7ce ILT |
539 | |
540 | void | |
09ec0418 CC |
541 | Incremental_inputs::report_input_section(Object* obj, unsigned int shndx, |
542 | const char* name, off_t sh_size) | |
072fe7ce | 543 | { |
09ec0418 | 544 | Stringpool::Key key = 0; |
072fe7ce | 545 | |
09ec0418 CC |
546 | if (name != NULL) |
547 | this->strtab_->add(name, true, &key); | |
548 | ||
549 | gold_assert(obj == this->current_object_); | |
550 | this->current_object_entry_->add_input_section(shndx, key, sh_size); | |
551 | } | |
552 | ||
553 | // Record that the input argument INPUT is a script SCRIPT. This is | |
554 | // called by read_script after parsing the script and reading the list | |
555 | // of inputs added by this script. | |
556 | ||
557 | void | |
558 | Incremental_inputs::report_script(const std::string& filename, | |
559 | Script_info* script, Timespec mtime) | |
560 | { | |
561 | Stringpool::Key filename_key; | |
562 | ||
563 | this->strtab_->add(filename.c_str(), false, &filename_key); | |
564 | Incremental_script_entry* entry = | |
565 | new Incremental_script_entry(filename_key, script, mtime); | |
566 | this->inputs_.push_back(entry); | |
c7975edd | 567 | script->set_incremental_info(entry); |
072fe7ce ILT |
568 | } |
569 | ||
3ce2c28e ILT |
570 | // Finalize the incremental link information. Called from |
571 | // Layout::finalize. | |
572 | ||
573 | void | |
574 | Incremental_inputs::finalize() | |
575 | { | |
09ec0418 | 576 | // Finalize the string table. |
3ce2c28e ILT |
577 | this->strtab_->set_string_offsets(); |
578 | } | |
579 | ||
09ec0418 | 580 | // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections. |
3ce2c28e | 581 | |
09ec0418 CC |
582 | void |
583 | Incremental_inputs::create_data_sections(Symbol_table* symtab) | |
3ce2c28e ILT |
584 | { |
585 | switch (parameters->size_and_endianness()) | |
586 | { | |
587 | #ifdef HAVE_TARGET_32_LITTLE | |
588 | case Parameters::TARGET_32_LITTLE: | |
09ec0418 CC |
589 | this->inputs_section_ = |
590 | new Output_section_incremental_inputs<32, false>(this, symtab); | |
591 | break; | |
3ce2c28e ILT |
592 | #endif |
593 | #ifdef HAVE_TARGET_32_BIG | |
594 | case Parameters::TARGET_32_BIG: | |
09ec0418 CC |
595 | this->inputs_section_ = |
596 | new Output_section_incremental_inputs<32, true>(this, symtab); | |
597 | break; | |
3ce2c28e ILT |
598 | #endif |
599 | #ifdef HAVE_TARGET_64_LITTLE | |
600 | case Parameters::TARGET_64_LITTLE: | |
09ec0418 CC |
601 | this->inputs_section_ = |
602 | new Output_section_incremental_inputs<64, false>(this, symtab); | |
603 | break; | |
3ce2c28e ILT |
604 | #endif |
605 | #ifdef HAVE_TARGET_64_BIG | |
606 | case Parameters::TARGET_64_BIG: | |
09ec0418 CC |
607 | this->inputs_section_ = |
608 | new Output_section_incremental_inputs<64, true>(this, symtab); | |
609 | break; | |
3ce2c28e ILT |
610 | #endif |
611 | default: | |
612 | gold_unreachable(); | |
072fe7ce | 613 | } |
09ec0418 CC |
614 | this->symtab_section_ = new Output_data_space(4, "** incremental_symtab"); |
615 | this->relocs_section_ = new Output_data_space(4, "** incremental_relocs"); | |
0e70b911 | 616 | this->got_plt_section_ = new Output_data_space(4, "** incremental_got_plt"); |
3ce2c28e ILT |
617 | } |
618 | ||
09ec0418 CC |
619 | // Return the sh_entsize value for the .gnu_incremental_relocs section. |
620 | unsigned int | |
621 | Incremental_inputs::relocs_entsize() const | |
622 | { | |
623 | return 8 + 2 * parameters->target().get_size() / 8; | |
624 | } | |
625 | ||
626 | // Class Output_section_incremental_inputs. | |
627 | ||
628 | // Finalize the offsets for each input section and supplemental info block, | |
629 | // and set the final data size of the incremental output sections. | |
3ce2c28e ILT |
630 | |
631 | template<int size, bool big_endian> | |
09ec0418 CC |
632 | void |
633 | Output_section_incremental_inputs<size, big_endian>::set_final_data_size() | |
072fe7ce | 634 | { |
09ec0418 CC |
635 | const Incremental_inputs* inputs = this->inputs_; |
636 | const unsigned int sizeof_addr = size / 8; | |
637 | const unsigned int rel_size = 8 + 2 * sizeof_addr; | |
638 | ||
639 | // Offset of each input entry. | |
640 | unsigned int input_offset = this->header_size; | |
641 | ||
642 | // Offset of each supplemental info block. | |
643 | unsigned int info_offset = this->header_size; | |
644 | info_offset += this->input_entry_size * inputs->input_file_count(); | |
645 | ||
646 | // Count each input file and its supplemental information block. | |
647 | for (Incremental_inputs::Input_list::const_iterator p = | |
648 | inputs->input_files().begin(); | |
649 | p != inputs->input_files().end(); | |
650 | ++p) | |
072fe7ce | 651 | { |
09ec0418 CC |
652 | // Set the offset of the input file entry. |
653 | (*p)->set_offset(input_offset); | |
654 | input_offset += this->input_entry_size; | |
655 | ||
656 | // Set the offset of the supplemental info block. | |
657 | switch ((*p)->type()) | |
658 | { | |
659 | case INCREMENTAL_INPUT_SCRIPT: | |
c7975edd CC |
660 | { |
661 | Incremental_script_entry *entry = (*p)->script_entry(); | |
662 | gold_assert(entry != NULL); | |
663 | (*p)->set_info_offset(info_offset); | |
664 | // Object count. | |
665 | info_offset += 4; | |
666 | // Each member. | |
667 | info_offset += (entry->get_object_count() * 4); | |
668 | } | |
09ec0418 CC |
669 | break; |
670 | case INCREMENTAL_INPUT_OBJECT: | |
671 | case INCREMENTAL_INPUT_ARCHIVE_MEMBER: | |
672 | { | |
ca09d69a | 673 | Incremental_object_entry* entry = (*p)->object_entry(); |
09ec0418 CC |
674 | gold_assert(entry != NULL); |
675 | (*p)->set_info_offset(info_offset); | |
676 | // Input section count + global symbol count. | |
677 | info_offset += 8; | |
678 | // Each input section. | |
679 | info_offset += (entry->get_input_section_count() | |
680 | * (8 + 2 * sizeof_addr)); | |
681 | // Each global symbol. | |
682 | const Object::Symbols* syms = entry->object()->get_global_symbols(); | |
683 | info_offset += syms->size() * 16; | |
684 | } | |
685 | break; | |
686 | case INCREMENTAL_INPUT_SHARED_LIBRARY: | |
687 | { | |
ca09d69a | 688 | Incremental_object_entry* entry = (*p)->object_entry(); |
09ec0418 CC |
689 | gold_assert(entry != NULL); |
690 | (*p)->set_info_offset(info_offset); | |
691 | // Global symbol count. | |
692 | info_offset += 4; | |
693 | // Each global symbol. | |
694 | const Object::Symbols* syms = entry->object()->get_global_symbols(); | |
695 | unsigned int nsyms = syms != NULL ? syms->size() : 0; | |
696 | info_offset += nsyms * 4; | |
697 | } | |
698 | break; | |
699 | case INCREMENTAL_INPUT_ARCHIVE: | |
700 | { | |
ca09d69a | 701 | Incremental_archive_entry* entry = (*p)->archive_entry(); |
09ec0418 CC |
702 | gold_assert(entry != NULL); |
703 | (*p)->set_info_offset(info_offset); | |
704 | // Member count + unused global symbol count. | |
705 | info_offset += 8; | |
706 | // Each member. | |
707 | info_offset += (entry->get_member_count() * 4); | |
708 | // Each global symbol. | |
709 | info_offset += (entry->get_unused_global_symbol_count() * 4); | |
710 | } | |
711 | break; | |
712 | default: | |
713 | gold_unreachable(); | |
714 | } | |
072fe7ce ILT |
715 | } |
716 | ||
09ec0418 CC |
717 | this->set_data_size(info_offset); |
718 | ||
719 | // Set the size of the .gnu_incremental_symtab section. | |
720 | inputs->symtab_section()->set_current_data_size(this->symtab_->output_count() | |
721 | * sizeof(unsigned int)); | |
722 | ||
723 | // Set the size of the .gnu_incremental_relocs section. | |
724 | inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count() | |
725 | * rel_size); | |
0e70b911 CC |
726 | |
727 | // Set the size of the .gnu_incremental_got_plt section. | |
728 | Sized_target<size, big_endian>* target = | |
729 | parameters->sized_target<size, big_endian>(); | |
730 | unsigned int got_count = target->got_entry_count(); | |
731 | unsigned int plt_count = target->plt_entry_count(); | |
732 | unsigned int got_plt_size = 8; // GOT entry count, PLT entry count. | |
733 | got_plt_size = (got_plt_size + got_count + 3) & ~3; // GOT type array. | |
734 | got_plt_size += got_count * 4 + plt_count * 4; // GOT array, PLT array. | |
735 | inputs->got_plt_section()->set_current_data_size(got_plt_size); | |
09ec0418 CC |
736 | } |
737 | ||
738 | // Write the contents of the .gnu_incremental_inputs and | |
739 | // .gnu_incremental_symtab sections. | |
740 | ||
741 | template<int size, bool big_endian> | |
742 | void | |
743 | Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of) | |
744 | { | |
745 | const Incremental_inputs* inputs = this->inputs_; | |
746 | Stringpool* strtab = inputs->get_stringpool(); | |
747 | ||
748 | // Get a view into the .gnu_incremental_inputs section. | |
749 | const off_t off = this->offset(); | |
750 | const off_t oview_size = this->data_size(); | |
751 | unsigned char* const oview = of->get_output_view(off, oview_size); | |
752 | unsigned char* pov = oview; | |
753 | ||
754 | // Get a view into the .gnu_incremental_symtab section. | |
755 | const off_t symtab_off = inputs->symtab_section()->offset(); | |
756 | const off_t symtab_size = inputs->symtab_section()->data_size(); | |
757 | unsigned char* const symtab_view = of->get_output_view(symtab_off, | |
758 | symtab_size); | |
759 | ||
760 | // Allocate an array of linked list heads for the .gnu_incremental_symtab | |
761 | // section. Each element corresponds to a global symbol in the output | |
762 | // symbol table, and points to the head of the linked list that threads | |
763 | // through the object file input entries. The value of each element | |
764 | // is the section-relative offset to a global symbol entry in a | |
765 | // supplemental information block. | |
766 | unsigned int global_sym_count = this->symtab_->output_count(); | |
767 | unsigned int* global_syms = new unsigned int[global_sym_count]; | |
768 | memset(global_syms, 0, global_sym_count * sizeof(unsigned int)); | |
769 | ||
770 | // Write the section header. | |
771 | Stringpool::Key command_line_key = inputs->command_line_key(); | |
772 | pov = this->write_header(pov, inputs->input_file_count(), | |
773 | strtab->get_offset_from_key(command_line_key)); | |
774 | ||
775 | // Write the list of input files. | |
776 | pov = this->write_input_files(oview, pov, strtab); | |
777 | ||
778 | // Write the supplemental information blocks for each input file. | |
779 | pov = this->write_info_blocks(oview, pov, strtab, global_syms, | |
780 | global_sym_count); | |
781 | ||
782 | gold_assert(pov - oview == oview_size); | |
783 | ||
784 | // Write the .gnu_incremental_symtab section. | |
785 | gold_assert(global_sym_count * 4 == symtab_size); | |
786 | this->write_symtab(symtab_view, global_syms, global_sym_count); | |
787 | ||
788 | delete[] global_syms; | |
789 | ||
0e70b911 CC |
790 | // Write the .gnu_incremental_got_plt section. |
791 | const off_t got_plt_off = inputs->got_plt_section()->offset(); | |
792 | const off_t got_plt_size = inputs->got_plt_section()->data_size(); | |
793 | unsigned char* const got_plt_view = of->get_output_view(got_plt_off, | |
794 | got_plt_size); | |
795 | this->write_got_plt(got_plt_view, got_plt_size); | |
796 | ||
09ec0418 CC |
797 | of->write_output_view(off, oview_size, oview); |
798 | of->write_output_view(symtab_off, symtab_size, symtab_view); | |
0e70b911 | 799 | of->write_output_view(got_plt_off, got_plt_size, got_plt_view); |
09ec0418 CC |
800 | } |
801 | ||
802 | // Write the section header: version, input file count, offset of command line | |
803 | // in the string table, and 4 bytes of padding. | |
804 | ||
805 | template<int size, bool big_endian> | |
806 | unsigned char* | |
807 | Output_section_incremental_inputs<size, big_endian>::write_header( | |
808 | unsigned char* pov, | |
809 | unsigned int input_file_count, | |
810 | section_offset_type command_line_offset) | |
811 | { | |
812 | Swap32::writeval(pov, INCREMENTAL_LINK_VERSION); | |
813 | Swap32::writeval(pov + 4, input_file_count); | |
814 | Swap32::writeval(pov + 8, command_line_offset); | |
815 | Swap32::writeval(pov + 12, 0); | |
816 | return pov + this->header_size; | |
817 | } | |
818 | ||
819 | // Write the input file entries. | |
820 | ||
821 | template<int size, bool big_endian> | |
822 | unsigned char* | |
823 | Output_section_incremental_inputs<size, big_endian>::write_input_files( | |
824 | unsigned char* oview, | |
825 | unsigned char* pov, | |
826 | Stringpool* strtab) | |
827 | { | |
828 | const Incremental_inputs* inputs = this->inputs_; | |
829 | ||
830 | for (Incremental_inputs::Input_list::const_iterator p = | |
831 | inputs->input_files().begin(); | |
832 | p != inputs->input_files().end(); | |
833 | ++p) | |
834 | { | |
56f75c03 | 835 | gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset()); |
09ec0418 CC |
836 | section_offset_type filename_offset = |
837 | strtab->get_offset_from_key((*p)->get_filename_key()); | |
838 | const Timespec& mtime = (*p)->get_mtime(); | |
839 | Swap32::writeval(pov, filename_offset); | |
840 | Swap32::writeval(pov + 4, (*p)->get_info_offset()); | |
841 | Swap64::writeval(pov + 8, mtime.seconds); | |
842 | Swap32::writeval(pov + 16, mtime.nanoseconds); | |
843 | Swap16::writeval(pov + 20, (*p)->type()); | |
844 | Swap16::writeval(pov + 22, 0); | |
845 | pov += this->input_entry_size; | |
846 | } | |
847 | return pov; | |
848 | } | |
849 | ||
850 | // Write the supplemental information blocks. | |
851 | ||
852 | template<int size, bool big_endian> | |
853 | unsigned char* | |
854 | Output_section_incremental_inputs<size, big_endian>::write_info_blocks( | |
855 | unsigned char* oview, | |
856 | unsigned char* pov, | |
857 | Stringpool* strtab, | |
858 | unsigned int* global_syms, | |
859 | unsigned int global_sym_count) | |
860 | { | |
861 | const Incremental_inputs* inputs = this->inputs_; | |
862 | unsigned int first_global_index = this->symtab_->first_global_index(); | |
863 | ||
864 | for (Incremental_inputs::Input_list::const_iterator p = | |
865 | inputs->input_files().begin(); | |
866 | p != inputs->input_files().end(); | |
867 | ++p) | |
868 | { | |
869 | switch ((*p)->type()) | |
870 | { | |
871 | case INCREMENTAL_INPUT_SCRIPT: | |
c7975edd CC |
872 | { |
873 | gold_assert(static_cast<unsigned int>(pov - oview) | |
874 | == (*p)->get_info_offset()); | |
875 | Incremental_script_entry* entry = (*p)->script_entry(); | |
876 | gold_assert(entry != NULL); | |
877 | ||
878 | // Write the object count. | |
879 | unsigned int nobjects = entry->get_object_count(); | |
880 | Swap32::writeval(pov, nobjects); | |
881 | pov += 4; | |
882 | ||
883 | // For each object, write the offset to its input file entry. | |
884 | for (unsigned int i = 0; i < nobjects; ++i) | |
885 | { | |
886 | Incremental_input_entry* obj = entry->get_object(i); | |
887 | Swap32::writeval(pov, obj->get_offset()); | |
888 | pov += 4; | |
889 | } | |
890 | } | |
09ec0418 CC |
891 | break; |
892 | ||
893 | case INCREMENTAL_INPUT_OBJECT: | |
894 | case INCREMENTAL_INPUT_ARCHIVE_MEMBER: | |
895 | { | |
56f75c03 ILT |
896 | gold_assert(static_cast<unsigned int>(pov - oview) |
897 | == (*p)->get_info_offset()); | |
09ec0418 CC |
898 | Incremental_object_entry* entry = (*p)->object_entry(); |
899 | gold_assert(entry != NULL); | |
900 | const Object* obj = entry->object(); | |
901 | const Object::Symbols* syms = obj->get_global_symbols(); | |
902 | // Write the input section count and global symbol count. | |
903 | unsigned int nsections = entry->get_input_section_count(); | |
904 | unsigned int nsyms = syms->size(); | |
905 | Swap32::writeval(pov, nsections); | |
906 | Swap32::writeval(pov + 4, nsyms); | |
907 | pov += 8; | |
908 | ||
909 | // For each input section, write the name, output section index, | |
910 | // offset within output section, and input section size. | |
911 | for (unsigned int i = 0; i < nsections; i++) | |
912 | { | |
913 | Stringpool::Key key = entry->get_input_section_name_key(i); | |
914 | off_t name_offset = 0; | |
915 | if (key != 0) | |
916 | name_offset = strtab->get_offset_from_key(key); | |
917 | int out_shndx = 0; | |
918 | off_t out_offset = 0; | |
919 | off_t sh_size = 0; | |
920 | Output_section* os = obj->output_section(i); | |
921 | if (os != NULL) | |
922 | { | |
923 | out_shndx = os->out_shndx(); | |
924 | out_offset = obj->output_section_offset(i); | |
925 | sh_size = entry->get_input_section_size(i); | |
926 | } | |
927 | Swap32::writeval(pov, name_offset); | |
928 | Swap32::writeval(pov + 4, out_shndx); | |
929 | Swap::writeval(pov + 8, out_offset); | |
930 | Swap::writeval(pov + 8 + sizeof_addr, sh_size); | |
931 | pov += 8 + 2 * sizeof_addr; | |
932 | } | |
933 | ||
934 | // For each global symbol, write its associated relocations, | |
935 | // add it to the linked list of globals, then write the | |
936 | // supplemental information: global symbol table index, | |
937 | // linked list chain pointer, relocation count, and offset | |
938 | // to the relocations. | |
939 | for (unsigned int i = 0; i < nsyms; i++) | |
940 | { | |
941 | const Symbol* sym = (*syms)[i]; | |
793990de CC |
942 | if (sym->is_forwarder()) |
943 | sym = this->symtab_->resolve_forwards(sym); | |
09ec0418 CC |
944 | unsigned int symtab_index = sym->symtab_index(); |
945 | unsigned int chain = 0; | |
946 | unsigned int first_reloc = 0; | |
947 | unsigned int nrelocs = obj->get_incremental_reloc_count(i); | |
948 | if (nrelocs > 0) | |
949 | { | |
950 | gold_assert(symtab_index != -1U | |
951 | && (symtab_index - first_global_index | |
952 | < global_sym_count)); | |
953 | first_reloc = obj->get_incremental_reloc_base(i); | |
954 | chain = global_syms[symtab_index - first_global_index]; | |
955 | global_syms[symtab_index - first_global_index] = | |
956 | pov - oview; | |
957 | } | |
958 | Swap32::writeval(pov, symtab_index); | |
959 | Swap32::writeval(pov + 4, chain); | |
960 | Swap32::writeval(pov + 8, nrelocs); | |
961 | Swap32::writeval(pov + 12, first_reloc * 3 * sizeof_addr); | |
962 | pov += 16; | |
963 | } | |
964 | } | |
965 | break; | |
966 | ||
967 | case INCREMENTAL_INPUT_SHARED_LIBRARY: | |
968 | { | |
56f75c03 ILT |
969 | gold_assert(static_cast<unsigned int>(pov - oview) |
970 | == (*p)->get_info_offset()); | |
09ec0418 CC |
971 | Incremental_object_entry* entry = (*p)->object_entry(); |
972 | gold_assert(entry != NULL); | |
973 | const Object* obj = entry->object(); | |
974 | const Object::Symbols* syms = obj->get_global_symbols(); | |
975 | ||
976 | // Write the global symbol count. | |
977 | unsigned int nsyms = syms != NULL ? syms->size() : 0; | |
978 | Swap32::writeval(pov, nsyms); | |
979 | pov += 4; | |
980 | ||
981 | // For each global symbol, write the global symbol table index. | |
982 | for (unsigned int i = 0; i < nsyms; i++) | |
983 | { | |
984 | const Symbol* sym = (*syms)[i]; | |
985 | Swap32::writeval(pov, sym->symtab_index()); | |
986 | pov += 4; | |
987 | } | |
988 | } | |
989 | break; | |
990 | ||
991 | case INCREMENTAL_INPUT_ARCHIVE: | |
992 | { | |
56f75c03 ILT |
993 | gold_assert(static_cast<unsigned int>(pov - oview) |
994 | == (*p)->get_info_offset()); | |
09ec0418 CC |
995 | Incremental_archive_entry* entry = (*p)->archive_entry(); |
996 | gold_assert(entry != NULL); | |
997 | ||
998 | // Write the member count and unused global symbol count. | |
999 | unsigned int nmembers = entry->get_member_count(); | |
1000 | unsigned int nsyms = entry->get_unused_global_symbol_count(); | |
1001 | Swap32::writeval(pov, nmembers); | |
1002 | Swap32::writeval(pov + 4, nsyms); | |
1003 | pov += 8; | |
1004 | ||
1005 | // For each member, write the offset to its input file entry. | |
1006 | for (unsigned int i = 0; i < nmembers; ++i) | |
1007 | { | |
1008 | Incremental_object_entry* member = entry->get_member(i); | |
1009 | Swap32::writeval(pov, member->get_offset()); | |
1010 | pov += 4; | |
1011 | } | |
1012 | ||
1013 | // For each global symbol, write the name offset. | |
1014 | for (unsigned int i = 0; i < nsyms; ++i) | |
1015 | { | |
1016 | Stringpool::Key key = entry->get_unused_global_symbol(i); | |
1017 | Swap32::writeval(pov, strtab->get_offset_from_key(key)); | |
1018 | pov += 4; | |
1019 | } | |
1020 | } | |
1021 | break; | |
1022 | ||
1023 | default: | |
1024 | gold_unreachable(); | |
1025 | } | |
1026 | } | |
1027 | return pov; | |
1028 | } | |
1029 | ||
1030 | // Write the contents of the .gnu_incremental_symtab section. | |
1031 | ||
1032 | template<int size, bool big_endian> | |
1033 | void | |
1034 | Output_section_incremental_inputs<size, big_endian>::write_symtab( | |
1035 | unsigned char* pov, | |
1036 | unsigned int* global_syms, | |
1037 | unsigned int global_sym_count) | |
1038 | { | |
1039 | for (unsigned int i = 0; i < global_sym_count; ++i) | |
1040 | { | |
1041 | Swap32::writeval(pov, global_syms[i]); | |
1042 | pov += 4; | |
1043 | } | |
3ce2c28e ILT |
1044 | } |
1045 | ||
0e70b911 CC |
1046 | // This struct holds the view information needed to write the |
1047 | // .gnu_incremental_got_plt section. | |
1048 | ||
1049 | struct Got_plt_view_info | |
1050 | { | |
1051 | // Start of the GOT type array in the output view. | |
1052 | unsigned char* got_type_p; | |
1053 | // Start of the GOT descriptor array in the output view. | |
1054 | unsigned char* got_desc_p; | |
1055 | // Start of the PLT descriptor array in the output view. | |
1056 | unsigned char* plt_desc_p; | |
1057 | // Number of GOT entries. | |
1058 | unsigned int got_count; | |
1059 | // Number of PLT entries. | |
1060 | unsigned int plt_count; | |
1061 | // Offset of the first non-reserved PLT entry (this is a target-dependent value). | |
1062 | unsigned int first_plt_entry_offset; | |
1063 | // Size of a PLT entry (this is a target-dependent value). | |
1064 | unsigned int plt_entry_size; | |
1065 | // Value to write in the GOT descriptor array. For global symbols, | |
1066 | // this is the global symbol table index; for local symbols, it is | |
1067 | // the offset of the input file entry in the .gnu_incremental_inputs | |
1068 | // section. | |
1069 | unsigned int got_descriptor; | |
1070 | }; | |
1071 | ||
1072 | // Functor class for processing a GOT offset list for local symbols. | |
1073 | // Writes the GOT type and symbol index into the GOT type and descriptor | |
1074 | // arrays in the output section. | |
1075 | ||
1076 | template<int size, bool big_endian> | |
1077 | class Local_got_offset_visitor | |
1078 | { | |
1079 | public: | |
1080 | Local_got_offset_visitor(struct Got_plt_view_info& info) | |
1081 | : info_(info) | |
1082 | { } | |
1083 | ||
1084 | void | |
1085 | operator()(unsigned int got_type, unsigned int got_offset) | |
1086 | { | |
1087 | unsigned int got_index = got_offset / this->got_entry_size_; | |
1088 | gold_assert(got_index < this->info_.got_count); | |
1089 | // We can only handle GOT entry types in the range 0..0x7e | |
1090 | // because we use a byte array to store them, and we use the | |
1091 | // high bit to flag a local symbol. | |
1092 | gold_assert(got_type < 0x7f); | |
1093 | this->info_.got_type_p[got_index] = got_type | 0x80; | |
1094 | unsigned char* pov = this->info_.got_desc_p + got_index * 4; | |
1095 | elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.got_descriptor); | |
1096 | } | |
1097 | ||
1098 | private: | |
1099 | static const unsigned int got_entry_size_ = size / 8; | |
1100 | struct Got_plt_view_info& info_; | |
1101 | }; | |
1102 | ||
1103 | // Functor class for processing a GOT offset list. Writes the GOT type | |
1104 | // and symbol index into the GOT type and descriptor arrays in the output | |
1105 | // section. | |
1106 | ||
1107 | template<int size, bool big_endian> | |
1108 | class Global_got_offset_visitor | |
1109 | { | |
1110 | public: | |
1111 | Global_got_offset_visitor(struct Got_plt_view_info& info) | |
1112 | : info_(info) | |
1113 | { } | |
1114 | ||
1115 | void | |
1116 | operator()(unsigned int got_type, unsigned int got_offset) | |
1117 | { | |
1118 | unsigned int got_index = got_offset / this->got_entry_size_; | |
1119 | gold_assert(got_index < this->info_.got_count); | |
1120 | // We can only handle GOT entry types in the range 0..0x7e | |
1121 | // because we use a byte array to store them, and we use the | |
1122 | // high bit to flag a local symbol. | |
1123 | gold_assert(got_type < 0x7f); | |
1124 | this->info_.got_type_p[got_index] = got_type; | |
1125 | unsigned char* pov = this->info_.got_desc_p + got_index * 4; | |
1126 | elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.got_descriptor); | |
1127 | } | |
1128 | ||
1129 | private: | |
1130 | static const unsigned int got_entry_size_ = size / 8; | |
1131 | struct Got_plt_view_info& info_; | |
1132 | }; | |
1133 | ||
1134 | // Functor class for processing the global symbol table. Processes the | |
1135 | // GOT offset list for the symbol, and writes the symbol table index | |
1136 | // into the PLT descriptor array in the output section. | |
1137 | ||
1138 | template<int size, bool big_endian> | |
1139 | class Global_symbol_visitor_got_plt | |
1140 | { | |
1141 | public: | |
1142 | Global_symbol_visitor_got_plt(struct Got_plt_view_info& info) | |
1143 | : info_(info) | |
1144 | { } | |
1145 | ||
1146 | void | |
1147 | operator()(const Sized_symbol<size>* sym) | |
1148 | { | |
1149 | typedef Global_got_offset_visitor<size, big_endian> Got_visitor; | |
1150 | const Got_offset_list* got_offsets = sym->got_offset_list(); | |
1151 | if (got_offsets != NULL) | |
1152 | { | |
1153 | info_.got_descriptor = sym->symtab_index(); | |
1154 | got_offsets->for_all_got_offsets(Got_visitor(info_)); | |
1155 | } | |
1156 | if (sym->has_plt_offset()) | |
1157 | { | |
1158 | unsigned int plt_index = | |
1159 | ((sym->plt_offset() - this->info_.first_plt_entry_offset) | |
1160 | / this->info_.plt_entry_size); | |
1161 | gold_assert(plt_index < this->info_.plt_count); | |
1162 | unsigned char* pov = this->info_.plt_desc_p + plt_index * 4; | |
1163 | elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index()); | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | private: | |
1168 | struct Got_plt_view_info& info_; | |
1169 | }; | |
1170 | ||
1171 | // Write the contents of the .gnu_incremental_got_plt section. | |
1172 | ||
1173 | template<int size, bool big_endian> | |
1174 | void | |
1175 | Output_section_incremental_inputs<size, big_endian>::write_got_plt( | |
1176 | unsigned char* pov, | |
1177 | off_t view_size) | |
1178 | { | |
1179 | Sized_target<size, big_endian>* target = | |
1180 | parameters->sized_target<size, big_endian>(); | |
1181 | ||
1182 | // Set up the view information for the functors. | |
1183 | struct Got_plt_view_info view_info; | |
1184 | view_info.got_count = target->got_entry_count(); | |
1185 | view_info.plt_count = target->plt_entry_count(); | |
1186 | view_info.first_plt_entry_offset = target->first_plt_entry_offset(); | |
1187 | view_info.plt_entry_size = target->plt_entry_size(); | |
1188 | view_info.got_type_p = pov + 8; | |
1189 | view_info.got_desc_p = (view_info.got_type_p | |
1190 | + ((view_info.got_count + 3) & ~3)); | |
1191 | view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 4; | |
1192 | ||
1193 | gold_assert(pov + view_size == | |
1194 | view_info.plt_desc_p + view_info.plt_count * 4); | |
1195 | ||
1196 | // Write the section header. | |
1197 | Swap32::writeval(pov, view_info.got_count); | |
1198 | Swap32::writeval(pov + 4, view_info.plt_count); | |
1199 | ||
1200 | // Initialize the GOT type array to 0xff (reserved). | |
1201 | memset(view_info.got_type_p, 0xff, view_info.got_count); | |
1202 | ||
1203 | // Write the incremental GOT descriptors for local symbols. | |
1204 | for (Incremental_inputs::Input_list::const_iterator p = | |
1205 | this->inputs_->input_files().begin(); | |
1206 | p != this->inputs_->input_files().end(); | |
1207 | ++p) | |
1208 | { | |
1209 | if ((*p)->type() != INCREMENTAL_INPUT_OBJECT | |
1210 | && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER) | |
1211 | continue; | |
1212 | Incremental_object_entry* entry = (*p)->object_entry(); | |
1213 | gold_assert(entry != NULL); | |
1214 | const Sized_relobj<size, big_endian>* obj = | |
1215 | static_cast<Sized_relobj<size, big_endian>*>(entry->object()); | |
1216 | gold_assert(obj != NULL); | |
1217 | unsigned int nsyms = obj->local_symbol_count(); | |
1218 | for (unsigned int i = 0; i < nsyms; i++) | |
1219 | { | |
1220 | const Got_offset_list* got_offsets = obj->local_got_offset_list(i); | |
1221 | if (got_offsets != NULL) | |
1222 | { | |
1223 | typedef Local_got_offset_visitor<size, big_endian> Got_visitor; | |
1224 | view_info.got_descriptor = (*p)->get_offset(); | |
1225 | got_offsets->for_all_got_offsets(Got_visitor(view_info)); | |
1226 | } | |
1227 | } | |
1228 | } | |
1229 | ||
1230 | // Write the incremental GOT and PLT descriptors for global symbols. | |
1231 | typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor; | |
1232 | symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info)); | |
1233 | } | |
1234 | ||
c549a694 ILT |
1235 | // Instantiate the templates we need. |
1236 | ||
1237 | #ifdef HAVE_TARGET_32_LITTLE | |
1238 | template | |
1239 | class Sized_incremental_binary<32, false>; | |
1240 | #endif | |
1241 | ||
1242 | #ifdef HAVE_TARGET_32_BIG | |
1243 | template | |
1244 | class Sized_incremental_binary<32, true>; | |
1245 | #endif | |
1246 | ||
1247 | #ifdef HAVE_TARGET_64_LITTLE | |
1248 | template | |
1249 | class Sized_incremental_binary<64, false>; | |
1250 | #endif | |
1251 | ||
1252 | #ifdef HAVE_TARGET_64_BIG | |
1253 | template | |
1254 | class Sized_incremental_binary<64, true>; | |
1255 | #endif | |
1256 | ||
0e879927 | 1257 | } // End namespace gold. |