Allow public Triple deduction from ObjectFiles.

Move logic that allows for Triple deduction from an ObjectFile object
out of llvm-objdump.cpp into a public factory, found in the ObjectFile
class.

This should allow other tools in the future to use this logic without
reimplementation.

Patch by Mitch Phillips

Differential Revision: https://reviews.llvm.org/D37719

llvm-svn: 313605
This commit is contained in:
Vlad Tsyrklevich
2017-09-19 02:22:48 +00:00
parent b7fdaf2cd4
commit de62046d68
3 changed files with 30 additions and 20 deletions

View File

@@ -79,6 +79,31 @@ section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
return section_iterator(SectionRef(Sec, this));
}
Triple ObjectFile::makeTriple() const {
Triple TheTriple;
auto Arch = getArch();
TheTriple.setArch(Triple::ArchType(Arch));
// For ARM targets, try to use the build attributes to build determine
// the build target. Target features are also added, but later during
// disassembly.
if (Arch == Triple::arm || Arch == Triple::armeb)
setARMSubArch(TheTriple);
// TheTriple defaults to ELF, and COFF doesn't have an environment:
// the best we can do here is indicate that it is mach-o.
if (isMachO())
TheTriple.setObjectFormat(Triple::MachO);
if (isCOFF()) {
const auto COFFObj = dyn_cast<COFFObjectFile>(this);
if (COFFObj->getArch() == Triple::thumb)
TheTriple.setTriple("thumbv7-windows");
}
return TheTriple;
}
Expected<std::unique_ptr<ObjectFile>>
ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) {
StringRef Data = Object.getBuffer();