Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // archive.cc -- archive support for gold |
2 | ||
43819297 RM |
3 | // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2013 |
4 | // Free Software Foundation, Inc. | |
6cb15b7f ILT |
5 | // Written by Ian Lance Taylor <iant@google.com>. |
6 | ||
7 | // This file is part of gold. | |
8 | ||
9 | // This program is free software; you can redistribute it and/or modify | |
10 | // it under the terms of the GNU General Public License as published by | |
11 | // the Free Software Foundation; either version 3 of the License, or | |
12 | // (at your option) any later version. | |
13 | ||
14 | // This program is distributed in the hope that it will be useful, | |
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | // GNU General Public License for more details. | |
18 | ||
19 | // You should have received a copy of the GNU General Public License | |
20 | // along with this program; if not, write to the Free Software | |
21 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
22 | // MA 02110-1301, USA. | |
23 | ||
61ba1cf9 ILT |
24 | #include "gold.h" |
25 | ||
26 | #include <cerrno> | |
27 | #include <cstring> | |
28 | #include <climits> | |
29 | #include <vector> | |
a1207466 CC |
30 | #include "libiberty.h" |
31 | #include "filenames.h" | |
61ba1cf9 ILT |
32 | |
33 | #include "elfcpp.h" | |
7e1edb90 | 34 | #include "options.h" |
7d9e3d98 | 35 | #include "mapfile.h" |
61ba1cf9 | 36 | #include "fileread.h" |
ead1e424 | 37 | #include "readsyms.h" |
61ba1cf9 ILT |
38 | #include "symtab.h" |
39 | #include "object.h" | |
88a4108b | 40 | #include "layout.h" |
61ba1cf9 | 41 | #include "archive.h" |
89fc3421 | 42 | #include "plugin.h" |
09ec0418 | 43 | #include "incremental.h" |
61ba1cf9 ILT |
44 | |
45 | namespace gold | |
46 | { | |
47 | ||
e0c52780 CC |
48 | // Library_base methods. |
49 | ||
50 | // Determine whether a definition of SYM_NAME should cause an archive | |
51 | // library member to be included in the link. Returns SHOULD_INCLUDE_YES | |
52 | // if the symbol is referenced but not defined, SHOULD_INCLUDE_NO if the | |
53 | // symbol is already defined, and SHOULD_INCLUDE_UNKNOWN if the symbol is | |
54 | // neither referenced nor defined. | |
55 | ||
56 | Library_base::Should_include | |
57 | Library_base::should_include_member(Symbol_table* symtab, Layout* layout, | |
58 | const char* sym_name, Symbol** symp, | |
59 | std::string* why, char** tmpbufp, | |
60 | size_t* tmpbuflen) | |
61 | { | |
62 | // In an object file, and therefore in an archive map, an | |
63 | // '@' in the name separates the symbol name from the | |
64 | // version name. If there are two '@' characters, this is | |
65 | // the default version. | |
66 | char* tmpbuf = *tmpbufp; | |
67 | const char* ver = strchr(sym_name, '@'); | |
68 | bool def = false; | |
69 | if (ver != NULL) | |
70 | { | |
71 | size_t symlen = ver - sym_name; | |
72 | if (symlen + 1 > *tmpbuflen) | |
73 | { | |
74 | tmpbuf = static_cast<char*>(xrealloc(tmpbuf, symlen + 1)); | |
75 | *tmpbufp = tmpbuf; | |
76 | *tmpbuflen = symlen + 1; | |
77 | } | |
78 | memcpy(tmpbuf, sym_name, symlen); | |
79 | tmpbuf[symlen] = '\0'; | |
80 | sym_name = tmpbuf; | |
81 | ||
82 | ++ver; | |
83 | if (*ver == '@') | |
84 | { | |
85 | ++ver; | |
86 | def = true; | |
87 | } | |
88 | } | |
89 | ||
90 | Symbol* sym = symtab->lookup(sym_name, ver); | |
91 | if (def | |
92 | && ver != NULL | |
93 | && (sym == NULL | |
94 | || !sym->is_undefined() | |
95 | || sym->binding() == elfcpp::STB_WEAK)) | |
96 | sym = symtab->lookup(sym_name, NULL); | |
97 | ||
98 | *symp = sym; | |
99 | ||
100 | if (sym == NULL) | |
101 | { | |
102 | // Check whether the symbol was named in a -u option. | |
103 | if (parameters->options().is_undefined(sym_name)) | |
104 | { | |
105 | *why = "-u "; | |
106 | *why += sym_name; | |
107 | } | |
31821be0 CC |
108 | else if (parameters->options().is_export_dynamic_symbol(sym_name)) |
109 | { | |
110 | *why = "--export-dynamic-symbol "; | |
111 | *why += sym_name; | |
112 | } | |
e0c52780 CC |
113 | else if (layout->script_options()->is_referenced(sym_name)) |
114 | { | |
115 | size_t alc = 100 + strlen(sym_name); | |
116 | char* buf = new char[alc]; | |
117 | snprintf(buf, alc, _("script or expression reference to %s"), | |
118 | sym_name); | |
119 | *why = buf; | |
120 | delete[] buf; | |
121 | } | |
a10ae760 ILT |
122 | else if (strcmp(sym_name, parameters->entry()) == 0) |
123 | { | |
124 | *why = "entry symbol "; | |
125 | *why += sym_name; | |
126 | } | |
e0c52780 CC |
127 | else |
128 | return Library_base::SHOULD_INCLUDE_UNKNOWN; | |
129 | } | |
130 | else if (!sym->is_undefined()) | |
131 | return Library_base::SHOULD_INCLUDE_NO; | |
132 | // PR 12001: Do not include an archive when the undefined | |
133 | // symbol has actually been defined on the command line. | |
134 | else if (layout->script_options()->is_pending_assignment(sym_name)) | |
135 | return Library_base::SHOULD_INCLUDE_NO; | |
136 | else if (sym->binding() == elfcpp::STB_WEAK) | |
137 | return Library_base::SHOULD_INCLUDE_UNKNOWN; | |
138 | ||
139 | return Library_base::SHOULD_INCLUDE_YES; | |
140 | } | |
141 | ||
61ba1cf9 | 142 | // The header of an entry in the archive. This is all readable text, |
9b547ce6 | 143 | // padded with spaces where necessary. If the contents of an archive |
61ba1cf9 ILT |
144 | // are all text file, the entire archive is readable. |
145 | ||
146 | struct Archive::Archive_header | |
147 | { | |
148 | // The entry name. | |
149 | char ar_name[16]; | |
150 | // The file modification time. | |
151 | char ar_date[12]; | |
152 | // The user's UID in decimal. | |
153 | char ar_uid[6]; | |
154 | // The user's GID in decimal. | |
155 | char ar_gid[6]; | |
156 | // The file mode in octal. | |
157 | char ar_mode[8]; | |
158 | // The file size in decimal. | |
159 | char ar_size[10]; | |
160 | // The final magic code. | |
161 | char ar_fmag[2]; | |
162 | }; | |
163 | ||
ac45a351 CC |
164 | // Class Archive static variables. |
165 | unsigned int Archive::total_archives; | |
166 | unsigned int Archive::total_members; | |
167 | unsigned int Archive::total_members_loaded; | |
168 | ||
61ba1cf9 ILT |
169 | // Archive methods. |
170 | ||
171 | const char Archive::armag[sarmag] = | |
172 | { | |
173 | '!', '<', 'a', 'r', 'c', 'h', '>', '\n' | |
174 | }; | |
175 | ||
a1207466 CC |
176 | const char Archive::armagt[sarmag] = |
177 | { | |
178 | '!', '<', 't', 'h', 'i', 'n', '>', '\n' | |
179 | }; | |
180 | ||
61ba1cf9 ILT |
181 | const char Archive::arfmag[2] = { '`', '\n' }; |
182 | ||
2ea97941 ILT |
183 | Archive::Archive(const std::string& name, Input_file* input_file, |
184 | bool is_thin_archive, Dirsearch* dirpath, Task* task) | |
e0c52780 CC |
185 | : Library_base(task), name_(name), input_file_(input_file), armap_(), |
186 | armap_names_(), extended_names_(), armap_checked_(), seen_offsets_(), | |
187 | members_(), is_thin_archive_(is_thin_archive), included_member_(false), | |
7cdb37d9 CC |
188 | nested_archives_(), dirpath_(dirpath), num_members_(0), |
189 | included_all_members_(false) | |
65514900 CC |
190 | { |
191 | this->no_export_ = | |
2ea97941 | 192 | parameters->options().check_excluded_libs(input_file->found_name()); |
65514900 CC |
193 | } |
194 | ||
61ba1cf9 ILT |
195 | // Set up the archive: read the symbol map and the extended name |
196 | // table. | |
197 | ||
198 | void | |
15f8229b | 199 | Archive::setup() |
61ba1cf9 | 200 | { |
3e95a404 ILT |
201 | // We need to ignore empty archives. |
202 | if (this->input_file_->file().filesize() == sarmag) | |
a1207466 | 203 | return; |
3e95a404 | 204 | |
61ba1cf9 ILT |
205 | // The first member of the archive should be the symbol table. |
206 | std::string armap_name; | |
61ab3e40 ILT |
207 | off_t header_size = this->read_header(sarmag, false, &armap_name, NULL); |
208 | if (header_size == -1) | |
209 | return; | |
210 | ||
211 | section_size_type armap_size = convert_to_section_size_type(header_size); | |
75f2446e | 212 | off_t off = sarmag; |
4973341a ILT |
213 | if (armap_name.empty()) |
214 | { | |
215 | this->read_armap(sarmag + sizeof(Archive_header), armap_size); | |
216 | off = sarmag + sizeof(Archive_header) + armap_size; | |
217 | } | |
45aa233b | 218 | else if (!this->input_file_->options().whole_archive()) |
75f2446e ILT |
219 | gold_error(_("%s: no archive symbol table (run ranlib)"), |
220 | this->name().c_str()); | |
4973341a | 221 | |
cb295612 ILT |
222 | // See if there is an extended name table. We cache these views |
223 | // because it is likely that we will want to read the following | |
224 | // header in the add_symbols routine. | |
4973341a ILT |
225 | if ((off & 1) != 0) |
226 | ++off; | |
227 | std::string xname; | |
61ab3e40 ILT |
228 | header_size = this->read_header(off, true, &xname, NULL); |
229 | if (header_size == -1) | |
230 | return; | |
231 | ||
232 | section_size_type extended_size = convert_to_section_size_type(header_size); | |
4973341a ILT |
233 | if (xname == "/") |
234 | { | |
235 | const unsigned char* p = this->get_view(off + sizeof(Archive_header), | |
39d0cb0e | 236 | extended_size, false, true); |
4973341a ILT |
237 | const char* px = reinterpret_cast<const char*>(p); |
238 | this->extended_names_.assign(px, extended_size); | |
239 | } | |
ac45a351 CC |
240 | bool preread_syms = (parameters->options().threads() |
241 | && parameters->options().preread_archive_symbols()); | |
242 | #ifndef ENABLE_THREADS | |
243 | preread_syms = false; | |
89fc3421 CC |
244 | #else |
245 | if (parameters->options().has_plugins()) | |
246 | preread_syms = false; | |
ac45a351 CC |
247 | #endif |
248 | if (preread_syms) | |
15f8229b | 249 | this->read_all_symbols(); |
a1207466 CC |
250 | } |
251 | ||
252 | // Unlock any nested archives. | |
4973341a | 253 | |
a1207466 CC |
254 | void |
255 | Archive::unlock_nested_archives() | |
256 | { | |
257 | for (Nested_archive_table::iterator p = this->nested_archives_.begin(); | |
258 | p != this->nested_archives_.end(); | |
259 | ++p) | |
260 | { | |
261 | p->second->unlock(this->task_); | |
262 | } | |
4973341a | 263 | } |
61ba1cf9 | 264 | |
4973341a ILT |
265 | // Read the archive symbol map. |
266 | ||
267 | void | |
8383303e | 268 | Archive::read_armap(off_t start, section_size_type size) |
4973341a | 269 | { |
ac45a351 CC |
270 | // To count the total number of archive members, we'll just count |
271 | // the number of times the file offset changes. Since most archives | |
272 | // group the symbols in the armap by object, this ought to give us | |
273 | // an accurate count. | |
274 | off_t last_seen_offset = -1; | |
275 | ||
61ba1cf9 | 276 | // Read in the entire armap. |
39d0cb0e | 277 | const unsigned char* p = this->get_view(start, size, true, false); |
61ba1cf9 ILT |
278 | |
279 | // Numbers in the armap are always big-endian. | |
280 | const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p); | |
f6ce93d6 | 281 | unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword); |
61ba1cf9 ILT |
282 | ++pword; |
283 | ||
284 | // Note that the addition is in units of sizeof(elfcpp::Elf_Word). | |
285 | const char* pnames = reinterpret_cast<const char*>(pword + nsyms); | |
8383303e ILT |
286 | section_size_type names_size = |
287 | reinterpret_cast<const char*>(p) + size - pnames; | |
9eb9fa57 | 288 | this->armap_names_.assign(pnames, names_size); |
61ba1cf9 ILT |
289 | |
290 | this->armap_.resize(nsyms); | |
291 | ||
8383303e | 292 | section_offset_type name_offset = 0; |
61ba1cf9 ILT |
293 | for (unsigned int i = 0; i < nsyms; ++i) |
294 | { | |
9eb9fa57 ILT |
295 | this->armap_[i].name_offset = name_offset; |
296 | this->armap_[i].file_offset = elfcpp::Swap<32, true>::readval(pword); | |
297 | name_offset += strlen(pnames + name_offset) + 1; | |
61ba1cf9 | 298 | ++pword; |
ac45a351 CC |
299 | if (this->armap_[i].file_offset != last_seen_offset) |
300 | { | |
301 | last_seen_offset = this->armap_[i].file_offset; | |
302 | ++this->num_members_; | |
303 | } | |
61ba1cf9 ILT |
304 | } |
305 | ||
8383303e | 306 | if (static_cast<section_size_type>(name_offset) > names_size) |
75f2446e ILT |
307 | gold_error(_("%s: bad archive symbol table names"), |
308 | this->name().c_str()); | |
a93d6d07 ILT |
309 | |
310 | // This array keeps track of which symbols are for archive elements | |
311 | // which we have already included in the link. | |
312 | this->armap_checked_.resize(nsyms); | |
61ba1cf9 ILT |
313 | } |
314 | ||
315 | // Read the header of an archive member at OFF. Fail if something | |
316 | // goes wrong. Return the size of the member. Set *PNAME to the name | |
317 | // of the member. | |
318 | ||
319 | off_t | |
a1207466 CC |
320 | Archive::read_header(off_t off, bool cache, std::string* pname, |
321 | off_t* nested_off) | |
61ba1cf9 | 322 | { |
39d0cb0e ILT |
323 | const unsigned char* p = this->get_view(off, sizeof(Archive_header), true, |
324 | cache); | |
61ba1cf9 | 325 | const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p); |
a1207466 | 326 | return this->interpret_header(hdr, off, pname, nested_off); |
4973341a | 327 | } |
61ba1cf9 | 328 | |
4973341a | 329 | // Interpret the header of HDR, the header of the archive member at |
61ab3e40 ILT |
330 | // file offset OFF. Return the size of the member, or -1 if something |
331 | // has gone wrong. Set *PNAME to the name of the member. | |
4973341a ILT |
332 | |
333 | off_t | |
334 | Archive::interpret_header(const Archive_header* hdr, off_t off, | |
92de84a6 | 335 | std::string* pname, off_t* nested_off) const |
4973341a | 336 | { |
61ba1cf9 ILT |
337 | if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0) |
338 | { | |
75f2446e ILT |
339 | gold_error(_("%s: malformed archive header at %zu"), |
340 | this->name().c_str(), static_cast<size_t>(off)); | |
61ab3e40 | 341 | return -1; |
61ba1cf9 ILT |
342 | } |
343 | ||
344 | const int size_string_size = sizeof hdr->ar_size; | |
345 | char size_string[size_string_size + 1]; | |
346 | memcpy(size_string, hdr->ar_size, size_string_size); | |
347 | char* ps = size_string + size_string_size; | |
348 | while (ps[-1] == ' ') | |
349 | --ps; | |
350 | *ps = '\0'; | |
351 | ||
352 | errno = 0; | |
2ea97941 ILT |
353 | char* end; |
354 | off_t member_size = strtol(size_string, &end, 10); | |
355 | if (*end != '\0' | |
61ba1cf9 ILT |
356 | || member_size < 0 |
357 | || (member_size == LONG_MAX && errno == ERANGE)) | |
358 | { | |
75f2446e ILT |
359 | gold_error(_("%s: malformed archive header size at %zu"), |
360 | this->name().c_str(), static_cast<size_t>(off)); | |
61ab3e40 | 361 | return -1; |
61ba1cf9 ILT |
362 | } |
363 | ||
364 | if (hdr->ar_name[0] != '/') | |
365 | { | |
366 | const char* name_end = strchr(hdr->ar_name, '/'); | |
367 | if (name_end == NULL | |
368 | || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name)) | |
369 | { | |
a0c4fb0a | 370 | gold_error(_("%s: malformed archive header name at %zu"), |
75f2446e | 371 | this->name().c_str(), static_cast<size_t>(off)); |
61ab3e40 | 372 | return -1; |
61ba1cf9 ILT |
373 | } |
374 | pname->assign(hdr->ar_name, name_end - hdr->ar_name); | |
a1207466 CC |
375 | if (nested_off != NULL) |
376 | *nested_off = 0; | |
61ba1cf9 ILT |
377 | } |
378 | else if (hdr->ar_name[1] == ' ') | |
379 | { | |
380 | // This is the symbol table. | |
114dfbe1 ILT |
381 | if (!pname->empty()) |
382 | pname->clear(); | |
61ba1cf9 ILT |
383 | } |
384 | else if (hdr->ar_name[1] == '/') | |
385 | { | |
386 | // This is the extended name table. | |
387 | pname->assign(1, '/'); | |
388 | } | |
389 | else | |
390 | { | |
391 | errno = 0; | |
2ea97941 | 392 | long x = strtol(hdr->ar_name + 1, &end, 10); |
a1207466 | 393 | long y = 0; |
2ea97941 ILT |
394 | if (*end == ':') |
395 | y = strtol(end + 1, &end, 10); | |
396 | if (*end != ' ' | |
61ba1cf9 ILT |
397 | || x < 0 |
398 | || (x == LONG_MAX && errno == ERANGE) | |
399 | || static_cast<size_t>(x) >= this->extended_names_.size()) | |
400 | { | |
75f2446e ILT |
401 | gold_error(_("%s: bad extended name index at %zu"), |
402 | this->name().c_str(), static_cast<size_t>(off)); | |
61ab3e40 | 403 | return -1; |
61ba1cf9 ILT |
404 | } |
405 | ||
2ea97941 ILT |
406 | const char* name = this->extended_names_.data() + x; |
407 | const char* name_end = strchr(name, '\n'); | |
408 | if (static_cast<size_t>(name_end - name) > this->extended_names_.size() | |
a1207466 | 409 | || name_end[-1] != '/') |
61ba1cf9 | 410 | { |
75f2446e ILT |
411 | gold_error(_("%s: bad extended name entry at header %zu"), |
412 | this->name().c_str(), static_cast<size_t>(off)); | |
61ab3e40 | 413 | return -1; |
61ba1cf9 | 414 | } |
2ea97941 | 415 | pname->assign(name, name_end - 1 - name); |
a1207466 CC |
416 | if (nested_off != NULL) |
417 | *nested_off = y; | |
61ba1cf9 ILT |
418 | } |
419 | ||
420 | return member_size; | |
421 | } | |
422 | ||
92de84a6 ILT |
423 | // An archive member iterator. |
424 | ||
425 | class Archive::const_iterator | |
426 | { | |
427 | public: | |
428 | // The header of an archive member. This is what this iterator | |
429 | // points to. | |
430 | struct Header | |
431 | { | |
432 | // The name of the member. | |
433 | std::string name; | |
434 | // The file offset of the member. | |
435 | off_t off; | |
436 | // The file offset of a nested archive member. | |
437 | off_t nested_off; | |
438 | // The size of the member. | |
439 | off_t size; | |
440 | }; | |
441 | ||
2a00e4fb | 442 | const_iterator(Archive* archive, off_t off) |
92de84a6 ILT |
443 | : archive_(archive), off_(off) |
444 | { this->read_next_header(); } | |
445 | ||
446 | const Header& | |
447 | operator*() const | |
448 | { return this->header_; } | |
449 | ||
450 | const Header* | |
451 | operator->() const | |
452 | { return &this->header_; } | |
453 | ||
454 | const_iterator& | |
455 | operator++() | |
456 | { | |
457 | if (this->off_ == this->archive_->file().filesize()) | |
458 | return *this; | |
459 | this->off_ += sizeof(Archive_header); | |
460 | if (!this->archive_->is_thin_archive()) | |
461 | this->off_ += this->header_.size; | |
462 | if ((this->off_ & 1) != 0) | |
463 | ++this->off_; | |
464 | this->read_next_header(); | |
465 | return *this; | |
466 | } | |
467 | ||
468 | const_iterator | |
469 | operator++(int) | |
470 | { | |
471 | const_iterator ret = *this; | |
472 | ++*this; | |
473 | return ret; | |
474 | } | |
475 | ||
476 | bool | |
477 | operator==(const const_iterator p) const | |
478 | { return this->off_ == p->off; } | |
479 | ||
480 | bool | |
481 | operator!=(const const_iterator p) const | |
482 | { return this->off_ != p->off; } | |
483 | ||
484 | private: | |
485 | void | |
486 | read_next_header(); | |
487 | ||
488 | // The underlying archive. | |
2a00e4fb | 489 | Archive* archive_; |
92de84a6 ILT |
490 | // The current offset in the file. |
491 | off_t off_; | |
492 | // The current archive header. | |
493 | Header header_; | |
494 | }; | |
495 | ||
496 | // Read the next archive header. | |
4973341a ILT |
497 | |
498 | void | |
92de84a6 | 499 | Archive::const_iterator::read_next_header() |
4973341a | 500 | { |
92de84a6 | 501 | off_t filesize = this->archive_->file().filesize(); |
4973341a ILT |
502 | while (true) |
503 | { | |
92de84a6 ILT |
504 | if (filesize - this->off_ < static_cast<off_t>(sizeof(Archive_header))) |
505 | { | |
506 | if (filesize != this->off_) | |
507 | { | |
508 | gold_error(_("%s: short archive header at %zu"), | |
509 | this->archive_->filename().c_str(), | |
510 | static_cast<size_t>(this->off_)); | |
511 | this->off_ = filesize; | |
512 | } | |
513 | this->header_.off = filesize; | |
514 | return; | |
515 | } | |
516 | ||
517 | unsigned char buf[sizeof(Archive_header)]; | |
518 | this->archive_->file().read(this->off_, sizeof(Archive_header), buf); | |
519 | ||
520 | const Archive_header* hdr = reinterpret_cast<const Archive_header*>(buf); | |
61ab3e40 ILT |
521 | off_t size = this->archive_->interpret_header(hdr, this->off_, |
522 | &this->header_.name, | |
523 | &this->header_.nested_off); | |
524 | if (size == -1) | |
525 | { | |
526 | this->header_.off = filesize; | |
527 | return; | |
528 | } | |
529 | ||
530 | this->header_.size = size; | |
92de84a6 ILT |
531 | this->header_.off = this->off_; |
532 | ||
533 | // Skip special members. | |
534 | if (!this->header_.name.empty() && this->header_.name != "/") | |
535 | return; | |
536 | ||
537 | this->off_ += sizeof(Archive_header) + this->header_.size; | |
538 | if ((this->off_ & 1) != 0) | |
539 | ++this->off_; | |
4973341a ILT |
540 | } |
541 | } | |
542 | ||
92de84a6 ILT |
543 | // Initial iterator. |
544 | ||
545 | Archive::const_iterator | |
2a00e4fb | 546 | Archive::begin() |
92de84a6 ILT |
547 | { |
548 | return Archive::const_iterator(this, sarmag); | |
549 | } | |
550 | ||
551 | // Final iterator. | |
552 | ||
553 | Archive::const_iterator | |
2a00e4fb | 554 | Archive::end() |
92de84a6 ILT |
555 | { |
556 | return Archive::const_iterator(this, this->input_file_->file().filesize()); | |
557 | } | |
558 | ||
ac45a351 CC |
559 | // Get the file and offset for an archive member, which may be an |
560 | // external member of a thin archive. Set *INPUT_FILE to the | |
561 | // file containing the actual member, *MEMOFF to the offset | |
562 | // within that file (0 if not a nested archive), and *MEMBER_NAME | |
563 | // to the name of the archive member. Return TRUE on success. | |
564 | ||
565 | bool | |
2ea97941 | 566 | Archive::get_file_and_offset(off_t off, Input_file** input_file, off_t* memoff, |
89fc3421 | 567 | off_t* memsize, std::string* member_name) |
ac45a351 CC |
568 | { |
569 | off_t nested_off; | |
570 | ||
89fc3421 | 571 | *memsize = this->read_header(off, false, member_name, &nested_off); |
61ab3e40 ILT |
572 | if (*memsize == -1) |
573 | return false; | |
ac45a351 | 574 | |
2ea97941 | 575 | *input_file = this->input_file_; |
ac45a351 CC |
576 | *memoff = off + static_cast<off_t>(sizeof(Archive_header)); |
577 | ||
578 | if (!this->is_thin_archive_) | |
579 | return true; | |
580 | ||
581 | // Adjust a relative pathname so that it is relative | |
582 | // to the directory containing the archive. | |
583 | if (!IS_ABSOLUTE_PATH(member_name->c_str())) | |
584 | { | |
fbd8a257 | 585 | const char* arch_path = this->filename().c_str(); |
ac45a351 CC |
586 | const char* basename = lbasename(arch_path); |
587 | if (basename > arch_path) | |
588 | member_name->replace(0, 0, | |
fbd8a257 | 589 | this->filename().substr(0, basename - arch_path)); |
ac45a351 CC |
590 | } |
591 | ||
592 | if (nested_off > 0) | |
593 | { | |
594 | // This is a member of a nested archive. Open the containing | |
595 | // archive if we don't already have it open, then do a recursive | |
596 | // call to include the member from that archive. | |
597 | Archive* arch; | |
598 | Nested_archive_table::const_iterator p = | |
599 | this->nested_archives_.find(*member_name); | |
600 | if (p != this->nested_archives_.end()) | |
601 | arch = p->second; | |
602 | else | |
603 | { | |
604 | Input_file_argument* input_file_arg = | |
ae3b5189 CD |
605 | new Input_file_argument(member_name->c_str(), |
606 | Input_file_argument::INPUT_FILE_TYPE_FILE, | |
607 | "", false, parameters->options()); | |
2ea97941 | 608 | *input_file = new Input_file(input_file_arg); |
15f8229b | 609 | int dummy = 0; |
2ea97941 | 610 | if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy)) |
ac45a351 | 611 | return false; |
2ea97941 | 612 | arch = new Archive(*member_name, *input_file, false, this->dirpath_, |
ac45a351 | 613 | this->task_); |
15f8229b | 614 | arch->setup(); |
ac45a351 CC |
615 | std::pair<Nested_archive_table::iterator, bool> ins = |
616 | this->nested_archives_.insert(std::make_pair(*member_name, arch)); | |
617 | gold_assert(ins.second); | |
618 | } | |
2ea97941 | 619 | return arch->get_file_and_offset(nested_off, input_file, memoff, |
15f8229b | 620 | memsize, member_name); |
ac45a351 CC |
621 | } |
622 | ||
623 | // This is an external member of a thin archive. Open the | |
624 | // file as a regular relocatable object file. | |
625 | Input_file_argument* input_file_arg = | |
ae3b5189 CD |
626 | new Input_file_argument(member_name->c_str(), |
627 | Input_file_argument::INPUT_FILE_TYPE_FILE, | |
628 | "", false, this->input_file_->options()); | |
2ea97941 | 629 | *input_file = new Input_file(input_file_arg); |
15f8229b | 630 | int dummy = 0; |
2ea97941 | 631 | if (!(*input_file)->open(*this->dirpath_, this->task_, &dummy)) |
ac45a351 CC |
632 | return false; |
633 | ||
634 | *memoff = 0; | |
2ea97941 | 635 | *memsize = (*input_file)->file().filesize(); |
ac45a351 CC |
636 | return true; |
637 | } | |
638 | ||
c20cbc06 ILT |
639 | // Return an ELF object for the member at offset OFF. If |
640 | // PUNCONFIGURED is not NULL, then if the ELF object has an | |
641 | // unsupported target type, set *PUNCONFIGURED to true and return | |
642 | // NULL. | |
ac45a351 CC |
643 | |
644 | Object* | |
15f8229b | 645 | Archive::get_elf_object_for_member(off_t off, bool* punconfigured) |
ac45a351 | 646 | { |
c20cbc06 ILT |
647 | if (punconfigured != NULL) |
648 | *punconfigured = false; | |
15f8229b | 649 | |
2ea97941 | 650 | Input_file* input_file; |
ac45a351 | 651 | off_t memoff; |
89fc3421 | 652 | off_t memsize; |
15f8229b | 653 | std::string member_name; |
2ea97941 | 654 | if (!this->get_file_and_offset(off, &input_file, &memoff, &memsize, |
15f8229b | 655 | &member_name)) |
ac45a351 CC |
656 | return NULL; |
657 | ||
9993ba11 ST |
658 | const unsigned char* ehdr; |
659 | int read_size; | |
660 | Object *obj = NULL; | |
661 | bool is_elf_obj = false; | |
662 | ||
663 | if (is_elf_object(input_file, memoff, &ehdr, &read_size)) | |
664 | { | |
665 | obj = make_elf_object((std::string(this->input_file_->filename()) | |
666 | + "(" + member_name + ")"), | |
667 | input_file, memoff, ehdr, read_size, | |
668 | punconfigured); | |
669 | is_elf_obj = true; | |
670 | } | |
671 | ||
89fc3421 CC |
672 | if (parameters->options().has_plugins()) |
673 | { | |
9993ba11 ST |
674 | Object* plugin_obj |
675 | = parameters->options().plugins()->claim_file(input_file, | |
676 | memoff, | |
677 | memsize, | |
678 | obj); | |
679 | if (plugin_obj != NULL) | |
89fc3421 CC |
680 | { |
681 | // The input file was claimed by a plugin, and its symbols | |
682 | // have been provided by the plugin. | |
9993ba11 ST |
683 | // Delete its elf object. |
684 | if (obj != NULL) | |
685 | delete obj; | |
686 | return plugin_obj; | |
89fc3421 CC |
687 | } |
688 | } | |
689 | ||
9993ba11 | 690 | if (!is_elf_obj) |
ac45a351 CC |
691 | { |
692 | gold_error(_("%s: member at %zu is not an ELF object"), | |
693 | this->name().c_str(), static_cast<size_t>(off)); | |
694 | return NULL; | |
695 | } | |
696 | ||
029ba973 ILT |
697 | if (obj == NULL) |
698 | return NULL; | |
65514900 CC |
699 | obj->set_no_export(this->no_export()); |
700 | return obj; | |
ac45a351 CC |
701 | } |
702 | ||
703 | // Read the symbols from all the archive members in the link. | |
704 | ||
705 | void | |
15f8229b | 706 | Archive::read_all_symbols() |
ac45a351 CC |
707 | { |
708 | for (Archive::const_iterator p = this->begin(); | |
709 | p != this->end(); | |
710 | ++p) | |
15f8229b | 711 | this->read_symbols(p->off); |
ac45a351 CC |
712 | } |
713 | ||
714 | // Read the symbols from an archive member in the link. OFF is the file | |
715 | // offset of the member header. | |
716 | ||
717 | void | |
15f8229b | 718 | Archive::read_symbols(off_t off) |
ac45a351 | 719 | { |
c20cbc06 | 720 | Object* obj = this->get_elf_object_for_member(off, NULL); |
ac45a351 CC |
721 | if (obj == NULL) |
722 | return; | |
723 | ||
724 | Read_symbols_data* sd = new Read_symbols_data; | |
725 | obj->read_symbols(sd); | |
726 | Archive_member member(obj, sd); | |
727 | this->members_[off] = member; | |
728 | } | |
729 | ||
730 | // Select members from the archive and add them to the link. We walk | |
731 | // through the elements in the archive map, and look each one up in | |
732 | // the symbol table. If it exists as a strong undefined symbol, we | |
733 | // pull in the corresponding element. We have to do this in a loop, | |
734 | // since pulling in one element may create new undefined symbols which | |
15f8229b ILT |
735 | // may be satisfied by other objects in the archive. Return true in |
736 | // the normal case, false if the first member we tried to add from | |
737 | // this archive had an incompatible target. | |
ac45a351 | 738 | |
15f8229b | 739 | bool |
ac45a351 CC |
740 | Archive::add_symbols(Symbol_table* symtab, Layout* layout, |
741 | Input_objects* input_objects, Mapfile* mapfile) | |
742 | { | |
743 | ++Archive::total_archives; | |
744 | ||
745 | if (this->input_file_->options().whole_archive()) | |
746 | return this->include_all_members(symtab, layout, input_objects, | |
747 | mapfile); | |
748 | ||
749 | Archive::total_members += this->num_members_; | |
750 | ||
751 | input_objects->archive_start(this); | |
752 | ||
753 | const size_t armap_size = this->armap_.size(); | |
754 | ||
755 | // This is a quick optimization, since we usually see many symbols | |
756 | // in a row with the same offset. last_seen_offset holds the last | |
757 | // offset we saw that was present in the seen_offsets_ set. | |
758 | off_t last_seen_offset = -1; | |
759 | ||
760 | // Track which symbols in the symbol table we've already found to be | |
761 | // defined. | |
762 | ||
9c5b8369 ILT |
763 | char* tmpbuf = NULL; |
764 | size_t tmpbuflen = 0; | |
ac45a351 CC |
765 | bool added_new_object; |
766 | do | |
767 | { | |
768 | added_new_object = false; | |
769 | for (size_t i = 0; i < armap_size; ++i) | |
770 | { | |
771 | if (this->armap_checked_[i]) | |
772 | continue; | |
773 | if (this->armap_[i].file_offset == last_seen_offset) | |
774 | { | |
775 | this->armap_checked_[i] = true; | |
776 | continue; | |
777 | } | |
778 | if (this->seen_offsets_.find(this->armap_[i].file_offset) | |
779 | != this->seen_offsets_.end()) | |
780 | { | |
781 | this->armap_checked_[i] = true; | |
782 | last_seen_offset = this->armap_[i].file_offset; | |
783 | continue; | |
784 | } | |
785 | ||
786 | const char* sym_name = (this->armap_names_.data() | |
787 | + this->armap_[i].name_offset); | |
9c5b8369 | 788 | |
473b7a13 RÁE |
789 | Symbol* sym; |
790 | std::string why; | |
b0193076 | 791 | Archive::Should_include t = |
88a4108b ILT |
792 | Archive::should_include_member(symtab, layout, sym_name, &sym, |
793 | &why, &tmpbuf, &tmpbuflen); | |
9c5b8369 | 794 | |
b0193076 RÁE |
795 | if (t == Archive::SHOULD_INCLUDE_NO |
796 | || t == Archive::SHOULD_INCLUDE_YES) | |
473b7a13 | 797 | this->armap_checked_[i] = true; |
9c5b8369 | 798 | |
b0193076 | 799 | if (t != Archive::SHOULD_INCLUDE_YES) |
ac45a351 CC |
800 | continue; |
801 | ||
802 | // We want to include this object in the link. | |
803 | last_seen_offset = this->armap_[i].file_offset; | |
804 | this->seen_offsets_.insert(last_seen_offset); | |
ac45a351 | 805 | |
15f8229b ILT |
806 | if (!this->include_member(symtab, layout, input_objects, |
807 | last_seen_offset, mapfile, sym, | |
808 | why.c_str())) | |
9c5b8369 ILT |
809 | { |
810 | if (tmpbuf != NULL) | |
811 | free(tmpbuf); | |
812 | return false; | |
813 | } | |
ac45a351 CC |
814 | |
815 | added_new_object = true; | |
816 | } | |
817 | } | |
818 | while (added_new_object); | |
819 | ||
9c5b8369 ILT |
820 | if (tmpbuf != NULL) |
821 | free(tmpbuf); | |
822 | ||
ac45a351 | 823 | input_objects->archive_stop(this); |
15f8229b ILT |
824 | |
825 | return true; | |
ac45a351 CC |
826 | } |
827 | ||
0f3b89d8 ILT |
828 | // Return whether the archive includes a member which defines the |
829 | // symbol SYM. | |
830 | ||
831 | bool | |
832 | Archive::defines_symbol(Symbol* sym) const | |
833 | { | |
834 | const char* symname = sym->name(); | |
835 | size_t symname_len = strlen(symname); | |
836 | size_t armap_size = this->armap_.size(); | |
837 | for (size_t i = 0; i < armap_size; ++i) | |
838 | { | |
839 | if (this->armap_checked_[i]) | |
840 | continue; | |
841 | const char* archive_symname = (this->armap_names_.data() | |
842 | + this->armap_[i].name_offset); | |
843 | if (strncmp(archive_symname, symname, symname_len) != 0) | |
844 | continue; | |
845 | char c = archive_symname[symname_len]; | |
846 | if (c == '\0' && sym->version() == NULL) | |
847 | return true; | |
848 | if (c == '@') | |
849 | { | |
850 | const char* ver = archive_symname + symname_len + 1; | |
851 | if (*ver == '@') | |
852 | { | |
853 | if (sym->version() == NULL) | |
854 | return true; | |
855 | ++ver; | |
856 | } | |
857 | if (sym->version() != NULL && strcmp(sym->version(), ver) == 0) | |
858 | return true; | |
859 | } | |
860 | } | |
861 | return false; | |
862 | } | |
863 | ||
92de84a6 ILT |
864 | // Include all the archive members in the link. This is for --whole-archive. |
865 | ||
15f8229b | 866 | bool |
92de84a6 ILT |
867 | Archive::include_all_members(Symbol_table* symtab, Layout* layout, |
868 | Input_objects* input_objects, Mapfile* mapfile) | |
869 | { | |
7cdb37d9 CC |
870 | // Don't include the same archive twice. This can happen if |
871 | // --whole-archive is nested inside --start-group (PR gold/12163). | |
872 | if (this->included_all_members_) | |
873 | return true; | |
874 | ||
875 | this->included_all_members_ = true; | |
876 | ||
92de84a6 ILT |
877 | input_objects->archive_start(this); |
878 | ||
ac45a351 CC |
879 | if (this->members_.size() > 0) |
880 | { | |
881 | std::map<off_t, Archive_member>::const_iterator p; | |
882 | for (p = this->members_.begin(); | |
883 | p != this->members_.end(); | |
884 | ++p) | |
885 | { | |
15f8229b ILT |
886 | if (!this->include_member(symtab, layout, input_objects, p->first, |
887 | mapfile, NULL, "--whole-archive")) | |
888 | return false; | |
ac45a351 CC |
889 | ++Archive::total_members; |
890 | } | |
891 | } | |
892 | else | |
893 | { | |
894 | for (Archive::const_iterator p = this->begin(); | |
895 | p != this->end(); | |
896 | ++p) | |
897 | { | |
15f8229b ILT |
898 | if (!this->include_member(symtab, layout, input_objects, p->off, |
899 | mapfile, NULL, "--whole-archive")) | |
900 | return false; | |
ac45a351 CC |
901 | ++Archive::total_members; |
902 | } | |
903 | } | |
92de84a6 ILT |
904 | |
905 | input_objects->archive_stop(this); | |
15f8229b ILT |
906 | |
907 | return true; | |
92de84a6 ILT |
908 | } |
909 | ||
910 | // Return the number of members in the archive. This is only used for | |
911 | // reports. | |
912 | ||
913 | size_t | |
2a00e4fb | 914 | Archive::count_members() |
92de84a6 ILT |
915 | { |
916 | size_t ret = 0; | |
917 | for (Archive::const_iterator p = this->begin(); | |
918 | p != this->end(); | |
919 | ++p) | |
920 | ++ret; | |
921 | return ret; | |
922 | } | |
923 | ||
61ba1cf9 | 924 | // Include an archive member in the link. OFF is the file offset of |
7d9e3d98 | 925 | // the member header. WHY is the reason we are including this member. |
15f8229b ILT |
926 | // Return true if we added the member or if we had an error, return |
927 | // false if this was the first member we tried to add from this | |
928 | // archive and it had an incompatible format. | |
61ba1cf9 | 929 | |
15f8229b | 930 | bool |
7e1edb90 | 931 | Archive::include_member(Symbol_table* symtab, Layout* layout, |
7d9e3d98 ILT |
932 | Input_objects* input_objects, off_t off, |
933 | Mapfile* mapfile, Symbol* sym, const char* why) | |
61ba1cf9 | 934 | { |
ac45a351 | 935 | ++Archive::total_members_loaded; |
7d9e3d98 | 936 | |
ac45a351 CC |
937 | std::map<off_t, Archive_member>::const_iterator p = this->members_.find(off); |
938 | if (p != this->members_.end()) | |
a1207466 | 939 | { |
ca09d69a | 940 | Object* obj = p->second.obj_; |
15f8229b | 941 | |
ca09d69a | 942 | Read_symbols_data* sd = p->second.sd_; |
ac45a351 CC |
943 | if (mapfile != NULL) |
944 | mapfile->report_include_archive_member(obj->name(), sym, why); | |
945 | if (input_objects->add_object(obj)) | |
a1207466 | 946 | { |
ac45a351 | 947 | obj->layout(symtab, layout, sd); |
f488e4b0 | 948 | obj->add_symbols(symtab, sd, layout); |
15f8229b | 949 | this->included_member_ = true; |
a1207466 | 950 | } |
ac45a351 | 951 | delete sd; |
15f8229b ILT |
952 | return true; |
953 | } | |
954 | ||
c20cbc06 ILT |
955 | // If this is the first object we are including from this archive, |
956 | // and we searched for this archive, most likely because it was | |
957 | // found via a -l option, then if the target is incompatible we want | |
958 | // to move on to the next archive found in the search path. | |
959 | bool unconfigured = false; | |
960 | bool* punconfigured = NULL; | |
961 | if (!this->included_member_ && this->searched_for()) | |
962 | punconfigured = &unconfigured; | |
61ba1cf9 | 963 | |
c20cbc06 | 964 | Object* obj = this->get_elf_object_for_member(off, punconfigured); |
ac45a351 | 965 | if (obj == NULL) |
c20cbc06 ILT |
966 | { |
967 | // Return false to search for another archive, true if we found | |
968 | // an error. | |
969 | return unconfigured ? false : true; | |
970 | } | |
61ba1cf9 | 971 | |
ac45a351 CC |
972 | if (mapfile != NULL) |
973 | mapfile->report_include_archive_member(obj->name(), sym, why); | |
61ba1cf9 | 974 | |
89fc3421 CC |
975 | Pluginobj* pluginobj = obj->pluginobj(); |
976 | if (pluginobj != NULL) | |
977 | { | |
f488e4b0 | 978 | pluginobj->add_symbols(symtab, NULL, layout); |
15f8229b ILT |
979 | this->included_member_ = true; |
980 | return true; | |
89fc3421 CC |
981 | } |
982 | ||
15f8229b | 983 | if (!input_objects->add_object(obj)) |
f2d707b5 ILT |
984 | { |
985 | // If this is an external member of a thin archive, unlock the | |
986 | // file. | |
987 | if (obj->offset() == 0) | |
988 | obj->unlock(this->task_); | |
989 | delete obj; | |
990 | } | |
15f8229b | 991 | else |
019cdb1a | 992 | { |
00698fc5 | 993 | { |
09ec0418 | 994 | if (layout->incremental_inputs() != NULL) |
cdc29364 | 995 | layout->incremental_inputs()->report_object(obj, 0, this, NULL); |
00698fc5 CC |
996 | Read_symbols_data sd; |
997 | obj->read_symbols(&sd); | |
998 | obj->layout(symtab, layout, &sd); | |
999 | obj->add_symbols(symtab, &sd, layout); | |
1000 | } | |
fbd8a257 CC |
1001 | |
1002 | // If this is an external member of a thin archive, unlock the file | |
1003 | // for the next task. | |
1004 | if (obj->offset() == 0) | |
1005 | obj->unlock(this->task_); | |
15f8229b ILT |
1006 | |
1007 | this->included_member_ = true; | |
019cdb1a | 1008 | } |
15f8229b ILT |
1009 | |
1010 | return true; | |
ac45a351 | 1011 | } |
61ba1cf9 | 1012 | |
e0c52780 CC |
1013 | // Iterate over all unused symbols, and call the visitor class V for each. |
1014 | ||
1015 | void | |
1016 | Archive::do_for_all_unused_symbols(Symbol_visitor_base* v) const | |
1017 | { | |
1018 | for (std::vector<Armap_entry>::const_iterator p = this->armap_.begin(); | |
1019 | p != this->armap_.end(); | |
1020 | ++p) | |
1021 | { | |
1022 | if (this->seen_offsets_.find(p->file_offset) | |
1023 | == this->seen_offsets_.end()) | |
1024 | v->visit(this->armap_names_.data() + p->name_offset); | |
1025 | } | |
1026 | } | |
1027 | ||
ac45a351 CC |
1028 | // Print statistical information to stderr. This is used for --stats. |
1029 | ||
1030 | void | |
1031 | Archive::print_stats() | |
1032 | { | |
1033 | fprintf(stderr, _("%s: archive libraries: %u\n"), | |
1034 | program_name, Archive::total_archives); | |
1035 | fprintf(stderr, _("%s: total archive members: %u\n"), | |
1036 | program_name, Archive::total_members); | |
1037 | fprintf(stderr, _("%s: loaded archive members: %u\n"), | |
1038 | program_name, Archive::total_members_loaded); | |
61ba1cf9 ILT |
1039 | } |
1040 | ||
1041 | // Add_archive_symbols methods. | |
1042 | ||
1043 | Add_archive_symbols::~Add_archive_symbols() | |
1044 | { | |
1045 | if (this->this_blocker_ != NULL) | |
1046 | delete this->this_blocker_; | |
1047 | // next_blocker_ is deleted by the task associated with the next | |
1048 | // input file. | |
1049 | } | |
1050 | ||
1051 | // Return whether we can add the archive symbols. We are blocked by | |
1052 | // this_blocker_. We block next_blocker_. We also lock the file. | |
1053 | ||
17a1d0a9 ILT |
1054 | Task_token* |
1055 | Add_archive_symbols::is_runnable() | |
61ba1cf9 ILT |
1056 | { |
1057 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
17a1d0a9 ILT |
1058 | return this->this_blocker_; |
1059 | return NULL; | |
61ba1cf9 ILT |
1060 | } |
1061 | ||
17a1d0a9 ILT |
1062 | void |
1063 | Add_archive_symbols::locks(Task_locker* tl) | |
61ba1cf9 | 1064 | { |
17a1d0a9 ILT |
1065 | tl->add(this, this->next_blocker_); |
1066 | tl->add(this, this->archive_->token()); | |
61ba1cf9 ILT |
1067 | } |
1068 | ||
1069 | void | |
15f8229b | 1070 | Add_archive_symbols::run(Workqueue* workqueue) |
61ba1cf9 | 1071 | { |
09ec0418 CC |
1072 | // For an incremental link, begin recording layout information. |
1073 | Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs(); | |
1074 | if (incremental_inputs != NULL) | |
cdc29364 CC |
1075 | { |
1076 | unsigned int arg_serial = this->input_argument_->file().arg_serial(); | |
1077 | Script_info* script_info = this->input_argument_->script_info(); | |
1078 | incremental_inputs->report_archive_begin(this->archive_, arg_serial, | |
1079 | script_info); | |
1080 | } | |
09ec0418 | 1081 | |
15f8229b ILT |
1082 | bool added = this->archive_->add_symbols(this->symtab_, this->layout_, |
1083 | this->input_objects_, | |
1084 | this->mapfile_); | |
a1207466 CC |
1085 | this->archive_->unlock_nested_archives(); |
1086 | ||
17a1d0a9 | 1087 | this->archive_->release(); |
39d0cb0e | 1088 | this->archive_->clear_uncached_views(); |
17a1d0a9 | 1089 | |
15f8229b ILT |
1090 | if (!added) |
1091 | { | |
1092 | // This archive holds object files which are incompatible with | |
1093 | // our output file. | |
1094 | Read_symbols::incompatible_warning(this->input_argument_, | |
1095 | this->archive_->input_file()); | |
1096 | Read_symbols::requeue(workqueue, this->input_objects_, this->symtab_, | |
1097 | this->layout_, this->dirpath_, this->dirindex_, | |
1098 | this->mapfile_, this->input_argument_, | |
1099 | this->input_group_, this->next_blocker_); | |
1100 | delete this->archive_; | |
1101 | return; | |
1102 | } | |
1103 | ||
ead1e424 ILT |
1104 | if (this->input_group_ != NULL) |
1105 | this->input_group_->add_archive(this->archive_); | |
1106 | else | |
1107 | { | |
09ec0418 | 1108 | // For an incremental link, finish recording the layout information. |
09ec0418 CC |
1109 | if (incremental_inputs != NULL) |
1110 | incremental_inputs->report_archive_end(this->archive_); | |
1111 | ||
0f3b89d8 ILT |
1112 | if (!parameters->options().has_plugins() |
1113 | || this->archive_->input_file()->options().whole_archive()) | |
1114 | { | |
1115 | // We no longer need to know about this archive. | |
1116 | delete this->archive_; | |
1117 | } | |
1118 | else | |
1119 | { | |
1120 | // The plugin interface may want to rescan this archive. | |
1121 | parameters->options().plugins()->save_archive(this->archive_); | |
1122 | } | |
1123 | ||
c7912668 | 1124 | this->archive_ = NULL; |
ead1e424 | 1125 | } |
61ba1cf9 ILT |
1126 | } |
1127 | ||
b0193076 RÁE |
1128 | // Class Lib_group static variables. |
1129 | unsigned int Lib_group::total_lib_groups; | |
1130 | unsigned int Lib_group::total_members; | |
1131 | unsigned int Lib_group::total_members_loaded; | |
1132 | ||
1133 | Lib_group::Lib_group(const Input_file_lib* lib, Task* task) | |
43819297 | 1134 | : Library_base(task), members_() |
b0193076 RÁE |
1135 | { |
1136 | this->members_.resize(lib->size()); | |
1137 | } | |
1138 | ||
e0c52780 CC |
1139 | const std::string& |
1140 | Lib_group::do_filename() const | |
1141 | { | |
cdc29364 | 1142 | std::string *filename = new std::string("/group/"); |
e0c52780 CC |
1143 | return *filename; |
1144 | } | |
1145 | ||
b0193076 | 1146 | // Select members from the lib group and add them to the link. We walk |
9b547ce6 | 1147 | // through the members, and check if each one up should be included. |
b0193076 RÁE |
1148 | // If the object says it should be included, we do so. We have to do |
1149 | // this in a loop, since including one member may create new undefined | |
1150 | // symbols which may be satisfied by other members. | |
1151 | ||
1152 | void | |
1153 | Lib_group::add_symbols(Symbol_table* symtab, Layout* layout, | |
1154 | Input_objects* input_objects) | |
1155 | { | |
1156 | ++Lib_group::total_lib_groups; | |
1157 | ||
1158 | Lib_group::total_members += this->members_.size(); | |
1159 | ||
1160 | bool added_new_object; | |
1161 | do | |
1162 | { | |
1163 | added_new_object = false; | |
1164 | unsigned int i = 0; | |
1165 | while (i < this->members_.size()) | |
1166 | { | |
1167 | const Archive_member& member = this->members_[i]; | |
ca09d69a | 1168 | Object* obj = member.obj_; |
b0193076 RÁE |
1169 | std::string why; |
1170 | ||
1171 | // Skip files with no symbols. Plugin objects have | |
1172 | // member.sd_ == NULL. | |
1173 | if (obj != NULL | |
1174 | && (member.sd_ == NULL || member.sd_->symbol_names != NULL)) | |
1175 | { | |
1176 | Archive::Should_include t = obj->should_include_member(symtab, | |
88a4108b | 1177 | layout, |
b0193076 RÁE |
1178 | member.sd_, |
1179 | &why); | |
1180 | ||
1181 | if (t != Archive::SHOULD_INCLUDE_YES) | |
1182 | { | |
1183 | ++i; | |
1184 | continue; | |
1185 | } | |
1186 | ||
1187 | this->include_member(symtab, layout, input_objects, member); | |
1188 | ||
1189 | added_new_object = true; | |
1190 | } | |
1191 | else | |
1192 | { | |
1193 | if (member.sd_ != NULL) | |
9919d93b CC |
1194 | { |
1195 | // The file must be locked in order to destroy the views | |
1196 | // associated with it. | |
1197 | gold_assert(obj != NULL); | |
1198 | obj->lock(this->task_); | |
1199 | delete member.sd_; | |
1200 | obj->unlock(this->task_); | |
1201 | } | |
b0193076 RÁE |
1202 | } |
1203 | ||
1204 | this->members_[i] = this->members_.back(); | |
1205 | this->members_.pop_back(); | |
1206 | } | |
1207 | } | |
1208 | while (added_new_object); | |
1209 | } | |
1210 | ||
1211 | // Include a lib group member in the link. | |
1212 | ||
1213 | void | |
1214 | Lib_group::include_member(Symbol_table* symtab, Layout* layout, | |
1215 | Input_objects* input_objects, | |
1216 | const Archive_member& member) | |
1217 | { | |
1218 | ++Lib_group::total_members_loaded; | |
1219 | ||
1220 | Object* obj = member.obj_; | |
1221 | gold_assert(obj != NULL); | |
1222 | ||
1223 | Pluginobj* pluginobj = obj->pluginobj(); | |
1224 | if (pluginobj != NULL) | |
1225 | { | |
1226 | pluginobj->add_symbols(symtab, NULL, layout); | |
1227 | return; | |
1228 | } | |
1229 | ||
1230 | Read_symbols_data* sd = member.sd_; | |
1231 | gold_assert(sd != NULL); | |
1232 | obj->lock(this->task_); | |
1233 | if (input_objects->add_object(obj)) | |
1234 | { | |
09ec0418 | 1235 | if (layout->incremental_inputs() != NULL) |
cdc29364 CC |
1236 | layout->incremental_inputs()->report_object(obj, member.arg_serial_, |
1237 | this, NULL); | |
b0193076 RÁE |
1238 | obj->layout(symtab, layout, sd); |
1239 | obj->add_symbols(symtab, sd, layout); | |
b0193076 RÁE |
1240 | } |
1241 | delete sd; | |
9919d93b CC |
1242 | // Unlock the file for the next task. |
1243 | obj->unlock(this->task_); | |
b0193076 RÁE |
1244 | } |
1245 | ||
e0c52780 CC |
1246 | // Iterate over all unused symbols, and call the visitor class V for each. |
1247 | ||
1248 | void | |
1249 | Lib_group::do_for_all_unused_symbols(Symbol_visitor_base* v) const | |
1250 | { | |
1251 | // Files are removed from the members list when used, so all the | |
1252 | // files remaining on the list are unused. | |
1253 | for (std::vector<Archive_member>::const_iterator p = this->members_.begin(); | |
1254 | p != this->members_.end(); | |
1255 | ++p) | |
1256 | { | |
1257 | Object* obj = p->obj_; | |
1258 | obj->for_all_global_symbols(p->sd_, v); | |
1259 | } | |
1260 | } | |
1261 | ||
b0193076 RÁE |
1262 | // Print statistical information to stderr. This is used for --stats. |
1263 | ||
1264 | void | |
1265 | Lib_group::print_stats() | |
1266 | { | |
1267 | fprintf(stderr, _("%s: lib groups: %u\n"), | |
1268 | program_name, Lib_group::total_lib_groups); | |
1269 | fprintf(stderr, _("%s: total lib groups members: %u\n"), | |
1270 | program_name, Lib_group::total_members); | |
1271 | fprintf(stderr, _("%s: loaded lib groups members: %u\n"), | |
1272 | program_name, Lib_group::total_members_loaded); | |
1273 | } | |
1274 | ||
1275 | Task_token* | |
1276 | Add_lib_group_symbols::is_runnable() | |
1277 | { | |
97b4be1c CC |
1278 | if (this->readsyms_blocker_ != NULL && this->readsyms_blocker_->is_blocked()) |
1279 | return this->readsyms_blocker_; | |
b0193076 RÁE |
1280 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) |
1281 | return this->this_blocker_; | |
1282 | return NULL; | |
1283 | } | |
1284 | ||
1285 | void | |
1286 | Add_lib_group_symbols::locks(Task_locker* tl) | |
1287 | { | |
1288 | tl->add(this, this->next_blocker_); | |
1289 | } | |
1290 | ||
1291 | void | |
1292 | Add_lib_group_symbols::run(Workqueue*) | |
1293 | { | |
e0c52780 CC |
1294 | // For an incremental link, begin recording layout information. |
1295 | Incremental_inputs* incremental_inputs = this->layout_->incremental_inputs(); | |
1296 | if (incremental_inputs != NULL) | |
cdc29364 | 1297 | incremental_inputs->report_archive_begin(this->lib_, 0, NULL); |
e0c52780 | 1298 | |
b0193076 | 1299 | this->lib_->add_symbols(this->symtab_, this->layout_, this->input_objects_); |
09ec0418 | 1300 | |
e0c52780 CC |
1301 | if (incremental_inputs != NULL) |
1302 | incremental_inputs->report_archive_end(this->lib_); | |
b0193076 RÁE |
1303 | } |
1304 | ||
1305 | Add_lib_group_symbols::~Add_lib_group_symbols() | |
1306 | { | |
1307 | if (this->this_blocker_ != NULL) | |
1308 | delete this->this_blocker_; | |
1309 | // next_blocker_ is deleted by the task associated with the next | |
1310 | // input file. | |
1311 | } | |
1312 | ||
61ba1cf9 | 1313 | } // End namespace gold. |