Skip to content

Commit

Permalink
[Object] Extend MachOUniversalBinary::getObjectForArch
Browse files Browse the repository at this point in the history
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


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@372278 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
alexander-shaposhnikov committed Sep 19, 2019
1 parent da4df3b commit b239f05
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
8 changes: 7 additions & 1 deletion include/llvm/Object/MachOUniversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ class MachOUniversalBinary : public Binary {
return V->isMachOUniversalBinary();
}

Expected<std::unique_ptr<MachOObjectFile>>
Expected<ObjectForArch>
getObjectForArch(StringRef ArchName) const;

Expected<std::unique_ptr<MachOObjectFile>>
getMachOObjectForArch(StringRef ArchName) const;

Expected<std::unique_ptr<Archive>>
getArchiveForArch(StringRef ArchName) const;
};

}
Expand Down
2 changes: 1 addition & 1 deletion lib/DebugInfo/Symbolize/Symbolize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ LLVMSymbolizer::getOrCreateObject(const std::string &Path,
return I->second.get();

Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
UB->getObjectForArch(ArchName);
UB->getMachOObjectForArch(ArchName);
if (!ObjOrErr) {
ObjectForUBPathAndArch.emplace(std::make_pair(Path, ArchName),
std::unique_ptr<ObjectFile>());
Expand Down
23 changes: 19 additions & 4 deletions lib/Object/MachOUniversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,34 @@ MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err)
Err = Error::success();
}

Expected<std::unique_ptr<MachOObjectFile>>
Expected<MachOUniversalBinary::ObjectForArch>
MachOUniversalBinary::getObjectForArch(StringRef ArchName) const {
if (Triple(ArchName).getArch() == Triple::ArchType::UnknownArch)
return make_error<GenericBinaryError>("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<GenericBinaryError>("fat file does not "
"contain " +
ArchName,
object_error::arch_not_found);
}

Expected<std::unique_ptr<MachOObjectFile>>
MachOUniversalBinary::getMachOObjectForArch(StringRef ArchName) const {
Expected<ObjectForArch> O = getObjectForArch(ArchName);
if (!O)
return O.takeError();
return O->getAsObjectFile();
}

Expected<std::unique_ptr<Archive>>
MachOUniversalBinary::getArchiveForArch(StringRef ArchName) const {
Expected<ObjectForArch> O = getObjectForArch(ArchName);
if (!O)
return O.takeError();
return O->getAsArchive();
}
2 changes: 1 addition & 1 deletion lib/Object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ LLVMBinaryRef LLVMMachOUniversalBinaryCopyObjectForArch(LLVMBinaryRef BR,
char **ErrorMessage) {
auto universal = cast<MachOUniversalBinary>(unwrap(BR));
Expected<std::unique_ptr<ObjectFile>> ObjOrErr(
universal->getObjectForArch({Arch, ArchLen}));
universal->getMachOObjectForArch({Arch, ArchLen}));
if (!ObjOrErr) {
*ErrorMessage = strdup(toString(ObjOrErr.takeError()).c_str());
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion lib/ProfileData/Coverage/CoverageMappingReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ loadBinaryFormat(std::unique_ptr<Binary> Bin, StringRef Arch) {
if (auto *Universal = dyn_cast<MachOUniversalBinary>(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());
Expand Down
7 changes: 3 additions & 4 deletions tools/llvm-lipo/llvm-lipo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,8 @@ static void verifyArch(ArrayRef<OwningBinary<Binary>> InputBinaries,
if (auto UO =
dyn_cast<MachOUniversalBinary>(InputBinaries.front().getBinary())) {
for (StringRef Arch : VerifyArchList) {
Expected<std::unique_ptr<MachOObjectFile>> Obj =
UO->getObjectForArch(Arch);
if (!Obj)
auto ObjForArch = UO->getObjectForArch(Arch);
if (!ObjForArch)
exit(EXIT_FAILURE);
}
} else if (auto O =
Expand Down Expand Up @@ -399,7 +398,7 @@ static void extractSlice(ArrayRef<OwningBinary<Binary>> InputBinaries,

auto *UO = cast<MachOUniversalBinary>(InputBinaries.front().getBinary());
Expected<std::unique_ptr<MachOObjectFile>> Obj =
UO->getObjectForArch(ThinArchType);
UO->getMachOObjectForArch(ThinArchType);
if (!Obj)
reportError("fat input file " + UO->getFileName() +
" does not contain the specified architecture " + ThinArchType +
Expand Down
2 changes: 1 addition & 1 deletion tools/llvm-objdump/MachODump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7387,7 +7387,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
Triple T = MachOObjectFile::getArchTriple(CPUType, CPUSubType, nullptr,
&ArchFlag);
Expected<std::unique_ptr<MachOObjectFile>> MachDSYM =
UB->getObjectForArch(ArchFlag);
UB->getMachOObjectForArch(ArchFlag);
if (!MachDSYM) {
reportError(MachDSYM.takeError(), DSYMFile);
return;
Expand Down

0 comments on commit b239f05

Please sign in to comment.