10b57cec5SDimitry Andric //===- ArchiveWriter.cpp - ar File Format implementation --------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file defines the writeArchive function. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/Object/ArchiveWriter.h" 140b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 155ffd83dbSDimitry Andric #include "llvm/ADT/StringMap.h" 160b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 170b57cec5SDimitry Andric #include "llvm/BinaryFormat/Magic.h" 180b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 190b57cec5SDimitry Andric #include "llvm/Object/Archive.h" 2006c3fb27SDimitry Andric #include "llvm/Object/COFF.h" 215f757f3fSDimitry Andric #include "llvm/Object/COFFImportFile.h" 228bcb0991SDimitry Andric #include "llvm/Object/Error.h" 2381ad6265SDimitry Andric #include "llvm/Object/IRObjectFile.h" 2481ad6265SDimitry Andric #include "llvm/Object/MachO.h" 250b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h" 260b57cec5SDimitry Andric #include "llvm/Object/SymbolicFile.h" 2781ad6265SDimitry Andric #include "llvm/Object/XCOFFObjectFile.h" 288bcb0991SDimitry Andric #include "llvm/Support/Alignment.h" 290b57cec5SDimitry Andric #include "llvm/Support/EndianStream.h" 300b57cec5SDimitry Andric #include "llvm/Support/Errc.h" 310b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 320b57cec5SDimitry Andric #include "llvm/Support/Format.h" 3381ad6265SDimitry Andric #include "llvm/Support/MathExtras.h" 340b57cec5SDimitry Andric #include "llvm/Support/Path.h" 35e8d8bef9SDimitry Andric #include "llvm/Support/SmallVectorMemoryBuffer.h" 360b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 370b57cec5SDimitry Andric 3806c3fb27SDimitry Andric #include <cerrno> 390b57cec5SDimitry Andric #include <map> 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric #if !defined(_MSC_VER) && !defined(__MINGW32__) 420b57cec5SDimitry Andric #include <unistd.h> 430b57cec5SDimitry Andric #else 440b57cec5SDimitry Andric #include <io.h> 450b57cec5SDimitry Andric #endif 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric using namespace llvm; 4806c3fb27SDimitry Andric using namespace llvm::object; 4906c3fb27SDimitry Andric 5006c3fb27SDimitry Andric struct SymMap { 51*0fca6ea1SDimitry Andric bool UseECMap = false; 5206c3fb27SDimitry Andric std::map<std::string, uint16_t> Map; 5306c3fb27SDimitry Andric std::map<std::string, uint16_t> ECMap; 5406c3fb27SDimitry Andric }; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef) 570b57cec5SDimitry Andric : Buf(MemoryBuffer::getMemBuffer(BufRef, false)), 580b57cec5SDimitry Andric MemberName(BufRef.getBufferIdentifier()) {} 590b57cec5SDimitry Andric 6081ad6265SDimitry Andric object::Archive::Kind NewArchiveMember::detectKindFromObject() const { 6181ad6265SDimitry Andric auto MemBufferRef = this->Buf->getMemBufferRef(); 6281ad6265SDimitry Andric Expected<std::unique_ptr<object::ObjectFile>> OptionalObject = 6381ad6265SDimitry Andric object::ObjectFile::createObjectFile(MemBufferRef); 6481ad6265SDimitry Andric 65*0fca6ea1SDimitry Andric if (OptionalObject) { 66*0fca6ea1SDimitry Andric if (isa<object::MachOObjectFile>(**OptionalObject)) 67*0fca6ea1SDimitry Andric return object::Archive::K_DARWIN; 68*0fca6ea1SDimitry Andric if (isa<object::XCOFFObjectFile>(**OptionalObject)) 69*0fca6ea1SDimitry Andric return object::Archive::K_AIXBIG; 70*0fca6ea1SDimitry Andric if (isa<object::COFFObjectFile>(**OptionalObject) || 71*0fca6ea1SDimitry Andric isa<object::COFFImportFile>(**OptionalObject)) 72*0fca6ea1SDimitry Andric return object::Archive::K_COFF; 73*0fca6ea1SDimitry Andric return object::Archive::K_GNU; 74*0fca6ea1SDimitry Andric } 7581ad6265SDimitry Andric 7681ad6265SDimitry Andric // Squelch the error in case we had a non-object file. 7781ad6265SDimitry Andric consumeError(OptionalObject.takeError()); 7881ad6265SDimitry Andric 7981ad6265SDimitry Andric // If we're adding a bitcode file to the archive, detect the Archive kind 8081ad6265SDimitry Andric // based on the target triple. 8181ad6265SDimitry Andric LLVMContext Context; 8281ad6265SDimitry Andric if (identify_magic(MemBufferRef.getBuffer()) == file_magic::bitcode) { 8381ad6265SDimitry Andric if (auto ObjOrErr = object::SymbolicFile::createSymbolicFile( 8481ad6265SDimitry Andric MemBufferRef, file_magic::bitcode, &Context)) { 8581ad6265SDimitry Andric auto &IRObject = cast<object::IRObjectFile>(**ObjOrErr); 8606c3fb27SDimitry Andric auto TargetTriple = Triple(IRObject.getTargetTriple()); 87*0fca6ea1SDimitry Andric return object::Archive::getDefaultKindForTriple(TargetTriple); 8881ad6265SDimitry Andric } else { 8981ad6265SDimitry Andric // Squelch the error in case this was not a SymbolicFile. 9081ad6265SDimitry Andric consumeError(ObjOrErr.takeError()); 9181ad6265SDimitry Andric } 9281ad6265SDimitry Andric } 9381ad6265SDimitry Andric 94*0fca6ea1SDimitry Andric return object::Archive::getDefaultKind(); 9581ad6265SDimitry Andric } 9681ad6265SDimitry Andric 970b57cec5SDimitry Andric Expected<NewArchiveMember> 980b57cec5SDimitry Andric NewArchiveMember::getOldMember(const object::Archive::Child &OldMember, 990b57cec5SDimitry Andric bool Deterministic) { 1000b57cec5SDimitry Andric Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef(); 1010b57cec5SDimitry Andric if (!BufOrErr) 1020b57cec5SDimitry Andric return BufOrErr.takeError(); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric NewArchiveMember M; 1050b57cec5SDimitry Andric M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false); 1060b57cec5SDimitry Andric M.MemberName = M.Buf->getBufferIdentifier(); 1070b57cec5SDimitry Andric if (!Deterministic) { 1080b57cec5SDimitry Andric auto ModTimeOrErr = OldMember.getLastModified(); 1090b57cec5SDimitry Andric if (!ModTimeOrErr) 1100b57cec5SDimitry Andric return ModTimeOrErr.takeError(); 1110b57cec5SDimitry Andric M.ModTime = ModTimeOrErr.get(); 1120b57cec5SDimitry Andric Expected<unsigned> UIDOrErr = OldMember.getUID(); 1130b57cec5SDimitry Andric if (!UIDOrErr) 1140b57cec5SDimitry Andric return UIDOrErr.takeError(); 1150b57cec5SDimitry Andric M.UID = UIDOrErr.get(); 1160b57cec5SDimitry Andric Expected<unsigned> GIDOrErr = OldMember.getGID(); 1170b57cec5SDimitry Andric if (!GIDOrErr) 1180b57cec5SDimitry Andric return GIDOrErr.takeError(); 1190b57cec5SDimitry Andric M.GID = GIDOrErr.get(); 1200b57cec5SDimitry Andric Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode(); 1210b57cec5SDimitry Andric if (!AccessModeOrErr) 1220b57cec5SDimitry Andric return AccessModeOrErr.takeError(); 1230b57cec5SDimitry Andric M.Perms = AccessModeOrErr.get(); 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric return std::move(M); 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName, 1290b57cec5SDimitry Andric bool Deterministic) { 1300b57cec5SDimitry Andric sys::fs::file_status Status; 1310b57cec5SDimitry Andric auto FDOrErr = sys::fs::openNativeFileForRead(FileName); 1320b57cec5SDimitry Andric if (!FDOrErr) 1330b57cec5SDimitry Andric return FDOrErr.takeError(); 1340b57cec5SDimitry Andric sys::fs::file_t FD = *FDOrErr; 1350b57cec5SDimitry Andric assert(FD != sys::fs::kInvalidFile); 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric if (auto EC = sys::fs::status(FD, Status)) 1380b57cec5SDimitry Andric return errorCodeToError(EC); 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric // Opening a directory doesn't make sense. Let it fail. 1410b57cec5SDimitry Andric // Linux cannot open directories with open(2), although 1420b57cec5SDimitry Andric // cygwin and *bsd can. 1430b57cec5SDimitry Andric if (Status.type() == sys::fs::file_type::directory_file) 1440b57cec5SDimitry Andric return errorCodeToError(make_error_code(errc::is_a_directory)); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr = 1470b57cec5SDimitry Andric MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false); 1480b57cec5SDimitry Andric if (!MemberBufferOrErr) 1490b57cec5SDimitry Andric return errorCodeToError(MemberBufferOrErr.getError()); 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric if (auto EC = sys::fs::closeFile(FD)) 1520b57cec5SDimitry Andric return errorCodeToError(EC); 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric NewArchiveMember M; 1550b57cec5SDimitry Andric M.Buf = std::move(*MemberBufferOrErr); 1560b57cec5SDimitry Andric M.MemberName = M.Buf->getBufferIdentifier(); 1570b57cec5SDimitry Andric if (!Deterministic) { 1580b57cec5SDimitry Andric M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>( 1590b57cec5SDimitry Andric Status.getLastModificationTime()); 1600b57cec5SDimitry Andric M.UID = Status.getUser(); 1610b57cec5SDimitry Andric M.GID = Status.getGroup(); 1620b57cec5SDimitry Andric M.Perms = Status.permissions(); 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric return std::move(M); 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric template <typename T> 1680b57cec5SDimitry Andric static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) { 1690b57cec5SDimitry Andric uint64_t OldPos = OS.tell(); 1700b57cec5SDimitry Andric OS << Data; 1710b57cec5SDimitry Andric unsigned SizeSoFar = OS.tell() - OldPos; 1720b57cec5SDimitry Andric assert(SizeSoFar <= Size && "Data doesn't fit in Size"); 1730b57cec5SDimitry Andric OS.indent(Size - SizeSoFar); 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric static bool isDarwin(object::Archive::Kind Kind) { 1770b57cec5SDimitry Andric return Kind == object::Archive::K_DARWIN || 1780b57cec5SDimitry Andric Kind == object::Archive::K_DARWIN64; 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric 18181ad6265SDimitry Andric static bool isAIXBigArchive(object::Archive::Kind Kind) { 18281ad6265SDimitry Andric return Kind == object::Archive::K_AIXBIG; 18381ad6265SDimitry Andric } 18481ad6265SDimitry Andric 18506c3fb27SDimitry Andric static bool isCOFFArchive(object::Archive::Kind Kind) { 18606c3fb27SDimitry Andric return Kind == object::Archive::K_COFF; 18706c3fb27SDimitry Andric } 18806c3fb27SDimitry Andric 1890b57cec5SDimitry Andric static bool isBSDLike(object::Archive::Kind Kind) { 1900b57cec5SDimitry Andric switch (Kind) { 1910b57cec5SDimitry Andric case object::Archive::K_GNU: 1920b57cec5SDimitry Andric case object::Archive::K_GNU64: 19381ad6265SDimitry Andric case object::Archive::K_AIXBIG: 19406c3fb27SDimitry Andric case object::Archive::K_COFF: 1950b57cec5SDimitry Andric return false; 1960b57cec5SDimitry Andric case object::Archive::K_BSD: 1970b57cec5SDimitry Andric case object::Archive::K_DARWIN: 1980b57cec5SDimitry Andric case object::Archive::K_DARWIN64: 1990b57cec5SDimitry Andric return true; 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric llvm_unreachable("not supported for writting"); 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric template <class T> 2050b57cec5SDimitry Andric static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val) { 2060b57cec5SDimitry Andric support::endian::write(Out, Val, 2075f757f3fSDimitry Andric isBSDLike(Kind) ? llvm::endianness::little 2085f757f3fSDimitry Andric : llvm::endianness::big); 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 21106c3fb27SDimitry Andric template <class T> static void printLE(raw_ostream &Out, T Val) { 2125f757f3fSDimitry Andric support::endian::write(Out, Val, llvm::endianness::little); 21306c3fb27SDimitry Andric } 21406c3fb27SDimitry Andric 2150b57cec5SDimitry Andric static void printRestOfMemberHeader( 2160b57cec5SDimitry Andric raw_ostream &Out, const sys::TimePoint<std::chrono::seconds> &ModTime, 2178bcb0991SDimitry Andric unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) { 2180b57cec5SDimitry Andric printWithSpacePadding(Out, sys::toTimeT(ModTime), 12); 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric // The format has only 6 chars for uid and gid. Truncate if the provided 2210b57cec5SDimitry Andric // values don't fit. 2220b57cec5SDimitry Andric printWithSpacePadding(Out, UID % 1000000, 6); 2230b57cec5SDimitry Andric printWithSpacePadding(Out, GID % 1000000, 6); 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric printWithSpacePadding(Out, format("%o", Perms), 8); 2260b57cec5SDimitry Andric printWithSpacePadding(Out, Size, 10); 2270b57cec5SDimitry Andric Out << "`\n"; 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric static void 2310b57cec5SDimitry Andric printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name, 2320b57cec5SDimitry Andric const sys::TimePoint<std::chrono::seconds> &ModTime, 2330b57cec5SDimitry Andric unsigned UID, unsigned GID, unsigned Perms, 2348bcb0991SDimitry Andric uint64_t Size) { 2350b57cec5SDimitry Andric printWithSpacePadding(Out, Twine(Name) + "/", 16); 2360b57cec5SDimitry Andric printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size); 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric static void 2400b57cec5SDimitry Andric printBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name, 2410b57cec5SDimitry Andric const sys::TimePoint<std::chrono::seconds> &ModTime, 2428bcb0991SDimitry Andric unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) { 2430b57cec5SDimitry Andric uint64_t PosAfterHeader = Pos + 60 + Name.size(); 2440b57cec5SDimitry Andric // Pad so that even 64 bit object files are aligned. 2458bcb0991SDimitry Andric unsigned Pad = offsetToAlignment(PosAfterHeader, Align(8)); 2460b57cec5SDimitry Andric unsigned NameWithPadding = Name.size() + Pad; 2470b57cec5SDimitry Andric printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16); 2480b57cec5SDimitry Andric printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, 2490b57cec5SDimitry Andric NameWithPadding + Size); 2500b57cec5SDimitry Andric Out << Name; 2510b57cec5SDimitry Andric while (Pad--) 2520b57cec5SDimitry Andric Out.write(uint8_t(0)); 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 25581ad6265SDimitry Andric static void 25681ad6265SDimitry Andric printBigArchiveMemberHeader(raw_ostream &Out, StringRef Name, 25781ad6265SDimitry Andric const sys::TimePoint<std::chrono::seconds> &ModTime, 25881ad6265SDimitry Andric unsigned UID, unsigned GID, unsigned Perms, 25906c3fb27SDimitry Andric uint64_t Size, uint64_t PrevOffset, 26006c3fb27SDimitry Andric uint64_t NextOffset) { 26181ad6265SDimitry Andric unsigned NameLen = Name.size(); 26281ad6265SDimitry Andric 26381ad6265SDimitry Andric printWithSpacePadding(Out, Size, 20); // File member size 26481ad6265SDimitry Andric printWithSpacePadding(Out, NextOffset, 20); // Next member header offset 26581ad6265SDimitry Andric printWithSpacePadding(Out, PrevOffset, 20); // Previous member header offset 26681ad6265SDimitry Andric printWithSpacePadding(Out, sys::toTimeT(ModTime), 12); // File member date 26781ad6265SDimitry Andric // The big archive format has 12 chars for uid and gid. 26881ad6265SDimitry Andric printWithSpacePadding(Out, UID % 1000000000000, 12); // UID 26981ad6265SDimitry Andric printWithSpacePadding(Out, GID % 1000000000000, 12); // GID 27081ad6265SDimitry Andric printWithSpacePadding(Out, format("%o", Perms), 12); // Permission 27181ad6265SDimitry Andric printWithSpacePadding(Out, NameLen, 4); // Name length 27281ad6265SDimitry Andric if (NameLen) { 27381ad6265SDimitry Andric printWithSpacePadding(Out, Name, NameLen); // Name 27481ad6265SDimitry Andric if (NameLen % 2) 27581ad6265SDimitry Andric Out.write(uint8_t(0)); // Null byte padding 27681ad6265SDimitry Andric } 27781ad6265SDimitry Andric Out << "`\n"; // Terminator 27881ad6265SDimitry Andric } 27981ad6265SDimitry Andric 2800b57cec5SDimitry Andric static bool useStringTable(bool Thin, StringRef Name) { 2810b57cec5SDimitry Andric return Thin || Name.size() >= 16 || Name.contains('/'); 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric static bool is64BitKind(object::Archive::Kind Kind) { 2850b57cec5SDimitry Andric switch (Kind) { 2860b57cec5SDimitry Andric case object::Archive::K_GNU: 2870b57cec5SDimitry Andric case object::Archive::K_BSD: 2880b57cec5SDimitry Andric case object::Archive::K_DARWIN: 2890b57cec5SDimitry Andric case object::Archive::K_COFF: 2900b57cec5SDimitry Andric return false; 29181ad6265SDimitry Andric case object::Archive::K_AIXBIG: 2920b57cec5SDimitry Andric case object::Archive::K_DARWIN64: 2930b57cec5SDimitry Andric case object::Archive::K_GNU64: 2940b57cec5SDimitry Andric return true; 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric llvm_unreachable("not supported for writting"); 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric static void 3000b57cec5SDimitry Andric printMemberHeader(raw_ostream &Out, uint64_t Pos, raw_ostream &StringTable, 3010b57cec5SDimitry Andric StringMap<uint64_t> &MemberNames, object::Archive::Kind Kind, 3020b57cec5SDimitry Andric bool Thin, const NewArchiveMember &M, 3038bcb0991SDimitry Andric sys::TimePoint<std::chrono::seconds> ModTime, uint64_t Size) { 3040b57cec5SDimitry Andric if (isBSDLike(Kind)) 3050b57cec5SDimitry Andric return printBSDMemberHeader(Out, Pos, M.MemberName, ModTime, M.UID, M.GID, 3060b57cec5SDimitry Andric M.Perms, Size); 3070b57cec5SDimitry Andric if (!useStringTable(Thin, M.MemberName)) 3080b57cec5SDimitry Andric return printGNUSmallMemberHeader(Out, M.MemberName, ModTime, M.UID, M.GID, 3090b57cec5SDimitry Andric M.Perms, Size); 3100b57cec5SDimitry Andric Out << '/'; 3110b57cec5SDimitry Andric uint64_t NamePos; 3120b57cec5SDimitry Andric if (Thin) { 3130b57cec5SDimitry Andric NamePos = StringTable.tell(); 3140b57cec5SDimitry Andric StringTable << M.MemberName << "/\n"; 3150b57cec5SDimitry Andric } else { 3160b57cec5SDimitry Andric auto Insertion = MemberNames.insert({M.MemberName, uint64_t(0)}); 3170b57cec5SDimitry Andric if (Insertion.second) { 3180b57cec5SDimitry Andric Insertion.first->second = StringTable.tell(); 31906c3fb27SDimitry Andric StringTable << M.MemberName; 32006c3fb27SDimitry Andric if (isCOFFArchive(Kind)) 32106c3fb27SDimitry Andric StringTable << '\0'; 32206c3fb27SDimitry Andric else 32306c3fb27SDimitry Andric StringTable << "/\n"; 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric NamePos = Insertion.first->second; 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric printWithSpacePadding(Out, NamePos, 15); 3280b57cec5SDimitry Andric printRestOfMemberHeader(Out, ModTime, M.UID, M.GID, M.Perms, Size); 3290b57cec5SDimitry Andric } 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric namespace { 3320b57cec5SDimitry Andric struct MemberData { 3330b57cec5SDimitry Andric std::vector<unsigned> Symbols; 3340b57cec5SDimitry Andric std::string Header; 3350b57cec5SDimitry Andric StringRef Data; 3360b57cec5SDimitry Andric StringRef Padding; 3375f757f3fSDimitry Andric uint64_t PreHeadPadSize = 0; 3385f757f3fSDimitry Andric std::unique_ptr<SymbolicFile> SymFile = nullptr; 3390b57cec5SDimitry Andric }; 3400b57cec5SDimitry Andric } // namespace 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric static MemberData computeStringTable(StringRef Names) { 3430b57cec5SDimitry Andric unsigned Size = Names.size(); 3448bcb0991SDimitry Andric unsigned Pad = offsetToAlignment(Size, Align(2)); 3450b57cec5SDimitry Andric std::string Header; 3460b57cec5SDimitry Andric raw_string_ostream Out(Header); 3470b57cec5SDimitry Andric printWithSpacePadding(Out, "//", 48); 3480b57cec5SDimitry Andric printWithSpacePadding(Out, Size + Pad, 10); 3490b57cec5SDimitry Andric Out << "`\n"; 3500b57cec5SDimitry Andric Out.flush(); 3510b57cec5SDimitry Andric return {{}, std::move(Header), Names, Pad ? "\n" : ""}; 3520b57cec5SDimitry Andric } 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) { 3550b57cec5SDimitry Andric using namespace std::chrono; 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric if (!Deterministic) 3580b57cec5SDimitry Andric return time_point_cast<seconds>(system_clock::now()); 3590b57cec5SDimitry Andric return sys::TimePoint<seconds>(); 3600b57cec5SDimitry Andric } 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric static bool isArchiveSymbol(const object::BasicSymbolRef &S) { 3635ffd83dbSDimitry Andric Expected<uint32_t> SymFlagsOrErr = S.getFlags(); 3645ffd83dbSDimitry Andric if (!SymFlagsOrErr) 3655ffd83dbSDimitry Andric // TODO: Actually report errors helpfully. 3665ffd83dbSDimitry Andric report_fatal_error(SymFlagsOrErr.takeError()); 3675ffd83dbSDimitry Andric if (*SymFlagsOrErr & object::SymbolRef::SF_FormatSpecific) 3680b57cec5SDimitry Andric return false; 3695ffd83dbSDimitry Andric if (!(*SymFlagsOrErr & object::SymbolRef::SF_Global)) 3700b57cec5SDimitry Andric return false; 3715ffd83dbSDimitry Andric if (*SymFlagsOrErr & object::SymbolRef::SF_Undefined) 3720b57cec5SDimitry Andric return false; 3730b57cec5SDimitry Andric return true; 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric static void printNBits(raw_ostream &Out, object::Archive::Kind Kind, 3770b57cec5SDimitry Andric uint64_t Val) { 3780b57cec5SDimitry Andric if (is64BitKind(Kind)) 3790b57cec5SDimitry Andric print<uint64_t>(Out, Kind, Val); 3800b57cec5SDimitry Andric else 3810b57cec5SDimitry Andric print<uint32_t>(Out, Kind, Val); 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 384e8d8bef9SDimitry Andric static uint64_t computeSymbolTableSize(object::Archive::Kind Kind, 385e8d8bef9SDimitry Andric uint64_t NumSyms, uint64_t OffsetSize, 38606c3fb27SDimitry Andric uint64_t StringTableSize, 387e8d8bef9SDimitry Andric uint32_t *Padding = nullptr) { 388e8d8bef9SDimitry Andric assert((OffsetSize == 4 || OffsetSize == 8) && "Unsupported OffsetSize"); 389e8d8bef9SDimitry Andric uint64_t Size = OffsetSize; // Number of entries 390e8d8bef9SDimitry Andric if (isBSDLike(Kind)) 391e8d8bef9SDimitry Andric Size += NumSyms * OffsetSize * 2; // Table 392e8d8bef9SDimitry Andric else 393e8d8bef9SDimitry Andric Size += NumSyms * OffsetSize; // Table 394e8d8bef9SDimitry Andric if (isBSDLike(Kind)) 395e8d8bef9SDimitry Andric Size += OffsetSize; // byte count 39606c3fb27SDimitry Andric Size += StringTableSize; 397e8d8bef9SDimitry Andric // ld64 expects the members to be 8-byte aligned for 64-bit content and at 398e8d8bef9SDimitry Andric // least 4-byte aligned for 32-bit content. Opt for the larger encoding 399e8d8bef9SDimitry Andric // uniformly. 400e8d8bef9SDimitry Andric // We do this for all bsd formats because it simplifies aligning members. 40181ad6265SDimitry Andric // For the big archive format, the symbol table is the last member, so there 40281ad6265SDimitry Andric // is no need to align. 40381ad6265SDimitry Andric uint32_t Pad = isAIXBigArchive(Kind) 40481ad6265SDimitry Andric ? 0 40581ad6265SDimitry Andric : offsetToAlignment(Size, Align(isBSDLike(Kind) ? 8 : 2)); 40606c3fb27SDimitry Andric 40706c3fb27SDimitry Andric Size += Pad; 40806c3fb27SDimitry Andric if (Padding) 40906c3fb27SDimitry Andric *Padding = Pad; 41006c3fb27SDimitry Andric return Size; 41106c3fb27SDimitry Andric } 41206c3fb27SDimitry Andric 41306c3fb27SDimitry Andric static uint64_t computeSymbolMapSize(uint64_t NumObj, SymMap &SymMap, 41406c3fb27SDimitry Andric uint32_t *Padding = nullptr) { 41506c3fb27SDimitry Andric uint64_t Size = sizeof(uint32_t) * 2; // Number of symbols and objects entries 41606c3fb27SDimitry Andric Size += NumObj * sizeof(uint32_t); // Offset table 41706c3fb27SDimitry Andric 41806c3fb27SDimitry Andric for (auto S : SymMap.Map) 41906c3fb27SDimitry Andric Size += sizeof(uint16_t) + S.first.length() + 1; 42006c3fb27SDimitry Andric 42106c3fb27SDimitry Andric uint32_t Pad = offsetToAlignment(Size, Align(2)); 42206c3fb27SDimitry Andric Size += Pad; 42306c3fb27SDimitry Andric if (Padding) 42406c3fb27SDimitry Andric *Padding = Pad; 42506c3fb27SDimitry Andric return Size; 42606c3fb27SDimitry Andric } 42706c3fb27SDimitry Andric 42806c3fb27SDimitry Andric static uint64_t computeECSymbolsSize(SymMap &SymMap, 42906c3fb27SDimitry Andric uint32_t *Padding = nullptr) { 43006c3fb27SDimitry Andric uint64_t Size = sizeof(uint32_t); // Number of symbols 43106c3fb27SDimitry Andric 43206c3fb27SDimitry Andric for (auto S : SymMap.ECMap) 43306c3fb27SDimitry Andric Size += sizeof(uint16_t) + S.first.length() + 1; 43406c3fb27SDimitry Andric 43506c3fb27SDimitry Andric uint32_t Pad = offsetToAlignment(Size, Align(2)); 436e8d8bef9SDimitry Andric Size += Pad; 437e8d8bef9SDimitry Andric if (Padding) 438e8d8bef9SDimitry Andric *Padding = Pad; 439e8d8bef9SDimitry Andric return Size; 440e8d8bef9SDimitry Andric } 441e8d8bef9SDimitry Andric 442e8d8bef9SDimitry Andric static void writeSymbolTableHeader(raw_ostream &Out, object::Archive::Kind Kind, 44381ad6265SDimitry Andric bool Deterministic, uint64_t Size, 44406c3fb27SDimitry Andric uint64_t PrevMemberOffset = 0, 44506c3fb27SDimitry Andric uint64_t NextMemberOffset = 0) { 446e8d8bef9SDimitry Andric if (isBSDLike(Kind)) { 447e8d8bef9SDimitry Andric const char *Name = is64BitKind(Kind) ? "__.SYMDEF_64" : "__.SYMDEF"; 448e8d8bef9SDimitry Andric printBSDMemberHeader(Out, Out.tell(), Name, now(Deterministic), 0, 0, 0, 449e8d8bef9SDimitry Andric Size); 45081ad6265SDimitry Andric } else if (isAIXBigArchive(Kind)) { 45106c3fb27SDimitry Andric printBigArchiveMemberHeader(Out, "", now(Deterministic), 0, 0, 0, Size, 45206c3fb27SDimitry Andric PrevMemberOffset, NextMemberOffset); 453e8d8bef9SDimitry Andric } else { 454e8d8bef9SDimitry Andric const char *Name = is64BitKind(Kind) ? "/SYM64" : ""; 455e8d8bef9SDimitry Andric printGNUSmallMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size); 456e8d8bef9SDimitry Andric } 457e8d8bef9SDimitry Andric } 458e8d8bef9SDimitry Andric 45906c3fb27SDimitry Andric static uint64_t computeHeadersSize(object::Archive::Kind Kind, 46006c3fb27SDimitry Andric uint64_t NumMembers, 46106c3fb27SDimitry Andric uint64_t StringMemberSize, uint64_t NumSyms, 46206c3fb27SDimitry Andric uint64_t SymNamesSize, SymMap *SymMap) { 46306c3fb27SDimitry Andric uint32_t OffsetSize = is64BitKind(Kind) ? 8 : 4; 46406c3fb27SDimitry Andric uint64_t SymtabSize = 46506c3fb27SDimitry Andric computeSymbolTableSize(Kind, NumSyms, OffsetSize, SymNamesSize); 46606c3fb27SDimitry Andric auto computeSymbolTableHeaderSize = [=] { 46706c3fb27SDimitry Andric SmallString<0> TmpBuf; 46806c3fb27SDimitry Andric raw_svector_ostream Tmp(TmpBuf); 46906c3fb27SDimitry Andric writeSymbolTableHeader(Tmp, Kind, true, SymtabSize); 47006c3fb27SDimitry Andric return TmpBuf.size(); 47106c3fb27SDimitry Andric }; 47206c3fb27SDimitry Andric uint32_t HeaderSize = computeSymbolTableHeaderSize(); 47306c3fb27SDimitry Andric uint64_t Size = strlen("!<arch>\n") + HeaderSize + SymtabSize; 47406c3fb27SDimitry Andric 47506c3fb27SDimitry Andric if (SymMap) { 47606c3fb27SDimitry Andric Size += HeaderSize + computeSymbolMapSize(NumMembers, *SymMap); 47706c3fb27SDimitry Andric if (SymMap->ECMap.size()) 47806c3fb27SDimitry Andric Size += HeaderSize + computeECSymbolsSize(*SymMap); 47906c3fb27SDimitry Andric } 48006c3fb27SDimitry Andric 48106c3fb27SDimitry Andric return Size + StringMemberSize; 48206c3fb27SDimitry Andric } 48306c3fb27SDimitry Andric 48406c3fb27SDimitry Andric static Expected<std::unique_ptr<SymbolicFile>> 485*0fca6ea1SDimitry Andric getSymbolicFile(MemoryBufferRef Buf, LLVMContext &Context, 486*0fca6ea1SDimitry Andric object::Archive::Kind Kind, function_ref<void(Error)> Warn) { 48706c3fb27SDimitry Andric const file_magic Type = identify_magic(Buf.getBuffer()); 48806c3fb27SDimitry Andric // Don't attempt to read non-symbolic file types. 48906c3fb27SDimitry Andric if (!object::SymbolicFile::isSymbolicFile(Type, &Context)) 49006c3fb27SDimitry Andric return nullptr; 49106c3fb27SDimitry Andric if (Type == file_magic::bitcode) { 49206c3fb27SDimitry Andric auto ObjOrErr = object::SymbolicFile::createSymbolicFile( 49306c3fb27SDimitry Andric Buf, file_magic::bitcode, &Context); 494*0fca6ea1SDimitry Andric // An error reading a bitcode file most likely indicates that the file 495*0fca6ea1SDimitry Andric // was created by a compiler from the future. Normally we don't try to 496*0fca6ea1SDimitry Andric // implement forwards compatibility for bitcode files, but when creating an 497*0fca6ea1SDimitry Andric // archive we can implement best-effort forwards compatibility by treating 498*0fca6ea1SDimitry Andric // the file as a blob and not creating symbol index entries for it. lld and 499*0fca6ea1SDimitry Andric // mold ignore the archive symbol index, so provided that you use one of 500*0fca6ea1SDimitry Andric // these linkers, LTO will work as long as lld or the gold plugin is newer 501*0fca6ea1SDimitry Andric // than the compiler. We only ignore errors if the archive format is one 502*0fca6ea1SDimitry Andric // that is supported by a linker that is known to ignore the index, 503*0fca6ea1SDimitry Andric // otherwise there's no chance of this working so we may as well error out. 504*0fca6ea1SDimitry Andric // We print a warning on read failure so that users of linkers that rely on 505*0fca6ea1SDimitry Andric // the symbol index can diagnose the issue. 506*0fca6ea1SDimitry Andric // 507*0fca6ea1SDimitry Andric // This is the same behavior as GNU ar when the linker plugin returns an 508*0fca6ea1SDimitry Andric // error when reading the input file. If the bitcode file is actually 509*0fca6ea1SDimitry Andric // malformed, it will be diagnosed at link time. 510*0fca6ea1SDimitry Andric if (!ObjOrErr) { 511*0fca6ea1SDimitry Andric switch (Kind) { 512*0fca6ea1SDimitry Andric case object::Archive::K_BSD: 513*0fca6ea1SDimitry Andric case object::Archive::K_GNU: 514*0fca6ea1SDimitry Andric case object::Archive::K_GNU64: 515*0fca6ea1SDimitry Andric Warn(ObjOrErr.takeError()); 516*0fca6ea1SDimitry Andric return nullptr; 517*0fca6ea1SDimitry Andric case object::Archive::K_AIXBIG: 518*0fca6ea1SDimitry Andric case object::Archive::K_COFF: 519*0fca6ea1SDimitry Andric case object::Archive::K_DARWIN: 520*0fca6ea1SDimitry Andric case object::Archive::K_DARWIN64: 52106c3fb27SDimitry Andric return ObjOrErr.takeError(); 522*0fca6ea1SDimitry Andric } 523*0fca6ea1SDimitry Andric } 52406c3fb27SDimitry Andric return std::move(*ObjOrErr); 52506c3fb27SDimitry Andric } else { 52606c3fb27SDimitry Andric auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf); 52706c3fb27SDimitry Andric if (!ObjOrErr) 52806c3fb27SDimitry Andric return ObjOrErr.takeError(); 52906c3fb27SDimitry Andric return std::move(*ObjOrErr); 53006c3fb27SDimitry Andric } 53106c3fb27SDimitry Andric } 53206c3fb27SDimitry Andric 5335f757f3fSDimitry Andric static bool is64BitSymbolicFile(const SymbolicFile *SymObj) { 5345f757f3fSDimitry Andric return SymObj != nullptr ? SymObj->is64Bit() : false; 5355f757f3fSDimitry Andric } 53606c3fb27SDimitry Andric 5375f757f3fSDimitry Andric // Log2 of PAGESIZE(4096) on an AIX system. 5385f757f3fSDimitry Andric static const uint32_t Log2OfAIXPageSize = 12; 53906c3fb27SDimitry Andric 5405f757f3fSDimitry Andric // In the AIX big archive format, since the data content follows the member file 5415f757f3fSDimitry Andric // name, if the name ends on an odd byte, an extra byte will be added for 5425f757f3fSDimitry Andric // padding. This ensures that the data within the member file starts at an even 5435f757f3fSDimitry Andric // byte. 5445f757f3fSDimitry Andric static const uint32_t MinBigArchiveMemDataAlign = 2; 5455f757f3fSDimitry Andric 5465f757f3fSDimitry Andric template <typename AuxiliaryHeader> 5475f757f3fSDimitry Andric uint16_t getAuxMaxAlignment(uint16_t AuxHeaderSize, AuxiliaryHeader *AuxHeader, 5485f757f3fSDimitry Andric uint16_t Log2OfMaxAlign) { 5495f757f3fSDimitry Andric // If the member doesn't have an auxiliary header, it isn't a loadable object 5505f757f3fSDimitry Andric // and so it just needs aligning at the minimum value. 5515f757f3fSDimitry Andric if (AuxHeader == nullptr) 5525f757f3fSDimitry Andric return MinBigArchiveMemDataAlign; 5535f757f3fSDimitry Andric 5545f757f3fSDimitry Andric // If the auxiliary header does not have both MaxAlignOfData and 5555f757f3fSDimitry Andric // MaxAlignOfText field, it is not a loadable shared object file, so align at 5565f757f3fSDimitry Andric // the minimum value. The 'ModuleType' member is located right after 5575f757f3fSDimitry Andric // 'MaxAlignOfData' in the AuxiliaryHeader. 5585f757f3fSDimitry Andric if (AuxHeaderSize < offsetof(AuxiliaryHeader, ModuleType)) 5595f757f3fSDimitry Andric return MinBigArchiveMemDataAlign; 5605f757f3fSDimitry Andric 5615f757f3fSDimitry Andric // If the XCOFF object file does not have a loader section, it is not 5625f757f3fSDimitry Andric // loadable, so align at the minimum value. 5635f757f3fSDimitry Andric if (AuxHeader->SecNumOfLoader == 0) 5645f757f3fSDimitry Andric return MinBigArchiveMemDataAlign; 5655f757f3fSDimitry Andric 5665f757f3fSDimitry Andric // The content of the loadable member file needs to be aligned at MAX(maximum 5675f757f3fSDimitry Andric // alignment of .text, maximum alignment of .data) if there are both fields. 5685f757f3fSDimitry Andric // If the desired alignment is > PAGESIZE, 32-bit members are aligned on a 5695f757f3fSDimitry Andric // word boundary, while 64-bit members are aligned on a PAGESIZE(2^12=4096) 5705f757f3fSDimitry Andric // boundary. 5715f757f3fSDimitry Andric uint16_t Log2OfAlign = 5725f757f3fSDimitry Andric std::max(AuxHeader->MaxAlignOfText, AuxHeader->MaxAlignOfData); 5735f757f3fSDimitry Andric return 1 << (Log2OfAlign > Log2OfAIXPageSize ? Log2OfMaxAlign : Log2OfAlign); 5745f757f3fSDimitry Andric } 5755f757f3fSDimitry Andric 5765f757f3fSDimitry Andric // AIX big archives may contain shared object members. The AIX OS requires these 5775f757f3fSDimitry Andric // members to be aligned if they are 64-bit and recommends it for 32-bit 5785f757f3fSDimitry Andric // members. This ensures that when these members are loaded they are aligned in 5795f757f3fSDimitry Andric // memory. 5805f757f3fSDimitry Andric static uint32_t getMemberAlignment(SymbolicFile *SymObj) { 5815f757f3fSDimitry Andric XCOFFObjectFile *XCOFFObj = dyn_cast_or_null<XCOFFObjectFile>(SymObj); 5825f757f3fSDimitry Andric if (!XCOFFObj) 5835f757f3fSDimitry Andric return MinBigArchiveMemDataAlign; 5845f757f3fSDimitry Andric 5855f757f3fSDimitry Andric // If the desired alignment is > PAGESIZE, 32-bit members are aligned on a 5865f757f3fSDimitry Andric // word boundary, while 64-bit members are aligned on a PAGESIZE boundary. 5875f757f3fSDimitry Andric return XCOFFObj->is64Bit() 5885f757f3fSDimitry Andric ? getAuxMaxAlignment(XCOFFObj->fileHeader64()->AuxHeaderSize, 5895f757f3fSDimitry Andric XCOFFObj->auxiliaryHeader64(), 5905f757f3fSDimitry Andric Log2OfAIXPageSize) 5915f757f3fSDimitry Andric : getAuxMaxAlignment(XCOFFObj->fileHeader32()->AuxHeaderSize, 5925f757f3fSDimitry Andric XCOFFObj->auxiliaryHeader32(), 2); 59306c3fb27SDimitry Andric } 59406c3fb27SDimitry Andric 5950b57cec5SDimitry Andric static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind, 5960b57cec5SDimitry Andric bool Deterministic, ArrayRef<MemberData> Members, 59706c3fb27SDimitry Andric StringRef StringTable, uint64_t MembersOffset, 59806c3fb27SDimitry Andric unsigned NumSyms, uint64_t PrevMemberOffset = 0, 59906c3fb27SDimitry Andric uint64_t NextMemberOffset = 0, 60006c3fb27SDimitry Andric bool Is64Bit = false) { 6010b57cec5SDimitry Andric // We don't write a symbol table on an archive with no members -- except on 6020b57cec5SDimitry Andric // Darwin, where the linker will abort unless the archive has a symbol table. 60306c3fb27SDimitry Andric if (StringTable.empty() && !isDarwin(Kind) && !isCOFFArchive(Kind)) 6040b57cec5SDimitry Andric return; 6050b57cec5SDimitry Andric 606e8d8bef9SDimitry Andric uint64_t OffsetSize = is64BitKind(Kind) ? 8 : 4; 607e8d8bef9SDimitry Andric uint32_t Pad; 60806c3fb27SDimitry Andric uint64_t Size = computeSymbolTableSize(Kind, NumSyms, OffsetSize, 60906c3fb27SDimitry Andric StringTable.size(), &Pad); 61006c3fb27SDimitry Andric writeSymbolTableHeader(Out, Kind, Deterministic, Size, PrevMemberOffset, 61106c3fb27SDimitry Andric NextMemberOffset); 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric if (isBSDLike(Kind)) 6140b57cec5SDimitry Andric printNBits(Out, Kind, NumSyms * 2 * OffsetSize); 6150b57cec5SDimitry Andric else 6160b57cec5SDimitry Andric printNBits(Out, Kind, NumSyms); 6170b57cec5SDimitry Andric 61806c3fb27SDimitry Andric uint64_t Pos = MembersOffset; 6190b57cec5SDimitry Andric for (const MemberData &M : Members) { 62006c3fb27SDimitry Andric if (isAIXBigArchive(Kind)) { 6215f757f3fSDimitry Andric Pos += M.PreHeadPadSize; 6225f757f3fSDimitry Andric if (is64BitSymbolicFile(M.SymFile.get()) != Is64Bit) { 62306c3fb27SDimitry Andric Pos += M.Header.size() + M.Data.size() + M.Padding.size(); 62406c3fb27SDimitry Andric continue; 62506c3fb27SDimitry Andric } 62606c3fb27SDimitry Andric } 62706c3fb27SDimitry Andric 6280b57cec5SDimitry Andric for (unsigned StringOffset : M.Symbols) { 6290b57cec5SDimitry Andric if (isBSDLike(Kind)) 6300b57cec5SDimitry Andric printNBits(Out, Kind, StringOffset); 6310b57cec5SDimitry Andric printNBits(Out, Kind, Pos); // member offset 6320b57cec5SDimitry Andric } 6330b57cec5SDimitry Andric Pos += M.Header.size() + M.Data.size() + M.Padding.size(); 6340b57cec5SDimitry Andric } 6350b57cec5SDimitry Andric 6360b57cec5SDimitry Andric if (isBSDLike(Kind)) 6370b57cec5SDimitry Andric // byte count of the string table 6380b57cec5SDimitry Andric printNBits(Out, Kind, StringTable.size()); 6390b57cec5SDimitry Andric Out << StringTable; 6400b57cec5SDimitry Andric 6410b57cec5SDimitry Andric while (Pad--) 6420b57cec5SDimitry Andric Out.write(uint8_t(0)); 6430b57cec5SDimitry Andric } 6440b57cec5SDimitry Andric 64506c3fb27SDimitry Andric static void writeSymbolMap(raw_ostream &Out, object::Archive::Kind Kind, 64606c3fb27SDimitry Andric bool Deterministic, ArrayRef<MemberData> Members, 64706c3fb27SDimitry Andric SymMap &SymMap, uint64_t MembersOffset) { 64806c3fb27SDimitry Andric uint32_t Pad; 64906c3fb27SDimitry Andric uint64_t Size = computeSymbolMapSize(Members.size(), SymMap, &Pad); 65006c3fb27SDimitry Andric writeSymbolTableHeader(Out, Kind, Deterministic, Size, 0); 6510b57cec5SDimitry Andric 65206c3fb27SDimitry Andric uint32_t Pos = MembersOffset; 65306c3fb27SDimitry Andric 65406c3fb27SDimitry Andric printLE<uint32_t>(Out, Members.size()); 65506c3fb27SDimitry Andric for (const MemberData &M : Members) { 65606c3fb27SDimitry Andric printLE(Out, Pos); // member offset 65706c3fb27SDimitry Andric Pos += M.Header.size() + M.Data.size() + M.Padding.size(); 65806c3fb27SDimitry Andric } 65906c3fb27SDimitry Andric 66006c3fb27SDimitry Andric printLE<uint32_t>(Out, SymMap.Map.size()); 66106c3fb27SDimitry Andric 66206c3fb27SDimitry Andric for (auto S : SymMap.Map) 66306c3fb27SDimitry Andric printLE(Out, S.second); 66406c3fb27SDimitry Andric for (auto S : SymMap.Map) 66506c3fb27SDimitry Andric Out << S.first << '\0'; 66606c3fb27SDimitry Andric 66706c3fb27SDimitry Andric while (Pad--) 66806c3fb27SDimitry Andric Out.write(uint8_t(0)); 66906c3fb27SDimitry Andric } 67006c3fb27SDimitry Andric 67106c3fb27SDimitry Andric static void writeECSymbols(raw_ostream &Out, object::Archive::Kind Kind, 67206c3fb27SDimitry Andric bool Deterministic, ArrayRef<MemberData> Members, 67306c3fb27SDimitry Andric SymMap &SymMap) { 67406c3fb27SDimitry Andric uint32_t Pad; 67506c3fb27SDimitry Andric uint64_t Size = computeECSymbolsSize(SymMap, &Pad); 67606c3fb27SDimitry Andric printGNUSmallMemberHeader(Out, "/<ECSYMBOLS>", now(Deterministic), 0, 0, 0, 67706c3fb27SDimitry Andric Size); 67806c3fb27SDimitry Andric 67906c3fb27SDimitry Andric printLE<uint32_t>(Out, SymMap.ECMap.size()); 68006c3fb27SDimitry Andric 68106c3fb27SDimitry Andric for (auto S : SymMap.ECMap) 68206c3fb27SDimitry Andric printLE(Out, S.second); 68306c3fb27SDimitry Andric for (auto S : SymMap.ECMap) 68406c3fb27SDimitry Andric Out << S.first << '\0'; 68506c3fb27SDimitry Andric while (Pad--) 68606c3fb27SDimitry Andric Out.write(uint8_t(0)); 68706c3fb27SDimitry Andric } 68806c3fb27SDimitry Andric 68906c3fb27SDimitry Andric static bool isECObject(object::SymbolicFile &Obj) { 69006c3fb27SDimitry Andric if (Obj.isCOFF()) 69106c3fb27SDimitry Andric return cast<llvm::object::COFFObjectFile>(&Obj)->getMachine() != 69206c3fb27SDimitry Andric COFF::IMAGE_FILE_MACHINE_ARM64; 69306c3fb27SDimitry Andric 6945f757f3fSDimitry Andric if (Obj.isCOFFImportFile()) 6955f757f3fSDimitry Andric return cast<llvm::object::COFFImportFile>(&Obj)->getMachine() != 6965f757f3fSDimitry Andric COFF::IMAGE_FILE_MACHINE_ARM64; 6975f757f3fSDimitry Andric 69806c3fb27SDimitry Andric if (Obj.isIR()) { 69906c3fb27SDimitry Andric Expected<std::string> TripleStr = 70006c3fb27SDimitry Andric getBitcodeTargetTriple(Obj.getMemoryBufferRef()); 70106c3fb27SDimitry Andric if (!TripleStr) 70206c3fb27SDimitry Andric return false; 70306c3fb27SDimitry Andric Triple T(*TripleStr); 70406c3fb27SDimitry Andric return T.isWindowsArm64EC() || T.getArch() == Triple::x86_64; 70506c3fb27SDimitry Andric } 70606c3fb27SDimitry Andric 70706c3fb27SDimitry Andric return false; 70806c3fb27SDimitry Andric } 70906c3fb27SDimitry Andric 710*0fca6ea1SDimitry Andric static bool isAnyArm64COFF(object::SymbolicFile &Obj) { 711*0fca6ea1SDimitry Andric if (Obj.isCOFF()) 712*0fca6ea1SDimitry Andric return COFF::isAnyArm64(cast<COFFObjectFile>(&Obj)->getMachine()); 713*0fca6ea1SDimitry Andric 714*0fca6ea1SDimitry Andric if (Obj.isCOFFImportFile()) 715*0fca6ea1SDimitry Andric return COFF::isAnyArm64(cast<COFFImportFile>(&Obj)->getMachine()); 716*0fca6ea1SDimitry Andric 717*0fca6ea1SDimitry Andric if (Obj.isIR()) { 718*0fca6ea1SDimitry Andric Expected<std::string> TripleStr = 719*0fca6ea1SDimitry Andric getBitcodeTargetTriple(Obj.getMemoryBufferRef()); 720*0fca6ea1SDimitry Andric if (!TripleStr) 721*0fca6ea1SDimitry Andric return false; 722*0fca6ea1SDimitry Andric Triple T(*TripleStr); 723*0fca6ea1SDimitry Andric return T.isOSWindows() && T.getArch() == Triple::aarch64; 724*0fca6ea1SDimitry Andric } 725*0fca6ea1SDimitry Andric 726*0fca6ea1SDimitry Andric return false; 727*0fca6ea1SDimitry Andric } 728*0fca6ea1SDimitry Andric 729439352acSDimitry Andric bool isImportDescriptor(StringRef Name) { 730439352acSDimitry Andric return Name.starts_with(ImportDescriptorPrefix) || 731439352acSDimitry Andric Name == StringRef{NullImportDescriptorSymbolName} || 732439352acSDimitry Andric (Name.starts_with(NullThunkDataPrefix) && 733439352acSDimitry Andric Name.ends_with(NullThunkDataSuffix)); 734439352acSDimitry Andric } 735439352acSDimitry Andric 7365f757f3fSDimitry Andric static Expected<std::vector<unsigned>> getSymbols(SymbolicFile *Obj, 7375f757f3fSDimitry Andric uint16_t Index, 7385f757f3fSDimitry Andric raw_ostream &SymNames, 7395f757f3fSDimitry Andric SymMap *SymMap) { 74006c3fb27SDimitry Andric std::vector<unsigned> Ret; 74106c3fb27SDimitry Andric 7425f757f3fSDimitry Andric if (Obj == nullptr) 743e8d8bef9SDimitry Andric return Ret; 7440b57cec5SDimitry Andric 74506c3fb27SDimitry Andric std::map<std::string, uint16_t> *Map = nullptr; 74606c3fb27SDimitry Andric if (SymMap) 74706c3fb27SDimitry Andric Map = SymMap->UseECMap && isECObject(*Obj) ? &SymMap->ECMap : &SymMap->Map; 7485f757f3fSDimitry Andric 7490b57cec5SDimitry Andric for (const object::BasicSymbolRef &S : Obj->symbols()) { 7500b57cec5SDimitry Andric if (!isArchiveSymbol(S)) 7510b57cec5SDimitry Andric continue; 75206c3fb27SDimitry Andric if (Map) { 75306c3fb27SDimitry Andric std::string Name; 75406c3fb27SDimitry Andric raw_string_ostream NameStream(Name); 75506c3fb27SDimitry Andric if (Error E = S.printName(NameStream)) 75606c3fb27SDimitry Andric return std::move(E); 75706c3fb27SDimitry Andric if (Map->find(Name) != Map->end()) 75806c3fb27SDimitry Andric continue; // ignore duplicated symbol 75906c3fb27SDimitry Andric (*Map)[Name] = Index; 76006c3fb27SDimitry Andric if (Map == &SymMap->Map) { 76106c3fb27SDimitry Andric Ret.push_back(SymNames.tell()); 76206c3fb27SDimitry Andric SymNames << Name << '\0'; 763439352acSDimitry Andric // If EC is enabled, then the import descriptors are NOT put into EC 764439352acSDimitry Andric // objects so we need to copy them to the EC map manually. 765439352acSDimitry Andric if (SymMap->UseECMap && isImportDescriptor(Name)) 766439352acSDimitry Andric SymMap->ECMap[Name] = Index; 76706c3fb27SDimitry Andric } 76806c3fb27SDimitry Andric } else { 7690b57cec5SDimitry Andric Ret.push_back(SymNames.tell()); 7700b57cec5SDimitry Andric if (Error E = S.printName(SymNames)) 7710b57cec5SDimitry Andric return std::move(E); 7720b57cec5SDimitry Andric SymNames << '\0'; 7730b57cec5SDimitry Andric } 77406c3fb27SDimitry Andric } 7750b57cec5SDimitry Andric return Ret; 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric 7780b57cec5SDimitry Andric static Expected<std::vector<MemberData>> 7790b57cec5SDimitry Andric computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames, 7800b57cec5SDimitry Andric object::Archive::Kind Kind, bool Thin, bool Deterministic, 7815f757f3fSDimitry Andric SymtabWritingMode NeedSymbols, SymMap *SymMap, 782*0fca6ea1SDimitry Andric LLVMContext &Context, ArrayRef<NewArchiveMember> NewMembers, 783*0fca6ea1SDimitry Andric std::optional<bool> IsEC, function_ref<void(Error)> Warn) { 7840b57cec5SDimitry Andric static char PaddingData[8] = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'}; 7855f757f3fSDimitry Andric uint64_t MemHeadPadSize = 0; 78681ad6265SDimitry Andric uint64_t Pos = 78781ad6265SDimitry Andric isAIXBigArchive(Kind) ? sizeof(object::BigArchive::FixLenHdr) : 0; 7880b57cec5SDimitry Andric 7890b57cec5SDimitry Andric std::vector<MemberData> Ret; 7900b57cec5SDimitry Andric bool HasObject = false; 7910b57cec5SDimitry Andric 7920b57cec5SDimitry Andric // Deduplicate long member names in the string table and reuse earlier name 7930b57cec5SDimitry Andric // offsets. This especially saves space for COFF Import libraries where all 7940b57cec5SDimitry Andric // members have the same name. 7950b57cec5SDimitry Andric StringMap<uint64_t> MemberNames; 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric // UniqueTimestamps is a special case to improve debugging on Darwin: 7980b57cec5SDimitry Andric // 7990b57cec5SDimitry Andric // The Darwin linker does not link debug info into the final 8005f757f3fSDimitry Andric // binary. Instead, it emits entries of type N_OSO in the output 8010b57cec5SDimitry Andric // binary's symbol table, containing references to the linked-in 8020b57cec5SDimitry Andric // object files. Using that reference, the debugger can read the 8030b57cec5SDimitry Andric // debug data directly from the object files. Alternatively, an 8040b57cec5SDimitry Andric // invocation of 'dsymutil' will link the debug data from the object 8050b57cec5SDimitry Andric // files into a dSYM bundle, which can be loaded by the debugger, 8060b57cec5SDimitry Andric // instead of the object files. 8070b57cec5SDimitry Andric // 8080b57cec5SDimitry Andric // For an object file, the N_OSO entries contain the absolute path 8090b57cec5SDimitry Andric // path to the file, and the file's timestamp. For an object 8100b57cec5SDimitry Andric // included in an archive, the path is formatted like 8110b57cec5SDimitry Andric // "/absolute/path/to/archive.a(member.o)", and the timestamp is the 8120b57cec5SDimitry Andric // archive member's timestamp, rather than the archive's timestamp. 8130b57cec5SDimitry Andric // 8140b57cec5SDimitry Andric // However, this doesn't always uniquely identify an object within 8150b57cec5SDimitry Andric // an archive -- an archive file can have multiple entries with the 8160b57cec5SDimitry Andric // same filename. (This will happen commonly if the original object 8170b57cec5SDimitry Andric // files started in different directories.) The only way they get 8180b57cec5SDimitry Andric // distinguished, then, is via the timestamp. But this process is 8190b57cec5SDimitry Andric // unable to find the correct object file in the archive when there 8200b57cec5SDimitry Andric // are two files of the same name and timestamp. 8210b57cec5SDimitry Andric // 8220b57cec5SDimitry Andric // Additionally, timestamp==0 is treated specially, and causes the 8230b57cec5SDimitry Andric // timestamp to be ignored as a match criteria. 8240b57cec5SDimitry Andric // 8250b57cec5SDimitry Andric // That will "usually" work out okay when creating an archive not in 8260b57cec5SDimitry Andric // deterministic timestamp mode, because the objects will probably 8270b57cec5SDimitry Andric // have been created at different timestamps. 8280b57cec5SDimitry Andric // 8290b57cec5SDimitry Andric // To ameliorate this problem, in deterministic archive mode (which 8300b57cec5SDimitry Andric // is the default), on Darwin we will emit a unique non-zero 8310b57cec5SDimitry Andric // timestamp for each entry with a duplicated name. This is still 8320b57cec5SDimitry Andric // deterministic: the only thing affecting that timestamp is the 8330b57cec5SDimitry Andric // order of the files in the resultant archive. 8340b57cec5SDimitry Andric // 8350b57cec5SDimitry Andric // See also the functions that handle the lookup: 8360b57cec5SDimitry Andric // in lldb: ObjectContainerBSDArchive::Archive::FindObject() 8370b57cec5SDimitry Andric // in llvm/tools/dsymutil: BinaryHolder::GetArchiveMemberBuffers(). 8380b57cec5SDimitry Andric bool UniqueTimestamps = Deterministic && isDarwin(Kind); 8390b57cec5SDimitry Andric std::map<StringRef, unsigned> FilenameCount; 8400b57cec5SDimitry Andric if (UniqueTimestamps) { 8410b57cec5SDimitry Andric for (const NewArchiveMember &M : NewMembers) 8420b57cec5SDimitry Andric FilenameCount[M.MemberName]++; 8430b57cec5SDimitry Andric for (auto &Entry : FilenameCount) 8440b57cec5SDimitry Andric Entry.second = Entry.second > 1 ? 1 : 0; 8450b57cec5SDimitry Andric } 8460b57cec5SDimitry Andric 847*0fca6ea1SDimitry Andric std::vector<std::unique_ptr<SymbolicFile>> SymFiles; 848*0fca6ea1SDimitry Andric 849*0fca6ea1SDimitry Andric if (NeedSymbols != SymtabWritingMode::NoSymtab || isAIXBigArchive(Kind)) { 850*0fca6ea1SDimitry Andric for (const NewArchiveMember &M : NewMembers) { 851*0fca6ea1SDimitry Andric Expected<std::unique_ptr<SymbolicFile>> SymFileOrErr = getSymbolicFile( 852*0fca6ea1SDimitry Andric M.Buf->getMemBufferRef(), Context, Kind, [&](Error Err) { 853*0fca6ea1SDimitry Andric Warn(createFileError(M.MemberName, std::move(Err))); 854*0fca6ea1SDimitry Andric }); 855*0fca6ea1SDimitry Andric if (!SymFileOrErr) 856*0fca6ea1SDimitry Andric return createFileError(M.MemberName, SymFileOrErr.takeError()); 857*0fca6ea1SDimitry Andric SymFiles.push_back(std::move(*SymFileOrErr)); 858*0fca6ea1SDimitry Andric } 859*0fca6ea1SDimitry Andric } 860*0fca6ea1SDimitry Andric 861*0fca6ea1SDimitry Andric if (SymMap) { 862*0fca6ea1SDimitry Andric if (IsEC) { 863*0fca6ea1SDimitry Andric SymMap->UseECMap = *IsEC; 864*0fca6ea1SDimitry Andric } else { 865*0fca6ea1SDimitry Andric // When IsEC is not specified by the caller, use it when we have both 866*0fca6ea1SDimitry Andric // any ARM64 object (ARM64 or ARM64EC) and any EC object (ARM64EC or 867*0fca6ea1SDimitry Andric // AMD64). This may be a single ARM64EC object, but may also be separate 868*0fca6ea1SDimitry Andric // ARM64 and AMD64 objects. 869*0fca6ea1SDimitry Andric bool HaveArm64 = false, HaveEC = false; 870*0fca6ea1SDimitry Andric for (std::unique_ptr<SymbolicFile> &SymFile : SymFiles) { 871*0fca6ea1SDimitry Andric if (!SymFile) 872*0fca6ea1SDimitry Andric continue; 873*0fca6ea1SDimitry Andric if (!HaveArm64) 874*0fca6ea1SDimitry Andric HaveArm64 = isAnyArm64COFF(*SymFile); 875*0fca6ea1SDimitry Andric if (!HaveEC) 876*0fca6ea1SDimitry Andric HaveEC = isECObject(*SymFile); 877*0fca6ea1SDimitry Andric if (HaveArm64 && HaveEC) { 878*0fca6ea1SDimitry Andric SymMap->UseECMap = true; 879*0fca6ea1SDimitry Andric break; 880*0fca6ea1SDimitry Andric } 881*0fca6ea1SDimitry Andric } 882*0fca6ea1SDimitry Andric } 883*0fca6ea1SDimitry Andric } 884*0fca6ea1SDimitry Andric 88581ad6265SDimitry Andric // The big archive format needs to know the offset of the previous member 88681ad6265SDimitry Andric // header. 88706c3fb27SDimitry Andric uint64_t PrevOffset = 0; 8885f757f3fSDimitry Andric uint64_t NextMemHeadPadSize = 0; 8895f757f3fSDimitry Andric 890*0fca6ea1SDimitry Andric for (uint32_t Index = 0; Index < NewMembers.size(); ++Index) { 891*0fca6ea1SDimitry Andric const NewArchiveMember *M = &NewMembers[Index]; 8920b57cec5SDimitry Andric std::string Header; 8930b57cec5SDimitry Andric raw_string_ostream Out(Header); 8940b57cec5SDimitry Andric 8955f757f3fSDimitry Andric MemoryBufferRef Buf = M->Buf->getMemBufferRef(); 8960b57cec5SDimitry Andric StringRef Data = Thin ? "" : Buf.getBuffer(); 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric // ld64 expects the members to be 8-byte aligned for 64-bit content and at 8990b57cec5SDimitry Andric // least 4-byte aligned for 32-bit content. Opt for the larger encoding 9000b57cec5SDimitry Andric // uniformly. This matches the behaviour with cctools and ensures that ld64 9010b57cec5SDimitry Andric // is happy with archives that we generate. 9020b57cec5SDimitry Andric unsigned MemberPadding = 9038bcb0991SDimitry Andric isDarwin(Kind) ? offsetToAlignment(Data.size(), Align(8)) : 0; 9048bcb0991SDimitry Andric unsigned TailPadding = 9058bcb0991SDimitry Andric offsetToAlignment(Data.size() + MemberPadding, Align(2)); 9060b57cec5SDimitry Andric StringRef Padding = StringRef(PaddingData, MemberPadding + TailPadding); 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric sys::TimePoint<std::chrono::seconds> ModTime; 9090b57cec5SDimitry Andric if (UniqueTimestamps) 9100b57cec5SDimitry Andric // Increment timestamp for each file of a given name. 9115f757f3fSDimitry Andric ModTime = sys::toTimePoint(FilenameCount[M->MemberName]++); 9120b57cec5SDimitry Andric else 9135f757f3fSDimitry Andric ModTime = M->ModTime; 9148bcb0991SDimitry Andric 9158bcb0991SDimitry Andric uint64_t Size = Buf.getBufferSize() + MemberPadding; 9168bcb0991SDimitry Andric if (Size > object::Archive::MaxMemberSize) { 9178bcb0991SDimitry Andric std::string StringMsg = 9185f757f3fSDimitry Andric "File " + M->MemberName.str() + " exceeds size limit"; 9198bcb0991SDimitry Andric return make_error<object::GenericBinaryError>( 9208bcb0991SDimitry Andric std::move(StringMsg), object::object_error::parse_failed); 9218bcb0991SDimitry Andric } 9228bcb0991SDimitry Andric 923*0fca6ea1SDimitry Andric std::unique_ptr<SymbolicFile> CurSymFile; 924*0fca6ea1SDimitry Andric if (!SymFiles.empty()) 925*0fca6ea1SDimitry Andric CurSymFile = std::move(SymFiles[Index]); 9265f757f3fSDimitry Andric 9275f757f3fSDimitry Andric // In the big archive file format, we need to calculate and include the next 9285f757f3fSDimitry Andric // member offset and previous member offset in the file member header. 92981ad6265SDimitry Andric if (isAIXBigArchive(Kind)) { 9305f757f3fSDimitry Andric uint64_t OffsetToMemData = Pos + sizeof(object::BigArMemHdrType) + 9315f757f3fSDimitry Andric alignTo(M->MemberName.size(), 2); 9325f757f3fSDimitry Andric 9335f757f3fSDimitry Andric if (M == NewMembers.begin()) 9345f757f3fSDimitry Andric NextMemHeadPadSize = 9355f757f3fSDimitry Andric alignToPowerOf2(OffsetToMemData, 9365f757f3fSDimitry Andric getMemberAlignment(CurSymFile.get())) - 9375f757f3fSDimitry Andric OffsetToMemData; 9385f757f3fSDimitry Andric 9395f757f3fSDimitry Andric MemHeadPadSize = NextMemHeadPadSize; 9405f757f3fSDimitry Andric Pos += MemHeadPadSize; 94106c3fb27SDimitry Andric uint64_t NextOffset = Pos + sizeof(object::BigArMemHdrType) + 9425f757f3fSDimitry Andric alignTo(M->MemberName.size(), 2) + alignTo(Size, 2); 9435f757f3fSDimitry Andric 9445f757f3fSDimitry Andric // If there is another member file after this, we need to calculate the 9455f757f3fSDimitry Andric // padding before the header. 946*0fca6ea1SDimitry Andric if (Index + 1 != SymFiles.size()) { 947*0fca6ea1SDimitry Andric uint64_t OffsetToNextMemData = 948*0fca6ea1SDimitry Andric NextOffset + sizeof(object::BigArMemHdrType) + 949*0fca6ea1SDimitry Andric alignTo(NewMembers[Index + 1].MemberName.size(), 2); 9505f757f3fSDimitry Andric NextMemHeadPadSize = 9515f757f3fSDimitry Andric alignToPowerOf2(OffsetToNextMemData, 952*0fca6ea1SDimitry Andric getMemberAlignment(SymFiles[Index + 1].get())) - 9535f757f3fSDimitry Andric OffsetToNextMemData; 9545f757f3fSDimitry Andric NextOffset += NextMemHeadPadSize; 9555f757f3fSDimitry Andric } 9565f757f3fSDimitry Andric printBigArchiveMemberHeader(Out, M->MemberName, ModTime, M->UID, M->GID, 9575f757f3fSDimitry Andric M->Perms, Size, PrevOffset, NextOffset); 95881ad6265SDimitry Andric PrevOffset = Pos; 95981ad6265SDimitry Andric } else { 9605f757f3fSDimitry Andric printMemberHeader(Out, Pos, StringTable, MemberNames, Kind, Thin, *M, 9618bcb0991SDimitry Andric ModTime, Size); 96281ad6265SDimitry Andric } 9630b57cec5SDimitry Andric Out.flush(); 9640b57cec5SDimitry Andric 965e8d8bef9SDimitry Andric std::vector<unsigned> Symbols; 9665f757f3fSDimitry Andric if (NeedSymbols != SymtabWritingMode::NoSymtab) { 967e8d8bef9SDimitry Andric Expected<std::vector<unsigned>> SymbolsOrErr = 968*0fca6ea1SDimitry Andric getSymbols(CurSymFile.get(), Index + 1, SymNames, SymMap); 969bdd1243dSDimitry Andric if (!SymbolsOrErr) 9705f757f3fSDimitry Andric return createFileError(M->MemberName, SymbolsOrErr.takeError()); 971e8d8bef9SDimitry Andric Symbols = std::move(*SymbolsOrErr); 9725f757f3fSDimitry Andric if (CurSymFile) 9735f757f3fSDimitry Andric HasObject = true; 974e8d8bef9SDimitry Andric } 9750b57cec5SDimitry Andric 9760b57cec5SDimitry Andric Pos += Header.size() + Data.size() + Padding.size(); 9775f757f3fSDimitry Andric Ret.push_back({std::move(Symbols), std::move(Header), Data, Padding, 9785f757f3fSDimitry Andric MemHeadPadSize, std::move(CurSymFile)}); 9790b57cec5SDimitry Andric } 9800b57cec5SDimitry Andric // If there are no symbols, emit an empty symbol table, to satisfy Solaris 9810b57cec5SDimitry Andric // tools, older versions of which expect a symbol table in a non-empty 9820b57cec5SDimitry Andric // archive, regardless of whether there are any symbols in it. 98306c3fb27SDimitry Andric if (HasObject && SymNames.tell() == 0 && !isCOFFArchive(Kind)) 9840b57cec5SDimitry Andric SymNames << '\0' << '\0' << '\0'; 9855f757f3fSDimitry Andric return std::move(Ret); 9860b57cec5SDimitry Andric } 9870b57cec5SDimitry Andric 9880b57cec5SDimitry Andric namespace llvm { 9890b57cec5SDimitry Andric 9900b57cec5SDimitry Andric static ErrorOr<SmallString<128>> canonicalizePath(StringRef P) { 9910b57cec5SDimitry Andric SmallString<128> Ret = P; 9920b57cec5SDimitry Andric std::error_code Err = sys::fs::make_absolute(Ret); 9930b57cec5SDimitry Andric if (Err) 9940b57cec5SDimitry Andric return Err; 9950b57cec5SDimitry Andric sys::path::remove_dots(Ret, /*removedotdot*/ true); 9960b57cec5SDimitry Andric return Ret; 9970b57cec5SDimitry Andric } 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric // Compute the relative path from From to To. 10000b57cec5SDimitry Andric Expected<std::string> computeArchiveRelativePath(StringRef From, StringRef To) { 10010b57cec5SDimitry Andric ErrorOr<SmallString<128>> PathToOrErr = canonicalizePath(To); 10020b57cec5SDimitry Andric ErrorOr<SmallString<128>> DirFromOrErr = canonicalizePath(From); 10030b57cec5SDimitry Andric if (!PathToOrErr || !DirFromOrErr) 1004*0fca6ea1SDimitry Andric return errorCodeToError(errnoAsErrorCode()); 10050b57cec5SDimitry Andric 10060b57cec5SDimitry Andric const SmallString<128> &PathTo = *PathToOrErr; 10070b57cec5SDimitry Andric const SmallString<128> &DirFrom = sys::path::parent_path(*DirFromOrErr); 10080b57cec5SDimitry Andric 10090b57cec5SDimitry Andric // Can't construct a relative path between different roots 10100b57cec5SDimitry Andric if (sys::path::root_name(PathTo) != sys::path::root_name(DirFrom)) 10110b57cec5SDimitry Andric return sys::path::convert_to_slash(PathTo); 10120b57cec5SDimitry Andric 10130b57cec5SDimitry Andric // Skip common prefixes 10140b57cec5SDimitry Andric auto FromTo = 10150b57cec5SDimitry Andric std::mismatch(sys::path::begin(DirFrom), sys::path::end(DirFrom), 10160b57cec5SDimitry Andric sys::path::begin(PathTo)); 10170b57cec5SDimitry Andric auto FromI = FromTo.first; 10180b57cec5SDimitry Andric auto ToI = FromTo.second; 10190b57cec5SDimitry Andric 10200b57cec5SDimitry Andric // Construct relative path 10210b57cec5SDimitry Andric SmallString<128> Relative; 10220b57cec5SDimitry Andric for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI) 10230b57cec5SDimitry Andric sys::path::append(Relative, sys::path::Style::posix, ".."); 10240b57cec5SDimitry Andric 10250b57cec5SDimitry Andric for (auto ToE = sys::path::end(PathTo); ToI != ToE; ++ToI) 10260b57cec5SDimitry Andric sys::path::append(Relative, sys::path::Style::posix, *ToI); 10270b57cec5SDimitry Andric 10287a6dacacSDimitry Andric return std::string(Relative); 10290b57cec5SDimitry Andric } 10300b57cec5SDimitry Andric 1031*0fca6ea1SDimitry Andric Error writeArchiveToStream(raw_ostream &Out, 1032e8d8bef9SDimitry Andric ArrayRef<NewArchiveMember> NewMembers, 10335f757f3fSDimitry Andric SymtabWritingMode WriteSymtab, 1034*0fca6ea1SDimitry Andric object::Archive::Kind Kind, bool Deterministic, 1035*0fca6ea1SDimitry Andric bool Thin, std::optional<bool> IsEC, 1036*0fca6ea1SDimitry Andric function_ref<void(Error)> Warn) { 10370b57cec5SDimitry Andric assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode"); 10380b57cec5SDimitry Andric 10390b57cec5SDimitry Andric SmallString<0> SymNamesBuf; 10400b57cec5SDimitry Andric raw_svector_ostream SymNames(SymNamesBuf); 10410b57cec5SDimitry Andric SmallString<0> StringTableBuf; 10420b57cec5SDimitry Andric raw_svector_ostream StringTable(StringTableBuf); 104306c3fb27SDimitry Andric SymMap SymMap; 1044*0fca6ea1SDimitry Andric bool ShouldWriteSymtab = WriteSymtab != SymtabWritingMode::NoSymtab; 10450b57cec5SDimitry Andric 104606c3fb27SDimitry Andric // COFF symbol map uses 16-bit indexes, so we can't use it if there are too 1047*0fca6ea1SDimitry Andric // many members. COFF format also requires symbol table presence, so use 1048*0fca6ea1SDimitry Andric // GNU format when NoSymtab is requested. 1049*0fca6ea1SDimitry Andric if (isCOFFArchive(Kind) && (NewMembers.size() > 0xfffe || !ShouldWriteSymtab)) 105006c3fb27SDimitry Andric Kind = object::Archive::K_GNU; 105106c3fb27SDimitry Andric 10525f757f3fSDimitry Andric // In the scenario when LLVMContext is populated SymbolicFile will contain a 10535f757f3fSDimitry Andric // reference to it, thus SymbolicFile should be destroyed first. 10545f757f3fSDimitry Andric LLVMContext Context; 10555f757f3fSDimitry Andric 105606c3fb27SDimitry Andric Expected<std::vector<MemberData>> DataOrErr = computeMemberData( 105706c3fb27SDimitry Andric StringTable, SymNames, Kind, Thin, Deterministic, WriteSymtab, 1058*0fca6ea1SDimitry Andric isCOFFArchive(Kind) ? &SymMap : nullptr, Context, NewMembers, IsEC, Warn); 10590b57cec5SDimitry Andric if (Error E = DataOrErr.takeError()) 10600b57cec5SDimitry Andric return E; 10610b57cec5SDimitry Andric std::vector<MemberData> &Data = *DataOrErr; 10620b57cec5SDimitry Andric 106306c3fb27SDimitry Andric uint64_t StringTableSize = 0; 106406c3fb27SDimitry Andric MemberData StringTableMember; 106506c3fb27SDimitry Andric if (!StringTableBuf.empty() && !isAIXBigArchive(Kind)) { 106606c3fb27SDimitry Andric StringTableMember = computeStringTable(StringTableBuf); 106706c3fb27SDimitry Andric StringTableSize = StringTableMember.Header.size() + 106806c3fb27SDimitry Andric StringTableMember.Data.size() + 106906c3fb27SDimitry Andric StringTableMember.Padding.size(); 107006c3fb27SDimitry Andric } 10710b57cec5SDimitry Andric 10720b57cec5SDimitry Andric // We would like to detect if we need to switch to a 64-bit symbol table. 107306c3fb27SDimitry Andric uint64_t LastMemberEndOffset = 0; 107406c3fb27SDimitry Andric uint64_t LastMemberHeaderOffset = 0; 1075e8d8bef9SDimitry Andric uint64_t NumSyms = 0; 107606c3fb27SDimitry Andric uint64_t NumSyms32 = 0; // Store symbol number of 32-bit member files. 107706c3fb27SDimitry Andric 10780b57cec5SDimitry Andric for (const auto &M : Data) { 10790b57cec5SDimitry Andric // Record the start of the member's offset 10805f757f3fSDimitry Andric LastMemberEndOffset += M.PreHeadPadSize; 108181ad6265SDimitry Andric LastMemberHeaderOffset = LastMemberEndOffset; 10820b57cec5SDimitry Andric // Account for the size of each part associated with the member. 108381ad6265SDimitry Andric LastMemberEndOffset += M.Header.size() + M.Data.size() + M.Padding.size(); 1084e8d8bef9SDimitry Andric NumSyms += M.Symbols.size(); 108506c3fb27SDimitry Andric 108606c3fb27SDimitry Andric // AIX big archive files may contain two global symbol tables. The 108706c3fb27SDimitry Andric // first global symbol table locates 32-bit file members that define global 108806c3fb27SDimitry Andric // symbols; the second global symbol table does the same for 64-bit file 108906c3fb27SDimitry Andric // members. As a big archive can have both 32-bit and 64-bit file members, 109006c3fb27SDimitry Andric // we need to know the number of symbols in each symbol table individually. 10915f757f3fSDimitry Andric if (isAIXBigArchive(Kind) && ShouldWriteSymtab) { 10925f757f3fSDimitry Andric if (!is64BitSymbolicFile(M.SymFile.get())) 109306c3fb27SDimitry Andric NumSyms32 += M.Symbols.size(); 10940b57cec5SDimitry Andric } 109506c3fb27SDimitry Andric } 109606c3fb27SDimitry Andric 109706c3fb27SDimitry Andric std::optional<uint64_t> HeadersSize; 10980b57cec5SDimitry Andric 109981ad6265SDimitry Andric // The symbol table is put at the end of the big archive file. The symbol 110081ad6265SDimitry Andric // table is at the start of the archive file for other archive formats. 11015f757f3fSDimitry Andric if (ShouldWriteSymtab && !is64BitKind(Kind)) { 1102e8d8bef9SDimitry Andric // We assume 32-bit offsets to see if 32-bit symbols are possible or not. 110306c3fb27SDimitry Andric HeadersSize = computeHeadersSize(Kind, Data.size(), StringTableSize, 110406c3fb27SDimitry Andric NumSyms, SymNamesBuf.size(), 110506c3fb27SDimitry Andric isCOFFArchive(Kind) ? &SymMap : nullptr); 1106e8d8bef9SDimitry Andric 11070b57cec5SDimitry Andric // The SYM64 format is used when an archive's member offsets are larger than 11080b57cec5SDimitry Andric // 32-bits can hold. The need for this shift in format is detected by 11090b57cec5SDimitry Andric // writeArchive. To test this we need to generate a file with a member that 11100b57cec5SDimitry Andric // has an offset larger than 32-bits but this demands a very slow test. To 11110b57cec5SDimitry Andric // speed the test up we use this environment variable to pretend like the 11120b57cec5SDimitry Andric // cutoff happens before 32-bits and instead happens at some much smaller 11130b57cec5SDimitry Andric // value. 1114e8d8bef9SDimitry Andric uint64_t Sym64Threshold = 1ULL << 32; 11150b57cec5SDimitry Andric const char *Sym64Env = std::getenv("SYM64_THRESHOLD"); 11160b57cec5SDimitry Andric if (Sym64Env) 11170b57cec5SDimitry Andric StringRef(Sym64Env).getAsInteger(10, Sym64Threshold); 11180b57cec5SDimitry Andric 111981ad6265SDimitry Andric // If LastMemberHeaderOffset isn't going to fit in a 32-bit varible we need 112081ad6265SDimitry Andric // to switch to 64-bit. Note that the file can be larger than 4GB as long as 112181ad6265SDimitry Andric // the last member starts before the 4GB offset. 112206c3fb27SDimitry Andric if (*HeadersSize + LastMemberHeaderOffset >= Sym64Threshold) { 11230b57cec5SDimitry Andric if (Kind == object::Archive::K_DARWIN) 11240b57cec5SDimitry Andric Kind = object::Archive::K_DARWIN64; 11250b57cec5SDimitry Andric else 11260b57cec5SDimitry Andric Kind = object::Archive::K_GNU64; 112706c3fb27SDimitry Andric HeadersSize.reset(); 11280b57cec5SDimitry Andric } 11290b57cec5SDimitry Andric } 11300b57cec5SDimitry Andric 11310b57cec5SDimitry Andric if (Thin) 11320b57cec5SDimitry Andric Out << "!<thin>\n"; 113381ad6265SDimitry Andric else if (isAIXBigArchive(Kind)) 113481ad6265SDimitry Andric Out << "<bigaf>\n"; 11350b57cec5SDimitry Andric else 11360b57cec5SDimitry Andric Out << "!<arch>\n"; 11370b57cec5SDimitry Andric 113881ad6265SDimitry Andric if (!isAIXBigArchive(Kind)) { 11395f757f3fSDimitry Andric if (ShouldWriteSymtab) { 114006c3fb27SDimitry Andric if (!HeadersSize) 114106c3fb27SDimitry Andric HeadersSize = computeHeadersSize( 114206c3fb27SDimitry Andric Kind, Data.size(), StringTableSize, NumSyms, SymNamesBuf.size(), 114306c3fb27SDimitry Andric isCOFFArchive(Kind) ? &SymMap : nullptr); 114406c3fb27SDimitry Andric writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf, 114506c3fb27SDimitry Andric *HeadersSize, NumSyms); 114606c3fb27SDimitry Andric 114706c3fb27SDimitry Andric if (isCOFFArchive(Kind)) 114806c3fb27SDimitry Andric writeSymbolMap(Out, Kind, Deterministic, Data, SymMap, *HeadersSize); 114906c3fb27SDimitry Andric } 115006c3fb27SDimitry Andric 115106c3fb27SDimitry Andric if (StringTableSize) 115206c3fb27SDimitry Andric Out << StringTableMember.Header << StringTableMember.Data 115306c3fb27SDimitry Andric << StringTableMember.Padding; 115406c3fb27SDimitry Andric 11555f757f3fSDimitry Andric if (ShouldWriteSymtab && SymMap.ECMap.size()) 115606c3fb27SDimitry Andric writeECSymbols(Out, Kind, Deterministic, Data, SymMap); 115706c3fb27SDimitry Andric 11580b57cec5SDimitry Andric for (const MemberData &M : Data) 11590b57cec5SDimitry Andric Out << M.Header << M.Data << M.Padding; 116081ad6265SDimitry Andric } else { 116106c3fb27SDimitry Andric HeadersSize = sizeof(object::BigArchive::FixLenHdr); 116206c3fb27SDimitry Andric LastMemberEndOffset += *HeadersSize; 116306c3fb27SDimitry Andric LastMemberHeaderOffset += *HeadersSize; 116406c3fb27SDimitry Andric 116581ad6265SDimitry Andric // For the big archive (AIX) format, compute a table of member names and 116681ad6265SDimitry Andric // offsets, used in the member table. 116781ad6265SDimitry Andric uint64_t MemberTableNameStrTblSize = 0; 116881ad6265SDimitry Andric std::vector<size_t> MemberOffsets; 116981ad6265SDimitry Andric std::vector<StringRef> MemberNames; 117081ad6265SDimitry Andric // Loop across object to find offset and names. 117181ad6265SDimitry Andric uint64_t MemberEndOffset = sizeof(object::BigArchive::FixLenHdr); 117281ad6265SDimitry Andric for (size_t I = 0, Size = NewMembers.size(); I != Size; ++I) { 117381ad6265SDimitry Andric const NewArchiveMember &Member = NewMembers[I]; 117481ad6265SDimitry Andric MemberTableNameStrTblSize += Member.MemberName.size() + 1; 11755f757f3fSDimitry Andric MemberEndOffset += Data[I].PreHeadPadSize; 117681ad6265SDimitry Andric MemberOffsets.push_back(MemberEndOffset); 117781ad6265SDimitry Andric MemberNames.push_back(Member.MemberName); 117881ad6265SDimitry Andric // File member name ended with "`\n". The length is included in 117981ad6265SDimitry Andric // BigArMemHdrType. 118081ad6265SDimitry Andric MemberEndOffset += sizeof(object::BigArMemHdrType) + 118181ad6265SDimitry Andric alignTo(Data[I].Data.size(), 2) + 118281ad6265SDimitry Andric alignTo(Member.MemberName.size(), 2); 118381ad6265SDimitry Andric } 11840b57cec5SDimitry Andric 118581ad6265SDimitry Andric // AIX member table size. 118606c3fb27SDimitry Andric uint64_t MemberTableSize = 20 + // Number of members field 118781ad6265SDimitry Andric 20 * MemberOffsets.size() + 118881ad6265SDimitry Andric MemberTableNameStrTblSize; 118981ad6265SDimitry Andric 119006c3fb27SDimitry Andric SmallString<0> SymNamesBuf32; 119106c3fb27SDimitry Andric SmallString<0> SymNamesBuf64; 119206c3fb27SDimitry Andric raw_svector_ostream SymNames32(SymNamesBuf32); 119306c3fb27SDimitry Andric raw_svector_ostream SymNames64(SymNamesBuf64); 119406c3fb27SDimitry Andric 11955f757f3fSDimitry Andric if (ShouldWriteSymtab && NumSyms) 119606c3fb27SDimitry Andric // Generate the symbol names for the members. 11975f757f3fSDimitry Andric for (const auto &M : Data) { 11985f757f3fSDimitry Andric Expected<std::vector<unsigned>> SymbolsOrErr = getSymbols( 11995f757f3fSDimitry Andric M.SymFile.get(), 0, 12005f757f3fSDimitry Andric is64BitSymbolicFile(M.SymFile.get()) ? SymNames64 : SymNames32, 12015f757f3fSDimitry Andric nullptr); 120206c3fb27SDimitry Andric if (!SymbolsOrErr) 120306c3fb27SDimitry Andric return SymbolsOrErr.takeError(); 120406c3fb27SDimitry Andric } 120506c3fb27SDimitry Andric 120606c3fb27SDimitry Andric uint64_t MemberTableEndOffset = 120706c3fb27SDimitry Andric LastMemberEndOffset + 120806c3fb27SDimitry Andric alignTo(sizeof(object::BigArMemHdrType) + MemberTableSize, 2); 120906c3fb27SDimitry Andric 121006c3fb27SDimitry Andric // In AIX OS, The 'GlobSymOffset' field in the fixed-length header contains 121106c3fb27SDimitry Andric // the offset to the 32-bit global symbol table, and the 'GlobSym64Offset' 121206c3fb27SDimitry Andric // contains the offset to the 64-bit global symbol table. 121306c3fb27SDimitry Andric uint64_t GlobalSymbolOffset = 12145f757f3fSDimitry Andric (ShouldWriteSymtab && 12155f757f3fSDimitry Andric (WriteSymtab != SymtabWritingMode::BigArchive64) && NumSyms32 > 0) 12165f757f3fSDimitry Andric ? MemberTableEndOffset 12175f757f3fSDimitry Andric : 0; 121806c3fb27SDimitry Andric 121906c3fb27SDimitry Andric uint64_t GlobalSymbolOffset64 = 0; 122006c3fb27SDimitry Andric uint64_t NumSyms64 = NumSyms - NumSyms32; 12215f757f3fSDimitry Andric if (ShouldWriteSymtab && (WriteSymtab != SymtabWritingMode::BigArchive32) && 12225f757f3fSDimitry Andric NumSyms64 > 0) { 122306c3fb27SDimitry Andric if (GlobalSymbolOffset == 0) 122406c3fb27SDimitry Andric GlobalSymbolOffset64 = MemberTableEndOffset; 122506c3fb27SDimitry Andric else 122606c3fb27SDimitry Andric // If there is a global symbol table for 32-bit members, 122706c3fb27SDimitry Andric // the 64-bit global symbol table is after the 32-bit one. 122806c3fb27SDimitry Andric GlobalSymbolOffset64 = 122906c3fb27SDimitry Andric GlobalSymbolOffset + sizeof(object::BigArMemHdrType) + 123006c3fb27SDimitry Andric (NumSyms32 + 1) * 8 + alignTo(SymNamesBuf32.size(), 2); 123106c3fb27SDimitry Andric } 123281ad6265SDimitry Andric 123381ad6265SDimitry Andric // Fixed Sized Header. 123481ad6265SDimitry Andric printWithSpacePadding(Out, NewMembers.size() ? LastMemberEndOffset : 0, 123581ad6265SDimitry Andric 20); // Offset to member table 123681ad6265SDimitry Andric // If there are no file members in the archive, there will be no global 123781ad6265SDimitry Andric // symbol table. 123806c3fb27SDimitry Andric printWithSpacePadding(Out, GlobalSymbolOffset, 20); 123906c3fb27SDimitry Andric printWithSpacePadding(Out, GlobalSymbolOffset64, 20); 12405f757f3fSDimitry Andric printWithSpacePadding(Out, 12415f757f3fSDimitry Andric NewMembers.size() 12425f757f3fSDimitry Andric ? sizeof(object::BigArchive::FixLenHdr) + 12435f757f3fSDimitry Andric Data[0].PreHeadPadSize 12445f757f3fSDimitry Andric : 0, 124581ad6265SDimitry Andric 20); // Offset to first archive member 124681ad6265SDimitry Andric printWithSpacePadding(Out, NewMembers.size() ? LastMemberHeaderOffset : 0, 124781ad6265SDimitry Andric 20); // Offset to last archive member 124881ad6265SDimitry Andric printWithSpacePadding( 124981ad6265SDimitry Andric Out, 0, 125081ad6265SDimitry Andric 20); // Offset to first member of free list - Not supported yet 125181ad6265SDimitry Andric 125281ad6265SDimitry Andric for (const MemberData &M : Data) { 12535f757f3fSDimitry Andric Out << std::string(M.PreHeadPadSize, '\0'); 125481ad6265SDimitry Andric Out << M.Header << M.Data; 125581ad6265SDimitry Andric if (M.Data.size() % 2) 125681ad6265SDimitry Andric Out << '\0'; 125781ad6265SDimitry Andric } 125881ad6265SDimitry Andric 125981ad6265SDimitry Andric if (NewMembers.size()) { 126081ad6265SDimitry Andric // Member table. 126181ad6265SDimitry Andric printBigArchiveMemberHeader(Out, "", sys::toTimePoint(0), 0, 0, 0, 126281ad6265SDimitry Andric MemberTableSize, LastMemberHeaderOffset, 126306c3fb27SDimitry Andric GlobalSymbolOffset ? GlobalSymbolOffset 126406c3fb27SDimitry Andric : GlobalSymbolOffset64); 126581ad6265SDimitry Andric printWithSpacePadding(Out, MemberOffsets.size(), 20); // Number of members 126681ad6265SDimitry Andric for (uint64_t MemberOffset : MemberOffsets) 126781ad6265SDimitry Andric printWithSpacePadding(Out, MemberOffset, 126881ad6265SDimitry Andric 20); // Offset to member file header. 126981ad6265SDimitry Andric for (StringRef MemberName : MemberNames) 127081ad6265SDimitry Andric Out << MemberName << '\0'; // Member file name, null byte padding. 127181ad6265SDimitry Andric 127281ad6265SDimitry Andric if (MemberTableNameStrTblSize % 2) 127381ad6265SDimitry Andric Out << '\0'; // Name table must be tail padded to an even number of 127481ad6265SDimitry Andric // bytes. 127581ad6265SDimitry Andric 12765f757f3fSDimitry Andric if (ShouldWriteSymtab) { 127706c3fb27SDimitry Andric // Write global symbol table for 32-bit file members. 127806c3fb27SDimitry Andric if (GlobalSymbolOffset) { 127906c3fb27SDimitry Andric writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf32, 128006c3fb27SDimitry Andric *HeadersSize, NumSyms32, LastMemberEndOffset, 128106c3fb27SDimitry Andric GlobalSymbolOffset64); 128206c3fb27SDimitry Andric // Add padding between the symbol tables, if needed. 128306c3fb27SDimitry Andric if (GlobalSymbolOffset64 && (SymNamesBuf32.size() % 2)) 128406c3fb27SDimitry Andric Out << '\0'; 128506c3fb27SDimitry Andric } 128606c3fb27SDimitry Andric 128706c3fb27SDimitry Andric // Write global symbol table for 64-bit file members. 128806c3fb27SDimitry Andric if (GlobalSymbolOffset64) 128906c3fb27SDimitry Andric writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf64, 129006c3fb27SDimitry Andric *HeadersSize, NumSyms64, 129106c3fb27SDimitry Andric GlobalSymbolOffset ? GlobalSymbolOffset 129206c3fb27SDimitry Andric : LastMemberEndOffset, 129306c3fb27SDimitry Andric 0, true); 129406c3fb27SDimitry Andric } 129581ad6265SDimitry Andric } 129681ad6265SDimitry Andric } 12970b57cec5SDimitry Andric Out.flush(); 1298e8d8bef9SDimitry Andric return Error::success(); 1299e8d8bef9SDimitry Andric } 1300e8d8bef9SDimitry Andric 1301*0fca6ea1SDimitry Andric void warnToStderr(Error Err) { 1302*0fca6ea1SDimitry Andric llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "warning: "); 1303*0fca6ea1SDimitry Andric } 1304*0fca6ea1SDimitry Andric 1305e8d8bef9SDimitry Andric Error writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers, 13065f757f3fSDimitry Andric SymtabWritingMode WriteSymtab, object::Archive::Kind Kind, 1307e8d8bef9SDimitry Andric bool Deterministic, bool Thin, 1308*0fca6ea1SDimitry Andric std::unique_ptr<MemoryBuffer> OldArchiveBuf, 1309*0fca6ea1SDimitry Andric std::optional<bool> IsEC, function_ref<void(Error)> Warn) { 1310e8d8bef9SDimitry Andric Expected<sys::fs::TempFile> Temp = 1311e8d8bef9SDimitry Andric sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a"); 1312e8d8bef9SDimitry Andric if (!Temp) 1313e8d8bef9SDimitry Andric return Temp.takeError(); 1314e8d8bef9SDimitry Andric raw_fd_ostream Out(Temp->FD, false); 1315e8d8bef9SDimitry Andric 1316e8d8bef9SDimitry Andric if (Error E = writeArchiveToStream(Out, NewMembers, WriteSymtab, Kind, 1317*0fca6ea1SDimitry Andric Deterministic, Thin, IsEC, Warn)) { 1318e8d8bef9SDimitry Andric if (Error DiscardError = Temp->discard()) 1319e8d8bef9SDimitry Andric return joinErrors(std::move(E), std::move(DiscardError)); 1320e8d8bef9SDimitry Andric return E; 1321e8d8bef9SDimitry Andric } 13220b57cec5SDimitry Andric 13230b57cec5SDimitry Andric // At this point, we no longer need whatever backing memory 13240b57cec5SDimitry Andric // was used to generate the NewMembers. On Windows, this buffer 13250b57cec5SDimitry Andric // could be a mapped view of the file we want to replace (if 13260b57cec5SDimitry Andric // we're updating an existing archive, say). In that case, the 13270b57cec5SDimitry Andric // rename would still succeed, but it would leave behind a 13280b57cec5SDimitry Andric // temporary file (actually the original file renamed) because 13290b57cec5SDimitry Andric // a file cannot be deleted while there's a handle open on it, 13300b57cec5SDimitry Andric // only renamed. So by freeing this buffer, this ensures that 13310b57cec5SDimitry Andric // the last open handle on the destination file, if any, is 13320b57cec5SDimitry Andric // closed before we attempt to rename. 13330b57cec5SDimitry Andric OldArchiveBuf.reset(); 13340b57cec5SDimitry Andric 13350b57cec5SDimitry Andric return Temp->keep(ArcName); 13360b57cec5SDimitry Andric } 13370b57cec5SDimitry Andric 1338e8d8bef9SDimitry Andric Expected<std::unique_ptr<MemoryBuffer>> 13395f757f3fSDimitry Andric writeArchiveToBuffer(ArrayRef<NewArchiveMember> NewMembers, 13405f757f3fSDimitry Andric SymtabWritingMode WriteSymtab, object::Archive::Kind Kind, 1341*0fca6ea1SDimitry Andric bool Deterministic, bool Thin, 1342*0fca6ea1SDimitry Andric function_ref<void(Error)> Warn) { 1343e8d8bef9SDimitry Andric SmallVector<char, 0> ArchiveBufferVector; 1344e8d8bef9SDimitry Andric raw_svector_ostream ArchiveStream(ArchiveBufferVector); 1345e8d8bef9SDimitry Andric 1346*0fca6ea1SDimitry Andric if (Error E = 1347*0fca6ea1SDimitry Andric writeArchiveToStream(ArchiveStream, NewMembers, WriteSymtab, Kind, 1348*0fca6ea1SDimitry Andric Deterministic, Thin, std::nullopt, Warn)) 1349e8d8bef9SDimitry Andric return std::move(E); 1350e8d8bef9SDimitry Andric 1351e8d8bef9SDimitry Andric return std::make_unique<SmallVectorMemoryBuffer>( 13520eae32dcSDimitry Andric std::move(ArchiveBufferVector), /*RequiresNullTerminator=*/false); 1353e8d8bef9SDimitry Andric } 1354e8d8bef9SDimitry Andric 13550b57cec5SDimitry Andric } // namespace llvm 1356