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