Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // archive.cc -- archive support for gold |
2 | ||
0f7c0701 | 3 | // Copyright 2006, 2007, 2008, 2009 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" | |
39 | #include "archive.h" | |
89fc3421 | 40 | #include "plugin.h" |
61ba1cf9 ILT |
41 | |
42 | namespace gold | |
43 | { | |
44 | ||
45 | // The header of an entry in the archive. This is all readable text, | |
46 | // padded with spaces where necesary. If the contents of an archive | |
47 | // are all text file, the entire archive is readable. | |
48 | ||
49 | struct Archive::Archive_header | |
50 | { | |
51 | // The entry name. | |
52 | char ar_name[16]; | |
53 | // The file modification time. | |
54 | char ar_date[12]; | |
55 | // The user's UID in decimal. | |
56 | char ar_uid[6]; | |
57 | // The user's GID in decimal. | |
58 | char ar_gid[6]; | |
59 | // The file mode in octal. | |
60 | char ar_mode[8]; | |
61 | // The file size in decimal. | |
62 | char ar_size[10]; | |
63 | // The final magic code. | |
64 | char ar_fmag[2]; | |
65 | }; | |
66 | ||
ac45a351 CC |
67 | // Class Archive static variables. |
68 | unsigned int Archive::total_archives; | |
69 | unsigned int Archive::total_members; | |
70 | unsigned int Archive::total_members_loaded; | |
71 | ||
61ba1cf9 ILT |
72 | // Archive methods. |
73 | ||
74 | const char Archive::armag[sarmag] = | |
75 | { | |
76 | '!', '<', 'a', 'r', 'c', 'h', '>', '\n' | |
77 | }; | |
78 | ||
a1207466 CC |
79 | const char Archive::armagt[sarmag] = |
80 | { | |
81 | '!', '<', 't', 'h', 'i', 'n', '>', '\n' | |
82 | }; | |
83 | ||
61ba1cf9 ILT |
84 | const char Archive::arfmag[2] = { '`', '\n' }; |
85 | ||
61ba1cf9 ILT |
86 | // Set up the archive: read the symbol map and the extended name |
87 | // table. | |
88 | ||
89 | void | |
15f8229b | 90 | Archive::setup() |
61ba1cf9 | 91 | { |
3e95a404 ILT |
92 | // We need to ignore empty archives. |
93 | if (this->input_file_->file().filesize() == sarmag) | |
a1207466 | 94 | return; |
3e95a404 | 95 | |
61ba1cf9 ILT |
96 | // The first member of the archive should be the symbol table. |
97 | std::string armap_name; | |
8383303e | 98 | section_size_type armap_size = |
cb295612 | 99 | convert_to_section_size_type(this->read_header(sarmag, false, |
a1207466 | 100 | &armap_name, NULL)); |
75f2446e | 101 | off_t off = sarmag; |
4973341a ILT |
102 | if (armap_name.empty()) |
103 | { | |
104 | this->read_armap(sarmag + sizeof(Archive_header), armap_size); | |
105 | off = sarmag + sizeof(Archive_header) + armap_size; | |
106 | } | |
45aa233b | 107 | else if (!this->input_file_->options().whole_archive()) |
75f2446e ILT |
108 | gold_error(_("%s: no archive symbol table (run ranlib)"), |
109 | this->name().c_str()); | |
4973341a | 110 | |
cb295612 ILT |
111 | // See if there is an extended name table. We cache these views |
112 | // because it is likely that we will want to read the following | |
113 | // header in the add_symbols routine. | |
4973341a ILT |
114 | if ((off & 1) != 0) |
115 | ++off; | |
116 | std::string xname; | |
cb295612 | 117 | section_size_type extended_size = |
a1207466 | 118 | convert_to_section_size_type(this->read_header(off, true, &xname, NULL)); |
4973341a ILT |
119 | if (xname == "/") |
120 | { | |
121 | const unsigned char* p = this->get_view(off + sizeof(Archive_header), | |
39d0cb0e | 122 | extended_size, false, true); |
4973341a ILT |
123 | const char* px = reinterpret_cast<const char*>(p); |
124 | this->extended_names_.assign(px, extended_size); | |
125 | } | |
ac45a351 CC |
126 | bool preread_syms = (parameters->options().threads() |
127 | && parameters->options().preread_archive_symbols()); | |
128 | #ifndef ENABLE_THREADS | |
129 | preread_syms = false; | |
89fc3421 CC |
130 | #else |
131 | if (parameters->options().has_plugins()) | |
132 | preread_syms = false; | |
ac45a351 CC |
133 | #endif |
134 | if (preread_syms) | |
15f8229b | 135 | this->read_all_symbols(); |
a1207466 CC |
136 | } |
137 | ||
138 | // Unlock any nested archives. | |
4973341a | 139 | |
a1207466 CC |
140 | void |
141 | Archive::unlock_nested_archives() | |
142 | { | |
143 | for (Nested_archive_table::iterator p = this->nested_archives_.begin(); | |
144 | p != this->nested_archives_.end(); | |
145 | ++p) | |
146 | { | |
147 | p->second->unlock(this->task_); | |
148 | } | |
4973341a | 149 | } |
61ba1cf9 | 150 | |
4973341a ILT |
151 | // Read the archive symbol map. |
152 | ||
153 | void | |
8383303e | 154 | Archive::read_armap(off_t start, section_size_type size) |
4973341a | 155 | { |
ac45a351 CC |
156 | // To count the total number of archive members, we'll just count |
157 | // the number of times the file offset changes. Since most archives | |
158 | // group the symbols in the armap by object, this ought to give us | |
159 | // an accurate count. | |
160 | off_t last_seen_offset = -1; | |
161 | ||
61ba1cf9 | 162 | // Read in the entire armap. |
39d0cb0e | 163 | const unsigned char* p = this->get_view(start, size, true, false); |
61ba1cf9 ILT |
164 | |
165 | // Numbers in the armap are always big-endian. | |
166 | const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p); | |
f6ce93d6 | 167 | unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword); |
61ba1cf9 ILT |
168 | ++pword; |
169 | ||
170 | // Note that the addition is in units of sizeof(elfcpp::Elf_Word). | |
171 | const char* pnames = reinterpret_cast<const char*>(pword + nsyms); | |
8383303e ILT |
172 | section_size_type names_size = |
173 | reinterpret_cast<const char*>(p) + size - pnames; | |
9eb9fa57 | 174 | this->armap_names_.assign(pnames, names_size); |
61ba1cf9 ILT |
175 | |
176 | this->armap_.resize(nsyms); | |
177 | ||
8383303e | 178 | section_offset_type name_offset = 0; |
61ba1cf9 ILT |
179 | for (unsigned int i = 0; i < nsyms; ++i) |
180 | { | |
9eb9fa57 ILT |
181 | this->armap_[i].name_offset = name_offset; |
182 | this->armap_[i].file_offset = elfcpp::Swap<32, true>::readval(pword); | |
183 | name_offset += strlen(pnames + name_offset) + 1; | |
61ba1cf9 | 184 | ++pword; |
ac45a351 CC |
185 | if (this->armap_[i].file_offset != last_seen_offset) |
186 | { | |
187 | last_seen_offset = this->armap_[i].file_offset; | |
188 | ++this->num_members_; | |
189 | } | |
61ba1cf9 ILT |
190 | } |
191 | ||
8383303e | 192 | if (static_cast<section_size_type>(name_offset) > names_size) |
75f2446e ILT |
193 | gold_error(_("%s: bad archive symbol table names"), |
194 | this->name().c_str()); | |
a93d6d07 ILT |
195 | |
196 | // This array keeps track of which symbols are for archive elements | |
197 | // which we have already included in the link. | |
198 | this->armap_checked_.resize(nsyms); | |
61ba1cf9 ILT |
199 | } |
200 | ||
201 | // Read the header of an archive member at OFF. Fail if something | |
202 | // goes wrong. Return the size of the member. Set *PNAME to the name | |
203 | // of the member. | |
204 | ||
205 | off_t | |
a1207466 CC |
206 | Archive::read_header(off_t off, bool cache, std::string* pname, |
207 | off_t* nested_off) | |
61ba1cf9 | 208 | { |
39d0cb0e ILT |
209 | const unsigned char* p = this->get_view(off, sizeof(Archive_header), true, |
210 | cache); | |
61ba1cf9 | 211 | const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p); |
a1207466 | 212 | return this->interpret_header(hdr, off, pname, nested_off); |
4973341a | 213 | } |
61ba1cf9 | 214 | |
4973341a ILT |
215 | // Interpret the header of HDR, the header of the archive member at |
216 | // file offset OFF. Fail if something goes wrong. Return the size of | |
217 | // the member. Set *PNAME to the name of the member. | |
218 | ||
219 | off_t | |
220 | Archive::interpret_header(const Archive_header* hdr, off_t off, | |
92de84a6 | 221 | std::string* pname, off_t* nested_off) const |
4973341a | 222 | { |
61ba1cf9 ILT |
223 | if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0) |
224 | { | |
75f2446e ILT |
225 | gold_error(_("%s: malformed archive header at %zu"), |
226 | this->name().c_str(), static_cast<size_t>(off)); | |
227 | return this->input_file_->file().filesize() - off; | |
61ba1cf9 ILT |
228 | } |
229 | ||
230 | const int size_string_size = sizeof hdr->ar_size; | |
231 | char size_string[size_string_size + 1]; | |
232 | memcpy(size_string, hdr->ar_size, size_string_size); | |
233 | char* ps = size_string + size_string_size; | |
234 | while (ps[-1] == ' ') | |
235 | --ps; | |
236 | *ps = '\0'; | |
237 | ||
238 | errno = 0; | |
239 | char* end; | |
240 | off_t member_size = strtol(size_string, &end, 10); | |
241 | if (*end != '\0' | |
242 | || member_size < 0 | |
243 | || (member_size == LONG_MAX && errno == ERANGE)) | |
244 | { | |
75f2446e ILT |
245 | gold_error(_("%s: malformed archive header size at %zu"), |
246 | this->name().c_str(), static_cast<size_t>(off)); | |
247 | return this->input_file_->file().filesize() - off; | |
61ba1cf9 ILT |
248 | } |
249 | ||
250 | if (hdr->ar_name[0] != '/') | |
251 | { | |
252 | const char* name_end = strchr(hdr->ar_name, '/'); | |
253 | if (name_end == NULL | |
254 | || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name)) | |
255 | { | |
a0c4fb0a | 256 | gold_error(_("%s: malformed archive header name at %zu"), |
75f2446e ILT |
257 | this->name().c_str(), static_cast<size_t>(off)); |
258 | return this->input_file_->file().filesize() - off; | |
61ba1cf9 ILT |
259 | } |
260 | pname->assign(hdr->ar_name, name_end - hdr->ar_name); | |
a1207466 CC |
261 | if (nested_off != NULL) |
262 | *nested_off = 0; | |
61ba1cf9 ILT |
263 | } |
264 | else if (hdr->ar_name[1] == ' ') | |
265 | { | |
266 | // This is the symbol table. | |
267 | pname->clear(); | |
268 | } | |
269 | else if (hdr->ar_name[1] == '/') | |
270 | { | |
271 | // This is the extended name table. | |
272 | pname->assign(1, '/'); | |
273 | } | |
274 | else | |
275 | { | |
276 | errno = 0; | |
277 | long x = strtol(hdr->ar_name + 1, &end, 10); | |
a1207466 CC |
278 | long y = 0; |
279 | if (*end == ':') | |
280 | y = strtol(end + 1, &end, 10); | |
61ba1cf9 ILT |
281 | if (*end != ' ' |
282 | || x < 0 | |
283 | || (x == LONG_MAX && errno == ERANGE) | |
284 | || static_cast<size_t>(x) >= this->extended_names_.size()) | |
285 | { | |
75f2446e ILT |
286 | gold_error(_("%s: bad extended name index at %zu"), |
287 | this->name().c_str(), static_cast<size_t>(off)); | |
288 | return this->input_file_->file().filesize() - off; | |
61ba1cf9 ILT |
289 | } |
290 | ||
291 | const char* name = this->extended_names_.data() + x; | |
a1207466 | 292 | const char* name_end = strchr(name, '\n'); |
61ba1cf9 | 293 | if (static_cast<size_t>(name_end - name) > this->extended_names_.size() |
a1207466 | 294 | || name_end[-1] != '/') |
61ba1cf9 | 295 | { |
75f2446e ILT |
296 | gold_error(_("%s: bad extended name entry at header %zu"), |
297 | this->name().c_str(), static_cast<size_t>(off)); | |
298 | return this->input_file_->file().filesize() - off; | |
61ba1cf9 | 299 | } |
a1207466 CC |
300 | pname->assign(name, name_end - 1 - name); |
301 | if (nested_off != NULL) | |
302 | *nested_off = y; | |
61ba1cf9 ILT |
303 | } |
304 | ||
305 | return member_size; | |
306 | } | |
307 | ||
92de84a6 ILT |
308 | // An archive member iterator. |
309 | ||
310 | class Archive::const_iterator | |
311 | { | |
312 | public: | |
313 | // The header of an archive member. This is what this iterator | |
314 | // points to. | |
315 | struct Header | |
316 | { | |
317 | // The name of the member. | |
318 | std::string name; | |
319 | // The file offset of the member. | |
320 | off_t off; | |
321 | // The file offset of a nested archive member. | |
322 | off_t nested_off; | |
323 | // The size of the member. | |
324 | off_t size; | |
325 | }; | |
326 | ||
2a00e4fb | 327 | const_iterator(Archive* archive, off_t off) |
92de84a6 ILT |
328 | : archive_(archive), off_(off) |
329 | { this->read_next_header(); } | |
330 | ||
331 | const Header& | |
332 | operator*() const | |
333 | { return this->header_; } | |
334 | ||
335 | const Header* | |
336 | operator->() const | |
337 | { return &this->header_; } | |
338 | ||
339 | const_iterator& | |
340 | operator++() | |
341 | { | |
342 | if (this->off_ == this->archive_->file().filesize()) | |
343 | return *this; | |
344 | this->off_ += sizeof(Archive_header); | |
345 | if (!this->archive_->is_thin_archive()) | |
346 | this->off_ += this->header_.size; | |
347 | if ((this->off_ & 1) != 0) | |
348 | ++this->off_; | |
349 | this->read_next_header(); | |
350 | return *this; | |
351 | } | |
352 | ||
353 | const_iterator | |
354 | operator++(int) | |
355 | { | |
356 | const_iterator ret = *this; | |
357 | ++*this; | |
358 | return ret; | |
359 | } | |
360 | ||
361 | bool | |
362 | operator==(const const_iterator p) const | |
363 | { return this->off_ == p->off; } | |
364 | ||
365 | bool | |
366 | operator!=(const const_iterator p) const | |
367 | { return this->off_ != p->off; } | |
368 | ||
369 | private: | |
370 | void | |
371 | read_next_header(); | |
372 | ||
373 | // The underlying archive. | |
2a00e4fb | 374 | Archive* archive_; |
92de84a6 ILT |
375 | // The current offset in the file. |
376 | off_t off_; | |
377 | // The current archive header. | |
378 | Header header_; | |
379 | }; | |
380 | ||
381 | // Read the next archive header. | |
4973341a ILT |
382 | |
383 | void | |
92de84a6 | 384 | Archive::const_iterator::read_next_header() |
4973341a | 385 | { |
92de84a6 | 386 | off_t filesize = this->archive_->file().filesize(); |
4973341a ILT |
387 | while (true) |
388 | { | |
92de84a6 ILT |
389 | if (filesize - this->off_ < static_cast<off_t>(sizeof(Archive_header))) |
390 | { | |
391 | if (filesize != this->off_) | |
392 | { | |
393 | gold_error(_("%s: short archive header at %zu"), | |
394 | this->archive_->filename().c_str(), | |
395 | static_cast<size_t>(this->off_)); | |
396 | this->off_ = filesize; | |
397 | } | |
398 | this->header_.off = filesize; | |
399 | return; | |
400 | } | |
401 | ||
402 | unsigned char buf[sizeof(Archive_header)]; | |
403 | this->archive_->file().read(this->off_, sizeof(Archive_header), buf); | |
404 | ||
405 | const Archive_header* hdr = reinterpret_cast<const Archive_header*>(buf); | |
406 | this->header_.size = | |
407 | this->archive_->interpret_header(hdr, this->off_, &this->header_.name, | |
408 | &this->header_.nested_off); | |
409 | this->header_.off = this->off_; | |
410 | ||
411 | // Skip special members. | |
412 | if (!this->header_.name.empty() && this->header_.name != "/") | |
413 | return; | |
414 | ||
415 | this->off_ += sizeof(Archive_header) + this->header_.size; | |
416 | if ((this->off_ & 1) != 0) | |
417 | ++this->off_; | |
4973341a ILT |
418 | } |
419 | } | |
420 | ||
92de84a6 ILT |
421 | // Initial iterator. |
422 | ||
423 | Archive::const_iterator | |
2a00e4fb | 424 | Archive::begin() |
92de84a6 ILT |
425 | { |
426 | return Archive::const_iterator(this, sarmag); | |
427 | } | |
428 | ||
429 | // Final iterator. | |
430 | ||
431 | Archive::const_iterator | |
2a00e4fb | 432 | Archive::end() |
92de84a6 ILT |
433 | { |
434 | return Archive::const_iterator(this, this->input_file_->file().filesize()); | |
435 | } | |
436 | ||
ac45a351 CC |
437 | // Get the file and offset for an archive member, which may be an |
438 | // external member of a thin archive. Set *INPUT_FILE to the | |
439 | // file containing the actual member, *MEMOFF to the offset | |
440 | // within that file (0 if not a nested archive), and *MEMBER_NAME | |
441 | // to the name of the archive member. Return TRUE on success. | |
442 | ||
443 | bool | |
15f8229b | 444 | Archive::get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff, |
89fc3421 | 445 | off_t* memsize, std::string* member_name) |
ac45a351 CC |
446 | { |
447 | off_t nested_off; | |
448 | ||
89fc3421 | 449 | *memsize = this->read_header(off, false, member_name, &nested_off); |
ac45a351 CC |
450 | |
451 | *input_file = this->input_file_; | |
452 | *memoff = off + static_cast<off_t>(sizeof(Archive_header)); | |
453 | ||
454 | if (!this->is_thin_archive_) | |
455 | return true; | |
456 | ||
457 | // Adjust a relative pathname so that it is relative | |
458 | // to the directory containing the archive. | |
459 | if (!IS_ABSOLUTE_PATH(member_name->c_str())) | |
460 | { | |
fbd8a257 | 461 | const char* arch_path = this->filename().c_str(); |
ac45a351 CC |
462 | const char* basename = lbasename(arch_path); |
463 | if (basename > arch_path) | |
464 | member_name->replace(0, 0, | |
fbd8a257 | 465 | this->filename().substr(0, basename - arch_path)); |
ac45a351 CC |
466 | } |
467 | ||
468 | if (nested_off > 0) | |
469 | { | |
470 | // This is a member of a nested archive. Open the containing | |
471 | // archive if we don't already have it open, then do a recursive | |
472 | // call to include the member from that archive. | |
473 | Archive* arch; | |
474 | Nested_archive_table::const_iterator p = | |
475 | this->nested_archives_.find(*member_name); | |
476 | if (p != this->nested_archives_.end()) | |
477 | arch = p->second; | |
478 | else | |
479 | { | |
480 | Input_file_argument* input_file_arg = | |
481 | new Input_file_argument(member_name->c_str(), false, "", false, | |
482 | parameters->options()); | |
483 | *input_file = new Input_file(input_file_arg); | |
15f8229b ILT |
484 | int dummy = 0; |
485 | if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy)) | |
ac45a351 CC |
486 | return false; |
487 | arch = new Archive(*member_name, *input_file, false, this->dirpath_, | |
488 | this->task_); | |
15f8229b | 489 | arch->setup(); |
ac45a351 CC |
490 | std::pair<Nested_archive_table::iterator, bool> ins = |
491 | this->nested_archives_.insert(std::make_pair(*member_name, arch)); | |
492 | gold_assert(ins.second); | |
493 | } | |
15f8229b ILT |
494 | return arch->get_file_and_offset(nested_off, input_file, memoff, |
495 | memsize, member_name); | |
ac45a351 CC |
496 | } |
497 | ||
498 | // This is an external member of a thin archive. Open the | |
499 | // file as a regular relocatable object file. | |
500 | Input_file_argument* input_file_arg = | |
501 | new Input_file_argument(member_name->c_str(), false, "", false, | |
502 | this->input_file_->options()); | |
503 | *input_file = new Input_file(input_file_arg); | |
15f8229b ILT |
504 | int dummy = 0; |
505 | if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy)) | |
ac45a351 CC |
506 | return false; |
507 | ||
508 | *memoff = 0; | |
89fc3421 | 509 | *memsize = (*input_file)->file().filesize(); |
ac45a351 CC |
510 | return true; |
511 | } | |
512 | ||
15f8229b ILT |
513 | // Return an ELF object for the member at offset OFF. If the ELF |
514 | // object has an unsupported target type, set *PUNCONFIGURED to true | |
515 | // and return NULL. | |
ac45a351 CC |
516 | |
517 | Object* | |
15f8229b | 518 | Archive::get_elf_object_for_member(off_t off, bool* punconfigured) |
ac45a351 | 519 | { |
15f8229b ILT |
520 | *punconfigured = false; |
521 | ||
ac45a351 CC |
522 | Input_file* input_file; |
523 | off_t memoff; | |
89fc3421 | 524 | off_t memsize; |
15f8229b ILT |
525 | std::string member_name; |
526 | if (!this->get_file_and_offset(off, &input_file, &memoff, &memsize, | |
527 | &member_name)) | |
ac45a351 CC |
528 | return NULL; |
529 | ||
89fc3421 CC |
530 | if (parameters->options().has_plugins()) |
531 | { | |
532 | Object* obj = parameters->options().plugins()->claim_file(input_file, | |
533 | memoff, | |
534 | memsize); | |
535 | if (obj != NULL) | |
536 | { | |
537 | // The input file was claimed by a plugin, and its symbols | |
538 | // have been provided by the plugin. | |
89fc3421 CC |
539 | return obj; |
540 | } | |
541 | } | |
542 | ||
f6060a4d ILT |
543 | const unsigned char* ehdr; |
544 | int read_size; | |
545 | if (!is_elf_object(input_file, memoff, &ehdr, &read_size)) | |
ac45a351 CC |
546 | { |
547 | gold_error(_("%s: member at %zu is not an ELF object"), | |
548 | this->name().c_str(), static_cast<size_t>(off)); | |
549 | return NULL; | |
550 | } | |
551 | ||
552 | return make_elf_object((std::string(this->input_file_->filename()) | |
15f8229b ILT |
553 | + "(" + member_name + ")"), |
554 | input_file, memoff, ehdr, read_size, | |
555 | punconfigured); | |
ac45a351 CC |
556 | } |
557 | ||
558 | // Read the symbols from all the archive members in the link. | |
559 | ||
560 | void | |
15f8229b | 561 | Archive::read_all_symbols() |
ac45a351 CC |
562 | { |
563 | for (Archive::const_iterator p = this->begin(); | |
564 | p != this->end(); | |
565 | ++p) | |
15f8229b | 566 | this->read_symbols(p->off); |
ac45a351 CC |
567 | } |
568 | ||
569 | // Read the symbols from an archive member in the link. OFF is the file | |
570 | // offset of the member header. | |
571 | ||
572 | void | |
15f8229b | 573 | Archive::read_symbols(off_t off) |
ac45a351 | 574 | { |
15f8229b ILT |
575 | bool dummy; |
576 | Object* obj = this->get_elf_object_for_member(off, &dummy); | |
ac45a351 CC |
577 | |
578 | if (obj == NULL) | |
579 | return; | |
580 | ||
581 | Read_symbols_data* sd = new Read_symbols_data; | |
582 | obj->read_symbols(sd); | |
583 | Archive_member member(obj, sd); | |
584 | this->members_[off] = member; | |
585 | } | |
586 | ||
587 | // Select members from the archive and add them to the link. We walk | |
588 | // through the elements in the archive map, and look each one up in | |
589 | // the symbol table. If it exists as a strong undefined symbol, we | |
590 | // pull in the corresponding element. We have to do this in a loop, | |
591 | // since pulling in one element may create new undefined symbols which | |
15f8229b ILT |
592 | // may be satisfied by other objects in the archive. Return true in |
593 | // the normal case, false if the first member we tried to add from | |
594 | // this archive had an incompatible target. | |
ac45a351 | 595 | |
15f8229b | 596 | bool |
ac45a351 CC |
597 | Archive::add_symbols(Symbol_table* symtab, Layout* layout, |
598 | Input_objects* input_objects, Mapfile* mapfile) | |
599 | { | |
600 | ++Archive::total_archives; | |
601 | ||
602 | if (this->input_file_->options().whole_archive()) | |
603 | return this->include_all_members(symtab, layout, input_objects, | |
604 | mapfile); | |
605 | ||
606 | Archive::total_members += this->num_members_; | |
607 | ||
608 | input_objects->archive_start(this); | |
609 | ||
610 | const size_t armap_size = this->armap_.size(); | |
611 | ||
612 | // This is a quick optimization, since we usually see many symbols | |
613 | // in a row with the same offset. last_seen_offset holds the last | |
614 | // offset we saw that was present in the seen_offsets_ set. | |
615 | off_t last_seen_offset = -1; | |
616 | ||
617 | // Track which symbols in the symbol table we've already found to be | |
618 | // defined. | |
619 | ||
9c5b8369 ILT |
620 | char* tmpbuf = NULL; |
621 | size_t tmpbuflen = 0; | |
ac45a351 CC |
622 | bool added_new_object; |
623 | do | |
624 | { | |
625 | added_new_object = false; | |
626 | for (size_t i = 0; i < armap_size; ++i) | |
627 | { | |
628 | if (this->armap_checked_[i]) | |
629 | continue; | |
630 | if (this->armap_[i].file_offset == last_seen_offset) | |
631 | { | |
632 | this->armap_checked_[i] = true; | |
633 | continue; | |
634 | } | |
635 | if (this->seen_offsets_.find(this->armap_[i].file_offset) | |
636 | != this->seen_offsets_.end()) | |
637 | { | |
638 | this->armap_checked_[i] = true; | |
639 | last_seen_offset = this->armap_[i].file_offset; | |
640 | continue; | |
641 | } | |
642 | ||
643 | const char* sym_name = (this->armap_names_.data() | |
644 | + this->armap_[i].name_offset); | |
9c5b8369 ILT |
645 | |
646 | // In an object file, and therefore in an archive map, an | |
647 | // '@' in the name separates the symbol name from the | |
648 | // version name. If there are two '@' characters, this is | |
649 | // the default version. | |
650 | const char* ver = strchr(sym_name, '@'); | |
651 | bool def = false; | |
652 | if (ver != NULL) | |
653 | { | |
654 | size_t symlen = ver - sym_name; | |
655 | if (symlen + 1 > tmpbuflen) | |
656 | { | |
657 | tmpbuf = static_cast<char*>(realloc(tmpbuf, symlen + 1)); | |
658 | tmpbuflen = symlen + 1; | |
659 | } | |
660 | memcpy(tmpbuf, sym_name, symlen); | |
661 | tmpbuf[symlen] = '\0'; | |
662 | sym_name = tmpbuf; | |
663 | ||
664 | ++ver; | |
665 | if (*ver == '@') | |
666 | { | |
667 | ++ver; | |
668 | def = true; | |
669 | } | |
670 | } | |
671 | ||
672 | Symbol* sym = symtab->lookup(sym_name, ver); | |
673 | if (def | |
674 | && (sym == NULL | |
675 | || !sym->is_undefined() | |
676 | || sym->binding() == elfcpp::STB_WEAK)) | |
677 | sym = symtab->lookup(sym_name, NULL); | |
678 | ||
ac45a351 CC |
679 | if (sym == NULL) |
680 | { | |
681 | // Check whether the symbol was named in a -u option. | |
682 | if (!parameters->options().is_undefined(sym_name)) | |
683 | continue; | |
684 | } | |
685 | else if (!sym->is_undefined()) | |
686 | { | |
687 | this->armap_checked_[i] = true; | |
688 | continue; | |
689 | } | |
690 | else if (sym->binding() == elfcpp::STB_WEAK) | |
691 | continue; | |
692 | ||
693 | // We want to include this object in the link. | |
694 | last_seen_offset = this->armap_[i].file_offset; | |
695 | this->seen_offsets_.insert(last_seen_offset); | |
696 | this->armap_checked_[i] = true; | |
697 | ||
698 | std::string why; | |
699 | if (sym == NULL) | |
700 | { | |
701 | why = "-u "; | |
702 | why += sym_name; | |
703 | } | |
15f8229b ILT |
704 | if (!this->include_member(symtab, layout, input_objects, |
705 | last_seen_offset, mapfile, sym, | |
706 | why.c_str())) | |
9c5b8369 ILT |
707 | { |
708 | if (tmpbuf != NULL) | |
709 | free(tmpbuf); | |
710 | return false; | |
711 | } | |
ac45a351 CC |
712 | |
713 | added_new_object = true; | |
714 | } | |
715 | } | |
716 | while (added_new_object); | |
717 | ||
9c5b8369 ILT |
718 | if (tmpbuf != NULL) |
719 | free(tmpbuf); | |
720 | ||
ac45a351 | 721 | input_objects->archive_stop(this); |
15f8229b ILT |
722 | |
723 | return true; | |
ac45a351 CC |
724 | } |
725 | ||
92de84a6 ILT |
726 | // Include all the archive members in the link. This is for --whole-archive. |
727 | ||
15f8229b | 728 | bool |
92de84a6 ILT |
729 | Archive::include_all_members(Symbol_table* symtab, Layout* layout, |
730 | Input_objects* input_objects, Mapfile* mapfile) | |
731 | { | |
732 | input_objects->archive_start(this); | |
733 | ||
ac45a351 CC |
734 | if (this->members_.size() > 0) |
735 | { | |
736 | std::map<off_t, Archive_member>::const_iterator p; | |
737 | for (p = this->members_.begin(); | |
738 | p != this->members_.end(); | |
739 | ++p) | |
740 | { | |
15f8229b ILT |
741 | if (!this->include_member(symtab, layout, input_objects, p->first, |
742 | mapfile, NULL, "--whole-archive")) | |
743 | return false; | |
ac45a351 CC |
744 | ++Archive::total_members; |
745 | } | |
746 | } | |
747 | else | |
748 | { | |
749 | for (Archive::const_iterator p = this->begin(); | |
750 | p != this->end(); | |
751 | ++p) | |
752 | { | |
15f8229b ILT |
753 | if (!this->include_member(symtab, layout, input_objects, p->off, |
754 | mapfile, NULL, "--whole-archive")) | |
755 | return false; | |
ac45a351 CC |
756 | ++Archive::total_members; |
757 | } | |
758 | } | |
92de84a6 ILT |
759 | |
760 | input_objects->archive_stop(this); | |
15f8229b ILT |
761 | |
762 | return true; | |
92de84a6 ILT |
763 | } |
764 | ||
765 | // Return the number of members in the archive. This is only used for | |
766 | // reports. | |
767 | ||
768 | size_t | |
2a00e4fb | 769 | Archive::count_members() |
92de84a6 ILT |
770 | { |
771 | size_t ret = 0; | |
772 | for (Archive::const_iterator p = this->begin(); | |
773 | p != this->end(); | |
774 | ++p) | |
775 | ++ret; | |
776 | return ret; | |
777 | } | |
778 | ||
61ba1cf9 | 779 | // Include an archive member in the link. OFF is the file offset of |
7d9e3d98 | 780 | // the member header. WHY is the reason we are including this member. |
15f8229b ILT |
781 | // Return true if we added the member or if we had an error, return |
782 | // false if this was the first member we tried to add from this | |
783 | // archive and it had an incompatible format. | |
61ba1cf9 | 784 | |
15f8229b | 785 | bool |
7e1edb90 | 786 | Archive::include_member(Symbol_table* symtab, Layout* layout, |
7d9e3d98 ILT |
787 | Input_objects* input_objects, off_t off, |
788 | Mapfile* mapfile, Symbol* sym, const char* why) | |
61ba1cf9 | 789 | { |
ac45a351 | 790 | ++Archive::total_members_loaded; |
7d9e3d98 | 791 | |
ac45a351 CC |
792 | std::map<off_t, Archive_member>::const_iterator p = this->members_.find(off); |
793 | if (p != this->members_.end()) | |
a1207466 | 794 | { |
ac45a351 | 795 | Object *obj = p->second.obj_; |
15f8229b ILT |
796 | |
797 | if (!this->included_member_ | |
798 | && this->searched_for() | |
799 | && !parameters->is_compatible_target(obj->target())) | |
800 | return false; | |
801 | ||
ac45a351 CC |
802 | Read_symbols_data *sd = p->second.sd_; |
803 | if (mapfile != NULL) | |
804 | mapfile->report_include_archive_member(obj->name(), sym, why); | |
805 | if (input_objects->add_object(obj)) | |
a1207466 | 806 | { |
ac45a351 | 807 | obj->layout(symtab, layout, sd); |
f488e4b0 | 808 | obj->add_symbols(symtab, sd, layout); |
15f8229b | 809 | this->included_member_ = true; |
a1207466 | 810 | } |
ac45a351 | 811 | delete sd; |
15f8229b ILT |
812 | return true; |
813 | } | |
814 | ||
815 | bool unconfigured; | |
816 | Object* obj = this->get_elf_object_for_member(off, &unconfigured); | |
817 | ||
818 | if (!this->included_member_ | |
819 | && this->searched_for() | |
820 | && (obj == NULL | |
821 | ? unconfigured | |
822 | : !parameters->is_compatible_target(obj->target()))) | |
823 | { | |
824 | if (obj != NULL) | |
825 | delete obj; | |
826 | return false; | |
61ba1cf9 ILT |
827 | } |
828 | ||
ac45a351 | 829 | if (obj == NULL) |
15f8229b | 830 | return true; |
61ba1cf9 | 831 | |
ac45a351 CC |
832 | if (mapfile != NULL) |
833 | mapfile->report_include_archive_member(obj->name(), sym, why); | |
61ba1cf9 | 834 | |
89fc3421 CC |
835 | Pluginobj* pluginobj = obj->pluginobj(); |
836 | if (pluginobj != NULL) | |
837 | { | |
f488e4b0 | 838 | pluginobj->add_symbols(symtab, NULL, layout); |
15f8229b ILT |
839 | this->included_member_ = true; |
840 | return true; | |
89fc3421 CC |
841 | } |
842 | ||
15f8229b ILT |
843 | if (!input_objects->add_object(obj)) |
844 | delete obj; | |
845 | else | |
019cdb1a ILT |
846 | { |
847 | Read_symbols_data sd; | |
848 | obj->read_symbols(&sd); | |
849 | obj->layout(symtab, layout, &sd); | |
f488e4b0 | 850 | obj->add_symbols(symtab, &sd, layout); |
fbd8a257 CC |
851 | |
852 | // If this is an external member of a thin archive, unlock the file | |
853 | // for the next task. | |
854 | if (obj->offset() == 0) | |
855 | obj->unlock(this->task_); | |
15f8229b ILT |
856 | |
857 | this->included_member_ = true; | |
019cdb1a | 858 | } |
15f8229b ILT |
859 | |
860 | return true; | |
ac45a351 | 861 | } |
61ba1cf9 | 862 | |
ac45a351 CC |
863 | // Print statistical information to stderr. This is used for --stats. |
864 | ||
865 | void | |
866 | Archive::print_stats() | |
867 | { | |
868 | fprintf(stderr, _("%s: archive libraries: %u\n"), | |
869 | program_name, Archive::total_archives); | |
870 | fprintf(stderr, _("%s: total archive members: %u\n"), | |
871 | program_name, Archive::total_members); | |
872 | fprintf(stderr, _("%s: loaded archive members: %u\n"), | |
873 | program_name, Archive::total_members_loaded); | |
61ba1cf9 ILT |
874 | } |
875 | ||
876 | // Add_archive_symbols methods. | |
877 | ||
878 | Add_archive_symbols::~Add_archive_symbols() | |
879 | { | |
880 | if (this->this_blocker_ != NULL) | |
881 | delete this->this_blocker_; | |
882 | // next_blocker_ is deleted by the task associated with the next | |
883 | // input file. | |
884 | } | |
885 | ||
886 | // Return whether we can add the archive symbols. We are blocked by | |
887 | // this_blocker_. We block next_blocker_. We also lock the file. | |
888 | ||
17a1d0a9 ILT |
889 | Task_token* |
890 | Add_archive_symbols::is_runnable() | |
61ba1cf9 ILT |
891 | { |
892 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
17a1d0a9 ILT |
893 | return this->this_blocker_; |
894 | return NULL; | |
61ba1cf9 ILT |
895 | } |
896 | ||
17a1d0a9 ILT |
897 | void |
898 | Add_archive_symbols::locks(Task_locker* tl) | |
61ba1cf9 | 899 | { |
17a1d0a9 ILT |
900 | tl->add(this, this->next_blocker_); |
901 | tl->add(this, this->archive_->token()); | |
61ba1cf9 ILT |
902 | } |
903 | ||
904 | void | |
15f8229b | 905 | Add_archive_symbols::run(Workqueue* workqueue) |
61ba1cf9 | 906 | { |
15f8229b ILT |
907 | bool added = this->archive_->add_symbols(this->symtab_, this->layout_, |
908 | this->input_objects_, | |
909 | this->mapfile_); | |
a1207466 CC |
910 | this->archive_->unlock_nested_archives(); |
911 | ||
17a1d0a9 | 912 | this->archive_->release(); |
39d0cb0e | 913 | this->archive_->clear_uncached_views(); |
17a1d0a9 | 914 | |
15f8229b ILT |
915 | if (!added) |
916 | { | |
917 | // This archive holds object files which are incompatible with | |
918 | // our output file. | |
919 | Read_symbols::incompatible_warning(this->input_argument_, | |
920 | this->archive_->input_file()); | |
921 | Read_symbols::requeue(workqueue, this->input_objects_, this->symtab_, | |
922 | this->layout_, this->dirpath_, this->dirindex_, | |
923 | this->mapfile_, this->input_argument_, | |
924 | this->input_group_, this->next_blocker_); | |
925 | delete this->archive_; | |
926 | return; | |
927 | } | |
928 | ||
ead1e424 ILT |
929 | if (this->input_group_ != NULL) |
930 | this->input_group_->add_archive(this->archive_); | |
931 | else | |
932 | { | |
933 | // We no longer need to know about this archive. | |
934 | delete this->archive_; | |
c7912668 | 935 | this->archive_ = NULL; |
ead1e424 | 936 | } |
61ba1cf9 ILT |
937 | } |
938 | ||
939 | } // End namespace gold. |