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.
endswith(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.equals(
"/<XFGHASHMAP>/"))
276 if (
Name.equals(
"/<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.startswith(
"#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.startswith(
"#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.
str());
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.startswith(
"#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>/") {
981 bool SkipInternal)
const {
987 Child(
this, FirstRegularData, FirstRegularStartOfFile), Err);
1004 return SymbolCount <= SymbolIndex &&
1010 return Parent->ECSymbolTable.
begin() + StringIndex;
1011 return Parent->getSymbolTable().begin() + StringIndex;
1015 const char *Buf = Parent->getSymbolTable().begin();
1016 const char *Offsets = Buf;
1023 if (Parent->kind() ==
K_GNU) {
1027 }
else if (Parent->kind() ==
K_BSD) {
1046 Buf += MemberCount * 4 + 4;
1050 if (SymbolIndex < SymbolCount) {
1052 const char *Indices = Buf + 4;
1056 OffsetIndex =
read16le(Indices + SymbolIndex * 2);
1057 }
else if (isECSymbol()) {
1059 const char *Indices = Parent->ECSymbolTable.begin() + 4;
1063 OffsetIndex =
read16le(Indices + (SymbolIndex - SymbolCount) * 2);
1070 if (OffsetIndex >= MemberCount)
1076 const char *Loc = Parent->getData().begin() +
Offset;
1078 Child C(Parent, Loc, &Err);
1080 return std::move(Err);
1086 if (Parent->kind() ==
K_BSD) {
1100 const char *Buf = Parent->getSymbolTable().begin();
1106 if (t.SymbolIndex + 1 < RanlibCount) {
1107 const char *Ranlibs = Buf + 4;
1110 CurRanStrx =
read32le(Ranlibs + t.SymbolIndex * 8);
1111 NextRanStrx =
read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
1112 t.StringIndex -= CurRanStrx;
1113 t.StringIndex += NextRanStrx;
1117 t.StringIndex = Parent->ECSymbolTable.find(
'\0', t.StringIndex) + 1;
1120 t.StringIndex = Parent->getSymbolTable().find(
'\0', t.StringIndex) + 1;
1147 const char *ranlibs = buf + 4;
1163 const char *ranlibs = buf + 8;
1176 buf += 4 + (member_count * 4);
1178 buf += 4 + (symbol_count * 2);
1205 Twine(StringIndex));
1210 for (
uint32_t i = 0; i < Count; ++i) {
1214 if (
Index > MemberCount)
1216 " is larger than member count " +
1217 Twine(MemberCount));
1221 return malformedError(
"malformed EC symbol names: not null-terminated");
1247 buf += 4 + (member_count * 4);
1261 for (; bs != es; ++bs) {
1263 if (SymName ==
name) {
1265 return Child(*MemberOrErr);
1267 return MemberOrErr.takeError();
1270 return std::nullopt;
1282 const char *&GlobalSymtabLoc,
1285 uint64_t GlobalSymtabContentOffset =
1287 if (GlobalSymtabContentOffset > BufferSize)
1289 Twine(BitMessage) +
" global symbol table header at offset 0x" +
1292 " goes past the end of file");
1300 RawOffset +
"\" is not a number");
1302 if (GlobalSymtabContentOffset +
Size > BufferSize)
1304 Twine(BitMessage) +
" global symbol table content at offset 0x" +
1332 unsigned SymOffsetsSize = 8 * (SymNum + 1);
1333 uint64_t SymbolTableStringSize =
Size - SymOffsetsSize;
1347 Err =
malformedError(
"malformed AIX big archive: incomplete fixed length "
1348 "header, the archive is only" +
1349 Twine(BufferSize) +
" byte(s)");
1356 Err =
malformedError(
"malformed AIX big archive: first member offset \"" +
1357 RawOffset +
"\" is not a number");
1362 Err =
malformedError(
"malformed AIX big archive: last member offset \"" +
1363 RawOffset +
"\" is not a number");
1369 "offset of 32-bit members \"" +
1370 RawOffset +
"\" is not a number");
1378 "offset of 64-bit members\"" +
1379 RawOffset +
"\" is not a number");
1383 const char *GlobSymtab32Loc =
nullptr;
1384 const char *GlobSymtab64Loc =
nullptr;
1389 if (GlobSymtab32Offset) {
1392 GlobSymtab32Loc, GlobSymtab32Size,
"32-bit");
1399 if (GlobSymtab64Offset) {
1402 GlobSymtab64Loc, GlobSymtab64Size,
"64-bit");
1411 if (GlobSymtab32Offset)
1413 if (GlobSymtab64Offset)
1416 if (SymtabInfos.
size() == 1) {
1419 }
else if (SymtabInfos.
size() == 2) {
1423 uint64_t SymNum = SymtabInfos[0].SymNum + SymtabInfos[1].SymNum;
1426 Out << SymtabInfos[0].SymbolOffsetTable;
1427 Out << SymtabInfos[1].SymbolOffsetTable;
1429 Out << SymtabInfos[0].StringTable;
1430 Out << SymtabInfos[1].StringTable;
1434 SymtabInfos[0].StringTable.
size() +
1435 SymtabInfos[1].StringTable.
size());
#define offsetof(TYPE, MEMBER)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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...
StringRef str() const
Explicit conversion to StringRef.
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.
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
uint32_t getNumberOfECSymbols() 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< 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 object::Archive::Kind getDefaultKindForHost()
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 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.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, bool ContinueOnCuIndexOverflow)
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.