Break out default pbytes argument to read and get_view routines,
[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 while (true)
330 {
331 off_t bytes;
332 const unsigned char* p = this->get_view_and_size(off,
333 sizeof(Archive_header),
334 &bytes);
335 if (bytes < sizeof(Archive_header))
336 {
337 if (bytes != 0)
338 {
339 fprintf(stderr, _("%s: %s: short archive header at %ld\n"),
340 program_name, this->name().c_str(),
341 static_cast<long>(off));
342 gold_exit(false);
343 }
344
345 break;
346 }
347
348 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
349 std::string name;
350 off_t size = this->interpret_header(hdr, off, &name);
351 if (name.empty())
352 {
353 // Symbol table.
354 }
355 else if (name == "/")
356 {
357 // Extended name table.
358 }
359 else
360 this->include_member(symtab, layout, input_objects, off);
361
362 off += sizeof(Archive_header) + size;
363 if ((off & 1) != 0)
364 ++off;
365 }
366 }
367
368 // Include an archive member in the link. OFF is the file offset of
369 // the member header.
370
371 void
372 Archive::include_member(Symbol_table* symtab, Layout* layout,
373 Input_objects* input_objects, off_t off)
374 {
375 std::string n;
376 this->read_header(off, &n);
377
378 size_t memoff = off + sizeof(Archive_header);
379
380 // Read enough of the file to pick up the entire ELF header.
381 int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
382 off_t bytes;
383 const unsigned char* p =
384 this->input_file_->file().get_view_and_size(memoff, ehdr_size, &bytes);
385 if (bytes < 4)
386 {
387 fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"),
388 program_name, this->name().c_str(),
389 static_cast<long>(off));
390 gold_exit(false);
391 }
392
393 static unsigned char elfmagic[4] =
394 {
395 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
396 elfcpp::ELFMAG2, elfcpp::ELFMAG3
397 };
398 if (memcmp(p, elfmagic, 4) != 0)
399 {
400 fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"),
401 program_name, this->name().c_str(),
402 static_cast<long>(off));
403 gold_exit(false);
404 }
405
406 Object* obj = make_elf_object((std::string(this->input_file_->filename())
407 + "(" + n + ")"),
408 this->input_file_, memoff, p, bytes);
409
410 input_objects->add_object(obj);
411
412 Read_symbols_data sd;
413 obj->read_symbols(&sd);
414 obj->layout(symtab, layout, &sd);
415 obj->add_symbols(symtab, &sd);
416 }
417
418 // Add_archive_symbols methods.
419
420 Add_archive_symbols::~Add_archive_symbols()
421 {
422 if (this->this_blocker_ != NULL)
423 delete this->this_blocker_;
424 // next_blocker_ is deleted by the task associated with the next
425 // input file.
426 }
427
428 // Return whether we can add the archive symbols. We are blocked by
429 // this_blocker_. We block next_blocker_. We also lock the file.
430
431 Task::Is_runnable_type
432 Add_archive_symbols::is_runnable(Workqueue*)
433 {
434 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
435 return IS_BLOCKED;
436 return IS_RUNNABLE;
437 }
438
439 class Add_archive_symbols::Add_archive_symbols_locker : public Task_locker
440 {
441 public:
442 Add_archive_symbols_locker(Task_token& token, Workqueue* workqueue,
443 File_read& file)
444 : blocker_(token, workqueue), filelock_(file)
445 { }
446
447 private:
448 Task_locker_block blocker_;
449 Task_locker_obj<File_read> filelock_;
450 };
451
452 Task_locker*
453 Add_archive_symbols::locks(Workqueue* workqueue)
454 {
455 return new Add_archive_symbols_locker(*this->next_blocker_,
456 workqueue,
457 this->archive_->file());
458 }
459
460 void
461 Add_archive_symbols::run(Workqueue*)
462 {
463 this->archive_->add_symbols(this->symtab_, this->layout_,
464 this->input_objects_);
465
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_;
472 }
473 }
474
475 } // End namespace gold.
This page took 0.053693 seconds and 5 git commands to generate.