Add licensing text to every source file.
[binutils.git] / gold / archive.cc
blobef6ff749b052bad44d494d5f08995215d1ee40b3
1 // archive.cc -- archive support for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <cerrno>
26 #include <cstring>
27 #include <climits>
28 #include <vector>
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"
38 namespace gold
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.
45 struct Archive::Archive_header
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];
63 // Archive methods.
65 const char Archive::armag[sarmag] =
67 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
70 const char Archive::arfmag[2] = { '`', '\n' };
72 // Set up the archive: read the symbol map and the extended name
73 // table.
75 void
76 Archive::setup()
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())
84 this->read_armap(sarmag + sizeof(Archive_header), armap_size);
85 off = sarmag + sizeof(Archive_header) + armap_size;
87 else if (!this->input_file_->options().include_whole_archive())
89 fprintf(stderr, _("%s: %s: no archive symbol table (run ranlib)\n"),
90 program_name, this->name().c_str());
91 gold_exit(false);
93 else
94 off = sarmag;
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 == "/")
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);
109 // Opening the file locked it. Unlock it now.
110 this->input_file_->file().unlock();
113 // Read the archive symbol map.
115 void
116 Archive::read_armap(off_t start, off_t size)
118 // Read in the entire armap.
119 const unsigned char* p = this->get_view(start, size);
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;
126 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
127 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
129 this->armap_.resize(nsyms);
131 for (unsigned int i = 0; i < nsyms; ++i)
133 this->armap_[i].name = pnames;
134 this->armap_[i].offset = elfcpp::Swap<32, true>::readval(pword);
135 pnames += strlen(pnames) + 1;
136 ++pword;
139 if (reinterpret_cast<const unsigned char*>(pnames) - p > size)
141 fprintf(stderr, _("%s: %s: bad archive symbol table names\n"),
142 program_name, this->name().c_str());
143 gold_exit(false);
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);
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.
155 off_t
156 Archive::read_header(off_t off, std::string* pname)
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);
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.
167 off_t
168 Archive::interpret_header(const Archive_header* hdr, off_t off,
169 std::string* pname)
171 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
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);
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';
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))
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);
200 if (hdr->ar_name[0] != '/')
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))
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);
211 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
213 else if (hdr->ar_name[1] == ' ')
215 // This is the symbol table.
216 pname->clear();
218 else if (hdr->ar_name[1] == '/')
220 // This is the extended name table.
221 pname->assign(1, '/');
223 else
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())
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);
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')
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);
248 pname->assign(name, name_end - name);
251 return member_size;
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.
261 void
262 Archive::add_symbols(Symbol_table* symtab, Layout* layout,
263 Input_objects* input_objects)
265 if (this->input_file_->options().include_whole_archive())
266 return this->include_all_members(symtab, layout, input_objects);
268 const size_t armap_size = this->armap_.size();
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;
275 // Track which symbols in the symbol table we've already found to be
276 // defined.
278 bool added_new_object;
281 added_new_object = false;
282 for (size_t i = 0; i < armap_size; ++i)
284 if (this->armap_checked_[i])
285 continue;
286 if (this->armap_[i].offset == last_seen_offset)
288 this->armap_checked_[i] = true;
289 continue;
291 if (this->seen_offsets_.find(this->armap_[i].offset)
292 != this->seen_offsets_.end())
294 this->armap_checked_[i] = true;
295 last_seen_offset = this->armap_[i].offset;
296 continue;
299 Symbol* sym = symtab->lookup(this->armap_[i].name);
300 if (sym == NULL)
301 continue;
302 else if (!sym->is_undefined())
304 this->armap_checked_[i] = true;
305 continue;
307 else if (sym->binding() == elfcpp::STB_WEAK)
308 continue;
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;
319 while (added_new_object);
322 // Include all the archive members in the link. This is for --whole-archive.
324 void
325 Archive::include_all_members(Symbol_table* symtab, Layout* layout,
326 Input_objects* input_objects)
328 off_t off = sarmag;
329 while (true)
331 off_t bytes;
332 const unsigned char* p = this->get_view(off, sizeof(Archive_header),
333 &bytes);
334 if (bytes < sizeof(Archive_header))
336 if (bytes != 0)
338 fprintf(stderr, _("%s: %s: short archive header at %ld\n"),
339 program_name, this->name().c_str(),
340 static_cast<long>(off));
341 gold_exit(false);
344 break;
347 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
348 std::string name;
349 off_t size = this->interpret_header(hdr, off, &name);
350 if (name.empty())
352 // Symbol table.
354 else if (name == "/")
356 // Extended name table.
358 else
359 this->include_member(symtab, layout, input_objects, off);
361 off += sizeof(Archive_header) + size;
362 if ((off & 1) != 0)
363 ++off;
367 // Include an archive member in the link. OFF is the file offset of
368 // the member header.
370 void
371 Archive::include_member(Symbol_table* symtab, Layout* layout,
372 Input_objects* input_objects, off_t off)
374 std::string n;
375 this->read_header(off, &n);
377 size_t memoff = off + sizeof(Archive_header);
379 // Read enough of the file to pick up the entire ELF header.
380 int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
381 off_t bytes;
382 const unsigned char* p = this->input_file_->file().get_view(memoff,
383 ehdr_size,
384 &bytes);
385 if (bytes < 4)
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);
393 static unsigned char elfmagic[4] =
395 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
396 elfcpp::ELFMAG2, elfcpp::ELFMAG3
398 if (memcmp(p, elfmagic, 4) != 0)
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);
406 Object* obj = make_elf_object((std::string(this->input_file_->filename())
407 + "(" + n + ")"),
408 this->input_file_, memoff, p, bytes);
410 input_objects->add_object(obj);
412 Read_symbols_data sd;
413 obj->read_symbols(&sd);
414 obj->layout(symtab, layout, &sd);
415 obj->add_symbols(symtab, &sd);
418 // Add_archive_symbols methods.
420 Add_archive_symbols::~Add_archive_symbols()
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.
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.
431 Task::Is_runnable_type
432 Add_archive_symbols::is_runnable(Workqueue*)
434 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
435 return IS_BLOCKED;
436 return IS_RUNNABLE;
439 class Add_archive_symbols::Add_archive_symbols_locker : public Task_locker
441 public:
442 Add_archive_symbols_locker(Task_token& token, Workqueue* workqueue,
443 File_read& file)
444 : blocker_(token, workqueue), filelock_(file)
447 private:
448 Task_locker_block blocker_;
449 Task_locker_obj<File_read> filelock_;
452 Task_locker*
453 Add_archive_symbols::locks(Workqueue* workqueue)
455 return new Add_archive_symbols_locker(*this->next_blocker_,
456 workqueue,
457 this->archive_->file());
460 void
461 Add_archive_symbols::run(Workqueue*)
463 this->archive_->add_symbols(this->symtab_, this->layout_,
464 this->input_objects_);
466 if (this->input_group_ != NULL)
467 this->input_group_->add_archive(this->archive_);
468 else
470 // We no longer need to know about this archive.
471 delete this->archive_;
475 } // End namespace gold.