Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame^] | 1 | //===- FileAnalysis.cpp -----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "FileAnalysis.h" |
| 11 | |
| 12 | #include "llvm/BinaryFormat/ELF.h" |
| 13 | #include "llvm/MC/MCAsmInfo.h" |
| 14 | #include "llvm/MC/MCContext.h" |
| 15 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 16 | #include "llvm/MC/MCInst.h" |
| 17 | #include "llvm/MC/MCInstPrinter.h" |
| 18 | #include "llvm/MC/MCInstrAnalysis.h" |
| 19 | #include "llvm/MC/MCInstrDesc.h" |
| 20 | #include "llvm/MC/MCInstrInfo.h" |
| 21 | #include "llvm/MC/MCObjectFileInfo.h" |
| 22 | #include "llvm/MC/MCRegisterInfo.h" |
| 23 | #include "llvm/MC/MCSubtargetInfo.h" |
| 24 | #include "llvm/Object/Binary.h" |
| 25 | #include "llvm/Object/COFF.h" |
| 26 | #include "llvm/Object/ELFObjectFile.h" |
| 27 | #include "llvm/Object/ObjectFile.h" |
| 28 | #include "llvm/Support/Casting.h" |
| 29 | #include "llvm/Support/CommandLine.h" |
| 30 | #include "llvm/Support/Error.h" |
| 31 | #include "llvm/Support/FormatVariadic.h" |
| 32 | #include "llvm/Support/MemoryBuffer.h" |
| 33 | #include "llvm/Support/TargetRegistry.h" |
| 34 | #include "llvm/Support/TargetSelect.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
| 36 | |
| 37 | #include <functional> |
| 38 | |
| 39 | using Instr = llvm::cfi_verify::FileAnalysis::Instr; |
| 40 | |
| 41 | namespace llvm { |
| 42 | namespace cfi_verify { |
| 43 | |
| 44 | Expected<FileAnalysis> FileAnalysis::Create(StringRef Filename) { |
| 45 | // Open the filename provided. |
| 46 | Expected<object::OwningBinary<object::Binary>> BinaryOrErr = |
| 47 | object::createBinary(Filename); |
| 48 | if (!BinaryOrErr) |
| 49 | return BinaryOrErr.takeError(); |
| 50 | |
| 51 | // Construct the object and allow it to take ownership of the binary. |
| 52 | object::OwningBinary<object::Binary> Binary = std::move(BinaryOrErr.get()); |
| 53 | FileAnalysis Analysis(std::move(Binary)); |
| 54 | |
| 55 | Analysis.Object = dyn_cast<object::ObjectFile>(Analysis.Binary.getBinary()); |
| 56 | if (!Analysis.Object) |
| 57 | return make_error<UnsupportedDisassembly>(); |
| 58 | |
| 59 | Analysis.ObjectTriple = Analysis.Object->makeTriple(); |
| 60 | Analysis.Features = Analysis.Object->getFeatures(); |
| 61 | |
| 62 | // Init the rest of the object. |
| 63 | if (auto InitResponse = Analysis.initialiseDisassemblyMembers()) |
| 64 | return std::move(InitResponse); |
| 65 | |
| 66 | if (auto SectionParseResponse = Analysis.parseCodeSections()) |
| 67 | return std::move(SectionParseResponse); |
| 68 | |
| 69 | return std::move(Analysis); |
| 70 | } |
| 71 | |
| 72 | FileAnalysis::FileAnalysis(object::OwningBinary<object::Binary> Binary) |
| 73 | : Binary(std::move(Binary)) {} |
| 74 | |
| 75 | FileAnalysis::FileAnalysis(const Triple &ObjectTriple, |
| 76 | const SubtargetFeatures &Features) |
| 77 | : ObjectTriple(ObjectTriple), Features(Features) {} |
| 78 | |
| 79 | const Instr * |
| 80 | FileAnalysis::getPrevInstructionSequential(const Instr &InstrMeta) const { |
| 81 | std::map<uint64_t, Instr>::const_iterator KV = |
| 82 | Instructions.find(InstrMeta.VMAddress); |
| 83 | if (KV == Instructions.end() || KV == Instructions.begin()) |
| 84 | return nullptr; |
| 85 | |
| 86 | if (!(--KV)->second.Valid) |
| 87 | return nullptr; |
| 88 | |
| 89 | return &KV->second; |
| 90 | } |
| 91 | |
| 92 | const Instr * |
| 93 | FileAnalysis::getNextInstructionSequential(const Instr &InstrMeta) const { |
| 94 | std::map<uint64_t, Instr>::const_iterator KV = |
| 95 | Instructions.find(InstrMeta.VMAddress); |
| 96 | if (KV == Instructions.end() || ++KV == Instructions.end()) |
| 97 | return nullptr; |
| 98 | |
| 99 | if (!KV->second.Valid) |
| 100 | return nullptr; |
| 101 | |
| 102 | return &KV->second; |
| 103 | } |
| 104 | |
| 105 | bool FileAnalysis::usesRegisterOperand(const Instr &InstrMeta) const { |
| 106 | for (const auto &Operand : InstrMeta.Instruction) { |
| 107 | if (Operand.isReg()) |
| 108 | return true; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | const Instr *FileAnalysis::getInstruction(uint64_t Address) const { |
| 114 | const auto &InstrKV = Instructions.find(Address); |
| 115 | if (InstrKV == Instructions.end()) |
| 116 | return nullptr; |
| 117 | |
| 118 | return &InstrKV->second; |
| 119 | } |
| 120 | |
| 121 | const Instr &FileAnalysis::getInstructionOrDie(uint64_t Address) const { |
| 122 | const auto &InstrKV = Instructions.find(Address); |
| 123 | assert(InstrKV != Instructions.end() && "Address doesn't exist."); |
| 124 | return InstrKV->second; |
| 125 | } |
| 126 | |
| 127 | const std::set<uint64_t> &FileAnalysis::getIndirectInstructions() const { |
| 128 | return IndirectInstructions; |
| 129 | } |
| 130 | |
| 131 | const MCRegisterInfo *FileAnalysis::getRegisterInfo() const { |
| 132 | return RegisterInfo.get(); |
| 133 | } |
| 134 | |
| 135 | const MCInstrInfo *FileAnalysis::getMCInstrInfo() const { return MII.get(); } |
| 136 | |
| 137 | const MCInstrAnalysis *FileAnalysis::getMCInstrAnalysis() const { |
| 138 | return MIA.get(); |
| 139 | } |
| 140 | |
| 141 | Error FileAnalysis::initialiseDisassemblyMembers() { |
| 142 | std::string TripleName = ObjectTriple.getTriple(); |
| 143 | ArchName = ""; |
| 144 | MCPU = ""; |
| 145 | std::string ErrorString; |
| 146 | |
| 147 | ObjectTarget = |
| 148 | TargetRegistry::lookupTarget(ArchName, ObjectTriple, ErrorString); |
| 149 | if (!ObjectTarget) |
| 150 | return make_error<StringError>(Twine("Couldn't find target \"") + |
| 151 | ObjectTriple.getTriple() + |
| 152 | "\", failed with error: " + ErrorString, |
| 153 | inconvertibleErrorCode()); |
| 154 | |
| 155 | RegisterInfo.reset(ObjectTarget->createMCRegInfo(TripleName)); |
| 156 | if (!RegisterInfo) |
| 157 | return make_error<StringError>("Failed to initialise RegisterInfo.", |
| 158 | inconvertibleErrorCode()); |
| 159 | |
| 160 | AsmInfo.reset(ObjectTarget->createMCAsmInfo(*RegisterInfo, TripleName)); |
| 161 | if (!AsmInfo) |
| 162 | return make_error<StringError>("Failed to initialise AsmInfo.", |
| 163 | inconvertibleErrorCode()); |
| 164 | |
| 165 | SubtargetInfo.reset(ObjectTarget->createMCSubtargetInfo( |
| 166 | TripleName, MCPU, Features.getString())); |
| 167 | if (!SubtargetInfo) |
| 168 | return make_error<StringError>("Failed to initialise SubtargetInfo.", |
| 169 | inconvertibleErrorCode()); |
| 170 | |
| 171 | MII.reset(ObjectTarget->createMCInstrInfo()); |
| 172 | if (!MII) |
| 173 | return make_error<StringError>("Failed to initialise MII.", |
| 174 | inconvertibleErrorCode()); |
| 175 | |
| 176 | Context.reset(new MCContext(AsmInfo.get(), RegisterInfo.get(), &MOFI)); |
| 177 | |
| 178 | Disassembler.reset( |
| 179 | ObjectTarget->createMCDisassembler(*SubtargetInfo, *Context)); |
| 180 | |
| 181 | if (!Disassembler) |
| 182 | return make_error<StringError>("No disassembler available for target", |
| 183 | inconvertibleErrorCode()); |
| 184 | |
| 185 | MIA.reset(ObjectTarget->createMCInstrAnalysis(MII.get())); |
| 186 | |
| 187 | Printer.reset(ObjectTarget->createMCInstPrinter( |
| 188 | ObjectTriple, AsmInfo->getAssemblerDialect(), *AsmInfo, *MII, |
| 189 | *RegisterInfo)); |
| 190 | |
| 191 | return Error::success(); |
| 192 | } |
| 193 | |
| 194 | Error FileAnalysis::parseCodeSections() { |
| 195 | for (const object::SectionRef &Section : Object->sections()) { |
| 196 | // Ensure only executable sections get analysed. |
| 197 | if (!(object::ELFSectionRef(Section).getFlags() & ELF::SHF_EXECINSTR)) |
| 198 | continue; |
| 199 | |
| 200 | StringRef SectionContents; |
| 201 | if (Section.getContents(SectionContents)) |
| 202 | return make_error<StringError>("Failed to retrieve section contents", |
| 203 | inconvertibleErrorCode()); |
| 204 | |
| 205 | ArrayRef<uint8_t> SectionBytes((const uint8_t *)SectionContents.data(), |
| 206 | Section.getSize()); |
| 207 | parseSectionContents(SectionBytes, Section.getAddress()); |
| 208 | } |
| 209 | return Error::success(); |
| 210 | } |
| 211 | |
| 212 | void FileAnalysis::parseSectionContents(ArrayRef<uint8_t> SectionBytes, |
| 213 | uint64_t SectionAddress) { |
| 214 | MCInst Instruction; |
| 215 | Instr InstrMeta; |
| 216 | uint64_t InstructionSize; |
| 217 | |
| 218 | for (uint64_t Byte = 0; Byte < SectionBytes.size();) { |
| 219 | bool ValidInstruction = |
| 220 | Disassembler->getInstruction(Instruction, InstructionSize, |
| 221 | SectionBytes.drop_front(Byte), 0, nulls(), |
| 222 | outs()) == MCDisassembler::Success; |
| 223 | |
| 224 | Byte += InstructionSize; |
| 225 | |
| 226 | uint64_t VMAddress = SectionAddress + Byte - InstructionSize; |
| 227 | InstrMeta.Instruction = Instruction; |
| 228 | InstrMeta.VMAddress = VMAddress; |
| 229 | InstrMeta.InstructionSize = InstructionSize; |
| 230 | InstrMeta.Valid = ValidInstruction; |
| 231 | addInstruction(InstrMeta); |
| 232 | |
| 233 | if (!ValidInstruction) |
| 234 | continue; |
| 235 | |
| 236 | // Skip additional parsing for instructions that do not affect the control |
| 237 | // flow. |
| 238 | const auto &InstrDesc = MII->get(Instruction.getOpcode()); |
| 239 | if (!InstrDesc.mayAffectControlFlow(Instruction, *RegisterInfo)) |
| 240 | continue; |
| 241 | |
| 242 | uint64_t Target; |
| 243 | if (MIA->evaluateBranch(Instruction, VMAddress, InstructionSize, Target)) { |
| 244 | // If the target can be evaluated, it's not indirect. |
| 245 | StaticBranchTargetings[Target].push_back(VMAddress); |
| 246 | continue; |
| 247 | } |
| 248 | |
| 249 | if (!usesRegisterOperand(InstrMeta)) |
| 250 | continue; |
| 251 | |
| 252 | IndirectInstructions.insert(VMAddress); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void FileAnalysis::addInstruction(const Instr &Instruction) { |
| 257 | const auto &KV = |
| 258 | Instructions.insert(std::make_pair(Instruction.VMAddress, Instruction)); |
| 259 | if (!KV.second) { |
| 260 | errs() << "Failed to add instruction at address " |
| 261 | << format_hex(Instruction.VMAddress, 2) |
| 262 | << ": Instruction at this address already exists.\n"; |
| 263 | exit(EXIT_FAILURE); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | char UnsupportedDisassembly::ID; |
| 268 | void UnsupportedDisassembly::log(raw_ostream &OS) const { |
| 269 | OS << "Dissassembling of non-objects not currently supported.\n"; |
| 270 | } |
| 271 | |
| 272 | std::error_code UnsupportedDisassembly::convertToErrorCode() const { |
| 273 | return std::error_code(); |
| 274 | } |
| 275 | |
| 276 | } // namespace cfi_verify |
| 277 | } // namespace llvm |