Add multilib.am to the list of top level files included in any release created by...
[binutils-gdb.git] / gold / archive.cc
blob3318ba3a00807f9f19c1ba2b5921d84df6ff8b5d
1 // archive.cc -- archive support for gold
3 // Copyright (C) 2006-2024 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"
42 #include "incremental.h"
44 namespace gold
47 // Library_base methods.
49 // Determine whether a definition of SYM_NAME should cause an archive
50 // library member to be included in the link. Returns SHOULD_INCLUDE_YES
51 // if the symbol is referenced but not defined, SHOULD_INCLUDE_NO if the
52 // symbol is already defined, and SHOULD_INCLUDE_UNKNOWN if the symbol is
53 // neither referenced nor defined.
55 Library_base::Should_include
56 Library_base::should_include_member(Symbol_table* symtab, Layout* layout,
57 const char* sym_name, Symbol** symp,
58 std::string* why, char** tmpbufp,
59 size_t* tmpbuflen)
61 // In an object file, and therefore in an archive map, an
62 // '@' in the name separates the symbol name from the
63 // version name. If there are two '@' characters, this is
64 // the default version.
65 char* tmpbuf = *tmpbufp;
66 const char* ver = strchr(sym_name, '@');
67 bool def = false;
68 if (ver != NULL)
70 size_t symlen = ver - sym_name;
71 if (symlen + 1 > *tmpbuflen)
73 tmpbuf = static_cast<char*>(xrealloc(tmpbuf, symlen + 1));
74 *tmpbufp = tmpbuf;
75 *tmpbuflen = symlen + 1;
77 memcpy(tmpbuf, sym_name, symlen);
78 tmpbuf[symlen] = '\0';
79 sym_name = tmpbuf;
81 ++ver;
82 if (*ver == '@')
84 ++ver;
85 def = true;
89 Symbol* sym = symtab->lookup(sym_name, ver);
90 if (def
91 && ver != NULL
92 && (sym == NULL
93 || !sym->is_undefined()
94 || sym->binding() == elfcpp::STB_WEAK))
95 sym = symtab->lookup(sym_name, NULL);
97 *symp = sym;
99 if (sym != NULL)
101 if (!sym->is_undefined())
102 return Library_base::SHOULD_INCLUDE_NO;
104 // PR 12001: Do not include an archive when the undefined
105 // symbol has actually been defined on the command line.
106 if (layout->script_options()->is_pending_assignment(sym_name))
107 return Library_base::SHOULD_INCLUDE_NO;
109 // If the symbol is weak undefined, we still need to check
110 // for other reasons (like a -u option).
111 if (sym->binding() != elfcpp::STB_WEAK)
112 return Library_base::SHOULD_INCLUDE_YES;
115 // Check whether the symbol was named in a -u option.
116 if (parameters->options().is_undefined(sym_name))
118 *why = "-u ";
119 *why += sym_name;
120 return Library_base::SHOULD_INCLUDE_YES;
123 if (layout->script_options()->is_referenced(sym_name))
125 size_t alc = 100 + strlen(sym_name);
126 char* buf = new char[alc];
127 snprintf(buf, alc, _("script or expression reference to %s"),
128 sym_name);
129 *why = buf;
130 delete[] buf;
131 return Library_base::SHOULD_INCLUDE_YES;
134 if (!parameters->options().relocatable())
136 const char* entry_sym = parameters->entry();
137 if (entry_sym != NULL && strcmp(sym_name, entry_sym) == 0)
139 *why = "entry symbol ";
140 *why += sym_name;
141 return Library_base::SHOULD_INCLUDE_YES;
145 return Library_base::SHOULD_INCLUDE_UNKNOWN;
148 // The header of an entry in the archive. This is all readable text,
149 // padded with spaces where necessary. If the contents of an archive
150 // are all text file, the entire archive is readable.
152 struct Archive::Archive_header
154 // The entry name.
155 char ar_name[16];
156 // The file modification time.
157 char ar_date[12];
158 // The user's UID in decimal.
159 char ar_uid[6];
160 // The user's GID in decimal.
161 char ar_gid[6];
162 // The file mode in octal.
163 char ar_mode[8];
164 // The file size in decimal.
165 char ar_size[10];
166 // The final magic code.
167 char ar_fmag[2];
170 // Class Archive static variables.
171 unsigned int Archive::total_archives;
172 unsigned int Archive::total_members;
173 unsigned int Archive::total_members_loaded;
175 // Archive methods.
177 const char Archive::armag[sarmag] =
179 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
182 const char Archive::armagt[sarmag] =
184 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
187 const char Archive::arfmag[2] = { '`', '\n' };
189 const char Archive::sym64name[7] = { '/', 'S', 'Y', 'M', '6', '4', '/' };
191 Archive::Archive(const std::string& name, Input_file* input_file,
192 bool is_thin_archive, Dirsearch* dirpath, Task* task)
193 : Library_base(task), name_(name), input_file_(input_file), armap_(),
194 armap_names_(), extended_names_(), armap_checked_(), seen_offsets_(),
195 members_(), is_thin_archive_(is_thin_archive), included_member_(false),
196 nested_archives_(), dirpath_(dirpath), num_members_(0),
197 included_all_members_(false)
199 this->no_export_ =
200 parameters->options().check_excluded_libs(input_file->found_name());
203 // Set up the archive: read the symbol map and the extended name
204 // table.
206 void
207 Archive::setup()
209 // We need to ignore empty archives.
210 if (this->input_file_->file().filesize() == sarmag)
211 return;
213 // The first member of the archive should be the symbol table.
214 std::string armap_name;
215 off_t header_size = this->read_header(sarmag, false, &armap_name, NULL);
216 if (header_size == -1)
217 return;
219 section_size_type armap_size = convert_to_section_size_type(header_size);
220 off_t off = sarmag;
221 if (armap_name.empty())
223 this->read_armap<32>(sarmag + sizeof(Archive_header), armap_size);
224 off = sarmag + sizeof(Archive_header) + armap_size;
226 else if (armap_name == "/SYM64/")
228 this->read_armap<64>(sarmag + sizeof(Archive_header), armap_size);
229 off = sarmag + sizeof(Archive_header) + armap_size;
231 else if (!this->input_file_->options().whole_archive())
232 gold_error(_("%s: no archive symbol table (run ranlib)"),
233 this->name().c_str());
235 // See if there is an extended name table. We cache these views
236 // because it is likely that we will want to read the following
237 // header in the add_symbols routine.
238 if ((off & 1) != 0)
239 ++off;
240 std::string xname;
241 header_size = this->read_header(off, true, &xname, NULL);
242 if (header_size == -1)
243 return;
245 section_size_type extended_size = convert_to_section_size_type(header_size);
246 if (xname == "/")
248 const unsigned char* p = this->get_view(off + sizeof(Archive_header),
249 extended_size, false, true);
250 const char* px = reinterpret_cast<const char*>(p);
251 this->extended_names_.assign(px, extended_size);
253 bool preread_syms = (parameters->options().threads()
254 && parameters->options().preread_archive_symbols());
255 #ifndef ENABLE_THREADS
256 preread_syms = false;
257 #else
258 if (parameters->options().has_plugins())
259 preread_syms = false;
260 #endif
261 if (preread_syms)
262 this->read_all_symbols();
265 // Unlock any nested archives.
267 void
268 Archive::unlock_nested_archives()
270 for (Nested_archive_table::iterator p = this->nested_archives_.begin();
271 p != this->nested_archives_.end();
272 ++p)
274 p->second->unlock(this->task_);
278 // Read the archive symbol map.
280 template<int mapsize>
281 void
282 Archive::read_armap(off_t start, section_size_type size)
284 // To count the total number of archive members, we'll just count
285 // the number of times the file offset changes. Since most archives
286 // group the symbols in the armap by object, this ought to give us
287 // an accurate count.
288 off_t last_seen_offset = -1;
290 // Read in the entire armap.
291 const unsigned char* p = this->get_view(start, size, true, false);
293 // Numbers in the armap are always big-endian.
294 typedef typename elfcpp::Elf_types<mapsize>::Elf_Addr Entry_type;
295 const Entry_type* pword = reinterpret_cast<const Entry_type*>(p);
296 unsigned long nsyms = convert_types<unsigned long, Entry_type>(
297 elfcpp::Swap<mapsize, true>::readval(pword));
298 ++pword;
300 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
301 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
302 section_size_type names_size =
303 reinterpret_cast<const char*>(p) + size - pnames;
304 this->armap_names_.assign(pnames, names_size);
306 this->armap_.resize(nsyms);
308 section_offset_type name_offset = 0;
309 for (unsigned long i = 0; i < nsyms; ++i)
311 this->armap_[i].name_offset = name_offset;
312 this->armap_[i].file_offset = convert_types<off_t, Entry_type>(
313 elfcpp::Swap<mapsize, true>::readval(pword));
314 name_offset += strlen(pnames + name_offset) + 1;
315 ++pword;
316 if (this->armap_[i].file_offset != last_seen_offset)
318 last_seen_offset = this->armap_[i].file_offset;
319 ++this->num_members_;
323 if (static_cast<section_size_type>(name_offset) > names_size)
324 gold_error(_("%s: bad archive symbol table names"),
325 this->name().c_str());
327 // This array keeps track of which symbols are for archive elements
328 // which we have already included in the link.
329 this->armap_checked_.resize(nsyms);
332 // Read the header of an archive member at OFF. Fail if something
333 // goes wrong. Return the size of the member. Set *PNAME to the name
334 // of the member.
336 off_t
337 Archive::read_header(off_t off, bool cache, std::string* pname,
338 off_t* nested_off)
340 const unsigned char* p = this->get_view(off, sizeof(Archive_header), true,
341 cache);
342 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
343 return this->interpret_header(hdr, off, pname, nested_off);
346 // Interpret the header of HDR, the header of the archive member at
347 // file offset OFF. Return the size of the member, or -1 if something
348 // has gone wrong. Set *PNAME to the name of the member.
350 off_t
351 Archive::interpret_header(const Archive_header* hdr, off_t off,
352 std::string* pname, off_t* nested_off) const
354 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
356 gold_error(_("%s: malformed archive header at %zu"),
357 this->name().c_str(), static_cast<size_t>(off));
358 return -1;
361 const int size_string_size = sizeof hdr->ar_size;
362 char size_string[size_string_size + 1];
363 memcpy(size_string, hdr->ar_size, size_string_size);
364 char* ps = size_string + size_string_size;
365 while (ps[-1] == ' ')
366 --ps;
367 *ps = '\0';
369 errno = 0;
370 char* end;
371 off_t member_size = strtol(size_string, &end, 10);
372 if (*end != '\0'
373 || member_size < 0
374 || (member_size == LONG_MAX && errno == ERANGE))
376 gold_error(_("%s: malformed archive header size at %zu"),
377 this->name().c_str(), static_cast<size_t>(off));
378 return -1;
381 if (hdr->ar_name[0] != '/')
383 const char* name_end = strchr(hdr->ar_name, '/');
384 if (name_end == NULL
385 || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name))
387 gold_error(_("%s: malformed archive header name at %zu"),
388 this->name().c_str(), static_cast<size_t>(off));
389 return -1;
391 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
392 if (nested_off != NULL)
393 *nested_off = 0;
395 else if (hdr->ar_name[1] == ' ')
397 // This is the symbol table.
398 if (!pname->empty())
399 pname->clear();
401 else if (memcmp(hdr->ar_name, sym64name, sizeof sym64name) == 0)
403 // This is the symbol table, 64-bit version.
404 pname->assign(sym64name, sizeof sym64name);
406 else if (hdr->ar_name[1] == '/')
408 // This is the extended name table.
409 pname->assign(1, '/');
411 else
413 errno = 0;
414 long x = strtol(hdr->ar_name + 1, &end, 10);
415 long y = 0;
416 if (*end == ':')
417 y = strtol(end + 1, &end, 10);
418 if (*end != ' '
419 || x < 0
420 || (x == LONG_MAX && errno == ERANGE)
421 || static_cast<size_t>(x) >= this->extended_names_.size())
423 gold_error(_("%s: bad extended name index at %zu"),
424 this->name().c_str(), static_cast<size_t>(off));
425 return -1;
428 const char* name = this->extended_names_.data() + x;
429 const char* name_end = strchr(name, '\n');
430 if (static_cast<size_t>(name_end - name) > this->extended_names_.size()
431 || name_end[-1] != '/')
433 gold_error(_("%s: bad extended name entry at header %zu"),
434 this->name().c_str(), static_cast<size_t>(off));
435 return -1;
437 pname->assign(name, name_end - 1 - name);
438 if (nested_off != NULL)
439 *nested_off = y;
442 return member_size;
445 // An archive member iterator.
447 class Archive::const_iterator
449 public:
450 // The header of an archive member. This is what this iterator
451 // points to.
452 struct Header
454 // The name of the member.
455 std::string name;
456 // The file offset of the member.
457 off_t off;
458 // The file offset of a nested archive member.
459 off_t nested_off;
460 // The size of the member.
461 off_t size;
464 const_iterator(Archive* archive, off_t off)
465 : archive_(archive), off_(off)
466 { this->read_next_header(); }
468 const Header&
469 operator*() const
470 { return this->header_; }
472 const Header*
473 operator->() const
474 { return &this->header_; }
476 const_iterator&
477 operator++()
479 if (this->off_ == this->archive_->file().filesize())
480 return *this;
481 this->off_ += sizeof(Archive_header);
482 if (!this->archive_->is_thin_archive())
483 this->off_ += this->header_.size;
484 if ((this->off_ & 1) != 0)
485 ++this->off_;
486 this->read_next_header();
487 return *this;
490 const_iterator
491 operator++(int)
493 const_iterator ret = *this;
494 ++*this;
495 return ret;
498 bool
499 operator==(const const_iterator p) const
500 { return this->off_ == p->off; }
502 bool
503 operator!=(const const_iterator p) const
504 { return this->off_ != p->off; }
506 private:
507 void
508 read_next_header();
510 // The underlying archive.
511 Archive* archive_;
512 // The current offset in the file.
513 off_t off_;
514 // The current archive header.
515 Header header_;
518 // Read the next archive header.
520 void
521 Archive::const_iterator::read_next_header()
523 off_t filesize = this->archive_->file().filesize();
524 while (true)
526 if (filesize - this->off_ < static_cast<off_t>(sizeof(Archive_header)))
528 if (filesize != this->off_)
530 gold_error(_("%s: short archive header at %zu"),
531 this->archive_->filename().c_str(),
532 static_cast<size_t>(this->off_));
533 this->off_ = filesize;
535 this->header_.off = filesize;
536 return;
539 unsigned char buf[sizeof(Archive_header)];
540 this->archive_->file().read(this->off_, sizeof(Archive_header), buf);
542 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(buf);
543 off_t size = this->archive_->interpret_header(hdr, this->off_,
544 &this->header_.name,
545 &this->header_.nested_off);
546 if (size == -1)
548 this->header_.off = filesize;
549 return;
552 this->header_.size = size;
553 this->header_.off = this->off_;
555 // Skip special members.
556 if (!this->header_.name.empty()
557 && this->header_.name != "/"
558 && this->header_.name != "/SYM64/")
559 return;
561 this->off_ += sizeof(Archive_header) + this->header_.size;
562 if ((this->off_ & 1) != 0)
563 ++this->off_;
567 // Initial iterator.
569 Archive::const_iterator
570 Archive::begin()
572 return Archive::const_iterator(this, sarmag);
575 // Final iterator.
577 Archive::const_iterator
578 Archive::end()
580 return Archive::const_iterator(this, this->input_file_->file().filesize());
583 // Get the file and offset for an archive member, which may be an
584 // external member of a thin archive. Set *INPUT_FILE to the
585 // file containing the actual member, *MEMOFF to the offset
586 // within that file (0 if not a nested archive), and *MEMBER_NAME
587 // to the name of the archive member. Return TRUE on success.
589 bool
590 Archive::get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff,
591 off_t* memsize, std::string* member_name)
593 off_t nested_off;
595 *memsize = this->read_header(off, false, member_name, &nested_off);
596 if (*memsize == -1)
597 return false;
599 *input_file = this->input_file_;
600 *memoff = off + static_cast<off_t>(sizeof(Archive_header));
602 if (!this->is_thin_archive_)
603 return true;
605 // Adjust a relative pathname so that it is relative
606 // to the directory containing the archive.
607 if (!IS_ABSOLUTE_PATH(member_name->c_str()))
609 const char* arch_path = this->filename().c_str();
610 const char* basename = lbasename(arch_path);
611 if (basename > arch_path)
612 member_name->replace(0, 0,
613 this->filename().substr(0, basename - arch_path));
616 if (nested_off > 0)
618 // This is a member of a nested archive. Open the containing
619 // archive if we don't already have it open, then do a recursive
620 // call to include the member from that archive.
621 Archive* arch;
622 Nested_archive_table::const_iterator p =
623 this->nested_archives_.find(*member_name);
624 if (p != this->nested_archives_.end())
625 arch = p->second;
626 else
628 Input_file_argument* input_file_arg =
629 new Input_file_argument(member_name->c_str(),
630 Input_file_argument::INPUT_FILE_TYPE_FILE,
631 "", false, parameters->options());
632 *input_file = new Input_file(input_file_arg);
633 int dummy = 0;
634 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
635 return false;
636 arch = new Archive(*member_name, *input_file, false, this->dirpath_,
637 this->task_);
638 arch->setup();
639 std::pair<Nested_archive_table::iterator, bool> ins =
640 this->nested_archives_.insert(std::make_pair(*member_name, arch));
641 gold_assert(ins.second);
643 return arch->get_file_and_offset(nested_off, input_file, memoff,
644 memsize, member_name);
647 // This is an external member of a thin archive. Open the
648 // file as a regular relocatable object file.
649 Input_file_argument* input_file_arg =
650 new Input_file_argument(member_name->c_str(),
651 Input_file_argument::INPUT_FILE_TYPE_FILE,
652 "", false, this->input_file_->options());
653 *input_file = new Input_file(input_file_arg);
654 int dummy = 0;
655 if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy))
656 return false;
658 *memoff = 0;
659 *memsize = (*input_file)->file().filesize();
660 return true;
663 // Return an ELF object for the member at offset OFF. If
664 // PUNCONFIGURED is not NULL, then if the ELF object has an
665 // unsupported target type, set *PUNCONFIGURED to true and return
666 // NULL.
668 Object*
669 Archive::get_elf_object_for_member(off_t off, bool* punconfigured)
671 if (punconfigured != NULL)
672 *punconfigured = false;
674 Input_file* input_file;
675 off_t memoff;
676 off_t memsize;
677 std::string member_name;
678 if (!this->get_file_and_offset(off, &input_file, &memoff, &memsize,
679 &member_name))
680 return NULL;
682 const unsigned char* ehdr;
683 int read_size;
684 Object *obj = NULL;
685 bool is_elf_obj = false;
686 bool unclaimed = false;
688 if (is_elf_object(input_file, memoff, &ehdr, &read_size))
690 obj = make_elf_object((std::string(this->input_file_->filename())
691 + "(" + member_name + ")"),
692 input_file, memoff, ehdr, read_size,
693 punconfigured);
694 is_elf_obj = true;
697 if (parameters->options().has_plugins())
699 Object* plugin_obj
700 = parameters->options().plugins()->claim_file(input_file,
701 memoff,
702 memsize,
703 obj);
704 if (plugin_obj != NULL)
706 // The input file was claimed by a plugin, and its symbols
707 // have been provided by the plugin.
708 // Delete its elf object.
709 if (obj != NULL)
710 delete obj;
711 return plugin_obj;
714 unclaimed = true;
717 if (!is_elf_obj)
719 if (unclaimed)
720 gold_error(_("%s: plugin failed to claim member %s at %zu"),
721 this->name().c_str(), member_name.c_str(),
722 static_cast<size_t>(off));
723 else
724 gold_error(_("%s: member %s at %zu is not an ELF object"),
725 this->name().c_str(), member_name.c_str(),
726 static_cast<size_t>(off));
727 return NULL;
730 if (obj == NULL)
731 return NULL;
732 obj->set_no_export(this->no_export());
733 return obj;
736 // Read the symbols from all the archive members in the link.
738 void
739 Archive::read_all_symbols()
741 for (Archive::const_iterator p = this->begin();
742 p != this->end();
743 ++p)
744 this->read_symbols(p->off);
747 // Read the symbols from an archive member in the link. OFF is the file
748 // offset of the member header.
750 void
751 Archive::read_symbols(off_t off)
753 Object* obj = this->get_elf_object_for_member(off, NULL);
754 if (obj == NULL)
755 return;
757 Read_symbols_data* sd = new Read_symbols_data;
758 obj->read_symbols(sd);
759 Archive_member member(obj, sd);
760 this->members_[off] = member;
763 // Select members from the archive and add them to the link. We walk
764 // through the elements in the archive map, and look each one up in
765 // the symbol table. If it exists as a strong undefined symbol, we
766 // pull in the corresponding element. We have to do this in a loop,
767 // since pulling in one element may create new undefined symbols which
768 // may be satisfied by other objects in the archive. Return true in
769 // the normal case, false if the first member we tried to add from
770 // this archive had an incompatible target.
772 bool
773 Archive::add_symbols(Symbol_table* symtab, Layout* layout,
774 Input_objects* input_objects, Mapfile* mapfile)
776 ++Archive::total_archives;
778 if (this->input_file_->options().whole_archive())
779 return this->include_all_members(symtab, layout, input_objects,
780 mapfile);
782 Archive::total_members += this->num_members_;
784 input_objects->archive_start(this);
786 const size_t armap_size = this->armap_.size();
788 // This is a quick optimization, since we usually see many symbols
789 // in a row with the same offset. last_seen_offset holds the last
790 // offset we saw that was present in the seen_offsets_ set.
791 off_t last_seen_offset = -1;
793 // Track which symbols in the symbol table we've already found to be
794 // defined.
796 char* tmpbuf = NULL;
797 size_t tmpbuflen = 0;
798 bool added_new_object;
801 added_new_object = false;
802 for (size_t i = 0; i < armap_size; ++i)
804 if (this->armap_checked_[i])
805 continue;
806 if (this->armap_[i].file_offset == last_seen_offset)
808 this->armap_checked_[i] = true;
809 continue;
811 if (this->seen_offsets_.find(this->armap_[i].file_offset)
812 != this->seen_offsets_.end())
814 this->armap_checked_[i] = true;
815 last_seen_offset = this->armap_[i].file_offset;
816 continue;
819 const char* sym_name = (this->armap_names_.data()
820 + this->armap_[i].name_offset);
822 Symbol* sym;
823 std::string why;
824 Archive::Should_include t =
825 Archive::should_include_member(symtab, layout, sym_name, &sym,
826 &why, &tmpbuf, &tmpbuflen);
828 if (t == Archive::SHOULD_INCLUDE_NO
829 || t == Archive::SHOULD_INCLUDE_YES)
830 this->armap_checked_[i] = true;
832 if (t != Archive::SHOULD_INCLUDE_YES)
833 continue;
835 // We want to include this object in the link.
836 last_seen_offset = this->armap_[i].file_offset;
837 this->seen_offsets_.insert(last_seen_offset);
839 if (!this->include_member(symtab, layout, input_objects,
840 last_seen_offset, mapfile, sym,
841 why.c_str()))
843 if (tmpbuf != NULL)
844 free(tmpbuf);
845 return false;
848 added_new_object = true;
851 while (added_new_object);
853 if (tmpbuf != NULL)
854 free(tmpbuf);
856 input_objects->archive_stop(this);
858 return true;
861 // Return whether the archive includes a member which defines the
862 // symbol SYM.
864 bool
865 Archive::defines_symbol(Symbol* sym) const
867 const char* symname = sym->name();
868 size_t symname_len = strlen(symname);
869 size_t armap_size = this->armap_.size();
870 for (size_t i = 0; i < armap_size; ++i)
872 if (this->armap_checked_[i])
873 continue;
874 const char* archive_symname = (this->armap_names_.data()
875 + this->armap_[i].name_offset);
876 if (strncmp(archive_symname, symname, symname_len) != 0)
877 continue;
878 char c = archive_symname[symname_len];
879 if (c == '\0' && sym->version() == NULL)
880 return true;
881 if (c == '@')
883 const char* ver = archive_symname + symname_len + 1;
884 if (*ver == '@')
886 if (sym->version() == NULL)
887 return true;
888 ++ver;
890 if (sym->version() != NULL && strcmp(sym->version(), ver) == 0)
891 return true;
894 return false;
897 // Include all the archive members in the link. This is for --whole-archive.
899 bool
900 Archive::include_all_members(Symbol_table* symtab, Layout* layout,
901 Input_objects* input_objects, Mapfile* mapfile)
903 // Don't include the same archive twice. This can happen if
904 // --whole-archive is nested inside --start-group (PR gold/12163).
905 if (this->included_all_members_)
906 return true;
908 this->included_all_members_ = true;
910 input_objects->archive_start(this);
912 if (this->members_.size() > 0)
914 std::map<off_t, Archive_member>::const_iterator p;
915 for (p = this->members_.begin();
916 p != this->members_.end();
917 ++p)
919 if (!this->include_member(symtab, layout, input_objects, p->first,
920 mapfile, NULL, "--whole-archive"))
921 return false;
922 ++Archive::total_members;
925 else
927 for (Archive::const_iterator p = this->begin();
928 p != this->end();
929 ++p)
931 if (!this->include_member(symtab, layout, input_objects, p->off,
932 mapfile, NULL, "--whole-archive"))
933 return false;
934 ++Archive::total_members;
938 input_objects->archive_stop(this);
940 return true;
943 // Return the number of members in the archive. This is only used for
944 // reports.
946 size_t
947 Archive::count_members()
949 size_t ret = 0;
950 for (Archive::const_iterator p = this->begin();
951 p != this->end();
952 ++p)
953 ++ret;
954 return ret;
957 // RAII class to ensure we unlock the object if it's a member of a
958 // thin archive. We can't use Task_lock_obj in Archive::include_member
959 // because the object file is already locked when it's opened by
960 // get_elf_object_for_member.
962 class Thin_archive_object_unlocker
964 public:
965 Thin_archive_object_unlocker(const Task *task, Object* obj)
966 : task_(task), obj_(obj)
969 ~Thin_archive_object_unlocker()
971 if (this->obj_->offset() == 0)
972 this->obj_->unlock(this->task_);
975 private:
976 Thin_archive_object_unlocker(const Thin_archive_object_unlocker&);
977 Thin_archive_object_unlocker& operator=(const Thin_archive_object_unlocker&);
979 const Task* task_;
980 Object* obj_;
983 // Include an archive member in the link. OFF is the file offset of
984 // the member header. WHY is the reason we are including this member.
985 // Return true if we added the member or if we had an error, return
986 // false if this was the first member we tried to add from this
987 // archive and it had an incompatible format.
989 bool
990 Archive::include_member(Symbol_table* symtab, Layout* layout,
991 Input_objects* input_objects, off_t off,
992 Mapfile* mapfile, Symbol* sym, const char* why)
994 ++Archive::total_members_loaded;
996 std::map<off_t, Archive_member>::const_iterator p = this->members_.find(off);
997 if (p != this->members_.end())
999 Object* obj = p->second.obj_;
1001 Read_symbols_data* sd = p->second.sd_;
1002 if (mapfile != NULL)
1003 mapfile->report_include_archive_member(obj->name(), sym, why);
1004 if (input_objects->add_object(obj))
1006 obj->layout(symtab, layout, sd);
1007 obj->add_symbols(symtab, sd, layout);
1008 this->included_member_ = true;
1010 delete sd;
1011 return true;
1014 // If this is the first object we are including from this archive,
1015 // and we searched for this archive, most likely because it was
1016 // found via a -l option, then if the target is incompatible we want
1017 // to move on to the next archive found in the search path.
1018 bool unconfigured = false;
1019 bool* punconfigured = NULL;
1020 if (!this->included_member_ && this->searched_for())
1021 punconfigured = &unconfigured;
1023 Object* obj = this->get_elf_object_for_member(off, punconfigured);
1024 if (obj == NULL)
1026 // Return false to search for another archive, true if we found
1027 // an error.
1028 return unconfigured ? false : true;
1031 // If the object is an external member of a thin archive,
1032 // unlock it when we're done here.
1033 Thin_archive_object_unlocker unlocker(this->task_, obj);
1035 if (mapfile != NULL)
1036 mapfile->report_include_archive_member(obj->name(), sym, why);
1038 Pluginobj* pluginobj = obj->pluginobj();
1039 if (pluginobj != NULL)
1041 pluginobj->add_symbols(symtab, NULL, layout);
1042 this->included_member_ = true;
1043 return true;
1046 if (!input_objects->add_object(obj))
1048 delete obj;
1049 return true;
1052 if (layout->incremental_inputs() != NULL)
1053 layout->incremental_inputs()->report_object(obj, 0, this, NULL);
1056 Read_symbols_data sd;
1057 obj->read_symbols(&sd);
1058 obj->layout(symtab, layout, &sd);
1059 obj->add_symbols(symtab, &sd, layout);
1062 this->included_member_ = true;
1063 return true;
1066 // Iterate over all unused symbols, and call the visitor class V for each.
1068 void
1069 Archive::do_for_all_unused_symbols(Symbol_visitor_base* v) const
1071 for (std::vector<Armap_entry>::const_iterator p = this->armap_.begin();
1072 p != this->armap_.end();
1073 ++p)
1075 if (this->seen_offsets_.find(p->file_offset)
1076 == this->seen_offsets_.end())
1077 v->visit(this->armap_names_.data() + p->name_offset);
1081 // Print statistical information to stderr. This is used for --stats.
1083 void
1084 Archive::print_stats()
1086 fprintf(stderr, _("%s: archive libraries: %u\n"),
1087 program_name, Archive::total_archives);
1088 fprintf(stderr, _("%s: total archive members: %u\n"),
1089 program_name, Archive::total_members);
1090 fprintf(stderr, _("%s: loaded archive members: %u\n"),
1091 program_name, Archive::total_members_loaded);
1094 // Add_archive_symbols methods.
1096 Add_archive_symbols::~Add_archive_symbols()
1098 if (this->this_blocker_ != NULL)
1099 delete this->this_blocker_;
1100 // next_blocker_ is deleted by the task associated with the next
1101 // input file.
1104 // Return whether we can add the archive symbols. We are blocked by
1105 // this_blocker_. We block next_blocker_. We also lock the file.
1107 Task_token*
1108 Add_archive_symbols::is_runnable()
1110 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1111 return this->this_blocker_;
1112 return NULL;
1115 void
1116 Add_archive_symbols::locks(Task_locker* tl)
1118 tl->add(this, this->next_blocker_);
1119 tl->add(this, this->archive_->token());
1122 void
1123 Add_archive_symbols::run(Workqueue* workqueue)
1125 // For an incremental link, begin recording layout information.
1126 Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
1127 if (incremental_inputs != NULL)
1129 unsigned int arg_serial = this->input_argument_->file().arg_serial();
1130 Script_info* script_info = this->input_argument_->script_info();
1131 incremental_inputs->report_archive_begin(this->archive_, arg_serial,
1132 script_info);
1135 bool added = this->archive_->add_symbols(this->symtab_, this->layout_,
1136 this->input_objects_,
1137 this->mapfile_);
1138 this->archive_->unlock_nested_archives();
1140 this->archive_->release();
1141 this->archive_->clear_uncached_views();
1143 if (!added)
1145 // This archive holds object files which are incompatible with
1146 // our output file.
1147 Read_symbols::incompatible_warning(this->input_argument_,
1148 this->archive_->input_file());
1149 Read_symbols::requeue(workqueue, this->input_objects_, this->symtab_,
1150 this->layout_, this->dirpath_, this->dirindex_,
1151 this->mapfile_, this->input_argument_,
1152 this->input_group_, this->next_blocker_);
1153 delete this->archive_;
1154 return;
1157 if (this->input_group_ != NULL)
1158 this->input_group_->add_archive(this->archive_);
1159 else
1161 // For an incremental link, finish recording the layout information.
1162 if (incremental_inputs != NULL)
1163 incremental_inputs->report_archive_end(this->archive_);
1165 if (!parameters->options().has_plugins()
1166 || this->archive_->input_file()->options().whole_archive())
1168 // We no longer need to know about this archive.
1169 delete this->archive_;
1171 else
1173 // The plugin interface may want to rescan this archive.
1174 parameters->options().plugins()->save_archive(this->archive_);
1177 this->archive_ = NULL;
1181 // Class Lib_group static variables.
1182 unsigned int Lib_group::total_lib_groups;
1183 unsigned int Lib_group::total_members;
1184 unsigned int Lib_group::total_members_loaded;
1186 Lib_group::Lib_group(const Input_file_lib* lib, Task* task)
1187 : Library_base(task), members_()
1189 this->members_.resize(lib->size());
1192 const std::string&
1193 Lib_group::do_filename() const
1195 std::string *filename = new std::string("/group/");
1196 return *filename;
1199 // Select members from the lib group and add them to the link. We walk
1200 // through the members, and check if each one up should be included.
1201 // If the object says it should be included, we do so. We have to do
1202 // this in a loop, since including one member may create new undefined
1203 // symbols which may be satisfied by other members.
1205 void
1206 Lib_group::add_symbols(Symbol_table* symtab, Layout* layout,
1207 Input_objects* input_objects)
1209 ++Lib_group::total_lib_groups;
1211 Lib_group::total_members += this->members_.size();
1213 bool added_new_object;
1216 added_new_object = false;
1217 unsigned int i = 0;
1218 while (i < this->members_.size())
1220 const Archive_member& member = this->members_[i];
1221 Object* obj = member.obj_;
1222 std::string why;
1224 // Skip files with no symbols. Plugin objects have
1225 // member.sd_ == NULL.
1226 if (obj != NULL
1227 && (member.sd_ == NULL || member.sd_->symbol_names != NULL))
1229 Archive::Should_include t = obj->should_include_member(symtab,
1230 layout,
1231 member.sd_,
1232 &why);
1234 if (t != Archive::SHOULD_INCLUDE_YES)
1236 ++i;
1237 continue;
1240 this->include_member(symtab, layout, input_objects, member);
1242 added_new_object = true;
1244 else
1246 if (member.sd_ != NULL)
1248 // The file must be locked in order to destroy the views
1249 // associated with it.
1250 gold_assert(obj != NULL);
1251 obj->lock(this->task_);
1252 delete member.sd_;
1253 obj->unlock(this->task_);
1257 this->members_[i] = this->members_.back();
1258 this->members_.pop_back();
1261 while (added_new_object);
1264 // Include a lib group member in the link.
1266 void
1267 Lib_group::include_member(Symbol_table* symtab, Layout* layout,
1268 Input_objects* input_objects,
1269 const Archive_member& member)
1271 ++Lib_group::total_members_loaded;
1273 Object* obj = member.obj_;
1274 gold_assert(obj != NULL);
1276 Pluginobj* pluginobj = obj->pluginobj();
1277 if (pluginobj != NULL)
1279 pluginobj->add_symbols(symtab, NULL, layout);
1280 return;
1283 Read_symbols_data* sd = member.sd_;
1284 gold_assert(sd != NULL);
1285 obj->lock(this->task_);
1286 if (input_objects->add_object(obj))
1288 if (layout->incremental_inputs() != NULL)
1289 layout->incremental_inputs()->report_object(obj, member.arg_serial_,
1290 this, NULL);
1291 obj->layout(symtab, layout, sd);
1292 obj->add_symbols(symtab, sd, layout);
1294 delete sd;
1295 // Unlock the file for the next task.
1296 obj->unlock(this->task_);
1299 // Iterate over all unused symbols, and call the visitor class V for each.
1301 void
1302 Lib_group::do_for_all_unused_symbols(Symbol_visitor_base* v) const
1304 // Files are removed from the members list when used, so all the
1305 // files remaining on the list are unused.
1306 for (std::vector<Archive_member>::const_iterator p = this->members_.begin();
1307 p != this->members_.end();
1308 ++p)
1310 Object* obj = p->obj_;
1311 obj->for_all_global_symbols(p->sd_, v);
1315 // Print statistical information to stderr. This is used for --stats.
1317 void
1318 Lib_group::print_stats()
1320 fprintf(stderr, _("%s: lib groups: %u\n"),
1321 program_name, Lib_group::total_lib_groups);
1322 fprintf(stderr, _("%s: total lib groups members: %u\n"),
1323 program_name, Lib_group::total_members);
1324 fprintf(stderr, _("%s: loaded lib groups members: %u\n"),
1325 program_name, Lib_group::total_members_loaded);
1328 Task_token*
1329 Add_lib_group_symbols::is_runnable()
1331 if (this->readsyms_blocker_ != NULL && this->readsyms_blocker_->is_blocked())
1332 return this->readsyms_blocker_;
1333 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1334 return this->this_blocker_;
1335 return NULL;
1338 void
1339 Add_lib_group_symbols::locks(Task_locker* tl)
1341 tl->add(this, this->next_blocker_);
1344 void
1345 Add_lib_group_symbols::run(Workqueue*)
1347 // For an incremental link, begin recording layout information.
1348 Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs();
1349 if (incremental_inputs != NULL)
1350 incremental_inputs->report_archive_begin(this->lib_, 0, NULL);
1352 this->lib_->add_symbols(this->symtab_, this->layout_, this->input_objects_);
1354 if (incremental_inputs != NULL)
1355 incremental_inputs->report_archive_end(this->lib_);
1358 Add_lib_group_symbols::~Add_lib_group_symbols()
1360 if (this->this_blocker_ != NULL)
1361 delete this->this_blocker_;
1362 // next_blocker_ is deleted by the task associated with the next
1363 // input file.
1366 } // End namespace gold.