PR 11136
[binutils/dougsmingw.git] / gold / archive.h
blob7f567b7f0d5734698b4c4a0496934b37e51a3baf
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;
45 // This class represents an archive--generally a libNAME.a file.
46 // Archives have a symbol table and a list of objects.
48 class Archive
50 public:
51 Archive(const std::string& name, Input_file* input_file,
52 bool is_thin_archive, Dirsearch* dirpath, Task* task);
54 // The length of the magic string at the start of an archive.
55 static const int sarmag = 8;
57 // The magic string at the start of an archive.
58 static const char armag[sarmag];
59 static const char armagt[sarmag];
61 // The string expected at the end of an archive member header.
62 static const char arfmag[2];
64 // The name of the object. This is the name used on the command
65 // line; e.g., if "-lgcc" is on the command line, this will be
66 // "gcc".
67 const std::string&
68 name() const
69 { return this->name_; }
71 // The input file.
72 const Input_file*
73 input_file() const
74 { return this->input_file_; }
76 // The file name.
77 const std::string&
78 filename() const
79 { return this->input_file_->filename(); }
81 // Set up the archive: read the symbol map.
82 void
83 setup();
85 // Get a reference to the underlying file.
86 File_read&
87 file()
88 { return this->input_file_->file(); }
90 const File_read&
91 file() const
92 { return this->input_file_->file(); }
94 // Lock the underlying file.
95 void
96 lock(const Task* t)
97 { this->input_file_->file().lock(t); }
99 // Unlock the underlying file.
100 void
101 unlock(const Task* t)
102 { this->input_file_->file().unlock(t); }
104 // Return whether the underlying file is locked.
105 bool
106 is_locked() const
107 { return this->input_file_->file().is_locked(); }
109 // Return the token, so that the task can be queued.
110 Task_token*
111 token()
112 { return this->input_file_->file().token(); }
114 // Release the underlying file.
115 void
116 release()
117 { this->input_file_->file().release(); }
119 // Clear uncached views in the underlying file.
120 void
121 clear_uncached_views()
122 { this->input_file_->file().clear_uncached_views(); }
124 // Whether this is a thin archive.
125 bool
126 is_thin_archive() const
127 { return this->is_thin_archive_; }
129 // Unlock any nested archives.
130 void
131 unlock_nested_archives();
133 // Select members from the archive as needed and add them to the
134 // link.
135 bool
136 add_symbols(Symbol_table*, Layout*, Input_objects*, Mapfile*);
138 // Dump statistical information to stderr.
139 static void
140 print_stats();
142 // Return the number of members in the archive.
143 size_t
144 count_members();
146 // Return the no-export flag.
147 bool
148 no_export()
149 { return this->no_export_; }
151 private:
152 Archive(const Archive&);
153 Archive& operator=(const Archive&);
155 struct Archive_header;
157 // Total number of archives seen.
158 static unsigned int total_archives;
159 // Total number of archive members seen.
160 static unsigned int total_members;
161 // Number of archive members loaded.
162 static unsigned int total_members_loaded;
164 // Get a view into the underlying file.
165 const unsigned char*
166 get_view(off_t start, section_size_type size, bool aligned, bool cache)
167 { return this->input_file_->file().get_view(0, start, size, aligned, cache); }
169 // Read the archive symbol map.
170 void
171 read_armap(off_t start, section_size_type size);
173 // Read an archive member header at OFF. CACHE is whether to cache
174 // the file view. Return the size of the member, and set *PNAME to
175 // the name.
176 off_t
177 read_header(off_t off, bool cache, std::string* pname, off_t* nested_off);
179 // Interpret an archive header HDR at OFF. Return the size of the
180 // member, and set *PNAME to the name.
181 off_t
182 interpret_header(const Archive_header* hdr, off_t off, std::string* pname,
183 off_t* nested_off) const;
185 // Get the file and offset for an archive member, which may be an
186 // external member of a thin archive. Set *INPUT_FILE to the
187 // file containing the actual member, *MEMOFF to the offset
188 // within that file (0 if not a nested archive), and *MEMBER_NAME
189 // to the name of the archive member. Return TRUE on success.
190 bool
191 get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
192 off_t* memsize, std::string* member_name);
194 // Return an ELF object for the member at offset OFF.
195 Object*
196 get_elf_object_for_member(off_t off, bool*);
198 // Read the symbols from all the archive members in the link.
199 void
200 read_all_symbols();
202 // Read the symbols from an archive member in the link. OFF is the file
203 // offset of the member header.
204 void
205 read_symbols(off_t off);
207 // Include all the archive members in the link.
208 bool
209 include_all_members(Symbol_table*, Layout*, Input_objects*, Mapfile*);
211 // Include an archive member in the link.
212 bool
213 include_member(Symbol_table*, Layout*, Input_objects*, off_t off,
214 Mapfile*, Symbol*, const char* why);
216 // Return whether we found this archive by searching a directory.
217 bool
218 searched_for() const
219 { return this->input_file_->will_search_for(); }
221 // Iterate over archive members.
222 class const_iterator;
224 const_iterator
225 begin();
227 const_iterator
228 end();
230 friend class const_iterator;
232 // An entry in the archive map of symbols to object files.
233 struct Armap_entry
235 // The offset to the symbol name in armap_names_.
236 off_t name_offset;
237 // The file offset to the object in the archive.
238 off_t file_offset;
241 // An entry in the archive map of offsets to members.
242 struct Archive_member
244 Archive_member()
245 : obj_(NULL), sd_(NULL)
247 Archive_member(Object* obj, Read_symbols_data* sd)
248 : obj_(obj), sd_(sd)
250 // The object file.
251 Object* obj_;
252 // The data to pass from read_symbols() to add_symbols().
253 Read_symbols_data* sd_;
256 // A simple hash code for off_t values.
257 class Seen_hash
259 public:
260 size_t operator()(off_t val) const
261 { return static_cast<size_t>(val); }
264 // For keeping track of open nested archives in a thin archive file.
265 typedef Unordered_map<std::string, Archive*> Nested_archive_table;
267 // Name of object as printed to user.
268 std::string name_;
269 // For reading the file.
270 Input_file* input_file_;
271 // The archive map.
272 std::vector<Armap_entry> armap_;
273 // The names in the archive map.
274 std::string armap_names_;
275 // The extended name table.
276 std::string extended_names_;
277 // Track which symbols in the archive map are for elements which are
278 // defined or which have already been included in the link.
279 std::vector<bool> armap_checked_;
280 // Track which elements have been included by offset.
281 Unordered_set<off_t, Seen_hash> seen_offsets_;
282 // Table of objects whose symbols have been pre-read.
283 std::map<off_t, Archive_member> members_;
284 // True if this is a thin archive.
285 const bool is_thin_archive_;
286 // True if we have included at least one object from this archive.
287 bool included_member_;
288 // Table of nested archives, indexed by filename.
289 Nested_archive_table nested_archives_;
290 // The directory search path.
291 Dirsearch* dirpath_;
292 // The task reading this archive.
293 Task *task_;
294 // Number of members in this archive;
295 unsigned int num_members_;
296 // True if we exclude this library archive from automatic export.
297 bool no_export_;
300 // This class is used to read an archive and pick out the desired
301 // elements and add them to the link.
303 class Add_archive_symbols : public Task
305 public:
306 Add_archive_symbols(Symbol_table* symtab, Layout* layout,
307 Input_objects* input_objects, Dirsearch* dirpath,
308 int dirindex, Mapfile* mapfile,
309 const Input_argument* input_argument,
310 Archive* archive, Input_group* input_group,
311 Task_token* this_blocker,
312 Task_token* next_blocker)
313 : symtab_(symtab), layout_(layout), input_objects_(input_objects),
314 dirpath_(dirpath), dirindex_(dirindex), mapfile_(mapfile),
315 input_argument_(input_argument), archive_(archive),
316 input_group_(input_group), this_blocker_(this_blocker),
317 next_blocker_(next_blocker)
320 ~Add_archive_symbols();
322 // The standard Task methods.
324 Task_token*
325 is_runnable();
327 void
328 locks(Task_locker*);
330 void
331 run(Workqueue*);
333 std::string
334 get_name() const
336 if (this->archive_ == NULL)
337 return "Add_archive_symbols";
338 return "Add_archive_symbols " + this->archive_->file().filename();
341 private:
342 Symbol_table* symtab_;
343 Layout* layout_;
344 Input_objects* input_objects_;
345 Dirsearch* dirpath_;
346 int dirindex_;
347 Mapfile* mapfile_;
348 const Input_argument* input_argument_;
349 Archive* archive_;
350 Input_group* input_group_;
351 Task_token* this_blocker_;
352 Task_token* next_blocker_;
355 } // End namespace gold.
357 #endif // !defined(GOLD_ARCHIVE_H)