* sparc-linux-tdep.c, sparc64-linux-tdep.c: Use
[deliverable/binutils-gdb.git] / gold / archive.cc
CommitLineData
61ba1cf9
ILT
1// archive.cc -- archive support for gold
2
ebdbb458 3// Copyright 2006, 2007, 2008 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"
61ba1cf9 34#include "fileread.h"
ead1e424 35#include "readsyms.h"
61ba1cf9
ILT
36#include "symtab.h"
37#include "object.h"
38#include "archive.h"
39
40namespace gold
41{
42
43// The header of an entry in the archive. This is all readable text,
44// padded with spaces where necesary. If the contents of an archive
45// are all text file, the entire archive is readable.
46
47struct Archive::Archive_header
48{
49 // The entry name.
50 char ar_name[16];
51 // The file modification time.
52 char ar_date[12];
53 // The user's UID in decimal.
54 char ar_uid[6];
55 // The user's GID in decimal.
56 char ar_gid[6];
57 // The file mode in octal.
58 char ar_mode[8];
59 // The file size in decimal.
60 char ar_size[10];
61 // The final magic code.
62 char ar_fmag[2];
63};
64
65// Archive methods.
66
67const char Archive::armag[sarmag] =
68{
69 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
70};
71
a1207466
CC
72const char Archive::armagt[sarmag] =
73{
74 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
75};
76
61ba1cf9
ILT
77const char Archive::arfmag[2] = { '`', '\n' };
78
61ba1cf9
ILT
79// Set up the archive: read the symbol map and the extended name
80// table.
81
82void
a1207466 83Archive::setup()
61ba1cf9 84{
3e95a404
ILT
85 // We need to ignore empty archives.
86 if (this->input_file_->file().filesize() == sarmag)
a1207466 87 return;
3e95a404 88
61ba1cf9
ILT
89 // The first member of the archive should be the symbol table.
90 std::string armap_name;
8383303e 91 section_size_type armap_size =
cb295612 92 convert_to_section_size_type(this->read_header(sarmag, false,
a1207466 93 &armap_name, NULL));
75f2446e 94 off_t off = sarmag;
4973341a
ILT
95 if (armap_name.empty())
96 {
97 this->read_armap(sarmag + sizeof(Archive_header), armap_size);
98 off = sarmag + sizeof(Archive_header) + armap_size;
99 }
45aa233b 100 else if (!this->input_file_->options().whole_archive())
75f2446e
ILT
101 gold_error(_("%s: no archive symbol table (run ranlib)"),
102 this->name().c_str());
4973341a 103
cb295612
ILT
104 // See if there is an extended name table. We cache these views
105 // because it is likely that we will want to read the following
106 // header in the add_symbols routine.
4973341a
ILT
107 if ((off & 1) != 0)
108 ++off;
109 std::string xname;
cb295612 110 section_size_type extended_size =
a1207466 111 convert_to_section_size_type(this->read_header(off, true, &xname, NULL));
4973341a
ILT
112 if (xname == "/")
113 {
114 const unsigned char* p = this->get_view(off + sizeof(Archive_header),
39d0cb0e 115 extended_size, false, true);
4973341a
ILT
116 const char* px = reinterpret_cast<const char*>(p);
117 this->extended_names_.assign(px, extended_size);
118 }
a1207466
CC
119}
120
121// Unlock any nested archives.
4973341a 122
a1207466
CC
123void
124Archive::unlock_nested_archives()
125{
126 for (Nested_archive_table::iterator p = this->nested_archives_.begin();
127 p != this->nested_archives_.end();
128 ++p)
129 {
130 p->second->unlock(this->task_);
131 }
4973341a 132}
61ba1cf9 133
4973341a
ILT
134// Read the archive symbol map.
135
136void
8383303e 137Archive::read_armap(off_t start, section_size_type size)
4973341a 138{
61ba1cf9 139 // Read in the entire armap.
39d0cb0e 140 const unsigned char* p = this->get_view(start, size, true, false);
61ba1cf9
ILT
141
142 // Numbers in the armap are always big-endian.
143 const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p);
f6ce93d6 144 unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword);
61ba1cf9
ILT
145 ++pword;
146
147 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
148 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
8383303e
ILT
149 section_size_type names_size =
150 reinterpret_cast<const char*>(p) + size - pnames;
9eb9fa57 151 this->armap_names_.assign(pnames, names_size);
61ba1cf9
ILT
152
153 this->armap_.resize(nsyms);
154
8383303e 155 section_offset_type name_offset = 0;
61ba1cf9
ILT
156 for (unsigned int i = 0; i < nsyms; ++i)
157 {
9eb9fa57
ILT
158 this->armap_[i].name_offset = name_offset;
159 this->armap_[i].file_offset = elfcpp::Swap<32, true>::readval(pword);
160 name_offset += strlen(pnames + name_offset) + 1;
61ba1cf9
ILT
161 ++pword;
162 }
163
8383303e 164 if (static_cast<section_size_type>(name_offset) > names_size)
75f2446e
ILT
165 gold_error(_("%s: bad archive symbol table names"),
166 this->name().c_str());
a93d6d07
ILT
167
168 // This array keeps track of which symbols are for archive elements
169 // which we have already included in the link.
170 this->armap_checked_.resize(nsyms);
61ba1cf9
ILT
171}
172
173// Read the header of an archive member at OFF. Fail if something
174// goes wrong. Return the size of the member. Set *PNAME to the name
175// of the member.
176
177off_t
a1207466
CC
178Archive::read_header(off_t off, bool cache, std::string* pname,
179 off_t* nested_off)
61ba1cf9 180{
39d0cb0e
ILT
181 const unsigned char* p = this->get_view(off, sizeof(Archive_header), true,
182 cache);
61ba1cf9 183 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
a1207466 184 return this->interpret_header(hdr, off, pname, nested_off);
4973341a 185}
61ba1cf9 186
4973341a
ILT
187// Interpret the header of HDR, the header of the archive member at
188// file offset OFF. Fail if something goes wrong. Return the size of
189// the member. Set *PNAME to the name of the member.
190
191off_t
192Archive::interpret_header(const Archive_header* hdr, off_t off,
a1207466 193 std::string* pname, off_t* nested_off)
4973341a 194{
61ba1cf9
ILT
195 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
196 {
75f2446e
ILT
197 gold_error(_("%s: malformed archive header at %zu"),
198 this->name().c_str(), static_cast<size_t>(off));
199 return this->input_file_->file().filesize() - off;
61ba1cf9
ILT
200 }
201
202 const int size_string_size = sizeof hdr->ar_size;
203 char size_string[size_string_size + 1];
204 memcpy(size_string, hdr->ar_size, size_string_size);
205 char* ps = size_string + size_string_size;
206 while (ps[-1] == ' ')
207 --ps;
208 *ps = '\0';
209
210 errno = 0;
211 char* end;
212 off_t member_size = strtol(size_string, &end, 10);
213 if (*end != '\0'
214 || member_size < 0
215 || (member_size == LONG_MAX && errno == ERANGE))
216 {
75f2446e
ILT
217 gold_error(_("%s: malformed archive header size at %zu"),
218 this->name().c_str(), static_cast<size_t>(off));
219 return this->input_file_->file().filesize() - off;
61ba1cf9
ILT
220 }
221
222 if (hdr->ar_name[0] != '/')
223 {
224 const char* name_end = strchr(hdr->ar_name, '/');
225 if (name_end == NULL
226 || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name))
227 {
a0c4fb0a 228 gold_error(_("%s: malformed archive header name at %zu"),
75f2446e
ILT
229 this->name().c_str(), static_cast<size_t>(off));
230 return this->input_file_->file().filesize() - off;
61ba1cf9
ILT
231 }
232 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
a1207466
CC
233 if (nested_off != NULL)
234 *nested_off = 0;
61ba1cf9
ILT
235 }
236 else if (hdr->ar_name[1] == ' ')
237 {
238 // This is the symbol table.
239 pname->clear();
240 }
241 else if (hdr->ar_name[1] == '/')
242 {
243 // This is the extended name table.
244 pname->assign(1, '/');
245 }
246 else
247 {
248 errno = 0;
249 long x = strtol(hdr->ar_name + 1, &end, 10);
a1207466
CC
250 long y = 0;
251 if (*end == ':')
252 y = strtol(end + 1, &end, 10);
61ba1cf9
ILT
253 if (*end != ' '
254 || x < 0
255 || (x == LONG_MAX && errno == ERANGE)
256 || static_cast<size_t>(x) >= this->extended_names_.size())
257 {
75f2446e
ILT
258 gold_error(_("%s: bad extended name index at %zu"),
259 this->name().c_str(), static_cast<size_t>(off));
260 return this->input_file_->file().filesize() - off;
61ba1cf9
ILT
261 }
262
263 const char* name = this->extended_names_.data() + x;
a1207466 264 const char* name_end = strchr(name, '\n');
61ba1cf9 265 if (static_cast<size_t>(name_end - name) > this->extended_names_.size()
a1207466 266 || name_end[-1] != '/')
61ba1cf9 267 {
75f2446e
ILT
268 gold_error(_("%s: bad extended name entry at header %zu"),
269 this->name().c_str(), static_cast<size_t>(off));
270 return this->input_file_->file().filesize() - off;
61ba1cf9 271 }
a1207466
CC
272 pname->assign(name, name_end - 1 - name);
273 if (nested_off != NULL)
274 *nested_off = y;
61ba1cf9
ILT
275 }
276
277 return member_size;
278}
279
280// Select members from the archive and add them to the link. We walk
281// through the elements in the archive map, and look each one up in
282// the symbol table. If it exists as a strong undefined symbol, we
283// pull in the corresponding element. We have to do this in a loop,
284// since pulling in one element may create new undefined symbols which
285// may be satisfied by other objects in the archive.
286
287void
7e1edb90
ILT
288Archive::add_symbols(Symbol_table* symtab, Layout* layout,
289 Input_objects* input_objects)
61ba1cf9 290{
45aa233b 291 if (this->input_file_->options().whole_archive())
7e1edb90 292 return this->include_all_members(symtab, layout, input_objects);
4973341a 293
ead1e424 294 const size_t armap_size = this->armap_.size();
61ba1cf9 295
e243ffc6 296 // This is a quick optimization, since we usually see many symbols
8c838dbd
ILT
297 // in a row with the same offset. last_seen_offset holds the last
298 // offset we saw that was present in the seen_offsets_ set.
a93d6d07
ILT
299 off_t last_seen_offset = -1;
300
301 // Track which symbols in the symbol table we've already found to be
302 // defined.
e243ffc6 303
61ba1cf9
ILT
304 bool added_new_object;
305 do
306 {
307 added_new_object = false;
61ba1cf9
ILT
308 for (size_t i = 0; i < armap_size; ++i)
309 {
a93d6d07
ILT
310 if (this->armap_checked_[i])
311 continue;
9eb9fa57 312 if (this->armap_[i].file_offset == last_seen_offset)
a93d6d07
ILT
313 {
314 this->armap_checked_[i] = true;
315 continue;
316 }
9eb9fa57 317 if (this->seen_offsets_.find(this->armap_[i].file_offset)
a93d6d07 318 != this->seen_offsets_.end())
61ba1cf9 319 {
a93d6d07 320 this->armap_checked_[i] = true;
9eb9fa57 321 last_seen_offset = this->armap_[i].file_offset;
61ba1cf9
ILT
322 continue;
323 }
324
9eb9fa57
ILT
325 const char* sym_name = (this->armap_names_.data()
326 + this->armap_[i].name_offset);
327 Symbol* sym = symtab->lookup(sym_name);
61ba1cf9 328 if (sym == NULL)
f3e9c5c5
ILT
329 {
330 // Check whether the symbol was named in a -u option.
331 if (!parameters->options().is_undefined(sym_name))
332 continue;
333 }
ead1e424 334 else if (!sym->is_undefined())
61ba1cf9 335 {
a93d6d07 336 this->armap_checked_[i] = true;
61ba1cf9
ILT
337 continue;
338 }
339 else if (sym->binding() == elfcpp::STB_WEAK)
340 continue;
341
342 // We want to include this object in the link.
9eb9fa57 343 last_seen_offset = this->armap_[i].file_offset;
a93d6d07
ILT
344 this->seen_offsets_.insert(last_seen_offset);
345 this->armap_checked_[i] = true;
7e1edb90 346 this->include_member(symtab, layout, input_objects,
a93d6d07 347 last_seen_offset);
61ba1cf9
ILT
348 added_new_object = true;
349 }
350 }
351 while (added_new_object);
352}
353
4973341a
ILT
354// Include all the archive members in the link. This is for --whole-archive.
355
356void
7e1edb90 357Archive::include_all_members(Symbol_table* symtab, Layout* layout,
4973341a
ILT
358 Input_objects* input_objects)
359{
360 off_t off = sarmag;
82dcae9d 361 off_t filesize = this->input_file_->file().filesize();
4973341a
ILT
362 while (true)
363 {
f5c3f225 364 if (filesize - off < static_cast<off_t>(sizeof(Archive_header)))
4973341a 365 {
82dcae9d 366 if (filesize != off)
75f2446e
ILT
367 gold_error(_("%s: short archive header at %zu"),
368 this->name().c_str(), static_cast<size_t>(off));
4973341a
ILT
369 break;
370 }
371
82dcae9d
ILT
372 unsigned char hdr_buf[sizeof(Archive_header)];
373 this->input_file_->file().read(off, sizeof(Archive_header), hdr_buf);
374
bae3688d
ILT
375 const Archive_header* hdr =
376 reinterpret_cast<const Archive_header*>(hdr_buf);
4973341a 377 std::string name;
a1207466 378 off_t size = this->interpret_header(hdr, off, &name, NULL);
4973341a
ILT
379 if (name.empty())
380 {
381 // Symbol table.
382 }
383 else if (name == "/")
384 {
385 // Extended name table.
386 }
387 else
7e1edb90 388 this->include_member(symtab, layout, input_objects, off);
4973341a 389
a1207466
CC
390 off += sizeof(Archive_header);
391 if (!this->is_thin_archive_)
392 off += size;
4973341a
ILT
393 if ((off & 1) != 0)
394 ++off;
395 }
396}
397
61ba1cf9
ILT
398// Include an archive member in the link. OFF is the file offset of
399// the member header.
400
401void
7e1edb90
ILT
402Archive::include_member(Symbol_table* symtab, Layout* layout,
403 Input_objects* input_objects, off_t off)
61ba1cf9
ILT
404{
405 std::string n;
a1207466
CC
406 off_t nested_off;
407 this->read_header(off, false, &n, &nested_off);
61ba1cf9 408
a1207466
CC
409 Input_file* input_file;
410 off_t memoff;
411
412 if (!this->is_thin_archive_)
413 {
414 input_file = this->input_file_;
415 memoff = off + static_cast<off_t>(sizeof(Archive_header));
416 }
417 else
418 {
419 // Adjust a relative pathname so that it is relative
420 // to the directory containing the archive.
421 if (!IS_ABSOLUTE_PATH(n.c_str()))
422 {
423 const char *arch_path = this->name().c_str();
424 const char *basename = lbasename(arch_path);
425 if (basename > arch_path)
426 n.replace(0, 0, this->name().substr(0, basename - arch_path));
427 }
428 if (nested_off > 0)
429 {
430 // This is a member of a nested archive. Open the containing
431 // archive if we don't already have it open, then do a recursive
432 // call to include the member from that archive.
433 Archive* arch;
434 Nested_archive_table::const_iterator p =
435 this->nested_archives_.find(n);
436 if (p != this->nested_archives_.end())
437 arch = p->second;
438 else
439 {
440 Input_file_argument* input_file_arg =
441 new Input_file_argument(n.c_str(), false, "", false,
442 parameters->options());
443 input_file = new Input_file(input_file_arg);
444 if (!input_file->open(parameters->options(), *this->dirpath_,
445 this->task_))
446 return;
447 arch = new Archive(n, input_file, false, this->dirpath_,
448 this->task_);
449 arch->setup();
450 std::pair<Nested_archive_table::iterator, bool> ins =
451 this->nested_archives_.insert(std::make_pair(n, arch));
452 gold_assert(ins.second);
453 }
454 arch->include_member(symtab, layout, input_objects, nested_off);
455 return;
456 }
457 // This is an external member of a thin archive. Open the
458 // file as a regular relocatable object file.
459 Input_file_argument* input_file_arg =
460 new Input_file_argument(n.c_str(), false, "", false,
461 this->input_file_->options());
462 input_file = new Input_file(input_file_arg);
463 if (!input_file->open(parameters->options(), *this->dirpath_,
464 this->task_))
465 {
466 return;
467 }
468 memoff = 0;
469 }
61ba1cf9 470
a1207466 471 off_t filesize = input_file->file().filesize();
82dcae9d
ILT
472 int read_size = elfcpp::Elf_sizes<64>::ehdr_size;
473 if (filesize - memoff < read_size)
474 read_size = filesize - memoff;
475
476 if (read_size < 4)
61ba1cf9 477 {
75f2446e
ILT
478 gold_error(_("%s: member at %zu is not an ELF object"),
479 this->name().c_str(), static_cast<size_t>(off));
480 return;
61ba1cf9
ILT
481 }
482
7ef73768
ILT
483 const unsigned char* ehdr = input_file->file().get_view(memoff, 0, read_size,
484 true, false);
82dcae9d 485
61ba1cf9
ILT
486 static unsigned char elfmagic[4] =
487 {
488 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
489 elfcpp::ELFMAG2, elfcpp::ELFMAG3
490 };
7ef73768 491 if (memcmp(ehdr, elfmagic, 4) != 0)
61ba1cf9 492 {
75f2446e
ILT
493 gold_error(_("%s: member at %zu is not an ELF object"),
494 this->name().c_str(), static_cast<size_t>(off));
495 return;
61ba1cf9
ILT
496 }
497
92e059d8 498 Object* obj = make_elf_object((std::string(this->input_file_->filename())
61ba1cf9 499 + "(" + n + ")"),
7ef73768 500 input_file, memoff, ehdr, read_size);
61ba1cf9 501
019cdb1a
ILT
502 if (input_objects->add_object(obj))
503 {
504 Read_symbols_data sd;
505 obj->read_symbols(&sd);
506 obj->layout(symtab, layout, &sd);
507 obj->add_symbols(symtab, &sd);
508 }
509 else
510 {
511 // FIXME: We need to close the descriptor here.
512 delete obj;
513 }
61ba1cf9 514
a1207466
CC
515 if (this->is_thin_archive_)
516 {
517 // Opening the file locked it. Unlock it now.
518 input_file->file().unlock(this->task_);
519 }
61ba1cf9
ILT
520}
521
522// Add_archive_symbols methods.
523
524Add_archive_symbols::~Add_archive_symbols()
525{
526 if (this->this_blocker_ != NULL)
527 delete this->this_blocker_;
528 // next_blocker_ is deleted by the task associated with the next
529 // input file.
530}
531
532// Return whether we can add the archive symbols. We are blocked by
533// this_blocker_. We block next_blocker_. We also lock the file.
534
17a1d0a9
ILT
535Task_token*
536Add_archive_symbols::is_runnable()
61ba1cf9
ILT
537{
538 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
17a1d0a9
ILT
539 return this->this_blocker_;
540 return NULL;
61ba1cf9
ILT
541}
542
17a1d0a9
ILT
543void
544Add_archive_symbols::locks(Task_locker* tl)
61ba1cf9 545{
17a1d0a9
ILT
546 tl->add(this, this->next_blocker_);
547 tl->add(this, this->archive_->token());
61ba1cf9
ILT
548}
549
550void
551Add_archive_symbols::run(Workqueue*)
552{
7e1edb90
ILT
553 this->archive_->add_symbols(this->symtab_, this->layout_,
554 this->input_objects_);
ead1e424 555
a1207466
CC
556 this->archive_->unlock_nested_archives();
557
17a1d0a9 558 this->archive_->release();
39d0cb0e 559 this->archive_->clear_uncached_views();
17a1d0a9 560
ead1e424
ILT
561 if (this->input_group_ != NULL)
562 this->input_group_->add_archive(this->archive_);
563 else
564 {
565 // We no longer need to know about this archive.
566 delete this->archive_;
c7912668 567 this->archive_ = NULL;
ead1e424 568 }
61ba1cf9
ILT
569}
570
571} // End namespace gold.
This page took 0.105948 seconds and 4 git commands to generate.