[llvm-lipo] Add support for -info with archive files (#155309)

Previously trying to print the info of an archive caused a crash. Now we
correctly report the architecture of the contained object files.

Fixes https://github.com/llvm/llvm-project/issues/41655
This commit is contained in:
Keith Smiley 2025-08-28 10:02:29 -07:00 committed by GitHub
parent 85c7a7f3b8
commit 30002f22b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 2 deletions

View File

@ -10,11 +10,12 @@
# RUN: llvm-ar cr %t.different_architectures.a %t-i386.o %t-x86_64.o
# RUN: not llvm-lipo %t.different_architectures.a -create -output /dev/null 2>&1 | FileCheck --check-prefix=ARCHIVE-WITH-DIFFERENT-ARCHS %s
# RUN: llvm-ar cr %t.contains_fat_binary.a %t-universal.o
# RUN: llvm-ar cr %t.contains_fat_binary.a %t-universal.o
# RUN: not llvm-lipo %t.contains_fat_binary.a -create -output /dev/null 2>&1 | FileCheck --check-prefix=ARCHIVE-WITH-FAT-BINARY %s
# RUN: llvm-ar cr %t-i386-lib.a %t-i386.o
# RUN: llvm-lipo %t-i386-lib.a %t-x86_64.o -create -output %t-i386-x86_64-universal.o
# RUN: llvm-lipo %t-i386-lib.a -info | FileCheck --check-prefix=INFO-i386 %s
# RUN: llvm-lipo %t-i386-x86_64-universal.o -info | FileCheck --check-prefix=INFO-i386-x86_64 %s
# RUN: llvm-lipo %t-i386-x86_64-universal.o -thin i386 -output %t-extracted-i386-lib.a
# RUN: cmp %t-extracted-i386-lib.a %t-i386-lib.a
@ -51,4 +52,5 @@
# ARCHIVE-WITH-DIFFERENT-ARCHS: all members must match
# ARCHIVE-WITH-FAT-BINARY: fat file (not allowed in an archive)
#
# INFO-i386: i386
# INFO-i386-x86_64: i386 x86_64

View File

@ -425,6 +425,11 @@ static void printBinaryArchs(LLVMContext &LLVMCtx, const Binary *Binary,
return;
}
if (const auto *A = dyn_cast<Archive>(Binary)) {
OS << createSliceFromArchive(LLVMCtx, *A).getArchString() << "\n";
return;
}
// This should be always the case, as this is tested in readInputBinaries
const auto *IR = cast<IRObjectFile>(Binary);
Expected<Slice> SliceOrErr = createSliceFromIR(*IR, 0);
@ -455,7 +460,8 @@ printInfo(LLVMContext &LLVMCtx, ArrayRef<OwningBinary<Binary>> InputBinaries) {
for (auto &IB : InputBinaries) {
const Binary *Binary = IB.getBinary();
if (!Binary->isMachOUniversalBinary()) {
assert(Binary->isMachO() && "expected MachO binary");
assert(Binary->isMachO() ||
Binary->isArchive() && "expected MachO binary");
outs() << "Non-fat file: " << Binary->getFileName()
<< " is architecture: ";
printBinaryArchs(LLVMCtx, Binary, outs());