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