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