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.
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
49 // The file modification time.
51 // The user's UID in decimal.
53 // The user's GID in decimal.
55 // The file mode in octal.
57 // The file size in decimal.
59 // The final magic code.
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
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
);
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());
96 // See if there is an extended name table.
100 off_t extended_size
= this->read_header(off
, &xname
);
103 const unsigned char* p
= this->get_view(off
+ sizeof(Archive_header
),
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.
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
);
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;
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());
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
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.
168 Archive::interpret_header(const Archive_header
* hdr
, off_t off
,
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
));
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] == ' ')
189 off_t member_size
= strtol(size_string
, &end
, 10);
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
));
200 if (hdr
->ar_name
[0] != '/')
202 const char* name_end
= strchr(hdr
->ar_name
, '/');
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
));
211 pname
->assign(hdr
->ar_name
, name_end
- hdr
->ar_name
);
213 else if (hdr
->ar_name
[1] == ' ')
215 // This is the symbol table.
218 else if (hdr
->ar_name
[1] == '/')
220 // This is the extended name table.
221 pname
->assign(1, '/');
226 long x
= strtol(hdr
->ar_name
+ 1, &end
, 10);
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
));
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
));
248 pname
->assign(name
, name_end
- name
);
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.
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
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
])
286 if (this->armap_
[i
].offset
== last_seen_offset
)
288 this->armap_checked_
[i
] = true;
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
;
299 Symbol
* sym
= symtab
->lookup(this->armap_
[i
].name
);
302 else if (!sym
->is_undefined())
304 this->armap_checked_
[i
] = true;
307 else if (sym
->binding() == elfcpp::STB_WEAK
)
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
,
316 added_new_object
= true;
319 while (added_new_object
);
322 // Include all the archive members in the link. This is for --whole-archive.
325 Archive::include_all_members(Symbol_table
* symtab
, Layout
* layout
,
326 Input_objects
* input_objects
)
332 const unsigned char* p
= this->get_view(off
, sizeof(Archive_header
),
334 if (bytes
< sizeof(Archive_header
))
338 fprintf(stderr
, _("%s: %s: short archive header at %ld\n"),
339 program_name
, this->name().c_str(),
340 static_cast<long>(off
));
347 const Archive_header
* hdr
= reinterpret_cast<const Archive_header
*>(p
);
349 off_t size
= this->interpret_header(hdr
, off
, &name
);
354 else if (name
== "/")
356 // Extended name table.
359 this->include_member(symtab
, layout
, input_objects
, off
);
361 off
+= sizeof(Archive_header
) + size
;
367 // Include an archive member in the link. OFF is the file offset of
368 // the member header.
371 Archive::include_member(Symbol_table
* symtab
, Layout
* layout
,
372 Input_objects
* input_objects
, off_t off
)
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
;
382 const unsigned char* p
= this->input_file_
->file().get_view(memoff
,
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
));
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
));
406 Object
* obj
= make_elf_object((std::string(this->input_file_
->filename())
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
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())
439 class Add_archive_symbols::Add_archive_symbols_locker
: public Task_locker
442 Add_archive_symbols_locker(Task_token
& token
, Workqueue
* workqueue
,
444 : blocker_(token
, workqueue
), filelock_(file
)
448 Task_locker_block blocker_
;
449 Task_locker_obj
<File_read
> filelock_
;
453 Add_archive_symbols::locks(Workqueue
* workqueue
)
455 return new Add_archive_symbols_locker(*this->next_blocker_
,
457 this->archive_
->file());
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_
);
470 // We no longer need to know about this archive.
471 delete this->archive_
;
475 } // End namespace gold.