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