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