Fix typo in comment.
[deliverable/binutils-gdb.git] / gold / archive.cc
CommitLineData
61ba1cf9
ILT
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 "symtab.h"
13#include "object.h"
14#include "archive.h"
15
16namespace gold
17{
18
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.
22
23struct Archive::Archive_header
24{
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];
39};
40
41// Archive methods.
42
43const char Archive::armag[sarmag] =
44{
45 '!', '<', 'a', 'r', 'c', 'h', '>', '\n'
46};
47
48const char Archive::arfmag[2] = { '`', '\n' };
49
50// Get a view into the underlying file.
51
52const unsigned char*
53Archive::get_view(off_t start, off_t size)
54{
55 return this->input_file_->file().get_view(start, size);
56}
57
58// Set up the archive: read the symbol map and the extended name
59// table.
60
61void
62Archive::setup()
63{
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())
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
74 // Read in the entire armap.
75 const unsigned char* p = this->get_view(sarmag + sizeof(Archive_header),
76 armap_size);
77
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;
82
83 // Note that the addition is in units of sizeof(elfcpp::Elf_Word).
84 const char* pnames = reinterpret_cast<const char*>(pword + nsyms);
85
86 this->armap_.resize(nsyms);
87
88 for (unsigned int i = 0; i < nsyms; ++i)
89 {
90 this->armap_[i].name = pnames;
91 this->armap_[i].offset = elfcpp::read_elf_word<true>(pword);
92 pnames += strlen(pnames) + 1;
93 ++pword;
94 }
95
96 if (reinterpret_cast<const unsigned char*>(pnames) - p > armap_size)
97 {
98 fprintf(stderr, _("%s: %s: bad archive symbol table names\n"),
99 program_name, this->name().c_str());
100 gold_exit(false);
101 }
102
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 == "/")
110 {
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);
114 }
115
116 // Opening the file locked it. Unlock it now.
117 this->input_file_->file().unlock();
118}
119
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.
123
124off_t
125Archive::read_header(off_t off, std::string* pname)
126{
127 const unsigned char* p = this->get_view(off, sizeof(Archive_header));
128 const Archive_header* hdr = reinterpret_cast<const Archive_header*>(p);
129
130 if (memcmp(hdr->ar_fmag, arfmag, sizeof arfmag) != 0)
131 {
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);
136 }
137
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';
145
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))
152 {
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);
157 }
158
159 if (hdr->ar_name[0] != '/')
160 {
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))
164 {
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);
169 }
170 pname->assign(hdr->ar_name, name_end - hdr->ar_name);
171 }
172 else if (hdr->ar_name[1] == ' ')
173 {
174 // This is the symbol table.
175 pname->clear();
176 }
177 else if (hdr->ar_name[1] == '/')
178 {
179 // This is the extended name table.
180 pname->assign(1, '/');
181 }
182 else
183 {
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())
190 {
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);
195 }
196
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')
201 {
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);
206 }
207 pname->assign(name, name_end - name);
208 }
209
210 return member_size;
211}
212
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.
219
220void
12e14209
ILT
221Archive::add_symbols(Symbol_table* symtab, Layout* layout,
222 Input_objects* input_objects)
61ba1cf9
ILT
223{
224 size_t armap_size = this->armap_.size();
225 std::vector<bool> seen;
226 seen.resize(this->armap_.size());
227 seen.clear();
228
229 bool added_new_object;
230 do
231 {
232 added_new_object = false;
233 off_t last = -1;
234 for (size_t i = 0; i < armap_size; ++i)
235 {
236 if (seen[i])
237 continue;
238 if (this->armap_[i].offset == last)
239 {
240 seen[i] = true;
241 continue;
242 }
243
244 Symbol* sym = symtab->lookup(this->armap_[i].name);
245 if (sym == NULL)
246 continue;
247 else if (sym->shnum() != elfcpp::SHN_UNDEF)
248 {
249 seen[i] = true;
250 continue;
251 }
252 else if (sym->binding() == elfcpp::STB_WEAK)
253 continue;
254
255 // We want to include this object in the link.
256 last = this->armap_[i].offset;
12e14209 257 this->include_member(symtab, layout, input_objects, last);
61ba1cf9
ILT
258 added_new_object = true;
259 }
260 }
261 while (added_new_object);
262}
263
264// Include an archive member in the link. OFF is the file offset of
265// the member header.
266
267void
12e14209
ILT
268Archive::include_member(Symbol_table* symtab, Layout* layout,
269 Input_objects* input_objects, off_t off)
61ba1cf9
ILT
270{
271 std::string n;
272 this->read_header(off, &n);
273
274 size_t memoff = off + sizeof(Archive_header);
275
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)
283 {
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);
288 }
289
290 static unsigned char elfmagic[4] =
291 {
292 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
293 elfcpp::ELFMAG2, elfcpp::ELFMAG3
294 };
295 if (memcmp(p, elfmagic, 4) != 0)
296 {
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);
301 }
302
92e059d8 303 Object* obj = make_elf_object((std::string(this->input_file_->filename())
61ba1cf9
ILT
304 + "(" + n + ")"),
305 this->input_file_, memoff, p, bytes);
306
307 input_objects->add_object(obj);
308
12e14209
ILT
309 Read_symbols_data sd;
310 obj->read_symbols(&sd);
311 obj->layout(layout, &sd);
312 obj->add_symbols(symtab, &sd);
61ba1cf9
ILT
313}
314
315// Add_archive_symbols methods.
316
317Add_archive_symbols::~Add_archive_symbols()
318{
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.
323}
324
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.
327
328Task::Is_runnable_type
329Add_archive_symbols::is_runnable(Workqueue*)
330{
331 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
332 return IS_BLOCKED;
333 return IS_RUNNABLE;
334}
335
336class Add_archive_symbols::Add_archive_symbols_locker : public Task_locker
337{
338 public:
339 Add_archive_symbols_locker(Task_token& token, Workqueue* workqueue,
340 Archive* archive)
341 : blocker_(token, workqueue), archlock_(*archive)
342 { }
343
344 private:
345 Task_locker_block blocker_;
346 Task_locker_obj<Archive> archlock_;
347};
348
349Task_locker*
350Add_archive_symbols::locks(Workqueue* workqueue)
351{
352 return new Add_archive_symbols_locker(*this->next_blocker_,
353 workqueue,
354 this->archive_);
355}
356
357void
358Add_archive_symbols::run(Workqueue*)
359{
12e14209
ILT
360 this->archive_->add_symbols(this->symtab_, this->layout_,
361 this->input_objects_);
61ba1cf9
ILT
362}
363
364} // End namespace gold.
This page took 0.038083 seconds and 4 git commands to generate.