ld/
[binutils.git] / gold / archive.cc
blob86396439678ffa734c85b1e8b261dd5659422ebc
1 // archive.cc -- archive support for gold
3 #include "gold.h"
5 #include <cerrno>
6 #include <cstring>
7 #include <climits>
8 #include <vector>
10 #include "elfcpp.h"
11 #include "fileread.h"
12 #include "symtab.h"
13 #include "object.h"
14 #include "archive.h"
16 namespace 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
25 // The entry name.
26 char ar_name[16];
27 // The file modification time.
28 char ar_date[12];
29 // The user's UID in decimal.
30 char ar_uid[6];
31 // The user's GID in decimal.
32 char ar_gid[6];
33 // The file mode in octal.
34 char ar_mode[8];
35 // The file size in decimal.
36 char ar_size[10];
37 // The final magic code.
38 char ar_fmag[2];
41 // Archive methods.
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.
52 const unsigned char*
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
59 // table.
61 void
62 Archive::setup()
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());
71 gold_exit(false);
74 // Read in the entire armap.
75 const unsigned char* p = this->get_view(sarmag + sizeof(Archive_header),
76 armap_size);
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);
81 ++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;
93 ++pword;
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());
100 gold_exit(false);
103 // See if there is an extended name table.
104 off_t off = sarmag + sizeof(Archive_header) + armap_size;
105 if ((off & 1) != 0)
106 ++off;
107 std::string xname;
108 off_t extended_size = this->read_header(off, &xname);
109 if (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
122 // of the member.
124 off_t
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));
135 gold_exit(false);
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] == ' ')
143 --ps;
144 *ps = '\0';
146 errno = 0;
147 char* end;
148 off_t member_size = strtol(size_string, &end, 10);
149 if (*end != '\0'
150 || member_size < 0
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));
156 gold_exit(false);
159 if (hdr->ar_name[0] != '/')
161 const char* name_end = strchr(hdr->ar_name, '/');
162 if (name_end == NULL
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));
168 gold_exit(false);
170 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
172 else if (hdr->ar_name[1] == ' ')
174 // This is the symbol table.
175 pname->clear();
177 else if (hdr->ar_name[1] == '/')
179 // This is the extended name table.
180 pname->assign(1, '/');
182 else
184 errno = 0;
185 long x = strtol(hdr->ar_name + 1, &end, 10);
186 if (*end != ' '
187 || x < 0
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));
194 gold_exit(false);
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));
205 gold_exit(false);
207 pname->assign(name, name_end - name);
210 return member_size;
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.
220 void
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());
227 seen.clear();
229 bool added_new_object;
232 added_new_object = false;
233 off_t last = -1;
234 for (size_t i = 0; i < armap_size; ++i)
236 if (seen[i])
237 continue;
238 if (this->armap_[i].offset == last)
240 seen[i] = true;
241 continue;
244 Symbol* sym = symtab->lookup(this->armap_[i].name);
245 if (sym == NULL)
246 continue;
247 else if (sym->shnum() != elfcpp::SHN_UNDEF)
249 seen[i] = true;
250 continue;
252 else if (sym->binding() == elfcpp::STB_WEAK)
253 continue;
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.
267 void
268 Archive::include_member(Symbol_table* symtab, Layout* layout,
269 Input_objects* input_objects, off_t off)
271 std::string n;
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;
278 off_t bytes;
279 const unsigned char* p = this->input_file_->file().get_view(memoff,
280 ehdr_size,
281 &bytes);
282 if (bytes < 4)
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));
287 gold_exit(false);
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));
300 gold_exit(false);
303 Object* obj = make_elf_object((std::string(this->input_file_->name())
304 + "(" + n + ")"),
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
322 // input file.
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())
332 return IS_BLOCKED;
333 return IS_RUNNABLE;
336 class Add_archive_symbols::Add_archive_symbols_locker : public Task_locker
338 public:
339 Add_archive_symbols_locker(Task_token& token, Workqueue* workqueue,
340 Archive* archive)
341 : blocker_(token, workqueue), archlock_(*archive)
344 private:
345 Task_locker_block blocker_;
346 Task_locker_obj<Archive> archlock_;
349 Task_locker*
350 Add_archive_symbols::locks(Workqueue* workqueue)
352 return new Add_archive_symbols_locker(*this->next_blocker_,
353 workqueue,
354 this->archive_);
357 void
358 Add_archive_symbols::run(Workqueue*)
360 this->archive_->add_symbols(this->symtab_, this->layout_,
361 this->input_objects_);
364 } // End namespace gold.