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