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