1 //===- ArchiveWriter.cpp - ar File Format implementation --------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the writeArchive function. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Object/ArchiveWriter.h" 14 #include "llvm/ADT/ArrayRef.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/BinaryFormat/Magic.h" 17 #include "llvm/IR/LLVMContext.h" 18 #include "llvm/Object/Archive.h" 19 #include "llvm/Object/ObjectFile.h" 20 #include "llvm/Object/SymbolicFile.h" 21 #include "llvm/Support/EndianStream.h" 22 #include "llvm/Support/Errc.h" 23 #include "llvm/Support/ErrorHandling.h" 24 #include "llvm/Support/Format.h" 25 #include "llvm/Support/Path.h" 26 #include "llvm/Support/ToolOutputFile.h" 27 #include "llvm/Support/raw_ostream.h" 28 29 #include <map> 30 31 #if !defined(_MSC_VER) && !defined(__MINGW32__) 32 #include <unistd.h> 33 #else 34 #include <io.h> 35 #endif 36 37 using namespace llvm; 38 39 NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef) 40 : Buf(MemoryBuffer::getMemBuffer(BufRef, false)), 41 MemberName(BufRef.getBufferIdentifier()) {} 42 43 Expected<NewArchiveMember> 44 NewArchiveMember::getOldMember(const object::Archive::Child &OldMember, 45 bool Deterministic) { 46 Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef(); 47 if (!BufOrErr) 48 return BufOrErr.takeError(); 49 50 NewArchiveMember M; 51 M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false); 52 M.MemberName = M.Buf->getBufferIdentifier(); 53 if (!Deterministic) { 54 auto ModTimeOrErr = OldMember.getLastModified(); 55 if (!ModTimeOrErr) 56 return ModTimeOrErr.takeError(); 57 M.ModTime = ModTimeOrErr.get(); 58 Expected<unsigned> UIDOrErr = OldMember.getUID(); 59 if (!UIDOrErr) 60 return UIDOrErr.takeError(); 61 M.UID = UIDOrErr.get(); 62 Expected<unsigned> GIDOrErr = OldMember.getGID(); 63 if (!GIDOrErr) 64 return GIDOrErr.takeError(); 65 M.GID = GIDOrErr.get(); 66 Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode(); 67 if (!AccessModeOrErr) 68 return AccessModeOrErr.takeError(); 69 M.Perms = AccessModeOrErr.get(); 70 } 71 return std::move(M); 72 } 73 74 Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName, 75 bool Deterministic) { 76 sys::fs::file_status Status; 77 int FD; 78 if (auto EC = sys::fs::openFileForRead(FileName, FD)) 79 return errorCodeToError(EC); 80 assert(FD != -1); 81 82 if (auto EC = sys::fs::status(FD, Status)) 83 return errorCodeToError(EC); 84 85 // Opening a directory doesn't make sense. Let it fail. 86 // Linux cannot open directories with open(2), although 87 // cygwin and *bsd can. 88 if (Status.type() == sys::fs::file_type::directory_file) 89 return errorCodeToError(make_error_code(errc::is_a_directory)); 90 91 ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr = 92 MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false); 93 if (!MemberBufferOrErr) 94 return errorCodeToError(MemberBufferOrErr.getError()); 95 96 if (close(FD) != 0) 97 return errorCodeToError(std::error_code(errno, std::generic_category())); 98 99 NewArchiveMember M; 100 M.Buf = std::move(*MemberBufferOrErr); 101 M.MemberName = M.Buf->getBufferIdentifier(); 102 if (!Deterministic) { 103 M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>( 104 Status.getLastModificationTime()); 105 M.UID = Status.getUser(); 106 M.GID = Status.getGroup(); 107 M.Perms = Status.permissions(); 108 } 109 return std::move(M); 110 } 111 112 template <typename T> 113 static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) { 114 uint64_t OldPos = OS.tell(); 115 OS << Data; 116 unsigned SizeSoFar = OS.tell() - OldPos; 117 assert(SizeSoFar <= Size && "Data doesn't fit in Size"); 118 OS.indent(Size - SizeSoFar); 119 } 120 121 static bool isDarwin(object::Archive::Kind Kind) { 122 return Kind == object::Archive::K_DARWIN || 123 Kind == object::Archive::K_DARWIN64; 124 } 125 126 static bool isBSDLike(object::Archive::Kind Kind) { 127 switch (Kind) { 128 case object::Archive::K_GNU: 129 case object::Archive::K_GNU64: 130 return false; 131 case object::Archive::K_BSD: 132 case object::Archive::K_DARWIN: 133 case object::Archive::K_DARWIN64: 134 return true; 135 case object::Archive::K_COFF: 136 break; 137 } 138 llvm_unreachable("not supported for writting"); 139 } 140 141 template <class T> 142 static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val) { 143 support::endian::write(Out, Val, 144 isBSDLike(Kind) ? support::little : support::big); 145 } 146 147 static void printRestOfMemberHeader( 148 raw_ostream &Out, const sys::TimePoint<std::chrono::seconds> &ModTime, 149 unsigned UID, unsigned GID, unsigned Perms, unsigned Size) { 150 printWithSpacePadding(Out, sys::toTimeT(ModTime), 12); 151 152 // The format has only 6 chars for uid and gid. Truncate if the provided 153 // values don't fit. 154 printWithSpacePadding(Out, UID % 1000000, 6); 155 printWithSpacePadding(Out, GID % 1000000, 6); 156 157 printWithSpacePadding(Out, format("%o", Perms), 8); 158 printWithSpacePadding(Out, Size, 10); 159 Out << "`\n"; 160 } 161 162 static void 163 printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name, 164 const sys::TimePoint<std::chrono::seconds> &ModTime, 165 unsigned UID, unsigned GID, unsigned Perms, 166 unsigned Size) { 167 printWithSpacePadding(Out, Twine(Name) + "/", 16); 168 printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size); 169 } 170 171 static void 172 printBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name, 173 const sys::TimePoint<std::chrono::seconds> &ModTime, 174 unsigned UID, unsigned GID, unsigned Perms, 175 unsigned Size) { 176 uint64_t PosAfterHeader = Pos + 60 + Name.size(); 177 // Pad so that even 64 bit object files are aligned. 178 unsigned Pad = OffsetToAlignment(PosAfterHeader, 8); 179 unsigned NameWithPadding = Name.size() + Pad; 180 printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16); 181 printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, 182 NameWithPadding + Size); 183 Out << Name; 184 while (Pad--) 185 Out.write(uint8_t(0)); 186 } 187 188 static bool useStringTable(bool Thin, StringRef Name) { 189 return Thin || Name.size() >= 16 || Name.contains('/'); 190 } 191 192 static bool is64BitKind(object::Archive::Kind Kind) { 193 switch (Kind) { 194 case object::Archive::K_GNU: 195 case object::Archive::K_BSD: 196 case object::Archive::K_DARWIN: 197 case object::Archive::K_COFF: 198 return false; 199 case object::Archive::K_DARWIN64: 200 case object::Archive::K_GNU64: 201 return true; 202 } 203 llvm_unreachable("not supported for writting"); 204 } 205 206 static void 207 printMemberHeader(raw_ostream &Out, uint64_t Pos, raw_ostream &StringTable, 208 StringMap<uint64_t> &MemberNames, object::Archive::Kind Kind, 209 bool Thin, const NewArchiveMember &M, 210 sys::TimePoint<std::chrono::seconds> ModTime, unsigned Size) { 211 if (isBSDLike(Kind)) 212 return printBSDMemberHeader(Out, Pos, M.MemberName, ModTime, M.UID, M.GID, 213 M.Perms, Size); 214 if (!useStringTable(Thin, M.MemberName)) 215 return printGNUSmallMemberHeader(Out, M.MemberName, ModTime, M.UID, M.GID, 216 M.Perms, Size); 217 Out << '/'; 218 uint64_t NamePos; 219 if (Thin) { 220 NamePos = StringTable.tell(); 221 StringTable << M.MemberName << "/\n"; 222 } else { 223 auto Insertion = MemberNames.insert({M.MemberName, uint64_t(0)}); 224 if (Insertion.second) { 225 Insertion.first->second = StringTable.tell(); 226 StringTable << M.MemberName << "/\n"; 227 } 228 NamePos = Insertion.first->second; 229 } 230 printWithSpacePadding(Out, NamePos, 15); 231 printRestOfMemberHeader(Out, ModTime, M.UID, M.GID, M.Perms, Size); 232 } 233 234 namespace { 235 struct MemberData { 236 std::vector<unsigned> Symbols; 237 std::string Header; 238 StringRef Data; 239 StringRef Padding; 240 }; 241 } // namespace 242 243 static MemberData computeStringTable(StringRef Names) { 244 unsigned Size = Names.size(); 245 unsigned Pad = OffsetToAlignment(Size, 2); 246 std::string Header; 247 raw_string_ostream Out(Header); 248 printWithSpacePadding(Out, "//", 48); 249 printWithSpacePadding(Out, Size + Pad, 10); 250 Out << "`\n"; 251 Out.flush(); 252 return {{}, std::move(Header), Names, Pad ? "\n" : ""}; 253 } 254 255 static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) { 256 using namespace std::chrono; 257 258 if (!Deterministic) 259 return time_point_cast<seconds>(system_clock::now()); 260 return sys::TimePoint<seconds>(); 261 } 262 263 static bool isArchiveSymbol(const object::BasicSymbolRef &S) { 264 uint32_t Symflags = S.getFlags(); 265 if (Symflags & object::SymbolRef::SF_FormatSpecific) 266 return false; 267 if (!(Symflags & object::SymbolRef::SF_Global)) 268 return false; 269 if (Symflags & object::SymbolRef::SF_Undefined) 270 return false; 271 return true; 272 } 273 274 static void printNBits(raw_ostream &Out, object::Archive::Kind Kind, 275 uint64_t Val) { 276 if (is64BitKind(Kind)) 277 print<uint64_t>(Out, Kind, Val); 278 else 279 print<uint32_t>(Out, Kind, Val); 280 } 281 282 static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind, 283 bool Deterministic, ArrayRef<MemberData> Members, 284 StringRef StringTable) { 285 // We don't write a symbol table on an archive with no members -- except on 286 // Darwin, where the linker will abort unless the archive has a symbol table. 287 if (StringTable.empty() && !isDarwin(Kind)) 288 return; 289 290 unsigned NumSyms = 0; 291 for (const MemberData &M : Members) 292 NumSyms += M.Symbols.size(); 293 294 unsigned Size = 0; 295 unsigned OffsetSize = is64BitKind(Kind) ? sizeof(uint64_t) : sizeof(uint32_t); 296 297 Size += OffsetSize; // Number of entries 298 if (isBSDLike(Kind)) 299 Size += NumSyms * OffsetSize * 2; // Table 300 else 301 Size += NumSyms * OffsetSize; // Table 302 if (isBSDLike(Kind)) 303 Size += OffsetSize; // byte count 304 Size += StringTable.size(); 305 // ld64 expects the members to be 8-byte aligned for 64-bit content and at 306 // least 4-byte aligned for 32-bit content. Opt for the larger encoding 307 // uniformly. 308 // We do this for all bsd formats because it simplifies aligning members. 309 unsigned Alignment = isBSDLike(Kind) ? 8 : 2; 310 unsigned Pad = OffsetToAlignment(Size, Alignment); 311 Size += Pad; 312 313 if (isBSDLike(Kind)) { 314 const char *Name = is64BitKind(Kind) ? "__.SYMDEF_64" : "__.SYMDEF"; 315 printBSDMemberHeader(Out, Out.tell(), Name, now(Deterministic), 0, 0, 0, 316 Size); 317 } else { 318 const char *Name = is64BitKind(Kind) ? "/SYM64" : ""; 319 printGNUSmallMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size); 320 } 321 322 uint64_t Pos = Out.tell() + Size; 323 324 if (isBSDLike(Kind)) 325 printNBits(Out, Kind, NumSyms * 2 * OffsetSize); 326 else 327 printNBits(Out, Kind, NumSyms); 328 329 for (const MemberData &M : Members) { 330 for (unsigned StringOffset : M.Symbols) { 331 if (isBSDLike(Kind)) 332 printNBits(Out, Kind, StringOffset); 333 printNBits(Out, Kind, Pos); // member offset 334 } 335 Pos += M.Header.size() + M.Data.size() + M.Padding.size(); 336 } 337 338 if (isBSDLike(Kind)) 339 // byte count of the string table 340 printNBits(Out, Kind, StringTable.size()); 341 Out << StringTable; 342 343 while (Pad--) 344 Out.write(uint8_t(0)); 345 } 346 347 static Expected<std::vector<unsigned>> 348 getSymbols(MemoryBufferRef Buf, raw_ostream &SymNames, bool &HasObject) { 349 std::vector<unsigned> Ret; 350 351 // In the scenario when LLVMContext is populated SymbolicFile will contain a 352 // reference to it, thus SymbolicFile should be destroyed first. 353 LLVMContext Context; 354 std::unique_ptr<object::SymbolicFile> Obj; 355 if (identify_magic(Buf.getBuffer()) == file_magic::bitcode) { 356 auto ObjOrErr = object::SymbolicFile::createSymbolicFile( 357 Buf, file_magic::bitcode, &Context); 358 if (!ObjOrErr) { 359 // FIXME: check only for "not an object file" errors. 360 consumeError(ObjOrErr.takeError()); 361 return Ret; 362 } 363 Obj = std::move(*ObjOrErr); 364 } else { 365 auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf); 366 if (!ObjOrErr) { 367 // FIXME: check only for "not an object file" errors. 368 consumeError(ObjOrErr.takeError()); 369 return Ret; 370 } 371 Obj = std::move(*ObjOrErr); 372 } 373 374 HasObject = true; 375 for (const object::BasicSymbolRef &S : Obj->symbols()) { 376 if (!isArchiveSymbol(S)) 377 continue; 378 Ret.push_back(SymNames.tell()); 379 if (Error E = S.printName(SymNames)) 380 return std::move(E); 381 SymNames << '\0'; 382 } 383 return Ret; 384 } 385 386 static Expected<std::vector<MemberData>> 387 computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames, 388 object::Archive::Kind Kind, bool Thin, bool Deterministic, 389 ArrayRef<NewArchiveMember> NewMembers) { 390 static char PaddingData[8] = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'}; 391 392 // This ignores the symbol table, but we only need the value mod 8 and the 393 // symbol table is aligned to be a multiple of 8 bytes 394 uint64_t Pos = 0; 395 396 std::vector<MemberData> Ret; 397 bool HasObject = false; 398 399 // Deduplicate long member names in the string table and reuse earlier name 400 // offsets. This especially saves space for COFF Import libraries where all 401 // members have the same name. 402 StringMap<uint64_t> MemberNames; 403 404 // UniqueTimestamps is a special case to improve debugging on Darwin: 405 // 406 // The Darwin linker does not link debug info into the final 407 // binary. Instead, it emits entries of type N_OSO in in the output 408 // binary's symbol table, containing references to the linked-in 409 // object files. Using that reference, the debugger can read the 410 // debug data directly from the object files. Alternatively, an 411 // invocation of 'dsymutil' will link the debug data from the object 412 // files into a dSYM bundle, which can be loaded by the debugger, 413 // instead of the object files. 414 // 415 // For an object file, the N_OSO entries contain the absolute path 416 // path to the file, and the file's timestamp. For an object 417 // included in an archive, the path is formatted like 418 // "/absolute/path/to/archive.a(member.o)", and the timestamp is the 419 // archive member's timestamp, rather than the archive's timestamp. 420 // 421 // However, this doesn't always uniquely identify an object within 422 // an archive -- an archive file can have multiple entries with the 423 // same filename. (This will happen commonly if the original object 424 // files started in different directories.) The only way they get 425 // distinguished, then, is via the timestamp. But this process is 426 // unable to find the correct object file in the archive when there 427 // are two files of the same name and timestamp. 428 // 429 // Additionally, timestamp==0 is treated specially, and causes the 430 // timestamp to be ignored as a match criteria. 431 // 432 // That will "usually" work out okay when creating an archive not in 433 // deterministic timestamp mode, because the objects will probably 434 // have been created at different timestamps. 435 // 436 // To ameliorate this problem, in deterministic archive mode (which 437 // is the default), on Darwin we will emit a unique non-zero 438 // timestamp for each entry with a duplicated name. This is still 439 // deterministic: the only thing affecting that timestamp is the 440 // order of the files in the resultant archive. 441 // 442 // See also the functions that handle the lookup: 443 // in lldb: ObjectContainerBSDArchive::Archive::FindObject() 444 // in llvm/tools/dsymutil: BinaryHolder::GetArchiveMemberBuffers(). 445 bool UniqueTimestamps = Deterministic && isDarwin(Kind); 446 std::map<StringRef, unsigned> FilenameCount; 447 if (UniqueTimestamps) { 448 for (const NewArchiveMember &M : NewMembers) 449 FilenameCount[M.MemberName]++; 450 for (auto &Entry : FilenameCount) 451 Entry.second = Entry.second > 1 ? 1 : 0; 452 } 453 454 for (const NewArchiveMember &M : NewMembers) { 455 std::string Header; 456 raw_string_ostream Out(Header); 457 458 MemoryBufferRef Buf = M.Buf->getMemBufferRef(); 459 StringRef Data = Thin ? "" : Buf.getBuffer(); 460 461 // ld64 expects the members to be 8-byte aligned for 64-bit content and at 462 // least 4-byte aligned for 32-bit content. Opt for the larger encoding 463 // uniformly. This matches the behaviour with cctools and ensures that ld64 464 // is happy with archives that we generate. 465 unsigned MemberPadding = 466 isDarwin(Kind) ? OffsetToAlignment(Data.size(), 8) : 0; 467 unsigned TailPadding = OffsetToAlignment(Data.size() + MemberPadding, 2); 468 StringRef Padding = StringRef(PaddingData, MemberPadding + TailPadding); 469 470 sys::TimePoint<std::chrono::seconds> ModTime; 471 if (UniqueTimestamps) 472 // Increment timestamp for each file of a given name. 473 ModTime = sys::toTimePoint(FilenameCount[M.MemberName]++); 474 else 475 ModTime = M.ModTime; 476 printMemberHeader(Out, Pos, StringTable, MemberNames, Kind, Thin, M, 477 ModTime, Buf.getBufferSize() + MemberPadding); 478 Out.flush(); 479 480 Expected<std::vector<unsigned>> Symbols = 481 getSymbols(Buf, SymNames, HasObject); 482 if (auto E = Symbols.takeError()) 483 return std::move(E); 484 485 Pos += Header.size() + Data.size() + Padding.size(); 486 Ret.push_back({std::move(*Symbols), std::move(Header), Data, Padding}); 487 } 488 // If there are no symbols, emit an empty symbol table, to satisfy Solaris 489 // tools, older versions of which expect a symbol table in a non-empty 490 // archive, regardless of whether there are any symbols in it. 491 if (HasObject && SymNames.tell() == 0) 492 SymNames << '\0' << '\0' << '\0'; 493 return Ret; 494 } 495 496 namespace llvm { 497 // Compute the relative path from From to To. 498 std::string computeArchiveRelativePath(StringRef From, StringRef To) { 499 if (sys::path::is_absolute(From) || sys::path::is_absolute(To)) 500 return To; 501 502 StringRef DirFrom = sys::path::parent_path(From); 503 auto FromI = sys::path::begin(DirFrom); 504 auto ToI = sys::path::begin(To); 505 while (*FromI == *ToI) { 506 ++FromI; 507 ++ToI; 508 } 509 510 SmallString<128> Relative; 511 for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI) 512 sys::path::append(Relative, ".."); 513 514 for (auto ToE = sys::path::end(To); ToI != ToE; ++ToI) 515 sys::path::append(Relative, *ToI); 516 517 // Replace backslashes with slashes so that the path is portable between *nix 518 // and Windows. 519 return sys::path::convert_to_slash(Relative); 520 } 521 522 Error writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers, 523 bool WriteSymtab, object::Archive::Kind Kind, 524 bool Deterministic, bool Thin, 525 std::unique_ptr<MemoryBuffer> OldArchiveBuf) { 526 assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode"); 527 528 SmallString<0> SymNamesBuf; 529 raw_svector_ostream SymNames(SymNamesBuf); 530 SmallString<0> StringTableBuf; 531 raw_svector_ostream StringTable(StringTableBuf); 532 533 Expected<std::vector<MemberData>> DataOrErr = computeMemberData( 534 StringTable, SymNames, Kind, Thin, Deterministic, NewMembers); 535 if (Error E = DataOrErr.takeError()) 536 return E; 537 std::vector<MemberData> &Data = *DataOrErr; 538 539 if (!StringTableBuf.empty()) 540 Data.insert(Data.begin(), computeStringTable(StringTableBuf)); 541 542 // We would like to detect if we need to switch to a 64-bit symbol table. 543 if (WriteSymtab) { 544 uint64_t MaxOffset = 0; 545 uint64_t LastOffset = MaxOffset; 546 for (const auto &M : Data) { 547 // Record the start of the member's offset 548 LastOffset = MaxOffset; 549 // Account for the size of each part associated with the member. 550 MaxOffset += M.Header.size() + M.Data.size() + M.Padding.size(); 551 // We assume 32-bit symbols to see if 32-bit symbols are possible or not. 552 MaxOffset += M.Symbols.size() * 4; 553 } 554 555 // The SYM64 format is used when an archive's member offsets are larger than 556 // 32-bits can hold. The need for this shift in format is detected by 557 // writeArchive. To test this we need to generate a file with a member that 558 // has an offset larger than 32-bits but this demands a very slow test. To 559 // speed the test up we use this environment variable to pretend like the 560 // cutoff happens before 32-bits and instead happens at some much smaller 561 // value. 562 const char *Sym64Env = std::getenv("SYM64_THRESHOLD"); 563 int Sym64Threshold = 32; 564 if (Sym64Env) 565 StringRef(Sym64Env).getAsInteger(10, Sym64Threshold); 566 567 // If LastOffset isn't going to fit in a 32-bit varible we need to switch 568 // to 64-bit. Note that the file can be larger than 4GB as long as the last 569 // member starts before the 4GB offset. 570 if (LastOffset >= (1ULL << Sym64Threshold)) { 571 if (Kind == object::Archive::K_DARWIN) 572 Kind = object::Archive::K_DARWIN64; 573 else 574 Kind = object::Archive::K_GNU64; 575 } 576 } 577 578 Expected<sys::fs::TempFile> Temp = 579 sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a"); 580 if (!Temp) 581 return Temp.takeError(); 582 583 raw_fd_ostream Out(Temp->FD, false); 584 if (Thin) 585 Out << "!<thin>\n"; 586 else 587 Out << "!<arch>\n"; 588 589 if (WriteSymtab) 590 writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf); 591 592 for (const MemberData &M : Data) 593 Out << M.Header << M.Data << M.Padding; 594 595 Out.flush(); 596 597 // At this point, we no longer need whatever backing memory 598 // was used to generate the NewMembers. On Windows, this buffer 599 // could be a mapped view of the file we want to replace (if 600 // we're updating an existing archive, say). In that case, the 601 // rename would still succeed, but it would leave behind a 602 // temporary file (actually the original file renamed) because 603 // a file cannot be deleted while there's a handle open on it, 604 // only renamed. So by freeing this buffer, this ensures that 605 // the last open handle on the destination file, if any, is 606 // closed before we attempt to rename. 607 OldArchiveBuf.reset(); 608 609 return Temp->keep(ArcName); 610 } 611 612 } // namespace llvm 613