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