blob: 04cc981f79608dbaa2dfb29848f0bf149c9875df [file] [log] [blame]
Reid Spencercf6afc62004-11-14 21:56:59 +00001//===-- ArchiveWriter.cpp - Write LLVM archive files ----------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Reid Spencer362cbf02004-11-06 08:51:45 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Reid Spencer362cbf02004-11-06 08:51:45 +00008//===----------------------------------------------------------------------===//
9//
Gabor Greifa99be512007-07-05 17:07:56 +000010// Builds up an LLVM archive file (.a) containing LLVM bitcode.
Reid Spencer362cbf02004-11-06 08:51:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "ArchiveInternals.h"
Chris Lattnere07c15c2007-05-06 06:18:07 +000015#include "llvm/Bitcode/ReaderWriter.h"
Reid Spencercf6afc62004-11-14 21:56:59 +000016#include "llvm/System/Signals.h"
Reid Spencer3468e572005-04-21 16:15:19 +000017#include "llvm/System/Process.h"
Chris Lattnerc1d56242007-05-06 09:28:33 +000018#include "llvm/ModuleProvider.h"
Reid Spencer362cbf02004-11-06 08:51:45 +000019#include <fstream>
Bill Wendlinga21900d2006-11-28 22:49:32 +000020#include <ostream>
Reid Spencercf6afc62004-11-14 21:56:59 +000021#include <iomanip>
Reid Spencer362cbf02004-11-06 08:51:45 +000022using namespace llvm;
23
Reid Spencercf6afc62004-11-14 21:56:59 +000024// Write an integer using variable bit rate encoding. This saves a few bytes
25// per entry in the symbol table.
26inline void writeInteger(unsigned num, std::ofstream& ARFile) {
27 while (1) {
28 if (num < 0x80) { // done?
29 ARFile << (unsigned char)num;
30 return;
31 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000032
Reid Spencercf6afc62004-11-14 21:56:59 +000033 // Nope, we are bigger than a character, output the next 7 bits and set the
34 // high bit to say that there is more coming...
35 ARFile << (unsigned char)(0x80 | ((unsigned char)num & 0x7F));
36 num >>= 7; // Shift out 7 bits now...
37 }
38}
39
40// Compute how many bytes are taken by a given VBR encoded value. This is needed
41// to pre-compute the size of the symbol table.
42inline unsigned numVbrBytes(unsigned num) {
Reid Spencercf6afc62004-11-14 21:56:59 +000043
Reid Spencer87f90722004-11-16 06:47:30 +000044 // Note that the following nested ifs are somewhat equivalent to a binary
45 // search. We split it in half by comparing against 2^14 first. This allows
Misha Brukman2b37d7c2005-04-21 21:13:18 +000046 // most reasonable values to be done in 2 comparisons instead of 1 for
Reid Spencer87f90722004-11-16 06:47:30 +000047 // small ones and four for large ones. We expect this to access file offsets
Misha Brukman2b37d7c2005-04-21 21:13:18 +000048 // in the 2^10 to 2^24 range and symbol lengths in the 2^0 to 2^8 range,
Reid Spencer87f90722004-11-16 06:47:30 +000049 // so this approach is reasonable.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +000050 if (num < 1<<14) {
Reid Spencer87f90722004-11-16 06:47:30 +000051 if (num < 1<<7)
52 return 1;
53 else
54 return 2;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +000055 }
Reid Spencer87f90722004-11-16 06:47:30 +000056 if (num < 1<<21)
57 return 3;
58
59 if (num < 1<<28)
60 return 4;
61 return 5; // anything >= 2^28 takes 5 bytes
Reid Spencercf6afc62004-11-14 21:56:59 +000062}
63
64// Create an empty archive.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000065Archive*
Reid Spencercf6afc62004-11-14 21:56:59 +000066Archive::CreateEmpty(const sys::Path& FilePath ) {
Reid Spencer0ff2d312006-08-24 23:45:08 +000067 Archive* result = new Archive(FilePath);
Reid Spencer362cbf02004-11-06 08:51:45 +000068 return result;
69}
70
Misha Brukman2b37d7c2005-04-21 21:13:18 +000071// Fill the ArchiveMemberHeader with the information from a member. If
Reid Spencer87f90722004-11-16 06:47:30 +000072// TruncateNames is true, names are flattened to 15 chars or less. The sz field
Misha Brukman2b37d7c2005-04-21 21:13:18 +000073// is provided here instead of coming from the mbr because the member might be
74// stored compressed and the compressed size is not the ArchiveMember's size.
75// Furthermore compressed files have negative size fields to identify them as
Reid Spencer87f90722004-11-16 06:47:30 +000076// compressed.
Reid Spencercf6afc62004-11-14 21:56:59 +000077bool
78Archive::fillHeader(const ArchiveMember &mbr, ArchiveMemberHeader& hdr,
79 int sz, bool TruncateNames) const {
Reid Spencer362cbf02004-11-06 08:51:45 +000080
Reid Spencercf6afc62004-11-14 21:56:59 +000081 // Set the permissions mode, uid and gid
82 hdr.init();
83 char buffer[32];
84 sprintf(buffer, "%-8o", mbr.getMode());
85 memcpy(hdr.mode,buffer,8);
86 sprintf(buffer, "%-6u", mbr.getUser());
87 memcpy(hdr.uid,buffer,6);
88 sprintf(buffer, "%-6u", mbr.getGroup());
89 memcpy(hdr.gid,buffer,6);
90
Reid Spencercf6afc62004-11-14 21:56:59 +000091 // Set the last modification date
92 uint64_t secondsSinceEpoch = mbr.getModTime().toEpochTime();
93 sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch));
94 memcpy(hdr.date,buffer,12);
95
Reid Spencerd4543da2004-11-17 18:28:29 +000096 // Get rid of trailing blanks in the name
Reid Spencer1fce0912004-12-11 00:14:15 +000097 std::string mbrPath = mbr.getPath().toString();
Reid Spencerd4543da2004-11-17 18:28:29 +000098 size_t mbrLen = mbrPath.length();
99 while (mbrLen > 0 && mbrPath[mbrLen-1] == ' ') {
100 mbrPath.erase(mbrLen-1,1);
101 mbrLen--;
102 }
103
Reid Spencercf6afc62004-11-14 21:56:59 +0000104 // Set the name field in one of its various flavors.
105 bool writeLongName = false;
Reid Spencercf6afc62004-11-14 21:56:59 +0000106 if (mbr.isStringTable()) {
107 memcpy(hdr.name,ARFILE_STRTAB_NAME,16);
Reid Spencer9a29db42004-11-20 07:29:40 +0000108 } else if (mbr.isSVR4SymbolTable()) {
109 memcpy(hdr.name,ARFILE_SVR4_SYMTAB_NAME,16);
110 } else if (mbr.isBSD4SymbolTable()) {
111 memcpy(hdr.name,ARFILE_BSD4_SYMTAB_NAME,16);
Reid Spencercf6afc62004-11-14 21:56:59 +0000112 } else if (mbr.isLLVMSymbolTable()) {
113 memcpy(hdr.name,ARFILE_LLVM_SYMTAB_NAME,16);
114 } else if (TruncateNames) {
115 const char* nm = mbrPath.c_str();
116 unsigned len = mbrPath.length();
117 size_t slashpos = mbrPath.rfind('/');
118 if (slashpos != std::string::npos) {
119 nm += slashpos + 1;
120 len -= slashpos +1;
121 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000122 if (len > 15)
Reid Spencercf6afc62004-11-14 21:56:59 +0000123 len = 15;
Reid Spencer87f90722004-11-16 06:47:30 +0000124 memcpy(hdr.name,nm,len);
Reid Spencercf6afc62004-11-14 21:56:59 +0000125 hdr.name[len] = '/';
126 } else if (mbrPath.length() < 16 && mbrPath.find('/') == std::string::npos) {
Reid Spencerd4543da2004-11-17 18:28:29 +0000127 memcpy(hdr.name,mbrPath.c_str(),mbrPath.length());
Reid Spencercf6afc62004-11-14 21:56:59 +0000128 hdr.name[mbrPath.length()] = '/';
129 } else {
130 std::string nm = "#1/";
131 nm += utostr(mbrPath.length());
Reid Spencerd4543da2004-11-17 18:28:29 +0000132 memcpy(hdr.name,nm.data(),nm.length());
Reid Spencer96ce3352004-11-17 16:14:21 +0000133 if (sz < 0)
134 sz -= mbrPath.length();
135 else
136 sz += mbrPath.length();
Reid Spencercf6afc62004-11-14 21:56:59 +0000137 writeLongName = true;
138 }
Reid Spencer96ce3352004-11-17 16:14:21 +0000139
140 // Set the size field
141 if (sz < 0) {
142 buffer[0] = '-';
143 sprintf(&buffer[1],"%-9u",(unsigned)-sz);
144 } else {
145 sprintf(buffer, "%-10u", (unsigned)sz);
146 }
147 memcpy(hdr.size,buffer,10);
148
Reid Spencercf6afc62004-11-14 21:56:59 +0000149 return writeLongName;
150}
151
Reid Spencer87f90722004-11-16 06:47:30 +0000152// Insert a file into the archive before some other member. This also takes care
153// of extracting the necessary flags and information from the file.
Reid Spencer0ff2d312006-08-24 23:45:08 +0000154bool
155Archive::addFileBefore(const sys::Path& filePath, iterator where,
156 std::string* ErrMsg) {
Reid Spencercd5561a2006-12-15 19:44:51 +0000157 if (!filePath.exists()) {
158 if (ErrMsg)
159 *ErrMsg = "Can not add a non-existent file to archive";
160 return true;
161 }
Reid Spencercf6afc62004-11-14 21:56:59 +0000162
163 ArchiveMember* mbr = new ArchiveMember(this);
164
165 mbr->data = 0;
166 mbr->path = filePath;
Reid Spencer8475ec02007-03-29 19:05:44 +0000167 const sys::FileStatus *FSInfo = mbr->path.getFileStatus(false, ErrMsg);
168 if (FSInfo)
169 mbr->info = *FSInfo;
170 else
Reid Spencer0ff2d312006-08-24 23:45:08 +0000171 return true;
Reid Spencercf6afc62004-11-14 21:56:59 +0000172
173 unsigned flags = 0;
Reid Spencer1fce0912004-12-11 00:14:15 +0000174 bool hasSlash = filePath.toString().find('/') != std::string::npos;
Reid Spencercf6afc62004-11-14 21:56:59 +0000175 if (hasSlash)
176 flags |= ArchiveMember::HasPathFlag;
Reid Spencer1fce0912004-12-11 00:14:15 +0000177 if (hasSlash || filePath.toString().length() > 15)
Reid Spencercf6afc62004-11-14 21:56:59 +0000178 flags |= ArchiveMember::HasLongFilenameFlag;
179 std::string magic;
180 mbr->path.getMagicNumber(magic,4);
181 switch (sys::IdentifyFileType(magic.c_str(),4)) {
Chris Lattnere07c15c2007-05-06 06:18:07 +0000182 case sys::Bitcode_FileType:
Gabor Greife75ca3d2007-07-06 13:38:17 +0000183 flags |= ArchiveMember::BitcodeFlag;
Reid Spencercf6afc62004-11-14 21:56:59 +0000184 break;
185 default:
186 break;
187 }
188 mbr->flags = flags;
189 members.insert(where,mbr);
Reid Spencer0ff2d312006-08-24 23:45:08 +0000190 return false;
Reid Spencercf6afc62004-11-14 21:56:59 +0000191}
192
Reid Spencer87f90722004-11-16 06:47:30 +0000193// Write one member out to the file.
Reid Spencer3039b992006-07-07 19:09:14 +0000194bool
Reid Spencercf6afc62004-11-14 21:56:59 +0000195Archive::writeMember(
196 const ArchiveMember& member,
197 std::ofstream& ARFile,
198 bool CreateSymbolTable,
199 bool TruncateNames,
Reid Spencer3039b992006-07-07 19:09:14 +0000200 bool ShouldCompress,
Reid Spencer0ff2d312006-08-24 23:45:08 +0000201 std::string* ErrMsg
Reid Spencercf6afc62004-11-14 21:56:59 +0000202) {
203
204 unsigned filepos = ARFile.tellp();
205 filepos -= 8;
206
207 // Get the data and its size either from the
208 // member's in-memory data or directly from the file.
209 size_t fSize = member.getSize();
210 const char* data = (const char*)member.getData();
211 sys::MappedFile* mFile = 0;
212 if (!data) {
Reid Spencer751ca6b2006-08-22 16:07:44 +0000213 mFile = new sys::MappedFile();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000214 if (mFile->open(member.getPath(), sys::MappedFile::READ_ACCESS, ErrMsg))
215 return true;
216 if (!(data = (const char*) mFile->map(ErrMsg)))
217 return true;
Reid Spencercf6afc62004-11-14 21:56:59 +0000218 fSize = mFile->size();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000219 }
Reid Spencercf6afc62004-11-14 21:56:59 +0000220
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000221 // Now that we have the data in memory, update the
Gabor Greifa99be512007-07-05 17:07:56 +0000222 // symbol table if its a bitcode file.
Gabor Greife75ca3d2007-07-06 13:38:17 +0000223 if (CreateSymbolTable && member.isBitcode()) {
Reid Spencercf6afc62004-11-14 21:56:59 +0000224 std::vector<std::string> symbols;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000225 std::string FullMemberName = archPath.toString() + "(" +
226 member.getPath().toString()
Reid Spencerd4543da2004-11-17 18:28:29 +0000227 + ")";
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000228 ModuleProvider* MP =
Gabor Greifa99be512007-07-05 17:07:56 +0000229 GetBitcodeSymbols((const unsigned char*)data,fSize,
230 FullMemberName, symbols, ErrMsg);
Reid Spencercf6afc62004-11-14 21:56:59 +0000231
Gabor Greifa99be512007-07-05 17:07:56 +0000232 // If the bitcode parsed successfully
Reid Spencer766b7932004-11-15 01:20:11 +0000233 if ( MP ) {
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000234 for (std::vector<std::string>::iterator SI = symbols.begin(),
Reid Spencer766b7932004-11-15 01:20:11 +0000235 SE = symbols.end(); SI != SE; ++SI) {
Reid Spencercf6afc62004-11-14 21:56:59 +0000236
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000237 std::pair<SymTabType::iterator,bool> Res =
Reid Spencer766b7932004-11-15 01:20:11 +0000238 symTab.insert(std::make_pair(*SI,filepos));
239
240 if (Res.second) {
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000241 symTabSize += SI->length() +
242 numVbrBytes(SI->length()) +
Reid Spencer766b7932004-11-15 01:20:11 +0000243 numVbrBytes(filepos);
244 }
Reid Spencer362cbf02004-11-06 08:51:45 +0000245 }
Reid Spencer766b7932004-11-15 01:20:11 +0000246 // We don't need this module any more.
247 delete MP;
248 } else {
Reid Spencer3039b992006-07-07 19:09:14 +0000249 if (mFile != 0) {
250 mFile->close();
251 delete mFile;
252 }
Reid Spencer0ff2d312006-08-24 23:45:08 +0000253 if (ErrMsg)
Gabor Greifa99be512007-07-05 17:07:56 +0000254 *ErrMsg = "Can't parse bitcode member: " + member.getPath().toString()
Reid Spencer0b5a5042006-08-25 17:43:11 +0000255 + ": " + *ErrMsg;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000256 return true;
Reid Spencer362cbf02004-11-06 08:51:45 +0000257 }
258 }
Reid Spencer362cbf02004-11-06 08:51:45 +0000259
Chris Lattnere07c15c2007-05-06 06:18:07 +0000260 int hdrSize = fSize;
Reid Spencer362cbf02004-11-06 08:51:45 +0000261
Reid Spencercf6afc62004-11-14 21:56:59 +0000262 // Compute the fields of the header
Reid Spencer362cbf02004-11-06 08:51:45 +0000263 ArchiveMemberHeader Hdr;
Reid Spencercf6afc62004-11-14 21:56:59 +0000264 bool writeLongName = fillHeader(member,Hdr,hdrSize,TruncateNames);
Reid Spencer362cbf02004-11-06 08:51:45 +0000265
266 // Write header to archive file
267 ARFile.write((char*)&Hdr, sizeof(Hdr));
Reid Spencer362cbf02004-11-06 08:51:45 +0000268
Reid Spencercf6afc62004-11-14 21:56:59 +0000269 // Write the long filename if its long
270 if (writeLongName) {
Reid Spencer1fce0912004-12-11 00:14:15 +0000271 ARFile.write(member.getPath().toString().data(),
272 member.getPath().toString().length());
Reid Spencercf6afc62004-11-14 21:56:59 +0000273 }
274
Reid Spencercf6afc62004-11-14 21:56:59 +0000275 // Write the (possibly compressed) member's content to the file.
276 ARFile.write(data,fSize);
277
278 // Make sure the member is an even length
Jeff Cohene1337212004-12-20 03:23:46 +0000279 if ((ARFile.tellp() & 1) == 1)
Reid Spencercf6afc62004-11-14 21:56:59 +0000280 ARFile << ARFILE_PAD;
281
Reid Spencercf6afc62004-11-14 21:56:59 +0000282 // Close the mapped file if it was opened
283 if (mFile != 0) {
Jeff Cohend19d89a2005-01-28 01:17:07 +0000284 mFile->close();
Reid Spencercf6afc62004-11-14 21:56:59 +0000285 delete mFile;
286 }
Reid Spencer0ff2d312006-08-24 23:45:08 +0000287 return false;
Reid Spencer362cbf02004-11-06 08:51:45 +0000288}
289
Reid Spencer87f90722004-11-16 06:47:30 +0000290// Write out the LLVM symbol table as an archive member to the file.
Reid Spencer362cbf02004-11-06 08:51:45 +0000291void
Reid Spencer87f90722004-11-16 06:47:30 +0000292Archive::writeSymbolTable(std::ofstream& ARFile) {
Reid Spencercf6afc62004-11-14 21:56:59 +0000293
294 // Construct the symbol table's header
295 ArchiveMemberHeader Hdr;
296 Hdr.init();
297 memcpy(Hdr.name,ARFILE_LLVM_SYMTAB_NAME,16);
298 uint64_t secondsSinceEpoch = sys::TimeValue::now().toEpochTime();
299 char buffer[32];
Misha Brukman4b2afe62005-04-20 03:55:35 +0000300 sprintf(buffer, "%-8o", 0644);
301 memcpy(Hdr.mode,buffer,8);
Reid Spencer3468e572005-04-21 16:15:19 +0000302 sprintf(buffer, "%-6u", sys::Process::GetCurrentUserId());
Misha Brukman4b2afe62005-04-20 03:55:35 +0000303 memcpy(Hdr.uid,buffer,6);
Reid Spencer3468e572005-04-21 16:15:19 +0000304 sprintf(buffer, "%-6u", sys::Process::GetCurrentGroupId());
Misha Brukman4b2afe62005-04-20 03:55:35 +0000305 memcpy(Hdr.gid,buffer,6);
Reid Spencercf6afc62004-11-14 21:56:59 +0000306 sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch));
307 memcpy(Hdr.date,buffer,12);
308 sprintf(buffer,"%-10u",symTabSize);
309 memcpy(Hdr.size,buffer,10);
310
311 // Write the header
312 ARFile.write((char*)&Hdr, sizeof(Hdr));
313
314 // Save the starting position of the symbol tables data content.
315 unsigned startpos = ARFile.tellp();
316
Reid Spencercf6afc62004-11-14 21:56:59 +0000317 // Write out the symbols sequentially
318 for ( Archive::SymTabType::iterator I = symTab.begin(), E = symTab.end();
319 I != E; ++I)
320 {
321 // Write out the file index
322 writeInteger(I->second, ARFile);
323 // Write out the length of the symbol
324 writeInteger(I->first.length(), ARFile);
325 // Write out the symbol
326 ARFile.write(I->first.data(), I->first.length());
Reid Spencer362cbf02004-11-06 08:51:45 +0000327 }
328
Reid Spencercf6afc62004-11-14 21:56:59 +0000329 // Now that we're done with the symbol table, get the ending file position
330 unsigned endpos = ARFile.tellp();
Reid Spencer362cbf02004-11-06 08:51:45 +0000331
Reid Spencercf6afc62004-11-14 21:56:59 +0000332 // Make sure that the amount we wrote is what we pre-computed. This is
333 // critical for file integrity purposes.
334 assert(endpos - startpos == symTabSize && "Invalid symTabSize computation");
Reid Spencer362cbf02004-11-06 08:51:45 +0000335
Reid Spencercf6afc62004-11-14 21:56:59 +0000336 // Make sure the symbol table is even sized
337 if (symTabSize % 2 != 0 )
338 ARFile << ARFILE_PAD;
Reid Spencer362cbf02004-11-06 08:51:45 +0000339}
340
Reid Spencer87f90722004-11-16 06:47:30 +0000341// Write the entire archive to the file specified when the archive was created.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000342// This writes to a temporary file first. Options are for creating a symbol
343// table, flattening the file names (no directories, 15 chars max) and
Reid Spencer87f90722004-11-16 06:47:30 +0000344// compressing each archive member.
Reid Spencer3039b992006-07-07 19:09:14 +0000345bool
346Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
Reid Spencer0ff2d312006-08-24 23:45:08 +0000347 std::string* ErrMsg)
Reid Spencer3039b992006-07-07 19:09:14 +0000348{
Reid Spencercf6afc62004-11-14 21:56:59 +0000349 // Make sure they haven't opened up the file, not loaded it,
350 // but are now trying to write it which would wipe out the file.
Andrew Lenharth63b8c1f2008-02-28 22:24:48 +0000351 if (members.empty() && mapfile && mapfile->size() > 8) {
Reid Spencercd5561a2006-12-15 19:44:51 +0000352 if (ErrMsg)
353 *ErrMsg = "Can't write an archive not opened for writing";
354 return true;
355 }
Reid Spencercf6afc62004-11-14 21:56:59 +0000356
357 // Create a temporary file to store the archive in
358 sys::Path TmpArchive = archPath;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000359 if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
360 return true;
Reid Spencercf6afc62004-11-14 21:56:59 +0000361
362 // Make sure the temporary gets removed if we crash
363 sys::RemoveFileOnSignal(TmpArchive);
364
Reid Spencer3039b992006-07-07 19:09:14 +0000365 // Create archive file for output.
366 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
367 std::ios::binary;
368 std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000369
Reid Spencer3039b992006-07-07 19:09:14 +0000370 // Check for errors opening or creating archive file.
Chris Lattner0c332312006-07-28 22:29:50 +0000371 if (!ArchiveFile.is_open() || ArchiveFile.bad()) {
Reid Spencercf6afc62004-11-14 21:56:59 +0000372 if (TmpArchive.exists())
Reid Spencera229c5c2005-07-08 03:08:58 +0000373 TmpArchive.eraseFromDisk();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000374 if (ErrMsg)
375 *ErrMsg = "Error opening archive file: " + archPath.toString();
376 return true;
Reid Spencercf6afc62004-11-14 21:56:59 +0000377 }
Reid Spencer3039b992006-07-07 19:09:14 +0000378
379 // If we're creating a symbol table, reset it now
380 if (CreateSymbolTable) {
381 symTabSize = 0;
382 symTab.clear();
383 }
384
385 // Write magic string to archive.
386 ArchiveFile << ARFILE_MAGIC;
387
388 // Loop over all member files, and write them out. Note that this also
389 // builds the symbol table, symTab.
Chris Lattner0c332312006-07-28 22:29:50 +0000390 for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000391 if (writeMember(*I, ArchiveFile, CreateSymbolTable,
392 TruncateNames, Compress, ErrMsg)) {
Reid Spencer3039b992006-07-07 19:09:14 +0000393 if (TmpArchive.exists())
394 TmpArchive.eraseFromDisk();
395 ArchiveFile.close();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000396 return true;
Reid Spencer3039b992006-07-07 19:09:14 +0000397 }
398 }
399
400 // Close archive file.
401 ArchiveFile.close();
402
403 // Write the symbol table
404 if (CreateSymbolTable) {
405 // At this point we have written a file that is a legal archive but it
406 // doesn't have a symbol table in it. To aid in faster reading and to
407 // ensure compatibility with other archivers we need to put the symbol
408 // table first in the file. Unfortunately, this means mapping the file
409 // we just wrote back in and copying it to the destination file.
410
411 // Map in the archive we just wrote.
Reid Spencer751ca6b2006-08-22 16:07:44 +0000412 sys::MappedFile arch;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000413 if (arch.open(TmpArchive, sys::MappedFile::READ_ACCESS, ErrMsg))
414 return true;
Reid Spencer751ca6b2006-08-22 16:07:44 +0000415 const char* base;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000416 if (!(base = (const char*) arch.map(ErrMsg)))
417 return true;
Reid Spencer3039b992006-07-07 19:09:14 +0000418
419 // Open another temporary file in order to avoid invalidating the
420 // mmapped data
421 sys::Path FinalFilePath = archPath;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000422 if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
423 return true;
Reid Spencer3039b992006-07-07 19:09:14 +0000424 sys::RemoveFileOnSignal(FinalFilePath);
425
426 std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
Chris Lattner0c332312006-07-28 22:29:50 +0000427 if (!FinalFile.is_open() || FinalFile.bad()) {
Reid Spencer3039b992006-07-07 19:09:14 +0000428 if (TmpArchive.exists())
429 TmpArchive.eraseFromDisk();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000430 if (ErrMsg)
431 *ErrMsg = "Error opening archive file: " + FinalFilePath.toString();
432 return true;
Reid Spencer3039b992006-07-07 19:09:14 +0000433 }
434
435 // Write the file magic number
436 FinalFile << ARFILE_MAGIC;
437
438 // If there is a foreign symbol table, put it into the file now. Most
439 // ar(1) implementations require the symbol table to be first but llvm-ar
440 // can deal with it being after a foreign symbol table. This ensures
441 // compatibility with other ar(1) implementations as well as allowing the
442 // archive to store both native .o and LLVM .bc files, both indexed.
443 if (foreignST) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000444 if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000445 FinalFile.close();
446 if (TmpArchive.exists())
447 TmpArchive.eraseFromDisk();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000448 return true;
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000449 }
Reid Spencer3039b992006-07-07 19:09:14 +0000450 }
451
452 // Put out the LLVM symbol table now.
453 writeSymbolTable(FinalFile);
454
455 // Copy the temporary file contents being sure to skip the file's magic
456 // number.
457 FinalFile.write(base + sizeof(ARFILE_MAGIC)-1,
458 arch.size()-sizeof(ARFILE_MAGIC)+1);
459
460 // Close up shop
461 FinalFile.close();
462 arch.close();
463
464 // Move the final file over top of TmpArchive
Reid Spencer0ff2d312006-08-24 23:45:08 +0000465 if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
466 return true;
Reid Spencer3039b992006-07-07 19:09:14 +0000467 }
468
469 // Before we replace the actual archive, we need to forget all the
470 // members, since they point to data in that old archive. We need to do
471 // this because we cannot replace an open file on Windows.
472 cleanUpMemory();
473
Reid Spencer0ff2d312006-08-24 23:45:08 +0000474 if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
475 return true;
Reid Spencer3039b992006-07-07 19:09:14 +0000476
Reid Spencer0ff2d312006-08-24 23:45:08 +0000477 return false;
Reid Spencercf6afc62004-11-14 21:56:59 +0000478}