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)
113 std::string Msg(
"terminator characters in archive member \"" + Buf +
114 "\" not the correct \"`\\n\" values for the archive "
129 const char *RawHeaderPtr,
133 if (RawHeaderPtr ==
nullptr)
139 *Err =
malformedError(
"malformed AIX big archive: remaining buffer is "
140 "unable to contain next archive member");
147 *Err = std::move(SubErr);
159 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
160 return malformedError(
"name contains a leading space for archive member "
161 "header at offset " +
186 " field in archive member header are not "
187 "all decimal numbers: '" +
190 "member header at offset " +
204 " field in archive member header are not "
205 "all octal numbers: '" +
208 "member header at offset " +
228 if (!NameStringWithNameTerminator.
ends_with(NameTerminator)) {
230 reinterpret_cast<const char *
>(
ArMemHdr->Name + NameLenWithPadding) -
234 "name does not have name terminator \"`\\n\" for archive member"
235 "header at offset " +
250 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
251 return malformedError(
"archive header truncated before the name field "
252 "for archive member header at offset " +
253 Twine(ArchiveOffset));
263 if (Name[0] ==
'/') {
264 if (Name.size() == 1)
266 if (Name.size() == 2 && Name[1] ==
'/')
270 if (Name ==
"/<XFGHASHMAP>/")
274 if (Name ==
"/<ECSYMBOLS>/")
278 std::size_t StringOffset;
279 if (Name.substr(1).rtrim(
' ').getAsInteger(10, StringOffset)) {
284 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
285 return malformedError(
"long name offset characters after the '/' are "
286 "not all decimal numbers: '" +
287 Buf +
"' for archive member header at offset " +
288 Twine(ArchiveOffset));
292 if (StringOffset >=
Parent->getStringTable().size()) {
294 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
296 " past the end of the string table for archive "
297 "member header at offset " +
298 Twine(ArchiveOffset));
304 size_t End =
Parent->getStringTable().find(
'\n', StringOffset);
306 Parent->getStringTable()[End - 1] !=
'/') {
308 Twine(StringOffset) +
" not terminated");
310 return Parent->getStringTable().slice(StringOffset, End - 1);
312 return Parent->getStringTable().begin() + StringOffset;
315 if (Name.starts_with(
"#1/")) {
317 if (Name.substr(3).rtrim(
' ').getAsInteger(10, NameLength)) {
322 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
323 return malformedError(
"long name length characters after the #1/ are "
324 "not all decimal numbers: '" +
325 Buf +
"' for archive member header at offset " +
326 Twine(ArchiveOffset));
330 reinterpret_cast<const char *
>(
ArMemHdr) -
Parent->getData().data();
332 " extends past the end of the member or archive "
333 "for archive member header at offset " +
334 Twine(ArchiveOffset));
342 if (Name[Name.size() - 1] !=
'/')
343 return Name.rtrim(
' ');
346 return Name.drop_back(1);
368 return *SizeOrErr +
alignTo(*NameLenOrErr, 2);
384 if (!AccessModeOrErr)
419 return Parent->isThin() && Name !=
"/" && Name !=
"//" && Name !=
"/SYM64/";
438 const char *NextLoc =
441 if (NextLoc ==
Parent->getMemoryBufferRef().getBufferEnd())
453 if (!NextOffsetOrErr)
455 return Parent->getData().data() + NextOffsetOrErr.
get();
460 : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {
461 Header = Parent->createArchiveMemberHeader(Data.data(), Data.size(),
nullptr);
472 Header = Parent->createArchiveMemberHeader(
473 Start, Parent->getData().size() - (Start - Parent->getData().data()),
480 assert(Err &&
"Err can't be nullptr if Start is not a nullptr");
508 StartOfFile = Header->getSizeOf();
520 StartOfFile += ((Name.size() + 1) >> 1) << 1;
521 }
else if (Name.starts_with(
"#1/")) {
523 StringRef RawNameSize = Name.substr(3).rtrim(
' ');
526 *Err =
malformedError(
"long name length characters after the #1/ are "
527 "not all decimal numbers: '" +
529 "' for archive member header at offset " +
539 return Header->getSize();
540 return Data.size() - StartOfFile;
544 return Header->getSize();
547Expected<bool> Archive::Child::isThinMember()
const {
return Header->isThin(); }
552 return isThin.takeError();
559 return std::string(Name);
562 Parent->getMemoryBufferRef().getBufferIdentifier());
564 return std::string(FullName);
575 return Size.takeError();
581 const std::string &FullName = *FullNameOrErr;
584 if (std::error_code EC = Buf.
getError())
586 Parent->ThinBuffers.push_back(std::move(*Buf));
587 return Parent->ThinBuffers.back()->getBuffer();
595 const char *NextLoc = *NextLocOrErr;
598 if (NextLoc ==
nullptr)
599 return Child(
nullptr,
nullptr,
nullptr);
602 if (NextLoc > Parent->Data.getBufferEnd()) {
603 std::string Msg(
"offset to next archive member past the end of the archive "
615 Child Ret(Parent, NextLoc, &Err);
617 return std::move(Err);
622 const char *a = Parent->Data.getBuffer().data();
623 const char *c = Data.data();
634 Header->getName(Header->getSizeOf() + RawSize);
660 return std::move(*BinaryOrErr);
661 return BinaryOrErr.takeError();
666 std::unique_ptr<Archive> Ret;
670 Ret = std::make_unique<BigArchive>(Source, Err);
672 Ret = std::make_unique<Archive>(Source, Err);
675 return std::move(Err);
676 return std::move(Ret);
679std::unique_ptr<AbstractArchiveMemberHeader>
684 return std::make_unique<ArchiveMemberHeader>(
this, RawHeaderPtr,
Size, Err);
685 return std::make_unique<BigArchiveMemberHeader>(
this, RawHeaderPtr,
Size,
700 FirstRegularData =
C.Data;
701 FirstRegularStartOfFile =
C.StartOfFile;
777 if (Name ==
"__.SYMDEF" || Name ==
"__.SYMDEF_64") {
778 if (Name ==
"__.SYMDEF")
798 if (Name.starts_with(
"#1/")) {
806 Name = NameOrErr.
get();
807 if (Name ==
"__.SYMDEF SORTED" || Name ==
"__.SYMDEF") {
818 }
else if (Name ==
"__.SYMDEF_64 SORTED" || Name ==
"__.SYMDEF_64") {
840 bool has64SymTable =
false;
841 if (Name ==
"/" || Name ==
"/SYM64/") {
850 if (Name ==
"/SYM64/")
851 has64SymTable =
true;
864 Name = NameOrErr.
get();
884 if (Name[0] !=
'/') {
915 NameOrErr =
C->getRawName();
920 Name = NameOrErr.
get();
940 NameOrErr =
C->getRawName();
945 Name = NameOrErr.
get();
948 if (Name ==
"/<ECSYMBOLS>/") {
983 bool SkipInternal)
const {
989 Child(
this, FirstRegularData, FirstRegularStartOfFile), Err);
1005 uint32_t SymbolCount = Parent->getNumberOfSymbols();
1006 return SymbolCount <= SymbolIndex &&
1007 SymbolIndex < SymbolCount + Parent->getNumberOfECSymbols();
1012 return Parent->ECSymbolTable.
begin() + StringIndex;
1013 return Parent->getSymbolTable().begin() + StringIndex;
1017 const char *Buf = Parent->getSymbolTable().begin();
1018 const char *Offsets = Buf;
1025 if (Parent->kind() ==
K_GNU) {
1029 }
else if (Parent->kind() ==
K_BSD) {
1048 Buf += MemberCount * 4 + 4;
1052 if (SymbolIndex < SymbolCount) {
1054 const char *Indices = Buf + 4;
1058 OffsetIndex =
read16le(Indices + SymbolIndex * 2);
1061 const char *Indices = Parent->ECSymbolTable.begin() + 4;
1065 OffsetIndex =
read16le(Indices + (SymbolIndex - SymbolCount) * 2);
1072 if (OffsetIndex >= MemberCount)
1078 const char *
Loc = Parent->getData().begin() +
Offset;
1082 return std::move(Err);
1088 if (Parent->kind() ==
K_BSD) {
1102 const char *Buf = Parent->getSymbolTable().begin();
1108 if (t.SymbolIndex + 1 < RanlibCount) {
1109 const char *Ranlibs = Buf + 4;
1112 CurRanStrx =
read32le(Ranlibs + t.SymbolIndex * 8);
1113 NextRanStrx =
read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
1114 t.StringIndex -= CurRanStrx;
1115 t.StringIndex += NextRanStrx;
1119 t.StringIndex = Parent->ECSymbolTable.find(
'\0', t.StringIndex) + 1;
1122 t.StringIndex = Parent->getSymbolTable().find(
'\0', t.StringIndex) + 1;
1149 const char *ranlibs = buf + 4;
1165 const char *ranlibs = buf + 8;
1178 buf += 4 + (member_count * 4);
1180 buf += 4 + (symbol_count * 2);
1207 Twine(StringIndex));
1216 if (Index > MemberCount)
1218 " is larger than member count " +
1219 Twine(MemberCount));
1223 return malformedError(
"malformed EC symbol names: not null-terminated");
1249 buf += 4 + (member_count * 4);
1263 for (; bs != es; ++bs) {
1265 if (SymName ==
name) {
1267 return Child(*MemberOrErr);
1269 return MemberOrErr.takeError();
1272 return std::nullopt;
1284 const char *&GlobalSymtabLoc,
1287 uint64_t GlobalSymtabContentOffset =
1289 if (GlobalSymtabContentOffset > BufferSize)
1291 Twine(BitMessage) +
" global symbol table header at offset 0x" +
1294 " goes past the end of file");
1296 GlobalSymtabLoc =
Data.getBufferStart() + GlobalSymtabOffset;
1302 RawOffset +
"\" is not a number");
1304 if (GlobalSymtabContentOffset +
Size > BufferSize)
1306 Twine(BitMessage) +
" global symbol table content at offset 0x" +
1334 unsigned SymOffsetsSize = 8 * (SymNum + 1);
1335 uint64_t SymbolTableStringSize =
Size - SymOffsetsSize;
1349 Err =
malformedError(
"malformed AIX big archive: incomplete fixed length "
1350 "header, the archive is only" +
1351 Twine(BufferSize) +
" byte(s)");
1358 Err =
malformedError(
"malformed AIX big archive: first member offset \"" +
1359 RawOffset +
"\" is not a number");
1364 Err =
malformedError(
"malformed AIX big archive: last member offset \"" +
1365 RawOffset +
"\" is not a number");
1371 "offset of 32-bit members \"" +
1372 RawOffset +
"\" is not a number");
1380 "offset of 64-bit members\"" +
1381 RawOffset +
"\" is not a number");
1385 const char *GlobSymtab32Loc =
nullptr;
1386 const char *GlobSymtab64Loc =
nullptr;
1391 if (GlobSymtab32Offset) {
1394 GlobSymtab32Loc, GlobSymtab32Size,
"32-bit");
1401 if (GlobSymtab64Offset) {
1404 GlobSymtab64Loc, GlobSymtab64Size,
"64-bit");
1413 if (GlobSymtab32Offset)
1415 if (GlobSymtab64Offset)
1418 if (SymtabInfos.
size() == 1) {
1421 }
else if (SymtabInfos.
size() == 2) {
1425 uint64_t SymNum = SymtabInfos[0].SymNum + SymtabInfos[1].SymNum;
1428 Out << SymtabInfos[0].SymbolOffsetTable;
1429 Out << SymtabInfos[1].SymbolOffsetTable;
1431 Out << SymtabInfos[0].StringTable;
1432 Out << SymtabInfos[1].StringTable;
1436 SymtabInfos[0].StringTable.
size() +
1437 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.
static constexpr size_t npos
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.
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.
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 write(MCStreamer &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue)
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
@ Increment
Incrementally increasing token ID.
void consumeError(Error Err)
Consume a Error without doing anything.
StringRef SymbolOffsetTable