1 // archive.h -- archive support for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 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
;
46 // An entry in the archive map of offsets to members.
50 : obj_(NULL
), sd_(NULL
)
52 Archive_member(Object
* obj
, Read_symbols_data
* sd
)
57 // The data to pass from read_symbols() to add_symbols().
58 Read_symbols_data
* sd_
;
61 // This class represents an archive--generally a libNAME.a file.
62 // Archives have a symbol table and a list of objects.
67 Archive(const std::string
& name
, Input_file
* input_file
,
68 bool is_thin_archive
, Dirsearch
* dirpath
, Task
* task
);
70 // The length of the magic string at the start of an archive.
71 static const int sarmag
= 8;
73 // The magic string at the start of an archive.
74 static const char armag
[sarmag
];
75 static const char armagt
[sarmag
];
77 // The string expected at the end of an archive member header.
78 static const char arfmag
[2];
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
85 { return this->name_
; }
90 { return this->input_file_
; }
95 { return this->input_file_
->filename(); }
97 // Set up the archive: read the symbol map.
101 // Get a reference to the underlying file.
104 { return this->input_file_
->file(); }
108 { return this->input_file_
->file(); }
110 // Lock the underlying file.
113 { this->input_file_
->file().lock(t
); }
115 // Unlock the underlying file.
117 unlock(const Task
* t
)
118 { this->input_file_
->file().unlock(t
); }
120 // Return whether the underlying file is locked.
123 { return this->input_file_
->file().is_locked(); }
125 // Return the token, so that the task can be queued.
128 { return this->input_file_
->file().token(); }
130 // Release the underlying file.
133 { this->input_file_
->file().release(); }
135 // Clear uncached views in the underlying file.
137 clear_uncached_views()
138 { this->input_file_
->file().clear_uncached_views(); }
140 // Whether this is a thin archive.
142 is_thin_archive() const
143 { return this->is_thin_archive_
; }
145 // Unlock any nested archives.
147 unlock_nested_archives();
149 // Select members from the archive as needed and add them to the
152 add_symbols(Symbol_table
*, Layout
*, Input_objects
*, Mapfile
*);
154 // Dump statistical information to stderr.
158 // Return the number of members in the archive.
162 // Return the no-export flag.
165 { return this->no_export_
; }
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
175 SHOULD_INCLUDE_UNKNOWN
178 static Should_include
179 should_include_member(Symbol_table
* symtab
, const char* sym_name
,
180 Symbol
** symp
, std::string
* why
, char** tmpbufp
,
184 Archive(const Archive
&);
185 Archive
& operator=(const Archive
&);
187 struct Archive_header
;
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
;
196 // Get a view into the underlying file.
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
); }
201 // Read the archive symbol map.
203 read_armap(off_t start
, section_size_type size
);
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
209 read_header(off_t off
, bool cache
, std::string
* pname
, off_t
* nested_off
);
211 // Interpret an archive header HDR at OFF. Return the size of the
212 // member, and set *PNAME to the name.
214 interpret_header(const Archive_header
* hdr
, off_t off
, std::string
* pname
,
215 off_t
* nested_off
) const;
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.
223 get_file_and_offset(off_t off
, Input_file
** input_file
, off_t
* memoff
,
224 off_t
* memsize
, std::string
* member_name
);
226 // Return an ELF object for the member at offset OFF.
228 get_elf_object_for_member(off_t off
, bool*);
230 // Read the symbols from all the archive members in the link.
234 // Read the symbols from an archive member in the link. OFF is the file
235 // offset of the member header.
237 read_symbols(off_t off
);
239 // Include all the archive members in the link.
241 include_all_members(Symbol_table
*, Layout
*, Input_objects
*, Mapfile
*);
243 // Include an archive member in the link.
245 include_member(Symbol_table
*, Layout
*, Input_objects
*, off_t off
,
246 Mapfile
*, Symbol
*, const char* why
);
248 // Return whether we found this archive by searching a directory.
251 { return this->input_file_
->will_search_for(); }
253 // Iterate over archive members.
254 class const_iterator
;
262 friend class const_iterator
;
264 // An entry in the archive map of symbols to object files.
267 // The offset to the symbol name in armap_names_.
269 // The file offset to the object in the archive.
273 // A simple hash code for off_t values.
277 size_t operator()(off_t val
) const
278 { return static_cast<size_t>(val
); }
281 // For keeping track of open nested archives in a thin archive file.
282 typedef Unordered_map
<std::string
, Archive
*> Nested_archive_table
;
284 // Name of object as printed to user.
286 // For reading the file.
287 Input_file
* input_file_
;
289 std::vector
<Armap_entry
> armap_
;
290 // The names in the archive map.
291 std::string armap_names_
;
292 // The extended name table.
293 std::string extended_names_
;
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_
;
299 // Table of objects whose symbols have been pre-read.
300 std::map
<off_t
, Archive_member
> members_
;
301 // True if this is a thin archive.
302 const bool is_thin_archive_
;
303 // True if we have included at least one object from this archive.
304 bool included_member_
;
305 // Table of nested archives, indexed by filename.
306 Nested_archive_table nested_archives_
;
307 // The directory search path.
309 // The task reading this archive.
311 // Number of members in this archive;
312 unsigned int num_members_
;
313 // True if we exclude this library archive from automatic export.
317 // This class is used to read an archive and pick out the desired
318 // elements and add them to the link.
320 class Add_archive_symbols
: public Task
323 Add_archive_symbols(Symbol_table
* symtab
, Layout
* layout
,
324 Input_objects
* input_objects
, Dirsearch
* dirpath
,
325 int dirindex
, Mapfile
* mapfile
,
326 const Input_argument
* input_argument
,
327 Archive
* archive
, Input_group
* input_group
,
328 Task_token
* this_blocker
,
329 Task_token
* next_blocker
)
330 : symtab_(symtab
), layout_(layout
), input_objects_(input_objects
),
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
)
337 ~Add_archive_symbols();
339 // The standard Task methods.
353 if (this->archive_
== NULL
)
354 return "Add_archive_symbols";
355 return "Add_archive_symbols " + this->archive_
->file().filename();
359 Symbol_table
* symtab_
;
361 Input_objects
* input_objects_
;
365 const Input_argument
* input_argument_
;
367 Input_group
* input_group_
;
368 Task_token
* this_blocker_
;
369 Task_token
* next_blocker_
;
372 // This class represents the files surrunded by a --start-lib ... --end-lib.
377 Lib_group(const Input_file_lib
* lib
, Task
* task
);
379 // Select members from the lib group as needed and add them to the link.
381 add_symbols(Symbol_table
*, Layout
*, Input_objects
*);
383 // Include a member of the lib group in the link.
385 include_member(Symbol_table
*, Layout
*, Input_objects
*, const Archive_member
&);
390 return &this->members_
[i
];
393 // Dump statistical information to stderr.
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
;
405 // For reading the files.
406 const Input_file_lib
* lib_
;
407 // The task reading this lib group.
409 // Table of the objects in the group.
410 std::vector
<Archive_member
> members_
;
413 // This class is used to pick out the desired elements and add them to the link.
415 class Add_lib_group_symbols
: public Task
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
)
425 ~Add_lib_group_symbols();
427 // The standard Task methods.
438 // Set the blocker to use for this task.
440 set_blocker(Task_token
* this_blocker
)
442 gold_assert(this->this_blocker_
== NULL
);
443 this->this_blocker_
= this_blocker
;
449 return "Add_lib_group_symbols";
453 Symbol_table
* symtab_
;
455 Input_objects
* input_objects_
;
457 Task_token
* this_blocker_
;
458 Task_token
* next_blocker_
;
461 } // End namespace gold.
463 #endif // !defined(GOLD_ARCHIVE_H)