blob: efe1180762f33da3c00dd55eb85a5722ddffb135 [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Bitcode/Archive.h"
Chris Lattner5b183222007-05-06 19:49:28 +000015#include "ArchiveInternals.h"
Rafael Espindolaabf456e2012-01-23 03:41:53 +000016#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner5b183222007-05-06 19:49:28 +000017#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramerb0640db2012-03-23 11:35:30 +000020#include <cstdio>
Dan Gohman906152a2009-01-05 17:59:02 +000021#include <cstdlib>
Chris Lattner5b183222007-05-06 19:49:28 +000022#include <memory>
23using namespace llvm;
24
25/// Read a variable-bit-rate encoded unsigned integer
Dan Gohmand78c4002008-05-13 00:00:25 +000026static inline unsigned readInteger(const char*&At, const char*End) {
Chris Lattner5b183222007-05-06 19:49:28 +000027 unsigned Shift = 0;
28 unsigned Result = 0;
29
30 do {
31 if (At == End)
32 return Result;
33 Result |= (unsigned)((*At++) & 0x7F) << Shift;
34 Shift += 7;
35 } while (At[-1] & 0x80);
36 return Result;
37}
38
39// Completely parse the Archive's symbol table and populate symTab member var.
40bool
41Archive::parseSymbolTable(const void* data, unsigned size, std::string* error) {
42 const char* At = (const char*) data;
43 const char* End = At + size;
44 while (At < End) {
45 unsigned offset = readInteger(At, End);
46 if (At == End) {
47 if (error)
48 *error = "Ran out of data reading vbr_uint for symtab offset!";
49 return false;
50 }
51 unsigned length = readInteger(At, End);
52 if (At == End) {
53 if (error)
54 *error = "Ran out of data reading vbr_uint for symtab length!";
55 return false;
56 }
57 if (At + length > End) {
58 if (error)
59 *error = "Malformed symbol table: length not consistent with size";
60 return false;
61 }
62 // we don't care if it can't be inserted (duplicate entry)
63 symTab.insert(std::make_pair(std::string(At, length), offset));
64 At += length;
65 }
66 symTabSize = size;
67 return true;
68}
69
70// This member parses an ArchiveMemberHeader that is presumed to be pointed to
71// by At. The At pointer is updated to the byte just after the header, which
72// can be variable in size.
73ArchiveMember*
74Archive::parseMemberHeader(const char*& At, const char* End, std::string* error)
75{
76 if (At + sizeof(ArchiveMemberHeader) >= End) {
77 if (error)
78 *error = "Unexpected end of file";
79 return 0;
80 }
81
82 // Cast archive member header
Roman Divacky9f33d682012-09-05 22:09:23 +000083 const ArchiveMemberHeader* Hdr = (const ArchiveMemberHeader*)At;
Chris Lattner5b183222007-05-06 19:49:28 +000084 At += sizeof(ArchiveMemberHeader);
85
Chris Lattner5b183222007-05-06 19:49:28 +000086 int flags = 0;
87 int MemberSize = atoi(Hdr->size);
Rafael Espindola740a6bc2012-08-10 01:57:52 +000088 assert(MemberSize >= 0);
Chris Lattner5b183222007-05-06 19:49:28 +000089
90 // Check the size of the member for sanity
91 if (At + MemberSize > End) {
92 if (error)
93 *error = "invalid member length in archive file";
94 return 0;
95 }
96
97 // Check the member signature
98 if (!Hdr->checkSignature()) {
99 if (error)
100 *error = "invalid file member signature";
101 return 0;
102 }
103
104 // Convert and check the member name
105 // The empty name ( '/' and 15 blanks) is for a foreign (non-LLVM) symbol
106 // table. The special name "//" and 14 blanks is for a string table, used
107 // for long file names. This library doesn't generate either of those but
108 // it will accept them. If the name starts with #1/ and the remainder is
109 // digits, then those digits specify the length of the name that is
110 // stored immediately following the header. The special name
Gabor Greife16561c2007-07-05 17:07:56 +0000111 // __LLVM_SYM_TAB__ identifies the symbol table for LLVM bitcode.
Chris Lattner5b183222007-05-06 19:49:28 +0000112 // Anything else is a regular, short filename that is terminated with
113 // a '/' and blanks.
114
115 std::string pathname;
116 switch (Hdr->name[0]) {
117 case '#':
118 if (Hdr->name[1] == '1' && Hdr->name[2] == '/') {
119 if (isdigit(Hdr->name[3])) {
120 unsigned len = atoi(&Hdr->name[3]);
Chris Lattner21fb0242010-02-04 06:19:43 +0000121 const char *nulp = (const char *)memchr(At, '\0', len);
Chris Lattner9deb87a2010-04-25 04:44:26 +0000122 pathname.assign(At, nulp != 0 ? (uintptr_t)(nulp - At) : len);
Chris Lattner5b183222007-05-06 19:49:28 +0000123 At += len;
124 MemberSize -= len;
125 flags |= ArchiveMember::HasLongFilenameFlag;
126 } else {
127 if (error)
128 *error = "invalid long filename";
129 return 0;
130 }
131 } else if (Hdr->name[1] == '_' &&
132 (0 == memcmp(Hdr->name, ARFILE_LLVM_SYMTAB_NAME, 16))) {
133 // The member is using a long file name (>15 chars) format.
134 // This format is standard for 4.4BSD and Mac OSX operating
135 // systems. LLVM uses it similarly. In this format, the
136 // remainder of the name field (after #1/) specifies the
137 // length of the file name which occupy the first bytes of
138 // the member's data. The pathname already has the #1/ stripped.
139 pathname.assign(ARFILE_LLVM_SYMTAB_NAME);
140 flags |= ArchiveMember::LLVMSymbolTableFlag;
141 }
142 break;
143 case '/':
144 if (Hdr->name[1]== '/') {
145 if (0 == memcmp(Hdr->name, ARFILE_STRTAB_NAME, 16)) {
146 pathname.assign(ARFILE_STRTAB_NAME);
147 flags |= ArchiveMember::StringTableFlag;
148 } else {
149 if (error)
150 *error = "invalid string table name";
151 return 0;
152 }
153 } else if (Hdr->name[1] == ' ') {
154 if (0 == memcmp(Hdr->name, ARFILE_SVR4_SYMTAB_NAME, 16)) {
155 pathname.assign(ARFILE_SVR4_SYMTAB_NAME);
156 flags |= ArchiveMember::SVR4SymbolTableFlag;
157 } else {
158 if (error)
159 *error = "invalid SVR4 symbol table name";
160 return 0;
161 }
162 } else if (isdigit(Hdr->name[1])) {
163 unsigned index = atoi(&Hdr->name[1]);
164 if (index < strtab.length()) {
165 const char* namep = strtab.c_str() + index;
166 const char* endp = strtab.c_str() + strtab.length();
167 const char* p = namep;
168 const char* last_p = p;
169 while (p < endp) {
170 if (*p == '\n' && *last_p == '/') {
171 pathname.assign(namep, last_p - namep);
172 flags |= ArchiveMember::HasLongFilenameFlag;
173 break;
174 }
175 last_p = p;
176 p++;
177 }
178 if (p >= endp) {
179 if (error)
180 *error = "missing name termiantor in string table";
181 return 0;
182 }
183 } else {
184 if (error)
185 *error = "name index beyond string table";
186 return 0;
187 }
188 }
189 break;
190 case '_':
191 if (Hdr->name[1] == '_' &&
192 (0 == memcmp(Hdr->name, ARFILE_BSD4_SYMTAB_NAME, 16))) {
193 pathname.assign(ARFILE_BSD4_SYMTAB_NAME);
194 flags |= ArchiveMember::BSD4SymbolTableFlag;
195 break;
196 }
197 /* FALL THROUGH */
198
199 default:
Roman Divacky9f33d682012-09-05 22:09:23 +0000200 const char* slash = (const char*) memchr(Hdr->name, '/', 16);
Chris Lattner5b183222007-05-06 19:49:28 +0000201 if (slash == 0)
202 slash = Hdr->name + 16;
203 pathname.assign(Hdr->name, slash - Hdr->name);
204 break;
205 }
206
Gabor Greif24027b52007-07-06 20:28:40 +0000207 // Determine if this is a bitcode file
Chris Lattner5b183222007-05-06 19:49:28 +0000208 switch (sys::IdentifyFileType(At, 4)) {
209 case sys::Bitcode_FileType:
Gabor Greif3d3fc322007-07-06 13:38:17 +0000210 flags |= ArchiveMember::BitcodeFlag;
Chris Lattner5b183222007-05-06 19:49:28 +0000211 break;
212 default:
Gabor Greif3d3fc322007-07-06 13:38:17 +0000213 flags &= ~ArchiveMember::BitcodeFlag;
Chris Lattner5b183222007-05-06 19:49:28 +0000214 break;
215 }
216
217 // Instantiate the ArchiveMember to be filled
218 ArchiveMember* member = new ArchiveMember(this);
219
220 // Fill in fields of the ArchiveMember
Chris Lattner5b183222007-05-06 19:49:28 +0000221 member->parent = this;
222 member->path.set(pathname);
223 member->info.fileSize = MemberSize;
224 member->info.modTime.fromEpochTime(atoi(Hdr->date));
225 unsigned int mode;
226 sscanf(Hdr->mode, "%o", &mode);
227 member->info.mode = mode;
228 member->info.user = atoi(Hdr->uid);
229 member->info.group = atoi(Hdr->gid);
230 member->flags = flags;
231 member->data = At;
232
233 return member;
234}
235
236bool
237Archive::checkSignature(std::string* error) {
238 // Check the magic string at file's header
Chris Lattnerd4310a22008-04-01 04:26:46 +0000239 if (mapfile->getBufferSize() < 8 || memcmp(base, ARFILE_MAGIC, 8)) {
Chris Lattner5b183222007-05-06 19:49:28 +0000240 if (error)
241 *error = "invalid signature for an archive file";
242 return false;
243 }
244 return true;
245}
246
247// This function loads the entire archive and fully populates its ilist with
248// the members of the archive file. This is typically used in preparation for
249// editing the contents of the archive.
250bool
251Archive::loadArchive(std::string* error) {
252
253 // Set up parsing
254 members.clear();
255 symTab.clear();
256 const char *At = base;
Chris Lattnerd4310a22008-04-01 04:26:46 +0000257 const char *End = mapfile->getBufferEnd();
Chris Lattner5b183222007-05-06 19:49:28 +0000258
259 if (!checkSignature(error))
260 return false;
261
262 At += 8; // Skip the magic string.
263
264 bool seenSymbolTable = false;
265 bool foundFirstFile = false;
266 while (At < End) {
267 // parse the member header
268 const char* Save = At;
269 ArchiveMember* mbr = parseMemberHeader(At, End, error);
270 if (!mbr)
271 return false;
272
273 // check if this is the foreign symbol table
274 if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
275 // We just save this but don't do anything special
276 // with it. It doesn't count as the "first file".
277 if (foreignST) {
278 // What? Multiple foreign symbol tables? Just chuck it
279 // and retain the last one found.
280 delete foreignST;
281 }
282 foreignST = mbr;
283 At += mbr->getSize();
284 if ((intptr_t(At) & 1) == 1)
285 At++;
286 } else if (mbr->isStringTable()) {
287 // Simply suck the entire string table into a string
288 // variable. This will be used to get the names of the
289 // members that use the "/ddd" format for their names
290 // (SVR4 style long names).
291 strtab.assign(At, mbr->getSize());
292 At += mbr->getSize();
293 if ((intptr_t(At) & 1) == 1)
294 At++;
295 delete mbr;
296 } else if (mbr->isLLVMSymbolTable()) {
297 // This is the LLVM symbol table for the archive. If we've seen it
298 // already, its an error. Otherwise, parse the symbol table and move on.
299 if (seenSymbolTable) {
300 if (error)
301 *error = "invalid archive: multiple symbol tables";
302 return false;
303 }
304 if (!parseSymbolTable(mbr->getData(), mbr->getSize(), error))
305 return false;
306 seenSymbolTable = true;
307 At += mbr->getSize();
308 if ((intptr_t(At) & 1) == 1)
309 At++;
310 delete mbr; // We don't need this member in the list of members.
311 } else {
312 // This is just a regular file. If its the first one, save its offset.
313 // Otherwise just push it on the list and move on to the next file.
314 if (!foundFirstFile) {
315 firstFileOffset = Save - base;
316 foundFirstFile = true;
317 }
318 members.push_back(mbr);
319 At += mbr->getSize();
320 if ((intptr_t(At) & 1) == 1)
321 At++;
322 }
323 }
324 return true;
325}
326
327// Open and completely load the archive file.
328Archive*
Owen Anderson2a154432009-07-01 23:13:44 +0000329Archive::OpenAndLoad(const sys::Path& file, LLVMContext& C,
Owen Anderson6773d382009-07-01 16:58:40 +0000330 std::string* ErrorMessage) {
331 std::auto_ptr<Archive> result ( new Archive(file, C));
Chris Lattner5b183222007-05-06 19:49:28 +0000332 if (result->mapToMemory(ErrorMessage))
333 return 0;
334 if (!result->loadArchive(ErrorMessage))
335 return 0;
336 return result.release();
337}
338
Gabor Greife16561c2007-07-05 17:07:56 +0000339// Get all the bitcode modules from the archive
Chris Lattner5b183222007-05-06 19:49:28 +0000340bool
Owen Anderson6773d382009-07-01 16:58:40 +0000341Archive::getAllModules(std::vector<Module*>& Modules,
342 std::string* ErrMessage) {
Chris Lattner5b183222007-05-06 19:49:28 +0000343
344 for (iterator I=begin(), E=end(); I != E; ++I) {
Gabor Greif3d3fc322007-07-06 13:38:17 +0000345 if (I->isBitcode()) {
Chris Lattnerc521f542009-08-23 22:45:37 +0000346 std::string FullMemberName = archPath.str() +
347 "(" + I->getPath().str() + ")";
Chris Lattner5b183222007-05-06 19:49:28 +0000348 MemoryBuffer *Buffer =
Benjamin Kramer3576b742010-04-19 16:15:31 +0000349 MemoryBuffer::getMemBufferCopy(StringRef(I->getData(), I->getSize()),
350 FullMemberName.c_str());
Chris Lattner5b183222007-05-06 19:49:28 +0000351
Owen Anderson6773d382009-07-01 16:58:40 +0000352 Module *M = ParseBitcodeFile(Buffer, Context, ErrMessage);
Chris Lattner5b183222007-05-06 19:49:28 +0000353 delete Buffer;
354 if (!M)
355 return true;
356
357 Modules.push_back(M);
358 }
359 }
360 return false;
361}
362
363// Load just the symbol table from the archive file
364bool
365Archive::loadSymbolTable(std::string* ErrorMsg) {
366
367 // Set up parsing
368 members.clear();
369 symTab.clear();
370 const char *At = base;
Chris Lattnerd4310a22008-04-01 04:26:46 +0000371 const char *End = mapfile->getBufferEnd();
Chris Lattner5b183222007-05-06 19:49:28 +0000372
373 // Make sure we're dealing with an archive
374 if (!checkSignature(ErrorMsg))
375 return false;
376
377 At += 8; // Skip signature
378
379 // Parse the first file member header
380 const char* FirstFile = At;
381 ArchiveMember* mbr = parseMemberHeader(At, End, ErrorMsg);
382 if (!mbr)
383 return false;
384
385 if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
386 // Skip the foreign symbol table, we don't do anything with it
387 At += mbr->getSize();
388 if ((intptr_t(At) & 1) == 1)
389 At++;
390 delete mbr;
391
392 // Read the next one
393 FirstFile = At;
394 mbr = parseMemberHeader(At, End, ErrorMsg);
395 if (!mbr) {
396 delete mbr;
397 return false;
398 }
399 }
400
401 if (mbr->isStringTable()) {
402 // Process the string table entry
403 strtab.assign((const char*)mbr->getData(), mbr->getSize());
404 At += mbr->getSize();
405 if ((intptr_t(At) & 1) == 1)
406 At++;
407 delete mbr;
408 // Get the next one
409 FirstFile = At;
410 mbr = parseMemberHeader(At, End, ErrorMsg);
411 if (!mbr) {
412 delete mbr;
413 return false;
414 }
415 }
416
417 // See if its the symbol table
418 if (mbr->isLLVMSymbolTable()) {
419 if (!parseSymbolTable(mbr->getData(), mbr->getSize(), ErrorMsg)) {
420 delete mbr;
421 return false;
422 }
423
424 At += mbr->getSize();
425 if ((intptr_t(At) & 1) == 1)
426 At++;
427 delete mbr;
428 // Can't be any more symtab headers so just advance
429 FirstFile = At;
430 } else {
431 // There's no symbol table in the file. We have to rebuild it from scratch
432 // because the intent of this method is to get the symbol table loaded so
433 // it can be searched efficiently.
434 // Add the member to the members list
435 members.push_back(mbr);
436 }
437
438 firstFileOffset = FirstFile - base;
439 return true;
440}
441
442// Open the archive and load just the symbol tables
Owen Anderson1cf085d2009-07-01 21:22:36 +0000443Archive* Archive::OpenAndLoadSymbols(const sys::Path& file,
Owen Anderson2a154432009-07-01 23:13:44 +0000444 LLVMContext& C,
Owen Anderson6773d382009-07-01 16:58:40 +0000445 std::string* ErrorMessage) {
446 std::auto_ptr<Archive> result ( new Archive(file, C) );
Chris Lattner5b183222007-05-06 19:49:28 +0000447 if (result->mapToMemory(ErrorMessage))
448 return 0;
449 if (!result->loadSymbolTable(ErrorMessage))
450 return 0;
451 return result.release();
452}
453
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000454// Look up one symbol in the symbol table and return the module that defines
455// that symbol.
456Module*
Chris Lattner5b183222007-05-06 19:49:28 +0000457Archive::findModuleDefiningSymbol(const std::string& symbol,
458 std::string* ErrMsg) {
459 SymTabType::iterator SI = symTab.find(symbol);
460 if (SI == symTab.end())
461 return 0;
462
463 // The symbol table was previously constructed assuming that the members were
464 // written without the symbol table header. Because VBR encoding is used, the
465 // values could not be adjusted to account for the offset of the symbol table
466 // because that could affect the size of the symbol table due to VBR encoding.
467 // We now have to account for this by adjusting the offset by the size of the
468 // symbol table and its header.
469 unsigned fileOffset =
470 SI->second + // offset in symbol-table-less file
471 firstFileOffset; // add offset to first "real" file in archive
472
473 // See if the module is already loaded
474 ModuleMap::iterator MI = modules.find(fileOffset);
475 if (MI != modules.end())
476 return MI->second.first;
477
478 // Module hasn't been loaded yet, we need to load it
479 const char* modptr = base + fileOffset;
Chris Lattnerd4310a22008-04-01 04:26:46 +0000480 ArchiveMember* mbr = parseMemberHeader(modptr, mapfile->getBufferEnd(),
481 ErrMsg);
Chris Lattner5b183222007-05-06 19:49:28 +0000482 if (!mbr)
483 return 0;
484
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000485 // Now, load the bitcode module to get the Module.
Chris Lattnerc521f542009-08-23 22:45:37 +0000486 std::string FullMemberName = archPath.str() + "(" +
487 mbr->getPath().str() + ")";
Benjamin Kramer3576b742010-04-19 16:15:31 +0000488 MemoryBuffer *Buffer =
489 MemoryBuffer::getMemBufferCopy(StringRef(mbr->getData(), mbr->getSize()),
490 FullMemberName.c_str());
Chris Lattner5b183222007-05-06 19:49:28 +0000491
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000492 Module *m = getLazyBitcodeModule(Buffer, Context, ErrMsg);
493 if (!m)
Chris Lattner5b183222007-05-06 19:49:28 +0000494 return 0;
495
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000496 modules.insert(std::make_pair(fileOffset, std::make_pair(m, mbr)));
Chris Lattner5b183222007-05-06 19:49:28 +0000497
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000498 return m;
Chris Lattner5b183222007-05-06 19:49:28 +0000499}
500
501// Look up multiple symbols in the symbol table and return a set of
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000502// Modules that define those symbols.
Chris Lattner5b183222007-05-06 19:49:28 +0000503bool
504Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
Rafael Espindolaabf456e2012-01-23 03:41:53 +0000505 SmallVectorImpl<Module*>& result,
Chris Lattner5b183222007-05-06 19:49:28 +0000506 std::string* error) {
507 if (!mapfile || !base) {
508 if (error)
509 *error = "Empty archive invalid for finding modules defining symbols";
510 return false;
511 }
512
513 if (symTab.empty()) {
514 // We don't have a symbol table, so we must build it now but lets also
515 // make sure that we populate the modules table as we do this to ensure
516 // that we don't load them twice when findModuleDefiningSymbol is called
517 // below.
518
519 // Get a pointer to the first file
Chris Lattnerd4310a22008-04-01 04:26:46 +0000520 const char* At = base + firstFileOffset;
521 const char* End = mapfile->getBufferEnd();
Chris Lattner5b183222007-05-06 19:49:28 +0000522
523 while ( At < End) {
524 // Compute the offset to be put in the symbol table
525 unsigned offset = At - base - firstFileOffset;
526
527 // Parse the file's header
528 ArchiveMember* mbr = parseMemberHeader(At, End, error);
529 if (!mbr)
530 return false;
531
532 // If it contains symbols
Gabor Greif3d3fc322007-07-06 13:38:17 +0000533 if (mbr->isBitcode()) {
Chris Lattner5b183222007-05-06 19:49:28 +0000534 // Get the symbols
535 std::vector<std::string> symbols;
Chris Lattnerc521f542009-08-23 22:45:37 +0000536 std::string FullMemberName = archPath.str() + "(" +
537 mbr->getPath().str() + ")";
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000538 Module* M =
Benjamin Kramer3576b742010-04-19 16:15:31 +0000539 GetBitcodeSymbols(At, mbr->getSize(), FullMemberName, Context,
540 symbols, error);
Chris Lattner5b183222007-05-06 19:49:28 +0000541
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000542 if (M) {
Chris Lattner5b183222007-05-06 19:49:28 +0000543 // Insert the module's symbols into the symbol table
544 for (std::vector<std::string>::iterator I = symbols.begin(),
545 E=symbols.end(); I != E; ++I ) {
546 symTab.insert(std::make_pair(*I, offset));
547 }
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000548 // Insert the Module and the ArchiveMember into the table of
Chris Lattner5b183222007-05-06 19:49:28 +0000549 // modules.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000550 modules.insert(std::make_pair(offset, std::make_pair(M, mbr)));
Chris Lattner5b183222007-05-06 19:49:28 +0000551 } else {
552 if (error)
Gabor Greife16561c2007-07-05 17:07:56 +0000553 *error = "Can't parse bitcode member: " +
Chris Lattnerc521f542009-08-23 22:45:37 +0000554 mbr->getPath().str() + ": " + *error;
Chris Lattner5b183222007-05-06 19:49:28 +0000555 delete mbr;
556 return false;
557 }
558 }
559
560 // Go to the next file location
561 At += mbr->getSize();
562 if ((intptr_t(At) & 1) == 1)
563 At++;
564 }
565 }
566
567 // At this point we have a valid symbol table (one way or another) so we
568 // just use it to quickly find the symbols requested.
569
Rafael Espindolaabf456e2012-01-23 03:41:53 +0000570 SmallPtrSet<Module*, 16> Added;
Chris Lattner5b183222007-05-06 19:49:28 +0000571 for (std::set<std::string>::iterator I=symbols.begin(),
Rafael Espindola624e3082012-01-23 05:07:16 +0000572 Next = I,
573 E=symbols.end(); I != E; I = Next) {
574 // Increment Next before we invalidate it.
575 ++Next;
576
Chris Lattner5b183222007-05-06 19:49:28 +0000577 // See if this symbol exists
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000578 Module* m = findModuleDefiningSymbol(*I,error);
Rafael Espindolaabf456e2012-01-23 03:41:53 +0000579 if (!m)
580 continue;
581 bool NewMember = Added.insert(m);
582 if (!NewMember)
583 continue;
Chris Lattner5b183222007-05-06 19:49:28 +0000584
Rafael Espindolaabf456e2012-01-23 03:41:53 +0000585 // The symbol exists, insert the Module into our result.
586 result.push_back(m);
587
588 // Remove the symbol now that its been resolved.
589 symbols.erase(I);
Chris Lattner5b183222007-05-06 19:49:28 +0000590 }
591 return true;
592}
593
Gabor Greife16561c2007-07-05 17:07:56 +0000594bool Archive::isBitcodeArchive() {
Chris Lattner5b183222007-05-06 19:49:28 +0000595 // Make sure the symTab has been loaded. In most cases this should have been
596 // done when the archive was constructed, but still, this is just in case.
Dan Gohman70de4cb2008-01-29 13:02:09 +0000597 if (symTab.empty())
Chris Lattner5b183222007-05-06 19:49:28 +0000598 if (!loadSymbolTable(0))
599 return false;
600
601 // Now that we know it's been loaded, return true
602 // if it has a size
603 if (symTab.size()) return true;
604
Gabor Greife16561c2007-07-05 17:07:56 +0000605 // We still can't be sure it isn't a bitcode archive
Chris Lattner5b183222007-05-06 19:49:28 +0000606 if (!loadArchive(0))
607 return false;
608
609 std::vector<Module *> Modules;
610 std::string ErrorMessage;
611
Gabor Greife16561c2007-07-05 17:07:56 +0000612 // Scan the archive, trying to load a bitcode member. We only load one to
Chris Lattner5b183222007-05-06 19:49:28 +0000613 // see if this works.
614 for (iterator I = begin(), E = end(); I != E; ++I) {
Gabor Greif3d3fc322007-07-06 13:38:17 +0000615 if (!I->isBitcode())
Chris Lattner5b183222007-05-06 19:49:28 +0000616 continue;
617
618 std::string FullMemberName =
Chris Lattnerc521f542009-08-23 22:45:37 +0000619 archPath.str() + "(" + I->getPath().str() + ")";
Chris Lattner5b183222007-05-06 19:49:28 +0000620
621 MemoryBuffer *Buffer =
Benjamin Kramer3576b742010-04-19 16:15:31 +0000622 MemoryBuffer::getMemBufferCopy(StringRef(I->getData(), I->getSize()),
623 FullMemberName.c_str());
Owen Anderson6773d382009-07-01 16:58:40 +0000624 Module *M = ParseBitcodeFile(Buffer, Context);
Chris Lattner5b183222007-05-06 19:49:28 +0000625 delete Buffer;
626 if (!M)
Gabor Greife16561c2007-07-05 17:07:56 +0000627 return false; // Couldn't parse bitcode, not a bitcode archive.
Chris Lattner5b183222007-05-06 19:49:28 +0000628 delete M;
629 return true;
630 }
631
632 return false;
633}