Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2011, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Disassembler.h" |
| 18 | |
| 19 | #include "Config.h" |
| 20 | |
| 21 | #include "ExecutionEngine/Compiler.h" |
| 22 | |
| 23 | #include "llvm/MC/MCAsmInfo.h" |
| 24 | #include "llvm/MC/MCDisassembler.h" |
| 25 | #include "llvm/MC/MCInst.h" |
| 26 | #include "llvm/MC/MCInstPrinter.h" |
| 27 | |
| 28 | #include "llvm/Support/MemoryObject.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | |
| 31 | #include "llvm/Target/TargetData.h" |
| 32 | #include "llvm/Target/TargetMachine.h" |
| 33 | #include "llvm/Target/TargetOptions.h" |
| 34 | #include "llvm/Target/TargetRegistry.h" |
| 35 | #include "llvm/Target/TargetSelect.h" |
| 36 | |
| 37 | #include "llvm/LLVMContext.h" |
| 38 | |
| 39 | #if USE_DISASSEMBLER |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | class BufferMemoryObject : public llvm::MemoryObject { |
| 44 | private: |
| 45 | const uint8_t *mBytes; |
| 46 | uint64_t mLength; |
| 47 | |
| 48 | public: |
| 49 | BufferMemoryObject(const uint8_t *Bytes, uint64_t Length) |
| 50 | : mBytes(Bytes), mLength(Length) { |
| 51 | } |
| 52 | |
| 53 | virtual uint64_t getBase() const { return 0; } |
| 54 | virtual uint64_t getExtent() const { return mLength; } |
| 55 | |
| 56 | virtual int readByte(uint64_t Addr, uint8_t *Byte) const { |
| 57 | if (Addr > getExtent()) |
| 58 | return -1; |
| 59 | *Byte = mBytes[Addr]; |
| 60 | return 0; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | } // namespace anonymous |
| 65 | |
| 66 | namespace bcc { |
| 67 | |
| 68 | void InitializeDisassembler() { |
| 69 | #if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN) |
| 70 | LLVMInitializeARMDisassembler(); |
| 71 | LLVMInitializeARMAsmPrinter(); |
| 72 | #endif |
| 73 | |
| 74 | #if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN) |
| 75 | LLVMInitializeX86Disassembler(); |
| 76 | LLVMInitializeX86AsmPrinter(); |
| 77 | #endif |
| 78 | |
| 79 | #if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN) |
| 80 | LLVMInitializeX86Disassembler(); |
| 81 | LLVMInitializeX86AsmPrinter(); |
| 82 | #endif |
| 83 | } |
| 84 | |
| 85 | void Disassemble(char const *OutputFileName, |
| 86 | llvm::Target const *Target, |
| 87 | llvm::TargetMachine *TM, |
| 88 | std::string const &Name, |
| 89 | unsigned char const *Func, |
| 90 | size_t FuncSize) { |
| 91 | llvm::raw_ostream *OS; |
| 92 | |
| 93 | #if USE_DISASSEMBLER_FILE |
| 94 | std::string ErrorInfo; |
| 95 | OS = new llvm::raw_fd_ostream(OutputFileName, ErrorInfo, |
| 96 | llvm::raw_fd_ostream::F_Append); |
| 97 | |
| 98 | if (!ErrorInfo.empty()) { // some errors occurred |
| 99 | // LOGE("Error in creating disassembly file"); |
| 100 | delete OS; |
| 101 | return; |
| 102 | } |
| 103 | #else |
| 104 | OS = &llvm::outs(); |
| 105 | #endif |
| 106 | |
| 107 | *OS << "Disassembled code: " << Name << "\n"; |
| 108 | |
| 109 | const llvm::MCAsmInfo *AsmInfo; |
| 110 | const llvm::MCDisassembler *Disassmbler; |
| 111 | llvm::MCInstPrinter *IP; |
| 112 | |
| 113 | AsmInfo = Target->createAsmInfo(Compiler::getTargetTriple()); |
| 114 | Disassmbler = Target->createMCDisassembler(); |
| 115 | IP = Target->createMCInstPrinter(*TM, |
| 116 | AsmInfo->getAssemblerDialect(), |
| 117 | *AsmInfo); |
| 118 | |
| 119 | const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Func, FuncSize); |
| 120 | |
| 121 | uint64_t Size; |
| 122 | uint64_t Index; |
| 123 | |
| 124 | for (Index = 0; Index < FuncSize; Index += Size) { |
| 125 | llvm::MCInst Inst; |
| 126 | |
| 127 | if (Disassmbler->getInstruction(Inst, Size, *BufferMObj, Index, |
| 128 | /* REMOVED */ llvm::nulls())) { |
| 129 | OS->indent(4); |
| 130 | OS->write("0x", 2); |
| 131 | OS->write_hex((uint32_t)Func + Index); |
| 132 | OS->write(": 0x", 4); |
| 133 | OS->write_hex(*(uint32_t *)(Func + Index)); |
| 134 | IP->printInst(&Inst, *OS); |
| 135 | *OS << "\n"; |
| 136 | } else { |
| 137 | if (Size == 0) |
| 138 | Size = 1; // skip illegible bytes |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | *OS << "\n"; |
| 143 | delete BufferMObj; |
| 144 | |
| 145 | delete AsmInfo; |
| 146 | delete Disassmbler; |
| 147 | delete IP; |
| 148 | |
| 149 | #if USE_DISASSEMBLER_FILE |
| 150 | ((llvm::raw_fd_ostream*)OS)->close(); |
| 151 | delete OS; |
| 152 | #endif |
| 153 | } |
| 154 | |
| 155 | } // namespace bcc |
| 156 | |
| 157 | #endif // USE_DISASSEMBLER |