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"
44 // The header of an entry in the archive. This is all readable text,
45 // padded with spaces where necesary. If the contents of an archive
46 // are all text file, the entire archive is readable.
48 struct Archive::Archive_header
52 // The file modification time.
54 // The user's UID in decimal.
56 // The user's GID in decimal.
58 // The file mode in octal.
60 // The file size in decimal.
62 // The final magic code.
68 const char Archive::armag
[sarmag
] =
70 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
73 const char Archive::armagt
[sarmag
] =
75 '!', '<', 't', 'h', 'i', 'n', '>', '\n'
78 const char Archive::arfmag
[2] = { '`', '\n' };
80 // Set up the archive: read the symbol map and the extended name
86 // We need to ignore empty archives.
87 if (this->input_file_
->file().filesize() == sarmag
)
90 // The first member of the archive should be the symbol table.
91 std::string armap_name
;
92 section_size_type armap_size
=
93 convert_to_section_size_type(this->read_header(sarmag
, false,
96 if (armap_name
.empty())
98 this->read_armap(sarmag
+ sizeof(Archive_header
), armap_size
);
99 off
= sarmag
+ sizeof(Archive_header
) + armap_size
;
101 else if (!this->input_file_
->options().whole_archive())
102 gold_error(_("%s: no archive symbol table (run ranlib)"),
103 this->name().c_str());
105 // See if there is an extended name table. We cache these views
106 // because it is likely that we will want to read the following
107 // header in the add_symbols routine.
111 section_size_type extended_size
=
112 convert_to_section_size_type(this->read_header(off
, true, &xname
, NULL
));
115 const unsigned char* p
= this->get_view(off
+ sizeof(Archive_header
),
116 extended_size
, false, true);
117 const char* px
= reinterpret_cast<const char*>(p
);
118 this->extended_names_
.assign(px
, extended_size
);
122 // Unlock any nested archives.
125 Archive::unlock_nested_archives()
127 for (Nested_archive_table::iterator p
= this->nested_archives_
.begin();
128 p
!= this->nested_archives_
.end();
131 p
->second
->unlock(this->task_
);
135 // Read the archive symbol map.
138 Archive::read_armap(off_t start
, section_size_type size
)
140 // Read in the entire armap.
141 const unsigned char* p
= this->get_view(start
, size
, true, false);
143 // Numbers in the armap are always big-endian.
144 const elfcpp::Elf_Word
* pword
= reinterpret_cast<const elfcpp::Elf_Word
*>(p
);
145 unsigned int nsyms
= elfcpp::Swap
<32, true>::readval(pword
);
148 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
149 const char* pnames
= reinterpret_cast<const char*>(pword
+ nsyms
);
150 section_size_type names_size
=
151 reinterpret_cast<const char*>(p
) + size
- pnames
;
152 this->armap_names_
.assign(pnames
, names_size
);
154 this->armap_
.resize(nsyms
);
156 section_offset_type name_offset
= 0;
157 for (unsigned int i
= 0; i
< nsyms
; ++i
)
159 this->armap_
[i
].name_offset
= name_offset
;
160 this->armap_
[i
].file_offset
= elfcpp::Swap
<32, true>::readval(pword
);
161 name_offset
+= strlen(pnames
+ name_offset
) + 1;
165 if (static_cast<section_size_type
>(name_offset
) > names_size
)
166 gold_error(_("%s: bad archive symbol table names"),
167 this->name().c_str());
169 // This array keeps track of which symbols are for archive elements
170 // which we have already included in the link.
171 this->armap_checked_
.resize(nsyms
);
174 // Read the header of an archive member at OFF. Fail if something
175 // goes wrong. Return the size of the member. Set *PNAME to the name
179 Archive::read_header(off_t off
, bool cache
, std::string
* pname
,
182 const unsigned char* p
= this->get_view(off
, sizeof(Archive_header
), true,
184 const Archive_header
* hdr
= reinterpret_cast<const Archive_header
*>(p
);
185 return this->interpret_header(hdr
, off
, pname
, nested_off
);
188 // Interpret the header of HDR, the header of the archive member at
189 // file offset OFF. Fail if something goes wrong. Return the size of
190 // the member. Set *PNAME to the name of the member.
193 Archive::interpret_header(const Archive_header
* hdr
, off_t off
,
194 std::string
* pname
, off_t
* nested_off
)
196 if (memcmp(hdr
->ar_fmag
, arfmag
, sizeof arfmag
) != 0)
198 gold_error(_("%s: malformed archive header at %zu"),
199 this->name().c_str(), static_cast<size_t>(off
));
200 return this->input_file_
->file().filesize() - off
;
203 const int size_string_size
= sizeof hdr
->ar_size
;
204 char size_string
[size_string_size
+ 1];
205 memcpy(size_string
, hdr
->ar_size
, size_string_size
);
206 char* ps
= size_string
+ size_string_size
;
207 while (ps
[-1] == ' ')
213 off_t member_size
= strtol(size_string
, &end
, 10);
216 || (member_size
== LONG_MAX
&& errno
== ERANGE
))
218 gold_error(_("%s: malformed archive header size at %zu"),
219 this->name().c_str(), static_cast<size_t>(off
));
220 return this->input_file_
->file().filesize() - off
;
223 if (hdr
->ar_name
[0] != '/')
225 const char* name_end
= strchr(hdr
->ar_name
, '/');
227 || name_end
- hdr
->ar_name
>= static_cast<int>(sizeof hdr
->ar_name
))
229 gold_error(_("%s: malformed archive header name at %zu"),
230 this->name().c_str(), static_cast<size_t>(off
));
231 return this->input_file_
->file().filesize() - off
;
233 pname
->assign(hdr
->ar_name
, name_end
- hdr
->ar_name
);
234 if (nested_off
!= NULL
)
237 else if (hdr
->ar_name
[1] == ' ')
239 // This is the symbol table.
242 else if (hdr
->ar_name
[1] == '/')
244 // This is the extended name table.
245 pname
->assign(1, '/');
250 long x
= strtol(hdr
->ar_name
+ 1, &end
, 10);
253 y
= strtol(end
+ 1, &end
, 10);
256 || (x
== LONG_MAX
&& errno
== ERANGE
)
257 || static_cast<size_t>(x
) >= this->extended_names_
.size())
259 gold_error(_("%s: bad extended name index at %zu"),
260 this->name().c_str(), static_cast<size_t>(off
));
261 return this->input_file_
->file().filesize() - off
;
264 const char* name
= this->extended_names_
.data() + x
;
265 const char* name_end
= strchr(name
, '\n');
266 if (static_cast<size_t>(name_end
- name
) > this->extended_names_
.size()
267 || name_end
[-1] != '/')
269 gold_error(_("%s: bad extended name entry at header %zu"),
270 this->name().c_str(), static_cast<size_t>(off
));
271 return this->input_file_
->file().filesize() - off
;
273 pname
->assign(name
, name_end
- 1 - name
);
274 if (nested_off
!= NULL
)
281 // Select members from the archive and add them to the link. We walk
282 // through the elements in the archive map, and look each one up in
283 // the symbol table. If it exists as a strong undefined symbol, we
284 // pull in the corresponding element. We have to do this in a loop,
285 // since pulling in one element may create new undefined symbols which
286 // may be satisfied by other objects in the archive.
289 Archive::add_symbols(Symbol_table
* symtab
, Layout
* layout
,
290 Input_objects
* input_objects
, Mapfile
* mapfile
)
292 if (this->input_file_
->options().whole_archive())
293 return this->include_all_members(symtab
, layout
, input_objects
,
296 const size_t armap_size
= this->armap_
.size();
298 // This is a quick optimization, since we usually see many symbols
299 // in a row with the same offset. last_seen_offset holds the last
300 // offset we saw that was present in the seen_offsets_ set.
301 off_t last_seen_offset
= -1;
303 // Track which symbols in the symbol table we've already found to be
306 bool added_new_object
;
309 added_new_object
= false;
310 for (size_t i
= 0; i
< armap_size
; ++i
)
312 if (this->armap_checked_
[i
])
314 if (this->armap_
[i
].file_offset
== last_seen_offset
)
316 this->armap_checked_
[i
] = true;
319 if (this->seen_offsets_
.find(this->armap_
[i
].file_offset
)
320 != this->seen_offsets_
.end())
322 this->armap_checked_
[i
] = true;
323 last_seen_offset
= this->armap_
[i
].file_offset
;
327 const char* sym_name
= (this->armap_names_
.data()
328 + this->armap_
[i
].name_offset
);
329 Symbol
* sym
= symtab
->lookup(sym_name
);
332 // Check whether the symbol was named in a -u option.
333 if (!parameters
->options().is_undefined(sym_name
))
336 else if (!sym
->is_undefined())
338 this->armap_checked_
[i
] = true;
341 else if (sym
->binding() == elfcpp::STB_WEAK
)
344 // We want to include this object in the link.
345 last_seen_offset
= this->armap_
[i
].file_offset
;
346 this->seen_offsets_
.insert(last_seen_offset
);
347 this->armap_checked_
[i
] = true;
355 this->include_member(symtab
, layout
, input_objects
,
356 last_seen_offset
, mapfile
, sym
, why
.c_str());
358 added_new_object
= true;
361 while (added_new_object
);
364 // Include all the archive members in the link. This is for --whole-archive.
367 Archive::include_all_members(Symbol_table
* symtab
, Layout
* layout
,
368 Input_objects
* input_objects
, Mapfile
* mapfile
)
371 off_t filesize
= this->input_file_
->file().filesize();
374 if (filesize
- off
< static_cast<off_t
>(sizeof(Archive_header
)))
377 gold_error(_("%s: short archive header at %zu"),
378 this->name().c_str(), static_cast<size_t>(off
));
382 unsigned char hdr_buf
[sizeof(Archive_header
)];
383 this->input_file_
->file().read(off
, sizeof(Archive_header
), hdr_buf
);
385 const Archive_header
* hdr
=
386 reinterpret_cast<const Archive_header
*>(hdr_buf
);
388 off_t size
= this->interpret_header(hdr
, off
, &name
, NULL
);
389 bool special_member
= false;
393 special_member
= true;
395 else if (name
== "/")
397 // Extended name table.
398 special_member
= true;
401 this->include_member(symtab
, layout
, input_objects
, off
,
402 mapfile
, NULL
, "--whole-archive");
404 off
+= sizeof(Archive_header
);
405 if (special_member
|| !this->is_thin_archive_
)
412 // Include an archive member in the link. OFF is the file offset of
413 // the member header. WHY is the reason we are including this member.
416 Archive::include_member(Symbol_table
* symtab
, Layout
* layout
,
417 Input_objects
* input_objects
, off_t off
,
418 Mapfile
* mapfile
, Symbol
* sym
, const char* why
)
422 this->read_header(off
, false, &n
, &nested_off
);
425 mapfile
->report_include_archive_member(this, n
, sym
, why
);
427 Input_file
* input_file
;
430 if (!this->is_thin_archive_
)
432 input_file
= this->input_file_
;
433 memoff
= off
+ static_cast<off_t
>(sizeof(Archive_header
));
437 // Adjust a relative pathname so that it is relative
438 // to the directory containing the archive.
439 if (!IS_ABSOLUTE_PATH(n
.c_str()))
441 const char *arch_path
= this->name().c_str();
442 const char *basename
= lbasename(arch_path
);
443 if (basename
> arch_path
)
444 n
.replace(0, 0, this->name().substr(0, basename
- arch_path
));
448 // This is a member of a nested archive. Open the containing
449 // archive if we don't already have it open, then do a recursive
450 // call to include the member from that archive.
452 Nested_archive_table::const_iterator p
=
453 this->nested_archives_
.find(n
);
454 if (p
!= this->nested_archives_
.end())
458 Input_file_argument
* input_file_arg
=
459 new Input_file_argument(n
.c_str(), false, "", false,
460 parameters
->options());
461 input_file
= new Input_file(input_file_arg
);
462 if (!input_file
->open(parameters
->options(), *this->dirpath_
,
465 arch
= new Archive(n
, input_file
, false, this->dirpath_
,
468 std::pair
<Nested_archive_table::iterator
, bool> ins
=
469 this->nested_archives_
.insert(std::make_pair(n
, arch
));
470 gold_assert(ins
.second
);
472 arch
->include_member(symtab
, layout
, input_objects
, nested_off
,
476 // This is an external member of a thin archive. Open the
477 // file as a regular relocatable object file.
478 Input_file_argument
* input_file_arg
=
479 new Input_file_argument(n
.c_str(), false, "", false,
480 this->input_file_
->options());
481 input_file
= new Input_file(input_file_arg
);
482 if (!input_file
->open(parameters
->options(), *this->dirpath_
,
490 off_t filesize
= input_file
->file().filesize();
491 int read_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
492 if (filesize
- memoff
< read_size
)
493 read_size
= filesize
- memoff
;
497 gold_error(_("%s: member at %zu is not an ELF object"),
498 this->name().c_str(), static_cast<size_t>(off
));
502 const unsigned char* ehdr
= input_file
->file().get_view(memoff
, 0, read_size
,
505 static unsigned char elfmagic
[4] =
507 elfcpp::ELFMAG0
, elfcpp::ELFMAG1
,
508 elfcpp::ELFMAG2
, elfcpp::ELFMAG3
510 if (memcmp(ehdr
, elfmagic
, 4) != 0)
512 gold_error(_("%s: member at %zu is not an ELF object"),
513 this->name().c_str(), static_cast<size_t>(off
));
517 Object
* obj
= make_elf_object((std::string(this->input_file_
->filename())
519 input_file
, memoff
, ehdr
, read_size
);
521 if (input_objects
->add_object(obj
))
523 Read_symbols_data sd
;
524 obj
->read_symbols(&sd
);
525 obj
->layout(symtab
, layout
, &sd
);
526 obj
->add_symbols(symtab
, &sd
);
530 // FIXME: We need to close the descriptor here.
534 if (this->is_thin_archive_
)
536 // Opening the file locked it. Unlock it now.
537 input_file
->file().unlock(this->task_
);
541 // Add_archive_symbols methods.
543 Add_archive_symbols::~Add_archive_symbols()
545 if (this->this_blocker_
!= NULL
)
546 delete this->this_blocker_
;
547 // next_blocker_ is deleted by the task associated with the next
551 // Return whether we can add the archive symbols. We are blocked by
552 // this_blocker_. We block next_blocker_. We also lock the file.
555 Add_archive_symbols::is_runnable()
557 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
558 return this->this_blocker_
;
563 Add_archive_symbols::locks(Task_locker
* tl
)
565 tl
->add(this, this->next_blocker_
);
566 tl
->add(this, this->archive_
->token());
570 Add_archive_symbols::run(Workqueue
*)
572 this->archive_
->add_symbols(this->symtab_
, this->layout_
,
573 this->input_objects_
, this->mapfile_
);
575 this->archive_
->unlock_nested_archives();
577 this->archive_
->release();
578 this->archive_
->clear_uncached_views();
580 if (this->input_group_
!= NULL
)
581 this->input_group_
->add_archive(this->archive_
);
584 // We no longer need to know about this archive.
585 delete this->archive_
;
586 this->archive_
= NULL
;
590 } // End namespace gold.