Can now do a full static link of hello, world in C or C++
[external/binutils.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   if (!armap_name.empty())
61     {
62       fprintf(stderr, _("%s: %s: no archive symbol table (run ranlib)\n"),
63               program_name, this->name().c_str());
64       gold_exit(false);
65     }
66
67   // Read in the entire armap.
68   const unsigned char* p = this->get_view(sarmag + sizeof(Archive_header),
69                                           armap_size);
70
71   // Numbers in the armap are always big-endian.
72   const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p);
73   unsigned int nsyms = elfcpp::read_elf_word<true>(pword);
74   ++pword;
75
76   // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
77   const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
78
79   this->armap_.resize(nsyms);
80
81   for (unsigned int i = 0; i < nsyms; ++i)
82     {
83       this->armap_[i].name = pnames;
84       this->armap_[i].offset = elfcpp::read_elf_word<true>(pword);
85       pnames += strlen(pnames) + 1;
86       ++pword;
87     }
88
89   if (reinterpret_cast<const unsigned char*>(pnames) - p > armap_size)
90     {
91       fprintf(stderr, _("%s: %s: bad archive symbol table names\n"),
92               program_name, this->name().c_str());
93       gold_exit(false);
94     }
95
96   // See if there is an extended name table.
97   off_t off = sarmag + sizeof(Archive_header) + armap_size;
98   if ((off & 1) != 0)
99     ++off;
100   std::string xname;
101   off_t extended_size = this->read_header(off, &xname);
102   if (xname == "/")
103     {
104       p = this->get_view(off + sizeof(Archive_header), extended_size);
105       const char* px = reinterpret_cast<const char*>(p);
106       this->extended_names_.assign(px, extended_size);
107     }
108
109   // This array keeps track of which symbols are for archive elements
110   // which we have already included in the link.
111   this->seen_.resize(nsyms);
112
113   // Opening the file locked it.  Unlock it now.
114   this->input_file_->file().unlock();
115 }
116
117 // Read the header of an archive member at OFF.  Fail if something
118 // goes wrong.  Return the size of the member.  Set *PNAME to the name
119 // of the member.
120
121 off_t
122 Archive::read_header(off_t off, std::string* pname)
123 {
124   const unsigned char* p = this->get_view(off, sizeof(Archive_header));
125   const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
126
127   if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
128     {
129       fprintf(stderr, _("%s; %s: malformed archive header at %ld\n"),
130               program_name, this->name().c_str(),
131               static_cast<long>(off));
132       gold_exit(false);
133     }
134
135   const int size_string_size = sizeof hdr->ar_size;
136   char size_string[size_string_size + 1];
137   memcpy(size_string, hdr->ar_size, size_string_size);
138   char* ps = size_string + size_string_size;
139   while (ps[-1] == ' ')
140     --ps;
141   *ps = '\0';
142
143   errno = 0;
144   char* end;
145   off_t member_size = strtol(size_string, &end, 10);
146   if (*end != '\0'
147       || member_size < 0
148       || (member_size == LONG_MAX && errno == ERANGE))
149     {
150       fprintf(stderr, _("%s: %s: malformed archive header size at %ld\n"),
151               program_name, this->name().c_str(),
152               static_cast<long>(off));
153       gold_exit(false);
154     }
155
156   if (hdr->ar_name[0] != '/')
157     {
158       const char* name_end = strchr(hdr->ar_name, '/');
159       if (name_end == NULL
160           || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name))
161         {
162           fprintf(stderr, _("%s: %s: malformed archive header name at %ld\n"),
163                   program_name, this->name().c_str(),
164                   static_cast<long>(off));
165           gold_exit(false);
166         }
167       pname->assign(hdr->ar_name, name_end - hdr->ar_name);
168     }
169   else if (hdr->ar_name[1] == ' ')
170     {
171       // This is the symbol table.
172       pname->clear();
173     }
174   else if (hdr->ar_name[1] == '/')
175     {
176       // This is the extended name table.
177       pname->assign(1, '/');
178     }
179   else
180     {
181       errno = 0;
182       long x = strtol(hdr->ar_name + 1, &end, 10);
183       if (*end != ' '
184           || x < 0
185           || (x == LONG_MAX && errno == ERANGE)
186           || static_cast<size_t>(x) >= this->extended_names_.size())
187         {
188           fprintf(stderr, _("%s: %s: bad extended name index at %ld\n"),
189                   program_name, this->name().c_str(),
190                   static_cast<long>(off));
191           gold_exit(false);
192         }
193
194       const char* name = this->extended_names_.data() + x;
195       const char* name_end = strchr(name, '/');
196       if (static_cast<size_t>(name_end - name) > this->extended_names_.size()
197           || name_end[1] != '\n')
198         {
199           fprintf(stderr, _("%s: %s: bad extended name entry at header %ld\n"),
200                   program_name, this->name().c_str(),
201                   static_cast<long>(off));
202           gold_exit(false);
203         }
204       pname->assign(name, name_end - name);
205     }
206
207   return member_size;
208 }
209
210 // Select members from the archive and add them to the link.  We walk
211 // through the elements in the archive map, and look each one up in
212 // the symbol table.  If it exists as a strong undefined symbol, we
213 // pull in the corresponding element.  We have to do this in a loop,
214 // since pulling in one element may create new undefined symbols which
215 // may be satisfied by other objects in the archive.
216
217 void
218 Archive::add_symbols(Symbol_table* symtab, Layout* layout,
219                      Input_objects* input_objects)
220 {
221   const size_t armap_size = this->armap_.size();
222
223   bool added_new_object;
224   do
225     {
226       added_new_object = false;
227       off_t last = -1;
228       for (size_t i = 0; i < armap_size; ++i)
229         {
230           if (this->seen_[i])
231             continue;
232           if (this->armap_[i].offset == last)
233             {
234               this->seen_[i] = true;
235               continue;
236             }
237
238           Symbol* sym = symtab->lookup(this->armap_[i].name);
239           if (sym == NULL)
240             continue;
241           else if (!sym->is_undefined())
242             {
243               this->seen_[i] = true;
244               continue;
245             }
246           else if (sym->binding() == elfcpp::STB_WEAK)
247             continue;
248
249           // We want to include this object in the link.
250           last = this->armap_[i].offset;
251           this->include_member(symtab, layout, input_objects, last);
252           this->seen_[i] = true;
253           added_new_object = true;
254         }
255     }
256   while (added_new_object);
257 }
258
259 // Include an archive member in the link.  OFF is the file offset of
260 // the member header.
261
262 void
263 Archive::include_member(Symbol_table* symtab, Layout* layout,
264                         Input_objects* input_objects, off_t off)
265 {
266   std::string n;
267   this->read_header(off, &n);
268
269   size_t memoff = off + sizeof(Archive_header);
270
271   // Read enough of the file to pick up the entire ELF header.
272   int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
273   off_t bytes;
274   const unsigned char* p = this->input_file_->file().get_view(memoff,
275                                                               ehdr_size,
276                                                               &bytes);
277   if (bytes < 4)
278     {
279       fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"),
280               program_name, this->name().c_str(),
281               static_cast<long>(off));
282       gold_exit(false);
283     }
284
285   static unsigned char elfmagic[4] =
286     {
287       elfcpp::ELFMAG0, elfcpp::ELFMAG1,
288       elfcpp::ELFMAG2, elfcpp::ELFMAG3
289     };
290   if (memcmp(p, elfmagic, 4) != 0)
291     {
292       fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"),
293               program_name, this->name().c_str(),
294               static_cast<long>(off));
295       gold_exit(false);
296     }
297
298   Object* obj = make_elf_object((std::string(this->input_file_->filename())
299                                  + "(" + n + ")"),
300                                 this->input_file_, memoff, p, bytes);
301
302   input_objects->add_object(obj);
303
304   Read_symbols_data sd;
305   obj->read_symbols(&sd);
306   obj->layout(layout, &sd);
307   obj->add_symbols(symtab, &sd);
308 }
309
310 // Add_archive_symbols methods.
311
312 Add_archive_symbols::~Add_archive_symbols()
313 {
314   if (this->this_blocker_ != NULL)
315     delete this->this_blocker_;
316   // next_blocker_ is deleted by the task associated with the next
317   // input file.
318 }
319
320 // Return whether we can add the archive symbols.  We are blocked by
321 // this_blocker_.  We block next_blocker_.  We also lock the file.
322
323 Task::Is_runnable_type
324 Add_archive_symbols::is_runnable(Workqueue*)
325 {
326   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
327     return IS_BLOCKED;
328   return IS_RUNNABLE;
329 }
330
331 class Add_archive_symbols::Add_archive_symbols_locker : public Task_locker
332 {
333  public:
334   Add_archive_symbols_locker(Task_token& token, Workqueue* workqueue,
335                              File_read& file)
336     : blocker_(token, workqueue), filelock_(file)
337   { }
338
339  private:
340   Task_locker_block blocker_;
341   Task_locker_obj<File_read> filelock_;                      
342 };
343
344 Task_locker*
345 Add_archive_symbols::locks(Workqueue* workqueue)
346 {
347   return new Add_archive_symbols_locker(*this->next_blocker_,
348                                         workqueue,
349                                         this->archive_->file());
350 }
351
352 void
353 Add_archive_symbols::run(Workqueue*)
354 {
355   this->archive_->add_symbols(this->symtab_, this->layout_,
356                               this->input_objects_);
357
358   if (this->input_group_ != NULL)
359     this->input_group_->add_archive(this->archive_);
360   else
361     {
362       // We no longer need to know about this archive.
363       delete this->archive_;
364     }
365 }
366
367 } // End namespace gold.