PR binutils/11742
[binutils.git] / gold / archive.cc
blobf1000a195de25c38588ae3960188767cc7fae337
1 // archive.cc -- archive support for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010 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 #include "gold.h"
25 #include <cerrno>
26 #include <cstring>
27 #include <climits>
28 #include <vector>
29 #include "libiberty.h"
30 #include "filenames.h"
32 #include "elfcpp.h"
33 #include "options.h"
34 #include "mapfile.h"
35 #include "fileread.h"
36 #include "readsyms.h"
37 #include "symtab.h"
38 #include "object.h"
39 #include "layout.h"
40 #include "archive.h"
41 #include "plugin.h"
43 namespace gold
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.
50 struct Archive::Archive_header
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];
68 // Class Archive static variables.
69 unsigned int Archive::total_archives;
70 unsigned int Archive::total_members;
71 unsigned int Archive::total_members_loaded;
73 // Archive methods.
75 const char Archive::armag[sarmag] =
77 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
80 const char Archive::armagt[sarmag] =
82 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
85 const char Archive::arfmag[2] = { '`', '\n' };
87 Archive::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_(),
90 extended_names_(), armap_checked_(), seen_offsets_(), members_(),
91 is_thin_archive_(is_thin_archive), included_member_(false),
92 nested_archives_(), dirpath_(dirpath), task_(task), num_members_(0)
94 this->no_export_ =
95 parameters->options().check_excluded_libs(input_file->found_name());
98 // Set up the archive: read the symbol map and the extended name
99 // table.
101 void
102 Archive::setup()
104 // We need to ignore empty archives.
105 if (this->input_file_->file().filesize() == sarmag)
106 return;
108 // The first member of the archive should be the symbol table.
109 std::string armap_name;
110 section_size_type armap_size =
111 convert_to_section_size_type(this->read_header(sarmag, false,
112 &armap_name, NULL));
113 off_t off = sarmag;
114 if (armap_name.empty())
116 this->read_armap(sarmag + sizeof(Archive_header), armap_size);
117 off = sarmag + sizeof(Archive_header) + armap_size;
119 else if (!this->input_file_->options().whole_archive())
120 gold_error(_("%s: no archive symbol table (run ranlib)"),
121 this->name().c_str());
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.
126 if ((off & 1) != 0)
127 ++off;
128 std::string xname;
129 section_size_type extended_size =
130 convert_to_section_size_type(this->read_header(off, true, &xname, NULL));
131 if (xname == "/")
133 const unsigned char* p = this->get_view(off + sizeof(Archive_header),
134 extended_size, false, true);
135 const char* px = reinterpret_cast<const char*>(p);
136 this->extended_names_.assign(px, extended_size);
138 bool preread_syms = (parameters->options().threads()
139 && parameters->options().preread_archive_symbols());
140 #ifndef ENABLE_THREADS
141 preread_syms = false;
142 #else
143 if (parameters->options().has_plugins())
144 preread_syms = false;
145 #endif
146 if (preread_syms)
147 this->read_all_symbols();
150 // Unlock any nested archives.
152 void
153 Archive::unlock_nested_archives()
155 for (Nested_archive_table::iterator p = this->nested_archives_.begin();
156 p != this->nested_archives_.end();
157 ++p)
159 p->second->unlock(this->task_);
163 // Read the archive symbol map.
165 void
166 Archive::read_armap(off_t start, section_size_type size)
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;
174 // Read in the entire armap.
175 const unsigned char* p = this->get_view(start, size, true, false);
177 // Numbers in the armap are always big-endian.
178 const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p);
179 unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword);
180 ++pword;
182 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
183 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
184 section_size_type names_size =
185 reinterpret_cast<const char*>(p) + size - pnames;
186 this->armap_names_.assign(pnames, names_size);
188 this->armap_.resize(nsyms);
190 section_offset_type name_offset = 0;
191 for (unsigned int i = 0; i < nsyms; ++i)
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;
196 ++pword;
197 if (this->armap_[i].file_offset != last_seen_offset)
199 last_seen_offset = this->armap_[i].file_offset;
200 ++this->num_members_;
204 if (static_cast<section_size_type>(name_offset) > names_size)
205 gold_error(_("%s: bad archive symbol table names"),
206 this->name().c_str());
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);
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.
217 off_t
218 Archive::read_header(off_t off, bool cache, std::string* pname,
219 off_t* nested_off)
221 const unsigned char* p = this->get_view(off, sizeof(Archive_header), true,
222 cache);
223 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
224 return this->interpret_header(hdr, off, pname, nested_off);
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.
231 off_t
232 Archive::interpret_header(const Archive_header* hdr, off_t off,
233 std::string* pname, off_t* nested_off) const
235 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
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;
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';
250 errno = 0;
251 char* end;
252 off_t member_size = strtol(size_string, &end, 10);
253 if (*end != '\0'
254 || member_size < 0
255 || (member_size == LONG_MAX && errno == ERANGE))
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;
262 if (hdr->ar_name[0] != '/')
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))
268 gold_error(_("%s: malformed archive header name at %zu"),
269 this->name().c_str(), static_cast<size_t>(off));
270 return this->input_file_->file().filesize() - off;
272 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
273 if (nested_off != NULL)
274 *nested_off = 0;
276 else if (hdr->ar_name[1] == ' ')
278 // This is the symbol table.
279 if (!pname->empty())
280 pname->clear();
282 else if (hdr->ar_name[1] == '/')
284 // This is the extended name table.
285 pname->assign(1, '/');
287 else
289 errno = 0;
290 long x = strtol(hdr->ar_name + 1, &end, 10);
291 long y = 0;
292 if (*end == ':')
293 y = strtol(end + 1, &end, 10);
294 if (*end != ' '
295 || x < 0
296 || (x == LONG_MAX && errno == ERANGE)
297 || static_cast<size_t>(x) >= this->extended_names_.size())
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;
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()
307 || name_end[-1] != '/')
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;
313 pname->assign(name, name_end - 1 - name);
314 if (nested_off != NULL)
315 *nested_off = y;
318 return member_size;
321 // An archive member iterator.
323 class Archive::const_iterator
325 public:
326 // The header of an archive member. This is what this iterator
327 // points to.
328 struct Header
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;
340 const_iterator(Archive* archive, off_t off)
341 : archive_(archive), off_(off)
342 { this->read_next_header(); }
344 const Header&
345 operator*() const
346 { return this->header_; }
348 const Header*
349 operator->() const
350 { return &this->header_; }
352 const_iterator&
353 operator++()
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;
366 const_iterator
367 operator++(int)
369 const_iterator ret = *this;
370 ++*this;
371 return ret;
374 bool
375 operator==(const const_iterator p) const
376 { return this->off_ == p->off; }
378 bool
379 operator!=(const const_iterator p) const
380 { return this->off_ != p->off; }
382 private:
383 void
384 read_next_header();
386 // The underlying archive.
387 Archive* archive_;
388 // The current offset in the file.
389 off_t off_;
390 // The current archive header.
391 Header header_;
394 // Read the next archive header.
396 void
397 Archive::const_iterator::read_next_header()
399 off_t filesize = this->archive_->file().filesize();
400 while (true)
402 if (filesize - this->off_ < static_cast<off_t>(sizeof(Archive_header)))
404 if (filesize != this->off_)
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;
411 this->header_.off = filesize;
412 return;
415 unsigned char buf[sizeof(Archive_header)];
416 this->archive_->file().read(this->off_, sizeof(Archive_header), buf);
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_;
424 // Skip special members.
425 if (!this->header_.name.empty() && this->header_.name != "/")
426 return;
428 this->off_ += sizeof(Archive_header) + this->header_.size;
429 if ((this->off_ & 1) != 0)
430 ++this->off_;
434 // Initial iterator.
436 Archive::const_iterator
437 Archive::begin()
439 return Archive::const_iterator(this, sarmag);
442 // Final iterator.
444 Archive::const_iterator
445 Archive::end()
447 return Archive::const_iterator(this, this->input_file_->file().filesize());
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.
456 bool
457 Archive::get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
458 off_t* memsize, std::string* member_name)
460 off_t nested_off;
462 *memsize = this->read_header(off, false, member_name, &nested_off);
464 *input_file = this->input_file_;
465 *memoff = off + static_cast<off_t>(sizeof(Archive_header));
467 if (!this->is_thin_archive_)
468 return true;
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()))
474 const char* arch_path = this->filename().c_str();
475 const char* basename = lbasename(arch_path);
476 if (basename > arch_path)
477 member_name->replace(0, 0,
478 this->filename().substr(0, basename - arch_path));
481 if (nested_off > 0)
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
493 Input_file_argument* input_file_arg =
494 new Input_file_argument(member_name->c_str(),
495 Input_file_argument::INPUT_FILE_TYPE_FILE,
496 "", false, parameters->options());
497 *input_file = new Input_file(input_file_arg);
498 int dummy = 0;
499 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
500 return false;
501 arch = new Archive(*member_name, *input_file, false, this->dirpath_,
502 this->task_);
503 arch->setup();
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);
508 return arch->get_file_and_offset(nested_off, input_file, memoff,
509 memsize, member_name);
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 =
515 new Input_file_argument(member_name->c_str(),
516 Input_file_argument::INPUT_FILE_TYPE_FILE,
517 "", false, this->input_file_->options());
518 *input_file = new Input_file(input_file_arg);
519 int dummy = 0;
520 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
521 return false;
523 *memoff = 0;
524 *memsize = (*input_file)->file().filesize();
525 return true;
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.
532 Object*
533 Archive::get_elf_object_for_member(off_t off, bool* punconfigured)
535 *punconfigured = false;
537 Input_file* input_file;
538 off_t memoff;
539 off_t memsize;
540 std::string member_name;
541 if (!this->get_file_and_offset(off, &input_file, &memoff, &memsize,
542 &member_name))
543 return NULL;
545 if (parameters->options().has_plugins())
547 Object* obj = parameters->options().plugins()->claim_file(input_file,
548 memoff,
549 memsize);
550 if (obj != NULL)
552 // The input file was claimed by a plugin, and its symbols
553 // have been provided by the plugin.
554 return obj;
558 const unsigned char* ehdr;
559 int read_size;
560 if (!is_elf_object(input_file, memoff, &ehdr, &read_size))
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;
567 Object *obj = make_elf_object((std::string(this->input_file_->filename())
568 + "(" + member_name + ")"),
569 input_file, memoff, ehdr, read_size,
570 punconfigured);
571 if (obj == NULL)
572 return NULL;
573 obj->set_no_export(this->no_export());
574 return obj;
577 // Read the symbols from all the archive members in the link.
579 void
580 Archive::read_all_symbols()
582 for (Archive::const_iterator p = this->begin();
583 p != this->end();
584 ++p)
585 this->read_symbols(p->off);
588 // Read the symbols from an archive member in the link. OFF is the file
589 // offset of the member header.
591 void
592 Archive::read_symbols(off_t off)
594 bool dummy;
595 Object* obj = this->get_elf_object_for_member(off, &dummy);
597 if (obj == NULL)
598 return;
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;
606 Archive::Should_include
607 Archive::should_include_member(Symbol_table* symtab, Layout* layout,
608 const char* sym_name, Symbol** symp,
609 std::string* why, char** tmpbufp,
610 size_t* tmpbuflen)
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)
621 size_t symlen = ver - sym_name;
622 if (symlen + 1 > *tmpbuflen)
624 tmpbuf = static_cast<char*>(xrealloc(tmpbuf, symlen + 1));
625 *tmpbufp = tmpbuf;
626 *tmpbuflen = symlen + 1;
628 memcpy(tmpbuf, sym_name, symlen);
629 tmpbuf[symlen] = '\0';
630 sym_name = tmpbuf;
632 ++ver;
633 if (*ver == '@')
635 ++ver;
636 def = true;
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);
648 *symp = sym;
650 if (sym == NULL)
652 // Check whether the symbol was named in a -u option.
653 if (parameters->options().is_undefined(sym_name))
655 *why = "-u ";
656 *why += sym_name;
658 else if (layout->script_options()->is_referenced(sym_name))
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;
667 else
668 return Archive::SHOULD_INCLUDE_UNKNOWN;
670 else if (!sym->is_undefined())
671 return Archive::SHOULD_INCLUDE_NO;
672 else if (sym->binding() == elfcpp::STB_WEAK)
673 return Archive::SHOULD_INCLUDE_UNKNOWN;
675 return Archive::SHOULD_INCLUDE_YES;
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
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.
687 bool
688 Archive::add_symbols(Symbol_table* symtab, Layout* layout,
689 Input_objects* input_objects, Mapfile* mapfile)
691 ++Archive::total_archives;
693 if (this->input_file_->options().whole_archive())
694 return this->include_all_members(symtab, layout, input_objects,
695 mapfile);
697 Archive::total_members += this->num_members_;
699 input_objects->archive_start(this);
701 const size_t armap_size = this->armap_.size();
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;
708 // Track which symbols in the symbol table we've already found to be
709 // defined.
711 char* tmpbuf = NULL;
712 size_t tmpbuflen = 0;
713 bool added_new_object;
716 added_new_object = false;
717 for (size_t i = 0; i < armap_size; ++i)
719 if (this->armap_checked_[i])
720 continue;
721 if (this->armap_[i].file_offset == last_seen_offset)
723 this->armap_checked_[i] = true;
724 continue;
726 if (this->seen_offsets_.find(this->armap_[i].file_offset)
727 != this->seen_offsets_.end())
729 this->armap_checked_[i] = true;
730 last_seen_offset = this->armap_[i].file_offset;
731 continue;
734 const char* sym_name = (this->armap_names_.data()
735 + this->armap_[i].name_offset);
737 Symbol* sym;
738 std::string why;
739 Archive::Should_include t =
740 Archive::should_include_member(symtab, layout, sym_name, &sym,
741 &why, &tmpbuf, &tmpbuflen);
743 if (t == Archive::SHOULD_INCLUDE_NO
744 || t == Archive::SHOULD_INCLUDE_YES)
745 this->armap_checked_[i] = true;
747 if (t != Archive::SHOULD_INCLUDE_YES)
748 continue;
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);
754 if (!this->include_member(symtab, layout, input_objects,
755 last_seen_offset, mapfile, sym,
756 why.c_str()))
758 if (tmpbuf != NULL)
759 free(tmpbuf);
760 return false;
763 added_new_object = true;
766 while (added_new_object);
768 if (tmpbuf != NULL)
769 free(tmpbuf);
771 input_objects->archive_stop(this);
773 return true;
776 // Include all the archive members in the link. This is for --whole-archive.
778 bool
779 Archive::include_all_members(Symbol_table* symtab, Layout* layout,
780 Input_objects* input_objects, Mapfile* mapfile)
782 input_objects->archive_start(this);
784 if (this->members_.size() > 0)
786 std::map<off_t, Archive_member>::const_iterator p;
787 for (p = this->members_.begin();
788 p != this->members_.end();
789 ++p)
791 if (!this->include_member(symtab, layout, input_objects, p->first,
792 mapfile, NULL, "--whole-archive"))
793 return false;
794 ++Archive::total_members;
797 else
799 for (Archive::const_iterator p = this->begin();
800 p != this->end();
801 ++p)
803 if (!this->include_member(symtab, layout, input_objects, p->off,
804 mapfile, NULL, "--whole-archive"))
805 return false;
806 ++Archive::total_members;
810 input_objects->archive_stop(this);
812 return true;
815 // Return the number of members in the archive. This is only used for
816 // reports.
818 size_t
819 Archive::count_members()
821 size_t ret = 0;
822 for (Archive::const_iterator p = this->begin();
823 p != this->end();
824 ++p)
825 ++ret;
826 return ret;
829 // Include an archive member in the link. OFF is the file offset of
830 // the member header. WHY is the reason we are including this member.
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.
835 bool
836 Archive::include_member(Symbol_table* symtab, Layout* layout,
837 Input_objects* input_objects, off_t off,
838 Mapfile* mapfile, Symbol* sym, const char* why)
840 ++Archive::total_members_loaded;
842 std::map<off_t, Archive_member>::const_iterator p = this->members_.find(off);
843 if (p != this->members_.end())
845 Object *obj = p->second.obj_;
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))
852 obj->layout(symtab, layout, sd);
853 obj->add_symbols(symtab, sd, layout);
854 this->included_member_ = true;
856 delete sd;
857 return true;
860 bool unconfigured;
861 Object* obj = this->get_elf_object_for_member(off, &unconfigured);
863 if (!this->included_member_
864 && this->searched_for()
865 && obj == NULL
866 && unconfigured)
867 return false;
869 if (obj == NULL)
870 return true;
872 if (mapfile != NULL)
873 mapfile->report_include_archive_member(obj->name(), sym, why);
875 Pluginobj* pluginobj = obj->pluginobj();
876 if (pluginobj != NULL)
878 pluginobj->add_symbols(symtab, NULL, layout);
879 this->included_member_ = true;
880 return true;
883 if (!input_objects->add_object(obj))
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;
891 else
894 Read_symbols_data sd;
895 obj->read_symbols(&sd);
896 obj->layout(symtab, layout, &sd);
897 obj->add_symbols(symtab, &sd, layout);
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_);
905 this->included_member_ = true;
908 return true;
911 // Print statistical information to stderr. This is used for --stats.
913 void
914 Archive::print_stats()
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);
924 // Add_archive_symbols methods.
926 Add_archive_symbols::~Add_archive_symbols()
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.
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.
937 Task_token*
938 Add_archive_symbols::is_runnable()
940 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
941 return this->this_blocker_;
942 return NULL;
945 void
946 Add_archive_symbols::locks(Task_locker* tl)
948 tl->add(this, this->next_blocker_);
949 tl->add(this, this->archive_->token());
952 void
953 Add_archive_symbols::run(Workqueue* workqueue)
955 bool added = this->archive_->add_symbols(this->symtab_, this->layout_,
956 this->input_objects_,
957 this->mapfile_);
958 this->archive_->unlock_nested_archives();
960 this->archive_->release();
961 this->archive_->clear_uncached_views();
963 if (!added)
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;
977 if (this->input_group_ != NULL)
978 this->input_group_->add_archive(this->archive_);
979 else
981 // We no longer need to know about this archive.
982 delete this->archive_;
983 this->archive_ = NULL;
987 // Class Lib_group static variables.
988 unsigned int Lib_group::total_lib_groups;
989 unsigned int Lib_group::total_members;
990 unsigned int Lib_group::total_members_loaded;
992 Lib_group::Lib_group(const Input_file_lib* lib, Task* task)
993 : lib_(lib), task_(task), members_()
995 this->members_.resize(lib->size());
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.
1004 void
1005 Lib_group::add_symbols(Symbol_table* symtab, Layout* layout,
1006 Input_objects* input_objects)
1008 ++Lib_group::total_lib_groups;
1010 Lib_group::total_members += this->members_.size();
1012 bool added_new_object;
1015 added_new_object = false;
1016 unsigned int i = 0;
1017 while (i < this->members_.size())
1019 const Archive_member& member = this->members_[i];
1020 Object *obj = member.obj_;
1021 std::string why;
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))
1028 Archive::Should_include t = obj->should_include_member(symtab,
1029 layout,
1030 member.sd_,
1031 &why);
1033 if (t != Archive::SHOULD_INCLUDE_YES)
1035 ++i;
1036 continue;
1039 this->include_member(symtab, layout, input_objects, member);
1041 added_new_object = true;
1043 else
1045 if (member.sd_ != NULL)
1046 delete member.sd_;
1049 this->members_[i] = this->members_.back();
1050 this->members_.pop_back();
1053 while (added_new_object);
1056 // Include a lib group member in the link.
1058 void
1059 Lib_group::include_member(Symbol_table* symtab, Layout* layout,
1060 Input_objects* input_objects,
1061 const Archive_member& member)
1063 ++Lib_group::total_members_loaded;
1065 Object* obj = member.obj_;
1066 gold_assert(obj != NULL);
1068 Pluginobj* pluginobj = obj->pluginobj();
1069 if (pluginobj != NULL)
1071 pluginobj->add_symbols(symtab, NULL, layout);
1072 return;
1075 Read_symbols_data* sd = member.sd_;
1076 gold_assert(sd != NULL);
1077 obj->lock(this->task_);
1078 if (input_objects->add_object(obj))
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_);
1085 delete sd;
1088 // Print statistical information to stderr. This is used for --stats.
1090 void
1091 Lib_group::print_stats()
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);
1101 Task_token*
1102 Add_lib_group_symbols::is_runnable()
1104 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1105 return this->this_blocker_;
1106 return NULL;
1109 void
1110 Add_lib_group_symbols::locks(Task_locker* tl)
1112 tl->add(this, this->next_blocker_);
1115 void
1116 Add_lib_group_symbols::run(Workqueue*)
1118 this->lib_->add_symbols(this->symtab_, this->layout_, this->input_objects_);
1121 Add_lib_group_symbols::~Add_lib_group_symbols()
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.
1129 } // End namespace gold.