1 // archive.cc -- archive support for gold
19 // The header of an entry in the archive. This is all readable text,
20 // padded with spaces where necesary. If the contents of an archive
21 // are all text file, the entire archive is readable.
23 struct Archive::Archive_header
27 // The file modification time.
29 // The user's UID in decimal.
31 // The user's GID in decimal.
33 // The file mode in octal.
35 // The file size in decimal.
37 // The final magic code.
43 const char Archive::armag[sarmag] =
45 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
48 const char Archive::arfmag[2] = { '`', '\n' };
50 // Get a view into the underlying file.
53 Archive::get_view(off_t start, off_t size)
55 return this->input_file_->file().get_view(start, size);
58 // Set up the archive: read the symbol map and the extended name
64 // The first member of the archive should be the symbol table.
65 std::string armap_name;
66 off_t armap_size = this->read_header(sarmag, &armap_name);
67 if (!armap_name.empty())
69 fprintf(stderr, _("%s: %s: no archive symbol table (run ranlib)\n"),
70 program_name, this->name().c_str());
74 // Read in the entire armap.
75 const unsigned char* p = this->get_view(sarmag + sizeof(Archive_header),
78 // Numbers in the armap are always big-endian.
79 const elfcpp::Elf_Word* pword = reinterpret_cast<const elfcpp::Elf_Word*>(p);
80 unsigned int nsyms = elfcpp::read_elf_word<true>(pword);
83 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
84 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
86 this->armap_.resize(nsyms);
88 for (unsigned int i = 0; i < nsyms; ++i)
90 this->armap_[i].name = pnames;
91 this->armap_[i].offset = elfcpp::read_elf_word<true>(pword);
92 pnames += strlen(pnames) + 1;
96 if (reinterpret_cast<const unsigned char*>(pnames) - p > armap_size)
98 fprintf(stderr, _("%s: %s: bad archive symbol table names\n"),
99 program_name, this->name().c_str());
103 // See if there is an extended name table.
104 off_t off = sarmag + sizeof(Archive_header) + armap_size;
108 off_t extended_size = this->read_header(off, &xname);
111 p = this->get_view(off + sizeof(Archive_header), extended_size);
112 const char* px = reinterpret_cast<const char*>(p);
113 this->extended_names_.assign(px, extended_size);
116 // Opening the file locked it. Unlock it now.
117 this->input_file_->file().unlock();
120 // Read the header of an archive member at OFF. Fail if something
121 // goes wrong. Return the size of the member. Set *PNAME to the name
125 Archive::read_header(off_t off, std::string* pname)
127 const unsigned char* p = this->get_view(off, sizeof(Archive_header));
128 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
130 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
132 fprintf(stderr, _("%s; %s: malformed archive header at %ld\n"),
133 program_name, this->name().c_str(),
134 static_cast<long>(off));
138 const int size_string_size = sizeof hdr->ar_size;
139 char size_string[size_string_size + 1];
140 memcpy(size_string, hdr->ar_size, size_string_size);
141 char* ps = size_string + size_string_size;
142 while (ps[-1] == ' ')
148 off_t member_size = strtol(size_string, &end, 10);
151 || (member_size == LONG_MAX && errno == ERANGE))
153 fprintf(stderr, _("%s: %s: malformed archive header size at %ld\n"),
154 program_name, this->name().c_str(),
155 static_cast<long>(off));
159 if (hdr->ar_name[0] != '/')
161 const char* name_end = strchr(hdr->ar_name, '/');
163 || name_end - hdr->ar_name >= static_cast<int>(sizeof hdr->ar_name))
165 fprintf(stderr, _("%s: %s: malformed archive header name at %ld\n"),
166 program_name, this->name().c_str(),
167 static_cast<long>(off));
170 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
172 else if (hdr->ar_name[1] == ' ')
174 // This is the symbol table.
177 else if (hdr->ar_name[1] == '/')
179 // This is the extended name table.
180 pname->assign(1, '/');
185 long x = strtol(hdr->ar_name + 1, &end, 10);
188 || (x == LONG_MAX && errno == ERANGE)
189 || static_cast<size_t>(x) >= this->extended_names_.size())
191 fprintf(stderr, _("%s: %s: bad extended name index at %ld\n"),
192 program_name, this->name().c_str(),
193 static_cast<long>(off));
197 const char* name = this->extended_names_.data() + x;
198 const char* name_end = strchr(name, '/');
199 if (static_cast<size_t>(name_end - name) > this->extended_names_.size()
200 || name_end[1] != '\n')
202 fprintf(stderr, _("%s: %s: bad extended name entry at header %ld\n"),
203 program_name, this->name().c_str(),
204 static_cast<long>(off));
207 pname->assign(name, name_end - name);
213 // Select members from the archive and add them to the link. We walk
214 // through the elements in the archive map, and look each one up in
215 // the symbol table. If it exists as a strong undefined symbol, we
216 // pull in the corresponding element. We have to do this in a loop,
217 // since pulling in one element may create new undefined symbols which
218 // may be satisfied by other objects in the archive.
221 Archive::add_symbols(Symbol_table* symtab, Layout* layout,
222 Input_objects* input_objects)
224 size_t armap_size = this->armap_.size();
225 std::vector<bool> seen;
226 seen.resize(this->armap_.size());
229 bool added_new_object;
232 added_new_object = false;
234 for (size_t i = 0; i < armap_size; ++i)
238 if (this->armap_[i].offset == last)
244 Symbol* sym = symtab->lookup(this->armap_[i].name);
247 else if (sym->shnum() != elfcpp::SHN_UNDEF)
252 else if (sym->binding() == elfcpp::STB_WEAK)
255 // We want to include this object in the link.
256 last = this->armap_[i].offset;
257 this->include_member(symtab, layout, input_objects, last);
258 added_new_object = true;
261 while (added_new_object);
264 // Include an archive member in the link. OFF is the file offset of
265 // the member header.
268 Archive::include_member(Symbol_table* symtab, Layout* layout,
269 Input_objects* input_objects, off_t off)
272 this->read_header(off, &n);
274 size_t memoff = off + sizeof(Archive_header);
276 // Read enough of the file to pick up the entire ELF header.
277 int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
279 const unsigned char* p = this->input_file_->file().get_view(memoff,
284 fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"),
285 program_name, this->name().c_str(),
286 static_cast<long>(off));
290 static unsigned char elfmagic[4] =
292 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
293 elfcpp::ELFMAG2, elfcpp::ELFMAG3
295 if (memcmp(p, elfmagic, 4) != 0)
297 fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"),
298 program_name, this->name().c_str(),
299 static_cast<long>(off));
303 Object* obj = make_elf_object((std::string(this->input_file_->filename())
305 this->input_file_, memoff, p, bytes);
307 input_objects->add_object(obj);
309 Read_symbols_data sd;
310 obj->read_symbols(&sd);
311 obj->layout(layout, &sd);
312 obj->add_symbols(symtab, &sd);
315 // Add_archive_symbols methods.
317 Add_archive_symbols::~Add_archive_symbols()
319 if (this->this_blocker_ != NULL)
320 delete this->this_blocker_;
321 // next_blocker_ is deleted by the task associated with the next
325 // Return whether we can add the archive symbols. We are blocked by
326 // this_blocker_. We block next_blocker_. We also lock the file.
328 Task::Is_runnable_type
329 Add_archive_symbols::is_runnable(Workqueue*)
331 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
336 class Add_archive_symbols::Add_archive_symbols_locker : public Task_locker
339 Add_archive_symbols_locker(Task_token& token, Workqueue* workqueue,
341 : blocker_(token, workqueue), archlock_(*archive)
345 Task_locker_block blocker_;
346 Task_locker_obj<Archive> archlock_;
350 Add_archive_symbols::locks(Workqueue* workqueue)
352 return new Add_archive_symbols_locker(*this->next_blocker_,
358 Add_archive_symbols::run(Workqueue*)
360 this->archive_->add_symbols(this->symtab_, this->layout_,
361 this->input_objects_);
364 } // End namespace gold.