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.
ends_with(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 ==
"/<XFGHASHMAP>/")
275 if (
Name ==
"/<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.starts_with(
"#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) {
485 assert(Err &&
"Err can't be nullptr if Start is not a nullptr");
513 StartOfFile = Header->getSizeOf();
525 StartOfFile += ((
Name.size() + 1) >> 1) << 1;
526 }
else if (
Name.starts_with(
"#1/")) {
531 *Err =
malformedError(
"long name length characters after the #1/ are "
532 "not all decimal numbers: '" +
534 "' for archive member header at offset " +
544 return Header->getSize();
545 return Data.size() - StartOfFile;
549 return Header->getSize();
552Expected<bool> Archive::Child::isThinMember()
const {
return Header->isThin(); }
557 return isThin.takeError();
564 return std::string(
Name);
567 Parent->getMemoryBufferRef().getBufferIdentifier());
569 return std::string(FullName);
580 return Size.takeError();
586 const std::string &FullName = *FullNameOrErr;
588 if (std::error_code EC = Buf.
getError())
590 Parent->ThinBuffers.push_back(std::move(*Buf));
591 return Parent->ThinBuffers.back()->getBuffer();
599 const char *NextLoc = *NextLocOrErr;
602 if (NextLoc ==
nullptr)
603 return Child(
nullptr,
nullptr,
nullptr);
606 if (NextLoc > Parent->Data.getBufferEnd()) {
607 std::string Msg(
"offset to next archive member past the end of the archive "
619 Child Ret(Parent, NextLoc, &Err);
621 return std::move(Err);
626 const char *a = Parent->Data.getBuffer().data();
627 const char *c =
Data.data();
638 Header->getName(Header->getSizeOf() + RawSize);
664 return std::move(*BinaryOrErr);
665 return BinaryOrErr.takeError();
670 std::unique_ptr<Archive> Ret;
674 Ret = std::make_unique<BigArchive>(Source, Err);
676 Ret = std::make_unique<Archive>(Source, Err);
679 return std::move(Err);
680 return std::move(Ret);
683std::unique_ptr<AbstractArchiveMemberHeader>
688 return std::make_unique<ArchiveMemberHeader>(
this, RawHeaderPtr,
Size, Err);
689 return std::make_unique<BigArchiveMemberHeader>(
this, RawHeaderPtr,
Size,
704 FirstRegularData =
C.Data;
705 FirstRegularStartOfFile =
C.StartOfFile;
722 Err = make_error<GenericBinaryError>(
"file too small to be an archive",
747 auto Increment = [&]() {
781 if (
Name ==
"__.SYMDEF" ||
Name ==
"__.SYMDEF_64") {
782 if (
Name ==
"__.SYMDEF")
802 if (
Name.starts_with(
"#1/")) {
811 if (
Name ==
"__.SYMDEF SORTED" ||
Name ==
"__.SYMDEF") {
822 }
else if (
Name ==
"__.SYMDEF_64 SORTED" ||
Name ==
"__.SYMDEF_64") {
844 bool has64SymTable =
false;
845 if (
Name ==
"/" ||
Name ==
"/SYM64/") {
854 if (
Name ==
"/SYM64/")
855 has64SymTable =
true;
888 if (
Name[0] !=
'/') {
919 NameOrErr =
C->getRawName();
944 NameOrErr =
C->getRawName();
952 if (
Name ==
"/<ECSYMBOLS>/") {
987 bool SkipInternal)
const {
993 Child(
this, FirstRegularData, FirstRegularStartOfFile), Err);
1010 return SymbolCount <= SymbolIndex &&
1016 return Parent->ECSymbolTable.
begin() + StringIndex;
1017 return Parent->getSymbolTable().begin() + StringIndex;
1021 const char *Buf = Parent->getSymbolTable().begin();
1022 const char *Offsets = Buf;
1029 if (Parent->kind() ==
K_GNU) {
1033 }
else if (Parent->kind() ==
K_BSD) {
1052 Buf += MemberCount * 4 + 4;
1056 if (SymbolIndex < SymbolCount) {
1058 const char *Indices = Buf + 4;
1062 OffsetIndex =
read16le(Indices + SymbolIndex * 2);
1063 }
else if (isECSymbol()) {
1065 const char *Indices = Parent->ECSymbolTable.begin() + 4;
1069 OffsetIndex =
read16le(Indices + (SymbolIndex - SymbolCount) * 2);
1076 if (OffsetIndex >= MemberCount)
1082 const char *Loc = Parent->getData().begin() +
Offset;
1084 Child C(Parent, Loc, &Err);
1086 return std::move(Err);
1092 if (Parent->kind() ==
K_BSD) {
1106 const char *Buf = Parent->getSymbolTable().begin();
1112 if (t.SymbolIndex + 1 < RanlibCount) {
1113 const char *Ranlibs = Buf + 4;
1116 CurRanStrx =
read32le(Ranlibs + t.SymbolIndex * 8);
1117 NextRanStrx =
read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
1118 t.StringIndex -= CurRanStrx;
1119 t.StringIndex += NextRanStrx;
1123 t.StringIndex = Parent->ECSymbolTable.find(
'\0', t.StringIndex) + 1;
1126 t.StringIndex = Parent->getSymbolTable().find(
'\0', t.StringIndex) + 1;
1153 const char *ranlibs = buf + 4;
1169 const char *ranlibs = buf + 8;
1182 buf += 4 + (member_count * 4);
1184 buf += 4 + (symbol_count * 2);
1211 Twine(StringIndex));
1216 for (
uint32_t i = 0; i < Count; ++i) {
1220 if (Index > MemberCount)
1222 " is larger than member count " +
1223 Twine(MemberCount));
1227 return malformedError(
"malformed EC symbol names: not null-terminated");
1253 buf += 4 + (member_count * 4);
1267 for (; bs != es; ++bs) {
1269 if (SymName ==
name) {
1271 return Child(*MemberOrErr);
1273 return MemberOrErr.takeError();
1276 return std::nullopt;
1288 const char *&GlobalSymtabLoc,
1291 uint64_t GlobalSymtabContentOffset =
1293 if (GlobalSymtabContentOffset > BufferSize)
1295 Twine(BitMessage) +
" global symbol table header at offset 0x" +
1298 " goes past the end of file");
1306 RawOffset +
"\" is not a number");
1308 if (GlobalSymtabContentOffset +
Size > BufferSize)
1310 Twine(BitMessage) +
" global symbol table content at offset 0x" +
1338 unsigned SymOffsetsSize = 8 * (SymNum + 1);
1339 uint64_t SymbolTableStringSize =
Size - SymOffsetsSize;
1353 Err =
malformedError(
"malformed AIX big archive: incomplete fixed length "
1354 "header, the archive is only" +
1355 Twine(BufferSize) +
" byte(s)");
1362 Err =
malformedError(
"malformed AIX big archive: first member offset \"" +
1363 RawOffset +
"\" is not a number");
1368 Err =
malformedError(
"malformed AIX big archive: last member offset \"" +
1369 RawOffset +
"\" is not a number");
1375 "offset of 32-bit members \"" +
1376 RawOffset +
"\" is not a number");
1384 "offset of 64-bit members\"" +
1385 RawOffset +
"\" is not a number");
1389 const char *GlobSymtab32Loc =
nullptr;
1390 const char *GlobSymtab64Loc =
nullptr;
1395 if (GlobSymtab32Offset) {
1398 GlobSymtab32Loc, GlobSymtab32Size,
"32-bit");
1405 if (GlobSymtab64Offset) {
1408 GlobSymtab64Loc, GlobSymtab64Size,
"64-bit");
1417 if (GlobSymtab32Offset)
1419 if (GlobSymtab64Offset)
1422 if (SymtabInfos.
size() == 1) {
1425 }
else if (SymtabInfos.
size() == 2) {
1429 uint64_t SymNum = SymtabInfos[0].SymNum + SymtabInfos[1].SymNum;
1432 Out << SymtabInfos[0].SymbolOffsetTable;
1433 Out << SymtabInfos[1].SymbolOffsetTable;
1435 Out << SymtabInfos[0].StringTable;
1436 Out << SymtabInfos[1].StringTable;
1440 SymtabInfos[0].StringTable.
size() +
1441 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
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...
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)
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
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.