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