1 // archive.cc -- archive support for gold
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.
24 struct Archive::Archive_header
28 // The file modification time.
30 // The user's UID in decimal.
32 // The user's GID in decimal.
34 // The file mode in octal.
36 // The file size in decimal.
38 // The final magic code.
44 const char Archive::armag[sarmag] =
46 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
49 const char Archive::arfmag[2] = { '`', '\n' };
51 // Set up the archive: read the symbol map and the extended name
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())
62 fprintf(stderr, _("%s: %s: no archive symbol table (run ranlib)\n"),
63 program_name, this->name().c_str());
67 // Read in the entire armap.
68 const unsigned char* p = this->get_view(sarmag + sizeof(Archive_header),
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::Swap<32, true>::readval(pword);
76 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
77 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
79 this->armap_.resize(nsyms);
81 for (unsigned int i = 0; i < nsyms; ++i)
83 this->armap_[i].name = pnames;
84 this->armap_[i].offset = elfcpp::Swap<32, true>::readval(pword);
85 pnames += strlen(pnames) + 1;
89 if (reinterpret_cast<const unsigned char*>(pnames) - p > armap_size)
91 fprintf(stderr, _("%s: %s: bad archive symbol table names\n"),
92 program_name, this->name().c_str());
96 // See if there is an extended name table.
97 off_t off = sarmag + sizeof(Archive_header) + armap_size;
101 off_t extended_size = this->read_header(off, &xname);
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);
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);
113 // Opening the file locked it. Unlock it now.
114 this->input_file_->file().unlock();
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
122 Archive::read_header(off_t off, std::string* pname)
124 const unsigned char* p = this->get_view(off, sizeof(Archive_header));
125 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
127 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
129 fprintf(stderr, _("%s; %s: malformed archive header at %ld\n"),
130 program_name, this->name().c_str(),
131 static_cast<long>(off));
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] == ' ')
145 off_t member_size = strtol(size_string, &end, 10);
148 || (member_size == LONG_MAX && errno == ERANGE))
150 fprintf(stderr, _("%s: %s: malformed archive header size at %ld\n"),
151 program_name, this->name().c_str(),
152 static_cast<long>(off));
156 if (hdr->ar_name[0] != '/')
158 const char* name_end = strchr(hdr->ar_name, '/');
160 || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name))
162 fprintf(stderr, _("%s: %s: malformed archive header name at %ld\n"),
163 program_name, this->name().c_str(),
164 static_cast<long>(off));
167 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
169 else if (hdr->ar_name[1] == ' ')
171 // This is the symbol table.
174 else if (hdr->ar_name[1] == '/')
176 // This is the extended name table.
177 pname->assign(1, '/');
182 long x = strtol(hdr->ar_name + 1, &end, 10);
185 || (x == LONG_MAX && errno == ERANGE)
186 || static_cast<size_t>(x) >= this->extended_names_.size())
188 fprintf(stderr, _("%s: %s: bad extended name index at %ld\n"),
189 program_name, this->name().c_str(),
190 static_cast<long>(off));
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')
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));
204 pname->assign(name, name_end - name);
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.
218 Archive::add_symbols(const General_options& options, Symbol_table* symtab,
219 Layout* layout, Input_objects* input_objects)
221 const size_t armap_size = this->armap_.size();
223 bool added_new_object;
226 added_new_object = false;
228 for (size_t i = 0; i < armap_size; ++i)
232 if (this->armap_[i].offset == last)
234 this->seen_[i] = true;
238 Symbol* sym = symtab->lookup(this->armap_[i].name);
241 else if (!sym->is_undefined())
243 this->seen_[i] = true;
246 else if (sym->binding() == elfcpp::STB_WEAK)
249 // We want to include this object in the link.
250 last = this->armap_[i].offset;
251 this->include_member(options, symtab, layout, input_objects, last);
252 this->seen_[i] = true;
253 added_new_object = true;
256 while (added_new_object);
259 // Include an archive member in the link. OFF is the file offset of
260 // the member header.
263 Archive::include_member(const General_options& options, Symbol_table* symtab,
264 Layout* layout, Input_objects* input_objects,
268 this->read_header(off, &n);
270 size_t memoff = off + sizeof(Archive_header);
272 // Read enough of the file to pick up the entire ELF header.
273 int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
275 const unsigned char* p = this->input_file_->file().get_view(memoff,
280 fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"),
281 program_name, this->name().c_str(),
282 static_cast<long>(off));
286 static unsigned char elfmagic[4] =
288 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
289 elfcpp::ELFMAG2, elfcpp::ELFMAG3
291 if (memcmp(p, elfmagic, 4) != 0)
293 fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"),
294 program_name, this->name().c_str(),
295 static_cast<long>(off));
299 Object* obj = make_elf_object((std::string(this->input_file_->filename())
301 this->input_file_, memoff, p, bytes);
303 input_objects->add_object(obj);
305 Read_symbols_data sd;
306 obj->read_symbols(&sd);
307 obj->layout(options, symtab, layout, &sd);
308 obj->add_symbols(symtab, &sd);
311 // Add_archive_symbols methods.
313 Add_archive_symbols::~Add_archive_symbols()
315 if (this->this_blocker_ != NULL)
316 delete this->this_blocker_;
317 // next_blocker_ is deleted by the task associated with the next
321 // Return whether we can add the archive symbols. We are blocked by
322 // this_blocker_. We block next_blocker_. We also lock the file.
324 Task::Is_runnable_type
325 Add_archive_symbols::is_runnable(Workqueue*)
327 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
332 class Add_archive_symbols::Add_archive_symbols_locker : public Task_locker
335 Add_archive_symbols_locker(Task_token& token, Workqueue* workqueue,
337 : blocker_(token, workqueue), filelock_(file)
341 Task_locker_block blocker_;
342 Task_locker_obj<File_read> filelock_;
346 Add_archive_symbols::locks(Workqueue* workqueue)
348 return new Add_archive_symbols_locker(*this->next_blocker_,
350 this->archive_->file());
354 Add_archive_symbols::run(Workqueue*)
356 this->archive_->add_symbols(this->options_, this->symtab_, this->layout_,
357 this->input_objects_);
359 if (this->input_group_ != NULL)
360 this->input_group_->add_archive(this->archive_);
363 // We no longer need to know about this archive.
364 delete this->archive_;
368 } // End namespace gold.