*** empty log message ***
[deliverable/binutils-gdb.git] / gold / archive.h
CommitLineData
61ba1cf9
ILT
1// archive.h -- archive support for gold -*- C++ -*-
2
0f3b89d8 3// Copyright 2006, 2007, 2008, 2010, 2011 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;
b0193076 44class Input_file_lib;
09ec0418 45class Incremental_archive_entry;
b0193076
RÁE
46
47// An entry in the archive map of offsets to members.
48struct Archive_member
49{
50 Archive_member()
51 : obj_(NULL), sd_(NULL)
52 { }
53 Archive_member(Object* obj, Read_symbols_data* sd)
54 : obj_(obj), sd_(sd)
55 { }
56 // The object file.
57 Object* obj_;
58 // The data to pass from read_symbols() to add_symbols().
59 Read_symbols_data* sd_;
60};
61ba1cf9
ILT
61
62// This class represents an archive--generally a libNAME.a file.
63// Archives have a symbol table and a list of objects.
64
65class Archive
66{
67 public:
a1207466 68 Archive(const std::string& name, Input_file* input_file,
65514900 69 bool is_thin_archive, Dirsearch* dirpath, Task* task);
61ba1cf9
ILT
70
71 // The length of the magic string at the start of an archive.
72 static const int sarmag = 8;
73
74 // The magic string at the start of an archive.
75 static const char armag[sarmag];
a1207466 76 static const char armagt[sarmag];
61ba1cf9
ILT
77
78 // The string expected at the end of an archive member header.
79 static const char arfmag[2];
80
92de84a6
ILT
81 // The name of the object. This is the name used on the command
82 // line; e.g., if "-lgcc" is on the command line, this will be
83 // "gcc".
61ba1cf9
ILT
84 const std::string&
85 name() const
86 { return this->name_; }
87
15f8229b
ILT
88 // The input file.
89 const Input_file*
90 input_file() const
91 { return this->input_file_; }
92
92de84a6
ILT
93 // The file name.
94 const std::string&
95 filename() const
96 { return this->input_file_->filename(); }
97
61ba1cf9
ILT
98 // Set up the archive: read the symbol map.
99 void
15f8229b 100 setup();
61ba1cf9 101
ead1e424
ILT
102 // Get a reference to the underlying file.
103 File_read&
104 file()
105 { return this->input_file_->file(); }
106
7d9e3d98
ILT
107 const File_read&
108 file() const
109 { return this->input_file_->file(); }
110
61ba1cf9
ILT
111 // Lock the underlying file.
112 void
17a1d0a9
ILT
113 lock(const Task* t)
114 { this->input_file_->file().lock(t); }
61ba1cf9
ILT
115
116 // Unlock the underlying file.
117 void
17a1d0a9
ILT
118 unlock(const Task* t)
119 { this->input_file_->file().unlock(t); }
61ba1cf9
ILT
120
121 // Return whether the underlying file is locked.
122 bool
123 is_locked() const
124 { return this->input_file_->file().is_locked(); }
125
17a1d0a9
ILT
126 // Return the token, so that the task can be queued.
127 Task_token*
128 token()
129 { return this->input_file_->file().token(); }
130
131 // Release the underlying file.
132 void
133 release()
134 { this->input_file_->file().release(); }
135
39d0cb0e
ILT
136 // Clear uncached views in the underlying file.
137 void
138 clear_uncached_views()
139 { this->input_file_->file().clear_uncached_views(); }
140
92de84a6
ILT
141 // Whether this is a thin archive.
142 bool
143 is_thin_archive() const
144 { return this->is_thin_archive_; }
145
a1207466
CC
146 // Unlock any nested archives.
147 void
148 unlock_nested_archives();
149
61ba1cf9
ILT
150 // Select members from the archive as needed and add them to the
151 // link.
15f8229b 152 bool
7d9e3d98 153 add_symbols(Symbol_table*, Layout*, Input_objects*, Mapfile*);
61ba1cf9 154
0f3b89d8
ILT
155 // Return whether the archive defines the symbol.
156 bool
157 defines_symbol(Symbol*) const;
158
ac45a351
CC
159 // Dump statistical information to stderr.
160 static void
161 print_stats();
162
92de84a6
ILT
163 // Return the number of members in the archive.
164 size_t
2a00e4fb 165 count_members();
92de84a6 166
65514900
CC
167 // Return the no-export flag.
168 bool
169 no_export()
170 { return this->no_export_; }
171
09ec0418
CC
172 // Store a pointer to the incremental link info for the archive.
173 void
174 set_incremental_info(Incremental_archive_entry* info)
175 { this->incremental_info_ = info; }
176
177 // Return the pointer to the incremental link info for the archive.
178 Incremental_archive_entry*
179 incremental_info() const
180 { return this->incremental_info_; }
181
b0193076
RÁE
182 // When we see a symbol in an archive we might decide to include the member,
183 // not include the member or be undecided. This enum represents these
184 // possibilities.
185
186 enum Should_include
187 {
188 SHOULD_INCLUDE_NO,
189 SHOULD_INCLUDE_YES,
190 SHOULD_INCLUDE_UNKNOWN
191 };
192
193 static Should_include
88a4108b 194 should_include_member(Symbol_table* symtab, Layout*, const char* sym_name,
b0193076
RÁE
195 Symbol** symp, std::string* why, char** tmpbufp,
196 size_t* tmpbuflen);
197
09ec0418
CC
198 private:
199 struct Armap_entry;
200
201 public:
202 // Iterator class for unused global symbols. This iterator is used
203 // for incremental links.
204
205 class Unused_symbol_iterator
206 {
207 public:
208 Unused_symbol_iterator(Archive* arch,
209 std::vector<Armap_entry>::const_iterator it)
210 : arch_(arch), it_(it)
211 { this->skip_used_symbols(); }
212
213 const char*
214 operator*() const
215 { return this->arch_->armap_names_.data() + this->it_->name_offset; }
216
217 Unused_symbol_iterator&
218 operator++()
219 {
220 ++this->it_;
221 this->skip_used_symbols();
222 return *this;
223 }
224
225 bool
226 operator==(const Unused_symbol_iterator p) const
227 { return this->it_ == p.it_; }
228
229 bool
230 operator!=(const Unused_symbol_iterator p) const
231 { return this->it_ != p.it_; }
232
233 private:
234 // Skip over symbols defined by members that have been included.
235 void
236 skip_used_symbols()
237 {
238 while (this->it_ != this->arch_->armap_.end()
239 && (this->arch_->seen_offsets_.find(this->it_->file_offset)
240 != this->arch_->seen_offsets_.end()))
241 ++it_;
242 }
243
244 // The underlying archive.
245 Archive* arch_;
246
247 // The underlying iterator over all entries in the archive map.
248 std::vector<Armap_entry>::const_iterator it_;
249 };
250
251 // Return an iterator referring to the first unused symbol.
252 Unused_symbol_iterator
253 unused_symbols_begin()
254 { return Unused_symbol_iterator(this, this->armap_.begin()); }
255
256 // Return an iterator referring to the end of the unused symbols.
257 Unused_symbol_iterator
258 unused_symbols_end()
259 { return Unused_symbol_iterator(this, this->armap_.end()); }
260
61ba1cf9
ILT
261 private:
262 Archive(const Archive&);
263 Archive& operator=(const Archive&);
264
265 struct Archive_header;
61ba1cf9 266
ac45a351
CC
267 // Total number of archives seen.
268 static unsigned int total_archives;
269 // Total number of archive members seen.
270 static unsigned int total_members;
271 // Number of archive members loaded.
272 static unsigned int total_members_loaded;
273
61ba1cf9
ILT
274 // Get a view into the underlying file.
275 const unsigned char*
39d0cb0e
ILT
276 get_view(off_t start, section_size_type size, bool aligned, bool cache)
277 { return this->input_file_->file().get_view(0, start, size, aligned, cache); }
ba45d247 278
4973341a
ILT
279 // Read the archive symbol map.
280 void
8383303e 281 read_armap(off_t start, section_size_type size);
61ba1cf9 282
cb295612
ILT
283 // Read an archive member header at OFF. CACHE is whether to cache
284 // the file view. Return the size of the member, and set *PNAME to
285 // the name.
61ba1cf9 286 off_t
a1207466 287 read_header(off_t off, bool cache, std::string* pname, off_t* nested_off);
61ba1cf9 288
4973341a
ILT
289 // Interpret an archive header HDR at OFF. Return the size of the
290 // member, and set *PNAME to the name.
291 off_t
a1207466 292 interpret_header(const Archive_header* hdr, off_t off, std::string* pname,
92de84a6 293 off_t* nested_off) const;
4973341a 294
ac45a351
CC
295 // Get the file and offset for an archive member, which may be an
296 // external member of a thin archive. Set *INPUT_FILE to the
297 // file containing the actual member, *MEMOFF to the offset
298 // within that file (0 if not a nested archive), and *MEMBER_NAME
299 // to the name of the archive member. Return TRUE on success.
300 bool
15f8229b 301 get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
89fc3421 302 off_t* memsize, std::string* member_name);
ac45a351 303
15f8229b 304 // Return an ELF object for the member at offset OFF.
ac45a351 305 Object*
15f8229b 306 get_elf_object_for_member(off_t off, bool*);
ac45a351
CC
307
308 // Read the symbols from all the archive members in the link.
309 void
15f8229b 310 read_all_symbols();
ac45a351
CC
311
312 // Read the symbols from an archive member in the link. OFF is the file
313 // offset of the member header.
314 void
15f8229b 315 read_symbols(off_t off);
ac45a351 316
4973341a 317 // Include all the archive members in the link.
15f8229b 318 bool
7d9e3d98 319 include_all_members(Symbol_table*, Layout*, Input_objects*, Mapfile*);
4973341a 320
61ba1cf9 321 // Include an archive member in the link.
15f8229b 322 bool
7d9e3d98
ILT
323 include_member(Symbol_table*, Layout*, Input_objects*, off_t off,
324 Mapfile*, Symbol*, const char* why);
61ba1cf9 325
15f8229b
ILT
326 // Return whether we found this archive by searching a directory.
327 bool
328 searched_for() const
329 { return this->input_file_->will_search_for(); }
330
92de84a6
ILT
331 // Iterate over archive members.
332 class const_iterator;
333
334 const_iterator
2a00e4fb 335 begin();
92de84a6
ILT
336
337 const_iterator
2a00e4fb 338 end();
92de84a6
ILT
339
340 friend class const_iterator;
341
61ba1cf9
ILT
342 // An entry in the archive map of symbols to object files.
343 struct Armap_entry
344 {
9eb9fa57
ILT
345 // The offset to the symbol name in armap_names_.
346 off_t name_offset;
347 // The file offset to the object in the archive.
348 off_t file_offset;
61ba1cf9
ILT
349 };
350
a93d6d07
ILT
351 // A simple hash code for off_t values.
352 class Seen_hash
353 {
354 public:
355 size_t operator()(off_t val) const
356 { return static_cast<size_t>(val); }
357 };
358
a1207466
CC
359 // For keeping track of open nested archives in a thin archive file.
360 typedef Unordered_map<std::string, Archive*> Nested_archive_table;
361
61ba1cf9
ILT
362 // Name of object as printed to user.
363 std::string name_;
364 // For reading the file.
365 Input_file* input_file_;
366 // The archive map.
367 std::vector<Armap_entry> armap_;
9eb9fa57
ILT
368 // The names in the archive map.
369 std::string armap_names_;
61ba1cf9
ILT
370 // The extended name table.
371 std::string extended_names_;
a93d6d07
ILT
372 // Track which symbols in the archive map are for elements which are
373 // defined or which have already been included in the link.
374 std::vector<bool> armap_checked_;
375 // Track which elements have been included by offset.
376 Unordered_set<off_t, Seen_hash> seen_offsets_;
ac45a351
CC
377 // Table of objects whose symbols have been pre-read.
378 std::map<off_t, Archive_member> members_;
a1207466
CC
379 // True if this is a thin archive.
380 const bool is_thin_archive_;
15f8229b
ILT
381 // True if we have included at least one object from this archive.
382 bool included_member_;
a1207466
CC
383 // Table of nested archives, indexed by filename.
384 Nested_archive_table nested_archives_;
385 // The directory search path.
386 Dirsearch* dirpath_;
387 // The task reading this archive.
ca09d69a 388 Task* task_;
ac45a351
CC
389 // Number of members in this archive;
390 unsigned int num_members_;
65514900
CC
391 // True if we exclude this library archive from automatic export.
392 bool no_export_;
09ec0418
CC
393 // The incremental link information for this archive.
394 Incremental_archive_entry* incremental_info_;
61ba1cf9
ILT
395};
396
397// This class is used to read an archive and pick out the desired
398// elements and add them to the link.
399
400class Add_archive_symbols : public Task
401{
402 public:
7e1edb90 403 Add_archive_symbols(Symbol_table* symtab, Layout* layout,
15f8229b
ILT
404 Input_objects* input_objects, Dirsearch* dirpath,
405 int dirindex, Mapfile* mapfile,
406 const Input_argument* input_argument,
ead1e424
ILT
407 Archive* archive, Input_group* input_group,
408 Task_token* this_blocker,
61ba1cf9 409 Task_token* next_blocker)
7e1edb90 410 : symtab_(symtab), layout_(layout), input_objects_(input_objects),
15f8229b
ILT
411 dirpath_(dirpath), dirindex_(dirindex), mapfile_(mapfile),
412 input_argument_(input_argument), archive_(archive),
413 input_group_(input_group), this_blocker_(this_blocker),
414 next_blocker_(next_blocker)
61ba1cf9
ILT
415 { }
416
417 ~Add_archive_symbols();
418
419 // The standard Task methods.
420
17a1d0a9
ILT
421 Task_token*
422 is_runnable();
61ba1cf9 423
17a1d0a9
ILT
424 void
425 locks(Task_locker*);
61ba1cf9
ILT
426
427 void
428 run(Workqueue*);
429
c7912668
ILT
430 std::string
431 get_name() const
432 {
433 if (this->archive_ == NULL)
434 return "Add_archive_symbols";
435 return "Add_archive_symbols " + this->archive_->file().filename();
436 }
437
61ba1cf9 438 private:
61ba1cf9 439 Symbol_table* symtab_;
12e14209 440 Layout* layout_;
61ba1cf9 441 Input_objects* input_objects_;
15f8229b
ILT
442 Dirsearch* dirpath_;
443 int dirindex_;
7d9e3d98 444 Mapfile* mapfile_;
15f8229b 445 const Input_argument* input_argument_;
61ba1cf9 446 Archive* archive_;
ead1e424 447 Input_group* input_group_;
61ba1cf9
ILT
448 Task_token* this_blocker_;
449 Task_token* next_blocker_;
450};
451
9b547ce6 452// This class represents the files surrounded by a --start-lib ... --end-lib.
b0193076
RÁE
453
454class Lib_group
455{
456 public:
457 Lib_group(const Input_file_lib* lib, Task* task);
458
459 // Select members from the lib group as needed and add them to the link.
460 void
461 add_symbols(Symbol_table*, Layout*, Input_objects*);
462
463 // Include a member of the lib group in the link.
464 void
465 include_member(Symbol_table*, Layout*, Input_objects*, const Archive_member&);
466
467 Archive_member*
468 get_member(int i)
469 {
470 return &this->members_[i];
471 }
472
473 // Dump statistical information to stderr.
474 static void
475 print_stats();
476
477 // Total number of archives seen.
478 static unsigned int total_lib_groups;
479 // Total number of archive members seen.
480 static unsigned int total_members;
481 // Number of archive members loaded.
482 static unsigned int total_members_loaded;
483
484 private:
485 // For reading the files.
486 const Input_file_lib* lib_;
487 // The task reading this lib group.
ca09d69a 488 Task* task_;
b0193076
RÁE
489 // Table of the objects in the group.
490 std::vector<Archive_member> members_;
491};
492
493// This class is used to pick out the desired elements and add them to the link.
494
495class Add_lib_group_symbols : public Task
496{
497 public:
498 Add_lib_group_symbols(Symbol_table* symtab, Layout* layout,
499 Input_objects* input_objects,
500 Lib_group* lib, Task_token* next_blocker)
501 : symtab_(symtab), layout_(layout), input_objects_(input_objects),
97b4be1c
CC
502 lib_(lib), readsyms_blocker_(NULL), this_blocker_(NULL),
503 next_blocker_(next_blocker)
b0193076
RÁE
504 { }
505
506 ~Add_lib_group_symbols();
507
508 // The standard Task methods.
509
510 Task_token*
511 is_runnable();
512
513 void
514 locks(Task_locker*);
515
516 void
517 run(Workqueue*);
518
519 // Set the blocker to use for this task.
520 void
97b4be1c 521 set_blocker(Task_token* readsyms_blocker, Task_token* this_blocker)
b0193076 522 {
97b4be1c
CC
523 gold_assert(this->readsyms_blocker_ == NULL && this->this_blocker_ == NULL);
524 this->readsyms_blocker_ = readsyms_blocker;
b0193076
RÁE
525 this->this_blocker_ = this_blocker;
526 }
527
528 std::string
529 get_name() const
530 {
531 return "Add_lib_group_symbols";
532 }
533
534 private:
535 Symbol_table* symtab_;
536 Layout* layout_;
537 Input_objects* input_objects_;
ca09d69a 538 Lib_group* lib_;
97b4be1c 539 Task_token* readsyms_blocker_;
b0193076
RÁE
540 Task_token* this_blocker_;
541 Task_token* next_blocker_;
542};
543
61ba1cf9
ILT
544} // End namespace gold.
545
546#endif // !defined(GOLD_ARCHIVE_H)
This page took 0.212914 seconds and 4 git commands to generate.