1 // archive.cc -- archive support for gold
3 // Copyright 2006, 2007, 2008 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.
29 #include "libiberty.h"
30 #include "filenames.h"
43 // The header of an entry in the archive. This is all readable text,
44 // padded with spaces where necesary. If the contents of an archive
45 // are all text file, the entire archive is readable.
47 struct Archive::Archive_header
51 // The file modification time.
53 // The user's UID in decimal.
55 // The user's GID in decimal.
57 // The file mode in octal.
59 // The file size in decimal.
61 // The final magic code.
67 const char Archive::armag
[sarmag
] =
69 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
72 const char Archive::armagt
[sarmag
] =
74 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
77 const char Archive::arfmag
[2] = { '`', '\n' };
79 // Set up the archive: read the symbol map and the extended name
85 // We need to ignore empty archives.
86 if (this->input_file_
->file().filesize() == sarmag
)
89 // The first member of the archive should be the symbol table.
90 std::string armap_name
;
91 section_size_type armap_size
=
92 convert_to_section_size_type(this->read_header(sarmag
, false,
95 if (armap_name
.empty())
97 this->read_armap(sarmag
+ sizeof(Archive_header
), armap_size
);
98 off
= sarmag
+ sizeof(Archive_header
) + armap_size
;
100 else if (!this->input_file_
->options().whole_archive())
101 gold_error(_("%s: no archive symbol table (run ranlib)"),
102 this->name().c_str());
104 // See if there is an extended name table. We cache these views
105 // because it is likely that we will want to read the following
106 // header in the add_symbols routine.
110 section_size_type extended_size
=
111 convert_to_section_size_type(this->read_header(off
, true, &xname
, NULL
));
114 const unsigned char* p
= this->get_view(off
+ sizeof(Archive_header
),
115 extended_size
, true);
116 const char* px
= reinterpret_cast<const char*>(p
);
117 this->extended_names_
.assign(px
, extended_size
);
121 // Unlock any nested archives.
124 Archive::unlock_nested_archives()
126 for (Nested_archive_table::iterator p
= this->nested_archives_
.begin();
127 p
!= this->nested_archives_
.end();
130 p
->second
->unlock(this->task_
);
134 // Read the archive symbol map.
137 Archive::read_armap(off_t start
, section_size_type size
)
139 // Read in the entire armap.
140 const unsigned char* p
= this->get_view(start
, size
, false);
142 // Numbers in the armap are always big-endian.
143 const elfcpp::Elf_Word
* pword
= reinterpret_cast<const elfcpp::Elf_Word
*>(p
);
144 unsigned int nsyms
= elfcpp::Swap
<32, true>::readval(pword
);
147 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
148 const char* pnames
= reinterpret_cast<const char*>(pword
+ nsyms
);
149 section_size_type names_size
=
150 reinterpret_cast<const char*>(p
) + size
- pnames
;
151 this->armap_names_
.assign(pnames
, names_size
);
153 this->armap_
.resize(nsyms
);
155 section_offset_type name_offset
= 0;
156 for (unsigned int i
= 0; i
< nsyms
; ++i
)
158 this->armap_
[i
].name_offset
= name_offset
;
159 this->armap_
[i
].file_offset
= elfcpp::Swap
<32, true>::readval(pword
);
160 name_offset
+= strlen(pnames
+ name_offset
) + 1;
164 if (static_cast<section_size_type
>(name_offset
) > names_size
)
165 gold_error(_("%s: bad archive symbol table names"),
166 this->name().c_str());
168 // This array keeps track of which symbols are for archive elements
169 // which we have already included in the link.
170 this->armap_checked_
.resize(nsyms
);
173 // Read the header of an archive member at OFF. Fail if something
174 // goes wrong. Return the size of the member. Set *PNAME to the name
178 Archive::read_header(off_t off
, bool cache
, std::string
* pname
,
181 const unsigned char* p
= this->get_view(off
, sizeof(Archive_header
), cache
);
182 const Archive_header
* hdr
= reinterpret_cast<const Archive_header
*>(p
);
183 return this->interpret_header(hdr
, off
, pname
, nested_off
);
186 // Interpret the header of HDR, the header of the archive member at
187 // file offset OFF. Fail if something goes wrong. Return the size of
188 // the member. Set *PNAME to the name of the member.
191 Archive::interpret_header(const Archive_header
* hdr
, off_t off
,
192 std::string
* pname
, off_t
* nested_off
)
194 if (memcmp(hdr
->ar_fmag
, arfmag
, sizeof arfmag
) != 0)
196 gold_error(_("%s: malformed archive header at %zu"),
197 this->name().c_str(), static_cast<size_t>(off
));
198 return this->input_file_
->file().filesize() - off
;
201 const int size_string_size
= sizeof hdr
->ar_size
;
202 char size_string
[size_string_size
+ 1];
203 memcpy(size_string
, hdr
->ar_size
, size_string_size
);
204 char* ps
= size_string
+ size_string_size
;
205 while (ps
[-1] == ' ')
211 off_t member_size
= strtol(size_string
, &end
, 10);
214 || (member_size
== LONG_MAX
&& errno
== ERANGE
))
216 gold_error(_("%s: malformed archive header size at %zu"),
217 this->name().c_str(), static_cast<size_t>(off
));
218 return this->input_file_
->file().filesize() - off
;
221 if (hdr
->ar_name
[0] != '/')
223 const char* name_end
= strchr(hdr
->ar_name
, '/');
225 || name_end
- hdr
->ar_name
>= static_cast<int>(sizeof hdr
->ar_name
))
227 gold_error(_("%s: malformed archive header name at %zu"),
228 this->name().c_str(), static_cast<size_t>(off
));
229 return this->input_file_
->file().filesize() - off
;
231 pname
->assign(hdr
->ar_name
, name_end
- hdr
->ar_name
);
232 if (nested_off
!= NULL
)
235 else if (hdr
->ar_name
[1] == ' ')
237 // This is the symbol table.
240 else if (hdr
->ar_name
[1] == '/')
242 // This is the extended name table.
243 pname
->assign(1, '/');
248 long x
= strtol(hdr
->ar_name
+ 1, &end
, 10);
251 y
= strtol(end
+ 1, &end
, 10);
254 || (x
== LONG_MAX
&& errno
== ERANGE
)
255 || static_cast<size_t>(x
) >= this->extended_names_
.size())
257 gold_error(_("%s: bad extended name index at %zu"),
258 this->name().c_str(), static_cast<size_t>(off
));
259 return this->input_file_
->file().filesize() - off
;
262 const char* name
= this->extended_names_
.data() + x
;
263 const char* name_end
= strchr(name
, '\n');
264 if (static_cast<size_t>(name_end
- name
) > this->extended_names_
.size()
265 || name_end
[-1] != '/')
267 gold_error(_("%s: bad extended name entry at header %zu"),
268 this->name().c_str(), static_cast<size_t>(off
));
269 return this->input_file_
->file().filesize() - off
;
271 pname
->assign(name
, name_end
- 1 - name
);
272 if (nested_off
!= NULL
)
279 // Select members from the archive and add them to the link. We walk
280 // through the elements in the archive map, and look each one up in
281 // the symbol table. If it exists as a strong undefined symbol, we
282 // pull in the corresponding element. We have to do this in a loop,
283 // since pulling in one element may create new undefined symbols which
284 // may be satisfied by other objects in the archive.
287 Archive::add_symbols(Symbol_table
* symtab
, Layout
* layout
,
288 Input_objects
* input_objects
)
290 if (this->input_file_
->options().whole_archive())
291 return this->include_all_members(symtab
, layout
, input_objects
);
293 const size_t armap_size
= this->armap_
.size();
295 // This is a quick optimization, since we usually see many symbols
296 // in a row with the same offset. last_seen_offset holds the last
297 // offset we saw that was present in the seen_offsets_ set.
298 off_t last_seen_offset
= -1;
300 // Track which symbols in the symbol table we've already found to be
303 bool added_new_object
;
306 added_new_object
= false;
307 for (size_t i
= 0; i
< armap_size
; ++i
)
309 if (this->armap_checked_
[i
])
311 if (this->armap_
[i
].file_offset
== last_seen_offset
)
313 this->armap_checked_
[i
] = true;
316 if (this->seen_offsets_
.find(this->armap_
[i
].file_offset
)
317 != this->seen_offsets_
.end())
319 this->armap_checked_
[i
] = true;
320 last_seen_offset
= this->armap_
[i
].file_offset
;
324 const char* sym_name
= (this->armap_names_
.data()
325 + this->armap_
[i
].name_offset
);
326 Symbol
* sym
= symtab
->lookup(sym_name
);
329 else if (!sym
->is_undefined())
331 this->armap_checked_
[i
] = true;
334 else if (sym
->binding() == elfcpp::STB_WEAK
)
337 // We want to include this object in the link.
338 last_seen_offset
= this->armap_
[i
].file_offset
;
339 this->seen_offsets_
.insert(last_seen_offset
);
340 this->armap_checked_
[i
] = true;
341 this->include_member(symtab
, layout
, input_objects
,
343 added_new_object
= true;
346 while (added_new_object
);
349 // Include all the archive members in the link. This is for --whole-archive.
352 Archive::include_all_members(Symbol_table
* symtab
, Layout
* layout
,
353 Input_objects
* input_objects
)
356 off_t filesize
= this->input_file_
->file().filesize();
359 if (filesize
- off
< static_cast<off_t
>(sizeof(Archive_header
)))
362 gold_error(_("%s: short archive header at %zu"),
363 this->name().c_str(), static_cast<size_t>(off
));
367 unsigned char hdr_buf
[sizeof(Archive_header
)];
368 this->input_file_
->file().read(off
, sizeof(Archive_header
), hdr_buf
);
370 const Archive_header
* hdr
=
371 reinterpret_cast<const Archive_header
*>(hdr_buf
);
373 off_t size
= this->interpret_header(hdr
, off
, &name
, NULL
);
378 else if (name
== "/")
380 // Extended name table.
383 this->include_member(symtab
, layout
, input_objects
, off
);
385 off
+= sizeof(Archive_header
);
386 if (!this->is_thin_archive_
)
393 // Include an archive member in the link. OFF is the file offset of
394 // the member header.
397 Archive::include_member(Symbol_table
* symtab
, Layout
* layout
,
398 Input_objects
* input_objects
, off_t off
)
402 this->read_header(off
, false, &n
, &nested_off
);
404 Input_file
* input_file
;
407 if (!this->is_thin_archive_
)
409 input_file
= this->input_file_
;
410 memoff
= off
+ static_cast<off_t
>(sizeof(Archive_header
));
414 // Adjust a relative pathname so that it is relative
415 // to the directory containing the archive.
416 if (!IS_ABSOLUTE_PATH(n
.c_str()))
418 const char *arch_path
= this->name().c_str();
419 const char *basename
= lbasename(arch_path
);
420 if (basename
> arch_path
)
421 n
.replace(0, 0, this->name().substr(0, basename
- arch_path
));
425 // This is a member of a nested archive. Open the containing
426 // archive if we don't already have it open, then do a recursive
427 // call to include the member from that archive.
429 Nested_archive_table::const_iterator p
=
430 this->nested_archives_
.find(n
);
431 if (p
!= this->nested_archives_
.end())
435 Input_file_argument
* input_file_arg
=
436 new Input_file_argument(n
.c_str(), false, "", false,
437 parameters
->options());
438 input_file
= new Input_file(input_file_arg
);
439 if (!input_file
->open(parameters
->options(), *this->dirpath_
,
442 arch
= new Archive(n
, input_file
, false, this->dirpath_
,
445 std::pair
<Nested_archive_table::iterator
, bool> ins
=
446 this->nested_archives_
.insert(std::make_pair(n
, arch
));
447 gold_assert(ins
.second
);
449 arch
->include_member(symtab
, layout
, input_objects
, nested_off
);
452 // This is an external member of a thin archive. Open the
453 // file as a regular relocatable object file.
454 Input_file_argument
* input_file_arg
=
455 new Input_file_argument(n
.c_str(), false, "", false,
456 this->input_file_
->options());
457 input_file
= new Input_file(input_file_arg
);
458 if (!input_file
->open(parameters
->options(), *this->dirpath_
,
466 // Read enough of the file to pick up the entire ELF header.
467 unsigned char ehdr_buf
[elfcpp::Elf_sizes
<64>::ehdr_size
];
469 off_t filesize
= input_file
->file().filesize();
470 int read_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
471 if (filesize
- memoff
< read_size
)
472 read_size
= filesize
- memoff
;
476 gold_error(_("%s: member at %zu is not an ELF object"),
477 this->name().c_str(), static_cast<size_t>(off
));
481 input_file
->file().read(memoff
, read_size
, ehdr_buf
);
483 static unsigned char elfmagic
[4] =
485 elfcpp::ELFMAG0
, elfcpp::ELFMAG1
,
486 elfcpp::ELFMAG2
, elfcpp::ELFMAG3
488 if (memcmp(ehdr_buf
, elfmagic
, 4) != 0)
490 gold_error(_("%s: member at %zu is not an ELF object"),
491 this->name().c_str(), static_cast<size_t>(off
));
495 Object
* obj
= make_elf_object((std::string(this->input_file_
->filename())
497 input_file
, memoff
, ehdr_buf
,
500 if (input_objects
->add_object(obj
))
502 Read_symbols_data sd
;
503 obj
->read_symbols(&sd
);
504 obj
->layout(symtab
, layout
, &sd
);
505 obj
->add_symbols(symtab
, &sd
);
509 // FIXME: We need to close the descriptor here.
513 if (this->is_thin_archive_
)
515 // Opening the file locked it. Unlock it now.
516 input_file
->file().unlock(this->task_
);
520 // Add_archive_symbols methods.
522 Add_archive_symbols::~Add_archive_symbols()
524 if (this->this_blocker_
!= NULL
)
525 delete this->this_blocker_
;
526 // next_blocker_ is deleted by the task associated with the next
530 // Return whether we can add the archive symbols. We are blocked by
531 // this_blocker_. We block next_blocker_. We also lock the file.
534 Add_archive_symbols::is_runnable()
536 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
537 return this->this_blocker_
;
542 Add_archive_symbols::locks(Task_locker
* tl
)
544 tl
->add(this, this->next_blocker_
);
545 tl
->add(this, this->archive_
->token());
549 Add_archive_symbols::run(Workqueue
*)
551 this->archive_
->add_symbols(this->symtab_
, this->layout_
,
552 this->input_objects_
);
554 this->archive_
->unlock_nested_archives();
556 this->archive_
->release();
558 if (this->input_group_
!= NULL
)
559 this->input_group_
->add_archive(this->archive_
);
562 // We no longer need to know about this archive.
563 delete this->archive_
;
564 this->archive_
= NULL
;
568 } // End namespace gold.