LLVM API Documentation
00001 //===-- ArchiveReader.cpp - Read LLVM archive files -------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Builds up standard unix archive files (.a) containing LLVM bitcode. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "ArchiveInternals.h" 00015 #include "llvm/Bitcode/ReaderWriter.h" 00016 #include "llvm/Support/MemoryBuffer.h" 00017 #include "llvm/Module.h" 00018 #include <cstdlib> 00019 #include <memory> 00020 using namespace llvm; 00021 00022 /// Read a variable-bit-rate encoded unsigned integer 00023 static inline unsigned readInteger(const char*&At, const char*End) { 00024 unsigned Shift = 0; 00025 unsigned Result = 0; 00026 00027 do { 00028 if (At == End) 00029 return Result; 00030 Result |= (unsigned)((*At++) & 0x7F) << Shift; 00031 Shift += 7; 00032 } while (At[-1] & 0x80); 00033 return Result; 00034 } 00035 00036 // Completely parse the Archive's symbol table and populate symTab member var. 00037 bool 00038 Archive::parseSymbolTable(const void* data, unsigned size, std::string* error) { 00039 const char* At = (const char*) data; 00040 const char* End = At + size; 00041 while (At < End) { 00042 unsigned offset = readInteger(At, End); 00043 if (At == End) { 00044 if (error) 00045 *error = "Ran out of data reading vbr_uint for symtab offset!"; 00046 return false; 00047 } 00048 unsigned length = readInteger(At, End); 00049 if (At == End) { 00050 if (error) 00051 *error = "Ran out of data reading vbr_uint for symtab length!"; 00052 return false; 00053 } 00054 if (At + length > End) { 00055 if (error) 00056 *error = "Malformed symbol table: length not consistent with size"; 00057 return false; 00058 } 00059 // we don't care if it can't be inserted (duplicate entry) 00060 symTab.insert(std::make_pair(std::string(At, length), offset)); 00061 At += length; 00062 } 00063 symTabSize = size; 00064 return true; 00065 } 00066 00067 // This member parses an ArchiveMemberHeader that is presumed to be pointed to 00068 // by At. The At pointer is updated to the byte just after the header, which 00069 // can be variable in size. 00070 ArchiveMember* 00071 Archive::parseMemberHeader(const char*& At, const char* End, std::string* error) 00072 { 00073 if (At + sizeof(ArchiveMemberHeader) >= End) { 00074 if (error) 00075 *error = "Unexpected end of file"; 00076 return 0; 00077 } 00078 00079 // Cast archive member header 00080 ArchiveMemberHeader* Hdr = (ArchiveMemberHeader*)At; 00081 At += sizeof(ArchiveMemberHeader); 00082 00083 // Extract the size and determine if the file is 00084 // compressed or not (negative length). 00085 int flags = 0; 00086 int MemberSize = atoi(Hdr->size); 00087 if (MemberSize < 0) { 00088 flags |= ArchiveMember::CompressedFlag; 00089 MemberSize = -MemberSize; 00090 } 00091 00092 // Check the size of the member for sanity 00093 if (At + MemberSize > End) { 00094 if (error) 00095 *error = "invalid member length in archive file"; 00096 return 0; 00097 } 00098 00099 // Check the member signature 00100 if (!Hdr->checkSignature()) { 00101 if (error) 00102 *error = "invalid file member signature"; 00103 return 0; 00104 } 00105 00106 // Convert and check the member name 00107 // The empty name ( '/' and 15 blanks) is for a foreign (non-LLVM) symbol 00108 // table. The special name "//" and 14 blanks is for a string table, used 00109 // for long file names. This library doesn't generate either of those but 00110 // it will accept them. If the name starts with #1/ and the remainder is 00111 // digits, then those digits specify the length of the name that is 00112 // stored immediately following the header. The special name 00113 // __LLVM_SYM_TAB__ identifies the symbol table for LLVM bitcode. 00114 // Anything else is a regular, short filename that is terminated with 00115 // a '/' and blanks. 00116 00117 std::string pathname; 00118 switch (Hdr->name[0]) { 00119 case '#': 00120 if (Hdr->name[1] == '1' && Hdr->name[2] == '/') { 00121 if (isdigit(Hdr->name[3])) { 00122 unsigned len = atoi(&Hdr->name[3]); 00123 pathname.assign(At, len); 00124 At += len; 00125 MemberSize -= len; 00126 flags |= ArchiveMember::HasLongFilenameFlag; 00127 } else { 00128 if (error) 00129 *error = "invalid long filename"; 00130 return 0; 00131 } 00132 } else if (Hdr->name[1] == '_' && 00133 (0 == memcmp(Hdr->name, ARFILE_LLVM_SYMTAB_NAME, 16))) { 00134 // The member is using a long file name (>15 chars) format. 00135 // This format is standard for 4.4BSD and Mac OSX operating 00136 // systems. LLVM uses it similarly. In this format, the 00137 // remainder of the name field (after #1/) specifies the 00138 // length of the file name which occupy the first bytes of 00139 // the member's data. The pathname already has the #1/ stripped. 00140 pathname.assign(ARFILE_LLVM_SYMTAB_NAME); 00141 flags |= ArchiveMember::LLVMSymbolTableFlag; 00142 } 00143 break; 00144 case '/': 00145 if (Hdr->name[1]== '/') { 00146 if (0 == memcmp(Hdr->name, ARFILE_STRTAB_NAME, 16)) { 00147 pathname.assign(ARFILE_STRTAB_NAME); 00148 flags |= ArchiveMember::StringTableFlag; 00149 } else { 00150 if (error) 00151 *error = "invalid string table name"; 00152 return 0; 00153 } 00154 } else if (Hdr->name[1] == ' ') { 00155 if (0 == memcmp(Hdr->name, ARFILE_SVR4_SYMTAB_NAME, 16)) { 00156 pathname.assign(ARFILE_SVR4_SYMTAB_NAME); 00157 flags |= ArchiveMember::SVR4SymbolTableFlag; 00158 } else { 00159 if (error) 00160 *error = "invalid SVR4 symbol table name"; 00161 return 0; 00162 } 00163 } else if (isdigit(Hdr->name[1])) { 00164 unsigned index = atoi(&Hdr->name[1]); 00165 if (index < strtab.length()) { 00166 const char* namep = strtab.c_str() + index; 00167 const char* endp = strtab.c_str() + strtab.length(); 00168 const char* p = namep; 00169 const char* last_p = p; 00170 while (p < endp) { 00171 if (*p == '\n' && *last_p == '/') { 00172 pathname.assign(namep, last_p - namep); 00173 flags |= ArchiveMember::HasLongFilenameFlag; 00174 break; 00175 } 00176 last_p = p; 00177 p++; 00178 } 00179 if (p >= endp) { 00180 if (error) 00181 *error = "missing name termiantor in string table"; 00182 return 0; 00183 } 00184 } else { 00185 if (error) 00186 *error = "name index beyond string table"; 00187 return 0; 00188 } 00189 } 00190 break; 00191 case '_': 00192 if (Hdr->name[1] == '_' && 00193 (0 == memcmp(Hdr->name, ARFILE_BSD4_SYMTAB_NAME, 16))) { 00194 pathname.assign(ARFILE_BSD4_SYMTAB_NAME); 00195 flags |= ArchiveMember::BSD4SymbolTableFlag; 00196 break; 00197 } 00198 /* FALL THROUGH */ 00199 00200 default: 00201 char* slash = (char*) memchr(Hdr->name, '/', 16); 00202 if (slash == 0) 00203 slash = Hdr->name + 16; 00204 pathname.assign(Hdr->name, slash - Hdr->name); 00205 break; 00206 } 00207 00208 // Determine if this is a bitcode file 00209 switch (sys::IdentifyFileType(At, 4)) { 00210 case sys::Bitcode_FileType: 00211 flags |= ArchiveMember::BitcodeFlag; 00212 break; 00213 default: 00214 flags &= ~ArchiveMember::BitcodeFlag; 00215 break; 00216 } 00217 00218 // Instantiate the ArchiveMember to be filled 00219 ArchiveMember* member = new ArchiveMember(this); 00220 00221 // Fill in fields of the ArchiveMember 00222 member->parent = this; 00223 member->path.set(pathname); 00224 member->info.fileSize = MemberSize; 00225 member->info.modTime.fromEpochTime(atoi(Hdr->date)); 00226 unsigned int mode; 00227 sscanf(Hdr->mode, "%o", &mode); 00228 member->info.mode = mode; 00229 member->info.user = atoi(Hdr->uid); 00230 member->info.group = atoi(Hdr->gid); 00231 member->flags = flags; 00232 member->data = At; 00233 00234 return member; 00235 } 00236 00237 bool 00238 Archive::checkSignature(std::string* error) { 00239 // Check the magic string at file's header 00240 if (mapfile->getBufferSize() < 8 || memcmp(base, ARFILE_MAGIC, 8)) { 00241 if (error) 00242 *error = "invalid signature for an archive file"; 00243 return false; 00244 } 00245 return true; 00246 } 00247 00248 // This function loads the entire archive and fully populates its ilist with 00249 // the members of the archive file. This is typically used in preparation for 00250 // editing the contents of the archive. 00251 bool 00252 Archive::loadArchive(std::string* error) { 00253 00254 // Set up parsing 00255 members.clear(); 00256 symTab.clear(); 00257 const char *At = base; 00258 const char *End = mapfile->getBufferEnd(); 00259 00260 if (!checkSignature(error)) 00261 return false; 00262 00263 At += 8; // Skip the magic string. 00264 00265 bool seenSymbolTable = false; 00266 bool foundFirstFile = false; 00267 while (At < End) { 00268 // parse the member header 00269 const char* Save = At; 00270 ArchiveMember* mbr = parseMemberHeader(At, End, error); 00271 if (!mbr) 00272 return false; 00273 00274 // check if this is the foreign symbol table 00275 if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) { 00276 // We just save this but don't do anything special 00277 // with it. It doesn't count as the "first file". 00278 if (foreignST) { 00279 // What? Multiple foreign symbol tables? Just chuck it 00280 // and retain the last one found. 00281 delete foreignST; 00282 } 00283 foreignST = mbr; 00284 At += mbr->getSize(); 00285 if ((intptr_t(At) & 1) == 1) 00286 At++; 00287 } else if (mbr->isStringTable()) { 00288 // Simply suck the entire string table into a string 00289 // variable. This will be used to get the names of the 00290 // members that use the "/ddd" format for their names 00291 // (SVR4 style long names). 00292 strtab.assign(At, mbr->getSize()); 00293 At += mbr->getSize(); 00294 if ((intptr_t(At) & 1) == 1) 00295 At++; 00296 delete mbr; 00297 } else if (mbr->isLLVMSymbolTable()) { 00298 // This is the LLVM symbol table for the archive. If we've seen it 00299 // already, its an error. Otherwise, parse the symbol table and move on. 00300 if (seenSymbolTable) { 00301 if (error) 00302 *error = "invalid archive: multiple symbol tables"; 00303 return false; 00304 } 00305 if (!parseSymbolTable(mbr->getData(), mbr->getSize(), error)) 00306 return false; 00307 seenSymbolTable = true; 00308 At += mbr->getSize(); 00309 if ((intptr_t(At) & 1) == 1) 00310 At++; 00311 delete mbr; // We don't need this member in the list of members. 00312 } else { 00313 // This is just a regular file. If its the first one, save its offset. 00314 // Otherwise just push it on the list and move on to the next file. 00315 if (!foundFirstFile) { 00316 firstFileOffset = Save - base; 00317 foundFirstFile = true; 00318 } 00319 members.push_back(mbr); 00320 At += mbr->getSize(); 00321 if ((intptr_t(At) & 1) == 1) 00322 At++; 00323 } 00324 } 00325 return true; 00326 } 00327 00328 // Open and completely load the archive file. 00329 Archive* 00330 Archive::OpenAndLoad(const sys::Path& file, LLVMContext& C, 00331 std::string* ErrorMessage) { 00332 std::auto_ptr<Archive> result ( new Archive(file, C)); 00333 if (result->mapToMemory(ErrorMessage)) 00334 return 0; 00335 if (!result->loadArchive(ErrorMessage)) 00336 return 0; 00337 return result.release(); 00338 } 00339 00340 // Get all the bitcode modules from the archive 00341 bool 00342 Archive::getAllModules(std::vector<Module*>& Modules, 00343 std::string* ErrMessage) { 00344 00345 for (iterator I=begin(), E=end(); I != E; ++I) { 00346 if (I->isBitcode()) { 00347 std::string FullMemberName = archPath.toString() + 00348 "(" + I->getPath().toString() + ")"; 00349 MemoryBuffer *Buffer = 00350 MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str()); 00351 memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize()); 00352 00353 Module *M = ParseBitcodeFile(Buffer, Context, ErrMessage); 00354 delete Buffer; 00355 if (!M) 00356 return true; 00357 00358 Modules.push_back(M); 00359 } 00360 } 00361 return false; 00362 } 00363 00364 // Load just the symbol table from the archive file 00365 bool 00366 Archive::loadSymbolTable(std::string* ErrorMsg) { 00367 00368 // Set up parsing 00369 members.clear(); 00370 symTab.clear(); 00371 const char *At = base; 00372 const char *End = mapfile->getBufferEnd(); 00373 00374 // Make sure we're dealing with an archive 00375 if (!checkSignature(ErrorMsg)) 00376 return false; 00377 00378 At += 8; // Skip signature 00379 00380 // Parse the first file member header 00381 const char* FirstFile = At; 00382 ArchiveMember* mbr = parseMemberHeader(At, End, ErrorMsg); 00383 if (!mbr) 00384 return false; 00385 00386 if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) { 00387 // Skip the foreign symbol table, we don't do anything with it 00388 At += mbr->getSize(); 00389 if ((intptr_t(At) & 1) == 1) 00390 At++; 00391 delete mbr; 00392 00393 // Read the next one 00394 FirstFile = At; 00395 mbr = parseMemberHeader(At, End, ErrorMsg); 00396 if (!mbr) { 00397 delete mbr; 00398 return false; 00399 } 00400 } 00401 00402 if (mbr->isStringTable()) { 00403 // Process the string table entry 00404 strtab.assign((const char*)mbr->getData(), mbr->getSize()); 00405 At += mbr->getSize(); 00406 if ((intptr_t(At) & 1) == 1) 00407 At++; 00408 delete mbr; 00409 // Get the next one 00410 FirstFile = At; 00411 mbr = parseMemberHeader(At, End, ErrorMsg); 00412 if (!mbr) { 00413 delete mbr; 00414 return false; 00415 } 00416 } 00417 00418 // See if its the symbol table 00419 if (mbr->isLLVMSymbolTable()) { 00420 if (!parseSymbolTable(mbr->getData(), mbr->getSize(), ErrorMsg)) { 00421 delete mbr; 00422 return false; 00423 } 00424 00425 At += mbr->getSize(); 00426 if ((intptr_t(At) & 1) == 1) 00427 At++; 00428 delete mbr; 00429 // Can't be any more symtab headers so just advance 00430 FirstFile = At; 00431 } else { 00432 // There's no symbol table in the file. We have to rebuild it from scratch 00433 // because the intent of this method is to get the symbol table loaded so 00434 // it can be searched efficiently. 00435 // Add the member to the members list 00436 members.push_back(mbr); 00437 } 00438 00439 firstFileOffset = FirstFile - base; 00440 return true; 00441 } 00442 00443 // Open the archive and load just the symbol tables 00444 Archive* Archive::OpenAndLoadSymbols(const sys::Path& file, 00445 LLVMContext& C, 00446 std::string* ErrorMessage) { 00447 std::auto_ptr<Archive> result ( new Archive(file, C) ); 00448 if (result->mapToMemory(ErrorMessage)) 00449 return 0; 00450 if (!result->loadSymbolTable(ErrorMessage)) 00451 return 0; 00452 return result.release(); 00453 } 00454 00455 // Look up one symbol in the symbol table and return a ModuleProvider for the 00456 // module that defines that symbol. 00457 ModuleProvider* 00458 Archive::findModuleDefiningSymbol(const std::string& symbol, 00459 std::string* ErrMsg) { 00460 SymTabType::iterator SI = symTab.find(symbol); 00461 if (SI == symTab.end()) 00462 return 0; 00463 00464 // The symbol table was previously constructed assuming that the members were 00465 // written without the symbol table header. Because VBR encoding is used, the 00466 // values could not be adjusted to account for the offset of the symbol table 00467 // because that could affect the size of the symbol table due to VBR encoding. 00468 // We now have to account for this by adjusting the offset by the size of the 00469 // symbol table and its header. 00470 unsigned fileOffset = 00471 SI->second + // offset in symbol-table-less file 00472 firstFileOffset; // add offset to first "real" file in archive 00473 00474 // See if the module is already loaded 00475 ModuleMap::iterator MI = modules.find(fileOffset); 00476 if (MI != modules.end()) 00477 return MI->second.first; 00478 00479 // Module hasn't been loaded yet, we need to load it 00480 const char* modptr = base + fileOffset; 00481 ArchiveMember* mbr = parseMemberHeader(modptr, mapfile->getBufferEnd(), 00482 ErrMsg); 00483 if (!mbr) 00484 return 0; 00485 00486 // Now, load the bitcode module to get the ModuleProvider 00487 std::string FullMemberName = archPath.toString() + "(" + 00488 mbr->getPath().toString() + ")"; 00489 MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(mbr->getSize(), 00490 FullMemberName.c_str()); 00491 memcpy((char*)Buffer->getBufferStart(), mbr->getData(), mbr->getSize()); 00492 00493 ModuleProvider *mp = getBitcodeModuleProvider(Buffer, Context, ErrMsg); 00494 if (!mp) 00495 return 0; 00496 00497 modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr))); 00498 00499 return mp; 00500 } 00501 00502 // Look up multiple symbols in the symbol table and return a set of 00503 // ModuleProviders that define those symbols. 00504 bool 00505 Archive::findModulesDefiningSymbols(std::set<std::string>& symbols, 00506 std::set<ModuleProvider*>& result, 00507 std::string* error) { 00508 if (!mapfile || !base) { 00509 if (error) 00510 *error = "Empty archive invalid for finding modules defining symbols"; 00511 return false; 00512 } 00513 00514 if (symTab.empty()) { 00515 // We don't have a symbol table, so we must build it now but lets also 00516 // make sure that we populate the modules table as we do this to ensure 00517 // that we don't load them twice when findModuleDefiningSymbol is called 00518 // below. 00519 00520 // Get a pointer to the first file 00521 const char* At = base + firstFileOffset; 00522 const char* End = mapfile->getBufferEnd(); 00523 00524 while ( At < End) { 00525 // Compute the offset to be put in the symbol table 00526 unsigned offset = At - base - firstFileOffset; 00527 00528 // Parse the file's header 00529 ArchiveMember* mbr = parseMemberHeader(At, End, error); 00530 if (!mbr) 00531 return false; 00532 00533 // If it contains symbols 00534 if (mbr->isBitcode()) { 00535 // Get the symbols 00536 std::vector<std::string> symbols; 00537 std::string FullMemberName = archPath.toString() + "(" + 00538 mbr->getPath().toString() + ")"; 00539 ModuleProvider* MP = 00540 GetBitcodeSymbols((const unsigned char*)At, mbr->getSize(), 00541 FullMemberName, Context, symbols, error); 00542 00543 if (MP) { 00544 // Insert the module's symbols into the symbol table 00545 for (std::vector<std::string>::iterator I = symbols.begin(), 00546 E=symbols.end(); I != E; ++I ) { 00547 symTab.insert(std::make_pair(*I, offset)); 00548 } 00549 // Insert the ModuleProvider and the ArchiveMember into the table of 00550 // modules. 00551 modules.insert(std::make_pair(offset, std::make_pair(MP, mbr))); 00552 } else { 00553 if (error) 00554 *error = "Can't parse bitcode member: " + 00555 mbr->getPath().toString() + ": " + *error; 00556 delete mbr; 00557 return false; 00558 } 00559 } 00560 00561 // Go to the next file location 00562 At += mbr->getSize(); 00563 if ((intptr_t(At) & 1) == 1) 00564 At++; 00565 } 00566 } 00567 00568 // At this point we have a valid symbol table (one way or another) so we 00569 // just use it to quickly find the symbols requested. 00570 00571 for (std::set<std::string>::iterator I=symbols.begin(), 00572 E=symbols.end(); I != E;) { 00573 // See if this symbol exists 00574 ModuleProvider* mp = findModuleDefiningSymbol(*I,error); 00575 if (mp) { 00576 // The symbol exists, insert the ModuleProvider into our result, 00577 // duplicates wil be ignored 00578 result.insert(mp); 00579 00580 // Remove the symbol now that its been resolved, being careful to 00581 // post-increment the iterator. 00582 symbols.erase(I++); 00583 } else { 00584 ++I; 00585 } 00586 } 00587 return true; 00588 } 00589 00590 bool Archive::isBitcodeArchive() { 00591 // Make sure the symTab has been loaded. In most cases this should have been 00592 // done when the archive was constructed, but still, this is just in case. 00593 if (symTab.empty()) 00594 if (!loadSymbolTable(0)) 00595 return false; 00596 00597 // Now that we know it's been loaded, return true 00598 // if it has a size 00599 if (symTab.size()) return true; 00600 00601 // We still can't be sure it isn't a bitcode archive 00602 if (!loadArchive(0)) 00603 return false; 00604 00605 std::vector<Module *> Modules; 00606 std::string ErrorMessage; 00607 00608 // Scan the archive, trying to load a bitcode member. We only load one to 00609 // see if this works. 00610 for (iterator I = begin(), E = end(); I != E; ++I) { 00611 if (!I->isBitcode()) 00612 continue; 00613 00614 std::string FullMemberName = 00615 archPath.toString() + "(" + I->getPath().toString() + ")"; 00616 00617 MemoryBuffer *Buffer = 00618 MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str()); 00619 memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize()); 00620 Module *M = ParseBitcodeFile(Buffer, Context); 00621 delete Buffer; 00622 if (!M) 00623 return false; // Couldn't parse bitcode, not a bitcode archive. 00624 delete M; 00625 return true; 00626 } 00627 00628 return false; 00629 }