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 @@ -405,6 +405,7 @@ const FixLenHdr *ArFixLenHdr; uint64_t FirstChildOffset = 0; uint64_t LastChildOffset = 0; + std::string MergedGloSymTblBuf; public: BigArchive(MemoryBufferRef Source, Error &Err); 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 @@ -18,6 +18,7 @@ #include "llvm/Object/Error.h" #include "llvm/Support/Chrono.h" #include "llvm/Support/Endian.h" +#include "llvm/Support/EndianStream.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/FileSystem.h" @@ -1167,6 +1168,58 @@ bool Archive::hasSymbolTable() const { return !SymbolTable.empty(); } +static Error GetGlobSymTblLocAndSize(const MemoryBufferRef &Data, + uint64_t GlobalSymOffset, + const char *&GlobSymTblLoc, uint64_t &Size, + const char *BitMessage) { + uint64_t BufferSize = Data.getBufferSize(); + uint64_t GlobalSymTblContentOffset = + GlobalSymOffset + sizeof(BigArMemHdrType); + if (GlobalSymTblContentOffset > BufferSize) + return malformedError(Twine(BitMessage) + + " global symbol table header at offset 0x" + + Twine::utohexstr(GlobalSymOffset) + " and size 0x" + + Twine::utohexstr(sizeof(BigArMemHdrType)) + + " goes past the end of file"); + + GlobSymTblLoc = Data.getBufferStart() + GlobalSymOffset; + const BigArMemHdrType *GlobalSymHdr = + reinterpret_cast(GlobSymTblLoc); + StringRef RawOffset = getFieldRawString(GlobalSymHdr->Size); + if (RawOffset.getAsInteger(10, Size)) + return malformedError("malformed AIX big archive: " + Twine(BitMessage) + + " global symbol table size \"" + RawOffset + + "\" is not a number"); + + if (GlobalSymTblContentOffset + Size > BufferSize) + return malformedError( + Twine(BitMessage) + " global symbol table content at offset 0x" + + Twine::utohexstr(GlobalSymTblContentOffset) + " and size 0x" + + Twine::utohexstr(Size) + " goes past the end of file"); + + return Error::success(); +} + +struct GlobalSymTblInfo { + uint64_t SymNum; + StringRef SymbolTable; + StringRef SymbolOffestTable; + StringRef StringTable; +}; + +static void GetGlobalSymbolTableInfo(SmallVector &SymTblInfos, + const char *GlobSymTblLoc, uint64_t Size) { + StringRef SymbolTable = + StringRef(GlobSymTblLoc + sizeof(BigArMemHdrType), Size); + uint64_t SymNum = read64be(GlobSymTblLoc + sizeof(BigArMemHdrType)); + StringRef SymbolOffestTable = StringRef(SymbolTable.data() + 8, 8 * SymNum); + unsigned SymOffsetsSize = 8 * (SymNum + 1); + uint64_t SymbolTableStringSize = Size - SymOffsetsSize; + StringRef StringTable = + StringRef(SymbolTable.data() + SymOffsetsSize, SymbolTableStringSize); + SymTblInfos.push_back({SymNum, SymbolTable, SymbolOffestTable, StringTable}); +} + BigArchive::BigArchive(MemoryBufferRef Source, Error &Err) : Archive(Source, Err) { ErrorAsOutParameter ErrAsOutParam(&Err); @@ -1185,56 +1238,71 @@ Err = malformedError("malformed AIX big archive: last member offset \"" + RawOffset + "\" is not a number"); - // Calculate the global symbol table. - uint64_t GlobSymOffset = 0; + uint64_t GlobSym32Offset = 0; RawOffset = getFieldRawString(ArFixLenHdr->GlobSymOffset); - if (RawOffset.getAsInteger(10, GlobSymOffset)) - // TODO: add test case. - Err = malformedError( - "malformed AIX big archive: global symbol table offset \"" + RawOffset + - "\" is not a number"); + if (RawOffset.getAsInteger(10, GlobSym32Offset)) { + Err = malformedError("malformed AIX big archive: global symbol table " + "offset of 32-bit members \"" + + RawOffset + "\" is not a number"); + return; + } - if (Err) + uint64_t GlobSym64Offset = 0; + RawOffset = getFieldRawString(ArFixLenHdr->GlobSym64Offset); + if (RawOffset.getAsInteger(10, GlobSym64Offset)) { + Err = malformedError("malformed AIX big archive: global symbol table " + "offset of 64-bit members\"" + + RawOffset + "\" is not a number"); return; + } - if (GlobSymOffset > 0) { - uint64_t BufferSize = Data.getBufferSize(); - uint64_t GlobalSymTblContentOffset = - GlobSymOffset + sizeof(BigArMemHdrType); - if (GlobalSymTblContentOffset > BufferSize) { - Err = malformedError("global symbol table header at offset 0x" + - Twine::utohexstr(GlobSymOffset) + " and size 0x" + - Twine::utohexstr(sizeof(BigArMemHdrType)) + - " goes past the end of file"); - return; - } + const char *GlobSym32TblLoc = nullptr; + const char *GlobSym64TblLoc = nullptr; + uint64_t GlobSym32Size = 0; + uint64_t GlobSym64Size = 0; + const MemoryBufferRef &MemBuffRef = getMemoryBufferRef(); - const char *GlobSymTblLoc = Data.getBufferStart() + GlobSymOffset; - const BigArMemHdrType *GlobalSymHdr = - reinterpret_cast(GlobSymTblLoc); - RawOffset = getFieldRawString(GlobalSymHdr->Size); - uint64_t Size; - if (RawOffset.getAsInteger(10, Size)) { - // TODO: add test case. - Err = malformedError( - "malformed AIX big archive: global symbol table size \"" + RawOffset + - "\" is not a number"); + if (GlobSym32Offset) { + Err = GetGlobSymTblLocAndSize(MemBuffRef, GlobSym32Offset, GlobSym32TblLoc, + GlobSym32Size, "32-bit"); + if (Err) return; - } - if (GlobalSymTblContentOffset + Size > BufferSize) { - Err = malformedError("global symbol table content at offset 0x" + - Twine::utohexstr(GlobalSymTblContentOffset) + - " and size 0x" + Twine::utohexstr(Size) + - " goes past the end of file"); + } + + if (GlobSym64Offset) { + Err = GetGlobSymTblLocAndSize(MemBuffRef, GlobSym64Offset, GlobSym64TblLoc, + GlobSym64Size, "64-bit"); + if (Err) return; - } - SymbolTable = StringRef(GlobSymTblLoc + sizeof(BigArMemHdrType), Size); - unsigned SymNum = getNumberOfSymbols(); - unsigned SymOffsetsSize = 8 * (SymNum + 1); - uint64_t SymbolTableStringSize = Size - SymOffsetsSize; - StringTable = - StringRef(GlobSymTblLoc + sizeof(BigArMemHdrType) + SymOffsetsSize, - SymbolTableStringSize); + } + + SmallVector SymTblInfos; + + if (GlobSym32Offset) + GetGlobalSymbolTableInfo(SymTblInfos, GlobSym32TblLoc, GlobSym32Size); + if (GlobSym64Offset) + GetGlobalSymbolTableInfo(SymTblInfos, GlobSym64TblLoc, GlobSym64Size); + + if (SymTblInfos.size() == 1) { + SymbolTable = SymTblInfos[0].SymbolTable; + StringTable = SymTblInfos[0].StringTable; + } else if (SymTblInfos.size() == 2) { + // In order to let the Archive::Symbol::getNext() work for 32-bit and 64-bit + // global symbol table, we need to merge two global symbol table into + // one. + raw_string_ostream Out(MergedGloSymTblBuf); + uint64_t SymNum = SymTblInfos[0].SymNum + SymTblInfos[0].SymNum; + write(Out, SymNum, support::big); + // Merge symbol offset. + Out << SymTblInfos[0].SymbolOffestTable; + Out << SymTblInfos[1].SymbolOffestTable; + // Merge string table. + Out << SymTblInfos[0].StringTable; + Out << SymTblInfos[1].StringTable; + SymbolTable = MergedGloSymTblBuf; + StringTable = StringRef(SymbolTable.begin() + (SymNum + 1) * 8, + SymTblInfos[0].StringTable.size() + + SymTblInfos[1].StringTable.size()); } child_iterator I = child_begin(Err, false); diff --git a/llvm/lib/Object/ArchiveWriter.cpp b/llvm/lib/Object/ArchiveWriter.cpp --- a/llvm/lib/Object/ArchiveWriter.cpp +++ b/llvm/lib/Object/ArchiveWriter.cpp @@ -42,6 +42,7 @@ #endif using namespace llvm; +using namespace llvm::object; NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef) : Buf(MemoryBuffer::getMemBuffer(BufRef, false)), @@ -384,37 +385,76 @@ static void writeSymbolTableHeader(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, uint64_t Size, - uint64_t PrevMemberOffset = 0) { + uint64_t PrevMemberOffset = 0, + uint64_t NextMemberOffset = 0) { if (isBSDLike(Kind)) { const char *Name = is64BitKind(Kind) ? "__.SYMDEF_64" : "__.SYMDEF"; printBSDMemberHeader(Out, Out.tell(), Name, now(Deterministic), 0, 0, 0, Size); } else if (isAIXBigArchive(Kind)) { - printBigArchiveMemberHeader(Out, "", now(Deterministic), 0, 0, - 0, Size, PrevMemberOffset, 0); + printBigArchiveMemberHeader(Out, "", now(Deterministic), 0, 0, 0, Size, + PrevMemberOffset, NextMemberOffset); } else { const char *Name = is64BitKind(Kind) ? "/SYM64" : ""; printGNUSmallMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size); } } +static Expected> +getSymbolFile(MemoryBufferRef Buf, LLVMContext &Context) { + std::unique_ptr Obj; + const file_magic Type = identify_magic(Buf.getBuffer()); + // Treat non symbolic file types as nullptr. + if (!object::SymbolicFile::isSymbolicFile(Type, &Context)) + return nullptr; + if (Type == file_magic::bitcode) { + auto ObjOrErr = object::SymbolicFile::createSymbolicFile( + Buf, file_magic::bitcode, &Context); + if (!ObjOrErr) + return ObjOrErr.takeError(); + return std::move(*ObjOrErr); + } else { + auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf); + if (!ObjOrErr) + return ObjOrErr.takeError(); + return std::move(*ObjOrErr); + } +} + +static Expected is64BitSymbolicFile(const StringRef &ObjStringRef) { + MemoryBufferRef ObjMbf(ObjStringRef, ""); + // In the scenario when LLVMContext is populated SymbolicFile will contain a + // reference to it, thus SymbolicFile should be destroyed first. + LLVMContext Context; + Expected> ObjOrErr = + getSymbolFile(ObjMbf, Context); + if (!ObjOrErr) + return ObjOrErr.takeError(); + + // Treated non symbolic file types as false. + if (!*ObjOrErr) + return false; + + return (*ObjOrErr)->is64Bit(); +} + static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef Members, - StringRef StringTable, - uint64_t PrevMemberOffset = 0) { + StringRef StringTable, unsigned NumSyms, + uint64_t PrevMemberOffset = 0, + uint64_t NextMemberOffset = 0, + bool Is64Bit = false) { // We don't write a symbol table on an archive with no members -- except on // Darwin, where the linker will abort unless the archive has a symbol table. if (StringTable.empty() && !isDarwin(Kind)) return; - unsigned NumSyms = 0; - for (const MemberData &M : Members) - NumSyms += M.Symbols.size(); - uint64_t OffsetSize = is64BitKind(Kind) ? 8 : 4; uint32_t Pad; - uint64_t Size = computeSymbolTableSize(Kind, NumSyms, OffsetSize, StringTable, &Pad); - writeSymbolTableHeader(Out, Kind, Deterministic, Size, PrevMemberOffset); + uint64_t Size = + computeSymbolTableSize(Kind, NumSyms, OffsetSize, StringTable, &Pad); + writeSymbolTableHeader(Out, Kind, Deterministic, Size, PrevMemberOffset, + NextMemberOffset); uint64_t Pos = isAIXBigArchive(Kind) ? sizeof(object::BigArchive::FixLenHdr) : Out.tell() + Size; @@ -425,6 +465,18 @@ printNBits(Out, Kind, NumSyms); for (const MemberData &M : Members) { + if (isAIXBigArchive(Kind)) { + Expected Is64BitOrErr = is64BitSymbolicFile(M.Data); + // If there is an error, the error will be emit when 'computeMemberData' + // call 'getSymbol' function, we use consumeError here. + if (!Is64BitOrErr) + consumeError(Is64BitOrErr.takeError()); + if (*Is64BitOrErr != Is64Bit) { + Pos += M.Header.size() + M.Data.size() + M.Padding.size(); + continue; + } + } + for (unsigned StringOffset : M.Symbols) { if (isBSDLike(Kind)) printNBits(Out, Kind, StringOffset); @@ -444,29 +496,21 @@ static Expected> getSymbols(MemoryBufferRef Buf, raw_ostream &SymNames, bool &HasObject) { - std::vector Ret; - // In the scenario when LLVMContext is populated SymbolicFile will contain a // reference to it, thus SymbolicFile should be destroyed first. LLVMContext Context; - std::unique_ptr Obj; - const file_magic Type = identify_magic(Buf.getBuffer()); - // Treat unsupported file types as having no symbols. - if (!object::SymbolicFile::isSymbolicFile(Type, &Context)) + std::vector Ret; + Expected> ObjOrErr = + getSymbolFile(Buf, Context); + if (!ObjOrErr) + return ObjOrErr.takeError(); + + // If the member is not a symbolic file, treated as no symbol. + if (!*ObjOrErr) return Ret; - if (Type == file_magic::bitcode) { - auto ObjOrErr = object::SymbolicFile::createSymbolicFile( - Buf, file_magic::bitcode, &Context); - if (!ObjOrErr) - return ObjOrErr.takeError(); - Obj = std::move(*ObjOrErr); - } else { - auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf); - if (!ObjOrErr) - return ObjOrErr.takeError(); - Obj = std::move(*ObjOrErr); - } + + std::unique_ptr Obj = std::move(*ObjOrErr); HasObject = true; for (const object::BasicSymbolRef &S : Obj->symbols()) { @@ -683,12 +727,29 @@ isAIXBigArchive(Kind) ? sizeof(object::BigArchive::FixLenHdr) : 8; uint64_t LastMemberHeaderOffset = LastMemberEndOffset; uint64_t NumSyms = 0; + uint64_t NumSyms32 = 0; // Store symbol number of 32-bits member files. + for (const auto &M : Data) { // Record the start of the member's offset LastMemberHeaderOffset = LastMemberEndOffset; // Account for the size of each part associated with the member. LastMemberEndOffset += M.Header.size() + M.Data.size() + M.Padding.size(); NumSyms += M.Symbols.size(); + + // In AIX OS, the big archive file contains two global symbol tables. The + // first global symbol table locates 32-bit file members that define global + // symbols; the second global symbol table does the same for 64-bit file + // members. A big archive can has 32-bit and 64-bit file members, we need to + // know the symbol number of 32-bit global symbol table and symbol number of + // 64-bit global table. + if (isAIXBigArchive(Kind) && WriteSymtab) { + Expected Is64BitOrErr = is64BitSymbolicFile(M.Data); + if (Error E = Is64BitOrErr.takeError()) + return E; + + if (!*Is64BitOrErr) + NumSyms32 += M.Symbols.size(); + } } // The symbol table is put at the end of the big archive file. The symbol @@ -737,7 +798,7 @@ if (!isAIXBigArchive(Kind)) { if (WriteSymtab) - writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf); + writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf, NumSyms); for (const MemberData &M : Data) Out << M.Header << M.Data << M.Padding; } else { @@ -765,21 +826,57 @@ 20 * MemberOffsets.size() + MemberTableNameStrTblSize; - unsigned GlobalSymbolOffset = - (WriteSymtab && NumSyms > 0) + SmallString<0> SymNamesBuf32; + SmallString<0> SymNamesBuf64; + raw_svector_ostream SymNames32(SymNamesBuf32); + raw_svector_ostream SymNames64(SymNamesBuf64); + + if (WriteSymtab && NumSyms) + // Generate the 32-bits object member symbol name string and 64-bits' one. + for (const NewArchiveMember &M : NewMembers) { + MemoryBufferRef Buf = M.Buf->getMemBufferRef(); + Expected Is64BitOrErr = is64BitSymbolicFile(Buf.getBuffer()); + if (!Is64BitOrErr) + consumeError(Is64BitOrErr.takeError()); + + bool HasObject; + Expected> SymbolsOrErr = + getSymbols(Buf, *Is64BitOrErr ? SymNames64 : SymNames32, HasObject); + if (!SymbolsOrErr) + return SymbolsOrErr.takeError(); + } + + // In AIX OS, The 'GlobSymOffset' field in the fixed-length header contains + // the offset to the 32-bit global symbol table, and the 'GlobSym64Offset' + // contains the offset to the 64-bit global symbol table. + uint64_t GlobalSymbolOffset = + (WriteSymtab && NumSyms32 > 0) ? LastMemberEndOffset + alignTo(sizeof(object::BigArMemHdrType) + MemberTableSize, 2) : 0; + uint64_t GlobalSymbolOffset64 = 0; + uint64_t NumSyms64 = NumSyms - NumSyms32; + if (WriteSymtab && NumSyms64 > 0) { + if (GlobalSymbolOffset == 0) + GlobalSymbolOffset64 = + LastMemberEndOffset + + alignTo(sizeof(object::BigArMemHdrType) + MemberTableSize, 2); + else + // If there is global symbol table for 32-bit member file, + // the 64 bit global symbol table is after for 32-bit one. + GlobalSymbolOffset64 = + GlobalSymbolOffset + sizeof(object::BigArMemHdrType) + + (NumSyms32 + 1) * 8 + alignTo(SymNamesBuf32.size(), 2); + } + // Fixed Sized Header. printWithSpacePadding(Out, NewMembers.size() ? LastMemberEndOffset : 0, 20); // Offset to member table // If there are no file members in the archive, there will be no global // symbol table. printWithSpacePadding(Out, NewMembers.size() ? GlobalSymbolOffset : 0, 20); - printWithSpacePadding( - Out, 0, - 20); // Offset to 64 bits global symbol table - Not supported yet + printWithSpacePadding(Out, GlobalSymbolOffset64, 20); printWithSpacePadding( Out, NewMembers.size() ? sizeof(object::BigArchive::FixLenHdr) : 0, 20); // Offset to first archive member @@ -799,8 +896,10 @@ // Member table. printBigArchiveMemberHeader(Out, "", sys::toTimePoint(0), 0, 0, 0, MemberTableSize, LastMemberHeaderOffset, - GlobalSymbolOffset); - printWithSpacePadding(Out, MemberOffsets.size(), 20); // Number of members + GlobalSymbolOffset ? GlobalSymbolOffset + : GlobalSymbolOffset64); + printWithSpacePadding(Out, MemberOffsets.size(), + 20); // Number of members for (uint64_t MemberOffset : MemberOffsets) printWithSpacePadding(Out, MemberOffset, 20); // Offset to member file header. @@ -811,9 +910,25 @@ Out << '\0'; // Name table must be tail padded to an even number of // bytes. - if (WriteSymtab && NumSyms > 0) - writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf, - LastMemberEndOffset); + if (WriteSymtab) { + // Write global table for 32-bit file members. + if (GlobalSymbolOffset) { + writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf32, + NumSyms32, LastMemberEndOffset, + GlobalSymbolOffset64); + // Align the 32-bit global symbol table for 64-bit global symbol table + // if there is. + if (GlobalSymbolOffset64 && (SymNamesBuf32.size() % 2)) + Out.write(uint8_t(0)); + } + + // Write global table for 64-bit file members. + if (GlobalSymbolOffset64) + writeSymbolTable( + Out, Kind, Deterministic, Data, SymNamesBuf64, NumSyms64, + GlobalSymbolOffset ? GlobalSymbolOffset : LastMemberEndOffset, 0, + true); + } } } Out.flush(); diff --git a/llvm/test/Object/Inputs/bitcode-sym32.ll b/llvm/test/Object/Inputs/bitcode-sym32.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Object/Inputs/bitcode-sym32.ll @@ -0,0 +1,14 @@ +target triple = "powerpcle-unknown-linux-gnu" + +@C32 = dso_local global i32 5, align 4 +@undef_var32 = external dso_local global i32, align 4 + +define dso_local i32 @foo32(i32 %i) #0 { +entry: + %i.addr = alloca i32, align 4 + store i32 %i, ptr %i.addr, align 4 + %0 = load i32, ptr %i.addr, align 4 + %1 = load i32, ptr @undef_var32, align 4 + %add = add nsw i32 %0, %1 + ret i32 %add +} diff --git a/llvm/test/Object/Inputs/bitcode-sym64.ll b/llvm/test/Object/Inputs/bitcode-sym64.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Object/Inputs/bitcode-sym64.ll @@ -0,0 +1,12 @@ +target triple = "powerpc64le-unknown-linux-gnu" + +@C64 = dso_local global i32 5, align 4 +@static_var64 = internal global i32 2, align 4 + +define dso_local signext i32 @bar64() #0 { +entry: + %0 = load i32, ptr @static_var64, align 4 + %1 = load i32, ptr @C64, align 4 + %add = add nsw i32 %0, %1 + ret i32 %add +} diff --git a/llvm/test/Object/bigarchive-malformed-global-symbol-table b/llvm/test/Object/bigarchive-malformed-global-symbol-table new file mode 100644 --- /dev/null +++ b/llvm/test/Object/bigarchive-malformed-global-symbol-table @@ -0,0 +1,49 @@ +## Test malformed global symbal table of big archive. + +# RUN: rm -rf %t && mkdir %t && cd %t +# RUN: yaml2obj -DFLAG=0x1DF %s -o t32.o +# RUN: yaml2obj -DFLAG=0x1F7 %s -o t64.o +# RUN: llvm-ar q t.a t32.o t64.o + +# RUN: cp t.a t32_1.a +# RUN: cp t.a t32_2.a +# RUN: cp t.a t64_1.a +# RUN: cp t.a t64_2.a + +## Truncate the file to end before the 32-bits global symbol table header ends. +# RUN: %python -c "with open('t32_1.a', 'r+b') as input: input.truncate(856)" +## Truncate the file to end before the 32-bits global symbol table ends. +# RUN: %python -c "with open('t32_2.a', 'r+b') as input: input.truncate(995)" + +# RUN: not llvm-ar t t32_1.a 2>&1 | FileCheck -DFILE=t32_1.a --check-prefixes=HEADER32 %s +# RUN: not llvm-ar t t32_2.a 2>&1 | FileCheck -DFILE=t32_2.a --check-prefixes=CONTENT32 %s + +# HEADER32: error: unable to load '[[FILE]]': truncated or malformed archive (32-bits global symbol table header at offset 0x34e and size 0x72 goes past the end of file) +# CONTENT32: error: unable to load '[[FILE]]': truncated or malformed archive (32-bits global symbol table content at offset 0x3c0 and size 0x2b goes past the end of file) + +## Truncate the file to end before the 64-bits global symbol table header ends. +# RUN: %python -c "with open('t64_1.a', 'r+b') as input: input.truncate(1014)" +## Truncate the file to end before the 64-bits global symbol table ends. +# RUN: %python -c "with open('t64_2.a', 'r+b') as input: input.truncate(1096)" + +# RUN: not llvm-ar t t64_1.a 2>&1 | FileCheck -DFILE=t64_1.a --check-prefixes=HEADER64 %s +# RUN: not llvm-ar t t64_2.a 2>&1 | FileCheck -DFILE=t64_2.a --check-prefixes=CONTENT64 %s + +# HEADER64: error: unable to load '[[FILE]]': truncated or malformed archive (64-bits global symbol table header at offset 0x3ec and size 0x72 goes past the end of file) +# CONTENT64: error: unable to load '[[FILE]]': truncated or malformed archive (64-bits global symbol table header at offset 0x3ec and size 0x72 goes past the end of file) + +--- !XCOFF +FileHeader: + MagicNumber: [[FLAG]] +Sections: + - Name: .data + Flags: [ STYP_DATA ] +Symbols: + - Name: export_protected_var_[[FLAG]] + Section: .data + Type: 0x4000 + StorageClass: C_EXT + AuxEntries: + - Type: AUX_CSECT + SymbolAlignmentAndType: 0x09 + StorageMappingClass: XMC_RW diff --git a/llvm/test/Object/bigarchive-malformed-header.test b/llvm/test/Object/bigarchive-malformed-header.test new file mode 100644 --- /dev/null +++ b/llvm/test/Object/bigarchive-malformed-header.test @@ -0,0 +1,13 @@ +## Test malformed big archive file in Fixed-Length header field. + +# RUN: echo "" > %t_mal_globalsym.a +# RUN: echo -n "0 1i28 0 0 0 0 " >> %t_mal_globalsym.a +# RUN: not llvm-ar tv %t_mal_globalsym.a 2>&1 | FileCheck %s -DFILE=%t_mal_globalsym.a --check-prefixes=GLOBOFFSET + +# GLOBOFFSET: llvm-ar: error: unable to load '[[FILE]]': truncated or malformed archive (malformed AIX big archive: global symbol table offset of 32-bit members "1i28" is not a number) + +# RUN: echo "" > %t_mal_globalsym64.a +# RUN: echo -n "0 0 1i28 0 0 0 " >> %t_mal_globalsym64.a +# RUN: not llvm-ar tv %t_mal_globalsym64.a 2>&1 | FileCheck %s -DFILE=%t_mal_globalsym64.a --check-prefixes=GLOB64OFFSET + +# GLOB64OFFSET: llvm-ar: error: unable to load '[[FILE]]': truncated or malformed archive (malformed AIX big archive: global symbol table offset of 64-bit members"1i28" is not a number) diff --git a/llvm/test/Object/bigarchive-symboltable.test b/llvm/test/Object/bigarchive-symboltable.test new file mode 100644 --- /dev/null +++ b/llvm/test/Object/bigarchive-symboltable.test @@ -0,0 +1,98 @@ +## Test global symbal table of big archive. + +# RUN: rm -rf %t && mkdir %t && cd %t +# RUN: yaml2obj --docnum=1 -DFLAG=0x1DF %s -o t32_1.o +# RUN: yaml2obj --docnum=1 -DFLAG=0x1F7 %s -o t64_1.o +# RUN: yaml2obj --docnum=2 -DFLAG=0x1DF %s -o t32_2.o +# RUN: yaml2obj --docnum=2 -DFLAG=0x1F7 %s -o t64_2.o +# RUN: llvm-as -o 32.bc %p/Inputs/bitcode-sym32.ll +# RUN: llvm-as -o 64.bc %p/Inputs/bitcode-sym64.ll + +## Test printing a big archive which only has 32-bits symbol names +# RUN: llvm-ar q t32.a t32_1.o t32_2.o 32.bc +# RUN: llvm-nm --print-armap t32.a 2>&1 | FileCheck --check-prefixes=GLOB32,NOGL64 %s + +## Test printing a big archive which only has 64-bits symbol names +# RUN: llvm-ar q t64.a t64_1.o t64_2.o 64.bc +# RUN: llvm-nm --print-armap t64.a 2>&1 | FileCheck --check-prefixes=GLOB64,NOGL32 %s + +## Test printing all 32-bits symbol names first and then 64-bits +# RUN: llvm-ar q t32_64_all.a t32_1.o t64_1.o t32_2.o t64_2.o 32.bc 64.bc + +# RUN: llvm-nm --print-armap t32_64_all.a 2>&1 | FileCheck --check-prefixes=GLOB32,GLOB64 %s + +#GLOB32: var_0x1DF in t32_1.o +#GLOB32-NEXT: array_0x1DF in t32_1.o +#GLOB32-NEXT: func_0x1DF in t32_2.o +#GLOB32-NEXT: bar_0x1DF in t32_2.o +#GLOB32-NEXT: foo32 in 32.bc +#GLOB32-NEXT: C32 in 32.bc + +#NOGL32-NOT: var_0x1DF in t32_1.o +#NOGL32-NOT: array_0x1DF in t32_1.o +#NOGL32-NOT: func_0x1DF in t32_2.o +#NOGL32-NOT: bar_0x1DF in t32_2.o +#NOGL32-NOT: foo32 in 32.bc +#NOGL32-NOT: C32 in 32.bc + +#GLOB64: var_0x1F7 in t64_1.o +#GLOB64-NEXT: array_0x1F7 in t64_1.o +#GLOB64-NEXT: func_0x1F7 in t64_2.o +#GLOB64-NEXT: bar_0x1F7 in t64_2.o +#GLOB64-NEXT: bar64 in 64.bc +#GLOB64-NEXT: C64 in 64.bc + +#NOGL64-NOT: var_0x1F7 in t64_1.o +#NOGL64-NOT: array_0x1F7 in t64_1.o +#NOGL64-NOT: func_0x1F7 in t64_2.o +#NOGL64-NOT: bar_0x1F7 in t64_2.o +#NOGL64-NOT: bar64 in 64.bc +#NOGL64-NOT: C64 in 64.bc + +--- !XCOFF +FileHeader: + MagicNumber: [[FLAG]] +Sections: + - Name: .data + Flags: [ STYP_DATA ] +Symbols: + - Name: var_[[FLAG]] + Section: .data + Type: 0x4000 + StorageClass: C_EXT + AuxEntries: + - Type: AUX_CSECT + SymbolAlignmentAndType: 0x09 + StorageMappingClass: XMC_RW + - Name: array_[[FLAG]] + Section: .data + Type: 0x4000 + StorageClass: C_EXT + AuxEntries: + - Type: AUX_CSECT + SymbolAlignmentAndType: 0x09 + StorageMappingClass: XMC_RW + +--- !XCOFF +FileHeader: + MagicNumber: [[FLAG]] +Sections: + - Name: .text + Flags: [ STYP_DATA ] +Symbols: + - Name: func_[[FLAG]] + Section: .text + Type: 0x4000 + StorageClass: C_EXT + AuxEntries: + - Type: AUX_CSECT + SymbolAlignmentAndType: 0x09 + StorageMappingClass: XMC_PR + - Name: bar_[[FLAG]] + Section: .text + Type: 0x4000 + StorageClass: C_EXT + AuxEntries: + - Type: AUX_CSECT + SymbolAlignmentAndType: 0x09 + StorageMappingClass: XMC_PR diff --git a/llvm/test/tools/llvm-ar/malformed-global-symbol-table-bigarchive.test b/llvm/test/tools/llvm-ar/malformed-global-symbol-table-bigarchive.test deleted file mode 100644 --- a/llvm/test/tools/llvm-ar/malformed-global-symbol-table-bigarchive.test +++ /dev/null @@ -1,33 +0,0 @@ -## Test malformed global symbal table of big archive. - -# RUN: rm -rf %t && mkdir %t && cd %t -# RUN: yaml2obj %s -o t.o -# RUN: llvm-ar q t.a t.o -# RUN: cp t.a t2.a - -## Truncate the file to end before the global symbol table header ends. -# RUN: %python -c "with open('t.a', 'r+b') as input: input.truncate(560)" -## Truncate the file to end before the global symbol table ends. -# RUN: %python -c "with open('t2.a', 'r+b') as input: input.truncate(656)" - -# RUN: not llvm-ar t t.a 2>&1 | FileCheck -DFILE=t.a %s -# RUN: not llvm-ar t t2.a 2>&1 | FileCheck -DFILE=t2.a --check-prefixes=CHECK2 %s - -# CHECK: error: unable to load '[[FILE]]': truncated or malformed archive (global symbol table header at offset 0x20e and size 0x72 goes past the end of file) -# CHECK2: error: unable to load '[[FILE]]': truncated or malformed archive (global symbol table content at offset 0x280 and size 0x25 goes past the end of file) - ---- !XCOFF -FileHeader: - MagicNumber: 0x1DF -Sections: - - Name: .data - Flags: [ STYP_DATA ] -Symbols: - - Name: export_protected_var - Section: .data - Type: 0x4000 - StorageClass: C_EXT - AuxEntries: - - Type: AUX_CSECT - SymbolAlignmentAndType: 0x09 - StorageMappingClass: XMC_RW