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