36#include <system_error>
42void Archive::anchor() {}
45 std::string StringMsg =
"truncated or malformed archive (" + Msg.
str() +
")";
53 StringRef Msg(
"remaining size of archive too small for next archive "
65template <
class T, std::
size_t N>
89 return reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
96 const char *RawHeaderPtr,
100 if (RawHeaderPtr ==
nullptr)
110 bool ValidTerminator =
112 ? (
ArMemHdr->Terminator[0] ==
'\x79' &&
115 if (!ValidTerminator) {
121 std::string Msg(
"terminator characters in archive member \"" + Buf +
122 "\" not the correct \"`\\n\" values for the archive "
138 const char *RawHeaderPtr,
142 if (RawHeaderPtr ==
nullptr)
148 *Err =
malformedError(
"malformed AIX big archive: remaining buffer is "
149 "unable to contain next archive member");
156 *Err = std::move(SubErr);
168 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
169 return malformedError(
"name contains a leading space for archive member "
170 "header at offset " +
195 " field in archive member header are not "
196 "all decimal numbers: '" +
199 "member header at offset " +
213 " field in archive member header are not "
214 "all octal numbers: '" +
217 "member header at offset " +
237 if (!NameStringWithNameTerminator.
ends_with(NameTerminator)) {
239 reinterpret_cast<const char *
>(
ArMemHdr->Name + NameLenWithPadding) -
243 "name does not have name terminator \"`\\n\" for archive member"
244 "header at offset " +
259 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
260 return malformedError(
"archive header truncated before the name field "
261 "for archive member header at offset " +
262 Twine(ArchiveOffset));
272 if (Name[0] ==
'/') {
273 if (Name.size() == 1)
275 if (Name.size() == 2 && Name[1] ==
'/')
279 if (Name ==
"/<XFGHASHMAP>/")
283 if (Name ==
"/<ECSYMBOLS>/")
287 std::size_t StringOffset;
288 if (Name.substr(1).rtrim(
' ').getAsInteger(10, StringOffset)) {
293 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
294 return malformedError(
"long name offset characters after the '/' are "
295 "not all decimal numbers: '" +
296 Buf +
"' for archive member header at offset " +
297 Twine(ArchiveOffset));
301 if (StringOffset >=
Parent->getStringTable().size()) {
303 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
305 " past the end of the string table for archive "
306 "member header at offset " +
307 Twine(ArchiveOffset));
313 size_t End =
Parent->getStringTable().find(
'\n', StringOffset);
315 Parent->getStringTable()[End - 1] !=
'/') {
317 Twine(StringOffset) +
" not terminated");
319 return Parent->getStringTable().slice(StringOffset, End - 1);
321 return Parent->getStringTable().begin() + StringOffset;
324 if (Name.starts_with(
"#1/")) {
326 if (Name.substr(3).rtrim(
' ').getAsInteger(10, NameLength)) {
331 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
332 return malformedError(
"long name length characters after the #1/ are "
333 "not all decimal numbers: '" +
334 Buf +
"' for archive member header at offset " +
335 Twine(ArchiveOffset));
339 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
341 " extends past the end of the member or archive "
342 "for archive member header at offset " +
343 Twine(ArchiveOffset));
351 if (Name[Name.size() - 1] !=
'/')
352 return Name.rtrim(
' ');
355 return Name.drop_back(1);
377 return *SizeOrErr +
alignTo(*NameLenOrErr, 2);
380template <std::
size_t N>
385 return Dst.str().rtrim(
" ").str();
389 const char *RawHeaderPtr,
427 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
432 *Err =
malformedError(
"name contains a leading space for archive member "
433 "header at offset " +
457 malformedError(
"LastModified field is empty or contains only spaces in "
458 "archive member header at offset " +
466 *Err =
malformedError(
"UID field is empty or contains only spaces in "
467 "archive member header at offset " +
475 *Err =
malformedError(
"GID field is empty or contains only spaces in "
476 "archive member header at offset " +
485 malformedError(
"AccessMode field is empty or contains only spaces in "
486 "archive member header at offset " +
505 if (!AccessModeOrErr)
540 return Parent->isThin() && Name !=
"/" && Name !=
"//" && Name !=
"/SYM64/";
559 const char *NextLoc =
562 if (NextLoc ==
Parent->getMemoryBufferRef().getBufferEnd())
574 if (!NextOffsetOrErr)
576 return Parent->getData().data() + NextOffsetOrErr.
get();
581 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {
582 Header = Parent->createArchiveMemberHeader(Data.data(), Data.size(),
nullptr);
593 Header = Parent->createArchiveMemberHeader(
594 Start, Parent->getData().size() - (Start - Parent->getData().data()),
601 assert(Err &&
"Err can't be nullptr if Start is not a nullptr");
629 StartOfFile = Header->getSizeOf();
641 StartOfFile += ((Name.size() + 1) >> 1) << 1;
642 }
else if (Name.starts_with(
"#1/")) {
644 StringRef RawNameSize = Name.substr(3).rtrim(
' ');
647 *Err =
malformedError(
"long name length characters after the #1/ are "
648 "not all decimal numbers: '" +
650 "' for archive member header at offset " +
660 return Header->getSize();
661 return Data.size() - StartOfFile;
665 return Header->getSize();
668Expected<bool> Archive::Child::isThinMember()
const {
return Header->isThin(); }
673 return isThin.takeError();
680 return std::string(Name);
683 Parent->getMemoryBufferRef().getBufferIdentifier());
685 return std::string(FullName);
696 return Size.takeError();
702 const std::string &FullName = *FullNameOrErr;
705 if (std::error_code EC = Buf.
getError())
707 Parent->ThinBuffers.push_back(std::move(*Buf));
708 return Parent->ThinBuffers.back()->getBuffer();
716 const char *NextLoc = *NextLocOrErr;
719 if (NextLoc ==
nullptr)
720 return Child(
nullptr,
nullptr,
nullptr);
723 if (NextLoc > Parent->Data.getBufferEnd()) {
724 std::string Msg(
"offset to next archive member past the end of the archive "
736 Child Ret(Parent, NextLoc, &Err);
738 return std::move(Err);
743 const char *a = Parent->Data.getBuffer().data();
744 const char *c = Data.data();
755 Header->getName(Header->getSizeOf() + RawSize);
781 return std::move(*BinaryOrErr);
782 return BinaryOrErr.takeError();
787 std::unique_ptr<Archive> Ret;
791 Ret = std::make_unique<BigArchive>(Source, Err);
793 Ret = std::make_unique<ZOSArchive>(Source, Err);
795 Ret = std::make_unique<Archive>(Source, Err);
798 return std::move(Err);
799 return std::move(Ret);
802std::unique_ptr<AbstractArchiveMemberHeader>
808 return std::make_unique<ZOSArchiveMemberHeader>(
this, RawHeaderPtr,
Size,
811 return std::make_unique<ArchiveMemberHeader>(
this, RawHeaderPtr,
Size, Err);
812 return std::make_unique<BigArchiveMemberHeader>(
this, RawHeaderPtr,
Size,
827 FirstRegularData =
C.Data;
828 FirstRegularStartOfFile =
C.StartOfFile;
908 if (Name ==
"__.SYMDEF" || Name ==
"__.SYMDEF_64") {
909 if (Name ==
"__.SYMDEF")
929 if (Name.starts_with(
"#1/")) {
937 Name = NameOrErr.
get();
938 if (Name ==
"__.SYMDEF SORTED" || Name ==
"__.SYMDEF") {
949 }
else if (Name ==
"__.SYMDEF_64 SORTED" || Name ==
"__.SYMDEF_64") {
971 bool has64SymTable =
false;
972 if (Name ==
"/" || Name ==
"/SYM64/") {
981 if (Name ==
"/SYM64/")
982 has64SymTable =
true;
995 Name = NameOrErr.
get();
1015 if (Name[0] !=
'/') {
1046 NameOrErr =
C->getRawName();
1051 Name = NameOrErr.
get();
1071 NameOrErr =
C->getRawName();
1076 Name = NameOrErr.
get();
1079 if (Name ==
"/<ECSYMBOLS>/") {
1103 if (
T.isOSWindows())
1116 bool SkipInternal)
const {
1122 Child(
this, FirstRegularData, FirstRegularStartOfFile), Err);
1138 uint32_t SymbolCount = Parent->getNumberOfSymbols();
1139 return SymbolCount <= SymbolIndex &&
1140 SymbolIndex < SymbolCount + Parent->getNumberOfECSymbols();
1145 return Parent->ECSymbolTable.
begin() + StringIndex;
1146 return Parent->getSymbolTable().begin() + StringIndex;
1150 const char *Buf = Parent->getSymbolTable().begin();
1151 const char *Offsets = Buf;
1158 if (Parent->kind() ==
K_GNU) {
1162 }
else if (Parent->kind() ==
K_BSD) {
1178 }
else if (Parent->kind() ==
K_ZOS) {
1187 Buf += MemberCount * 4 + 4;
1191 if (SymbolIndex < SymbolCount) {
1193 const char *Indices = Buf + 4;
1197 OffsetIndex =
read16le(Indices + SymbolIndex * 2);
1200 const char *Indices = Parent->ECSymbolTable.begin() + 4;
1204 OffsetIndex =
read16le(Indices + (SymbolIndex - SymbolCount) * 2);
1211 if (OffsetIndex >= MemberCount)
1217 const char *
Loc = Parent->getData().begin() +
Offset;
1221 return std::move(Err);
1227 if (Parent->kind() ==
K_BSD) {
1241 const char *Buf = Parent->getSymbolTable().begin();
1247 if (t.SymbolIndex + 1 < RanlibCount) {
1248 const char *Ranlibs = Buf + 4;
1251 CurRanStrx =
read32le(Ranlibs + t.SymbolIndex * 8);
1252 NextRanStrx =
read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
1253 t.StringIndex -= CurRanStrx;
1254 t.StringIndex += NextRanStrx;
1258 t.StringIndex = Parent->ECSymbolTable.find(
'\0', t.StringIndex) + 1;
1261 t.StringIndex = Parent->getSymbolTable().find(
'\0', t.StringIndex) + 1;
1288 const char *ranlibs = buf + 4;
1304 const char *ranlibs = buf + 8;
1326 buf += 4 + (member_count * 4);
1328 buf += 4 + (symbol_count * 2);
1355 Twine(StringIndex));
1364 if (Index > MemberCount)
1366 " is larger than member count " +
1367 Twine(MemberCount));
1371 return malformedError(
"malformed EC symbol names: not null-terminated");
1399 buf += 4 + (member_count * 4);
1413 for (; bs != es; ++bs) {
1415 if (SymName ==
name) {
1417 return Child(*MemberOrErr);
1419 return MemberOrErr.takeError();
1422 return std::nullopt;
1434 const char *&GlobalSymtabLoc,
1437 uint64_t GlobalSymtabContentOffset =
1439 if (GlobalSymtabContentOffset > BufferSize)
1441 Twine(BitMessage) +
" global symbol table header at offset 0x" +
1444 " goes past the end of file");
1446 GlobalSymtabLoc =
Data.getBufferStart() + GlobalSymtabOffset;
1452 RawOffset +
"\" is not a number");
1454 if (GlobalSymtabContentOffset +
Size > BufferSize)
1456 Twine(BitMessage) +
" global symbol table content at offset 0x" +
1484 unsigned SymOffsetsSize = 8 * (SymNum + 1);
1485 uint64_t SymbolTableStringSize =
Size - SymOffsetsSize;
1499 Err =
malformedError(
"malformed AIX big archive: incomplete fixed length "
1500 "header, the archive is only" +
1501 Twine(BufferSize) +
" byte(s)");
1508 Err =
malformedError(
"malformed AIX big archive: first member offset \"" +
1509 RawOffset +
"\" is not a number");
1514 Err =
malformedError(
"malformed AIX big archive: last member offset \"" +
1515 RawOffset +
"\" is not a number");
1521 "offset of 32-bit members \"" +
1522 RawOffset +
"\" is not a number");
1530 "offset of 64-bit members\"" +
1531 RawOffset +
"\" is not a number");
1535 const char *GlobSymtab32Loc =
nullptr;
1536 const char *GlobSymtab64Loc =
nullptr;
1541 if (GlobSymtab32Offset) {
1544 GlobSymtab32Loc, GlobSymtab32Size,
"32-bit");
1551 if (GlobSymtab64Offset) {
1554 GlobSymtab64Loc, GlobSymtab64Size,
"64-bit");
1563 if (GlobSymtab32Offset)
1565 if (GlobSymtab64Offset)
1568 if (SymtabInfos.
size() == 1) {
1571 }
else if (SymtabInfos.
size() == 2) {
1575 uint64_t SymNum = SymtabInfos[0].SymNum + SymtabInfos[1].SymNum;
1578 Out << SymtabInfos[0].SymbolOffsetTable;
1579 Out << SymtabInfos[1].SymbolOffsetTable;
1581 Out << SymtabInfos[0].StringTable;
1582 Out << SymtabInfos[1].StringTable;
1586 SymtabInfos[0].StringTable.
size() +
1587 SymtabInfos[1].StringTable.
size());
1626 if (Name ==
"__.SYMDEF") {
1634 "z/OS archive symbol table is too small to read the symbol count, "
1635 "symbol table size is " +
1642 if (OffsetToEbcdicNames > EbcdicSymbolTable.
size()) {
1644 Twine(OffsetToEbcdicNames) +
1645 " exceeds symbol table size " +
1649 uint64_t EbcdicNamesSize = EbcdicSymbolTable.
size() - OffsetToEbcdicNames;
1650 const char *EbcdicNamesPtr = EbcdicSymbolTable.
data() + OffsetToEbcdicNames;
1651 StringRef EbcdicNames(EbcdicNamesPtr, EbcdicNamesSize);
1655 SymbolTableBuf.append(EbcdicSymbolTable.
data(), OffsetToEbcdicNames);
1656 SymbolTableBuf.append(Dst.str());
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file provides utility functions for converting between EBCDIC-1047 and UTF-8.
Provides ErrorOr<T> smart pointer.
#define offsetof(TYPE, MEMBER)
std::string ebcdicFieldToASCII(const char(&Field)[N])
static void appendGlobalSymbolTableInfo(SmallVector< GlobalSymtabInfo > &SymtabInfos, const char *GlobalSymtabLoc, uint64_t Size)
static Error getGlobalSymtabLocAndSize(const MemoryBufferRef &Data, uint64_t GlobalSymtabOffset, const char *&GlobalSymtabLoc, uint64_t &Size, const char *BitMessage)
static Error malformedError(Twine Msg)
StringRef getFieldRawString(const T(&Field)[N])
Expected< uint64_t > getArchiveMemberOctField(Twine FieldName, const StringRef RawField, const Archive *Parent, const AbstractArchiveMemberHeader *MemHeader)
Expected< uint64_t > getArchiveMemberDecField(Twine FieldName, const StringRef RawField, const Archive *Parent, const AbstractArchiveMemberHeader *MemHeader)
static Error createMemberHeaderParseError(const AbstractArchiveMemberHeader *ArMemHeader, const char *RawHeaderPtr, uint64_t Size)
OptimizedStructLayoutField Field
static StringRef getName(Value *V)
This file defines the SmallString class.
static unsigned getSize(unsigned Kind)
Helper for Errors used as out-parameters.
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.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
static constexpr size_t npos
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
Check if the string is empty.
constexpr size_t size() const
Get the string size.
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
A table of densely packed, null-terminated strings indexed by offset.
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
static Twine utohexstr(uint64_t Val)
LLVM Value Representation.
static fallible_iterator end(ChildFallibleIterator I)
static fallible_iterator itr(ChildFallibleIterator I, Error &Err)
LLVM_ABI Expected< StringRef > getBuffer() const
LLVM_ABI Expected< Child > getNext() const
LLVM_ABI Expected< std::string > getFullName() const
LLVM_ABI uint64_t getChildOffset() const
LLVM_ABI Expected< uint64_t > getRawSize() const
LLVM_ABI Expected< StringRef > getName() const
LLVM_ABI Expected< uint64_t > getSize() const
Expected< StringRef > getRawName() const
LLVM_ABI Expected< std::unique_ptr< Binary > > getAsBinary(LLVMContext *Context=nullptr) const
LLVM_ABI Expected< MemoryBufferRef > getMemoryBufferRef() const
LLVM_ABI Child(const Archive *Parent, const char *Start, Error *Err)
LLVM_ABI Symbol getNext() const
Symbol(const Archive *p, uint32_t symi, uint32_t stri)
LLVM_ABI Expected< Child > getMember() const
LLVM_ABI StringRef getName() const
LLVM_ABI bool isECSymbol() const
std::unique_ptr< AbstractArchiveMemberHeader > createArchiveMemberHeader(const char *RawHeaderPtr, uint64_t Size, Error *Err) const
symbol_iterator symbol_begin() const
virtual uint64_t getFirstChildOffset() const
StringRef getStringTable() const
uint32_t getNumberOfSymbols() const
fallible_iterator< ChildFallibleIterator > child_iterator
uint32_t getNumberOfECSymbols() const
void setFirstRegular(const Child &C)
uint64_t getArchiveMagicLen() const
StringRef getSymbolTable() const
symbol_iterator symbol_end() const
static object::Archive::Kind getDefaultKind()
virtual bool isEmpty() const
child_iterator child_end() const
bool hasSymbolTable() const
Archive(MemoryBufferRef Source, Error &Err)
static object::Archive::Kind getDefaultKindForTriple(const Triple &T)
Expected< iterator_range< symbol_iterator > > ec_symbols() const
Expected< std::optional< Child > > findSym(StringRef name) const
child_iterator child_begin(Error &Err, bool SkipInternal=true) const
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
bool Has32BitGlobalSymtab
std::string MergedGlobalSymtabBuf
bool Has64BitGlobalSymtab
const FixLenHdr * ArFixLenHdr
uint64_t FirstChildOffset
LLVM_ABI BigArchive(MemoryBufferRef Source, Error &Err)
StringRef getData() const
Binary(unsigned int Type, MemoryBufferRef Source)
MemoryBufferRef getMemoryBufferRef() const
ZOSArchive(MemoryBufferRef Source, Error &Err)
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
A raw_ostream that writes to an std::string.
@ C
The default llvm calling convention, compatible with C.
LLVM_ABI void convertToUTF8(StringRef Source, SmallVectorImpl< char > &Result)
constexpr size_t NameSize
const char ArchiveMagic[]
const char ZOSArchiveMagic[]
const char ThinArchiveMagic[]
const char BigArchiveMagic[]
LLVM_ABI Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
uint64_t read64le(const void *P)
uint16_t read16le(const void *P)
uint64_t read64be(const void *P)
uint32_t read32be(const void *P)
uint32_t read32le(const void *P)
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
LLVM_ABI bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
LLVM_ABI std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
This is an optimization pass for GlobalISel generic memory operations.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
FunctionAddr VTableAddr Count
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
@ Increment
Incrementally increasing token ID.
void consumeError(Error Err)
Consume a Error without doing anything.
LLVM_ABI Error write(DWPWriter &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue, raw_pwrite_stream *OS=nullptr)
StringRef SymbolOffsetTable