35#include <system_error>
38using namespace object;
41void Archive::anchor() {}
44 std::string StringMsg =
"truncated or malformed archive (" + Msg.str() +
")";
45 return make_error<GenericBinaryError>(std::move(StringMsg),
52 StringRef Msg(
"remaining size of archive too small for next archive "
64template <
class T, std::
size_t N>
88 return reinterpret_cast<const char *
>(ArMemHdr) - Parent->getData().data();
95 const char *RawHeaderPtr,
99 if (RawHeaderPtr ==
nullptr)
114 std::string Msg(
"terminator characters in archive member \"" + Buf +
115 "\" not the correct \"`\\n\" values for the archive "
130 const char *RawHeaderPtr,
134 if (RawHeaderPtr ==
nullptr)
140 *Err =
malformedError(
"malformed AIX big archive: remaining buffer is "
141 "unable to contain next archive member");
148 *Err = std::move(SubErr);
161 return malformedError(
"name contains a leading space for archive member "
162 "header at offset " +
187 " field in archive member header are not "
188 "all decimal numbers: '" +
191 "member header at offset " +
205 " field in archive member header are not "
206 "all octal numbers: '" +
209 "member header at offset " +
229 if (!NameStringWithNameTerminator.
endswith(NameTerminator)) {
231 reinterpret_cast<const char *
>(
ArMemHdr->
Name + NameLenWithPadding) -
235 "name does not have name terminator \"`\\n\" for archive member"
236 "header at offset " +
252 return malformedError(
"archive header truncated before the name field "
253 "for archive member header at offset " +
254 Twine(ArchiveOffset));
264 if (
Name[0] ==
'/') {
265 if (
Name.size() == 1)
267 if (
Name.size() == 2 &&
Name[1] ==
'/')
271 if (
Name.equals(
"/<XFGHASHMAP>/"))
275 if (
Name.equals(
"/<ECSYMBOLS>/"))
279 std::size_t StringOffset;
280 if (
Name.substr(1).rtrim(
' ').getAsInteger(10, StringOffset)) {
287 return malformedError(
"long name offset characters after the '/' are "
288 "not all decimal numbers: '" +
289 Buf +
"' for archive member header at offset " +
290 Twine(ArchiveOffset));
298 " past the end of the string table for archive "
299 "member header at offset " +
300 Twine(ArchiveOffset));
310 Twine(StringOffset) +
"not terminated");
317 if (
Name.startswith(
"#1/")) {
319 if (
Name.substr(3).rtrim(
' ').getAsInteger(10, NameLength)) {
326 return malformedError(
"long name length characters after the #1/ are "
327 "not all decimal numbers: '" +
328 Buf +
"' for archive member header at offset " +
329 Twine(ArchiveOffset));
335 " extends past the end of the member or archive "
336 "for archive member header at offset " +
337 Twine(ArchiveOffset));
346 return Name.rtrim(
' ');
349 return Name.drop_back(1);
371 return *SizeOrErr +
alignTo(*NameLenOrErr, 2);
387 if (!AccessModeOrErr)
441 const char *NextLoc =
456 if (!NextOffsetOrErr)
463 : Parent(Parent),
Data(
Data), StartOfFile(StartOfFile) {
484 assert(Err &&
"Err can't be nullptr if Start is not a nullptr");
512 StartOfFile = Header->getSizeOf();
524 StartOfFile += ((
Name.size() + 1) >> 1) << 1;
525 }
else if (
Name.startswith(
"#1/")) {
530 *Err =
malformedError(
"long name length characters after the #1/ are "
531 "not all decimal numbers: '" +
533 "' for archive member header at offset " +
543 return Header->getSize();
544 return Data.size() - StartOfFile;
548 return Header->getSize();
551Expected<bool> Archive::Child::isThinMember()
const {
return Header->isThin(); }
556 return isThin.takeError();
563 return std::string(
Name);
566 Parent->getMemoryBufferRef().getBufferIdentifier());
568 return std::string(FullName.
str());
579 return Size.takeError();
585 const std::string &FullName = *FullNameOrErr;
587 if (std::error_code EC = Buf.
getError())
589 Parent->ThinBuffers.push_back(std::move(*Buf));
590 return Parent->ThinBuffers.back()->getBuffer();
598 const char *NextLoc = *NextLocOrErr;
601 if (NextLoc ==
nullptr)
602 return Child(
nullptr,
nullptr,
nullptr);
605 if (NextLoc > Parent->Data.getBufferEnd()) {
606 std::string Msg(
"offset to next archive member past the end of the archive "
618 Child Ret(Parent, NextLoc, &Err);
620 return std::move(Err);
625 const char *a = Parent->Data.getBuffer().data();
626 const char *c =
Data.data();
637 Header->getName(Header->getSizeOf() + RawSize);
663 return std::move(*BinaryOrErr);
664 return BinaryOrErr.takeError();
669 std::unique_ptr<Archive> Ret;
673 Ret = std::make_unique<BigArchive>(Source, Err);
675 Ret = std::make_unique<Archive>(Source, Err);
678 return std::move(Err);
679 return std::move(Ret);
682std::unique_ptr<AbstractArchiveMemberHeader>
687 return std::make_unique<ArchiveMemberHeader>(
this, RawHeaderPtr,
Size, Err);
688 return std::make_unique<BigArchiveMemberHeader>(
this, RawHeaderPtr,
Size,
703 FirstRegularData =
C.Data;
704 FirstRegularStartOfFile =
C.StartOfFile;
721 Err = make_error<GenericBinaryError>(
"file too small to be an archive",
746 auto Increment = [&]() {
780 if (
Name ==
"__.SYMDEF" ||
Name ==
"__.SYMDEF_64") {
781 if (
Name ==
"__.SYMDEF")
801 if (
Name.startswith(
"#1/")) {
810 if (
Name ==
"__.SYMDEF SORTED" ||
Name ==
"__.SYMDEF") {
821 }
else if (
Name ==
"__.SYMDEF_64 SORTED" ||
Name ==
"__.SYMDEF_64") {
843 bool has64SymTable =
false;
844 if (
Name ==
"/" ||
Name ==
"/SYM64/") {
853 if (
Name ==
"/SYM64/")
854 has64SymTable =
true;
887 if (
Name[0] !=
'/') {
918 NameOrErr =
C->getRawName();
951 bool SkipInternal)
const {
957 Child(
this, FirstRegularData, FirstRegularStartOfFile), Err);
975 const char *Buf = Parent->getSymbolTable().begin();
976 const char *Offsets = Buf;
983 if (Parent->kind() ==
K_GNU) {
987 }
else if (Parent->kind() ==
K_BSD) {
1006 Buf += MemberCount * 4 + 4;
1009 if (SymbolIndex >= SymbolCount)
1013 const char *Indices = Buf + 4;
1021 if (OffsetIndex >= MemberCount)
1027 const char *Loc = Parent->getData().begin() +
Offset;
1029 Child C(Parent, Loc, &Err);
1031 return std::move(Err);
1037 if (Parent->kind() ==
K_BSD) {
1051 const char *Buf = Parent->getSymbolTable().begin();
1057 if (t.SymbolIndex + 1 < RanlibCount) {
1058 const char *Ranlibs = Buf + 4;
1061 CurRanStrx =
read32le(Ranlibs + t.SymbolIndex * 8);
1062 NextRanStrx =
read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
1063 t.StringIndex -= CurRanStrx;
1064 t.StringIndex += NextRanStrx;
1068 t.StringIndex = Parent->getSymbolTable().find(
'\0', t.StringIndex) + 1;
1095 const char *ranlibs = buf + 4;
1111 const char *ranlibs = buf + 8;
1124 buf += 4 + (member_count * 4);
1126 buf += 4 + (symbol_count * 2);
1150 buf += 4 + (member_count * 4);
1158 for (; bs != es; ++bs) {
1160 if (SymName ==
name) {
1162 return Child(*MemberOrErr);
1164 return MemberOrErr.takeError();
1167 return std::nullopt;
1185 Err =
malformedError(
"malformed AIX big archive: incomplete fixed length "
1186 "header, the archive is only" +
1187 Twine(BufferSize) +
" byte(s)");
1194 Err =
malformedError(
"malformed AIX big archive: first member offset \"" +
1195 RawOffset +
"\" is not a number");
1200 Err =
malformedError(
"malformed AIX big archive: last member offset \"" +
1201 RawOffset +
"\" is not a number");
1209 "malformed AIX big archive: global symbol table offset \"" + RawOffset +
1210 "\" is not a number");
1215 if (GlobSymOffset > 0) {
1216 uint64_t GlobalSymTblContentOffset =
1218 if (GlobalSymTblContentOffset > BufferSize) {
1222 " goes past the end of file");
1234 "malformed AIX big archive: global symbol table size \"" + RawOffset +
1235 "\" is not a number");
1238 if (GlobalSymTblContentOffset +
Size > BufferSize) {
1239 Err =
malformedError(
"global symbol table content at offset 0x" +
1242 " goes past the end of file");
1247 unsigned SymOffsetsSize = 8 * (SymNum + 1);
1248 uint64_t SymbolTableStringSize =
Size - SymOffsetsSize;
1251 SymbolTableStringSize);
#define offsetof(TYPE, MEMBER)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Provides ErrorOr<T> smart pointer.
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...
StringRef str() const
Explicit conversion to StringRef.
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.
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.
bool startswith(StringRef Prefix) const
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 endswith(StringRef Suffix) const
static constexpr size_t npos
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Triple - Helper class for working with autoconf configuration names.
bool isOSAIX() const
Tests whether the OS is AIX.
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...
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
void setFirstRegular(const Child &C)
uint64_t getArchiveMagicLen() const
StringRef getSymbolTable() const
symbol_iterator symbol_end() const
virtual bool isEmpty() const
child_iterator child_end() const
bool hasSymbolTable() const
Archive(MemoryBufferRef Source, Error &Err)
Expected< std::optional< Child > > findSym(StringRef name) const
child_iterator child_begin(Error &Err, bool SkipInternal=true) const
static object::Archive::Kind getDefaultKindForHost()
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
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 getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
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.
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.
char FirstChildOffset[20]
Offset to first archive member.
char LastChildOffset[20]
Offset to last archive member.
char GlobSymOffset[20]
Offset to global symbol table.
char Size[10]
Size of data, not including header or padding.