rrcSmall/src/AnalysisState.h
DataHoarder 0caa10273e
All checks were successful
continuous-integration/drone/push Build is passing
Added interrupt handler known jump and parsing when found
2021-01-11 13:18:17 +01:00

100 lines
4 KiB
C++

/*****************************************************************************
* Copyright (c) 2020, rrcSmall FM10K-Documentation Contributors
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#pragma once
#include <cstdint>
#include <unordered_map>
#include <algorithm>
#include "SmallFirmwareFormat.h"
#include "instructions/AddressOffset.h"
enum class JumpKind {
Continue,
Absolute,
Speculative,
Branch,
Loop,
Return,
Interrupt
};
typedef std::unordered_map<uint32_t, std::vector<std::pair<uint32_t, JumpKind>>> AnalysisJumpTable;
class AnalysisState {
public:
std::unordered_map<uint32_t, uint32_t> memory;
uint32_t previous;
uint32_t current;
SmallFirmwareFormat pciSPICOFirmware;
SmallFirmwareFormat pciSerDesFirmware;
AnalysisJumpTable &jumpTable;
void addKnownJump(uint32_t to, uint32_t from, JumpKind kind) {
if (jumpTable.find(to) == jumpTable.end()) {
jumpTable[to] = std::vector<std::pair<uint32_t, JumpKind>>{{from, kind}};
} else if (std::find(jumpTable[to].begin(), jumpTable[to].end(), std::pair<uint32_t, JumpKind>(from, kind)) ==
jumpTable[to].end()) {
jumpTable[to].emplace_back(from, kind);
}
}
AnalysisState(uint32_t initial, AnalysisJumpTable &table) : current(initial), previous(0), jumpTable(table) {
}
AnalysisState(const AnalysisState &oldState) : current(oldState.current), previous(oldState.previous),
memory(oldState.memory), pciSPICOFirmware(oldState.pciSPICOFirmware),
pciSerDesFirmware(oldState.pciSerDesFirmware),
jumpTable(oldState.jumpTable) {
}
bool operator==(const AnalysisState &other) const {
return current == other.current && memory == other.memory;
}
void setRegister(const Instruction::AddressWithOffset &address, uint32_t value);
void setRegister(uint32_t addr, uint32_t value);
uint32_t getRegister(const Instruction::AddressWithOffset &address) const;
uint32_t getRegister(uint32_t addr) const {
return memory.find(addr) == memory.end() ? getDefaultRegisterValue(addr) : memory.at(addr);
}
static uint32_t getDefaultRegisterValue(uint32_t addr);
uint32_t getAddressWithOffset(const Instruction::AddressWithOffset &address) const;
uint32_t getAddressOffset(uint8_t offsetEntry) const;
};