blob: 28327df4cb0bee1659d4c5a5eb1e15f1247f2eae [file] [log] [blame]
Reid Spencerf9d7a512004-11-14 21:58:33 +00001//===-- ArchiveReader.cpp - Read LLVM archive files -------------*- C++ -*-===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencerf9d7a512004-11-14 21:58:33 +00005// 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 Criswellb576c942003-10-20 19:43:21 +00007//
8//===----------------------------------------------------------------------===//
Chris Lattner968cfd02003-04-19 21:45:34 +00009//
Reid Spencerf9d7a512004-11-14 21:58:33 +000010// Builds up standard unix archive files (.a) containing LLVM bytecode.
Chris Lattner968cfd02003-04-19 21:45:34 +000011//
12//===----------------------------------------------------------------------===//
13
Reid Spencerf9d7a512004-11-14 21:58:33 +000014#include "ArchiveInternals.h"
Chris Lattner968cfd02003-04-19 21:45:34 +000015#include "llvm/Bytecode/Reader.h"
Reid Spencerf9d7a512004-11-14 21:58:33 +000016
Chris Lattner3446ae82004-01-10 19:00:15 +000017using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000018
Reid Spencerf9d7a512004-11-14 21:58:33 +000019/// Read a variable-bit-rate encoded unsigned integer
20inline 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 Lattner968cfd02003-04-19 21:45:34 +000031}
32
Reid Spencerf9d7a512004-11-14 21:58:33 +000033// Completely parse the Archive's symbol table and populate symTab member var.
34void
35Archive::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 Spencer9a29db42004-11-20 07:29:40 +000044 symTab.insert(std::make_pair(std::string(At, length), offset));
Reid Spencerf9d7a512004-11-14 21:58:33 +000045 At += length;
46 }
47 symTabSize = size;
Chris Lattner968cfd02003-04-19 21:45:34 +000048}
49
Reid Spencerf9d7a512004-11-14 21:58:33 +000050// 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.
53ArchiveMember*
54Archive::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;
Reid Spencerf9d7a512004-11-14 21:58:33 +000093 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 Spencer9a29db42004-11-20 07:29:40 +000098 pathname.assign(At, len);
Reid Spencerdd95e8d2004-11-17 16:13:11 +000099 At += len;
100 MemberSize -= len;
Reid Spencerf9d7a512004-11-14 21:58:33 +0000101 flags |= ArchiveMember::HasLongFilenameFlag;
102 } else
103 throw std::string("invalid long filename");
104 } else if (Hdr->name[1] == '_' &&
Reid Spencer9a29db42004-11-20 07:29:40 +0000105 (0 == memcmp(Hdr->name, ARFILE_LLVM_SYMTAB_NAME, 16))) {
Reid Spencerf9d7a512004-11-14 21:58:33 +0000106 // 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 Spencer9a29db42004-11-20 07:29:40 +0000118 if (0 == memcmp(Hdr->name, ARFILE_STRTAB_NAME, 16)) {
Reid Spencerf9d7a512004-11-14 21:58:33 +0000119 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 Spencer9a29db42004-11-20 07:29:40 +0000125 if (0 == memcmp(Hdr->name, ARFILE_SVR4_SYMTAB_NAME, 16)) {
126 pathname.assign(ARFILE_SVR4_SYMTAB_NAME);
127 flags |= ArchiveMember::SVR4SymbolTableFlag;
Reid Spencerf9d7a512004-11-14 21:58:33 +0000128 } else {
Reid Spencer9a29db42004-11-20 07:29:40 +0000129 throw std::string("invalid SVR4 symbol table name");
Reid Spencerf9d7a512004-11-14 21:58:33 +0000130 }
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 Spencer9a29db42004-11-20 07:29:40 +0000140 pathname.assign(namep, last_p - namep);
Reid Spencerf9d7a512004-11-14 21:58:33 +0000141 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 Spencer9a29db42004-11-20 07:29:40 +0000154 case '_':
155 if (Hdr->name[1] == '_' &&
156 (0 == memcmp(Hdr->name, ARFILE_BSD4_SYMTAB_NAME, 16))) {
157 pathname.assign(ARFILE_BSD4_SYMTAB_NAME);
158 flags |= ArchiveMember::BSD4SymbolTableFlag;
Reid Spencer84b9ced2004-11-23 22:35:39 +0000159 break;
Reid Spencer9a29db42004-11-20 07:29:40 +0000160 }
Reid Spencer84b9ced2004-11-23 22:35:39 +0000161 /* FALL THROUGH */
Reid Spencerf9d7a512004-11-14 21:58:33 +0000162
163 default:
Reid Spencer9a29db42004-11-20 07:29:40 +0000164 char* slash = (char*) memchr(Hdr->name, '/', 16);
Reid Spencerf9d7a512004-11-14 21:58:33 +0000165 if (slash == 0)
Reid Spencerdd95e8d2004-11-17 16:13:11 +0000166 slash = Hdr->name + 16;
Reid Spencer9a29db42004-11-20 07:29:40 +0000167 pathname.assign(Hdr->name, slash - Hdr->name);
Reid Spencerf9d7a512004-11-14 21:58:33 +0000168 break;
169 }
170
171 // Determine if this is a bytecode file
Reid Spencer9a29db42004-11-20 07:29:40 +0000172 switch (sys::IdentifyFileType(At, 4)) {
Reid Spencerf9d7a512004-11-14 21:58:33 +0000173 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 Spencer9a29db42004-11-20 07:29:40 +0000193 sscanf(Hdr->mode, "%o", &(member->info.mode));
Reid Spencerf9d7a512004-11-14 21:58:33 +0000194 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
202void
203Archive::checkSignature() {
204 // Check the magic string at file's header
Reid Spencer9a29db42004-11-20 07:29:40 +0000205 if (mapfile->size() < 8 || memcmp(base, ARFILE_MAGIC, 8))
Reid Spencerf9d7a512004-11-14 21:58:33 +0000206 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.
212void
213Archive::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 Spencer9a29db42004-11-20 07:29:40 +0000232 if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
Reid Spencer4a980d12004-11-16 06:47:19 +0000233 // We just save this but don't do anything special
234 // with it. It doesn't count as the "first file".
Reid Spencer9a29db42004-11-20 07:29:40 +0000235 if (foreignST) {
236 // What? Multiple foreign symbol tables? Just chuck it
237 // and retain the last one found.
238 delete foreignST;
239 }
Reid Spencer4a980d12004-11-16 06:47:19 +0000240 foreignST = mbr;
Reid Spencerf9d7a512004-11-14 21:58:33 +0000241 At += mbr->getSize();
Reid Spencer6405c9e2004-11-19 17:08:00 +0000242 if ((intptr_t(At) & 1) == 1)
Reid Spencerf9d7a512004-11-14 21:58:33 +0000243 At++;
244 } else if (mbr->isStringTable()) {
Reid Spencer4a980d12004-11-16 06:47:19 +0000245 // 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 Spencer9a29db42004-11-20 07:29:40 +0000249 strtab.assign(At, mbr->getSize());
Reid Spencerf9d7a512004-11-14 21:58:33 +0000250 At += mbr->getSize();
Reid Spencer6405c9e2004-11-19 17:08:00 +0000251 if ((intptr_t(At) & 1) == 1)
Reid Spencerf9d7a512004-11-14 21:58:33 +0000252 At++;
253 delete mbr;
254 } else if (mbr->isLLVMSymbolTable()) {
Reid Spencer4a980d12004-11-16 06:47:19 +0000255 // 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 Spencerf9d7a512004-11-14 21:58:33 +0000257 if (seenSymbolTable)
258 throw std::string("invalid archive: multiple symbol tables");
Reid Spencer9a29db42004-11-20 07:29:40 +0000259 parseSymbolTable(mbr->getData(), mbr->getSize());
Reid Spencerf9d7a512004-11-14 21:58:33 +0000260 seenSymbolTable = true;
261 At += mbr->getSize();
Reid Spencer6405c9e2004-11-19 17:08:00 +0000262 if ((intptr_t(At) & 1) == 1)
Reid Spencerf9d7a512004-11-14 21:58:33 +0000263 At++;
Reid Spencer4a980d12004-11-16 06:47:19 +0000264 delete mbr; // We don't need this member in the list of members.
Reid Spencerf9d7a512004-11-14 21:58:33 +0000265 } else {
Reid Spencer4a980d12004-11-16 06:47:19 +0000266 // 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 Spencerf9d7a512004-11-14 21:58:33 +0000268 if (!foundFirstFile) {
269 firstFileOffset = Save - base;
270 foundFirstFile = true;
271 }
272 members.push_back(mbr);
273 At += mbr->getSize();
Reid Spencer6405c9e2004-11-19 17:08:00 +0000274 if ((intptr_t(At) & 1) == 1)
Reid Spencerf9d7a512004-11-14 21:58:33 +0000275 At++;
276 }
277 }
278}
279
280// Open and completely load the archive file.
281Archive*
Reid Spencer5af46882004-12-13 02:59:03 +0000282Archive::OpenAndLoad(const sys::Path& file, std::string* ErrorMessage) {
283 try {
Reid Spencer518ec2e2004-12-13 03:22:31 +0000284 std::auto_ptr<Archive> result ( new Archive(file, true));
Reid Spencer5af46882004-12-13 02:59:03 +0000285 result->loadArchive();
Reid Spencer518ec2e2004-12-13 03:22:31 +0000286 return result.release();
Reid Spencer5af46882004-12-13 02:59:03 +0000287 } catch (const std::string& msg) {
288 if (ErrorMessage) {
289 *ErrorMessage = msg;
290 }
291 return 0;
292 }
Reid Spencerf9d7a512004-11-14 21:58:33 +0000293}
294
295// Get all the bytecode modules from the archive
296bool
297Archive::getAllModules(std::vector<Module*>& Modules, std::string* ErrMessage) {
298
299 for (iterator I=begin(), E=end(); I != E; ++I) {
300 if (I->isBytecode() || I->isCompressedBytecode()) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000301 std::string FullMemberName = archPath.toString() +
302 "(" + I->getPath().toString() + ")";
Reid Spencerf9d7a512004-11-14 21:58:33 +0000303 Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
Reid Spencer6ab7a4f2004-11-17 18:25:21 +0000304 I->getSize(), FullMemberName, ErrMessage);
Reid Spencerf9d7a512004-11-14 21:58:33 +0000305 if (!M)
306 return true;
307
308 Modules.push_back(M);
309 }
310 }
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000311 return false;
312}
Chris Lattner968cfd02003-04-19 21:45:34 +0000313
Reid Spencerf9d7a512004-11-14 21:58:33 +0000314// Load just the symbol table from the archive file
315void
316Archive::loadSymbolTable() {
Chris Lattner968cfd02003-04-19 21:45:34 +0000317
Reid Spencerf9d7a512004-11-14 21:58:33 +0000318 // Set up parsing
319 members.clear();
320 symTab.clear();
321 const char *At = base;
322 const char *End = base + mapfile->size();
Chris Lattner968cfd02003-04-19 21:45:34 +0000323
Reid Spencerf9d7a512004-11-14 21:58:33 +0000324 // Make sure we're dealing with an archive
325 checkSignature();
Chris Lattner968cfd02003-04-19 21:45:34 +0000326
Reid Spencerf9d7a512004-11-14 21:58:33 +0000327 At += 8; // Skip signature
328
329 // Parse the first file member header
330 const char* FirstFile = At;
331 ArchiveMember* mbr = parseMemberHeader(At, End);
332
Reid Spencer9a29db42004-11-20 07:29:40 +0000333 if (mbr->isSVR4SymbolTable() || mbr->isBSD4SymbolTable()) {
Reid Spencerf9d7a512004-11-14 21:58:33 +0000334 // Skip the foreign symbol table, we don't do anything with it
335 At += mbr->getSize();
Reid Spencer6405c9e2004-11-19 17:08:00 +0000336 if ((intptr_t(At) & 1) == 1)
Reid Spencerb3231132004-11-15 01:40:20 +0000337 At++;
Reid Spencerf9d7a512004-11-14 21:58:33 +0000338 delete mbr;
339
Reid Spencerb3231132004-11-15 01:40:20 +0000340 // Read the next one
Reid Spencerf9d7a512004-11-14 21:58:33 +0000341 FirstFile = At;
Reid Spencer9a29db42004-11-20 07:29:40 +0000342 mbr = parseMemberHeader(At, End);
Reid Spencerb3231132004-11-15 01:40:20 +0000343 }
344
345 if (mbr->isStringTable()) {
346 // Process the string table entry
Reid Spencer9a29db42004-11-20 07:29:40 +0000347 strtab.assign((const char*)mbr->getData(), mbr->getSize());
Reid Spencerb3231132004-11-15 01:40:20 +0000348 At += mbr->getSize();
Reid Spencer6405c9e2004-11-19 17:08:00 +0000349 if ((intptr_t(At) & 1) == 1)
Reid Spencerb3231132004-11-15 01:40:20 +0000350 At++;
351 delete mbr;
352 // Get the next one
353 FirstFile = At;
Reid Spencer9a29db42004-11-20 07:29:40 +0000354 mbr = parseMemberHeader(At, End);
Chris Lattner968cfd02003-04-19 21:45:34 +0000355 }
356
Reid Spencerf9d7a512004-11-14 21:58:33 +0000357 // See if its the symbol table
358 if (mbr->isLLVMSymbolTable()) {
Reid Spencer9a29db42004-11-20 07:29:40 +0000359 parseSymbolTable(mbr->getData(), mbr->getSize());
Reid Spencer8dde18f2004-11-28 03:13:02 +0000360 At += mbr->getSize();
Reid Spencer6405c9e2004-11-19 17:08:00 +0000361 if ((intptr_t(At) & 1) == 1)
Reid Spencer8dde18f2004-11-28 03:13:02 +0000362 At++;
363 FirstFile = At;
Reid Spencerf9d7a512004-11-14 21:58:33 +0000364 } else {
365 // There's no symbol table in the file. We have to rebuild it from scratch
366 // because the intent of this method is to get the symbol table loaded so
367 // it can be searched efficiently.
368 // Add the member to the members list
369 members.push_back(mbr);
370 }
371
372 firstFileOffset = FirstFile - base;
Chris Lattner968cfd02003-04-19 21:45:34 +0000373}
374
Reid Spencerf9d7a512004-11-14 21:58:33 +0000375// Open the archive and load just the symbol tables
376Archive*
Reid Spencer5af46882004-12-13 02:59:03 +0000377Archive::OpenAndLoadSymbols(const sys::Path& file, std::string* ErrorMessage) {
378 try {
Reid Spencer518ec2e2004-12-13 03:22:31 +0000379 std::auto_ptr<Archive> result ( new Archive(file, true) );
Reid Spencer5af46882004-12-13 02:59:03 +0000380 result->loadSymbolTable();
Reid Spencer518ec2e2004-12-13 03:22:31 +0000381 return result.release();
Reid Spencer5af46882004-12-13 02:59:03 +0000382 } catch (const std::string& msg) {
383 if (ErrorMessage) {
384 *ErrorMessage = msg;
385 }
386 return 0;
387 }
Reid Spencerf9d7a512004-11-14 21:58:33 +0000388}
Chris Lattner968cfd02003-04-19 21:45:34 +0000389
Reid Spencerf9d7a512004-11-14 21:58:33 +0000390// Look up one symbol in the symbol table and return a ModuleProvider for the
391// module that defines that symbol.
392ModuleProvider*
393Archive::findModuleDefiningSymbol(const std::string& symbol) {
394 SymTabType::iterator SI = symTab.find(symbol);
395 if (SI == symTab.end())
396 return 0;
397
398 // The symbol table was previously constructed assuming that the members were
399 // written without the symbol table header. Because VBR encoding is used, the
400 // values could not be adjusted to account for the offset of the symbol table
401 // because that could affect the size of the symbol table due to VBR encoding.
402 // We now have to account for this by adjusting the offset by the size of the
403 // symbol table and its header.
404 unsigned fileOffset =
405 SI->second + // offset in symbol-table-less file
406 firstFileOffset; // add offset to first "real" file in archive
407
408 // See if the module is already loaded
409 ModuleMap::iterator MI = modules.find(fileOffset);
410 if (MI != modules.end())
411 return MI->second.first;
412
413 // Module hasn't been loaded yet, we need to load it
414 const char* modptr = base + fileOffset;
415 ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size());
416
417 // Now, load the bytecode module to get the ModuleProvider
Reid Spencer1fce0912004-12-11 00:14:15 +0000418 std::string FullMemberName = archPath.toString() + "(" +
419 mbr->getPath().toString() + ")";
Reid Spencerf9d7a512004-11-14 21:58:33 +0000420 ModuleProvider* mp = getBytecodeBufferModuleProvider(
421 (const unsigned char*) mbr->getData(), mbr->getSize(),
Reid Spencer6ab7a4f2004-11-17 18:25:21 +0000422 FullMemberName, 0);
Reid Spencerf9d7a512004-11-14 21:58:33 +0000423
Reid Spencer9a29db42004-11-20 07:29:40 +0000424 modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr)));
Reid Spencerf9d7a512004-11-14 21:58:33 +0000425
426 return mp;
427}
428
429// Look up multiple symbols in the symbol table and return a set of
430// ModuleProviders that define those symbols.
431void
Reid Spencer7783e8a2004-11-19 03:18:22 +0000432Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
Reid Spencer766b7932004-11-15 01:20:11 +0000433 std::set<ModuleProvider*>& result)
Reid Spencerf9d7a512004-11-14 21:58:33 +0000434{
Reid Spencer766b7932004-11-15 01:20:11 +0000435 assert(mapfile && base && "Can't findModulesDefiningSymbols on new archive");
436 if (symTab.empty()) {
437 // We don't have a symbol table, so we must build it now but lets also
438 // make sure that we populate the modules table as we do this to ensure
439 // that we don't load them twice when findModuleDefiningSymbol is called
440 // below.
441
442 // Get a pointer to the first file
443 const char* At = ((const char*)base) + firstFileOffset;
444 const char* End = ((const char*)base) + mapfile->size();
445
446 while ( At < End) {
447 // Compute the offset to be put in the symbol table
448 unsigned offset = At - base - firstFileOffset;
449
450 // Parse the file's header
451 ArchiveMember* mbr = parseMemberHeader(At, End);
452
453 // If it contains symbols
454 if (mbr->isBytecode() || mbr->isCompressedBytecode()) {
455 // Get the symbols
456 std::vector<std::string> symbols;
Reid Spencer1fce0912004-12-11 00:14:15 +0000457 std::string FullMemberName = archPath.toString() + "(" +
458 mbr->getPath().toString() + ")";
Reid Spencer766b7932004-11-15 01:20:11 +0000459 ModuleProvider* MP = GetBytecodeSymbols((const unsigned char*)At,
Reid Spencer6ab7a4f2004-11-17 18:25:21 +0000460 mbr->getSize(), FullMemberName, symbols);
Reid Spencer766b7932004-11-15 01:20:11 +0000461
462 if (MP) {
463 // Insert the module's symbols into the symbol table
464 for (std::vector<std::string>::iterator I = symbols.begin(),
465 E=symbols.end(); I != E; ++I ) {
Reid Spencer9a29db42004-11-20 07:29:40 +0000466 symTab.insert(std::make_pair(*I, offset));
Reid Spencer766b7932004-11-15 01:20:11 +0000467 }
468 // Insert the ModuleProvider and the ArchiveMember into the table of
469 // modules.
Reid Spencer9a29db42004-11-20 07:29:40 +0000470 modules.insert(std::make_pair(offset, std::make_pair(MP, mbr)));
Reid Spencer766b7932004-11-15 01:20:11 +0000471 } else {
472 throw std::string("Can't parse bytecode member: ") +
Reid Spencer1fce0912004-12-11 00:14:15 +0000473 mbr->getPath().toString();
Reid Spencer766b7932004-11-15 01:20:11 +0000474 }
475 }
Reid Spencerb3231132004-11-15 01:40:20 +0000476
477 // Go to the next file location
478 At += mbr->getSize();
Reid Spencer6405c9e2004-11-19 17:08:00 +0000479 if ((intptr_t(At) & 1) == 1)
Reid Spencerb3231132004-11-15 01:40:20 +0000480 At++;
Reid Spencer766b7932004-11-15 01:20:11 +0000481 }
482 }
483
484 // At this point we have a valid symbol table (one way or another) so we
485 // just use it to quickly find the symbols requested.
486
Reid Spencer7783e8a2004-11-19 03:18:22 +0000487 for (std::set<std::string>::iterator I=symbols.begin(),
488 E=symbols.end(); I != E;) {
489 // See if this symbol exists
Reid Spencerf9d7a512004-11-14 21:58:33 +0000490 ModuleProvider* mp = findModuleDefiningSymbol(*I);
491 if (mp) {
Reid Spencer7783e8a2004-11-19 03:18:22 +0000492 // The symbol exists, insert the ModuleProvider into our result,
493 // duplicates wil be ignored
Reid Spencer766b7932004-11-15 01:20:11 +0000494 result.insert(mp);
Reid Spencer7783e8a2004-11-19 03:18:22 +0000495
496 // Remove the symbol now that its been resolved, being careful to
Reid Spencer57646ec2004-11-19 03:44:10 +0000497 // post-increment the iterator.
498 symbols.erase(I++);
Reid Spencer7783e8a2004-11-19 03:18:22 +0000499 } else {
500 ++I;
Chris Lattner968cfd02003-04-19 21:45:34 +0000501 }
Reid Spencerf9d7a512004-11-14 21:58:33 +0000502 }
Chris Lattner968cfd02003-04-19 21:45:34 +0000503}