Reid Spencer | 362cbf0 | 2004-11-06 08:51:45 +0000 | [diff] [blame] | 1 | //===-- Archive.cpp - Generic LLVM archive functions ------------*- C++ -*-===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 362cbf0 | 2004-11-06 08:51:45 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | 362cbf0 | 2004-11-06 08:51:45 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 362cbf0 | 2004-11-06 08:51:45 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 10 | // This file contains the implementation of the Archive and ArchiveMember |
| 11 | // classes that is common to both reading and writing archives.. |
Reid Spencer | 362cbf0 | 2004-11-06 08:51:45 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "ArchiveInternals.h" |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 16 | #include "llvm/ModuleProvider.h" |
Reid Spencer | 8e827e8 | 2005-04-21 17:49:57 +0000 | [diff] [blame] | 17 | #include "llvm/System/Process.h" |
Reid Spencer | 362cbf0 | 2004-11-06 08:51:45 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 21 | // getMemberSize - compute the actual physical size of the file member as seen |
| 22 | // on disk. This isn't the size of member's payload. Use getSize() for that. |
| 23 | unsigned |
| 24 | ArchiveMember::getMemberSize() const { |
| 25 | // Basically its the file size plus the header size |
| 26 | unsigned result = info.fileSize + sizeof(ArchiveMemberHeader); |
| 27 | |
| 28 | // If it has a long filename, include the name length |
| 29 | if (hasLongFilename()) |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 30 | result += path.toString().length() + 1; |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 31 | |
| 32 | // If its now odd lengthed, include the padding byte |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 33 | if (result % 2 != 0 ) |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 34 | result++; |
| 35 | |
| 36 | return result; |
Reid Spencer | 362cbf0 | 2004-11-06 08:51:45 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 39 | // This default constructor is only use by the ilist when it creates its |
| 40 | // sentry node. We give it specific static values to make it stand out a bit. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 41 | ArchiveMember::ArchiveMember() |
Jeff Cohen | 943b9b6 | 2006-05-06 23:25:53 +0000 | [diff] [blame] | 42 | : next(0), prev(0), parent(0), path("--invalid--"), flags(0), data(0) |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 43 | { |
Reid Spencer | 8e827e8 | 2005-04-21 17:49:57 +0000 | [diff] [blame] | 44 | info.user = sys::Process::GetCurrentUserId(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 45 | info.group = sys::Process::GetCurrentGroupId(); |
| 46 | info.mode = 0777; |
| 47 | info.fileSize = 0; |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 48 | info.modTime = sys::TimeValue::now(); |
| 49 | } |
| 50 | |
| 51 | // This is the constructor that the Archive class uses when it is building or |
| 52 | // reading an archive. It just defaults a few things and ensures the parent is |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 53 | // set for the iplist. The Archive class fills in the ArchiveMember's data. |
| 54 | // This is required because correctly setting the data may depend on other |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 55 | // things in the Archive. |
| 56 | ArchiveMember::ArchiveMember(Archive* PAR) |
| 57 | : next(0), prev(0), parent(PAR), path(), flags(0), data(0) |
| 58 | { |
| 59 | } |
| 60 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 61 | // This method allows an ArchiveMember to be replaced with the data for a |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 62 | // different file, presumably as an update to the member. It also makes sure |
| 63 | // the flags are reset correctly. |
Reid Spencer | 0ff2d31 | 2006-08-24 23:45:08 +0000 | [diff] [blame] | 64 | bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) { |
Reid Spencer | cd5561a | 2006-12-15 19:44:51 +0000 | [diff] [blame] | 65 | if (!newFile.exists()) { |
| 66 | if (ErrMsg) |
| 67 | *ErrMsg = "Can not replace an archive member with a non-existent file"; |
| 68 | return true; |
| 69 | } |
| 70 | |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 71 | data = 0; |
| 72 | path = newFile; |
| 73 | |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 74 | // SVR4 symbol tables have an empty name |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 75 | if (path.toString() == ARFILE_SVR4_SYMTAB_NAME) |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 76 | flags |= SVR4SymbolTableFlag; |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 77 | else |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 78 | flags &= ~SVR4SymbolTableFlag; |
| 79 | |
| 80 | // BSD4.4 symbol tables have a special name |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 81 | if (path.toString() == ARFILE_BSD4_SYMTAB_NAME) |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 82 | flags |= BSD4SymbolTableFlag; |
| 83 | else |
| 84 | flags &= ~BSD4SymbolTableFlag; |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 85 | |
| 86 | // LLVM symbol tables have a very specific name |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 87 | if (path.toString() == ARFILE_LLVM_SYMTAB_NAME) |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 88 | flags |= LLVMSymbolTableFlag; |
| 89 | else |
| 90 | flags &= ~LLVMSymbolTableFlag; |
| 91 | |
| 92 | // String table name |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 93 | if (path.toString() == ARFILE_STRTAB_NAME) |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 94 | flags |= StringTableFlag; |
| 95 | else |
| 96 | flags &= ~StringTableFlag; |
| 97 | |
| 98 | // If it has a slash then it has a path |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 99 | bool hasSlash = path.toString().find('/') != std::string::npos; |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 100 | if (hasSlash) |
| 101 | flags |= HasPathFlag; |
| 102 | else |
| 103 | flags &= ~HasPathFlag; |
| 104 | |
| 105 | // If it has a slash or its over 15 chars then its a long filename format |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 106 | if (hasSlash || path.toString().length() > 15) |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 107 | flags |= HasLongFilenameFlag; |
| 108 | else |
| 109 | flags &= ~HasLongFilenameFlag; |
| 110 | |
| 111 | // Get the signature and status info |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 112 | const char* signature = (const char*) data; |
Chris Lattner | 252ad03 | 2006-07-28 22:03:44 +0000 | [diff] [blame] | 113 | std::string magic; |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 114 | if (!signature) { |
| 115 | path.getMagicNumber(magic,4); |
| 116 | signature = magic.c_str(); |
Chris Lattner | 252ad03 | 2006-07-28 22:03:44 +0000 | [diff] [blame] | 117 | std::string err; |
Reid Spencer | 0ff2d31 | 2006-08-24 23:45:08 +0000 | [diff] [blame] | 118 | if (path.getFileStatus(info, ErrMsg)) |
| 119 | return true; |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Determine what kind of file it is |
| 123 | switch (sys::IdentifyFileType(signature,4)) { |
| 124 | case sys::BytecodeFileType: |
| 125 | flags |= BytecodeFlag; |
| 126 | break; |
| 127 | case sys::CompressedBytecodeFileType: |
| 128 | flags |= CompressedBytecodeFlag; |
| 129 | flags &= ~CompressedFlag; |
| 130 | break; |
| 131 | default: |
| 132 | flags &= ~(BytecodeFlag|CompressedBytecodeFlag); |
| 133 | break; |
| 134 | } |
Reid Spencer | 0ff2d31 | 2006-08-24 23:45:08 +0000 | [diff] [blame] | 135 | return false; |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Archive constructor - this is the only constructor that gets used for the |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 139 | // Archive class. Everything else (default,copy) is deprecated. This just |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 140 | // initializes and maps the file into memory, if requested. |
Chris Lattner | f2e292c | 2007-02-07 21:41:02 +0000 | [diff] [blame] | 141 | Archive::Archive(const sys::Path& filename, BCDecompressor_t *BCDC) |
Reid Spencer | 1f46580 | 2004-11-16 06:47:07 +0000 | [diff] [blame] | 142 | : archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(), |
Chris Lattner | f2e292c | 2007-02-07 21:41:02 +0000 | [diff] [blame] | 143 | symTabSize(0), firstFileOffset(0), modules(), foreignST(0), |
| 144 | Decompressor(BCDC) { |
Reid Spencer | 0ff2d31 | 2006-08-24 23:45:08 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool |
| 148 | Archive::mapToMemory(std::string* ErrMsg) |
| 149 | { |
| 150 | mapfile = new sys::MappedFile(); |
| 151 | if (mapfile->open(archPath, sys::MappedFile::READ_ACCESS, ErrMsg)) |
| 152 | return true; |
| 153 | if (!(base = (char*) mapfile->map(ErrMsg))) |
| 154 | return true; |
| 155 | return false; |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Reid Spencer | 6ff7240 | 2005-11-30 05:21:10 +0000 | [diff] [blame] | 158 | void Archive::cleanUpMemory() { |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 159 | // Shutdown the file mapping |
| 160 | if (mapfile) { |
Jeff Cohen | d19d89a | 2005-01-28 01:17:07 +0000 | [diff] [blame] | 161 | mapfile->close(); |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 162 | delete mapfile; |
Reid Spencer | 6ff7240 | 2005-11-30 05:21:10 +0000 | [diff] [blame] | 163 | |
| 164 | mapfile = 0; |
| 165 | base = 0; |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 166 | } |
Reid Spencer | 6ff7240 | 2005-11-30 05:21:10 +0000 | [diff] [blame] | 167 | |
| 168 | // Forget the entire symbol table |
| 169 | symTab.clear(); |
| 170 | symTabSize = 0; |
| 171 | |
| 172 | firstFileOffset = 0; |
| 173 | |
| 174 | // Free the foreign symbol table member |
| 175 | if (foreignST) { |
| 176 | delete foreignST; |
| 177 | foreignST = 0; |
| 178 | } |
| 179 | |
Reid Spencer | cf6afc6 | 2004-11-14 21:56:59 +0000 | [diff] [blame] | 180 | // Delete any ModuleProviders and ArchiveMember's we've allocated as a result |
| 181 | // of symbol table searches. |
| 182 | for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) { |
| 183 | delete I->second.first; |
| 184 | delete I->second.second; |
| 185 | } |
Reid Spencer | 362cbf0 | 2004-11-06 08:51:45 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Reid Spencer | 6ff7240 | 2005-11-30 05:21:10 +0000 | [diff] [blame] | 188 | // Archive destructor - just clean up memory |
| 189 | Archive::~Archive() { |
| 190 | cleanUpMemory(); |
| 191 | } |
| 192 | |