File indexing completed on 2024-05-12 05:43:26

0001 /*
0002     Copyright (C) 2013-2014 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #ifndef DISASSEMBLER_H
0019 #define DISASSEMBLER_H
0020 
0021 #include <cstdint>
0022 
0023 class QString;
0024 
0025 class ElfFile;
0026 class ElfPltEntry;
0027 class ElfSection;
0028 class ElfSymbolTableEntry;
0029 class ElfGotEntry;
0030 
0031 class DwarfLine;
0032 
0033 class Disassembler
0034 {
0035 public:
0036     Disassembler();
0037     Disassembler(const Disassembler&) = delete;
0038     virtual ~Disassembler();
0039 
0040     Disassembler& operator=(const Disassembler&) = delete;
0041 
0042     QString disassemble(ElfSymbolTableEntry *entry);
0043     QString disassemble(ElfSection *section);
0044     QString disassemble(ElfPltEntry *entry);
0045 
0046     // internal
0047     ElfFile* file() const;
0048     uint64_t baseAddress() const;
0049     void printAddress(uint64_t addr, QString *s) const;
0050 
0051     /** Pretty-print symbol name, override for adding navigation links etc. */
0052     virtual QString printSymbol(ElfSymbolTableEntry *entry) const;
0053     /** ditto, for .got entries. */
0054     virtual QString printGotEntry(ElfGotEntry *entry) const;
0055     /** ditto, for .plt entries. */
0056     virtual QString printPltEntry(ElfPltEntry *entry) const;
0057 
0058 private:
0059     QString disassemble(const unsigned char* data, uint64_t size);
0060     QString disassembleBinutils(const unsigned char* data, uint64_t size);
0061     QString disassembleCapstone(const unsigned char* data, uint64_t size);
0062 
0063     DwarfLine lineForAddress(uint64_t addr) const;
0064     QString printSourceLine(DwarfLine line) const;
0065 
0066     ElfFile *m_file = nullptr;
0067     uint64_t m_baseAddress = 0;
0068 };
0069 
0070 #endif // DISASSEMBLER_H