==> bfd/ChangeLog <==
[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;
15f8229b 36class Input_argument;
61ba1cf9
ILT
37class Input_file;
38class Input_objects;
ead1e424 39class Input_group;
12e14209 40class Layout;
61ba1cf9 41class Symbol_table;
ac45a351
CC
42class Object;
43class Read_symbols_data;
61ba1cf9
ILT
44
45// This class represents an archive--generally a libNAME.a file.
46// Archives have a symbol table and a list of objects.
47
48class Archive
49{
50 public:
a1207466
CC
51 Archive(const std::string& name, Input_file* input_file,
52 bool is_thin_archive, Dirsearch* dirpath, Task* task)
9eb9fa57 53 : name_(name), input_file_(input_file), armap_(), armap_names_(),
ac45a351 54 extended_names_(), armap_checked_(), seen_offsets_(), members_(),
15f8229b
ILT
55 is_thin_archive_(is_thin_archive), included_member_(false),
56 nested_archives_(), dirpath_(dirpath), task_(task), num_members_(0)
61ba1cf9
ILT
57 { }
58
59 // The length of the magic string at the start of an archive.
60 static const int sarmag = 8;
61
62 // The magic string at the start of an archive.
63 static const char armag[sarmag];
a1207466 64 static const char armagt[sarmag];
61ba1cf9
ILT
65
66 // The string expected at the end of an archive member header.
67 static const char arfmag[2];
68
92de84a6
ILT
69 // The name of the object. This is the name used on the command
70 // line; e.g., if "-lgcc" is on the command line, this will be
71 // "gcc".
61ba1cf9
ILT
72 const std::string&
73 name() const
74 { return this->name_; }
75
15f8229b
ILT
76 // The input file.
77 const Input_file*
78 input_file() const
79 { return this->input_file_; }
80
92de84a6
ILT
81 // The file name.
82 const std::string&
83 filename() const
84 { return this->input_file_->filename(); }
85
61ba1cf9
ILT
86 // Set up the archive: read the symbol map.
87 void
15f8229b 88 setup();
61ba1cf9 89
ead1e424
ILT
90 // Get a reference to the underlying file.
91 File_read&
92 file()
93 { return this->input_file_->file(); }
94
7d9e3d98
ILT
95 const File_read&
96 file() const
97 { return this->input_file_->file(); }
98
61ba1cf9
ILT
99 // Lock the underlying file.
100 void
17a1d0a9
ILT
101 lock(const Task* t)
102 { this->input_file_->file().lock(t); }
61ba1cf9
ILT
103
104 // Unlock the underlying file.
105 void
17a1d0a9
ILT
106 unlock(const Task* t)
107 { this->input_file_->file().unlock(t); }
61ba1cf9
ILT
108
109 // Return whether the underlying file is locked.
110 bool
111 is_locked() const
112 { return this->input_file_->file().is_locked(); }
113
17a1d0a9
ILT
114 // Return the token, so that the task can be queued.
115 Task_token*
116 token()
117 { return this->input_file_->file().token(); }
118
119 // Release the underlying file.
120 void
121 release()
122 { this->input_file_->file().release(); }
123
39d0cb0e
ILT
124 // Clear uncached views in the underlying file.
125 void
126 clear_uncached_views()
127 { this->input_file_->file().clear_uncached_views(); }
128
92de84a6
ILT
129 // Whether this is a thin archive.
130 bool
131 is_thin_archive() const
132 { return this->is_thin_archive_; }
133
a1207466
CC
134 // Unlock any nested archives.
135 void
136 unlock_nested_archives();
137
61ba1cf9
ILT
138 // Select members from the archive as needed and add them to the
139 // link.
15f8229b 140 bool
7d9e3d98 141 add_symbols(Symbol_table*, Layout*, Input_objects*, Mapfile*);
61ba1cf9 142
ac45a351
CC
143 // Dump statistical information to stderr.
144 static void
145 print_stats();
146
92de84a6
ILT
147 // Return the number of members in the archive.
148 size_t
2a00e4fb 149 count_members();
92de84a6 150
61ba1cf9
ILT
151 private:
152 Archive(const Archive&);
153 Archive& operator=(const Archive&);
154
155 struct Archive_header;
61ba1cf9 156
ac45a351
CC
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;
163
61ba1cf9
ILT
164 // Get a view into the underlying file.
165 const unsigned char*
39d0cb0e
ILT
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); }
ba45d247 168
4973341a
ILT
169 // Read the archive symbol map.
170 void
8383303e 171 read_armap(off_t start, section_size_type size);
61ba1cf9 172
cb295612
ILT
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.
61ba1cf9 176 off_t
a1207466 177 read_header(off_t off, bool cache, std::string* pname, off_t* nested_off);
61ba1cf9 178
4973341a
ILT
179 // Interpret an archive header HDR at OFF. Return the size of the
180 // member, and set *PNAME to the name.
181 off_t
a1207466 182 interpret_header(const Archive_header* hdr, off_t off, std::string* pname,
92de84a6 183 off_t* nested_off) const;
4973341a 184
ac45a351
CC
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
15f8229b 191 get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
89fc3421 192 off_t* memsize, std::string* member_name);
ac45a351 193
15f8229b 194 // Return an ELF object for the member at offset OFF.
ac45a351 195 Object*
15f8229b 196 get_elf_object_for_member(off_t off, bool*);
ac45a351
CC
197
198 // Read the symbols from all the archive members in the link.
199 void
15f8229b 200 read_all_symbols();
ac45a351
CC
201
202 // Read the symbols from an archive member in the link. OFF is the file
203 // offset of the member header.
204 void
15f8229b 205 read_symbols(off_t off);
ac45a351 206
4973341a 207 // Include all the archive members in the link.
15f8229b 208 bool
7d9e3d98 209 include_all_members(Symbol_table*, Layout*, Input_objects*, Mapfile*);
4973341a 210
61ba1cf9 211 // Include an archive member in the link.
15f8229b 212 bool
7d9e3d98
ILT
213 include_member(Symbol_table*, Layout*, Input_objects*, off_t off,
214 Mapfile*, Symbol*, const char* why);
61ba1cf9 215
15f8229b
ILT
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(); }
220
92de84a6
ILT
221 // Iterate over archive members.
222 class const_iterator;
223
224 const_iterator
2a00e4fb 225 begin();
92de84a6
ILT
226
227 const_iterator
2a00e4fb 228 end();
92de84a6
ILT
229
230 friend class const_iterator;
231
61ba1cf9
ILT
232 // An entry in the archive map of symbols to object files.
233 struct Armap_entry
234 {
9eb9fa57
ILT
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;
61ba1cf9
ILT
239 };
240
ac45a351
CC
241 // An entry in the archive map of offsets to members.
242 struct Archive_member
243 {
244 Archive_member()
245 : obj_(NULL), sd_(NULL)
246 { }
247 Archive_member(Object* obj, Read_symbols_data* sd)
248 : obj_(obj), sd_(sd)
249 { }
250 // The object file.
251 Object* obj_;
252 // The data to pass from read_symbols() to add_symbols().
253 Read_symbols_data* sd_;
254 };
255
a93d6d07
ILT
256 // A simple hash code for off_t values.
257 class Seen_hash
258 {
259 public:
260 size_t operator()(off_t val) const
261 { return static_cast<size_t>(val); }
262 };
263
a1207466
CC
264 // For keeping track of open nested archives in a thin archive file.
265 typedef Unordered_map<std::string, Archive*> Nested_archive_table;
266
61ba1cf9
ILT
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_;
9eb9fa57
ILT
273 // The names in the archive map.
274 std::string armap_names_;
61ba1cf9
ILT
275 // The extended name table.
276 std::string extended_names_;
a93d6d07
ILT
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_;
ac45a351
CC
282 // Table of objects whose symbols have been pre-read.
283 std::map<off_t, Archive_member> members_;
a1207466
CC
284 // True if this is a thin archive.
285 const bool is_thin_archive_;
15f8229b
ILT
286 // True if we have included at least one object from this archive.
287 bool included_member_;
a1207466
CC
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_;
ac45a351
CC
294 // Number of members in this archive;
295 unsigned int num_members_;
61ba1cf9
ILT
296};
297
298// This class is used to read an archive and pick out the desired
299// elements and add them to the link.
300
301class Add_archive_symbols : public Task
302{
303 public:
7e1edb90 304 Add_archive_symbols(Symbol_table* symtab, Layout* layout,
15f8229b
ILT
305 Input_objects* input_objects, Dirsearch* dirpath,
306 int dirindex, Mapfile* mapfile,
307 const Input_argument* input_argument,
ead1e424
ILT
308 Archive* archive, Input_group* input_group,
309 Task_token* this_blocker,
61ba1cf9 310 Task_token* next_blocker)
7e1edb90 311 : symtab_(symtab), layout_(layout), input_objects_(input_objects),
15f8229b
ILT
312 dirpath_(dirpath), dirindex_(dirindex), mapfile_(mapfile),
313 input_argument_(input_argument), archive_(archive),
314 input_group_(input_group), this_blocker_(this_blocker),
315 next_blocker_(next_blocker)
61ba1cf9
ILT
316 { }
317
318 ~Add_archive_symbols();
319
320 // The standard Task methods.
321
17a1d0a9
ILT
322 Task_token*
323 is_runnable();
61ba1cf9 324
17a1d0a9
ILT
325 void
326 locks(Task_locker*);
61ba1cf9
ILT
327
328 void
329 run(Workqueue*);
330
c7912668
ILT
331 std::string
332 get_name() const
333 {
334 if (this->archive_ == NULL)
335 return "Add_archive_symbols";
336 return "Add_archive_symbols " + this->archive_->file().filename();
337 }
338
61ba1cf9 339 private:
61ba1cf9 340 Symbol_table* symtab_;
12e14209 341 Layout* layout_;
61ba1cf9 342 Input_objects* input_objects_;
15f8229b
ILT
343 Dirsearch* dirpath_;
344 int dirindex_;
7d9e3d98 345 Mapfile* mapfile_;
15f8229b 346 const Input_argument* input_argument_;
61ba1cf9 347 Archive* archive_;
ead1e424 348 Input_group* input_group_;
61ba1cf9
ILT
349 Task_token* this_blocker_;
350 Task_token* next_blocker_;
351};
352
353} // End namespace gold.
354
355#endif // !defined(GOLD_ARCHIVE_H)
This page took 0.17951 seconds and 4 git commands to generate.