* archive.cc (Library_base::should_include_member): Move
[deliverable/binutils-gdb.git] / gold / archive.h
1 // archive.h -- archive support for gold -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2010, 2011 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 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
157 // This class represents an archive--generally a libNAME.a file.
158 // Archives have a symbol table and a list of objects.
159
160 class Archive : public Library_base
161 {
162 public:
163 Archive(const std::string& name, Input_file* input_file,
164 bool is_thin_archive, Dirsearch* dirpath, Task* task);
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];
171 static const char armagt[sarmag];
172
173 // The string expected at the end of an archive member header.
174 static const char arfmag[2];
175
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".
179 const std::string&
180 name() const
181 { return this->name_; }
182
183 // The input file.
184 const Input_file*
185 input_file() const
186 { return this->input_file_; }
187
188 // Set up the archive: read the symbol map.
189 void
190 setup();
191
192 // Get a reference to the underlying file.
193 File_read&
194 file()
195 { return this->input_file_->file(); }
196
197 const File_read&
198 file() const
199 { return this->input_file_->file(); }
200
201 // Lock the underlying file.
202 void
203 lock(const Task* t)
204 { this->input_file_->file().lock(t); }
205
206 // Unlock the underlying file.
207 void
208 unlock(const Task* t)
209 { this->input_file_->file().unlock(t); }
210
211 // Return whether the underlying file is locked.
212 bool
213 is_locked() const
214 { return this->input_file_->file().is_locked(); }
215
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
226 // Clear uncached views in the underlying file.
227 void
228 clear_uncached_views()
229 { this->input_file_->file().clear_uncached_views(); }
230
231 // Whether this is a thin archive.
232 bool
233 is_thin_archive() const
234 { return this->is_thin_archive_; }
235
236 // Unlock any nested archives.
237 void
238 unlock_nested_archives();
239
240 // Select members from the archive as needed and add them to the
241 // link.
242 bool
243 add_symbols(Symbol_table*, Layout*, Input_objects*, Mapfile*);
244
245 // Return whether the archive defines the symbol.
246 bool
247 defines_symbol(Symbol*) const;
248
249 // Dump statistical information to stderr.
250 static void
251 print_stats();
252
253 // Return the number of members in the archive.
254 size_t
255 count_members();
256
257 // Return the no-export flag.
258 bool
259 no_export()
260 { return this->no_export_; }
261
262 private:
263 Archive(const Archive&);
264 Archive& operator=(const Archive&);
265
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
276 struct Archive_header;
277
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
285 // Get a view into the underlying file.
286 const unsigned char*
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); }
289
290 // Read the archive symbol map.
291 void
292 read_armap(off_t start, section_size_type size);
293
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.
297 off_t
298 read_header(off_t off, bool cache, std::string* pname, off_t* nested_off);
299
300 // Interpret an archive header HDR at OFF. Return the size of the
301 // member, and set *PNAME to the name.
302 off_t
303 interpret_header(const Archive_header* hdr, off_t off, std::string* pname,
304 off_t* nested_off) const;
305
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
312 get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
313 off_t* memsize, std::string* member_name);
314
315 // Return an ELF object for the member at offset OFF.
316 Object*
317 get_elf_object_for_member(off_t off, bool*);
318
319 // Read the symbols from all the archive members in the link.
320 void
321 read_all_symbols();
322
323 // Read the symbols from an archive member in the link. OFF is the file
324 // offset of the member header.
325 void
326 read_symbols(off_t off);
327
328 // Include all the archive members in the link.
329 bool
330 include_all_members(Symbol_table*, Layout*, Input_objects*, Mapfile*);
331
332 // Include an archive member in the link.
333 bool
334 include_member(Symbol_table*, Layout*, Input_objects*, off_t off,
335 Mapfile*, Symbol*, const char* why);
336
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
342 // Iterate over archive members.
343 class const_iterator;
344
345 const_iterator
346 begin();
347
348 const_iterator
349 end();
350
351 friend class const_iterator;
352
353 // Iterator for unused global symbols in the library.
354 void
355 do_for_all_unused_symbols(Symbol_visitor_base* v) const;
356
357 // An entry in the archive map of symbols to object files.
358 struct Armap_entry
359 {
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;
364 };
365
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
374 // For keeping track of open nested archives in a thin archive file.
375 typedef Unordered_map<std::string, Archive*> Nested_archive_table;
376
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_;
383 // The names in the archive map.
384 std::string armap_names_;
385 // The extended name table.
386 std::string extended_names_;
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_;
392 // Table of objects whose symbols have been pre-read.
393 std::map<off_t, Archive_member> members_;
394 // True if this is a thin archive.
395 const bool is_thin_archive_;
396 // True if we have included at least one object from this archive.
397 bool included_member_;
398 // Table of nested archives, indexed by filename.
399 Nested_archive_table nested_archives_;
400 // The directory search path.
401 Dirsearch* dirpath_;
402 // Number of members in this archive;
403 unsigned int num_members_;
404 // True if we exclude this library archive from automatic export.
405 bool no_export_;
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:
414 Add_archive_symbols(Symbol_table* symtab, Layout* layout,
415 Input_objects* input_objects, Dirsearch* dirpath,
416 int dirindex, Mapfile* mapfile,
417 const Input_argument* input_argument,
418 Archive* archive, Input_group* input_group,
419 Task_token* this_blocker,
420 Task_token* next_blocker)
421 : symtab_(symtab), layout_(layout), input_objects_(input_objects),
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)
426 { }
427
428 ~Add_archive_symbols();
429
430 // The standard Task methods.
431
432 Task_token*
433 is_runnable();
434
435 void
436 locks(Task_locker*);
437
438 void
439 run(Workqueue*);
440
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
449 private:
450 Symbol_table* symtab_;
451 Layout* layout_;
452 Input_objects* input_objects_;
453 Dirsearch* dirpath_;
454 int dirindex_;
455 Mapfile* mapfile_;
456 const Input_argument* input_argument_;
457 Archive* archive_;
458 Input_group* input_group_;
459 Task_token* this_blocker_;
460 Task_token* next_blocker_;
461 };
462
463 // This class represents the files surrounded by a --start-lib ... --end-lib.
464
465 class Lib_group : public Library_base
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
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
491 // Dump statistical information to stderr.
492 static void
493 print_stats();
494
495 private:
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
510 // For reading the files.
511 const Input_file_lib* lib_;
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),
525 lib_(lib), readsyms_blocker_(NULL), this_blocker_(NULL),
526 next_blocker_(next_blocker)
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
544 set_blocker(Task_token* readsyms_blocker, Task_token* this_blocker)
545 {
546 gold_assert(this->readsyms_blocker_ == NULL && this->this_blocker_ == NULL);
547 this->readsyms_blocker_ = readsyms_blocker;
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_;
561 Lib_group* lib_;
562 Task_token* readsyms_blocker_;
563 Task_token* this_blocker_;
564 Task_token* next_blocker_;
565 };
566
567 } // End namespace gold.
568
569 #endif // !defined(GOLD_ARCHIVE_H)
This page took 0.042292 seconds and 5 git commands to generate.