2010-08-12 Daniel Jacobowitz <dan@codesourcery.com>
[deliverable/binutils-gdb.git] / gold / archive.cc
CommitLineData
61ba1cf9
ILT
1// archive.cc -- archive support for gold
2
114dfbe1 3// Copyright 2006, 2007, 2008, 2009, 2010 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#include "gold.h"
24
25#include <cerrno>
26#include <cstring>
27#include <climits>
28#include <vector>
a1207466
CC
29#include "libiberty.h"
30#include "filenames.h"
61ba1cf9
ILT
31
32#include "elfcpp.h"
7e1edb90 33#include "options.h"
7d9e3d98 34#include "mapfile.h"
61ba1cf9 35#include "fileread.h"
ead1e424 36#include "readsyms.h"
61ba1cf9
ILT
37#include "symtab.h"
38#include "object.h"
88a4108b 39#include "layout.h"
61ba1cf9 40#include "archive.h"
89fc3421 41#include "plugin.h"
61ba1cf9
ILT
42
43namespace gold
44{
45
46// The header of an entry in the archive. This is all readable text,
47// padded with spaces where necesary. If the contents of an archive
48// are all text file, the entire archive is readable.
49
50struct Archive::Archive_header
51{
52 // The entry name.
53 char ar_name[16];
54 // The file modification time.
55 char ar_date[12];
56 // The user's UID in decimal.
57 char ar_uid[6];
58 // The user's GID in decimal.
59 char ar_gid[6];
60 // The file mode in octal.
61 char ar_mode[8];
62 // The file size in decimal.
63 char ar_size[10];
64 // The final magic code.
65 char ar_fmag[2];
66};
67
ac45a351
CC
68// Class Archive static variables.
69unsigned int Archive::total_archives;
70unsigned int Archive::total_members;
71unsigned int Archive::total_members_loaded;
72
61ba1cf9
ILT
73// Archive methods.
74
75const char Archive::armag[sarmag] =
76{
77 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
78};
79
a1207466
CC
80const char Archive::armagt[sarmag] =
81{
82 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
83};
84
61ba1cf9
ILT
85const char Archive::arfmag[2] = { '`', '\n' };
86
2ea97941
ILT
87Archive::Archive(const std::string& name, Input_file* input_file,
88 bool is_thin_archive, Dirsearch* dirpath, Task* task)
89 : name_(name), input_file_(input_file), armap_(), armap_names_(),
65514900 90 extended_names_(), armap_checked_(), seen_offsets_(), members_(),
2ea97941 91 is_thin_archive_(is_thin_archive), included_member_(false),
65514900
CC
92 nested_archives_(), dirpath_(dirpath), task_(task), num_members_(0)
93{
94 this->no_export_ =
2ea97941 95 parameters->options().check_excluded_libs(input_file->found_name());
65514900
CC
96}
97
61ba1cf9
ILT
98// Set up the archive: read the symbol map and the extended name
99// table.
100
101void
15f8229b 102Archive::setup()
61ba1cf9 103{
3e95a404
ILT
104 // We need to ignore empty archives.
105 if (this->input_file_->file().filesize() == sarmag)
a1207466 106 return;
3e95a404 107
61ba1cf9
ILT
108 // The first member of the archive should be the symbol table.
109 std::string armap_name;
8383303e 110 section_size_type armap_size =
cb295612 111 convert_to_section_size_type(this->read_header(sarmag, false,
a1207466 112 &armap_name, NULL));
75f2446e 113 off_t off = sarmag;
4973341a
ILT
114 if (armap_name.empty())
115 {
116 this->read_armap(sarmag + sizeof(Archive_header), armap_size);
117 off = sarmag + sizeof(Archive_header) + armap_size;
118 }
45aa233b 119 else if (!this->input_file_->options().whole_archive())
75f2446e
ILT
120 gold_error(_("%s: no archive symbol table (run ranlib)"),
121 this->name().c_str());
4973341a 122
cb295612
ILT
123 // See if there is an extended name table. We cache these views
124 // because it is likely that we will want to read the following
125 // header in the add_symbols routine.
4973341a
ILT
126 if ((off & 1) != 0)
127 ++off;
128 std::string xname;
cb295612 129 section_size_type extended_size =
a1207466 130 convert_to_section_size_type(this->read_header(off, true, &xname, NULL));
4973341a
ILT
131 if (xname == "/")
132 {
133 const unsigned char* p = this->get_view(off + sizeof(Archive_header),
39d0cb0e 134 extended_size, false, true);
4973341a
ILT
135 const char* px = reinterpret_cast<const char*>(p);
136 this->extended_names_.assign(px, extended_size);
137 }
ac45a351
CC
138 bool preread_syms = (parameters->options().threads()
139 && parameters->options().preread_archive_symbols());
140#ifndef ENABLE_THREADS
141 preread_syms = false;
89fc3421
CC
142#else
143 if (parameters->options().has_plugins())
144 preread_syms = false;
ac45a351
CC
145#endif
146 if (preread_syms)
15f8229b 147 this->read_all_symbols();
a1207466
CC
148}
149
150// Unlock any nested archives.
4973341a 151
a1207466
CC
152void
153Archive::unlock_nested_archives()
154{
155 for (Nested_archive_table::iterator p = this->nested_archives_.begin();
156 p != this->nested_archives_.end();
157 ++p)
158 {
159 p->second->unlock(this->task_);
160 }
4973341a 161}
61ba1cf9 162
4973341a
ILT
163// Read the archive symbol map.
164
165void
8383303e 166Archive::read_armap(off_t start, section_size_type size)
4973341a 167{
ac45a351
CC
168 // To count the total number of archive members, we'll just count
169 // the number of times the file offset changes. Since most archives
170 // group the symbols in the armap by object, this ought to give us
171 // an accurate count.
172 off_t last_seen_offset = -1;
173
61ba1cf9 174 // Read in the entire armap.
39d0cb0e 175 const unsigned char* p = this->get_view(start, size, true, false);
61ba1cf9
ILT
176
177 // Numbers in the armap are always big-endian.
178 const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p);
f6ce93d6 179 unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword);
61ba1cf9
ILT
180 ++pword;
181
182 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
183 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
8383303e
ILT
184 section_size_type names_size =
185 reinterpret_cast<const char*>(p) + size - pnames;
9eb9fa57 186 this->armap_names_.assign(pnames, names_size);
61ba1cf9
ILT
187
188 this->armap_.resize(nsyms);
189
8383303e 190 section_offset_type name_offset = 0;
61ba1cf9
ILT
191 for (unsigned int i = 0; i < nsyms; ++i)
192 {
9eb9fa57
ILT
193 this->armap_[i].name_offset = name_offset;
194 this->armap_[i].file_offset = elfcpp::Swap<32, true>::readval(pword);
195 name_offset += strlen(pnames + name_offset) + 1;
61ba1cf9 196 ++pword;
ac45a351
CC
197 if (this->armap_[i].file_offset != last_seen_offset)
198 {
199 last_seen_offset = this->armap_[i].file_offset;
200 ++this->num_members_;
201 }
61ba1cf9
ILT
202 }
203
8383303e 204 if (static_cast<section_size_type>(name_offset) > names_size)
75f2446e
ILT
205 gold_error(_("%s: bad archive symbol table names"),
206 this->name().c_str());
a93d6d07
ILT
207
208 // This array keeps track of which symbols are for archive elements
209 // which we have already included in the link.
210 this->armap_checked_.resize(nsyms);
61ba1cf9
ILT
211}
212
213// Read the header of an archive member at OFF. Fail if something
214// goes wrong. Return the size of the member. Set *PNAME to the name
215// of the member.
216
217off_t
a1207466
CC
218Archive::read_header(off_t off, bool cache, std::string* pname,
219 off_t* nested_off)
61ba1cf9 220{
39d0cb0e
ILT
221 const unsigned char* p = this->get_view(off, sizeof(Archive_header), true,
222 cache);
61ba1cf9 223 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
a1207466 224 return this->interpret_header(hdr, off, pname, nested_off);
4973341a 225}
61ba1cf9 226
4973341a
ILT
227// Interpret the header of HDR, the header of the archive member at
228// file offset OFF. Fail if something goes wrong. Return the size of
229// the member. Set *PNAME to the name of the member.
230
231off_t
232Archive::interpret_header(const Archive_header* hdr, off_t off,
92de84a6 233 std::string* pname, off_t* nested_off) const
4973341a 234{
61ba1cf9
ILT
235 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
236 {
75f2446e
ILT
237 gold_error(_("%s: malformed archive header at %zu"),
238 this->name().c_str(), static_cast<size_t>(off));
239 return this->input_file_->file().filesize() - off;
61ba1cf9
ILT
240 }
241
242 const int size_string_size = sizeof hdr->ar_size;
243 char size_string[size_string_size + 1];
244 memcpy(size_string, hdr->ar_size, size_string_size);
245 char* ps = size_string + size_string_size;
246 while (ps[-1] == ' ')
247 --ps;
248 *ps = '\0';
249
250 errno = 0;
2ea97941
ILT
251 char* end;
252 off_t member_size = strtol(size_string, &end, 10);
253 if (*end != '\0'
61ba1cf9
ILT
254 || member_size < 0
255 || (member_size == LONG_MAX && errno == ERANGE))
256 {
75f2446e
ILT
257 gold_error(_("%s: malformed archive header size at %zu"),
258 this->name().c_str(), static_cast<size_t>(off));
259 return this->input_file_->file().filesize() - off;
61ba1cf9
ILT
260 }
261
262 if (hdr->ar_name[0] != '/')
263 {
264 const char* name_end = strchr(hdr->ar_name, '/');
265 if (name_end == NULL
266 || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name))
267 {
a0c4fb0a 268 gold_error(_("%s: malformed archive header name at %zu"),
75f2446e
ILT
269 this->name().c_str(), static_cast<size_t>(off));
270 return this->input_file_->file().filesize() - off;
61ba1cf9
ILT
271 }
272 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
a1207466
CC
273 if (nested_off != NULL)
274 *nested_off = 0;
61ba1cf9
ILT
275 }
276 else if (hdr->ar_name[1] == ' ')
277 {
278 // This is the symbol table.
114dfbe1
ILT
279 if (!pname->empty())
280 pname->clear();
61ba1cf9
ILT
281 }
282 else if (hdr->ar_name[1] == '/')
283 {
284 // This is the extended name table.
285 pname->assign(1, '/');
286 }
287 else
288 {
289 errno = 0;
2ea97941 290 long x = strtol(hdr->ar_name + 1, &end, 10);
a1207466 291 long y = 0;
2ea97941
ILT
292 if (*end == ':')
293 y = strtol(end + 1, &end, 10);
294 if (*end != ' '
61ba1cf9
ILT
295 || x < 0
296 || (x == LONG_MAX && errno == ERANGE)
297 || static_cast<size_t>(x) >= this->extended_names_.size())
298 {
75f2446e
ILT
299 gold_error(_("%s: bad extended name index at %zu"),
300 this->name().c_str(), static_cast<size_t>(off));
301 return this->input_file_->file().filesize() - off;
61ba1cf9
ILT
302 }
303
2ea97941
ILT
304 const char* name = this->extended_names_.data() + x;
305 const char* name_end = strchr(name, '\n');
306 if (static_cast<size_t>(name_end - name) > this->extended_names_.size()
a1207466 307 || name_end[-1] != '/')
61ba1cf9 308 {
75f2446e
ILT
309 gold_error(_("%s: bad extended name entry at header %zu"),
310 this->name().c_str(), static_cast<size_t>(off));
311 return this->input_file_->file().filesize() - off;
61ba1cf9 312 }
2ea97941 313 pname->assign(name, name_end - 1 - name);
a1207466
CC
314 if (nested_off != NULL)
315 *nested_off = y;
61ba1cf9
ILT
316 }
317
318 return member_size;
319}
320
92de84a6
ILT
321// An archive member iterator.
322
323class Archive::const_iterator
324{
325 public:
326 // The header of an archive member. This is what this iterator
327 // points to.
328 struct Header
329 {
330 // The name of the member.
331 std::string name;
332 // The file offset of the member.
333 off_t off;
334 // The file offset of a nested archive member.
335 off_t nested_off;
336 // The size of the member.
337 off_t size;
338 };
339
2a00e4fb 340 const_iterator(Archive* archive, off_t off)
92de84a6
ILT
341 : archive_(archive), off_(off)
342 { this->read_next_header(); }
343
344 const Header&
345 operator*() const
346 { return this->header_; }
347
348 const Header*
349 operator->() const
350 { return &this->header_; }
351
352 const_iterator&
353 operator++()
354 {
355 if (this->off_ == this->archive_->file().filesize())
356 return *this;
357 this->off_ += sizeof(Archive_header);
358 if (!this->archive_->is_thin_archive())
359 this->off_ += this->header_.size;
360 if ((this->off_ & 1) != 0)
361 ++this->off_;
362 this->read_next_header();
363 return *this;
364 }
365
366 const_iterator
367 operator++(int)
368 {
369 const_iterator ret = *this;
370 ++*this;
371 return ret;
372 }
373
374 bool
375 operator==(const const_iterator p) const
376 { return this->off_ == p->off; }
377
378 bool
379 operator!=(const const_iterator p) const
380 { return this->off_ != p->off; }
381
382 private:
383 void
384 read_next_header();
385
386 // The underlying archive.
2a00e4fb 387 Archive* archive_;
92de84a6
ILT
388 // The current offset in the file.
389 off_t off_;
390 // The current archive header.
391 Header header_;
392};
393
394// Read the next archive header.
4973341a
ILT
395
396void
92de84a6 397Archive::const_iterator::read_next_header()
4973341a 398{
92de84a6 399 off_t filesize = this->archive_->file().filesize();
4973341a
ILT
400 while (true)
401 {
92de84a6
ILT
402 if (filesize - this->off_ < static_cast<off_t>(sizeof(Archive_header)))
403 {
404 if (filesize != this->off_)
405 {
406 gold_error(_("%s: short archive header at %zu"),
407 this->archive_->filename().c_str(),
408 static_cast<size_t>(this->off_));
409 this->off_ = filesize;
410 }
411 this->header_.off = filesize;
412 return;
413 }
414
415 unsigned char buf[sizeof(Archive_header)];
416 this->archive_->file().read(this->off_, sizeof(Archive_header), buf);
417
418 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(buf);
419 this->header_.size =
420 this->archive_->interpret_header(hdr, this->off_, &this->header_.name,
421 &this->header_.nested_off);
422 this->header_.off = this->off_;
423
424 // Skip special members.
425 if (!this->header_.name.empty() && this->header_.name != "/")
426 return;
427
428 this->off_ += sizeof(Archive_header) + this->header_.size;
429 if ((this->off_ & 1) != 0)
430 ++this->off_;
4973341a
ILT
431 }
432}
433
92de84a6
ILT
434// Initial iterator.
435
436Archive::const_iterator
2a00e4fb 437Archive::begin()
92de84a6
ILT
438{
439 return Archive::const_iterator(this, sarmag);
440}
441
442// Final iterator.
443
444Archive::const_iterator
2a00e4fb 445Archive::end()
92de84a6
ILT
446{
447 return Archive::const_iterator(this, this->input_file_->file().filesize());
448}
449
ac45a351
CC
450// Get the file and offset for an archive member, which may be an
451// external member of a thin archive. Set *INPUT_FILE to the
452// file containing the actual member, *MEMOFF to the offset
453// within that file (0 if not a nested archive), and *MEMBER_NAME
454// to the name of the archive member. Return TRUE on success.
455
456bool
2ea97941 457Archive::get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
89fc3421 458 off_t* memsize, std::string* member_name)
ac45a351
CC
459{
460 off_t nested_off;
461
89fc3421 462 *memsize = this->read_header(off, false, member_name, &nested_off);
ac45a351 463
2ea97941 464 *input_file = this->input_file_;
ac45a351
CC
465 *memoff = off + static_cast<off_t>(sizeof(Archive_header));
466
467 if (!this->is_thin_archive_)
468 return true;
469
470 // Adjust a relative pathname so that it is relative
471 // to the directory containing the archive.
472 if (!IS_ABSOLUTE_PATH(member_name->c_str()))
473 {
fbd8a257 474 const char* arch_path = this->filename().c_str();
ac45a351
CC
475 const char* basename = lbasename(arch_path);
476 if (basename > arch_path)
477 member_name->replace(0, 0,
fbd8a257 478 this->filename().substr(0, basename - arch_path));
ac45a351
CC
479 }
480
481 if (nested_off > 0)
482 {
483 // This is a member of a nested archive. Open the containing
484 // archive if we don't already have it open, then do a recursive
485 // call to include the member from that archive.
486 Archive* arch;
487 Nested_archive_table::const_iterator p =
488 this->nested_archives_.find(*member_name);
489 if (p != this->nested_archives_.end())
490 arch = p->second;
491 else
492 {
493 Input_file_argument* input_file_arg =
ae3b5189
CD
494 new Input_file_argument(member_name->c_str(),
495 Input_file_argument::INPUT_FILE_TYPE_FILE,
496 "", false, parameters->options());
2ea97941 497 *input_file = new Input_file(input_file_arg);
15f8229b 498 int dummy = 0;
2ea97941 499 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
ac45a351 500 return false;
2ea97941 501 arch = new Archive(*member_name, *input_file, false, this->dirpath_,
ac45a351 502 this->task_);
15f8229b 503 arch->setup();
ac45a351
CC
504 std::pair<Nested_archive_table::iterator, bool> ins =
505 this->nested_archives_.insert(std::make_pair(*member_name, arch));
506 gold_assert(ins.second);
507 }
2ea97941 508 return arch->get_file_and_offset(nested_off, input_file, memoff,
15f8229b 509 memsize, member_name);
ac45a351
CC
510 }
511
512 // This is an external member of a thin archive. Open the
513 // file as a regular relocatable object file.
514 Input_file_argument* input_file_arg =
ae3b5189
CD
515 new Input_file_argument(member_name->c_str(),
516 Input_file_argument::INPUT_FILE_TYPE_FILE,
517 "", false, this->input_file_->options());
2ea97941 518 *input_file = new Input_file(input_file_arg);
15f8229b 519 int dummy = 0;
2ea97941 520 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
ac45a351
CC
521 return false;
522
523 *memoff = 0;
2ea97941 524 *memsize = (*input_file)->file().filesize();
ac45a351
CC
525 return true;
526}
527
15f8229b
ILT
528// Return an ELF object for the member at offset OFF. If the ELF
529// object has an unsupported target type, set *PUNCONFIGURED to true
530// and return NULL.
ac45a351
CC
531
532Object*
15f8229b 533Archive::get_elf_object_for_member(off_t off, bool* punconfigured)
ac45a351 534{
15f8229b
ILT
535 *punconfigured = false;
536
2ea97941 537 Input_file* input_file;
ac45a351 538 off_t memoff;
89fc3421 539 off_t memsize;
15f8229b 540 std::string member_name;
2ea97941 541 if (!this->get_file_and_offset(off, &input_file, &memoff, &memsize,
15f8229b 542 &member_name))
ac45a351
CC
543 return NULL;
544
89fc3421
CC
545 if (parameters->options().has_plugins())
546 {
2ea97941 547 Object* obj = parameters->options().plugins()->claim_file(input_file,
89fc3421
CC
548 memoff,
549 memsize);
550 if (obj != NULL)
551 {
552 // The input file was claimed by a plugin, and its symbols
553 // have been provided by the plugin.
89fc3421
CC
554 return obj;
555 }
556 }
557
f6060a4d
ILT
558 const unsigned char* ehdr;
559 int read_size;
2ea97941 560 if (!is_elf_object(input_file, memoff, &ehdr, &read_size))
ac45a351
CC
561 {
562 gold_error(_("%s: member at %zu is not an ELF object"),
563 this->name().c_str(), static_cast<size_t>(off));
564 return NULL;
565 }
566
65514900
CC
567 Object *obj = make_elf_object((std::string(this->input_file_->filename())
568 + "(" + member_name + ")"),
2ea97941 569 input_file, memoff, ehdr, read_size,
65514900 570 punconfigured);
029ba973
ILT
571 if (obj == NULL)
572 return NULL;
65514900
CC
573 obj->set_no_export(this->no_export());
574 return obj;
ac45a351
CC
575}
576
577// Read the symbols from all the archive members in the link.
578
579void
15f8229b 580Archive::read_all_symbols()
ac45a351
CC
581{
582 for (Archive::const_iterator p = this->begin();
583 p != this->end();
584 ++p)
15f8229b 585 this->read_symbols(p->off);
ac45a351
CC
586}
587
588// Read the symbols from an archive member in the link. OFF is the file
589// offset of the member header.
590
591void
15f8229b 592Archive::read_symbols(off_t off)
ac45a351 593{
15f8229b
ILT
594 bool dummy;
595 Object* obj = this->get_elf_object_for_member(off, &dummy);
ac45a351
CC
596
597 if (obj == NULL)
598 return;
599
600 Read_symbols_data* sd = new Read_symbols_data;
601 obj->read_symbols(sd);
602 Archive_member member(obj, sd);
603 this->members_[off] = member;
604}
605
b0193076 606Archive::Should_include
88a4108b
ILT
607Archive::should_include_member(Symbol_table* symtab, Layout* layout,
608 const char* sym_name, Symbol** symp,
609 std::string* why, char** tmpbufp,
b0193076 610 size_t* tmpbuflen)
473b7a13
RÁE
611{
612 // In an object file, and therefore in an archive map, an
613 // '@' in the name separates the symbol name from the
614 // version name. If there are two '@' characters, this is
615 // the default version.
616 char* tmpbuf = *tmpbufp;
617 const char* ver = strchr(sym_name, '@');
618 bool def = false;
619 if (ver != NULL)
620 {
621 size_t symlen = ver - sym_name;
622 if (symlen + 1 > *tmpbuflen)
623 {
624 tmpbuf = static_cast<char*>(xrealloc(tmpbuf, symlen + 1));
625 *tmpbufp = tmpbuf;
626 *tmpbuflen = symlen + 1;
627 }
628 memcpy(tmpbuf, sym_name, symlen);
629 tmpbuf[symlen] = '\0';
630 sym_name = tmpbuf;
631
632 ++ver;
633 if (*ver == '@')
634 {
635 ++ver;
636 def = true;
637 }
638 }
639
640 Symbol* sym = symtab->lookup(sym_name, ver);
641 if (def
642 && ver != NULL
643 && (sym == NULL
644 || !sym->is_undefined()
645 || sym->binding() == elfcpp::STB_WEAK))
646 sym = symtab->lookup(sym_name, NULL);
647
648 *symp = sym;
649
650 if (sym == NULL)
651 {
652 // Check whether the symbol was named in a -u option.
88a4108b 653 if (parameters->options().is_undefined(sym_name))
473b7a13
RÁE
654 {
655 *why = "-u ";
656 *why += sym_name;
657 }
88a4108b
ILT
658 else if (layout->script_options()->is_referenced(sym_name))
659 {
660 size_t alc = 100 + strlen(sym_name);
661 char* buf = new char[alc];
662 snprintf(buf, alc, _("script or expression reference to %s"),
663 sym_name);
664 *why = buf;
665 delete[] buf;
666 }
667 else
668 return Archive::SHOULD_INCLUDE_UNKNOWN;
473b7a13
RÁE
669 }
670 else if (!sym->is_undefined())
b0193076 671 return Archive::SHOULD_INCLUDE_NO;
473b7a13 672 else if (sym->binding() == elfcpp::STB_WEAK)
b0193076 673 return Archive::SHOULD_INCLUDE_UNKNOWN;
473b7a13 674
b0193076 675 return Archive::SHOULD_INCLUDE_YES;
473b7a13
RÁE
676}
677
ac45a351
CC
678// Select members from the archive and add them to the link. We walk
679// through the elements in the archive map, and look each one up in
680// the symbol table. If it exists as a strong undefined symbol, we
681// pull in the corresponding element. We have to do this in a loop,
682// since pulling in one element may create new undefined symbols which
15f8229b
ILT
683// may be satisfied by other objects in the archive. Return true in
684// the normal case, false if the first member we tried to add from
685// this archive had an incompatible target.
ac45a351 686
15f8229b 687bool
ac45a351
CC
688Archive::add_symbols(Symbol_table* symtab, Layout* layout,
689 Input_objects* input_objects, Mapfile* mapfile)
690{
691 ++Archive::total_archives;
692
693 if (this->input_file_->options().whole_archive())
694 return this->include_all_members(symtab, layout, input_objects,
695 mapfile);
696
697 Archive::total_members += this->num_members_;
698
699 input_objects->archive_start(this);
700
701 const size_t armap_size = this->armap_.size();
702
703 // This is a quick optimization, since we usually see many symbols
704 // in a row with the same offset. last_seen_offset holds the last
705 // offset we saw that was present in the seen_offsets_ set.
706 off_t last_seen_offset = -1;
707
708 // Track which symbols in the symbol table we've already found to be
709 // defined.
710
9c5b8369
ILT
711 char* tmpbuf = NULL;
712 size_t tmpbuflen = 0;
ac45a351
CC
713 bool added_new_object;
714 do
715 {
716 added_new_object = false;
717 for (size_t i = 0; i < armap_size; ++i)
718 {
719 if (this->armap_checked_[i])
720 continue;
721 if (this->armap_[i].file_offset == last_seen_offset)
722 {
723 this->armap_checked_[i] = true;
724 continue;
725 }
726 if (this->seen_offsets_.find(this->armap_[i].file_offset)
727 != this->seen_offsets_.end())
728 {
729 this->armap_checked_[i] = true;
730 last_seen_offset = this->armap_[i].file_offset;
731 continue;
732 }
733
734 const char* sym_name = (this->armap_names_.data()
735 + this->armap_[i].name_offset);
9c5b8369 736
473b7a13
RÁE
737 Symbol* sym;
738 std::string why;
b0193076 739 Archive::Should_include t =
88a4108b
ILT
740 Archive::should_include_member(symtab, layout, sym_name, &sym,
741 &why, &tmpbuf, &tmpbuflen);
9c5b8369 742
b0193076
RÁE
743 if (t == Archive::SHOULD_INCLUDE_NO
744 || t == Archive::SHOULD_INCLUDE_YES)
473b7a13 745 this->armap_checked_[i] = true;
9c5b8369 746
b0193076 747 if (t != Archive::SHOULD_INCLUDE_YES)
ac45a351
CC
748 continue;
749
750 // We want to include this object in the link.
751 last_seen_offset = this->armap_[i].file_offset;
752 this->seen_offsets_.insert(last_seen_offset);
ac45a351 753
15f8229b
ILT
754 if (!this->include_member(symtab, layout, input_objects,
755 last_seen_offset, mapfile, sym,
756 why.c_str()))
9c5b8369
ILT
757 {
758 if (tmpbuf != NULL)
759 free(tmpbuf);
760 return false;
761 }
ac45a351
CC
762
763 added_new_object = true;
764 }
765 }
766 while (added_new_object);
767
9c5b8369
ILT
768 if (tmpbuf != NULL)
769 free(tmpbuf);
770
ac45a351 771 input_objects->archive_stop(this);
15f8229b
ILT
772
773 return true;
ac45a351
CC
774}
775
92de84a6
ILT
776// Include all the archive members in the link. This is for --whole-archive.
777
15f8229b 778bool
92de84a6
ILT
779Archive::include_all_members(Symbol_table* symtab, Layout* layout,
780 Input_objects* input_objects, Mapfile* mapfile)
781{
782 input_objects->archive_start(this);
783
ac45a351
CC
784 if (this->members_.size() > 0)
785 {
786 std::map<off_t, Archive_member>::const_iterator p;
787 for (p = this->members_.begin();
788 p != this->members_.end();
789 ++p)
790 {
15f8229b
ILT
791 if (!this->include_member(symtab, layout, input_objects, p->first,
792 mapfile, NULL, "--whole-archive"))
793 return false;
ac45a351
CC
794 ++Archive::total_members;
795 }
796 }
797 else
798 {
799 for (Archive::const_iterator p = this->begin();
800 p != this->end();
801 ++p)
802 {
15f8229b
ILT
803 if (!this->include_member(symtab, layout, input_objects, p->off,
804 mapfile, NULL, "--whole-archive"))
805 return false;
ac45a351
CC
806 ++Archive::total_members;
807 }
808 }
92de84a6
ILT
809
810 input_objects->archive_stop(this);
15f8229b
ILT
811
812 return true;
92de84a6
ILT
813}
814
815// Return the number of members in the archive. This is only used for
816// reports.
817
818size_t
2a00e4fb 819Archive::count_members()
92de84a6
ILT
820{
821 size_t ret = 0;
822 for (Archive::const_iterator p = this->begin();
823 p != this->end();
824 ++p)
825 ++ret;
826 return ret;
827}
828
61ba1cf9 829// Include an archive member in the link. OFF is the file offset of
7d9e3d98 830// the member header. WHY is the reason we are including this member.
15f8229b
ILT
831// Return true if we added the member or if we had an error, return
832// false if this was the first member we tried to add from this
833// archive and it had an incompatible format.
61ba1cf9 834
15f8229b 835bool
7e1edb90 836Archive::include_member(Symbol_table* symtab, Layout* layout,
7d9e3d98
ILT
837 Input_objects* input_objects, off_t off,
838 Mapfile* mapfile, Symbol* sym, const char* why)
61ba1cf9 839{
ac45a351 840 ++Archive::total_members_loaded;
7d9e3d98 841
ac45a351
CC
842 std::map<off_t, Archive_member>::const_iterator p = this->members_.find(off);
843 if (p != this->members_.end())
a1207466 844 {
ac45a351 845 Object *obj = p->second.obj_;
15f8229b 846
ac45a351
CC
847 Read_symbols_data *sd = p->second.sd_;
848 if (mapfile != NULL)
849 mapfile->report_include_archive_member(obj->name(), sym, why);
850 if (input_objects->add_object(obj))
a1207466 851 {
ac45a351 852 obj->layout(symtab, layout, sd);
f488e4b0 853 obj->add_symbols(symtab, sd, layout);
15f8229b 854 this->included_member_ = true;
a1207466 855 }
ac45a351 856 delete sd;
15f8229b
ILT
857 return true;
858 }
859
860 bool unconfigured;
861 Object* obj = this->get_elf_object_for_member(off, &unconfigured);
862
863 if (!this->included_member_
864 && this->searched_for()
029ba973
ILT
865 && obj == NULL
866 && unconfigured)
f2d707b5 867 return false;
61ba1cf9 868
ac45a351 869 if (obj == NULL)
15f8229b 870 return true;
61ba1cf9 871
ac45a351
CC
872 if (mapfile != NULL)
873 mapfile->report_include_archive_member(obj->name(), sym, why);
61ba1cf9 874
89fc3421
CC
875 Pluginobj* pluginobj = obj->pluginobj();
876 if (pluginobj != NULL)
877 {
f488e4b0 878 pluginobj->add_symbols(symtab, NULL, layout);
15f8229b
ILT
879 this->included_member_ = true;
880 return true;
89fc3421
CC
881 }
882
15f8229b 883 if (!input_objects->add_object(obj))
f2d707b5
ILT
884 {
885 // If this is an external member of a thin archive, unlock the
886 // file.
887 if (obj->offset() == 0)
888 obj->unlock(this->task_);
889 delete obj;
890 }
15f8229b 891 else
019cdb1a 892 {
00698fc5
CC
893 {
894 Read_symbols_data sd;
895 obj->read_symbols(&sd);
896 obj->layout(symtab, layout, &sd);
897 obj->add_symbols(symtab, &sd, layout);
898 }
fbd8a257
CC
899
900 // If this is an external member of a thin archive, unlock the file
901 // for the next task.
902 if (obj->offset() == 0)
903 obj->unlock(this->task_);
15f8229b
ILT
904
905 this->included_member_ = true;
019cdb1a 906 }
15f8229b
ILT
907
908 return true;
ac45a351 909}
61ba1cf9 910
ac45a351
CC
911// Print statistical information to stderr. This is used for --stats.
912
913void
914Archive::print_stats()
915{
916 fprintf(stderr, _("%s: archive libraries: %u\n"),
917 program_name, Archive::total_archives);
918 fprintf(stderr, _("%s: total archive members: %u\n"),
919 program_name, Archive::total_members);
920 fprintf(stderr, _("%s: loaded archive members: %u\n"),
921 program_name, Archive::total_members_loaded);
61ba1cf9
ILT
922}
923
924// Add_archive_symbols methods.
925
926Add_archive_symbols::~Add_archive_symbols()
927{
928 if (this->this_blocker_ != NULL)
929 delete this->this_blocker_;
930 // next_blocker_ is deleted by the task associated with the next
931 // input file.
932}
933
934// Return whether we can add the archive symbols. We are blocked by
935// this_blocker_. We block next_blocker_. We also lock the file.
936
17a1d0a9
ILT
937Task_token*
938Add_archive_symbols::is_runnable()
61ba1cf9
ILT
939{
940 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
17a1d0a9
ILT
941 return this->this_blocker_;
942 return NULL;
61ba1cf9
ILT
943}
944
17a1d0a9
ILT
945void
946Add_archive_symbols::locks(Task_locker* tl)
61ba1cf9 947{
17a1d0a9
ILT
948 tl->add(this, this->next_blocker_);
949 tl->add(this, this->archive_->token());
61ba1cf9
ILT
950}
951
952void
15f8229b 953Add_archive_symbols::run(Workqueue* workqueue)
61ba1cf9 954{
15f8229b
ILT
955 bool added = this->archive_->add_symbols(this->symtab_, this->layout_,
956 this->input_objects_,
957 this->mapfile_);
a1207466
CC
958 this->archive_->unlock_nested_archives();
959
17a1d0a9 960 this->archive_->release();
39d0cb0e 961 this->archive_->clear_uncached_views();
17a1d0a9 962
15f8229b
ILT
963 if (!added)
964 {
965 // This archive holds object files which are incompatible with
966 // our output file.
967 Read_symbols::incompatible_warning(this->input_argument_,
968 this->archive_->input_file());
969 Read_symbols::requeue(workqueue, this->input_objects_, this->symtab_,
970 this->layout_, this->dirpath_, this->dirindex_,
971 this->mapfile_, this->input_argument_,
972 this->input_group_, this->next_blocker_);
973 delete this->archive_;
974 return;
975 }
976
ead1e424
ILT
977 if (this->input_group_ != NULL)
978 this->input_group_->add_archive(this->archive_);
979 else
980 {
981 // We no longer need to know about this archive.
982 delete this->archive_;
c7912668 983 this->archive_ = NULL;
ead1e424 984 }
61ba1cf9
ILT
985}
986
b0193076
RÁE
987// Class Lib_group static variables.
988unsigned int Lib_group::total_lib_groups;
989unsigned int Lib_group::total_members;
990unsigned int Lib_group::total_members_loaded;
991
992Lib_group::Lib_group(const Input_file_lib* lib, Task* task)
993 : lib_(lib), task_(task), members_()
994{
995 this->members_.resize(lib->size());
996}
997
998// Select members from the lib group and add them to the link. We walk
999// through the the members, and check if each one up should be included.
1000// If the object says it should be included, we do so. We have to do
1001// this in a loop, since including one member may create new undefined
1002// symbols which may be satisfied by other members.
1003
1004void
1005Lib_group::add_symbols(Symbol_table* symtab, Layout* layout,
1006 Input_objects* input_objects)
1007{
1008 ++Lib_group::total_lib_groups;
1009
1010 Lib_group::total_members += this->members_.size();
1011
1012 bool added_new_object;
1013 do
1014 {
1015 added_new_object = false;
1016 unsigned int i = 0;
1017 while (i < this->members_.size())
1018 {
1019 const Archive_member& member = this->members_[i];
1020 Object *obj = member.obj_;
1021 std::string why;
1022
1023 // Skip files with no symbols. Plugin objects have
1024 // member.sd_ == NULL.
1025 if (obj != NULL
1026 && (member.sd_ == NULL || member.sd_->symbol_names != NULL))
1027 {
1028 Archive::Should_include t = obj->should_include_member(symtab,
88a4108b 1029 layout,
b0193076
RÁE
1030 member.sd_,
1031 &why);
1032
1033 if (t != Archive::SHOULD_INCLUDE_YES)
1034 {
1035 ++i;
1036 continue;
1037 }
1038
1039 this->include_member(symtab, layout, input_objects, member);
1040
1041 added_new_object = true;
1042 }
1043 else
1044 {
1045 if (member.sd_ != NULL)
1046 delete member.sd_;
1047 }
1048
1049 this->members_[i] = this->members_.back();
1050 this->members_.pop_back();
1051 }
1052 }
1053 while (added_new_object);
1054}
1055
1056// Include a lib group member in the link.
1057
1058void
1059Lib_group::include_member(Symbol_table* symtab, Layout* layout,
1060 Input_objects* input_objects,
1061 const Archive_member& member)
1062{
1063 ++Lib_group::total_members_loaded;
1064
1065 Object* obj = member.obj_;
1066 gold_assert(obj != NULL);
1067
1068 Pluginobj* pluginobj = obj->pluginobj();
1069 if (pluginobj != NULL)
1070 {
1071 pluginobj->add_symbols(symtab, NULL, layout);
1072 return;
1073 }
1074
1075 Read_symbols_data* sd = member.sd_;
1076 gold_assert(sd != NULL);
1077 obj->lock(this->task_);
1078 if (input_objects->add_object(obj))
1079 {
1080 obj->layout(symtab, layout, sd);
1081 obj->add_symbols(symtab, sd, layout);
1082 // Unlock the file for the next task.
1083 obj->unlock(this->task_);
1084 }
1085 delete sd;
1086}
1087
1088// Print statistical information to stderr. This is used for --stats.
1089
1090void
1091Lib_group::print_stats()
1092{
1093 fprintf(stderr, _("%s: lib groups: %u\n"),
1094 program_name, Lib_group::total_lib_groups);
1095 fprintf(stderr, _("%s: total lib groups members: %u\n"),
1096 program_name, Lib_group::total_members);
1097 fprintf(stderr, _("%s: loaded lib groups members: %u\n"),
1098 program_name, Lib_group::total_members_loaded);
1099}
1100
1101Task_token*
1102Add_lib_group_symbols::is_runnable()
1103{
1104 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1105 return this->this_blocker_;
1106 return NULL;
1107}
1108
1109void
1110Add_lib_group_symbols::locks(Task_locker* tl)
1111{
1112 tl->add(this, this->next_blocker_);
1113}
1114
1115void
1116Add_lib_group_symbols::run(Workqueue*)
1117{
1118 this->lib_->add_symbols(this->symtab_, this->layout_, this->input_objects_);
1119}
1120
1121Add_lib_group_symbols::~Add_lib_group_symbols()
1122{
1123 if (this->this_blocker_ != NULL)
1124 delete this->this_blocker_;
1125 // next_blocker_ is deleted by the task associated with the next
1126 // input file.
1127}
1128
61ba1cf9 1129} // End namespace gold.
This page took 0.233392 seconds and 4 git commands to generate.