LLVM API Documentation

ArchiveReader.cpp

Go to the documentation of this file.
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, std::string* ErrorMessage) 
00331 {
00332   std::auto_ptr<Archive> result ( new Archive(file));
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, std::string* ErrMessage) {
00343 
00344   for (iterator I=begin(), E=end(); I != E; ++I) {
00345     if (I->isBitcode()) {
00346       std::string FullMemberName = archPath.toString() +
00347         "(" + I->getPath().toString() + ")";
00348       MemoryBuffer *Buffer =
00349         MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
00350       memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
00351       
00352       Module *M = ParseBitcodeFile(Buffer, ErrMessage);
00353       delete Buffer;
00354       if (!M)
00355         return true;
00356 
00357       Modules.push_back(M);
00358     }
00359   }
00360   return false;
00361 }
00362 
00363 // Load just the symbol table from the archive file
00364 bool
00365 Archive::loadSymbolTable(std::string* ErrorMsg) {
00366 
00367   // Set up parsing
00368   members.clear();
00369   symTab.clear();
00370   const char *At = base;
00371   const char *End = mapfile->getBufferEnd();
00372 
00373   // Make sure we're dealing with an archive
00374   if (!checkSignature(ErrorMsg))
00375     return false;
00376 
00377   At += 8; // Skip signature
00378 
00379   // Parse the first file member header
00380   const char* FirstFile = At;
00381   ArchiveMember* mbr = parseMemberHeader(At, End, ErrorMsg);
00382   if (!mbr)
00383     return false;
00384 
00385   if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
00386     // Skip the foreign symbol table, we don't do anything with it
00387     At += mbr->getSize();
00388     if ((intptr_t(At) & 1) == 1)
00389       At++;
00390     delete mbr;
00391 
00392     // Read the next one
00393     FirstFile = At;
00394     mbr = parseMemberHeader(At, End, ErrorMsg);
00395     if (!mbr) {
00396       delete mbr;
00397       return false;
00398     }
00399   }
00400 
00401   if (mbr->isStringTable()) {
00402     // Process the string table entry
00403     strtab.assign((const char*)mbr->getData(), mbr->getSize());
00404     At += mbr->getSize();
00405     if ((intptr_t(At) & 1) == 1)
00406       At++;
00407     delete mbr;
00408     // Get the next one
00409     FirstFile = At;
00410     mbr = parseMemberHeader(At, End, ErrorMsg);
00411     if (!mbr) {
00412       delete mbr;
00413       return false;
00414     }
00415   }
00416 
00417   // See if its the symbol table
00418   if (mbr->isLLVMSymbolTable()) {
00419     if (!parseSymbolTable(mbr->getData(), mbr->getSize(), ErrorMsg)) {
00420       delete mbr;
00421       return false;
00422     }
00423 
00424     At += mbr->getSize();
00425     if ((intptr_t(At) & 1) == 1)
00426       At++;
00427     delete mbr;
00428     // Can't be any more symtab headers so just advance
00429     FirstFile = At;
00430   } else {
00431     // There's no symbol table in the file. We have to rebuild it from scratch
00432     // because the intent of this method is to get the symbol table loaded so
00433     // it can be searched efficiently.
00434     // Add the member to the members list
00435     members.push_back(mbr);
00436   }
00437 
00438   firstFileOffset = FirstFile - base;
00439   return true;
00440 }
00441 
00442 // Open the archive and load just the symbol tables
00443 Archive*
00444 Archive::OpenAndLoadSymbols(const sys::Path& file, std::string* ErrorMessage) {
00445   std::auto_ptr<Archive> result ( new Archive(file) );
00446   if (result->mapToMemory(ErrorMessage))
00447     return 0;
00448   if (!result->loadSymbolTable(ErrorMessage))
00449     return 0;
00450   return result.release();
00451 }
00452 
00453 // Look up one symbol in the symbol table and return a ModuleProvider for the
00454 // module that defines that symbol.
00455 ModuleProvider*
00456 Archive::findModuleDefiningSymbol(const std::string& symbol, 
00457                                   std::string* ErrMsg) {
00458   SymTabType::iterator SI = symTab.find(symbol);
00459   if (SI == symTab.end())
00460     return 0;
00461 
00462   // The symbol table was previously constructed assuming that the members were
00463   // written without the symbol table header. Because VBR encoding is used, the
00464   // values could not be adjusted to account for the offset of the symbol table
00465   // because that could affect the size of the symbol table due to VBR encoding.
00466   // We now have to account for this by adjusting the offset by the size of the
00467   // symbol table and its header.
00468   unsigned fileOffset =
00469     SI->second +                // offset in symbol-table-less file
00470     firstFileOffset;            // add offset to first "real" file in archive
00471 
00472   // See if the module is already loaded
00473   ModuleMap::iterator MI = modules.find(fileOffset);
00474   if (MI != modules.end())
00475     return MI->second.first;
00476 
00477   // Module hasn't been loaded yet, we need to load it
00478   const char* modptr = base + fileOffset;
00479   ArchiveMember* mbr = parseMemberHeader(modptr, mapfile->getBufferEnd(),
00480                                          ErrMsg);
00481   if (!mbr)
00482     return 0;
00483 
00484   // Now, load the bitcode module to get the ModuleProvider
00485   std::string FullMemberName = archPath.toString() + "(" +
00486     mbr->getPath().toString() + ")";
00487   MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(mbr->getSize(),
00488                                                       FullMemberName.c_str());
00489   memcpy((char*)Buffer->getBufferStart(), mbr->getData(), mbr->getSize());
00490   
00491   ModuleProvider *mp = getBitcodeModuleProvider(Buffer, ErrMsg);
00492   if (!mp)
00493     return 0;
00494 
00495   modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr)));
00496 
00497   return mp;
00498 }
00499 
00500 // Look up multiple symbols in the symbol table and return a set of
00501 // ModuleProviders that define those symbols.
00502 bool
00503 Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
00504                                     std::set<ModuleProvider*>& result,
00505                                     std::string* error) {
00506   if (!mapfile || !base) {
00507     if (error)
00508       *error = "Empty archive invalid for finding modules defining symbols";
00509     return false;
00510   }
00511 
00512   if (symTab.empty()) {
00513     // We don't have a symbol table, so we must build it now but lets also
00514     // make sure that we populate the modules table as we do this to ensure
00515     // that we don't load them twice when findModuleDefiningSymbol is called
00516     // below.
00517 
00518     // Get a pointer to the first file
00519     const char* At  = base + firstFileOffset;
00520     const char* End = mapfile->getBufferEnd();
00521 
00522     while ( At < End) {
00523       // Compute the offset to be put in the symbol table
00524       unsigned offset = At - base - firstFileOffset;
00525 
00526       // Parse the file's header
00527       ArchiveMember* mbr = parseMemberHeader(At, End, error);
00528       if (!mbr)
00529         return false;
00530 
00531       // If it contains symbols
00532       if (mbr->isBitcode()) {
00533         // Get the symbols
00534         std::vector<std::string> symbols;
00535         std::string FullMemberName = archPath.toString() + "(" +
00536           mbr->getPath().toString() + ")";
00537         ModuleProvider* MP = 
00538           GetBitcodeSymbols((const unsigned char*)At, mbr->getSize(),
00539                             FullMemberName, symbols, error);
00540 
00541         if (MP) {
00542           // Insert the module's symbols into the symbol table
00543           for (std::vector<std::string>::iterator I = symbols.begin(),
00544                E=symbols.end(); I != E; ++I ) {
00545             symTab.insert(std::make_pair(*I, offset));
00546           }
00547           // Insert the ModuleProvider and the ArchiveMember into the table of
00548           // modules.
00549           modules.insert(std::make_pair(offset, std::make_pair(MP, mbr)));
00550         } else {
00551           if (error)
00552             *error = "Can't parse bitcode member: " + 
00553               mbr->getPath().toString() + ": " + *error;
00554           delete mbr;
00555           return false;
00556         }
00557       }
00558 
00559       // Go to the next file location
00560       At += mbr->getSize();
00561       if ((intptr_t(At) & 1) == 1)
00562         At++;
00563     }
00564   }
00565 
00566   // At this point we have a valid symbol table (one way or another) so we
00567   // just use it to quickly find the symbols requested.
00568 
00569   for (std::set<std::string>::iterator I=symbols.begin(),
00570        E=symbols.end(); I != E;) {
00571     // See if this symbol exists
00572     ModuleProvider* mp = findModuleDefiningSymbol(*I,error);
00573     if (mp) {
00574       // The symbol exists, insert the ModuleProvider into our result,
00575       // duplicates wil be ignored
00576       result.insert(mp);
00577 
00578       // Remove the symbol now that its been resolved, being careful to
00579       // post-increment the iterator.
00580       symbols.erase(I++);
00581     } else {
00582       ++I;
00583     }
00584   }
00585   return true;
00586 }
00587 
00588 bool Archive::isBitcodeArchive() {
00589   // Make sure the symTab has been loaded. In most cases this should have been
00590   // done when the archive was constructed, but still,  this is just in case.
00591   if (symTab.empty())
00592     if (!loadSymbolTable(0))
00593       return false;
00594 
00595   // Now that we know it's been loaded, return true
00596   // if it has a size
00597   if (symTab.size()) return true;
00598 
00599   // We still can't be sure it isn't a bitcode archive
00600   if (!loadArchive(0))
00601     return false;
00602 
00603   std::vector<Module *> Modules;
00604   std::string ErrorMessage;
00605 
00606   // Scan the archive, trying to load a bitcode member.  We only load one to
00607   // see if this works.
00608   for (iterator I = begin(), E = end(); I != E; ++I) {
00609     if (!I->isBitcode())
00610       continue;
00611     
00612     std::string FullMemberName = 
00613       archPath.toString() + "(" + I->getPath().toString() + ")";
00614 
00615     MemoryBuffer *Buffer =
00616       MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
00617     memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
00618     Module *M = ParseBitcodeFile(Buffer);
00619     delete Buffer;
00620     if (!M)
00621       return false;  // Couldn't parse bitcode, not a bitcode archive.
00622     delete M;
00623     return true;
00624   }
00625   
00626   return false;
00627 }