1 // archive.h -- archive support for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2010, 2011 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 class Read_symbols_data
;
45 class Incremental_archive_entry
;
47 // An entry in the archive map of offsets to members.
51 : obj_(NULL
), sd_(NULL
)
53 Archive_member(Object
* obj
, Read_symbols_data
* sd
)
58 // The data to pass from read_symbols() to add_symbols().
59 Read_symbols_data
* sd_
;
62 // This class serves as a base class for Archive and Lib_group objects.
67 Library_base(Task
* task
)
68 : task_(task
), incremental_info_(NULL
)
78 { return this->do_filename(); }
80 // The modification time of the archive file.
83 { return this->do_get_mtime(); }
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
93 SHOULD_INCLUDE_UNKNOWN
97 should_include_member(Symbol_table
* symtab
, Layout
*, const char* sym_name
,
98 Symbol
** symp
, std::string
* why
, char** tmpbufp
,
101 // Store a pointer to the incremental link info for the library.
103 set_incremental_info(Incremental_archive_entry
* info
)
104 { this->incremental_info_
= info
; }
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_
; }
111 // Abstract base class for processing unused symbols.
112 class Symbol_visitor_base
115 Symbol_visitor_base()
119 ~Symbol_visitor_base()
122 // This function will be called for each unused global
123 // symbol in a library, with a pointer to the symbol name.
125 visit(const char* /* name */) = 0;
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
133 for_all_unused_symbols(Symbol_visitor_base
* v
) const
134 { this->do_for_all_unused_symbols(v
); }
137 // The task reading this archive.
142 virtual const std::string
&
143 do_filename() const = 0;
145 // Return the modification time of the archive file.
149 // Iterator for unused global symbols in the library.
151 do_for_all_unused_symbols(Symbol_visitor_base
* v
) const = 0;
153 // The incremental link information for this archive.
154 Incremental_archive_entry
* incremental_info_
;
157 // This class represents an archive--generally a libNAME.a file.
158 // Archives have a symbol table and a list of objects.
160 class Archive
: public Library_base
163 Archive(const std::string
& name
, Input_file
* input_file
,
164 bool is_thin_archive
, Dirsearch
* dirpath
, Task
* task
);
166 // The length of the magic string at the start of an archive.
167 static const int sarmag
= 8;
169 // The magic string at the start of an archive.
170 static const char armag
[sarmag
];
171 static const char armagt
[sarmag
];
173 // The string expected at the end of an archive member header.
174 static const char arfmag
[2];
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
181 { return this->name_
; }
186 { return this->input_file_
; }
188 // Set up the archive: read the symbol map.
192 // Get a reference to the underlying file.
195 { return this->input_file_
->file(); }
199 { return this->input_file_
->file(); }
201 // Lock the underlying file.
204 { this->input_file_
->file().lock(t
); }
206 // Unlock the underlying file.
208 unlock(const Task
* t
)
209 { this->input_file_
->file().unlock(t
); }
211 // Return whether the underlying file is locked.
214 { return this->input_file_
->file().is_locked(); }
216 // Return the token, so that the task can be queued.
219 { return this->input_file_
->file().token(); }
221 // Release the underlying file.
224 { this->input_file_
->file().release(); }
226 // Clear uncached views in the underlying file.
228 clear_uncached_views()
229 { this->input_file_
->file().clear_uncached_views(); }
231 // Whether this is a thin archive.
233 is_thin_archive() const
234 { return this->is_thin_archive_
; }
236 // Unlock any nested archives.
238 unlock_nested_archives();
240 // Select members from the archive as needed and add them to the
243 add_symbols(Symbol_table
*, Layout
*, Input_objects
*, Mapfile
*);
245 // Return whether the archive defines the symbol.
247 defines_symbol(Symbol
*) const;
249 // Dump statistical information to stderr.
253 // Return the number of members in the archive.
257 // Return the no-export flag.
260 { return this->no_export_
; }
263 Archive(const Archive
&);
264 Archive
& operator=(const Archive
&);
269 { return this->input_file_
->filename(); }
271 // The modification time of the archive file.
274 { return this->file().get_mtime(); }
276 struct Archive_header
;
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
;
285 // Get a view into the underlying file.
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
); }
290 // Read the archive symbol map.
292 read_armap(off_t start
, section_size_type size
);
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
298 read_header(off_t off
, bool cache
, std::string
* pname
, off_t
* nested_off
);
300 // Interpret an archive header HDR at OFF. Return the size of the
301 // member, and set *PNAME to the name.
303 interpret_header(const Archive_header
* hdr
, off_t off
, std::string
* pname
,
304 off_t
* nested_off
) const;
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.
312 get_file_and_offset(off_t off
, Input_file
** input_file
, off_t
* memoff
,
313 off_t
* memsize
, std::string
* member_name
);
315 // Return an ELF object for the member at offset OFF.
317 get_elf_object_for_member(off_t off
, bool*);
319 // Read the symbols from all the archive members in the link.
323 // Read the symbols from an archive member in the link. OFF is the file
324 // offset of the member header.
326 read_symbols(off_t off
);
328 // Include all the archive members in the link.
330 include_all_members(Symbol_table
*, Layout
*, Input_objects
*, Mapfile
*);
332 // Include an archive member in the link.
334 include_member(Symbol_table
*, Layout
*, Input_objects
*, off_t off
,
335 Mapfile
*, Symbol
*, const char* why
);
337 // Return whether we found this archive by searching a directory.
340 { return this->input_file_
->will_search_for(); }
342 // Iterate over archive members.
343 class const_iterator
;
351 friend class const_iterator
;
353 // Iterator for unused global symbols in the library.
355 do_for_all_unused_symbols(Symbol_visitor_base
* v
) const;
357 // An entry in the archive map of symbols to object files.
360 // The offset to the symbol name in armap_names_.
362 // The file offset to the object in the archive.
366 // A simple hash code for off_t values.
370 size_t operator()(off_t val
) const
371 { return static_cast<size_t>(val
); }
374 // For keeping track of open nested archives in a thin archive file.
375 typedef Unordered_map
<std::string
, Archive
*> Nested_archive_table
;
377 // Name of object as printed to user.
379 // For reading the file.
380 Input_file
* input_file_
;
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.
402 // Number of members in this archive;
403 unsigned int num_members_
;
404 // True if we exclude this library archive from automatic export.
408 // This class is used to read an archive and pick out the desired
409 // elements and add them to the link.
411 class Add_archive_symbols
: public Task
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
)
428 ~Add_archive_symbols();
430 // The standard Task methods.
444 if (this->archive_
== NULL
)
445 return "Add_archive_symbols";
446 return "Add_archive_symbols " + this->archive_
->file().filename();
450 Symbol_table
* symtab_
;
452 Input_objects
* input_objects_
;
456 const Input_argument
* input_argument_
;
458 Input_group
* input_group_
;
459 Task_token
* this_blocker_
;
460 Task_token
* next_blocker_
;
463 // This class represents the files surrounded by a --start-lib ... --end-lib.
465 class Lib_group
: public Library_base
468 Lib_group(const Input_file_lib
* lib
, Task
* task
);
470 // Select members from the lib group as needed and add them to the link.
472 add_symbols(Symbol_table
*, Layout
*, Input_objects
*);
474 // Include a member of the lib group in the link.
476 include_member(Symbol_table
*, Layout
*, Input_objects
*, const Archive_member
&);
481 return &this->members_
[i
];
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
;
491 // Dump statistical information to stderr.
500 // A Lib_group does not have a modification time, since there is no
501 // real library file.
504 { return Timespec(0, 0); }
506 // Iterator for unused global symbols in the library.
508 do_for_all_unused_symbols(Symbol_visitor_base
*) const;
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_
;
516 // This class is used to pick out the desired elements and add them to the link.
518 class Add_lib_group_symbols
: public Task
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
)
529 ~Add_lib_group_symbols();
531 // The standard Task methods.
542 // Set the blocker to use for this task.
544 set_blocker(Task_token
* readsyms_blocker
, Task_token
* this_blocker
)
546 gold_assert(this->readsyms_blocker_
== NULL
&& this->this_blocker_
== NULL
);
547 this->readsyms_blocker_
= readsyms_blocker
;
548 this->this_blocker_
= this_blocker
;
554 return "Add_lib_group_symbols";
558 Symbol_table
* symtab_
;
560 Input_objects
* input_objects_
;
562 Task_token
* readsyms_blocker_
;
563 Task_token
* this_blocker_
;
564 Task_token
* next_blocker_
;
567 } // End namespace gold.
569 #endif // !defined(GOLD_ARCHIVE_H)