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 | |
Logan Chien | f11da30 | 2011-07-10 18:37:49 +0800 | [diff] [blame] | 21 | #include "DebugHelper.h" |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 22 | #include "ExecutionEngine/Compiler.h" |
| 23 | |
| 24 | #include "llvm/MC/MCAsmInfo.h" |
| 25 | #include "llvm/MC/MCDisassembler.h" |
| 26 | #include "llvm/MC/MCInst.h" |
| 27 | #include "llvm/MC/MCInstPrinter.h" |
| 28 | |
| 29 | #include "llvm/Support/MemoryObject.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | |
| 32 | #include "llvm/Target/TargetData.h" |
| 33 | #include "llvm/Target/TargetMachine.h" |
| 34 | #include "llvm/Target/TargetOptions.h" |
Andrew Hsieh | 7c8c59e | 2011-11-20 22:38:42 -0800 | [diff] [blame] | 35 | #include "llvm/Support/TargetRegistry.h" |
| 36 | #include "llvm/Support/TargetSelect.h" |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 37 | |
| 38 | #include "llvm/LLVMContext.h" |
| 39 | |
| 40 | #if USE_DISASSEMBLER |
| 41 | |
| 42 | namespace { |
| 43 | |
| 44 | class BufferMemoryObject : public llvm::MemoryObject { |
| 45 | private: |
| 46 | const uint8_t *mBytes; |
| 47 | uint64_t mLength; |
| 48 | |
| 49 | public: |
| 50 | BufferMemoryObject(const uint8_t *Bytes, uint64_t Length) |
| 51 | : mBytes(Bytes), mLength(Length) { |
| 52 | } |
| 53 | |
| 54 | virtual uint64_t getBase() const { return 0; } |
| 55 | virtual uint64_t getExtent() const { return mLength; } |
| 56 | |
| 57 | virtual int readByte(uint64_t Addr, uint8_t *Byte) const { |
| 58 | if (Addr > getExtent()) |
| 59 | return -1; |
| 60 | *Byte = mBytes[Addr]; |
| 61 | return 0; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | } // namespace anonymous |
| 66 | |
| 67 | namespace bcc { |
| 68 | |
| 69 | void InitializeDisassembler() { |
Logan Chien | 3bb7707 | 2011-09-17 16:53:53 +0800 | [diff] [blame] | 70 | #if defined(PROVIDE_ARM_CODEGEN) |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 71 | LLVMInitializeARMDisassembler(); |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 72 | #endif |
| 73 | |
Logan Chien | 3bb7707 | 2011-09-17 16:53:53 +0800 | [diff] [blame] | 74 | #if defined(PROVIDE_X86_CODEGEN) |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 75 | LLVMInitializeX86Disassembler(); |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 76 | #endif |
| 77 | } |
| 78 | |
| 79 | void Disassemble(char const *OutputFileName, |
| 80 | llvm::Target const *Target, |
| 81 | llvm::TargetMachine *TM, |
| 82 | std::string const &Name, |
| 83 | unsigned char const *Func, |
| 84 | size_t FuncSize) { |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 85 | |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 86 | std::string ErrorInfo; |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 87 | |
Logan Chien | f11da30 | 2011-07-10 18:37:49 +0800 | [diff] [blame] | 88 | // Open the disassembler output file |
| 89 | llvm::raw_fd_ostream OS(OutputFileName, ErrorInfo, |
| 90 | llvm::raw_fd_ostream::F_Append); |
| 91 | |
| 92 | if (!ErrorInfo.empty()) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame^] | 93 | ALOGE("Unable to open disassembler output file: %s\n", OutputFileName); |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 94 | return; |
| 95 | } |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 96 | |
Logan Chien | f11da30 | 2011-07-10 18:37:49 +0800 | [diff] [blame] | 97 | // Disassemble the given function |
| 98 | OS << "Disassembled code: " << Name << "\n"; |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 99 | |
| 100 | const llvm::MCAsmInfo *AsmInfo; |
Andrew Hsieh | 7c8c59e | 2011-11-20 22:38:42 -0800 | [diff] [blame] | 101 | const llvm::MCSubtargetInfo *SubtargetInfo; |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 102 | const llvm::MCDisassembler *Disassmbler; |
| 103 | llvm::MCInstPrinter *IP; |
| 104 | |
Shih-wei Liao | bb5df5c | 2011-07-19 05:15:11 -0700 | [diff] [blame] | 105 | AsmInfo = Target->createMCAsmInfo(Compiler::getTargetTriple()); |
Andrew Hsieh | 7c8c59e | 2011-11-20 22:38:42 -0800 | [diff] [blame] | 106 | SubtargetInfo = Target->createMCSubtargetInfo(Compiler::getTargetTriple(), "", ""); |
| 107 | Disassmbler = Target->createMCDisassembler(*SubtargetInfo); |
Shih-wei Liao | bb5df5c | 2011-07-19 05:15:11 -0700 | [diff] [blame] | 108 | IP = Target->createMCInstPrinter(AsmInfo->getAssemblerDialect(), |
Andrew Hsieh | 7c8c59e | 2011-11-20 22:38:42 -0800 | [diff] [blame] | 109 | *AsmInfo, *SubtargetInfo); |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 110 | |
| 111 | const BufferMemoryObject *BufferMObj = new BufferMemoryObject(Func, FuncSize); |
| 112 | |
| 113 | uint64_t Size; |
| 114 | uint64_t Index; |
| 115 | |
| 116 | for (Index = 0; Index < FuncSize; Index += Size) { |
| 117 | llvm::MCInst Inst; |
| 118 | |
| 119 | if (Disassmbler->getInstruction(Inst, Size, *BufferMObj, Index, |
Andrew Hsieh | 7c8c59e | 2011-11-20 22:38:42 -0800 | [diff] [blame] | 120 | /* REMOVED */ llvm::nulls(), llvm::nulls())) { |
Logan Chien | f11da30 | 2011-07-10 18:37:49 +0800 | [diff] [blame] | 121 | OS.indent(4); |
| 122 | OS.write("0x", 2); |
| 123 | OS.write_hex((uint32_t)Func + Index); |
| 124 | OS.write(": 0x", 4); |
| 125 | OS.write_hex(*(uint32_t *)(Func + Index)); |
Andrew Hsieh | 7c8c59e | 2011-11-20 22:38:42 -0800 | [diff] [blame] | 126 | IP->printInst(&Inst, OS, ""); |
Logan Chien | f11da30 | 2011-07-10 18:37:49 +0800 | [diff] [blame] | 127 | OS << "\n"; |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 128 | } else { |
| 129 | if (Size == 0) |
| 130 | Size = 1; // skip illegible bytes |
| 131 | } |
| 132 | } |
| 133 | |
Logan Chien | f11da30 | 2011-07-10 18:37:49 +0800 | [diff] [blame] | 134 | OS << "\n"; |
| 135 | |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 136 | delete BufferMObj; |
| 137 | |
| 138 | delete AsmInfo; |
| 139 | delete Disassmbler; |
| 140 | delete IP; |
| 141 | |
Logan Chien | f11da30 | 2011-07-10 18:37:49 +0800 | [diff] [blame] | 142 | OS.close(); |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace bcc |
| 146 | |
| 147 | #endif // USE_DISASSEMBLER |