36#include <system_error>
39using namespace object;
42void Archive::anchor() {}
45 std::string StringMsg =
"truncated or malformed archive (" + Msg.
str() +
")";
46 return make_error<GenericBinaryError>(std::move(StringMsg),
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)
115 std::string Msg(
"terminator characters in archive member \"" + Buf +
116 "\" not the correct \"`\\n\" values for the archive "
131 const char *RawHeaderPtr,
135 if (RawHeaderPtr ==
nullptr)
141 *Err =
malformedError(
"malformed AIX big archive: remaining buffer is "
142 "unable to contain next archive member");
149 *Err = std::move(SubErr);
162 return malformedError(
"name contains a leading space for archive member "
163 "header at offset " +
188 " field in archive member header are not "
189 "all decimal numbers: '" +
192 "member header at offset " +
206 " field in archive member header are not "
207 "all octal numbers: '" +
210 "member header at offset " +
230 if (!NameStringWithNameTerminator.
ends_with(NameTerminator)) {
232 reinterpret_cast<const char *
>(
ArMemHdr->
Name + NameLenWithPadding) -
236 "name does not have name terminator \"`\\n\" for archive member"
237 "header at offset " +
253 return malformedError(
"archive header truncated before the name field "
254 "for archive member header at offset " +
255 Twine(ArchiveOffset));
265 if (
Name[0] ==
'/') {
266 if (
Name.size() == 1)
268 if (
Name.size() == 2 &&
Name[1] ==
'/')
272 if (
Name ==
"/<XFGHASHMAP>/")
276 if (
Name ==
"/<ECSYMBOLS>/")
280 std::size_t StringOffset;
281 if (
Name.substr(1).rtrim(
' ').getAsInteger(10, StringOffset)) {
288 return malformedError(
"long name offset characters after the '/' are "
289 "not all decimal numbers: '" +
290 Buf +
"' for archive member header at offset " +
291 Twine(ArchiveOffset));
299 " past the end of the string table for archive "
300 "member header at offset " +
301 Twine(ArchiveOffset));
311 Twine(StringOffset) +
"not terminated");
318 if (
Name.starts_with(
"#1/")) {
320 if (
Name.substr(3).rtrim(
' ').getAsInteger(10, NameLength)) {
327 return malformedError(
"long name length characters after the #1/ are "
328 "not all decimal numbers: '" +
329 Buf +
"' for archive member header at offset " +
330 Twine(ArchiveOffset));
336 " extends past the end of the member or archive "
337 "for archive member header at offset " +
338 Twine(ArchiveOffset));
347 return Name.rtrim(
' ');
350 return Name.drop_back(1);
372 return *SizeOrErr +
alignTo(*NameLenOrErr, 2);
388 if (!AccessModeOrErr)
442 const char *NextLoc =
457 if (!NextOffsetOrErr)
464 : Parent(Parent),
Data(
Data), StartOfFile(StartOfFile) {
486 assert(Err &&
"Err can't be nullptr if Start is not a nullptr");
514 StartOfFile = Header->getSizeOf();
526 StartOfFile += ((
Name.size() + 1) >> 1) << 1;
527 }
else if (
Name.starts_with(
"#1/")) {
532 *Err =
malformedError(
"long name length characters after the #1/ are "
533 "not all decimal numbers: '" +
535 "' for archive member header at offset " +
545 return Header->getSize();
546 return Data.size() - StartOfFile;
550 return Header->getSize();
553Expected<bool> Archive::Child::isThinMember()
const {
return Header->isThin(); }
558 return isThin.takeError();
565 return std::string(
Name);
568 Parent->getMemoryBufferRef().getBufferIdentifier());
570 return std::string(FullName);
581 return Size.takeError();
587 const std::string &FullName = *FullNameOrErr;
589 if (std::error_code EC = Buf.
getError())
591 Parent->ThinBuffers.push_back(std::move(*Buf));
592 return Parent->ThinBuffers.back()->getBuffer();
600 const char *NextLoc = *NextLocOrErr;
603 if (NextLoc ==
nullptr)
604 return Child(
nullptr,
nullptr,
nullptr);
607 if (NextLoc > Parent->Data.getBufferEnd()) {
608 std::string Msg(
"offset to next archive member past the end of the archive "
620 Child Ret(Parent, NextLoc, &Err);
622 return std::move(Err);
627 const char *a = Parent->Data.getBuffer().data();
628 const char *c =
Data.data();
639 Header->getName(Header->getSizeOf() + RawSize);
665 return std::move(*BinaryOrErr);
666 return BinaryOrErr.takeError();
671 std::unique_ptr<Archive> Ret;
675 Ret = std::make_unique<BigArchive>(Source, Err);
677 Ret = std::make_unique<Archive>(Source, Err);
680 return std::move(Err);
681 return std::move(Ret);
684std::unique_ptr<AbstractArchiveMemberHeader>
689 return std::make_unique<ArchiveMemberHeader>(
this, RawHeaderPtr,
Size, Err);
690 return std::make_unique<BigArchiveMemberHeader>(
this, RawHeaderPtr,
Size,
705 FirstRegularData =
C.Data;
706 FirstRegularStartOfFile =
C.StartOfFile;
723 Err = make_error<GenericBinaryError>(
"file too small to be an archive",
748 auto Increment = [&]() {
782 if (
Name ==
"__.SYMDEF" ||
Name ==
"__.SYMDEF_64") {
783 if (
Name ==
"__.SYMDEF")
803 if (
Name.starts_with(
"#1/")) {
812 if (
Name ==
"__.SYMDEF SORTED" ||
Name ==
"__.SYMDEF") {
823 }
else if (
Name ==
"__.SYMDEF_64 SORTED" ||
Name ==
"__.SYMDEF_64") {
845 bool has64SymTable =
false;
846 if (
Name ==
"/" ||
Name ==
"/SYM64/") {
855 if (
Name ==
"/SYM64/")
856 has64SymTable =
true;
889 if (
Name[0] !=
'/') {
920 NameOrErr =
C->getRawName();
945 NameOrErr =
C->getRawName();
953 if (
Name ==
"/<ECSYMBOLS>/") {
988 bool SkipInternal)
const {
994 Child(
this, FirstRegularData, FirstRegularStartOfFile), Err);
1011 return SymbolCount <= SymbolIndex &&
1017 return Parent->ECSymbolTable.
begin() + StringIndex;
1018 return Parent->getSymbolTable().begin() + StringIndex;
1022 const char *Buf = Parent->getSymbolTable().begin();
1023 const char *Offsets = Buf;
1030 if (Parent->kind() ==
K_GNU) {
1034 }
else if (Parent->kind() ==
K_BSD) {
1053 Buf += MemberCount * 4 + 4;
1057 if (SymbolIndex < SymbolCount) {
1059 const char *Indices = Buf + 4;
1063 OffsetIndex =
read16le(Indices + SymbolIndex * 2);
1064 }
else if (isECSymbol()) {
1066 const char *Indices = Parent->ECSymbolTable.begin() + 4;
1070 OffsetIndex =
read16le(Indices + (SymbolIndex - SymbolCount) * 2);
1077 if (OffsetIndex >= MemberCount)
1083 const char *Loc = Parent->getData().begin() +
Offset;
1085 Child C(Parent, Loc, &Err);
1087 return std::move(Err);
1093 if (Parent->kind() ==
K_BSD) {
1107 const char *Buf = Parent->getSymbolTable().begin();
1113 if (t.SymbolIndex + 1 < RanlibCount) {
1114 const char *Ranlibs = Buf + 4;
1117 CurRanStrx =
read32le(Ranlibs + t.SymbolIndex * 8);
1118 NextRanStrx =
read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
1119 t.StringIndex -= CurRanStrx;
1120 t.StringIndex += NextRanStrx;
1124 t.StringIndex = Parent->ECSymbolTable.find(
'\0', t.StringIndex) + 1;
1127 t.StringIndex = Parent->getSymbolTable().find(
'\0', t.StringIndex) + 1;
1154 const char *ranlibs = buf + 4;
1170 const char *ranlibs = buf + 8;
1183 buf += 4 + (member_count * 4);
1185 buf += 4 + (symbol_count * 2);
1212 Twine(StringIndex));
1217 for (
uint32_t i = 0; i < Count; ++i) {
1221 if (
Index > MemberCount)
1223 " is larger than member count " +
1224 Twine(MemberCount));
1228 return malformedError(
"malformed EC symbol names: not null-terminated");
1254 buf += 4 + (member_count * 4);
1268 for (; bs != es; ++bs) {
1270 if (SymName ==
name) {
1272 return Child(*MemberOrErr);
1274 return MemberOrErr.takeError();
1277 return std::nullopt;
1289 const char *&GlobalSymtabLoc,
1292 uint64_t GlobalSymtabContentOffset =
1294 if (GlobalSymtabContentOffset > BufferSize)
1296 Twine(BitMessage) +
" global symbol table header at offset 0x" +
1299 " goes past the end of file");
1307 RawOffset +
"\" is not a number");
1309 if (GlobalSymtabContentOffset +
Size > BufferSize)
1311 Twine(BitMessage) +
" global symbol table content at offset 0x" +
1339 unsigned SymOffsetsSize = 8 * (SymNum + 1);
1340 uint64_t SymbolTableStringSize =
Size - SymOffsetsSize;
1354 Err =
malformedError(
"malformed AIX big archive: incomplete fixed length "
1355 "header, the archive is only" +
1356 Twine(BufferSize) +
" byte(s)");
1363 Err =
malformedError(
"malformed AIX big archive: first member offset \"" +
1364 RawOffset +
"\" is not a number");
1369 Err =
malformedError(
"malformed AIX big archive: last member offset \"" +
1370 RawOffset +
"\" is not a number");
1376 "offset of 32-bit members \"" +
1377 RawOffset +
"\" is not a number");
1385 "offset of 64-bit members\"" +
1386 RawOffset +
"\" is not a number");
1390 const char *GlobSymtab32Loc =
nullptr;
1391 const char *GlobSymtab64Loc =
nullptr;
1396 if (GlobSymtab32Offset) {
1399 GlobSymtab32Loc, GlobSymtab32Size,
"32-bit");
1406 if (GlobSymtab64Offset) {
1409 GlobSymtab64Loc, GlobSymtab64Size,
"64-bit");
1418 if (GlobSymtab32Offset)
1420 if (GlobSymtab64Offset)
1423 if (SymtabInfos.
size() == 1) {
1426 }
else if (SymtabInfos.
size() == 2) {
1430 uint64_t SymNum = SymtabInfos[0].SymNum + SymtabInfos[1].SymNum;
1433 Out << SymtabInfos[0].SymbolOffsetTable;
1434 Out << SymtabInfos[1].SymbolOffsetTable;
1436 Out << SymtabInfos[0].StringTable;
1437 Out << SymtabInfos[1].StringTable;
1441 SymtabInfos[0].StringTable.
size() +
1442 SymtabInfos[1].StringTable.
size());
#define offsetof(TYPE, MEMBER)
Provides ErrorOr<T> smart pointer.
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)
static StringRef getName(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
size_t getBufferSize() const
const char * getBufferStart() const
StringRef getBuffer() const
const char * getBufferEnd() const
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.
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.
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
empty - Check if the string is empty.
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - 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.
static constexpr size_t npos
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::string str() const
Return the twine contents as a std::string.
static Twine utohexstr(const uint64_t &Val)
LLVM Value Representation.
A wrapper class for fallible iterators.
static fallible_iterator end(Underlying I)
Construct a fallible iterator that can be used as an end-of-range value.
static fallible_iterator itr(Underlying I, Error &Err)
Construct a fallible iterator that cannot be used as an end-of-range value.
Expected< StringRef > getBuffer() const
Expected< Child > getNext() const
Expected< std::string > getFullName() const
uint64_t getChildOffset() const
Expected< uint64_t > getRawSize() const
Expected< StringRef > getName() const
Expected< uint64_t > getSize() const
Expected< StringRef > getRawName() const
Expected< std::unique_ptr< Binary > > getAsBinary(LLVMContext *Context=nullptr) const
Expected< MemoryBufferRef > getMemoryBufferRef() const
Child(const Archive *Parent, const char *Start, Error *Err)
Expected< Child > getMember() const
StringRef getName() 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
uint32_t getNumberOfECSymbols() const
void setFirstRegular(const Child &C)
static object::Archive::Kind getDefaultKindForTriple(Triple &T)
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)
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
BigArchive(MemoryBufferRef Source, Error &Err)
StringRef getData() const
MemoryBufferRef getMemoryBufferRef() const
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.
constexpr size_t NameSize
const char ArchiveMagic[]
const char ThinArchiveMagic[]
const char BigArchiveMagic[]
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)
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
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.
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
void consumeError(Error Err)
Consume a Error without doing anything.
StringRef SymbolOffsetTable
char FirstChildOffset[20]
Offset to first archive member.
char LastChildOffset[20]
Offset to last archive member.
char GlobSym64Offset[20]
Offset global symbol table for 64-bit objects.
char GlobSymOffset[20]
Offset to global symbol table.
char Size[10]
Size of data, not including header or padding.