diff --git a/llvm/include/llvm/Object/Archive.h b/llvm/include/llvm/Object/Archive.h --- a/llvm/include/llvm/Object/Archive.h +++ b/llvm/include/llvm/Object/Archive.h @@ -378,10 +378,10 @@ uint64_t getArchiveMagicLen() const; void setFirstRegular(const Child &C); -private: StringRef SymbolTable; StringRef StringTable; +private: StringRef FirstRegularData; uint16_t FirstRegularStartOfFile = -1; diff --git a/llvm/lib/Object/Archive.cpp b/llvm/lib/Object/Archive.cpp --- a/llvm/lib/Object/Archive.cpp +++ b/llvm/lib/Object/Archive.cpp @@ -952,14 +952,15 @@ Expected Archive::Symbol::getMember() const { const char *Buf = Parent->getSymbolTable().begin(); const char *Offsets = Buf; - if (Parent->kind() == K_GNU64 || Parent->kind() == K_DARWIN64) + if (Parent->kind() == K_GNU64 || Parent->kind() == K_DARWIN64 || + Parent->kind() == K_AIXBIG) Offsets += sizeof(uint64_t); else Offsets += sizeof(uint32_t); uint64_t Offset = 0; if (Parent->kind() == K_GNU) { Offset = read32be(Offsets + SymbolIndex * 4); - } else if (Parent->kind() == K_GNU64) { + } else if (Parent->kind() == K_GNU64 || Parent->kind() == K_AIXBIG) { Offset = read64be(Offsets + SymbolIndex * 8); } else if (Parent->kind() == K_BSD) { // The SymbolIndex is an index into the ranlib structs that start at @@ -1092,6 +1093,8 @@ // Skip the byte count of the string table. buf += sizeof(uint64_t); buf += ran_strx; + } else if (kind() == K_AIXBIG) { + buf = getStringTable().begin(); } else { uint32_t member_count = 0; uint32_t symbol_count = 0; @@ -1114,7 +1117,7 @@ const char *buf = getSymbolTable().begin(); if (kind() == K_GNU) return read32be(buf); - if (kind() == K_GNU64) + if (kind() == K_GNU64 || kind() == K_AIXBIG) return read64be(buf); if (kind() == K_BSD) return read32le(buf) / 8; @@ -1167,6 +1170,48 @@ Err = malformedError("malformed AIX big archive: last member offset \"" + RawOffset + "\" is not a number"); + // Calculate the global symbol table. + uint64_t GlobSymOffset = 0; + RawOffset = getFieldRawString(ArFixLenHdr->GlobSymOffset); + if (RawOffset.getAsInteger(10, GlobSymOffset)) + Err = malformedError( + "malformed AIX big archive: global symbol tables offset \"" + + RawOffset + "\" is not a number"); + + if (Err) + return; + + if (GlobSymOffset > 0) { + uint64_t BufferSize = Data.getBufferSize(); + if (GlobSymOffset + sizeof(BigArMemHdrType) > BufferSize) { + Err = malformedError("Global symbol table out of file"); + return; + } + + const char *GlobSymTblLoc = Data.getBufferStart() + GlobSymOffset; + const BigArMemHdrType *GlobalSymHdr = + reinterpret_cast(GlobSymTblLoc); + RawOffset = getFieldRawString(GlobalSymHdr->Size); + uint64_t Size; + if (RawOffset.getAsInteger(10, Size)) { + Err = malformedError( + "malformed AIX big archive: global symbol table size \"" + RawOffset + + "\" is not a number"); + return; + } + if (GlobSymOffset + sizeof(BigArMemHdrType) + Size > BufferSize) { + Err = malformedError("Global symbol table out of file"); + return; + } + SymbolTable = StringRef(GlobSymTblLoc + sizeof(BigArMemHdrType), Size); + unsigned SymNum = getNumberOfSymbols(); + unsigned SizeOfSymOffSets = 8 * (SymNum + 1); + uint64_t SymbolTableStringSize = Size - SizeOfSymOffSets; + StringTable = + StringRef(GlobSymTblLoc + sizeof(BigArMemHdrType) + SizeOfSymOffSets, + SymbolTableStringSize); + } + child_iterator I = child_begin(Err, false); if (Err) return; diff --git a/llvm/test/tools/llvm-ar/Inputs/bigarchive-global-symbol-table-malform1.a b/llvm/test/tools/llvm-ar/Inputs/bigarchive-global-symbol-table-malform1.a new file mode 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 GIT binary patch literal 0 Hc$@&1 | FileCheck -DFILE=%p/Inputs/bigarchive-global-symbol-table-malform1.a %s +#RUN: not llvm-ar t %p/Inputs/bigarchive-global-symbol-table-malform2.a 2>&1 | FileCheck -DFILE=%p/Inputs/bigarchive-global-symbol-table-malform2.a %s + +# CHECK: error: unable to load '[[FILE]]': truncated or malformed archive (Global symbol table out of file)