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