blob: eef6fe0b1c1d7fa0696f1f6de7470c713a22f2d2 [file] [log] [blame]
Chris Lattner5b183222007-05-06 19:49:28 +00001//===-- ArchiveReader.cpp - Read LLVM archive files -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner5b183222007-05-06 19:49:28 +00007//
8//===----------------------------------------------------------------------===//
9//
Gabor Greife16561c2007-07-05 17:07:56 +000010// Builds up standard unix archive files (.a) containing LLVM bitcode.
Chris Lattner5b183222007-05-06 19:49:28 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "ArchiveInternals.h"
15#include "llvm/Bitcode/ReaderWriter.h"
16#include "llvm/Support/MemoryBuffer.h"
17#include "llvm/Module.h"
Dan Gohman906152a2009-01-05 17:59:02 +000018#include <cstdlib>
Chris Lattner5b183222007-05-06 19:49:28 +000019#include <memory>
20using namespace llvm;
21
22/// Read a variable-bit-rate encoded unsigned integer
Dan Gohmand78c4002008-05-13 00:00:25 +000023static inline unsigned readInteger(const char*&At, const char*End) {
Chris Lattner5b183222007-05-06 19:49:28 +000024 unsigned Shift = 0;
25 unsigned Result = 0;
26
27 do {
28 if (At == End)
29 return Result;
30 Result |= (unsigned)((*At++) & 0x7F) << Shift;
31 Shift += 7;
32 } while (At[-1] & 0x80);
33 return Result;
34}
35
36// Completely parse the Archive's symbol table and populate symTab member var.
37bool
38Archive::parseSymbolTable(const void* data, unsigned size, std::string* error) {
39 const char* At = (const char*) data;
40 const char* End = At + size;
41 while (At < End) {
42 unsigned offset = readInteger(At, End);
43 if (At == End) {
44 if (error)
45 *error = "Ran out of data reading vbr_uint for symtab offset!";
46 return false;
47 }
48 unsigned length = readInteger(At, End);
49 if (At == End) {
50 if (error)
51 *error = "Ran out of data reading vbr_uint for symtab length!";
52 return false;
53 }
54 if (At + length > End) {
55 if (error)
56 *error = "Malformed symbol table: length not consistent with size";
57 return false;
58 }
59 // we don't care if it can't be inserted (duplicate entry)
60 symTab.insert(std::make_pair(std::string(At, length), offset));
61 At += length;
62 }
63 symTabSize = size;
64 return true;
65}
66
67// This member parses an ArchiveMemberHeader that is presumed to be pointed to
68// by At. The At pointer is updated to the byte just after the header, which
69// can be variable in size.
70ArchiveMember*
71Archive::parseMemberHeader(const char*& At, const char* End, std::string* error)
72{
73 if (At + sizeof(ArchiveMemberHeader) >= End) {
74 if (error)
75 *error = "Unexpected end of file";
76 return 0;
77 }
78
79 // Cast archive member header
80 ArchiveMemberHeader* Hdr = (ArchiveMemberHeader*)At;
81 At += sizeof(ArchiveMemberHeader);
82
83 // Extract the size and determine if the file is
84 // compressed or not (negative length).
85 int flags = 0;
86 int MemberSize = atoi(Hdr->size);
87 if (MemberSize < 0) {
88 flags |= ArchiveMember::CompressedFlag;
89 MemberSize = -MemberSize;
90 }
91
92 // Check the size of the member for sanity
93 if (At + MemberSize > End) {
94 if (error)
95 *error = "invalid member length in archive file";
96 return 0;
97 }
98
99 // Check the member signature
100 if (!Hdr->checkSignature()) {
101 if (error)
102 *error = "invalid file member signature";
103 return 0;
104 }
105
106 // Convert and check the member name
107 // The empty name ( '/' and 15 blanks) is for a foreign (non-LLVM) symbol
108 // table. The special name "//" and 14 blanks is for a string table, used
109 // for long file names. This library doesn't generate either of those but
110 // it will accept them. If the name starts with #1/ and the remainder is
111 // digits, then those digits specify the length of the name that is
112 // stored immediately following the header. The special name
Gabor Greife16561c2007-07-05 17:07:56 +0000113 // __LLVM_SYM_TAB__ identifies the symbol table for LLVM bitcode.
Chris Lattner5b183222007-05-06 19:49:28 +0000114 // Anything else is a regular, short filename that is terminated with
115 // a '/' and blanks.
116
117 std::string pathname;
118 switch (Hdr->name[0]) {
119 case '#':
120 if (Hdr->name[1] == '1' && Hdr->name[2] == '/') {
121 if (isdigit(Hdr->name[3])) {
122 unsigned len = atoi(&Hdr->name[3]);
Chris Lattner21fb0242010-02-04 06:19:43 +0000123 const char *nulp = (const char *)memchr(At, '\0', len);
Chris Lattner9deb87a2010-04-25 04:44:26 +0000124 pathname.assign(At, nulp != 0 ? (uintptr_t)(nulp - At) : len);
Chris Lattner5b183222007-05-06 19:49:28 +0000125 At += len;
126 MemberSize -= len;
127 flags |= ArchiveMember::HasLongFilenameFlag;
128 } else {
129 if (error)
130 *error = "invalid long filename";
131 return 0;
132 }
133 } else if (Hdr->name[1] == '_' &&
134 (0 == memcmp(Hdr->name, ARFILE_LLVM_SYMTAB_NAME, 16))) {
135 // The member is using a long file name (>15 chars) format.
136 // This format is standard for 4.4BSD and Mac OSX operating
137 // systems. LLVM uses it similarly. In this format, the
138 // remainder of the name field (after #1/) specifies the
139 // length of the file name which occupy the first bytes of
140 // the member's data. The pathname already has the #1/ stripped.
141 pathname.assign(ARFILE_LLVM_SYMTAB_NAME);
142 flags |= ArchiveMember::LLVMSymbolTableFlag;
143 }
144 break;
145 case '/':
146 if (Hdr->name[1]== '/') {
147 if (0 == memcmp(Hdr->name, ARFILE_STRTAB_NAME, 16)) {
148 pathname.assign(ARFILE_STRTAB_NAME);
149 flags |= ArchiveMember::StringTableFlag;
150 } else {
151 if (error)
152 *error = "invalid string table name";
153 return 0;
154 }
155 } else if (Hdr->name[1] == ' ') {
156 if (0 == memcmp(Hdr->name, ARFILE_SVR4_SYMTAB_NAME, 16)) {
157 pathname.assign(ARFILE_SVR4_SYMTAB_NAME);
158 flags |= ArchiveMember::SVR4SymbolTableFlag;
159 } else {
160 if (error)
161 *error = "invalid SVR4 symbol table name";
162 return 0;
163 }
164 } else if (isdigit(Hdr->name[1])) {
165 unsigned index = atoi(&Hdr->name[1]);
166 if (index < strtab.length()) {
167 const char* namep = strtab.c_str() + index;
168 const char* endp = strtab.c_str() + strtab.length();
169 const char* p = namep;
170 const char* last_p = p;
171 while (p < endp) {
172 if (*p == '\n' && *last_p == '/') {
173 pathname.assign(namep, last_p - namep);
174 flags |= ArchiveMember::HasLongFilenameFlag;
175 break;
176 }
177 last_p = p;
178 p++;
179 }
180 if (p >= endp) {
181 if (error)
182 *error = "missing name termiantor in string table";
183 return 0;
184 }
185 } else {
186 if (error)
187 *error = "name index beyond string table";
188 return 0;
189 }
190 }
191 break;
192 case '_':
193 if (Hdr->name[1] == '_' &&
194 (0 == memcmp(Hdr->name, ARFILE_BSD4_SYMTAB_NAME, 16))) {
195 pathname.assign(ARFILE_BSD4_SYMTAB_NAME);
196 flags |= ArchiveMember::BSD4SymbolTableFlag;
197 break;
198 }
199 /* FALL THROUGH */
200
201 default:
202 char* slash = (char*) memchr(Hdr->name, '/', 16);
203 if (slash == 0)
204 slash = Hdr->name + 16;
205 pathname.assign(Hdr->name, slash - Hdr->name);
206 break;
207 }
208
Gabor Greif24027b52007-07-06 20:28:40 +0000209 // Determine if this is a bitcode file
Chris Lattner5b183222007-05-06 19:49:28 +0000210 switch (sys::IdentifyFileType(At, 4)) {
211 case sys::Bitcode_FileType:
Gabor Greif3d3fc322007-07-06 13:38:17 +0000212 flags |= ArchiveMember::BitcodeFlag;
Chris Lattner5b183222007-05-06 19:49:28 +0000213 break;
214 default:
Gabor Greif3d3fc322007-07-06 13:38:17 +0000215 flags &= ~ArchiveMember::BitcodeFlag;
Chris Lattner5b183222007-05-06 19:49:28 +0000216 break;
217 }
218
219 // Instantiate the ArchiveMember to be filled
220 ArchiveMember* member = new ArchiveMember(this);
221
222 // Fill in fields of the ArchiveMember
Chris Lattner5b183222007-05-06 19:49:28 +0000223 member->parent = this;
224 member->path.set(pathname);
225 member->info.fileSize = MemberSize;
226 member->info.modTime.fromEpochTime(atoi(Hdr->date));
227 unsigned int mode;
228 sscanf(Hdr->mode, "%o", &mode);
229 member->info.mode = mode;
230 member->info.user = atoi(Hdr->uid);
231 member->info.group = atoi(Hdr->gid);
232 member->flags = flags;
233 member->data = At;
234
235 return member;
236}
237
238bool
239Archive::checkSignature(std::string* error) {
240 // Check the magic string at file's header
Chris Lattnerd4310a22008-04-01 04:26:46 +0000241 if (mapfile->getBufferSize() < 8 || memcmp(base, ARFILE_MAGIC, 8)) {
Chris Lattner5b183222007-05-06 19:49:28 +0000242 if (error)
243 *error = "invalid signature for an archive file";
244 return false;
245 }
246 return true;
247}
248
249// This function loads the entire archive and fully populates its ilist with
250// the members of the archive file. This is typically used in preparation for
251// editing the contents of the archive.
252bool
253Archive::loadArchive(std::string* error) {
254
255 // Set up parsing
256 members.clear();
257 symTab.clear();
258 const char *At = base;
Chris Lattnerd4310a22008-04-01 04:26:46 +0000259 const char *End = mapfile->getBufferEnd();
Chris Lattner5b183222007-05-06 19:49:28 +0000260
261 if (!checkSignature(error))
262 return false;
263
264 At += 8; // Skip the magic string.
265
266 bool seenSymbolTable = false;
267 bool foundFirstFile = false;
268 while (At < End) {
269 // parse the member header
270 const char* Save = At;
271 ArchiveMember* mbr = parseMemberHeader(At, End, error);
272 if (!mbr)
273 return false;
274
275 // check if this is the foreign symbol table
276 if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
277 // We just save this but don't do anything special
278 // with it. It doesn't count as the "first file".
279 if (foreignST) {
280 // What? Multiple foreign symbol tables? Just chuck it
281 // and retain the last one found.
282 delete foreignST;
283 }
284 foreignST = mbr;
285 At += mbr->getSize();
286 if ((intptr_t(At) & 1) == 1)
287 At++;
288 } else if (mbr->isStringTable()) {
289 // Simply suck the entire string table into a string
290 // variable. This will be used to get the names of the
291 // members that use the "/ddd" format for their names
292 // (SVR4 style long names).
293 strtab.assign(At, mbr->getSize());
294 At += mbr->getSize();
295 if ((intptr_t(At) & 1) == 1)
296 At++;
297 delete mbr;
298 } else if (mbr->isLLVMSymbolTable()) {
299 // This is the LLVM symbol table for the archive. If we've seen it
300 // already, its an error. Otherwise, parse the symbol table and move on.
301 if (seenSymbolTable) {
302 if (error)
303 *error = "invalid archive: multiple symbol tables";
304 return false;
305 }
306 if (!parseSymbolTable(mbr->getData(), mbr->getSize(), error))
307 return false;
308 seenSymbolTable = true;
309 At += mbr->getSize();
310 if ((intptr_t(At) & 1) == 1)
311 At++;
312 delete mbr; // We don't need this member in the list of members.
313 } else {
314 // This is just a regular file. If its the first one, save its offset.
315 // Otherwise just push it on the list and move on to the next file.
316 if (!foundFirstFile) {
317 firstFileOffset = Save - base;
318 foundFirstFile = true;
319 }
320 members.push_back(mbr);
321 At += mbr->getSize();
322 if ((intptr_t(At) & 1) == 1)
323 At++;
324 }
325 }
326 return true;
327}
328
329// Open and completely load the archive file.
330Archive*
Owen Anderson2a154432009-07-01 23:13:44 +0000331Archive::OpenAndLoad(const sys::Path& file, LLVMContext& C,
Owen Anderson6773d382009-07-01 16:58:40 +0000332 std::string* ErrorMessage) {
333 std::auto_ptr<Archive> result ( new Archive(file, C));
Chris Lattner5b183222007-05-06 19:49:28 +0000334 if (result->mapToMemory(ErrorMessage))
335 return 0;
336 if (!result->loadArchive(ErrorMessage))
337 return 0;
338 return result.release();
339}
340
Gabor Greife16561c2007-07-05 17:07:56 +0000341// Get all the bitcode modules from the archive
Chris Lattner5b183222007-05-06 19:49:28 +0000342bool
Owen Anderson6773d382009-07-01 16:58:40 +0000343Archive::getAllModules(std::vector<Module*>& Modules,
344 std::string* ErrMessage) {
Chris Lattner5b183222007-05-06 19:49:28 +0000345
346 for (iterator I=begin(), E=end(); I != E; ++I) {
Gabor Greif3d3fc322007-07-06 13:38:17 +0000347 if (I->isBitcode()) {
Chris Lattnerc521f542009-08-23 22:45:37 +0000348 std::string FullMemberName = archPath.str() +
349 "(" + I->getPath().str() + ")";
Chris Lattner5b183222007-05-06 19:49:28 +0000350 MemoryBuffer *Buffer =
Benjamin Kramer3576b742010-04-19 16:15:31 +0000351 MemoryBuffer::getMemBufferCopy(StringRef(I->getData(), I->getSize()),
352 FullMemberName.c_str());
Chris Lattner5b183222007-05-06 19:49:28 +0000353
Owen Anderson6773d382009-07-01 16:58:40 +0000354 Module *M = ParseBitcodeFile(Buffer, Context, ErrMessage);
Chris Lattner5b183222007-05-06 19:49:28 +0000355 delete Buffer;
356 if (!M)
357 return true;
358
359 Modules.push_back(M);
360 }
361 }
362 return false;
363}
364
365// Load just the symbol table from the archive file
366bool
367Archive::loadSymbolTable(std::string* ErrorMsg) {
368
369 // Set up parsing
370 members.clear();
371 symTab.clear();
372 const char *At = base;
Chris Lattnerd4310a22008-04-01 04:26:46 +0000373 const char *End = mapfile->getBufferEnd();
Chris Lattner5b183222007-05-06 19:49:28 +0000374
375 // Make sure we're dealing with an archive
376 if (!checkSignature(ErrorMsg))
377 return false;
378
379 At += 8; // Skip signature
380
381 // Parse the first file member header
382 const char* FirstFile = At;
383 ArchiveMember* mbr = parseMemberHeader(At, End, ErrorMsg);
384 if (!mbr)
385 return false;
386
387 if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
388 // Skip the foreign symbol table, we don't do anything with it
389 At += mbr->getSize();
390 if ((intptr_t(At) & 1) == 1)
391 At++;
392 delete mbr;
393
394 // Read the next one
395 FirstFile = At;
396 mbr = parseMemberHeader(At, End, ErrorMsg);
397 if (!mbr) {
398 delete mbr;
399 return false;
400 }
401 }
402
403 if (mbr->isStringTable()) {
404 // Process the string table entry
405 strtab.assign((const char*)mbr->getData(), mbr->getSize());
406 At += mbr->getSize();
407 if ((intptr_t(At) & 1) == 1)
408 At++;
409 delete mbr;
410 // Get the next one
411 FirstFile = At;
412 mbr = parseMemberHeader(At, End, ErrorMsg);
413 if (!mbr) {
414 delete mbr;
415 return false;
416 }
417 }
418
419 // See if its the symbol table
420 if (mbr->isLLVMSymbolTable()) {
421 if (!parseSymbolTable(mbr->getData(), mbr->getSize(), ErrorMsg)) {
422 delete mbr;
423 return false;
424 }
425
426 At += mbr->getSize();
427 if ((intptr_t(At) & 1) == 1)
428 At++;
429 delete mbr;
430 // Can't be any more symtab headers so just advance
431 FirstFile = At;
432 } else {
433 // There's no symbol table in the file. We have to rebuild it from scratch
434 // because the intent of this method is to get the symbol table loaded so
435 // it can be searched efficiently.
436 // Add the member to the members list
437 members.push_back(mbr);
438 }
439
440 firstFileOffset = FirstFile - base;
441 return true;
442}
443
444// Open the archive and load just the symbol tables
Owen Anderson1cf085d2009-07-01 21:22:36 +0000445Archive* Archive::OpenAndLoadSymbols(const sys::Path& file,
Owen Anderson2a154432009-07-01 23:13:44 +0000446 LLVMContext& C,
Owen Anderson6773d382009-07-01 16:58:40 +0000447 std::string* ErrorMessage) {
448 std::auto_ptr<Archive> result ( new Archive(file, C) );
Chris Lattner5b183222007-05-06 19:49:28 +0000449 if (result->mapToMemory(ErrorMessage))
450 return 0;
451 if (!result->loadSymbolTable(ErrorMessage))
452 return 0;
453 return result.release();
454}
455
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000456// Look up one symbol in the symbol table and return the module that defines
457// that symbol.
458Module*
Chris Lattner5b183222007-05-06 19:49:28 +0000459Archive::findModuleDefiningSymbol(const std::string& symbol,
460 std::string* ErrMsg) {
461 SymTabType::iterator SI = symTab.find(symbol);
462 if (SI == symTab.end())
463 return 0;
464
465 // The symbol table was previously constructed assuming that the members were
466 // written without the symbol table header. Because VBR encoding is used, the
467 // values could not be adjusted to account for the offset of the symbol table
468 // because that could affect the size of the symbol table due to VBR encoding.
469 // We now have to account for this by adjusting the offset by the size of the
470 // symbol table and its header.
471 unsigned fileOffset =
472 SI->second + // offset in symbol-table-less file
473 firstFileOffset; // add offset to first "real" file in archive
474
475 // See if the module is already loaded
476 ModuleMap::iterator MI = modules.find(fileOffset);
477 if (MI != modules.end())
478 return MI->second.first;
479
480 // Module hasn't been loaded yet, we need to load it
481 const char* modptr = base + fileOffset;
Chris Lattnerd4310a22008-04-01 04:26:46 +0000482 ArchiveMember* mbr = parseMemberHeader(modptr, mapfile->getBufferEnd(),
483 ErrMsg);
Chris Lattner5b183222007-05-06 19:49:28 +0000484 if (!mbr)
485 return 0;
486
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000487 // Now, load the bitcode module to get the Module.
Chris Lattnerc521f542009-08-23 22:45:37 +0000488 std::string FullMemberName = archPath.str() + "(" +
489 mbr->getPath().str() + ")";
Benjamin Kramer3576b742010-04-19 16:15:31 +0000490 MemoryBuffer *Buffer =
491 MemoryBuffer::getMemBufferCopy(StringRef(mbr->getData(), mbr->getSize()),
492 FullMemberName.c_str());
Chris Lattner5b183222007-05-06 19:49:28 +0000493
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000494 Module *m = getLazyBitcodeModule(Buffer, Context, ErrMsg);
495 if (!m)
Chris Lattner5b183222007-05-06 19:49:28 +0000496 return 0;
497
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000498 modules.insert(std::make_pair(fileOffset, std::make_pair(m, mbr)));
Chris Lattner5b183222007-05-06 19:49:28 +0000499
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000500 return m;
Chris Lattner5b183222007-05-06 19:49:28 +0000501}
502
503// Look up multiple symbols in the symbol table and return a set of
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000504// Modules that define those symbols.
Chris Lattner5b183222007-05-06 19:49:28 +0000505bool
506Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000507 std::set<Module*>& result,
Chris Lattner5b183222007-05-06 19:49:28 +0000508 std::string* error) {
509 if (!mapfile || !base) {
510 if (error)
511 *error = "Empty archive invalid for finding modules defining symbols";
512 return false;
513 }
514
515 if (symTab.empty()) {
516 // We don't have a symbol table, so we must build it now but lets also
517 // make sure that we populate the modules table as we do this to ensure
518 // that we don't load them twice when findModuleDefiningSymbol is called
519 // below.
520
521 // Get a pointer to the first file
Chris Lattnerd4310a22008-04-01 04:26:46 +0000522 const char* At = base + firstFileOffset;
523 const char* End = mapfile->getBufferEnd();
Chris Lattner5b183222007-05-06 19:49:28 +0000524
525 while ( At < End) {
526 // Compute the offset to be put in the symbol table
527 unsigned offset = At - base - firstFileOffset;
528
529 // Parse the file's header
530 ArchiveMember* mbr = parseMemberHeader(At, End, error);
531 if (!mbr)
532 return false;
533
534 // If it contains symbols
Gabor Greif3d3fc322007-07-06 13:38:17 +0000535 if (mbr->isBitcode()) {
Chris Lattner5b183222007-05-06 19:49:28 +0000536 // Get the symbols
537 std::vector<std::string> symbols;
Chris Lattnerc521f542009-08-23 22:45:37 +0000538 std::string FullMemberName = archPath.str() + "(" +
539 mbr->getPath().str() + ")";
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000540 Module* M =
Benjamin Kramer3576b742010-04-19 16:15:31 +0000541 GetBitcodeSymbols(At, mbr->getSize(), FullMemberName, Context,
542 symbols, error);
Chris Lattner5b183222007-05-06 19:49:28 +0000543
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000544 if (M) {
Chris Lattner5b183222007-05-06 19:49:28 +0000545 // Insert the module's symbols into the symbol table
546 for (std::vector<std::string>::iterator I = symbols.begin(),
547 E=symbols.end(); I != E; ++I ) {
548 symTab.insert(std::make_pair(*I, offset));
549 }
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000550 // Insert the Module and the ArchiveMember into the table of
Chris Lattner5b183222007-05-06 19:49:28 +0000551 // modules.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000552 modules.insert(std::make_pair(offset, std::make_pair(M, mbr)));
Chris Lattner5b183222007-05-06 19:49:28 +0000553 } else {
554 if (error)
Gabor Greife16561c2007-07-05 17:07:56 +0000555 *error = "Can't parse bitcode member: " +
Chris Lattnerc521f542009-08-23 22:45:37 +0000556 mbr->getPath().str() + ": " + *error;
Chris Lattner5b183222007-05-06 19:49:28 +0000557 delete mbr;
558 return false;
559 }
560 }
561
562 // Go to the next file location
563 At += mbr->getSize();
564 if ((intptr_t(At) & 1) == 1)
565 At++;
566 }
567 }
568
569 // At this point we have a valid symbol table (one way or another) so we
570 // just use it to quickly find the symbols requested.
571
572 for (std::set<std::string>::iterator I=symbols.begin(),
573 E=symbols.end(); I != E;) {
574 // See if this symbol exists
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000575 Module* m = findModuleDefiningSymbol(*I,error);
576 if (m) {
577 // The symbol exists, insert the Module into our result, duplicates will
578 // be ignored.
579 result.insert(m);
Chris Lattner5b183222007-05-06 19:49:28 +0000580
581 // Remove the symbol now that its been resolved, being careful to
582 // post-increment the iterator.
583 symbols.erase(I++);
584 } else {
585 ++I;
586 }
587 }
588 return true;
589}
590
Gabor Greife16561c2007-07-05 17:07:56 +0000591bool Archive::isBitcodeArchive() {
Chris Lattner5b183222007-05-06 19:49:28 +0000592 // Make sure the symTab has been loaded. In most cases this should have been
593 // done when the archive was constructed, but still, this is just in case.
Dan Gohman70de4cb2008-01-29 13:02:09 +0000594 if (symTab.empty())
Chris Lattner5b183222007-05-06 19:49:28 +0000595 if (!loadSymbolTable(0))
596 return false;
597
598 // Now that we know it's been loaded, return true
599 // if it has a size
600 if (symTab.size()) return true;
601
Gabor Greife16561c2007-07-05 17:07:56 +0000602 // We still can't be sure it isn't a bitcode archive
Chris Lattner5b183222007-05-06 19:49:28 +0000603 if (!loadArchive(0))
604 return false;
605
606 std::vector<Module *> Modules;
607 std::string ErrorMessage;
608
Gabor Greife16561c2007-07-05 17:07:56 +0000609 // Scan the archive, trying to load a bitcode member. We only load one to
Chris Lattner5b183222007-05-06 19:49:28 +0000610 // see if this works.
611 for (iterator I = begin(), E = end(); I != E; ++I) {
Gabor Greif3d3fc322007-07-06 13:38:17 +0000612 if (!I->isBitcode())
Chris Lattner5b183222007-05-06 19:49:28 +0000613 continue;
614
615 std::string FullMemberName =
Chris Lattnerc521f542009-08-23 22:45:37 +0000616 archPath.str() + "(" + I->getPath().str() + ")";
Chris Lattner5b183222007-05-06 19:49:28 +0000617
618 MemoryBuffer *Buffer =
Benjamin Kramer3576b742010-04-19 16:15:31 +0000619 MemoryBuffer::getMemBufferCopy(StringRef(I->getData(), I->getSize()),
620 FullMemberName.c_str());
Owen Anderson6773d382009-07-01 16:58:40 +0000621 Module *M = ParseBitcodeFile(Buffer, Context);
Chris Lattner5b183222007-05-06 19:49:28 +0000622 delete Buffer;
623 if (!M)
Gabor Greife16561c2007-07-05 17:07:56 +0000624 return false; // Couldn't parse bitcode, not a bitcode archive.
Chris Lattner5b183222007-05-06 19:49:28 +0000625 delete M;
626 return true;
627 }
628
629 return false;
630}