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