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