Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 1 | //===-- ArchiveReader.cpp - Read LLVM archive files -------------*- C++ -*-===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 9 | // |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 10 | // Builds up standard unix archive files (.a) containing LLVM bytecode. |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 14 | #include "ArchiveInternals.h" |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 15 | #include "llvm/Bytecode/Reader.h" |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 16 | |
Chris Lattner | 3446ae8 | 2004-01-10 19:00:15 +0000 | [diff] [blame] | 17 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 18 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 19 | /// Read a variable-bit-rate encoded unsigned integer |
| 20 | inline unsigned readInteger(const char*&At, const char*End) { |
| 21 | unsigned Shift = 0; |
| 22 | unsigned Result = 0; |
| 23 | |
| 24 | do { |
| 25 | if (At == End) |
| 26 | throw std::string("Ran out of data reading vbr_uint!"); |
| 27 | Result |= (unsigned)((*At++) & 0x7F) << Shift; |
| 28 | Shift += 7; |
| 29 | } while (At[-1] & 0x80); |
| 30 | return Result; |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 33 | // Completely parse the Archive's symbol table and populate symTab member var. |
| 34 | void |
| 35 | Archive::parseSymbolTable(const void* data, unsigned size) { |
| 36 | const char* At = (const char*) data; |
| 37 | const char* End = At + size; |
| 38 | while (At < End) { |
| 39 | unsigned offset = readInteger(At, End); |
| 40 | unsigned length = readInteger(At, End); |
| 41 | if (At + length > End) |
| 42 | throw std::string("malformed symbol table"); |
| 43 | // we don't care if it can't be inserted (duplicate entry) |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 44 | symTab.insert(std::make_pair(std::string(At, length), offset)); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 45 | At += length; |
| 46 | } |
| 47 | symTabSize = size; |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 50 | // This member parses an ArchiveMemberHeader that is presumed to be pointed to |
| 51 | // by At. The At pointer is updated to the byte just after the header, which |
| 52 | // can be variable in size. |
| 53 | ArchiveMember* |
| 54 | Archive::parseMemberHeader(const char*& At, const char* End) { |
| 55 | assert(At + sizeof(ArchiveMemberHeader) < End && "Not enough data"); |
| 56 | |
| 57 | // Cast archive member header |
| 58 | ArchiveMemberHeader* Hdr = (ArchiveMemberHeader*)At; |
| 59 | At += sizeof(ArchiveMemberHeader); |
| 60 | |
| 61 | // Instantiate the ArchiveMember to be filled |
| 62 | ArchiveMember* member = new ArchiveMember(this); |
| 63 | |
| 64 | // Extract the size and determine if the file is |
| 65 | // compressed or not (negative length). |
| 66 | int flags = 0; |
| 67 | int MemberSize = atoi(Hdr->size); |
| 68 | if (MemberSize < 0) { |
| 69 | flags |= ArchiveMember::CompressedFlag; |
| 70 | MemberSize = -MemberSize; |
| 71 | } |
| 72 | |
| 73 | // Check the size of the member for sanity |
| 74 | if (At + MemberSize > End) |
| 75 | throw std::string("invalid member length in archive file"); |
| 76 | |
| 77 | // Check the member signature |
| 78 | if (!Hdr->checkSignature()) |
| 79 | throw std::string("invalid file member signature"); |
| 80 | |
| 81 | // Convert and check the member name |
| 82 | // The empty name ( '/' and 15 blanks) is for a foreign (non-LLVM) symbol |
| 83 | // table. The special name "//" and 14 blanks is for a string table, used |
| 84 | // for long file names. This library doesn't generate either of those but |
| 85 | // it will accept them. If the name starts with #1/ and the remainder is |
| 86 | // digits, then those digits specify the length of the name that is |
| 87 | // stored immediately following the header. The special name |
| 88 | // __LLVM_SYM_TAB__ identifies the symbol table for LLVM bytecode. |
| 89 | // Anything else is a regular, short filename that is terminated with |
| 90 | // a '/' and blanks. |
| 91 | |
| 92 | std::string pathname; |
| 93 | unsigned index; |
| 94 | switch (Hdr->name[0]) { |
| 95 | case '#': |
| 96 | if (Hdr->name[1] == '1' && Hdr->name[2] == '/') { |
| 97 | if (isdigit(Hdr->name[3])) { |
| 98 | unsigned len = atoi(&Hdr->name[3]); |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 99 | pathname.assign(At, len); |
Reid Spencer | dd95e8d | 2004-11-17 16:13:11 +0000 | [diff] [blame] | 100 | At += len; |
| 101 | MemberSize -= len; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 102 | flags |= ArchiveMember::HasLongFilenameFlag; |
| 103 | } else |
| 104 | throw std::string("invalid long filename"); |
| 105 | } else if (Hdr->name[1] == '_' && |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 106 | (0 == memcmp(Hdr->name, ARFILE_LLVM_SYMTAB_NAME, 16))) { |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 107 | // The member is using a long file name (>15 chars) format. |
| 108 | // This format is standard for 4.4BSD and Mac OSX operating |
| 109 | // systems. LLVM uses it similarly. In this format, the |
| 110 | // remainder of the name field (after #1/) specifies the |
| 111 | // length of the file name which occupy the first bytes of |
| 112 | // the member's data. The pathname already has the #1/ stripped. |
| 113 | pathname.assign(ARFILE_LLVM_SYMTAB_NAME); |
| 114 | flags |= ArchiveMember::LLVMSymbolTableFlag; |
| 115 | } |
| 116 | break; |
| 117 | case '/': |
| 118 | if (Hdr->name[1]== '/') { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 119 | if (0 == memcmp(Hdr->name, ARFILE_STRTAB_NAME, 16)) { |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 120 | pathname.assign(ARFILE_STRTAB_NAME); |
| 121 | flags |= ArchiveMember::StringTableFlag; |
| 122 | } else { |
| 123 | throw std::string("invalid string table name"); |
| 124 | } |
| 125 | } else if (Hdr->name[1] == ' ') { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 126 | if (0 == memcmp(Hdr->name, ARFILE_SVR4_SYMTAB_NAME, 16)) { |
| 127 | pathname.assign(ARFILE_SVR4_SYMTAB_NAME); |
| 128 | flags |= ArchiveMember::SVR4SymbolTableFlag; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 129 | } else { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 130 | throw std::string("invalid SVR4 symbol table name"); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 131 | } |
| 132 | } else if (isdigit(Hdr->name[1])) { |
| 133 | unsigned index = atoi(&Hdr->name[1]); |
| 134 | if (index < strtab.length()) { |
| 135 | const char* namep = strtab.c_str() + index; |
| 136 | const char* endp = strtab.c_str() + strtab.length(); |
| 137 | const char* p = namep; |
| 138 | const char* last_p = p; |
| 139 | while (p < endp) { |
| 140 | if (*p == '\n' && *last_p == '/') { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 141 | pathname.assign(namep, last_p - namep); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 142 | flags |= ArchiveMember::HasLongFilenameFlag; |
| 143 | break; |
| 144 | } |
| 145 | last_p = p; |
| 146 | p++; |
| 147 | } |
| 148 | if (p >= endp) |
| 149 | throw std::string("missing name termiantor in string table"); |
| 150 | } else { |
| 151 | throw std::string("name index beyond string table"); |
| 152 | } |
| 153 | } |
| 154 | break; |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 155 | case '_': |
| 156 | if (Hdr->name[1] == '_' && |
| 157 | (0 == memcmp(Hdr->name, ARFILE_BSD4_SYMTAB_NAME, 16))) { |
| 158 | pathname.assign(ARFILE_BSD4_SYMTAB_NAME); |
| 159 | flags |= ArchiveMember::BSD4SymbolTableFlag; |
| 160 | } |
| 161 | break; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 162 | |
| 163 | default: |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 164 | char* slash = (char*) memchr(Hdr->name, '/', 16); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 165 | if (slash == 0) |
Reid Spencer | dd95e8d | 2004-11-17 16:13:11 +0000 | [diff] [blame] | 166 | slash = Hdr->name + 16; |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 167 | pathname.assign(Hdr->name, slash - Hdr->name); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 168 | break; |
| 169 | } |
| 170 | |
| 171 | // Determine if this is a bytecode file |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 172 | switch (sys::IdentifyFileType(At, 4)) { |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 173 | case sys::BytecodeFileType: |
| 174 | flags |= ArchiveMember::BytecodeFlag; |
| 175 | break; |
| 176 | case sys::CompressedBytecodeFileType: |
| 177 | flags |= ArchiveMember::CompressedBytecodeFlag; |
| 178 | flags &= ~ArchiveMember::CompressedFlag; |
| 179 | break; |
| 180 | default: |
| 181 | flags &= ~(ArchiveMember::BytecodeFlag| |
| 182 | ArchiveMember::CompressedBytecodeFlag); |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | // Fill in fields of the ArchiveMember |
| 187 | member->next = 0; |
| 188 | member->prev = 0; |
| 189 | member->parent = this; |
| 190 | member->path.setFile(pathname); |
| 191 | member->info.fileSize = MemberSize; |
| 192 | member->info.modTime.fromEpochTime(atoi(Hdr->date)); |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 193 | sscanf(Hdr->mode, "%o", &(member->info.mode)); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 194 | member->info.user = atoi(Hdr->uid); |
| 195 | member->info.group = atoi(Hdr->gid); |
| 196 | member->flags = flags; |
| 197 | member->data = At; |
| 198 | |
| 199 | return member; |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | Archive::checkSignature() { |
| 204 | // Check the magic string at file's header |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 205 | if (mapfile->size() < 8 || memcmp(base, ARFILE_MAGIC, 8)) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 206 | throw std::string("invalid signature for an archive file"); |
| 207 | } |
| 208 | |
| 209 | // This function loads the entire archive and fully populates its ilist with |
| 210 | // the members of the archive file. This is typically used in preparation for |
| 211 | // editing the contents of the archive. |
| 212 | void |
| 213 | Archive::loadArchive() { |
| 214 | |
| 215 | // Set up parsing |
| 216 | members.clear(); |
| 217 | symTab.clear(); |
| 218 | const char *At = base; |
| 219 | const char *End = base + mapfile->size(); |
| 220 | |
| 221 | checkSignature(); |
| 222 | At += 8; // Skip the magic string. |
| 223 | |
| 224 | bool seenSymbolTable = false; |
| 225 | bool foundFirstFile = false; |
| 226 | while (At < End) { |
| 227 | // parse the member header |
| 228 | const char* Save = At; |
| 229 | ArchiveMember* mbr = parseMemberHeader(At, End); |
| 230 | |
| 231 | // check if this is the foreign symbol table |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 232 | if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) { |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 233 | // We just save this but don't do anything special |
| 234 | // with it. It doesn't count as the "first file". |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 235 | if (foreignST) { |
| 236 | // What? Multiple foreign symbol tables? Just chuck it |
| 237 | // and retain the last one found. |
| 238 | delete foreignST; |
| 239 | } |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 240 | foreignST = mbr; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 241 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 242 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 243 | At++; |
| 244 | } else if (mbr->isStringTable()) { |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 245 | // Simply suck the entire string table into a string |
| 246 | // variable. This will be used to get the names of the |
| 247 | // members that use the "/ddd" format for their names |
| 248 | // (SVR4 style long names). |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 249 | strtab.assign(At, mbr->getSize()); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 250 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 251 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 252 | At++; |
| 253 | delete mbr; |
| 254 | } else if (mbr->isLLVMSymbolTable()) { |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 255 | // This is the LLVM symbol table for the archive. If we've seen it |
| 256 | // already, its an error. Otherwise, parse the symbol table and move on. |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 257 | if (seenSymbolTable) |
| 258 | throw std::string("invalid archive: multiple symbol tables"); |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 259 | parseSymbolTable(mbr->getData(), mbr->getSize()); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 260 | seenSymbolTable = true; |
| 261 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 262 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 263 | At++; |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 264 | delete mbr; // We don't need this member in the list of members. |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 265 | } else { |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 266 | // This is just a regular file. If its the first one, save its offset. |
| 267 | // Otherwise just push it on the list and move on to the next file. |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 268 | if (!foundFirstFile) { |
| 269 | firstFileOffset = Save - base; |
| 270 | foundFirstFile = true; |
| 271 | } |
| 272 | members.push_back(mbr); |
| 273 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 274 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 275 | At++; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // Open and completely load the archive file. |
| 281 | Archive* |
| 282 | Archive::OpenAndLoad(const sys::Path& file) { |
| 283 | |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 284 | Archive* result = new Archive(file, true); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 285 | |
| 286 | result->loadArchive(); |
| 287 | |
| 288 | return result; |
| 289 | } |
| 290 | |
| 291 | // Get all the bytecode modules from the archive |
| 292 | bool |
| 293 | Archive::getAllModules(std::vector<Module*>& Modules, std::string* ErrMessage) { |
| 294 | |
| 295 | for (iterator I=begin(), E=end(); I != E; ++I) { |
| 296 | if (I->isBytecode() || I->isCompressedBytecode()) { |
Reid Spencer | 6ab7a4f | 2004-11-17 18:25:21 +0000 | [diff] [blame] | 297 | std::string FullMemberName = archPath.get() + |
| 298 | "(" + I->getPath().get() + ")"; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 299 | Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(), |
Reid Spencer | 6ab7a4f | 2004-11-17 18:25:21 +0000 | [diff] [blame] | 300 | I->getSize(), FullMemberName, ErrMessage); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 301 | if (!M) |
| 302 | return true; |
| 303 | |
| 304 | Modules.push_back(M); |
| 305 | } |
| 306 | } |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame] | 307 | return false; |
| 308 | } |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 309 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 310 | // Load just the symbol table from the archive file |
| 311 | void |
| 312 | Archive::loadSymbolTable() { |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 313 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 314 | // Set up parsing |
| 315 | members.clear(); |
| 316 | symTab.clear(); |
| 317 | const char *At = base; |
| 318 | const char *End = base + mapfile->size(); |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 319 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 320 | // Make sure we're dealing with an archive |
| 321 | checkSignature(); |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 322 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 323 | At += 8; // Skip signature |
| 324 | |
| 325 | // Parse the first file member header |
| 326 | const char* FirstFile = At; |
| 327 | ArchiveMember* mbr = parseMemberHeader(At, End); |
| 328 | |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 329 | if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) { |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 330 | // Skip the foreign symbol table, we don't do anything with it |
| 331 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 332 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 333 | At++; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 334 | delete mbr; |
| 335 | |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 336 | // Read the next one |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 337 | FirstFile = At; |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 338 | mbr = parseMemberHeader(At, End); |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | if (mbr->isStringTable()) { |
| 342 | // Process the string table entry |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 343 | strtab.assign((const char*)mbr->getData(), mbr->getSize()); |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 344 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 345 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 346 | At++; |
| 347 | delete mbr; |
| 348 | // Get the next one |
| 349 | FirstFile = At; |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 350 | mbr = parseMemberHeader(At, End); |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 353 | // See if its the symbol table |
| 354 | if (mbr->isLLVMSymbolTable()) { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 355 | parseSymbolTable(mbr->getData(), mbr->getSize()); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 356 | FirstFile = At + mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 357 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 358 | FirstFile++; |
| 359 | } else { |
| 360 | // There's no symbol table in the file. We have to rebuild it from scratch |
| 361 | // because the intent of this method is to get the symbol table loaded so |
| 362 | // it can be searched efficiently. |
| 363 | // Add the member to the members list |
| 364 | members.push_back(mbr); |
| 365 | } |
| 366 | |
| 367 | firstFileOffset = FirstFile - base; |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 370 | // Open the archive and load just the symbol tables |
| 371 | Archive* |
| 372 | Archive::OpenAndLoadSymbols(const sys::Path& file) { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 373 | Archive* result = new Archive(file, true); |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 374 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 375 | result->loadSymbolTable(); |
Chris Lattner | b70abe1 | 2003-12-30 07:40:35 +0000 | [diff] [blame] | 376 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 377 | return result; |
| 378 | } |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 379 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 380 | // Look up one symbol in the symbol table and return a ModuleProvider for the |
| 381 | // module that defines that symbol. |
| 382 | ModuleProvider* |
| 383 | Archive::findModuleDefiningSymbol(const std::string& symbol) { |
| 384 | SymTabType::iterator SI = symTab.find(symbol); |
| 385 | if (SI == symTab.end()) |
| 386 | return 0; |
| 387 | |
| 388 | // The symbol table was previously constructed assuming that the members were |
| 389 | // written without the symbol table header. Because VBR encoding is used, the |
| 390 | // values could not be adjusted to account for the offset of the symbol table |
| 391 | // because that could affect the size of the symbol table due to VBR encoding. |
| 392 | // We now have to account for this by adjusting the offset by the size of the |
| 393 | // symbol table and its header. |
| 394 | unsigned fileOffset = |
| 395 | SI->second + // offset in symbol-table-less file |
| 396 | firstFileOffset; // add offset to first "real" file in archive |
| 397 | |
| 398 | // See if the module is already loaded |
| 399 | ModuleMap::iterator MI = modules.find(fileOffset); |
| 400 | if (MI != modules.end()) |
| 401 | return MI->second.first; |
| 402 | |
| 403 | // Module hasn't been loaded yet, we need to load it |
| 404 | const char* modptr = base + fileOffset; |
| 405 | ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size()); |
| 406 | |
| 407 | // Now, load the bytecode module to get the ModuleProvider |
Reid Spencer | 6ab7a4f | 2004-11-17 18:25:21 +0000 | [diff] [blame] | 408 | std::string FullMemberName = archPath.get() + "(" + |
| 409 | mbr->getPath().get() + ")"; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 410 | ModuleProvider* mp = getBytecodeBufferModuleProvider( |
| 411 | (const unsigned char*) mbr->getData(), mbr->getSize(), |
Reid Spencer | 6ab7a4f | 2004-11-17 18:25:21 +0000 | [diff] [blame] | 412 | FullMemberName, 0); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 413 | |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 414 | modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr))); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 415 | |
| 416 | return mp; |
| 417 | } |
| 418 | |
| 419 | // Look up multiple symbols in the symbol table and return a set of |
| 420 | // ModuleProviders that define those symbols. |
| 421 | void |
Reid Spencer | 7783e8a | 2004-11-19 03:18:22 +0000 | [diff] [blame] | 422 | Archive::findModulesDefiningSymbols(std::set<std::string>& symbols, |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 423 | std::set<ModuleProvider*>& result) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 424 | { |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 425 | assert(mapfile && base && "Can't findModulesDefiningSymbols on new archive"); |
| 426 | if (symTab.empty()) { |
| 427 | // We don't have a symbol table, so we must build it now but lets also |
| 428 | // make sure that we populate the modules table as we do this to ensure |
| 429 | // that we don't load them twice when findModuleDefiningSymbol is called |
| 430 | // below. |
| 431 | |
| 432 | // Get a pointer to the first file |
| 433 | const char* At = ((const char*)base) + firstFileOffset; |
| 434 | const char* End = ((const char*)base) + mapfile->size(); |
| 435 | |
| 436 | while ( At < End) { |
| 437 | // Compute the offset to be put in the symbol table |
| 438 | unsigned offset = At - base - firstFileOffset; |
| 439 | |
| 440 | // Parse the file's header |
| 441 | ArchiveMember* mbr = parseMemberHeader(At, End); |
| 442 | |
| 443 | // If it contains symbols |
| 444 | if (mbr->isBytecode() || mbr->isCompressedBytecode()) { |
| 445 | // Get the symbols |
| 446 | std::vector<std::string> symbols; |
Reid Spencer | 6ab7a4f | 2004-11-17 18:25:21 +0000 | [diff] [blame] | 447 | std::string FullMemberName = archPath.get() + "(" + |
| 448 | mbr->getPath().get() + ")"; |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 449 | ModuleProvider* MP = GetBytecodeSymbols((const unsigned char*)At, |
Reid Spencer | 6ab7a4f | 2004-11-17 18:25:21 +0000 | [diff] [blame] | 450 | mbr->getSize(), FullMemberName, symbols); |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 451 | |
| 452 | if (MP) { |
| 453 | // Insert the module's symbols into the symbol table |
| 454 | for (std::vector<std::string>::iterator I = symbols.begin(), |
| 455 | E=symbols.end(); I != E; ++I ) { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 456 | symTab.insert(std::make_pair(*I, offset)); |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 457 | } |
| 458 | // Insert the ModuleProvider and the ArchiveMember into the table of |
| 459 | // modules. |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame^] | 460 | modules.insert(std::make_pair(offset, std::make_pair(MP, mbr))); |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 461 | } else { |
| 462 | throw std::string("Can't parse bytecode member: ") + |
| 463 | mbr->getPath().get(); |
| 464 | } |
| 465 | } |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 466 | |
| 467 | // Go to the next file location |
| 468 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 469 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 470 | At++; |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
| 474 | // At this point we have a valid symbol table (one way or another) so we |
| 475 | // just use it to quickly find the symbols requested. |
| 476 | |
Reid Spencer | 7783e8a | 2004-11-19 03:18:22 +0000 | [diff] [blame] | 477 | for (std::set<std::string>::iterator I=symbols.begin(), |
| 478 | E=symbols.end(); I != E;) { |
| 479 | // See if this symbol exists |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 480 | ModuleProvider* mp = findModuleDefiningSymbol(*I); |
| 481 | if (mp) { |
Reid Spencer | 7783e8a | 2004-11-19 03:18:22 +0000 | [diff] [blame] | 482 | // The symbol exists, insert the ModuleProvider into our result, |
| 483 | // duplicates wil be ignored |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 484 | result.insert(mp); |
Reid Spencer | 7783e8a | 2004-11-19 03:18:22 +0000 | [diff] [blame] | 485 | |
| 486 | // Remove the symbol now that its been resolved, being careful to |
Reid Spencer | 57646ec | 2004-11-19 03:44:10 +0000 | [diff] [blame] | 487 | // post-increment the iterator. |
| 488 | symbols.erase(I++); |
Reid Spencer | 7783e8a | 2004-11-19 03:18:22 +0000 | [diff] [blame] | 489 | } else { |
| 490 | ++I; |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 491 | } |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 492 | } |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 493 | } |