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