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" | |
12 | #include "symtab.h" | |
13 | #include "object.h" | |
14 | #include "archive.h" | |
15 | ||
16 | namespace 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 | ||
23 | struct 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 | ||
43 | const char Archive::armag[sarmag] = | |
44 | { | |
45 | '!', '<', 'a', 'r', 'c', 'h', '>', '\n' | |
46 | }; | |
47 | ||
48 | const char Archive::arfmag[2] = { '`', '\n' }; | |
49 | ||
50 | // Get a view into the underlying file. | |
51 | ||
52 | const unsigned char* | |
53 | Archive::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 | ||
61 | void | |
62 | Archive::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 | ||
124 | off_t | |
125 | Archive::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 | ||
220 | void | |
221 | Archive::add_symbols(Symbol_table* symtab, Input_objects* input_objects) | |
222 | { | |
223 | size_t armap_size = this->armap_.size(); | |
224 | std::vector<bool> seen; | |
225 | seen.resize(this->armap_.size()); | |
226 | seen.clear(); | |
227 | ||
228 | bool added_new_object; | |
229 | do | |
230 | { | |
231 | added_new_object = false; | |
232 | off_t last = -1; | |
233 | for (size_t i = 0; i < armap_size; ++i) | |
234 | { | |
235 | if (seen[i]) | |
236 | continue; | |
237 | if (this->armap_[i].offset == last) | |
238 | { | |
239 | seen[i] = true; | |
240 | continue; | |
241 | } | |
242 | ||
243 | Symbol* sym = symtab->lookup(this->armap_[i].name); | |
244 | if (sym == NULL) | |
245 | continue; | |
246 | else if (sym->shnum() != elfcpp::SHN_UNDEF) | |
247 | { | |
248 | seen[i] = true; | |
249 | continue; | |
250 | } | |
251 | else if (sym->binding() == elfcpp::STB_WEAK) | |
252 | continue; | |
253 | ||
254 | // We want to include this object in the link. | |
255 | last = this->armap_[i].offset; | |
256 | this->include_member(symtab, input_objects, last); | |
257 | added_new_object = true; | |
258 | } | |
259 | } | |
260 | while (added_new_object); | |
261 | } | |
262 | ||
263 | // Include an archive member in the link. OFF is the file offset of | |
264 | // the member header. | |
265 | ||
266 | void | |
267 | Archive::include_member(Symbol_table* symtab, Input_objects* input_objects, | |
268 | off_t off) | |
269 | { | |
270 | std::string n; | |
271 | this->read_header(off, &n); | |
272 | ||
273 | size_t memoff = off + sizeof(Archive_header); | |
274 | ||
275 | // Read enough of the file to pick up the entire ELF header. | |
276 | int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size; | |
277 | off_t bytes; | |
278 | const unsigned char* p = this->input_file_->file().get_view(memoff, | |
279 | ehdr_size, | |
280 | &bytes); | |
281 | if (bytes < 4) | |
282 | { | |
283 | fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"), | |
284 | program_name, this->name().c_str(), | |
285 | static_cast<long>(off)); | |
286 | gold_exit(false); | |
287 | } | |
288 | ||
289 | static unsigned char elfmagic[4] = | |
290 | { | |
291 | elfcpp::ELFMAG0, elfcpp::ELFMAG1, | |
292 | elfcpp::ELFMAG2, elfcpp::ELFMAG3 | |
293 | }; | |
294 | if (memcmp(p, elfmagic, 4) != 0) | |
295 | { | |
296 | fprintf(stderr, _("%s: %s: member at %ld is not an ELF object"), | |
297 | program_name, this->name().c_str(), | |
298 | static_cast<long>(off)); | |
299 | gold_exit(false); | |
300 | } | |
301 | ||
302 | Object* obj = make_elf_object((std::string(this->input_file_->name()) | |
303 | + "(" + n + ")"), | |
304 | this->input_file_, memoff, p, bytes); | |
305 | ||
306 | input_objects->add_object(obj); | |
307 | ||
308 | Read_symbols_data sd = obj->read_symbols(); | |
309 | obj->add_symbols(symtab, sd); | |
310 | } | |
311 | ||
312 | // Add_archive_symbols methods. | |
313 | ||
314 | Add_archive_symbols::~Add_archive_symbols() | |
315 | { | |
316 | if (this->this_blocker_ != NULL) | |
317 | delete this->this_blocker_; | |
318 | // next_blocker_ is deleted by the task associated with the next | |
319 | // input file. | |
320 | } | |
321 | ||
322 | // Return whether we can add the archive symbols. We are blocked by | |
323 | // this_blocker_. We block next_blocker_. We also lock the file. | |
324 | ||
325 | Task::Is_runnable_type | |
326 | Add_archive_symbols::is_runnable(Workqueue*) | |
327 | { | |
328 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
329 | return IS_BLOCKED; | |
330 | return IS_RUNNABLE; | |
331 | } | |
332 | ||
333 | class Add_archive_symbols::Add_archive_symbols_locker : public Task_locker | |
334 | { | |
335 | public: | |
336 | Add_archive_symbols_locker(Task_token& token, Workqueue* workqueue, | |
337 | Archive* archive) | |
338 | : blocker_(token, workqueue), archlock_(*archive) | |
339 | { } | |
340 | ||
341 | private: | |
342 | Task_locker_block blocker_; | |
343 | Task_locker_obj<Archive> archlock_; | |
344 | }; | |
345 | ||
346 | Task_locker* | |
347 | Add_archive_symbols::locks(Workqueue* workqueue) | |
348 | { | |
349 | return new Add_archive_symbols_locker(*this->next_blocker_, | |
350 | workqueue, | |
351 | this->archive_); | |
352 | } | |
353 | ||
354 | void | |
355 | Add_archive_symbols::run(Workqueue*) | |
356 | { | |
357 | this->archive_->add_symbols(this->symtab_, this->input_objects_); | |
358 | } | |
359 | ||
360 | } // End namespace gold. |