mm/tools/buildtools/mkldscript.c
Tharo 7743e5a2c4
Overhaul the build system (#234)
* wip

* fix

* add disassembler

* Disasm builds OK

* Variable addends

* More wip

* Rodata migration implemented

* Cleanup old tools

* Try fix submodule -> subrepo merge

* git subrepo pull --force --remote=https://github.com/zeldaret/ZAPD.git tools/ZAPD

subrepo:
  subdir:   "tools/ZAPD"
  merged:   "602e609"
upstream:
  origin:   "https://github.com/zeldaret/ZAPD.git"
  branch:   "master"
  commit:   "602e609"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

* Builds again but assets are totally broken

* git subrepo pull --force tools/asm-processor

subrepo:
  subdir:   "tools/asm-processor"
  merged:   "1ffdb08a"
upstream:
  origin:   "https://github.com/simonlindholm/asm-processor.git"
  branch:   "master"
  commit:   "1ffdb08a"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

* More cleanup, move functions.txt and variables.txt to tools/disasm and rm tables

* rm z64compress in preparation for subrepo

* git subrepo clone (merge) https://github.com/z64me/z64compress.git tools/z64compress

subrepo:
  subdir:   "tools/z64compress"
  merged:   "eb11085c"
upstream:
  origin:   "https://github.com/z64me/z64compress.git"
  branch:   "main"
  commit:   "eb11085c"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

* Fix asset extraction

* Fix diff-init make rule

* Split code bss

* Split assumed linker bug padding from assembly files

* add filelists for mm.us.rev1

* Maybe working, but I'm not sure

* add overlays to spec

* Add rodata to actos

* Everything compiles

* Make a lot of C files for code

* Add almost every file in code to spec

* whoops

* 3 code files left

* add scenes to spec

* More progress on progress.py

* Fix skelanime in spec

* audio files!

* Fix merge issues

* Fix some C files in code

* Fix remaining code files

* Use existing O1 C files in spec

* reorder boot order in spec

* update spec

* fault.c

* Convert relocs on completed actors, fixbaserom uses current rom name

* more boot files

* Add VT macros and script

* finish already existing boot files

* most of  libultra

* fix 64bits libultra files

* Use C files for libultra, wrap some functions in NON_MATCHING

* Remove duplicate of OS_CLOCK_RATE from fault.c

* C files for fbdemos

* delete dumb files

* bootstrap C files, still need to add them to the spec

* update fixbaserom

* boot OK?

* I forgot to commit the spec

* C for gamestates

* C for kaleido

* Change all includes to ""

* copy actor sizes script from oot

* I forgot to delete those files

* Basic C files for effects

* Add effects initvars names

* Remove mislabelled boot functions from header/txt

* Begin porting bootstrap_fx, some sizes

* Fix <>

* Fix enum

* Fix diff.py

* fix libultra stuff

* update regconvert

* update setup warnings

* add some missing ;

* Fix some makefile stuff and other fixes on some non_matching functions

* add executable flag in extract_baserom and fixbaserom

* fix relative path

* copy assist from oot

* fix map path

* another assist path fix

* Delete C files for handwritten files

* add code_801A51F0 to spec

* add gfxbuffers to spec

* Move rodata to top of each file when possible

* UNK_TYPEs for func_801A51F0

* Remove kaleido rodata from spec

* Update spec and undefined_syms for recent merge

* GCC warnings and fix errors in nonmatchings,

* round percentage numbers

* progress script: format changes

* progress: error on non-existing files

* fix warning in z_scene_table

* Match 2 nonmatchings in z_actor

* Warnings in lightswitch and invadepoh

* Fix warning in z_actor_dlftbls

* I though I fixed this one

* whoops

* Comment out CC_CHECK

* Removed redundant ultra64.h includes

* Update asm_processor, sorted boot_O1 into other folders, completed the fbdemo bootstrap, cleaned up undefined_syms

* Completed gamestates bootstrap

* Split kaleido_scope

* Remove section.h and segment.h, move keep object externs to a common location in variables.h

* Completed effects bootstrap

* Segmented address externs for effects, fbdemos, gamestates and kaleido

* Move actor data externs out of the if 0

* Segmented address externs for actors

* Prepare actionfunc detection

* fix script, how did it even work before

* Fix actionfunc script again, re-introduce some more intermediate prints to the disassembler

* Automated actionFunc detection in actors

* Segmented addresses from player .text

* rm old segment addrs script and fix build

* Move sizes folder to tools

* Make build.py executable

* New Jenkinsfile Prayge

* Remove numpy dependencies

* Add warnings_disasm_current.txt

* my bad

* Update spec and undefined_syms

* Add z_eff_ss_hahen to pametfrog

* git subrepo pull (merge) --force tools/z64compress

subrepo:
  subdir:   "tools/z64compress"
  merged:   "163ca2af"
upstream:
  origin:   "https://github.com/z64me/z64compress.git"
  branch:   "main"
  commit:   "163ca2af"
git-subrepo:
  version:  "0.4.3"
  origin:   "https://github.com/ingydotnet/git-subrepo"
  commit:   "2f68596"

* Make z64compress print to stdout

* sneeky commit to update warnings tooling

* test

* Another test

* Mark fixing overlay reloc generating as a TODO

* Update warnings stuff

* Communicate the return code from running z64compress back to the Makefile through the wrapper

* Run formatter, remove extra commented copy of function

* Re-fix some includes

* Convert atan to hex to conform to decided style

* Some tidying up, remove c for fp and the other two handwritten code files

* BSS in z_collision_check & z_scene_proc

* add static back in

* Fix timerintr bss, add file to spec, some cleanup

* Remove externs

* Newline

* Readd enums

* Typo

* Colours

* Comments for hitmark enum values

Co-authored-by: EllipticEllipsis <73679967+EllipticEllipsis@users.noreply.github.com>

* Improvements and suggestions

* Organize and remove unused imports and use env for python3 scripts, delete unused overlay.py

Co-authored-by: angie <angheloalf95@gmail.com>
Co-authored-by: Elliptic Ellipsis <elliptic.ellipsis@gmail.com>
Co-authored-by: engineer124 <engineer124engineer124@gmail.com>
Co-authored-by: EllipticEllipsis <73679967+EllipticEllipsis@users.noreply.github.com>
2021-08-03 23:21:31 -04:00

543 lines
18 KiB
C

#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0]))
static FILE *fout;
enum
{
STMT_address,
STMT_after,
STMT_align,
STMT_beginseg,
STMT_compress,
STMT_endseg,
STMT_entry,
STMT_flags,
STMT_include,
STMT_include_readonly,
STMT_name,
STMT_number,
STMT_romalign,
STMT_stack,
STMT_increment,
STMT_pad_text,
};
enum
{
FLAG_BOOT = (1 << 0),
FLAG_OBJECT = (1 << 1),
FLAG_RAW = (1 << 2),
FLAG_NOLOAD = (1 << 3),
};
struct Include
{
char *fpath;
int linkerPadding;
uint8_t dataReadOnly;
};
struct Segment
{
uint32_t fields;
char *name;
char *after;
uint32_t flags;
uint32_t address;
uint32_t stack;
uint32_t align;
uint32_t romalign;
uint32_t increment;
uint32_t entry;
uint32_t number;
struct Include *includes;
int includesCount;
bool compress;
};
static struct Segment *g_segments = NULL;
static int g_segmentsCount = 0;
static struct Segment *add_segment(void)
{
struct Segment *seg;
g_segmentsCount++;
g_segments = realloc(g_segments, g_segmentsCount * sizeof(*g_segments));
seg = &g_segments[g_segmentsCount - 1];
memset(seg, 0, sizeof(*seg));
seg->align = 16;
return seg;
}
static char *skip_whitespace(char *str)
{
while (isspace(*str))
str++;
return str;
}
// null terminates the current token and returns a pointer to the next token
static char *token_split(char *str)
{
while (!isspace(*str))
{
if (*str == 0)
return str; // end of string
str++;
}
*str = 0; // terminate token
str++;
return skip_whitespace(str);
}
// null terminates the current line and returns a pointer to the next line
static char *line_split(char *str)
{
while (*str != '\n')
{
if (*str == 0)
return str; // end of string
str++;
}
*str = 0; // terminate line
return str + 1;
}
static bool parse_number(const char *str, unsigned int *num)
{
char *endptr;
long int n = strtol(str, &endptr, 0);
*num = n;
return endptr > str;
}
static bool parse_flags(char *str, unsigned int *flags)
{
unsigned int f = 0;
while (str[0] != 0)
{
char *next = token_split(str);
if (strcmp(str, "BOOT") == 0)
f |= FLAG_BOOT;
else if (strcmp(str, "OBJECT") == 0)
f |= FLAG_OBJECT;
else if (strcmp(str, "RAW") == 0)
f |= FLAG_RAW;
else if (strcmp(str, "NOLOAD") == 0)
f |= FLAG_NOLOAD;
else
return false;
str = next;
}
*flags = f;
return true;
}
static bool parse_quoted_string(char *str, char **out)
{
if (*str != '"')
return false;
str++;
*out = str;
while (*str != '"')
{
if (*str == 0)
return false; // unterminated quote
str++;
}
*str = 0;
str++;
str = skip_whitespace(str);
if (*str != 0)
return false; // garbage after filename
return true;
}
static bool is_pow_of_2(unsigned int n)
{
return (n & (n - 1)) == 0;
}
static const char *const stmtNames[] =
{
[STMT_address] = "address",
[STMT_after] = "after",
[STMT_align] = "align",
[STMT_beginseg] = "beginseg",
[STMT_compress] = "compress",
[STMT_endseg] = "endseg",
[STMT_entry] = "entry",
[STMT_flags] = "flags",
[STMT_include] = "include",
[STMT_include_readonly] = "include_readonly",
[STMT_name] = "name",
[STMT_number] = "number",
[STMT_romalign] = "romalign",
[STMT_stack] = "stack",
[STMT_increment] = "increment",
[STMT_pad_text] = "pad_text",
};
static void parse_rom_spec(char *spec)
{
int lineNum = 1;
char *line = spec;
struct Segment *currSeg = NULL;
// iterate over lines
while (line[0] != 0)
{
char *nextLine = line_split(line);
if (line[0] != 0)
{
char *stmtName = skip_whitespace(line);
if (strlen(stmtName) == 0) // skip empty lines
goto no_stmt;
char *args = token_split(stmtName);
unsigned int stmt;
for (stmt = 0; stmt < ARRAY_COUNT(stmtNames); stmt++)
if (strcmp(stmtName, stmtNames[stmt]) == 0)
goto got_stmt;
util_fatal_error("line %i: unknown statement '%s'", lineNum, stmtName);
got_stmt:
if (currSeg != NULL)
{
// ensure no duplicates (except for 'include' or 'pad_text')
if (stmt != STMT_include && stmt != STMT_include_readonly && stmt != STMT_pad_text &&
(currSeg->fields & (1 << stmt)))
util_fatal_error("line %i: duplicate '%s' statement", lineNum, stmtName);
currSeg->fields |= 1 << stmt;
currSeg->compress = false;
// statements valid within a segment definition
switch (stmt)
{
case STMT_beginseg:
util_fatal_error("line %i: '%s' inside of a segment definition", lineNum, stmtName);
break;
case STMT_endseg:
// verify segment data
if (currSeg->name == NULL)
util_fatal_error("line %i: no name specified for segment", lineNum);
if (currSeg->includesCount == 0)
util_fatal_error("line %i: no includes specified for segment", lineNum);
currSeg = NULL;
break;
case STMT_name:
if (!parse_quoted_string(args, &currSeg->name))
util_fatal_error("line %i: invalid name", lineNum);
break;
case STMT_after:
if (!parse_quoted_string(args, &currSeg->after))
util_fatal_error("line %i: invalid name for 'after'", lineNum);
break;
case STMT_address:
if (!parse_number(args, &currSeg->address))
util_fatal_error("line %i: expected number after 'address'", lineNum);
break;
case STMT_number:
if (!parse_number(args, &currSeg->number))
util_fatal_error("line %i: expected number after 'number'", lineNum);
break;
case STMT_flags:
if (!parse_flags(args, &currSeg->flags))
util_fatal_error("line %i: invalid flags", lineNum);
break;
case STMT_align:
if (!parse_number(args, &currSeg->align))
util_fatal_error("line %i: expected number after 'align'", lineNum);
if (!is_pow_of_2(currSeg->align))
util_fatal_error("line %i: alignment is not a power of two", lineNum);
break;
case STMT_romalign:
if (!parse_number(args, &currSeg->romalign))
util_fatal_error("line %i: expected number after 'romalign'", lineNum);
if (!is_pow_of_2(currSeg->romalign))
util_fatal_error("line %i: alignment is not a power of two", lineNum);
break;
case STMT_include:
case STMT_include_readonly:
currSeg->includesCount++;
currSeg->includes = realloc(currSeg->includes, currSeg->includesCount * sizeof(*currSeg->includes));
if (!parse_quoted_string(args, &currSeg->includes[currSeg->includesCount - 1].fpath))
util_fatal_error("line %i: invalid filename", lineNum);
currSeg->includes[currSeg->includesCount - 1].linkerPadding = 0;
currSeg->includes[currSeg->includesCount - 1].dataReadOnly = (stmt == STMT_include_readonly);
break;
case STMT_increment:
if (!parse_number(args, &currSeg->increment))
util_fatal_error("line %i: expected number after 'increment'", lineNum);
break;
case STMT_compress:
currSeg->compress = true;
break;
case STMT_pad_text:
currSeg->includes[currSeg->includesCount - 1].linkerPadding += 0x10;
break;
default:
fprintf(stderr, "warning: '%s' is not implemented\n", stmtName);
break;
}
}
else
{
// commands valid outside a segment definition
switch (stmt)
{
case STMT_beginseg:
currSeg = add_segment();
break;
case STMT_endseg:
util_fatal_error("line %i: '%s' outside of a segment definition", lineNum, stmtName);
break;
default:
fprintf(stderr, "warning: '%s' is not implemented\n", stmtName);
break;
}
}
}
no_stmt:
line = nextLine;
lineNum++;
}
}
static void write_ld_script(void)
{
int i;
int j;
fputs("SECTIONS {\n"
" _RomSize = 0;\n"
" _RomStart = _RomSize;\n\n",
fout);
for (i = 0; i < g_segmentsCount; i++)
{
const struct Segment *seg = &g_segments[i];
// align start of ROM segment
if (seg->fields & (1 << STMT_romalign))
fprintf(fout, " _RomSize = (_RomSize + %i) & ~ %i;\n", seg->romalign - 1, seg->romalign - 1);
else if (seg->fields & (1 << STMT_increment)) // align only start of ROM segment
fprintf(fout, " _RomSize = (_RomSize + %i) & ~ %i;\n", seg->increment - 1, seg->increment - 1);
// initialized data (.text, .data, .rodata, .sdata)
// Increment the start of the section
//if (seg->fields & (1 << STMT_increment))
//fprintf(fout, " . += 0x%08X;\n", seg->increment);
fprintf(fout, " _%sSegmentRomStartTemp = _RomSize;\n"
" _%sSegmentRomStart = _%sSegmentRomStartTemp;\n"
" ..%s ", seg->name, seg->name, seg->name, seg->name);
if (seg->fields & (1 << STMT_after))
fprintf(fout, "_%sSegmentEnd ", seg->after);
else if (seg->fields & (1 << STMT_number))
fprintf(fout, "0x%02X000000 ", seg->number);
else if (seg->fields & (1 << STMT_address))
fprintf(fout, "0x%08X ", seg->address);
// (AT(_RomSize) isn't necessary, but adds useful "load address" lines to the map file)
fprintf(fout, ": AT(_RomSize)\n {\n"
" _%sSegmentStart = .;\n"
" . = ALIGN(0x10);\n"
" _%sSegmentTextStart = .;\n",
seg->name, seg->name);
if (seg->fields & (1 << STMT_align))
fprintf(fout, " . = ALIGN(0x%X);\n", seg->align);
for (j = 0; j < seg->includesCount; j++) {
fprintf(fout, " %s (.text)\n", seg->includes[j].fpath);
if (seg->includes[j].linkerPadding != 0)
fprintf(fout, " . += 0x%X;\n", seg->includes[j].linkerPadding);
}
fprintf(fout, " _%sSegmentTextEnd = .;\n", seg->name);
fprintf(fout, " _%sSegmentTextSize = ABSOLUTE( _%sSegmentTextEnd - _%sSegmentTextStart );\n", seg->name, seg->name, seg->name);
fprintf(fout, " _%sSegmentDataStart = .;\n", seg->name);
for (j = 0; j < seg->includesCount; j++) {
if (!seg->includes[j].dataReadOnly)
fprintf(fout, " %s (.data)\n", seg->includes[j].fpath);
}
/*
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.rodata)\n", seg->includes[j].fpath);
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.sdata)\n", seg->includes[j].fpath);
*/
//fprintf(fout, " . = ALIGN(0x10);\n");
fprintf(fout, " _%sSegmentDataEnd = .;\n", seg->name);
fprintf(fout, " _%sSegmentDataSize = ABSOLUTE( _%sSegmentDataEnd - _%sSegmentDataStart );\n", seg->name, seg->name, seg->name);
fprintf(fout, " _%sSegmentRoDataStart = .;\n", seg->name);
for (j = 0; j < seg->includesCount; j++) {
if (seg->includes[j].dataReadOnly)
fprintf(fout, " %s (.data)\n", seg->includes[j].fpath);
fprintf(fout, " %s (.rodata)\n", seg->includes[j].fpath);
}
//fprintf(fout, " . = ALIGN(0x10);\n");
fprintf(fout, " _%sSegmentRoDataEnd = .;\n", seg->name);
fprintf(fout, " _%sSegmentRoDataSize = ABSOLUTE( _%sSegmentRoDataEnd - _%sSegmentRoDataStart );\n", seg->name, seg->name, seg->name);
fprintf(fout, " _%sSegmentSDataStart = .;\n", seg->name);
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.sdata)\n", seg->includes[j].fpath);
fprintf(fout, " . = ALIGN(0x10);\n");
fprintf(fout, " _%sSegmentSDataEnd = .;\n", seg->name);
fprintf(fout, " _%sSegmentOvlStart = .;\n", seg->name);
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.ovl)\n", seg->includes[j].fpath);
fprintf(fout, " . = ALIGN(0x10);\n");
fprintf(fout, " _%sSegmentOvlEnd = .;\n", seg->name);
//if (seg->fields & (1 << STMT_increment))
// fprintf(fout, " . += 0x%08X;\n", seg->increment);
fputs(" }\n", fout);
//fprintf(fout, " _RomSize += ( _%sSegmentDataEnd - _%sSegmentTextStart );\n", seg->name, seg->name);
fprintf(fout, " _RomSize += ( _%sSegmentOvlEnd - _%sSegmentTextStart );\n", seg->name, seg->name);
fprintf(fout, " _%sSegmentRomEndTemp = _RomSize;\n"
"_%sSegmentRomEnd = _%sSegmentRomEndTemp;\n\n",
seg->name, seg->name, seg->name);
// algn end of ROM segment
if (seg->fields & (1 << STMT_romalign))
fprintf(fout, " _RomSize = (_RomSize + %i) & ~ %i;\n", seg->romalign - 1, seg->romalign - 1);
// uninitialized data (.sbss, .scommon, .bss, COMMON)
fprintf(fout, " ..%s.bss ADDR(..%s) + SIZEOF(..%s) (NOLOAD) :\n"
/*" ..%s.bss :\n"*/
" {\n"
" . = ALIGN(0x10);\n"
" _%sSegmentBssStart = .;\n",
seg->name, seg->name, seg->name, seg->name);
if (seg->fields & (1 << STMT_align))
fprintf(fout, " . = ALIGN(0x%X);\n", seg->align);
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.sbss)\n", seg->includes[j].fpath);
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.scommon)\n", seg->includes[j].fpath);
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (.bss)\n", seg->includes[j].fpath);
for (j = 0; j < seg->includesCount; j++)
fprintf(fout, " %s (COMMON)\n", seg->includes[j].fpath);
fprintf(fout, " . = ALIGN(0x10);\n"
" _%sSegmentBssEnd = .;\n"
" _%sSegmentEnd = .;\n"
" }\n"
" _%sSegmentBssSize = ABSOLUTE( _%sSegmentBssEnd - _%sSegmentBssStart );\n\n",
seg->name, seg->name, seg->name, seg->name, seg->name);
// Increment the end of the segment
//if (seg->fields & (1 << STMT_increment))
// fprintf(fout, " . += 0x%08X;\n", seg->increment);
//fprintf(fout, " ..%s.ovl ADDR(..%s) + SIZEOF(..%s) :\n"
// /*" ..%s.bss :\n"*/
// " {\n",
// seg->name, seg->name, seg->name);
//fprintf(fout, " _%sSegmentOvlStart = .;\n", seg->name);
//for (j = 0; j < seg->includesCount; j++)
// fprintf(fout, " %s (.ovl)\n", seg->includes[j].fpath);
////fprintf(fout, " . = ALIGN(0x10);\n");
//fprintf(fout, " _%sSegmentOvlEnd = .;\n", seg->name);
//fprintf(fout, "\n }\n");
}
fputs(" _RomEnd = _RomSize;\n}\n", fout);
}
static void usage(const char *execname)
{
fprintf(stderr, "Nintendo 64 linker script generation tool v0.02\n"
"usage: %s [-c SEGMENTS] SPEC_FILE LD_SCRIPT\n"
"SPEC_FILE file describing the organization of object files into segments\n"
"LD_SCRIPT filename of output linker script\n"
"-c SEGMENTS (optional) output compression script for compressed segments in SEGMENTS folder\n",
execname);
}
int main(int argc, char **argv)
{
const char *spec_arg = NULL;
const char *ldscript_arg = NULL;
void *spec;
size_t size;
if (argc == 3) {
spec_arg = argv[1];
ldscript_arg = argv[2];
} else {
usage(argv[0]);
return 1;
}
spec = util_read_whole_file(spec_arg, &size);
parse_rom_spec(spec);
fout = fopen(ldscript_arg, "w");
if (fout == NULL)
util_fatal_error("failed to open file '%s' for writing", ldscript_arg);
write_ld_script();
free(spec);
fclose(fout);
return 0;
}