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