Decode new rrcc extended symbols with comments
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DataHoarder 2021-08-03 01:12:49 +02:00
parent 83a1b7d280
commit 926b1d6a82
3 changed files with 73 additions and 21 deletions

View file

@ -154,7 +154,7 @@ void OutputContext::analyze() {
if (writeInstruction.data.size() == 1 &&
writeInstruction.data[0] == e.first) { //Return value!
//TODO: mark join?
addComment(writeInstruction.getAddress() + 4, locText);
addComment(writeInstruction.getAddress() + 4, locText, true);
foundReturn = true;
break;
}
@ -176,7 +176,7 @@ void OutputContext::analyze() {
}
if (e.second.size() > 1 || (!hasContinue && !e.second.empty())) {
addComment(e.first, "<" + getLabel(e.first, true) + "> " + from.str());
addComment(e.first, "<" + getLabel(e.first, true) + "> " + from.str(), true);
}
}
@ -188,10 +188,10 @@ void OutputContext::addMapping(uint32_t location, const std::string &name, bool
}
}
void OutputContext::addComment(uint32_t location, const std::string &comment) {
void OutputContext::addComment(uint32_t location, const std::string &comment, bool force) {
if (comments.find(location) != comments.end()) {
comments[location] = comment + " --- " + comments[location];
comments[location] = force ? comment + " :: " + comments[location] : comments[location] + " :: " + comment;
} else {
comments[location] = comment;
}

View file

@ -52,7 +52,7 @@ public:
void addMapping(uint32_t location, const std::string &name, bool force = false);
void addComment(uint32_t location, const std::string &comment);
void addComment(uint32_t location, const std::string &comment, bool force = false);
void setNop(uint32_t location);

View file

@ -36,6 +36,23 @@
#include "OutputContext.h"
#include "instructions/End.h"
std::string ltrim(const std::string &s)
{
size_t start = s.find_first_not_of(" \n\r\t\f\v");
return (start == std::string::npos) ? "" : s.substr(start);
}
std::string rtrim(const std::string &s)
{
size_t end = s.find_last_not_of(" \n\r\t\f\v");
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
std::string trim(const std::string &s) {
return rtrim(ltrim(s));
}
void decodeImage(const std::string &fileName) {
std::ifstream image(fileName);
@ -377,48 +394,83 @@ void decodeImage(const std::string &fileName) {
decodedInstruction = Instruction::Instruction::decodeInstructionFromBytes(i.second->getEndAddress(), imageObject.getBaseImage());
if(decodedInstruction != nullptr && decodedInstruction->getCommand() == Instruction::Instruction::CommandOp::END){
endPointer = reinterpret_cast<const Instruction::End*>(decodedInstruction.get());
if(endPointer->reserved == 0 || endPointer->reserved == 0xFFFFFF){
if(endPointer->reserved != endPointer->getAddress()){
endPointer = nullptr;
}
}
}
if(endPointer != nullptr){ //Found backwards pointer
decodedInstruction2 = Instruction::Instruction::decodeInstructionFromBytes(endPointer->getEndAddress(), imageObject.getBaseImage());
if(decodedInstruction2 != nullptr && decodedInstruction2->getCommand() == Instruction::Instruction::CommandOp::LOAD){
auto loadPointer = reinterpret_cast<const Instruction::Load&>(*decodedInstruction2);
if(!loadPointer.increment && loadPointer.addressOffset == 0 && loadPointer.address == endPointer->getAddress()){ //found Function name
uint32_t functionStart = endPointer->reserved;
uint32_t functionEnd = loadPointer.address - 4;
std::string functionName;
bool hasMore = true;
ctx.setNop(endPointer->getAddress());
uint32_t functionEnd = endPointer->reserved - 4;
uint32_t nextAddress = endPointer->getEndAddress();
do{
decodedInstruction2 = Instruction::Instruction::decodeInstructionFromBytes(nextAddress, imageObject.getBaseImage());
if(decodedInstruction2 != nullptr && decodedInstruction2->getCommand() == Instruction::Instruction::CommandOp::LOAD){
auto loadPointer = reinterpret_cast<const Instruction::Load&>(*decodedInstruction2);
ctx.setNop(loadPointer.getAddress());
uint32_t targetAddress = loadPointer.address;
std::string stringData;
for(auto& d : loadPointer.data){
uint8_t c = ((d >> 24) & 0xFF);
if(c == 0){
break;
}
functionName.push_back(c);
stringData.push_back(c);
c = ((d >> 16) & 0xFF);
if(c == 0){
break;
}
functionName.push_back(c);
stringData.push_back(c);
c = ((d >> 8) & 0xFF);
if(c == 0){
break;
}
functionName.push_back(c);
stringData.push_back(c);
c = (d & 0xFF);
if(c == 0){
break;
}
functionName.push_back(c);
stringData.push_back(c);
}
knownNames[functionStart] = functionName;
ctx.setNop(loadPointer.getAddress());
ctx.setNop(endPointer->getAddress());
switch (loadPointer.addressOffset) {
case 0: //FunctionName
knownNames[targetAddress] = stringData;
break;
case 1: //Comment
std::string s = stringData;
size_t pos = 0;
std::string token;
while ((pos = s.find('\n')) != std::string::npos) {
token = s.substr(0, pos);
auto trimmed = trim(token);
if(!trimmed.empty()){
ctx.addComment(targetAddress, trimmed);
}
s.erase(0, pos + 1);
}
auto trimmed = trim(s);
if(!trimmed.empty()){
ctx.addComment(targetAddress, trimmed);
}
break;
}
nextAddress = loadPointer.getEndAddress();
hasMore = loadPointer.increment == 1;
}else{
break;
}
}
} while (hasMore);
}
}
}