]>
Commit | Line | Data |
---|---|---|
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" | |
ead1e424 | 12 | #include "readsyms.h" |
61ba1cf9 ILT |
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 | ||
61ba1cf9 ILT |
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); | |
f6ce93d6 | 73 | unsigned int nsyms = elfcpp::Swap<32, true>::readval(pword); |
61ba1cf9 ILT |
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; | |
f6ce93d6 | 84 | this->armap_[i].offset = elfcpp::Swap<32, true>::readval(pword); |
61ba1cf9 ILT |
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 | ||
ead1e424 ILT |
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 | ||
61ba1cf9 ILT |
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 | |
f6ce93d6 ILT |
218 | Archive::add_symbols(const General_options& options, Symbol_table* symtab, |
219 | Layout* layout, Input_objects* input_objects) | |
61ba1cf9 | 220 | { |
ead1e424 | 221 | const size_t armap_size = this->armap_.size(); |
61ba1cf9 ILT |
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 | { | |
ead1e424 | 230 | if (this->seen_[i]) |
61ba1cf9 ILT |
231 | continue; |
232 | if (this->armap_[i].offset == last) | |
233 | { | |
ead1e424 | 234 | this->seen_[i] = true; |
61ba1cf9 ILT |
235 | continue; |
236 | } | |
237 | ||
238 | Symbol* sym = symtab->lookup(this->armap_[i].name); | |
239 | if (sym == NULL) | |
240 | continue; | |
ead1e424 | 241 | else if (!sym->is_undefined()) |
61ba1cf9 | 242 | { |
ead1e424 | 243 | this->seen_[i] = true; |
61ba1cf9 ILT |
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; | |
f6ce93d6 | 251 | this->include_member(options, symtab, layout, input_objects, last); |
ead1e424 | 252 | this->seen_[i] = true; |
61ba1cf9 ILT |
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 | |
f6ce93d6 ILT |
263 | Archive::include_member(const General_options& options, Symbol_table* symtab, |
264 | Layout* layout, Input_objects* input_objects, | |
265 | off_t off) | |
61ba1cf9 ILT |
266 | { |
267 | std::string n; | |
268 | this->read_header(off, &n); | |
269 | ||
270 | size_t memoff = off + sizeof(Archive_header); | |
271 | ||
272 | // Read enough of the file to pick up the entire ELF header. | |
273 | int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size; | |
274 | off_t bytes; | |
275 | const unsigned char* p = this->input_file_->file().get_view(memoff, | |
276 | ehdr_size, | |
277 | &bytes); | |
278 | if (bytes < 4) | |
279 | { | |
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)); | |
283 | gold_exit(false); | |
284 | } | |
285 | ||
286 | static unsigned char elfmagic[4] = | |
287 | { | |
288 | elfcpp::ELFMAG0, elfcpp::ELFMAG1, | |
289 | elfcpp::ELFMAG2, elfcpp::ELFMAG3 | |
290 | }; | |
291 | if (memcmp(p, elfmagic, 4) != 0) | |
292 | { | |
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)); | |
296 | gold_exit(false); | |
297 | } | |
298 | ||
92e059d8 | 299 | Object* obj = make_elf_object((std::string(this->input_file_->filename()) |
61ba1cf9 ILT |
300 | + "(" + n + ")"), |
301 | this->input_file_, memoff, p, bytes); | |
302 | ||
303 | input_objects->add_object(obj); | |
304 | ||
12e14209 ILT |
305 | Read_symbols_data sd; |
306 | obj->read_symbols(&sd); | |
f6ce93d6 | 307 | obj->layout(options, symtab, layout, &sd); |
12e14209 | 308 | obj->add_symbols(symtab, &sd); |
61ba1cf9 ILT |
309 | } |
310 | ||
311 | // Add_archive_symbols methods. | |
312 | ||
313 | Add_archive_symbols::~Add_archive_symbols() | |
314 | { | |
315 | if (this->this_blocker_ != NULL) | |
316 | delete this->this_blocker_; | |
317 | // next_blocker_ is deleted by the task associated with the next | |
318 | // input file. | |
319 | } | |
320 | ||
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. | |
323 | ||
324 | Task::Is_runnable_type | |
325 | Add_archive_symbols::is_runnable(Workqueue*) | |
326 | { | |
327 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
328 | return IS_BLOCKED; | |
329 | return IS_RUNNABLE; | |
330 | } | |
331 | ||
332 | class Add_archive_symbols::Add_archive_symbols_locker : public Task_locker | |
333 | { | |
334 | public: | |
335 | Add_archive_symbols_locker(Task_token& token, Workqueue* workqueue, | |
ead1e424 ILT |
336 | File_read& file) |
337 | : blocker_(token, workqueue), filelock_(file) | |
61ba1cf9 ILT |
338 | { } |
339 | ||
340 | private: | |
341 | Task_locker_block blocker_; | |
ead1e424 | 342 | Task_locker_obj<File_read> filelock_; |
61ba1cf9 ILT |
343 | }; |
344 | ||
345 | Task_locker* | |
346 | Add_archive_symbols::locks(Workqueue* workqueue) | |
347 | { | |
348 | return new Add_archive_symbols_locker(*this->next_blocker_, | |
349 | workqueue, | |
ead1e424 | 350 | this->archive_->file()); |
61ba1cf9 ILT |
351 | } |
352 | ||
353 | void | |
354 | Add_archive_symbols::run(Workqueue*) | |
355 | { | |
f6ce93d6 | 356 | this->archive_->add_symbols(this->options_, this->symtab_, this->layout_, |
12e14209 | 357 | this->input_objects_); |
ead1e424 ILT |
358 | |
359 | if (this->input_group_ != NULL) | |
360 | this->input_group_->add_archive(this->archive_); | |
361 | else | |
362 | { | |
363 | // We no longer need to know about this archive. | |
364 | delete this->archive_; | |
365 | } | |
61ba1cf9 ILT |
366 | } |
367 | ||
368 | } // End namespace gold. |