1 // archive.h -- archive support for gold -*- C++ -*-
3 // Copyright (C) 2006-2017 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_ARCHIVE_H
24 #define GOLD_ARCHIVE_H
30 #include "workqueue.h"
43 struct Read_symbols_data
;
45 class Incremental_archive_entry
;
47 // An entry in the archive map of offsets to members.
51 : obj_(NULL
), sd_(NULL
), arg_serial_(0)
53 Archive_member(Object
* obj
, Read_symbols_data
* sd
)
54 : obj_(obj
), sd_(sd
), arg_serial_(0)
58 // The data to pass from read_symbols() to add_symbols().
59 Read_symbols_data
* sd_
;
60 // The serial number of the file in the argument list.
61 unsigned int arg_serial_
;
64 // This class serves as a base class for Archive and Lib_group objects.
69 Library_base(Task
* task
)
70 : task_(task
), incremental_info_(NULL
)
80 { return this->do_filename(); }
82 // The modification time of the archive file.
85 { return this->do_get_mtime(); }
87 // When we see a symbol in an archive we might decide to include the member,
88 // not include the member or be undecided. This enum represents these
95 SHOULD_INCLUDE_UNKNOWN
99 should_include_member(Symbol_table
* symtab
, Layout
*, const char* sym_name
,
100 Symbol
** symp
, std::string
* why
, char** tmpbufp
,
103 // Store a pointer to the incremental link info for the library.
105 set_incremental_info(Incremental_archive_entry
* info
)
106 { this->incremental_info_
= info
; }
108 // Return the pointer to the incremental link info for the library.
109 Incremental_archive_entry
*
110 incremental_info() const
111 { return this->incremental_info_
; }
113 // Abstract base class for processing unused symbols.
114 class Symbol_visitor_base
117 Symbol_visitor_base()
121 ~Symbol_visitor_base()
124 // This function will be called for each unused global
125 // symbol in a library, with a pointer to the symbol name.
127 visit(const char* /* name */) = 0;
130 // Iterator for unused global symbols in the library.
131 // Calls v->visit() for each global symbol defined
132 // in each unused library member, passing a pointer to
135 for_all_unused_symbols(Symbol_visitor_base
* v
) const
136 { this->do_for_all_unused_symbols(v
); }
139 // The task reading this archive.
144 virtual const std::string
&
145 do_filename() const = 0;
147 // Return the modification time of the archive file.
151 // Iterator for unused global symbols in the library.
153 do_for_all_unused_symbols(Symbol_visitor_base
* v
) const = 0;
155 // The incremental link information for this archive.
156 Incremental_archive_entry
* incremental_info_
;
159 // This class represents an archive--generally a libNAME.a file.
160 // Archives have a symbol table and a list of objects.
162 class Archive
: public Library_base
165 Archive(const std::string
& name
, Input_file
* input_file
,
166 bool is_thin_archive
, Dirsearch
* dirpath
, Task
* task
);
168 // The length of the magic string at the start of an archive.
169 static const int sarmag
= 8;
171 // The magic string at the start of an archive.
172 static const char armag
[sarmag
];
173 static const char armagt
[sarmag
];
175 // The string expected at the end of an archive member header.
176 static const char arfmag
[2];
178 // Name of 64-bit symbol table member.
179 static const char sym64name
[7];
181 // The name of the object. This is the name used on the command
182 // line; e.g., if "-lgcc" is on the command line, this will be
186 { return this->name_
; }
191 { return this->input_file_
; }
193 // Set up the archive: read the symbol map.
197 // Get a reference to the underlying file.
200 { return this->input_file_
->file(); }
204 { return this->input_file_
->file(); }
206 // Lock the underlying file.
209 { this->input_file_
->file().lock(t
); }
211 // Unlock the underlying file.
213 unlock(const Task
* t
)
214 { this->input_file_
->file().unlock(t
); }
216 // Return whether the underlying file is locked.
219 { return this->input_file_
->file().is_locked(); }
221 // Return the token, so that the task can be queued.
224 { return this->input_file_
->file().token(); }
226 // Release the underlying file.
229 { this->input_file_
->file().release(); }
231 // Clear uncached views in the underlying file.
233 clear_uncached_views()
234 { this->input_file_
->file().clear_uncached_views(); }
236 // Whether this is a thin archive.
238 is_thin_archive() const
239 { return this->is_thin_archive_
; }
241 // Unlock any nested archives.
243 unlock_nested_archives();
245 // Select members from the archive as needed and add them to the
248 add_symbols(Symbol_table
*, Layout
*, Input_objects
*, Mapfile
*);
250 // Return whether the archive defines the symbol.
252 defines_symbol(Symbol
*) const;
254 // Dump statistical information to stderr.
258 // Return the number of members in the archive.
262 // Return the no-export flag.
265 { return this->no_export_
; }
268 Archive(const Archive
&);
269 Archive
& operator=(const Archive
&);
274 { return this->input_file_
->filename(); }
276 // The modification time of the archive file.
279 { return this->file().get_mtime(); }
281 struct Archive_header
;
283 // Total number of archives seen.
284 static unsigned int total_archives
;
285 // Total number of archive members seen.
286 static unsigned int total_members
;
287 // Number of archive members loaded.
288 static unsigned int total_members_loaded
;
290 // Get a view into the underlying file.
292 get_view(off_t start
, section_size_type size
, bool aligned
, bool cache
)
293 { return this->input_file_
->file().get_view(0, start
, size
, aligned
, cache
); }
295 // Read the archive symbol map.
296 template<int mapsize
>
298 read_armap(off_t start
, section_size_type size
);
300 // Read an archive member header at OFF. CACHE is whether to cache
301 // the file view. Return the size of the member, and set *PNAME to
304 read_header(off_t off
, bool cache
, std::string
* pname
, off_t
* nested_off
);
306 // Interpret an archive header HDR at OFF. Return the size of the
307 // member, and set *PNAME to the name.
309 interpret_header(const Archive_header
* hdr
, off_t off
, std::string
* pname
,
310 off_t
* nested_off
) const;
312 // Get the file and offset for an archive member, which may be an
313 // external member of a thin archive. Set *INPUT_FILE to the
314 // file containing the actual member, *MEMOFF to the offset
315 // within that file (0 if not a nested archive), and *MEMBER_NAME
316 // to the name of the archive member. Return TRUE on success.
318 get_file_and_offset(off_t off
, Input_file
** input_file
, off_t
* memoff
,
319 off_t
* memsize
, std::string
* member_name
);
321 // Return an ELF object for the member at offset OFF.
323 get_elf_object_for_member(off_t off
, bool*);
325 // Read the symbols from all the archive members in the link.
329 // Read the symbols from an archive member in the link. OFF is the file
330 // offset of the member header.
332 read_symbols(off_t off
);
334 // Include all the archive members in the link.
336 include_all_members(Symbol_table
*, Layout
*, Input_objects
*, Mapfile
*);
338 // Include an archive member in the link.
340 include_member(Symbol_table
*, Layout
*, Input_objects
*, off_t off
,
341 Mapfile
*, Symbol
*, const char* why
);
343 // Return whether we found this archive by searching a directory.
346 { return this->input_file_
->will_search_for(); }
348 // Iterate over archive members.
349 class const_iterator
;
357 friend class const_iterator
;
359 // Iterator for unused global symbols in the library.
361 do_for_all_unused_symbols(Symbol_visitor_base
* v
) const;
363 // An entry in the archive map of symbols to object files.
366 // The offset to the symbol name in armap_names_.
368 // The file offset to the object in the archive.
372 // A simple hash code for off_t values.
376 size_t operator()(off_t val
) const
377 { return static_cast<size_t>(val
); }
380 // For keeping track of open nested archives in a thin archive file.
381 typedef Unordered_map
<std::string
, Archive
*> Nested_archive_table
;
383 // Name of object as printed to user.
385 // For reading the file.
386 Input_file
* input_file_
;
388 std::vector
<Armap_entry
> armap_
;
389 // The names in the archive map.
390 std::string armap_names_
;
391 // The extended name table.
392 std::string extended_names_
;
393 // Track which symbols in the archive map are for elements which are
394 // defined or which have already been included in the link.
395 std::vector
<bool> armap_checked_
;
396 // Track which elements have been included by offset.
397 Unordered_set
<off_t
, Seen_hash
> seen_offsets_
;
398 // Table of objects whose symbols have been pre-read.
399 std::map
<off_t
, Archive_member
> members_
;
400 // True if this is a thin archive.
401 const bool is_thin_archive_
;
402 // True if we have included at least one object from this archive.
403 bool included_member_
;
404 // Table of nested archives, indexed by filename.
405 Nested_archive_table nested_archives_
;
406 // The directory search path.
408 // Number of members in this archive;
409 unsigned int num_members_
;
410 // True if we exclude this library archive from automatic export.
412 // True if this library has been included as a --whole-archive.
413 bool included_all_members_
;
416 // This class is used to read an archive and pick out the desired
417 // elements and add them to the link.
419 class Add_archive_symbols
: public Task
422 Add_archive_symbols(Symbol_table
* symtab
, Layout
* layout
,
423 Input_objects
* input_objects
, Dirsearch
* dirpath
,
424 int dirindex
, Mapfile
* mapfile
,
425 const Input_argument
* input_argument
,
426 Archive
* archive
, Input_group
* input_group
,
427 Task_token
* this_blocker
,
428 Task_token
* next_blocker
)
429 : symtab_(symtab
), layout_(layout
), input_objects_(input_objects
),
430 dirpath_(dirpath
), dirindex_(dirindex
), mapfile_(mapfile
),
431 input_argument_(input_argument
), archive_(archive
),
432 input_group_(input_group
), this_blocker_(this_blocker
),
433 next_blocker_(next_blocker
)
436 ~Add_archive_symbols();
438 // The standard Task methods.
452 if (this->archive_
== NULL
)
453 return "Add_archive_symbols";
454 return "Add_archive_symbols " + this->archive_
->file().filename();
458 Symbol_table
* symtab_
;
460 Input_objects
* input_objects_
;
464 const Input_argument
* input_argument_
;
466 Input_group
* input_group_
;
467 Task_token
* this_blocker_
;
468 Task_token
* next_blocker_
;
471 // This class represents the files surrounded by a --start-lib ... --end-lib.
473 class Lib_group
: public Library_base
476 Lib_group(const Input_file_lib
* lib
, Task
* task
);
478 // Select members from the lib group as needed and add them to the link.
480 add_symbols(Symbol_table
*, Layout
*, Input_objects
*);
482 // Include a member of the lib group in the link.
484 include_member(Symbol_table
*, Layout
*, Input_objects
*, const Archive_member
&);
489 return &this->members_
[i
];
492 // Total number of archives seen.
493 static unsigned int total_lib_groups
;
494 // Total number of archive members seen.
495 static unsigned int total_members
;
496 // Number of archive members loaded.
497 static unsigned int total_members_loaded
;
499 // Dump statistical information to stderr.
508 // A Lib_group does not have a modification time, since there is no
509 // real library file.
512 { return Timespec(0, 0); }
514 // Iterator for unused global symbols in the library.
516 do_for_all_unused_symbols(Symbol_visitor_base
*) const;
518 // Table of the objects in the group.
519 std::vector
<Archive_member
> members_
;
522 // This class is used to pick out the desired elements and add them to the link.
524 class Add_lib_group_symbols
: public Task
527 Add_lib_group_symbols(Symbol_table
* symtab
, Layout
* layout
,
528 Input_objects
* input_objects
,
529 Lib_group
* lib
, Task_token
* next_blocker
)
530 : symtab_(symtab
), layout_(layout
), input_objects_(input_objects
),
531 lib_(lib
), readsyms_blocker_(NULL
), this_blocker_(NULL
),
532 next_blocker_(next_blocker
)
535 ~Add_lib_group_symbols();
537 // The standard Task methods.
548 // Set the blocker to use for this task.
550 set_blocker(Task_token
* readsyms_blocker
, Task_token
* this_blocker
)
552 gold_assert(this->readsyms_blocker_
== NULL
&& this->this_blocker_
== NULL
);
553 this->readsyms_blocker_
= readsyms_blocker
;
554 this->this_blocker_
= this_blocker
;
560 return "Add_lib_group_symbols";
564 Symbol_table
* symtab_
;
566 Input_objects
* input_objects_
;
568 Task_token
* readsyms_blocker_
;
569 Task_token
* this_blocker_
;
570 Task_token
* next_blocker_
;
573 } // End namespace gold.
575 #endif // !defined(GOLD_ARCHIVE_H)