Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/byakugan/symPort.cpp
Views: 11766
#include "byakugan.h"1#include "symport.h"23HRESULT addSymbol(ULONG64 address, char *symbolName) {4HRESULT retVal;5if (S_OK != (retVal = g_ExtSymbols->AddSyntheticSymbol(address, 1,6symbolName, DEBUG_ADDSYNTHSYM_DEFAULT, NULL))) {7dprintf("[S] Failed to add synthetic symbol: %s\n", symbolName);8return (-1);9}1011printf("[S] Successfully added symbol!\n");12return (S_OK);13}1415ULONG64 getBase(char *imageName) {16ULONG64 baseAddress;17DWORD index;1819if (S_OK != g_ExtSymbols->GetModuleByModuleName2(imageName, 0, 0, &index, &baseAddress))20return (0);21return (baseAddress);22}2324void parseMapLine(char *mapBuf, ULONG64 *symAddress, char **symbolName, BYTE *state) {25char *startAddr;26DWORD lineLen = strlen(mapBuf);2728if (*state & MAP_STATE_ENTRYPOINT)29return;30if (*state & MAP_STATE_LOCALSYM) {31// parse up local symbol section32if (strstr(mapBuf, "Program entry")) {33*state |= MAP_STATE_ENTRYPOINT;34return;35}3637// MEAT OF THE FUNCTION - Get the Addr and Name from the line38if ((startAddr = strchr(mapBuf, ':')) == NULL)39return;40if ((startAddr - mapBuf + 10) > lineLen) {41dprintf("[S] Malformed map line: %s\n", mapBuf);42return;43}44startAddr[9] = '\x00';45*symAddress = _strtoui64(startAddr+1, NULL, 16);4647startAddr += 10;48while (*startAddr == ' ' && (startAddr - mapBuf) < lineLen)49startAddr++;50if ((startAddr - mapBuf) >= lineLen) {51dprintf("[S] Malformed map line: %s\n", mapBuf);52return;53}5455*symbolName = startAddr;56startAddr = strchr(*symbolName, '\n');57if (startAddr)58*startAddr = '\x00';5960} else if (!(*state)) {61// Look for start of LOCALSYM section62if (strstr(mapBuf, "Publics by Value"))63*state |= MAP_STATE_LOCALSYM;64}65}6667HRESULT addMapFile(char *imageName, char *mapPath) {68HANDLE mapFile;69DWORD readOut = 1, i = 0, symCount = 0;70ULONG64 symAddress, imageBase;71char mapBuf[MAP_BUF_SIZE+1], *symbolName = NULL, out = ' ';72BYTE state = 0;7374if (0 == (imageBase = getBase(imageName))) {75dprintf("[S] Failed to get base address for module %s\n", imageName);76return (-1);77}78dprintf("[S] Adjusting symbols to base address of: 0x%16y\n", imageBase);7980if((mapFile = CreateFile(mapPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,81FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {82dprintf("[S] Unable to open map file: %s\n", mapPath);83return (-1);84}8586while (readOut > 0 && i < MAP_BUF_SIZE) {87ReadFile(mapFile, &out, 1, &readOut, NULL);88if (out == '\n') {89mapBuf[i] = '\x00';90i = 0;91parseMapLine(mapBuf, &symAddress, &symbolName, &state);92symAddress += imageBase;93if (symbolName != NULL) {94//dprintf("Addr: 0x%16y\tName: %s\n", symAddress, symbolName);95if (S_OK == addSymbol(symAddress, symbolName))96symCount++;97symAddress = 0; symbolName = NULL;98}99} else {100mapBuf[i++] = out;101}102}103dprintf("[S] Successfully imported %d symbols.\n", symCount);104return (S_OK);105}106107108