36 #include <system_error>
39 using namespace object;
40 using namespace llvm::support::endian;
42 static const char *
const Magic =
"!<arch>\n";
45 void Archive::anchor() {}
49 std::string StringMsg =
"truncated or malformed archive (" + Msg.
str() +
")";
50 return make_error<GenericBinaryError>(std::move(StringMsg),
55 const char *RawHeaderPtr,
58 ArMemHdr(reinterpret_cast<
const ArMemHdrType *>(RawHeaderPtr)) {
59 if (RawHeaderPtr ==
nullptr)
63 if (Size <
sizeof(ArMemHdrType)) {
65 std::string Msg(
"remaining size of archive too small for next archive "
77 if (ArMemHdr->Terminator[0] !=
'`' || ArMemHdr->Terminator[1] !=
'\n') {
82 sizeof(ArMemHdr->Terminator)));
84 std::string Msg(
"terminator characters in archive member \"" + Buf +
85 "\" not the correct \"`\\n\" values for the archive "
105 if (ArMemHdr->Name[0] ==
' ') {
106 uint64_t
Offset =
reinterpret_cast<const char *
>(ArMemHdr) -
108 return malformedError(
"name contains a leading space for archive member "
109 "header at offset " +
Twine(Offset));
113 else if (ArMemHdr->Name[0] ==
'/' || ArMemHdr->Name[0] ==
'#')
118 StringRef(ArMemHdr->Name,
sizeof(ArMemHdr->Name)).
find(EndCond);
120 end =
sizeof(ArMemHdr->Name);
121 assert(end <=
sizeof(ArMemHdr->Name) && end > 0);
134 if (Size < offsetof(ArMemHdrType,
Name) +
sizeof(ArMemHdr->Name)) {
135 uint64_t ArchiveOffset =
reinterpret_cast<const char *
>(ArMemHdr) -
137 return malformedError(
"archive header truncated before the name field "
138 "for archive member header at offset " +
139 Twine(ArchiveOffset));
149 if (Name[0] ==
'/') {
150 if (Name.
size() == 1)
152 if (Name.
size() == 2 && Name[1] ==
'/')
156 std::size_t StringOffset;
162 uint64_t ArchiveOffset =
reinterpret_cast<const char *
>(ArMemHdr) -
164 return malformedError(
"long name offset characters after the '/' are "
165 "not all decimal numbers: '" + Buf +
"' for "
166 "archive member header at offset " +
167 Twine(ArchiveOffset));
172 uint64_t ArchiveOffset =
reinterpret_cast<const char *
>(ArMemHdr) -
175 "the end of the string table for archive member "
176 "header at offset " +
Twine(ArchiveOffset));
196 uint64_t ArchiveOffset =
reinterpret_cast<const char *
>(ArMemHdr) -
198 return malformedError(
"long name length characters after the #1/ are "
199 "not all decimal numbers: '" + Buf +
"' for "
200 "archive member header at offset " +
201 Twine(ArchiveOffset));
204 uint64_t ArchiveOffset =
reinterpret_cast<const char *
>(ArMemHdr) -
207 " extends past the end of the member or archive "
208 "for archive member header at offset " +
209 Twine(ArchiveOffset));
212 NameLength).
rtrim(
'\0');
216 if (Name[Name.
size() - 1] !=
'/')
217 return Name.
rtrim(
' ');
230 sizeof(ArMemHdr->Size)).
rtrim(
" "));
232 uint64_t
Offset =
reinterpret_cast<const char *
>(ArMemHdr) -
234 return malformedError(
"characters in size field in archive header are not "
235 "all decimal numbers: '" + Buf +
"' for archive "
236 "member header at offset " +
Twine(Offset));
248 sizeof(ArMemHdr->AccessMode)).
rtrim(
" "));
250 uint64_t
Offset =
reinterpret_cast<const char *
>(ArMemHdr) -
252 return malformedError(
"characters in AccessMode field in archive header "
253 "are not all decimal numbers: '" + Buf +
"' for the "
254 "archive member header at offset " +
Twine(Offset));
263 sizeof(ArMemHdr->LastModified)).
rtrim(
' ')
268 sizeof(ArMemHdr->LastModified)).
rtrim(
" "));
270 uint64_t
Offset =
reinterpret_cast<const char *
>(ArMemHdr) -
272 return malformedError(
"characters in LastModified field in archive header "
273 "are not all decimal numbers: '" + Buf +
"' for the "
274 "archive member header at offset " +
Twine(Offset));
290 uint64_t
Offset =
reinterpret_cast<const char *
>(ArMemHdr) -
292 return malformedError(
"characters in UID field in archive header "
293 "are not all decimal numbers: '" + Buf +
"' for the "
294 "archive member header at offset " +
Twine(Offset));
309 uint64_t
Offset =
reinterpret_cast<const char *
>(ArMemHdr) -
311 return malformedError(
"characters in GID field in archive header "
312 "are not all decimal numbers: '" + Buf +
"' for the "
313 "archive member header at offset " +
Twine(Offset));
319 uint16_t StartOfFile)
320 : Parent(Parent), Header(Parent, Data.data(), Data.size(), nullptr),
321 Data(Data), StartOfFile(StartOfFile) {
326 Header(Parent, Start,
337 assert(Err &&
"Err can't be nullptr if Start is not a nullptr");
360 Size += MemberSize.
get();
381 *Err =
malformedError(
"long name length characters after the #1/ are "
382 "not all decimal numbers: '" + Buf +
"' for "
383 "archive member header at offset " +
392 if (Parent->IsThin) {
398 return Data.size() - StartOfFile;
402 return Header.getSize();
410 return Parent->IsThin && Name !=
"/" && Name !=
"//";
420 return NameOrErr.takeError();
426 Parent->getMemoryBufferRef().getBufferIdentifier());
445 const std::string &FullName = *FullNameOrErr;
447 if (std::error_code EC = Buf.
getError())
449 Parent->ThinBuffers.push_back(std::move(*Buf));
450 return Parent->ThinBuffers.back()->getBuffer();
454 size_t SpaceToSkip =
Data.size();
459 const char *NextLoc =
Data.data() + SpaceToSkip;
462 if (NextLoc == Parent->Data.getBufferEnd())
463 return Child(
nullptr,
nullptr,
nullptr);
466 if (NextLoc > Parent->Data.getBufferEnd()) {
467 std::string Msg(
"offset to next archive member past the end of the archive "
472 uint64_t
Offset =
Data.data() - Parent->getData().data();
481 return std::move(Err);
486 const char *a = Parent->Data.getBuffer().data();
487 const char *c =
Data.data();
488 uint64_t offset = c - a;
496 uint64_t RawSize = RawSizeOrErr.
get();
523 return std::move(*BinaryOrErr);
524 return BinaryOrErr.takeError();
529 std::unique_ptr<Archive>
Ret(
new Archive(Source, Err));
531 return std::move(Err);
532 return std::move(Ret);
535 void Archive::setFirstRegular(
const Child &
C) {
536 FirstRegularData = C.Data;
537 FirstRegularStartOfFile = C.StartOfFile;
550 Err = make_error<GenericBinaryError>(
"File too small to be an archive",
575 auto Increment = [&]() {
609 if (Name ==
"__.SYMDEF" || Name ==
"__.SYMDEF_64") {
610 if (Name ==
"__.SYMDEF")
621 SymbolTable = BufOrErr.
get();
638 Name = NameOrErr.
get();
639 if (Name ==
"__.SYMDEF SORTED" || Name ==
"__.SYMDEF") {
647 SymbolTable = BufOrErr.
get();
651 else if (Name ==
"__.SYMDEF_64 SORTED" || Name ==
"__.SYMDEF_64") {
660 SymbolTable = BufOrErr.
get();
673 bool has64SymTable =
false;
674 if (Name ==
"/" || Name ==
"/SYM64/") {
682 SymbolTable = BufOrErr.
get();
683 if (Name ==
"/SYM64/")
684 has64SymTable =
true;
697 Name = NameOrErr.
get();
709 StringTable = BufOrErr.
get();
717 if (Name[0] !=
'/') {
737 SymbolTable = BufOrErr.
get();
753 Name = NameOrErr.
get();
763 StringTable = BufOrErr.
get();
773 bool SkipInternal)
const {
779 FirstRegularStartOfFile),
798 const char *Buf = Parent->getSymbolTable().begin();
801 Offsets +=
sizeof(uint64_t);
805 if (Parent->kind() ==
K_GNU) {
806 Offset =
read32be(Offsets + SymbolIndex * 4);
807 }
else if (Parent->kind() ==
K_MIPS64) {
808 Offset =
read64be(Offsets + SymbolIndex * 8);
809 }
else if (Parent->kind() ==
K_BSD) {
816 Offset =
read32le(Offsets + SymbolIndex * 8 + 4);
824 Offset =
read64le(Offsets + SymbolIndex * 16 + 8);
828 Buf += MemberCount * 4 + 4;
831 if (SymbolIndex >= SymbolCount)
835 const char *Indices = Buf + 4;
839 uint16_t OffsetIndex =
read16le(Indices + SymbolIndex * 2);
843 if (OffsetIndex >= MemberCount)
846 Offset =
read32le(Offsets + OffsetIndex * 4);
849 const char *Loc = Parent->getData().begin() +
Offset;
851 Child C(Parent, Loc, &Err);
853 return std::move(Err);
859 if (Parent->kind() ==
K_BSD) {
873 const char *Buf = Parent->getSymbolTable().begin();
879 if (t.SymbolIndex + 1 < RanlibCount) {
880 const char *Ranlibs = Buf + 4;
883 CurRanStrx =
read32le(Ranlibs + t.SymbolIndex * 8);
884 NextRanStrx =
read32le(Ranlibs + (t.SymbolIndex + 1) * 8);
885 t.StringIndex -= CurRanStrx;
886 t.StringIndex += NextRanStrx;
890 t.StringIndex = Parent->getSymbolTable().find(
'\0', t.StringIndex) + 1;
906 uint64_t symbol_count =
read64be(buf);
907 buf +=
sizeof(uint64_t) + (symbol_count * (
sizeof(uint64_t)));
917 const char *ranlibs = buf + 4;
931 uint64_t ranlib_count = 0;
933 const char *ranlibs = buf + 8;
934 uint64_t ran_strx = 0;
936 buf +=
sizeof(uint64_t) + (ranlib_count * (2 * (
sizeof(uint64_t))));
938 buf +=
sizeof(uint64_t);
944 buf += 4 + (member_count * 4);
946 buf += 4 + (symbol_count * 2);
970 buf += 4 + (member_count * 4);
978 for (; bs != es; ++bs) {
980 if (SymName == name) {
982 return Child(*MemberOrErr);
984 return MemberOrErr.takeError();
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
const_iterator end(StringRef path)
Get end iterator over path.
std::error_code getError() const
Represents either an error or a value T.
symbol_iterator symbol_end() const
static const char *const ThinMagic
Expected< Optional< Child > > findSym(StringRef name) const
Expected< uint64_t > getRawSize() const
Archive(MemoryBufferRef Source, Error &Err)
uint64_t getChildOffset() const
StringRef getStringTable() const
child_iterator child_begin(Error &Err, bool SkipInternal=true) const
const char * getBufferStart() const
StringRef getSymbolTable() const
uint16_t read16le(const void *P)
Error takeError()
Take ownership of the stored error.
uint64_t read64be(const void *P)
LLVM_ATTRIBUTE_ALWAYS_INLINE TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Expected< uint64_t > getSize() const
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr)
Create a Binary from Source, autodetecting the file type.
Child(const Archive *Parent, const char *Start, Error *Err)
Expected< StringRef > getName() const
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
std::string str() const
Return the twine contents as a std::string.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
uint32_t read32be(const void *P)
StringRef getData() const
static StringRef getName(Value *V)
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
LLVM_NODISCARD StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
Tagged union holding either a T or a Error.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
MemoryBufferRef getMemoryBufferRef() const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
static Expected< std::unique_ptr< Archive > > create(MemoryBufferRef Source)
bool hasSymbolTable() const
Expected< Child > getNext() const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
child_iterator child_end() const
This is an important class for using LLVM in a threaded context.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
StringRef getName() const
static const unsigned End
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Expected< std::string > getFullName() const
void consumeError(Error Err)
Consume a Error without doing anything.
size_t getBufferSize() const
static const char *const Magic
Expected< std::unique_ptr< Binary > > getAsBinary(LLVMContext *Context=nullptr) const
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '', ' ', '"', and anything that doesn't satisfy std::isprint into an escape...
static ErrorSuccess success()
Create a success value.
symbol_iterator symbol_begin() const
StringRef getBuffer() const
reference get()
Returns a reference to the stored T value.
Helper for Errors used as out-parameters.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Expected< StringRef > getRawName() const
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
uint64_t read64le(const void *P)
static Error malformedError(Twine Msg)
Expected< Child > getMember() const
uint32_t read32le(const void *P)
uint32_t getNumberOfSymbols() const
Provides ErrorOr<T> smart pointer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Lightweight error class with error context and mandatory checking.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Expected< StringRef > getBuffer() const
StringRef - Represent a constant reference to a string, i.e.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Expected< MemoryBufferRef > getMemoryBufferRef() const