blob: 07516c6aa45eebf9bc234ce2ef5cfd067796aa46 [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"
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000015#include "llvm/Module.h"
Chris Lattner7f6b4472008-04-01 04:26:46 +000016#include "llvm/ADT/OwningPtr.h"
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000017#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner7f6b4472008-04-01 04:26:46 +000018#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000019#include "llvm/Support/Process.h"
20#include "llvm/Support/Signals.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000021#include "llvm/Support/system_error.h"
Reid Spencer362cbf02004-11-06 08:51:45 +000022#include <fstream>
Bill Wendlinga21900d2006-11-28 22:49:32 +000023#include <ostream>
Reid Spencercf6afc62004-11-14 21:56:59 +000024#include <iomanip>
Reid Spencer362cbf02004-11-06 08:51:45 +000025using namespace llvm;
26
Reid Spencercf6afc62004-11-14 21:56:59 +000027// Write an integer using variable bit rate encoding. This saves a few bytes
28// per entry in the symbol table.
Dan Gohman844731a2008-05-13 00:00:25 +000029static inline void writeInteger(unsigned num, std::ofstream& ARFile) {
Reid Spencercf6afc62004-11-14 21:56:59 +000030 while (1) {
31 if (num < 0x80) { // done?
32 ARFile << (unsigned char)num;
33 return;
34 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000035
Reid Spencercf6afc62004-11-14 21:56:59 +000036 // Nope, we are bigger than a character, output the next 7 bits and set the
37 // high bit to say that there is more coming...
38 ARFile << (unsigned char)(0x80 | ((unsigned char)num & 0x7F));
39 num >>= 7; // Shift out 7 bits now...
40 }
41}
42
43// Compute how many bytes are taken by a given VBR encoded value. This is needed
44// to pre-compute the size of the symbol table.
Dan Gohman844731a2008-05-13 00:00:25 +000045static inline unsigned numVbrBytes(unsigned num) {
Reid Spencercf6afc62004-11-14 21:56:59 +000046
Reid Spencer87f90722004-11-16 06:47:30 +000047 // Note that the following nested ifs are somewhat equivalent to a binary
48 // search. We split it in half by comparing against 2^14 first. This allows
Misha Brukman2b37d7c2005-04-21 21:13:18 +000049 // most reasonable values to be done in 2 comparisons instead of 1 for
Reid Spencer87f90722004-11-16 06:47:30 +000050 // small ones and four for large ones. We expect this to access file offsets
Misha Brukman2b37d7c2005-04-21 21:13:18 +000051 // 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 +000052 // so this approach is reasonable.
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +000053 if (num < 1<<14) {
Reid Spencer87f90722004-11-16 06:47:30 +000054 if (num < 1<<7)
55 return 1;
56 else
57 return 2;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +000058 }
Reid Spencer87f90722004-11-16 06:47:30 +000059 if (num < 1<<21)
60 return 3;
61
62 if (num < 1<<28)
63 return 4;
64 return 5; // anything >= 2^28 takes 5 bytes
Reid Spencercf6afc62004-11-14 21:56:59 +000065}
66
67// Create an empty archive.
Owen Anderson4434ed42009-07-01 23:13:44 +000068Archive* Archive::CreateEmpty(const sys::Path& FilePath, LLVMContext& C) {
Owen Anderson8b477ed2009-07-01 16:58:40 +000069 Archive* result = new Archive(FilePath, C);
Reid Spencer362cbf02004-11-06 08:51:45 +000070 return result;
71}
72
Misha Brukman2b37d7c2005-04-21 21:13:18 +000073// Fill the ArchiveMemberHeader with the information from a member. If
Reid Spencer87f90722004-11-16 06:47:30 +000074// TruncateNames is true, names are flattened to 15 chars or less. The sz field
Misha Brukman2b37d7c2005-04-21 21:13:18 +000075// is provided here instead of coming from the mbr because the member might be
76// stored compressed and the compressed size is not the ArchiveMember's size.
77// Furthermore compressed files have negative size fields to identify them as
Reid Spencer87f90722004-11-16 06:47:30 +000078// compressed.
Reid Spencercf6afc62004-11-14 21:56:59 +000079bool
80Archive::fillHeader(const ArchiveMember &mbr, ArchiveMemberHeader& hdr,
81 int sz, bool TruncateNames) const {
Reid Spencer362cbf02004-11-06 08:51:45 +000082
Reid Spencercf6afc62004-11-14 21:56:59 +000083 // Set the permissions mode, uid and gid
84 hdr.init();
85 char buffer[32];
86 sprintf(buffer, "%-8o", mbr.getMode());
87 memcpy(hdr.mode,buffer,8);
88 sprintf(buffer, "%-6u", mbr.getUser());
89 memcpy(hdr.uid,buffer,6);
90 sprintf(buffer, "%-6u", mbr.getGroup());
91 memcpy(hdr.gid,buffer,6);
92
Reid Spencercf6afc62004-11-14 21:56:59 +000093 // Set the last modification date
94 uint64_t secondsSinceEpoch = mbr.getModTime().toEpochTime();
95 sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch));
96 memcpy(hdr.date,buffer,12);
97
Reid Spencerd4543da2004-11-17 18:28:29 +000098 // Get rid of trailing blanks in the name
Chris Lattner74382b72009-08-23 22:45:37 +000099 std::string mbrPath = mbr.getPath().str();
Reid Spencerd4543da2004-11-17 18:28:29 +0000100 size_t mbrLen = mbrPath.length();
101 while (mbrLen > 0 && mbrPath[mbrLen-1] == ' ') {
102 mbrPath.erase(mbrLen-1,1);
103 mbrLen--;
104 }
105
Reid Spencercf6afc62004-11-14 21:56:59 +0000106 // Set the name field in one of its various flavors.
107 bool writeLongName = false;
Reid Spencercf6afc62004-11-14 21:56:59 +0000108 if (mbr.isStringTable()) {
109 memcpy(hdr.name,ARFILE_STRTAB_NAME,16);
Reid Spencer9a29db42004-11-20 07:29:40 +0000110 } else if (mbr.isSVR4SymbolTable()) {
111 memcpy(hdr.name,ARFILE_SVR4_SYMTAB_NAME,16);
112 } else if (mbr.isBSD4SymbolTable()) {
113 memcpy(hdr.name,ARFILE_BSD4_SYMTAB_NAME,16);
Reid Spencercf6afc62004-11-14 21:56:59 +0000114 } else if (mbr.isLLVMSymbolTable()) {
115 memcpy(hdr.name,ARFILE_LLVM_SYMTAB_NAME,16);
116 } else if (TruncateNames) {
117 const char* nm = mbrPath.c_str();
118 unsigned len = mbrPath.length();
119 size_t slashpos = mbrPath.rfind('/');
120 if (slashpos != std::string::npos) {
121 nm += slashpos + 1;
122 len -= slashpos +1;
123 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000124 if (len > 15)
Reid Spencercf6afc62004-11-14 21:56:59 +0000125 len = 15;
Reid Spencer87f90722004-11-16 06:47:30 +0000126 memcpy(hdr.name,nm,len);
Reid Spencercf6afc62004-11-14 21:56:59 +0000127 hdr.name[len] = '/';
128 } else if (mbrPath.length() < 16 && mbrPath.find('/') == std::string::npos) {
Reid Spencerd4543da2004-11-17 18:28:29 +0000129 memcpy(hdr.name,mbrPath.c_str(),mbrPath.length());
Reid Spencercf6afc62004-11-14 21:56:59 +0000130 hdr.name[mbrPath.length()] = '/';
131 } else {
132 std::string nm = "#1/";
133 nm += utostr(mbrPath.length());
Reid Spencerd4543da2004-11-17 18:28:29 +0000134 memcpy(hdr.name,nm.data(),nm.length());
Reid Spencer96ce3352004-11-17 16:14:21 +0000135 if (sz < 0)
136 sz -= mbrPath.length();
137 else
138 sz += mbrPath.length();
Reid Spencercf6afc62004-11-14 21:56:59 +0000139 writeLongName = true;
140 }
Reid Spencer96ce3352004-11-17 16:14:21 +0000141
142 // Set the size field
143 if (sz < 0) {
144 buffer[0] = '-';
145 sprintf(&buffer[1],"%-9u",(unsigned)-sz);
146 } else {
147 sprintf(buffer, "%-10u", (unsigned)sz);
148 }
149 memcpy(hdr.size,buffer,10);
150
Reid Spencercf6afc62004-11-14 21:56:59 +0000151 return writeLongName;
152}
153
Reid Spencer87f90722004-11-16 06:47:30 +0000154// Insert a file into the archive before some other member. This also takes care
155// of extracting the necessary flags and information from the file.
Reid Spencer0ff2d312006-08-24 23:45:08 +0000156bool
157Archive::addFileBefore(const sys::Path& filePath, iterator where,
158 std::string* ErrMsg) {
Reid Spencercd5561a2006-12-15 19:44:51 +0000159 if (!filePath.exists()) {
160 if (ErrMsg)
161 *ErrMsg = "Can not add a non-existent file to archive";
162 return true;
163 }
Reid Spencercf6afc62004-11-14 21:56:59 +0000164
165 ArchiveMember* mbr = new ArchiveMember(this);
166
167 mbr->data = 0;
168 mbr->path = filePath;
Reid Spencer8475ec02007-03-29 19:05:44 +0000169 const sys::FileStatus *FSInfo = mbr->path.getFileStatus(false, ErrMsg);
Duncan Sands861d20a2009-06-11 08:09:49 +0000170 if (!FSInfo) {
171 delete mbr;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000172 return true;
Duncan Sands861d20a2009-06-11 08:09:49 +0000173 }
174 mbr->info = *FSInfo;
Reid Spencercf6afc62004-11-14 21:56:59 +0000175
176 unsigned flags = 0;
Chris Lattner74382b72009-08-23 22:45:37 +0000177 bool hasSlash = filePath.str().find('/') != std::string::npos;
Reid Spencercf6afc62004-11-14 21:56:59 +0000178 if (hasSlash)
179 flags |= ArchiveMember::HasPathFlag;
Chris Lattner74382b72009-08-23 22:45:37 +0000180 if (hasSlash || filePath.str().length() > 15)
Reid Spencercf6afc62004-11-14 21:56:59 +0000181 flags |= ArchiveMember::HasLongFilenameFlag;
182 std::string magic;
183 mbr->path.getMagicNumber(magic,4);
184 switch (sys::IdentifyFileType(magic.c_str(),4)) {
Chris Lattnere07c15c2007-05-06 06:18:07 +0000185 case sys::Bitcode_FileType:
Gabor Greife75ca3d2007-07-06 13:38:17 +0000186 flags |= ArchiveMember::BitcodeFlag;
Reid Spencercf6afc62004-11-14 21:56:59 +0000187 break;
188 default:
189 break;
190 }
191 mbr->flags = flags;
192 members.insert(where,mbr);
Reid Spencer0ff2d312006-08-24 23:45:08 +0000193 return false;
Reid Spencercf6afc62004-11-14 21:56:59 +0000194}
195
Reid Spencer87f90722004-11-16 06:47:30 +0000196// Write one member out to the file.
Reid Spencer3039b992006-07-07 19:09:14 +0000197bool
Reid Spencercf6afc62004-11-14 21:56:59 +0000198Archive::writeMember(
199 const ArchiveMember& member,
200 std::ofstream& ARFile,
201 bool CreateSymbolTable,
202 bool TruncateNames,
Reid Spencer3039b992006-07-07 19:09:14 +0000203 bool ShouldCompress,
Reid Spencer0ff2d312006-08-24 23:45:08 +0000204 std::string* ErrMsg
Reid Spencercf6afc62004-11-14 21:56:59 +0000205) {
206
207 unsigned filepos = ARFile.tellp();
208 filepos -= 8;
209
210 // Get the data and its size either from the
211 // member's in-memory data or directly from the file.
212 size_t fSize = member.getSize();
Chris Lattner7f6b4472008-04-01 04:26:46 +0000213 const char *data = (const char*)member.getData();
214 MemoryBuffer *mFile = 0;
Reid Spencercf6afc62004-11-14 21:56:59 +0000215 if (!data) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000216 OwningPtr<MemoryBuffer> File;
217 if (error_code ec = MemoryBuffer::getFile(member.getPath().c_str(), File)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000218 if (ErrMsg)
219 *ErrMsg = ec.message();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000220 return true;
Michael J. Spencer333fb042010-12-09 17:36:48 +0000221 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000222 mFile = File.take();
Chris Lattner7f6b4472008-04-01 04:26:46 +0000223 data = mFile->getBufferStart();
224 fSize = mFile->getBufferSize();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000225 }
Reid Spencercf6afc62004-11-14 21:56:59 +0000226
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000227 // Now that we have the data in memory, update the
Dan Gohman4bb31bf2010-03-30 20:04:57 +0000228 // symbol table if it's a bitcode file.
Gabor Greife75ca3d2007-07-06 13:38:17 +0000229 if (CreateSymbolTable && member.isBitcode()) {
Reid Spencercf6afc62004-11-14 21:56:59 +0000230 std::vector<std::string> symbols;
Chris Lattner74382b72009-08-23 22:45:37 +0000231 std::string FullMemberName = archPath.str() + "(" + member.getPath().str()
Reid Spencerd4543da2004-11-17 18:28:29 +0000232 + ")";
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000233 Module* M =
Benjamin Kramer9d44e702010-04-19 16:15:31 +0000234 GetBitcodeSymbols(data, fSize, FullMemberName, Context, symbols, ErrMsg);
Reid Spencercf6afc62004-11-14 21:56:59 +0000235
Gabor Greifa99be512007-07-05 17:07:56 +0000236 // If the bitcode parsed successfully
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000237 if ( M ) {
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000238 for (std::vector<std::string>::iterator SI = symbols.begin(),
Reid Spencer766b7932004-11-15 01:20:11 +0000239 SE = symbols.end(); SI != SE; ++SI) {
Reid Spencercf6afc62004-11-14 21:56:59 +0000240
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000241 std::pair<SymTabType::iterator,bool> Res =
Reid Spencer766b7932004-11-15 01:20:11 +0000242 symTab.insert(std::make_pair(*SI,filepos));
243
244 if (Res.second) {
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000245 symTabSize += SI->length() +
246 numVbrBytes(SI->length()) +
Reid Spencer766b7932004-11-15 01:20:11 +0000247 numVbrBytes(filepos);
248 }
Reid Spencer362cbf02004-11-06 08:51:45 +0000249 }
Reid Spencer766b7932004-11-15 01:20:11 +0000250 // We don't need this module any more.
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000251 delete M;
Reid Spencer766b7932004-11-15 01:20:11 +0000252 } else {
Chris Lattner7f6b4472008-04-01 04:26:46 +0000253 delete mFile;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000254 if (ErrMsg)
Chris Lattner74382b72009-08-23 22:45:37 +0000255 *ErrMsg = "Can't parse bitcode member: " + member.getPath().str()
Reid Spencer0b5a5042006-08-25 17:43:11 +0000256 + ": " + *ErrMsg;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000257 return true;
Reid Spencer362cbf02004-11-06 08:51:45 +0000258 }
259 }
Reid Spencer362cbf02004-11-06 08:51:45 +0000260
Chris Lattnere07c15c2007-05-06 06:18:07 +0000261 int hdrSize = fSize;
Reid Spencer362cbf02004-11-06 08:51:45 +0000262
Reid Spencercf6afc62004-11-14 21:56:59 +0000263 // Compute the fields of the header
Reid Spencer362cbf02004-11-06 08:51:45 +0000264 ArchiveMemberHeader Hdr;
Reid Spencercf6afc62004-11-14 21:56:59 +0000265 bool writeLongName = fillHeader(member,Hdr,hdrSize,TruncateNames);
Reid Spencer362cbf02004-11-06 08:51:45 +0000266
267 // Write header to archive file
268 ARFile.write((char*)&Hdr, sizeof(Hdr));
Reid Spencer362cbf02004-11-06 08:51:45 +0000269
Reid Spencercf6afc62004-11-14 21:56:59 +0000270 // Write the long filename if its long
271 if (writeLongName) {
Chris Lattner74382b72009-08-23 22:45:37 +0000272 ARFile.write(member.getPath().str().data(),
273 member.getPath().str().length());
Reid Spencercf6afc62004-11-14 21:56:59 +0000274 }
275
Reid Spencercf6afc62004-11-14 21:56:59 +0000276 // Write the (possibly compressed) member's content to the file.
277 ARFile.write(data,fSize);
278
279 // Make sure the member is an even length
Jeff Cohene1337212004-12-20 03:23:46 +0000280 if ((ARFile.tellp() & 1) == 1)
Reid Spencercf6afc62004-11-14 21:56:59 +0000281 ARFile << ARFILE_PAD;
282
Reid Spencercf6afc62004-11-14 21:56:59 +0000283 // Close the mapped file if it was opened
Chris Lattner7f6b4472008-04-01 04:26:46 +0000284 delete mFile;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000285 return false;
Reid Spencer362cbf02004-11-06 08:51:45 +0000286}
287
Reid Spencer87f90722004-11-16 06:47:30 +0000288// Write out the LLVM symbol table as an archive member to the file.
Reid Spencer362cbf02004-11-06 08:51:45 +0000289void
Reid Spencer87f90722004-11-16 06:47:30 +0000290Archive::writeSymbolTable(std::ofstream& ARFile) {
Reid Spencercf6afc62004-11-14 21:56:59 +0000291
292 // Construct the symbol table's header
293 ArchiveMemberHeader Hdr;
294 Hdr.init();
295 memcpy(Hdr.name,ARFILE_LLVM_SYMTAB_NAME,16);
296 uint64_t secondsSinceEpoch = sys::TimeValue::now().toEpochTime();
297 char buffer[32];
Misha Brukman4b2afe62005-04-20 03:55:35 +0000298 sprintf(buffer, "%-8o", 0644);
299 memcpy(Hdr.mode,buffer,8);
Reid Spencer3468e572005-04-21 16:15:19 +0000300 sprintf(buffer, "%-6u", sys::Process::GetCurrentUserId());
Misha Brukman4b2afe62005-04-20 03:55:35 +0000301 memcpy(Hdr.uid,buffer,6);
Reid Spencer3468e572005-04-21 16:15:19 +0000302 sprintf(buffer, "%-6u", sys::Process::GetCurrentGroupId());
Misha Brukman4b2afe62005-04-20 03:55:35 +0000303 memcpy(Hdr.gid,buffer,6);
Reid Spencercf6afc62004-11-14 21:56:59 +0000304 sprintf(buffer,"%-12u", unsigned(secondsSinceEpoch));
305 memcpy(Hdr.date,buffer,12);
306 sprintf(buffer,"%-10u",symTabSize);
307 memcpy(Hdr.size,buffer,10);
308
309 // Write the header
310 ARFile.write((char*)&Hdr, sizeof(Hdr));
311
Devang Patel59500c82008-11-21 20:00:59 +0000312#ifndef NDEBUG
Reid Spencercf6afc62004-11-14 21:56:59 +0000313 // Save the starting position of the symbol tables data content.
314 unsigned startpos = ARFile.tellp();
Devang Patel59500c82008-11-21 20:00:59 +0000315#endif
Reid Spencercf6afc62004-11-14 21:56:59 +0000316
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
Devang Patel59500c82008-11-21 20:00:59 +0000329#ifndef NDEBUG
Reid Spencercf6afc62004-11-14 21:56:59 +0000330 // Now that we're done with the symbol table, get the ending file position
331 unsigned endpos = ARFile.tellp();
Devang Patel59500c82008-11-21 20:00:59 +0000332#endif
Reid Spencer362cbf02004-11-06 08:51:45 +0000333
Reid Spencercf6afc62004-11-14 21:56:59 +0000334 // Make sure that the amount we wrote is what we pre-computed. This is
335 // critical for file integrity purposes.
336 assert(endpos - startpos == symTabSize && "Invalid symTabSize computation");
Reid Spencer362cbf02004-11-06 08:51:45 +0000337
Reid Spencercf6afc62004-11-14 21:56:59 +0000338 // Make sure the symbol table is even sized
339 if (symTabSize % 2 != 0 )
340 ARFile << ARFILE_PAD;
Reid Spencer362cbf02004-11-06 08:51:45 +0000341}
342
Reid Spencer87f90722004-11-16 06:47:30 +0000343// Write the entire archive to the file specified when the archive was created.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000344// This writes to a temporary file first. Options are for creating a symbol
345// table, flattening the file names (no directories, 15 chars max) and
Reid Spencer87f90722004-11-16 06:47:30 +0000346// compressing each archive member.
Reid Spencer3039b992006-07-07 19:09:14 +0000347bool
348Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
Reid Spencer0ff2d312006-08-24 23:45:08 +0000349 std::string* ErrMsg)
Reid Spencer3039b992006-07-07 19:09:14 +0000350{
Reid Spencercf6afc62004-11-14 21:56:59 +0000351 // Make sure they haven't opened up the file, not loaded it,
352 // but are now trying to write it which would wipe out the file.
Chris Lattner7f6b4472008-04-01 04:26:46 +0000353 if (members.empty() && mapfile && mapfile->getBufferSize() > 8) {
Reid Spencercd5561a2006-12-15 19:44:51 +0000354 if (ErrMsg)
355 *ErrMsg = "Can't write an archive not opened for writing";
356 return true;
357 }
Reid Spencercf6afc62004-11-14 21:56:59 +0000358
359 // Create a temporary file to store the archive in
360 sys::Path TmpArchive = archPath;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000361 if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
362 return true;
Reid Spencercf6afc62004-11-14 21:56:59 +0000363
364 // Make sure the temporary gets removed if we crash
365 sys::RemoveFileOnSignal(TmpArchive);
366
Reid Spencer3039b992006-07-07 19:09:14 +0000367 // Create archive file for output.
368 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
369 std::ios::binary;
370 std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000371
Reid Spencer3039b992006-07-07 19:09:14 +0000372 // Check for errors opening or creating archive file.
Chris Lattner0c332312006-07-28 22:29:50 +0000373 if (!ArchiveFile.is_open() || ArchiveFile.bad()) {
Dan Gohmand27047f2010-05-27 20:51:54 +0000374 TmpArchive.eraseFromDisk();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000375 if (ErrMsg)
Chris Lattner74382b72009-08-23 22:45:37 +0000376 *ErrMsg = "Error opening archive file: " + archPath.str();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000377 return true;
Reid Spencercf6afc62004-11-14 21:56:59 +0000378 }
Reid Spencer3039b992006-07-07 19:09:14 +0000379
380 // If we're creating a symbol table, reset it now
381 if (CreateSymbolTable) {
382 symTabSize = 0;
383 symTab.clear();
384 }
385
386 // Write magic string to archive.
387 ArchiveFile << ARFILE_MAGIC;
388
389 // Loop over all member files, and write them out. Note that this also
390 // builds the symbol table, symTab.
Chris Lattner0c332312006-07-28 22:29:50 +0000391 for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000392 if (writeMember(*I, ArchiveFile, CreateSymbolTable,
393 TruncateNames, Compress, ErrMsg)) {
Dan Gohmand27047f2010-05-27 20:51:54 +0000394 TmpArchive.eraseFromDisk();
Reid Spencer3039b992006-07-07 19:09:14 +0000395 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.
Chris Lattner7f6b4472008-04-01 04:26:46 +0000410 sys::Path FinalFilePath = archPath;
Reid Spencer3039b992006-07-07 19:09:14 +0000411
412 // Map in the archive we just wrote.
Chris Lattner7f6b4472008-04-01 04:26:46 +0000413 {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000414 OwningPtr<MemoryBuffer> arch;
415 if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000416 if (ErrMsg)
417 *ErrMsg = ec.message();
418 return true;
419 }
Chris Lattner7f6b4472008-04-01 04:26:46 +0000420 const char* base = arch->getBufferStart();
Reid Spencer3039b992006-07-07 19:09:14 +0000421
422 // Open another temporary file in order to avoid invalidating the
423 // mmapped data
Reid Spencer0ff2d312006-08-24 23:45:08 +0000424 if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
425 return true;
Reid Spencer3039b992006-07-07 19:09:14 +0000426 sys::RemoveFileOnSignal(FinalFilePath);
427
428 std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
Chris Lattner0c332312006-07-28 22:29:50 +0000429 if (!FinalFile.is_open() || FinalFile.bad()) {
Dan Gohmand27047f2010-05-27 20:51:54 +0000430 TmpArchive.eraseFromDisk();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000431 if (ErrMsg)
Chris Lattner74382b72009-08-23 22:45:37 +0000432 *ErrMsg = "Error opening archive file: " + FinalFilePath.str();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000433 return true;
Reid Spencer3039b992006-07-07 19:09:14 +0000434 }
435
436 // Write the file magic number
437 FinalFile << ARFILE_MAGIC;
438
439 // If there is a foreign symbol table, put it into the file now. Most
440 // ar(1) implementations require the symbol table to be first but llvm-ar
441 // can deal with it being after a foreign symbol table. This ensures
442 // compatibility with other ar(1) implementations as well as allowing the
443 // archive to store both native .o and LLVM .bc files, both indexed.
444 if (foreignST) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000445 if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000446 FinalFile.close();
Dan Gohmand27047f2010-05-27 20:51:54 +0000447 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,
Chris Lattner7f6b4472008-04-01 04:26:46 +0000458 arch->getBufferSize()-sizeof(ARFILE_MAGIC)+1);
Reid Spencer3039b992006-07-07 19:09:14 +0000459
460 // Close up shop
461 FinalFile.close();
Chris Lattner7f6b4472008-04-01 04:26:46 +0000462 } // free arch.
Reid Spencer3039b992006-07-07 19:09:14 +0000463
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
Owen Andersona5464f32008-05-24 05:42:29 +0000477 // Set correct read and write permissions after temporary file is moved
478 // to final destination path.
479 if (archPath.makeReadableOnDisk(ErrMsg))
480 return true;
481 if (archPath.makeWriteableOnDisk(ErrMsg))
482 return true;
483
Reid Spencer0ff2d312006-08-24 23:45:08 +0000484 return false;
Reid Spencercf6afc62004-11-14 21:56:59 +0000485}