import: overlay helper tool, for generating linker script symbols

This commit is contained in:
Jason Andersen 2024-02-03 14:07:46 -05:00
parent 0279da40c1
commit e0056e1f59
9 changed files with 721 additions and 0 deletions

BIN
bin/overlayhelper.exe Normal file

Binary file not shown.

View file

@ -0,0 +1,48 @@
/*
bctypes.h
Because I need Standard Types
*/
#ifndef _bctypes_h
#define _bctypes_h
typedef signed char i8;
typedef unsigned char u8;
typedef signed short i16;
typedef unsigned short u16;
typedef signed long i32;
typedef unsigned long u32;
typedef signed long long i64;
typedef unsigned long long u64;
// If we're using C, I still like having a bool around
#ifndef __cplusplus
typedef i32 bool;
#define false (0)
#define true (!false)
#define nullptr 0
#endif
typedef float f32;
typedef float r32;
typedef double f64;
typedef double r64;
#define null (0)
// Odd Types
typedef union {
// u128 ul128;
u64 ul64[2];
u32 ui32[4];
} QWdata;
#endif // _bctypes_h
// EOF - bctypes.h

View file

@ -0,0 +1,208 @@
//
// cfile.cpp
//
#include "cfile.h"
#include <stdio.h>
//------------------------------------------------------------------------------
// Static Helpers
static bool contains(char x, const char* pSeparators)
{
while (*pSeparators)
{
if (x == *pSeparators)
{
return true;
}
pSeparators++;
}
return false;
}
static std::vector<std::string> split(const std::string& s, const char* separators)
{
std::vector<std::string> output;
std::string::size_type prev_pos = 0, pos = 0;
for (int index = 0; index < s.length(); ++index)
{
pos = index;
// if the index is a separator
if (contains(s[index], separators))
{
// if we've skipped a token, collect it
if (prev_pos != index)
{
output.push_back(s.substr(prev_pos, index-prev_pos));
// skip white space here
while (index < s.length())
{
if (contains(s[index], separators))
{
++index;
}
else
{
prev_pos = index;
pos = index;
break;
}
}
}
else
{
prev_pos++;
}
}
}
output.push_back(s.substr(prev_pos, pos-prev_pos+1)); // Last word
return output;
}
// make this string lowercase
static void tolower(std::string& s)
{
for (int index = 0; index < s.length(); ++index)
{
s[index] = tolower( s[index] );
}
}
static std::string next_token(std::vector<std::string>& tokens, MemoryStream& memStream)
{
std::string result = "";
if (tokens.size() >= 1)
{
result = tokens[0];
tokens.erase(tokens.cbegin());
}
else
{
while (memStream.NumBytesAvailable())
{
std::string lineData = memStream.ReadLine();
tokens = split(lineData, "[](), \t");
if (tokens.size() >= 1)
{
return next_token(tokens, memStream);
}
}
}
return result;
}
//------------------------------------------------------------------------------
CFile::CFile( std::string filepath )
: m_filepath( filepath )
{
FILE* pFile = nullptr;
errno_t err = fopen_s(&pFile, filepath.c_str(), "rb");
if (0==err)
{
fseek(pFile, 0, SEEK_END);
size_t length = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
//printf("\n// CFile %s - %lld bytes\n", filepath.c_str(), length);
//printf("// Reading %s\n", filepath.c_str());
//printf("//\n");
//printf("// This file generated by overlayhelper.exe\n");
//printf("//\n");
//printf("//\n");
u8* pData = new u8[ length ];
fread(pData, sizeof(u8), length / sizeof(u8), pFile);
// Now we have a buffer, with the whole C File
// Looking to parse 2 types of things
// CODE_BLOCK macros
// DATA_BLOCK macros
// INCBIN macros
{
MemoryStream memStream(pData, length);
while (memStream.NumBytesAvailable())
{
std::string lineData = memStream.ReadLine();
// symbols are case sensitive
// we care about the first 32 bit value in hex, and the last value on the line, the symbol
std::vector<std::string> tokens = split(lineData, "[](), \t");
if (tokens.size() >= 2)
{
std::string token = next_token(tokens, memStream);
if (token == "INCBIN")
{
// section
token = next_token(tokens, memStream);
token = next_token(tokens, memStream);
printf("incbin_%s_start_high = incbin_%s_start>>16;\n", token.c_str(),token.c_str());
printf("incbin_%s_start_BLOCK = incbin_%s_start/0x2000;\n", token.c_str(),token.c_str());
printf("incbin_%s_length = incbin_%s_end-incbin_%s_start;\n", token.c_str(),token.c_str(),token.c_str());
printf("incbin_%s_length_high = incbin_%s_length>>16;\n", token.c_str(),token.c_str());
}
else if ( (token == "CODE_BLOCK") ||
(token == "DATA_BLOCK") )
{
// We aren't as good as the C Preprocessor, but we will allow this
// to span 1 line, because RickJr has weird syntax
// First get the block number
token = next_token(tokens, memStream);
u32 block_num = strtoul(token.c_str(), nullptr, 0);
// Next we need the function name
do
{
token = next_token(tokens, memStream);
} while (token=="static" || token =="volatile");
// now token has return type
token = next_token(tokens, memStream);
// now token finally has our function or data name
printf("%s_BLOCK = %d;\n", token.c_str(), block_num); // just need the block number for mapper
}
}
}
}
delete[] pData;
fclose(pFile);
//printf("\n// Read Completed\n");
}
else
{
printf("// CFile could not open %s\n", filepath.c_str());
printf("//\n");
}
}
//------------------------------------------------------------------------------
CFile::~CFile()
{
}
//------------------------------------------------------------------------------

