Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // archive.cc -- archive support for gold |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
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> | |
29 | ||
30 | #include "elfcpp.h" | |
7e1edb90 | 31 | #include "options.h" |
61ba1cf9 | 32 | #include "fileread.h" |
ead1e424 | 33 | #include "readsyms.h" |
61ba1cf9 ILT |
34 | #include "symtab.h" |
35 | #include "object.h" | |
36 | #include "archive.h" | |
37 | ||
38 | namespace gold | |
39 | { | |
40 | ||
41 | // The header of an entry in the archive. This is all readable text, | |
42 | // padded with spaces where necesary. If the contents of an archive | |
43 | // are all text file, the entire archive is readable. | |
44 | ||
45 | struct Archive::Archive_header | |
46 | { | |
47 | // The entry name. | |
48 | char ar_name[16]; | |
49 | // The file modification time. | |
50 | char ar_date[12]; | |
51 | // The user's UID in decimal. | |
52 | char ar_uid[6]; | |
53 | // The user's GID in decimal. | |
54 | char ar_gid[6]; | |
55 | // The file mode in octal. | |
56 | char ar_mode[8]; | |
57 | // The file size in decimal. | |
58 | char ar_size[10]; | |
59 | // The final magic code. | |
60 | char ar_fmag[2]; | |
61 | }; | |
62 | ||
63 | // Archive methods. | |
64 | ||
65 | const char Archive::armag[sarmag] = | |
66 | { | |
67 | '!', '<', 'a', 'r', 'c', 'h', '>', '\n' | |
68 | }; | |
69 | ||
70 | const char Archive::arfmag[2] = { '`', '\n' }; | |
71 | ||
61ba1cf9 ILT |
72 | // Set up the archive: read the symbol map and the extended name |
73 | // table. | |
74 | ||
75 | void | |
17a1d0a9 | 76 | Archive::setup(Task* task) |
61ba1cf9 | 77 | { |
3e95a404 ILT |
78 | // We need to ignore empty archives. |
79 | if (this->input_file_->file().filesize() == sarmag) | |
80 | { | |
17a1d0a9 | 81 | this->input_file_->file().unlock(task); |
3e95a404 ILT |
82 | return; |
83 | } | |
84 | ||
61ba1cf9 ILT |
85 | // The first member of the archive should be the symbol table. |
86 | std::string armap_name; | |
8383303e | 87 | section_size_type armap_size = |
cb295612 ILT |
88 | convert_to_section_size_type(this->read_header(sarmag, false, |
89 | &armap_name)); | |
75f2446e | 90 | off_t off = sarmag; |
4973341a ILT |
91 | if (armap_name.empty()) |
92 | { | |
93 | this->read_armap(sarmag + sizeof(Archive_header), armap_size); | |
94 | off = sarmag + sizeof(Archive_header) + armap_size; | |
95 | } | |
96 | else if (!this->input_file_->options().include_whole_archive()) | |
75f2446e ILT |
97 | gold_error(_("%s: no archive symbol table (run ranlib)"), |
98 | this->name().c_str()); | |
4973341a | 99 | |
cb295612 ILT |
100 | // See if there is an extended name table. We cache these views |
101 | // because it is likely that we will want to read the following | |
102 | // header in the add_symbols routine. | |
4973341a ILT |
103 | if ((off & 1) != 0) |
104 | ++off; | |
105 | std::string xname; | |
cb295612 ILT |
106 | section_size_type extended_size = |
107 | convert_to_section_size_type(this->read_header(off, true, &xname)); | |
4973341a ILT |
108 | if (xname == "/") |
109 | { | |
110 | const unsigned char* p = this->get_view(off + sizeof(Archive_header), | |
cb295612 | 111 | extended_size, true); |
4973341a ILT |
112 | const char* px = reinterpret_cast<const char*>(p); |
113 | this->extended_names_.assign(px, extended_size); | |
114 | } | |
115 | ||
116 | // Opening the file locked it. Unlock it now. | |
17a1d0a9 | 117 | this->input_file_->file().unlock(task); |
4973341a | 118 | } |
61ba1cf9 | 119 | |
4973341a ILT |
120 | // Read the archive symbol map. |
121 | ||
122 | void | |
8383303e | 123 | Archive::read_armap(off_t start, section_size_type size) |
4973341a | 124 | { |
61ba1cf9 | 125 | // Read in the entire armap. |
9eb9fa57 | 126 | const unsigned char* p = this->get_view(start, size, false); |
61ba1cf9 ILT |
127 | |
128 | // Numbers in the armap are always big-endian. | |
129 | const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p); | |
f6ce93d6 | 130 | unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword); |
61ba1cf9 ILT |
131 | ++pword; |
132 | ||
133 | // Note that the addition is in units of sizeof(elfcpp::Elf_Word). | |
134 | const char* pnames = reinterpret_cast<const char*>(pword + nsyms); | |
8383303e ILT |
135 | section_size_type names_size = |
136 | reinterpret_cast<const char*>(p) + size - pnames; | |
9eb9fa57 | 137 | this->armap_names_.assign(pnames, names_size); |
61ba1cf9 ILT |
138 | |
139 | this->armap_.resize(nsyms); | |
140 | ||
8383303e | 141 | section_offset_type name_offset = 0; |
61ba1cf9 ILT |
142 | for (unsigned int i = 0; i < nsyms; ++i) |
143 | { | |
9eb9fa57 ILT |
144 | this->armap_[i].name_offset = name_offset; |
145 | this->armap_[i].file_offset = elfcpp::Swap<32, true>::readval(pword); | |
146 | name_offset += strlen(pnames + name_offset) + 1; | |
61ba1cf9 ILT |
147 | ++pword; |
148 | } | |
149 | ||
8383303e | 150 | if (static_cast<section_size_type>(name_offset) > names_size) |
75f2446e ILT |
151 | gold_error(_("%s: bad archive symbol table names"), |
152 | this->name().c_str()); | |
a93d6d07 ILT |
153 | |
154 | // This array keeps track of which symbols are for archive elements | |
155 | // which we have already included in the link. | |
156 | this->armap_checked_.resize(nsyms); | |
61ba1cf9 ILT |
157 | } |
158 | ||
159 | // Read the header of an archive member at OFF. Fail if something | |
160 | // goes wrong. Return the size of the member. Set *PNAME to the name | |
161 | // of the member. | |
162 | ||
163 | off_t | |
cb295612 | 164 | Archive::read_header(off_t off, bool cache, std::string* pname) |
61ba1cf9 | 165 | { |
cb295612 | 166 | const unsigned char* p = this->get_view(off, sizeof(Archive_header), cache); |
61ba1cf9 | 167 | const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p); |
4973341a ILT |
168 | return this->interpret_header(hdr, off, pname); |
169 | } | |
61ba1cf9 | 170 | |
4973341a ILT |
171 | // Interpret the header of HDR, the header of the archive member at |
172 | // file offset OFF. Fail if something goes wrong. Return the size of | |
173 | // the member. Set *PNAME to the name of the member. | |
174 | ||
175 | off_t | |
176 | Archive::interpret_header(const Archive_header* hdr, off_t off, | |
177 | std::string* pname) | |
178 | { | |
61ba1cf9 ILT |
179 | if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0) |
180 | { | |
75f2446e ILT |
181 | gold_error(_("%s: malformed archive header at %zu"), |
182 | this->name().c_str(), static_cast<size_t>(off)); | |
183 | return this->input_file_->file().filesize() - off; | |
61ba1cf9 ILT |
184 | } |
185 | ||
186 | const int size_string_size = sizeof hdr->ar_size; | |
187 | char size_string[size_string_size + 1]; | |
188 | memcpy(size_string, hdr->ar_size, size_string_size); | |
189 | char* ps = size_string + size_string_size; | |
190 | while (ps[-1] == ' ') | |
191 | --ps; | |
192 | *ps = '\0'; | |
193 | ||
194 | errno = 0; | |
195 | char* end; | |
196 | off_t member_size = strtol(size_string, &end, 10); | |
197 | if (*end != '\0' | |
198 | || member_size < 0 | |
199 | || (member_size == LONG_MAX && errno == ERANGE)) | |
200 | { | |
75f2446e ILT |
201 | gold_error(_("%s: malformed archive header size at %zu"), |
202 | this->name().c_str(), static_cast<size_t>(off)); | |
203 | return this->input_file_->file().filesize() - off; | |
61ba1cf9 ILT |
204 | } |
205 | ||
206 | if (hdr->ar_name[0] != '/') | |
207 | { | |
208 | const char* name_end = strchr(hdr->ar_name, '/'); | |
209 | if (name_end == NULL | |
210 | || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name)) | |
211 | { | |
a0c4fb0a | 212 | gold_error(_("%s: malformed archive header name at %zu"), |
75f2446e ILT |
213 | this->name().c_str(), static_cast<size_t>(off)); |
214 | return this->input_file_->file().filesize() - off; | |
61ba1cf9 ILT |
215 | } |
216 | pname->assign(hdr->ar_name, name_end - hdr->ar_name); | |
217 | } | |
218 | else if (hdr->ar_name[1] == ' ') | |
219 | { | |
220 | // This is the symbol table. | |
221 | pname->clear(); | |
222 | } | |
223 | else if (hdr->ar_name[1] == '/') | |
224 | { | |
225 | // This is the extended name table. | |
226 | pname->assign(1, '/'); | |
227 | } | |
228 | else | |
229 | { | |
230 | errno = 0; | |
231 | long x = strtol(hdr->ar_name + 1, &end, 10); | |
232 | if (*end != ' ' | |
233 | || x < 0 | |
234 | || (x == LONG_MAX && errno == ERANGE) | |
235 | || static_cast<size_t>(x) >= this->extended_names_.size()) | |
236 | { | |
75f2446e ILT |
237 | gold_error(_("%s: bad extended name index at %zu"), |
238 | this->name().c_str(), static_cast<size_t>(off)); | |
239 | return this->input_file_->file().filesize() - off; | |
61ba1cf9 ILT |
240 | } |
241 | ||
242 | const char* name = this->extended_names_.data() + x; | |
243 | const char* name_end = strchr(name, '/'); | |
244 | if (static_cast<size_t>(name_end - name) > this->extended_names_.size() | |
245 | || name_end[1] != '\n') | |
246 | { | |
75f2446e ILT |
247 | gold_error(_("%s: bad extended name entry at header %zu"), |
248 | this->name().c_str(), static_cast<size_t>(off)); | |
249 | return this->input_file_->file().filesize() - off; | |
61ba1cf9 ILT |
250 | } |
251 | pname->assign(name, name_end - name); | |
252 | } | |
253 | ||
254 | return member_size; | |
255 | } | |
256 | ||
257 | // Select members from the archive and add them to the link. We walk | |
258 | // through the elements in the archive map, and look each one up in | |
259 | // the symbol table. If it exists as a strong undefined symbol, we | |
260 | // pull in the corresponding element. We have to do this in a loop, | |
261 | // since pulling in one element may create new undefined symbols which | |
262 | // may be satisfied by other objects in the archive. | |
263 | ||
264 | void | |
7e1edb90 ILT |
265 | Archive::add_symbols(Symbol_table* symtab, Layout* layout, |
266 | Input_objects* input_objects) | |
61ba1cf9 | 267 | { |
4973341a | 268 | if (this->input_file_->options().include_whole_archive()) |
7e1edb90 | 269 | return this->include_all_members(symtab, layout, input_objects); |
4973341a | 270 | |
ead1e424 | 271 | const size_t armap_size = this->armap_.size(); |
61ba1cf9 | 272 | |
e243ffc6 | 273 | // This is a quick optimization, since we usually see many symbols |
8c838dbd ILT |
274 | // in a row with the same offset. last_seen_offset holds the last |
275 | // offset we saw that was present in the seen_offsets_ set. | |
a93d6d07 ILT |
276 | off_t last_seen_offset = -1; |
277 | ||
278 | // Track which symbols in the symbol table we've already found to be | |
279 | // defined. | |
e243ffc6 | 280 | |
61ba1cf9 ILT |
281 | bool added_new_object; |
282 | do | |
283 | { | |
284 | added_new_object = false; | |
61ba1cf9 ILT |
285 | for (size_t i = 0; i < armap_size; ++i) |
286 | { | |
a93d6d07 ILT |
287 | if (this->armap_checked_[i]) |
288 | continue; | |
9eb9fa57 | 289 | if (this->armap_[i].file_offset == last_seen_offset) |
a93d6d07 ILT |
290 | { |
291 | this->armap_checked_[i] = true; | |
292 | continue; | |
293 | } | |
9eb9fa57 | 294 | if (this->seen_offsets_.find(this->armap_[i].file_offset) |
a93d6d07 | 295 | != this->seen_offsets_.end()) |
61ba1cf9 | 296 | { |
a93d6d07 | 297 | this->armap_checked_[i] = true; |
9eb9fa57 | 298 | last_seen_offset = this->armap_[i].file_offset; |
61ba1cf9 ILT |
299 | continue; |
300 | } | |
301 | ||
9eb9fa57 ILT |
302 | const char* sym_name = (this->armap_names_.data() |
303 | + this->armap_[i].name_offset); | |
304 | Symbol* sym = symtab->lookup(sym_name); | |
61ba1cf9 ILT |
305 | if (sym == NULL) |
306 | continue; | |
ead1e424 | 307 | else if (!sym->is_undefined()) |
61ba1cf9 | 308 | { |
a93d6d07 | 309 | this->armap_checked_[i] = true; |
61ba1cf9 ILT |
310 | continue; |
311 | } | |
312 | else if (sym->binding() == elfcpp::STB_WEAK) | |
313 | continue; | |
314 | ||
315 | // We want to include this object in the link. | |
9eb9fa57 | 316 | last_seen_offset = this->armap_[i].file_offset; |
a93d6d07 ILT |
317 | this->seen_offsets_.insert(last_seen_offset); |
318 | this->armap_checked_[i] = true; | |
7e1edb90 | 319 | this->include_member(symtab, layout, input_objects, |
a93d6d07 | 320 | last_seen_offset); |
61ba1cf9 ILT |
321 | added_new_object = true; |
322 | } | |
323 | } | |
324 | while (added_new_object); | |
325 | } | |
326 | ||
4973341a ILT |
327 | // Include all the archive members in the link. This is for --whole-archive. |
328 | ||
329 | void | |
7e1edb90 | 330 | Archive::include_all_members(Symbol_table* symtab, Layout* layout, |
4973341a ILT |
331 | Input_objects* input_objects) |
332 | { | |
333 | off_t off = sarmag; | |
82dcae9d | 334 | off_t filesize = this->input_file_->file().filesize(); |
4973341a ILT |
335 | while (true) |
336 | { | |
f5c3f225 | 337 | if (filesize - off < static_cast<off_t>(sizeof(Archive_header))) |
4973341a | 338 | { |
82dcae9d | 339 | if (filesize != off) |
75f2446e ILT |
340 | gold_error(_("%s: short archive header at %zu"), |
341 | this->name().c_str(), static_cast<size_t>(off)); | |
4973341a ILT |
342 | break; |
343 | } | |
344 | ||
82dcae9d ILT |
345 | unsigned char hdr_buf[sizeof(Archive_header)]; |
346 | this->input_file_->file().read(off, sizeof(Archive_header), hdr_buf); | |
347 | ||
bae3688d ILT |
348 | const Archive_header* hdr = |
349 | reinterpret_cast<const Archive_header*>(hdr_buf); | |
4973341a ILT |
350 | std::string name; |
351 | off_t size = this->interpret_header(hdr, off, &name); | |
352 | if (name.empty()) | |
353 | { | |
354 | // Symbol table. | |
355 | } | |
356 | else if (name == "/") | |
357 | { | |
358 | // Extended name table. | |
359 | } | |
360 | else | |
7e1edb90 | 361 | this->include_member(symtab, layout, input_objects, off); |
4973341a ILT |
362 | |
363 | off += sizeof(Archive_header) + size; | |
364 | if ((off & 1) != 0) | |
365 | ++off; | |
366 | } | |
367 | } | |
368 | ||
61ba1cf9 ILT |
369 | // Include an archive member in the link. OFF is the file offset of |
370 | // the member header. | |
371 | ||
372 | void | |
7e1edb90 ILT |
373 | Archive::include_member(Symbol_table* symtab, Layout* layout, |
374 | Input_objects* input_objects, off_t off) | |
61ba1cf9 ILT |
375 | { |
376 | std::string n; | |
cb295612 | 377 | this->read_header(off, false, &n); |
61ba1cf9 | 378 | |
f5c3f225 | 379 | const off_t memoff = off + static_cast<off_t>(sizeof(Archive_header)); |
61ba1cf9 ILT |
380 | |
381 | // Read enough of the file to pick up the entire ELF header. | |
82dcae9d ILT |
382 | unsigned char ehdr_buf[elfcpp::Elf_sizes<64>::ehdr_size]; |
383 | ||
384 | off_t filesize = this->input_file_->file().filesize(); | |
385 | int read_size = elfcpp::Elf_sizes<64>::ehdr_size; | |
386 | if (filesize - memoff < read_size) | |
387 | read_size = filesize - memoff; | |
388 | ||
389 | if (read_size < 4) | |
61ba1cf9 | 390 | { |
75f2446e ILT |
391 | gold_error(_("%s: member at %zu is not an ELF object"), |
392 | this->name().c_str(), static_cast<size_t>(off)); | |
393 | return; | |
61ba1cf9 ILT |
394 | } |
395 | ||
82dcae9d ILT |
396 | this->input_file_->file().read(memoff, read_size, ehdr_buf); |
397 | ||
61ba1cf9 ILT |
398 | static unsigned char elfmagic[4] = |
399 | { | |
400 | elfcpp::ELFMAG0, elfcpp::ELFMAG1, | |
401 | elfcpp::ELFMAG2, elfcpp::ELFMAG3 | |
402 | }; | |
bae3688d | 403 | if (memcmp(ehdr_buf, elfmagic, 4) != 0) |
61ba1cf9 | 404 | { |
75f2446e ILT |
405 | gold_error(_("%s: member at %zu is not an ELF object"), |
406 | this->name().c_str(), static_cast<size_t>(off)); | |
407 | return; | |
61ba1cf9 ILT |
408 | } |
409 | ||
92e059d8 | 410 | Object* obj = make_elf_object((std::string(this->input_file_->filename()) |
61ba1cf9 | 411 | + "(" + n + ")"), |
82dcae9d ILT |
412 | this->input_file_, memoff, ehdr_buf, |
413 | read_size); | |
61ba1cf9 | 414 | |
019cdb1a ILT |
415 | if (input_objects->add_object(obj)) |
416 | { | |
417 | Read_symbols_data sd; | |
418 | obj->read_symbols(&sd); | |
419 | obj->layout(symtab, layout, &sd); | |
420 | obj->add_symbols(symtab, &sd); | |
421 | } | |
422 | else | |
423 | { | |
424 | // FIXME: We need to close the descriptor here. | |
425 | delete obj; | |
426 | } | |
61ba1cf9 | 427 | |
61ba1cf9 ILT |
428 | } |
429 | ||
430 | // Add_archive_symbols methods. | |
431 | ||
432 | Add_archive_symbols::~Add_archive_symbols() | |
433 | { | |
434 | if (this->this_blocker_ != NULL) | |
435 | delete this->this_blocker_; | |
436 | // next_blocker_ is deleted by the task associated with the next | |
437 | // input file. | |
438 | } | |
439 | ||
440 | // Return whether we can add the archive symbols. We are blocked by | |
441 | // this_blocker_. We block next_blocker_. We also lock the file. | |
442 | ||
17a1d0a9 ILT |
443 | Task_token* |
444 | Add_archive_symbols::is_runnable() | |
61ba1cf9 ILT |
445 | { |
446 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
17a1d0a9 ILT |
447 | return this->this_blocker_; |
448 | return NULL; | |
61ba1cf9 ILT |
449 | } |
450 | ||
17a1d0a9 ILT |
451 | void |
452 | Add_archive_symbols::locks(Task_locker* tl) | |
61ba1cf9 | 453 | { |
17a1d0a9 ILT |
454 | tl->add(this, this->next_blocker_); |
455 | tl->add(this, this->archive_->token()); | |
61ba1cf9 ILT |
456 | } |
457 | ||
458 | void | |
459 | Add_archive_symbols::run(Workqueue*) | |
460 | { | |
7e1edb90 ILT |
461 | this->archive_->add_symbols(this->symtab_, this->layout_, |
462 | this->input_objects_); | |
ead1e424 | 463 | |
17a1d0a9 ILT |
464 | this->archive_->release(); |
465 | ||
ead1e424 ILT |
466 | if (this->input_group_ != NULL) |
467 | this->input_group_->add_archive(this->archive_); | |
468 | else | |
469 | { | |
470 | // We no longer need to know about this archive. | |
471 | delete this->archive_; | |
c7912668 | 472 | this->archive_ = NULL; |
ead1e424 | 473 | } |
61ba1cf9 ILT |
474 | } |
475 | ||
476 | } // End namespace gold. |