2008-09-19 Andrew Stubbs <ams@codesourcery.com>
[deliverable/binutils-gdb.git] / gold / archive.h
CommitLineData
61ba1cf9
ILT
1// archive.h -- archive support for gold -*- C++ -*-
2
ebdbb458 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
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.
12
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.
17
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.
22
61ba1cf9
ILT
23#ifndef GOLD_ARCHIVE_H
24#define GOLD_ARCHIVE_H
25
26#include <string>
27#include <vector>
28
7d9e3d98 29#include "fileread.h"
61ba1cf9
ILT
30#include "workqueue.h"
31
32namespace gold
33{
34
17a1d0a9 35class Task;
61ba1cf9
ILT
36class Input_file;
37class Input_objects;
ead1e424 38class Input_group;
12e14209 39class Layout;
61ba1cf9 40class Symbol_table;
ac45a351
CC
41class Object;
42class Read_symbols_data;
61ba1cf9
ILT
43
44// This class represents an archive--generally a libNAME.a file.
45// Archives have a symbol table and a list of objects.
46
47class Archive
48{
49 public:
a1207466
CC
50 Archive(const std::string& name, Input_file* input_file,
51 bool is_thin_archive, Dirsearch* dirpath, Task* task)
9eb9fa57 52 : name_(name), input_file_(input_file), armap_(), armap_names_(),
ac45a351
CC
53 extended_names_(), armap_checked_(), seen_offsets_(), members_(),
54 is_thin_archive_(is_thin_archive), nested_archives_(),
55 dirpath_(dirpath), task_(task), num_members_(0)
61ba1cf9
ILT
56 { }
57
58 // The length of the magic string at the start of an archive.
59 static const int sarmag = 8;
60
61 // The magic string at the start of an archive.
62 static const char armag[sarmag];
a1207466 63 static const char armagt[sarmag];
61ba1cf9
ILT
64
65 // The string expected at the end of an archive member header.
66 static const char arfmag[2];
67
92de84a6
ILT
68 // The name of the object. This is the name used on the command
69 // line; e.g., if "-lgcc" is on the command line, this will be
70 // "gcc".
61ba1cf9
ILT
71 const std::string&
72 name() const
73 { return this->name_; }
74
92de84a6
ILT
75 // The file name.
76 const std::string&
77 filename() const
78 { return this->input_file_->filename(); }
79
61ba1cf9
ILT
80 // Set up the archive: read the symbol map.
81 void
ac45a351 82 setup(Input_objects*);
61ba1cf9 83
ead1e424
ILT
84 // Get a reference to the underlying file.
85 File_read&
86 file()
87 { return this->input_file_->file(); }
88
7d9e3d98
ILT
89 const File_read&
90 file() const
91 { return this->input_file_->file(); }
92
61ba1cf9
ILT
93 // Lock the underlying file.
94 void
17a1d0a9
ILT
95 lock(const Task* t)
96 { this->input_file_->file().lock(t); }
61ba1cf9
ILT
97
98 // Unlock the underlying file.
99 void
17a1d0a9
ILT
100 unlock(const Task* t)
101 { this->input_file_->file().unlock(t); }
61ba1cf9
ILT
102
103 // Return whether the underlying file is locked.
104 bool
105 is_locked() const
106 { return this->input_file_->file().is_locked(); }
107
17a1d0a9
ILT
108 // Return the token, so that the task can be queued.
109 Task_token*
110 token()
111 { return this->input_file_->file().token(); }
112
113 // Release the underlying file.
114 void
115 release()
116 { this->input_file_->file().release(); }
117
39d0cb0e
ILT
118 // Clear uncached views in the underlying file.
119 void
120 clear_uncached_views()
121 { this->input_file_->file().clear_uncached_views(); }
122
92de84a6
ILT
123 // Whether this is a thin archive.
124 bool
125 is_thin_archive() const
126 { return this->is_thin_archive_; }
127
a1207466
CC
128 // Unlock any nested archives.
129 void
130 unlock_nested_archives();
131
61ba1cf9
ILT
132 // Select members from the archive as needed and add them to the
133 // link.
134 void
7d9e3d98 135 add_symbols(Symbol_table*, Layout*, Input_objects*, Mapfile*);
61ba1cf9 136
ac45a351
CC
137 // Dump statistical information to stderr.
138 static void
139 print_stats();
140
92de84a6
ILT
141 // Return the number of members in the archive.
142 size_t
2a00e4fb 143 count_members();
92de84a6 144
61ba1cf9
ILT
145 private:
146 Archive(const Archive&);
147 Archive& operator=(const Archive&);
148
149 struct Archive_header;
61ba1cf9 150
ac45a351
CC
151 // Total number of archives seen.
152 static unsigned int total_archives;
153 // Total number of archive members seen.
154 static unsigned int total_members;
155 // Number of archive members loaded.
156 static unsigned int total_members_loaded;
157
61ba1cf9
ILT
158 // Get a view into the underlying file.
159 const unsigned char*
39d0cb0e
ILT
160 get_view(off_t start, section_size_type size, bool aligned, bool cache)
161 { return this->input_file_->file().get_view(0, start, size, aligned, cache); }
ba45d247 162
4973341a
ILT
163 // Read the archive symbol map.
164 void
8383303e 165 read_armap(off_t start, section_size_type size);
61ba1cf9 166
cb295612
ILT
167 // Read an archive member header at OFF. CACHE is whether to cache
168 // the file view. Return the size of the member, and set *PNAME to
169 // the name.
61ba1cf9 170 off_t
a1207466 171 read_header(off_t off, bool cache, std::string* pname, off_t* nested_off);
61ba1cf9 172
4973341a
ILT
173 // Interpret an archive header HDR at OFF. Return the size of the
174 // member, and set *PNAME to the name.
175 off_t
a1207466 176 interpret_header(const Archive_header* hdr, off_t off, std::string* pname,
92de84a6 177 off_t* nested_off) const;
4973341a 178
ac45a351
CC
179 // Get the file and offset for an archive member, which may be an
180 // external member of a thin archive. Set *INPUT_FILE to the
181 // file containing the actual member, *MEMOFF to the offset
182 // within that file (0 if not a nested archive), and *MEMBER_NAME
183 // to the name of the archive member. Return TRUE on success.
184 bool
185 get_file_and_offset(off_t off, Input_objects* input_objects,
186 Input_file** input_file, off_t* memoff,
187 std::string* member_name);
188
189 // Return an ELF object for the member at offset OFF. Set *MEMBER_NAME to
190 // the name of the member.
191 Object*
192 get_elf_object_for_member(off_t off, Input_objects* input_objects);
193
194 // Read the symbols from all the archive members in the link.
195 void
196 read_all_symbols(Input_objects* input_objects);
197
198 // Read the symbols from an archive member in the link. OFF is the file
199 // offset of the member header.
200 void
201 read_symbols(Input_objects* input_objects, off_t off);
202
4973341a
ILT
203 // Include all the archive members in the link.
204 void
7d9e3d98 205 include_all_members(Symbol_table*, Layout*, Input_objects*, Mapfile*);
4973341a 206
61ba1cf9
ILT
207 // Include an archive member in the link.
208 void
7d9e3d98
ILT
209 include_member(Symbol_table*, Layout*, Input_objects*, off_t off,
210 Mapfile*, Symbol*, const char* why);
61ba1cf9 211
92de84a6
ILT
212 // Iterate over archive members.
213 class const_iterator;
214
215 const_iterator
2a00e4fb 216 begin();
92de84a6
ILT
217
218 const_iterator
2a00e4fb 219 end();
92de84a6
ILT
220
221 friend class const_iterator;
222
61ba1cf9
ILT
223 // An entry in the archive map of symbols to object files.
224 struct Armap_entry
225 {
9eb9fa57
ILT
226 // The offset to the symbol name in armap_names_.
227 off_t name_offset;
228 // The file offset to the object in the archive.
229 off_t file_offset;
61ba1cf9
ILT
230 };
231
ac45a351
CC
232 // An entry in the archive map of offsets to members.
233 struct Archive_member
234 {
235 Archive_member()
236 : obj_(NULL), sd_(NULL)
237 { }
238 Archive_member(Object* obj, Read_symbols_data* sd)
239 : obj_(obj), sd_(sd)
240 { }
241 // The object file.
242 Object* obj_;
243 // The data to pass from read_symbols() to add_symbols().
244 Read_symbols_data* sd_;
245 };
246
a93d6d07
ILT
247 // A simple hash code for off_t values.
248 class Seen_hash
249 {
250 public:
251 size_t operator()(off_t val) const
252 { return static_cast<size_t>(val); }
253 };
254
a1207466
CC
255 // For keeping track of open nested archives in a thin archive file.
256 typedef Unordered_map<std::string, Archive*> Nested_archive_table;
257
61ba1cf9
ILT
258 // Name of object as printed to user.
259 std::string name_;
260 // For reading the file.
261 Input_file* input_file_;
262 // The archive map.
263 std::vector<Armap_entry> armap_;
9eb9fa57
ILT
264 // The names in the archive map.
265 std::string armap_names_;
61ba1cf9
ILT
266 // The extended name table.
267 std::string extended_names_;
a93d6d07
ILT
268 // Track which symbols in the archive map are for elements which are
269 // defined or which have already been included in the link.
270 std::vector<bool> armap_checked_;
271 // Track which elements have been included by offset.
272 Unordered_set<off_t, Seen_hash> seen_offsets_;
ac45a351
CC
273 // Table of objects whose symbols have been pre-read.
274 std::map<off_t, Archive_member> members_;
a1207466
CC
275 // True if this is a thin archive.
276 const bool is_thin_archive_;
277 // Table of nested archives, indexed by filename.
278 Nested_archive_table nested_archives_;
279 // The directory search path.
280 Dirsearch* dirpath_;
281 // The task reading this archive.
282 Task *task_;
ac45a351
CC
283 // Number of members in this archive;
284 unsigned int num_members_;
61ba1cf9
ILT
285};
286
287// This class is used to read an archive and pick out the desired
288// elements and add them to the link.
289
290class Add_archive_symbols : public Task
291{
292 public:
7e1edb90 293 Add_archive_symbols(Symbol_table* symtab, Layout* layout,
7d9e3d98 294 Input_objects* input_objects, Mapfile* mapfile,
ead1e424
ILT
295 Archive* archive, Input_group* input_group,
296 Task_token* this_blocker,
61ba1cf9 297 Task_token* next_blocker)
7e1edb90 298 : symtab_(symtab), layout_(layout), input_objects_(input_objects),
7d9e3d98 299 mapfile_(mapfile), archive_(archive), input_group_(input_group),
7e1edb90 300 this_blocker_(this_blocker), next_blocker_(next_blocker)
61ba1cf9
ILT
301 { }
302
303 ~Add_archive_symbols();
304
305 // The standard Task methods.
306
17a1d0a9
ILT
307 Task_token*
308 is_runnable();
61ba1cf9 309
17a1d0a9
ILT
310 void
311 locks(Task_locker*);
61ba1cf9
ILT
312
313 void
314 run(Workqueue*);
315
c7912668
ILT
316 std::string
317 get_name() const
318 {
319 if (this->archive_ == NULL)
320 return "Add_archive_symbols";
321 return "Add_archive_symbols " + this->archive_->file().filename();
322 }
323
61ba1cf9 324 private:
61ba1cf9 325 Symbol_table* symtab_;
12e14209 326 Layout* layout_;
61ba1cf9 327 Input_objects* input_objects_;
7d9e3d98 328 Mapfile* mapfile_;
61ba1cf9 329 Archive* archive_;
ead1e424 330 Input_group* input_group_;
61ba1cf9
ILT
331 Task_token* this_blocker_;
332 Task_token* next_blocker_;
333};
334
335} // End namespace gold.
336
337#endif // !defined(GOLD_ARCHIVE_H)
This page took 0.105505 seconds and 4 git commands to generate.