View file

@ -0,0 +1,27 @@
//
// CFile Class
//
#ifndef CFILE_H_
#define CFILE_H_
#include <string>
#include <vector>
#include "bctypes.h"
#include "memstream.h"
//------------------------------------------------------------------------------
class CFile
{
public:
CFile( std::string filepath );
~CFile();
private:
std::string m_filepath;
};
#endif // CFILE_H_

View file

@ -0,0 +1,164 @@
//
// Jason's Memory Stream Helper thing
// Serialize a byte stream into real types
//
// Currently only support Little Endian
//
#ifndef MEMSTREAM_H_
#define MEMSTREAM_H_
#include <string>
// Prototypes
void *memcpy(void *dest, const void *src, size_t n);
class MemoryStream
{
public:
MemoryStream(unsigned char* pStreamStart, size_t streamSize)
: m_pStreamStart( pStreamStart )
, m_streamSize(streamSize)
, m_pStreamCurrent( pStreamStart )
{}
~MemoryStream()
{
m_pStreamStart = nullptr;
m_streamSize = 0;
m_pStreamCurrent = nullptr;
}
// Really Dumb Reader Template
template <class T>
T Read()
{
T result;
memcpy(&result, m_pStreamCurrent, sizeof(T));
m_pStreamCurrent += sizeof(T);
return result;
}
template <class T>
void Read(T& result)
{
memcpy(&result, m_pStreamCurrent, sizeof(result));
m_pStreamCurrent += sizeof(result);
}
void ReadBytes(u8* pDest, size_t numBytes)
{
memcpy(pDest, m_pStreamCurrent, numBytes);
m_pStreamCurrent += numBytes;
}
std::string ReadPString()
{
std::string result;
u8 length = *m_pStreamCurrent++;
result.insert(0, (char*)m_pStreamCurrent, (size_t)length);
m_pStreamCurrent += length;
return result;
}
std::string ReadLine()
{
unsigned char* pStreamEnd = m_pStreamStart + m_streamSize;
std::string result;
u8 *pEndOfLine = m_pStreamCurrent;
while (pEndOfLine < pStreamEnd)
{
if ((0x0a != *pEndOfLine) && (0x0d != *pEndOfLine))
{
pEndOfLine++;
}
else
{
break;
}
}
size_t length = pEndOfLine - m_pStreamCurrent;
if (length)
{
// Capture the line
result.insert(0, (char*)m_pStreamCurrent, (size_t)length);
m_pStreamCurrent += length;
}
// Figure out if that loop broke out because we hit the end of the file
while (pEndOfLine < pStreamEnd)
{
if ((0x0a == *pEndOfLine) || (0x0d == *pEndOfLine))
{
pEndOfLine++;
}
else
{
break;
}
}
m_pStreamCurrent = pEndOfLine;
if (m_pStreamCurrent > pStreamEnd)
{
m_pStreamCurrent = pStreamEnd;
}
return result;
}
size_t SeekCurrent(int delta)
{
m_pStreamCurrent += delta;
return m_pStreamCurrent - m_pStreamStart;
}
size_t SeekSet(size_t offset)
{
m_pStreamCurrent = m_pStreamStart + offset;
return m_pStreamCurrent - m_pStreamStart;
}
unsigned char* GetPointer() { return m_pStreamCurrent; }
size_t NumBytesAvailable() {
unsigned char* pStreamEnd = m_pStreamStart + m_streamSize;
if (pStreamEnd - m_pStreamCurrent >= 0)
{
return pStreamEnd - m_pStreamCurrent;
}
else
{
return 0;
}
}
private:
unsigned char* m_pStreamStart;
size_t m_streamSize;
unsigned char* m_pStreamCurrent;
};
#endif // MEMSTREAM_H_

