From 7afab9f42ffc778c2cba060ef20a3179797947f7 Mon Sep 17 00:00:00 2001 From: Alexander Shaposhnikov Date: Thu, 19 Sep 2019 00:02:12 +0000 Subject: [PATCH] [Object] Extend MachOUniversalBinary::getObjectForArch Make the method MachOUniversalBinary::getObjectForArch return MachOUniversalBinary::ObjectForArch and add helper methods MachOUniversalBinary::getMachOObjectForArch, MachOUniversalBinary::getArchiveForArch for those who explicitly expect to get a MachOObjectFile or an Archive. Differential revision: https://reviews.llvm.org/D67700 Test plan: make check-all llvm-svn: 372278 --- include/llvm/Object/MachOUniversal.h | 8 ++++++- lib/DebugInfo/Symbolize/Symbolize.cpp | 2 +- lib/Object/MachOUniversal.cpp | 23 +++++++++++++++---- lib/Object/Object.cpp | 2 +- .../Coverage/CoverageMappingReader.cpp | 2 +- tools/llvm-lipo/llvm-lipo.cpp | 7 +++--- tools/llvm-objdump/MachODump.cpp | 2 +- 7 files changed, 33 insertions(+), 13 deletions(-) diff --git a/include/llvm/Object/MachOUniversal.h b/include/llvm/Object/MachOUniversal.h index 4eb06526f7d..490d73f4c7e 100644 --- a/include/llvm/Object/MachOUniversal.h +++ b/include/llvm/Object/MachOUniversal.h @@ -159,8 +159,14 @@ public: return V->isMachOUniversalBinary(); } - Expected> + Expected getObjectForArch(StringRef ArchName) const; + + Expected> + getMachOObjectForArch(StringRef ArchName) const; + + Expected> + getArchiveForArch(StringRef ArchName) const; }; } diff --git a/lib/DebugInfo/Symbolize/Symbolize.cpp b/lib/DebugInfo/Symbolize/Symbolize.cpp index a5559373b05..8e1852bcf8e 100644 --- a/lib/DebugInfo/Symbolize/Symbolize.cpp +++ b/lib/DebugInfo/Symbolize/Symbolize.cpp @@ -401,7 +401,7 @@ LLVMSymbolizer::getOrCreateObject(const std::string &Path, return I->second.get(); Expected> ObjOrErr = - UB->getObjectForArch(ArchName); + UB->getMachOObjectForArch(ArchName); if (!ObjOrErr) { ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName), std::unique_ptr()); diff --git a/lib/Object/MachOUniversal.cpp b/lib/Object/MachOUniversal.cpp index 8b5cd0518a5..3e5be913c25 100644 --- a/lib/Object/MachOUniversal.cpp +++ b/lib/Object/MachOUniversal.cpp @@ -210,19 +210,34 @@ MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err) Err = Error::success(); } -Expected> +Expected MachOUniversalBinary::getObjectForArch(StringRef ArchName) const { if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch) return make_error("Unknown architecture " "named: " + ArchName, object_error::arch_not_found); - - for (auto &Obj : objects()) + for (const auto &Obj : objects()) if (Obj.getArchFlagName() == ArchName) - return Obj.getAsObjectFile(); + return Obj; return make_error("fat file does not " "contain " + ArchName, object_error::arch_not_found); } + +Expected> +MachOUniversalBinary::getMachOObjectForArch(StringRef ArchName) const { + Expected O = getObjectForArch(ArchName); + if (!O) + return O.takeError(); + return O->getAsObjectFile(); +} + +Expected> +MachOUniversalBinary::getArchiveForArch(StringRef ArchName) const { + Expected O = getObjectForArch(ArchName); + if (!O) + return O.takeError(); + return O->getAsArchive(); +} diff --git a/lib/Object/Object.cpp b/lib/Object/Object.cpp index b518e2d02c9..b486e9f5c9a 100644 --- a/lib/Object/Object.cpp +++ b/lib/Object/Object.cpp @@ -138,7 +138,7 @@ LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR, char **ErrorMessage) { auto universal = cast(unwrap(BR)); Expected> ObjOrErr( - universal->getObjectForArch({Arch, ArchLen})); + universal->getMachOObjectForArch({Arch, ArchLen})); if (!ObjOrErr) { *ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str()); return nullptr; diff --git a/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/lib/ProfileData/Coverage/CoverageMappingReader.cpp index 4e0b911c142..6fffacfde74 100644 --- a/lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ b/lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -682,7 +682,7 @@ loadBinaryFormat(std::unique_ptr Bin, StringRef Arch) { if (auto *Universal = dyn_cast(Bin.get())) { // If we have a universal binary, try to look up the object for the // appropriate architecture. - auto ObjectFileOrErr = Universal->getObjectForArch(Arch); + auto ObjectFileOrErr = Universal->getMachOObjectForArch(Arch); if (!ObjectFileOrErr) return ObjectFileOrErr.takeError(); OF = std::move(ObjectFileOrErr.get()); diff --git a/tools/llvm-lipo/llvm-lipo.cpp b/tools/llvm-lipo/llvm-lipo.cpp index da5889f37d5..a8389039dc2 100644 --- a/tools/llvm-lipo/llvm-lipo.cpp +++ b/tools/llvm-lipo/llvm-lipo.cpp @@ -306,9 +306,8 @@ static void verifyArch(ArrayRef> InputBinaries, if (auto UO = dyn_cast(InputBinaries.front().getBinary())) { for (StringRef Arch : VerifyArchList) { - Expected> Obj = - UO->getObjectForArch(Arch); - if (!Obj) + auto ObjForArch = UO->getObjectForArch(Arch); + if (!ObjForArch) exit(EXIT_FAILURE); } } else if (auto O = @@ -399,7 +398,7 @@ static void extractSlice(ArrayRef> InputBinaries, auto *UO = cast(InputBinaries.front().getBinary()); Expected> Obj = - UO->getObjectForArch(ThinArchType); + UO->getMachOObjectForArch(ThinArchType); if (!Obj) reportError("fat input file " + UO->getFileName() + " does not contain the specified architecture " + ThinArchType + diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index 5f4ff0c636c..25b721ae558 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -7387,7 +7387,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, Triple T = MachOObjectFile::getArchTriple(CPUType, CPUSubType, nullptr, &ArchFlag); Expected> MachDSYM = - UB->getObjectForArch(ArchFlag); + UB->getMachOObjectForArch(ArchFlag); if (!MachDSYM) { reportError(MachDSYM.takeError(), DSYMFile); return;