35#include <system_error>
41void Archive::anchor() {}
44 std::string StringMsg =
"truncated or malformed archive (" + Msg.
str() +
")";
52 StringRef Msg(
"remaining size of archive too small for next archive "
64template <
class T, std::
size_t N>
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);
160 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
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 " +
251 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
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)) {
286 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
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));
294 if (StringOffset >=
Parent->getStringTable().size()) {
296 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
298 " past the end of the string table for archive "
299 "member header at offset " +
300 Twine(ArchiveOffset));
306 size_t End =
Parent->getStringTable().find(
'\n', StringOffset);
308 Parent->getStringTable()[End - 1] !=
'/') {
310 Twine(StringOffset) +
"not terminated");
312 return Parent->getStringTable().slice(StringOffset, End - 1);
314 return Parent->getStringTable().begin() + StringOffset;
317 if (Name.starts_with(
"#1/")) {
319 if (Name.substr(3).rtrim(
' ').getAsInteger(10, NameLength)) {
325 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
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));
333 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
335 " extends past the end of the member or archive "
336 "for archive member header at offset " +
337 Twine(ArchiveOffset));
345 if (Name[Name.size() - 1] !=
'/')
346 return Name.rtrim(
' ');
349 return Name.drop_back(1);
371 return *SizeOrErr +
alignTo(*NameLenOrErr, 2);
387 if (!AccessModeOrErr)
422 return Parent->isThin() && Name !=
"/" && Name !=
"//" && Name !=
"/SYM64/";
441 const char *NextLoc =
444 if (NextLoc ==
Parent->getMemoryBufferRef().getBufferEnd())
456 if (!NextOffsetOrErr)
458 return Parent->getData().data() + NextOffsetOrErr.
get();
463 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {
464 Header = Parent->createArchiveMemberHeader(Data.data(), Data.size(),
nullptr);
475 Header = Parent->createArchiveMemberHeader(
476 Start, Parent->getData().size() - (Start - Parent->getData().data()),
483 assert(Err &&
"Err can't be nullptr if Start is not a nullptr");
511 StartOfFile = Header->getSizeOf();
523 StartOfFile += ((Name.size() + 1) >> 1) << 1;
524 }
else if (Name.starts_with(
"#1/")) {
526 StringRef RawNameSize = Name.substr(3).rtrim(
' ');
529 *Err =
malformedError(
"long name length characters after the #1/ are "
530 "not all decimal numbers: '" +
532 "' for archive member header at offset " +
542 return Header->getSize();
543 return Data.size() - StartOfFile;
547 return Header->getSize();
550Expected<bool> Archive::Child::isThinMember()
const {
return Header->isThin(); }
555 return isThin.takeError();
562 return std::string(Name);
565 Parent->getMemoryBufferRef().getBufferIdentifier());
567 return std::string(FullName);
578 return Size.takeError();
584 const std::string &FullName = *FullNameOrErr;
586 if (std::error_code EC = Buf.
getError())
588 Parent->ThinBuffers.push_back(std::move(*Buf));
589 return Parent->ThinBuffers.back()->getBuffer();
597 const char *NextLoc = *NextLocOrErr;
600 if (NextLoc ==
nullptr)
601 return Child(
nullptr,
nullptr,
nullptr);
604 if (NextLoc > Parent->Data.getBufferEnd()) {
605 std::string Msg(
"offset to next archive member past the end of the archive "
617 Child Ret(Parent, NextLoc, &Err);
619 return std::move(Err);
624 const char *a = Parent->Data.getBuffer().data();
625 const char *c = Data.data();
636 Header->getName(Header->getSizeOf() + RawSize);
662 return std::move(*BinaryOrErr);
663 return BinaryOrErr.takeError();
668 std::unique_ptr<Archive> Ret;
672 Ret = std::make_unique<BigArchive>(Source, Err);
674 Ret = std::make_unique<Archive>(Source, Err);
677 return std::move(Err);
678 return std::move(Ret);
681std::unique_ptr<AbstractArchiveMemberHeader>
686 return std::make_unique<ArchiveMemberHeader>(
this, RawHeaderPtr,
Size, Err);
687 return std::make_unique<BigArchiveMemberHeader>(
this, RawHeaderPtr,
Size,
702 FirstRegularData =
C.Data;
703 FirstRegularStartOfFile =
C.StartOfFile;
745 auto Increment = [&]() {
779 if (Name ==
"__.SYMDEF" || Name ==
"__.SYMDEF_64") {
780 if (Name ==
"__.SYMDEF")
800 if (Name.starts_with(
"#1/")) {
808 Name = NameOrErr.
get();
809 if (Name ==
"__.SYMDEF SORTED" || Name ==
"__.SYMDEF") {
820 }
else if (Name ==
"__.SYMDEF_64 SORTED" || Name ==
"__.SYMDEF_64") {
842 bool has64SymTable =
false;
843 if (Name ==
"/" || Name ==
"/SYM64/") {
852 if (Name ==
"/SYM64/")
853 has64SymTable =
true;
866 Name = NameOrErr.
get();
886 if (Name[0] !=
'/') {
917 NameOrErr =
C->getRawName();
922 Name = NameOrErr.
get();
942 NameOrErr =
C->getRawName();
947 Name = NameOrErr.
get();
950 if (Name ==
"/<ECSYMBOLS>/") {
985 bool SkipInternal)
const {
991 Child(
this, FirstRegularData, FirstRegularStartOfFile), Err);
1007 uint32_t SymbolCount = Parent->getNumberOfSymbols();
1008 return SymbolCount <= SymbolIndex &&
1009 SymbolIndex < SymbolCount + Parent->getNumberOfECSymbols();
1014 return Parent->ECSymbolTable.
begin() + StringIndex;
1015 return Parent->getSymbolTable().begin() + StringIndex;
1019 const char *Buf = Parent->getSymbolTable().begin();
1020 const char *Offsets = Buf;
1027 if (Parent->kind() ==
K_GNU) {
1031 }
else if (Parent->kind() ==
K_BSD) {
1050 Buf += MemberCount * 4 + 4;
1054 if (SymbolIndex < SymbolCount) {
1056 const char *Indices = Buf + 4;
1060 OffsetIndex =
read16le(Indices + SymbolIndex * 2);
1063 const char *Indices = Parent->ECSymbolTable.begin() + 4;
1067 OffsetIndex =
read16le(Indices + (SymbolIndex - SymbolCount) * 2);
1074 if (OffsetIndex >= MemberCount)
1080 const char *
Loc = Parent->getData().begin() +
Offset;
1084 return std::move(Err);
1090 if (Parent->kind() ==
K_BSD) {
1104 const char *Buf = Parent->getSymbolTable().begin();
1110 if (t.SymbolIndex + 1 < RanlibCount) {
1111 const char *Ranlibs = Buf + 4;
1114 CurRanStrx =
read32le(Ranlibs + t.SymbolIndex * 8);
1115 NextRanStrx =
read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
1116 t.StringIndex -= CurRanStrx;
1117 t.StringIndex += NextRanStrx;
1121 t.StringIndex = Parent->ECSymbolTable.find(
'\0', t.StringIndex) + 1;
1124 t.StringIndex = Parent->getSymbolTable().find(
'\0', t.StringIndex) + 1;
1151 const char *ranlibs = buf + 4;
1167 const char *ranlibs = buf + 8;
1180 buf += 4 + (member_count * 4);
1182 buf += 4 + (symbol_count * 2);
1209 Twine(StringIndex));
1218 if (Index > MemberCount)
1220 " is larger than member count " +
1221 Twine(MemberCount));
1225 return malformedError(
"malformed EC symbol names: not null-terminated");
1251 buf += 4 + (member_count * 4);
1265 for (; bs != es; ++bs) {
1267 if (SymName ==
name) {
1269 return Child(*MemberOrErr);
1271 return MemberOrErr.takeError();
1274 return std::nullopt;
1286 const char *&GlobalSymtabLoc,
1289 uint64_t GlobalSymtabContentOffset =
1291 if (GlobalSymtabContentOffset > BufferSize)
1293 Twine(BitMessage) +
" global symbol table header at offset 0x" +
1296 " goes past the end of file");
1298 GlobalSymtabLoc =
Data.getBufferStart() + GlobalSymtabOffset;
1304 RawOffset +
"\" is not a number");
1306 if (GlobalSymtabContentOffset +
Size > BufferSize)
1308 Twine(BitMessage) +
" global symbol table content at offset 0x" +
1336 unsigned SymOffsetsSize = 8 * (SymNum + 1);
1337 uint64_t SymbolTableStringSize =
Size - SymOffsetsSize;
1351 Err =
malformedError(
"malformed AIX big archive: incomplete fixed length "
1352 "header, the archive is only" +
1353 Twine(BufferSize) +
" byte(s)");
1360 Err =
malformedError(
"malformed AIX big archive: first member offset \"" +
1361 RawOffset +
"\" is not a number");
1366 Err =
malformedError(
"malformed AIX big archive: last member offset \"" +
1367 RawOffset +
"\" is not a number");
1373 "offset of 32-bit members \"" +
1374 RawOffset +
"\" is not a number");
1382 "offset of 64-bit members\"" +
1383 RawOffset +
"\" is not a number");
1387 const char *GlobSymtab32Loc =
nullptr;
1388 const char *GlobSymtab64Loc =
nullptr;
1393 if (GlobSymtab32Offset) {
1396 GlobSymtab32Loc, GlobSymtab32Size,
"32-bit");
1403 if (GlobSymtab64Offset) {
1406 GlobSymtab64Loc, GlobSymtab64Size,
"64-bit");
1415 if (GlobSymtab32Offset)
1417 if (GlobSymtab64Offset)
1420 if (SymtabInfos.
size() == 1) {
1423 }
else if (SymtabInfos.
size() == 2) {
1427 uint64_t SymNum = SymtabInfos[0].SymNum + SymtabInfos[1].SymNum;
1430 Out << SymtabInfos[0].SymbolOffsetTable;
1431 Out << SymtabInfos[1].SymbolOffsetTable;
1433 Out << SymtabInfos[0].StringTable;
1434 Out << SymtabInfos[1].StringTable;
1438 SymtabInfos[0].StringTable.
size() +
1439 SymtabInfos[1].StringTable.
size());
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Provides ErrorOr<T> smart pointer.
#define offsetof(TYPE, MEMBER)
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.
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.
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...
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
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[]
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.
LLVM_ABI Error write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue)
FunctionAddr VTableAddr Count
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
LLVM_ABI 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