View file

@ -0,0 +1,70 @@
// overlayhelper
//
// - generate linker script crap, so that we can resolve addresses larger
// than 32-bit with mos llvm (hacky work around for 16 bit address linker)
#include <stdio.h>
#include <stdlib.h>
#include "cfile.h"
//------------------------------------------------------------------------------
void helpText()
{
printf("overlayhelper - v1.0\n");
printf("--------------------\n");
printf("Convert macros found in C code, into mos-llvm .ld linker format\n");
printf("\noverlayhelper [options] <file.c>\n");
printf(" -v verbose\n\n");
exit(-1);
}
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
char* pInfilePath = nullptr;
bool bVerbose = false;
for (int idx = 1; idx < argc; ++idx )
{
char* arg = argv[ idx ];
if ('-' == arg[0])
{
// Parse as an option
if ('v'==arg[1])
{
bVerbose = true;
}
}
else if (nullptr == pInfilePath)
{
// Assume the first non-option is an input file path
pInfilePath = argv[ idx ];
}
else
{
// Oh Crap, we have a non-option, but we don't know what to do with
// it
printf("ERROR: Invalid option, Arg %d = %s\n\n", idx, argv[ idx ]);
helpText();
}
}
if (pInfilePath)
{
// Load the .c File
CFile *pCFile = new CFile(std::string(pInfilePath));
delete pCFile;
}
else
{
helpText();
}
return 0;
}

View file

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "overlayhelper", "overlayhelper.vcxproj", "{77D5186F-C388-4740-8BA7-23997A659FB8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{77D5186F-C388-4740-8BA7-23997A659FB8}.Debug|x64.ActiveCfg = Debug|x64
{77D5186F-C388-4740-8BA7-23997A659FB8}.Debug|x64.Build.0 = Debug|x64
{77D5186F-C388-4740-8BA7-23997A659FB8}.Debug|x86.ActiveCfg = Debug|Win32
{77D5186F-C388-4740-8BA7-23997A659FB8}.Debug|x86.Build.0 = Debug|Win32
{77D5186F-C388-4740-8BA7-23997A659FB8}.Release|x64.ActiveCfg = Release|x64
{77D5186F-C388-4740-8BA7-23997A659FB8}.Release|x64.Build.0 = Release|x64
{77D5186F-C388-4740-8BA7-23997A659FB8}.Release|x86.ActiveCfg = Release|Win32
{77D5186F-C388-4740-8BA7-23997A659FB8}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4AE0A6E5-5981-4BAE-96E2-DDD5656B0B2E}
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,147 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{77d5186f-c388-4740-8ba7-23997a659fb8}</ProjectGuid>
<RootNamespace>overlayhelper</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)\..\..</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)\..\..</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\source\bctypes.h" />
<ClInclude Include="..\source\cfile.h" />
<ClInclude Include="..\source\memstream.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\source\cfile.cpp" />
<ClCompile Include="..\source\overlayhelper.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\source\bctypes.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="..\source\cfile.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="..\source\memstream.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\source\cfile.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\source\overlayhelper.cpp" />
</ItemGroup>
</Project>