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