OSDN Git Service

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