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