bfd/
[binutils.git] / gold / archive.h
bloba2d2af485e08134bec24fc578dc382ac3b7267cf
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
26 #include <string>
27 #include <vector>
29 #include "fileread.h"
30 #include "workqueue.h"
32 namespace gold
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;
46 // An entry in the archive map of offsets to members.
47 struct Archive_member
49 Archive_member()
50 : obj_(NULL), sd_(NULL)
51 { }
52 Archive_member(Object* obj, Read_symbols_data* sd)
53 : obj_(obj), sd_(sd)
54 { }
55 // The object file.
56 Object* obj_;
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.
64 class Archive
66 public:
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
82 // "gcc".
83 const std::string&
84 name() const
85 { return this->name_; }
87 // The input file.
88 const Input_file*
89 input_file() const
90 { return this->input_file_; }
92 // The file name.
93 const std::string&
94 filename() const
95 { return this->input_file_->filename(); }
97 // Set up the archive: read the symbol map.
98 void
99 setup();
101 // Get a reference to the underlying file.
102 File_read&
103 file()
104 { return this->input_file_->file(); }
106 const File_read&
107 file() const
108 { return this->input_file_->file(); }
110 // Lock the underlying file.
111 void
112 lock(const Task* t)
113 { this->input_file_->file().lock(t); }
115 // Unlock the underlying file.
116 void
117 unlock(const Task* t)
118 { this->input_file_->file().unlock(t); }
120 // Return whether the underlying file is locked.
121 bool
122 is_locked() const
123 { return this->input_file_->file().is_locked(); }
125 // Return the token, so that the task can be queued.
126 Task_token*
127 token()
128 { return this->input_file_->file().token(); }
130 // Release the underlying file.
131 void
132 release()
133 { this->input_file_->file().release(); }
135 // Clear uncached views in the underlying file.
136 void
137 clear_uncached_views()
138 { this->input_file_->file().clear_uncached_views(); }
140 // Whether this is a thin archive.
141 bool
142 is_thin_archive() const
143 { return this->is_thin_archive_; }
145 // Unlock any nested archives.
146 void
147 unlock_nested_archives();
149 // Select members from the archive as needed and add them to the
150 // link.
151 bool
152 add_symbols(Symbol_table*, Layout*, Input_objects*, Mapfile*);
154 // Dump statistical information to stderr.
155 static void
156 print_stats();
158 // Return the number of members in the archive.
159 size_t
160 count_members();
162 // Return the no-export flag.
163 bool
164 no_export()
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
169 // possibilities.
171 enum Should_include
173 SHOULD_INCLUDE_NO,
174 SHOULD_INCLUDE_YES,
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,
181 size_t* tmpbuflen);
183 private:
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.
197 const unsigned char*
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.
202 void
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
207 // the name.
208 off_t
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.
213 off_t
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.
222 bool
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.
227 Object*
228 get_elf_object_for_member(off_t off, bool*);
230 // Read the symbols from all the archive members in the link.
231 void
232 read_all_symbols();
234 // Read the symbols from an archive member in the link. OFF is the file
235 // offset of the member header.
236 void
237 read_symbols(off_t off);
239 // Include all the archive members in the link.
240 bool
241 include_all_members(Symbol_table*, Layout*, Input_objects*, Mapfile*);
243 // Include an archive member in the link.
244 bool
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.
249 bool
250 searched_for() const
251 { return this->input_file_->will_search_for(); }
253 // Iterate over archive members.
254 class const_iterator;
256 const_iterator
257 begin();
259 const_iterator
260 end();
262 friend class const_iterator;
264 // An entry in the archive map of symbols to object files.
265 struct Armap_entry
267 // The offset to the symbol name in armap_names_.
268 off_t name_offset;
269 // The file offset to the object in the archive.
270 off_t file_offset;
273 // A simple hash code for off_t values.
274 class Seen_hash
276 public:
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.
285 std::string name_;
286 // For reading the file.
287 Input_file* input_file_;
288 // The archive map.
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.
308 Dirsearch* dirpath_;
309 // The task reading this archive.
310 Task *task_;
311 // Number of members in this archive;
312 unsigned int num_members_;
313 // True if we exclude this library archive from automatic export.
314 bool no_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
322 public:
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.
341 Task_token*
342 is_runnable();
344 void
345 locks(Task_locker*);
347 void
348 run(Workqueue*);
350 std::string
351 get_name() const
353 if (this->archive_ == NULL)
354 return "Add_archive_symbols";
355 return "Add_archive_symbols " + this->archive_->file().filename();
358 private:
359 Symbol_table* symtab_;
360 Layout* layout_;
361 Input_objects* input_objects_;
362 Dirsearch* dirpath_;
363 int dirindex_;
364 Mapfile* mapfile_;
365 const Input_argument* input_argument_;
366 Archive* archive_;
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.
374 class Lib_group
376 public:
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.
380 void
381 add_symbols(Symbol_table*, Layout*, Input_objects*);
383 // Include a member of the lib group in the link.
384 void
385 include_member(Symbol_table*, Layout*, Input_objects*, const Archive_member&);
387 Archive_member*
388 get_member(int i)
390 return &this->members_[i];
393 // Dump statistical information to stderr.
394 static void
395 print_stats();
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;
404 private:
405 // For reading the files.
406 const Input_file_lib* lib_;
407 // The task reading this lib group.
408 Task *task_;
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
417 public:
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.
429 Task_token*
430 is_runnable();
432 void
433 locks(Task_locker*);
435 void
436 run(Workqueue*);
438 // Set the blocker to use for this task.
439 void
440 set_blocker(Task_token* this_blocker)
442 gold_assert(this->this_blocker_ == NULL);
443 this->this_blocker_ = this_blocker;
446 std::string
447 get_name() const
449 return "Add_lib_group_symbols";
452 private:
453 Symbol_table* symtab_;
454 Layout* layout_;
455 Input_objects* input_objects_;
456 Lib_group * lib_;
457 Task_token* this_blocker_;
458 Task_token* next_blocker_;
461 } // End namespace gold.
463 #endif // !defined(GOLD_ARCHIVE_H)