40#if !defined(_MSC_VER) && !defined(__MINGW32__)
51 std::map<std::string, uint16_t>
Map;
52 std::map<std::string, uint16_t>
ECMap;
57 MemberName(BufRef.getBufferIdentifier()) {}
60 auto MemBufferRef = this->
Buf->getMemBufferRef();
65 return isa<object::MachOObjectFile>(**OptionalObject)
67 : (isa<object::XCOFFObjectFile>(**OptionalObject)
80 auto &IRObject = cast<object::IRObjectFile>(**ObjOrErr);
81 auto TargetTriple =
Triple(IRObject.getTargetTriple());
82 return TargetTriple.isOSDarwin()
104 M.MemberName = M.Buf->getBufferIdentifier();
105 if (!Deterministic) {
108 return ModTimeOrErr.takeError();
109 M.ModTime = ModTimeOrErr.get();
113 M.UID = UIDOrErr.
get();
117 M.GID = GIDOrErr.
get();
119 if (!AccessModeOrErr)
121 M.Perms = AccessModeOrErr.
get();
127 bool Deterministic) {
131 return FDOrErr.takeError();
146 if (!MemberBufferOrErr)
153 M.Buf = std::move(*MemberBufferOrErr);
154 M.MemberName = M.Buf->getBufferIdentifier();
155 if (!Deterministic) {
156 M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
157 Status.getLastModificationTime());
159 M.GID =
Status.getGroup();
160 M.Perms =
Status.permissions();
169 unsigned SizeSoFar =
OS.
tell() - OldPos;
170 assert(SizeSoFar <=
Size &&
"Data doesn't fit in Size");
214 unsigned UID,
unsigned GID,
unsigned Perms,
uint64_t Size) {
230 unsigned UID,
unsigned GID,
unsigned Perms,
239 unsigned UID,
unsigned GID,
unsigned Perms,
uint64_t Size) {
243 unsigned NameWithPadding =
Name.size() + Pad;
246 NameWithPadding +
Size);
249 Out.
write(uint8_t(0));
255 unsigned UID,
unsigned GID,
unsigned Perms,
258 unsigned NameLen =
Name.size();
272 Out.
write(uint8_t(0));
278 return Thin ||
Name.size() >= 16 ||
Name.contains(
'/');
310 NamePos = StringTable.tell();
311 StringTable << M.MemberName <<
"/\n";
314 if (Insertion.second) {
315 Insertion.first->second = StringTable.tell();
316 StringTable << M.MemberName;
320 StringTable <<
"/\n";
322 NamePos = Insertion.first->second;
330 std::vector<unsigned> Symbols;
346 return {{}, std::move(Header),
Names, Pad ?
"\n" :
""};
350 using namespace std::chrono;
353 return time_point_cast<seconds>(system_clock::now());
374 print<uint64_t>(Out,
Kind, Val);
376 print<uint32_t>(Out,
Kind, Val);
383 assert((OffsetSize == 4 || OffsetSize == 8) &&
"Unsupported OffsetSize");
386 Size += NumSyms * OffsetSize * 2;
388 Size += NumSyms * OffsetSize;
391 Size += StringTableSize;
447 PrevMemberOffset, NextMemberOffset);
461 auto computeSymbolTableHeaderSize = [=] {
465 return TmpBuf.
size();
467 uint32_t HeaderSize = computeSymbolTableHeaderSize();
468 uint64_t Size = strlen(
"!<arch>\n") + HeaderSize + SymtabSize;
476 return Size + StringMemberSize;
489 return ObjOrErr.takeError();
490 return std::move(*ObjOrErr);
494 return ObjOrErr.takeError();
495 return std::move(*ObjOrErr);
513 return (*ObjOrErr)->is64Bit();
519 unsigned NumSyms,
uint64_t PrevMemberOffset = 0,
521 bool Is64Bit =
false) {
530 StringTable.size(), &Pad);
540 for (
const MemberData &M : Members) {
548 if (*Is64BitOrErr != Is64Bit) {
549 Pos += M.Header.size() + M.Data.size() + M.Padding.size();
554 for (
unsigned StringOffset : M.Symbols) {
559 Pos += M.Header.size() + M.Data.size() + M.Padding.size();
568 Out.
write(uint8_t(0));
580 printLE<uint32_t>(Out, Members.
size());
581 for (
const MemberData &M : Members) {
583 Pos += M.Header.size() + M.Data.size() + M.Padding.size();
586 printLE<uint32_t>(Out,
SymMap.
Map.size());
591 Out << S.first <<
'\0';
594 Out.
write(uint8_t(0));
610 Out << S.first <<
'\0';
612 Out.
write(uint8_t(0));
617 return cast<llvm::object::COFFObjectFile>(&Obj)->getMachine() !=
639 std::vector<unsigned> Ret;
649 std::unique_ptr<object::SymbolicFile> Obj = std::move(*ObjOrErr);
651 std::map<std::string, uint16_t> *Map =
nullptr;
661 if (
Error E = S.printName(NameStream))
663 if (Map->find(
Name) != Map->end())
667 Ret.push_back(SymNames.
tell());
668 SymNames <<
Name <<
'\0';
671 Ret.push_back(SymNames.
tell());
672 if (
Error E = S.printName(SymNames))
685 static char PaddingData[8] = {
'\n',
'\n',
'\n',
'\n',
'\n',
'\n',
'\n',
'\n'};
690 std::vector<MemberData> Ret;
691 bool HasObject =
false;
739 bool UniqueTimestamps = Deterministic &&
isDarwin(
Kind);
740 std::map<StringRef, unsigned> FilenameCount;
741 if (UniqueTimestamps) {
743 FilenameCount[M.MemberName]++;
744 for (
auto &Entry : FilenameCount)
745 Entry.second = Entry.second > 1 ? 1 : 0;
765 unsigned MemberPadding =
767 unsigned TailPadding =
772 if (UniqueTimestamps)
780 std::string StringMsg =
781 "File " + M.MemberName.str() +
" exceeds size limit";
782 return make_error<object::GenericBinaryError>(
790 M.Perms,
Size, PrevOffset, NextOffset);
798 std::vector<unsigned> Symbols;
804 Symbols = std::move(*SymbolsOrErr);
807 Pos += Header.size() +
Data.size() + Padding.size();
808 Ret.push_back({std::move(Symbols), std::move(Header),
Data, Padding});
814 SymNames <<
'\0' <<
'\0' <<
'\0';
833 if (!PathToOrErr || !DirFromOrErr)
847 auto FromI = FromTo.first;
848 auto ToI = FromTo.second;
852 for (
auto FromE =
sys::path::end(DirFrom); FromI != FromE; ++FromI)
858 return std::string(Relative.
str());
864 bool Deterministic,
bool Thin,
bool IsEC) {
880 StringTable, SymNames,
Kind, Thin, Deterministic, WriteSymtab,
884 std::vector<MemberData> &
Data = *DataOrErr;
887 MemberData StringTableMember;
890 StringTableSize = StringTableMember.Header.size() +
891 StringTableMember.Data.size() +
892 StringTableMember.Padding.size();
897 uint64_t LastMemberHeaderOffset = 0;
901 for (
const auto &M :
Data) {
903 LastMemberHeaderOffset = LastMemberEndOffset;
905 LastMemberEndOffset += M.Header.size() + M.Data.size() + M.Padding.size();
906 NumSyms += M.Symbols.size();
919 NumSyms32 += M.Symbols.size();
923 std::optional<uint64_t> HeadersSize;
930 NumSyms, SymNamesBuf.
size(),
940 uint64_t Sym64Threshold = 1ULL << 32;
941 const char *Sym64Env = std::getenv(
"SYM64_THRESHOLD");
948 if (*HeadersSize + LastMemberHeaderOffset >= Sym64Threshold) {
968 Kind,
Data.size(), StringTableSize, NumSyms, SymNamesBuf.
size(),
971 *HeadersSize, NumSyms);
978 Out << StringTableMember.Header << StringTableMember.Data
979 << StringTableMember.Padding;
984 for (
const MemberData &M :
Data)
985 Out << M.Header << M.Data << M.Padding;
988 LastMemberEndOffset += *HeadersSize;
989 LastMemberHeaderOffset += *HeadersSize;
993 uint64_t MemberTableNameStrTblSize = 0;
994 std::vector<size_t> MemberOffsets;
995 std::vector<StringRef> MemberNames;
1000 MemberTableNameStrTblSize += Member.MemberName.size() + 1;
1001 MemberOffsets.push_back(MemberEndOffset);
1002 MemberNames.push_back(Member.MemberName);
1007 alignTo(Member.MemberName.size(), 2);
1012 20 * MemberOffsets.size() +
1013 MemberTableNameStrTblSize;
1020 if (WriteSymtab && NumSyms)
1030 getSymbols(Buf, 0, *Is64BitOrErr ? SymNames64 : SymNames32,
nullptr,
1037 LastMemberEndOffset +
1044 (WriteSymtab && NumSyms32 > 0) ? MemberTableEndOffset : 0;
1047 uint64_t NumSyms64 = NumSyms - NumSyms32;
1048 if (WriteSymtab && NumSyms64 > 0) {
1049 if (GlobalSymbolOffset == 0)
1050 GlobalSymbolOffset64 = MemberTableEndOffset;
1054 GlobalSymbolOffset64 =
1056 (NumSyms32 + 1) * 8 +
alignTo(SymNamesBuf32.
size(), 2);
1075 for (
const MemberData &M :
Data) {
1076 Out << M.Header << M.Data;
1077 if (M.Data.size() % 2)
1081 if (NewMembers.size()) {
1084 MemberTableSize, LastMemberHeaderOffset,
1085 GlobalSymbolOffset ? GlobalSymbolOffset
1086 : GlobalSymbolOffset64);
1088 for (
uint64_t MemberOffset : MemberOffsets)
1091 for (
StringRef MemberName : MemberNames)
1092 Out << MemberName <<
'\0';
1094 if (MemberTableNameStrTblSize % 2)
1100 if (GlobalSymbolOffset) {
1102 *HeadersSize, NumSyms32, LastMemberEndOffset,
1103 GlobalSymbolOffset64);
1105 if (GlobalSymbolOffset64 && (SymNamesBuf32.
size() % 2))
1110 if (GlobalSymbolOffset64)
1112 *HeadersSize, NumSyms64,
1113 GlobalSymbolOffset ? GlobalSymbolOffset
1114 : LastMemberEndOffset,
1125 bool Deterministic,
bool Thin,
1126 std::unique_ptr<MemoryBuffer> OldArchiveBuf,
bool IsEC) {
1134 Deterministic, Thin, IsEC)) {
1135 if (
Error DiscardError = Temp->discard())
1136 return joinErrors(std::move(
E), std::move(DiscardError));
1150 OldArchiveBuf.reset();
1152 return Temp->keep(ArcName);
1163 Kind, Deterministic, Thin,
false))
1164 return std::move(
E);
1166 return std::make_unique<SmallVectorMemoryBuffer>(
1167 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 computeECSymbolsSize(SymMap &SymMap, uint32_t *Padding=nullptr)
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 writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef< MemberData > Members, StringRef StringTable, uint64_t MembersOffset, unsigned NumSyms, uint64_t PrevMemberOffset=0, uint64_t NextMemberOffset=0, bool Is64Bit=false)
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 bool isECObject(object::SymbolicFile &Obj)
static bool isAIXBigArchive(object::Archive::Kind Kind)
static void writeSymbolTableHeader(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, uint64_t Size, uint64_t PrevMemberOffset=0, uint64_t NextMemberOffset=0)
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 void writeECSymbols(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef< MemberData > Members, SymMap &SymMap)
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 printBigArchiveMemberHeader(raw_ostream &Out, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size, uint64_t PrevOffset, uint64_t NextOffset)
static Expected< std::unique_ptr< SymbolicFile > > getSymbolicFile(MemoryBufferRef Buf, LLVMContext &Context)
static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size)
static Expected< bool > is64BitSymbolicFile(const StringRef &ObjStringRef)
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.
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)
MemoryBufferRef getMemoryBufferRef() const
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.
@ IMAGE_FILE_MACHINE_ARM64
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, bool IsEC)
Expected< std::string > getBitcodeTargetTriple(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the triple information.
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...
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
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.
Error writeArchive(StringRef ArcName, ArrayRef< NewArchiveMember > NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr, bool IsEC=false)
std::map< std::string, uint16_t > ECMap
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)...