Commit | Line | Data |
---|---|---|
3ce2c28e ILT |
1 | // inremental.h -- incremental linking support for gold -*- C++ -*- |
2 | ||
09ec0418 | 3 | // Copyright 2009, 2010 Free Software Foundation, Inc. |
3ce2c28e 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 | #ifndef GOLD_INCREMENTAL_H | |
24 | #define GOLD_INCREMENTAL_H | |
25 | ||
072fe7ce | 26 | #include <map> |
3ce2c28e ILT |
27 | #include <vector> |
28 | ||
c549a694 | 29 | #include "elfcpp_file.h" |
3ce2c28e ILT |
30 | #include "stringpool.h" |
31 | #include "workqueue.h" | |
98fa85cb | 32 | #include "fileread.h" |
c549a694 | 33 | #include "output.h" |
3ce2c28e ILT |
34 | |
35 | namespace gold | |
36 | { | |
37 | ||
38 | class Archive; | |
39 | class Input_argument; | |
40 | class Incremental_inputs_checker; | |
09ec0418 CC |
41 | class Incremental_script_entry; |
42 | class Incremental_object_entry; | |
43 | class Incremental_archive_entry; | |
44 | class Incremental_inputs; | |
3ce2c28e | 45 | class Object; |
3ce2c28e | 46 | |
072fe7ce ILT |
47 | // Incremental input type as stored in .gnu_incremental_inputs. |
48 | ||
49 | enum Incremental_input_type | |
50 | { | |
072fe7ce | 51 | INCREMENTAL_INPUT_OBJECT = 1, |
09ec0418 CC |
52 | INCREMENTAL_INPUT_ARCHIVE_MEMBER = 2, |
53 | INCREMENTAL_INPUT_ARCHIVE = 3, | |
54 | INCREMENTAL_INPUT_SHARED_LIBRARY = 4, | |
55 | INCREMENTAL_INPUT_SCRIPT = 5 | |
ca55d848 RÁE |
56 | }; |
57 | ||
c549a694 ILT |
58 | // An object representing the ELF file we edit during an incremental build. |
59 | // Similar to Object or Dynobj, but operates on Output_file and contains | |
60 | // method specific to file edition (TBD). This is the abstract parent class | |
61 | // implemented in Sized_incremental_binary<size, big_endian> for a specific | |
62 | // endianness and size. | |
63 | ||
64 | class Incremental_binary | |
65 | { | |
66 | public: | |
67 | Incremental_binary(Output_file* output, Target* target) | |
68 | : output_(output), target_(target) | |
69 | { } | |
70 | ||
71 | virtual | |
72 | ~Incremental_binary() | |
73 | { } | |
74 | ||
75 | // Functions and types for the elfcpp::Elf_file interface. This | |
76 | // permit us to use Incremental_binary as the File template parameter for | |
77 | // elfcpp::Elf_file. | |
78 | ||
79 | // The View class is returned by view. It must support a single | |
80 | // method, data(). This is trivial, because Output_file::get_output_view | |
81 | // does what we need. | |
82 | class View | |
83 | { | |
84 | public: | |
85 | View(const unsigned char* p) | |
86 | : p_(p) | |
87 | { } | |
88 | ||
89 | const unsigned char* | |
90 | data() const | |
91 | { return this->p_; } | |
92 | ||
93 | private: | |
94 | const unsigned char* p_; | |
95 | }; | |
96 | ||
97 | // Return a View. | |
98 | View | |
99 | view(off_t file_offset, section_size_type data_size) | |
100 | { return View(this->output_->get_input_view(file_offset, data_size)); } | |
101 | ||
102 | // A location in the file. | |
103 | struct Location | |
104 | { | |
105 | off_t file_offset; | |
106 | off_t data_size; | |
107 | ||
108 | Location(off_t fo, section_size_type ds) | |
109 | : file_offset(fo), data_size(ds) | |
110 | { } | |
111 | ||
112 | Location() | |
113 | : file_offset(0), data_size(0) | |
114 | { } | |
115 | }; | |
116 | ||
117 | // Get a View given a Location. | |
09ec0418 CC |
118 | View |
119 | view(Location loc) | |
c549a694 ILT |
120 | { return View(this->view(loc.file_offset, loc.data_size)); } |
121 | ||
122 | // Report an error. | |
123 | void | |
124 | error(const char* format, ...) const ATTRIBUTE_PRINTF_2; | |
125 | ||
09ec0418 CC |
126 | // Find the .gnu_incremental_inputs and related sections. It selects the |
127 | // first section of type SHT_GNU_INCREMENTAL_INPUTS, | |
9b547ce6 | 128 | // SHT_GNU_INCREMENTAL_SYMTAB, and SHT_GNU_INCREMENTAL_RELOCS. |
09ec0418 | 129 | // Returns false if the sections are not found. |
c549a694 | 130 | bool |
09ec0418 CC |
131 | find_incremental_inputs_sections(unsigned int* p_inputs_shndx, |
132 | unsigned int* p_symtab_shndx, | |
133 | unsigned int* p_relocs_shndx, | |
0e70b911 | 134 | unsigned int* p_got_plt_shndx, |
09ec0418 CC |
135 | unsigned int* p_strtab_shndx) |
136 | { | |
137 | return do_find_incremental_inputs_sections(p_inputs_shndx, p_symtab_shndx, | |
0e70b911 CC |
138 | p_relocs_shndx, p_got_plt_shndx, |
139 | p_strtab_shndx); | |
09ec0418 | 140 | } |
c4aa1e2d ILT |
141 | |
142 | // Check the .gnu_incremental_inputs section to see whether an incremental | |
143 | // build is possible. | |
144 | // TODO: on success, should report what files needs to be rebuilt. | |
145 | // INCREMENTAL_INPUTS is used to read the canonical form of the command line | |
146 | // and read the input arguments. TODO: for items that don't need to be | |
147 | // rebuilt, we should also copy the incremental input information. | |
148 | virtual bool | |
149 | check_inputs(Incremental_inputs* incremental_inputs) | |
150 | { return do_check_inputs(incremental_inputs); } | |
c549a694 ILT |
151 | |
152 | protected: | |
153 | // Find incremental inputs section. | |
154 | virtual bool | |
09ec0418 CC |
155 | do_find_incremental_inputs_sections(unsigned int* p_inputs_shndx, |
156 | unsigned int* p_symtab_shndx, | |
157 | unsigned int* p_relocs_shndx, | |
0e70b911 | 158 | unsigned int* p_got_plt_shndx, |
09ec0418 | 159 | unsigned int* p_strtab_shndx) = 0; |
c4aa1e2d ILT |
160 | |
161 | // Check the .gnu_incremental_inputs section to see whether an incremental | |
162 | // build is possible. | |
163 | virtual bool | |
164 | do_check_inputs(Incremental_inputs* incremental_inputs) = 0; | |
c549a694 ILT |
165 | |
166 | private: | |
167 | // Edited output file object. | |
168 | Output_file* output_; | |
169 | // Target of the output file. | |
170 | Target* target_; | |
171 | }; | |
172 | ||
173 | template<int size, bool big_endian> | |
174 | class Sized_incremental_binary : public Incremental_binary | |
175 | { | |
176 | public: | |
177 | Sized_incremental_binary(Output_file* output, | |
178 | const elfcpp::Ehdr<size, big_endian>& ehdr, | |
179 | Target* target) | |
180 | : Incremental_binary(output, target), elf_file_(this, ehdr) | |
181 | { } | |
182 | ||
183 | protected: | |
184 | virtual bool | |
09ec0418 CC |
185 | do_find_incremental_inputs_sections(unsigned int* p_inputs_shndx, |
186 | unsigned int* p_symtab_shndx, | |
187 | unsigned int* p_relocs_shndx, | |
0e70b911 | 188 | unsigned int* p_got_plt_shndx, |
09ec0418 | 189 | unsigned int* p_strtab_shndx); |
c4aa1e2d ILT |
190 | |
191 | virtual bool | |
192 | do_check_inputs(Incremental_inputs* incremental_inputs); | |
c549a694 ILT |
193 | |
194 | private: | |
195 | // Output as an ELF file. | |
196 | elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_; | |
197 | }; | |
198 | ||
199 | // Create an Incremental_binary object for FILE. Returns NULL is this is not | |
200 | // possible, e.g. FILE is not an ELF file or has an unsupported target. | |
09ec0418 | 201 | |
c549a694 ILT |
202 | Incremental_binary* |
203 | open_incremental_binary(Output_file* file); | |
204 | ||
44453f85 ILT |
205 | // Code invoked early during an incremental link that checks what files need |
206 | // to be relinked. | |
09ec0418 | 207 | |
44453f85 ILT |
208 | class Incremental_checker |
209 | { | |
210 | public: | |
c4aa1e2d ILT |
211 | // Check if the file named OUTPUT_NAME can be linked incrementally. |
212 | // INCREMENTAL_INPUTS must have the canonical form of the command line | |
213 | // and input arguments filled - at this point of linking other fields are | |
214 | // probably not filled yet. TODO: for inputs that don't need to be | |
215 | // rebuilt, this function should fill the incremental input information. | |
216 | Incremental_checker(const char* output_name, | |
217 | Incremental_inputs* incremental_inputs) | |
218 | : output_name_(output_name), incremental_inputs_(incremental_inputs) | |
44453f85 ILT |
219 | { } |
220 | ||
221 | // Analyzes the output file to check if incremental linking is possible and | |
222 | // what files needs to be relinked. | |
223 | bool | |
224 | can_incrementally_link_output_file(); | |
225 | ||
226 | private: | |
c4aa1e2d | 227 | // Name of the output file to analyze. |
44453f85 | 228 | const char* output_name_; |
c4aa1e2d ILT |
229 | |
230 | // The Incremental_inputs object. At this stage of link, only the command | |
231 | // line and inputs are filled. | |
232 | Incremental_inputs* incremental_inputs_; | |
44453f85 ILT |
233 | }; |
234 | ||
09ec0418 CC |
235 | // Base class for recording each input file. |
236 | ||
237 | class Incremental_input_entry | |
238 | { | |
239 | public: | |
240 | Incremental_input_entry(Stringpool::Key filename_key, Timespec mtime) | |
241 | : filename_key_(filename_key), offset_(0), info_offset_(0), mtime_(mtime) | |
242 | { } | |
243 | ||
b56648ad ILT |
244 | virtual |
245 | ~Incremental_input_entry() | |
246 | { } | |
247 | ||
09ec0418 CC |
248 | // Return the type of input file. |
249 | Incremental_input_type | |
250 | type() const | |
251 | { return this->do_type(); } | |
252 | ||
253 | // Set the section offset of this input file entry. | |
254 | void | |
255 | set_offset(unsigned int offset) | |
256 | { this->offset_ = offset; } | |
257 | ||
258 | // Set the section offset of the supplemental information for this entry. | |
259 | void | |
260 | set_info_offset(unsigned int info_offset) | |
261 | { this->info_offset_ = info_offset; } | |
262 | ||
263 | // Get the section offset of this input file entry. | |
264 | unsigned int | |
265 | get_offset() const | |
266 | { return this->offset_; } | |
267 | ||
268 | // Get the section offset of the supplemental information for this entry. | |
269 | unsigned int | |
270 | get_info_offset() const | |
271 | { return this->info_offset_; } | |
272 | ||
273 | // Get the stringpool key for the input filename. | |
274 | Stringpool::Key | |
275 | get_filename_key() const | |
276 | { return this->filename_key_; } | |
277 | ||
278 | // Get the modification time of the input file. | |
279 | const Timespec& | |
280 | get_mtime() const | |
281 | { return this->mtime_; } | |
282 | ||
283 | // Return a pointer to the derived Incremental_script_entry object. | |
284 | // Return NULL for input entries that are not script files. | |
285 | Incremental_script_entry* | |
286 | script_entry() | |
287 | { return this->do_script_entry(); } | |
288 | ||
289 | // Return a pointer to the derived Incremental_object_entry object. | |
290 | // Return NULL for input entries that are not object files. | |
291 | Incremental_object_entry* | |
292 | object_entry() | |
293 | { return this->do_object_entry(); } | |
294 | ||
295 | // Return a pointer to the derived Incremental_archive_entry object. | |
296 | // Return NULL for input entries that are not archive files. | |
297 | Incremental_archive_entry* | |
298 | archive_entry() | |
299 | { return this->do_archive_entry(); } | |
300 | ||
301 | protected: | |
302 | // Return the type of input file. | |
303 | virtual Incremental_input_type | |
304 | do_type() const = 0; | |
305 | ||
306 | // Return a pointer to the derived Incremental_script_entry object. | |
307 | // Return NULL for input entries that are not script files. | |
308 | virtual Incremental_script_entry* | |
309 | do_script_entry() | |
310 | { return NULL; } | |
311 | ||
312 | // Return a pointer to the derived Incremental_object_entry object. | |
313 | // Return NULL for input entries that are not object files. | |
314 | virtual Incremental_object_entry* | |
315 | do_object_entry() | |
316 | { return NULL; } | |
317 | ||
318 | // Return a pointer to the derived Incremental_archive_entry object. | |
319 | // Return NULL for input entries that are not archive files. | |
320 | virtual Incremental_archive_entry* | |
321 | do_archive_entry() | |
322 | { return NULL; } | |
323 | ||
324 | private: | |
325 | // Key of the filename string in the section stringtable. | |
326 | Stringpool::Key filename_key_; | |
327 | ||
328 | // Offset of the entry in the output section. | |
329 | unsigned int offset_; | |
330 | ||
331 | // Offset of the extra information in the output section. | |
332 | unsigned int info_offset_; | |
333 | ||
334 | // Last modification time of the file. | |
335 | Timespec mtime_; | |
336 | }; | |
337 | ||
338 | // Class for recording input scripts. | |
339 | ||
340 | class Incremental_script_entry : public Incremental_input_entry | |
341 | { | |
342 | public: | |
343 | Incremental_script_entry(Stringpool::Key filename_key, Script_info* script, | |
344 | Timespec mtime) | |
345 | : Incremental_input_entry(filename_key, mtime), script_(script) | |
346 | { } | |
347 | ||
348 | protected: | |
349 | virtual Incremental_input_type | |
350 | do_type() const | |
351 | { return INCREMENTAL_INPUT_SCRIPT; } | |
352 | ||
353 | // Return a pointer to the derived Incremental_script_entry object. | |
354 | virtual Incremental_script_entry* | |
355 | do_script_entry() | |
356 | { return this; } | |
357 | ||
358 | private: | |
359 | // Information about the script file. | |
360 | Script_info* script_; | |
361 | }; | |
362 | ||
363 | // Class for recording input object files. | |
364 | ||
365 | class Incremental_object_entry : public Incremental_input_entry | |
366 | { | |
367 | public: | |
368 | Incremental_object_entry(Stringpool::Key filename_key, Object* obj, | |
369 | Timespec mtime) | |
370 | : Incremental_input_entry(filename_key, mtime), obj_(obj), | |
371 | is_member_(false), sections_() | |
372 | { | |
373 | if (!obj_->is_dynamic()) | |
374 | this->sections_.reserve(obj->shnum()); | |
375 | } | |
376 | ||
377 | // Get the object. | |
378 | Object* | |
379 | object() const | |
380 | { return this->obj_; } | |
381 | ||
382 | // Record that this object is an archive member. | |
383 | void | |
384 | set_is_member() | |
385 | { this->is_member_ = true; } | |
386 | ||
387 | // Return true if this object is an archive member. | |
388 | bool | |
389 | is_member() const | |
390 | { return this->is_member_; } | |
391 | ||
392 | // Add an input section. | |
393 | void | |
394 | add_input_section(unsigned int shndx, Stringpool::Key name_key, off_t sh_size) | |
395 | { | |
396 | if (shndx >= this->sections_.size()) | |
397 | this->sections_.resize(shndx + 1); | |
398 | this->sections_[shndx].name_key = name_key; | |
399 | this->sections_[shndx].sh_size = sh_size; | |
400 | } | |
401 | ||
402 | // Return the number of input sections in this object. | |
403 | unsigned int | |
404 | get_input_section_count() const | |
405 | { return this->sections_.size(); } | |
406 | ||
407 | // Return the stringpool key of the Nth input section. | |
408 | Stringpool::Key | |
409 | get_input_section_name_key(unsigned int n) const | |
410 | { return this->sections_[n].name_key; } | |
411 | ||
412 | // Return the size of the Nth input section. | |
413 | off_t | |
414 | get_input_section_size(unsigned int n) const | |
415 | { return this->sections_[n].sh_size; } | |
416 | ||
417 | protected: | |
418 | virtual Incremental_input_type | |
419 | do_type() const | |
420 | { | |
421 | return (this->is_member_ | |
422 | ? INCREMENTAL_INPUT_ARCHIVE_MEMBER | |
423 | : (this->obj_->is_dynamic() | |
424 | ? INCREMENTAL_INPUT_SHARED_LIBRARY | |
425 | : INCREMENTAL_INPUT_OBJECT)); | |
426 | } | |
427 | ||
428 | // Return a pointer to the derived Incremental_object_entry object. | |
429 | virtual Incremental_object_entry* | |
430 | do_object_entry() | |
431 | { return this; } | |
432 | ||
433 | private: | |
434 | // The object file itself. | |
435 | Object* obj_; | |
436 | ||
437 | // Whether this object is an archive member. | |
438 | bool is_member_; | |
439 | ||
440 | // Input sections. | |
441 | struct Input_section | |
442 | { | |
443 | Stringpool::Key name_key; | |
444 | off_t sh_size; | |
445 | }; | |
446 | std::vector<Input_section> sections_; | |
447 | }; | |
448 | ||
449 | // Class for recording archive library input files. | |
450 | ||
451 | class Incremental_archive_entry : public Incremental_input_entry | |
452 | { | |
453 | public: | |
e0c52780 | 454 | Incremental_archive_entry(Stringpool::Key filename_key, Timespec mtime) |
09ec0418 CC |
455 | : Incremental_input_entry(filename_key, mtime), members_(), unused_syms_() |
456 | { } | |
457 | ||
458 | // Add a member object to the archive. | |
459 | void | |
460 | add_object(Incremental_object_entry* obj_entry) | |
461 | { | |
462 | this->members_.push_back(obj_entry); | |
463 | obj_entry->set_is_member(); | |
464 | } | |
465 | ||
466 | // Add an unused global symbol to the archive. | |
467 | void | |
468 | add_unused_global_symbol(Stringpool::Key symbol_key) | |
469 | { this->unused_syms_.push_back(symbol_key); } | |
470 | ||
471 | // Return the number of member objects included in the link. | |
472 | unsigned int | |
473 | get_member_count() | |
474 | { return this->members_.size(); } | |
475 | ||
476 | // Return the Nth member object. | |
477 | Incremental_object_entry* | |
478 | get_member(unsigned int n) | |
479 | { return this->members_[n]; } | |
480 | ||
481 | // Return the number of unused global symbols in this archive. | |
482 | unsigned int | |
483 | get_unused_global_symbol_count() | |
484 | { return this->unused_syms_.size(); } | |
485 | ||
486 | // Return the Nth unused global symbol. | |
487 | Stringpool::Key | |
488 | get_unused_global_symbol(unsigned int n) | |
489 | { return this->unused_syms_[n]; } | |
490 | ||
491 | protected: | |
492 | virtual Incremental_input_type | |
493 | do_type() const | |
494 | { return INCREMENTAL_INPUT_ARCHIVE; } | |
495 | ||
496 | // Return a pointer to the derived Incremental_archive_entry object. | |
497 | virtual Incremental_archive_entry* | |
498 | do_archive_entry() | |
499 | { return this; } | |
500 | ||
501 | private: | |
502 | // Members of the archive that have been included in the link. | |
503 | std::vector<Incremental_object_entry*> members_; | |
504 | ||
505 | // Unused global symbols from this archive. | |
506 | std::vector<Stringpool::Key> unused_syms_; | |
507 | }; | |
508 | ||
3ce2c28e ILT |
509 | // This class contains the information needed during an incremental |
510 | // build about the inputs necessary to build the .gnu_incremental_inputs. | |
09ec0418 | 511 | |
3ce2c28e ILT |
512 | class Incremental_inputs |
513 | { | |
514 | public: | |
09ec0418 CC |
515 | typedef std::vector<Incremental_input_entry*> Input_list; |
516 | ||
3ce2c28e | 517 | Incremental_inputs() |
09ec0418 CC |
518 | : inputs_(), command_line_(), command_line_key_(0), |
519 | strtab_(new Stringpool()), current_object_(NULL), | |
520 | current_object_entry_(NULL), inputs_section_(NULL), | |
521 | symtab_section_(NULL), relocs_section_(NULL), | |
522 | reloc_count_(0) | |
3ce2c28e | 523 | { } |
09ec0418 | 524 | |
3ce2c28e ILT |
525 | ~Incremental_inputs() { delete this->strtab_; } |
526 | ||
527 | // Record the command line. | |
528 | void | |
529 | report_command_line(int argc, const char* const* argv); | |
072fe7ce | 530 | |
09ec0418 CC |
531 | // Record the initial info for archive file ARCHIVE. |
532 | void | |
e0c52780 | 533 | report_archive_begin(Library_base* arch); |
09ec0418 CC |
534 | |
535 | // Record the final info for archive file ARCHIVE. | |
536 | void | |
e0c52780 | 537 | report_archive_end(Library_base* arch); |
09ec0418 CC |
538 | |
539 | // Record the info for object file OBJ. If ARCH is not NULL, | |
540 | // attach the object file to the archive. | |
072fe7ce | 541 | void |
e0c52780 | 542 | report_object(Object* obj, Library_base* arch); |
072fe7ce | 543 | |
09ec0418 | 544 | // Record an input section belonging to object file OBJ. |
072fe7ce | 545 | void |
09ec0418 CC |
546 | report_input_section(Object* obj, unsigned int shndx, const char* name, |
547 | off_t sh_size); | |
072fe7ce | 548 | |
09ec0418 | 549 | // Record the info for input script SCRIPT. |
072fe7ce | 550 | void |
09ec0418 CC |
551 | report_script(const std::string& filename, Script_info* script, |
552 | Timespec mtime); | |
553 | ||
554 | // Return the running count of incremental relocations. | |
555 | unsigned int | |
556 | get_reloc_count() const | |
557 | { return this->reloc_count_; } | |
072fe7ce | 558 | |
09ec0418 | 559 | // Update the running count of incremental relocations. |
072fe7ce | 560 | void |
09ec0418 CC |
561 | set_reloc_count(unsigned int count) |
562 | { this->reloc_count_ = count; } | |
072fe7ce | 563 | |
3ce2c28e ILT |
564 | // Prepare for layout. Called from Layout::finalize. |
565 | void | |
566 | finalize(); | |
567 | ||
09ec0418 CC |
568 | // Create the .gnu_incremental_inputs and related sections. |
569 | void | |
570 | create_data_sections(Symbol_table* symtab); | |
571 | ||
572 | // Return the .gnu_incremental_inputs section. | |
3ce2c28e | 573 | Output_section_data* |
09ec0418 CC |
574 | inputs_section() const |
575 | { return this->inputs_section_; } | |
576 | ||
577 | // Return the .gnu_incremental_symtab section. | |
578 | Output_data_space* | |
579 | symtab_section() const | |
580 | { return this->symtab_section_; } | |
581 | ||
582 | // Return the .gnu_incremental_relocs section. | |
583 | Output_data_space* | |
584 | relocs_section() const | |
585 | { return this->relocs_section_; } | |
072fe7ce | 586 | |
0e70b911 CC |
587 | // Return the .gnu_incremental_got_plt section. |
588 | Output_data_space* | |
589 | got_plt_section() const | |
590 | { return this->got_plt_section_; } | |
591 | ||
072fe7ce | 592 | // Return the .gnu_incremental_strtab stringpool. |
3ce2c28e | 593 | Stringpool* |
09ec0418 | 594 | get_stringpool() const |
3ce2c28e ILT |
595 | { return this->strtab_; } |
596 | ||
c4aa1e2d ILT |
597 | // Return the canonical form of the command line, as will be stored in |
598 | // .gnu_incremental_strtab. | |
599 | const std::string& | |
09ec0418 | 600 | command_line() const |
c4aa1e2d ILT |
601 | { return this->command_line_; } |
602 | ||
09ec0418 CC |
603 | // Return the stringpool key of the command line. |
604 | Stringpool::Key | |
605 | command_line_key() const | |
606 | { return this->command_line_key_; } | |
607 | ||
608 | // Return the number of input files. | |
609 | int | |
610 | input_file_count() const | |
611 | { return this->inputs_.size(); } | |
612 | ||
613 | // Return the input files. | |
614 | const Input_list& | |
615 | input_files() const | |
c4aa1e2d ILT |
616 | { return this->inputs_; } |
617 | ||
09ec0418 CC |
618 | // Return the sh_entsize value for the .gnu_incremental_relocs section. |
619 | unsigned int | |
620 | relocs_entsize() const; | |
621 | ||
3ce2c28e | 622 | private: |
09ec0418 CC |
623 | // The list of input files. |
624 | Input_list inputs_; | |
072fe7ce | 625 | |
09ec0418 CC |
626 | // Canonical form of the command line, as will be stored in |
627 | // .gnu_incremental_strtab. | |
628 | std::string command_line_; | |
072fe7ce | 629 | |
09ec0418 CC |
630 | // The key of the command line string in the string pool. |
631 | Stringpool::Key command_line_key_; | |
632 | ||
633 | // The .gnu_incremental_strtab string pool associated with the | |
634 | // .gnu_incremental_inputs. | |
635 | Stringpool* strtab_; | |
636 | ||
637 | // Keep track of the object currently being processed. | |
638 | Object* current_object_; | |
639 | Incremental_object_entry* current_object_entry_; | |
640 | ||
641 | // The .gnu_incremental_inputs section. | |
642 | Output_section_data* inputs_section_; | |
072fe7ce | 643 | |
09ec0418 CC |
644 | // The .gnu_incremental_symtab section. |
645 | Output_data_space* symtab_section_; | |
646 | ||
647 | // The .gnu_incremental_relocs section. | |
648 | Output_data_space* relocs_section_; | |
649 | ||
0e70b911 CC |
650 | // The .gnu_incremental_got_plt section. |
651 | Output_data_space* got_plt_section_; | |
652 | ||
09ec0418 CC |
653 | // Total count of incremental relocations. Updated during Scan_relocs |
654 | // phase at the completion of each object file. | |
655 | unsigned int reloc_count_; | |
656 | }; | |
657 | ||
658 | // Reader class for .gnu_incremental_inputs section. | |
659 | ||
660 | template<int size, bool big_endian> | |
661 | class Incremental_inputs_reader | |
662 | { | |
663 | private: | |
664 | typedef elfcpp::Swap<size, big_endian> Swap; | |
665 | typedef elfcpp::Swap<16, big_endian> Swap16; | |
666 | typedef elfcpp::Swap<32, big_endian> Swap32; | |
667 | typedef elfcpp::Swap<64, big_endian> Swap64; | |
668 | ||
669 | public: | |
670 | Incremental_inputs_reader(const unsigned char* p, elfcpp::Elf_strtab& strtab) | |
671 | : p_(p), strtab_(strtab) | |
672 | { this->input_file_count_ = Swap32::readval(this->p_ + 4); } | |
673 | ||
674 | // Return the version number. | |
675 | unsigned int | |
676 | version() const | |
677 | { return Swap32::readval(this->p_); } | |
678 | ||
679 | // Return the count of input file entries. | |
680 | unsigned int | |
681 | input_file_count() const | |
682 | { return this->input_file_count_; } | |
683 | ||
684 | // Return the command line. | |
685 | const char* | |
686 | command_line() const | |
072fe7ce | 687 | { |
09ec0418 CC |
688 | unsigned int offset = Swap32::readval(this->p_ + 8); |
689 | return this->get_string(offset); | |
690 | } | |
072fe7ce | 691 | |
09ec0418 CC |
692 | // Reader class for an input file entry and its supplemental info. |
693 | class Incremental_input_entry_reader | |
694 | { | |
695 | public: | |
696 | Incremental_input_entry_reader(const Incremental_inputs_reader* inputs, | |
697 | unsigned int offset) | |
698 | : inputs_(inputs), offset_(offset) | |
699 | { | |
700 | this->info_offset_ = Swap32::readval(inputs->p_ + offset + 4); | |
701 | int type = Swap16::readval(this->inputs_->p_ + offset + 20); | |
702 | this->type_ = static_cast<Incremental_input_type>(type); | |
703 | } | |
704 | ||
705 | // Return the filename. | |
706 | const char* | |
707 | filename() const | |
708 | { | |
709 | unsigned int offset = Swap32::readval(this->inputs_->p_ + this->offset_); | |
710 | return this->inputs_->get_string(offset); | |
711 | } | |
072fe7ce | 712 | |
09ec0418 CC |
713 | // Return the timestamp. |
714 | Timespec | |
715 | get_mtime() const | |
716 | { | |
717 | Timespec t; | |
718 | const unsigned char* p = this->inputs_->p_ + this->offset_ + 8; | |
719 | t.seconds = Swap64::readval(p); | |
720 | t.nanoseconds = Swap32::readval(p+8); | |
721 | return t; | |
722 | } | |
723 | ||
724 | // Return the type of input file. | |
725 | Incremental_input_type | |
726 | type() const | |
727 | { return this->type_; } | |
728 | ||
729 | // Return the input section count -- for objects only. | |
730 | unsigned int | |
731 | get_input_section_count() const | |
98fa85cb | 732 | { |
09ec0418 CC |
733 | gold_assert(this->type_ == INCREMENTAL_INPUT_OBJECT |
734 | || this->type_ == INCREMENTAL_INPUT_ARCHIVE_MEMBER); | |
735 | return Swap32::readval(this->inputs_->p_ + this->info_offset_); | |
736 | } | |
737 | ||
738 | // Return the offset of the supplemental info for symbol SYMNDX -- | |
739 | // for objects only. | |
740 | unsigned int | |
741 | get_symbol_offset(unsigned int symndx) const | |
742 | { | |
743 | gold_assert(this->type_ == INCREMENTAL_INPUT_OBJECT | |
744 | || this->type_ == INCREMENTAL_INPUT_ARCHIVE_MEMBER); | |
745 | ||
746 | unsigned int section_count = this->get_input_section_count(); | |
747 | return (this->info_offset_ + 8 | |
748 | + section_count * input_section_entry_size | |
749 | + symndx * 16); | |
750 | } | |
751 | ||
752 | // Return the global symbol count -- for objects & shared libraries only. | |
753 | unsigned int | |
754 | get_global_symbol_count() const | |
755 | { | |
756 | switch (this->type_) | |
757 | { | |
758 | case INCREMENTAL_INPUT_OBJECT: | |
759 | case INCREMENTAL_INPUT_ARCHIVE_MEMBER: | |
760 | return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4); | |
761 | case INCREMENTAL_INPUT_SHARED_LIBRARY: | |
762 | return Swap32::readval(this->inputs_->p_ + this->info_offset_); | |
763 | default: | |
764 | gold_unreachable(); | |
765 | } | |
766 | } | |
767 | ||
768 | // Return the member count -- for archives only. | |
769 | unsigned int | |
770 | get_member_count() const | |
771 | { | |
772 | gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE); | |
773 | return Swap32::readval(this->inputs_->p_ + this->info_offset_); | |
774 | } | |
44453f85 | 775 | |
09ec0418 CC |
776 | // Return the unused symbol count -- for archives only. |
777 | unsigned int | |
778 | get_unused_symbol_count() const | |
779 | { | |
780 | gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE); | |
781 | return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 4); | |
782 | } | |
44453f85 | 783 | |
09ec0418 CC |
784 | // Return the input file offset for archive member N -- for archives only. |
785 | unsigned int | |
786 | get_member_offset(unsigned int n) const | |
787 | { | |
788 | gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE); | |
789 | return Swap32::readval(this->inputs_->p_ + this->info_offset_ | |
790 | + 8 + n * 4); | |
791 | } | |
792 | ||
793 | // Return the Nth unused global symbol -- for archives only. | |
794 | const char* | |
795 | get_unused_symbol(unsigned int n) const | |
796 | { | |
797 | gold_assert(this->type_ == INCREMENTAL_INPUT_ARCHIVE); | |
798 | unsigned int member_count = this->get_member_count(); | |
799 | unsigned int offset = Swap32::readval(this->inputs_->p_ | |
800 | + this->info_offset_ + 8 | |
801 | + member_count * 4 | |
802 | + n * 4); | |
803 | return this->inputs_->get_string(offset); | |
804 | } | |
805 | ||
806 | // Information about an input section. | |
807 | struct Input_section_info | |
808 | { | |
809 | const char* name; | |
810 | unsigned int output_shndx; | |
811 | off_t sh_offset; | |
812 | off_t sh_size; | |
98fa85cb | 813 | }; |
072fe7ce | 814 | |
09ec0418 CC |
815 | // Return info about the Nth input section -- for objects only. |
816 | Input_section_info | |
817 | get_input_section(unsigned int n) const | |
818 | { | |
819 | Input_section_info info; | |
820 | const unsigned char* p = (this->inputs_->p_ | |
821 | + this->info_offset_ + 8 | |
822 | + n * input_section_entry_size); | |
823 | unsigned int name_offset = Swap32::readval(p); | |
824 | info.name = this->inputs_->get_string(name_offset); | |
825 | info.output_shndx = Swap32::readval(p + 4); | |
826 | info.sh_offset = Swap::readval(p + 8); | |
827 | info.sh_size = Swap::readval(p + 8 + size / 8); | |
828 | return info; | |
829 | } | |
830 | ||
831 | // Information about a global symbol. | |
832 | struct Global_symbol_info | |
833 | { | |
834 | unsigned int output_symndx; | |
835 | unsigned int next_offset; | |
836 | unsigned int reloc_count; | |
837 | unsigned int reloc_offset; | |
838 | }; | |
072fe7ce | 839 | |
09ec0418 CC |
840 | // Return info about the Nth global symbol -- for objects only. |
841 | Global_symbol_info | |
842 | get_global_symbol_info(unsigned int n) const | |
843 | { | |
844 | Global_symbol_info info; | |
845 | unsigned int section_count = this->get_input_section_count(); | |
846 | const unsigned char* p = (this->inputs_->p_ | |
847 | + this->info_offset_ + 8 | |
848 | + section_count * input_section_entry_size | |
849 | + n * 16); | |
850 | info.output_symndx = Swap32::readval(p); | |
851 | info.next_offset = Swap32::readval(p + 4); | |
852 | info.reloc_count = Swap::readval(p + 8); | |
853 | info.reloc_offset = Swap::readval(p + 12); | |
854 | return info; | |
855 | } | |
44453f85 | 856 | |
09ec0418 CC |
857 | private: |
858 | // Size of an input section entry. | |
859 | static const unsigned int input_section_entry_size = 8 + 2 * size / 8; | |
860 | // The reader instance for the containing section. | |
861 | const Incremental_inputs_reader* inputs_; | |
862 | // The type of input file. | |
863 | Incremental_input_type type_; | |
864 | // Section offset to the input file entry. | |
865 | unsigned int offset_; | |
866 | // Section offset to the supplemental info for the input file. | |
867 | unsigned int info_offset_; | |
072fe7ce ILT |
868 | }; |
869 | ||
09ec0418 CC |
870 | // Return a reader for the Nth input file entry. |
871 | Incremental_input_entry_reader | |
872 | input_file(unsigned int n) const | |
873 | { | |
874 | gold_assert(n < this->input_file_count_); | |
875 | Incremental_input_entry_reader input(this, 16 + n * 24); | |
876 | return input; | |
877 | } | |
878 | ||
879 | private: | |
880 | // Lookup a string in the ELF string table. | |
881 | const char* get_string(unsigned int offset) const | |
882 | { | |
883 | const char* s; | |
884 | if (this->strtab_.get_c_string(offset, &s)) | |
885 | return s; | |
886 | return NULL; | |
887 | } | |
888 | ||
889 | // Base address of the .gnu_incremental_inputs section. | |
890 | const unsigned char* p_; | |
891 | // The associated ELF string table. | |
892 | elfcpp::Elf_strtab strtab_; | |
893 | // The number of input file entries in this section. | |
894 | unsigned int input_file_count_; | |
895 | }; | |
896 | ||
897 | // Reader class for the .gnu_incremental_symtab section. | |
072fe7ce | 898 | |
09ec0418 CC |
899 | template<bool big_endian> |
900 | class Incremental_symtab_reader | |
901 | { | |
902 | public: | |
903 | Incremental_symtab_reader(const unsigned char* p) : p_(p) | |
904 | { } | |
44453f85 | 905 | |
09ec0418 | 906 | // Return the list head for symbol table entry N. |
0e70b911 | 907 | unsigned int get_list_head(unsigned int n) const |
09ec0418 | 908 | { return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4 * n); } |
072fe7ce | 909 | |
09ec0418 CC |
910 | private: |
911 | // Base address of the .gnu_incremental_relocs section. | |
912 | const unsigned char* p_; | |
913 | }; | |
3ce2c28e | 914 | |
09ec0418 | 915 | // Reader class for the .gnu_incremental_relocs section. |
c4aa1e2d | 916 | |
09ec0418 CC |
917 | template<int size, bool big_endian> |
918 | class Incremental_relocs_reader | |
919 | { | |
920 | private: | |
921 | // Size of each field. | |
922 | static const unsigned int field_size = size / 8; | |
c4aa1e2d | 923 | |
09ec0418 CC |
924 | public: |
925 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
926 | typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend; | |
927 | ||
928 | // Size of each entry. | |
929 | static const unsigned int reloc_size = 8 + 2 * field_size; | |
930 | ||
931 | Incremental_relocs_reader(const unsigned char* p) : p_(p) | |
932 | { } | |
933 | ||
934 | // Return the relocation type for relocation entry at offset OFF. | |
935 | unsigned int | |
0e70b911 | 936 | get_r_type(unsigned int off) const |
09ec0418 CC |
937 | { |
938 | return elfcpp::Swap<32, big_endian>::readval(this->p_ + off); | |
939 | } | |
940 | ||
941 | // Return the output section index for relocation entry at offset OFF. | |
942 | unsigned int | |
0e70b911 | 943 | get_r_shndx(unsigned int off) const |
09ec0418 CC |
944 | { |
945 | return elfcpp::Swap<32, big_endian>::readval(this->p_ + off + 4); | |
946 | } | |
947 | ||
948 | // Return the output section offset for relocation entry at offset OFF. | |
949 | Address | |
0e70b911 | 950 | get_r_offset(unsigned int off) const |
09ec0418 CC |
951 | { |
952 | return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8); | |
953 | } | |
954 | ||
955 | // Return the addend for relocation entry at offset OFF. | |
956 | Addend | |
0e70b911 | 957 | get_r_addend(unsigned int off) const |
09ec0418 CC |
958 | { |
959 | return elfcpp::Swap<size, big_endian>::readval(this->p_ + off + 8 | |
960 | + this->field_size); | |
961 | } | |
962 | ||
963 | private: | |
964 | // Base address of the .gnu_incremental_relocs section. | |
965 | const unsigned char* p_; | |
3ce2c28e ILT |
966 | }; |
967 | ||
0e70b911 CC |
968 | // Reader class for the .gnu_incremental_got_plt section. |
969 | ||
970 | template<bool big_endian> | |
971 | class Incremental_got_plt_reader | |
972 | { | |
973 | public: | |
974 | Incremental_got_plt_reader(const unsigned char* p) : p_(p) | |
975 | { | |
976 | this->got_count_ = elfcpp::Swap<32, big_endian>::readval(p); | |
977 | this->got_desc_p_ = p + 8 + ((this->got_count_ + 3) & ~3); | |
978 | this->plt_desc_p_ = this->got_desc_p_ + this->got_count_ * 4; | |
979 | } | |
980 | ||
981 | // Return the GOT entry count. | |
982 | unsigned int | |
983 | get_got_entry_count() const | |
984 | { | |
985 | return this->got_count_; | |
986 | } | |
987 | ||
988 | // Return the PLT entry count. | |
989 | unsigned int | |
990 | get_plt_entry_count() const | |
991 | { | |
992 | return elfcpp::Swap<32, big_endian>::readval(this->p_ + 4); | |
993 | } | |
994 | ||
995 | // Return the GOT type for GOT entry N. | |
996 | unsigned int | |
997 | get_got_type(unsigned int n) | |
998 | { | |
999 | return this->p_[8 + n]; | |
1000 | } | |
1001 | ||
1002 | // Return the GOT descriptor for GOT entry N. | |
1003 | unsigned int | |
1004 | get_got_desc(unsigned int n) | |
1005 | { | |
1006 | return elfcpp::Swap<32, big_endian>::readval(this->got_desc_p_ + n * 4); | |
1007 | } | |
1008 | ||
1009 | // Return the PLT descriptor for PLT entry N. | |
1010 | unsigned int | |
1011 | get_plt_desc(unsigned int n) | |
1012 | { | |
1013 | return elfcpp::Swap<32, big_endian>::readval(this->plt_desc_p_ + n * 4); | |
1014 | } | |
1015 | ||
1016 | private: | |
1017 | // Base address of the .gnu_incremental_got_plt section. | |
1018 | const unsigned char* p_; | |
1019 | // GOT entry count. | |
1020 | unsigned int got_count_; | |
1021 | // Base address of the GOT descriptor array. | |
1022 | const unsigned char* got_desc_p_; | |
1023 | // Base address of the PLT descriptor array. | |
1024 | const unsigned char* plt_desc_p_; | |
1025 | }; | |
1026 | ||
3ce2c28e ILT |
1027 | } // End namespace gold. |
1028 | ||
1029 | #endif // !defined(GOLD_INCREMENTAL_H) |