LLVM API Documentation
00001 //===-- Archive.cpp - Generic LLVM archive functions ------------*- 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 // This file contains the implementation of the Archive and ArchiveMember 00011 // classes that is common to both reading and writing archives.. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "ArchiveInternals.h" 00016 #include "llvm/Bitcode/ReaderWriter.h" 00017 #include "llvm/ModuleProvider.h" 00018 #include "llvm/Module.h" 00019 #include "llvm/Support/MemoryBuffer.h" 00020 #include "llvm/System/Process.h" 00021 #include <memory> 00022 #include <cstring> 00023 using namespace llvm; 00024 00025 // getMemberSize - compute the actual physical size of the file member as seen 00026 // on disk. This isn't the size of member's payload. Use getSize() for that. 00027 unsigned 00028 ArchiveMember::getMemberSize() const { 00029 // Basically its the file size plus the header size 00030 unsigned result = info.fileSize + sizeof(ArchiveMemberHeader); 00031 00032 // If it has a long filename, include the name length 00033 if (hasLongFilename()) 00034 result += path.toString().length() + 1; 00035 00036 // If its now odd lengthed, include the padding byte 00037 if (result % 2 != 0 ) 00038 result++; 00039 00040 return result; 00041 } 00042 00043 // This default constructor is only use by the ilist when it creates its 00044 // sentry node. We give it specific static values to make it stand out a bit. 00045 ArchiveMember::ArchiveMember() 00046 : parent(0), path("--invalid--"), flags(0), data(0) 00047 { 00048 info.user = sys::Process::GetCurrentUserId(); 00049 info.group = sys::Process::GetCurrentGroupId(); 00050 info.mode = 0777; 00051 info.fileSize = 0; 00052 info.modTime = sys::TimeValue::now(); 00053 } 00054 00055 // This is the constructor that the Archive class uses when it is building or 00056 // reading an archive. It just defaults a few things and ensures the parent is 00057 // set for the iplist. The Archive class fills in the ArchiveMember's data. 00058 // This is required because correctly setting the data may depend on other 00059 // things in the Archive. 00060 ArchiveMember::ArchiveMember(Archive* PAR) 00061 : parent(PAR), path(), flags(0), data(0) 00062 { 00063 } 00064 00065 // This method allows an ArchiveMember to be replaced with the data for a 00066 // different file, presumably as an update to the member. It also makes sure 00067 // the flags are reset correctly. 00068 bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) { 00069 if (!newFile.exists()) { 00070 if (ErrMsg) 00071 *ErrMsg = "Can not replace an archive member with a non-existent file"; 00072 return true; 00073 } 00074 00075 data = 0; 00076 path = newFile; 00077 00078 // SVR4 symbol tables have an empty name 00079 if (path.toString() == ARFILE_SVR4_SYMTAB_NAME) 00080 flags |= SVR4SymbolTableFlag; 00081 else 00082 flags &= ~SVR4SymbolTableFlag; 00083 00084 // BSD4.4 symbol tables have a special name 00085 if (path.toString() == ARFILE_BSD4_SYMTAB_NAME) 00086 flags |= BSD4SymbolTableFlag; 00087 else 00088 flags &= ~BSD4SymbolTableFlag; 00089 00090 // LLVM symbol tables have a very specific name 00091 if (path.toString() == ARFILE_LLVM_SYMTAB_NAME) 00092 flags |= LLVMSymbolTableFlag; 00093 else 00094 flags &= ~LLVMSymbolTableFlag; 00095 00096 // String table name 00097 if (path.toString() == ARFILE_STRTAB_NAME) 00098 flags |= StringTableFlag; 00099 else 00100 flags &= ~StringTableFlag; 00101 00102 // If it has a slash then it has a path 00103 bool hasSlash = path.toString().find('/') != std::string::npos; 00104 if (hasSlash) 00105 flags |= HasPathFlag; 00106 else 00107 flags &= ~HasPathFlag; 00108 00109 // If it has a slash or its over 15 chars then its a long filename format 00110 if (hasSlash || path.toString().length() > 15) 00111 flags |= HasLongFilenameFlag; 00112 else 00113 flags &= ~HasLongFilenameFlag; 00114 00115 // Get the signature and status info 00116 const char* signature = (const char*) data; 00117 std::string magic; 00118 if (!signature) { 00119 path.getMagicNumber(magic,4); 00120 signature = magic.c_str(); 00121 std::string err; 00122 const sys::FileStatus *FSinfo = path.getFileStatus(false, ErrMsg); 00123 if (FSinfo) 00124 info = *FSinfo; 00125 else 00126 return true; 00127 } 00128 00129 // Determine what kind of file it is. 00130 switch (sys::IdentifyFileType(signature,4)) { 00131 case sys::Bitcode_FileType: 00132 flags |= BitcodeFlag; 00133 break; 00134 default: 00135 flags &= ~BitcodeFlag; 00136 break; 00137 } 00138 return false; 00139 } 00140 00141 // Archive constructor - this is the only constructor that gets used for the 00142 // Archive class. Everything else (default,copy) is deprecated. This just 00143 // initializes and maps the file into memory, if requested. 00144 Archive::Archive(const sys::Path& filename, LLVMContext& C) 00145 : archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(), 00146 symTabSize(0), firstFileOffset(0), modules(), foreignST(0), Context(C) { 00147 } 00148 00149 bool 00150 Archive::mapToMemory(std::string* ErrMsg) { 00151 mapfile = MemoryBuffer::getFile(archPath.c_str(), ErrMsg); 00152 if (mapfile == 0) 00153 return true; 00154 base = mapfile->getBufferStart(); 00155 return false; 00156 } 00157 00158 void Archive::cleanUpMemory() { 00159 // Shutdown the file mapping 00160 delete mapfile; 00161 mapfile = 0; 00162 base = 0; 00163 00164 // Forget the entire symbol table 00165 symTab.clear(); 00166 symTabSize = 0; 00167 00168 firstFileOffset = 0; 00169 00170 // Free the foreign symbol table member 00171 if (foreignST) { 00172 delete foreignST; 00173 foreignST = 0; 00174 } 00175 00176 // Delete any ModuleProviders and ArchiveMember's we've allocated as a result 00177 // of symbol table searches. 00178 for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) { 00179 delete I->second.first; 00180 delete I->second.second; 00181 } 00182 } 00183 00184 // Archive destructor - just clean up memory 00185 Archive::~Archive() { 00186 cleanUpMemory(); 00187 } 00188 00189 00190 00191 static void getSymbols(Module*M, std::vector<std::string>& symbols) { 00192 // Loop over global variables 00193 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI) 00194 if (!GI->isDeclaration() && !GI->hasLocalLinkage()) 00195 if (!GI->getName().empty()) 00196 symbols.push_back(GI->getName()); 00197 00198 // Loop over functions 00199 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) 00200 if (!FI->isDeclaration() && !FI->hasLocalLinkage()) 00201 if (!FI->getName().empty()) 00202 symbols.push_back(FI->getName()); 00203 00204 // Loop over aliases 00205 for (Module::alias_iterator AI = M->alias_begin(), AE = M->alias_end(); 00206 AI != AE; ++AI) { 00207 if (AI->hasName()) 00208 symbols.push_back(AI->getName()); 00209 } 00210 } 00211 00212 // Get just the externally visible defined symbols from the bitcode 00213 bool llvm::GetBitcodeSymbols(const sys::Path& fName, 00214 LLVMContext& Context, 00215 std::vector<std::string>& symbols, 00216 std::string* ErrMsg) { 00217 std::auto_ptr<MemoryBuffer> Buffer( 00218 MemoryBuffer::getFileOrSTDIN(fName.c_str())); 00219 if (!Buffer.get()) { 00220 if (ErrMsg) *ErrMsg = "Could not open file '" + fName.toString() + "'"; 00221 return true; 00222 } 00223 00224 ModuleProvider *MP = getBitcodeModuleProvider(Buffer.get(), Context, ErrMsg); 00225 if (!MP) 00226 return true; 00227 00228 // Get the module from the provider 00229 Module* M = MP->materializeModule(); 00230 if (M == 0) { 00231 delete MP; 00232 return true; 00233 } 00234 00235 // Get the symbols 00236 getSymbols(M, symbols); 00237 00238 // Done with the module. 00239 delete MP; 00240 return true; 00241 } 00242 00243 ModuleProvider* 00244 llvm::GetBitcodeSymbols(const unsigned char *BufPtr, unsigned Length, 00245 const std::string& ModuleID, 00246 LLVMContext& Context, 00247 std::vector<std::string>& symbols, 00248 std::string* ErrMsg) { 00249 // Get the module provider 00250 MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(Length, ModuleID.c_str()); 00251 memcpy((char*)Buffer->getBufferStart(), BufPtr, Length); 00252 00253 ModuleProvider *MP = getBitcodeModuleProvider(Buffer, Context, ErrMsg); 00254 if (!MP) 00255 return 0; 00256 00257 // Get the module from the provider 00258 Module* M = MP->materializeModule(); 00259 if (M == 0) { 00260 delete MP; 00261 return 0; 00262 } 00263 00264 // Get the symbols 00265 getSymbols(M, symbols); 00266 00267 // Done with the module. Note that ModuleProvider will delete the 00268 // Module when it is deleted. Also note that its the caller's responsibility 00269 // to delete the ModuleProvider. 00270 return MP; 00271 }