From b7c2d05a1a83dd91052ca6df20c2f60c802e773e Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Fri, 22 Feb 2019 22:05:19 +0100 Subject: Add support for goto to selected symbol address. Signed-off-by: Pavel Pisa --- qtmips_machine/programloader.cpp | 33 +++++++++++++ qtmips_machine/programloader.h | 2 + qtmips_machine/qtmips_machine.pro | 6 ++- qtmips_machine/qtmipsmachine.cpp | 13 +++++- qtmips_machine/qtmipsmachine.h | 5 +- qtmips_machine/symboltable.cpp | 98 +++++++++++++++++++++++++++++++++++++++ qtmips_machine/symboltable.h | 82 ++++++++++++++++++++++++++++++++ 7 files changed, 235 insertions(+), 4 deletions(-) create mode 100644 qtmips_machine/symboltable.cpp create mode 100644 qtmips_machine/symboltable.h (limited to 'qtmips_machine') diff --git a/qtmips_machine/programloader.cpp b/qtmips_machine/programloader.cpp index 750fd00..859bab4 100644 --- a/qtmips_machine/programloader.cpp +++ b/qtmips_machine/programloader.cpp @@ -130,3 +130,36 @@ std::uint32_t ProgramLoader::end() { std::uint32_t ProgramLoader::get_executable_entry() { return executable_entry; } + +SymbolTable *ProgramLoader::get_symbol_table() { + SymbolTable *p_st = new SymbolTable(); + Elf_Scn *scn = NULL; + GElf_Shdr shdr; + Elf_Data *data; + int count, ii; + + elf_version(EV_CURRENT); + + while (1) { + if ((scn = elf_nextscn(this->elf, scn)) == NULL) + return p_st; + gelf_getshdr(scn, &shdr); + if (shdr.sh_type == SHT_SYMTAB) { + /* found a symbol table, go print it. */ + break; + } + } + + data = elf_getdata(scn, NULL); + count = shdr.sh_size / shdr.sh_entsize; + + /* rettrieve the symbol names */ + for (ii = 0; ii < count; ++ii) { + GElf_Sym sym; + gelf_getsym(data, ii, &sym); + p_st->add_symbol(elf_strptr(elf, shdr.sh_link, sym.st_name), + sym.st_value, sym.st_size, sym.st_info, sym.st_other); + } + + return p_st; +} diff --git a/qtmips_machine/programloader.h b/qtmips_machine/programloader.h index 607bdb0..b275c2f 100644 --- a/qtmips_machine/programloader.h +++ b/qtmips_machine/programloader.h @@ -43,6 +43,7 @@ #include #include #include +#include "symboltable.h" namespace machine { @@ -55,6 +56,7 @@ public: void to_memory(Memory *mem); // Writes all loaded sections to memory std::uint32_t end(); // Return address after which there is no more code for sure std::uint32_t get_executable_entry(); + SymbolTable *get_symbol_table(); private: int fd; Elf *elf; diff --git a/qtmips_machine/qtmips_machine.pro b/qtmips_machine/qtmips_machine.pro index 961800c..9964357 100644 --- a/qtmips_machine/qtmips_machine.pro +++ b/qtmips_machine/qtmips_machine.pro @@ -27,7 +27,8 @@ SOURCES += \ physaddrspace.cpp \ peripheral.cpp \ serialport.cpp \ - peripspiled.cpp + peripspiled.cpp \ + symboltable.cpp HEADERS += \ qtmipsmachine.h \ @@ -45,4 +46,5 @@ HEADERS += \ physaddrspace.h \ peripheral.h \ serialport.h \ - peripspiled.h + peripspiled.h \ + symboltable.h diff --git a/qtmips_machine/qtmipsmachine.cpp b/qtmips_machine/qtmipsmachine.cpp index 37e5461..e1ecf93 100644 --- a/qtmips_machine/qtmipsmachine.cpp +++ b/qtmips_machine/qtmipsmachine.cpp @@ -39,13 +39,17 @@ using namespace machine; -QtMipsMachine::QtMipsMachine(const MachineConfig &cc) : QObject(), mcnf(&cc) { +QtMipsMachine::QtMipsMachine(const MachineConfig &cc, bool load_symtab) : + QObject(), mcnf(&cc) { MemoryAccess *cpu_mem; stat = ST_READY; + symtab = nullptr; ProgramLoader program(cc.elf()); mem_program_only = new Memory(); program.to_memory(mem_program_only); + if (load_symtab) + symtab = program.get_symbol_table(); program_end = program.end(); regs = new Registers(); if (program.get_executable_entry()) @@ -102,6 +106,9 @@ QtMipsMachine::~QtMipsMachine() { if (mem_program_only != nullptr) delete mem_program_only; mem_program_only = nullptr; + if (symtab != nullptr) + delete symtab; + symtab = nullptr; } const MachineConfig &QtMipsMachine::config() { @@ -145,6 +152,10 @@ PeripSpiLed *QtMipsMachine::peripheral_spi_led() { return perip_spi_led; } +const SymbolTable *QtMipsMachine::symbol_table() { + return symtab; +} + const Core *QtMipsMachine::core() { return cr; } diff --git a/qtmips_machine/qtmipsmachine.h b/qtmips_machine/qtmipsmachine.h index b49cb2f..1c80b9c 100644 --- a/qtmips_machine/qtmipsmachine.h +++ b/qtmips_machine/qtmipsmachine.h @@ -49,13 +49,14 @@ #include #include #include +#include namespace machine { class QtMipsMachine : public QObject { Q_OBJECT public: - QtMipsMachine(const MachineConfig &cc); + QtMipsMachine(const MachineConfig &cc, bool load_symtab = false); ~QtMipsMachine(); const MachineConfig &config(); @@ -69,6 +70,7 @@ public: Cache *cache_data_rw(); SerialPort *serial_port(); PeripSpiLed *peripheral_spi_led(); + const SymbolTable *symbol_table(); const Core *core(); const CoreSingle *core_singe(); const CorePipelined *core_pipelined(); @@ -122,6 +124,7 @@ private: QTimer *run_t; unsigned int time_chunk; + SymbolTable *symtab; std::uint32_t program_end; enum Status stat; void set_status(enum Status st); diff --git a/qtmips_machine/symboltable.cpp b/qtmips_machine/symboltable.cpp new file mode 100644 index 0000000..f22acaa --- /dev/null +++ b/qtmips_machine/symboltable.cpp @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#include "symboltable.h" + +using namespace machine; + +SymbolTableEntry::SymbolTableEntry(QString name, std::uint32_t value, + std::uint32_t size, unsigned char info, unsigned char other) { + this->name = name; + this->value = value; + this->size = size; + this->info = info; + this->other = other; +} + +SymbolTable::SymbolTable(QObject *parent) : QObject(parent), + map_value_to_symbol(), map_name_to_symbol() { +} + +SymbolTable::~SymbolTable() { + while (!map_value_to_symbol.isEmpty()) { + SymbolTableEntry *p_ste = map_value_to_symbol.first(); + p_ste = map_value_to_symbol.take(p_ste->value); + map_name_to_symbol.remove(p_ste->name); + delete p_ste; + } +} + +void SymbolTable::add_symbol(QString name, std::uint32_t value, std::uint32_t size, + unsigned char info, unsigned char other) { + SymbolTableEntry *p_ste = new SymbolTableEntry(name, value, size, info, other); + map_value_to_symbol.insert(value, p_ste); + map_name_to_symbol.insert(name, p_ste); +} + +bool SymbolTable::name_to_value(std::uint32_t &value, QString name) const { + SymbolTableEntry *p_ste = map_name_to_symbol.value(name); + if (p_ste == nullptr) { + value = 0; + return false; + } + value = p_ste->value; + return true; +} + +bool SymbolTable::value_to_name(QString &name, std::uint32_t value) const { + SymbolTableEntry *p_ste = map_value_to_symbol.value(value); + if (p_ste == nullptr) { + name = ""; + return false; + } + name = p_ste->name; + return true; +} + +QStringList *SymbolTable::names() const { + QStringList *l = new QStringList(); + + auto i = map_name_to_symbol.begin(); + while (i != map_name_to_symbol.end()) { + l->append(i.value()->name); + i++; + } + return l; +} diff --git a/qtmips_machine/symboltable.h b/qtmips_machine/symboltable.h new file mode 100644 index 0000000..174917c --- /dev/null +++ b/qtmips_machine/symboltable.h @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#ifndef SYMBOLTABLE_H +#define SYMBOLTABLE_H + +#include +#include +#include +#include +#include + +namespace machine { + +class SymbolTableEntry { + friend class SymbolTable; + SymbolTableEntry(QString name, std::uint32_t value, std::uint32_t size, + unsigned char info = 0, unsigned char other = 0); +protected: + QString name; + std::uint32_t value; + std::uint32_t size; + unsigned char info; + unsigned char other; +}; + +class SymbolTable : public QObject +{ + Q_OBJECT +public: + SymbolTable(QObject *parent = 0); + ~SymbolTable(); + + void add_symbol(QString name, std::uint32_t value, std::uint32_t size, + unsigned char info = 0, unsigned char other = 0); + QStringList *names() const; +public slots: + bool name_to_value(std::uint32_t &value, QString name) const; + bool value_to_name(QString &name, std::uint32_t value) const; +signals: + + +private: + QMultiMap map_value_to_symbol; + QMap map_name_to_symbol; +}; + +} + +#endif // SYMBOLTABLE_H -- cgit v1.2.3