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