f1000a195de25c38588ae3960188767cc7fae337
[deliverable/binutils-gdb.git] / gold / archive.cc
1 // archive.cc -- archive support for gold
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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
23 #include "gold.h"
24
25 #include <cerrno>
26 #include <cstring>
27 #include <climits>
28 #include <vector>
29 #include "libiberty.h"
30 #include "filenames.h"
31
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
43 namespace 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
50 struct 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
68 // Class Archive static variables.
69 unsigned int Archive::total_archives;
70 unsigned int Archive::total_members;
71 unsigned int Archive::total_members_loaded;
72
73 // Archive methods.
74
75 const char Archive::armag[sarmag] =
76 {
77 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
78 };
79
80 const char Archive::armagt[sarmag] =
81 {
82 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
83 };
84
85 const char Archive::arfmag[2] = { '`', '\n' };
86
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)
93 {
94 this->no_export_ =
95 parameters->options().check_excluded_libs(input_file->found_name());
96 }
97
98 // Set up the archive: read the symbol map and the extended name
99 // table.
100
101 void
102 Archive::setup()
103 {
104 // We need to ignore empty archives.
105 if (this->input_file_->file().filesize() == sarmag)
106 return;
107
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())
115 {
116 this->read_armap(sarmag + sizeof(Archive_header), armap_size);
117 off = sarmag + sizeof(Archive_header) + armap_size;
118 }
119 else if (!this->input_file_->options().whole_archive())
120 gold_error(_("%s: no archive symbol table (run ranlib)"),
121 this->name().c_str());
122
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 == "/")
132 {
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);
137 }
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();
148 }
149
150 // Unlock any nested archives.
151
152 void
153 Archive::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 }
161 }
162
163 // Read the archive symbol map.
164
165 void
166 Archive::read_armap(off_t start, section_size_type size)
167 {
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
174 // Read in the entire armap.
175 const unsigned char* p = this->get_view(start, size, true, false);
176
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;
181
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);
187
188 this->armap_.resize(nsyms);
189
190 section_offset_type name_offset = 0;
191 for (unsigned int i = 0; i < nsyms; ++i)
192 {
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)
198 {
199 last_seen_offset = this->armap_[i].file_offset;
200 ++this->num_members_;
201 }
202 }
203
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());
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);
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
217 off_t
218 Archive::read_header(off_t off, bool cache, std::string* pname,
219 off_t* nested_off)
220 {
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);
225 }
226
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
231 off_t
232 Archive::interpret_header(const Archive_header* hdr, off_t off,
233 std::string* pname, off_t* nested_off) const
234 {
235 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
236 {
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;
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;
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))
256 {
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;
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 {
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;
271 }
272 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
273 if (nested_off != NULL)
274 *nested_off = 0;
275 }
276 else if (hdr->ar_name[1] == ' ')
277 {
278 // This is the symbol table.
279 if (!pname->empty())
280 pname->clear();
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;
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())
298 {
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;
302 }
303
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] != '/')
308 {
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;
312 }
313 pname->assign(name, name_end - 1 - name);
314 if (nested_off != NULL)
315 *nested_off = y;
316 }
317
318 return member_size;
319 }
320
321 // An archive member iterator.
322
323 class 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
340 const_iterator(Archive* archive, off_t off)
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.
387 Archive* archive_;
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.
395
396 void
397 Archive::const_iterator::read_next_header()
398 {
399 off_t filesize = this->archive_->file().filesize();
400 while (true)
401 {
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_;
431 }
432 }
433
434 // Initial iterator.
435
436 Archive::const_iterator
437 Archive::begin()
438 {
439 return Archive::const_iterator(this, sarmag);
440 }
441
442 // Final iterator.
443
444 Archive::const_iterator
445 Archive::end()
446 {
447 return Archive::const_iterator(this, this->input_file_->file().filesize());
448 }
449
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
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)
459 {
460 off_t nested_off;
461
462 *memsize = this->read_header(off, false, member_name, &nested_off);
463
464 *input_file = this->input_file_;
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 {
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));
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 =
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);
507 }
508 return arch->get_file_and_offset(nested_off, input_file, memoff,
509 memsize, member_name);
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 =
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;
522
523 *memoff = 0;
524 *memsize = (*input_file)->file().filesize();
525 return true;
526 }
527
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.
531
532 Object*
533 Archive::get_elf_object_for_member(off_t off, bool* punconfigured)
534 {
535 *punconfigured = false;
536
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;
544
545 if (parameters->options().has_plugins())
546 {
547 Object* obj = parameters->options().plugins()->claim_file(input_file,
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.
554 return obj;
555 }
556 }
557
558 const unsigned char* ehdr;
559 int read_size;
560 if (!is_elf_object(input_file, memoff, &ehdr, &read_size))
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
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;
575 }
576
577 // Read the symbols from all the archive members in the link.
578
579 void
580 Archive::read_all_symbols()
581 {
582 for (Archive::const_iterator p = this->begin();
583 p != this->end();
584 ++p)
585 this->read_symbols(p->off);
586 }
587
588 // Read the symbols from an archive member in the link. OFF is the file
589 // offset of the member header.
590
591 void
592 Archive::read_symbols(off_t off)
593 {
594 bool dummy;
595 Object* obj = this->get_elf_object_for_member(off, &dummy);
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
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)
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.
653 if (parameters->options().is_undefined(sym_name))
654 {
655 *why = "-u ";
656 *why += sym_name;
657 }
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;
669 }
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;
674
675 return Archive::SHOULD_INCLUDE_YES;
676 }
677
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.
686
687 bool
688 Archive::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
711 char* tmpbuf = NULL;
712 size_t tmpbuflen = 0;
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);
736
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);
742
743 if (t == Archive::SHOULD_INCLUDE_NO
744 || t == Archive::SHOULD_INCLUDE_YES)
745 this->armap_checked_[i] = true;
746
747 if (t != Archive::SHOULD_INCLUDE_YES)
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);
753
754 if (!this->include_member(symtab, layout, input_objects,
755 last_seen_offset, mapfile, sym,
756 why.c_str()))
757 {
758 if (tmpbuf != NULL)
759 free(tmpbuf);
760 return false;
761 }
762
763 added_new_object = true;
764 }
765 }
766 while (added_new_object);
767
768 if (tmpbuf != NULL)
769 free(tmpbuf);
770
771 input_objects->archive_stop(this);
772
773 return true;
774 }
775
776 // Include all the archive members in the link. This is for --whole-archive.
777
778 bool
779 Archive::include_all_members(Symbol_table* symtab, Layout* layout,
780 Input_objects* input_objects, Mapfile* mapfile)
781 {
782 input_objects->archive_start(this);
783
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 {
791 if (!this->include_member(symtab, layout, input_objects, p->first,
792 mapfile, NULL, "--whole-archive"))
793 return false;
794 ++Archive::total_members;
795 }
796 }
797 else
798 {
799 for (Archive::const_iterator p = this->begin();
800 p != this->end();
801 ++p)
802 {
803 if (!this->include_member(symtab, layout, input_objects, p->off,
804 mapfile, NULL, "--whole-archive"))
805 return false;
806 ++Archive::total_members;
807 }
808 }
809
810 input_objects->archive_stop(this);
811
812 return true;
813 }
814
815 // Return the number of members in the archive. This is only used for
816 // reports.
817
818 size_t
819 Archive::count_members()
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
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.
834
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)
839 {
840 ++Archive::total_members_loaded;
841
842 std::map<off_t, Archive_member>::const_iterator p = this->members_.find(off);
843 if (p != this->members_.end())
844 {
845 Object *obj = p->second.obj_;
846
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))
851 {
852 obj->layout(symtab, layout, sd);
853 obj->add_symbols(symtab, sd, layout);
854 this->included_member_ = true;
855 }
856 delete sd;
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()
865 && obj == NULL
866 && unconfigured)
867 return false;
868
869 if (obj == NULL)
870 return true;
871
872 if (mapfile != NULL)
873 mapfile->report_include_archive_member(obj->name(), sym, why);
874
875 Pluginobj* pluginobj = obj->pluginobj();
876 if (pluginobj != NULL)
877 {
878 pluginobj->add_symbols(symtab, NULL, layout);
879 this->included_member_ = true;
880 return true;
881 }
882
883 if (!input_objects->add_object(obj))
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 }
891 else
892 {
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 }
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_);
904
905 this->included_member_ = true;
906 }
907
908 return true;
909 }
910
911 // Print statistical information to stderr. This is used for --stats.
912
913 void
914 Archive::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);
922 }
923
924 // Add_archive_symbols methods.
925
926 Add_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
937 Task_token*
938 Add_archive_symbols::is_runnable()
939 {
940 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
941 return this->this_blocker_;
942 return NULL;
943 }
944
945 void
946 Add_archive_symbols::locks(Task_locker* tl)
947 {
948 tl->add(this, this->next_blocker_);
949 tl->add(this, this->archive_->token());
950 }
951
952 void
953 Add_archive_symbols::run(Workqueue* workqueue)
954 {
955 bool added = this->archive_->add_symbols(this->symtab_, this->layout_,
956 this->input_objects_,
957 this->mapfile_);
958 this->archive_->unlock_nested_archives();
959
960 this->archive_->release();
961 this->archive_->clear_uncached_views();
962
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
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_;
983 this->archive_ = NULL;
984 }
985 }
986
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;
991
992 Lib_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
1004 void
1005 Lib_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,
1029 layout,
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
1058 void
1059 Lib_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
1090 void
1091 Lib_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
1101 Task_token*
1102 Add_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
1109 void
1110 Add_lib_group_symbols::locks(Task_locker* tl)
1111 {
1112 tl->add(this, this->next_blocker_);
1113 }
1114
1115 void
1116 Add_lib_group_symbols::run(Workqueue*)
1117 {
1118 this->lib_->add_symbols(this->symtab_, this->layout_, this->input_objects_);
1119 }
1120
1121 Add_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
1129 } // End namespace gold.
This page took 0.065968 seconds and 4 git commands to generate.