Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 1 | //===-- ArchiveReader.cpp - Read LLVM archive files -------------*- C++ -*-===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +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 | f9d7a51 | 2004-11-14 21:58:33 +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 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 23 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 24 | do { |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 25 | if (At == End) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 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 |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 52 | // can be variable in size. |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 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 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 64 | // Extract the size and determine if the file is |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 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 |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 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 |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 84 | // for long file names. This library doesn't generate either of those but |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 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 |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 90 | // a '/' and blanks. |
| 91 | |
| 92 | std::string pathname; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 93 | switch (Hdr->name[0]) { |
| 94 | case '#': |
| 95 | if (Hdr->name[1] == '1' && Hdr->name[2] == '/') { |
| 96 | if (isdigit(Hdr->name[3])) { |
| 97 | unsigned len = atoi(&Hdr->name[3]); |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 98 | pathname.assign(At, len); |
Reid Spencer | dd95e8d | 2004-11-17 16:13:11 +0000 | [diff] [blame] | 99 | At += len; |
| 100 | MemberSize -= len; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 101 | flags |= ArchiveMember::HasLongFilenameFlag; |
| 102 | } else |
| 103 | throw std::string("invalid long filename"); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 104 | } else if (Hdr->name[1] == '_' && |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 105 | (0 == memcmp(Hdr->name, ARFILE_LLVM_SYMTAB_NAME, 16))) { |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 106 | // The member is using a long file name (>15 chars) format. |
| 107 | // This format is standard for 4.4BSD and Mac OSX operating |
| 108 | // systems. LLVM uses it similarly. In this format, the |
| 109 | // remainder of the name field (after #1/) specifies the |
| 110 | // length of the file name which occupy the first bytes of |
| 111 | // the member's data. The pathname already has the #1/ stripped. |
| 112 | pathname.assign(ARFILE_LLVM_SYMTAB_NAME); |
| 113 | flags |= ArchiveMember::LLVMSymbolTableFlag; |
| 114 | } |
| 115 | break; |
| 116 | case '/': |
| 117 | if (Hdr->name[1]== '/') { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 118 | if (0 == memcmp(Hdr->name, ARFILE_STRTAB_NAME, 16)) { |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 119 | pathname.assign(ARFILE_STRTAB_NAME); |
| 120 | flags |= ArchiveMember::StringTableFlag; |
| 121 | } else { |
| 122 | throw std::string("invalid string table name"); |
| 123 | } |
| 124 | } else if (Hdr->name[1] == ' ') { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 125 | if (0 == memcmp(Hdr->name, ARFILE_SVR4_SYMTAB_NAME, 16)) { |
| 126 | pathname.assign(ARFILE_SVR4_SYMTAB_NAME); |
| 127 | flags |= ArchiveMember::SVR4SymbolTableFlag; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 128 | } else { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 129 | throw std::string("invalid SVR4 symbol table name"); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 130 | } |
| 131 | } else if (isdigit(Hdr->name[1])) { |
| 132 | unsigned index = atoi(&Hdr->name[1]); |
| 133 | if (index < strtab.length()) { |
| 134 | const char* namep = strtab.c_str() + index; |
| 135 | const char* endp = strtab.c_str() + strtab.length(); |
| 136 | const char* p = namep; |
| 137 | const char* last_p = p; |
| 138 | while (p < endp) { |
| 139 | if (*p == '\n' && *last_p == '/') { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 140 | pathname.assign(namep, last_p - namep); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 141 | flags |= ArchiveMember::HasLongFilenameFlag; |
| 142 | break; |
| 143 | } |
| 144 | last_p = p; |
| 145 | p++; |
| 146 | } |
| 147 | if (p >= endp) |
| 148 | throw std::string("missing name termiantor in string table"); |
| 149 | } else { |
| 150 | throw std::string("name index beyond string table"); |
| 151 | } |
| 152 | } |
| 153 | break; |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 154 | case '_': |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 155 | if (Hdr->name[1] == '_' && |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 156 | (0 == memcmp(Hdr->name, ARFILE_BSD4_SYMTAB_NAME, 16))) { |
| 157 | pathname.assign(ARFILE_BSD4_SYMTAB_NAME); |
| 158 | flags |= ArchiveMember::BSD4SymbolTableFlag; |
Reid Spencer | 84b9ced | 2004-11-23 22:35:39 +0000 | [diff] [blame] | 159 | break; |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 160 | } |
Reid Spencer | 84b9ced | 2004-11-23 22:35:39 +0000 | [diff] [blame] | 161 | /* FALL THROUGH */ |
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 | 5612762 | 2004-12-29 01:20:24 +0000 | [diff] [blame] | 193 | unsigned int mode; |
| 194 | sscanf(Hdr->mode, "%o", &mode); |
| 195 | member->info.mode = mode; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 196 | member->info.user = atoi(Hdr->uid); |
| 197 | member->info.group = atoi(Hdr->gid); |
| 198 | member->flags = flags; |
| 199 | member->data = At; |
| 200 | |
| 201 | return member; |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | Archive::checkSignature() { |
| 206 | // Check the magic string at file's header |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 207 | if (mapfile->size() < 8 || memcmp(base, ARFILE_MAGIC, 8)) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 208 | throw std::string("invalid signature for an archive file"); |
| 209 | } |
| 210 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 211 | // This function loads the entire archive and fully populates its ilist with |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 212 | // the members of the archive file. This is typically used in preparation for |
| 213 | // editing the contents of the archive. |
| 214 | void |
| 215 | Archive::loadArchive() { |
| 216 | |
| 217 | // Set up parsing |
| 218 | members.clear(); |
| 219 | symTab.clear(); |
| 220 | const char *At = base; |
| 221 | const char *End = base + mapfile->size(); |
| 222 | |
| 223 | checkSignature(); |
| 224 | At += 8; // Skip the magic string. |
| 225 | |
| 226 | bool seenSymbolTable = false; |
| 227 | bool foundFirstFile = false; |
| 228 | while (At < End) { |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 229 | // parse the member header |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 230 | const char* Save = At; |
| 231 | ArchiveMember* mbr = parseMemberHeader(At, End); |
| 232 | |
| 233 | // check if this is the foreign symbol table |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 234 | if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) { |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 235 | // We just save this but don't do anything special |
| 236 | // with it. It doesn't count as the "first file". |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 237 | if (foreignST) { |
| 238 | // What? Multiple foreign symbol tables? Just chuck it |
| 239 | // and retain the last one found. |
| 240 | delete foreignST; |
| 241 | } |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 242 | foreignST = mbr; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 243 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 244 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 245 | At++; |
| 246 | } else if (mbr->isStringTable()) { |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 247 | // Simply suck the entire string table into a string |
| 248 | // variable. This will be used to get the names of the |
| 249 | // members that use the "/ddd" format for their names |
| 250 | // (SVR4 style long names). |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 251 | strtab.assign(At, mbr->getSize()); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 252 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 253 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 254 | At++; |
| 255 | delete mbr; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 256 | } else if (mbr->isLLVMSymbolTable()) { |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 257 | // This is the LLVM symbol table for the archive. If we've seen it |
| 258 | // already, its an error. Otherwise, parse the symbol table and move on. |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 259 | if (seenSymbolTable) |
| 260 | throw std::string("invalid archive: multiple symbol tables"); |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 261 | parseSymbolTable(mbr->getData(), mbr->getSize()); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 262 | seenSymbolTable = true; |
| 263 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 264 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 265 | At++; |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 266 | 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] | 267 | } else { |
Reid Spencer | 4a980d1 | 2004-11-16 06:47:19 +0000 | [diff] [blame] | 268 | // This is just a regular file. If its the first one, save its offset. |
| 269 | // 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] | 270 | if (!foundFirstFile) { |
| 271 | firstFileOffset = Save - base; |
| 272 | foundFirstFile = true; |
| 273 | } |
| 274 | members.push_back(mbr); |
| 275 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 276 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 277 | At++; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // Open and completely load the archive file. |
| 283 | Archive* |
Reid Spencer | 5af4688 | 2004-12-13 02:59:03 +0000 | [diff] [blame] | 284 | Archive::OpenAndLoad(const sys::Path& file, std::string* ErrorMessage) { |
| 285 | try { |
Reid Spencer | 518ec2e | 2004-12-13 03:22:31 +0000 | [diff] [blame] | 286 | std::auto_ptr<Archive> result ( new Archive(file, true)); |
Reid Spencer | 5af4688 | 2004-12-13 02:59:03 +0000 | [diff] [blame] | 287 | result->loadArchive(); |
Reid Spencer | 518ec2e | 2004-12-13 03:22:31 +0000 | [diff] [blame] | 288 | return result.release(); |
Reid Spencer | 5af4688 | 2004-12-13 02:59:03 +0000 | [diff] [blame] | 289 | } catch (const std::string& msg) { |
| 290 | if (ErrorMessage) { |
| 291 | *ErrorMessage = msg; |
| 292 | } |
| 293 | return 0; |
| 294 | } |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // Get all the bytecode modules from the archive |
| 298 | bool |
| 299 | Archive::getAllModules(std::vector<Module*>& Modules, std::string* ErrMessage) { |
| 300 | |
| 301 | for (iterator I=begin(), E=end(); I != E; ++I) { |
| 302 | if (I->isBytecode() || I->isCompressedBytecode()) { |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 303 | std::string FullMemberName = archPath.toString() + |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 304 | "(" + I->getPath().toString() + ")"; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 305 | Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(), |
Reid Spencer | 6ab7a4f | 2004-11-17 18:25:21 +0000 | [diff] [blame] | 306 | I->getSize(), FullMemberName, ErrMessage); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 307 | if (!M) |
| 308 | return true; |
| 309 | |
| 310 | Modules.push_back(M); |
| 311 | } |
| 312 | } |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame] | 313 | return false; |
| 314 | } |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 315 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 316 | // Load just the symbol table from the archive file |
| 317 | void |
| 318 | Archive::loadSymbolTable() { |
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 | // Set up parsing |
| 321 | members.clear(); |
| 322 | symTab.clear(); |
| 323 | const char *At = base; |
| 324 | const char *End = base + mapfile->size(); |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 325 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 326 | // Make sure we're dealing with an archive |
| 327 | checkSignature(); |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 328 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 329 | At += 8; // Skip signature |
| 330 | |
| 331 | // Parse the first file member header |
| 332 | const char* FirstFile = At; |
| 333 | ArchiveMember* mbr = parseMemberHeader(At, End); |
| 334 | |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 335 | if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) { |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 336 | // Skip the foreign symbol table, we don't do anything with it |
| 337 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 338 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 339 | At++; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 340 | delete mbr; |
| 341 | |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 342 | // Read the next one |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 343 | FirstFile = At; |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 344 | mbr = parseMemberHeader(At, End); |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | if (mbr->isStringTable()) { |
| 348 | // Process the string table entry |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 349 | strtab.assign((const char*)mbr->getData(), mbr->getSize()); |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 350 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 351 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 352 | At++; |
| 353 | delete mbr; |
| 354 | // Get the next one |
| 355 | FirstFile = At; |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 356 | mbr = parseMemberHeader(At, End); |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 359 | // See if its the symbol table |
| 360 | if (mbr->isLLVMSymbolTable()) { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 361 | parseSymbolTable(mbr->getData(), mbr->getSize()); |
Reid Spencer | 8dde18f | 2004-11-28 03:13:02 +0000 | [diff] [blame] | 362 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 363 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | 8dde18f | 2004-11-28 03:13:02 +0000 | [diff] [blame] | 364 | At++; |
| 365 | FirstFile = At; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 366 | } else { |
| 367 | // There's no symbol table in the file. We have to rebuild it from scratch |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 368 | // because the intent of this method is to get the symbol table loaded so |
| 369 | // it can be searched efficiently. |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 370 | // Add the member to the members list |
| 371 | members.push_back(mbr); |
| 372 | } |
| 373 | |
| 374 | firstFileOffset = FirstFile - base; |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 377 | // Open the archive and load just the symbol tables |
| 378 | Archive* |
Reid Spencer | 5af4688 | 2004-12-13 02:59:03 +0000 | [diff] [blame] | 379 | Archive::OpenAndLoadSymbols(const sys::Path& file, std::string* ErrorMessage) { |
| 380 | try { |
Reid Spencer | 518ec2e | 2004-12-13 03:22:31 +0000 | [diff] [blame] | 381 | std::auto_ptr<Archive> result ( new Archive(file, true) ); |
Reid Spencer | 5af4688 | 2004-12-13 02:59:03 +0000 | [diff] [blame] | 382 | result->loadSymbolTable(); |
Reid Spencer | 518ec2e | 2004-12-13 03:22:31 +0000 | [diff] [blame] | 383 | return result.release(); |
Reid Spencer | 5af4688 | 2004-12-13 02:59:03 +0000 | [diff] [blame] | 384 | } catch (const std::string& msg) { |
| 385 | if (ErrorMessage) { |
| 386 | *ErrorMessage = msg; |
| 387 | } |
| 388 | return 0; |
| 389 | } |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 390 | } |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 391 | |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 392 | // Look up one symbol in the symbol table and return a ModuleProvider for the |
| 393 | // module that defines that symbol. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 394 | ModuleProvider* |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 395 | Archive::findModuleDefiningSymbol(const std::string& symbol) { |
| 396 | SymTabType::iterator SI = symTab.find(symbol); |
| 397 | if (SI == symTab.end()) |
| 398 | return 0; |
| 399 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 400 | // The symbol table was previously constructed assuming that the members were |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 401 | // written without the symbol table header. Because VBR encoding is used, the |
| 402 | // values could not be adjusted to account for the offset of the symbol table |
| 403 | // because that could affect the size of the symbol table due to VBR encoding. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 404 | // We now have to account for this by adjusting the offset by the size of the |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 405 | // symbol table and its header. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 406 | unsigned fileOffset = |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 407 | SI->second + // offset in symbol-table-less file |
| 408 | firstFileOffset; // add offset to first "real" file in archive |
| 409 | |
| 410 | // See if the module is already loaded |
| 411 | ModuleMap::iterator MI = modules.find(fileOffset); |
| 412 | if (MI != modules.end()) |
| 413 | return MI->second.first; |
| 414 | |
| 415 | // Module hasn't been loaded yet, we need to load it |
| 416 | const char* modptr = base + fileOffset; |
| 417 | ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size()); |
| 418 | |
| 419 | // Now, load the bytecode module to get the ModuleProvider |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 420 | std::string FullMemberName = archPath.toString() + "(" + |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 421 | mbr->getPath().toString() + ")"; |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 422 | ModuleProvider* mp = getBytecodeBufferModuleProvider( |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 423 | (const unsigned char*) mbr->getData(), mbr->getSize(), |
Reid Spencer | 6ab7a4f | 2004-11-17 18:25:21 +0000 | [diff] [blame] | 424 | FullMemberName, 0); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 425 | |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 426 | modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr))); |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 427 | |
| 428 | return mp; |
| 429 | } |
| 430 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 431 | // Look up multiple symbols in the symbol table and return a set of |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 432 | // ModuleProviders that define those symbols. |
| 433 | void |
Reid Spencer | 7783e8a | 2004-11-19 03:18:22 +0000 | [diff] [blame] | 434 | Archive::findModulesDefiningSymbols(std::set<std::string>& symbols, |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 435 | std::set<ModuleProvider*>& result) |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 436 | { |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 437 | assert(mapfile && base && "Can't findModulesDefiningSymbols on new archive"); |
| 438 | if (symTab.empty()) { |
| 439 | // We don't have a symbol table, so we must build it now but lets also |
| 440 | // make sure that we populate the modules table as we do this to ensure |
| 441 | // that we don't load them twice when findModuleDefiningSymbol is called |
| 442 | // below. |
| 443 | |
| 444 | // Get a pointer to the first file |
| 445 | const char* At = ((const char*)base) + firstFileOffset; |
| 446 | const char* End = ((const char*)base) + mapfile->size(); |
| 447 | |
| 448 | while ( At < End) { |
| 449 | // Compute the offset to be put in the symbol table |
| 450 | unsigned offset = At - base - firstFileOffset; |
| 451 | |
| 452 | // Parse the file's header |
| 453 | ArchiveMember* mbr = parseMemberHeader(At, End); |
| 454 | |
| 455 | // If it contains symbols |
| 456 | if (mbr->isBytecode() || mbr->isCompressedBytecode()) { |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 457 | // Get the symbols |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 458 | std::vector<std::string> symbols; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 459 | std::string FullMemberName = archPath.toString() + "(" + |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 460 | mbr->getPath().toString() + ")"; |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 461 | ModuleProvider* MP = GetBytecodeSymbols((const unsigned char*)At, |
Reid Spencer | 6ab7a4f | 2004-11-17 18:25:21 +0000 | [diff] [blame] | 462 | mbr->getSize(), FullMemberName, symbols); |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 463 | |
| 464 | if (MP) { |
| 465 | // Insert the module's symbols into the symbol table |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 466 | for (std::vector<std::string>::iterator I = symbols.begin(), |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 467 | E=symbols.end(); I != E; ++I ) { |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 468 | symTab.insert(std::make_pair(*I, offset)); |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 469 | } |
| 470 | // Insert the ModuleProvider and the ArchiveMember into the table of |
| 471 | // modules. |
Reid Spencer | 9a29db4 | 2004-11-20 07:29:40 +0000 | [diff] [blame] | 472 | modules.insert(std::make_pair(offset, std::make_pair(MP, mbr))); |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 473 | } else { |
| 474 | throw std::string("Can't parse bytecode member: ") + |
Reid Spencer | 1fce091 | 2004-12-11 00:14:15 +0000 | [diff] [blame] | 475 | mbr->getPath().toString(); |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 478 | |
| 479 | // Go to the next file location |
| 480 | At += mbr->getSize(); |
Reid Spencer | 6405c9e | 2004-11-19 17:08:00 +0000 | [diff] [blame] | 481 | if ((intptr_t(At) & 1) == 1) |
Reid Spencer | b323113 | 2004-11-15 01:40:20 +0000 | [diff] [blame] | 482 | At++; |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 486 | // At this point we have a valid symbol table (one way or another) so we |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 487 | // just use it to quickly find the symbols requested. |
| 488 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 489 | for (std::set<std::string>::iterator I=symbols.begin(), |
Reid Spencer | 7783e8a | 2004-11-19 03:18:22 +0000 | [diff] [blame] | 490 | E=symbols.end(); I != E;) { |
| 491 | // See if this symbol exists |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 492 | ModuleProvider* mp = findModuleDefiningSymbol(*I); |
| 493 | if (mp) { |
Reid Spencer | 7783e8a | 2004-11-19 03:18:22 +0000 | [diff] [blame] | 494 | // The symbol exists, insert the ModuleProvider into our result, |
| 495 | // duplicates wil be ignored |
Reid Spencer | 766b793 | 2004-11-15 01:20:11 +0000 | [diff] [blame] | 496 | result.insert(mp); |
Reid Spencer | 7783e8a | 2004-11-19 03:18:22 +0000 | [diff] [blame] | 497 | |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 498 | // Remove the symbol now that its been resolved, being careful to |
Reid Spencer | 57646ec | 2004-11-19 03:44:10 +0000 | [diff] [blame] | 499 | // post-increment the iterator. |
| 500 | symbols.erase(I++); |
Reid Spencer | 7783e8a | 2004-11-19 03:18:22 +0000 | [diff] [blame] | 501 | } else { |
| 502 | ++I; |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 503 | } |
Reid Spencer | f9d7a51 | 2004-11-14 21:58:33 +0000 | [diff] [blame] | 504 | } |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 505 | } |
Reid Spencer | eaa06bb | 2005-02-26 22:00:32 +0000 | [diff] [blame] | 506 | |
| 507 | bool |
| 508 | Archive::isBytecodeArchive() |
| 509 | { |
| 510 | //Make sure the symTab has been loaded... |
| 511 | //in most cases this should have been done |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 512 | //when the archive was constructed, but still, |
Reid Spencer | eaa06bb | 2005-02-26 22:00:32 +0000 | [diff] [blame] | 513 | //this is just in case. |
| 514 | if ( !symTab.size() ) |
| 515 | loadSymbolTable(); |
| 516 | |
| 517 | //Now that we know it's been loaded, return true |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame^] | 518 | //if it has a size |
Reid Spencer | eaa06bb | 2005-02-26 22:00:32 +0000 | [diff] [blame] | 519 | if ( symTab.size() ) return true; |
| 520 | |
| 521 | //We still can't be sure it isn't a bytecode archive |
| 522 | loadArchive(); |
| 523 | |
| 524 | std::vector<Module *> Modules; |
| 525 | std::string ErrorMessage; |
| 526 | |
| 527 | //If getAllModules gives an error then this isn't a proper |
| 528 | //bytecode archive |
| 529 | if ( getAllModules( Modules, &ErrorMessage ) ) return false; |
| 530 | |
| 531 | //Finally, if we find any bytecode modules then this is a proper |
| 532 | //bytecode archive |
| 533 | return Modules.size(); |
| 534 | } |