From Craig Silverstein: rework seen handling to be based on archive
[deliverable/binutils-gdb.git] / gold / archive.cc
1 // archive.cc -- archive support for gold
2
3 #include "gold.h"
4
5 #include <cerrno>
6 #include <cstring>
7 #include <climits>
8 #include <vector>
9
10 #include "elfcpp.h"
11 #include "fileread.h"
12 #include "readsyms.h"
13 #include "symtab.h"
14 #include "object.h"
15 #include "archive.h"
16
17 namespace gold
18 {
19
20 // The header of an entry in the archive. This is all readable text,
21 // padded with spaces where necesary. If the contents of an archive
22 // are all text file, the entire archive is readable.
23
24 struct Archive::Archive_header
25 {
26 // The entry name.
27 char ar_name[16];
28 // The file modification time.
29 char ar_date[12];
30 // The user's UID in decimal.
31 char ar_uid[6];
32 // The user's GID in decimal.
33 char ar_gid[6];
34 // The file mode in octal.
35 char ar_mode[8];
36 // The file size in decimal.
37 char ar_size[10];
38 // The final magic code.
39 char ar_fmag[2];
40 };
41
42 // Archive methods.
43
44 const char Archive::armag[sarmag] =
45 {
46 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
47 };
48
49 const char Archive::arfmag[2] = { '`', '\n' };
50
51 // Set up the archive: read the symbol map and the extended name
52 // table.
53
54 void
55 Archive::setup()
56 {
57 // The first member of the archive should be the symbol table.
58 std::string armap_name;
59 off_t armap_size = this->read_header(sarmag, &armap_name);
60 off_t off;
61 if (armap_name.empty())
62 {
63 this->read_armap(sarmag + sizeof(Archive_header), armap_size);
64 off = sarmag + sizeof(Archive_header) + armap_size;
65 }
66 else if (!this->input_file_->options().include_whole_archive())
67 {
68 fprintf(stderr, _("%s: %s: no archive symbol table (run ranlib)\n"),
69 program_name, this->name().c_str());
70 gold_exit(false);
71 }
72 else
73 off = sarmag;
74
75 // See if there is an extended name table.
76 if ((off & 1) != 0)
77 ++off;
78 std::string xname;
79 off_t extended_size = this->read_header(off, &xname);
80 if (xname == "/")
81 {
82 const unsigned char* p = this->get_view(off + sizeof(Archive_header),
83 extended_size);
84 const char* px = reinterpret_cast<const char*>(p);
85 this->extended_names_.assign(px, extended_size);
86 }
87
88 // Opening the file locked it. Unlock it now.
89 this->input_file_->file().unlock();
90 }
91
92 // Read the archive symbol map.
93
94 void
95 Archive::read_armap(off_t start, off_t size)
96 {
97 // Read in the entire armap.
98 const unsigned char* p = this->get_view(start, size);
99
100 // Numbers in the armap are always big-endian.
101 const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p);
102 unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword);
103 ++pword;
104
105 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
106 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
107
108 this->armap_.resize(nsyms);
109
110 for (unsigned int i = 0; i < nsyms; ++i)
111 {
112 this->armap_[i].name = pnames;
113 this->armap_[i].offset = elfcpp::Swap<32, true>::readval(pword);
114 pnames += strlen(pnames) + 1;
115 ++pword;
116 }
117
118 if (reinterpret_cast<const unsigned char*>(pnames) - p > size)
119 {
120 fprintf(stderr, _("%s: %s: bad archive symbol table names\n"),
121 program_name, this->name().c_str());
122 gold_exit(false);
123 }
124 }
125
126 // Read the header of an archive member at OFF. Fail if something
127 // goes wrong. Return the size of the member. Set *PNAME to the name
128 // of the member.
129
130 off_t
131 Archive::read_header(off_t off, std::string* pname)
132 {
133 const unsigned char* p = this->get_view(off, sizeof(Archive_header));
134 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
135 return this->interpret_header(hdr, off, pname);
136 }
137
138 // Interpret the header of HDR, the header of the archive member at
139 // file offset OFF. Fail if something goes wrong. Return the size of
140 // the member. Set *PNAME to the name of the member.
141
142 off_t
143 Archive::interpret_header(const Archive_header* hdr, off_t off,
144 std::string* pname)
145 {
146 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
147 {
148 fprintf(stderr, _("%s; %s: malformed archive header at %ld\n"),
149 program_name, this->name().c_str(),
150 static_cast<long>(off));
151 gold_exit(false);
152 }
153
154 const int size_string_size = sizeof hdr->ar_size;
155 char size_string[size_string_size + 1];
156 memcpy(size_string, hdr->ar_size, size_string_size);
157 char* ps = size_string + size_string_size;
158 while (ps[-1] == ' ')
159 --ps;
160 *ps = '\0';
161
162 errno = 0;
163 char* end;
164 off_t member_size = strtol(size_string, &end, 10);
165 if (*end != '\0'
166 || member_size < 0
167 || (member_size == LONG_MAX && errno == ERANGE))
168 {
169 fprintf(stderr, _("%s: %s: malformed archive header size at %ld\n"),
170 program_name, this->name().c_str(),
171 static_cast<long>(off));
172 gold_exit(false);
173 }
174
175 if (hdr->ar_name[0] != '/')
176 {
177 const char* name_end = strchr(hdr->ar_name, '/');
178 if (name_end == NULL
179 || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name))
180 {
181 fprintf(stderr, _("%s: %s: malformed archive header name at %ld\n"),
182 program_name, this->name().c_str(),
183 static_cast<long>(off));
184 gold_exit(false);
185 }
186 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
187 }
188 else if (hdr->ar_name[1] == ' ')
189 {
190 // This is the symbol table.
191 pname->clear();
192 }
193 else if (hdr->ar_name[1] == '/')
194 {
195 // This is the extended name table.
196 pname->assign(1, '/');
197 }
198 else
199 {
200 errno = 0;
201 long x = strtol(hdr->ar_name + 1, &end, 10);
202 if (*end != ' '
203 || x < 0
204 || (x == LONG_MAX && errno == ERANGE)
205 || static_cast<size_t>(x) >= this->extended_names_.size())
206 {
207 fprintf(stderr, _("%s: %s: bad extended name index at %ld\n"),
208 program_name, this->name().c_str(),
209 static_cast<long>(off));
210 gold_exit(false);
211 }
212
213 const char* name = this->extended_names_.data() + x;
214 const char* name_end = strchr(name, '/');
215 if (static_cast<size_t>(name_end - name) > this->extended_names_.size()
216 || name_end[1] != '\n')
217 {
218 fprintf(stderr, _("%s: %s: bad extended name entry at header %ld\n"),
219 program_name, this->name().c_str(),
220 static_cast<long>(off));
221 gold_exit(false);
222 }
223 pname->assign(name, name_end - name);
224 }
225
226 return member_size;
227 }
228
229 // A simple hash code for off_t values.
230 class Seen_hash
231 {
232 public:
233 size_t operator()(off_t val) const
234 { return static_cast<size_t>(val); }
235 };
236
237 // Select members from the archive and add them to the link. We walk
238 // through the elements in the archive map, and look each one up in
239 // the symbol table. If it exists as a strong undefined symbol, we
240 // pull in the corresponding element. We have to do this in a loop,
241 // since pulling in one element may create new undefined symbols which
242 // may be satisfied by other objects in the archive.
243
244 void
245 Archive::add_symbols(const General_options& options, Symbol_table* symtab,
246 Layout* layout, Input_objects* input_objects)
247 {
248 if (this->input_file_->options().include_whole_archive())
249 return this->include_all_members(options, symtab, layout, input_objects);
250
251 const size_t armap_size = this->armap_.size();
252
253 // Track which elements in the archive map have already been included
254 // in the link. Elements are identified by their offset.
255 Unordered_set<off_t, Seen_hash> seen;
256 // This is a quick optimization, since we usually see many symbols
257 // in a row with the same offset. last_seen holds the last offset
258 // we saw that was present in the seen_ set.
259 off_t last_seen = -1;
260
261 bool added_new_object;
262 do
263 {
264 added_new_object = false;
265 for (size_t i = 0; i < armap_size; ++i)
266 {
267 if (this->armap_[i].offset == last_seen)
268 continue;
269 if (seen.find(this->armap_[i].offset) != seen.end())
270 {
271 last_seen = this->armap_[i].offset;
272 continue;
273 }
274
275 Symbol* sym = symtab->lookup(this->armap_[i].name);
276 if (sym == NULL)
277 continue;
278 else if (!sym->is_undefined())
279 {
280 seen.insert(this->armap_[i].offset);
281 last_seen = this->armap_[i].offset;
282 continue;
283 }
284 else if (sym->binding() == elfcpp::STB_WEAK)
285 continue;
286
287 // We want to include this object in the link.
288 last_seen = this->armap_[i].offset;
289 seen.insert(last_seen);
290 this->include_member(options, symtab, layout, input_objects,
291 last_seen);
292 added_new_object = true;
293 }
294 }
295 while (added_new_object);
296 }
297
298 // Include all the archive members in the link. This is for --whole-archive.
299
300 void
301 Archive::include_all_members(const General_options& options,
302 Symbol_table* symtab, Layout* layout,
303 Input_objects* input_objects)
304 {
305 off_t off = sarmag;
306 while (true)
307 {
308 off_t bytes;
309 const unsigned char* p = this->get_view(off, sizeof(Archive_header),
310 &bytes);
311 if (bytes < sizeof(Archive_header))
312 {
313 if (bytes != 0)
314 {
315 fprintf(stderr, _("%s: %s: short archive header at %ld\n"),
316 program_name, this->name().c_str(),
317 static_cast<long>(off));
318 gold_exit(false);
319 }
320
321 break;
322 }
323
324 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
325 std::string name;
326 off_t size = this->interpret_header(hdr, off, &name);
327 if (name.empty())
328 {
329 // Symbol table.
330 }
331 else if (name == "/")
332 {
333 // Extended name table.
334 }
335 else
336 this->include_member(options, symtab, layout, input_objects, off);
337
338 off += sizeof(Archive_header) + size;
339 if ((off & 1) != 0)
340 ++off;
341 }
342 }
343
344 // Include an archive member in the link. OFF is the file offset of
345 // the member header.
346
347 void
348 Archive::include_member(const General_options& options, Symbol_table* symtab,
349 Layout* layout, Input_objects* input_objects,
350 off_t off)
351 {
352 std::string n;
353 this->read_header(off, &n);
354
355 size_t memoff = off + sizeof(Archive_header);
356
357 // Read enough of the file to pick up the entire ELF header.
358 int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
359 off_t bytes;
360 const unsigned char* p = this->input_file_->file().get_view(memoff,
361 ehdr_size,
362 &bytes);
363 if (bytes < 4)
364 {
365 fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"),
366 program_name, this->name().c_str(),
367 static_cast<long>(off));
368 gold_exit(false);
369 }
370
371 static unsigned char elfmagic[4] =
372 {
373 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
374 elfcpp::ELFMAG2, elfcpp::ELFMAG3
375 };
376 if (memcmp(p, elfmagic, 4) != 0)
377 {
378 fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"),
379 program_name, this->name().c_str(),
380 static_cast<long>(off));
381 gold_exit(false);
382 }
383
384 Object* obj = make_elf_object((std::string(this->input_file_->filename())
385 + "(" + n + ")"),
386 this->input_file_, memoff, p, bytes);
387
388 input_objects->add_object(obj);
389
390 Read_symbols_data sd;
391 obj->read_symbols(&sd);
392 obj->layout(options, symtab, layout, &sd);
393 obj->add_symbols(symtab, &sd);
394 }
395
396 // Add_archive_symbols methods.
397
398 Add_archive_symbols::~Add_archive_symbols()
399 {
400 if (this->this_blocker_ != NULL)
401 delete this->this_blocker_;
402 // next_blocker_ is deleted by the task associated with the next
403 // input file.
404 }
405
406 // Return whether we can add the archive symbols. We are blocked by
407 // this_blocker_. We block next_blocker_. We also lock the file.
408
409 Task::Is_runnable_type
410 Add_archive_symbols::is_runnable(Workqueue*)
411 {
412 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
413 return IS_BLOCKED;
414 return IS_RUNNABLE;
415 }
416
417 class Add_archive_symbols::Add_archive_symbols_locker : public Task_locker
418 {
419 public:
420 Add_archive_symbols_locker(Task_token& token, Workqueue* workqueue,
421 File_read& file)
422 : blocker_(token, workqueue), filelock_(file)
423 { }
424
425 private:
426 Task_locker_block blocker_;
427 Task_locker_obj<File_read> filelock_;
428 };
429
430 Task_locker*
431 Add_archive_symbols::locks(Workqueue* workqueue)
432 {
433 return new Add_archive_symbols_locker(*this->next_blocker_,
434 workqueue,
435 this->archive_->file());
436 }
437
438 void
439 Add_archive_symbols::run(Workqueue*)
440 {
441 this->archive_->add_symbols(this->options_, this->symtab_, this->layout_,
442 this->input_objects_);
443
444 if (this->input_group_ != NULL)
445 this->input_group_->add_archive(this->archive_);
446 else
447 {
448 // We no longer need to know about this archive.
449 delete this->archive_;
450 }
451 }
452
453 } // End namespace gold.
This page took 0.040753 seconds and 5 git commands to generate.