39#if !defined(_MSC_VER) && !defined(__MINGW32__)
48 std::map<std::string, uint16_t>
Map;
53 MemberName(BufRef.getBufferIdentifier()) {}
56 auto MemBufferRef = this->
Buf->getMemBufferRef();
61 return isa<object::MachOObjectFile>(**OptionalObject)
63 : (isa<object::XCOFFObjectFile>(**OptionalObject)
76 auto &IRObject = cast<object::IRObjectFile>(**ObjOrErr);
98 M.MemberName = M.Buf->getBufferIdentifier();
102 return ModTimeOrErr.takeError();
103 M.ModTime = ModTimeOrErr.get();
107 M.UID = UIDOrErr.
get();
111 M.GID = GIDOrErr.
get();
113 if (!AccessModeOrErr)
115 M.Perms = AccessModeOrErr.
get();
121 bool Deterministic) {
125 return FDOrErr.takeError();
140 if (!MemberBufferOrErr)
147 M.Buf = std::move(*MemberBufferOrErr);
148 M.MemberName = M.Buf->getBufferIdentifier();
149 if (!Deterministic) {
150 M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
151 Status.getLastModificationTime());
153 M.GID =
Status.getGroup();
154 M.Perms =
Status.permissions();
163 unsigned SizeSoFar =
OS.
tell() - OldPos;
164 assert(SizeSoFar <=
Size &&
"Data doesn't fit in Size");
208 unsigned UID,
unsigned GID,
unsigned Perms,
uint64_t Size) {
224 unsigned UID,
unsigned GID,
unsigned Perms,
233 unsigned UID,
unsigned GID,
unsigned Perms,
uint64_t Size) {
237 unsigned NameWithPadding =
Name.size() + Pad;
240 NameWithPadding +
Size);
243 Out.
write(uint8_t(0));
249 unsigned UID,
unsigned GID,
unsigned Perms,
251 unsigned NextOffset) {
252 unsigned NameLen =
Name.size();
266 Out.
write(uint8_t(0));
272 return Thin ||
Name.size() >= 16 ||
Name.contains(
'/');
304 NamePos = StringTable.tell();
305 StringTable << M.MemberName <<
"/\n";
308 if (Insertion.second) {
309 Insertion.first->second = StringTable.tell();
310 StringTable << M.MemberName;
314 StringTable <<
"/\n";
316 NamePos = Insertion.first->second;
324 std::vector<unsigned> Symbols;
340 return {{}, std::move(Header),
Names, Pad ?
"\n" :
""};
344 using namespace std::chrono;
347 return time_point_cast<seconds>(system_clock::now());
368 print<uint64_t>(Out, Kind, Val);
370 print<uint32_t>(Out, Kind, Val);
377 assert((OffsetSize == 4 || OffsetSize == 8) &&
"Unsupported OffsetSize");
380 Size += NumSyms * OffsetSize * 2;
382 Size += NumSyms * OffsetSize;
385 Size += StringTableSize;
426 0,
Size, PrevMemberOffset, 0);
440 auto computeSymbolTableHeaderSize = [=] {
444 return TmpBuf.
size();
446 uint32_t HeaderSize = computeSymbolTableHeaderSize();
447 uint64_t Size = strlen(
"!<arch>\n") + HeaderSize + SymtabSize;
452 return Size + StringMemberSize;
464 unsigned NumSyms = 0;
465 for (
const MemberData &M : Members)
466 NumSyms += M.Symbols.size();
471 StringTable.size(), &Pad);
475 printNBits(Out, Kind, NumSyms * 2 * OffsetSize);
480 for (
const MemberData &M : Members) {
481 for (
unsigned StringOffset : M.Symbols) {
486 Pos += M.Header.size() + M.Data.size() + M.Padding.size();
495 Out.
write(uint8_t(0));
507 printLE<uint32_t>(Out, Members.
size());
508 for (
const MemberData &M : Members) {
510 Pos += M.Header.size() + M.Data.size() + M.Padding.size();
513 printLE<uint32_t>(Out,
SymMap.
Map.size());
518 Out << S.first <<
'\0';
521 Out.
write(uint8_t(0));
527 std::vector<unsigned> Ret;
532 std::unique_ptr<object::SymbolicFile> Obj;
542 return ObjOrErr.takeError();
543 Obj = std::move(*ObjOrErr);
547 return ObjOrErr.takeError();
548 Obj = std::move(*ObjOrErr);
558 if (
Error E = S.printName(NameStream))
563 Ret.push_back(SymNames.
tell());
564 SymNames <<
Name <<
'\0';
566 Ret.push_back(SymNames.
tell());
567 if (
Error E = S.printName(SymNames))
580 static char PaddingData[8] = {
'\n',
'\n',
'\n',
'\n',
'\n',
'\n',
'\n',
'\n'};
585 std::vector<MemberData> Ret;
586 bool HasObject =
false;
634 bool UniqueTimestamps = Deterministic &&
isDarwin(Kind);
635 std::map<StringRef, unsigned> FilenameCount;
636 if (UniqueTimestamps) {
638 FilenameCount[M.MemberName]++;
639 for (
auto &Entry : FilenameCount)
640 Entry.second = Entry.second > 1 ? 1 : 0;
645 unsigned PrevOffset = 0,
Index = 0;
659 unsigned MemberPadding =
661 unsigned TailPadding =
666 if (UniqueTimestamps)
674 std::string StringMsg =
675 "File " + M.MemberName.str() +
" exceeds size limit";
676 return make_error<object::GenericBinaryError>(
684 M.Perms,
Size, PrevOffset, NextOffset);
692 std::vector<unsigned> Symbols;
698 Symbols = std::move(*SymbolsOrErr);
701 Pos += Header.size() +
Data.size() + Padding.size();
702 Ret.push_back({std::move(Symbols), std::move(Header),
Data, Padding});
708 SymNames <<
'\0' <<
'\0' <<
'\0';
727 if (!PathToOrErr || !DirFromOrErr)
741 auto FromI = FromTo.first;
742 auto ToI = FromTo.second;
746 for (
auto FromE =
sys::path::end(DirFrom); FromI != FromE; ++FromI)
752 return std::string(Relative.
str());
758 bool Deterministic,
bool Thin) {
759 assert((!Thin || !
isBSDLike(Kind)) &&
"Only the gnu format has a thin mode");
773 StringTable, SymNames, Kind, Thin, Deterministic, WriteSymtab,
777 std::vector<MemberData> &
Data = *DataOrErr;
780 MemberData StringTableMember;
783 StringTableSize = StringTableMember.Header.size() +
784 StringTableMember.Data.size() +
785 StringTableMember.Padding.size();
790 uint64_t LastMemberHeaderOffset = 0;
792 for (
const auto &M :
Data) {
794 LastMemberHeaderOffset = LastMemberEndOffset;
796 LastMemberEndOffset += M.Header.size() + M.Data.size() + M.Padding.size();
797 NumSyms += M.Symbols.size();
800 std::optional<uint64_t> HeadersSize;
807 NumSyms, SymNamesBuf.
size(),
817 uint64_t Sym64Threshold = 1ULL << 32;
818 const char *Sym64Env = std::getenv(
"SYM64_THRESHOLD");
825 if (*HeadersSize + LastMemberHeaderOffset >= Sym64Threshold) {
845 Kind,
Data.size(), StringTableSize, NumSyms, SymNamesBuf.
size(),
855 Out << StringTableMember.Header << StringTableMember.Data
856 << StringTableMember.Padding;
858 for (
const MemberData &M :
Data)
859 Out << M.Header << M.Data << M.Padding;
862 LastMemberEndOffset += *HeadersSize;
863 LastMemberHeaderOffset += *HeadersSize;
867 uint64_t MemberTableNameStrTblSize = 0;
868 std::vector<size_t> MemberOffsets;
869 std::vector<StringRef> MemberNames;
874 MemberTableNameStrTblSize += Member.MemberName.size() + 1;
875 MemberOffsets.push_back(MemberEndOffset);
876 MemberNames.push_back(Member.MemberName);
881 alignTo(Member.MemberName.size(), 2);
885 unsigned MemberTableSize = 20 +
886 20 * MemberOffsets.size() +
887 MemberTableNameStrTblSize;
889 unsigned GlobalSymbolOffset =
890 (WriteSymtab && NumSyms > 0)
891 ? LastMemberEndOffset +
913 for (
const MemberData &M :
Data) {
914 Out << M.Header << M.Data;
915 if (M.Data.size() % 2)
919 if (NewMembers.
size()) {
922 MemberTableSize, LastMemberHeaderOffset,
925 for (
uint64_t MemberOffset : MemberOffsets)
929 Out << MemberName <<
'\0';
931 if (MemberTableNameStrTblSize % 2)
935 if (WriteSymtab && NumSyms > 0)
937 *HeadersSize, LastMemberEndOffset);
946 bool Deterministic,
bool Thin,
947 std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
955 Deterministic, Thin)) {
956 if (
Error DiscardError = Temp->discard())
957 return joinErrors(std::move(
E), std::move(DiscardError));
971 OldArchiveBuf.reset();
973 return Temp->keep(ArcName);
984 Kind, Deterministic, Thin))
987 return std::make_unique<SmallVectorMemoryBuffer>(
988 std::move(ArchiveBufferVector),
false);
This file defines the StringMap class.
static void printNBits(raw_ostream &Out, object::Archive::Kind Kind, uint64_t Val)
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
static bool isDarwin(object::Archive::Kind Kind)
static uint64_t computeHeadersSize(object::Archive::Kind Kind, uint64_t NumMembers, uint64_t StringMemberSize, uint64_t NumSyms, uint64_t SymNamesSize, SymMap *SymMap)
static bool isBSDLike(object::Archive::Kind Kind)
static void printBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size)
static void writeSymbolMap(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef< MemberData > Members, SymMap &SymMap, uint64_t MembersOffset)
static MemberData computeStringTable(StringRef Names)
static void printBigArchiveMemberHeader(raw_ostream &Out, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size, unsigned PrevOffset, unsigned NextOffset)
static bool isAIXBigArchive(object::Archive::Kind Kind)
static void printRestOfMemberHeader(raw_ostream &Out, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size)
static uint64_t computeSymbolTableSize(object::Archive::Kind Kind, uint64_t NumSyms, uint64_t OffsetSize, uint64_t StringTableSize, uint32_t *Padding=nullptr)
static bool isArchiveSymbol(const object::BasicSymbolRef &S)
static Expected< std::vector< unsigned > > getSymbols(MemoryBufferRef Buf, uint16_t Index, raw_ostream &SymNames, SymMap *SymMap, bool &HasObject)
static bool isCOFFArchive(object::Archive::Kind Kind)
static Expected< std::vector< MemberData > > computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames, object::Archive::Kind Kind, bool Thin, bool Deterministic, bool NeedSymbols, SymMap *SymMap, ArrayRef< NewArchiveMember > NewMembers)
static void printMemberHeader(raw_ostream &Out, uint64_t Pos, raw_ostream &StringTable, StringMap< uint64_t > &MemberNames, object::Archive::Kind Kind, bool Thin, const NewArchiveMember &M, sys::TimePoint< std::chrono::seconds > ModTime, uint64_t Size)
static uint64_t computeSymbolMapSize(uint64_t NumObj, SymMap &SymMap, uint32_t *Padding=nullptr)
static void printLE(raw_ostream &Out, T Val)
static void printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size)
static bool useStringTable(bool Thin, StringRef Name)
static bool is64BitKind(object::Archive::Kind Kind)
static void writeSymbolTableHeader(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, uint64_t Size, uint64_t PrevMemberOffset=0)
static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef< MemberData > Members, StringRef StringTable, uint64_t MembersOffset, uint64_t PrevMemberOffset=0)
static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size)
BlockVerifier::State From
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Represents either an error or a value T.
std::error_code getError() const
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
reference get()
Returns a reference to the stored T value.
This is an important class for using LLVM in a threaded context.
size_t getBufferSize() const
StringRef getBuffer() const
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
StringRef str() const
Explicit conversion to StringRef.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
StringRef - Represent a constant reference to a string, i.e.
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Triple - Helper class for working with autoconf configuration names.
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, or DriverKit).
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
Expected< unsigned > getGID() const
Expected< MemoryBufferRef > getMemoryBufferRef() const
Expected< unsigned > getUID() const
Expected< sys::fs::perms > getAccessMode() const
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
static const uint64_t MaxMemberSize
Size field is 10 decimal digits long.
static object::Archive::Kind getDefaultKindForHost()
This is a value type class that represents a single symbol in the list of symbols in the object file.
Expected< uint32_t > getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context, bool InitContent=true)
static bool isSymbolicFile(file_magic Type, const LLVMContext *Context)
A raw_ostream that writes to a file descriptor.
This class implements an extremely fast bulk output stream that can only output to a stream.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & write(unsigned char C)
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
A raw_ostream that writes to an std::string.
A raw_ostream that writes to an SmallVector or SmallString.
static Expected< TempFile > create(const Twine &Model, unsigned Mode=all_read|all_write, OpenFlags ExtraFlags=OF_None)
This creates a temporary file with createUniqueFile and schedules it for deletion with sys::RemoveFil...
Represents the result of a call to sys::fs::status().
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
void make_absolute(const Twine ¤t_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
const file_t kInvalidFile
std::error_code closeFile(file_t &F)
Close the file object.
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
const_iterator end(StringRef path)
Get end iterator over path.
bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
In-place remove any '.
StringRef root_name(StringRef path, Style style=Style::native)
Get root name.
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
This is an optimization pass for GlobalISel generic memory operations.
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Expected< std::unique_ptr< MemoryBuffer > > writeArchiveToBuffer(ArrayRef< NewArchiveMember > NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin)
std::error_code make_error_code(BitcodeError E)
static Error writeArchiveToStream(raw_ostream &Out, ArrayRef< NewArchiveMember > NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin)
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
Error joinErrors(Error E1, Error E2)
Concatenate errors.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Error writeArchive(StringRef ArcName, ArrayRef< NewArchiveMember > NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Expected< std::string > computeArchiveRelativePath(StringRef From, StringRef To)
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
static ErrorOr< SmallString< 128 > > canonicalizePath(StringRef P)
void consumeError(Error Err)
Consume a Error without doing anything.
std::map< std::string, uint16_t > Map
This struct is a compact representation of a valid (non-zero power of two) alignment.
object::Archive::Kind detectKindFromObject() const
NewArchiveMember()=default
static Expected< NewArchiveMember > getFile(StringRef FileName, bool Deterministic)
static Expected< NewArchiveMember > getOldMember(const object::Archive::Child &OldMember, bool Deterministic)
std::unique_ptr< MemoryBuffer > Buf
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...