Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // archive.h -- archive support for gold -*- C++ -*- |
2 | ||
0f3b89d8 | 3 | // Copyright 2006, 2007, 2008, 2010, 2011 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@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 | ||
61ba1cf9 ILT |
23 | #ifndef GOLD_ARCHIVE_H |
24 | #define GOLD_ARCHIVE_H | |
25 | ||
26 | #include <string> | |
27 | #include <vector> | |
28 | ||
7d9e3d98 | 29 | #include "fileread.h" |
61ba1cf9 ILT |
30 | #include "workqueue.h" |
31 | ||
32 | namespace gold | |
33 | { | |
34 | ||
17a1d0a9 | 35 | class Task; |
15f8229b | 36 | class Input_argument; |
61ba1cf9 ILT |
37 | class Input_file; |
38 | class Input_objects; | |
ead1e424 | 39 | class Input_group; |
12e14209 | 40 | class Layout; |
61ba1cf9 | 41 | class Symbol_table; |
ac45a351 CC |
42 | class Object; |
43 | class Read_symbols_data; | |
b0193076 | 44 | class Input_file_lib; |
09ec0418 | 45 | class Incremental_archive_entry; |
b0193076 RÁE |
46 | |
47 | // An entry in the archive map of offsets to members. | |
48 | struct Archive_member | |
49 | { | |
50 | Archive_member() | |
51 | : obj_(NULL), sd_(NULL) | |
52 | { } | |
53 | Archive_member(Object* obj, Read_symbols_data* sd) | |
54 | : obj_(obj), sd_(sd) | |
55 | { } | |
56 | // The object file. | |
57 | Object* obj_; | |
58 | // The data to pass from read_symbols() to add_symbols(). | |
59 | Read_symbols_data* sd_; | |
60 | }; | |
61ba1cf9 | 61 | |
e0c52780 CC |
62 | // This class serves as a base class for Archive and Lib_group objects. |
63 | ||
64 | class Library_base | |
65 | { | |
66 | public: | |
67 | Library_base(Task* task) | |
68 | : task_(task), incremental_info_(NULL) | |
69 | { } | |
70 | ||
71 | virtual | |
72 | ~Library_base() | |
73 | { } | |
74 | ||
75 | // The file name. | |
76 | const std::string& | |
77 | filename() const | |
78 | { return this->do_filename(); } | |
79 | ||
80 | // The modification time of the archive file. | |
81 | Timespec | |
82 | get_mtime() | |
83 | { return this->do_get_mtime(); } | |
84 | ||
85 | // When we see a symbol in an archive we might decide to include the member, | |
86 | // not include the member or be undecided. This enum represents these | |
87 | // possibilities. | |
88 | ||
89 | enum Should_include | |
90 | { | |
91 | SHOULD_INCLUDE_NO, | |
92 | SHOULD_INCLUDE_YES, | |
93 | SHOULD_INCLUDE_UNKNOWN | |
94 | }; | |
95 | ||
96 | static Should_include | |
97 | should_include_member(Symbol_table* symtab, Layout*, const char* sym_name, | |
98 | Symbol** symp, std::string* why, char** tmpbufp, | |
99 | size_t* tmpbuflen); | |
100 | ||
101 | // Store a pointer to the incremental link info for the library. | |
102 | void | |
103 | set_incremental_info(Incremental_archive_entry* info) | |
104 | { this->incremental_info_ = info; } | |
105 | ||
106 | // Return the pointer to the incremental link info for the library. | |
107 | Incremental_archive_entry* | |
108 | incremental_info() const | |
109 | { return this->incremental_info_; } | |
110 | ||
111 | // Abstract base class for processing unused symbols. | |
112 | class Symbol_visitor_base | |
113 | { | |
114 | public: | |
115 | Symbol_visitor_base() | |
116 | { } | |
117 | ||
118 | virtual | |
119 | ~Symbol_visitor_base() | |
120 | { } | |
121 | ||
122 | // This function will be called for each unused global | |
123 | // symbol in a library, with a pointer to the symbol name. | |
124 | virtual void | |
125 | visit(const char* /* name */) = 0; | |
126 | }; | |
127 | ||
128 | // Iterator for unused global symbols in the library. | |
129 | // Calls v->visit() for each global symbol defined | |
130 | // in each unused library member, passing a pointer to | |
131 | // the symbol name. | |
132 | void | |
133 | for_all_unused_symbols(Symbol_visitor_base* v) const | |
134 | { this->do_for_all_unused_symbols(v); } | |
135 | ||
136 | protected: | |
137 | // The task reading this archive. | |
138 | Task *task_; | |
139 | ||
140 | private: | |
141 | // The file name. | |
142 | virtual const std::string& | |
143 | do_filename() const = 0; | |
144 | ||
145 | // Return the modification time of the archive file. | |
146 | virtual Timespec | |
147 | do_get_mtime() = 0; | |
148 | ||
149 | // Iterator for unused global symbols in the library. | |
150 | virtual void | |
151 | do_for_all_unused_symbols(Symbol_visitor_base* v) const = 0; | |
152 | ||
153 | // The incremental link information for this archive. | |
154 | Incremental_archive_entry* incremental_info_; | |
155 | }; | |
156 | ||
61ba1cf9 ILT |
157 | // This class represents an archive--generally a libNAME.a file. |
158 | // Archives have a symbol table and a list of objects. | |
159 | ||
e0c52780 | 160 | class Archive : public Library_base |
61ba1cf9 ILT |
161 | { |
162 | public: | |
a1207466 | 163 | Archive(const std::string& name, Input_file* input_file, |
65514900 | 164 | bool is_thin_archive, Dirsearch* dirpath, Task* task); |
61ba1cf9 ILT |
165 | |
166 | // The length of the magic string at the start of an archive. | |
167 | static const int sarmag = 8; | |
168 | ||
169 | // The magic string at the start of an archive. | |
170 | static const char armag[sarmag]; | |
a1207466 | 171 | static const char armagt[sarmag]; |
61ba1cf9 ILT |
172 | |
173 | // The string expected at the end of an archive member header. | |
174 | static const char arfmag[2]; | |
175 | ||
92de84a6 ILT |
176 | // The name of the object. This is the name used on the command |
177 | // line; e.g., if "-lgcc" is on the command line, this will be | |
178 | // "gcc". | |
61ba1cf9 ILT |
179 | const std::string& |
180 | name() const | |
181 | { return this->name_; } | |
182 | ||
15f8229b ILT |
183 | // The input file. |
184 | const Input_file* | |
185 | input_file() const | |
186 | { return this->input_file_; } | |
187 | ||
61ba1cf9 ILT |
188 | // Set up the archive: read the symbol map. |
189 | void | |
15f8229b | 190 | setup(); |
61ba1cf9 | 191 | |
ead1e424 ILT |
192 | // Get a reference to the underlying file. |
193 | File_read& | |
194 | file() | |
195 | { return this->input_file_->file(); } | |
196 | ||
7d9e3d98 ILT |
197 | const File_read& |
198 | file() const | |
199 | { return this->input_file_->file(); } | |
200 | ||
61ba1cf9 ILT |
201 | // Lock the underlying file. |
202 | void | |
17a1d0a9 ILT |
203 | lock(const Task* t) |
204 | { this->input_file_->file().lock(t); } | |
61ba1cf9 ILT |
205 | |
206 | // Unlock the underlying file. | |
207 | void | |
17a1d0a9 ILT |
208 | unlock(const Task* t) |
209 | { this->input_file_->file().unlock(t); } | |
61ba1cf9 ILT |
210 | |
211 | // Return whether the underlying file is locked. | |
212 | bool | |
213 | is_locked() const | |
214 | { return this->input_file_->file().is_locked(); } | |
215 | ||
17a1d0a9 ILT |
216 | // Return the token, so that the task can be queued. |
217 | Task_token* | |
218 | token() | |
219 | { return this->input_file_->file().token(); } | |
220 | ||
221 | // Release the underlying file. | |
222 | void | |
223 | release() | |
224 | { this->input_file_->file().release(); } | |
225 | ||
39d0cb0e ILT |
226 | // Clear uncached views in the underlying file. |
227 | void | |
228 | clear_uncached_views() | |
229 | { this->input_file_->file().clear_uncached_views(); } | |
230 | ||
92de84a6 ILT |
231 | // Whether this is a thin archive. |
232 | bool | |
233 | is_thin_archive() const | |
234 | { return this->is_thin_archive_; } | |
235 | ||
a1207466 CC |
236 | // Unlock any nested archives. |
237 | void | |
238 | unlock_nested_archives(); | |
239 | ||
61ba1cf9 ILT |
240 | // Select members from the archive as needed and add them to the |
241 | // link. | |
15f8229b | 242 | bool |
7d9e3d98 | 243 | add_symbols(Symbol_table*, Layout*, Input_objects*, Mapfile*); |
61ba1cf9 | 244 | |
0f3b89d8 ILT |
245 | // Return whether the archive defines the symbol. |
246 | bool | |
247 | defines_symbol(Symbol*) const; | |
248 | ||
ac45a351 CC |
249 | // Dump statistical information to stderr. |
250 | static void | |
251 | print_stats(); | |
252 | ||
92de84a6 ILT |
253 | // Return the number of members in the archive. |
254 | size_t | |
2a00e4fb | 255 | count_members(); |
92de84a6 | 256 | |
65514900 CC |
257 | // Return the no-export flag. |
258 | bool | |
259 | no_export() | |
260 | { return this->no_export_; } | |
261 | ||
61ba1cf9 ILT |
262 | private: |
263 | Archive(const Archive&); | |
264 | Archive& operator=(const Archive&); | |
265 | ||
e0c52780 CC |
266 | // The file name. |
267 | const std::string& | |
268 | do_filename() const | |
269 | { return this->input_file_->filename(); } | |
270 | ||
271 | // The modification time of the archive file. | |
272 | Timespec | |
273 | do_get_mtime() | |
274 | { return this->file().get_mtime(); } | |
275 | ||
61ba1cf9 | 276 | struct Archive_header; |
61ba1cf9 | 277 | |
ac45a351 CC |
278 | // Total number of archives seen. |
279 | static unsigned int total_archives; | |
280 | // Total number of archive members seen. | |
281 | static unsigned int total_members; | |
282 | // Number of archive members loaded. | |
283 | static unsigned int total_members_loaded; | |
284 | ||
61ba1cf9 ILT |
285 | // Get a view into the underlying file. |
286 | const unsigned char* | |
39d0cb0e ILT |
287 | get_view(off_t start, section_size_type size, bool aligned, bool cache) |
288 | { return this->input_file_->file().get_view(0, start, size, aligned, cache); } | |
ba45d247 | 289 | |
4973341a ILT |
290 | // Read the archive symbol map. |
291 | void | |
8383303e | 292 | read_armap(off_t start, section_size_type size); |
61ba1cf9 | 293 | |
cb295612 ILT |
294 | // Read an archive member header at OFF. CACHE is whether to cache |
295 | // the file view. Return the size of the member, and set *PNAME to | |
296 | // the name. | |
61ba1cf9 | 297 | off_t |
a1207466 | 298 | read_header(off_t off, bool cache, std::string* pname, off_t* nested_off); |
61ba1cf9 | 299 | |
4973341a ILT |
300 | // Interpret an archive header HDR at OFF. Return the size of the |
301 | // member, and set *PNAME to the name. | |
302 | off_t | |
a1207466 | 303 | interpret_header(const Archive_header* hdr, off_t off, std::string* pname, |
92de84a6 | 304 | off_t* nested_off) const; |
4973341a | 305 | |
ac45a351 CC |
306 | // Get the file and offset for an archive member, which may be an |
307 | // external member of a thin archive. Set *INPUT_FILE to the | |
308 | // file containing the actual member, *MEMOFF to the offset | |
309 | // within that file (0 if not a nested archive), and *MEMBER_NAME | |
310 | // to the name of the archive member. Return TRUE on success. | |
311 | bool | |
15f8229b | 312 | get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff, |
89fc3421 | 313 | off_t* memsize, std::string* member_name); |
ac45a351 | 314 | |
15f8229b | 315 | // Return an ELF object for the member at offset OFF. |
ac45a351 | 316 | Object* |
15f8229b | 317 | get_elf_object_for_member(off_t off, bool*); |
ac45a351 CC |
318 | |
319 | // Read the symbols from all the archive members in the link. | |
320 | void | |
15f8229b | 321 | read_all_symbols(); |
ac45a351 CC |
322 | |
323 | // Read the symbols from an archive member in the link. OFF is the file | |
324 | // offset of the member header. | |
325 | void | |
15f8229b | 326 | read_symbols(off_t off); |
ac45a351 | 327 | |
4973341a | 328 | // Include all the archive members in the link. |
15f8229b | 329 | bool |
7d9e3d98 | 330 | include_all_members(Symbol_table*, Layout*, Input_objects*, Mapfile*); |
4973341a | 331 | |
61ba1cf9 | 332 | // Include an archive member in the link. |
15f8229b | 333 | bool |
7d9e3d98 ILT |
334 | include_member(Symbol_table*, Layout*, Input_objects*, off_t off, |
335 | Mapfile*, Symbol*, const char* why); | |
61ba1cf9 | 336 | |
15f8229b ILT |
337 | // Return whether we found this archive by searching a directory. |
338 | bool | |
339 | searched_for() const | |
340 | { return this->input_file_->will_search_for(); } | |
341 | ||
92de84a6 ILT |
342 | // Iterate over archive members. |
343 | class const_iterator; | |
344 | ||
345 | const_iterator | |
2a00e4fb | 346 | begin(); |
92de84a6 ILT |
347 | |
348 | const_iterator | |
2a00e4fb | 349 | end(); |
92de84a6 ILT |
350 | |
351 | friend class const_iterator; | |
352 | ||
e0c52780 CC |
353 | // Iterator for unused global symbols in the library. |
354 | void | |
355 | do_for_all_unused_symbols(Symbol_visitor_base* v) const; | |
356 | ||
61ba1cf9 ILT |
357 | // An entry in the archive map of symbols to object files. |
358 | struct Armap_entry | |
359 | { | |
9eb9fa57 ILT |
360 | // The offset to the symbol name in armap_names_. |
361 | off_t name_offset; | |
362 | // The file offset to the object in the archive. | |
363 | off_t file_offset; | |
61ba1cf9 ILT |
364 | }; |
365 | ||
a93d6d07 ILT |
366 | // A simple hash code for off_t values. |
367 | class Seen_hash | |
368 | { | |
369 | public: | |
370 | size_t operator()(off_t val) const | |
371 | { return static_cast<size_t>(val); } | |
372 | }; | |
373 | ||
a1207466 CC |
374 | // For keeping track of open nested archives in a thin archive file. |
375 | typedef Unordered_map<std::string, Archive*> Nested_archive_table; | |
376 | ||
61ba1cf9 ILT |
377 | // Name of object as printed to user. |
378 | std::string name_; | |
379 | // For reading the file. | |
380 | Input_file* input_file_; | |
381 | // The archive map. | |
382 | std::vector<Armap_entry> armap_; | |
9eb9fa57 ILT |
383 | // The names in the archive map. |
384 | std::string armap_names_; | |
61ba1cf9 ILT |
385 | // The extended name table. |
386 | std::string extended_names_; | |
a93d6d07 ILT |
387 | // Track which symbols in the archive map are for elements which are |
388 | // defined or which have already been included in the link. | |
389 | std::vector<bool> armap_checked_; | |
390 | // Track which elements have been included by offset. | |
391 | Unordered_set<off_t, Seen_hash> seen_offsets_; | |
ac45a351 CC |
392 | // Table of objects whose symbols have been pre-read. |
393 | std::map<off_t, Archive_member> members_; | |
a1207466 CC |
394 | // True if this is a thin archive. |
395 | const bool is_thin_archive_; | |
15f8229b ILT |
396 | // True if we have included at least one object from this archive. |
397 | bool included_member_; | |
a1207466 CC |
398 | // Table of nested archives, indexed by filename. |
399 | Nested_archive_table nested_archives_; | |
400 | // The directory search path. | |
401 | Dirsearch* dirpath_; | |
ac45a351 CC |
402 | // Number of members in this archive; |
403 | unsigned int num_members_; | |
65514900 CC |
404 | // True if we exclude this library archive from automatic export. |
405 | bool no_export_; | |
61ba1cf9 ILT |
406 | }; |
407 | ||
408 | // This class is used to read an archive and pick out the desired | |
409 | // elements and add them to the link. | |
410 | ||
411 | class Add_archive_symbols : public Task | |
412 | { | |
413 | public: | |
7e1edb90 | 414 | Add_archive_symbols(Symbol_table* symtab, Layout* layout, |
15f8229b ILT |
415 | Input_objects* input_objects, Dirsearch* dirpath, |
416 | int dirindex, Mapfile* mapfile, | |
417 | const Input_argument* input_argument, | |
ead1e424 ILT |
418 | Archive* archive, Input_group* input_group, |
419 | Task_token* this_blocker, | |
61ba1cf9 | 420 | Task_token* next_blocker) |
7e1edb90 | 421 | : symtab_(symtab), layout_(layout), input_objects_(input_objects), |
15f8229b ILT |
422 | dirpath_(dirpath), dirindex_(dirindex), mapfile_(mapfile), |
423 | input_argument_(input_argument), archive_(archive), | |
424 | input_group_(input_group), this_blocker_(this_blocker), | |
425 | next_blocker_(next_blocker) | |
61ba1cf9 ILT |
426 | { } |
427 | ||
428 | ~Add_archive_symbols(); | |
429 | ||
430 | // The standard Task methods. | |
431 | ||
17a1d0a9 ILT |
432 | Task_token* |
433 | is_runnable(); | |
61ba1cf9 | 434 | |
17a1d0a9 ILT |
435 | void |
436 | locks(Task_locker*); | |
61ba1cf9 ILT |
437 | |
438 | void | |
439 | run(Workqueue*); | |
440 | ||
c7912668 ILT |
441 | std::string |
442 | get_name() const | |
443 | { | |
444 | if (this->archive_ == NULL) | |
445 | return "Add_archive_symbols"; | |
446 | return "Add_archive_symbols " + this->archive_->file().filename(); | |
447 | } | |
448 | ||
61ba1cf9 | 449 | private: |
61ba1cf9 | 450 | Symbol_table* symtab_; |
12e14209 | 451 | Layout* layout_; |
61ba1cf9 | 452 | Input_objects* input_objects_; |
15f8229b ILT |
453 | Dirsearch* dirpath_; |
454 | int dirindex_; | |
7d9e3d98 | 455 | Mapfile* mapfile_; |
15f8229b | 456 | const Input_argument* input_argument_; |
61ba1cf9 | 457 | Archive* archive_; |
ead1e424 | 458 | Input_group* input_group_; |
61ba1cf9 ILT |
459 | Task_token* this_blocker_; |
460 | Task_token* next_blocker_; | |
461 | }; | |
462 | ||
9b547ce6 | 463 | // This class represents the files surrounded by a --start-lib ... --end-lib. |
b0193076 | 464 | |
e0c52780 | 465 | class Lib_group : public Library_base |
b0193076 RÁE |
466 | { |
467 | public: | |
468 | Lib_group(const Input_file_lib* lib, Task* task); | |
469 | ||
470 | // Select members from the lib group as needed and add them to the link. | |
471 | void | |
472 | add_symbols(Symbol_table*, Layout*, Input_objects*); | |
473 | ||
474 | // Include a member of the lib group in the link. | |
475 | void | |
476 | include_member(Symbol_table*, Layout*, Input_objects*, const Archive_member&); | |
477 | ||
478 | Archive_member* | |
479 | get_member(int i) | |
480 | { | |
481 | return &this->members_[i]; | |
482 | } | |
483 | ||
b0193076 RÁE |
484 | // Total number of archives seen. |
485 | static unsigned int total_lib_groups; | |
486 | // Total number of archive members seen. | |
487 | static unsigned int total_members; | |
488 | // Number of archive members loaded. | |
489 | static unsigned int total_members_loaded; | |
490 | ||
e0c52780 CC |
491 | // Dump statistical information to stderr. |
492 | static void | |
493 | print_stats(); | |
494 | ||
b0193076 | 495 | private: |
e0c52780 CC |
496 | // The file name. |
497 | const std::string& | |
498 | do_filename() const; | |
499 | ||
500 | // A Lib_group does not have a modification time, since there is no | |
501 | // real library file. | |
502 | Timespec | |
503 | do_get_mtime() | |
504 | { return Timespec(0, 0); } | |
505 | ||
506 | // Iterator for unused global symbols in the library. | |
507 | void | |
508 | do_for_all_unused_symbols(Symbol_visitor_base*) const; | |
509 | ||
b0193076 RÁE |
510 | // For reading the files. |
511 | const Input_file_lib* lib_; | |
b0193076 RÁE |
512 | // Table of the objects in the group. |
513 | std::vector<Archive_member> members_; | |
514 | }; | |
515 | ||
516 | // This class is used to pick out the desired elements and add them to the link. | |
517 | ||
518 | class Add_lib_group_symbols : public Task | |
519 | { | |
520 | public: | |
521 | Add_lib_group_symbols(Symbol_table* symtab, Layout* layout, | |
522 | Input_objects* input_objects, | |
523 | Lib_group* lib, Task_token* next_blocker) | |
524 | : symtab_(symtab), layout_(layout), input_objects_(input_objects), | |
97b4be1c CC |
525 | lib_(lib), readsyms_blocker_(NULL), this_blocker_(NULL), |
526 | next_blocker_(next_blocker) | |
b0193076 RÁE |
527 | { } |
528 | ||
529 | ~Add_lib_group_symbols(); | |
530 | ||
531 | // The standard Task methods. | |
532 | ||
533 | Task_token* | |
534 | is_runnable(); | |
535 | ||
536 | void | |
537 | locks(Task_locker*); | |
538 | ||
539 | void | |
540 | run(Workqueue*); | |
541 | ||
542 | // Set the blocker to use for this task. | |
543 | void | |
97b4be1c | 544 | set_blocker(Task_token* readsyms_blocker, Task_token* this_blocker) |
b0193076 | 545 | { |
97b4be1c CC |
546 | gold_assert(this->readsyms_blocker_ == NULL && this->this_blocker_ == NULL); |
547 | this->readsyms_blocker_ = readsyms_blocker; | |
b0193076 RÁE |
548 | this->this_blocker_ = this_blocker; |
549 | } | |
550 | ||
551 | std::string | |
552 | get_name() const | |
553 | { | |
554 | return "Add_lib_group_symbols"; | |
555 | } | |
556 | ||
557 | private: | |
558 | Symbol_table* symtab_; | |
559 | Layout* layout_; | |
560 | Input_objects* input_objects_; | |
ca09d69a | 561 | Lib_group* lib_; |
97b4be1c | 562 | Task_token* readsyms_blocker_; |
b0193076 RÁE |
563 | Task_token* this_blocker_; |
564 | Task_token* next_blocker_; | |
565 | }; | |
566 | ||
61ba1cf9 ILT |
567 | } // End namespace gold. |
568 | ||
569 | #endif // !defined(GOLD_ARCHIVE_H) |